Coverage Report

Created: 2026-07-19 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/pkcs7/pk7_doit.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <openssl/rand.h>
12
#include <openssl/objects.h>
13
#include <openssl/x509.h>
14
#include <openssl/x509v3.h>
15
#include <openssl/err.h>
16
#include "internal/cryptlib.h"
17
#include "internal/sizes.h"
18
#include "crypto/evp.h"
19
#include "pk7_local.h"
20
21
#include <crypto/asn1.h>
22
23
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
24
    void *value);
25
static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid);
26
27
int PKCS7_type_is_other(PKCS7 *p7)
28
0
{
29
0
    int isOther = 1;
30
31
0
    int nid = OBJ_obj2nid(p7->type);
32
33
0
    switch (nid) {
34
0
    case NID_pkcs7_data:
35
0
    case NID_pkcs7_signed:
36
0
    case NID_pkcs7_enveloped:
37
0
    case NID_pkcs7_signedAndEnveloped:
38
0
    case NID_pkcs7_digest:
39
0
    case NID_pkcs7_encrypted:
40
0
        isOther = 0;
41
0
        break;
42
0
    default:
43
0
        isOther = 1;
44
0
    }
45
46
0
    return isOther;
47
0
}
48
49
ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
50
0
{
51
0
    if (PKCS7_type_is_data(p7))
52
0
        return p7->d.data;
53
0
    if (PKCS7_type_is_other(p7) && p7->d.other
54
0
        && (p7->d.other->type == V_ASN1_OCTET_STRING))
55
0
        return p7->d.other->value.octet_string;
56
0
    return NULL;
57
0
}
58
59
static ASN1_OCTET_STRING *pkcs7_get1_data(PKCS7 *p7)
60
0
{
61
0
    ASN1_OCTET_STRING *os = PKCS7_get_octet_string(p7);
62
63
0
    if (os != NULL) {
64
        /* Edge case for MIME content, see RFC 5652 section-5.2.1 */
65
0
        ASN1_OCTET_STRING *osdup = ASN1_OCTET_STRING_dup(os);
66
67
0
        if (osdup != NULL && (os->flags & ASN1_STRING_FLAG_NDEF))
68
            /* ASN1_STRING_FLAG_NDEF flag is currently used by openssl-smime */
69
0
            ASN1_STRING_set0(osdup, NULL, 0);
70
0
        return osdup;
71
0
    }
72
73
    /* General case for PKCS#7 content, see RFC 2315 section-7 */
74
0
    if (PKCS7_type_is_other(p7) && (p7->d.other != NULL)
75
0
        && (p7->d.other->type == V_ASN1_SEQUENCE)
76
0
        && (p7->d.other->value.sequence != NULL)
77
0
        && (ASN1_STRING_length_ex(p7->d.other->value.sequence) > 0)) {
78
0
        const unsigned char *data = ASN1_STRING_get0_data(p7->d.other->value.sequence);
79
0
        long len;
80
0
        int inf, tag, class;
81
0
        size_t tmp;
82
83
0
        tmp = ASN1_STRING_length_ex(p7->d.other->value.sequence);
84
0
        if (tmp > INT_MAX)
85
0
            return NULL;
86
0
        os = ASN1_OCTET_STRING_new();
87
0
        if (os == NULL)
88
0
            return NULL;
89
0
        inf = ASN1_get_object(&data, &len, &tag, &class, (int)tmp);
90
0
        if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE
91
0
            || !ASN1_OCTET_STRING_set(os, data, len)) {
92
0
            ASN1_OCTET_STRING_free(os);
93
0
            os = NULL;
94
0
        }
95
0
    }
96
0
    return os;
97
0
}
98
99
static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
100
    const PKCS7_CTX *ctx)
101
0
{
102
0
    BIO *btmp;
103
0
    char name[OSSL_MAX_NAME_SIZE];
104
0
    EVP_MD *md = NULL;
105
106
0
    if ((btmp = BIO_new(BIO_f_md())) == NULL) {
107
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
108
0
        goto err;
109
0
    }
110
111
0
    OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
112
113
0
    md = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
114
0
        ossl_pkcs7_ctx_get0_propq(ctx));
115
116
0
    if (md == NULL) {
117
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
118
0
        goto err;
119
0
    }
120
121
0
    if (BIO_set_md(btmp, md) <= 0) {
122
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
123
0
        EVP_MD_free(md);
124
0
        goto err;
125
0
    }
126
0
    EVP_MD_free(md);
127
0
    if (*pbio == NULL)
128
0
        *pbio = btmp;
129
0
    else if (!BIO_push(*pbio, btmp)) {
130
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
131
0
        goto err;
132
0
    }
133
0
    btmp = NULL;
134
135
0
    return 1;
136
137
0
err:
138
0
    BIO_free(btmp);
139
0
    return 0;
140
0
}
141
142
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
143
    unsigned char *key, int keylen)
144
0
{
145
0
    EVP_PKEY_CTX *pctx = NULL;
146
0
    EVP_PKEY *pkey = NULL;
147
0
    unsigned char *ek = NULL;
148
0
    int ret = 0;
149
0
    size_t eklen;
150
0
    const PKCS7_CTX *ctx = ri->ctx;
151
152
0
    pkey = X509_get0_pubkey(ri->cert);
153
0
    if (pkey == NULL)
154
0
        return 0;
155
156
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
157
0
        ossl_pkcs7_ctx_get0_propq(ctx));
