1 /* $OpenBSD: asn_mime.c,v 1.25 2015/02/10 11:22:21 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2008 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 * ====================================================================
60 #include <openssl/asn1.h>
61 #include <openssl/asn1t.h>
62 #include <openssl/err.h>
63 #include <openssl/x509.h>
65 #include "asn1_locl.h"
67 /* Generalised MIME like utilities for streaming ASN1. Although many
68 * have a PKCS7/CMS like flavour others are more general purpose.
71 /* MIME format structures
72 * Note that all are translated to lower case apart from
73 * parameter values. Quotes are stripped off
77 char *param_name; /* Param name e.g. "micalg" */
78 char *param_value; /* Param value e.g. "sha1" */
81 DECLARE_STACK_OF(MIME_PARAM)
84 char *name; /* Name of line e.g. "content-type" */
85 char *value; /* Value of line e.g. "text/plain" */
86 STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
89 DECLARE_STACK_OF(MIME_HEADER)
91 static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
93 static char * strip_ends(char *name);
94 static char * strip_start(char *name);
95 static char * strip_end(char *name);
96 static MIME_HEADER *mime_hdr_new(char *name, char *value);
97 static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
98 static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
99 static int mime_hdr_cmp(const MIME_HEADER * const *a,
100 const MIME_HEADER * const *b);
101 static int mime_param_cmp(const MIME_PARAM * const *a,
102 const MIME_PARAM * const *b);
103 static void mime_param_free(MIME_PARAM *param);
104 static int mime_bound_check(char *line, int linelen, char *bound, int blen);
105 static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
106 static int strip_eol(char *linebuf, int *plen);
107 static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
108 static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
109 static void mime_hdr_free(MIME_HEADER *hdr);
111 #define MAX_SMLEN 1024
112 #define mime_debug(x) /* x */
114 /* Output an ASN1 structure in BER format streaming if necessary */
117 i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
120 /* If streaming create stream BIO and copy all content through it */
121 if (flags & SMIME_STREAM) {
123 bio = BIO_new_NDEF(out, val, it);
125 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,
126 ERR_R_MALLOC_FAILURE);
129 SMIME_crlf_copy(in, bio, flags);
130 (void)BIO_flush(bio);
131 /* Free up successive BIOs until we hit the old output BIO */
136 } while (bio != out);
138 /* else just write out ASN1 structure which will have all content
142 ASN1_item_i2d_bio(it, out, val);
146 /* Base 64 read and write of ASN1 structure */
149 B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
155 b64 = BIO_new(BIO_f_base64());
157 ASN1err(ASN1_F_B64_WRITE_ASN1, ERR_R_MALLOC_FAILURE);
160 /* prepend the b64 BIO so all data is base64 encoded.
162 out = BIO_push(b64, out);
163 r = i2d_ASN1_bio_stream(out, val, in, flags, it);
164 (void)BIO_flush(out);
170 /* Streaming ASN1 PEM write */
173 PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
174 const char *hdr, const ASN1_ITEM *it)
178 BIO_printf(out, "-----BEGIN %s-----\n", hdr);
179 r = B64_write_ASN1(out, val, in, flags, it);
180 BIO_printf(out, "-----END %s-----\n", hdr);
185 b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
189 if (!(b64 = BIO_new(BIO_f_base64()))) {
190 ASN1err(ASN1_F_B64_READ_ASN1, ERR_R_MALLOC_FAILURE);
193 bio = BIO_push(b64, bio);
194 val = ASN1_item_d2i_bio(it, bio, NULL);
196 ASN1err(ASN1_F_B64_READ_ASN1, ASN1_R_DECODE_ERROR);
197 (void)BIO_flush(bio);
203 /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
206 asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
209 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
213 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) {
215 BIO_write(out, ",", 1);
217 md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
218 md = EVP_get_digestbynid(md_nid);
219 if (md && md->md_ctrl) {
222 rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
224 BIO_puts(out, micstr);
233 BIO_puts(out, "sha1");
237 BIO_puts(out, "md5");
241 BIO_puts(out, "sha-256");
245 BIO_puts(out, "sha-384");
249 BIO_puts(out, "sha-512");
252 case NID_id_GostR3411_94:
253 BIO_puts(out, "gostr3411-94");
261 BIO_puts(out, "unknown");
278 SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
279 int ctype_nid, int econt_nid, STACK_OF(X509_ALGOR) *mdalgs,
284 const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
285 const char *msg_type = NULL;
287 if (flags & SMIME_OLDMIME)
288 mime_prefix = "application/x-pkcs7-";
290 mime_prefix = "application/pkcs7-";
292 if (flags & SMIME_CRLFEOL)
296 if ((flags & SMIME_DETACHED) && data) {
297 /* We want multipart/signed */
298 /* Generate a random boundary */
299 arc4random_buf(bound, 32);
300 for (i = 0; i < 32; i++) {
309 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
310 BIO_printf(bio, "Content-Type: multipart/signed;");
311 BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
312 BIO_puts(bio, " micalg=\"");
313 asn1_write_micalg(bio, mdalgs);
314 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
315 bound, mime_eol, mime_eol);
316 BIO_printf(bio, "This is an S/MIME signed message%s%s",
318 /* Now write out the first part */
319 BIO_printf(bio, "------%s%s", bound, mime_eol);
320 if (!asn1_output_data(bio, data, val, flags, it))
322 BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
324 /* Headers for signature */
326 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
327 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
328 BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
330 BIO_printf(bio, "Content-Disposition: attachment;");
331 BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
333 B64_write_ASN1(bio, val, NULL, 0, it);
334 BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound,
339 /* Determine smime-type header */
341 if (ctype_nid == NID_pkcs7_enveloped)
342 msg_type = "enveloped-data";
343 else if (ctype_nid == NID_pkcs7_signed) {
344 if (econt_nid == NID_id_smime_ct_receipt)
345 msg_type = "signed-receipt";
346 else if (sk_X509_ALGOR_num(mdalgs) >= 0)
347 msg_type = "signed-data";
349 msg_type = "certs-only";
350 } else if (ctype_nid == NID_id_smime_ct_compressedData) {
351 msg_type = "compressed-data";
355 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
356 BIO_printf(bio, "Content-Disposition: attachment;");
357 BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
358 BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
360 BIO_printf(bio, " smime-type=%s;", msg_type);
361 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
362 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
364 if (!B64_write_ASN1(bio, val, data, flags, it))
366 BIO_printf(bio, "%s", mime_eol);
370 /* Handle output of ASN1 data */
374 asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
378 const ASN1_AUX *aux = it->funcs;
379 ASN1_STREAM_ARG sarg;
382 /* If data is not deteched or resigning then the output BIO is
383 * already set up to finalise when it is written through.
385 if (!(flags & SMIME_DETACHED) || (flags & PKCS7_REUSE_DIGEST)) {
386 SMIME_crlf_copy(data, out, flags);
390 if (!aux || !aux->asn1_cb) {
391 ASN1err(ASN1_F_ASN1_OUTPUT_DATA,
392 ASN1_R_STREAMING_NOT_SUPPORTED);
397 sarg.ndef_bio = NULL;
398 sarg.boundary = NULL;
400 /* Let ASN1 code prepend any needed BIOs */
402 if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
405 /* Copy data across, passing through filter BIOs for processing */
406 SMIME_crlf_copy(data, sarg.ndef_bio, flags);
408 /* Finalize structure */
409 if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
412 /* Now remove any digests prepended to the BIO */
414 while (sarg.ndef_bio != out) {
415 tmpbio = BIO_pop(sarg.ndef_bio);
416 BIO_free(sarg.ndef_bio);
417 sarg.ndef_bio = tmpbio;
423 /* SMIME reader: handle multipart/signed and opaque signing.
424 * in multipart case the content is placed in a memory BIO
425 * pointed to by "bcont". In opaque this is set to NULL
429 SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
432 STACK_OF(MIME_HEADER) *headers = NULL;
433 STACK_OF(BIO) *parts = NULL;
442 if (!(headers = mime_parse_hdr(bio))) {
443 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_PARSE_ERROR);
447 if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
448 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
449 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
453 /* Handle multipart/signed */
455 if (!strcmp(hdr->value, "multipart/signed")) {
456 /* Split into two parts */
457 prm = mime_param_find(hdr, "boundary");
458 if (!prm || !prm->param_value) {
459 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
460 ASN1err(ASN1_F_SMIME_READ_ASN1,
461 ASN1_R_NO_MULTIPART_BOUNDARY);
464 ret = multi_split(bio, prm->param_value, &parts);
465 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
466 if (!ret || (sk_BIO_num(parts) != 2) ) {
467 ASN1err(ASN1_F_SMIME_READ_ASN1,
468 ASN1_R_NO_MULTIPART_BODY_FAILURE);
469 sk_BIO_pop_free(parts, BIO_vfree);
473 /* Parse the signature piece */
474 asnin = sk_BIO_value(parts, 1);
476 if (!(headers = mime_parse_hdr(asnin))) {
477 ASN1err(ASN1_F_SMIME_READ_ASN1,
478 ASN1_R_MIME_SIG_PARSE_ERROR);
479 sk_BIO_pop_free(parts, BIO_vfree);
483 /* Get content type */
485 if (!(hdr = mime_hdr_find(headers, "content-type")) ||
487 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
488 sk_BIO_pop_free(parts, BIO_vfree);
489 ASN1err(ASN1_F_SMIME_READ_ASN1,
490 ASN1_R_NO_SIG_CONTENT_TYPE);
494 if (strcmp(hdr->value, "application/x-pkcs7-signature") &&
495 strcmp(hdr->value, "application/pkcs7-signature")) {
496 ASN1err(ASN1_F_SMIME_READ_ASN1,
497 ASN1_R_SIG_INVALID_MIME_TYPE);
498 ERR_asprintf_error_data("type: %s", hdr->value);
499 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
500 sk_BIO_pop_free(parts, BIO_vfree);
503 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
505 if (!(val = b64_read_asn1(asnin, it))) {
506 ASN1err(ASN1_F_SMIME_READ_ASN1,
507 ASN1_R_ASN1_SIG_PARSE_ERROR);
508 sk_BIO_pop_free(parts, BIO_vfree);
513 *bcont = sk_BIO_value(parts, 0);
516 } else sk_BIO_pop_free(parts, BIO_vfree);
520 /* OK, if not multipart/signed try opaque signature */
522 if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
523 strcmp (hdr->value, "application/pkcs7-mime")) {
524 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_INVALID_MIME_TYPE);
525 ERR_asprintf_error_data("type: %s", hdr->value);
526 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
530 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
532 if (!(val = b64_read_asn1(bio, it))) {
533 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
539 /* Copy text from one BIO to another making the output CRLF at EOL */
541 SMIME_crlf_copy(BIO *in, BIO *out, int flags)
546 char linebuf[MAX_SMLEN];
548 /* Buffer output so we don't write one line at a time. This is
549 * useful when streaming as we don't end up with one OCTET STRING
552 bf = BIO_new(BIO_f_buffer());
555 out = BIO_push(bf, out);
556 if (flags & SMIME_BINARY) {
557 while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
558 BIO_write(out, linebuf, len);
560 if (flags & SMIME_TEXT)
561 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
562 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
563 eol = strip_eol(linebuf, &len);
565 BIO_write(out, linebuf, len);
567 BIO_write(out, "\r\n", 2);
570 (void)BIO_flush(out);
576 /* Strip off headers if they are text/plain */
578 SMIME_text(BIO *in, BIO *out)
582 STACK_OF(MIME_HEADER) *headers;
585 if (!(headers = mime_parse_hdr(in))) {
586 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_PARSE_ERROR);
589 if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
590 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_NO_CONTENT_TYPE);
591 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
594 if (strcmp (hdr->value, "text/plain")) {
595 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_INVALID_MIME_TYPE);
596 ERR_asprintf_error_data("type: %s", hdr->value);
597 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
600 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
601 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
602 BIO_write(out, iobuf, len);
609 * Split a multipart/XXX message body into component parts: result is
610 * canonical parts in a STACK of bios
613 multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
615 char linebuf[MAX_SMLEN];
617 int eol = 0, next_eol = 0;
619 STACK_OF(BIO) *parts;
620 char state, part, first;
622 blen = strlen(bound);
626 parts = sk_BIO_new_null();
630 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
631 state = mime_bound_check(linebuf, len, bound, blen);
635 } else if (state == 2) {
636 if (sk_BIO_push(parts, bpart) == 0)
640 /* Strip CR+LF from linebuf */
641 next_eol = strip_eol(linebuf, &len);
645 if (sk_BIO_push(parts, bpart) == 0)
648 bpart = BIO_new(BIO_s_mem());
651 BIO_set_mem_eof_return(bpart, 0);
653 BIO_write(bpart, "\r\n", 2);
656 BIO_write(bpart, linebuf, len);
663 /* This is the big one: parse MIME header lines up to message body */
665 #define MIME_INVALID 0
671 #define MIME_COMMENT 6
675 STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
679 char linebuf[MAX_SMLEN];
680 MIME_HEADER *mhdr = NULL;
681 STACK_OF(MIME_HEADER) *headers;
682 int len, state, save_state = 0;
684 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
687 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
688 /* If whitespace at line start then continuation line */
689 if (mhdr && isspace((unsigned char)linebuf[0]))
695 /* Go through all characters */
696 for (p = linebuf, q = linebuf;
697 (c = *p) && (c != '\r') && (c != '\n'); p++) {
699 /* State machine to handle MIME headers
700 * if this looks horrible that's because it *is*
708 ntmp = strip_ends(q);
715 mime_debug("Found End Value\n");
717 mhdr = mime_hdr_new(ntmp,
721 if (sk_MIME_HEADER_push(headers,
727 } else if (c == '(') {
729 state = MIME_COMMENT;
743 ntmp = strip_ends(q);
752 mime_hdr_addparam(mhdr, ntmp,
756 } else if (c == '"') {
757 mime_debug("Found Quote\n");
759 } else if (c == '(') {
761 state = MIME_COMMENT;
767 mime_debug("Found Match Quote\n");
774 if (state == MIME_TYPE) {
775 mhdr = mime_hdr_new(ntmp, strip_ends(q));
778 if (sk_MIME_HEADER_push(headers, mhdr) == 0)
780 } else if (state == MIME_VALUE)
781 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
784 break; /* Blank line means end of headers */
792 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
797 strip_ends(char *name)
799 return strip_end(strip_start(name));
802 /* Strip a parameter of whitespace from start of param */
804 strip_start(char *name)
808 /* Look for first non white space or quote */
809 for (p = name; (c = *p); p++) {
811 /* Next char is start of string if non null */
814 /* Else null string */
817 if (!isspace((unsigned char)c))
823 /* As above but strip from end of string : maybe should handle brackets? */
825 strip_end(char *name)
832 /* Look for first non white space or quote */
833 for (p = name + strlen(name) - 1; p >= name; p--) {
841 if (isspace((unsigned char)c))
850 mime_hdr_new(char *name, char *value)
853 char *tmpname = NULL, *tmpval = NULL, *p;
856 if (!(tmpname = strdup(name)))
858 for (p = tmpname; *p; p++)
859 *p = tolower((unsigned char)*p);
862 if (!(tmpval = strdup(value)))
864 for (p = tmpval; *p; p++)
865 *p = tolower((unsigned char)*p);
867 mhdr = malloc(sizeof(MIME_HEADER));
870 mhdr->name = tmpname;
871 mhdr->value = tmpval;
872 if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) {
884 mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
886 char *tmpname = NULL, *tmpval = NULL, *p;
890 tmpname = strdup(name);
893 for (p = tmpname; *p; p++)
894 *p = tolower((unsigned char)*p);
897 tmpval = strdup(value);
901 /* Parameter values are case sensitive so leave as is */
902 mparam = malloc(sizeof(MIME_PARAM));
905 mparam->param_name = tmpname;
906 mparam->param_value = tmpval;
907 if (sk_MIME_PARAM_push(mhdr->params, mparam) == 0) {
919 mime_hdr_cmp(const MIME_HEADER * const *a, const MIME_HEADER * const *b)
921 if (!(*a)->name || !(*b)->name)
922 return !!(*a)->name - !!(*b)->name;
923 return (strcmp((*a)->name, (*b)->name));
927 mime_param_cmp(const MIME_PARAM * const *a, const MIME_PARAM * const *b)
929 if (!(*a)->param_name || !(*b)->param_name)
930 return !!(*a)->param_name - !!(*b)->param_name;
931 return (strcmp((*a)->param_name, (*b)->param_name));
934 /* Find a header with a given name (if possible) */
937 mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
942 idx = sk_MIME_HEADER_find(hdrs, &htmp);
945 return sk_MIME_HEADER_value(hdrs, idx);
949 mime_param_find(MIME_HEADER *hdr, char *name)
953 param.param_name = name;
954 idx = sk_MIME_PARAM_find(hdr->params, ¶m);
957 return sk_MIME_PARAM_value(hdr->params, idx);
961 mime_hdr_free(MIME_HEADER *hdr)
966 sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
971 mime_param_free(MIME_PARAM *param)
973 free(param->param_name);
974 free(param->param_value);
978 /* Check for a multipart boundary. Returns:
984 mime_bound_check(char *line, int linelen, char *bound, int blen)
987 linelen = strlen(line);
989 blen = strlen(bound);
990 /* Quickly eliminate if line length too short */
991 if (blen + 2 > linelen)
993 /* Check for part boundary */
994 if (!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
995 if (!strncmp(line + blen + 2, "--", 2))
1004 strip_eol(char *linebuf, int *plen)
1010 for (p = linebuf + len - 1; len > 0; len--, p--) {