Coverage Report

Created: 2026-05-06 07:07

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