158
0
    if (pctx == NULL)
159
0
        return 0;
160
161
0
    if (EVP_PKEY_encrypt_init(pctx) <= 0)
162
0
        goto err;
163
164
0
    if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
165
0
        goto err;
166
167
0
    ek = OPENSSL_malloc(eklen);
168
0
    if (ek == NULL)
169
0
        goto err;
170
171
0
    if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
172
0
        goto err;
173
174
0
    ASN1_STRING_set0(ri->enc_key, ek, (int)eklen);
175
0
    ek = NULL;
176
177
0
    ret = 1;
178
179
0
err:
180
0
    EVP_PKEY_CTX_free(pctx);
181
0
    OPENSSL_free(ek);
182
0
    return ret;
183
0
}
184
185
static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
186
    PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
187
    size_t fixlen)
188
0
{
189
0
    EVP_PKEY_CTX *pctx = NULL;
190
0
    unsigned char *ek = NULL;
191
0
    size_t eklen;
192
0
    int ret = -1;
193
0
    const PKCS7_CTX *ctx = ri->ctx;
194
195
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
196
0
        ossl_pkcs7_ctx_get0_propq(ctx));
197
0
    if (pctx == NULL)
198
0
        return -1;
199
200
0
    if (EVP_PKEY_decrypt_init(pctx) <= 0)
201
0
        goto err;
202
203
0
    ret = evp_pkey_decrypt_alloc(pctx, &ek, &eklen, fixlen,
204
0
        ASN1_STRING_get0_data(ri->enc_key), ASN1_STRING_length_ex(ri->enc_key));
205
0
    if (ret <= 0)
206
0
        goto err;
207
208
0
    ret = 1;
209
210
0
    OPENSSL_clear_free(*pek, *peklen);
211
0
    *pek = ek;
212
0
    *peklen = (int)eklen;
213
214
0
err:
215
0
    EVP_PKEY_CTX_free(pctx);
216
0
    if (!ret)
217
0
        OPENSSL_free(ek);
218
219
0
    return ret;
220
0
}
221
222
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
223
0
{
224
0
    int i;
225
0
    BIO *out = NULL, *btmp = NULL;
226
0
    X509_ALGOR *xa = NULL;
227
0
    EVP_CIPHER *fetched_cipher = NULL;
228
0
    const EVP_CIPHER *cipher;
229
0
    const EVP_CIPHER *evp_cipher = NULL;
230
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
231
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
232
0
    X509_ALGOR *xalg = NULL;
233
0
    PKCS7_RECIP_INFO *ri = NULL;
234
0
    ASN1_OCTET_STRING *os = NULL;
235
0
    const PKCS7_CTX *p7_ctx;
236
0
    OSSL_LIB_CTX *libctx;
237
0
    const char *propq;
238
239
0
    if (p7 == NULL) {
240
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
241
0
        return NULL;
242
0
    }
243
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
244
0
    libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
245
0
    propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
246
247
    /*
248
     * The content field in the PKCS7 ContentInfo is optional, but that really
249
     * only applies to inner content (precisely, detached signatures).
250
     *
251
     * When reading content, missing outer content is therefore treated as an
252
     * error.
253
     *
254
     * When creating content, PKCS7_content_new() must be called before
255
     * calling this method, so a NULL p7->d is always an error.
256
     */
257
0
    if (p7->d.ptr == NULL) {
258
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
259
0
        return NULL;
260
0
    }
261
262
0
    i = OBJ_obj2nid(p7->type);
263
0
    p7->state = PKCS7_S_HEADER;
264
265
0
    switch (i) {
266
0
    case NID_pkcs7_signed:
267
0
        md_sk = p7->d.sign->md_algs;
268
0
        os = pkcs7_get1_data(p7->d.sign->contents);
269
0
        break;
270
0
    case NID_pkcs7_signedAndEnveloped:
271
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
272
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
273
0
        xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
274
0
        evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
275
0
        if (evp_cipher == NULL) {
276
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
277
0
            goto err;
278
0
        }
279
0
        break;
280
0
    case NID_pkcs7_enveloped:
281
0
        rsk = p7->d.enveloped->recipientinfo;
282
0
        xalg = p7->d.enveloped->enc_data->algorithm;
283
0
        evp_cipher = p7->d.enveloped->enc_data->cipher;
284
0
        if (evp_cipher == NULL) {
285
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
286
0
            goto err;
287
0
        }
288
0
        break;
289
0
    case NID_pkcs7_digest:
290
0
        xa = p7->d.digest->md;
291
0
        os = pkcs7_get1_data(p7->d.digest->contents);
292
0
        break;
293
0
    case NID_pkcs7_data:
294
0
        break;
295
0
    default:
296
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
297
0
        goto err;
298
0
    }
299
300
0
    for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
301
0
        if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
302
0
            goto err;
303
304
0
    if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
305
0
        goto err;
306
307
0
    if (evp_cipher != NULL) {
308
0
        unsigned char key[EVP_MAX_KEY_LENGTH];
309
0
        unsigned char iv[EVP_MAX_IV_LENGTH];
310
0
        int keylen, ivlen;
311
0
        EVP_CIPHER_CTX *ctx;
312
313
0
        if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
314
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
315
0
            goto err;
316
0
        }
317
0
        BIO_get_cipher_ctx(btmp, &ctx);
318
0
        keylen = EVP_CIPHER_get_key_length(evp_cipher);
319
0
        ivlen = EVP_CIPHER_get_iv_length(evp_cipher);
320
0
        xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher));
