/src/openssl33/crypto/asn1/asn_mime.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "crypto/ctype.h" |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/rand.h> |
14 | | #include <openssl/x509.h> |
15 | | #include <openssl/asn1.h> |
16 | | #include <openssl/asn1t.h> |
17 | | #include <openssl/cms.h> |
18 | | #include "crypto/evp.h" |
19 | | #include "internal/bio.h" |
20 | | #include "asn1_local.h" |
21 | | |
22 | | /* |
23 | | * Generalised MIME like utilities for streaming ASN1. Although many have a |
24 | | * PKCS7/CMS like flavour others are more general purpose. |
25 | | */ |
26 | | |
27 | | /* |
28 | | * MIME format structures Note that all are translated to lower case apart |
29 | | * from parameter values. Quotes are stripped off |
30 | | */ |
31 | | |
32 | | struct mime_param_st { |
33 | | char *param_name; /* Param name e.g. "micalg" */ |
34 | | char *param_value; /* Param value e.g. "sha1" */ |
35 | | }; |
36 | | |
37 | | struct mime_header_st { |
38 | | char *name; /* Name of line e.g. "content-type" */ |
39 | | char *value; /* Value of line e.g. "text/plain" */ |
40 | | STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */ |
41 | | }; |
42 | | |
43 | | static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, |
44 | | const ASN1_ITEM *it); |
45 | | static char *strip_ends(char *name); |
46 | | static char *strip_start(char *name); |
47 | | static char *strip_end(char *name); |
48 | | static MIME_HEADER *mime_hdr_new(const char *name, const char *value); |
49 | | static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value); |
50 | | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio); |
51 | | static int mime_hdr_cmp(const MIME_HEADER *const *a, |
52 | | const MIME_HEADER *const *b); |
53 | | static int mime_param_cmp(const MIME_PARAM *const *a, |
54 | | const MIME_PARAM *const *b); |
55 | | static void mime_param_free(MIME_PARAM *param); |
56 | | static int mime_bound_check(char *line, int linelen, const char *bound, int blen); |
57 | | static int multi_split(BIO *bio, int flags, const char *bound, STACK_OF(BIO) **ret); |
58 | | static int strip_eol(char *linebuf, int *plen, int flags); |
59 | | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name); |
60 | | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name); |
61 | | static void mime_hdr_free(MIME_HEADER *hdr); |
62 | | |
63 | 60.2M | #define MAX_SMLEN 1024 |
64 | | #define mime_debug(x) /* x */ |
65 | | |
66 | | /* Output an ASN1 structure in BER format streaming if necessary */ |
67 | | |
68 | | /* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */ |
69 | | int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, |
70 | | const ASN1_ITEM *it) |
71 | 0 | { |
72 | 0 | int rv = 1; |
73 | | |
74 | | /* If streaming create stream BIO and copy all content through it */ |
75 | 0 | if (flags & SMIME_STREAM) { |
76 | 0 | BIO *bio, *tbio; |
77 | 0 | bio = BIO_new_NDEF(out, val, it); |
78 | 0 | if (!bio) { |
79 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB); |
80 | 0 | return 0; |
81 | 0 | } |
82 | 0 | if (!SMIME_crlf_copy(in, bio, flags)) { |
83 | 0 | rv = 0; |
84 | 0 | } |
85 | |
|
86 | 0 | (void)BIO_flush(bio); |
87 | | /* Free up successive BIOs until we hit the old output BIO */ |
88 | 0 | do { |
89 | 0 | tbio = BIO_pop(bio); |
90 | 0 | BIO_free(bio); |
91 | 0 | bio = tbio; |
92 | 0 | } while (bio != out); |
93 | 0 | } |
94 | | /* |
95 | | * else just write out ASN1 structure which will have all content stored |
96 | | * internally |
97 | | */ |
98 | 0 | else |
99 | 0 | rv = ASN1_item_i2d_bio(it, out, val); |
100 | 0 | return rv; |
101 | 0 | } |
102 | | |
103 | | /* Base 64 read and write of ASN1 structure */ |
104 | | |
105 | | static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags, |
106 | | const ASN1_ITEM *it) |
107 | 0 | { |
108 | 0 | BIO *b64; |
109 | 0 | int r; |
110 | 0 | b64 = BIO_new(BIO_f_base64()); |
111 | 0 | if (b64 == NULL) { |
112 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BIO_LIB); |
113 | 0 | return 0; |
114 | 0 | } |
115 | | /* |
116 | | * prepend the b64 BIO so all data is base64 encoded. |
117 | | */ |
118 | 0 | out = BIO_push(b64, out); |
119 | 0 | r = i2d_ASN1_bio_stream(out, val, in, flags, it); |
120 | 0 | (void)BIO_flush(out); |
121 | 0 | BIO_pop(out); |
122 | 0 | BIO_free(b64); |
123 | 0 | return r; |
124 | 0 | } |
125 | | |
126 | | /* Streaming ASN1 PEM write */ |
127 | | |
128 | | int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, |
129 | | const char *hdr, const ASN1_ITEM *it) |
130 | 0 | { |
131 | 0 | int r; |
132 | 0 | BIO_printf(out, "-----BEGIN %s-----\n", hdr); |
133 | 0 | r = B64_write_ASN1(out, val, in, flags, it); |
134 | 0 | BIO_printf(out, "-----END %s-----\n", hdr); |
135 | 0 | return r; |
136 | 0 | } |
137 | | |
138 | | static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it, ASN1_VALUE **x, |
139 | | OSSL_LIB_CTX *libctx, const char *propq) |
140 | 12.3k | { |
141 | 12.3k | BIO *b64; |
142 | 12.3k | ASN1_VALUE *val; |
143 | | |
144 | 12.3k | if ((b64 = BIO_new(BIO_f_base64())) == NULL) { |
145 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BIO_LIB); |
146 | 0 | return 0; |
147 | 0 | } |
148 | 12.3k | bio = BIO_push(b64, bio); |
149 | 12.3k | val = ASN1_item_d2i_bio_ex(it, bio, x, libctx, propq); |
150 | 12.3k | if (!val) |
151 | 12.3k | ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR); |
152 | 12.3k | (void)BIO_flush(bio); |
153 | 12.3k | BIO_pop(bio); |
154 | 12.3k | BIO_free(b64); |
155 | 12.3k | return val; |
156 | 12.3k | } |
157 | | |
158 | | /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */ |
159 | | |
160 | | static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs) |
161 | 0 | { |
162 | 0 | const EVP_MD *md; |
163 | 0 | int i, have_unknown = 0, write_comma, ret = 0, md_nid; |
164 | 0 | have_unknown = 0; |
165 | 0 | write_comma = 0; |
166 | 0 | for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) { |
167 | 0 | if (write_comma) |
168 | 0 | BIO_write(out, ",", 1); |
169 | 0 | write_comma = 1; |
170 | 0 | md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm); |
171 | | |
172 | | /* RFC 8702 does not define a micalg for SHAKE, assuming "shake-<bitlen>" */ |
173 | 0 | if (md_nid == NID_shake128) { |
174 | 0 | if (BIO_puts(out, "shake-128") < 0) |
175 | 0 | goto err; |
176 | 0 | continue; |
177 | 0 | } |
178 | 0 | if (md_nid == NID_shake256) { |
179 | 0 | if (BIO_puts(out, "shake-256") < 0) |
180 | 0 | goto err; |
181 | 0 | continue; |
182 | 0 | } |
183 | | |
184 | 0 | md = EVP_get_digestbynid(md_nid); |
185 | 0 | if (md && md->md_ctrl) { |
186 | 0 | int rv; |
187 | 0 | char *micstr; |
188 | 0 | rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr); |
189 | 0 | if (rv > 0) { |
190 | 0 | BIO_puts(out, micstr); |
191 | 0 | OPENSSL_free(micstr); |
192 | 0 | continue; |
193 | 0 | } |
194 | 0 | if (rv != -2) |
195 | 0 | goto err; |
196 | 0 | } |
197 | 0 | switch (md_nid) { |
198 | 0 | case NID_sha1: |
199 | 0 | BIO_puts(out, "sha1"); |
200 | 0 | break; |
201 | | |
202 | 0 | case NID_md5: |
203 | 0 | BIO_puts(out, "md5"); |
204 | 0 | break; |
205 | | |
206 | 0 | case NID_sha256: |
207 | 0 | BIO_puts(out, "sha-256"); |
208 | 0 | break; |
209 | | |
210 | 0 | case NID_sha384: |
211 | 0 | BIO_puts(out, "sha-384"); |
212 | 0 | break; |
213 | | |
214 | 0 | case NID_sha512: |
215 | 0 | BIO_puts(out, "sha-512"); |
216 | 0 | break; |
217 | | |
218 | 0 | case NID_id_GostR3411_94: |
219 | 0 | BIO_puts(out, "gostr3411-94"); |
220 | 0 | break; |
221 | | |
222 | 0 | case NID_id_GostR3411_2012_256: |
223 | 0 | BIO_puts(out, "gostr3411-2012-256"); |
224 | 0 | break; |
225 | | |
226 | 0 | case NID_id_GostR3411_2012_512: |
227 | 0 | BIO_puts(out, "gostr3411-2012-512"); |
228 | 0 | break; |
229 | | |
230 | 0 | default: |
231 | 0 | if (have_unknown) { |
232 | 0 | write_comma = 0; |
233 | 0 | } else { |
234 | 0 | BIO_puts(out, "unknown"); |
235 | 0 | have_unknown = 1; |
236 | 0 | } |
237 | 0 | break; |
238 | 0 | } |
239 | 0 | } |
240 | | |
241 | 0 | ret = 1; |
242 | 0 | err: |
243 | |
|
244 | 0 | return ret; |
245 | 0 | } |
246 | | |
247 | | /* SMIME sender */ |
248 | | |
249 | | int SMIME_write_ASN1_ex(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, |
250 | | int ctype_nid, int econt_nid, |
251 | | STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it, |
252 | | OSSL_LIB_CTX *libctx, const char *propq) |
253 | 0 | { |
254 | 0 | char bound[33], c; |
255 | 0 | int i; |
256 | 0 | const char *mime_prefix, *mime_eol, *cname = "smime.p7m"; |
257 | 0 | const char *msg_type = NULL; |
258 | |
|
259 | 0 | if (flags & SMIME_OLDMIME) |
260 | 0 | mime_prefix = "application/x-pkcs7-"; |
261 | 0 | else |
262 | 0 | mime_prefix = "application/pkcs7-"; |
263 | |
|
264 | 0 | if (flags & SMIME_CRLFEOL) |
265 | 0 | mime_eol = "\r\n"; |
266 | 0 | else |
267 | 0 | mime_eol = "\n"; |
268 | 0 | if ((flags & SMIME_DETACHED) && data) { |
269 | | /* We want multipart/signed */ |
270 | | /* Generate a random boundary */ |
271 | 0 | if (RAND_bytes_ex(libctx, (unsigned char *)bound, 32, 0) <= 0) |
272 | 0 | return 0; |
273 | 0 | for (i = 0; i < 32; i++) { |
274 | 0 | c = bound[i] & 0xf; |
275 | 0 | if (c < 10) |
276 | 0 | c += '0'; |
277 | 0 | else |
278 | 0 | c += 'A' - 10; |
279 | 0 | bound[i] = c; |
280 | 0 | } |
281 | 0 | bound[32] = 0; |
282 | 0 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
283 | 0 | BIO_printf(bio, "Content-Type: multipart/signed;"); |
284 | 0 | BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix); |
285 | 0 | BIO_puts(bio, " micalg=\""); |
286 | 0 | if (!asn1_write_micalg(bio, mdalgs)) |
287 | 0 | return 0; |
288 | 0 | BIO_printf(bio, "\"; boundary=\"----%s\"%s%s", |
289 | 0 | bound, mime_eol, mime_eol); |
290 | 0 | BIO_printf(bio, "This is an S/MIME signed message%s%s", |
291 | 0 | mime_eol, mime_eol); |
292 | | /* Now write out the first part */ |
293 | 0 | BIO_printf(bio, "------%s%s", bound, mime_eol); |
294 | 0 | if (!asn1_output_data(bio, data, val, flags, it)) |
295 | 0 | return 0; |
296 | 0 | BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol); |
297 | | |
298 | | /* Headers for signature */ |
299 | |
|
300 | 0 | BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); |
301 | 0 | BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); |
302 | 0 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s", mime_eol); |
303 | 0 | BIO_printf(bio, "Content-Disposition: attachment;"); |
304 | 0 | BIO_printf(bio, " filename=\"smime.p7s\"%s%s", mime_eol, mime_eol); |
305 | 0 | B64_write_ASN1(bio, val, NULL, 0, it); |
306 | 0 | BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound, |
307 | 0 | mime_eol, mime_eol); |
308 | 0 | return 1; |
309 | 0 | } |
310 | | |
311 | | /* Determine smime-type header */ |
312 | | |
313 | 0 | if (ctype_nid == NID_pkcs7_enveloped) { |
314 | 0 | msg_type = "enveloped-data"; |
315 | 0 | } else if (ctype_nid == NID_id_smime_ct_authEnvelopedData) { |
316 | 0 | msg_type = "authEnveloped-data"; |
317 | 0 | } else if (ctype_nid == NID_pkcs7_signed) { |
318 | 0 | if (econt_nid == NID_id_smime_ct_receipt) |
319 | 0 | msg_type = "signed-receipt"; |
320 | 0 | else if (sk_X509_ALGOR_num(mdalgs) >= 0) |
321 | 0 | msg_type = "signed-data"; |
322 | 0 | else |
323 | 0 | msg_type = "certs-only"; |
324 | 0 | } else if (ctype_nid == NID_id_smime_ct_compressedData) { |
325 | 0 | msg_type = "compressed-data"; |
326 | 0 | cname = "smime.p7z"; |
327 | 0 | } |
328 | | /* MIME headers */ |
329 | 0 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
330 | 0 | BIO_printf(bio, "Content-Disposition: attachment;"); |
331 | 0 | BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol); |
332 | 0 | BIO_printf(bio, "Content-Type: %smime;", mime_prefix); |
333 | 0 | if (msg_type) |
334 | 0 | BIO_printf(bio, " smime-type=%s;", msg_type); |
335 | 0 | BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol); |
336 | 0 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", |
337 | 0 | mime_eol, mime_eol); |
338 | 0 | if (!B64_write_ASN1(bio, val, data, flags, it)) |
339 | 0 | return 0; |
340 | 0 | BIO_printf(bio, "%s", mime_eol); |
341 | 0 | return 1; |
342 | 0 | } |
343 | | |
344 | | int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, |
345 | | int ctype_nid, int econt_nid, |
346 | | STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it) |
347 | 0 | { |
348 | 0 | return SMIME_write_ASN1_ex(bio, val, data, flags, ctype_nid, econt_nid, |
349 | 0 | mdalgs, it, NULL, NULL); |
350 | 0 | } |
351 | | |
352 | | /* Handle output of ASN1 data */ |
353 | | |
354 | | /* cannot constify val because of CMS_dataFinal() */ |
355 | | static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, |
356 | | const ASN1_ITEM *it) |
357 | 0 | { |
358 | 0 | BIO *tmpbio; |
359 | 0 | const ASN1_AUX *aux = it->funcs; |
360 | 0 | ASN1_STREAM_ARG sarg; |
361 | 0 | int rv = 1; |
362 | | |
363 | | /* |
364 | | * If data is not detached or resigning then the output BIO is already |
365 | | * set up to finalise when it is written through. |
366 | | */ |
367 | 0 | if (!(flags & SMIME_DETACHED) || (flags & PKCS7_REUSE_DIGEST)) { |
368 | 0 | return SMIME_crlf_copy(data, out, flags); |
369 | 0 | } |
370 | | |
371 | 0 | if (!aux || !aux->asn1_cb) { |
372 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STREAMING_NOT_SUPPORTED); |
373 | 0 | return 0; |
374 | 0 | } |
375 | | |
376 | 0 | sarg.out = out; |
377 | 0 | sarg.ndef_bio = NULL; |
378 | 0 | sarg.boundary = NULL; |
379 | | |
380 | | /* Let ASN1 code prepend any needed BIOs */ |
381 | |
|
382 | 0 | if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0) |
383 | 0 | return 0; |
384 | | |
385 | | /* Copy data across, passing through filter BIOs for processing */ |
386 | 0 | if (!SMIME_crlf_copy(data, sarg.ndef_bio, flags)) |
387 | 0 | rv = 0; |
388 | | |
389 | | /* Finalize structure */ |
390 | 0 | if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0) |
391 | 0 | rv = 0; |
392 | | |
393 | | /* Now remove any digests prepended to the BIO */ |
394 | |
|
395 | 0 | while (sarg.ndef_bio != out) { |
396 | 0 | tmpbio = BIO_pop(sarg.ndef_bio); |
397 | 0 | BIO_free(sarg.ndef_bio); |
398 | 0 | sarg.ndef_bio = tmpbio; |
399 | 0 | } |
400 | |
|
401 | 0 | return rv; |
402 | 0 | } |
403 | | |
404 | | /* |
405 | | * SMIME reader: handle multipart/signed and opaque signing. in multipart |
406 | | * case the content is placed in a memory BIO pointed to by "bcont". In |
407 | | * opaque this is set to NULL |
408 | | */ |
409 | | |
410 | | ASN1_VALUE *SMIME_read_ASN1_ex(BIO *bio, int flags, BIO **bcont, |
411 | | const ASN1_ITEM *it, ASN1_VALUE **x, |
412 | | OSSL_LIB_CTX *libctx, const char *propq) |
413 | 22.3k | { |
414 | 22.3k | BIO *asnin; |
415 | 22.3k | STACK_OF(MIME_HEADER) *headers = NULL; |
416 | 22.3k | STACK_OF(BIO) *parts = NULL; |
417 | 22.3k | MIME_HEADER *hdr; |
418 | 22.3k | MIME_PARAM *prm; |
419 | 22.3k | ASN1_VALUE *val; |
420 | 22.3k | int ret; |
421 | | |
422 | 22.3k | if (bcont) |
423 | 0 | *bcont = NULL; |
424 | | |
425 | 22.3k | if ((headers = mime_parse_hdr(bio)) == NULL) { |
426 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_PARSE_ERROR); |
427 | 0 | return NULL; |
428 | 0 | } |
429 | | |
430 | 22.3k | if ((hdr = mime_hdr_find(headers, "content-type")) == NULL |
431 | 18.1k | || hdr->value == NULL) { |
432 | 4.81k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
433 | 4.81k | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_CONTENT_TYPE); |
434 | 4.81k | return NULL; |
435 | 4.81k | } |
436 | | |
437 | | /* Handle multipart/signed */ |
438 | | |
439 | 17.5k | if (strcmp(hdr->value, "multipart/signed") == 0) { |
440 | | /* Split into two parts */ |
441 | 5.52k | prm = mime_param_find(hdr, "boundary"); |
442 | 5.52k | if (prm == NULL || prm->param_value == NULL) { |
443 | 49 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
444 | 49 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY); |
445 | 49 | return NULL; |
446 | 49 | } |
447 | 5.47k | ret = multi_split(bio, flags, prm->param_value, &parts); |
448 | 5.47k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
449 | 5.47k | if (!ret || (sk_BIO_num(parts) != 2)) { |
450 | 2.19k | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE); |
451 | 2.19k | sk_BIO_pop_free(parts, BIO_vfree); |
452 | 2.19k | return NULL; |
453 | 2.19k | } |
454 | | |
455 | | /* Parse the signature piece */ |
456 | 3.28k | asnin = sk_BIO_value(parts, 1); |
457 | | |
458 | 3.28k | if ((headers = mime_parse_hdr(asnin)) == NULL) { |
459 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_SIG_PARSE_ERROR); |
460 | 0 | sk_BIO_pop_free(parts, BIO_vfree); |
461 | 0 | return NULL; |
462 | 0 | } |
463 | | |
464 | | /* Get content type */ |
465 | | |
466 | 3.28k | if ((hdr = mime_hdr_find(headers, "content-type")) == NULL |
467 | 3.09k | || hdr->value == NULL) { |
468 | 202 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
469 | 202 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE); |
470 | 202 | sk_BIO_pop_free(parts, BIO_vfree); |
471 | 202 | return NULL; |
472 | 202 | } |
473 | | |
474 | 3.07k | if (strcmp(hdr->value, "application/x-pkcs7-signature") && strcmp(hdr->value, "application/pkcs7-signature")) { |
475 | 922 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_SIG_INVALID_MIME_TYPE, |
476 | 922 | "type: %s", hdr->value); |
477 | 922 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
478 | 922 | sk_BIO_pop_free(parts, BIO_vfree); |
479 | 922 | return NULL; |
480 | 922 | } |
481 | 2.15k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
482 | | /* Read in ASN1 */ |
483 | 2.15k | if ((val = b64_read_asn1(asnin, it, x, libctx, propq)) == NULL) { |
484 | 1.98k | ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_SIG_PARSE_ERROR); |
485 | 1.98k | sk_BIO_pop_free(parts, BIO_vfree); |
486 | 1.98k | return NULL; |
487 | 1.98k | } |
488 | | |
489 | 176 | if (bcont) { |
490 | 0 | *bcont = sk_BIO_value(parts, 0); |
491 | 0 | BIO_free(asnin); |
492 | 0 | sk_BIO_free(parts); |
493 | 176 | } else { |
494 | 176 | sk_BIO_pop_free(parts, BIO_vfree); |
495 | 176 | } |
496 | 176 | return val; |
497 | 2.15k | } |
498 | | |
499 | | /* OK, if not multipart/signed try opaque signature */ |
500 | | |
501 | 12.0k | if (strcmp(hdr->value, "application/x-pkcs7-mime") && strcmp(hdr->value, "application/pkcs7-mime")) { |
502 | 1.81k | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MIME_TYPE, |
503 | 1.81k | "type: %s", hdr->value); |
504 | 1.81k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
505 | 1.81k | return NULL; |
506 | 1.81k | } |
507 | | |
508 | 10.2k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
509 | | |
510 | 10.2k | if ((val = b64_read_asn1(bio, it, x, libctx, propq)) == NULL) { |
511 | 9.52k | ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_PARSE_ERROR); |
512 | 9.52k | return NULL; |
513 | 9.52k | } |
514 | 687 | return val; |
515 | 10.2k | } |
516 | | |
517 | | ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it) |
518 | 0 | { |
519 | 0 | return SMIME_read_ASN1_ex(bio, 0, bcont, it, NULL, NULL, NULL); |
520 | 0 | } |
521 | | |
522 | | /* Copy text from one BIO to another making the output CRLF at EOL */ |
523 | | int SMIME_crlf_copy(BIO *in, BIO *out, int flags) |
524 | 0 | { |
525 | 0 | BIO *bf; |
526 | 0 | char eol; |
527 | 0 | int len; |
528 | 0 | char linebuf[MAX_SMLEN]; |
529 | 0 | int ret; |
530 | |
|
531 | 0 | if (in == NULL || out == NULL) { |
532 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); |
533 | 0 | return 0; |
534 | 0 | } |
535 | | |
536 | | /* |
537 | | * Buffer output so we don't write one line at a time. This is useful |
538 | | * when streaming as we don't end up with one OCTET STRING per line. |
539 | | */ |
540 | 0 | bf = BIO_new(BIO_f_buffer()); |
541 | 0 | if (bf == NULL) { |
542 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BIO_LIB); |
543 | 0 | return 0; |
544 | 0 | } |
545 | 0 | out = BIO_push(bf, out); |
546 | 0 | if (flags & SMIME_BINARY) { |
547 | 0 | while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) |
548 | 0 | BIO_write(out, linebuf, len); |
549 | 0 | } else { |
550 | 0 | int eolcnt = 0; |
551 | 0 | if (flags & SMIME_TEXT) |
552 | 0 | BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); |
553 | 0 | while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { |
554 | 0 | eol = strip_eol(linebuf, &len, flags); |
555 | 0 | if (len > 0) { |
556 | | /* Not EOF: write out all CRLF */ |
557 | 0 | if (flags & SMIME_ASCIICRLF) { |
558 | 0 | int i; |
559 | 0 | for (i = 0; i < eolcnt; i++) |
560 | 0 | BIO_write(out, "\r\n", 2); |
561 | 0 | eolcnt = 0; |
562 | 0 | } |
563 | 0 | BIO_write(out, linebuf, len); |
564 | 0 | if (eol) |
565 | 0 | BIO_write(out, "\r\n", 2); |
566 | 0 | } else if (flags & SMIME_ASCIICRLF) { |
567 | 0 | eolcnt++; |
568 | 0 | } else if (eol) { |
569 | 0 | BIO_write(out, "\r\n", 2); |
570 | 0 | } |
571 | 0 | } |
572 | 0 | } |
573 | 0 | ret = BIO_flush(out); |
574 | 0 | BIO_pop(out); |
575 | 0 | BIO_free(bf); |
576 | 0 | if (ret <= 0) |
577 | 0 | return 0; |
578 | | |
579 | 0 | return 1; |
580 | 0 | } |
581 | | |
582 | | /* Strip off headers if they are text/plain */ |
583 | | int SMIME_text(BIO *in, BIO *out) |
584 | 0 | { |
585 | 0 | char iobuf[4096]; |
586 | 0 | int len; |
587 | 0 | STACK_OF(MIME_HEADER) *headers; |
588 | 0 | MIME_HEADER *hdr; |
589 | |
|
590 | 0 | if ((headers = mime_parse_hdr(in)) == NULL) { |
591 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_PARSE_ERROR); |
592 | 0 | return 0; |
593 | 0 | } |
594 | 0 | if ((hdr = mime_hdr_find(headers, "content-type")) == NULL |
595 | 0 | || hdr->value == NULL) { |
596 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_NO_CONTENT_TYPE); |
597 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
598 | 0 | return 0; |
599 | 0 | } |
600 | 0 | if (strcmp(hdr->value, "text/plain")) { |
601 | 0 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MIME_TYPE, |
602 | 0 | "type: %s", hdr->value); |
603 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
604 | 0 | return 0; |
605 | 0 | } |
606 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
607 | 0 | while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) |
608 | 0 | BIO_write(out, iobuf, len); |
609 | 0 | if (len < 0) |
610 | 0 | return 0; |
611 | 0 | return 1; |
612 | 0 | } |
613 | | |
614 | | /* |
615 | | * Split a multipart/XXX message body into component parts: result is |
616 | | * canonical parts in a STACK of bios |
617 | | */ |
618 | | |
619 | | static int multi_split(BIO *bio, int flags, const char *bound, STACK_OF(BIO) **ret) |
620 | 3.52k | { |
621 | 3.52k | char linebuf[MAX_SMLEN]; |
622 | 3.52k | int len, blen; |
623 | 3.52k | int eol = 0, next_eol = 0; |
624 | 3.52k | BIO *bpart = NULL; |
625 | 3.52k | STACK_OF(BIO) *parts; |
626 | 3.52k | char state, part, first; |
627 | | |
628 | 3.52k | blen = strlen(bound); |
629 | 3.52k | part = 0; |
630 | 3.52k | state = 0; |
631 | 3.52k | first = 1; |
632 | 3.52k | parts = sk_BIO_new_null(); |
633 | 3.52k | *ret = parts; |
634 | 3.52k | if (*ret == NULL) |
635 | 0 | return 0; |
636 | 36.2M | while ((len = BIO_get_line(bio, linebuf, MAX_SMLEN)) > 0) { |
637 | 36.2M | state = mime_bound_check(linebuf, len, bound, blen); |
638 | 36.2M | if (state == 1) { |
639 | 1.61M | first = 1; |
640 | 1.61M | part++; |
641 | 34.6M | } else if (state == 2) { |
642 | 2.22k | if (!sk_BIO_push(parts, bpart)) { |
643 | 0 | BIO_free(bpart); |
644 | 0 | return 0; |
645 | 0 | } |
646 | 2.22k | return 1; |
647 | 34.6M | } else if (part != 0) { |
648 | | /* Strip (possibly CR +) LF from linebuf */ |
649 | 34.5M | next_eol = strip_eol(linebuf, &len, flags); |
650 | 34.5M | if (first) { |
651 | 1.59M | first = 0; |
652 | 1.59M | if (bpart) |
653 | 1.58M | if (!sk_BIO_push(parts, bpart)) { |
654 | 0 | BIO_free(bpart); |
655 | 0 | return 0; |
656 | 0 | } |
657 | 1.59M | bpart = BIO_new(BIO_s_mem()); |
658 | 1.59M | if (bpart == NULL) |
659 | 0 | return 0; |
660 | 1.59M | BIO_set_mem_eof_return(bpart, 0); |
661 | 32.9M | } else if (eol) { |
662 | 32.9M | if ( |
663 | 32.9M | #ifndef OPENSSL_NO_CMS |
664 | 32.9M | (flags & CMS_BINARY) == 0 |
665 | | #else |
666 | | 1 |
667 | | #endif |
668 | 0 | || (flags & SMIME_CRLFEOL) != 0) |
669 | 32.9M | BIO_write(bpart, "\r\n", 2); |
670 | 0 | else |
671 | 0 | BIO_write(bpart, "\n", 1); |
672 | 32.9M | } |
673 | 34.5M | eol = next_eol; |
674 | 34.5M | if (len > 0) |
675 | 6.12M | BIO_write(bpart, linebuf, len); |
676 | 34.5M | } |
677 | 36.2M | } |
678 | 1.29k | BIO_free(bpart); |
679 | 1.29k | return 0; |
680 | 3.52k | } |
681 | | |
682 | | /* This is the big one: parse MIME header lines up to message body */ |
683 | | |
684 | | #define MIME_INVALID 0 |
685 | 56.2M | #define MIME_START 1 |
686 | 49.1M | #define MIME_TYPE 2 |
687 | 5.02M | #define MIME_NAME 3 |
688 | 4.02M | #define MIME_VALUE 4 |
689 | 244k | #define MIME_QUOTE 5 |
690 | 129k | #define MIME_COMMENT 6 |
691 | | |
692 | | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) |
693 | 25.6k | { |
694 | 25.6k | char *p, *q, c; |
695 | 25.6k | char *ntmp; |
696 | 25.6k | char linebuf[MAX_SMLEN]; |
697 | 25.6k | MIME_HEADER *mhdr = NULL, *new_hdr = NULL; |
698 | 25.6k | STACK_OF(MIME_HEADER) *headers; |
699 | 25.6k | int i, len, state, save_state = 0; |
700 | | |
701 | 25.6k | headers = sk_MIME_HEADER_new(mime_hdr_cmp); |
702 | 25.6k | if (headers == NULL) |
703 | 0 | return NULL; |
704 | 23.9M | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { |
705 | | /* If whitespace at line start then continuation line */ |
706 | 23.9M | if (mhdr && ossl_isspace(linebuf[0])) |
707 | 897k | state = MIME_NAME; |
708 | 23.0M | else |
709 | 23.0M | state = MIME_START; |
710 | 23.9M | ntmp = NULL; |
711 | | /* Go through all characters */ |
712 | 64.1M | for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); |
713 | 40.2M | p++) { |
714 | | |
715 | | /* |
716 | | * State machine to handle MIME headers if this looks horrible |
717 | | * that's because it *is* |
718 | | */ |
719 | | |
720 | 40.2M | switch (state) { |
721 | 33.1M | case MIME_START: |
722 | 33.1M | if (c == ':') { |
723 | 22.9M | state = MIME_TYPE; |
724 | 22.9M | *p = 0; |
725 | 22.9M | ntmp = strip_ends(q); |
726 | 22.9M | q = p + 1; |
727 | 22.9M | } |
728 | 33.1M | break; |
729 | | |
730 | 2.22M | case MIME_TYPE: |
731 | 2.22M | if (c == ';') { |
732 | 47.1k | mime_debug("Found End Value\n"); |
733 | 47.1k | *p = 0; |
734 | 47.1k | new_hdr = mime_hdr_new(ntmp, strip_ends(q)); |
735 | 47.1k | if (new_hdr == NULL) |
736 | 0 | goto err; |
737 | 47.1k | if (!sk_MIME_HEADER_push(headers, new_hdr)) |
738 | 0 | goto err; |
739 | 47.1k | mhdr = new_hdr; |
740 | 47.1k | new_hdr = NULL; |
741 | 47.1k | ntmp = NULL; |
742 | 47.1k | q = p + 1; |
743 | 47.1k | state = MIME_NAME; |
744 | 2.17M | } else if (c == '(') { |
745 | 48.2k | save_state = state; |
746 | 48.2k | state = MIME_COMMENT; |
747 | 48.2k | } |
748 | 2.22M | break; |
749 | | |
750 | 2.22M | case MIME_COMMENT: |
751 | 80.3k | if (c == ')') { |
752 | 48.7k | state = save_state; |
753 | 48.7k | } |
754 | 80.3k | break; |
755 | | |
756 | 3.27M | case MIME_NAME: |
757 | 3.27M | if (c == '=') { |
758 | 1.68M | state = MIME_VALUE; |
759 | 1.68M | *p = 0; |
760 | 1.68M | ntmp = strip_ends(q); |
761 | 1.68M | q = p + 1; |
762 | 1.68M | } |
763 | 3.27M | break; |
764 | | |
765 | 1.27M | case MIME_VALUE: |
766 | 1.27M | if (c == ';') { |
767 | 810k | state = MIME_NAME; |
768 | 810k | *p = 0; |
769 | 810k | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); |
770 | 810k | ntmp = NULL; |
771 | 810k | q = p + 1; |
772 | 810k | } else if (c == '"') { |
773 | 19.3k | mime_debug("Found Quote\n"); |
774 | 19.3k | state = MIME_QUOTE; |
775 | 444k | } else if (c == '(') { |
776 | 1.11k | save_state = state; |
777 | 1.11k | state = MIME_COMMENT; |
778 | 1.11k | } |
779 | 1.27M | break; |
780 | | |
781 | 225k | case MIME_QUOTE: |
782 | 225k | if (c == '"') { |
783 | 18.7k | mime_debug("Found Match Quote\n"); |
784 | 18.7k | state = MIME_VALUE; |
785 | 18.7k | } |
786 | 225k | break; |
787 | 40.2M | } |
788 | 40.2M | } |
789 | | |
790 | 23.9M | if (state == MIME_TYPE) { |
791 | 22.9M | new_hdr = mime_hdr_new(ntmp, strip_ends(q)); |
792 | 22.9M | if (new_hdr == NULL) |
793 | 0 | goto err; |
794 | 22.9M | if (!sk_MIME_HEADER_push(headers, new_hdr)) |
795 | 0 | goto err; |
796 | 22.9M | mhdr = new_hdr; |
797 | 22.9M | new_hdr = NULL; |
798 | 22.9M | } else if (state == MIME_VALUE) { |
799 | 870k | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); |
800 | 870k | } |
801 | 23.9M | if (p == linebuf) |
802 | 18.3k | break; /* Blank line means end of headers */ |
803 | 23.9M | } |
804 | | |
805 | | /* Sort the headers and their params for faster searching */ |
806 | 25.6k | sk_MIME_HEADER_sort(headers); |
807 | 22.9M | for (i = 0; i < sk_MIME_HEADER_num(headers); i++) |
808 | 22.9M | if ((mhdr = sk_MIME_HEADER_value(headers, i)) != NULL |
809 | 22.9M | && mhdr->params != NULL) |
810 | 22.9M | sk_MIME_PARAM_sort(mhdr->params); |
811 | 25.6k | return headers; |
812 | | |
813 | 0 | err: |
814 | 0 | mime_hdr_free(new_hdr); |
815 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
816 | 0 | return NULL; |
817 | 25.6k | } |
818 | | |
819 | | static char *strip_ends(char *name) |
820 | 49.2M | { |
821 | 49.2M | return strip_end(strip_start(name)); |
822 | 49.2M | } |
823 | | |
824 | | /* Strip a parameter of whitespace from start of param */ |
825 | | static char *strip_start(char *name) |
826 | 49.2M | { |
827 | 49.2M | char *p, c; |
828 | | /* Look for first non whitespace or quote */ |
829 | 74.5M | for (p = name; (c = *p); p++) { |
830 | 25.9M | if (c == '"') { |
831 | | /* Next char is start of string if non null */ |
832 | 105k | if (p[1]) |
833 | 79.9k | return p + 1; |
834 | | /* Else null string */ |
835 | 25.6k | return NULL; |
836 | 105k | } |
837 | 25.8M | if (!ossl_isspace(c)) |
838 | 616k | return p; |
839 | 25.8M | } |
840 | 48.5M | return NULL; |
841 | 49.2M | } |
842 | | |
843 | | /* As above but strip from end of string : maybe should handle brackets? */ |
844 | | static char *strip_end(char *name) |
845 | 40.6M | { |
846 | 40.6M | char *p, c; |
847 | 40.6M | if (!name) |
848 | 39.9M | return NULL; |
849 | | /* Look for first non whitespace or quote */ |
850 | 1.07M | for (p = name + strlen(name) - 1; p >= name; p--) { |
851 | 1.05M | c = *p; |
852 | 1.05M | if (c == '"') { |
853 | 55.0k | if (p - 1 == name) |
854 | 4.43k | return NULL; |
855 | 50.6k | *p = 0; |
856 | 50.6k | return name; |
857 | 55.0k | } |
858 | 997k | if (ossl_isspace(c)) |
859 | 409k | *p = 0; |
860 | 587k | else |
861 | 587k | return name; |
862 | 997k | } |
863 | 26.8k | return NULL; |
864 | 669k | } |
865 | | |
866 | | static MIME_HEADER *mime_hdr_new(const char *name, const char *value) |
867 | 22.9M | { |
868 | 22.9M | MIME_HEADER *mhdr = NULL; |
869 | 22.9M | char *tmpname = NULL, *tmpval = NULL, *p; |
870 | | |
871 | 22.9M | if (name) { |
872 | 151k | if ((tmpname = OPENSSL_strdup(name)) == NULL) |
873 | 0 | return NULL; |
874 | 1.16M | for (p = tmpname; *p; p++) |
875 | 1.00M | *p = ossl_tolower(*p); |
876 | 151k | } |
877 | 22.9M | if (value) { |
878 | 269k | if ((tmpval = OPENSSL_strdup(value)) == NULL) |
879 | 0 | goto err; |
880 | 2.41M | for (p = tmpval; *p; p++) |
881 | 2.14M | *p = ossl_tolower(*p); |
882 | 269k | } |
883 | 22.9M | mhdr = OPENSSL_malloc(sizeof(*mhdr)); |
884 | 22.9M | if (mhdr == NULL) |
885 | 0 | goto err; |
886 | 22.9M | mhdr->name = tmpname; |
887 | 22.9M | mhdr->value = tmpval; |
888 | 22.9M | if ((mhdr->params = sk_MIME_PARAM_new(mime_param_cmp)) == NULL) |
889 | 0 | goto err; |
890 | 22.9M | return mhdr; |
891 | | |
892 | 0 | err: |
893 | 0 | OPENSSL_free(tmpname); |
894 | 0 | OPENSSL_free(tmpval); |
895 | 0 | OPENSSL_free(mhdr); |
896 | 0 | return NULL; |
897 | 22.9M | } |
898 | | |
899 | | static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value) |
900 | 1.68M | { |
901 | 1.68M | char *tmpname = NULL, *tmpval = NULL, *p; |
902 | 1.68M | MIME_PARAM *mparam = NULL; |
903 | | |
904 | 1.68M | if (name) { |
905 | 95.1k | tmpname = OPENSSL_strdup(name); |
906 | 95.1k | if (!tmpname) |
907 | 0 | goto err; |
908 | 600k | for (p = tmpname; *p; p++) |
909 | 505k | *p = ossl_tolower(*p); |
910 | 95.1k | } |
911 | 1.68M | if (value) { |
912 | 148k | tmpval = OPENSSL_strdup(value); |
913 | 148k | if (!tmpval) |
914 | 0 | goto err; |
915 | 148k | } |
916 | | /* Parameter values are case sensitive so leave as is */ |
917 | 1.68M | mparam = OPENSSL_malloc(sizeof(*mparam)); |
918 | 1.68M | if (mparam == NULL) |
919 | 0 | goto err; |
920 | 1.68M | mparam->param_name = tmpname; |
921 | 1.68M | mparam->param_value = tmpval; |
922 | 1.68M | if (!sk_MIME_PARAM_push(mhdr->params, mparam)) |
923 | 0 | goto err; |
924 | 1.68M | return 1; |
925 | 0 | err: |
926 | 0 | OPENSSL_free(tmpname); |
927 | 0 | OPENSSL_free(tmpval); |
928 | 0 | OPENSSL_free(mparam); |
929 | 0 | return 0; |
930 | 1.68M | } |
931 | | |
932 | | static int mime_hdr_cmp(const MIME_HEADER *const *a, |
933 | | const MIME_HEADER *const *b) |
934 | 253M | { |
935 | 253M | if ((*a)->name == NULL || (*b)->name == NULL) |
936 | 251M | return ((*a)->name != NULL) - ((*b)->name != NULL); |
937 | | |
938 | 1.46M | return strcmp((*a)->name, (*b)->name); |
939 | 253M | } |
940 | | |
941 | | static int mime_param_cmp(const MIME_PARAM *const *a, |
942 | | const MIME_PARAM *const *b) |
943 | 22.8M | { |
944 | 22.8M | if ((*a)->param_name == NULL || (*b)->param_name == NULL) |
945 | 21.9M | return ((*a)->param_name != NULL) - ((*b)->param_name != NULL); |
946 | 877k | return strcmp((*a)->param_name, (*b)->param_name); |
947 | 22.8M | } |
948 | | |
949 | | /* Find a header with a given name (if possible) */ |
950 | | |
951 | | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name) |
952 | 25.6k | { |
953 | 25.6k | MIME_HEADER htmp; |
954 | 25.6k | int idx; |
955 | | |
956 | 25.6k | htmp.name = (char *)name; |
957 | 25.6k | htmp.value = NULL; |
958 | 25.6k | htmp.params = NULL; |
959 | | |
960 | 25.6k | idx = sk_MIME_HEADER_find(hdrs, &htmp); |
961 | 25.6k | return sk_MIME_HEADER_value(hdrs, idx); |
962 | 25.6k | } |
963 | | |
964 | | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name) |
965 | 5.52k | { |
966 | 5.52k | MIME_PARAM param; |
967 | 5.52k | int idx; |
968 | | |
969 | 5.52k | param.param_name = (char *)name; |
970 | 5.52k | param.param_value = NULL; |
971 | 5.52k | idx = sk_MIME_PARAM_find(hdr->params, ¶m); |
972 | 5.52k | return sk_MIME_PARAM_value(hdr->params, idx); |
973 | 5.52k | } |
974 | | |
975 | | static void mime_hdr_free(MIME_HEADER *hdr) |
976 | 22.9M | { |
977 | 22.9M | if (hdr == NULL) |
978 | 0 | return; |
979 | 22.9M | OPENSSL_free(hdr->name); |
980 | 22.9M | OPENSSL_free(hdr->value); |
981 | 22.9M | if (hdr->params) |
982 | 22.9M | sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); |
983 | 22.9M | OPENSSL_free(hdr); |
984 | 22.9M | } |
985 | | |
986 | | static void mime_param_free(MIME_PARAM *param) |
987 | 1.68M | { |
988 | 1.68M | OPENSSL_free(param->param_name); |
989 | 1.68M | OPENSSL_free(param->param_value); |
990 | 1.68M | OPENSSL_free(param); |
991 | 1.68M | } |
992 | | |
993 | | /*- |
994 | | * Check for a multipart boundary. Returns: |
995 | | * 0 : no boundary |
996 | | * 1 : part boundary |
997 | | * 2 : final boundary |
998 | | */ |
999 | | static int mime_bound_check(char *line, int linelen, const char *bound, int blen) |
1000 | 36.2M | { |
1001 | 36.2M | if (linelen == -1) |
1002 | 0 | linelen = strlen(line); |
1003 | 36.2M | if (blen == -1) |
1004 | 0 | blen = strlen(bound); |
1005 | | /* Quickly eliminate if line length too short */ |
1006 | 36.2M | if (blen + 2 > linelen) |
1007 | 34.1M | return 0; |
1008 | | /* Check for part boundary */ |
1009 | 2.12M | if ((CHECK_AND_SKIP_PREFIX(line, "--")) && strncmp(line, bound, blen) == 0) |
1010 | 1.62M | return HAS_PREFIX(line + blen, "--") ? 2 : 1; |
1011 | 503k | return 0; |
1012 | 2.12M | } |
1013 | | |
1014 | | static int strip_eol(char *linebuf, int *plen, int flags) |
1015 | 42.5M | { |
1016 | 42.5M | int len = *plen; |
1017 | 42.5M | char *p, c; |
1018 | 42.5M | int is_eol = 0; |
1019 | | |
1020 | 42.5M | #ifndef OPENSSL_NO_CMS |
1021 | 42.5M | if ((flags & CMS_BINARY) != 0) { |
1022 | 0 | if (len <= 0 || linebuf[len - 1] != '\n') |
1023 | 0 | return 0; |
1024 | 0 | if ((flags & SMIME_CRLFEOL) != 0) { |
1025 | 0 | if (len <= 1 || linebuf[len - 2] != '\r') |
1026 | 0 | return 0; |
1027 | 0 | len--; |
1028 | 0 | } |
1029 | 0 | len--; |
1030 | 0 | *plen = len; |
1031 | 0 | return 1; |
1032 | 0 | } |
1033 | 42.5M | #endif |
1034 | | |
1035 | 85.0M | for (p = linebuf + len - 1; len > 0; len--, p--) { |
1036 | 50.7M | c = *p; |
1037 | 50.7M | if (c == '\n') { |
1038 | 42.5M | is_eol = 1; |
1039 | 42.5M | } else if (is_eol && (flags & SMIME_ASCIICRLF) != 0 && c == 32) { |
1040 | | /* Strip trailing space on a line; 32 == ASCII for ' ' */ |
1041 | 0 | continue; |
1042 | 8.22M | } else if (c != '\r') { |
1043 | 8.21M | break; |
1044 | 8.21M | } |
1045 | 50.7M | } |
1046 | 42.5M | *plen = len; |
1047 | 42.5M | return is_eol; |
1048 | 42.5M | } |