Coverage Report

Created: 2026-02-22 06:11

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-2025 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
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
22
    void *value);
23
static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid);
24
25
int PKCS7_type_is_other(PKCS7 *p7)
26
0
{
27
0
    int isOther = 1;
28
29
0
    int nid = OBJ_obj2nid(p7->type);
30
31
0
    switch (nid) {
32
0
    case NID_pkcs7_data:
33
0
    case NID_pkcs7_signed:
34
0
    case NID_pkcs7_enveloped:
35
0
    case NID_pkcs7_signedAndEnveloped:
36
0
    case NID_pkcs7_digest:
37
0
    case NID_pkcs7_encrypted:
38
0
        isOther = 0;
39
0
        break;
40
0
    default:
41
0
        isOther = 1;
42
0
    }
43
44
0
    return isOther;
45
0
}
46
47
ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
48
0
{
49
0
    if (PKCS7_type_is_data(p7))
50
0
        return p7->d.data;
51
0
    if (PKCS7_type_is_other(p7) && p7->d.other
52
0
        && (p7->d.other->type == V_ASN1_OCTET_STRING))
53
0
        return p7->d.other->value.octet_string;
54
0
    return NULL;
55
0
}
56
57
static ASN1_OCTET_STRING *pkcs7_get1_data(PKCS7 *p7)
58
0
{
59
0
    ASN1_OCTET_STRING *os = PKCS7_get_octet_string(p7);
60
61
0
    if (os != NULL) {
62
        /* Edge case for MIME content, see RFC 5652 section-5.2.1 */
63
0
        ASN1_OCTET_STRING *osdup = ASN1_OCTET_STRING_dup(os);
64
65
0
        if (osdup != NULL && (os->flags & ASN1_STRING_FLAG_NDEF))
66
            /* ASN1_STRING_FLAG_NDEF flag is currently used by openssl-smime */
67
0
            ASN1_STRING_set0(osdup, NULL, 0);
68
0
        return osdup;
69
0
    }
70
71
    /* General case for PKCS#7 content, see RFC 2315 section-7 */
72
0
    if (PKCS7_type_is_other(p7) && (p7->d.other != NULL)
73
0
        && (p7->d.other->type == V_ASN1_SEQUENCE)
74
0
        && (p7->d.other->value.sequence != NULL)
75
0
        && (p7->d.other->value.sequence->length > 0)) {
76
0
        const unsigned char *data = p7->d.other->value.sequence->data;
77
0
        long len;
78
0
        int inf, tag, class;
79
80
0
        os = ASN1_OCTET_STRING_new();
81
0
        if (os == NULL)
82
0
            return NULL;
83
0
        inf = ASN1_get_object(&data, &len, &tag, &class,
84
0
            p7->d.other->value.sequence->length);
85
0
        if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE
86
0
            || !ASN1_OCTET_STRING_set(os, data, len)) {
87
0
            ASN1_OCTET_STRING_free(os);
88
0
            os = NULL;
89
0
        }
90
0
    }
91
0
    return os;
92
0
}
93
94
static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
95
    const PKCS7_CTX *ctx)
96
0
{
97
0
    BIO *btmp;
98
0
    char name[OSSL_MAX_NAME_SIZE];
99
0
    EVP_MD *md = NULL;
100
101
0
    if ((btmp = BIO_new(BIO_f_md())) == NULL) {
102
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
103
0
        goto err;
104
0
    }
105
106
0
    OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
107
108
0
    md = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
109
0
        ossl_pkcs7_ctx_get0_propq(ctx));
110
111
0
    if (md == NULL) {
112
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
113
0
        goto err;
114
0
    }
115
116
0
    if (BIO_set_md(btmp, md) <= 0) {
117
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
118
0
        EVP_MD_free(md);
119
0
        goto err;
120
0
    }
121
0
    EVP_MD_free(md);
122
0
    if (*pbio == NULL)
123
0
        *pbio = btmp;
124
0
    else if (!BIO_push(*pbio, btmp)) {
125
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
126
0
        goto err;
127
0
    }