321
0
        if (ivlen > 0)
322
0
            if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
323
0
                goto err;
324
325
0
        (void)ERR_set_mark();
326
0
        fetched_cipher = EVP_CIPHER_fetch(libctx,
327
0
            EVP_CIPHER_get0_name(evp_cipher),
328
0
            propq);
329
0
        (void)ERR_pop_to_mark();
330
0
        if (fetched_cipher != NULL)
331
0
            cipher = fetched_cipher;
332
0
        else
333
0
            cipher = evp_cipher;
334
335
0
        if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
336
0
            goto err;
337
338
0
        EVP_CIPHER_free(fetched_cipher);
339
0
        fetched_cipher = NULL;
340
341
0
        if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
342
0
            goto err;
343
0
        if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
344
0
            goto err;
345
346
0
        if (ivlen > 0) {
347
0
            if (xalg->parameter == NULL) {
348
0
                xalg->parameter = ASN1_TYPE_new();
349
0
                if (xalg->parameter == NULL)
350
0
                    goto err;
351
0
            }
352
0
            if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0) {
353
0
                ASN1_TYPE_free(xalg->parameter);
354
0
                xalg->parameter = NULL;
355
0
                goto err;
356
0
            }
357
0
        }
358
359
        /* Lets do the pub key stuff :-) */
360
0
        for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
361
0
            ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
362
0
            if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
363
0
                goto err;
364
0
        }
365
0
        OPENSSL_cleanse(key, keylen);
366
367
0
        if (out == NULL)
368
0
            out = btmp;
369
0
        else
370
0
            BIO_push(out, btmp);
371
0
        btmp = NULL;
372
0
    }
373
374
0
    if (bio == NULL) {
375
0
        if (PKCS7_is_detached(p7)) {
376
0
            bio = BIO_new(BIO_s_null());
377
0
        } else if (os != NULL && ASN1_STRING_length_ex(os) > 0) {
378
            /*
379
             * bio needs a copy of os->data instead of a pointer because
380
             * the data will be used after os has been freed
381
             */
382
0
            bio = BIO_new(BIO_s_mem());
383
0
            if (bio != NULL) {
384
0
                BIO_set_mem_eof_return(bio, 0);
385
0
                const unsigned char *os_data = ASN1_STRING_get0_data(os);
386
0
                size_t os_len = ASN1_STRING_length_ex(os);
387
0
                if (os_len > INT_MAX || BIO_write(bio, os_data, (int)os_len) != (int)os_len) {
388
0
                    BIO_free_all(bio);
389
0
                    bio = NULL;
390
0
                }
391
0
            }
392
0
        } else {
393
0
            bio = BIO_new(BIO_s_mem());
394
0
            if (bio == NULL)
395
0
                goto err;
396
0
            BIO_set_mem_eof_return(bio, 0);
397
0
        }
398
0
        if (bio == NULL)
399
0
            goto err;
400
0
    }
401
0
    if (out)
402
0
        BIO_push(out, bio);
403
0
    else
404
0
        out = bio;
405
406
0
    ASN1_OCTET_STRING_free(os);
407
0
    return out;
408
409
0
err:
410
0
    ASN1_OCTET_STRING_free(os);
411
0
    EVP_CIPHER_free(fetched_cipher);
412
0
    BIO_free_all(out);
413
0
    BIO_free_all(btmp);
414
0
    return NULL;
415
0
}
416
417
static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
418
0
{
419
0
    int ret;
420
0
    ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
421
0
        X509_get_issuer_name(pcert));
422
0
    if (ret)
423
0
        return ret;
424
0
    return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
425
0
        ri->issuer_and_serial->serial);
