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
,
74 /* If streaming create stream BIO and copy all content through it */
75 if (flags
& SMIME_STREAM
) {
77 bio
= BIO_new_NDEF(out
, val
, it
);
79 ERR_raise(ERR_LIB_ASN1
, ERR_R_BUF_LIB
);
82 if (!SMIME_crlf_copy(in
, bio
, flags
)) {
87 /* Free up successive BIOs until we hit the old output BIO */
95 * else just write out ASN1 structure which will have all content stored
99 ASN1_item_i2d_bio(it
, out
, val
);
103 /* Base 64 read and write of ASN1 structure */
105 static int B64_write_ASN1(BIO
*out
, ASN1_VALUE
*val
, BIO
*in
, int flags
,
110 b64
= BIO_new(BIO_f_base64());
112 ERR_raise(ERR_LIB_ASN1
, ERR_R_BIO_LIB
);
116 * prepend the b64 BIO so all data is base64 encoded.
118 out
= BIO_push(b64
, out
);
119 r
= i2d_ASN1_bio_stream(out
, val
, in
, flags
, it
);
120 (void)BIO_flush(out
);
126 /* Streaming ASN1 PEM write */
128 int PEM_write_bio_ASN1_stream(BIO
*out
, ASN1_VALUE
*val
, BIO
*in
, int flags
,
129 const char *hdr
, const ASN1_ITEM
*it
)
132 BIO_printf(out
, "-----BEGIN %s-----\n", hdr
);
133 r
= B64_write_ASN1(out
, val
, in
, flags
, it
);
134 BIO_printf(out
, "-----END %s-----\n", hdr
);
138 static ASN1_VALUE
*b64_read_asn1(BIO
*bio
, const ASN1_ITEM
*it
, ASN1_VALUE
**x
,
139 OSSL_LIB_CTX
*libctx
, const char *propq
)
144 if ((b64
= BIO_new(BIO_f_base64())) == NULL
) {
145 ERR_raise(ERR_LIB_ASN1
, ERR_R_BIO_LIB
);
148 bio
= BIO_push(b64
, bio
);
149 val
= ASN1_item_d2i_bio_ex(it
, bio
, x
, libctx
, propq
);
151 ERR_raise(ERR_LIB_ASN1
, ASN1_R_DECODE_ERROR
);
152 (void)BIO_flush(bio
);
158 /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
160 static int asn1_write_micalg(BIO
*out
, STACK_OF(X509_ALGOR
) *mdalgs
)
163 int i
, have_unknown
= 0, write_comma
, ret
= 0, md_nid
;
166 for (i
= 0; i
< sk_X509_ALGOR_num(mdalgs
); i
++) {
168 BIO_write(out
, ",", 1);
170 md_nid
= OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs
, i
)->algorithm
);
171 md
= EVP_get_digestbynid(md_nid
);
172 if (md
&& md
->md_ctrl
) {
175 rv
= md
->md_ctrl(NULL
, EVP_MD_CTRL_MICALG
, 0, &micstr
);
177 BIO_puts(out
, micstr
);
178 OPENSSL_free(micstr
);
186 BIO_puts(out
, "sha1");
190 BIO_puts(out
, "md5");
194 BIO_puts(out
, "sha-256");
198 BIO_puts(out
, "sha-384");
202 BIO_puts(out
, "sha-512");
205 case NID_id_GostR3411_94
:
206 BIO_puts(out
, "gostr3411-94");
209 case NID_id_GostR3411_2012_256
:
210 BIO_puts(out
, "gostr3411-2012-256");
213 case NID_id_GostR3411_2012_512
:
214 BIO_puts(out
, "gostr3411-2012-512");
221 BIO_puts(out
, "unknown");
238 int SMIME_write_ASN1_ex(BIO
*bio
, ASN1_VALUE
*val
, BIO
*data
, int flags
,
239 int ctype_nid
, int econt_nid
,
240 STACK_OF(X509_ALGOR
) *mdalgs
, const ASN1_ITEM
*it
,
241 OSSL_LIB_CTX
*libctx
, const char *propq
)
245 const char *mime_prefix
, *mime_eol
, *cname
= "smime.p7m";
246 const char *msg_type
= NULL
;
248 if (flags
& SMIME_OLDMIME
)
249 mime_prefix
= "application/x-pkcs7-";
251 mime_prefix
= "application/pkcs7-";
253 if (flags
& SMIME_CRLFEOL
)
257 if ((flags
& SMIME_DETACHED
) && data
) {
258 /* We want multipart/signed */
259 /* Generate a random boundary */
260 if (RAND_bytes_ex(libctx
, (unsigned char *)bound
, 32, 0) <= 0)
262 for (i
= 0; i
< 32; i
++) {
271 BIO_printf(bio
, "MIME-Version: 1.0%s", mime_eol
);
272 BIO_printf(bio
, "Content-Type: multipart/signed;");
273 BIO_printf(bio
, " protocol=\"%ssignature\";", mime_prefix
);
274 BIO_puts(bio
, " micalg=\"");
275 asn1_write_micalg(bio
, mdalgs
);
276 BIO_printf(bio
, "\"; boundary=\"----%s\"%s%s",
277 bound
, mime_eol
, mime_eol
);
278 BIO_printf(bio
, "This is an S/MIME signed message%s%s",
280 /* Now write out the first part */
281 BIO_printf(bio
, "------%s%s", bound
, mime_eol
);
282 if (!asn1_output_data(bio
, data
, val
, flags
, it
))
284 BIO_printf(bio
, "%s------%s%s", mime_eol
, bound
, mime_eol
);
286 /* Headers for signature */
288 BIO_printf(bio
, "Content-Type: %ssignature;", mime_prefix
);
289 BIO_printf(bio
, " name=\"smime.p7s\"%s", mime_eol
);
290 BIO_printf(bio
, "Content-Transfer-Encoding: base64%s", mime_eol
);
291 BIO_printf(bio
, "Content-Disposition: attachment;");
292 BIO_printf(bio
, " filename=\"smime.p7s\"%s%s", mime_eol
, mime_eol
);
293 B64_write_ASN1(bio
, val
, NULL
, 0, it
);
294 BIO_printf(bio
, "%s------%s--%s%s", mime_eol
, bound
,
299 /* Determine smime-type header */
301 if (ctype_nid
== NID_pkcs7_enveloped
) {
302 msg_type
= "enveloped-data";
303 } else if (ctype_nid
== NID_pkcs7_signed
) {
304 if (econt_nid
== NID_id_smime_ct_receipt
)
305 msg_type
= "signed-receipt";
306 else if (sk_X509_ALGOR_num(mdalgs
) >= 0)
307 msg_type
= "signed-data";
309 msg_type
= "certs-only";
310 } else if (ctype_nid
== NID_id_smime_ct_compressedData
) {
311 msg_type
= "compressed-data";
315 BIO_printf(bio
, "MIME-Version: 1.0%s", mime_eol
);
316 BIO_printf(bio
, "Content-Disposition: attachment;");
317 BIO_printf(bio
, " filename=\"%s\"%s", cname
, mime_eol
);
318 BIO_printf(bio
, "Content-Type: %smime;", mime_prefix
);
320 BIO_printf(bio
, " smime-type=%s;", msg_type
);
321 BIO_printf(bio
, " name=\"%s\"%s", cname
, mime_eol
);
322 BIO_printf(bio
, "Content-Transfer-Encoding: base64%s%s",
324 if (!B64_write_ASN1(bio
, val
, data
, flags
, it
))
326 BIO_printf(bio
, "%s", mime_eol
);
330 int SMIME_write_ASN1(BIO
*bio
, ASN1_VALUE
*val
, BIO
*data
, int flags
,
331 int ctype_nid
, int econt_nid
,
332 STACK_OF(X509_ALGOR
) *mdalgs
, const ASN1_ITEM
*it
)
334 return SMIME_write_ASN1_ex(bio
, val
, data
, flags
, ctype_nid
, econt_nid
,
335 mdalgs
, it
, NULL
, NULL
);
338 /* Handle output of ASN1 data */
340 /* cannot constify val because of CMS_dataFinal() */
341 static int asn1_output_data(BIO
*out
, BIO
*data
, ASN1_VALUE
*val
, int flags
,
345 const ASN1_AUX
*aux
= it
->funcs
;
346 ASN1_STREAM_ARG sarg
;
350 * If data is not detached or resigning then the output BIO is already
351 * set up to finalise when it is written through.
353 if (!(flags
& SMIME_DETACHED
) || (flags
& PKCS7_REUSE_DIGEST
)) {
354 return SMIME_crlf_copy(data
, out
, flags
);
357 if (!aux
|| !aux
->asn1_cb
) {
358 ERR_raise(ERR_LIB_ASN1
, ASN1_R_STREAMING_NOT_SUPPORTED
);
363 sarg
.ndef_bio
= NULL
;
364 sarg
.boundary
= NULL
;
366 /* Let ASN1 code prepend any needed BIOs */
368 if (aux
->asn1_cb(ASN1_OP_DETACHED_PRE
, &val
, it
, &sarg
) <= 0)
371 /* Copy data across, passing through filter BIOs for processing */
372 if (!SMIME_crlf_copy(data
, sarg
.ndef_bio
, flags
))
375 /* Finalize structure */
376 if (aux
->asn1_cb(ASN1_OP_DETACHED_POST
, &val
, it
, &sarg
) <= 0)
379 /* Now remove any digests prepended to the BIO */
381 while (sarg
.ndef_bio
!= out
) {
382 tmpbio
= BIO_pop(sarg
.ndef_bio
);
383 BIO_free(sarg
.ndef_bio
);
384 sarg
.ndef_bio
= tmpbio
;
392 * SMIME reader: handle multipart/signed and opaque signing. in multipart
393 * case the content is placed in a memory BIO pointed to by "bcont". In
394 * opaque this is set to NULL
397 ASN1_VALUE
*SMIME_read_ASN1_ex(BIO
*bio
, int flags
, BIO
**bcont
,
398 const ASN1_ITEM
*it
, ASN1_VALUE
**x
,
399 OSSL_LIB_CTX
*libctx
, const char *propq
)
402 STACK_OF(MIME_HEADER
) *headers
= NULL
;
403 STACK_OF(BIO
) *parts
= NULL
;
412 if ((headers
= mime_parse_hdr(bio
)) == NULL
) {
413 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MIME_PARSE_ERROR
);
417 if ((hdr
= mime_hdr_find(headers
, "content-type")) == NULL
418 || hdr
->value
== NULL
) {
419 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
420 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NO_CONTENT_TYPE
);
424 /* Handle multipart/signed */
426 if (strcmp(hdr
->value
, "multipart/signed") == 0) {
427 /* Split into two parts */
428 prm
= mime_param_find(hdr
, "boundary");
429 if (prm
== NULL
|| prm
->param_value
== NULL
) {
430 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
431 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NO_MULTIPART_BOUNDARY
);
434 ret
= multi_split(bio
, flags
, prm
->param_value
, &parts
);
435 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
436 if (!ret
|| (sk_BIO_num(parts
) != 2)) {
437 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NO_MULTIPART_BODY_FAILURE
);
438 sk_BIO_pop_free(parts
, BIO_vfree
);
442 /* Parse the signature piece */
443 asnin
= sk_BIO_value(parts
, 1);
445 if ((headers
= mime_parse_hdr(asnin
)) == NULL
) {
446 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MIME_SIG_PARSE_ERROR
);
447 sk_BIO_pop_free(parts
, BIO_vfree
);
451 /* Get content type */
453 if ((hdr
= mime_hdr_find(headers
, "content-type")) == NULL
454 || hdr
->value
== NULL
) {
455 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
456 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NO_SIG_CONTENT_TYPE
);
457 sk_BIO_pop_free(parts
, BIO_vfree
);
461 if (strcmp(hdr
->value
, "application/x-pkcs7-signature") &&
462 strcmp(hdr
->value
, "application/pkcs7-signature")) {
463 ERR_raise_data(ERR_LIB_ASN1
, ASN1_R_SIG_INVALID_MIME_TYPE
,
464 "type: %s", hdr
->value
);
465 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
466 sk_BIO_pop_free(parts
, BIO_vfree
);
469 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
471 if ((val
= b64_read_asn1(asnin
, it
, x
, libctx
, propq
)) == NULL
) {
472 ERR_raise(ERR_LIB_ASN1
, ASN1_R_ASN1_SIG_PARSE_ERROR
);
473 sk_BIO_pop_free(parts
, BIO_vfree
);
478 *bcont
= sk_BIO_value(parts
, 0);
482 sk_BIO_pop_free(parts
, BIO_vfree
);
487 /* OK, if not multipart/signed try opaque signature */
489 if (strcmp(hdr
->value
, "application/x-pkcs7-mime") &&
490 strcmp(hdr
->value
, "application/pkcs7-mime")) {
491 ERR_raise_data(ERR_LIB_ASN1
, ASN1_R_INVALID_MIME_TYPE
,
492 "type: %s", hdr
->value
);
493 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
497 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
499 if ((val
= b64_read_asn1(bio
, it
, x
, libctx
, propq
)) == NULL
) {
500 ERR_raise(ERR_LIB_ASN1
, ASN1_R_ASN1_PARSE_ERROR
);
506 ASN1_VALUE
*SMIME_read_ASN1(BIO
*bio
, BIO
**bcont
, const ASN1_ITEM
*it
)
508 return SMIME_read_ASN1_ex(bio
, 0, bcont
, it
, NULL
, NULL
, NULL
);
511 /* Copy text from one BIO to another making the output CRLF at EOL */
512 int SMIME_crlf_copy(BIO
*in
, BIO
*out
, int flags
)
517 char linebuf
[MAX_SMLEN
];
519 * Buffer output so we don't write one line at a time. This is useful
520 * when streaming as we don't end up with one OCTET STRING per line.
522 bf
= BIO_new(BIO_f_buffer());
524 ERR_raise(ERR_LIB_ASN1
, ERR_R_BIO_LIB
);
527 out
= BIO_push(bf
, out
);
528 if (flags
& SMIME_BINARY
) {
529 while ((len
= BIO_read(in
, linebuf
, MAX_SMLEN
)) > 0)
530 BIO_write(out
, linebuf
, len
);
533 if (flags
& SMIME_TEXT
)
534 BIO_printf(out
, "Content-Type: text/plain\r\n\r\n");
535 while ((len
= BIO_gets(in
, linebuf
, MAX_SMLEN
)) > 0) {
536 eol
= strip_eol(linebuf
, &len
, flags
);
538 /* Not EOF: write out all CRLF */
539 if (flags
& SMIME_ASCIICRLF
) {
541 for (i
= 0; i
< eolcnt
; i
++)
542 BIO_write(out
, "\r\n", 2);
545 BIO_write(out
, linebuf
, len
);
547 BIO_write(out
, "\r\n", 2);
548 } else if (flags
& SMIME_ASCIICRLF
) {
551 BIO_write(out
, "\r\n", 2);
555 (void)BIO_flush(out
);
561 /* Strip off headers if they are text/plain */
562 int SMIME_text(BIO
*in
, BIO
*out
)
566 STACK_OF(MIME_HEADER
) *headers
;
569 if ((headers
= mime_parse_hdr(in
)) == NULL
) {
570 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MIME_PARSE_ERROR
);
573 if ((hdr
= mime_hdr_find(headers
, "content-type")) == NULL
574 || hdr
->value
== NULL
) {
575 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MIME_NO_CONTENT_TYPE
);
576 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
579 if (strcmp(hdr
->value
, "text/plain")) {
580 ERR_raise_data(ERR_LIB_ASN1
, ASN1_R_INVALID_MIME_TYPE
,
581 "type: %s", hdr
->value
);
582 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
585 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
586 while ((len
= BIO_read(in
, iobuf
, sizeof(iobuf
))) > 0)
587 BIO_write(out
, iobuf
, len
);
594 * Split a multipart/XXX message body into component parts: result is
595 * canonical parts in a STACK of bios
598 static int multi_split(BIO
*bio
, int flags
, const char *bound
, STACK_OF(BIO
) **ret
)
600 char linebuf
[MAX_SMLEN
];
602 int eol
= 0, next_eol
= 0;
604 STACK_OF(BIO
) *parts
;
605 char state
, part
, first
;
607 blen
= strlen(bound
);
611 parts
= sk_BIO_new_null();
615 while ((len
= BIO_get_line(bio
, linebuf
, MAX_SMLEN
)) > 0) {
616 state
= mime_bound_check(linebuf
, len
, bound
, blen
);
620 } else if (state
== 2) {
621 if (!sk_BIO_push(parts
, bpart
)) {
626 } else if (part
!= 0) {
627 /* Strip (possibly CR +) LF from linebuf */
628 next_eol
= strip_eol(linebuf
, &len
, flags
);
632 if (!sk_BIO_push(parts
, bpart
)) {
636 bpart
= BIO_new(BIO_s_mem());
639 BIO_set_mem_eof_return(bpart
, 0);
642 #ifndef OPENSSL_NO_CMS
643 (flags
& CMS_BINARY
) == 0
647 || (flags
& SMIME_CRLFEOL
) != 0)
648 BIO_write(bpart
, "\r\n", 2);
650 BIO_write(bpart
, "\n", 1);
654 BIO_write(bpart
, linebuf
, len
);
661 /* This is the big one: parse MIME header lines up to message body */
663 #define MIME_INVALID 0
669 #define MIME_COMMENT 6
671 static STACK_OF(MIME_HEADER
) *mime_parse_hdr(BIO
*bio
)
675 char linebuf
[MAX_SMLEN
];
676 MIME_HEADER
*mhdr
= NULL
, *new_hdr
= NULL
;
677 STACK_OF(MIME_HEADER
) *headers
;
678 int len
, state
, save_state
= 0;
680 headers
= sk_MIME_HEADER_new(mime_hdr_cmp
);
683 while ((len
= BIO_gets(bio
, linebuf
, MAX_SMLEN
)) > 0) {
684 /* If whitespace at line start then continuation line */
685 if (mhdr
&& ossl_isspace(linebuf
[0]))
690 /* Go through all characters */
691 for (p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!= '\r') && (c
!= '\n');
695 * State machine to handle MIME headers if this looks horrible
696 * that's because it *is*
704 ntmp
= strip_ends(q
);
711 mime_debug("Found End Value\n");
713 new_hdr
= mime_hdr_new(ntmp
, strip_ends(q
));
716 if (!sk_MIME_HEADER_push(headers
, new_hdr
))
723 } else if (c
== '(') {
725 state
= MIME_COMMENT
;
739 ntmp
= strip_ends(q
);
748 mime_hdr_addparam(mhdr
, ntmp
, strip_ends(q
));
751 } else if (c
== '"') {
752 mime_debug("Found Quote\n");
754 } else if (c
== '(') {
756 state
= MIME_COMMENT
;
762 mime_debug("Found Match Quote\n");
769 if (state
== MIME_TYPE
) {
770 new_hdr
= mime_hdr_new(ntmp
, strip_ends(q
));
773 if (!sk_MIME_HEADER_push(headers
, new_hdr
))
777 } else if (state
== MIME_VALUE
) {
778 mime_hdr_addparam(mhdr
, ntmp
, strip_ends(q
));
781 break; /* Blank line means end of headers */
787 mime_hdr_free(new_hdr
);
788 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
792 static char *strip_ends(char *name
)
794 return strip_end(strip_start(name
));
797 /* Strip a parameter of whitespace from start of param */
798 static char *strip_start(char *name
)
801 /* Look for first non whitespace or quote */
802 for (p
= name
; (c
= *p
); p
++) {
804 /* Next char is start of string if non null */
807 /* Else null string */
810 if (!ossl_isspace(c
))
816 /* As above but strip from end of string : maybe should handle brackets? */
817 static char *strip_end(char *name
)
822 /* Look for first non whitespace or quote */
823 for (p
= name
+ strlen(name
) - 1; p
>= name
; p
--) {
839 static MIME_HEADER
*mime_hdr_new(const char *name
, const char *value
)
841 MIME_HEADER
*mhdr
= NULL
;
842 char *tmpname
= NULL
, *tmpval
= NULL
, *p
;
845 if ((tmpname
= OPENSSL_strdup(name
)) == NULL
)
847 for (p
= tmpname
; *p
; p
++)
848 *p
= ossl_tolower(*p
);
851 if ((tmpval
= OPENSSL_strdup(value
)) == NULL
)
853 for (p
= tmpval
; *p
; p
++)
854 *p
= ossl_tolower(*p
);
856 mhdr
= OPENSSL_malloc(sizeof(*mhdr
));
859 mhdr
->name
= tmpname
;
860 mhdr
->value
= tmpval
;
861 if ((mhdr
->params
= sk_MIME_PARAM_new(mime_param_cmp
)) == NULL
)
866 OPENSSL_free(tmpname
);
867 OPENSSL_free(tmpval
);
872 static int mime_hdr_addparam(MIME_HEADER
*mhdr
, const char *name
, const char *value
)
874 char *tmpname
= NULL
, *tmpval
= NULL
, *p
;
875 MIME_PARAM
*mparam
= NULL
;
878 tmpname
= OPENSSL_strdup(name
);
881 for (p
= tmpname
; *p
; p
++)
882 *p
= ossl_tolower(*p
);
885 tmpval
= OPENSSL_strdup(value
);
889 /* Parameter values are case sensitive so leave as is */
890 mparam
= OPENSSL_malloc(sizeof(*mparam
));
893 mparam
->param_name
= tmpname
;
894 mparam
->param_value
= tmpval
;
895 if (!sk_MIME_PARAM_push(mhdr
->params
, mparam
))
899 OPENSSL_free(tmpname
);
900 OPENSSL_free(tmpval
);
901 OPENSSL_free(mparam
);
905 static int mime_hdr_cmp(const MIME_HEADER
*const *a
,
906 const MIME_HEADER
*const *b
)
908 if ((*a
)->name
== NULL
|| (*b
)->name
== NULL
)
909 return ((*a
)->name
!= NULL
) - ((*b
)->name
!= NULL
);
911 return strcmp((*a
)->name
, (*b
)->name
);
914 static int mime_param_cmp(const MIME_PARAM
*const *a
,
915 const MIME_PARAM
*const *b
)
917 if ((*a
)->param_name
== NULL
|| (*b
)->param_name
== NULL
)
918 return ((*a
)->param_name
!= NULL
) - ((*b
)->param_name
!= NULL
);
919 return strcmp((*a
)->param_name
, (*b
)->param_name
);
922 /* Find a header with a given name (if possible) */
924 static MIME_HEADER
*mime_hdr_find(STACK_OF(MIME_HEADER
) *hdrs
, const char *name
)
929 htmp
.name
= (char *)name
;
933 idx
= sk_MIME_HEADER_find(hdrs
, &htmp
);
934 return sk_MIME_HEADER_value(hdrs
, idx
);
937 static MIME_PARAM
*mime_param_find(MIME_HEADER
*hdr
, const char *name
)
942 param
.param_name
= (char *)name
;
943 param
.param_value
= NULL
;
944 idx
= sk_MIME_PARAM_find(hdr
->params
, ¶m
);
945 return sk_MIME_PARAM_value(hdr
->params
, idx
);
948 static void mime_hdr_free(MIME_HEADER
*hdr
)
952 OPENSSL_free(hdr
->name
);
953 OPENSSL_free(hdr
->value
);
955 sk_MIME_PARAM_pop_free(hdr
->params
, mime_param_free
);
959 static void mime_param_free(MIME_PARAM
*param
)
961 OPENSSL_free(param
->param_name
);
962 OPENSSL_free(param
->param_value
);
967 * Check for a multipart boundary. Returns:
972 static int mime_bound_check(char *line
, int linelen
, const char *bound
, int blen
)
975 linelen
= strlen(line
);
977 blen
= strlen(bound
);
978 /* Quickly eliminate if line length too short */
979 if (blen
+ 2 > linelen
)
981 /* Check for part boundary */
982 if ((CHECK_AND_SKIP_PREFIX(line
, "--")) && strncmp(line
, bound
, blen
) == 0)
983 return HAS_PREFIX(line
+ blen
, "--") ? 2 : 1;
987 static int strip_eol(char *linebuf
, int *plen
, int flags
)
993 #ifndef OPENSSL_NO_CMS
994 if ((flags
& CMS_BINARY
) != 0) {
995 if (len
<= 0 || linebuf
[len
- 1] != '\n')
997 if ((flags
& SMIME_CRLFEOL
) != 0) {
998 if (len
<= 1 || linebuf
[len
- 2] != '\r')
1008 for (p
= linebuf
+ len
- 1; len
> 0; len
--, p
--) {
1012 } else if (is_eol
&& (flags
& SMIME_ASCIICRLF
) != 0 && c
== 32) {
1013 /* Strip trailing space on a line; 32 == ASCII for ' ' */
1015 } else if (c
!= '\r') {