128
0
    btmp = NULL;
129
130
0
    return 1;
131
132
0
err:
133
0
    BIO_free(btmp);
134
0
    return 0;
135
0
}
136
137
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
138
    unsigned char *key, int keylen)
139
0
{
140
0
    EVP_PKEY_CTX *pctx = NULL;
141
0
    EVP_PKEY *pkey = NULL;
142
0
    unsigned char *ek = NULL;
143
0
    int ret = 0;
144
0
    size_t eklen;
145
0
    const PKCS7_CTX *ctx = ri->ctx;
146
147
0
    pkey = X509_get0_pubkey(ri->cert);
148
0
    if (pkey == NULL)
149
0
        return 0;
150
151
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
152
0
        ossl_pkcs7_ctx_get0_propq(ctx));
153
0
    if (pctx == NULL)
154
0
        return 0;
155
156
0
    if (EVP_PKEY_encrypt_init(pctx) <= 0)
157
0
        goto err;
158
159
0
    if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
160
0
        goto err;
161
162
0
    ek = OPENSSL_malloc(eklen);
163
0
    if (ek == NULL)
164
0
        goto err;
165
166
0
    if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
167
0
        goto err;
168
169
0
    ASN1_STRING_set0(ri->enc_key, ek, (int)eklen);
170
0
    ek = NULL;
171
172
0
    ret = 1;
173
174
0
err:
175
0
    EVP_PKEY_CTX_free(pctx);
176
0
    OPENSSL_free(ek);
177
0
    return ret;
178
0
}
179
180
static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
181
    PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
182
    size_t fixlen)
183
0
{
184
0
    EVP_PKEY_CTX *pctx = NULL;
185
0
    unsigned char *ek = NULL;
186
0
    size_t eklen;
187
0
    int ret = -1;
188
0
    const PKCS7_CTX *ctx = ri->ctx;
189
190
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
191
0
        ossl_pkcs7_ctx_get0_propq(ctx));
192
0
    if (pctx == NULL)
193
0
        return -1;
194
195
0
    if (EVP_PKEY_decrypt_init(pctx) <= 0)
196
0
        goto err;
197
198
0
    if (EVP_PKEY_is_a(pkey, "RSA"))
199
        /* upper layer pkcs7 code incorrectly assumes that a successful RSA
200
         * decryption means that the key matches ciphertext (which never
201
         * was the case, implicit rejection or not), so to make it work
202
         * disable implicit rejection for RSA keys */
203
0
        EVP_PKEY_CTX_ctrl_str(pctx, "rsa_pkcs1_implicit_rejection", "0");
204
205
0
    ret = evp_pkey_decrypt_alloc(pctx, &ek, &eklen, fixlen,
206
0
        ri->enc_key->data, ri->enc_key->length);
207
0
    if (ret <= 0)
208
0
        goto err;
209
210
0
    ret = 1;
211
212
0
    OPENSSL_clear_free(*pek, *peklen);
213
0
    *pek = ek;
214
0
    *peklen = (int)eklen;
215
216
0
err:
217
0
    EVP_PKEY_CTX_free(pctx);
218
0
    if (!ret)
219
0
        OPENSSL_free(ek);
220
221
0
    return ret;
