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