2 * Copyright 2008-2018 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 "crypto/evp.h"
18 #include "internal/bio.h"
19 #include "asn1_local.h"
22 * Generalised MIME like utilities for streaming ASN1. Although many have a
23 * PKCS7/CMS like flavour others are more general purpose.
27 * MIME format structures Note that all are translated to lower case apart
28 * from parameter values. Quotes are stripped off
31 struct mime_param_st
{
32 char *param_name
; /* Param name e.g. "micalg" */
33 char *param_value
; /* Param value e.g. "sha1" */
36 struct mime_header_st
{
37 char *name
; /* Name of line e.g. "content-type" */
38 char *value
; /* Value of line e.g. "text/plain" */
39 STACK_OF(MIME_PARAM
) *params
; /* Zero or more parameters */
42 static int asn1_output_data(BIO
*out
, BIO
*data
, ASN1_VALUE
*val
, int flags
,
44 static char *strip_ends(char *name
);
45 static char *strip_start(char *name
);
46 static char *strip_end(char *name
);
47 static MIME_HEADER
*mime_hdr_new(const char *name
, const char *value
);
48 static int mime_hdr_addparam(MIME_HEADER
*mhdr
, const char *name
, const char *value
);
49 static STACK_OF(MIME_HEADER
) *mime_parse_hdr(BIO
*bio
);
50 static int mime_hdr_cmp(const MIME_HEADER
*const *a
,
51 const MIME_HEADER
*const *b
);
52 static int mime_param_cmp(const MIME_PARAM
*const *a
,
53 const MIME_PARAM
*const *b
);
54 static void mime_param_free(MIME_PARAM
*param
);
55 static int mime_bound_check(char *line
, int linelen
, const char *bound
, int blen
);
56 static int multi_split(BIO
*bio
, const char *bound
, STACK_OF(BIO
) **ret
);
57 static int strip_eol(char *linebuf
, int *plen
, int flags
);
58 static MIME_HEADER
*mime_hdr_find(STACK_OF(MIME_HEADER
) *hdrs
, const char *name
);
59 static MIME_PARAM
*mime_param_find(MIME_HEADER
*hdr
, const char *name
);
60 static void mime_hdr_free(MIME_HEADER
*hdr
);
62 #define MAX_SMLEN 1024
63 #define mime_debug(x) /* x */
65 /* Output an ASN1 structure in BER format streaming if necessary */
67 /* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */
68 int i2d_ASN1_bio_stream(BIO
*out
, ASN1_VALUE
*val
, BIO
*in
, int flags
,
71 /* If streaming create stream BIO and copy all content through it */
72 if (flags
& SMIME_STREAM
) {
74 bio
= BIO_new_NDEF(out
, val
, it
);
76 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM
, ERR_R_MALLOC_FAILURE
);
79 SMIME_crlf_copy(in
, bio
, flags
);
81 /* Free up successive BIOs until we hit the old output BIO */
89 * else just write out ASN1 structure which will have all content stored
93 ASN1_item_i2d_bio(it
, out
, val
);
97 /* Base 64 read and write of ASN1 structure */
99 static int B64_write_ASN1(BIO
*out
, ASN1_VALUE
*val
, BIO
*in
, int flags
,
104 b64
= BIO_new(BIO_f_base64());
106 ASN1err(ASN1_F_B64_WRITE_ASN1
, ERR_R_MALLOC_FAILURE
);
110 * prepend the b64 BIO so all data is base64 encoded.
112 out
= BIO_push(b64
, out
);
113 r
= i2d_ASN1_bio_stream(out
, val
, in
, flags
, it
);
114 (void)BIO_flush(out
);
120 /* Streaming ASN1 PEM write */
122 int PEM_write_bio_ASN1_stream(BIO
*out
, ASN1_VALUE
*val
, BIO
*in
, int flags
,
123 const char *hdr
, const ASN1_ITEM
*it
)
126 BIO_printf(out
, "-----BEGIN %s-----\n", hdr
);
127 r
= B64_write_ASN1(out
, val
, in
, flags
, it
);
128 BIO_printf(out
, "-----END %s-----\n", hdr
);
132 static ASN1_VALUE
*b64_read_asn1(BIO
*bio
, const ASN1_ITEM
*it
)
137 if ((b64
= BIO_new(BIO_f_base64())) == NULL
) {
138 ASN1err(ASN1_F_B64_READ_ASN1
, ERR_R_MALLOC_FAILURE
);
141 bio
= BIO_push(b64
, bio
);
142 val
= ASN1_item_d2i_bio(it
, bio
, NULL
);
144 ASN1err(ASN1_F_B64_READ_ASN1
, ASN1_R_DECODE_ERROR
);
145 (void)BIO_flush(bio
);
151 /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
153 static int asn1_write_micalg(BIO
*out
, STACK_OF(X509_ALGOR
) *mdalgs
)
156 int i
, have_unknown
= 0, write_comma
, ret
= 0, md_nid
;
159 for (i
= 0; i
< sk_X509_ALGOR_num(mdalgs
); i
++) {
161 BIO_write(out
, ",", 1);
163 md_nid
= OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs
, i
)->algorithm
);
164 md
= EVP_get_digestbynid(md_nid
);
165 if (md
&& md
->md_ctrl
) {
168 rv
= md
->md_ctrl(NULL
, EVP_MD_CTRL_MICALG
, 0, &micstr
);
170 BIO_puts(out
, micstr
);
171 OPENSSL_free(micstr
);
179 BIO_puts(out
, "sha1");
183 BIO_puts(out
, "md5");
187 BIO_puts(out
, "sha-256");
191 BIO_puts(out
, "sha-384");
195 BIO_puts(out
, "sha-512");
198 case NID_id_GostR3411_94
:
199 BIO_puts(out
, "gostr3411-94");
202 case NID_id_GostR3411_2012_256
:
203 BIO_puts(out
, "gostr3411-2012-256");
206 case NID_id_GostR3411_2012_512
:
207 BIO_puts(out
, "gostr3411-2012-512");
214 BIO_puts(out
, "unknown");
231 int SMIME_write_ASN1(BIO
*bio
, ASN1_VALUE
*val
, BIO
*data
, int flags
,
232 int ctype_nid
, int econt_nid
,
233 STACK_OF(X509_ALGOR
) *mdalgs
, const ASN1_ITEM
*it
)
237 const char *mime_prefix
, *mime_eol
, *cname
= "smime.p7m";
238 const char *msg_type
= NULL
;
239 if (flags
& SMIME_OLDMIME
)
240 mime_prefix
= "application/x-pkcs7-";
242 mime_prefix
= "application/pkcs7-";
244 if (flags
& SMIME_CRLFEOL
)
248 if ((flags
& SMIME_DETACHED
) && data
) {
249 /* We want multipart/signed */
250 /* Generate a random boundary */
251 if (RAND_bytes((unsigned char *)bound
, 32) <= 0)
253 for (i
= 0; i
< 32; i
++) {
262 BIO_printf(bio
, "MIME-Version: 1.0%s", mime_eol
);
263 BIO_printf(bio
, "Content-Type: multipart/signed;");
264 BIO_printf(bio
, " protocol=\"%ssignature\";", mime_prefix
);
265 BIO_puts(bio
, " micalg=\"");
266 asn1_write_micalg(bio
, mdalgs
);
267 BIO_printf(bio
, "\"; boundary=\"----%s\"%s%s",
268 bound
, mime_eol
, mime_eol
);
269 BIO_printf(bio
, "This is an S/MIME signed message%s%s",
271 /* Now write out the first part */
272 BIO_printf(bio
, "------%s%s", bound
, mime_eol
);
273 if (!asn1_output_data(bio
, data
, val
, flags
, it
))
275 BIO_printf(bio
, "%s------%s%s", mime_eol
, bound
, mime_eol
);
277 /* Headers for signature */
279 BIO_printf(bio
, "Content-Type: %ssignature;", mime_prefix
);
280 BIO_printf(bio
, " name=\"smime.p7s\"%s", mime_eol
);
281 BIO_printf(bio
, "Content-Transfer-Encoding: base64%s", mime_eol
);
282 BIO_printf(bio
, "Content-Disposition: attachment;");
283 BIO_printf(bio
, " filename=\"smime.p7s\"%s%s", mime_eol
, mime_eol
);
284 B64_write_ASN1(bio
, val
, NULL
, 0, it
);
285 BIO_printf(bio
, "%s------%s--%s%s", mime_eol
, bound
,
290 /* Determine smime-type header */
292 if (ctype_nid
== NID_pkcs7_enveloped
)
293 msg_type
= "enveloped-data";
294 else if (ctype_nid
== NID_pkcs7_signed
) {
295 if (econt_nid
== NID_id_smime_ct_receipt
)
296 msg_type
= "signed-receipt";
297 else if (sk_X509_ALGOR_num(mdalgs
) >= 0)
298 msg_type
= "signed-data";
300 msg_type
= "certs-only";
301 } else if (ctype_nid
== NID_id_smime_ct_compressedData
) {
302 msg_type
= "compressed-data";
306 BIO_printf(bio
, "MIME-Version: 1.0%s", mime_eol
);
307 BIO_printf(bio
, "Content-Disposition: attachment;");
308 BIO_printf(bio
, " filename=\"%s\"%s", cname
, mime_eol
);
309 BIO_printf(bio
, "Content-Type: %smime;", mime_prefix
);
311 BIO_printf(bio
, " smime-type=%s;", msg_type
);
312 BIO_printf(bio
, " name=\"%s\"%s", cname
, mime_eol
);
313 BIO_printf(bio
, "Content-Transfer-Encoding: base64%s%s",
315 if (!B64_write_ASN1(bio
, val
, data
, flags
, it
))
317 BIO_printf(bio
, "%s", mime_eol
);
321 /* Handle output of ASN1 data */
323 /* cannot constify val because of CMS_dataFinal() */
324 static int asn1_output_data(BIO
*out
, BIO
*data
, ASN1_VALUE
*val
, int flags
,
328 const ASN1_AUX
*aux
= it
->funcs
;
329 ASN1_STREAM_ARG sarg
;
333 * If data is not detached or resigning then the output BIO is already
334 * set up to finalise when it is written through.
336 if (!(flags
& SMIME_DETACHED
) || (flags
& PKCS7_REUSE_DIGEST
)) {
337 SMIME_crlf_copy(data
, out
, flags
);
341 if (!aux
|| !aux
->asn1_cb
) {
342 ASN1err(ASN1_F_ASN1_OUTPUT_DATA
, ASN1_R_STREAMING_NOT_SUPPORTED
);
347 sarg
.ndef_bio
= NULL
;
348 sarg
.boundary
= NULL
;
350 /* Let ASN1 code prepend any needed BIOs */
352 if (aux
->asn1_cb(ASN1_OP_DETACHED_PRE
, &val
, it
, &sarg
) <= 0)
355 /* Copy data across, passing through filter BIOs for processing */
356 SMIME_crlf_copy(data
, sarg
.ndef_bio
, flags
);
358 /* Finalize structure */
359 if (aux
->asn1_cb(ASN1_OP_DETACHED_POST
, &val
, it
, &sarg
) <= 0)
362 /* Now remove any digests prepended to the BIO */
364 while (sarg
.ndef_bio
!= out
) {
365 tmpbio
= BIO_pop(sarg
.ndef_bio
);
366 BIO_free(sarg
.ndef_bio
);
367 sarg
.ndef_bio
= tmpbio
;
375 * SMIME reader: handle multipart/signed and opaque signing. in multipart
376 * case the content is placed in a memory BIO pointed to by "bcont". In
377 * opaque this is set to NULL
380 ASN1_VALUE
*SMIME_read_ASN1(BIO
*bio
, BIO
**bcont
, const ASN1_ITEM
*it
)
383 STACK_OF(MIME_HEADER
) *headers
= NULL
;
384 STACK_OF(BIO
) *parts
= NULL
;
393 if ((headers
= mime_parse_hdr(bio
)) == NULL
) {
394 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_MIME_PARSE_ERROR
);
398 if ((hdr
= mime_hdr_find(headers
, "content-type")) == NULL
399 || hdr
->value
== NULL
) {
400 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
401 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_NO_CONTENT_TYPE
);
405 /* Handle multipart/signed */
407 if (strcmp(hdr
->value
, "multipart/signed") == 0) {
408 /* Split into two parts */
409 prm
= mime_param_find(hdr
, "boundary");
410 if (prm
== NULL
|| prm
->param_value
== NULL
) {
411 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
412 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_NO_MULTIPART_BOUNDARY
);
415 ret
= multi_split(bio
, prm
->param_value
, &parts
);
416 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
417 if (!ret
|| (sk_BIO_num(parts
) != 2)) {
418 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_NO_MULTIPART_BODY_FAILURE
);
419 sk_BIO_pop_free(parts
, BIO_vfree
);
423 /* Parse the signature piece */
424 asnin
= sk_BIO_value(parts
, 1);
426 if ((headers
= mime_parse_hdr(asnin
)) == NULL
) {
427 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_MIME_SIG_PARSE_ERROR
);
428 sk_BIO_pop_free(parts
, BIO_vfree
);
432 /* Get content type */
434 if ((hdr
= mime_hdr_find(headers
, "content-type")) == NULL
435 || hdr
->value
== NULL
) {
436 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
437 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_NO_SIG_CONTENT_TYPE
);
438 sk_BIO_pop_free(parts
, BIO_vfree
);
442 if (strcmp(hdr
->value
, "application/x-pkcs7-signature") &&
443 strcmp(hdr
->value
, "application/pkcs7-signature")) {
444 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_SIG_INVALID_MIME_TYPE
);
445 ERR_add_error_data(2, "type: ", hdr
->value
);
446 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
447 sk_BIO_pop_free(parts
, BIO_vfree
);
450 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
452 if ((val
= b64_read_asn1(asnin
, it
)) == NULL
) {
453 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_ASN1_SIG_PARSE_ERROR
);
454 sk_BIO_pop_free(parts
, BIO_vfree
);
459 *bcont
= sk_BIO_value(parts
, 0);
463 sk_BIO_pop_free(parts
, BIO_vfree
);
467 /* OK, if not multipart/signed try opaque signature */
469 if (strcmp(hdr
->value
, "application/x-pkcs7-mime") &&
470 strcmp(hdr
->value
, "application/pkcs7-mime")) {
471 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_INVALID_MIME_TYPE
);
472 ERR_add_error_data(2, "type: ", hdr
->value
);
473 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
477 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
479 if ((val
= b64_read_asn1(bio
, it
)) == NULL
) {
480 ASN1err(ASN1_F_SMIME_READ_ASN1
, ASN1_R_ASN1_PARSE_ERROR
);
487 /* Copy text from one BIO to another making the output CRLF at EOL */
488 int SMIME_crlf_copy(BIO
*in
, BIO
*out
, int flags
)
493 char linebuf
[MAX_SMLEN
];
495 * Buffer output so we don't write one line at a time. This is useful
496 * when streaming as we don't end up with one OCTET STRING per line.
498 bf
= BIO_new(BIO_f_buffer());
501 out
= BIO_push(bf
, out
);
502 if (flags
& SMIME_BINARY
) {
503 while ((len
= BIO_read(in
, linebuf
, MAX_SMLEN
)) > 0)
504 BIO_write(out
, linebuf
, len
);
507 if (flags
& SMIME_TEXT
)
508 BIO_printf(out
, "Content-Type: text/plain\r\n\r\n");
509 while ((len
= BIO_gets(in
, linebuf
, MAX_SMLEN
)) > 0) {
510 eol
= strip_eol(linebuf
, &len
, flags
);
512 /* Not EOF: write out all CRLF */
513 if (flags
& SMIME_ASCIICRLF
) {
515 for (i
= 0; i
< eolcnt
; i
++)
516 BIO_write(out
, "\r\n", 2);
519 BIO_write(out
, linebuf
, len
);
521 BIO_write(out
, "\r\n", 2);
522 } else if (flags
& SMIME_ASCIICRLF
)
525 BIO_write(out
, "\r\n", 2);
528 (void)BIO_flush(out
);
534 /* Strip off headers if they are text/plain */
535 int SMIME_text(BIO
*in
, BIO
*out
)
539 STACK_OF(MIME_HEADER
) *headers
;
542 if ((headers
= mime_parse_hdr(in
)) == NULL
) {
543 ASN1err(ASN1_F_SMIME_TEXT
, ASN1_R_MIME_PARSE_ERROR
);
546 if ((hdr
= mime_hdr_find(headers
, "content-type")) == NULL
547 || hdr
->value
== NULL
) {
548 ASN1err(ASN1_F_SMIME_TEXT
, ASN1_R_MIME_NO_CONTENT_TYPE
);
549 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
552 if (strcmp(hdr
->value
, "text/plain")) {
553 ASN1err(ASN1_F_SMIME_TEXT
, ASN1_R_INVALID_MIME_TYPE
);
554 ERR_add_error_data(2, "type: ", hdr
->value
);
555 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
558 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
559 while ((len
= BIO_read(in
, iobuf
, sizeof(iobuf
))) > 0)
560 BIO_write(out
, iobuf
, len
);
567 * Split a multipart/XXX message body into component parts: result is
568 * canonical parts in a STACK of bios
571 static int multi_split(BIO
*bio
, const char *bound
, STACK_OF(BIO
) **ret
)
573 char linebuf
[MAX_SMLEN
];
575 int eol
= 0, next_eol
= 0;
577 STACK_OF(BIO
) *parts
;
578 char state
, part
, first
;
580 blen
= strlen(bound
);
584 parts
= sk_BIO_new_null();
588 while ((len
= BIO_gets(bio
, linebuf
, MAX_SMLEN
)) > 0) {
589 state
= mime_bound_check(linebuf
, len
, bound
, blen
);
593 } else if (state
== 2) {
594 if (!sk_BIO_push(parts
, bpart
)) {
600 /* Strip CR+LF from linebuf */
601 next_eol
= strip_eol(linebuf
, &len
, 0);
605 if (!sk_BIO_push(parts
, bpart
)) {
609 bpart
= BIO_new(BIO_s_mem());
612 BIO_set_mem_eof_return(bpart
, 0);
614 BIO_write(bpart
, "\r\n", 2);
617 BIO_write(bpart
, linebuf
, len
);
624 /* This is the big one: parse MIME header lines up to message body */
626 #define MIME_INVALID 0
632 #define MIME_COMMENT 6
634 static STACK_OF(MIME_HEADER
) *mime_parse_hdr(BIO
*bio
)
638 char linebuf
[MAX_SMLEN
];
639 MIME_HEADER
*mhdr
= NULL
, *new_hdr
= NULL
;
640 STACK_OF(MIME_HEADER
) *headers
;
641 int len
, state
, save_state
= 0;
643 headers
= sk_MIME_HEADER_new(mime_hdr_cmp
);
646 while ((len
= BIO_gets(bio
, linebuf
, MAX_SMLEN
)) > 0) {
647 /* If whitespace at line start then continuation line */
648 if (mhdr
&& ossl_isspace(linebuf
[0]))
653 /* Go through all characters */
654 for (p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!= '\r') && (c
!= '\n');
658 * State machine to handle MIME headers if this looks horrible
659 * that's because it *is*
667 ntmp
= strip_ends(q
);
674 mime_debug("Found End Value\n");
676 new_hdr
= mime_hdr_new(ntmp
, strip_ends(q
));
679 if (!sk_MIME_HEADER_push(headers
, new_hdr
))
686 } else if (c
== '(') {
688 state
= MIME_COMMENT
;
702 ntmp
= strip_ends(q
);
711 mime_hdr_addparam(mhdr
, ntmp
, strip_ends(q
));
714 } else if (c
== '"') {
715 mime_debug("Found Quote\n");
717 } else if (c
== '(') {
719 state
= MIME_COMMENT
;
725 mime_debug("Found Match Quote\n");
732 if (state
== MIME_TYPE
) {
733 new_hdr
= mime_hdr_new(ntmp
, strip_ends(q
));
736 if (!sk_MIME_HEADER_push(headers
, new_hdr
))
740 } else if (state
== MIME_VALUE
)
741 mime_hdr_addparam(mhdr
, ntmp
, strip_ends(q
));
743 break; /* Blank line means end of headers */
749 mime_hdr_free(new_hdr
);
750 sk_MIME_HEADER_pop_free(headers
, mime_hdr_free
);
754 static char *strip_ends(char *name
)
756 return strip_end(strip_start(name
));
759 /* Strip a parameter of whitespace from start of param */
760 static char *strip_start(char *name
)
763 /* Look for first non white space or quote */
764 for (p
= name
; (c
= *p
); p
++) {
766 /* Next char is start of string if non null */
769 /* Else null string */
772 if (!ossl_isspace(c
))
778 /* As above but strip from end of string : maybe should handle brackets? */
779 static char *strip_end(char *name
)
784 /* Look for first non white space or quote */
785 for (p
= name
+ strlen(name
) - 1; p
>= name
; p
--) {
801 static MIME_HEADER
*mime_hdr_new(const char *name
, const char *value
)
803 MIME_HEADER
*mhdr
= NULL
;
804 char *tmpname
= NULL
, *tmpval
= NULL
, *p
;
807 if ((tmpname
= OPENSSL_strdup(name
)) == NULL
)
809 for (p
= tmpname
; *p
; p
++)
810 *p
= ossl_tolower(*p
);
813 if ((tmpval
= OPENSSL_strdup(value
)) == NULL
)
815 for (p
= tmpval
; *p
; p
++)
816 *p
= ossl_tolower(*p
);
818 mhdr
= OPENSSL_malloc(sizeof(*mhdr
));
821 mhdr
->name
= tmpname
;
822 mhdr
->value
= tmpval
;
823 if ((mhdr
->params
= sk_MIME_PARAM_new(mime_param_cmp
)) == NULL
)
828 OPENSSL_free(tmpname
);
829 OPENSSL_free(tmpval
);
834 static int mime_hdr_addparam(MIME_HEADER
*mhdr
, const char *name
, const char *value
)
836 char *tmpname
= NULL
, *tmpval
= NULL
, *p
;
837 MIME_PARAM
*mparam
= NULL
;
840 tmpname
= OPENSSL_strdup(name
);
843 for (p
= tmpname
; *p
; p
++)
844 *p
= ossl_tolower(*p
);
847 tmpval
= OPENSSL_strdup(value
);
851 /* Parameter values are case sensitive so leave as is */
852 mparam
= OPENSSL_malloc(sizeof(*mparam
));
855 mparam
->param_name
= tmpname
;
856 mparam
->param_value
= tmpval
;
857 if (!sk_MIME_PARAM_push(mhdr
->params
, mparam
))
861 OPENSSL_free(tmpname
);
862 OPENSSL_free(tmpval
);
863 OPENSSL_free(mparam
);
867 static int mime_hdr_cmp(const MIME_HEADER
*const *a
,
868 const MIME_HEADER
*const *b
)
870 if (!(*a
)->name
|| !(*b
)->name
)
871 return ! !(*a
)->name
- ! !(*b
)->name
;
873 return strcmp((*a
)->name
, (*b
)->name
);
876 static int mime_param_cmp(const MIME_PARAM
*const *a
,
877 const MIME_PARAM
*const *b
)
879 if (!(*a
)->param_name
|| !(*b
)->param_name
)
880 return ! !(*a
)->param_name
- ! !(*b
)->param_name
;
881 return strcmp((*a
)->param_name
, (*b
)->param_name
);
884 /* Find a header with a given name (if possible) */
886 static MIME_HEADER
*mime_hdr_find(STACK_OF(MIME_HEADER
) *hdrs
, const char *name
)
891 htmp
.name
= (char *)name
;
895 idx
= sk_MIME_HEADER_find(hdrs
, &htmp
);
896 return sk_MIME_HEADER_value(hdrs
, idx
);
899 static MIME_PARAM
*mime_param_find(MIME_HEADER
*hdr
, const char *name
)
904 param
.param_name
= (char *)name
;
905 param
.param_value
= NULL
;
906 idx
= sk_MIME_PARAM_find(hdr
->params
, ¶m
);
907 return sk_MIME_PARAM_value(hdr
->params
, idx
);
910 static void mime_hdr_free(MIME_HEADER
*hdr
)
914 OPENSSL_free(hdr
->name
);
915 OPENSSL_free(hdr
->value
);
917 sk_MIME_PARAM_pop_free(hdr
->params
, mime_param_free
);
921 static void mime_param_free(MIME_PARAM
*param
)
923 OPENSSL_free(param
->param_name
);
924 OPENSSL_free(param
->param_value
);
929 * Check for a multipart boundary. Returns:
934 static int mime_bound_check(char *line
, int linelen
, const char *bound
, int blen
)
937 linelen
= strlen(line
);
939 blen
= strlen(bound
);
940 /* Quickly eliminate if line length too short */
941 if (blen
+ 2 > linelen
)
943 /* Check for part boundary */
944 if ((strncmp(line
, "--", 2) == 0)
945 && strncmp(line
+ 2, bound
, blen
) == 0) {
946 if (strncmp(line
+ blen
+ 2, "--", 2) == 0)
954 static int strip_eol(char *linebuf
, int *plen
, int flags
)
960 for (p
= linebuf
+ len
- 1; len
> 0; len
--, p
--) {
964 } else if (is_eol
&& flags
& SMIME_ASCIICRLF
&& c
== 32) {
965 /* Strip trailing space on a line; 32 == ASCII for ' ' */
967 } else if (c
!= '\r') {