222
0
}
223
224
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
225
0
{
226
0
    int i;
227
0
    BIO *out = NULL, *btmp = NULL;
228
0
    X509_ALGOR *xa = NULL;
229
0
    EVP_CIPHER *fetched_cipher = NULL;
230
0
    const EVP_CIPHER *cipher;
231
0
    const EVP_CIPHER *evp_cipher = NULL;
232
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
233
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
234
0
    X509_ALGOR *xalg = NULL;
235
0
    PKCS7_RECIP_INFO *ri = NULL;
236
0
    ASN1_OCTET_STRING *os = NULL;
237
0
    const PKCS7_CTX *p7_ctx;
238
0
    OSSL_LIB_CTX *libctx;
239
0
    const char *propq;
240
241
0
    if (p7 == NULL) {
242
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
243
0
        return NULL;
244
0
    }
245
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
246
0
    libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
247
0
    propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
248
249
    /*
250
     * The content field in the PKCS7 ContentInfo is optional, but that really
251
     * only applies to inner content (precisely, detached signatures).
252
     *
253
     * When reading content, missing outer content is therefore treated as an
254
     * error.
255
     *
256
     * When creating content, PKCS7_content_new() must be called before
257
     * calling this method, so a NULL p7->d is always an error.
258
     */
259
0
    if (p7->d.ptr == NULL) {
260
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
261
0
        return NULL;
262
0
    }
263
264
0
    i = OBJ_obj2nid(p7->type);
265
0
    p7->state = PKCS7_S_HEADER;
266
267
0
    switch (i) {
268
0
    case NID_pkcs7_signed:
269
0
        md_sk = p7->d.sign->md_algs;
270
0
        os = pkcs7_get1_data(p7->d.sign->contents);
271
0
        break;
272
0
    case NID_pkcs7_signedAndEnveloped:
273
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
274
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
275
0
        xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
276
0
        evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
277
0
        if (evp_cipher == NULL) {
278
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
279
0
            goto err;
280
0
        }
281
0
        break;
282
0
    case NID_pkcs7_enveloped:
283
0
        rsk = p7->d.enveloped->recipientinfo;
284
0
        xalg = p7->d.enveloped->enc_data->algorithm;
285
0
        evp_cipher = p7->d.enveloped->enc_data->cipher;
286
0
        if (evp_cipher == NULL) {
287
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
288
0
            goto err;
289
0
        }
290
0
        break;
291
0
    case NID_pkcs7_digest:
292
0
        xa = p7->d.digest->md;
293
0
        os = pkcs7_get1_data(p7->d.digest->contents);
294
0
        break;
295
0
    case NID_pkcs7_data:
296
0
        break;
297
0
    default:
298
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
299
0
        goto err;
300
0
    }
301
302
0
    for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
303
0
        if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
304
0
            goto err;
305
306
0
    if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
307
0
        goto err;
308
309
0
    if (evp_cipher != NULL) {
310
0
        unsigned char key[EVP_MAX_KEY_LENGTH];
311
0
        unsigned char iv[EVP_MAX_IV_LENGTH];
312
0
        int keylen, ivlen;
313
0
        EVP_CIPHER_CTX *ctx;
314
315
0
        if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
316
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
317
0
            goto err;
318
0
        }
319
0
        BIO_get_cipher_ctx(btmp, &ctx);
320
0
        keylen = EVP_CIPHER_get_key_length(evp_cipher);
321
0
        ivlen = EVP_CIPHER_get_iv_length(evp_cipher);
322
0
        xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher));
323
0
        if (ivlen > 0)
324
0
            if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
325
0
                goto err;
326
327
0
        (void)ERR_set_mark();
328
0
        fetched_cipher = EVP_CIPHER_fetch(libctx,
329
0
            EVP_CIPHER_get0_name(evp_cipher),
330
0
            propq);
331
0
        (void)ERR_pop_to_mark();
332
0
        if (fetched_cipher != NULL)
333
0
            cipher = fetched_cipher;
334
0
        else
335
0
            cipher = evp_cipher;
336
337
0
        if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
338
0
            goto err;
339
340
0
        EVP_CIPHER_free(fetched_cipher);
341
0
        fetched_cipher = NULL;
342
343
0
        if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
344
0
            goto err;
345
0
        if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
346
0
            goto err;
347
348
0
        if (ivlen > 0) {
349
0
            if (xalg->parameter == NULL) {
350
0
                xalg->parameter = ASN1_TYPE_new();
351
0
                if (xalg->parameter == NULL)
352
0
                    goto err;
353
0
            }
354
0
            if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0) {
355
0
                ASN1_TYPE_free(xalg->parameter);
356
0
                xalg->parameter = NULL;
357
0
                goto err;
358
0
            }
