]>
Commit | Line | Data |
---|---|---|
5a9a4b29 DSH |
1 | /* pk7_mime.c */ |
2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | |
27068df7 | 3 | * project. |
5a9a4b29 DSH |
4 | */ |
5 | /* ==================================================================== | |
05338b58 | 6 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. |
5a9a4b29 DSH |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <ctype.h> | |
61 | #include "cryptlib.h" | |
62 | #include <openssl/rand.h> | |
63 | #include <openssl/x509.h> | |
64 | ||
65 | /* MIME and related routines */ | |
66 | ||
67 | /* MIME format structures | |
68 | * Note that all are translated to lower case apart from | |
69 | * parameter values. Quotes are stripped off | |
70 | */ | |
71 | ||
5a9a4b29 DSH |
72 | typedef struct { |
73 | char *param_name; /* Param name e.g. "micalg" */ | |
74 | char *param_value; /* Param value e.g. "sha1" */ | |
75 | } MIME_PARAM; | |
76 | ||
e20d7d71 | 77 | DECLARE_STACK_OF(MIME_PARAM) |
371acb22 BL |
78 | IMPLEMENT_STACK_OF(MIME_PARAM) |
79 | ||
80 | typedef struct { | |
81 | char *name; /* Name of line e.g. "content-type" */ | |
82 | char *value; /* Value of line e.g. "text/plain" */ | |
83 | STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */ | |
84 | } MIME_HEADER; | |
85 | ||
e20d7d71 | 86 | DECLARE_STACK_OF(MIME_HEADER) |
371acb22 | 87 | IMPLEMENT_STACK_OF(MIME_HEADER) |
5a9a4b29 | 88 | |
27068df7 | 89 | static int pkcs7_output_data(BIO *bio, BIO *data, PKCS7 *p7, int flags); |
5a9a4b29 DSH |
90 | static int B64_write_PKCS7(BIO *bio, PKCS7 *p7); |
91 | static PKCS7 *B64_read_PKCS7(BIO *bio); | |
92 | static char * strip_ends(char *name); | |
93 | static char * strip_start(char *name); | |
94 | static char * strip_end(char *name); | |
95 | static MIME_HEADER *mime_hdr_new(char *name, char *value); | |
96 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value); | |
371acb22 | 97 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio); |
ccd86b68 GT |
98 | static int mime_hdr_cmp(const MIME_HEADER * const *a, |
99 | const MIME_HEADER * const *b); | |
100 | static int mime_param_cmp(const MIME_PARAM * const *a, | |
101 | const MIME_PARAM * const *b); | |
5a9a4b29 DSH |
102 | static void mime_param_free(MIME_PARAM *param); |
103 | static int mime_bound_check(char *line, int linelen, char *bound, int blen); | |
371acb22 | 104 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret); |
ca82ac1f | 105 | static int strip_eol(char *linebuf, int *plen); |
371acb22 | 106 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name); |
5a9a4b29 DSH |
107 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name); |
108 | static void mime_hdr_free(MIME_HEADER *hdr); | |
109 | ||
110 | #define MAX_SMLEN 1024 | |
111 | #define mime_debug(x) /* x */ | |
112 | ||
5a9a4b29 DSH |
113 | /* Base 64 read and write of PKCS#7 structure */ |
114 | ||
115 | static int B64_write_PKCS7(BIO *bio, PKCS7 *p7) | |
116 | { | |
117 | BIO *b64; | |
118 | if(!(b64 = BIO_new(BIO_f_base64()))) { | |
119 | PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE); | |
120 | return 0; | |
121 | } | |
122 | bio = BIO_push(b64, bio); | |
123 | i2d_PKCS7_bio(bio, p7); | |
ddb0cb5b | 124 | (void)BIO_flush(bio); |
5a9a4b29 DSH |
125 | bio = BIO_pop(bio); |
126 | BIO_free(b64); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | static PKCS7 *B64_read_PKCS7(BIO *bio) | |
131 | { | |
132 | BIO *b64; | |
133 | PKCS7 *p7; | |
134 | if(!(b64 = BIO_new(BIO_f_base64()))) { | |
135 | PKCS7err(PKCS7_F_B64_READ_PKCS7,ERR_R_MALLOC_FAILURE); | |
136 | return 0; | |
137 | } | |
138 | bio = BIO_push(b64, bio); | |
139 | if(!(p7 = d2i_PKCS7_bio(bio, NULL))) | |
140 | PKCS7err(PKCS7_F_B64_READ_PKCS7,PKCS7_R_DECODE_ERROR); | |
ddb0cb5b | 141 | (void)BIO_flush(bio); |
5a9a4b29 DSH |
142 | bio = BIO_pop(bio); |
143 | BIO_free(b64); | |
144 | return p7; | |
145 | } | |
146 | ||
147 | /* SMIME sender */ | |
148 | ||
149 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags) | |
150 | { | |
5a9a4b29 DSH |
151 | char bound[33], c; |
152 | int i; | |
05338b58 | 153 | char *mime_prefix, *mime_eol, *msg_type=NULL; |
beab098d DSH |
154 | if (flags & PKCS7_NOOLDMIMETYPE) |
155 | mime_prefix = "application/pkcs7-"; | |
156 | else | |
157 | mime_prefix = "application/x-pkcs7-"; | |
05338b58 | 158 | |
beab098d DSH |
159 | if (flags & PKCS7_CRLFEOL) |
160 | mime_eol = "\r\n"; | |
161 | else | |
162 | mime_eol = "\n"; | |
5a9a4b29 DSH |
163 | if((flags & PKCS7_DETACHED) && data) { |
164 | /* We want multipart/signed */ | |
165 | /* Generate a random boundary */ | |
373b575f | 166 | RAND_pseudo_bytes((unsigned char *)bound, 32); |
5a9a4b29 DSH |
167 | for(i = 0; i < 32; i++) { |
168 | c = bound[i] & 0xf; | |
169 | if(c < 10) c += '0'; | |
170 | else c += 'A' - 10; | |
171 | bound[i] = c; | |
172 | } | |
173 | bound[32] = 0; | |
beab098d | 174 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
16a44ae7 | 175 | BIO_printf(bio, "Content-Type: multipart/signed;"); |
beab098d DSH |
176 | BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix); |
177 | BIO_printf(bio, " micalg=sha1; boundary=\"----%s\"%s%s", | |
178 | bound, mime_eol, mime_eol); | |
179 | BIO_printf(bio, "This is an S/MIME signed message%s%s", | |
180 | mime_eol, mime_eol); | |
5a9a4b29 | 181 | /* Now write out the first part */ |
beab098d | 182 | BIO_printf(bio, "------%s%s", bound, mime_eol); |
27068df7 | 183 | pkcs7_output_data(bio, data, p7, flags); |
beab098d | 184 | BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol); |
5a9a4b29 DSH |
185 | |
186 | /* Headers for signature */ | |
187 | ||
beab098d DSH |
188 | BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); |
189 | BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); | |
190 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s", | |
191 | mime_eol); | |
192 | BIO_printf(bio, "Content-Disposition: attachment;"); | |
193 | BIO_printf(bio, " filename=\"smime.p7s\"%s%s", | |
194 | mime_eol, mime_eol); | |
5a9a4b29 | 195 | B64_write_PKCS7(bio, p7); |
beab098d DSH |
196 | BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound, |
197 | mime_eol, mime_eol); | |
5a9a4b29 DSH |
198 | return 1; |
199 | } | |
05338b58 DSH |
200 | |
201 | /* Determine smime-type header */ | |
202 | ||
203 | if (PKCS7_type_is_enveloped(p7)) | |
204 | msg_type = "enveloped-data"; | |
205 | else if (PKCS7_type_is_signed(p7)) | |
206 | { | |
207 | /* If we have any signers it is signed-data othewise | |
208 | * certs-only. | |
209 | */ | |
210 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | |
211 | sinfos = PKCS7_get_signer_info(p7); | |
212 | if (sk_PKCS7_SIGNER_INFO_num(sinfos) > 0) | |
213 | msg_type = "signed-data"; | |
214 | else | |
215 | msg_type = "certs-only"; | |
216 | } | |
5a9a4b29 | 217 | /* MIME headers */ |
beab098d DSH |
218 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
219 | BIO_printf(bio, "Content-Disposition: attachment;"); | |
220 | BIO_printf(bio, " filename=\"smime.p7m\"%s", mime_eol); | |
221 | BIO_printf(bio, "Content-Type: %smime;", mime_prefix); | |
05338b58 DSH |
222 | if (msg_type) |
223 | BIO_printf(bio, " smime-type=%s;", msg_type); | |
beab098d DSH |
224 | BIO_printf(bio, " name=\"smime.p7m\"%s", mime_eol); |
225 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", | |
226 | mime_eol, mime_eol); | |
5a9a4b29 | 227 | B64_write_PKCS7(bio, p7); |
beab098d | 228 | BIO_printf(bio, "%s", mime_eol); |
5a9a4b29 DSH |
229 | return 1; |
230 | } | |
231 | ||
27068df7 DSH |
232 | /* Handle output of PKCS#7 data */ |
233 | ||
234 | ||
235 | static int pkcs7_output_data(BIO *out, BIO *data, PKCS7 *p7, int flags) | |
236 | { | |
237 | BIO *tmpbio, *p7bio; | |
238 | ||
e9ec6396 | 239 | if (!(flags & PKCS7_STREAM)) |
27068df7 DSH |
240 | { |
241 | SMIME_crlf_copy(data, out, flags); | |
242 | return 1; | |
243 | } | |
244 | ||
245 | /* Partial sign operation */ | |
246 | ||
247 | /* Initialize sign operation */ | |
248 | p7bio = PKCS7_dataInit(p7, out); | |
249 | ||
250 | /* Copy data across, computing digests etc */ | |
251 | SMIME_crlf_copy(data, p7bio, flags); | |
252 | ||
253 | /* Must be detached */ | |
254 | PKCS7_set_detached(p7, 1); | |
255 | ||
256 | /* Finalize signatures */ | |
257 | PKCS7_dataFinal(p7, p7bio); | |
258 | ||
beedea2f | 259 | /* Now remove any digests prepended to the BIO */ |
27068df7 | 260 | |
beedea2f | 261 | while (p7bio != out) |
27068df7 DSH |
262 | { |
263 | tmpbio = BIO_pop(p7bio); | |
beedea2f DSH |
264 | BIO_free(p7bio); |
265 | p7bio = tmpbio; | |
27068df7 DSH |
266 | } |
267 | ||
268 | return 1; | |
269 | ||
270 | } | |
271 | ||
5a9a4b29 DSH |
272 | /* SMIME reader: handle multipart/signed and opaque signing. |
273 | * in multipart case the content is placed in a memory BIO | |
274 | * pointed to by "bcont". In opaque this is set to NULL | |
275 | */ | |
276 | ||
277 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont) | |
278 | { | |
279 | BIO *p7in; | |
371acb22 BL |
280 | STACK_OF(MIME_HEADER) *headers = NULL; |
281 | STACK_OF(BIO) *parts = NULL; | |
5a9a4b29 DSH |
282 | MIME_HEADER *hdr; |
283 | MIME_PARAM *prm; | |
284 | PKCS7 *p7; | |
285 | int ret; | |
286 | ||
287 | if(bcont) *bcont = NULL; | |
288 | ||
289 | if (!(headers = mime_parse_hdr(bio))) { | |
290 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR); | |
291 | return NULL; | |
292 | } | |
293 | ||
294 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | |
371acb22 | 295 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
296 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE); |
297 | return NULL; | |
298 | } | |
299 | ||
300 | /* Handle multipart/signed */ | |
301 | ||
302 | if(!strcmp(hdr->value, "multipart/signed")) { | |
303 | /* Split into two parts */ | |
304 | prm = mime_param_find(hdr, "boundary"); | |
305 | if(!prm || !prm->param_value) { | |
371acb22 | 306 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
307 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY); |
308 | return NULL; | |
309 | } | |
310 | ret = multi_split(bio, prm->param_value, &parts); | |
371acb22 BL |
311 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
312 | if(!ret || (sk_BIO_num(parts) != 2) ) { | |
5a9a4b29 | 313 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE); |
371acb22 | 314 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
315 | return NULL; |
316 | } | |
317 | ||
318 | /* Parse the signature piece */ | |
371acb22 | 319 | p7in = sk_BIO_value(parts, 1); |
5a9a4b29 DSH |
320 | |
321 | if (!(headers = mime_parse_hdr(p7in))) { | |
322 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR); | |
371acb22 | 323 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
324 | return NULL; |
325 | } | |
326 | ||
327 | /* Get content type */ | |
328 | ||
329 | if(!(hdr = mime_hdr_find(headers, "content-type")) || | |
330 | !hdr->value) { | |
371acb22 | 331 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
332 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE); |
333 | return NULL; | |
334 | } | |
335 | ||
336 | if(strcmp(hdr->value, "application/x-pkcs7-signature") && | |
337 | strcmp(hdr->value, "application/pkcs7-signature")) { | |
371acb22 | 338 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
339 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE); |
340 | ERR_add_error_data(2, "type: ", hdr->value); | |
371acb22 | 341 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
342 | return NULL; |
343 | } | |
371acb22 | 344 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
345 | /* Read in PKCS#7 */ |
346 | if(!(p7 = B64_read_PKCS7(p7in))) { | |
347 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR); | |
371acb22 | 348 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
349 | return NULL; |
350 | } | |
351 | ||
352 | if(bcont) { | |
371acb22 | 353 | *bcont = sk_BIO_value(parts, 0); |
5a9a4b29 | 354 | BIO_free(p7in); |
371acb22 BL |
355 | sk_BIO_free(parts); |
356 | } else sk_BIO_pop_free(parts, BIO_vfree); | |
5a9a4b29 DSH |
357 | return p7; |
358 | } | |
359 | ||
360 | /* OK, if not multipart/signed try opaque signature */ | |
361 | ||
362 | if (strcmp (hdr->value, "application/x-pkcs7-mime") && | |
363 | strcmp (hdr->value, "application/pkcs7-mime")) { | |
364 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE); | |
365 | ERR_add_error_data(2, "type: ", hdr->value); | |
371acb22 | 366 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
367 | return NULL; |
368 | } | |
369 | ||
371acb22 | 370 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
371 | |
372 | if(!(p7 = B64_read_PKCS7(bio))) { | |
373 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR); | |
374 | return NULL; | |
375 | } | |
376 | return p7; | |
377 | ||
378 | } | |
379 | ||
380 | /* Copy text from one BIO to another making the output CRLF at EOL */ | |
381 | int SMIME_crlf_copy(BIO *in, BIO *out, int flags) | |
382 | { | |
383 | char eol; | |
384 | int len; | |
385 | char linebuf[MAX_SMLEN]; | |
386 | if(flags & PKCS7_BINARY) { | |
387 | while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) | |
388 | BIO_write(out, linebuf, len); | |
389 | return 1; | |
390 | } | |
27068df7 DSH |
391 | if(flags & PKCS7_TEXT) |
392 | BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); | |
5a9a4b29 | 393 | while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { |
ca82ac1f | 394 | eol = strip_eol(linebuf, &len); |
aff05428 DSH |
395 | if (len) |
396 | BIO_write(out, linebuf, len); | |
5a9a4b29 DSH |
397 | if(eol) BIO_write(out, "\r\n", 2); |
398 | } | |
399 | return 1; | |
400 | } | |
401 | ||
402 | /* Strip off headers if they are text/plain */ | |
403 | int SMIME_text(BIO *in, BIO *out) | |
404 | { | |
405 | char iobuf[4096]; | |
406 | int len; | |
371acb22 | 407 | STACK_OF(MIME_HEADER) *headers; |
5a9a4b29 | 408 | MIME_HEADER *hdr; |
371acb22 | 409 | |
5a9a4b29 DSH |
410 | if (!(headers = mime_parse_hdr(in))) { |
411 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_PARSE_ERROR); | |
412 | return 0; | |
413 | } | |
414 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | |
415 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_NO_CONTENT_TYPE); | |
371acb22 | 416 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
417 | return 0; |
418 | } | |
419 | if (strcmp (hdr->value, "text/plain")) { | |
420 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_INVALID_MIME_TYPE); | |
421 | ERR_add_error_data(2, "type: ", hdr->value); | |
371acb22 | 422 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
423 | return 0; |
424 | } | |
371acb22 | 425 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
426 | while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) |
427 | BIO_write(out, iobuf, len); | |
428 | return 1; | |
429 | } | |
430 | ||
431 | /* Split a multipart/XXX message body into component parts: result is | |
432 | * canonical parts in a STACK of bios | |
433 | */ | |
434 | ||
371acb22 | 435 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret) |
5a9a4b29 DSH |
436 | { |
437 | char linebuf[MAX_SMLEN]; | |
438 | int len, blen; | |
aff05428 | 439 | int eol = 0, next_eol = 0; |
5a9a4b29 | 440 | BIO *bpart = NULL; |
371acb22 | 441 | STACK_OF(BIO) *parts; |
5a9a4b29 | 442 | char state, part, first; |
371acb22 | 443 | |
5a9a4b29 DSH |
444 | blen = strlen(bound); |
445 | part = 0; | |
446 | state = 0; | |
447 | first = 1; | |
62324627 | 448 | parts = sk_BIO_new_null(); |
5a9a4b29 DSH |
449 | *ret = parts; |
450 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { | |
451 | state = mime_bound_check(linebuf, len, bound, blen); | |
452 | if(state == 1) { | |
453 | first = 1; | |
454 | part++; | |
455 | } else if(state == 2) { | |
371acb22 | 456 | sk_BIO_push(parts, bpart); |
5a9a4b29 DSH |
457 | return 1; |
458 | } else if(part) { | |
aff05428 | 459 | /* Strip CR+LF from linebuf */ |
ca82ac1f | 460 | next_eol = strip_eol(linebuf, &len); |
5a9a4b29 DSH |
461 | if(first) { |
462 | first = 0; | |
371acb22 | 463 | if(bpart) sk_BIO_push(parts, bpart); |
5a9a4b29 | 464 | bpart = BIO_new(BIO_s_mem()); |
037f6e73 | 465 | BIO_set_mem_eof_return(bpart, 0); |
aff05428 DSH |
466 | } else if (eol) |
467 | BIO_write(bpart, "\r\n", 2); | |
468 | eol = next_eol; | |
469 | if (len) | |
470 | BIO_write(bpart, linebuf, len); | |
5a9a4b29 DSH |
471 | } |
472 | } | |
473 | return 0; | |
474 | } | |
475 | ||
5a9a4b29 DSH |
476 | /* This is the big one: parse MIME header lines up to message body */ |
477 | ||
478 | #define MIME_INVALID 0 | |
479 | #define MIME_START 1 | |
480 | #define MIME_TYPE 2 | |
481 | #define MIME_NAME 3 | |
482 | #define MIME_VALUE 4 | |
483 | #define MIME_QUOTE 5 | |
484 | #define MIME_COMMENT 6 | |
485 | ||
486 | ||
371acb22 | 487 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) |
5a9a4b29 DSH |
488 | { |
489 | char *p, *q, c; | |
490 | char *ntmp; | |
491 | char linebuf[MAX_SMLEN]; | |
492 | MIME_HEADER *mhdr = NULL; | |
371acb22 | 493 | STACK_OF(MIME_HEADER) *headers; |
5a9a4b29 | 494 | int len, state, save_state = 0; |
371acb22 BL |
495 | |
496 | headers = sk_MIME_HEADER_new(mime_hdr_cmp); | |
5a9a4b29 DSH |
497 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { |
498 | /* If whitespace at line start then continuation line */ | |
a8eeb155 | 499 | if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME; |
5a9a4b29 DSH |
500 | else state = MIME_START; |
501 | ntmp = NULL; | |
502 | /* Go through all characters */ | |
503 | for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { | |
504 | ||
505 | /* State machine to handle MIME headers | |
506 | * if this looks horrible that's because it *is* | |
507 | */ | |
508 | ||
509 | switch(state) { | |
510 | case MIME_START: | |
511 | if(c == ':') { | |
512 | state = MIME_TYPE; | |
513 | *p = 0; | |
514 | ntmp = strip_ends(q); | |
515 | q = p + 1; | |
516 | } | |
517 | break; | |
518 | ||
519 | case MIME_TYPE: | |
520 | if(c == ';') { | |
521 | mime_debug("Found End Value\n"); | |
522 | *p = 0; | |
523 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | |
371acb22 | 524 | sk_MIME_HEADER_push(headers, mhdr); |
5a9a4b29 DSH |
525 | ntmp = NULL; |
526 | q = p + 1; | |
527 | state = MIME_NAME; | |
528 | } else if(c == '(') { | |
529 | save_state = state; | |
530 | state = MIME_COMMENT; | |
531 | } | |
532 | break; | |
533 | ||
534 | case MIME_COMMENT: | |
535 | if(c == ')') { | |
536 | state = save_state; | |
537 | } | |
538 | break; | |
539 | ||
540 | case MIME_NAME: | |
541 | if(c == '=') { | |
542 | state = MIME_VALUE; | |
543 | *p = 0; | |
544 | ntmp = strip_ends(q); | |
545 | q = p + 1; | |
546 | } | |
547 | break ; | |
548 | ||
549 | case MIME_VALUE: | |
550 | if(c == ';') { | |
551 | state = MIME_NAME; | |
552 | *p = 0; | |
553 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | |
554 | ntmp = NULL; | |
555 | q = p + 1; | |
556 | } else if (c == '"') { | |
557 | mime_debug("Found Quote\n"); | |
558 | state = MIME_QUOTE; | |
559 | } else if(c == '(') { | |
560 | save_state = state; | |
561 | state = MIME_COMMENT; | |
562 | } | |
563 | break; | |
564 | ||
565 | case MIME_QUOTE: | |
566 | if(c == '"') { | |
567 | mime_debug("Found Match Quote\n"); | |
568 | state = MIME_VALUE; | |
569 | } | |
570 | break; | |
571 | } | |
572 | } | |
573 | ||
574 | if(state == MIME_TYPE) { | |
575 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | |
371acb22 | 576 | sk_MIME_HEADER_push(headers, mhdr); |
5a9a4b29 DSH |
577 | } else if(state == MIME_VALUE) |
578 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | |
579 | if(p == linebuf) break; /* Blank line means end of headers */ | |
580 | } | |
581 | ||
582 | return headers; | |
583 | ||
584 | } | |
585 | ||
586 | static char *strip_ends(char *name) | |
587 | { | |
588 | return strip_end(strip_start(name)); | |
589 | } | |
590 | ||
591 | /* Strip a parameter of whitespace from start of param */ | |
592 | static char *strip_start(char *name) | |
593 | { | |
594 | char *p, c; | |
595 | /* Look for first non white space or quote */ | |
596 | for(p = name; (c = *p) ;p++) { | |
597 | if(c == '"') { | |
598 | /* Next char is start of string if non null */ | |
599 | if(p[1]) return p + 1; | |
600 | /* Else null string */ | |
601 | return NULL; | |
602 | } | |
a8eeb155 | 603 | if(!isspace((unsigned char)c)) return p; |
5a9a4b29 DSH |
604 | } |
605 | return NULL; | |
606 | } | |
607 | ||
608 | /* As above but strip from end of string : maybe should handle brackets? */ | |
609 | static char *strip_end(char *name) | |
610 | { | |
611 | char *p, c; | |
612 | if(!name) return NULL; | |
613 | /* Look for first non white space or quote */ | |
614 | for(p = name + strlen(name) - 1; p >= name ;p--) { | |
615 | c = *p; | |
616 | if(c == '"') { | |
617 | if(p - 1 == name) return NULL; | |
618 | *p = 0; | |
619 | return name; | |
620 | } | |
a8eeb155 | 621 | if(isspace((unsigned char)c)) *p = 0; |
5a9a4b29 DSH |
622 | else return name; |
623 | } | |
624 | return NULL; | |
625 | } | |
626 | ||
627 | static MIME_HEADER *mime_hdr_new(char *name, char *value) | |
628 | { | |
629 | MIME_HEADER *mhdr; | |
630 | char *tmpname, *tmpval, *p; | |
631 | int c; | |
632 | if(name) { | |
633 | if(!(tmpname = BUF_strdup(name))) return NULL; | |
634 | for(p = tmpname ; *p; p++) { | |
635 | c = *p; | |
636 | if(isupper(c)) { | |
637 | c = tolower(c); | |
638 | *p = c; | |
639 | } | |
640 | } | |
641 | } else tmpname = NULL; | |
642 | if(value) { | |
643 | if(!(tmpval = BUF_strdup(value))) return NULL; | |
644 | for(p = tmpval ; *p; p++) { | |
645 | c = *p; | |
646 | if(isupper(c)) { | |
647 | c = tolower(c); | |
648 | *p = c; | |
649 | } | |
650 | } | |
651 | } else tmpval = NULL; | |
26a3a48d | 652 | mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER)); |
5a9a4b29 DSH |
653 | if(!mhdr) return NULL; |
654 | mhdr->name = tmpname; | |
655 | mhdr->value = tmpval; | |
371acb22 | 656 | if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL; |
5a9a4b29 DSH |
657 | return mhdr; |
658 | } | |
659 | ||
660 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) | |
661 | { | |
662 | char *tmpname, *tmpval, *p; | |
663 | int c; | |
664 | MIME_PARAM *mparam; | |
665 | if(name) { | |
666 | tmpname = BUF_strdup(name); | |
667 | if(!tmpname) return 0; | |
668 | for(p = tmpname ; *p; p++) { | |
669 | c = *p; | |
670 | if(isupper(c)) { | |
671 | c = tolower(c); | |
672 | *p = c; | |
673 | } | |
674 | } | |
675 | } else tmpname = NULL; | |
676 | if(value) { | |
677 | tmpval = BUF_strdup(value); | |
678 | if(!tmpval) return 0; | |
679 | } else tmpval = NULL; | |
657e60fa | 680 | /* Parameter values are case sensitive so leave as is */ |
26a3a48d | 681 | mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM)); |
5a9a4b29 DSH |
682 | if(!mparam) return 0; |
683 | mparam->param_name = tmpname; | |
684 | mparam->param_value = tmpval; | |
371acb22 | 685 | sk_MIME_PARAM_push(mhdr->params, mparam); |
5a9a4b29 DSH |
686 | return 1; |
687 | } | |
688 | ||
ccd86b68 GT |
689 | static int mime_hdr_cmp(const MIME_HEADER * const *a, |
690 | const MIME_HEADER * const *b) | |
5a9a4b29 DSH |
691 | { |
692 | return(strcmp((*a)->name, (*b)->name)); | |
693 | } | |
694 | ||
ccd86b68 GT |
695 | static int mime_param_cmp(const MIME_PARAM * const *a, |
696 | const MIME_PARAM * const *b) | |
5a9a4b29 DSH |
697 | { |
698 | return(strcmp((*a)->param_name, (*b)->param_name)); | |
699 | } | |
700 | ||
701 | /* Find a header with a given name (if possible) */ | |
702 | ||
371acb22 | 703 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name) |
5a9a4b29 DSH |
704 | { |
705 | MIME_HEADER htmp; | |
706 | int idx; | |
707 | htmp.name = name; | |
371acb22 | 708 | idx = sk_MIME_HEADER_find(hdrs, &htmp); |
5a9a4b29 | 709 | if(idx < 0) return NULL; |
371acb22 | 710 | return sk_MIME_HEADER_value(hdrs, idx); |
5a9a4b29 DSH |
711 | } |
712 | ||
713 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name) | |
714 | { | |
715 | MIME_PARAM param; | |
716 | int idx; | |
717 | param.param_name = name; | |
371acb22 | 718 | idx = sk_MIME_PARAM_find(hdr->params, ¶m); |
5a9a4b29 | 719 | if(idx < 0) return NULL; |
371acb22 | 720 | return sk_MIME_PARAM_value(hdr->params, idx); |
5a9a4b29 DSH |
721 | } |
722 | ||
723 | static void mime_hdr_free(MIME_HEADER *hdr) | |
724 | { | |
26a3a48d RL |
725 | if(hdr->name) OPENSSL_free(hdr->name); |
726 | if(hdr->value) OPENSSL_free(hdr->value); | |
371acb22 | 727 | if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); |
26a3a48d | 728 | OPENSSL_free(hdr); |
5a9a4b29 DSH |
729 | } |
730 | ||
731 | static void mime_param_free(MIME_PARAM *param) | |
732 | { | |
26a3a48d RL |
733 | if(param->param_name) OPENSSL_free(param->param_name); |
734 | if(param->param_value) OPENSSL_free(param->param_value); | |
735 | OPENSSL_free(param); | |
5a9a4b29 DSH |
736 | } |
737 | ||
738 | /* Check for a multipart boundary. Returns: | |
739 | * 0 : no boundary | |
740 | * 1 : part boundary | |
741 | * 2 : final boundary | |
742 | */ | |
743 | static int mime_bound_check(char *line, int linelen, char *bound, int blen) | |
744 | { | |
745 | if(linelen == -1) linelen = strlen(line); | |
746 | if(blen == -1) blen = strlen(bound); | |
747 | /* Quickly eliminate if line length too short */ | |
748 | if(blen + 2 > linelen) return 0; | |
749 | /* Check for part boundary */ | |
750 | if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) { | |
751 | if(!strncmp(line + blen + 2, "--", 2)) return 2; | |
752 | else return 1; | |
753 | } | |
754 | return 0; | |
755 | } | |
ca82ac1f DSH |
756 | |
757 | static int strip_eol(char *linebuf, int *plen) | |
758 | { | |
759 | int len = *plen; | |
760 | char *p, c; | |
761 | int is_eol = 0; | |
762 | p = linebuf + len - 1; | |
763 | for (p = linebuf + len - 1; len > 0; len--, p--) | |
764 | { | |
765 | c = *p; | |
766 | if (c == '\n') | |
767 | is_eol = 1; | |
768 | else if (c != '\r') | |
769 | break; | |
770 | } | |
771 | *plen = len; | |
772 | return is_eol; | |
773 | } |