Coverage Report

Created: 2026-07-23 06:28

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