359
0
        }
360
361
        /* Lets do the pub key stuff :-) */
362
0
        for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
363
0
            ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
364
0
            if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
365
0
                goto err;
366
0
        }
367
0
        OPENSSL_cleanse(key, keylen);
368
369
0
        if (out == NULL)
370
0
            out = btmp;
371
0
        else
372
0
            BIO_push(out, btmp);
373
0
        btmp = NULL;
374
0
    }
375
376
0
    if (bio == NULL) {
377
0
        if (PKCS7_is_detached(p7)) {
378
0
            bio = BIO_new(BIO_s_null());
379
0
        } else if (os != NULL && os->length > 0) {
380
            /*
381
             * bio needs a copy of os->data instead of a pointer because
382
             * the data will be used after os has been freed
383
             */
384
0
            bio = BIO_new(BIO_s_mem());
385
0
            if (bio != NULL) {
386
0
                BIO_set_mem_eof_return(bio, 0);
387
0
                if (BIO_write(bio, os->data, os->length) != os->length) {
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
        if (data_body->length > 0)
663
0
            bio = BIO_new_mem_buf(data_body->data, data_body->length);
664
0
        else {
665
0
            bio = BIO_new(BIO_s_mem());
666
0
            if (bio == NULL)
667
0
                goto err;
668
0
            BIO_set_mem_eof_return(bio, 0);
669
0
        }
670
0
        if (bio == NULL)
671
0
            goto err;
672
0
    }
673
0
    BIO_push(out, bio);
674
0
    bio = NULL;
675
0
    EVP_CIPHER_free(cipher);
676
0
    return out;
677
678
0
err:
679
0
    EVP_CIPHER_free(cipher);
680
0
    OPENSSL_clear_free(ek, eklen);
681
0
    OPENSSL_clear_free(tkey, tkeylen);
682
0
    BIO_free_all(out);
683
0
    BIO_free_all(btmp);
684
0
    BIO_free_all(etmp);
685
0
    BIO_free_all(bio);
686
0
    return NULL;
687
0
}
688
689
static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
690
0
{
691
0
    for (;;) {
692
0
        bio = BIO_find_type(bio, BIO_TYPE_MD);
693
0
        if (bio == NULL) {
694
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
695
0
            return NULL;
696
0
        }
697
0
        BIO_get_md_ctx(bio, pmd);
698
0
        if (*pmd == NULL) {
699
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
700
0
            return NULL;
701
0
        }
702
0
        if (EVP_MD_CTX_get_type(*pmd) == nid)
703
0
            return bio;
704
0
        bio = BIO_next(bio);
705
0
    }
706
0
    return NULL;
707
0
}
708
709
static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
710
0
{
711
0
    unsigned char md_data[EVP_MAX_MD_SIZE];
712
0
    unsigned int md_len;
713
714
    /* Add signing time if not already present */
715
0
    if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
716
0
        if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
717
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
718
0
            return 0;
719
0
        }
720
0
    }
721
722
    /* Add digest */
723
0
    if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
724
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
725
0
        return 0;
726
0
    }
727
0
    if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
728
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
729
0
        return 0;
730
0
    }
731
732
    /* Now sign the attributes */
733
0
    if (!PKCS7_SIGNER_INFO_sign(si))
734
0
        return 0;
735
736
0
    return 1;
