2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/rand.h>
63 #include <openssl/x509.h>
65 /* MIME and related routines */
67 /* MIME format structures
68 * Note that all are translated to lower case apart from
69 * parameter values. Quotes are stripped off
73 char *param_name; /* Param name e.g. "micalg" */
74 char *param_value; /* Param value e.g. "sha1" */
77 DECLARE_STACK_OF(MIME_PARAM)
78 IMPLEMENT_STACK_OF(MIME_PARAM)
81 char *name; /* Name of line e.g. "content-type" */
82 char *value; /* Value of line e.g. "text/plain" */
83 STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
86 DECLARE_STACK_OF(MIME_HEADER)
87 IMPLEMENT_STACK_OF(MIME_HEADER)
89 static int pkcs7_output_data(BIO *bio, BIO *data, PKCS7 *p7, int flags);
90 static int B64_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *in, int flags);
91 static PKCS7 *B64_read_PKCS7(BIO *bio);
92 static char * strip_ends(char *name);
93 static char * strip_start(char *name);
94 static char * strip_end(char *name);
95 static MIME_HEADER *mime_hdr_new(char *name, char *value);
96 static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
97 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
98 static int mime_hdr_cmp(const MIME_HEADER * const *a,
99 const MIME_HEADER * const *b);
100 static int mime_param_cmp(const MIME_PARAM * const *a,
101 const MIME_PARAM * const *b);
102 static void mime_param_free(MIME_PARAM *param);
103 static int mime_bound_check(char *line, int linelen, char *bound, int blen);
104 static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
105 static int strip_eol(char *linebuf, int *plen);
106 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
107 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
108 static void mime_hdr_free(MIME_HEADER *hdr);
110 #define MAX_SMLEN 1024
111 #define mime_debug(x) /* x */
113 /* Output a PKCS#7 structure in BER format streaming if necessary */
115 int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags)
117 /* If streaming create stream BIO and copy all content through it */
118 if (flags & PKCS7_STREAM)
121 bio = BIO_new_PKCS7(out, p7);
124 PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE);
127 SMIME_crlf_copy(in, bio, flags);
129 /* Free up successive BIOs until we hit the old output BIO */
135 } while (bio != out);
137 /* else just write out PKCS7 structure which will have all content
141 i2d_PKCS7_bio(out, p7);
145 /* Base 64 read and write of PKCS#7 structure */
147 static int B64_write_PKCS7(BIO *out, PKCS7 *p7, BIO *in, int flags)
151 b64 = BIO_new(BIO_f_base64());
154 PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE);
157 /* prepend the b64 BIO so all data is base64 encoded.
159 out = BIO_push(b64, out);
160 r = i2d_PKCS7_bio_stream(out, p7, in, flags);
166 static PKCS7 *B64_read_PKCS7(BIO *bio)
170 if(!(b64 = BIO_new(BIO_f_base64()))) {
171 PKCS7err(PKCS7_F_B64_READ_PKCS7,ERR_R_MALLOC_FAILURE);
174 bio = BIO_push(b64, bio);
175 if(!(p7 = d2i_PKCS7_bio(bio, NULL)))
176 PKCS7err(PKCS7_F_B64_READ_PKCS7,PKCS7_R_DECODE_ERROR);
183 /* Streaming PKCS#7 PEM write */
185 int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags)
188 BIO_puts(out, "-----BEGIN PKCS7-----\n");
189 r = B64_write_PKCS7(out, p7, in, flags);
190 BIO_puts(out, "-----END PKCS7-----\n");
194 /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
196 static int pk7_write_micalg(BIO *out, PKCS7 *p7)
198 STACK_OF(X509_ALGOR) *mdalgs;
200 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
201 mdalgs = p7->d.sign->md_algs;
204 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++)
207 BIO_write(out, ",", 1);
209 md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
210 md = EVP_get_digestbynid(md_nid);
211 if (md && md->md_ctrl)
215 rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
218 BIO_puts(out, micstr);
219 OPENSSL_free(micstr);
228 BIO_puts(out, "sha1");
232 BIO_puts(out, "md5");
236 BIO_puts(out, "sha-256");
240 BIO_puts(out, "sha-384");
244 BIO_puts(out, "sha-512");
247 case NID_id_GostR3411_94:
248 BIO_puts(out, "gostr3411-94");
257 BIO_puts(out, "unknown");
277 int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags)
281 char *mime_prefix, *mime_eol, *msg_type=NULL;
282 if (flags & PKCS7_NOOLDMIMETYPE)
283 mime_prefix = "application/pkcs7-";
285 mime_prefix = "application/x-pkcs7-";
287 if (flags & PKCS7_CRLFEOL)
291 if((flags & PKCS7_DETACHED) && data) {
292 /* We want multipart/signed */
293 /* Generate a random boundary */
294 RAND_pseudo_bytes((unsigned char *)bound, 32);
295 for(i = 0; i < 32; i++) {
302 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
303 BIO_printf(bio, "Content-Type: multipart/signed;");
304 BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
305 BIO_puts(bio, " micalg=\"");
306 pk7_write_micalg(bio, p7);
307 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
308 bound, mime_eol, mime_eol);
309 BIO_printf(bio, "This is an S/MIME signed message%s%s",
311 /* Now write out the first part */
312 BIO_printf(bio, "------%s%s", bound, mime_eol);
313 pkcs7_output_data(bio, data, p7, flags);
314 BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
316 /* Headers for signature */
318 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
319 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
320 BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
322 BIO_printf(bio, "Content-Disposition: attachment;");
323 BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
325 B64_write_PKCS7(bio, p7, NULL, 0);
326 BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound,
331 /* Determine smime-type header */
333 if (PKCS7_type_is_enveloped(p7))
334 msg_type = "enveloped-data";
335 else if (PKCS7_type_is_signed(p7))
337 /* If we have any signers it is signed-data otherwise
340 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
341 sinfos = PKCS7_get_signer_info(p7);
342 if (sk_PKCS7_SIGNER_INFO_num(sinfos) > 0)
343 msg_type = "signed-data";
345 msg_type = "certs-only";
348 flags &= ~PKCS7_STREAM;
350 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
351 BIO_printf(bio, "Content-Disposition: attachment;");
352 BIO_printf(bio, " filename=\"smime.p7m\"%s", mime_eol);
353 BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
355 BIO_printf(bio, " smime-type=%s;", msg_type);
356 BIO_printf(bio, " name=\"smime.p7m\"%s", mime_eol);
357 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
359 B64_write_PKCS7(bio, p7, data, flags);
360 BIO_printf(bio, "%s", mime_eol);
364 /* Handle output of PKCS#7 data */
367 static int pkcs7_output_data(BIO *out, BIO *data, PKCS7 *p7, int flags)
371 if (!(flags & PKCS7_STREAM))
373 SMIME_crlf_copy(data, out, flags);
377 /* Partial sign operation */
379 /* Initialize sign operation */
380 p7bio = PKCS7_dataInit(p7, out);
382 /* Copy data across, computing digests etc */
383 SMIME_crlf_copy(data, p7bio, flags);
385 /* Must be detached */
386 PKCS7_set_detached(p7, 1);
388 /* Finalize signatures */
389 PKCS7_dataFinal(p7, p7bio);
391 /* Now remove any digests prepended to the BIO */
395 tmpbio = BIO_pop(p7bio);
404 /* SMIME reader: handle multipart/signed and opaque signing.
405 * in multipart case the content is placed in a memory BIO
406 * pointed to by "bcont". In opaque this is set to NULL
409 PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont)
412 STACK_OF(MIME_HEADER) *headers = NULL;
413 STACK_OF(BIO) *parts = NULL;
419 if(bcont) *bcont = NULL;
421 if (!(headers = mime_parse_hdr(bio))) {
422 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR);
426 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
427 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
428 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE);
432 /* Handle multipart/signed */
434 if(!strcmp(hdr->value, "multipart/signed")) {
435 /* Split into two parts */
436 prm = mime_param_find(hdr, "boundary");
437 if(!prm || !prm->param_value) {
438 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
439 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY);
442 ret = multi_split(bio, prm->param_value, &parts);
443 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
444 if(!ret || (sk_BIO_num(parts) != 2) ) {
445 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE);
446 sk_BIO_pop_free(parts, BIO_vfree);
450 /* Parse the signature piece */
451 p7in = sk_BIO_value(parts, 1);
453 if (!(headers = mime_parse_hdr(p7in))) {
454 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR);
455 sk_BIO_pop_free(parts, BIO_vfree);
459 /* Get content type */
461 if(!(hdr = mime_hdr_find(headers, "content-type")) ||
463 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
464 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE);
468 if(strcmp(hdr->value, "application/x-pkcs7-signature") &&
469 strcmp(hdr->value, "application/pkcs7-signature")) {
470 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
471 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE);
472 ERR_add_error_data(2, "type: ", hdr->value);
473 sk_BIO_pop_free(parts, BIO_vfree);
476 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
478 if(!(p7 = B64_read_PKCS7(p7in))) {
479 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR);
480 sk_BIO_pop_free(parts, BIO_vfree);
485 *bcont = sk_BIO_value(parts, 0);
488 } else sk_BIO_pop_free(parts, BIO_vfree);
492 /* OK, if not multipart/signed try opaque signature */
494 if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
495 strcmp (hdr->value, "application/pkcs7-mime")) {
496 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE);
497 ERR_add_error_data(2, "type: ", hdr->value);
498 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
502 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
504 if(!(p7 = B64_read_PKCS7(bio))) {
505 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR);
512 /* Copy text from one BIO to another making the output CRLF at EOL */
513 int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
518 char linebuf[MAX_SMLEN];
519 /* Buffer output so we don't write one line at a time. This is
520 * useful when streaming as we don't end up with one OCTET STRING
523 bf = BIO_new(BIO_f_buffer());
526 out = BIO_push(bf, out);
527 if(flags & PKCS7_BINARY)
529 while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
530 BIO_write(out, linebuf, len);
534 if(flags & PKCS7_TEXT)
535 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
536 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0)
538 eol = strip_eol(linebuf, &len);
540 BIO_write(out, linebuf, len);
541 if(eol) BIO_write(out, "\r\n", 2);
550 /* Strip off headers if they are text/plain */
551 int SMIME_text(BIO *in, BIO *out)
555 STACK_OF(MIME_HEADER) *headers;
558 if (!(headers = mime_parse_hdr(in))) {
559 PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_PARSE_ERROR);
562 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
563 PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_NO_CONTENT_TYPE);
564 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
567 if (strcmp (hdr->value, "text/plain")) {
568 PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_INVALID_MIME_TYPE);
569 ERR_add_error_data(2, "type: ", hdr->value);
570 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
573 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
574 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
575 BIO_write(out, iobuf, len);
579 /* Split a multipart/XXX message body into component parts: result is
580 * canonical parts in a STACK of bios
583 static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
585 char linebuf[MAX_SMLEN];
587 int eol = 0, next_eol = 0;
589 STACK_OF(BIO) *parts;
590 char state, part, first;
592 blen = strlen(bound);
596 parts = sk_BIO_new_null();
598 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
599 state = mime_bound_check(linebuf, len, bound, blen);
603 } else if(state == 2) {
604 sk_BIO_push(parts, bpart);
607 /* Strip CR+LF from linebuf */
608 next_eol = strip_eol(linebuf, &len);
611 if(bpart) sk_BIO_push(parts, bpart);
612 bpart = BIO_new(BIO_s_mem());
613 BIO_set_mem_eof_return(bpart, 0);
615 BIO_write(bpart, "\r\n", 2);
618 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
635 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
639 char linebuf[MAX_SMLEN];
640 MIME_HEADER *mhdr = NULL;
641 STACK_OF(MIME_HEADER) *headers;
642 int len, state, save_state = 0;
644 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
645 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
646 /* If whitespace at line start then continuation line */
647 if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME;
648 else state = MIME_START;
650 /* Go through all characters */
651 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
653 /* State machine to handle MIME headers
654 * if this looks horrible that's because it *is*
662 ntmp = strip_ends(q);
669 mime_debug("Found End Value\n");
671 mhdr = mime_hdr_new(ntmp, strip_ends(q));
672 sk_MIME_HEADER_push(headers, mhdr);
676 } else if(c == '(') {
678 state = MIME_COMMENT;
692 ntmp = strip_ends(q);
701 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
704 } else if (c == '"') {
705 mime_debug("Found Quote\n");
707 } else if(c == '(') {
709 state = MIME_COMMENT;
715 mime_debug("Found Match Quote\n");
722 if(state == MIME_TYPE) {
723 mhdr = mime_hdr_new(ntmp, strip_ends(q));
724 sk_MIME_HEADER_push(headers, mhdr);
725 } else if(state == MIME_VALUE)
726 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
727 if(p == linebuf) break; /* Blank line means end of headers */
734 static char *strip_ends(char *name)
736 return strip_end(strip_start(name));
739 /* Strip a parameter of whitespace from start of param */
740 static char *strip_start(char *name)
743 /* Look for first non white space or quote */
744 for(p = name; (c = *p) ;p++) {
746 /* Next char is start of string if non null */
747 if(p[1]) return p + 1;
748 /* Else null string */
751 if(!isspace((unsigned char)c)) return p;
756 /* As above but strip from end of string : maybe should handle brackets? */
757 static char *strip_end(char *name)
760 if(!name) return NULL;
761 /* Look for first non white space or quote */
762 for(p = name + strlen(name) - 1; p >= name ;p--) {
765 if(p - 1 == name) return NULL;
769 if(isspace((unsigned char)c)) *p = 0;
775 static MIME_HEADER *mime_hdr_new(char *name, char *value)
778 char *tmpname, *tmpval, *p;
781 if(!(tmpname = BUF_strdup(name))) return NULL;
782 for(p = tmpname ; *p; p++) {
789 } else tmpname = NULL;
791 if(!(tmpval = BUF_strdup(value))) return NULL;
792 for(p = tmpval ; *p; p++) {
799 } else tmpval = NULL;
800 mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER));
801 if(!mhdr) return NULL;
802 mhdr->name = tmpname;
803 mhdr->value = tmpval;
804 if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL;
808 static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
810 char *tmpname, *tmpval, *p;
814 tmpname = BUF_strdup(name);
815 if(!tmpname) return 0;
816 for(p = tmpname ; *p; p++) {
823 } else tmpname = NULL;
825 tmpval = BUF_strdup(value);
826 if(!tmpval) return 0;
827 } else tmpval = NULL;
828 /* Parameter values are case sensitive so leave as is */
829 mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
830 if(!mparam) return 0;
831 mparam->param_name = tmpname;
832 mparam->param_value = tmpval;
833 sk_MIME_PARAM_push(mhdr->params, mparam);
837 static int mime_hdr_cmp(const MIME_HEADER * const *a,
838 const MIME_HEADER * const *b)
840 return(strcmp((*a)->name, (*b)->name));
843 static int mime_param_cmp(const MIME_PARAM * const *a,
844 const MIME_PARAM * const *b)
846 return(strcmp((*a)->param_name, (*b)->param_name));
849 /* Find a header with a given name (if possible) */
851 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
856 idx = sk_MIME_HEADER_find(hdrs, &htmp);
857 if(idx < 0) return NULL;
858 return sk_MIME_HEADER_value(hdrs, idx);
861 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
865 param.param_name = name;
866 idx = sk_MIME_PARAM_find(hdr->params, ¶m);
867 if(idx < 0) return NULL;
868 return sk_MIME_PARAM_value(hdr->params, idx);
871 static void mime_hdr_free(MIME_HEADER *hdr)
873 if(hdr->name) OPENSSL_free(hdr->name);
874 if(hdr->value) OPENSSL_free(hdr->value);
875 if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
879 static void mime_param_free(MIME_PARAM *param)
881 if(param->param_name) OPENSSL_free(param->param_name);
882 if(param->param_value) OPENSSL_free(param->param_value);
886 /* Check for a multipart boundary. Returns:
891 static int mime_bound_check(char *line, int linelen, char *bound, int blen)
893 if(linelen == -1) linelen = strlen(line);
894 if(blen == -1) blen = strlen(bound);
895 /* Quickly eliminate if line length too short */
896 if(blen + 2 > linelen) return 0;
897 /* Check for part boundary */
898 if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
899 if(!strncmp(line + blen + 2, "--", 2)) return 2;
905 static int strip_eol(char *linebuf, int *plen)
910 p = linebuf + len - 1;
911 for (p = linebuf + len - 1; len > 0; len--, p--)