426
0
}
427
428
/* int */
429
BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
430
0
{
431
0
    int i, len;
432
0
    BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
433
0
    X509_ALGOR *xa;
434
0
    ASN1_OCTET_STRING *data_body = NULL;
435
0
    EVP_MD *md = NULL;
436
0
    EVP_CIPHER *cipher = NULL;
437
0
    EVP_CIPHER_CTX *evp_ctx = NULL;
438
0
    X509_ALGOR *enc_alg = NULL;
439
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
440
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
441
0
    PKCS7_RECIP_INFO *ri = NULL;
442
0
    unsigned char *ek = NULL, *tkey = NULL;
443
0
    int eklen = 0, tkeylen = 0;
444
0
    char name[OSSL_MAX_NAME_SIZE];
445
0
    const PKCS7_CTX *p7_ctx;
446
0
    OSSL_LIB_CTX *libctx;
447
0
    const char *propq;
448
449
0
    if (p7 == NULL) {
450
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
451
0
        return NULL;
452
0
    }
453
454
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
455
0
    libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
456
0
    propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
457
458
0
    if (p7->d.ptr == NULL) {
459
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
460
0
        return NULL;
461
0
    }
462
463
0
    i = OBJ_obj2nid(p7->type);
464
0
    p7->state = PKCS7_S_HEADER;
465
466
0
    switch (i) {
467
0
    case NID_pkcs7_signed:
468
        /*
469
         * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
470
         * field and optional content.
471
         * data_body is NULL if that structure has no (=detached) content
472
         * or if the contentType is wrong (i.e., not "data").
473
         */
474
0
        data_body = PKCS7_get_octet_string(p7->d.sign->contents);
475
0
        if (!PKCS7_is_detached(p7) && data_body == NULL) {
476
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE);
477
0
            goto err;
478
0
        }
479
0
        md_sk = p7->d.sign->md_algs;
480
0
        break;
481
0
    case NID_pkcs7_signedAndEnveloped:
482
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
483
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
484
        /* data_body is NULL if the optional EncryptedContent is missing. */
485
0
        data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
486
0
        enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
487
488
0
        OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
489
490
0
        cipher = EVP_CIPHER_fetch(libctx, name, propq);
491
492
0
        if (cipher == NULL) {
493
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
494
0
            goto err;
495
0
        }
496
0
        break;
497
0
    case NID_pkcs7_enveloped:
498
0
        rsk = p7->d.enveloped->recipientinfo;
499
0
        enc_alg = p7->d.enveloped->enc_data->algorithm;
500
        /* data_body is NULL if the optional EncryptedContent is missing. */
501
0
        data_body = p7->d.enveloped->enc_data->enc_data;
502
0
        OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
503
504
0
        cipher = EVP_CIPHER_fetch(libctx, name, propq);
505
506
0
        if (cipher == NULL) {
507
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
508
0
            goto err;
509
0
        }
510
0
        break;
511
0
    default:
512
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
513
0
        goto err;
514
0
    }
515
516
    /* Detached content must be supplied via in_bio instead. */
517
0
    if (data_body == NULL && in_bio == NULL) {
518
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
519
0
        goto err;
520
0
    }
521
522
    /* We will be checking the signature */
523
0
    if (md_sk != NULL) {
524
0
        for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
525
0
            xa = sk_X509_ALGOR_value(md_sk, i);
526
0
            if ((btmp = BIO_new(BIO_f_md())) == NULL) {
527
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
528
0
                goto err;
529
0
            }
530
531
0
            OBJ_obj2txt(name, sizeof(name), xa->algorithm, 0);
532
533
0
            md = EVP_MD_fetch(libctx, name, propq);
534
535
0
            if (md == NULL) {
536
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
537
0
                goto err;
538
0
            }
539
540
0
            if (BIO_set_md(btmp, md) <= 0) {
541
0
                EVP_MD_free(md);
542
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
543
0
                goto err;
544
0
            }
545
0
            EVP_MD_free(md);
546
0
            if (out == NULL)
547
0
                out = btmp;
548
0
            else
549
0
                BIO_push(out, btmp);
550
0
            btmp = NULL;
551
0
        }
552
0
    }
553
554
0
    if (cipher != NULL) {
555
0
        if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
556
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
557
0
            goto err;
558
0
        }
559
560
        /*
561
         * It was encrypted, we need to decrypt the secret key with the
562
         * private key
563
         */
564
565
        /*
566
         * Find the recipientInfo which matches the passed certificate (if
567
         * any)
568
         */
569
570
0
        if (pcert) {
571
0
            for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
572
0
                ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
573
0
                if (!pkcs7_cmp_ri(ri, pcert))
574
0
                    break;
575
0
                ri = NULL;
576
0
            }
577
0
            if (ri == NULL) {
578
0
                ERR_raise(ERR_LIB_PKCS7,
579
0
                    PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
580
0
                goto err;
581
0
            }
582
0
        }
583
584
        /* If we haven't got a certificate try each ri in turn */
585
0
        if (pcert == NULL) {
586
            /*
587
             * Always attempt to decrypt all rinfo even after success as a
588
             * defence against MMA timing attacks.
589
             */
590
0
            for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
591
0
                ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
592
0
                ri->ctx = p7_ctx;
593
0
                if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
594
0
                        EVP_CIPHER_get_key_length(cipher))
595
0
                    < 0)
596
0
                    goto err;
597
0
                ERR_clear_error();
598
0
            }
599
0
        } else {
600
0
            ri->ctx = p7_ctx;
601
            /* Only exit on fatal errors, not decrypt failure */
602
0
            if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
603
0
                goto err;
604
0
            ERR_clear_error();
605
0
        }
606
607
0
        evp_ctx = NULL;
608
0
        BIO_get_cipher_ctx(etmp, &evp_ctx);
609
0
        if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
610
0
            goto err;
611
0
        if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0)
612
0
            goto err;
613
        /* Generate random key as MMA defence */
614
0
        len = EVP_CIPHER_CTX_get_key_length(evp_ctx);
615
0
        if (len <= 0)
616
0
            goto err;
617
0
        tkeylen = (size_t)len;
618
0
        tkey = OPENSSL_malloc(tkeylen);
619
0
        if (tkey == NULL)
620
0
            goto err;
621
0
        if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
622
0
            goto err;
623
0
        if (ek == NULL) {
624
0
            ek = tkey;
625
0
            eklen = tkeylen;
626
0
            tkey = NULL;
627
0
        }
