/src/openssl31/crypto/asn1/asn_mime.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-2023 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 | 17.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_MALLOC_FAILURE); |
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 | 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_MALLOC_FAILURE); |
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 | 6.09k | { |
141 | 6.09k | BIO *b64; |
142 | 6.09k | ASN1_VALUE *val; |
143 | | |
144 | 6.09k | if ((b64 = BIO_new(BIO_f_base64())) == NULL) { |
145 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
146 | 0 | return 0; |
147 | 0 | } |
148 | 6.09k | bio = BIO_push(b64, bio); |
149 | 6.09k | val = ASN1_item_d2i_bio_ex(it, bio, x, libctx, propq); |
150 | 6.09k | if (!val) |
151 | 6.09k | ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR); |
152 | 6.09k | (void)BIO_flush(bio); |
153 | 6.09k | BIO_pop(bio); |
154 | 6.09k | BIO_free(b64); |
155 | 6.09k | return val; |
156 | 6.09k | } |
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 | 0 | md = EVP_get_digestbynid(md_nid); |
172 | 0 | if (md && md->md_ctrl) { |
173 | 0 | int rv; |
174 | 0 | char *micstr; |
175 | 0 | rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr); |
176 | 0 | if (rv > 0) { |
177 | 0 | BIO_puts(out, micstr); |
178 | 0 | OPENSSL_free(micstr); |
179 | 0 | continue; |
180 | 0 | } |
181 | 0 | if (rv != -2) |
182 | 0 | goto err; |
183 | 0 | } |
184 | 0 | switch (md_nid) { |
185 | 0 | case NID_sha1: |
186 | 0 | BIO_puts(out, "sha1"); |
187 | 0 | break; |
188 | | |
189 | 0 | case NID_md5: |
190 | 0 | BIO_puts(out, "md5"); |
191 | 0 | break; |
192 | | |
193 | 0 | case NID_sha256: |
194 | 0 | BIO_puts(out, "sha-256"); |
195 | 0 | break; |
196 | | |
197 | 0 | case NID_sha384: |
198 | 0 | BIO_puts(out, "sha-384"); |
199 | 0 | break; |
200 | | |
201 | 0 | case NID_sha512: |
202 | 0 | BIO_puts(out, "sha-512"); |
203 | 0 | break; |
204 | | |
205 | 0 | case NID_id_GostR3411_94: |
206 | 0 | BIO_puts(out, "gostr3411-94"); |
207 | 0 | goto err; |
208 | | |
209 | 0 | case NID_id_GostR3411_2012_256: |
210 | 0 | BIO_puts(out, "gostr3411-2012-256"); |
211 | 0 | goto err; |
212 | | |
213 | 0 | case NID_id_GostR3411_2012_512: |
214 | 0 | BIO_puts(out, "gostr3411-2012-512"); |
215 | 0 | goto err; |
216 | | |
217 | 0 | default: |
218 | 0 | if (have_unknown) { |
219 | 0 | write_comma = 0; |
220 | 0 | } else { |
221 | 0 | BIO_puts(out, "unknown"); |
222 | 0 | have_unknown = 1; |
223 | 0 | } |
224 | 0 | break; |
225 | |
|
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | 0 | ret = 1; |
230 | 0 | err: |
231 | |
|
232 | 0 | return ret; |
233 | |
|
234 | 0 | } |
235 | | |
236 | | /* SMIME sender */ |
237 | | |
238 | | int SMIME_write_ASN1_ex(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, |
239 | | int ctype_nid, int econt_nid, |
240 | | STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it, |
241 | | OSSL_LIB_CTX *libctx, const char *propq) |
242 | 0 | { |
243 | 0 | char bound[33], c; |
244 | 0 | int i; |
245 | 0 | const char *mime_prefix, *mime_eol, *cname = "smime.p7m"; |
246 | 0 | const char *msg_type = NULL; |
247 | |
|
248 | 0 | if (flags & SMIME_OLDMIME) |
249 | 0 | mime_prefix = "application/x-pkcs7-"; |
250 | 0 | else |
251 | 0 | mime_prefix = "application/pkcs7-"; |
252 | |
|
253 | 0 | if (flags & SMIME_CRLFEOL) |
254 | 0 | mime_eol = "\r\n"; |
255 | 0 | else |
256 | 0 | mime_eol = "\n"; |
257 | 0 | if ((flags & SMIME_DETACHED) && data) { |
258 | | /* We want multipart/signed */ |
259 | | /* Generate a random boundary */ |
260 | 0 | if (RAND_bytes_ex(libctx, (unsigned char *)bound, 32, 0) <= 0) |
261 | 0 | return 0; |
262 | 0 | for (i = 0; i < 32; i++) { |
263 | 0 | c = bound[i] & 0xf; |
264 | 0 | if (c < 10) |
265 | 0 | c += '0'; |
266 | 0 | else |
267 | 0 | c += 'A' - 10; |
268 | 0 | bound[i] = c; |
269 | 0 | } |
270 | 0 | bound[32] = 0; |
271 | 0 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
272 | 0 | BIO_printf(bio, "Content-Type: multipart/signed;"); |
273 | 0 | BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix); |
274 | 0 | BIO_puts(bio, " micalg=\""); |
275 | 0 | asn1_write_micalg(bio, mdalgs); |
276 | 0 | BIO_printf(bio, "\"; boundary=\"----%s\"%s%s", |
277 | 0 | bound, mime_eol, mime_eol); |
278 | 0 | BIO_printf(bio, "This is an S/MIME signed message%s%s", |
279 | 0 | mime_eol, mime_eol); |
280 | | /* Now write out the first part */ |
281 | 0 | BIO_printf(bio, "------%s%s", bound, mime_eol); |
282 | 0 | if (!asn1_output_data(bio, data, val, flags, it)) |
283 | 0 | return 0; |
284 | 0 | BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol); |
285 | | |
286 | | /* Headers for signature */ |
287 | |
|
288 | 0 | BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); |
289 | 0 | BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); |
290 | 0 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s", mime_eol); |
291 | 0 | BIO_printf(bio, "Content-Disposition: attachment;"); |
292 | 0 | BIO_printf(bio, " filename=\"smime.p7s\"%s%s", mime_eol, mime_eol); |
293 | 0 | B64_write_ASN1(bio, val, NULL, 0, it); |
294 | 0 | BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound, |
295 | 0 | mime_eol, mime_eol); |
296 | 0 | return 1; |
297 | 0 | } |
298 | | |
299 | | /* Determine smime-type header */ |
300 | | |
301 | 0 | if (ctype_nid == NID_pkcs7_enveloped) { |
302 | 0 | msg_type = "enveloped-data"; |
303 | 0 | } else if (ctype_nid == NID_id_smime_ct_authEnvelopedData) { |
304 | 0 | msg_type = "authEnveloped-data"; |
305 | 0 | } else if (ctype_nid == NID_pkcs7_signed) { |
306 | 0 | if (econt_nid == NID_id_smime_ct_receipt) |
307 | 0 | msg_type = "signed-receipt"; |
308 | 0 | else if (sk_X509_ALGOR_num(mdalgs) >= 0) |
309 | 0 | msg_type = "signed-data"; |
310 | 0 | else |
311 | 0 | msg_type = "certs-only"; |
312 | 0 | } else if (ctype_nid == NID_id_smime_ct_compressedData) { |
313 | 0 | msg_type = "compressed-data"; |
314 | 0 | cname = "smime.p7z"; |
315 | 0 | } |
316 | | /* MIME headers */ |
317 | 0 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); |
318 | 0 | BIO_printf(bio, "Content-Disposition: attachment;"); |
319 | 0 | BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol); |
320 | 0 | BIO_printf(bio, "Content-Type: %smime;", mime_prefix); |
321 | 0 | if (msg_type) |
322 | 0 | BIO_printf(bio, " smime-type=%s;", msg_type); |
323 | 0 | BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol); |
324 | 0 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", |
325 | 0 | mime_eol, mime_eol); |
326 | 0 | if (!B64_write_ASN1(bio, val, data, flags, it)) |
327 | 0 | return 0; |
328 | 0 | BIO_printf(bio, "%s", mime_eol); |
329 | 0 | return 1; |
330 | 0 | } |
331 | | |
332 | | int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, |
333 | | int ctype_nid, int econt_nid, |
334 | | STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it) |
335 | 0 | { |
336 | 0 | return SMIME_write_ASN1_ex(bio, val, data, flags, ctype_nid, econt_nid, |
337 | 0 | mdalgs, it, NULL, NULL); |
338 | 0 | } |
339 | | |
340 | | /* Handle output of ASN1 data */ |
341 | | |
342 | | /* cannot constify val because of CMS_dataFinal() */ |
343 | | static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, |
344 | | const ASN1_ITEM *it) |
345 | 0 | { |
346 | 0 | BIO *tmpbio; |
347 | 0 | const ASN1_AUX *aux = it->funcs; |
348 | 0 | ASN1_STREAM_ARG sarg; |
349 | 0 | int rv = 1; |
350 | | |
351 | | /* |
352 | | * If data is not detached or resigning then the output BIO is already |
353 | | * set up to finalise when it is written through. |
354 | | */ |
355 | 0 | if (!(flags & SMIME_DETACHED) || (flags & PKCS7_REUSE_DIGEST)) { |
356 | 0 | return SMIME_crlf_copy(data, out, flags); |
357 | 0 | } |
358 | | |
359 | 0 | if (!aux || !aux->asn1_cb) { |
360 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STREAMING_NOT_SUPPORTED); |
361 | 0 | return 0; |
362 | 0 | } |
363 | | |
364 | 0 | sarg.out = out; |
365 | 0 | sarg.ndef_bio = NULL; |
366 | 0 | sarg.boundary = NULL; |
367 | | |
368 | | /* Let ASN1 code prepend any needed BIOs */ |
369 | |
|
370 | 0 | if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0) |
371 | 0 | return 0; |
372 | | |
373 | | /* Copy data across, passing through filter BIOs for processing */ |
374 | 0 | if (!SMIME_crlf_copy(data, sarg.ndef_bio, flags)) |
375 | 0 | rv = 0; |
376 | | |
377 | | /* Finalize structure */ |
378 | 0 | if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0) |
379 | 0 | rv = 0; |
380 | | |
381 | | /* Now remove any digests prepended to the BIO */ |
382 | |
|
383 | 0 | while (sarg.ndef_bio != out) { |
384 | 0 | tmpbio = BIO_pop(sarg.ndef_bio); |
385 | 0 | BIO_free(sarg.ndef_bio); |
386 | 0 | sarg.ndef_bio = tmpbio; |
387 | 0 | } |
388 | |
|
389 | 0 | return rv; |
390 | |
|
391 | 0 | } |
392 | | |
393 | | /* |
394 | | * SMIME reader: handle multipart/signed and opaque signing. in multipart |
395 | | * case the content is placed in a memory BIO pointed to by "bcont". In |
396 | | * opaque this is set to NULL |
397 | | */ |
398 | | |
399 | | ASN1_VALUE *SMIME_read_ASN1_ex(BIO *bio, int flags, BIO **bcont, |
400 | | const ASN1_ITEM *it, ASN1_VALUE **x, |
401 | | OSSL_LIB_CTX *libctx, const char *propq) |
402 | 10.0k | { |
403 | 10.0k | BIO *asnin; |
404 | 10.0k | STACK_OF(MIME_HEADER) *headers = NULL; |
405 | 10.0k | STACK_OF(BIO) *parts = NULL; |
406 | 10.0k | MIME_HEADER *hdr; |
407 | 10.0k | MIME_PARAM *prm; |
408 | 10.0k | ASN1_VALUE *val; |
409 | 10.0k | int ret; |
410 | | |
411 | 10.0k | if (bcont) |
412 | 0 | *bcont = NULL; |
413 | | |
414 | 10.0k | if ((headers = mime_parse_hdr(bio)) == NULL) { |
415 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_PARSE_ERROR); |
416 | 0 | return NULL; |
417 | 0 | } |
418 | | |
419 | 10.0k | if ((hdr = mime_hdr_find(headers, "content-type")) == NULL |
420 | 10.0k | || hdr->value == NULL) { |
421 | 1.96k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
422 | 1.96k | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_CONTENT_TYPE); |
423 | 1.96k | return NULL; |
424 | 1.96k | } |
425 | | |
426 | | /* Handle multipart/signed */ |
427 | | |
428 | 8.13k | if (strcmp(hdr->value, "multipart/signed") == 0) { |
429 | | /* Split into two parts */ |
430 | 1.70k | prm = mime_param_find(hdr, "boundary"); |
431 | 1.70k | if (prm == NULL || prm->param_value == NULL) { |
432 | 14 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
433 | 14 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY); |
434 | 14 | return NULL; |
435 | 14 | } |
436 | 1.68k | ret = multi_split(bio, flags, prm->param_value, &parts); |
437 | 1.68k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
438 | 1.68k | if (!ret || (sk_BIO_num(parts) != 2)) { |
439 | 852 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE); |
440 | 852 | sk_BIO_pop_free(parts, BIO_vfree); |
441 | 852 | return NULL; |
442 | 852 | } |
443 | | |
444 | | /* Parse the signature piece */ |
445 | 837 | asnin = sk_BIO_value(parts, 1); |
446 | | |
447 | 837 | if ((headers = mime_parse_hdr(asnin)) == NULL) { |
448 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_SIG_PARSE_ERROR); |
449 | 0 | sk_BIO_pop_free(parts, BIO_vfree); |
450 | 0 | return NULL; |
451 | 0 | } |
452 | | |
453 | | /* Get content type */ |
454 | | |
455 | 837 | if ((hdr = mime_hdr_find(headers, "content-type")) == NULL |
456 | 837 | || hdr->value == NULL) { |
457 | 94 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
458 | 94 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE); |
459 | 94 | sk_BIO_pop_free(parts, BIO_vfree); |
460 | 94 | return NULL; |
461 | 94 | } |
462 | | |
463 | 743 | if (strcmp(hdr->value, "application/x-pkcs7-signature") && |
464 | 743 | strcmp(hdr->value, "application/pkcs7-signature")) { |
465 | 374 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_SIG_INVALID_MIME_TYPE, |
466 | 374 | "type: %s", hdr->value); |
467 | 374 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
468 | 374 | sk_BIO_pop_free(parts, BIO_vfree); |
469 | 374 | return NULL; |
470 | 374 | } |
471 | 369 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
472 | | /* Read in ASN1 */ |
473 | 369 | if ((val = b64_read_asn1(asnin, it, x, libctx, propq)) == NULL) { |
474 | 366 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_SIG_PARSE_ERROR); |
475 | 366 | sk_BIO_pop_free(parts, BIO_vfree); |
476 | 366 | return NULL; |
477 | 366 | } |
478 | | |
479 | 3 | if (bcont) { |
480 | 0 | *bcont = sk_BIO_value(parts, 0); |
481 | 0 | BIO_free(asnin); |
482 | 0 | sk_BIO_free(parts); |
483 | 3 | } else { |
484 | 3 | sk_BIO_pop_free(parts, BIO_vfree); |
485 | 3 | } |
486 | 3 | return val; |
487 | 369 | } |
488 | | |
489 | | /* OK, if not multipart/signed try opaque signature */ |
490 | | |
491 | 6.42k | if (strcmp(hdr->value, "application/x-pkcs7-mime") && |
492 | 6.42k | strcmp(hdr->value, "application/pkcs7-mime")) { |
493 | 708 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MIME_TYPE, |
494 | 708 | "type: %s", hdr->value); |
495 | 708 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
496 | 708 | return NULL; |
497 | 708 | } |
498 | | |
499 | 5.72k | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
500 | | |
501 | 5.72k | if ((val = b64_read_asn1(bio, it, x, libctx, propq)) == NULL) { |
502 | 5.32k | ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_PARSE_ERROR); |
503 | 5.32k | return NULL; |
504 | 5.32k | } |
505 | 396 | return val; |
506 | 5.72k | } |
507 | | |
508 | | ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it) |
509 | 0 | { |
510 | 0 | return SMIME_read_ASN1_ex(bio, 0, bcont, it, NULL, NULL, NULL); |
511 | 0 | } |
512 | | |
513 | | /* Copy text from one BIO to another making the output CRLF at EOL */ |
514 | | int SMIME_crlf_copy(BIO *in, BIO *out, int flags) |
515 | 0 | { |
516 | 0 | BIO *bf; |
517 | 0 | char eol; |
518 | 0 | int len; |
519 | 0 | char linebuf[MAX_SMLEN]; |
520 | 0 | int ret; |
521 | |
|
522 | 0 | if (in == NULL || out == NULL) { |
523 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); |
524 | 0 | return 0; |
525 | 0 | } |
526 | | |
527 | | /* |
528 | | * Buffer output so we don't write one line at a time. This is useful |
529 | | * when streaming as we don't end up with one OCTET STRING per line. |
530 | | */ |
531 | 0 | bf = BIO_new(BIO_f_buffer()); |
532 | 0 | if (bf == NULL) { |
533 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
534 | 0 | return 0; |
535 | 0 | } |
536 | 0 | out = BIO_push(bf, out); |
537 | 0 | if (flags & SMIME_BINARY) { |
538 | 0 | while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) |
539 | 0 | BIO_write(out, linebuf, len); |
540 | 0 | } else { |
541 | 0 | int eolcnt = 0; |
542 | 0 | if (flags & SMIME_TEXT) |
543 | 0 | BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); |
544 | 0 | while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { |
545 | 0 | eol = strip_eol(linebuf, &len, flags); |
546 | 0 | if (len > 0) { |
547 | | /* Not EOF: write out all CRLF */ |
548 | 0 | if (flags & SMIME_ASCIICRLF) { |
549 | 0 | int i; |
550 | 0 | for (i = 0; i < eolcnt; i++) |
551 | 0 | BIO_write(out, "\r\n", 2); |
552 | 0 | eolcnt = 0; |
553 | 0 | } |
554 | 0 | BIO_write(out, linebuf, len); |
555 | 0 | if (eol) |
556 | 0 | BIO_write(out, "\r\n", 2); |
557 | 0 | } else if (flags & SMIME_ASCIICRLF) { |
558 | 0 | eolcnt++; |
559 | 0 | } else if (eol) { |
560 | 0 | BIO_write(out, "\r\n", 2); |
561 | 0 | } |
562 | 0 | } |
563 | 0 | } |
564 | 0 | ret = BIO_flush(out); |
565 | 0 | BIO_pop(out); |
566 | 0 | BIO_free(bf); |
567 | 0 | if (ret <= 0) |
568 | 0 | return 0; |
569 | | |
570 | 0 | return 1; |
571 | 0 | } |
572 | | |
573 | | /* Strip off headers if they are text/plain */ |
574 | | int SMIME_text(BIO *in, BIO *out) |
575 | 0 | { |
576 | 0 | char iobuf[4096]; |
577 | 0 | int len; |
578 | 0 | STACK_OF(MIME_HEADER) *headers; |
579 | 0 | MIME_HEADER *hdr; |
580 | |
|
581 | 0 | if ((headers = mime_parse_hdr(in)) == NULL) { |
582 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_PARSE_ERROR); |
583 | 0 | return 0; |
584 | 0 | } |
585 | 0 | if ((hdr = mime_hdr_find(headers, "content-type")) == NULL |
586 | 0 | || hdr->value == NULL) { |
587 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MIME_NO_CONTENT_TYPE); |
588 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
589 | 0 | return 0; |
590 | 0 | } |
591 | 0 | if (strcmp(hdr->value, "text/plain")) { |
592 | 0 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MIME_TYPE, |
593 | 0 | "type: %s", hdr->value); |
594 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
595 | 0 | return 0; |
596 | 0 | } |
597 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
598 | 0 | while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) |
599 | 0 | BIO_write(out, iobuf, len); |
600 | 0 | if (len < 0) |
601 | 0 | return 0; |
602 | 0 | return 1; |
603 | 0 | } |
604 | | |
605 | | /* |
606 | | * Split a multipart/XXX message body into component parts: result is |
607 | | * canonical parts in a STACK of bios |
608 | | */ |
609 | | |
610 | | static int multi_split(BIO *bio, int flags, const char *bound, STACK_OF(BIO) **ret) |
611 | 847 | { |
612 | 847 | char linebuf[MAX_SMLEN]; |
613 | 847 | int len, blen; |
614 | 847 | int eol = 0, next_eol = 0; |
615 | 847 | BIO *bpart = NULL; |
616 | 847 | STACK_OF(BIO) *parts; |
617 | 847 | char state, part, first; |
618 | | |
619 | 847 | blen = strlen(bound); |
620 | 847 | part = 0; |
621 | 847 | state = 0; |
622 | 847 | first = 1; |
623 | 847 | parts = sk_BIO_new_null(); |
624 | 847 | *ret = parts; |
625 | 847 | if (*ret == NULL) |
626 | 0 | return 0; |
627 | 17.2M | while ((len = BIO_get_line(bio, linebuf, MAX_SMLEN)) > 0) { |
628 | 17.2M | state = mime_bound_check(linebuf, len, bound, blen); |
629 | 17.2M | if (state == 1) { |
630 | 594k | first = 1; |
631 | 594k | part++; |
632 | 16.6M | } else if (state == 2) { |
633 | 449 | if (!sk_BIO_push(parts, bpart)) { |
634 | 0 | BIO_free(bpart); |
635 | 0 | return 0; |
636 | 0 | } |
637 | 449 | return 1; |
638 | 16.6M | } else if (part != 0) { |
639 | | /* Strip (possibly CR +) LF from linebuf */ |
640 | 16.6M | next_eol = strip_eol(linebuf, &len, flags); |
641 | 16.6M | if (first) { |
642 | 591k | first = 0; |
643 | 591k | if (bpart) |
644 | 591k | if (!sk_BIO_push(parts, bpart)) { |
645 | 0 | BIO_free(bpart); |
646 | 0 | return 0; |
647 | 0 | } |
648 | 591k | bpart = BIO_new(BIO_s_mem()); |
649 | 591k | if (bpart == NULL) |
650 | 0 | return 0; |
651 | 591k | BIO_set_mem_eof_return(bpart, 0); |
652 | 16.0M | } else if (eol) { |
653 | 16.0M | if ( |
654 | 16.0M | #ifndef OPENSSL_NO_CMS |
655 | 16.0M | (flags & CMS_BINARY) == 0 |
656 | | #else |
657 | | 1 |
658 | | #endif |
659 | 16.0M | || (flags & SMIME_CRLFEOL) != 0) |
660 | 16.0M | BIO_write(bpart, "\r\n", 2); |
661 | 0 | else |
662 | 0 | BIO_write(bpart, "\n", 1); |
663 | 16.0M | } |
664 | 16.6M | eol = next_eol; |
665 | 16.6M | if (len > 0) |
666 | 528k | BIO_write(bpart, linebuf, len); |
667 | 16.6M | } |
668 | 17.2M | } |
669 | 398 | BIO_free(bpart); |
670 | 398 | return 0; |
671 | 847 | } |
672 | | |
673 | | /* This is the big one: parse MIME header lines up to message body */ |
674 | | |
675 | | #define MIME_INVALID 0 |
676 | 0 | #define MIME_START 1 |
677 | 0 | #define MIME_TYPE 2 |
678 | 0 | #define MIME_NAME 3 |
679 | 0 | #define MIME_VALUE 4 |
680 | 0 | #define MIME_QUOTE 5 |
681 | 0 | #define MIME_COMMENT 6 |
682 | | |
683 | | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) |
684 | 0 | { |
685 | 0 | char *p, *q, c; |
686 | 0 | char *ntmp; |
687 | 0 | char linebuf[MAX_SMLEN]; |
688 | 0 | MIME_HEADER *mhdr = NULL, *new_hdr = NULL; |
689 | 0 | STACK_OF(MIME_HEADER) *headers; |
690 | 0 | int len, state, save_state = 0; |
691 | |
|
692 | 0 | headers = sk_MIME_HEADER_new(mime_hdr_cmp); |
693 | 0 | if (headers == NULL) |
694 | 0 | return NULL; |
695 | 0 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { |
696 | | /* If whitespace at line start then continuation line */ |
697 | 0 | if (mhdr && ossl_isspace(linebuf[0])) |
698 | 0 | state = MIME_NAME; |
699 | 0 | else |
700 | 0 | state = MIME_START; |
701 | 0 | ntmp = NULL; |
702 | | /* Go through all characters */ |
703 | 0 | for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); |
704 | 0 | p++) { |
705 | | |
706 | | /* |
707 | | * State machine to handle MIME headers if this looks horrible |
708 | | * that's because it *is* |
709 | | */ |
710 | |
|
711 | 0 | switch (state) { |
712 | 0 | case MIME_START: |
713 | 0 | if (c == ':') { |
714 | 0 | state = MIME_TYPE; |
715 | 0 | *p = 0; |
716 | 0 | ntmp = strip_ends(q); |
717 | 0 | q = p + 1; |
718 | 0 | } |
719 | 0 | break; |
720 | | |
721 | 0 | case MIME_TYPE: |
722 | 0 | if (c == ';') { |
723 | 0 | mime_debug("Found End Value\n"); |
724 | 0 | *p = 0; |
725 | 0 | new_hdr = mime_hdr_new(ntmp, strip_ends(q)); |
726 | 0 | if (new_hdr == NULL) |
727 | 0 | goto err; |
728 | 0 | if (!sk_MIME_HEADER_push(headers, new_hdr)) |
729 | 0 | goto err; |
730 | 0 | mhdr = new_hdr; |
731 | 0 | new_hdr = NULL; |
732 | 0 | ntmp = NULL; |
733 | 0 | q = p + 1; |
734 | 0 | state = MIME_NAME; |
735 | 0 | } else if (c == '(') { |
736 | 0 | save_state = state; |
737 | 0 | state = MIME_COMMENT; |
738 | 0 | } |
739 | 0 | break; |
740 | | |
741 | 0 | case MIME_COMMENT: |
742 | 0 | if (c == ')') { |
743 | 0 | state = save_state; |
744 | 0 | } |
745 | 0 | break; |
746 | | |
747 | 0 | case MIME_NAME: |
748 | 0 | if (c == '=') { |
749 | 0 | state = MIME_VALUE; |
750 | 0 | *p = 0; |
751 | 0 | ntmp = strip_ends(q); |
752 | 0 | q = p + 1; |
753 | 0 | } |
754 | 0 | break; |
755 | | |
756 | 0 | case MIME_VALUE: |
757 | 0 | if (c == ';') { |
758 | 0 | state = MIME_NAME; |
759 | 0 | *p = 0; |
760 | 0 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); |
761 | 0 | ntmp = NULL; |
762 | 0 | q = p + 1; |
763 | 0 | } else if (c == '"') { |
764 | 0 | mime_debug("Found Quote\n"); |
765 | 0 | state = MIME_QUOTE; |
766 | 0 | } else if (c == '(') { |
767 | 0 | save_state = state; |
768 | 0 | state = MIME_COMMENT; |
769 | 0 | } |
770 | 0 | break; |
771 | | |
772 | 0 | case MIME_QUOTE: |
773 | 0 | if (c == '"') { |
774 | 0 | mime_debug("Found Match Quote\n"); |
775 | 0 | state = MIME_VALUE; |
776 | 0 | } |
777 | 0 | break; |
778 | 0 | } |
779 | 0 | } |
780 | | |
781 | 0 | if (state == MIME_TYPE) { |
782 | 0 | new_hdr = mime_hdr_new(ntmp, strip_ends(q)); |
783 | 0 | if (new_hdr == NULL) |
784 | 0 | goto err; |
785 | 0 | if (!sk_MIME_HEADER_push(headers, new_hdr)) |
786 | 0 | goto err; |
787 | 0 | mhdr = new_hdr; |
788 | 0 | new_hdr = NULL; |
789 | 0 | } else if (state == MIME_VALUE) { |
790 | 0 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); |
791 | 0 | } |
792 | 0 | if (p == linebuf) |
793 | 0 | break; /* Blank line means end of headers */ |
794 | 0 | } |
795 | | |
796 | 0 | return headers; |
797 | | |
798 | 0 | err: |
799 | 0 | mime_hdr_free(new_hdr); |
800 | 0 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); |
801 | 0 | return NULL; |
802 | 0 | } |
803 | | |
804 | | static char *strip_ends(char *name) |
805 | 16.4M | { |
806 | 16.4M | return strip_end(strip_start(name)); |
807 | 16.4M | } |
808 | | |
809 | | /* Strip a parameter of whitespace from start of param */ |
810 | | static char *strip_start(char *name) |
811 | 16.4M | { |
812 | 16.4M | char *p, c; |
813 | | /* Look for first non whitespace or quote */ |
814 | 23.9M | for (p = name; (c = *p); p++) { |
815 | 7.71M | if (c == '"') { |
816 | | /* Next char is start of string if non null */ |
817 | 16.9k | if (p[1]) |
818 | 16.1k | return p + 1; |
819 | | /* Else null string */ |
820 | 837 | return NULL; |
821 | 16.9k | } |
822 | 7.69M | if (!ossl_isspace(c)) |
823 | 145k | return p; |
824 | 7.69M | } |
825 | 16.2M | return NULL; |
826 | 16.4M | } |
827 | | |
828 | | /* As above but strip from end of string : maybe should handle brackets? */ |
829 | | static char *strip_end(char *name) |
830 | 16.4M | { |
831 | 16.4M | char *p, c; |
832 | 16.4M | if (!name) |
833 | 16.2M | return NULL; |
834 | | /* Look for first non whitespace or quote */ |
835 | 203k | for (p = name + strlen(name) - 1; p >= name; p--) { |
836 | 202k | c = *p; |
837 | 202k | if (c == '"') { |
838 | 6.21k | if (p - 1 == name) |
839 | 670 | return NULL; |
840 | 5.54k | *p = 0; |
841 | 5.54k | return name; |
842 | 6.21k | } |
843 | 196k | if (ossl_isspace(c)) |
844 | 41.4k | *p = 0; |
845 | 155k | else |
846 | 155k | return name; |
847 | 196k | } |
848 | 390 | return NULL; |
849 | 161k | } |
850 | | |
851 | | static MIME_HEADER *mime_hdr_new(const char *name, const char *value) |
852 | 7.40M | { |
853 | 7.40M | MIME_HEADER *mhdr = NULL; |
854 | 7.40M | char *tmpname = NULL, *tmpval = NULL, *p; |
855 | | |
856 | 7.40M | if (name) { |
857 | 14.1k | if ((tmpname = OPENSSL_strdup(name)) == NULL) |
858 | 0 | return NULL; |
859 | 172k | for (p = tmpname; *p; p++) |
860 | 158k | *p = ossl_tolower(*p); |
861 | 14.1k | } |
862 | 7.40M | if (value) { |
863 | 28.0k | if ((tmpval = OPENSSL_strdup(value)) == NULL) |
864 | 0 | goto err; |
865 | 536k | for (p = tmpval; *p; p++) |
866 | 508k | *p = ossl_tolower(*p); |
867 | 28.0k | } |
868 | 7.40M | mhdr = OPENSSL_malloc(sizeof(*mhdr)); |
869 | 7.40M | if (mhdr == NULL) |
870 | 0 | goto err; |
871 | 7.40M | mhdr->name = tmpname; |
872 | 7.40M | mhdr->value = tmpval; |
873 | 7.40M | if ((mhdr->params = sk_MIME_PARAM_new(mime_param_cmp)) == NULL) |
874 | 0 | goto err; |
875 | 7.40M | return mhdr; |
876 | | |
877 | 0 | err: |
878 | 0 | OPENSSL_free(tmpname); |
879 | 0 | OPENSSL_free(tmpval); |
880 | 0 | OPENSSL_free(mhdr); |
881 | 0 | return NULL; |
882 | 7.40M | } |
883 | | |
884 | | static int mime_hdr_addparam(MIME_HEADER *mhdr, const char *name, const char *value) |
885 | 813k | { |
886 | 813k | char *tmpname = NULL, *tmpval = NULL, *p; |
887 | 813k | MIME_PARAM *mparam = NULL; |
888 | | |
889 | 813k | if (name) { |
890 | 65.9k | tmpname = OPENSSL_strdup(name); |
891 | 65.9k | if (!tmpname) |
892 | 0 | goto err; |
893 | 472k | for (p = tmpname; *p; p++) |
894 | 406k | *p = ossl_tolower(*p); |
895 | 65.9k | } |
896 | 813k | if (value) { |
897 | 52.3k | tmpval = OPENSSL_strdup(value); |
898 | 52.3k | if (!tmpval) |
899 | 0 | goto err; |
900 | 52.3k | } |
901 | | /* Parameter values are case sensitive so leave as is */ |
902 | 813k | mparam = OPENSSL_malloc(sizeof(*mparam)); |
903 | 813k | if (mparam == NULL) |
904 | 0 | goto err; |
905 | 813k | mparam->param_name = tmpname; |
906 | 813k | mparam->param_value = tmpval; |
907 | 813k | if (!sk_MIME_PARAM_push(mhdr->params, mparam)) |
908 | 0 | goto err; |
909 | 813k | return 1; |
910 | 0 | err: |
911 | 0 | OPENSSL_free(tmpname); |
912 | 0 | OPENSSL_free(tmpval); |
913 | 0 | OPENSSL_free(mparam); |
914 | 0 | return 0; |
915 | 813k | } |
916 | | |
917 | | static int mime_hdr_cmp(const MIME_HEADER *const *a, |
918 | | const MIME_HEADER *const *b) |
919 | 73.4M | { |
920 | 73.4M | if ((*a)->name == NULL || (*b)->name == NULL) |
921 | 73.4M | return ((*a)->name != NULL) - ((*b)->name != NULL); |
922 | | |
923 | 29.3k | return strcmp((*a)->name, (*b)->name); |
924 | 73.4M | } |
925 | | |
926 | | static int mime_param_cmp(const MIME_PARAM *const *a, |
927 | | const MIME_PARAM *const *b) |
928 | 12.2M | { |
929 | 12.2M | if ((*a)->param_name == NULL || (*b)->param_name == NULL) |
930 | 11.5M | return ((*a)->param_name != NULL) - ((*b)->param_name != NULL); |
931 | 729k | return strcmp((*a)->param_name, (*b)->param_name); |
932 | 12.2M | } |
933 | | |
934 | | /* Find a header with a given name (if possible) */ |
935 | | |
936 | | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, const char *name) |
937 | 10.9k | { |
938 | 10.9k | MIME_HEADER htmp; |
939 | 10.9k | int idx; |
940 | | |
941 | 10.9k | htmp.name = (char *)name; |
942 | 10.9k | htmp.value = NULL; |
943 | 10.9k | htmp.params = NULL; |
944 | | |
945 | 10.9k | idx = sk_MIME_HEADER_find(hdrs, &htmp); |
946 | 10.9k | return sk_MIME_HEADER_value(hdrs, idx); |
947 | 10.9k | } |
948 | | |
949 | | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, const char *name) |
950 | 1.70k | { |
951 | 1.70k | MIME_PARAM param; |
952 | 1.70k | int idx; |
953 | | |
954 | 1.70k | param.param_name = (char *)name; |
955 | 1.70k | param.param_value = NULL; |
956 | 1.70k | idx = sk_MIME_PARAM_find(hdr->params, ¶m); |
957 | 1.70k | return sk_MIME_PARAM_value(hdr->params, idx); |
958 | 1.70k | } |
959 | | |
960 | | static void mime_hdr_free(MIME_HEADER *hdr) |
961 | 7.40M | { |
962 | 7.40M | if (hdr == NULL) |
963 | 0 | return; |
964 | 7.40M | OPENSSL_free(hdr->name); |
965 | 7.40M | OPENSSL_free(hdr->value); |
966 | 7.40M | if (hdr->params) |
967 | 7.40M | sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); |
968 | 7.40M | OPENSSL_free(hdr); |
969 | 7.40M | } |
970 | | |
971 | | static void mime_param_free(MIME_PARAM *param) |
972 | 813k | { |
973 | 813k | OPENSSL_free(param->param_name); |
974 | 813k | OPENSSL_free(param->param_value); |
975 | 813k | OPENSSL_free(param); |
976 | 813k | } |
977 | | |
978 | | /*- |
979 | | * Check for a multipart boundary. Returns: |
980 | | * 0 : no boundary |
981 | | * 1 : part boundary |
982 | | * 2 : final boundary |
983 | | */ |
984 | | static int mime_bound_check(char *line, int linelen, const char *bound, int blen) |
985 | 0 | { |
986 | 0 | if (linelen == -1) |
987 | 0 | linelen = strlen(line); |
988 | 0 | if (blen == -1) |
989 | 0 | blen = strlen(bound); |
990 | | /* Quickly eliminate if line length too short */ |
991 | 0 | if (blen + 2 > linelen) |
992 | 0 | return 0; |
993 | | /* Check for part boundary */ |
994 | 0 | if ((strncmp(line, "--", 2) == 0) |
995 | 0 | && strncmp(line + 2, bound, blen) == 0) { |
996 | 0 | if (strncmp(line + blen + 2, "--", 2) == 0) |
997 | 0 | return 2; |
998 | 0 | else |
999 | 0 | return 1; |
1000 | 0 | } |
1001 | 0 | return 0; |
1002 | 0 | } |
1003 | | |
1004 | | static int strip_eol(char *linebuf, int *plen, int flags) |
1005 | 31.2M | { |
1006 | 31.2M | int len = *plen; |
1007 | 31.2M | char *p, c; |
1008 | 31.2M | int is_eol = 0; |
1009 | | |
1010 | 31.2M | #ifndef OPENSSL_NO_CMS |
1011 | 31.2M | if ((flags & CMS_BINARY) != 0) { |
1012 | 0 | if (len <= 0 || linebuf[len - 1] != '\n') |
1013 | 0 | return 0; |
1014 | 0 | if ((flags & SMIME_CRLFEOL) != 0) { |
1015 | 0 | if (len <= 1 || linebuf[len - 2] != '\r') |
1016 | 0 | return 0; |
1017 | 0 | len--; |
1018 | 0 | } |
1019 | 0 | len--; |
1020 | 0 | *plen = len; |
1021 | 0 | return 1; |
1022 | 0 | } |
1023 | 31.2M | #endif |
1024 | | |
1025 | 62.4M | for (p = linebuf + len - 1; len > 0; len--, p--) { |
1026 | 32.1M | c = *p; |
1027 | 32.1M | if (c == '\n') { |
1028 | 31.1M | is_eol = 1; |
1029 | 31.1M | } else if (is_eol && (flags & SMIME_ASCIICRLF) != 0 && c == 32) { |
1030 | | /* Strip trailing space on a line; 32 == ASCII for ' ' */ |
1031 | 0 | continue; |
1032 | 930k | } else if (c != '\r') { |
1033 | 929k | break; |
1034 | 929k | } |
1035 | 32.1M | } |
1036 | 31.2M | *plen = len; |
1037 | 31.2M | return is_eol; |
1038 | 31.2M | } |