1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004, 2015 */
7 /* SPDX-License-Identifier: GPL-2.0-or-later */
9 /* Copyright (c) University of Cambridge 1995 - 2018 */
10 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
11 /* See the file NOTICE for conditions of use and distribution. */
15 #ifdef WITH_CONTENT_SCAN /* file-IO specific decode function */
18 /* BASE64 decoder matrix */
19 static unsigned char mime_b64[256]={
20 /* 0 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
21 /* 16 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
22 /* 32 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,
23 /* 48 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 255, 128, 128,
24 /* 64 */ 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
25 /* 80 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128,
26 /* 96 */ 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
27 /* 112 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128,
28 /* 128 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
29 /* 144 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
30 /* 160 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
31 /* 176 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
32 /* 192 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
33 /* 208 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
34 /* 224 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
35 /* 240 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
38 /* decode base64 MIME part */
40 mime_decode_base64(FILE * in, FILE * out, uschar * boundary)
42 uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
44 ssize_t len, size = 0;
49 while (Ufgets(ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
52 && Ustrncmp(ibuf, "--", 2) == 0
53 && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0
57 for (uschar * ipos = ibuf ; *ipos != '\r' && *ipos != '\n' && *ipos; ++ipos)
58 if (*ipos == '=') /* skip padding */
61 else if (mime_b64[*ipos] == 128) /* skip bad characters */
62 mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
64 /* simple state-machine */
65 else switch((bytestate++) & 3)
68 *opos = mime_b64[*ipos] << 2; break;
70 *opos++ |= mime_b64[*ipos] >> 4;
71 *opos = mime_b64[*ipos] << 4; break;
73 *opos++ |= mime_b64[*ipos] >> 2;
74 *opos = mime_b64[*ipos] << 6; break;
76 *opos++ |= mime_b64[*ipos]; break;
79 /* something to write? */
83 if (fwrite(obuf, 1, len, out) != len) return -1; /* error */
85 /* copy incomplete last byte to start of obuf, where we continue */
86 if ((bytestate & 3) != 0)
92 /* write out last byte if it was incomplete */
95 if (fwrite(obuf, 1, 1, out) != 1) return -1;
102 #endif /*WITH_CONTENT_SCAN*/
104 /*************************************************
105 *************************************************
106 *************************************************
107 *************************************************
108 *************************************************
109 *************************************************
110 *************************************************
111 *************************************************
112 *************************************************
113 *************************************************
114 *************************************************
115 *************************************************
116 *************************************************
117 *************************************************
118 *************************************************
119 *************************************************/
122 /*************************************************
123 * Decode byte-string in base 64 *
124 *************************************************/
126 /* This function decodes a string in base 64 format as defined in RFC 2045
127 (MIME) and required by the SMTP AUTH extension (RFC 2554). The decoding
128 algorithm is written out in a straightforward way. Turning it into some kind of
129 compact loop is messy and would probably run more slowly.
132 code points to the coded string, zero-terminated
133 ptr where to put the pointer to the result, which is in
134 allocated store, and zero-terminated
136 Returns: the number of bytes in the result,
137 or -1 if the input was malformed
139 Whitespace in the input is ignored.
140 A zero is added on to the end to make it easy in cases where the result is to
141 be interpreted as text. This is not included in the count. */
143 static uschar dec64table[] = {
144 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0-15 */
145 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 16-31 */
146 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, /* 32-47 */
147 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255, /* 48-63 */
148 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 64-79 */
149 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, /* 80-95 */
150 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 96-111 */
151 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255 /* 112-127*/
155 b64decode(const uschar *code, uschar **ptr)
162 int l = Ustrlen(code);
163 *ptr = result = store_get(1 + l/4 * 3 + l%4, code);
166 /* Each cycle of the loop handles a quantum of 4 input bytes. For the last
167 quantum this may decode to 1, 2, or 3 output bytes. */
169 while ((x = *code++) != 0)
171 if (isspace(x)) continue;
172 /* debug_printf("b64d: '%c'\n", x); */
174 if (x > 127 || (x = dec64table[x]) == 255) return -1;
176 while (isspace(y = *code++)) ;
177 /* debug_printf("b64d: '%c'\n", y); */
178 if (y > 127 || (y = dec64table[y]) == 255)
181 *result++ = (x << 2) | (y >> 4);
182 /* debug_printf("b64d: -> %02x\n", result[-1]); */
184 while (isspace(x = *code++)) ;
185 /* debug_printf("b64d: '%c'\n", x); */
186 if (x == '=') /* endmarker, but there should be another */
188 while (isspace(x = *code++)) ;
189 /* debug_printf("b64d: '%c'\n", x); */
190 if (x != '=') return -1;
191 while (isspace(y = *code++)) ;
192 if (y != 0) return -1;
193 /* debug_printf("b64d: DONE\n"); */
198 if (x > 127 || (x = dec64table[x]) == 255) return -1;
199 *result++ = (y << 4) | (x >> 2);
200 /* debug_printf("b64d: -> %02x\n", result[-1]); */
202 while (isspace(y = *code++)) ;
203 /* debug_printf("b64d: '%c'\n", y); */
206 while (isspace(y = *code++)) ;
207 if (y != 0) return -1;
208 /* debug_printf("b64d: DONE\n"); */
213 if (y > 127 || (y = dec64table[y]) == 255) return -1;
214 *result++ = (x << 6) | y;
215 /* debug_printf("b64d: -> %02x\n", result[-1]); */
221 return result - *ptr;
225 /*************************************************
226 * Encode byte-string in base 64 *
227 *************************************************/
229 /* This function encodes a string of bytes, containing any values whatsoever,
230 in base 64 as defined in RFC 2045 (MIME) and required by the SMTP AUTH
231 extension (RFC 2554). The encoding algorithm is written out in a
232 straightforward way. Turning it into some kind of compact loop is messy and
233 would probably run more slowly.
236 clear points to the clear text bytes
237 len the number of bytes to encode
238 proto_mem taint indicator
240 Returns: a pointer to the zero-terminated base 64 string, which
244 static uschar *enc64table =
245 US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
248 b64encode_taint(const uschar * clear, int len, const void * proto_mem)
250 uschar * code = store_get(4*((len+2)/3) + 1, proto_mem);
258 *p++ = enc64table[(x >> 2) & 63];
262 *p++ = enc64table[(x << 4) & 63];
269 *p++ = enc64table[((x << 4) | ((y >> 4) & 15)) & 63];
273 *p++ = enc64table[(y << 2) & 63];
279 *p++ = enc64table[((y << 2) | ((x >> 6) & 3)) & 63];
281 *p++ = enc64table[x & 63];
290 b64encode(const uschar * clear, int len)
292 return b64encode_taint(clear, len, clear);
296 /* End of base64.c */