628
629
0
        if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) {
630
            /*
631
             * Some S/MIME clients don't use the same key and effective key
632
             * length. The key length is determined by the size of the
633
             * decrypted RSA key.
634
             */
635
0
            if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) {
636
                /* Use random key as MMA defence */
637
0
                OPENSSL_clear_free(ek, eklen);
638
0
                ek = tkey;
639
0
                eklen = tkeylen;
640
0
                tkey = NULL;
641
0
            }
642
0
        }
643
        /* Clear errors so we don't leak information useful in MMA */
644
0
        ERR_clear_error();
645
0
        if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
646
0
            goto err;
647
648
0
        OPENSSL_clear_free(ek, eklen);
649
0
        ek = NULL;
650
0
        OPENSSL_clear_free(tkey, tkeylen);
651
0
        tkey = NULL;
652
653
0
        if (out == NULL)
654
0
            out = etmp;
655
0
        else
656
0
            BIO_push(out, etmp);
657
0
        etmp = NULL;
658
0
    }
659
0
    if (in_bio != NULL) {
660
0
        bio = in_bio;
661
0
    } else {
662
0
        size_t data_body_len = ASN1_STRING_length_ex(data_body);
663
0
        if (data_body_len > INT_MAX)
664
0
            goto err;
665
0
        if (data_body_len > 0)
666
0
            bio = BIO_new_mem_buf(ASN1_STRING_get0_data(data_body),
667
0
                (int)data_body_len);
668
0
        else {
669
0
            bio = BIO_new(BIO_s_mem());
670
0
            if (bio == NULL)
671
0
                goto err;
672
0
            BIO_set_mem_eof_return(bio, 0);
673
0
        }
674
0
        if (bio == NULL)
675
0
            goto err;
676
0
    }
677
0
    BIO_push(out, bio);
678
0
    bio = NULL;
679
0
    EVP_CIPHER_free(cipher);
680
0
    return out;
681
682
0
err:
683
0
    EVP_CIPHER_free(cipher);
684
0
    OPENSSL_clear_free(ek, eklen);
685
0
    OPENSSL_clear_free(tkey, tkeylen);
686
0
    BIO_free_all(out);
687
0
    BIO_free_all(btmp);
688
0
    BIO_free_all(etmp);
689
0
    BIO_free_all(bio);
690
0
    return NULL;
691
0
}
692
693
static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
694
0
{
695
0
    for (;;) {
696
0
        bio = BIO_find_type(bio, BIO_TYPE_MD);
697
0
        if (bio == NULL) {
698
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
699
0
            return NULL;
700
0
        }
701
0
        BIO_get_md_ctx(bio, pmd);
702
0
        if (*pmd == NULL) {
703
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
704
0
            return NULL;
705
0
        }
706
0
        if (EVP_MD_CTX_get_type(*pmd) == nid)
707
0
            return bio;
708
0
        bio = BIO_next(bio);
709
0
    }
710
0
    return NULL;
711
0
}
712
713
static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
714
0
{
715
0
    unsigned char md_data[EVP_MAX_MD_SIZE];
716
0
    unsigned int md_len;
717
718
    /* Add signing time if not already present */
719
0
    if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
720
0
        if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
721
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
722
0
            return 0;
723
0
        }
724
0
    }
725
726
    /* Add digest */
727
0
    if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
728
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
729
0
        return 0;
730
0
    }
731
0
    if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
732
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
733
0
        return 0;
734
0
    }
735
736
    /* Now sign the attributes */
737
0
    if (!PKCS7_SIGNER_INFO_sign(si))
738
0
        return 0;
739
740
0
    return 1;
741
0
}
742
743
int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
744
0
{
745
0
    int ret = 0;
746
0
    int i, j;
747
0
    BIO *btmp;
748
0
    PKCS7_SIGNER_INFO *si;
749
0
    EVP_MD_CTX *mdc, *ctx_tmp;
750
0
    STACK_OF(X509_ATTRIBUTE) *sk;
751
0
    STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
752
0
    ASN1_OCTET_STRING *os = NULL;
753
0
    const PKCS7_CTX *p7_ctx;
754
755
0
    if (p7 == NULL) {
756
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
757
0
        return 0;
758
0
    }
759
760
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
761
762
0
    if (p7->d.ptr == NULL) {
763
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
764
0
        return 0;
765
0
    }
766
767
0
    ctx_tmp = EVP_MD_CTX_new();
768
0
    if (ctx_tmp == NULL) {
769
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
770
0
        return 0;
771
0
    }
772
773
0
    i = OBJ_obj2nid(p7->type);
774
0
    p7->state = PKCS7_S_HEADER;
775
776
0
    switch (i) {
777
0
    case NID_pkcs7_data:
778
0
        os = p7->d.data;
779
0
        break;
780
0
    case NID_pkcs7_signedAndEnveloped:
781
        /* XXXXXXXXXXXXXXXX */
782
0
        si_sk = p7->d.signed_and_enveloped->signer_info;
783
0
        os = p7->d.signed_and_enveloped->enc_data->enc_data;
784
0
        if (os == NULL) {
785
0
            os = ASN1_OCTET_STRING_new();
786
0
            if (os == NULL) {
787
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
788
0
                goto err;
789
0
            }
790
0
            p7->d.signed_and_enveloped->enc_data->enc_data = os;
791
0
        }
792
0
        break;
793
0
    case NID_pkcs7_enveloped:
794
        /* XXXXXXXXXXXXXXXX */
795
0
        os = p7->d.enveloped->enc_data->enc_data;
796
0
        if (os == NULL) {
797
0
            os = ASN1_OCTET_STRING_new();
798
0
            if (os == NULL) {
799
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
800
0
                goto err;
801
0
            }
802
0
            p7->d.enveloped->enc_data->enc_data = os;
803
0
        }
804
0
        break;
805
0
    case NID_pkcs7_signed:
806
0
        si_sk = p7->d.sign->signer_info;
807
0
        if (p7->d.sign->contents == NULL) {
808
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
809
0
            goto err;
810
0
        }
811
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
812
        /* If detached data then the content is excluded */
813
0
        if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
814
0
            ASN1_OCTET_STRING_free(os);
815
0
            os = NULL;
816
0
            p7->d.sign->contents->d.data = NULL;
817
0
        }
818
0
        break;
819
820
0
    case NID_pkcs7_digest:
821
0
        if (p7->d.digest->contents == NULL) {
822
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
823
0
            goto err;
824
0
        }
825
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
826
        /* If detached data then the content is excluded */
827
0
        if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
828
0
            ASN1_OCTET_STRING_free(os);
829
0
            os = NULL;
830
0
            p7->d.digest->contents->d.data = NULL;
831
0
        }
