]>
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 | /* ==================================================================== | |
27068df7 | 6 | * Copyright (c) 1999-2003 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); | |
124 | BIO_flush(bio); | |
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); | |
141 | BIO_flush(bio); | |
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; | |
beab098d DSH |
153 | char *mime_prefix, *mime_eol; |
154 | if (flags & PKCS7_NOOLDMIMETYPE) | |
155 | mime_prefix = "application/pkcs7-"; | |
156 | else | |
157 | mime_prefix = "application/x-pkcs7-"; | |
158 | if (flags & PKCS7_CRLFEOL) | |
159 | mime_eol = "\r\n"; | |
160 | else | |
161 | mime_eol = "\n"; | |
5a9a4b29 DSH |
162 | if((flags & PKCS7_DETACHED) && data) { |
163 | /* We want multipart/signed */ | |
164 | /* Generate a random boundary */ | |
373b575f | 165 | RAND_pseudo_bytes((unsigned char *)bound, 32); |
5a9a4b29 DSH |
166 | for(i = 0; i < 32; i++) { |
167 | c = bound[i] & 0xf; | |
168 | if(c < 10) c += '0'; | |
169 | else c += 'A' - 10; | |
170 | bound[i] = c; | |
171 | } | |
172 | bound[32] = 0; | |
beab098d | 173 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
16a44ae7 | 174 | BIO_printf(bio, "Content-Type: multipart/signed;"); |
beab098d DSH |
175 | BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix); |
176 | BIO_printf(bio, " micalg=sha1; boundary=\"----%s\"%s%s", | |
177 | bound, mime_eol, mime_eol); | |
178 | BIO_printf(bio, "This is an S/MIME signed message%s%s", | |
179 | mime_eol, mime_eol); | |
5a9a4b29 | 180 | /* Now write out the first part */ |
beab098d | 181 | BIO_printf(bio, "------%s%s", bound, mime_eol); |
27068df7 | 182 | pkcs7_output_data(bio, data, p7, flags); |
beab098d | 183 | BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol); |
5a9a4b29 DSH |
184 | |
185 | /* Headers for signature */ | |
186 | ||
beab098d DSH |
187 | BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); |
188 | BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); | |
189 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s", | |
190 | mime_eol); | |
191 | BIO_printf(bio, "Content-Disposition: attachment;"); | |
192 | BIO_printf(bio, " filename=\"smime.p7s\"%s%s", | |
193 | mime_eol, mime_eol); | |
5a9a4b29 | 194 | B64_write_PKCS7(bio, p7); |
beab098d DSH |
195 | BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound, |
196 | mime_eol, mime_eol); | |
5a9a4b29 DSH |
197 | return 1; |
198 | } | |
199 | /* MIME headers */ | |
beab098d DSH |
200 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
201 | BIO_printf(bio, "Content-Disposition: attachment;"); | |
202 | BIO_printf(bio, " filename=\"smime.p7m\"%s", mime_eol); | |
203 | BIO_printf(bio, "Content-Type: %smime;", mime_prefix); | |
204 | BIO_printf(bio, " name=\"smime.p7m\"%s", mime_eol); | |
205 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", | |
206 | mime_eol, mime_eol); | |
5a9a4b29 | 207 | B64_write_PKCS7(bio, p7); |
beab098d | 208 | BIO_printf(bio, "%s", mime_eol); |
5a9a4b29 DSH |
209 | return 1; |
210 | } | |
211 | ||
27068df7 DSH |
212 | /* Handle output of PKCS#7 data */ |
213 | ||
214 | ||
215 | static int pkcs7_output_data(BIO *out, BIO *data, PKCS7 *p7, int flags) | |
216 | { | |
217 | BIO *tmpbio, *p7bio; | |
218 | ||
e9ec6396 | 219 | if (!(flags & PKCS7_STREAM)) |
27068df7 DSH |
220 | { |
221 | SMIME_crlf_copy(data, out, flags); | |
222 | return 1; | |
223 | } | |
224 | ||
225 | /* Partial sign operation */ | |
226 | ||
227 | /* Initialize sign operation */ | |
228 | p7bio = PKCS7_dataInit(p7, out); | |
229 | ||
230 | /* Copy data across, computing digests etc */ | |
231 | SMIME_crlf_copy(data, p7bio, flags); | |
232 | ||
233 | /* Must be detached */ | |
234 | PKCS7_set_detached(p7, 1); | |
235 | ||
236 | /* Finalize signatures */ | |
237 | PKCS7_dataFinal(p7, p7bio); | |
238 | ||
beedea2f | 239 | /* Now remove any digests prepended to the BIO */ |
27068df7 | 240 | |
beedea2f | 241 | while (p7bio != out) |
27068df7 DSH |
242 | { |
243 | tmpbio = BIO_pop(p7bio); | |
beedea2f DSH |
244 | BIO_free(p7bio); |
245 | p7bio = tmpbio; | |
27068df7 DSH |
246 | } |
247 | ||
248 | return 1; | |
249 | ||
250 | } | |
251 | ||
5a9a4b29 DSH |
252 | /* SMIME reader: handle multipart/signed and opaque signing. |
253 | * in multipart case the content is placed in a memory BIO | |
254 | * pointed to by "bcont". In opaque this is set to NULL | |
255 | */ | |
256 | ||
257 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont) | |
258 | { | |
259 | BIO *p7in; | |
371acb22 BL |
260 | STACK_OF(MIME_HEADER) *headers = NULL; |
261 | STACK_OF(BIO) *parts = NULL; | |
5a9a4b29 DSH |
262 | MIME_HEADER *hdr; |
263 | MIME_PARAM *prm; | |
264 | PKCS7 *p7; | |
265 | int ret; | |
266 | ||
267 | if(bcont) *bcont = NULL; | |
268 | ||
269 | if (!(headers = mime_parse_hdr(bio))) { | |
270 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR); | |
271 | return NULL; | |
272 | } | |
273 | ||
274 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | |
371acb22 | 275 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
276 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE); |
277 | return NULL; | |
278 | } | |
279 | ||
280 | /* Handle multipart/signed */ | |
281 | ||
282 | if(!strcmp(hdr->value, "multipart/signed")) { | |
283 | /* Split into two parts */ | |
284 | prm = mime_param_find(hdr, "boundary"); | |
285 | if(!prm || !prm->param_value) { | |
371acb22 | 286 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
287 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY); |
288 | return NULL; | |
289 | } | |
290 | ret = multi_split(bio, prm->param_value, &parts); | |
371acb22 BL |
291 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
292 | if(!ret || (sk_BIO_num(parts) != 2) ) { | |
5a9a4b29 | 293 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE); |
371acb22 | 294 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
295 | return NULL; |
296 | } | |
297 | ||
298 | /* Parse the signature piece */ | |
371acb22 | 299 | p7in = sk_BIO_value(parts, 1); |
5a9a4b29 DSH |
300 | |
301 | if (!(headers = mime_parse_hdr(p7in))) { | |
302 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR); | |
371acb22 | 303 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
304 | return NULL; |
305 | } | |
306 | ||
307 | /* Get content type */ | |
308 | ||
309 | if(!(hdr = mime_hdr_find(headers, "content-type")) || | |
310 | !hdr->value) { | |
371acb22 | 311 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
312 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE); |
313 | return NULL; | |
314 | } | |
315 | ||
316 | if(strcmp(hdr->value, "application/x-pkcs7-signature") && | |
317 | strcmp(hdr->value, "application/pkcs7-signature")) { | |
371acb22 | 318 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
319 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE); |
320 | ERR_add_error_data(2, "type: ", hdr->value); | |
371acb22 | 321 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
322 | return NULL; |
323 | } | |
371acb22 | 324 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
325 | /* Read in PKCS#7 */ |
326 | if(!(p7 = B64_read_PKCS7(p7in))) { | |
327 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR); | |
371acb22 | 328 | sk_BIO_pop_free(parts, BIO_vfree); |
5a9a4b29 DSH |
329 | return NULL; |
330 | } | |
331 | ||
332 | if(bcont) { | |
371acb22 | 333 | *bcont = sk_BIO_value(parts, 0); |
5a9a4b29 | 334 | BIO_free(p7in); |
371acb22 BL |
335 | sk_BIO_free(parts); |
336 | } else sk_BIO_pop_free(parts, BIO_vfree); | |
5a9a4b29 DSH |
337 | return p7; |
338 | } | |
339 | ||
340 | /* OK, if not multipart/signed try opaque signature */ | |
341 | ||
342 | if (strcmp (hdr->value, "application/x-pkcs7-mime") && | |
343 | strcmp (hdr->value, "application/pkcs7-mime")) { | |
344 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE); | |
345 | ERR_add_error_data(2, "type: ", hdr->value); | |
371acb22 | 346 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
347 | return NULL; |
348 | } | |
349 | ||
371acb22 | 350 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
351 | |
352 | if(!(p7 = B64_read_PKCS7(bio))) { | |
353 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR); | |
354 | return NULL; | |
355 | } | |
356 | return p7; | |
357 | ||
358 | } | |
359 | ||
360 | /* Copy text from one BIO to another making the output CRLF at EOL */ | |
361 | int SMIME_crlf_copy(BIO *in, BIO *out, int flags) | |
362 | { | |
363 | char eol; | |
364 | int len; | |
365 | char linebuf[MAX_SMLEN]; | |
366 | if(flags & PKCS7_BINARY) { | |
367 | while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) | |
368 | BIO_write(out, linebuf, len); | |
369 | return 1; | |
370 | } | |
27068df7 DSH |
371 | if(flags & PKCS7_TEXT) |
372 | BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); | |
5a9a4b29 | 373 | while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { |
ca82ac1f | 374 | eol = strip_eol(linebuf, &len); |
aff05428 DSH |
375 | if (len) |
376 | BIO_write(out, linebuf, len); | |
5a9a4b29 DSH |
377 | if(eol) BIO_write(out, "\r\n", 2); |
378 | } | |
379 | return 1; | |
380 | } | |
381 | ||
382 | /* Strip off headers if they are text/plain */ | |
383 | int SMIME_text(BIO *in, BIO *out) | |
384 | { | |
385 | char iobuf[4096]; | |
386 | int len; | |
371acb22 | 387 | STACK_OF(MIME_HEADER) *headers; |
5a9a4b29 | 388 | MIME_HEADER *hdr; |
371acb22 | 389 | |
5a9a4b29 DSH |
390 | if (!(headers = mime_parse_hdr(in))) { |
391 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_PARSE_ERROR); | |
392 | return 0; | |
393 | } | |
394 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | |
395 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_NO_CONTENT_TYPE); | |
371acb22 | 396 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
397 | return 0; |
398 | } | |
399 | if (strcmp (hdr->value, "text/plain")) { | |
400 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_INVALID_MIME_TYPE); | |
401 | ERR_add_error_data(2, "type: ", hdr->value); | |
371acb22 | 402 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
403 | return 0; |
404 | } | |
371acb22 | 405 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
5a9a4b29 DSH |
406 | while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) |
407 | BIO_write(out, iobuf, len); | |
408 | return 1; | |
409 | } | |
410 | ||
411 | /* Split a multipart/XXX message body into component parts: result is | |
412 | * canonical parts in a STACK of bios | |
413 | */ | |
414 | ||
371acb22 | 415 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret) |
5a9a4b29 DSH |
416 | { |
417 | char linebuf[MAX_SMLEN]; | |
418 | int len, blen; | |
aff05428 | 419 | int eol = 0, next_eol = 0; |
5a9a4b29 | 420 | BIO *bpart = NULL; |
371acb22 | 421 | STACK_OF(BIO) *parts; |
5a9a4b29 | 422 | char state, part, first; |
371acb22 | 423 | |
5a9a4b29 DSH |
424 | blen = strlen(bound); |
425 | part = 0; | |
426 | state = 0; | |
427 | first = 1; | |
62324627 | 428 | parts = sk_BIO_new_null(); |
5a9a4b29 DSH |
429 | *ret = parts; |
430 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { | |
431 | state = mime_bound_check(linebuf, len, bound, blen); | |
432 | if(state == 1) { | |
433 | first = 1; | |
434 | part++; | |
435 | } else if(state == 2) { | |
371acb22 | 436 | sk_BIO_push(parts, bpart); |
5a9a4b29 DSH |
437 | return 1; |
438 | } else if(part) { | |
aff05428 | 439 | /* Strip CR+LF from linebuf */ |
ca82ac1f | 440 | next_eol = strip_eol(linebuf, &len); |
5a9a4b29 DSH |
441 | if(first) { |
442 | first = 0; | |
371acb22 | 443 | if(bpart) sk_BIO_push(parts, bpart); |
5a9a4b29 | 444 | bpart = BIO_new(BIO_s_mem()); |
037f6e73 | 445 | BIO_set_mem_eof_return(bpart, 0); |
aff05428 DSH |
446 | } else if (eol) |
447 | BIO_write(bpart, "\r\n", 2); | |
448 | eol = next_eol; | |
449 | if (len) | |
450 | BIO_write(bpart, linebuf, len); | |
5a9a4b29 DSH |
451 | } |
452 | } | |
453 | return 0; | |
454 | } | |
455 | ||
5a9a4b29 DSH |
456 | /* This is the big one: parse MIME header lines up to message body */ |
457 | ||
458 | #define MIME_INVALID 0 | |
459 | #define MIME_START 1 | |
460 | #define MIME_TYPE 2 | |
461 | #define MIME_NAME 3 | |
462 | #define MIME_VALUE 4 | |
463 | #define MIME_QUOTE 5 | |
464 | #define MIME_COMMENT 6 | |
465 | ||
466 | ||
371acb22 | 467 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) |
5a9a4b29 DSH |
468 | { |
469 | char *p, *q, c; | |
470 | char *ntmp; | |
471 | char linebuf[MAX_SMLEN]; | |
472 | MIME_HEADER *mhdr = NULL; | |
371acb22 | 473 | STACK_OF(MIME_HEADER) *headers; |
5a9a4b29 | 474 | int len, state, save_state = 0; |
371acb22 BL |
475 | |
476 | headers = sk_MIME_HEADER_new(mime_hdr_cmp); | |
5a9a4b29 DSH |
477 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { |
478 | /* If whitespace at line start then continuation line */ | |
a8eeb155 | 479 | if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME; |
5a9a4b29 DSH |
480 | else state = MIME_START; |
481 | ntmp = NULL; | |
482 | /* Go through all characters */ | |
483 | for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { | |
484 | ||
485 | /* State machine to handle MIME headers | |
486 | * if this looks horrible that's because it *is* | |
487 | */ | |
488 | ||
489 | switch(state) { | |
490 | case MIME_START: | |
491 | if(c == ':') { | |
492 | state = MIME_TYPE; | |
493 | *p = 0; | |
494 | ntmp = strip_ends(q); | |
495 | q = p + 1; | |
496 | } | |
497 | break; | |
498 | ||
499 | case MIME_TYPE: | |
500 | if(c == ';') { | |
501 | mime_debug("Found End Value\n"); | |
502 | *p = 0; | |
503 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | |
371acb22 | 504 | sk_MIME_HEADER_push(headers, mhdr); |
5a9a4b29 DSH |
505 | ntmp = NULL; |
506 | q = p + 1; | |
507 | state = MIME_NAME; | |
508 | } else if(c == '(') { | |
509 | save_state = state; | |
510 | state = MIME_COMMENT; | |
511 | } | |
512 | break; | |
513 | ||
514 | case MIME_COMMENT: | |
515 | if(c == ')') { | |
516 | state = save_state; | |
517 | } | |
518 | break; | |
519 | ||
520 | case MIME_NAME: | |
521 | if(c == '=') { | |
522 | state = MIME_VALUE; | |
523 | *p = 0; | |
524 | ntmp = strip_ends(q); | |
525 | q = p + 1; | |
526 | } | |
527 | break ; | |
528 | ||
529 | case MIME_VALUE: | |
530 | if(c == ';') { | |
531 | state = MIME_NAME; | |
532 | *p = 0; | |
533 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | |
534 | ntmp = NULL; | |
535 | q = p + 1; | |
536 | } else if (c == '"') { | |
537 | mime_debug("Found Quote\n"); | |
538 | state = MIME_QUOTE; | |
539 | } else if(c == '(') { | |
540 | save_state = state; | |
541 | state = MIME_COMMENT; | |
542 | } | |
543 | break; | |
544 | ||
545 | case MIME_QUOTE: | |
546 | if(c == '"') { | |
547 | mime_debug("Found Match Quote\n"); | |
548 | state = MIME_VALUE; | |
549 | } | |
550 | break; | |
551 | } | |
552 | } | |
553 | ||
554 | if(state == MIME_TYPE) { | |
555 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | |
371acb22 | 556 | sk_MIME_HEADER_push(headers, mhdr); |
5a9a4b29 DSH |
557 | } else if(state == MIME_VALUE) |
558 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | |
559 | if(p == linebuf) break; /* Blank line means end of headers */ | |
560 | } | |
561 | ||
562 | return headers; | |
563 | ||
564 | } | |
565 | ||
566 | static char *strip_ends(char *name) | |
567 | { | |
568 | return strip_end(strip_start(name)); | |
569 | } | |
570 | ||
571 | /* Strip a parameter of whitespace from start of param */ | |
572 | static char *strip_start(char *name) | |
573 | { | |
574 | char *p, c; | |
575 | /* Look for first non white space or quote */ | |
576 | for(p = name; (c = *p) ;p++) { | |
577 | if(c == '"') { | |
578 | /* Next char is start of string if non null */ | |
579 | if(p[1]) return p + 1; | |
580 | /* Else null string */ | |
581 | return NULL; | |
582 | } | |
a8eeb155 | 583 | if(!isspace((unsigned char)c)) return p; |
5a9a4b29 DSH |
584 | } |
585 | return NULL; | |
586 | } | |
587 | ||
588 | /* As above but strip from end of string : maybe should handle brackets? */ | |
589 | static char *strip_end(char *name) | |
590 | { | |
591 | char *p, c; | |
592 | if(!name) return NULL; | |
593 | /* Look for first non white space or quote */ | |
594 | for(p = name + strlen(name) - 1; p >= name ;p--) { | |
595 | c = *p; | |
596 | if(c == '"') { | |
597 | if(p - 1 == name) return NULL; | |
598 | *p = 0; | |
599 | return name; | |
600 | } | |
a8eeb155 | 601 | if(isspace((unsigned char)c)) *p = 0; |
5a9a4b29 DSH |
602 | else return name; |
603 | } | |
604 | return NULL; | |
605 | } | |
606 | ||
607 | static MIME_HEADER *mime_hdr_new(char *name, char *value) | |
608 | { | |
609 | MIME_HEADER *mhdr; | |
610 | char *tmpname, *tmpval, *p; | |
611 | int c; | |
612 | if(name) { | |
613 | if(!(tmpname = BUF_strdup(name))) return NULL; | |
614 | for(p = tmpname ; *p; p++) { | |
615 | c = *p; | |
616 | if(isupper(c)) { | |
617 | c = tolower(c); | |
618 | *p = c; | |
619 | } | |
620 | } | |
621 | } else tmpname = NULL; | |
622 | if(value) { | |
623 | if(!(tmpval = BUF_strdup(value))) return NULL; | |
624 | for(p = tmpval ; *p; p++) { | |
625 | c = *p; | |
626 | if(isupper(c)) { | |
627 | c = tolower(c); | |
628 | *p = c; | |
629 | } | |
630 | } | |
631 | } else tmpval = NULL; | |
26a3a48d | 632 | mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER)); |
5a9a4b29 DSH |
633 | if(!mhdr) return NULL; |
634 | mhdr->name = tmpname; | |
635 | mhdr->value = tmpval; | |
371acb22 | 636 | if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL; |
5a9a4b29 DSH |
637 | return mhdr; |
638 | } | |
639 | ||
640 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) | |
641 | { | |
642 | char *tmpname, *tmpval, *p; | |
643 | int c; | |
644 | MIME_PARAM *mparam; | |
645 | if(name) { | |
646 | tmpname = BUF_strdup(name); | |
647 | if(!tmpname) return 0; | |
648 | for(p = tmpname ; *p; p++) { | |
649 | c = *p; | |
650 | if(isupper(c)) { | |
651 | c = tolower(c); | |
652 | *p = c; | |
653 | } | |
654 | } | |
655 | } else tmpname = NULL; | |
656 | if(value) { | |
657 | tmpval = BUF_strdup(value); | |
658 | if(!tmpval) return 0; | |
659 | } else tmpval = NULL; | |
657e60fa | 660 | /* Parameter values are case sensitive so leave as is */ |
26a3a48d | 661 | mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM)); |
5a9a4b29 DSH |
662 | if(!mparam) return 0; |
663 | mparam->param_name = tmpname; | |
664 | mparam->param_value = tmpval; | |
371acb22 | 665 | sk_MIME_PARAM_push(mhdr->params, mparam); |
5a9a4b29 DSH |
666 | return 1; |
667 | } | |
668 | ||
ccd86b68 GT |
669 | static int mime_hdr_cmp(const MIME_HEADER * const *a, |
670 | const MIME_HEADER * const *b) | |
5a9a4b29 DSH |
671 | { |
672 | return(strcmp((*a)->name, (*b)->name)); | |
673 | } | |
674 | ||
ccd86b68 GT |
675 | static int mime_param_cmp(const MIME_PARAM * const *a, |
676 | const MIME_PARAM * const *b) | |
5a9a4b29 DSH |
677 | { |
678 | return(strcmp((*a)->param_name, (*b)->param_name)); | |
679 | } | |
680 | ||
681 | /* Find a header with a given name (if possible) */ | |
682 | ||
371acb22 | 683 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name) |
5a9a4b29 DSH |
684 | { |
685 | MIME_HEADER htmp; | |
686 | int idx; | |
687 | htmp.name = name; | |
371acb22 | 688 | idx = sk_MIME_HEADER_find(hdrs, &htmp); |
5a9a4b29 | 689 | if(idx < 0) return NULL; |
371acb22 | 690 | return sk_MIME_HEADER_value(hdrs, idx); |
5a9a4b29 DSH |
691 | } |
692 | ||
693 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name) | |
694 | { | |
695 | MIME_PARAM param; | |
696 | int idx; | |
697 | param.param_name = name; | |
371acb22 | 698 | idx = sk_MIME_PARAM_find(hdr->params, ¶m); |
5a9a4b29 | 699 | if(idx < 0) return NULL; |
371acb22 | 700 | return sk_MIME_PARAM_value(hdr->params, idx); |
5a9a4b29 DSH |
701 | } |
702 | ||
703 | static void mime_hdr_free(MIME_HEADER *hdr) | |
704 | { | |
26a3a48d RL |
705 | if(hdr->name) OPENSSL_free(hdr->name); |
706 | if(hdr->value) OPENSSL_free(hdr->value); | |
371acb22 | 707 | if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); |
26a3a48d | 708 | OPENSSL_free(hdr); |
5a9a4b29 DSH |
709 | } |
710 | ||
711 | static void mime_param_free(MIME_PARAM *param) | |
712 | { | |
26a3a48d RL |
713 | if(param->param_name) OPENSSL_free(param->param_name); |
714 | if(param->param_value) OPENSSL_free(param->param_value); | |
715 | OPENSSL_free(param); | |
5a9a4b29 DSH |
716 | } |
717 | ||
718 | /* Check for a multipart boundary. Returns: | |
719 | * 0 : no boundary | |
720 | * 1 : part boundary | |
721 | * 2 : final boundary | |
722 | */ | |
723 | static int mime_bound_check(char *line, int linelen, char *bound, int blen) | |
724 | { | |
725 | if(linelen == -1) linelen = strlen(line); | |
726 | if(blen == -1) blen = strlen(bound); | |
727 | /* Quickly eliminate if line length too short */ | |
728 | if(blen + 2 > linelen) return 0; | |
729 | /* Check for part boundary */ | |
730 | if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) { | |
731 | if(!strncmp(line + blen + 2, "--", 2)) return 2; | |
732 | else return 1; | |
733 | } | |
734 | return 0; | |
735 | } | |
ca82ac1f DSH |
736 | |
737 | static int strip_eol(char *linebuf, int *plen) | |
738 | { | |
739 | int len = *plen; | |
740 | char *p, c; | |
741 | int is_eol = 0; | |
742 | p = linebuf + len - 1; | |
743 | for (p = linebuf + len - 1; len > 0; len--, p--) | |
744 | { | |
745 | c = *p; | |
746 | if (c == '\n') | |
747 | is_eol = 1; | |
748 | else if (c != '\r') | |
749 | break; | |
750 | } | |
751 | *plen = len; | |
752 | return is_eol; | |
753 | } |