Coverage Report

Created: 2025-08-28 07:07

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