832
0
        break;
833
834
0
    default:
835
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
836
0
        goto err;
837
0
    }
838
839
0
    if (si_sk != NULL) {
840
0
        for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
841
0
            si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
842
0
            if (si->pkey == NULL)
843
0
                continue;
844
845
0
            j = OBJ_obj2nid(si->digest_alg->algorithm);
846
847
0
            btmp = bio;
848
849
0
            btmp = PKCS7_find_digest(&mdc, btmp, j);
850
851
0
            if (btmp == NULL)
852
0
                goto err;
853
854
            /*
855
             * We now have the EVP_MD_CTX, lets do the signing.
856
             */
857
0
            if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
858
0
                goto err;
859
860
0
            sk = si->auth_attr;
861
862
            /*
863
             * If there are attributes, we add the digest attribute and only
864
             * sign the attributes
865
             */
866
0
            if (sk_X509_ATTRIBUTE_num(sk) > 0) {
867
0
                if (!do_pkcs7_signed_attrib(si, ctx_tmp))
868
0
                    goto err;
869
0
            } else {
870
0
                unsigned char *abuf = NULL;
871
0
                unsigned int abuflen = EVP_PKEY_get_size(si->pkey);
872
873
0
                if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL)
874
0
                    goto err;
875
876
0
                if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
877
0
                        ossl_pkcs7_ctx_get0_libctx(p7_ctx),
878
0
                        ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
879
0
                    OPENSSL_free(abuf);
880
0
                    ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
881
0
                    goto err;
882
0
                }
883
0
                ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
884
0
            }
885
0
        }
886
0
    } else if (i == NID_pkcs7_digest) {
887
0
        unsigned char md_data[EVP_MAX_MD_SIZE];
888
0
        unsigned int md_len;
889
0
        if (!PKCS7_find_digest(&mdc, bio,
890
0
                OBJ_obj2nid(p7->d.digest->md->algorithm)))
891
0
            goto err;
892
0
        if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
893
0
            goto err;
894
0
        if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
895
0
            goto err;
896
0
    }
897
898
0
    if (!PKCS7_is_detached(p7)) {
899
        /*
900
         * NOTE(emilia): I think we only reach os == NULL here because detached
901
         * digested data support is broken.
902
         */
903
0
        if (os == NULL)
904
0
            goto err;
905
0
        if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
906
0
            char *cont;
907
0
            long contlen;
908
0
            btmp = BIO_find_type(bio, BIO_TYPE_MEM);
909
0
            if (btmp == NULL) {
910
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
911
0
                goto err;
912
0
            }
913
0
            contlen = BIO_get_mem_data(btmp, &cont);
914
            /*
915
             * Mark the BIO read only then we can use its copy of the data
916
             * instead of making an extra copy.
917
             */
918
0
            BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
919
0
            BIO_set_mem_eof_return(btmp, 0);
920
0
            ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
921
0
        }
922
0
    }
923
0
    ret = 1;
924
0
err:
925
0
    EVP_MD_CTX_free(ctx_tmp);
926
0
    return ret;
927
0
}
928
929
int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
930
0
{
931
0
    EVP_MD_CTX *mctx;
932
0
    EVP_PKEY_CTX *pctx = NULL;
933
0
    unsigned char *abuf = NULL;
934
0
    int alen;
935
0
    size_t siglen;
936
0
    const EVP_MD *md = NULL;
937
0
    const PKCS7_CTX *ctx = si->ctx;
938
939
0
    md = EVP_get_digestbyobj(si->digest_alg->algorithm);
940
0
    if (md == NULL)
941
0
        return 0;
942
943
0
    mctx = EVP_MD_CTX_new();
944
0
    if (mctx == NULL) {
945
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
946
0
        goto err;
947
0
    }
948
949
0
    if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
950
0
            ossl_pkcs7_ctx_get0_libctx(ctx),
951
0
            ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
952
0
            NULL)
