10Sstevel@tonic-gate /* pk7_mime.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*2139Sjp161948 * project.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <ctype.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/rand.h>
630Sstevel@tonic-gate #include <openssl/x509.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate /* MIME and related routines */
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* MIME format structures
680Sstevel@tonic-gate * Note that all are translated to lower case apart from
690Sstevel@tonic-gate * parameter values. Quotes are stripped off
700Sstevel@tonic-gate */
710Sstevel@tonic-gate
720Sstevel@tonic-gate typedef struct {
730Sstevel@tonic-gate char *param_name; /* Param name e.g. "micalg" */
740Sstevel@tonic-gate char *param_value; /* Param value e.g. "sha1" */
750Sstevel@tonic-gate } MIME_PARAM;
760Sstevel@tonic-gate
770Sstevel@tonic-gate DECLARE_STACK_OF(MIME_PARAM)
780Sstevel@tonic-gate IMPLEMENT_STACK_OF(MIME_PARAM)
790Sstevel@tonic-gate
800Sstevel@tonic-gate typedef struct {
810Sstevel@tonic-gate char *name; /* Name of line e.g. "content-type" */
820Sstevel@tonic-gate char *value; /* Value of line e.g. "text/plain" */
830Sstevel@tonic-gate STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
840Sstevel@tonic-gate } MIME_HEADER;
850Sstevel@tonic-gate
860Sstevel@tonic-gate DECLARE_STACK_OF(MIME_HEADER)
870Sstevel@tonic-gate IMPLEMENT_STACK_OF(MIME_HEADER)
880Sstevel@tonic-gate
89*2139Sjp161948 static int pkcs7_output_data(BIO *bio, BIO *data, PKCS7 *p7, int flags);
900Sstevel@tonic-gate static int B64_write_PKCS7(BIO *bio, PKCS7 *p7);
910Sstevel@tonic-gate static PKCS7 *B64_read_PKCS7(BIO *bio);
920Sstevel@tonic-gate static char * strip_ends(char *name);
930Sstevel@tonic-gate static char * strip_start(char *name);
940Sstevel@tonic-gate static char * strip_end(char *name);
950Sstevel@tonic-gate static MIME_HEADER *mime_hdr_new(char *name, char *value);
960Sstevel@tonic-gate static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
970Sstevel@tonic-gate static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
980Sstevel@tonic-gate static int mime_hdr_cmp(const MIME_HEADER * const *a,
990Sstevel@tonic-gate const MIME_HEADER * const *b);
1000Sstevel@tonic-gate static int mime_param_cmp(const MIME_PARAM * const *a,
1010Sstevel@tonic-gate const MIME_PARAM * const *b);
1020Sstevel@tonic-gate static void mime_param_free(MIME_PARAM *param);
1030Sstevel@tonic-gate static int mime_bound_check(char *line, int linelen, char *bound, int blen);
1040Sstevel@tonic-gate static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
1050Sstevel@tonic-gate static int strip_eol(char *linebuf, int *plen);
1060Sstevel@tonic-gate static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
1070Sstevel@tonic-gate static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
1080Sstevel@tonic-gate static void mime_hdr_free(MIME_HEADER *hdr);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate #define MAX_SMLEN 1024
1110Sstevel@tonic-gate #define mime_debug(x) /* x */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /* Base 64 read and write of PKCS#7 structure */
1140Sstevel@tonic-gate
B64_write_PKCS7(BIO * bio,PKCS7 * p7)1150Sstevel@tonic-gate static int B64_write_PKCS7(BIO *bio, PKCS7 *p7)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate BIO *b64;
1180Sstevel@tonic-gate if(!(b64 = BIO_new(BIO_f_base64()))) {
1190Sstevel@tonic-gate PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE);
1200Sstevel@tonic-gate return 0;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate bio = BIO_push(b64, bio);
1230Sstevel@tonic-gate i2d_PKCS7_bio(bio, p7);
1240Sstevel@tonic-gate BIO_flush(bio);
1250Sstevel@tonic-gate bio = BIO_pop(bio);
1260Sstevel@tonic-gate BIO_free(b64);
1270Sstevel@tonic-gate return 1;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
B64_read_PKCS7(BIO * bio)1300Sstevel@tonic-gate static PKCS7 *B64_read_PKCS7(BIO *bio)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate BIO *b64;
1330Sstevel@tonic-gate PKCS7 *p7;
1340Sstevel@tonic-gate if(!(b64 = BIO_new(BIO_f_base64()))) {
1350Sstevel@tonic-gate PKCS7err(PKCS7_F_B64_READ_PKCS7,ERR_R_MALLOC_FAILURE);
1360Sstevel@tonic-gate return 0;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate bio = BIO_push(b64, bio);
1390Sstevel@tonic-gate if(!(p7 = d2i_PKCS7_bio(bio, NULL)))
1400Sstevel@tonic-gate PKCS7err(PKCS7_F_B64_READ_PKCS7,PKCS7_R_DECODE_ERROR);
1410Sstevel@tonic-gate BIO_flush(bio);
1420Sstevel@tonic-gate bio = BIO_pop(bio);
1430Sstevel@tonic-gate BIO_free(b64);
1440Sstevel@tonic-gate return p7;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* SMIME sender */
1480Sstevel@tonic-gate
SMIME_write_PKCS7(BIO * bio,PKCS7 * p7,BIO * data,int flags)1490Sstevel@tonic-gate int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate char bound[33], c;
1520Sstevel@tonic-gate int i;
153*2139Sjp161948 char *mime_prefix, *mime_eol, *msg_type=NULL;
1540Sstevel@tonic-gate if (flags & PKCS7_NOOLDMIMETYPE)
1550Sstevel@tonic-gate mime_prefix = "application/pkcs7-";
1560Sstevel@tonic-gate else
1570Sstevel@tonic-gate mime_prefix = "application/x-pkcs7-";
158*2139Sjp161948
1590Sstevel@tonic-gate if (flags & PKCS7_CRLFEOL)
1600Sstevel@tonic-gate mime_eol = "\r\n";
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate mime_eol = "\n";
1630Sstevel@tonic-gate if((flags & PKCS7_DETACHED) && data) {
1640Sstevel@tonic-gate /* We want multipart/signed */
1650Sstevel@tonic-gate /* Generate a random boundary */
1660Sstevel@tonic-gate RAND_pseudo_bytes((unsigned char *)bound, 32);
1670Sstevel@tonic-gate for(i = 0; i < 32; i++) {
1680Sstevel@tonic-gate c = bound[i] & 0xf;
1690Sstevel@tonic-gate if(c < 10) c += '0';
1700Sstevel@tonic-gate else c += 'A' - 10;
1710Sstevel@tonic-gate bound[i] = c;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate bound[32] = 0;
1740Sstevel@tonic-gate BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
1750Sstevel@tonic-gate BIO_printf(bio, "Content-Type: multipart/signed;");
1760Sstevel@tonic-gate BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
1770Sstevel@tonic-gate BIO_printf(bio, " micalg=sha1; boundary=\"----%s\"%s%s",
1780Sstevel@tonic-gate bound, mime_eol, mime_eol);
1790Sstevel@tonic-gate BIO_printf(bio, "This is an S/MIME signed message%s%s",
1800Sstevel@tonic-gate mime_eol, mime_eol);
1810Sstevel@tonic-gate /* Now write out the first part */
1820Sstevel@tonic-gate BIO_printf(bio, "------%s%s", bound, mime_eol);
183*2139Sjp161948 pkcs7_output_data(bio, data, p7, flags);
1840Sstevel@tonic-gate BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /* Headers for signature */
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
1890Sstevel@tonic-gate BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
1900Sstevel@tonic-gate BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
1910Sstevel@tonic-gate mime_eol);
1920Sstevel@tonic-gate BIO_printf(bio, "Content-Disposition: attachment;");
1930Sstevel@tonic-gate BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
1940Sstevel@tonic-gate mime_eol, mime_eol);
1950Sstevel@tonic-gate B64_write_PKCS7(bio, p7);
1960Sstevel@tonic-gate BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound,
197*2139Sjp161948 mime_eol, mime_eol);
1980Sstevel@tonic-gate return 1;
1990Sstevel@tonic-gate }
200*2139Sjp161948
201*2139Sjp161948 /* Determine smime-type header */
202*2139Sjp161948
203*2139Sjp161948 if (PKCS7_type_is_enveloped(p7))
204*2139Sjp161948 msg_type = "enveloped-data";
205*2139Sjp161948 else if (PKCS7_type_is_signed(p7))
206*2139Sjp161948 {
207*2139Sjp161948 /* If we have any signers it is signed-data othewise
208*2139Sjp161948 * certs-only.
209*2139Sjp161948 */
210*2139Sjp161948 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
211*2139Sjp161948 sinfos = PKCS7_get_signer_info(p7);
212*2139Sjp161948 if (sk_PKCS7_SIGNER_INFO_num(sinfos) > 0)
213*2139Sjp161948 msg_type = "signed-data";
214*2139Sjp161948 else
215*2139Sjp161948 msg_type = "certs-only";
216*2139Sjp161948 }
2170Sstevel@tonic-gate /* MIME headers */
2180Sstevel@tonic-gate BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
2190Sstevel@tonic-gate BIO_printf(bio, "Content-Disposition: attachment;");
2200Sstevel@tonic-gate BIO_printf(bio, " filename=\"smime.p7m\"%s", mime_eol);
2210Sstevel@tonic-gate BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
222*2139Sjp161948 if (msg_type)
223*2139Sjp161948 BIO_printf(bio, " smime-type=%s;", msg_type);
2240Sstevel@tonic-gate BIO_printf(bio, " name=\"smime.p7m\"%s", mime_eol);
2250Sstevel@tonic-gate BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
2260Sstevel@tonic-gate mime_eol, mime_eol);
2270Sstevel@tonic-gate B64_write_PKCS7(bio, p7);
2280Sstevel@tonic-gate BIO_printf(bio, "%s", mime_eol);
2290Sstevel@tonic-gate return 1;
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
232*2139Sjp161948 /* Handle output of PKCS#7 data */
233*2139Sjp161948
234*2139Sjp161948
pkcs7_output_data(BIO * out,BIO * data,PKCS7 * p7,int flags)235*2139Sjp161948 static int pkcs7_output_data(BIO *out, BIO *data, PKCS7 *p7, int flags)
236*2139Sjp161948 {
237*2139Sjp161948 BIO *tmpbio, *p7bio;
238*2139Sjp161948
239*2139Sjp161948 if (!(flags & PKCS7_STREAM))
240*2139Sjp161948 {
241*2139Sjp161948 SMIME_crlf_copy(data, out, flags);
242*2139Sjp161948 return 1;
243*2139Sjp161948 }
244*2139Sjp161948
245*2139Sjp161948 /* Partial sign operation */
246*2139Sjp161948
247*2139Sjp161948 /* Initialize sign operation */
248*2139Sjp161948 p7bio = PKCS7_dataInit(p7, out);
249*2139Sjp161948
250*2139Sjp161948 /* Copy data across, computing digests etc */
251*2139Sjp161948 SMIME_crlf_copy(data, p7bio, flags);
252*2139Sjp161948
253*2139Sjp161948 /* Must be detached */
254*2139Sjp161948 PKCS7_set_detached(p7, 1);
255*2139Sjp161948
256*2139Sjp161948 /* Finalize signatures */
257*2139Sjp161948 PKCS7_dataFinal(p7, p7bio);
258*2139Sjp161948
259*2139Sjp161948 /* Now remove any digests prepended to the BIO */
260*2139Sjp161948
261*2139Sjp161948 while (p7bio != out)
262*2139Sjp161948 {
263*2139Sjp161948 tmpbio = BIO_pop(p7bio);
264*2139Sjp161948 BIO_free(p7bio);
265*2139Sjp161948 p7bio = tmpbio;
266*2139Sjp161948 }
267*2139Sjp161948
268*2139Sjp161948 return 1;
269*2139Sjp161948
270*2139Sjp161948 }
271*2139Sjp161948
2720Sstevel@tonic-gate /* SMIME reader: handle multipart/signed and opaque signing.
2730Sstevel@tonic-gate * in multipart case the content is placed in a memory BIO
2740Sstevel@tonic-gate * pointed to by "bcont". In opaque this is set to NULL
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate
SMIME_read_PKCS7(BIO * bio,BIO ** bcont)2770Sstevel@tonic-gate PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate BIO *p7in;
2800Sstevel@tonic-gate STACK_OF(MIME_HEADER) *headers = NULL;
2810Sstevel@tonic-gate STACK_OF(BIO) *parts = NULL;
2820Sstevel@tonic-gate MIME_HEADER *hdr;
2830Sstevel@tonic-gate MIME_PARAM *prm;
2840Sstevel@tonic-gate PKCS7 *p7;
2850Sstevel@tonic-gate int ret;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate if(bcont) *bcont = NULL;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate if (!(headers = mime_parse_hdr(bio))) {
2900Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR);
2910Sstevel@tonic-gate return NULL;
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
2950Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
2960Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE);
2970Sstevel@tonic-gate return NULL;
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /* Handle multipart/signed */
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate if(!strcmp(hdr->value, "multipart/signed")) {
3030Sstevel@tonic-gate /* Split into two parts */
3040Sstevel@tonic-gate prm = mime_param_find(hdr, "boundary");
3050Sstevel@tonic-gate if(!prm || !prm->param_value) {
3060Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3070Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY);
3080Sstevel@tonic-gate return NULL;
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate ret = multi_split(bio, prm->param_value, &parts);
3110Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3120Sstevel@tonic-gate if(!ret || (sk_BIO_num(parts) != 2) ) {
3130Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE);
3140Sstevel@tonic-gate sk_BIO_pop_free(parts, BIO_vfree);
3150Sstevel@tonic-gate return NULL;
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate /* Parse the signature piece */
3190Sstevel@tonic-gate p7in = sk_BIO_value(parts, 1);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate if (!(headers = mime_parse_hdr(p7in))) {
3220Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR);
3230Sstevel@tonic-gate sk_BIO_pop_free(parts, BIO_vfree);
3240Sstevel@tonic-gate return NULL;
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /* Get content type */
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate if(!(hdr = mime_hdr_find(headers, "content-type")) ||
3300Sstevel@tonic-gate !hdr->value) {
3310Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3320Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE);
3330Sstevel@tonic-gate return NULL;
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate if(strcmp(hdr->value, "application/x-pkcs7-signature") &&
3370Sstevel@tonic-gate strcmp(hdr->value, "application/pkcs7-signature")) {
3380Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3390Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE);
3400Sstevel@tonic-gate ERR_add_error_data(2, "type: ", hdr->value);
3410Sstevel@tonic-gate sk_BIO_pop_free(parts, BIO_vfree);
3420Sstevel@tonic-gate return NULL;
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3450Sstevel@tonic-gate /* Read in PKCS#7 */
3460Sstevel@tonic-gate if(!(p7 = B64_read_PKCS7(p7in))) {
3470Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR);
3480Sstevel@tonic-gate sk_BIO_pop_free(parts, BIO_vfree);
3490Sstevel@tonic-gate return NULL;
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate if(bcont) {
3530Sstevel@tonic-gate *bcont = sk_BIO_value(parts, 0);
3540Sstevel@tonic-gate BIO_free(p7in);
3550Sstevel@tonic-gate sk_BIO_free(parts);
3560Sstevel@tonic-gate } else sk_BIO_pop_free(parts, BIO_vfree);
3570Sstevel@tonic-gate return p7;
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate /* OK, if not multipart/signed try opaque signature */
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
3630Sstevel@tonic-gate strcmp (hdr->value, "application/pkcs7-mime")) {
3640Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE);
3650Sstevel@tonic-gate ERR_add_error_data(2, "type: ", hdr->value);
3660Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3670Sstevel@tonic-gate return NULL;
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate if(!(p7 = B64_read_PKCS7(bio))) {
3730Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR);
3740Sstevel@tonic-gate return NULL;
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate return p7;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /* Copy text from one BIO to another making the output CRLF at EOL */
SMIME_crlf_copy(BIO * in,BIO * out,int flags)3810Sstevel@tonic-gate int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate char eol;
3840Sstevel@tonic-gate int len;
3850Sstevel@tonic-gate char linebuf[MAX_SMLEN];
3860Sstevel@tonic-gate if(flags & PKCS7_BINARY) {
3870Sstevel@tonic-gate while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
3880Sstevel@tonic-gate BIO_write(out, linebuf, len);
3890Sstevel@tonic-gate return 1;
3900Sstevel@tonic-gate }
391*2139Sjp161948 if(flags & PKCS7_TEXT)
392*2139Sjp161948 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
3930Sstevel@tonic-gate while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
3940Sstevel@tonic-gate eol = strip_eol(linebuf, &len);
3950Sstevel@tonic-gate if (len)
3960Sstevel@tonic-gate BIO_write(out, linebuf, len);
3970Sstevel@tonic-gate if(eol) BIO_write(out, "\r\n", 2);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate return 1;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate /* Strip off headers if they are text/plain */
SMIME_text(BIO * in,BIO * out)4030Sstevel@tonic-gate int SMIME_text(BIO *in, BIO *out)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate char iobuf[4096];
4060Sstevel@tonic-gate int len;
4070Sstevel@tonic-gate STACK_OF(MIME_HEADER) *headers;
4080Sstevel@tonic-gate MIME_HEADER *hdr;
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate if (!(headers = mime_parse_hdr(in))) {
4110Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_PARSE_ERROR);
4120Sstevel@tonic-gate return 0;
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
4150Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_NO_CONTENT_TYPE);
4160Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
4170Sstevel@tonic-gate return 0;
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate if (strcmp (hdr->value, "text/plain")) {
4200Sstevel@tonic-gate PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_INVALID_MIME_TYPE);
4210Sstevel@tonic-gate ERR_add_error_data(2, "type: ", hdr->value);
4220Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
4230Sstevel@tonic-gate return 0;
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
4260Sstevel@tonic-gate while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
4270Sstevel@tonic-gate BIO_write(out, iobuf, len);
4280Sstevel@tonic-gate return 1;
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate /* Split a multipart/XXX message body into component parts: result is
4320Sstevel@tonic-gate * canonical parts in a STACK of bios
4330Sstevel@tonic-gate */
4340Sstevel@tonic-gate
multi_split(BIO * bio,char * bound,STACK_OF (BIO)** ret)4350Sstevel@tonic-gate static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate char linebuf[MAX_SMLEN];
4380Sstevel@tonic-gate int len, blen;
4390Sstevel@tonic-gate int eol = 0, next_eol = 0;
4400Sstevel@tonic-gate BIO *bpart = NULL;
4410Sstevel@tonic-gate STACK_OF(BIO) *parts;
4420Sstevel@tonic-gate char state, part, first;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate blen = strlen(bound);
4450Sstevel@tonic-gate part = 0;
4460Sstevel@tonic-gate state = 0;
4470Sstevel@tonic-gate first = 1;
4480Sstevel@tonic-gate parts = sk_BIO_new_null();
4490Sstevel@tonic-gate *ret = parts;
4500Sstevel@tonic-gate while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
4510Sstevel@tonic-gate state = mime_bound_check(linebuf, len, bound, blen);
4520Sstevel@tonic-gate if(state == 1) {
4530Sstevel@tonic-gate first = 1;
4540Sstevel@tonic-gate part++;
4550Sstevel@tonic-gate } else if(state == 2) {
4560Sstevel@tonic-gate sk_BIO_push(parts, bpart);
4570Sstevel@tonic-gate return 1;
4580Sstevel@tonic-gate } else if(part) {
4590Sstevel@tonic-gate /* Strip CR+LF from linebuf */
4600Sstevel@tonic-gate next_eol = strip_eol(linebuf, &len);
4610Sstevel@tonic-gate if(first) {
4620Sstevel@tonic-gate first = 0;
4630Sstevel@tonic-gate if(bpart) sk_BIO_push(parts, bpart);
4640Sstevel@tonic-gate bpart = BIO_new(BIO_s_mem());
4650Sstevel@tonic-gate BIO_set_mem_eof_return(bpart, 0);
4660Sstevel@tonic-gate } else if (eol)
4670Sstevel@tonic-gate BIO_write(bpart, "\r\n", 2);
4680Sstevel@tonic-gate eol = next_eol;
4690Sstevel@tonic-gate if (len)
4700Sstevel@tonic-gate BIO_write(bpart, linebuf, len);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate return 0;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate /* This is the big one: parse MIME header lines up to message body */
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate #define MIME_INVALID 0
4790Sstevel@tonic-gate #define MIME_START 1
4800Sstevel@tonic-gate #define MIME_TYPE 2
4810Sstevel@tonic-gate #define MIME_NAME 3
4820Sstevel@tonic-gate #define MIME_VALUE 4
4830Sstevel@tonic-gate #define MIME_QUOTE 5
4840Sstevel@tonic-gate #define MIME_COMMENT 6
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate
STACK_OF(MIME_HEADER)4870Sstevel@tonic-gate static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate char *p, *q, c;
4900Sstevel@tonic-gate char *ntmp;
4910Sstevel@tonic-gate char linebuf[MAX_SMLEN];
4920Sstevel@tonic-gate MIME_HEADER *mhdr = NULL;
4930Sstevel@tonic-gate STACK_OF(MIME_HEADER) *headers;
4940Sstevel@tonic-gate int len, state, save_state = 0;
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate headers = sk_MIME_HEADER_new(mime_hdr_cmp);
4970Sstevel@tonic-gate while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
4980Sstevel@tonic-gate /* If whitespace at line start then continuation line */
4990Sstevel@tonic-gate if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME;
5000Sstevel@tonic-gate else state = MIME_START;
5010Sstevel@tonic-gate ntmp = NULL;
5020Sstevel@tonic-gate /* Go through all characters */
5030Sstevel@tonic-gate for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate /* State machine to handle MIME headers
5060Sstevel@tonic-gate * if this looks horrible that's because it *is*
5070Sstevel@tonic-gate */
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate switch(state) {
5100Sstevel@tonic-gate case MIME_START:
5110Sstevel@tonic-gate if(c == ':') {
5120Sstevel@tonic-gate state = MIME_TYPE;
5130Sstevel@tonic-gate *p = 0;
5140Sstevel@tonic-gate ntmp = strip_ends(q);
5150Sstevel@tonic-gate q = p + 1;
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate break;
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate case MIME_TYPE:
5200Sstevel@tonic-gate if(c == ';') {
5210Sstevel@tonic-gate mime_debug("Found End Value\n");
5220Sstevel@tonic-gate *p = 0;
5230Sstevel@tonic-gate mhdr = mime_hdr_new(ntmp, strip_ends(q));
5240Sstevel@tonic-gate sk_MIME_HEADER_push(headers, mhdr);
5250Sstevel@tonic-gate ntmp = NULL;
5260Sstevel@tonic-gate q = p + 1;
5270Sstevel@tonic-gate state = MIME_NAME;
5280Sstevel@tonic-gate } else if(c == '(') {
5290Sstevel@tonic-gate save_state = state;
5300Sstevel@tonic-gate state = MIME_COMMENT;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate break;
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate case MIME_COMMENT:
5350Sstevel@tonic-gate if(c == ')') {
5360Sstevel@tonic-gate state = save_state;
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate break;
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate case MIME_NAME:
5410Sstevel@tonic-gate if(c == '=') {
5420Sstevel@tonic-gate state = MIME_VALUE;
5430Sstevel@tonic-gate *p = 0;
5440Sstevel@tonic-gate ntmp = strip_ends(q);
5450Sstevel@tonic-gate q = p + 1;
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate break ;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate case MIME_VALUE:
5500Sstevel@tonic-gate if(c == ';') {
5510Sstevel@tonic-gate state = MIME_NAME;
5520Sstevel@tonic-gate *p = 0;
5530Sstevel@tonic-gate mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
5540Sstevel@tonic-gate ntmp = NULL;
5550Sstevel@tonic-gate q = p + 1;
5560Sstevel@tonic-gate } else if (c == '"') {
5570Sstevel@tonic-gate mime_debug("Found Quote\n");
5580Sstevel@tonic-gate state = MIME_QUOTE;
5590Sstevel@tonic-gate } else if(c == '(') {
5600Sstevel@tonic-gate save_state = state;
5610Sstevel@tonic-gate state = MIME_COMMENT;
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate break;
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate case MIME_QUOTE:
5660Sstevel@tonic-gate if(c == '"') {
5670Sstevel@tonic-gate mime_debug("Found Match Quote\n");
5680Sstevel@tonic-gate state = MIME_VALUE;
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate break;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate if(state == MIME_TYPE) {
5750Sstevel@tonic-gate mhdr = mime_hdr_new(ntmp, strip_ends(q));
5760Sstevel@tonic-gate sk_MIME_HEADER_push(headers, mhdr);
5770Sstevel@tonic-gate } else if(state == MIME_VALUE)
5780Sstevel@tonic-gate mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
5790Sstevel@tonic-gate if(p == linebuf) break; /* Blank line means end of headers */
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate return headers;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
strip_ends(char * name)5860Sstevel@tonic-gate static char *strip_ends(char *name)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate return strip_end(strip_start(name));
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /* Strip a parameter of whitespace from start of param */
strip_start(char * name)5920Sstevel@tonic-gate static char *strip_start(char *name)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate char *p, c;
5950Sstevel@tonic-gate /* Look for first non white space or quote */
5960Sstevel@tonic-gate for(p = name; (c = *p) ;p++) {
5970Sstevel@tonic-gate if(c == '"') {
5980Sstevel@tonic-gate /* Next char is start of string if non null */
5990Sstevel@tonic-gate if(p[1]) return p + 1;
6000Sstevel@tonic-gate /* Else null string */
6010Sstevel@tonic-gate return NULL;
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate if(!isspace((unsigned char)c)) return p;
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate return NULL;
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate /* As above but strip from end of string : maybe should handle brackets? */
strip_end(char * name)6090Sstevel@tonic-gate static char *strip_end(char *name)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate char *p, c;
6120Sstevel@tonic-gate if(!name) return NULL;
6130Sstevel@tonic-gate /* Look for first non white space or quote */
6140Sstevel@tonic-gate for(p = name + strlen(name) - 1; p >= name ;p--) {
6150Sstevel@tonic-gate c = *p;
6160Sstevel@tonic-gate if(c == '"') {
6170Sstevel@tonic-gate if(p - 1 == name) return NULL;
6180Sstevel@tonic-gate *p = 0;
6190Sstevel@tonic-gate return name;
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate if(isspace((unsigned char)c)) *p = 0;
6220Sstevel@tonic-gate else return name;
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate return NULL;
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate
mime_hdr_new(char * name,char * value)6270Sstevel@tonic-gate static MIME_HEADER *mime_hdr_new(char *name, char *value)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate MIME_HEADER *mhdr;
6300Sstevel@tonic-gate char *tmpname, *tmpval, *p;
6310Sstevel@tonic-gate int c;
6320Sstevel@tonic-gate if(name) {
6330Sstevel@tonic-gate if(!(tmpname = BUF_strdup(name))) return NULL;
6340Sstevel@tonic-gate for(p = tmpname ; *p; p++) {
6350Sstevel@tonic-gate c = *p;
6360Sstevel@tonic-gate if(isupper(c)) {
6370Sstevel@tonic-gate c = tolower(c);
6380Sstevel@tonic-gate *p = c;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate } else tmpname = NULL;
6420Sstevel@tonic-gate if(value) {
6430Sstevel@tonic-gate if(!(tmpval = BUF_strdup(value))) return NULL;
6440Sstevel@tonic-gate for(p = tmpval ; *p; p++) {
6450Sstevel@tonic-gate c = *p;
6460Sstevel@tonic-gate if(isupper(c)) {
6470Sstevel@tonic-gate c = tolower(c);
6480Sstevel@tonic-gate *p = c;
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate } else tmpval = NULL;
6520Sstevel@tonic-gate mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER));
6530Sstevel@tonic-gate if(!mhdr) return NULL;
6540Sstevel@tonic-gate mhdr->name = tmpname;
6550Sstevel@tonic-gate mhdr->value = tmpval;
6560Sstevel@tonic-gate if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL;
6570Sstevel@tonic-gate return mhdr;
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
mime_hdr_addparam(MIME_HEADER * mhdr,char * name,char * value)6600Sstevel@tonic-gate static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate char *tmpname, *tmpval, *p;
6630Sstevel@tonic-gate int c;
6640Sstevel@tonic-gate MIME_PARAM *mparam;
6650Sstevel@tonic-gate if(name) {
6660Sstevel@tonic-gate tmpname = BUF_strdup(name);
6670Sstevel@tonic-gate if(!tmpname) return 0;
6680Sstevel@tonic-gate for(p = tmpname ; *p; p++) {
6690Sstevel@tonic-gate c = *p;
6700Sstevel@tonic-gate if(isupper(c)) {
6710Sstevel@tonic-gate c = tolower(c);
6720Sstevel@tonic-gate *p = c;
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate } else tmpname = NULL;
6760Sstevel@tonic-gate if(value) {
6770Sstevel@tonic-gate tmpval = BUF_strdup(value);
6780Sstevel@tonic-gate if(!tmpval) return 0;
6790Sstevel@tonic-gate } else tmpval = NULL;
6800Sstevel@tonic-gate /* Parameter values are case sensitive so leave as is */
6810Sstevel@tonic-gate mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
6820Sstevel@tonic-gate if(!mparam) return 0;
6830Sstevel@tonic-gate mparam->param_name = tmpname;
6840Sstevel@tonic-gate mparam->param_value = tmpval;
6850Sstevel@tonic-gate sk_MIME_PARAM_push(mhdr->params, mparam);
6860Sstevel@tonic-gate return 1;
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate
mime_hdr_cmp(const MIME_HEADER * const * a,const MIME_HEADER * const * b)6890Sstevel@tonic-gate static int mime_hdr_cmp(const MIME_HEADER * const *a,
6900Sstevel@tonic-gate const MIME_HEADER * const *b)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate return(strcmp((*a)->name, (*b)->name));
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate
mime_param_cmp(const MIME_PARAM * const * a,const MIME_PARAM * const * b)6950Sstevel@tonic-gate static int mime_param_cmp(const MIME_PARAM * const *a,
6960Sstevel@tonic-gate const MIME_PARAM * const *b)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate return(strcmp((*a)->param_name, (*b)->param_name));
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate /* Find a header with a given name (if possible) */
7020Sstevel@tonic-gate
mime_hdr_find(STACK_OF (MIME_HEADER)* hdrs,char * name)7030Sstevel@tonic-gate static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate MIME_HEADER htmp;
7060Sstevel@tonic-gate int idx;
7070Sstevel@tonic-gate htmp.name = name;
7080Sstevel@tonic-gate idx = sk_MIME_HEADER_find(hdrs, &htmp);
7090Sstevel@tonic-gate if(idx < 0) return NULL;
7100Sstevel@tonic-gate return sk_MIME_HEADER_value(hdrs, idx);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate
mime_param_find(MIME_HEADER * hdr,char * name)7130Sstevel@tonic-gate static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate MIME_PARAM param;
7160Sstevel@tonic-gate int idx;
7170Sstevel@tonic-gate param.param_name = name;
7180Sstevel@tonic-gate idx = sk_MIME_PARAM_find(hdr->params, ¶m);
7190Sstevel@tonic-gate if(idx < 0) return NULL;
7200Sstevel@tonic-gate return sk_MIME_PARAM_value(hdr->params, idx);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate
mime_hdr_free(MIME_HEADER * hdr)7230Sstevel@tonic-gate static void mime_hdr_free(MIME_HEADER *hdr)
7240Sstevel@tonic-gate {
7250Sstevel@tonic-gate if(hdr->name) OPENSSL_free(hdr->name);
7260Sstevel@tonic-gate if(hdr->value) OPENSSL_free(hdr->value);
7270Sstevel@tonic-gate if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
7280Sstevel@tonic-gate OPENSSL_free(hdr);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
mime_param_free(MIME_PARAM * param)7310Sstevel@tonic-gate static void mime_param_free(MIME_PARAM *param)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate if(param->param_name) OPENSSL_free(param->param_name);
7340Sstevel@tonic-gate if(param->param_value) OPENSSL_free(param->param_value);
7350Sstevel@tonic-gate OPENSSL_free(param);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate /* Check for a multipart boundary. Returns:
7390Sstevel@tonic-gate * 0 : no boundary
7400Sstevel@tonic-gate * 1 : part boundary
7410Sstevel@tonic-gate * 2 : final boundary
7420Sstevel@tonic-gate */
mime_bound_check(char * line,int linelen,char * bound,int blen)7430Sstevel@tonic-gate static int mime_bound_check(char *line, int linelen, char *bound, int blen)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate if(linelen == -1) linelen = strlen(line);
7460Sstevel@tonic-gate if(blen == -1) blen = strlen(bound);
7470Sstevel@tonic-gate /* Quickly eliminate if line length too short */
7480Sstevel@tonic-gate if(blen + 2 > linelen) return 0;
7490Sstevel@tonic-gate /* Check for part boundary */
7500Sstevel@tonic-gate if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
7510Sstevel@tonic-gate if(!strncmp(line + blen + 2, "--", 2)) return 2;
7520Sstevel@tonic-gate else return 1;
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate return 0;
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
strip_eol(char * linebuf,int * plen)7570Sstevel@tonic-gate static int strip_eol(char *linebuf, int *plen)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate int len = *plen;
7600Sstevel@tonic-gate char *p, c;
7610Sstevel@tonic-gate int is_eol = 0;
7620Sstevel@tonic-gate p = linebuf + len - 1;
7630Sstevel@tonic-gate for (p = linebuf + len - 1; len > 0; len--, p--)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate c = *p;
7660Sstevel@tonic-gate if (c == '\n')
7670Sstevel@tonic-gate is_eol = 1;
7680Sstevel@tonic-gate else if (c != '\r')
7690Sstevel@tonic-gate break;
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate *plen = len;
7720Sstevel@tonic-gate return is_eol;
7730Sstevel@tonic-gate }
774