asn_mime.c revision bdfb8ad83da0647e9b9a32792598e8ce7ba3ef4d
1/* asn_mime.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2008 The OpenSSL Project.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
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
18 *    distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 *    acknowledgment:
36 *    "This product includes software developed by the OpenSSL Project
37 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
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 * ====================================================================
52 *
53 */
54
55#include <stdio.h>
56#include <ctype.h>
57#include "cryptlib.h"
58#include <openssl/rand.h>
59#include <openssl/x509.h>
60#include <openssl/asn1.h>
61#include <openssl/asn1t.h>
62
63/* Generalised MIME like utilities for streaming ASN1. Although many
64 * have a PKCS7/CMS like flavour others are more general purpose.
65 */
66
67/* MIME format structures
68 * Note that all are translated to lower case apart from
69 * parameter values. Quotes are stripped off
70 */
71
72typedef struct {
73char *param_name;			/* Param name e.g. "micalg" */
74char *param_value;			/* Param value e.g. "sha1" */
75} MIME_PARAM;
76
77DECLARE_STACK_OF(MIME_PARAM)
78IMPLEMENT_STACK_OF(MIME_PARAM)
79
80typedef struct {
81char *name;				/* Name of line e.g. "content-type" */
82char *value;				/* Value of line e.g. "text/plain" */
83STACK_OF(MIME_PARAM) *params;		/* Zero or more parameters */
84} MIME_HEADER;
85
86DECLARE_STACK_OF(MIME_HEADER)
87IMPLEMENT_STACK_OF(MIME_HEADER)
88
89static char * strip_ends(char *name);
90static char * strip_start(char *name);
91static char * strip_end(char *name);
92static MIME_HEADER *mime_hdr_new(char *name, char *value);
93static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
94static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
95static int mime_hdr_cmp(const MIME_HEADER * const *a,
96			const MIME_HEADER * const *b);
97static int mime_param_cmp(const MIME_PARAM * const *a,
98			const MIME_PARAM * const *b);
99static void mime_param_free(MIME_PARAM *param);
100static int mime_bound_check(char *line, int linelen, char *bound, int blen);
101static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
102static int strip_eol(char *linebuf, int *plen);
103static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
104static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
105static void mime_hdr_free(MIME_HEADER *hdr);
106
107#define MAX_SMLEN 1024
108#define mime_debug(x) /* x */
109
110/* Base 64 read and write of ASN1 structure */
111
112static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
113				const ASN1_ITEM *it)
114	{
115	BIO *b64;
116	int r;
117	b64 = BIO_new(BIO_f_base64());
118	if(!b64)
119		{
120		ASN1err(ASN1_F_B64_WRITE_ASN1,ERR_R_MALLOC_FAILURE);
121		return 0;
122		}
123	/* prepend the b64 BIO so all data is base64 encoded.
124	 */
125	out = BIO_push(b64, out);
126	r = ASN1_item_i2d_bio(it, out, val);
127	(void)BIO_flush(out);
128	BIO_pop(out);
129	BIO_free(b64);
130	return r;
131	}
132
133static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
134{
135	BIO *b64;
136	ASN1_VALUE *val;
137	if(!(b64 = BIO_new(BIO_f_base64()))) {
138		ASN1err(ASN1_F_B64_READ_ASN1,ERR_R_MALLOC_FAILURE);
139		return 0;
140	}
141	bio = BIO_push(b64, bio);
142	val = ASN1_item_d2i_bio(it, bio, NULL);
143	if(!val)
144		ASN1err(ASN1_F_B64_READ_ASN1,ASN1_R_DECODE_ERROR);
145	(void)BIO_flush(bio);
146	bio = BIO_pop(bio);
147	BIO_free(b64);
148	return val;
149}
150
151/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
152
153static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
154	{
155	const EVP_MD *md;
156	int i, have_unknown = 0, write_comma, md_nid;
157	have_unknown = 0;
158	write_comma = 0;
159	for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++)
160		{
161		if (write_comma)
162			BIO_write(out, ",", 1);
163		write_comma = 1;
164		md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
165		md = EVP_get_digestbynid(md_nid);
166		switch(md_nid)
167			{
168			case NID_sha1:
169			BIO_puts(out, "sha1");
170			break;
171
172			case NID_md5:
173			BIO_puts(out, "md5");
174			break;
175
176			case NID_sha256:
177			BIO_puts(out, "sha-256");
178			break;
179
180			case NID_sha384:
181			BIO_puts(out, "sha-384");
182			break;
183
184			case NID_sha512:
185			BIO_puts(out, "sha-512");
186			break;
187
188			default:
189			if (have_unknown)
190				write_comma = 0;
191			else
192				{
193				BIO_puts(out, "unknown");
194				have_unknown = 1;
195				}
196			break;
197
198			}
199		}
200
201	return 1;
202
203	}
204
205/* SMIME sender */
206
207int int_smime_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
208				int ctype_nid, int econt_nid,
209				STACK_OF(X509_ALGOR) *mdalgs,
210				asn1_output_data_fn *data_fn,
211				const ASN1_ITEM *it)
212{
213	char bound[33], c;
214	int i;
215	const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
216	const char *msg_type=NULL;
217	if (flags & SMIME_OLDMIME)
218		mime_prefix = "application/x-pkcs7-";
219	else
220		mime_prefix = "application/pkcs7-";
221
222	if (flags & SMIME_CRLFEOL)
223		mime_eol = "\r\n";
224	else
225		mime_eol = "\n";
226	if((flags & SMIME_DETACHED) && data) {
227	/* We want multipart/signed */
228		/* Generate a random boundary */
229		RAND_pseudo_bytes((unsigned char *)bound, 32);
230		for(i = 0; i < 32; i++) {
231			c = bound[i] & 0xf;
232			if(c < 10) c += '0';
233			else c += 'A' - 10;
234			bound[i] = c;
235		}
236		bound[32] = 0;
237		BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
238		BIO_printf(bio, "Content-Type: multipart/signed;");
239		BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
240		BIO_puts(bio, " micalg=\"");
241		asn1_write_micalg(bio, mdalgs);
242		BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
243						bound, mime_eol, mime_eol);
244		BIO_printf(bio, "This is an S/MIME signed message%s%s",
245						mime_eol, mime_eol);
246		/* Now write out the first part */
247		BIO_printf(bio, "------%s%s", bound, mime_eol);
248		if (!data_fn(bio, data, val, flags, it))
249			return 0;
250		BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
251
252		/* Headers for signature */
253
254		BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
255		BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
256		BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
257								mime_eol);
258		BIO_printf(bio, "Content-Disposition: attachment;");
259		BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
260							mime_eol, mime_eol);
261		B64_write_ASN1(bio, val, NULL, 0, it);
262		BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound,
263							mime_eol, mime_eol);
264		return 1;
265	}
266
267	/* Determine smime-type header */
268
269	if (ctype_nid == NID_pkcs7_enveloped)
270		msg_type = "enveloped-data";
271	else if (ctype_nid == NID_pkcs7_signed)
272		{
273		if (econt_nid == NID_id_smime_ct_receipt)
274			msg_type = "signed-receipt";
275		else if (sk_X509_ALGOR_num(mdalgs) >= 0)
276			msg_type = "signed-data";
277		else
278			msg_type = "certs-only";
279		}
280	else if (ctype_nid == NID_id_smime_ct_compressedData)
281		{
282		msg_type = "compressed-data";
283		cname = "smime.p7z";
284		}
285	/* MIME headers */
286	BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
287	BIO_printf(bio, "Content-Disposition: attachment;");
288	BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
289	BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
290	if (msg_type)
291		BIO_printf(bio, " smime-type=%s;", msg_type);
292	BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
293	BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
294						mime_eol, mime_eol);
295	if (!B64_write_ASN1(bio, val, data, flags, it))
296		return 0;
297	BIO_printf(bio, "%s", mime_eol);
298	return 1;
299}
300
301#if 0
302
303/* Handle output of ASN1 data */
304
305
306static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
307					const ASN1_ITEM *it)
308	{
309	BIO *tmpbio;
310	const ASN1_AUX *aux = it->funcs;
311	ASN1_STREAM_ARG sarg;
312
313	if (!(flags & SMIME_DETACHED))
314		{
315		SMIME_crlf_copy(data, out, flags);
316		return 1;
317		}
318
319	if (!aux || !aux->asn1_cb)
320		{
321		ASN1err(ASN1_F_ASN1_OUTPUT_DATA,
322					ASN1_R_STREAMING_NOT_SUPPORTED);
323		return 0;
324		}
325
326	sarg.out = out;
327	sarg.ndef_bio = NULL;
328	sarg.boundary = NULL;
329
330	/* Let ASN1 code prepend any needed BIOs */
331
332	if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
333		return 0;
334
335	/* Copy data across, passing through filter BIOs for processing */
336	SMIME_crlf_copy(data, sarg.ndef_bio, flags);
337
338	/* Finalize structure */
339	if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
340		return 0;
341
342	/* Now remove any digests prepended to the BIO */
343
344	while (sarg.ndef_bio != out)
345		{
346		tmpbio = BIO_pop(sarg.ndef_bio);
347		BIO_free(sarg.ndef_bio);
348		sarg.ndef_bio = tmpbio;
349		}
350
351	return 1;
352
353	}
354
355#endif
356
357/* SMIME reader: handle multipart/signed and opaque signing.
358 * in multipart case the content is placed in a memory BIO
359 * pointed to by "bcont". In opaque this is set to NULL
360 */
361
362ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
363{
364	BIO *asnin;
365	STACK_OF(MIME_HEADER) *headers = NULL;
366	STACK_OF(BIO) *parts = NULL;
367	MIME_HEADER *hdr;
368	MIME_PARAM *prm;
369	ASN1_VALUE *val;
370	int ret;
371
372	if(bcont) *bcont = NULL;
373
374	if (!(headers = mime_parse_hdr(bio))) {
375		ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_PARSE_ERROR);
376		return NULL;
377	}
378
379	if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
380		sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
381		ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
382		return NULL;
383	}
384
385	/* Handle multipart/signed */
386
387	if(!strcmp(hdr->value, "multipart/signed")) {
388		/* Split into two parts */
389		prm = mime_param_find(hdr, "boundary");
390		if(!prm || !prm->param_value) {
391			sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
392			ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY);
393			return NULL;
394		}
395		ret = multi_split(bio, prm->param_value, &parts);
396		sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
397		if(!ret || (sk_BIO_num(parts) != 2) ) {
398			ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE);
399			sk_BIO_pop_free(parts, BIO_vfree);
400			return NULL;
401		}
402
403		/* Parse the signature piece */
404		asnin = sk_BIO_value(parts, 1);
405
406		if (!(headers = mime_parse_hdr(asnin))) {
407			ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_SIG_PARSE_ERROR);
408			sk_BIO_pop_free(parts, BIO_vfree);
409			return NULL;
410		}
411
412		/* Get content type */
413
414		if(!(hdr = mime_hdr_find(headers, "content-type")) ||
415								 !hdr->value) {
416			sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
417			ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE);
418			return NULL;
419		}
420
421		if(strcmp(hdr->value, "application/x-pkcs7-signature") &&
422			strcmp(hdr->value, "application/pkcs7-signature")) {
423			sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
424			ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_SIG_INVALID_MIME_TYPE);
425			ERR_add_error_data(2, "type: ", hdr->value);
426			sk_BIO_pop_free(parts, BIO_vfree);
427			return NULL;
428		}
429		sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
430		/* Read in ASN1 */
431		if(!(val = b64_read_asn1(asnin, it))) {
432			ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_ASN1_SIG_PARSE_ERROR);
433			sk_BIO_pop_free(parts, BIO_vfree);
434			return NULL;
435		}
436
437		if(bcont) {
438			*bcont = sk_BIO_value(parts, 0);
439			BIO_free(asnin);
440			sk_BIO_free(parts);
441		} else sk_BIO_pop_free(parts, BIO_vfree);
442		return val;
443	}
444
445	/* OK, if not multipart/signed try opaque signature */
446
447	if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
448	    strcmp (hdr->value, "application/pkcs7-mime")) {
449		ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_INVALID_MIME_TYPE);
450		ERR_add_error_data(2, "type: ", hdr->value);
451		sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
452		return NULL;
453	}
454
455	sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
456
457	if(!(val = b64_read_asn1(bio, it))) {
458		ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
459		return NULL;
460	}
461	return val;
462
463}
464
465/* Copy text from one BIO to another making the output CRLF at EOL */
466int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
467{
468	BIO *bf;
469	char eol;
470	int len;
471	char linebuf[MAX_SMLEN];
472	/* Buffer output so we don't write one line at a time. This is
473	 * useful when streaming as we don't end up with one OCTET STRING
474	 * per line.
475	 */
476	bf = BIO_new(BIO_f_buffer());
477	if (!bf)
478		return 0;
479	out = BIO_push(bf, out);
480	if(flags & SMIME_BINARY)
481		{
482		while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
483						BIO_write(out, linebuf, len);
484		}
485	else
486		{
487		if(flags & SMIME_TEXT)
488			BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
489		while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0)
490			{
491			eol = strip_eol(linebuf, &len);
492			if (len)
493				BIO_write(out, linebuf, len);
494			if(eol) BIO_write(out, "\r\n", 2);
495			}
496		}
497	(void)BIO_flush(out);
498	BIO_pop(out);
499	BIO_free(bf);
500	return 1;
501}
502
503/* Strip off headers if they are text/plain */
504int SMIME_text(BIO *in, BIO *out)
505{
506	char iobuf[4096];
507	int len;
508	STACK_OF(MIME_HEADER) *headers;
509	MIME_HEADER *hdr;
510
511	if (!(headers = mime_parse_hdr(in))) {
512		ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_PARSE_ERROR);
513		return 0;
514	}
515	if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
516		ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_NO_CONTENT_TYPE);
517		sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
518		return 0;
519	}
520	if (strcmp (hdr->value, "text/plain")) {
521		ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_INVALID_MIME_TYPE);
522		ERR_add_error_data(2, "type: ", hdr->value);
523		sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
524		return 0;
525	}
526	sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
527	while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
528						BIO_write(out, iobuf, len);
529	return 1;
530}
531
532/* Split a multipart/XXX message body into component parts: result is
533 * canonical parts in a STACK of bios
534 */
535
536static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
537{
538	char linebuf[MAX_SMLEN];
539	int len, blen;
540	int eol = 0, next_eol = 0;
541	BIO *bpart = NULL;
542	STACK_OF(BIO) *parts;
543	char state, part, first;
544
545	blen = strlen(bound);
546	part = 0;
547	state = 0;
548	first = 1;
549	parts = sk_BIO_new_null();
550	*ret = parts;
551	while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
552		state = mime_bound_check(linebuf, len, bound, blen);
553		if(state == 1) {
554			first = 1;
555			part++;
556		} else if(state == 2) {
557			sk_BIO_push(parts, bpart);
558			return 1;
559		} else if(part) {
560			/* Strip CR+LF from linebuf */
561			next_eol = strip_eol(linebuf, &len);
562			if(first) {
563				first = 0;
564				if(bpart) sk_BIO_push(parts, bpart);
565				bpart = BIO_new(BIO_s_mem());
566				BIO_set_mem_eof_return(bpart, 0);
567			} else if (eol)
568				BIO_write(bpart, "\r\n", 2);
569			eol = next_eol;
570			if (len)
571				BIO_write(bpart, linebuf, len);
572		}
573	}
574	return 0;
575}
576
577/* This is the big one: parse MIME header lines up to message body */
578
579#define MIME_INVALID	0
580#define MIME_START	1
581#define MIME_TYPE	2
582#define MIME_NAME	3
583#define MIME_VALUE	4
584#define MIME_QUOTE	5
585#define MIME_COMMENT	6
586
587
588static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
589{
590	char *p, *q, c;
591	char *ntmp;
592	char linebuf[MAX_SMLEN];
593	MIME_HEADER *mhdr = NULL;
594	STACK_OF(MIME_HEADER) *headers;
595	int len, state, save_state = 0;
596
597	headers = sk_MIME_HEADER_new(mime_hdr_cmp);
598	while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
599	/* If whitespace at line start then continuation line */
600	if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME;
601	else state = MIME_START;
602	ntmp = NULL;
603	/* Go through all characters */
604	for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
605
606	/* State machine to handle MIME headers
607	 * if this looks horrible that's because it *is*
608         */
609
610		switch(state) {
611			case MIME_START:
612			if(c == ':') {
613				state = MIME_TYPE;
614				*p = 0;
615				ntmp = strip_ends(q);
616				q = p + 1;
617			}
618			break;
619
620			case MIME_TYPE:
621			if(c == ';') {
622				mime_debug("Found End Value\n");
623				*p = 0;
624				mhdr = mime_hdr_new(ntmp, strip_ends(q));
625				sk_MIME_HEADER_push(headers, mhdr);
626				ntmp = NULL;
627				q = p + 1;
628				state = MIME_NAME;
629			} else if(c == '(') {
630				save_state = state;
631				state = MIME_COMMENT;
632			}
633			break;
634
635			case MIME_COMMENT:
636			if(c == ')') {
637				state = save_state;
638			}
639			break;
640
641			case MIME_NAME:
642			if(c == '=') {
643				state = MIME_VALUE;
644				*p = 0;
645				ntmp = strip_ends(q);
646				q = p + 1;
647			}
648			break ;
649
650			case MIME_VALUE:
651			if(c == ';') {
652				state = MIME_NAME;
653				*p = 0;
654				mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
655				ntmp = NULL;
656				q = p + 1;
657			} else if (c == '"') {
658				mime_debug("Found Quote\n");
659				state = MIME_QUOTE;
660			} else if(c == '(') {
661				save_state = state;
662				state = MIME_COMMENT;
663			}
664			break;
665
666			case MIME_QUOTE:
667			if(c == '"') {
668				mime_debug("Found Match Quote\n");
669				state = MIME_VALUE;
670			}
671			break;
672		}
673	}
674
675	if(state == MIME_TYPE) {
676		mhdr = mime_hdr_new(ntmp, strip_ends(q));
677		sk_MIME_HEADER_push(headers, mhdr);
678	} else if(state == MIME_VALUE)
679			 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
680	if(p == linebuf) break;	/* Blank line means end of headers */
681}
682
683return headers;
684
685}
686
687static char *strip_ends(char *name)
688{
689	return strip_end(strip_start(name));
690}
691
692/* Strip a parameter of whitespace from start of param */
693static char *strip_start(char *name)
694{
695	char *p, c;
696	/* Look for first non white space or quote */
697	for(p = name; (c = *p) ;p++) {
698		if(c == '"') {
699			/* Next char is start of string if non null */
700			if(p[1]) return p + 1;
701			/* Else null string */
702			return NULL;
703		}
704		if(!isspace((unsigned char)c)) return p;
705	}
706	return NULL;
707}
708
709/* As above but strip from end of string : maybe should handle brackets? */
710static char *strip_end(char *name)
711{
712	char *p, c;
713	if(!name) return NULL;
714	/* Look for first non white space or quote */
715	for(p = name + strlen(name) - 1; p >= name ;p--) {
716		c = *p;
717		if(c == '"') {
718			if(p - 1 == name) return NULL;
719			*p = 0;
720			return name;
721		}
722		if(isspace((unsigned char)c)) *p = 0;
723		else return name;
724	}
725	return NULL;
726}
727
728static MIME_HEADER *mime_hdr_new(char *name, char *value)
729{
730	MIME_HEADER *mhdr;
731	char *tmpname, *tmpval, *p;
732	int c;
733	if(name) {
734		if(!(tmpname = BUF_strdup(name))) return NULL;
735		for(p = tmpname ; *p; p++) {
736			c = *p;
737			if(isupper(c)) {
738				c = tolower(c);
739				*p = c;
740			}
741		}
742	} else tmpname = NULL;
743	if(value) {
744		if(!(tmpval = BUF_strdup(value))) return NULL;
745		for(p = tmpval ; *p; p++) {
746			c = *p;
747			if(isupper(c)) {
748				c = tolower(c);
749				*p = c;
750			}
751		}
752	} else tmpval = NULL;
753	mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER));
754	if(!mhdr) return NULL;
755	mhdr->name = tmpname;
756	mhdr->value = tmpval;
757	if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL;
758	return mhdr;
759}
760
761static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
762{
763	char *tmpname, *tmpval, *p;
764	int c;
765	MIME_PARAM *mparam;
766	if(name) {
767		tmpname = BUF_strdup(name);
768		if(!tmpname) return 0;
769		for(p = tmpname ; *p; p++) {
770			c = *p;
771			if(isupper(c)) {
772				c = tolower(c);
773				*p = c;
774			}
775		}
776	} else tmpname = NULL;
777	if(value) {
778		tmpval = BUF_strdup(value);
779		if(!tmpval) return 0;
780	} else tmpval = NULL;
781	/* Parameter values are case sensitive so leave as is */
782	mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
783	if(!mparam) return 0;
784	mparam->param_name = tmpname;
785	mparam->param_value = tmpval;
786	sk_MIME_PARAM_push(mhdr->params, mparam);
787	return 1;
788}
789
790static int mime_hdr_cmp(const MIME_HEADER * const *a,
791			const MIME_HEADER * const *b)
792{
793	return(strcmp((*a)->name, (*b)->name));
794}
795
796static int mime_param_cmp(const MIME_PARAM * const *a,
797			const MIME_PARAM * const *b)
798{
799	return(strcmp((*a)->param_name, (*b)->param_name));
800}
801
802/* Find a header with a given name (if possible) */
803
804static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
805{
806	MIME_HEADER htmp;
807	int idx;
808	htmp.name = name;
809	idx = sk_MIME_HEADER_find(hdrs, &htmp);
810	if(idx < 0) return NULL;
811	return sk_MIME_HEADER_value(hdrs, idx);
812}
813
814static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
815{
816	MIME_PARAM param;
817	int idx;
818	param.param_name = name;
819	idx = sk_MIME_PARAM_find(hdr->params, &param);
820	if(idx < 0) return NULL;
821	return sk_MIME_PARAM_value(hdr->params, idx);
822}
823
824static void mime_hdr_free(MIME_HEADER *hdr)
825{
826	if(hdr->name) OPENSSL_free(hdr->name);
827	if(hdr->value) OPENSSL_free(hdr->value);
828	if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
829	OPENSSL_free(hdr);
830}
831
832static void mime_param_free(MIME_PARAM *param)
833{
834	if(param->param_name) OPENSSL_free(param->param_name);
835	if(param->param_value) OPENSSL_free(param->param_value);
836	OPENSSL_free(param);
837}
838
839/* Check for a multipart boundary. Returns:
840 * 0 : no boundary
841 * 1 : part boundary
842 * 2 : final boundary
843 */
844static int mime_bound_check(char *line, int linelen, char *bound, int blen)
845{
846	if(linelen == -1) linelen = strlen(line);
847	if(blen == -1) blen = strlen(bound);
848	/* Quickly eliminate if line length too short */
849	if(blen + 2 > linelen) return 0;
850	/* Check for part boundary */
851	if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
852		if(!strncmp(line + blen + 2, "--", 2)) return 2;
853		else return 1;
854	}
855	return 0;
856}
857
858static int strip_eol(char *linebuf, int *plen)
859	{
860	int len = *plen;
861	char *p, c;
862	int is_eol = 0;
863	p = linebuf + len - 1;
864	for (p = linebuf + len - 1; len > 0; len--, p--)
865		{
866		c = *p;
867		if (c == '\n')
868			is_eol = 1;
869		else if (c != '\r')
870			break;
871		}
872	*plen = len;
873	return is_eol;
874	}
875