737
0
}
738
739
int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
740
0
{
741
0
    int ret = 0;
742
0
    int i, j;
743
0
    BIO *btmp;
744
0
    PKCS7_SIGNER_INFO *si;
745
0
    EVP_MD_CTX *mdc, *ctx_tmp;
746
0
    STACK_OF(X509_ATTRIBUTE) *sk;
747
0
    STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
748
0
    ASN1_OCTET_STRING *os = NULL;
749
0
    const PKCS7_CTX *p7_ctx;
750
751
0
    if (p7 == NULL) {
752
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
753
0
        return 0;
754
0
    }
755
756
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
757
758
0
    if (p7->d.ptr == NULL) {
759
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
760
0
        return 0;
761
0
    }
762
763
0
    ctx_tmp = EVP_MD_CTX_new();
764
0
    if (ctx_tmp == NULL) {
765
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
766
0
        return 0;
767
0
    }
768
769
0
    i = OBJ_obj2nid(p7->type);
770
0
    p7->state = PKCS7_S_HEADER;
771
772
0
    switch (i) {
773
0
    case NID_pkcs7_data:
774
0
        os = p7->d.data;
775
0
        break;
776
0
    case NID_pkcs7_signedAndEnveloped:
777
        /* XXXXXXXXXXXXXXXX */
778
0
        si_sk = p7->d.signed_and_enveloped->signer_info;
779
0
        os = p7->d.signed_and_enveloped->enc_data->enc_data;
780
0
        if (os == NULL) {
781
0
            os = ASN1_OCTET_STRING_new();
782
0
            if (os == NULL) {
783
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
784
0
                goto err;
785
0
            }
786
0
            p7->d.signed_and_enveloped->enc_data->enc_data = os;
787
0
        }
788
0
        break;
789
0
    case NID_pkcs7_enveloped:
790
        /* XXXXXXXXXXXXXXXX */
791
0
        os = p7->d.enveloped->enc_data->enc_data;
792
0
        if (os == NULL) {
793
0
            os = ASN1_OCTET_STRING_new();
794
0
            if (os == NULL) {
795
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
796
0
                goto err;
797
0
            }
798
0
            p7->d.enveloped->enc_data->enc_data = os;
799
0
        }
800
0
        break;
801
0
    case NID_pkcs7_signed:
802
0
        si_sk = p7->d.sign->signer_info;
803
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
804
        /* If detached data then the content is excluded */
805
0
        if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
806
0
            ASN1_OCTET_STRING_free(os);
807
0
            os = NULL;
808
0
            p7->d.sign->contents->d.data = NULL;
809
0
        }
810
0
        break;
811
812
0
    case NID_pkcs7_digest:
813
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
814
        /* If detached data then the content is excluded */
815
0
        if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
816
0
            ASN1_OCTET_STRING_free(os);
817
0
            os = NULL;
818
0
            p7->d.digest->contents->d.data = NULL;
819
0
        }
820
0
        break;
821
822
0
    default:
823
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
824
0
        goto err;
825
0
    }
826
827
0
    if (si_sk != NULL) {
828
0
        for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
829
0
            si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
830
0
            if (si->pkey == NULL)
831
0
                continue;
832
833
0
            j = OBJ_obj2nid(si->digest_alg->algorithm);
834
835
0
            btmp = bio;
836
837
0
            btmp = PKCS7_find_digest(&mdc, btmp, j);
838
839
0
            if (btmp == NULL)
840
0
                goto err;
841
842
            /*
843
             * We now have the EVP_MD_CTX, lets do the signing.
844
             */
845
0
            if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
846
0
                goto err;
847
848
0
            sk = si->auth_attr;
849
850
            /*
851
             * If there are attributes, we add the digest attribute and only
852
             * sign the attributes
853
             */
854
0
            if (sk_X509_ATTRIBUTE_num(sk) > 0) {
855
0
                if (!do_pkcs7_signed_attrib(si, ctx_tmp))
856
0
                    goto err;
857
0
            } else {
858
0
                unsigned char *abuf = NULL;
859
0
                unsigned int abuflen = EVP_PKEY_get_size(si->pkey);
860
861
0
                if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL)
862
0
                    goto err;
863
864
0
                if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
865
0
                        ossl_pkcs7_ctx_get0_libctx(p7_ctx),
866
0
                        ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
867
0
                    OPENSSL_free(abuf);
868
0
                    ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
869
0
                    goto err;
870
0
                }
871
0
                ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
872
0
            }
873
0
        }
