2 * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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 <openssl/cms.h>
18 #include "crypto/evp.h"
19 #include "internal/bio.h"
20 #include "asn1_local.h"
23 * Generalised MIME like utilities for streaming ASN1. Although many have a
24 * PKCS7/CMS like flavour others are more general purpose.
28 * MIME format structures Note that all are translated to lower case apart
29 * from parameter values. Quotes are stripped off
32 struct mime_param_st {
33 char *param_name; /* Param name e.g. "micalg" */
34 char *param_value; /* Param value e.g. "sha1" */
37 struct mime_header_st {
38 char *name; /* Name of line e.g. "content-type" */
39 char *value; /* Value of line e.g. "text/plain" */
40 STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
43 static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
45 static char *strip_ends(char *name);
46 static char *strip_start(char *name);
47 static char *strip_end(char *name);
48 static MIME_HEADER *mime_hdr_new(const char *name, const char *value);
49 static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value);
50 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
51 static int mime_hdr_cmp(const MIME_HEADER *const *a,
52 const MIME_HEADER *const *b);
53 static int mime_param_cmp(const MIME_PARAM *const *a,
54 const MIME_PARAM *const *b);
55 static void mime_param_free(MIME_PARAM *param);
56 static int mime_bound_check(char *line, int linelen, const char *bound, int blen);
57 static int multi_split(BIO *bio, int flags, const char *bound, STACK_OF(BIO) **ret);
58 static int strip_eol(char *linebuf, int *plen, int flags);
59 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name);
60 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name);
61 static void mime_hdr_free(MIME_HEADER *hdr);
63 #define MAX_SMLEN 1024
64 #define mime_debug(x) /* x */
66 /* Output an ASN1 structure in BER format streaming if necessary */
68 /* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */
69 int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
72 /* If streaming create stream BIO and copy all content through it */
73 if (flags & SMIME_STREAM) {
75 bio = BIO_new_NDEF(out, val, it);
77 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
80 SMIME_crlf_copy(in, bio, flags);
82 /* Free up successive BIOs until we hit the old output BIO */
90 * else just write out ASN1 structure which will have all content stored
94 ASN1_item_i2d_bio(it, out, val);
98 /* Base 64 read and write of ASN1 structure */
100 static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
105 b64 = BIO_new(BIO_f_base64());
107 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
111 * prepend the b64 BIO so all data is base64 encoded.
113 out = BIO_push(b64, out);
114 r = i2d_ASN1_bio_stream(out, val, in, flags, it);
115 (void)BIO_flush(out);
121 /* Streaming ASN1 PEM write */
123 int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
124 const char *hdr, const ASN1_ITEM *it)
127 BIO_printf(out, "-----BEGIN %s-----\n", hdr);
128 r = B64_write_ASN1(out, val, in, flags, it);
129 BIO_printf(out, "-----END %s-----\n", hdr);
133 static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it, ASN1_VALUE **x)
138 if ((b64 = BIO_new(BIO_f_base64())) == NULL) {
139 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
142 bio = BIO_push(b64, bio);
143 val = ASN1_item_d2i_bio(it, bio, x);
145 ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
146 (void)BIO_flush(bio);
152 /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
154 static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
157 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
160 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) {
162 BIO_write(out, ",", 1);
164 md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
165 md = EVP_get_digestbynid(md_nid);
166 if (md && md->md_ctrl) {
169 rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
171 BIO_puts(out, micstr);
172 OPENSSL_free(micstr);
180 BIO_puts(out, "sha1");
184 BIO_puts(out, "md5");
188 BIO_puts(out, "sha-256");
192 BIO_puts(out, "sha-384");
196 BIO_puts(out, "sha-512");
199 case NID_id_GostR3411_94:
200 BIO_puts(out, "gostr3411-94");
203 case NID_id_GostR3411_2012_256:
204 BIO_puts(out, "gostr3411-2012-256");
207 case NID_id_GostR3411_2012_512:
208 BIO_puts(out, "gostr3411-2012-512");
215 BIO_puts(out, "unknown");
232 int SMIME_write_ASN1_ex(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
233 int ctype_nid, int econt_nid,
234 STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it,
235 OSSL_LIB_CTX *libctx, const char *propq)
239 const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
240 const char *msg_type = NULL;
242 if (flags & SMIME_OLDMIME)
243 mime_prefix = "application/x-pkcs7-";
245 mime_prefix = "application/pkcs7-";
247 if (flags & SMIME_CRLFEOL)
251 if ((flags & SMIME_DETACHED) && data) {
252 /* We want multipart/signed */
253 /* Generate a random boundary */
254 if (RAND_bytes_ex(libctx, (unsigned char *)bound, 32, 0) <= 0)
256 for (i = 0; i < 32; i++) {
265 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
266 BIO_printf(bio, "Content-Type: multipart/signed;");
267 BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
268 BIO_puts(bio, " micalg=\"");
269 asn1_write_micalg(bio, mdalgs);
270 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
271 bound, mime_eol, mime_eol);
272 BIO_printf(bio, "This is an S/MIME signed message%s%s",
274 /* Now write out the first part */
275 BIO_printf(bio, "------%s%s", bound, mime_eol);
276 if (!asn1_output_data(bio, data, val, flags, it))
278 BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
280 /* Headers for signature */
282 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
283 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
284 BIO_printf(bio, "Content-Transfer-Encoding: base64%s", mime_eol);
285 BIO_printf(bio, "Content-Disposition: attachment;");
286 BIO_printf(bio, " filename=\"smime.p7s\"%s%s", mime_eol, mime_eol);
287 B64_write_ASN1(bio, val, NULL, 0, it);
288 BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound,
293 /* Determine smime-type header */
295 if (ctype_nid == NID_pkcs7_enveloped) {
296 msg_type = "enveloped-data";
297 } else if (ctype_nid == NID_pkcs7_signed) {
298 if (econt_nid == NID_id_smime_ct_receipt)
299 msg_type = "signed-receipt";
300 else if (sk_X509_ALGOR_num(mdalgs) >= 0)
301 msg_type = "signed-data";
303 msg_type = "certs-only";
304 } else if (ctype_nid == NID_id_smime_ct_compressedData) {
305 msg_type = "compressed-data";
309 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
310 BIO_printf(bio, "Content-Disposition: attachment;");
311 BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
312 BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
314 BIO_printf(bio, " smime-type=%s;", msg_type);
315 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
316 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
318 if (!B64_write_ASN1(bio, val, data, flags, it))
320 BIO_printf(bio, "%s", mime_eol);
324 int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
325 int ctype_nid, int econt_nid,
326 STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it)
328 return SMIME_write_ASN1_ex(bio, val, data, flags, ctype_nid, econt_nid,
329 mdalgs, it, NULL, NULL);
332 /* Handle output of ASN1 data */
334 /* cannot constify val because of CMS_dataFinal() */
335 static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
339 const ASN1_AUX *aux = it->funcs;
340 ASN1_STREAM_ARG sarg;
344 * If data is not detached or resigning then the output BIO is already
345 * set up to finalise when it is written through.
347 if (!(flags & SMIME_DETACHED) || (flags & PKCS7_REUSE_DIGEST)) {
348 SMIME_crlf_copy(data, out, flags);
352 if (!aux || !aux->asn1_cb) {
353 ERR_raise(ERR_LIB_ASN1, ASN1_R_STREAMING_NOT_SUPPORTED);
358 sarg.ndef_bio = NULL;
359 sarg.boundary = NULL;
361 /* Let ASN1 code prepend any needed BIOs */
363 if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
366 /* Copy data across, passing through filter BIOs for processing */
367 SMIME_crlf_copy(data, sarg.ndef_bio, flags);
369 /* Finalize structure */
370 if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
373 /* Now remove any digests prepended to the BIO */
375 while (sarg.ndef_bio != out) {
376 tmpbio = BIO_pop(sarg.ndef_bio);
377 BIO_free(sarg.ndef_bio);
378 sarg.ndef_bio = tmpbio;
386 * SMIME reader: handle multipart/signed and opaque signing. in multipart
387 * case the content is placed in a memory BIO pointed to by "bcont". In
388 * opaque this is set to NULL
391 ASN1_VALUE *SMIME_read_ASN1_ex(BIO *bio, int flags, BIO **bcont, const ASN1_ITEM *it,
395 STACK_OF(MIME_HEADER) *headers = NULL;
396 STACK_OF(BIO) *parts = NULL;
405 if ((headers = mime_parse_hdr(bio)) == NULL) {
406 ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_PARSE_ERROR);
410 if ((hdr = mime_hdr_find(headers, "content-type")) == NULL
411 || hdr->value == NULL) {
412 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
413 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_CONTENT_TYPE);
417 /* Handle multipart/signed */
419 if (strcmp(hdr->value, "multipart/signed") == 0) {
420 /* Split into two parts */
421 prm = mime_param_find(hdr, "boundary");
422 if (prm == NULL || prm->param_value == NULL) {
423 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
424 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY);
427 ret = multi_split(bio, flags, prm->param_value, &parts);
428 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
429 if (!ret || (sk_BIO_num(parts) != 2)) {
430 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE);
431 sk_BIO_pop_free(parts, BIO_vfree);
435 /* Parse the signature piece */
436 asnin = sk_BIO_value(parts, 1);
438 if ((headers = mime_parse_hdr(asnin)) == NULL) {
439 ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_SIG_PARSE_ERROR);
440 sk_BIO_pop_free(parts, BIO_vfree);
444 /* Get content type */
446 if ((hdr = mime_hdr_find(headers, "content-type")) == NULL
447 || hdr->value == NULL) {
448 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
449 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE);
450 sk_BIO_pop_free(parts, BIO_vfree);
454 if (strcmp(hdr->value, "application/x-pkcs7-signature") &&
455 strcmp(hdr->value, "application/pkcs7-signature")) {
456 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_SIG_INVALID_MIME_TYPE,
457 "type: %s", hdr->value);
458 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
459 sk_BIO_pop_free(parts, BIO_vfree);
462 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
464 if ((val = b64_read_asn1(asnin, it, x)) == NULL) {
465 ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_SIG_PARSE_ERROR);
466 sk_BIO_pop_free(parts, BIO_vfree);
471 *bcont = sk_BIO_value(parts, 0);
475 sk_BIO_pop_free(parts, BIO_vfree);
480 /* OK, if not multipart/signed try opaque signature */
482 if (strcmp(hdr->value, "application/x-pkcs7-mime") &&
483 strcmp(hdr->value, "application/pkcs7-mime")) {
484 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MIME_TYPE,
485 "type: %s", hdr->value);
486 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
490 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
492 if ((val = b64_read_asn1(bio, it, x)) == NULL) {
493 ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_PARSE_ERROR);
499 ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
501 return SMIME_read_ASN1_ex(bio, 0, bcont, it, NULL);
504 /* Copy text from one BIO to another making the output CRLF at EOL */
505 int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
510 char linebuf[MAX_SMLEN];
512 * Buffer output so we don't write one line at a time. This is useful
513 * when streaming as we don't end up with one OCTET STRING per line.
515 bf = BIO_new(BIO_f_buffer());
518 out = BIO_push(bf, out);
519 if (flags & SMIME_BINARY) {
520 while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
521 BIO_write(out, linebuf, len);
524 if (flags & SMIME_TEXT)
525 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
526 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
527 eol = strip_eol(linebuf, &len, flags);
529 /* Not EOF: write out all CRLF */
530 if (flags & SMIME_ASCIICRLF) {
532 for (i = 0; i < eolcnt; i++)
533 BIO_write(out, "\r\n", 2);
536 BIO_write(out, linebuf, len);
538 BIO_write(out, "\r\n", 2);
539 } else if (flags & SMIME_ASCIICRLF) {
542 BIO_write(out, "\r\n", 2);
546 (void)BIO_flush(out);
552 /* Strip off headers if they are text/plain */
553 int SMIME_text(BIO *in, BIO *out)
557 STACK_OF(MIME_HEADER) *headers;
560 if ((headers = mime_parse_hdr(in)) == NULL) {
561 ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_PARSE_ERROR);
564 if ((hdr = mime_hdr_find(headers, "content-type")) == NULL
565 || hdr->value == NULL) {
566 ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_NO_CONTENT_TYPE);
567 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
570 if (strcmp(hdr->value, "text/plain")) {
571 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MIME_TYPE,
572 "type: %s", hdr->value);
573 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
576 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
577 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
578 BIO_write(out, iobuf, len);
585 * Split a multipart/XXX message body into component parts: result is
586 * canonical parts in a STACK of bios
589 static int multi_split(BIO *bio, int flags, const char *bound, STACK_OF(BIO) **ret)
591 char linebuf[MAX_SMLEN];
593 int eol = 0, next_eol = 0;
595 STACK_OF(BIO) *parts;
596 char state, part, first;
598 blen = strlen(bound);
602 parts = sk_BIO_new_null();
606 while ((len = BIO_get_line(bio, linebuf, MAX_SMLEN)) > 0) {
607 state = mime_bound_check(linebuf, len, bound, blen);
611 } else if (state == 2) {
612 if (!sk_BIO_push(parts, bpart)) {
617 } else if (part != 0) {
618 /* Strip (possibly CR +) LF from linebuf */
619 next_eol = strip_eol(linebuf, &len, flags);
623 if (!sk_BIO_push(parts, bpart)) {
627 bpart = BIO_new(BIO_s_mem());
630 BIO_set_mem_eof_return(bpart, 0);
633 #ifndef OPENSSL_NO_CMS
634 (flags & CMS_BINARY) == 0
638 || (flags & SMIME_CRLFEOL) != 0)
639 BIO_write(bpart, "\r\n", 2);
641 BIO_write(bpart, "\n", 1);
645 BIO_write(bpart, linebuf, len);
652 /* This is the big one: parse MIME header lines up to message body */
654 #define MIME_INVALID 0
660 #define MIME_COMMENT 6
662 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
666 char linebuf[MAX_SMLEN];
667 MIME_HEADER *mhdr = NULL, *new_hdr = NULL;
668 STACK_OF(MIME_HEADER) *headers;
669 int len, state, save_state = 0;
671 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
674 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
675 /* If whitespace at line start then continuation line */
676 if (mhdr && ossl_isspace(linebuf[0]))
681 /* Go through all characters */
682 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
686 * State machine to handle MIME headers if this looks horrible
687 * that's because it *is*
695 ntmp = strip_ends(q);
702 mime_debug("Found End Value\n");
704 new_hdr = mime_hdr_new(ntmp, strip_ends(q));
707 if (!sk_MIME_HEADER_push(headers, new_hdr))
714 } else if (c == '(') {
716 state = MIME_COMMENT;
730 ntmp = strip_ends(q);
739 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
742 } else if (c == '"') {
743 mime_debug("Found Quote\n");
745 } else if (c == '(') {
747 state = MIME_COMMENT;
753 mime_debug("Found Match Quote\n");
760 if (state == MIME_TYPE) {
761 new_hdr = mime_hdr_new(ntmp, strip_ends(q));
764 if (!sk_MIME_HEADER_push(headers, new_hdr))
768 } else if (state == MIME_VALUE) {
769 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
772 break; /* Blank line means end of headers */
778 mime_hdr_free(new_hdr);
779 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
783 static char *strip_ends(char *name)
785 return strip_end(strip_start(name));
788 /* Strip a parameter of whitespace from start of param */
789 static char *strip_start(char *name)
792 /* Look for first non whitespace or quote */
793 for (p = name; (c = *p); p++) {
795 /* Next char is start of string if non null */
798 /* Else null string */
801 if (!ossl_isspace(c))
807 /* As above but strip from end of string : maybe should handle brackets? */
808 static char *strip_end(char *name)
813 /* Look for first non whitespace or quote */
814 for (p = name + strlen(name) - 1; p >= name; p--) {
830 static MIME_HEADER *mime_hdr_new(const char *name, const char *value)
832 MIME_HEADER *mhdr = NULL;
833 char *tmpname = NULL, *tmpval = NULL, *p;
836 if ((tmpname = OPENSSL_strdup(name)) == NULL)
838 for (p = tmpname; *p; p++)
839 *p = ossl_tolower(*p);
842 if ((tmpval = OPENSSL_strdup(value)) == NULL)
844 for (p = tmpval; *p; p++)
845 *p = ossl_tolower(*p);
847 mhdr = OPENSSL_malloc(sizeof(*mhdr));
850 mhdr->name = tmpname;
851 mhdr->value = tmpval;
852 if ((mhdr->params = sk_MIME_PARAM_new(mime_param_cmp)) == NULL)
857 OPENSSL_free(tmpname);
858 OPENSSL_free(tmpval);
863 static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value)
865 char *tmpname = NULL, *tmpval = NULL, *p;
866 MIME_PARAM *mparam = NULL;
869 tmpname = OPENSSL_strdup(name);
872 for (p = tmpname; *p; p++)
873 *p = ossl_tolower(*p);
876 tmpval = OPENSSL_strdup(value);
880 /* Parameter values are case sensitive so leave as is */
881 mparam = OPENSSL_malloc(sizeof(*mparam));
884 mparam->param_name = tmpname;
885 mparam->param_value = tmpval;
886 if (!sk_MIME_PARAM_push(mhdr->params, mparam))
890 OPENSSL_free(tmpname);
891 OPENSSL_free(tmpval);
892 OPENSSL_free(mparam);
896 static int mime_hdr_cmp(const MIME_HEADER *const *a,
897 const MIME_HEADER *const *b)
899 if ((*a)->name == NULL || (*b)->name == NULL)
900 return ((*a)->name != NULL) - ((*b)->name != NULL);
902 return strcmp((*a)->name, (*b)->name);
905 static int mime_param_cmp(const MIME_PARAM *const *a,
906 const MIME_PARAM *const *b)
908 if ((*a)->param_name == NULL || (*b)->param_name == NULL)
909 return ((*a)->param_name != NULL) - ((*b)->param_name != NULL);
910 return strcmp((*a)->param_name, (*b)->param_name);
913 /* Find a header with a given name (if possible) */
915 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name)
920 htmp.name = (char *)name;
924 idx = sk_MIME_HEADER_find(hdrs, &htmp);
925 return sk_MIME_HEADER_value(hdrs, idx);
928 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name)
933 param.param_name = (char *)name;
934 param.param_value = NULL;
935 idx = sk_MIME_PARAM_find(hdr->params, ¶m);
936 return sk_MIME_PARAM_value(hdr->params, idx);
939 static void mime_hdr_free(MIME_HEADER *hdr)
943 OPENSSL_free(hdr->name);
944 OPENSSL_free(hdr->value);
946 sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
950 static void mime_param_free(MIME_PARAM *param)
952 OPENSSL_free(param->param_name);
953 OPENSSL_free(param->param_value);
958 * Check for a multipart boundary. Returns:
963 static int mime_bound_check(char *line, int linelen, const char *bound, int blen)
966 linelen = strlen(line);
968 blen = strlen(bound);
969 /* Quickly eliminate if line length too short */
970 if (blen + 2 > linelen)
972 /* Check for part boundary */
973 if ((strncmp(line, "--", 2) == 0)
974 && strncmp(line + 2, bound, blen) == 0) {
975 if (strncmp(line + blen + 2, "--", 2) == 0)
983 static int strip_eol(char *linebuf, int *plen, int flags)
989 #ifndef OPENSSL_NO_CMS
990 if ((flags & CMS_BINARY) != 0) {
991 if (len <= 0 || linebuf[len - 1] != '\n')
993 if ((flags & SMIME_CRLFEOL) != 0) {
994 if (len <= 1 || linebuf[len - 2] != '\r')
1004 for (p = linebuf + len - 1; len > 0; len--, p--) {
1008 } else if (is_eol && (flags & SMIME_ASCIICRLF) != 0 && c == 32) {
1009 /* Strip trailing space on a line; 32 == ASCII for ' ' */
1011 } else if (c != '\r') {