Coverage Report

Created: 2026-06-18 06:34

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