Coverage Report

Created: 2026-09-12 06:55

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