source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/asn1/asn_mime.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

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