Coverage Report

Created: 2026-04-01 06:39

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