953
0
        <= 0)
954
0
        goto err;
955
956
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
957
0
        ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
958
0
    if (alen < 0 || abuf == NULL)
959
0
        goto err;
960
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
961
0
        goto err;
962
0
    OPENSSL_free(abuf);
963
0
    abuf = NULL;
964
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
965
0
        goto err;
966
0
    abuf = OPENSSL_malloc(siglen);
967
0
    if (abuf == NULL)
968
0
        goto err;
969
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
970
0
        goto err;
971
972
0
    EVP_MD_CTX_free(mctx);
973
974
0
    ASN1_STRING_set0(si->enc_digest, abuf, (int)siglen);
975
976
0
    return 1;
977
978
0
err:
979
0
    OPENSSL_free(abuf);
980
0
    EVP_MD_CTX_free(mctx);
981
0
    return 0;
982
0
}
983
984
/* This partly overlaps with PKCS7_verify(). It does not support flags. */
985
int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
986
    PKCS7 *p7, PKCS7_SIGNER_INFO *si)
987
0
{
988
0
    PKCS7_ISSUER_AND_SERIAL *ias;
989
0
    int ret = 0, i;
990
0
    STACK_OF(X509) *untrusted;
991
0
    STACK_OF(X509_CRL) *crls;
992
0
    X509 *signer;
993
994
0
    if (p7 == NULL) {
995
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
996
0
        return 0;
997
0
    }
998
999
0
    if (p7->d.ptr == NULL) {
1000
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
1001
0
        return 0;
1002
0
    }
1003
1004
0
    if (PKCS7_type_is_signed(p7)) {
1005
0
        untrusted = p7->d.sign->cert;
1006
0
        crls = p7->d.sign->crl;
1007
0
    } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
1008
0
        untrusted = p7->d.signed_and_enveloped->cert;
1009
0
        crls = p7->d.signed_and_enveloped->crl;
1010
0
    } else {
1011
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1012
0
        goto err;
1013
0
    }
1014
0
    X509_STORE_CTX_set0_crls(ctx, crls);
1015
1016
    /* XXXXXXXXXXXXXXXXXXXXXXX */
1017
0
    ias = si->issuer_and_serial;
1018
1019
0
    signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
1020
1021
    /* Were we able to find the signer certificate in passed to us? */
1022
0
    if (signer == NULL) {
1023
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
1024
0
        goto err;
1025
0
    }
1026
1027
    /* Lets verify */
1028
0
    if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
1029
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1030
0
        goto err;
1031
0
    }
1032
0
    X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1033
0
    i = X509_verify_cert(ctx);
1034
0
    if (i <= 0) {
1035
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1036
0
        goto err;
1037
0
    }
1038
1039
0
    if (PKCS7_signatureVerify(bio, p7, si, signer) <= 0)
1040
0
        goto err;
1041
0
    ret = 1;
1042
0
err:
1043
0
    return ret;
1044
0
}
1045
1046
int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1047
    const X509 *signer)
1048
0
{
1049
0
    ASN1_OCTET_STRING *os;
1050
0
    EVP_MD_CTX *mdc_tmp, *mdc;
1051
0
    EVP_MD *md = NULL;
1052
0
    int ret = 0, i;
1053
0
    int md_type;
1054
0
    STACK_OF(X509_ATTRIBUTE) *sk;
1055
0
    BIO *btmp;
1056
0
    EVP_PKEY *pkey;
1057
0
    unsigned char *abuf = NULL;
1058
0
    const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1059
0
    OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1060
0
    const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1061
1062
0
    mdc_tmp = EVP_MD_CTX_new();
1063
0
    if (mdc_tmp == NULL) {
1064
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
1065
0
        goto err;
1066
0
    }
1067
1068
0
    if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1069
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1070
0
        goto err;
1071
0
    }
1072
1073
0
    md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1074
1075
0
    btmp = bio;
1076
0
    for (;;) {
1077
0
        if ((btmp == NULL) || ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1078
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1079
0
            goto err;
1080
0
        }
1081
0
        BIO_get_md_ctx(btmp, &mdc);
1082
0
        if (mdc == NULL) {
1083
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1084
0
            goto err;
1085
0
        }
1086
0
        if (EVP_MD_CTX_get_type(mdc) == md_type)
1087
0
            break;
1088
        /*
1089
         * Workaround for some broken clients that put the signature OID
1090
         * instead of the digest OID in digest_alg->algorithm
1091
         */
1092
0
        if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1093
0
            break;
1094
0
        btmp = BIO_next(btmp);
1095
0
    }
1096
1097
    /*
1098
     * mdc is the digest ctx that we want, unless there are attributes, in
1099
     * which case the digest is the signed attributes
1100
     */
1101
0
    if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1102
0
        goto err;
1103
1104
0
    sk = si->auth_attr;
1105
0
    if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1106
0
        unsigned char md_dat[EVP_MAX_MD_SIZE];
1107
0
        unsigned int md_len;
1108
0
        int alen;
1109
0
        const ASN1_OCTET_STRING *message_digest;
1110
1111
0
        if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1112
0
            goto err;
1113
0
        message_digest = PKCS7_digest_from_attributes(sk);