874
0
    } else if (i == NID_pkcs7_digest) {
875
0
        unsigned char md_data[EVP_MAX_MD_SIZE];
876
0
        unsigned int md_len;
877
0
        if (!PKCS7_find_digest(&mdc, bio,
878
0
                OBJ_obj2nid(p7->d.digest->md->algorithm)))
879
0
            goto err;
880
0
        if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
881
0
            goto err;
882
0
        if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
883
0
            goto err;
884
0
    }
885
886
0
    if (!PKCS7_is_detached(p7)) {
887
        /*
888
         * NOTE(emilia): I think we only reach os == NULL here because detached
889
         * digested data support is broken.
890
         */
891
0
        if (os == NULL)
892
0
            goto err;
893
0
        if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
894
0
            char *cont;
895
0
            long contlen;
896
0
            btmp = BIO_find_type(bio, BIO_TYPE_MEM);
897
0
            if (btmp == NULL) {
898
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
899
0
                goto err;
900
0
            }
901
0
            contlen = BIO_get_mem_data(btmp, &cont);
902
            /*
903
             * Mark the BIO read only then we can use its copy of the data
904
             * instead of making an extra copy.
905
             */
906
0
            BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
907
0
            BIO_set_mem_eof_return(btmp, 0);
908
0
            ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
909
0
        }
910
0
    }
911
0
    ret = 1;
912
0
err:
913
0
    EVP_MD_CTX_free(ctx_tmp);
914
0
    return ret;
915
0
}
916
917
int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
918
0
{
919
0
    EVP_MD_CTX *mctx;
920
0
    EVP_PKEY_CTX *pctx = NULL;
921
0
    unsigned char *abuf = NULL;
922
0
    int alen;
923
0
    size_t siglen;
924
0
    const EVP_MD *md = NULL;
925
0
    const PKCS7_CTX *ctx = si->ctx;
926
927
0
    md = EVP_get_digestbyobj(si->digest_alg->algorithm);
928
0
    if (md == NULL)
929
0
        return 0;
930
931
0
    mctx = EVP_MD_CTX_new();
932
0
    if (mctx == NULL) {
933
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
934
0
        goto err;
935
0
    }
936
937
0
    if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
938
0
            ossl_pkcs7_ctx_get0_libctx(ctx),
939
0
            ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
940
0
            NULL)
941
0
        <= 0)
942
0
        goto err;
943
944
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
945
0
        ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
946
0
    if (alen < 0 || abuf == NULL)
947
0
        goto err;
948
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
949
0
        goto err;
950
0
    OPENSSL_free(abuf);
951
0
    abuf = NULL;
952
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
953
0
        goto err;
954
0
    abuf = OPENSSL_malloc(siglen);
955
0
    if (abuf == NULL)
956
0
        goto err;
957
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
958
0
        goto err;
959
960
0
    EVP_MD_CTX_free(mctx);
961
962
0
    ASN1_STRING_set0(si->enc_digest, abuf, (int)siglen);
963
964
0
    return 1;
965
966
0
err:
967
0
    OPENSSL_free(abuf);
968
0
    EVP_MD_CTX_free(mctx);
969
0
    return 0;
970
0
}
971
972
/* This partly overlaps with PKCS7_verify(). It does not support flags. */
973
int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
974
    PKCS7 *p7, PKCS7_SIGNER_INFO *si)
975
0
{
976
0
    PKCS7_ISSUER_AND_SERIAL *ias;
977
0
    int ret = 0, i;
978
0
    STACK_OF(X509) *untrusted;
979
0
    STACK_OF(X509_CRL) *crls;
980
0
    X509 *signer;
981
982
0
    if (p7 == NULL) {
983
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
984
0
        return 0;
985
0
    }
986
987
0
    if (p7->d.ptr == NULL) {
988
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
989
0
        return 0;
990
0
    }
991
992
0
    if (PKCS7_type_is_signed(p7)) {
993
0
        untrusted = p7->d.sign->cert;
994
0
        crls = p7->d.sign->crl;
995
0
    } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
996
0
        untrusted = p7->d.signed_and_enveloped->cert;
997
0
        crls = p7->d.signed_and_enveloped->crl;
998
0
    } else {
999
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1000
0
        goto err;
1001
0
    }
