• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <assert.h>
16 #include <gmssl/base64.h>
17 #include <gmssl/error.h>
18 
19 static unsigned char conv_ascii2bin(unsigned char a);
20 #define conv_bin2ascii(a)       (data_bin2ascii[(a)&0x3f])
21 
22 
23 /*-
24  * 64 char lines
25  * pad input with 0
26  * left over chars are set to =
27  * 1 byte  => xx==
28  * 2 bytes => xxx=
29  * 3 bytes => xxxx
30  */
31 #define BIN_PER_LINE    (64/4*3)
32 #define CHUNKS_PER_LINE (64/4)
33 #define CHAR_PER_LINE   (64+1)
34 
35 static const unsigned char data_bin2ascii[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
36 abcdefghijklmnopqrstuvwxyz0123456789+/";
37 
38 /*-
39  * 0xF0 is a EOLN
40  * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing).
41  * 0xF2 is EOF
42  * 0xE0 is ignore at start of line.
43  * 0xFF is error
44  */
45 
46 #define B64_EOLN                0xF0
47 #define B64_CR                  0xF1
48 #define B64_EOF                 0xF2
49 #define B64_WS                  0xE0
50 #define B64_ERROR               0xFF
51 #define B64_NOT_BASE64(a)       (((a)|0x13) == 0xF3)
52 #define B64_BASE64(a)           (!B64_NOT_BASE64(a))
53 
54 static const unsigned char data_ascii2bin[128] = {
55     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
56     0xFF, 0xE0, 0xF0, 0xFF, 0xFF, 0xF1, 0xFF, 0xFF,
57     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
58     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
59     0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
60     0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xF2, 0xFF, 0x3F,
61     0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
62     0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF,
63     0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
64     0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
65     0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
66     0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
67     0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
68     0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
69     0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
70     0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
71 };
72 
conv_ascii2bin(unsigned char a)73 static unsigned char conv_ascii2bin(unsigned char a)
74 {
75     if (a & 0x80)
76         return B64_ERROR;
77     return data_ascii2bin[a];
78 }
79 
80 
base64_ctx_num(BASE64_CTX * ctx)81 int base64_ctx_num(BASE64_CTX *ctx)
82 {
83     return ctx->num;
84 }
85 
base64_encode_init(BASE64_CTX * ctx)86 void base64_encode_init(BASE64_CTX *ctx)
87 {
88     ctx->length = 48;
89     ctx->num = 0;
90     ctx->line_num = 0;
91 }
92 
base64_encode_update(BASE64_CTX * ctx,const uint8_t * in,int inl,uint8_t * out,int * outl)93 int base64_encode_update(BASE64_CTX *ctx, const uint8_t *in, int inl, uint8_t *out, int *outl)
94 {
95     int i, j;
96     size_t total = 0;
97 
98     *outl = 0;
99     if (inl <= 0)
100         return 0;
101     assert(ctx->length <= (int)sizeof(ctx->enc_data));
102     if (ctx->length - ctx->num > inl) {
103         memcpy(&(ctx->enc_data[ctx->num]), in, inl);
104         ctx->num += inl;
105         return 1;
106     }
107     if (ctx->num != 0) {
108         i = ctx->length - ctx->num;
109         memcpy(&(ctx->enc_data[ctx->num]), in, i);
110         in += i;
111         inl -= i;
112         j = base64_encode_block(out, ctx->enc_data, ctx->length);
113         ctx->num = 0;
114         out += j;
115         *(out++) = '\n';
116         *out = '\0';
117         total = j + 1;
118     }
119     while (inl >= ctx->length && total <= INT_MAX) {
120         j = base64_encode_block(out, in, ctx->length);
121         in += ctx->length;
122         inl -= ctx->length;
123         out += j;
124         *(out++) = '\n';
125         *out = '\0';
126         total += j + 1;
127     }
128     if (total > INT_MAX) {
129         /* Too much output data! */
130         *outl = 0;
131         return 0;
132     }
133     if (inl != 0)
134         memcpy(&(ctx->enc_data[0]), in, inl);
135     ctx->num = inl;
136     *outl = total;
137 
138     return 1;
139 }
140 
base64_encode_finish(BASE64_CTX * ctx,uint8_t * out,int * outl)141 void base64_encode_finish(BASE64_CTX *ctx, uint8_t *out, int *outl)
142 {
143     unsigned int ret = 0;
144 
145     if (ctx->num != 0) {
146         ret = base64_encode_block(out, ctx->enc_data, ctx->num);
147         out[ret++] = '\n';
148         out[ret] = '\0';
149         ctx->num = 0;
150     }
151     *outl = ret;
152 }
153 
base64_encode_block(unsigned char * t,const unsigned char * f,int dlen)154 int base64_encode_block(unsigned char *t, const unsigned char *f, int dlen)
155 {
156     int i, ret = 0;
157     unsigned long l;
158 
159     for (i = dlen; i > 0; i -= 3) {
160         if (i >= 3) {
161             l = (((unsigned long)f[0]) << 16L) |
162                 (((unsigned long)f[1]) << 8L) | f[2];
163             *(t++) = conv_bin2ascii(l >> 18L);
164             *(t++) = conv_bin2ascii(l >> 12L);
165             *(t++) = conv_bin2ascii(l >> 6L);
166             *(t++) = conv_bin2ascii(l);
167         } else {
168             l = ((unsigned long)f[0]) << 16L;
169             if (i == 2)
170                 l |= ((unsigned long)f[1] << 8L);
171 
172             *(t++) = conv_bin2ascii(l >> 18L);
173             *(t++) = conv_bin2ascii(l >> 12L);
174             *(t++) = (i == 1) ? '=' : conv_bin2ascii(l >> 6L);
175             *(t++) = '=';
176         }
177         ret += 4;
178         f += 3;
179     }
180 
181     *t = '\0';
182     return (ret);
183 }
184 
base64_decode_init(BASE64_CTX * ctx)185 void base64_decode_init(BASE64_CTX *ctx)
186 {
187     /* Only ctx->num is used during decoding. */
188     ctx->num = 0;
189     ctx->length = 0;
190     ctx->line_num = 0;
191     ctx->expect_nl = 0;
192 }
193 
194 /*-
195  * -1 for error
196  *  0 for last line
197  *  1 for full line
198  *
199  * Note: even though base64_decode_update attempts to detect and report end of
200  * content, the context doesn't currently remember it and will accept more data
201  * in the next call. Therefore, the caller is responsible for checking and
202  * rejecting a 0 return value in the middle of content.
203  *
204  * Note: even though base64_decode_update has historically tried to detect end of
205  * content based on line length, this has never worked properly. Therefore,
206  * we now return 0 when one of the following is true:
207  *   - Padding or B64_EOF was detected and the last block is complete.
208  *   - Input has zero-length.
209  * -1 is returned if:
210  *   - Invalid characters are detected.
211  *   - There is extra trailing padding, or data after padding.
212  *   - B64_EOF is detected after an incomplete base64 block.
213  */
base64_decode_update(BASE64_CTX * ctx,const uint8_t * in,int inl,uint8_t * out,int * outl)214 int base64_decode_update(BASE64_CTX *ctx, const uint8_t *in, int inl, uint8_t *out, int *outl)
215 {
216     int seof = 0, eof = 0, rv = -1, ret = 0, i, v, tmp, n, decoded_len;
217     unsigned char *d;
218 
219     n = ctx->num;
220     d = ctx->enc_data;
221 
222     if (n > 0 && d[n - 1] == '=') {
223         eof++;
224         if (n > 1 && d[n - 2] == '=')
225             eof++;
226     }
227 
228      /* Legacy behaviour: an empty input chunk signals end of input. */
229     if (inl == 0) {
230         rv = 0;
231         goto end;
232     }
233 
234     for (i = 0; i < inl; i++) {
235         tmp = *(in++);
236         v = conv_ascii2bin(tmp);
237         if (v == B64_ERROR) {
238             rv = -1;
239 		error_print();
240             goto end;
241         }
242 
243         if (tmp == '=') {
244             eof++;
245         } else if (eof > 0 && B64_BASE64(v)) {
246             /* More data after padding. */
247             rv = -1;
248 		error_print();
249             goto end;
250         }
251 
252         if (eof > 2) {
253             rv = -1;
254 		error_print();
255             goto end;
256         }
257 
258         if (v == B64_EOF) {
259             seof = 1;
260             goto tail;
261         }
262 
263         /* Only save valid base64 characters. */
264         if (B64_BASE64(v)) {
265             if (n >= 64) {
266                 /*
267                  * We increment n once per loop, and empty the buffer as soon as
268                  * we reach 64 characters, so this can only happen if someone's
269                  * manually messed with the ctx. Refuse to write any more data.
270                  */
271                 rv = -1;
272 		error_print();
273                 goto end;
274             }
275             assert(n < (int)sizeof(ctx->enc_data));
276             d[n++] = tmp;
277         }
278 
279         if (n == 64) {
280             decoded_len = base64_decode_block(out, d, n);
281             n = 0;
282             if (decoded_len < 0 || eof > decoded_len) {
283                 rv = -1;
284                 goto end;
285             }
286             ret += decoded_len - eof;
287             out += decoded_len - eof;
288         }
289     }
290 
291     /*
292      * Legacy behaviour: if the current line is a full base64-block (i.e., has
293      * 0 mod 4 base64 characters), it is processed immediately. We keep this
294      * behaviour as applications may not be calling base64_decode_final properly.
295      */
296 tail:
297     if (n > 0) {
298         if ((n & 3) == 0) {
299             decoded_len = base64_decode_block(out, d, n);
300             n = 0;
301             if (decoded_len < 0 || eof > decoded_len) {
302 		error_print();
303                 rv = -1;
304                 goto end;
305             }
306             ret += (decoded_len - eof);
307         } else if (seof) {
308             /* EOF in the middle of a base64 block. */
309 		error_print();
310             rv = -1;
311             goto end;
312         }
313     }
314 
315     rv = seof || (n == 0 && eof) ? 0 : 1;
316 end:
317     /* Legacy behaviour. This should probably rather be zeroed on error. */
318     *outl = ret;
319     ctx->num = n;
320     return (rv);
321 }
322 
base64_decode_block(unsigned char * t,const unsigned char * f,int n)323 int base64_decode_block(unsigned char *t, const unsigned char *f, int n)
324 {
325     int i, ret = 0, a, b, c, d;
326     unsigned long l;
327 
328     /* trim white space from the start of the line. */
329     while ((conv_ascii2bin(*f) == B64_WS) && (n > 0)) {
330         f++;
331         n--;
332     }
333 
334     /*
335      * strip off stuff at the end of the line ascii2bin values B64_WS,
336      * B64_EOLN, B64_EOLN and B64_EOF
337      */
338     while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n - 1]))))
339         n--;
340 
341     if (n % 4 != 0)
342         return (-1);
343 
344     for (i = 0; i < n; i += 4) {
345         a = conv_ascii2bin(*(f++));
346         b = conv_ascii2bin(*(f++));
347         c = conv_ascii2bin(*(f++));
348         d = conv_ascii2bin(*(f++));
349         if ((a & 0x80) || (b & 0x80) || (c & 0x80) || (d & 0x80))
350             return (-1);
351         l = ((((unsigned long)a) << 18L) |
352              (((unsigned long)b) << 12L) |
353              (((unsigned long)c) << 6L) | (((unsigned long)d)));
354         *(t++) = (unsigned char)(l >> 16L) & 0xff;
355         *(t++) = (unsigned char)(l >> 8L) & 0xff;
356         *(t++) = (unsigned char)(l) & 0xff;
357         ret += 3;
358     }
359     return (ret);
360 }
361 
base64_decode_finish(BASE64_CTX * ctx,uint8_t * out,int * outl)362 int base64_decode_finish(BASE64_CTX *ctx, uint8_t *out, int *outl)
363 {
364     int i;
365 
366     *outl = 0;
367     if (ctx->num != 0) {
368         i = base64_decode_block(out, ctx->enc_data, ctx->num);
369         if (i < 0) {
370 		error_print();
371             return (-1);
372         }
373         ctx->num = 0;
374         *outl = i;
375         return (1);
376     } else
377         return (1);
378 }
379