]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/asn_mime.c
Integer overflow in ASN1_STRING_set.
[thirdparty/openssl.git] / crypto / asn1 / asn_mime.c
1 /*
2 * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include "crypto/ctype.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/rand.h>
14 #include <openssl/x509.h>
15 #include <openssl/asn1.h>
16 #include <openssl/asn1t.h>
17 #include "crypto/evp.h"
18 #include "internal/bio.h"
19 #include "asn1_local.h"
20
21 /*
22 * Generalised MIME like utilities for streaming ASN1. Although many have a
23 * PKCS7/CMS like flavour others are more general purpose.
24 */
25
26 /*
27 * MIME format structures Note that all are translated to lower case apart
28 * from parameter values. Quotes are stripped off
29 */
30
31 struct mime_param_st {
32 char *param_name; /* Param name e.g. "micalg" */
33 char *param_value; /* Param value e.g. "sha1" */
34 };
35
36 struct mime_header_st {
37 char *name; /* Name of line e.g. "content-type" */
38 char *value; /* Value of line e.g. "text/plain" */
39 STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
40 };
41
42 static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
43 const ASN1_ITEM *it);
44 static char *strip_ends(char *name);
45 static char *strip_start(char *name);
46 static char *strip_end(char *name);
47 static MIME_HEADER *mime_hdr_new(const char *name, const char *value);
48 static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value);
49 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
50 static int mime_hdr_cmp(const MIME_HEADER *const *a,
51 const MIME_HEADER *const *b);
52 static int mime_param_cmp(const MIME_PARAM *const *a,
53 const MIME_PARAM *const *b);
54 static void mime_param_free(MIME_PARAM *param);
55 static int mime_bound_check(char *line, int linelen, const char *bound, int blen);
56 static int multi_split(BIO *bio, const char *bound, STACK_OF(BIO) **ret);
57 static int strip_eol(char *linebuf, int *plen, int flags);
58 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name);
59 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name);
60 static void mime_hdr_free(MIME_HEADER *hdr);
61
62 #define MAX_SMLEN 1024
63 #define mime_debug(x) /* x */
64
65 /* Output an ASN1 structure in BER format streaming if necessary */
66
67 /* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */
68 int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
69 const ASN1_ITEM *it)
70 {
71 /* If streaming create stream BIO and copy all content through it */
72 if (flags & SMIME_STREAM) {
73 BIO *bio, *tbio;
74 bio = BIO_new_NDEF(out, val, it);
75 if (!bio) {
76 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM, ERR_R_MALLOC_FAILURE);
77 return 0;
78 }
79 SMIME_crlf_copy(in, bio, flags);
80 (void)BIO_flush(bio);
81 /* Free up successive BIOs until we hit the old output BIO */
82 do {
83 tbio = BIO_pop(bio);
84 BIO_free(bio);
85 bio = tbio;
86 } while (bio != out);
87 }
88 /*
89 * else just write out ASN1 structure which will have all content stored
90 * internally
91 */
92 else
93 ASN1_item_i2d_bio(it, out, val);
94 return 1;
95 }
96
97 /* Base 64 read and write of ASN1 structure */
98
99 static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
100 const ASN1_ITEM *it)
101 {
102 BIO *b64;
103 int r;
104 b64 = BIO_new(BIO_f_base64());
105 if (b64 == NULL) {
106 ASN1err(ASN1_F_B64_WRITE_ASN1, ERR_R_MALLOC_FAILURE);
107 return 0;
108 }
109 /*
110 * prepend the b64 BIO so all data is base64 encoded.
111 */
112 out = BIO_push(b64, out);
113 r = i2d_ASN1_bio_stream(out, val, in, flags, it);
114 (void)BIO_flush(out);
115 BIO_pop(out);
116 BIO_free(b64);
117 return r;
118 }
119
120 /* Streaming ASN1 PEM write */
121
122 int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
123 const char *hdr, const ASN1_ITEM *it)
124 {
125 int r;
126 BIO_printf(out, "-----BEGIN %s-----\n", hdr);
127 r = B64_write_ASN1(out, val, in, flags, it);
128 BIO_printf(out, "-----END %s-----\n", hdr);
129 return r;
130 }
131
132 static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
133 {
134 BIO *b64;
135 ASN1_VALUE *val;
136
137 if ((b64 = BIO_new(BIO_f_base64())) == NULL) {
138 ASN1err(ASN1_F_B64_READ_ASN1, ERR_R_MALLOC_FAILURE);
139 return 0;
140 }
141 bio = BIO_push(b64, bio);
142 val = ASN1_item_d2i_bio(it, bio, NULL);
143 if (!val)
144 ASN1err(ASN1_F_B64_READ_ASN1, ASN1_R_DECODE_ERROR);
145 (void)BIO_flush(bio);
146 BIO_pop(bio);
147 BIO_free(b64);
148 return val;
149 }
150
151 /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
152
153 static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
154 {
155 const EVP_MD *md;
156 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
157 have_unknown = 0;
158 write_comma = 0;
159 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) {
160 if (write_comma)
161 BIO_write(out, ",", 1);
162 write_comma = 1;
163 md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
164 md = EVP_get_digestbynid(md_nid);
165 if (md && md->md_ctrl) {
166 int rv;
167 char *micstr;
168 rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
169 if (rv > 0) {
170 BIO_puts(out, micstr);
171 OPENSSL_free(micstr);
172 continue;
173 }
174 if (rv != -2)
175 goto err;
176 }
177 switch (md_nid) {
178 case NID_sha1:
179 BIO_puts(out, "sha1");
180 break;
181
182 case NID_md5:
183 BIO_puts(out, "md5");
184 break;
185
186 case NID_sha256:
187 BIO_puts(out, "sha-256");
188 break;
189
190 case NID_sha384:
191 BIO_puts(out, "sha-384");
192 break;
193
194 case NID_sha512:
195 BIO_puts(out, "sha-512");
196 break;
197
198 case NID_id_GostR3411_94:
199 BIO_puts(out, "gostr3411-94");
200 goto err;
201
202 case NID_id_GostR3411_2012_256:
203 BIO_puts(out, "gostr3411-2012-256");
204 goto err;
205
206 case NID_id_GostR3411_2012_512:
207 BIO_puts(out, "gostr3411-2012-512");
208 goto err;
209
210 default:
211 if (have_unknown)
212 write_comma = 0;
213 else {
214 BIO_puts(out, "unknown");
215 have_unknown = 1;
216 }
217 break;
218
219 }
220 }
221
222 ret = 1;
223 err:
224
225 return ret;
226
227 }
228
229 /* SMIME sender */
230
231 int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
232 int ctype_nid, int econt_nid,
233 STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it)
234 {
235 char bound[33], c;
236 int i;
237 const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
238 const char *msg_type = NULL;
239 if (flags & SMIME_OLDMIME)
240 mime_prefix = "application/x-pkcs7-";
241 else
242 mime_prefix = "application/pkcs7-";
243
244 if (flags & SMIME_CRLFEOL)
245 mime_eol = "\r\n";
246 else
247 mime_eol = "\n";
248 if ((flags & SMIME_DETACHED) && data) {
249 /* We want multipart/signed */
250 /* Generate a random boundary */
251 if (RAND_bytes((unsigned char *)bound, 32) <= 0)
252 return 0;
253 for (i = 0; i < 32; i++) {
254 c = bound[i] & 0xf;
255 if (c < 10)
256 c += '0';
257 else
258 c += 'A' - 10;
259 bound[i] = c;
260 }
261 bound[32] = 0;
262 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
263 BIO_printf(bio, "Content-Type: multipart/signed;");
264 BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
265 BIO_puts(bio, " micalg=\"");
266 asn1_write_micalg(bio, mdalgs);
267 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
268 bound, mime_eol, mime_eol);
269 BIO_printf(bio, "This is an S/MIME signed message%s%s",
270 mime_eol, mime_eol);
271 /* Now write out the first part */
272 BIO_printf(bio, "------%s%s", bound, mime_eol);
273 if (!asn1_output_data(bio, data, val, flags, it))
274 return 0;
275 BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
276
277 /* Headers for signature */
278
279 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
280 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
281 BIO_printf(bio, "Content-Transfer-Encoding: base64%s", mime_eol);
282 BIO_printf(bio, "Content-Disposition: attachment;");
283 BIO_printf(bio, " filename=\"smime.p7s\"%s%s", mime_eol, mime_eol);
284 B64_write_ASN1(bio, val, NULL, 0, it);
285 BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound,
286 mime_eol, mime_eol);
287 return 1;
288 }
289
290 /* Determine smime-type header */
291
292 if (ctype_nid == NID_pkcs7_enveloped)
293 msg_type = "enveloped-data";
294 else if (ctype_nid == NID_pkcs7_signed) {
295 if (econt_nid == NID_id_smime_ct_receipt)
296 msg_type = "signed-receipt";
297 else if (sk_X509_ALGOR_num(mdalgs) >= 0)
298 msg_type = "signed-data";
299 else
300 msg_type = "certs-only";
301 } else if (ctype_nid == NID_id_smime_ct_compressedData) {
302 msg_type = "compressed-data";
303 cname = "smime.p7z";
304 }
305 /* MIME headers */
306 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
307 BIO_printf(bio, "Content-Disposition: attachment;");
308 BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
309 BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
310 if (msg_type)
311 BIO_printf(bio, " smime-type=%s;", msg_type);
312 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
313 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
314 mime_eol, mime_eol);
315 if (!B64_write_ASN1(bio, val, data, flags, it))
316 return 0;
317 BIO_printf(bio, "%s", mime_eol);
318 return 1;
319 }
320
321 /* Handle output of ASN1 data */
322
323 /* cannot constify val because of CMS_dataFinal() */
324 static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
325 const ASN1_ITEM *it)
326 {
327 BIO *tmpbio;
328 const ASN1_AUX *aux = it->funcs;
329 ASN1_STREAM_ARG sarg;
330 int rv = 1;
331
332 /*
333 * If data is not detached or resigning then the output BIO is already
334 * set up to finalise when it is written through.
335 */
336 if (!(flags & SMIME_DETACHED) || (flags & PKCS7_REUSE_DIGEST)) {
337 SMIME_crlf_copy(data, out, flags);
338 return 1;
339 }
340
341 if (!aux || !aux->asn1_cb) {
342 ASN1err(ASN1_F_ASN1_OUTPUT_DATA, ASN1_R_STREAMING_NOT_SUPPORTED);
343 return 0;
344 }
345
346 sarg.out = out;
347 sarg.ndef_bio = NULL;
348 sarg.boundary = NULL;
349
350 /* Let ASN1 code prepend any needed BIOs */
351
352 if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
353 return 0;
354
355 /* Copy data across, passing through filter BIOs for processing */
356 SMIME_crlf_copy(data, sarg.ndef_bio, flags);
357
358 /* Finalize structure */
359 if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
360 rv = 0;
361
362 /* Now remove any digests prepended to the BIO */
363
364 while (sarg.ndef_bio != out) {
365 tmpbio = BIO_pop(sarg.ndef_bio);
366 BIO_free(sarg.ndef_bio);
367 sarg.ndef_bio = tmpbio;
368 }
369
370 return rv;
371
372 }
373
374 /*
375 * SMIME reader: handle multipart/signed and opaque signing. in multipart
376 * case the content is placed in a memory BIO pointed to by "bcont". In
377 * opaque this is set to NULL
378 */
379
380 ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
381 {
382 BIO *asnin;
383 STACK_OF(MIME_HEADER) *headers = NULL;
384 STACK_OF(BIO) *parts = NULL;
385 MIME_HEADER *hdr;
386 MIME_PARAM *prm;
387 ASN1_VALUE *val;
388 int ret;
389
390 if (bcont)
391 *bcont = NULL;
392
393 if ((headers = mime_parse_hdr(bio)) == NULL) {
394 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_PARSE_ERROR);
395 return NULL;
396 }
397
398 if ((hdr = mime_hdr_find(headers, "content-type")) == NULL
399 || hdr->value == NULL) {
400 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
401 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
402 return NULL;
403 }
404
405 /* Handle multipart/signed */
406
407 if (strcmp(hdr->value, "multipart/signed") == 0) {
408 /* Split into two parts */
409 prm = mime_param_find(hdr, "boundary");
410 if (prm == NULL || prm->param_value == NULL) {
411 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
412 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY);
413 return NULL;
414 }
415 ret = multi_split(bio, prm->param_value, &parts);
416 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
417 if (!ret || (sk_BIO_num(parts) != 2)) {
418 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE);
419 sk_BIO_pop_free(parts, BIO_vfree);
420 return NULL;
421 }
422
423 /* Parse the signature piece */
424 asnin = sk_BIO_value(parts, 1);
425
426 if ((headers = mime_parse_hdr(asnin)) == NULL) {
427 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_SIG_PARSE_ERROR);
428 sk_BIO_pop_free(parts, BIO_vfree);
429 return NULL;
430 }
431
432 /* Get content type */
433
434 if ((hdr = mime_hdr_find(headers, "content-type")) == NULL
435 || hdr->value == NULL) {
436 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
437 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE);
438 sk_BIO_pop_free(parts, BIO_vfree);
439 return NULL;
440 }
441
442 if (strcmp(hdr->value, "application/x-pkcs7-signature") &&
443 strcmp(hdr->value, "application/pkcs7-signature")) {
444 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_SIG_INVALID_MIME_TYPE);
445 ERR_add_error_data(2, "type: ", hdr->value);
446 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
447 sk_BIO_pop_free(parts, BIO_vfree);
448 return NULL;
449 }
450 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
451 /* Read in ASN1 */
452 if ((val = b64_read_asn1(asnin, it)) == NULL) {
453 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_SIG_PARSE_ERROR);
454 sk_BIO_pop_free(parts, BIO_vfree);
455 return NULL;
456 }
457
458 if (bcont) {
459 *bcont = sk_BIO_value(parts, 0);
460 BIO_free(asnin);
461 sk_BIO_free(parts);
462 } else
463 sk_BIO_pop_free(parts, BIO_vfree);
464 return val;
465 }
466
467 /* OK, if not multipart/signed try opaque signature */
468
469 if (strcmp(hdr->value, "application/x-pkcs7-mime") &&
470 strcmp(hdr->value, "application/pkcs7-mime")) {
471 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_INVALID_MIME_TYPE);
472 ERR_add_error_data(2, "type: ", hdr->value);
473 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
474 return NULL;
475 }
476
477 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
478
479 if ((val = b64_read_asn1(bio, it)) == NULL) {
480 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
481 return NULL;
482 }
483 return val;
484
485 }
486
487 /* Copy text from one BIO to another making the output CRLF at EOL */
488 int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
489 {
490 BIO *bf;
491 char eol;
492 int len;
493 char linebuf[MAX_SMLEN];
494 /*
495 * Buffer output so we don't write one line at a time. This is useful
496 * when streaming as we don't end up with one OCTET STRING per line.
497 */
498 bf = BIO_new(BIO_f_buffer());
499 if (bf == NULL)
500 return 0;
501 out = BIO_push(bf, out);
502 if (flags & SMIME_BINARY) {
503 while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
504 BIO_write(out, linebuf, len);
505 } else {
506 int eolcnt = 0;
507 if (flags & SMIME_TEXT)
508 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
509 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
510 eol = strip_eol(linebuf, &len, flags);
511 if (len) {
512 /* Not EOF: write out all CRLF */
513 if (flags & SMIME_ASCIICRLF) {
514 int i;
515 for (i = 0; i < eolcnt; i++)
516 BIO_write(out, "\r\n", 2);
517 eolcnt = 0;
518 }
519 BIO_write(out, linebuf, len);
520 if (eol)
521 BIO_write(out, "\r\n", 2);
522 } else if (flags & SMIME_ASCIICRLF)
523 eolcnt++;
524 else if (eol)
525 BIO_write(out, "\r\n", 2);
526 }
527 }
528 (void)BIO_flush(out);
529 BIO_pop(out);
530 BIO_free(bf);
531 return 1;
532 }
533
534 /* Strip off headers if they are text/plain */
535 int SMIME_text(BIO *in, BIO *out)
536 {
537 char iobuf[4096];
538 int len;
539 STACK_OF(MIME_HEADER) *headers;
540 MIME_HEADER *hdr;
541
542 if ((headers = mime_parse_hdr(in)) == NULL) {
543 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_PARSE_ERROR);
544 return 0;
545 }
546 if ((hdr = mime_hdr_find(headers, "content-type")) == NULL
547 || hdr->value == NULL) {
548 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_NO_CONTENT_TYPE);
549 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
550 return 0;
551 }
552 if (strcmp(hdr->value, "text/plain")) {
553 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_INVALID_MIME_TYPE);
554 ERR_add_error_data(2, "type: ", hdr->value);
555 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
556 return 0;
557 }
558 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
559 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
560 BIO_write(out, iobuf, len);
561 if (len < 0)
562 return 0;
563 return 1;
564 }
565
566 /*
567 * Split a multipart/XXX message body into component parts: result is
568 * canonical parts in a STACK of bios
569 */
570
571 static int multi_split(BIO *bio, const char *bound, STACK_OF(BIO) **ret)
572 {
573 char linebuf[MAX_SMLEN];
574 int len, blen;
575 int eol = 0, next_eol = 0;
576 BIO *bpart = NULL;
577 STACK_OF(BIO) *parts;
578 char state, part, first;
579
580 blen = strlen(bound);
581 part = 0;
582 state = 0;
583 first = 1;
584 parts = sk_BIO_new_null();
585 *ret = parts;
586 if (*ret == NULL)
587 return 0;
588 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
589 state = mime_bound_check(linebuf, len, bound, blen);
590 if (state == 1) {
591 first = 1;
592 part++;
593 } else if (state == 2) {
594 if (!sk_BIO_push(parts, bpart)) {
595 BIO_free(bpart);
596 return 0;
597 }
598 return 1;
599 } else if (part) {
600 /* Strip CR+LF from linebuf */
601 next_eol = strip_eol(linebuf, &len, 0);
602 if (first) {
603 first = 0;
604 if (bpart)
605 if (!sk_BIO_push(parts, bpart)) {
606 BIO_free(bpart);
607 return 0;
608 }
609 bpart = BIO_new(BIO_s_mem());
610 if (bpart == NULL)
611 return 0;
612 BIO_set_mem_eof_return(bpart, 0);
613 } else if (eol)
614 BIO_write(bpart, "\r\n", 2);
615 eol = next_eol;
616 if (len)
617 BIO_write(bpart, linebuf, len);
618 }
619 }
620 BIO_free(bpart);
621 return 0;
622 }
623
624 /* This is the big one: parse MIME header lines up to message body */
625
626 #define MIME_INVALID 0
627 #define MIME_START 1
628 #define MIME_TYPE 2
629 #define MIME_NAME 3
630 #define MIME_VALUE 4
631 #define MIME_QUOTE 5
632 #define MIME_COMMENT 6
633
634 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
635 {
636 char *p, *q, c;
637 char *ntmp;
638 char linebuf[MAX_SMLEN];
639 MIME_HEADER *mhdr = NULL, *new_hdr = NULL;
640 STACK_OF(MIME_HEADER) *headers;
641 int len, state, save_state = 0;
642
643 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
644 if (headers == NULL)
645 return NULL;
646 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
647 /* If whitespace at line start then continuation line */
648 if (mhdr && ossl_isspace(linebuf[0]))
649 state = MIME_NAME;
650 else
651 state = MIME_START;
652 ntmp = NULL;
653 /* Go through all characters */
654 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
655 p++) {
656
657 /*
658 * State machine to handle MIME headers if this looks horrible
659 * that's because it *is*
660 */
661
662 switch (state) {
663 case MIME_START:
664 if (c == ':') {
665 state = MIME_TYPE;
666 *p = 0;
667 ntmp = strip_ends(q);
668 q = p + 1;
669 }
670 break;
671
672 case MIME_TYPE:
673 if (c == ';') {
674 mime_debug("Found End Value\n");
675 *p = 0;
676 new_hdr = mime_hdr_new(ntmp, strip_ends(q));
677 if (new_hdr == NULL)
678 goto err;
679 if (!sk_MIME_HEADER_push(headers, new_hdr))
680 goto err;
681 mhdr = new_hdr;
682 new_hdr = NULL;
683 ntmp = NULL;
684 q = p + 1;
685 state = MIME_NAME;
686 } else if (c == '(') {
687 save_state = state;
688 state = MIME_COMMENT;
689 }
690 break;
691
692 case MIME_COMMENT:
693 if (c == ')') {
694 state = save_state;
695 }
696 break;
697
698 case MIME_NAME:
699 if (c == '=') {
700 state = MIME_VALUE;
701 *p = 0;
702 ntmp = strip_ends(q);
703 q = p + 1;
704 }
705 break;
706
707 case MIME_VALUE:
708 if (c == ';') {
709 state = MIME_NAME;
710 *p = 0;
711 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
712 ntmp = NULL;
713 q = p + 1;
714 } else if (c == '"') {
715 mime_debug("Found Quote\n");
716 state = MIME_QUOTE;
717 } else if (c == '(') {
718 save_state = state;
719 state = MIME_COMMENT;
720 }
721 break;
722
723 case MIME_QUOTE:
724 if (c == '"') {
725 mime_debug("Found Match Quote\n");
726 state = MIME_VALUE;
727 }
728 break;
729 }
730 }
731
732 if (state == MIME_TYPE) {
733 new_hdr = mime_hdr_new(ntmp, strip_ends(q));
734 if (new_hdr == NULL)
735 goto err;
736 if (!sk_MIME_HEADER_push(headers, new_hdr))
737 goto err;
738 mhdr = new_hdr;
739 new_hdr = NULL;
740 } else if (state == MIME_VALUE)
741 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
742 if (p == linebuf)
743 break; /* Blank line means end of headers */
744 }
745
746 return headers;
747
748 err:
749 mime_hdr_free(new_hdr);
750 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
751 return NULL;
752 }
753
754 static char *strip_ends(char *name)
755 {
756 return strip_end(strip_start(name));
757 }
758
759 /* Strip a parameter of whitespace from start of param */
760 static char *strip_start(char *name)
761 {
762 char *p, c;
763 /* Look for first non white space or quote */
764 for (p = name; (c = *p); p++) {
765 if (c == '"') {
766 /* Next char is start of string if non null */
767 if (p[1])
768 return p + 1;
769 /* Else null string */
770 return NULL;
771 }
772 if (!ossl_isspace(c))
773 return p;
774 }
775 return NULL;
776 }
777
778 /* As above but strip from end of string : maybe should handle brackets? */
779 static char *strip_end(char *name)
780 {
781 char *p, c;
782 if (!name)
783 return NULL;
784 /* Look for first non white space or quote */
785 for (p = name + strlen(name) - 1; p >= name; p--) {
786 c = *p;
787 if (c == '"') {
788 if (p - 1 == name)
789 return NULL;
790 *p = 0;
791 return name;
792 }
793 if (ossl_isspace(c))
794 *p = 0;
795 else
796 return name;
797 }
798 return NULL;
799 }
800
801 static MIME_HEADER *mime_hdr_new(const char *name, const char *value)
802 {
803 MIME_HEADER *mhdr = NULL;
804 char *tmpname = NULL, *tmpval = NULL, *p;
805
806 if (name) {
807 if ((tmpname = OPENSSL_strdup(name)) == NULL)
808 return NULL;
809 for (p = tmpname; *p; p++)
810 *p = ossl_tolower(*p);
811 }
812 if (value) {
813 if ((tmpval = OPENSSL_strdup(value)) == NULL)
814 goto err;
815 for (p = tmpval; *p; p++)
816 *p = ossl_tolower(*p);
817 }
818 mhdr = OPENSSL_malloc(sizeof(*mhdr));
819 if (mhdr == NULL)
820 goto err;
821 mhdr->name = tmpname;
822 mhdr->value = tmpval;
823 if ((mhdr->params = sk_MIME_PARAM_new(mime_param_cmp)) == NULL)
824 goto err;
825 return mhdr;
826
827 err:
828 OPENSSL_free(tmpname);
829 OPENSSL_free(tmpval);
830 OPENSSL_free(mhdr);
831 return NULL;
832 }
833
834 static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value)
835 {
836 char *tmpname = NULL, *tmpval = NULL, *p;
837 MIME_PARAM *mparam = NULL;
838
839 if (name) {
840 tmpname = OPENSSL_strdup(name);
841 if (!tmpname)
842 goto err;
843 for (p = tmpname; *p; p++)
844 *p = ossl_tolower(*p);
845 }
846 if (value) {
847 tmpval = OPENSSL_strdup(value);
848 if (!tmpval)
849 goto err;
850 }
851 /* Parameter values are case sensitive so leave as is */
852 mparam = OPENSSL_malloc(sizeof(*mparam));
853 if (mparam == NULL)
854 goto err;
855 mparam->param_name = tmpname;
856 mparam->param_value = tmpval;
857 if (!sk_MIME_PARAM_push(mhdr->params, mparam))
858 goto err;
859 return 1;
860 err:
861 OPENSSL_free(tmpname);
862 OPENSSL_free(tmpval);
863 OPENSSL_free(mparam);
864 return 0;
865 }
866
867 static int mime_hdr_cmp(const MIME_HEADER *const *a,
868 const MIME_HEADER *const *b)
869 {
870 if (!(*a)->name || !(*b)->name)
871 return ! !(*a)->name - ! !(*b)->name;
872
873 return strcmp((*a)->name, (*b)->name);
874 }
875
876 static int mime_param_cmp(const MIME_PARAM *const *a,
877 const MIME_PARAM *const *b)
878 {
879 if (!(*a)->param_name || !(*b)->param_name)
880 return ! !(*a)->param_name - ! !(*b)->param_name;
881 return strcmp((*a)->param_name, (*b)->param_name);
882 }
883
884 /* Find a header with a given name (if possible) */
885
886 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name)
887 {
888 MIME_HEADER htmp;
889 int idx;
890
891 htmp.name = (char *)name;
892 htmp.value = NULL;
893 htmp.params = NULL;
894
895 idx = sk_MIME_HEADER_find(hdrs, &htmp);
896 return sk_MIME_HEADER_value(hdrs, idx);
897 }
898
899 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name)
900 {
901 MIME_PARAM param;
902 int idx;
903
904 param.param_name = (char *)name;
905 param.param_value = NULL;
906 idx = sk_MIME_PARAM_find(hdr->params, &param);
907 return sk_MIME_PARAM_value(hdr->params, idx);
908 }
909
910 static void mime_hdr_free(MIME_HEADER *hdr)
911 {
912 if (hdr == NULL)
913 return;
914 OPENSSL_free(hdr->name);
915 OPENSSL_free(hdr->value);
916 if (hdr->params)
917 sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
918 OPENSSL_free(hdr);
919 }
920
921 static void mime_param_free(MIME_PARAM *param)
922 {
923 OPENSSL_free(param->param_name);
924 OPENSSL_free(param->param_value);
925 OPENSSL_free(param);
926 }
927
928 /*-
929 * Check for a multipart boundary. Returns:
930 * 0 : no boundary
931 * 1 : part boundary
932 * 2 : final boundary
933 */
934 static int mime_bound_check(char *line, int linelen, const char *bound, int blen)
935 {
936 if (linelen == -1)
937 linelen = strlen(line);
938 if (blen == -1)
939 blen = strlen(bound);
940 /* Quickly eliminate if line length too short */
941 if (blen + 2 > linelen)
942 return 0;
943 /* Check for part boundary */
944 if ((strncmp(line, "--", 2) == 0)
945 && strncmp(line + 2, bound, blen) == 0) {
946 if (strncmp(line + blen + 2, "--", 2) == 0)
947 return 2;
948 else
949 return 1;
950 }
951 return 0;
952 }
953
954 static int strip_eol(char *linebuf, int *plen, int flags)
955 {
956 int len = *plen;
957 char *p, c;
958 int is_eol = 0;
959
960 for (p = linebuf + len - 1; len > 0; len--, p--) {
961 c = *p;
962 if (c == '\n') {
963 is_eol = 1;
964 } else if (is_eol && flags & SMIME_ASCIICRLF && c == 32) {
965 /* Strip trailing space on a line; 32 == ASCII for ' ' */
966 continue;
967 } else if (c != '\r') {
968 break;
969 }
970 }
971 *plen = len;
972 return is_eol;
973 }