1002
0
    X509_STORE_CTX_set0_crls(ctx, crls);
1003
1004
    /* XXXXXXXXXXXXXXXXXXXXXXX */
1005
0
    ias = si->issuer_and_serial;
1006
1007
0
    signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
1008
1009
    /* Were we able to find the signer certificate in passed to us? */
1010
0
    if (signer == NULL) {
1011
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
1012
0
        goto err;
1013
0
    }
1014
1015
    /* Lets verify */
1016
0
    if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
1017
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1018
0
        goto err;
1019
0
    }
1020
0
    X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1021
0
    i = X509_verify_cert(ctx);
1022
0
    if (i <= 0) {
1023
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1024
0
        goto err;
1025
0
    }
1026
1027
0
    return PKCS7_signatureVerify(bio, p7, si, signer);
1028
0
err:
1029
0
    return ret;
1030
0
}
1031
1032
int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1033
    X509 *signer)
1034
0
{
1035
0
    ASN1_OCTET_STRING *os;
1036
0
    EVP_MD_CTX *mdc_tmp, *mdc;
1037
0
    EVP_MD *md = NULL;
1038
0
    int ret = 0, i;
1039
0
    int md_type;
1040
0
    STACK_OF(X509_ATTRIBUTE) *sk;
1041
0
    BIO *btmp;
1042
0
    EVP_PKEY *pkey;
1043
0
    unsigned char *abuf = NULL;
1044
0
    const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1045
0
    OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1046
0
    const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1047
1048
0
    mdc_tmp = EVP_MD_CTX_new();
1049
0
    if (mdc_tmp == NULL) {
1050
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
1051
0
        goto err;
1052
0
    }
1053
1054
0
    if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1055
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1056
0
        goto err;
1057
0
    }
1058
1059
0
    md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1060
1061
0
    btmp = bio;
1062
0
    for (;;) {
1063
0
        if ((btmp == NULL) || ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1064
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1065
0
            goto err;
1066
0
        }
1067
0
        BIO_get_md_ctx(btmp, &mdc);
1068
0
        if (mdc == NULL) {
1069
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1070
0
            goto err;
1071
0
        }
1072
0
        if (EVP_MD_CTX_get_type(mdc) == md_type)
1073
0
            break;
1074
        /*
1075
         * Workaround for some broken clients that put the signature OID
1076
         * instead of the digest OID in digest_alg->algorithm
1077
         */
1078
0
        if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1079
0
            break;
1080
0
        btmp = BIO_next(btmp);
1081
0
    }
1082
1083
    /*
1084
     * mdc is the digest ctx that we want, unless there are attributes, in
1085
     * which case the digest is the signed attributes
1086
     */
1087
0
    if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1088
0
        goto err;
1089
1090
0
    sk = si->auth_attr;
1091
0
    if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1092
0
        unsigned char md_dat[EVP_MAX_MD_SIZE];
1093
0
        unsigned int md_len;
1094
0
        int alen;
1095
0
        const ASN1_OCTET_STRING *message_digest;
1096
1097
0
        if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1098
0
            goto err;
1099
0
        message_digest = PKCS7_digest_from_attributes(sk);
1100
0
        if (!message_digest) {
1101
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1102
0
            goto err;
1103
0
        }
1104
0
        if ((message_digest->length != (int)md_len) || (memcmp(message_digest->data, md_dat, md_len))) {
1105
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1106
0
            ret = -1;
1107
0
            goto err;
1108
0
        }
1109
1110
0
        md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1111
1112
0
        if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1113
0
            goto err;
1114
0
        }
1115
1116
0
        alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1117
0
            ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1118
0
        if (alen <= 0 || abuf == NULL) {
1119
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1120
0
            ret = -1;
1121
0
            goto err;
1122
0
        }
1123
0
        if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1124
0
            goto err;
1125
0
    }
1126
1127
0
    os = si->enc_digest;
