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