1114
0
        if (!message_digest) {
1115
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1116
0
            goto err;
1117
0
        }
1118
0
        if ((ASN1_STRING_length_ex(message_digest) != md_len)
1119
0
            || (memcmp(ASN1_STRING_get0_data(message_digest), md_dat, md_len))) {
1120
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1121
0
            ret = -1;
1122
0
            goto err;
1123
0
        }
1124
1125
0
        md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1126
1127
0
        if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1128
0
            goto err;
1129
0
        }
1130
1131
0
        alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1132
0
            ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1133
0
        if (alen <= 0 || abuf == NULL) {
1134
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1135
0
            ret = -1;
1136
0
            goto err;
1137
0
        }
1138
0
        if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1139
0
            goto err;
1140
0
    }
1141
1142
0
    os = si->enc_digest;
1143
0
    pkey = X509_get0_pubkey(signer);
1144
0
    if (pkey == NULL) {
1145
0
        ret = -1;
1146
0
        goto err;
1147
0
    }
1148
1149
0
    const unsigned char *sig_data = ASN1_STRING_get0_data(os);
1150
0
    size_t sig_len = ASN1_STRING_length_ex(os);
1151
0
    if (sig_len > INT_MAX) {
1152
0
        ret = -1;
1153
0
        goto err;
1154
0
    }
1155
0
    i = EVP_VerifyFinal_ex(mdc_tmp, sig_data, (int)sig_len, pkey, libctx, propq);
1156
0
    if (i <= 0) {
1157
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1158
0
        ret = -1;
1159
0
        goto err;
1160
0
    }
1161
0
    ret = 1;
1162
0
err:
1163
0
    OPENSSL_free(abuf);
1164
0
    EVP_MD_CTX_free(mdc_tmp);
1165
0
    EVP_MD_free(md);
1166
0
    return ret;
1167
0
}
1168
1169
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1170
0
{
1171
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk;
1172
0
    PKCS7_RECIP_INFO *ri;
1173
0
    int i;
1174
1175
0
    i = OBJ_obj2nid(p7->type);
1176
0
    if (i != NID_pkcs7_signedAndEnveloped)
1177
0
        return NULL;
1178
0
    if (p7->d.signed_and_enveloped == NULL)
1179
0
        return NULL;
1180
0
    rsk = p7->d.signed_and_enveloped->recipientinfo;
1181
0
    if (rsk == NULL)
1182
0
        return NULL;
1183
0
    if (idx < 0 || sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1184
0
        return NULL;
1185
0
    ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1186
0
    return ri->issuer_and_serial;
1187
0
}
1188
1189
const ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1190
0
{
1191
0
    return get_attribute(si->auth_attr, nid);
1192
0
}
1193
1194
const ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1195
0
{
1196
0
    return get_attribute(si->unauth_attr, nid);
1197
0
}
1198
1199
static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1200
0
{
1201
0
    int idx = X509at_get_attr_by_NID(sk, nid, -1);
1202
1203
0
    if (idx < 0)
1204
0
        return NULL;
1205
0
    return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1206
0
}
1207
1208
const ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1209
0
{
1210
0
    const ASN1_TYPE *astype;
1211
0
    if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1212
0
        return NULL;
1213
0
    if (astype->type != V_ASN1_OCTET_STRING)
1214
0
        return NULL;
1215
0
    return astype->value.octet_string;
1216
0
}
1217
1218
int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1219
    STACK_OF(X509_ATTRIBUTE) *sk)
1220
0
{
1221
0
    sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1222
0
    p7si->auth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free);
1223
0
    if (p7si->auth_attr == NULL)
1224
0
        return 0;
1225
0
    return 1;
1226
0
}
1227
1228
int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1229
    STACK_OF(X509_ATTRIBUTE) *sk)
1230
0
{
1231
0
    sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1232
0
    p7si->unauth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free);
1233
0
    if (p7si->unauth_attr == NULL)
1234
0
        return 0;
1235
0
    return 1;
1236
0
}
1237
1238
int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1239
    void *value)
1240
0
{
1241
0
    return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1242
0
}
1243
1244
int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1245
    void *value)
1246
0
{
1247
0
    return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1248
0
}
1249
1250
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1251
    void *value)
1252
0
{
1253
0
    X509_ATTRIBUTE *attr = NULL;
1254
0
    int i, n;
1255
1256
0
    if (*sk == NULL) {
1257
0
        if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1258
0
            return 0;
1259
0
    }
1260
0
    n = sk_X509_ATTRIBUTE_num(*sk);
1261
0
    for (i = 0; i < n; i++) {
1262
0
        attr = sk_X509_ATTRIBUTE_value(*sk, i);
1263
0
        if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid)
1264
0
            goto end;
1265
0
    }
1266
0
    if (!sk_X509_ATTRIBUTE_push(*sk, NULL))
1267
0
        return 0;
1268
1269
0
end:
1270
0
    attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1271
0
    if (attr == NULL) {
1272
0
        if (i == n)
1273
0
            sk_X509_ATTRIBUTE_pop(*sk);
1274
0
        return 0;
1275
0
    }
1276
0
    X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i));
1277
0
    (void)sk_X509_ATTRIBUTE_set(*sk, i, attr);
1278
0
    return 1;
1279
0
}