1128
0
    pkey = X509_get0_pubkey(signer);
1129
0
    if (pkey == NULL) {
1130
0
        ret = -1;
1131
0
        goto err;
1132
0
    }
1133
1134
0
    i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1135
0
    if (i <= 0) {
1136
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1137
0
        ret = -1;
1138
0
        goto err;
1139
0
    }
1140
0
    ret = 1;
1141
0
err:
1142
0
    OPENSSL_free(abuf);
1143
0
    EVP_MD_CTX_free(mdc_tmp);
1144
0
    EVP_MD_free(md);
1145
0
    return ret;
1146
0
}
1147
1148
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1149
0
{
1150
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk;
1151
0
    PKCS7_RECIP_INFO *ri;
1152
0
    int i;
1153
1154
0
    i = OBJ_obj2nid(p7->type);
1155
0
    if (i != NID_pkcs7_signedAndEnveloped)
1156
0
        return NULL;
1157
0
    if (p7->d.signed_and_enveloped == NULL)
1158
0
        return NULL;
1159
0
    rsk = p7->d.signed_and_enveloped->recipientinfo;
1160
0
    if (rsk == NULL)
1161
0
        return NULL;
1162
0
    if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1163
0
        return NULL;
1164
0
    ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1165
0
    return ri->issuer_and_serial;
1166
0
}
1167
1168
const ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1169
0
{
1170
0
    return get_attribute(si->auth_attr, nid);
1171
0
}
1172
1173
const ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1174
0
{
1175
0
    return get_attribute(si->unauth_attr, nid);
1176
0
}
1177
1178
static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1179
0
{
1180
0
    int idx = X509at_get_attr_by_NID(sk, nid, -1);
1181
1182
0
    if (idx < 0)
1183
0
        return NULL;
1184
0
    return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1185
0
}
1186
1187
const ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1188
0
{
1189
0
    const ASN1_TYPE *astype;
1190
0
    if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1191
0
        return NULL;
1192
0
    if (astype->type != V_ASN1_OCTET_STRING)
1193
0
        return NULL;
1194
0
    return astype->value.octet_string;
1195
0
}
1196
1197
int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1198
    STACK_OF(X509_ATTRIBUTE) *sk)
1199
0
{
1200
0
    sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1201
0
    p7si->auth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free);
1202
0
    if (p7si->auth_attr == NULL)
1203
0
        return 0;
1204
0
    return 1;
1205
0
}
1206
1207
int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1208
    STACK_OF(X509_ATTRIBUTE) *sk)
1209
0
{
1210
0
    sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1211
0
    p7si->unauth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free);
1212
0
    if (p7si->unauth_attr == NULL)
1213
0
        return 0;
1214
0
    return 1;
1215
0
}
1216
1217
int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1218
    void *value)
1219
0
{
1220
0
    return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1221
0
}
1222
1223
int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1224
    void *value)
1225
0
{
1226
0
    return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1227
0
}
1228
1229
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1230
    void *value)
1231
0
{
1232
0
    X509_ATTRIBUTE *attr = NULL;
1233
0
    int i, n;
1234
1235
0
    if (*sk == NULL) {
1236
0
        if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1237
0
            return 0;
1238
0
    }
1239
0
    n = sk_X509_ATTRIBUTE_num(*sk);
1240
0
    for (i = 0; i < n; i++) {
1241
0
        attr = sk_X509_ATTRIBUTE_value(*sk, i);
1242
0
        if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid)
1243
0
            goto end;
1244
0
    }
1245
0
    if (!sk_X509_ATTRIBUTE_push(*sk, NULL))
1246
0
        return 0;
1247
1248
0
end:
1249
0
    attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1250
0
    if (attr == NULL) {
1251
0
        if (i == n)
1252
0
            sk_X509_ATTRIBUTE_pop(*sk);
1253
0
        return 0;
1254
0
    }
1255
0
    X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i));
1256
0
    (void)sk_X509_ATTRIBUTE_set(*sk, i, attr);
1257
0
    return 1;
1258
0
}