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