summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pkcs7/pk7_mime.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/pkcs7/pk7_mime.c')
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_mime.c734
1 files changed, 0 insertions, 734 deletions
diff --git a/src/lib/libcrypto/pkcs7/pk7_mime.c b/src/lib/libcrypto/pkcs7/pk7_mime.c
deleted file mode 100644
index 927b88c3e7..0000000000
--- a/src/lib/libcrypto/pkcs7/pk7_mime.c
+++ /dev/null
@@ -1,734 +0,0 @@
1/* pk7_mime.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2005 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 * 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).
56 *
57 */
58
59#include <stdio.h>
60#include <ctype.h>
61#include "cryptlib.h"
62#include <openssl/rand.h>
63#include <openssl/x509.h>
64
65/* MIME and related routines */
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 int B64_write_PKCS7(BIO *bio, PKCS7 *p7);
90static PKCS7 *B64_read_PKCS7(BIO *bio);
91static char * strip_ends(char *name);
92static char * strip_start(char *name);
93static char * strip_end(char *name);
94static MIME_HEADER *mime_hdr_new(char *name, char *value);
95static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
96static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
97static int mime_hdr_cmp(const MIME_HEADER * const *a,
98 const MIME_HEADER * const *b);
99static int mime_param_cmp(const MIME_PARAM * const *a,
100 const MIME_PARAM * const *b);
101static void mime_param_free(MIME_PARAM *param);
102static int mime_bound_check(char *line, int linelen, char *bound, int blen);
103static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
104static int strip_eol(char *linebuf, int *plen);
105static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
106static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
107static void mime_hdr_free(MIME_HEADER *hdr);
108
109#define MAX_SMLEN 1024
110#define mime_debug(x) /* x */
111
112
113typedef void (*stkfree)();
114
115/* Base 64 read and write of PKCS#7 structure */
116
117static int B64_write_PKCS7(BIO *bio, PKCS7 *p7)
118{
119 BIO *b64;
120 if(!(b64 = BIO_new(BIO_f_base64()))) {
121 PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE);
122 return 0;
123 }
124 bio = BIO_push(b64, bio);
125 i2d_PKCS7_bio(bio, p7);
126 BIO_flush(bio);
127 bio = BIO_pop(bio);
128 BIO_free(b64);
129 return 1;
130}
131
132static PKCS7 *B64_read_PKCS7(BIO *bio)
133{
134 BIO *b64;
135 PKCS7 *p7;
136 if(!(b64 = BIO_new(BIO_f_base64()))) {
137 PKCS7err(PKCS7_F_B64_READ_PKCS7,ERR_R_MALLOC_FAILURE);
138 return 0;
139 }
140 bio = BIO_push(b64, bio);
141 if(!(p7 = d2i_PKCS7_bio(bio, NULL)))
142 PKCS7err(PKCS7_F_B64_READ_PKCS7,PKCS7_R_DECODE_ERROR);
143 BIO_flush(bio);
144 bio = BIO_pop(bio);
145 BIO_free(b64);
146 return p7;
147}
148
149/* SMIME sender */
150
151int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags)
152{
153 char bound[33], c;
154 int i;
155 char *mime_prefix, *mime_eol, *msg_type=NULL;
156 if (flags & PKCS7_NOOLDMIMETYPE)
157 mime_prefix = "application/pkcs7-";
158 else
159 mime_prefix = "application/x-pkcs7-";
160
161 if (flags & PKCS7_CRLFEOL)
162 mime_eol = "\r\n";
163 else
164 mime_eol = "\n";
165 if((flags & PKCS7_DETACHED) && data) {
166 /* We want multipart/signed */
167 /* Generate a random boundary */
168 RAND_pseudo_bytes((unsigned char *)bound, 32);
169 for(i = 0; i < 32; i++) {
170 c = bound[i] & 0xf;
171 if(c < 10) c += '0';
172 else c += 'A' - 10;
173 bound[i] = c;
174 }
175 bound[32] = 0;
176 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
177 BIO_printf(bio, "Content-Type: multipart/signed;");
178 BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
179 BIO_printf(bio, " micalg=sha1; boundary=\"----%s\"%s%s",
180 bound, mime_eol, mime_eol);
181 BIO_printf(bio, "This is an S/MIME signed message%s%s",
182 mime_eol, mime_eol);
183 /* Now write out the first part */
184 BIO_printf(bio, "------%s%s", bound, mime_eol);
185 SMIME_crlf_copy(data, bio, flags);
186 BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
187
188 /* Headers for signature */
189
190 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
191 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
192 BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
193 mime_eol);
194 BIO_printf(bio, "Content-Disposition: attachment;");
195 BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
196 mime_eol, mime_eol);
197 B64_write_PKCS7(bio, p7);
198 BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound,
199 mime_eol, mime_eol);
200 return 1;
201 }
202
203 /* Determine smime-type header */
204
205 if (PKCS7_type_is_enveloped(p7))
206 msg_type = "enveloped-data";
207 else if (PKCS7_type_is_signed(p7))
208 {
209 /* If we have any signers it is signed-data othewise
210 * certs-only.
211 */
212 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
213 sinfos = PKCS7_get_signer_info(p7);
214 if (sk_PKCS7_SIGNER_INFO_num(sinfos) > 0)
215 msg_type = "signed-data";
216 else
217 msg_type = "certs-only";
218 }
219 /* MIME headers */
220 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
221 BIO_printf(bio, "Content-Disposition: attachment;");
222 BIO_printf(bio, " filename=\"smime.p7m\"%s", mime_eol);
223 BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
224 if (msg_type)
225 BIO_printf(bio, " smime-type=%s;", msg_type);
226 BIO_printf(bio, " name=\"smime.p7m\"%s", mime_eol);
227 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
228 mime_eol, mime_eol);
229 B64_write_PKCS7(bio, p7);
230 BIO_printf(bio, "%s", mime_eol);
231 return 1;
232}
233
234/* SMIME reader: handle multipart/signed and opaque signing.
235 * in multipart case the content is placed in a memory BIO
236 * pointed to by "bcont". In opaque this is set to NULL
237 */
238
239PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont)
240{
241 BIO *p7in;
242 STACK_OF(MIME_HEADER) *headers = NULL;
243 STACK_OF(BIO) *parts = NULL;
244 MIME_HEADER *hdr;
245 MIME_PARAM *prm;
246 PKCS7 *p7;
247 int ret;
248
249 if(bcont) *bcont = NULL;
250
251 if (!(headers = mime_parse_hdr(bio))) {
252 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR);
253 return NULL;
254 }
255
256 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
257 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
258 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE);
259 return NULL;
260 }
261
262 /* Handle multipart/signed */
263
264 if(!strcmp(hdr->value, "multipart/signed")) {
265 /* Split into two parts */
266 prm = mime_param_find(hdr, "boundary");
267 if(!prm || !prm->param_value) {
268 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
269 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY);
270 return NULL;
271 }
272 ret = multi_split(bio, prm->param_value, &parts);
273 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
274 if(!ret || (sk_BIO_num(parts) != 2) ) {
275 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE);
276 sk_BIO_pop_free(parts, BIO_vfree);
277 return NULL;
278 }
279
280 /* Parse the signature piece */
281 p7in = sk_BIO_value(parts, 1);
282
283 if (!(headers = mime_parse_hdr(p7in))) {
284 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR);
285 sk_BIO_pop_free(parts, BIO_vfree);
286 return NULL;
287 }
288
289 /* Get content type */
290
291 if(!(hdr = mime_hdr_find(headers, "content-type")) ||
292 !hdr->value) {
293 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
294 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE);
295 return NULL;
296 }
297
298 if(strcmp(hdr->value, "application/x-pkcs7-signature") &&
299 strcmp(hdr->value, "application/pkcs7-signature")) {
300 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
301 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE);
302 ERR_add_error_data(2, "type: ", hdr->value);
303 sk_BIO_pop_free(parts, BIO_vfree);
304 return NULL;
305 }
306 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
307 /* Read in PKCS#7 */
308 if(!(p7 = B64_read_PKCS7(p7in))) {
309 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR);
310 sk_BIO_pop_free(parts, BIO_vfree);
311 return NULL;
312 }
313
314 if(bcont) {
315 *bcont = sk_BIO_value(parts, 0);
316 BIO_free(p7in);
317 sk_BIO_free(parts);
318 } else sk_BIO_pop_free(parts, BIO_vfree);
319 return p7;
320 }
321
322 /* OK, if not multipart/signed try opaque signature */
323
324 if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
325 strcmp (hdr->value, "application/pkcs7-mime")) {
326 PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE);
327 ERR_add_error_data(2, "type: ", hdr->value);
328 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
329 return NULL;
330 }
331
332 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
333
334 if(!(p7 = B64_read_PKCS7(bio))) {
335 PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR);
336 return NULL;
337 }
338 return p7;
339
340}
341
342/* Copy text from one BIO to another making the output CRLF at EOL */
343int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
344{
345 char eol;
346 int len;
347 char linebuf[MAX_SMLEN];
348 if(flags & PKCS7_BINARY) {
349 while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
350 BIO_write(out, linebuf, len);
351 return 1;
352 }
353 if(flags & PKCS7_TEXT) BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
354 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
355 eol = strip_eol(linebuf, &len);
356 if (len)
357 BIO_write(out, linebuf, len);
358 if(eol) BIO_write(out, "\r\n", 2);
359 }
360 return 1;
361}
362
363/* Strip off headers if they are text/plain */
364int SMIME_text(BIO *in, BIO *out)
365{
366 char iobuf[4096];
367 int len;
368 STACK_OF(MIME_HEADER) *headers;
369 MIME_HEADER *hdr;
370
371 if (!(headers = mime_parse_hdr(in))) {
372 PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_PARSE_ERROR);
373 return 0;
374 }
375 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
376 PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_NO_CONTENT_TYPE);
377 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
378 return 0;
379 }
380 if (strcmp (hdr->value, "text/plain")) {
381 PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_INVALID_MIME_TYPE);
382 ERR_add_error_data(2, "type: ", hdr->value);
383 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
384 return 0;
385 }
386 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
387 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
388 BIO_write(out, iobuf, len);
389 return 1;
390}
391
392/* Split a multipart/XXX message body into component parts: result is
393 * canonical parts in a STACK of bios
394 */
395
396static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
397{
398 char linebuf[MAX_SMLEN];
399 int len, blen;
400 int eol = 0, next_eol = 0;
401 BIO *bpart = NULL;
402 STACK_OF(BIO) *parts;
403 char state, part, first;
404
405 blen = strlen(bound);
406 part = 0;
407 state = 0;
408 first = 1;
409 parts = sk_BIO_new_null();
410 *ret = parts;
411 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
412 state = mime_bound_check(linebuf, len, bound, blen);
413 if(state == 1) {
414 first = 1;
415 part++;
416 } else if(state == 2) {
417 sk_BIO_push(parts, bpart);
418 return 1;
419 } else if(part) {
420 /* Strip CR+LF from linebuf */
421 next_eol = strip_eol(linebuf, &len);
422 if(first) {
423 first = 0;
424 if(bpart) sk_BIO_push(parts, bpart);
425 bpart = BIO_new(BIO_s_mem());
426 BIO_set_mem_eof_return(bpart, 0);
427 } else if (eol)
428 BIO_write(bpart, "\r\n", 2);
429 eol = next_eol;
430 if (len)
431 BIO_write(bpart, linebuf, len);
432 }
433 }
434 return 0;
435}
436
437/* This is the big one: parse MIME header lines up to message body */
438
439#define MIME_INVALID 0
440#define MIME_START 1
441#define MIME_TYPE 2
442#define MIME_NAME 3
443#define MIME_VALUE 4
444#define MIME_QUOTE 5
445#define MIME_COMMENT 6
446
447
448static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
449{
450 char *p, *q, c;
451 char *ntmp;
452 char linebuf[MAX_SMLEN];
453 MIME_HEADER *mhdr = NULL;
454 STACK_OF(MIME_HEADER) *headers;
455 int len, state, save_state = 0;
456
457 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
458 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
459 /* If whitespace at line start then continuation line */
460 if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME;
461 else state = MIME_START;
462 ntmp = NULL;
463 /* Go through all characters */
464 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
465
466 /* State machine to handle MIME headers
467 * if this looks horrible that's because it *is*
468 */
469
470 switch(state) {
471 case MIME_START:
472 if(c == ':') {
473 state = MIME_TYPE;
474 *p = 0;
475 ntmp = strip_ends(q);
476 q = p + 1;
477 }
478 break;
479
480 case MIME_TYPE:
481 if(c == ';') {
482 mime_debug("Found End Value\n");
483 *p = 0;
484 mhdr = mime_hdr_new(ntmp, strip_ends(q));
485 sk_MIME_HEADER_push(headers, mhdr);
486 ntmp = NULL;
487 q = p + 1;
488 state = MIME_NAME;
489 } else if(c == '(') {
490 save_state = state;
491 state = MIME_COMMENT;
492 }
493 break;
494
495 case MIME_COMMENT:
496 if(c == ')') {
497 state = save_state;
498 }
499 break;
500
501 case MIME_NAME:
502 if(c == '=') {
503 state = MIME_VALUE;
504 *p = 0;
505 ntmp = strip_ends(q);
506 q = p + 1;
507 }
508 break ;
509
510 case MIME_VALUE:
511 if(c == ';') {
512 state = MIME_NAME;
513 *p = 0;
514 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
515 ntmp = NULL;
516 q = p + 1;
517 } else if (c == '"') {
518 mime_debug("Found Quote\n");
519 state = MIME_QUOTE;
520 } else if(c == '(') {
521 save_state = state;
522 state = MIME_COMMENT;
523 }
524 break;
525
526 case MIME_QUOTE:
527 if(c == '"') {
528 mime_debug("Found Match Quote\n");
529 state = MIME_VALUE;
530 }
531 break;
532 }
533 }
534
535 if(state == MIME_TYPE) {
536 mhdr = mime_hdr_new(ntmp, strip_ends(q));
537 sk_MIME_HEADER_push(headers, mhdr);
538 } else if(state == MIME_VALUE)
539 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
540 if(p == linebuf) break; /* Blank line means end of headers */
541}
542
543return headers;
544
545}
546
547static char *strip_ends(char *name)
548{
549 return strip_end(strip_start(name));
550}
551
552/* Strip a parameter of whitespace from start of param */
553static char *strip_start(char *name)
554{
555 char *p, c;
556 /* Look for first non white space or quote */
557 for(p = name; (c = *p) ;p++) {
558 if(c == '"') {
559 /* Next char is start of string if non null */
560 if(p[1]) return p + 1;
561 /* Else null string */
562 return NULL;
563 }
564 if(!isspace((unsigned char)c)) return p;
565 }
566 return NULL;
567}
568
569/* As above but strip from end of string : maybe should handle brackets? */
570static char *strip_end(char *name)
571{
572 char *p, c;
573 if(!name) return NULL;
574 /* Look for first non white space or quote */
575 for(p = name + strlen(name) - 1; p >= name ;p--) {
576 c = *p;
577 if(c == '"') {
578 if(p - 1 == name) return NULL;
579 *p = 0;
580 return name;
581 }
582 if(isspace((unsigned char)c)) *p = 0;
583 else return name;
584 }
585 return NULL;
586}
587
588static MIME_HEADER *mime_hdr_new(char *name, char *value)
589{
590 MIME_HEADER *mhdr;
591 char *tmpname, *tmpval, *p;
592 int c;
593 if(name) {
594 if(!(tmpname = BUF_strdup(name))) return NULL;
595 for(p = tmpname ; *p; p++) {
596 c = *p;
597 if(isupper(c)) {
598 c = tolower(c);
599 *p = c;
600 }
601 }
602 } else tmpname = NULL;
603 if(value) {
604 if(!(tmpval = BUF_strdup(value))) return NULL;
605 for(p = tmpval ; *p; p++) {
606 c = *p;
607 if(isupper(c)) {
608 c = tolower(c);
609 *p = c;
610 }
611 }
612 } else tmpval = NULL;
613 mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER));
614 if(!mhdr) return NULL;
615 mhdr->name = tmpname;
616 mhdr->value = tmpval;
617 if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL;
618 return mhdr;
619}
620
621static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
622{
623 char *tmpname, *tmpval, *p;
624 int c;
625 MIME_PARAM *mparam;
626 if(name) {
627 tmpname = BUF_strdup(name);
628 if(!tmpname) return 0;
629 for(p = tmpname ; *p; p++) {
630 c = *p;
631 if(isupper(c)) {
632 c = tolower(c);
633 *p = c;
634 }
635 }
636 } else tmpname = NULL;
637 if(value) {
638 tmpval = BUF_strdup(value);
639 if(!tmpval) return 0;
640 } else tmpval = NULL;
641 /* Parameter values are case sensitive so leave as is */
642 mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
643 if(!mparam) return 0;
644 mparam->param_name = tmpname;
645 mparam->param_value = tmpval;
646 sk_MIME_PARAM_push(mhdr->params, mparam);
647 return 1;
648}
649
650static int mime_hdr_cmp(const MIME_HEADER * const *a,
651 const MIME_HEADER * const *b)
652{
653 return(strcmp((*a)->name, (*b)->name));
654}
655
656static int mime_param_cmp(const MIME_PARAM * const *a,
657 const MIME_PARAM * const *b)
658{
659 return(strcmp((*a)->param_name, (*b)->param_name));
660}
661
662/* Find a header with a given name (if possible) */
663
664static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
665{
666 MIME_HEADER htmp;
667 int idx;
668 htmp.name = name;
669 idx = sk_MIME_HEADER_find(hdrs, &htmp);
670 if(idx < 0) return NULL;
671 return sk_MIME_HEADER_value(hdrs, idx);
672}
673
674static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
675{
676 MIME_PARAM param;
677 int idx;
678 param.param_name = name;
679 idx = sk_MIME_PARAM_find(hdr->params, &param);
680 if(idx < 0) return NULL;
681 return sk_MIME_PARAM_value(hdr->params, idx);
682}
683
684static void mime_hdr_free(MIME_HEADER *hdr)
685{
686 if(hdr->name) OPENSSL_free(hdr->name);
687 if(hdr->value) OPENSSL_free(hdr->value);
688 if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
689 OPENSSL_free(hdr);
690}
691
692static void mime_param_free(MIME_PARAM *param)
693{
694 if(param->param_name) OPENSSL_free(param->param_name);
695 if(param->param_value) OPENSSL_free(param->param_value);
696 OPENSSL_free(param);
697}
698
699/* Check for a multipart boundary. Returns:
700 * 0 : no boundary
701 * 1 : part boundary
702 * 2 : final boundary
703 */
704static int mime_bound_check(char *line, int linelen, char *bound, int blen)
705{
706 if(linelen == -1) linelen = strlen(line);
707 if(blen == -1) blen = strlen(bound);
708 /* Quickly eliminate if line length too short */
709 if(blen + 2 > linelen) return 0;
710 /* Check for part boundary */
711 if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
712 if(!strncmp(line + blen + 2, "--", 2)) return 2;
713 else return 1;
714 }
715 return 0;
716}
717
718static int strip_eol(char *linebuf, int *plen)
719 {
720 int len = *plen;
721 char *p, c;
722 int is_eol = 0;
723 p = linebuf + len - 1;
724 for (p = linebuf + len - 1; len > 0; len--, p--)
725 {
726 c = *p;
727 if (c == '\n')
728 is_eol = 1;
729 else if (c != '\r')
730 break;
731 }
732 *plen = len;
733 return is_eol;
734 }