Coverage Report

Created: 2025-08-28 07:07

/src/openssl30/crypto/pkcs7/pk7_doit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2024 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
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 int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
58
                                const PKCS7_CTX *ctx)
59
0
{
60
0
    BIO *btmp;
61
0
    char name[OSSL_MAX_NAME_SIZE];
62
0
    EVP_MD *fetched = NULL;
63
0
    const EVP_MD *md;
64
65
0
    if ((btmp = BIO_new(BIO_f_md())) == NULL) {
66
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
67
0
        goto err;
68
0
    }
69
70
0
    OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
71
72
0
    (void)ERR_set_mark();
73
0
    fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
74
0
                           ossl_pkcs7_ctx_get0_propq(ctx));
75
0
    if (fetched != NULL)
76
0
        md = fetched;
77
0
    else
78
0
        md = EVP_get_digestbyname(name);
79
80
0
    if (md == NULL) {
81
0
        (void)ERR_clear_last_mark();
82
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
83
0
        goto err;
84
0
    }
85
0
    (void)ERR_pop_to_mark();
86
87
0
    if (BIO_set_md(btmp, md) <= 0) {
88
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
89
0
        EVP_MD_free(fetched);
90
0
        goto err;
91
0
    }
92
0
    EVP_MD_free(fetched);
93
0
    if (*pbio == NULL)
94
0
        *pbio = btmp;
95
0
    else if (!BIO_push(*pbio, btmp)) {
96
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
97
0
        goto err;
98
0
    }
99
0
    btmp = NULL;
100
101
0
    return 1;
102
103
0
 err:
104
0
    BIO_free(btmp);
105
0
    return 0;
106
0
}
107
108
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
109
                              unsigned char *key, int keylen)
110
0
{
111
0
    EVP_PKEY_CTX *pctx = NULL;
112
0
    EVP_PKEY *pkey = NULL;
113
0
    unsigned char *ek = NULL;
114
0
    int ret = 0;
115
0
    size_t eklen;
116
0
    const PKCS7_CTX *ctx = ri->ctx;
117
118
0
    pkey = X509_get0_pubkey(ri->cert);
119
0
    if (pkey == NULL)
120
0
        return 0;
121
122
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
123
0
                                      ossl_pkcs7_ctx_get0_propq(ctx));
124
0
    if (pctx == NULL)
125
0
        return 0;
126
127
0
    if (EVP_PKEY_encrypt_init(pctx) <= 0)
128
0
        goto err;
129
130
0
    if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
131
0
        goto err;
132
133
0
    ek = OPENSSL_malloc(eklen);
134
135
0
    if (ek == NULL) {
136
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
137
0
        goto err;
138
0
    }
139
140
0
    if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
141
0
        goto err;
142
143
0
    ASN1_STRING_set0(ri->enc_key, ek, eklen);
144
0
    ek = NULL;
145
146
0
    ret = 1;
147
148
0
 err:
149
0
    EVP_PKEY_CTX_free(pctx);
150
0
    OPENSSL_free(ek);
151
0
    return ret;
152
153
0
}
154
155
static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
156
                               PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
157
                               size_t fixlen)
158
0
{
159
0
    EVP_PKEY_CTX *pctx = NULL;
160
0
    unsigned char *ek = NULL;
161
0
    size_t eklen;
162
0
    int ret = -1;
163
0
    const PKCS7_CTX *ctx = ri->ctx;
164
165
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
166
0
                                      ossl_pkcs7_ctx_get0_propq(ctx));
167
0
    if (pctx == NULL)
168
0
        return -1;
169
170
0
    if (EVP_PKEY_decrypt_init(pctx) <= 0)
171
0
        goto err;
172
173
0
    if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
174
0
                         ri->enc_key->data, ri->enc_key->length) <= 0)
175
0
        goto err;
176
177
0
    ek = OPENSSL_malloc(eklen);
178
179
0
    if (ek == NULL) {
180
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
181
0
        goto err;
182
0
    }
183
184
0
    if (EVP_PKEY_decrypt(pctx, ek, &eklen,
185
0
                         ri->enc_key->data, ri->enc_key->length) <= 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)) < 0)
587
0
                    goto err;
588
0
                ERR_clear_error();
589
0
            }
590
0
        } else {
591
0
            ri->ctx = p7_ctx;
592
            /* Only exit on fatal errors, not decrypt failure */
593
0
            if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
594
0
                goto err;
595
0
            ERR_clear_error();
596
0
        }
597
598
0
        evp_ctx = NULL;
599
0
        BIO_get_cipher_ctx(etmp, &evp_ctx);
600
0
        if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
601
0
            goto err;
602
0
        if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0)
603
0
            goto err;
604
        /* Generate random key as MMA defence */
605
0
        len = EVP_CIPHER_CTX_get_key_length(evp_ctx);
606
0
        if (len <= 0)
607
0
            goto err;
608
0
        tkeylen = (size_t)len;
609
0
        tkey = OPENSSL_malloc(tkeylen);
610
0
        if (tkey == NULL)
611
0
            goto err;
612
0
        if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
613
0
            goto err;
614
0
        if (ek == NULL) {
615
0
            ek = tkey;
616
0
            eklen = tkeylen;
617
0
            tkey = NULL;
618
0
        }
619
620
0
        if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) {
621
            /*
622
             * Some S/MIME clients don't use the same key and effective key
623
             * length. The key length is determined by the size of the
624
             * decrypted RSA key.
625
             */
626
0
            if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) {
627
                /* Use random key as MMA defence */
628
0
                OPENSSL_clear_free(ek, eklen);
629
0
                ek = tkey;
630
0
                eklen = tkeylen;
631
0
                tkey = NULL;
632
0
            }
633
0
        }
634
        /* Clear errors so we don't leak information useful in MMA */
635
0
        ERR_clear_error();
636
0
        if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
637
0
            goto err;
638
639
0
        OPENSSL_clear_free(ek, eklen);
640
0
        ek = NULL;
641
0
        OPENSSL_clear_free(tkey, tkeylen);
642
0
        tkey = NULL;
643
644
0
        if (out == NULL)
645
0
            out = etmp;
646
0
        else
647
0
            BIO_push(out, etmp);
648
0
        etmp = NULL;
649
0
    }
650
0
    if (in_bio != NULL) {
651
0
        bio = in_bio;
652
0
    } else {
653
0
        if (data_body->length > 0)
654
0
            bio = BIO_new_mem_buf(data_body->data, data_body->length);
655
0
        else {
656
0
            bio = BIO_new(BIO_s_mem());
657
0
            if (bio == NULL)
658
0
                goto err;
659
0
            BIO_set_mem_eof_return(bio, 0);
660
0
        }
661
0
        if (bio == NULL)
662
0
            goto err;
663
0
    }
664
0
    BIO_push(out, bio);
665
0
    bio = NULL;
666
0
    EVP_CIPHER_free(evp_cipher);
667
0
    return out;
668
669
0
 err:
670
0
    EVP_CIPHER_free(evp_cipher);
671
0
    OPENSSL_clear_free(ek, eklen);
672
0
    OPENSSL_clear_free(tkey, tkeylen);
673
0
    BIO_free_all(out);
674
0
    BIO_free_all(btmp);
675
0
    BIO_free_all(etmp);
676
0
    BIO_free_all(bio);
677
0
    return NULL;
678
0
}
679
680
static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
681
0
{
682
0
    for (;;) {
683
0
        bio = BIO_find_type(bio, BIO_TYPE_MD);
684
0
        if (bio == NULL) {
685
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
686
0
            return NULL;
687
0
        }
688
0
        BIO_get_md_ctx(bio, pmd);
689
0
        if (*pmd == NULL) {
690
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
691
0
            return NULL;
692
0
        }
693
0
        if (EVP_MD_CTX_get_type(*pmd) == nid)
694
0
            return bio;
695
0
        bio = BIO_next(bio);
696
0
    }
697
0
    return NULL;
698
0
}
699
700
static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
701
0
{
702
0
    unsigned char md_data[EVP_MAX_MD_SIZE];
703
0
    unsigned int md_len;
704
705
    /* Add signing time if not already present */
706
0
    if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
707
0
        if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
708
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
709
0
            return 0;
710
0
        }
711
0
    }
712
713
    /* Add digest */
714
0
    if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
715
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
716
0
        return 0;
717
0
    }
718
0
    if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
719
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
720
0
        return 0;
721
0
    }
722
723
    /* Now sign the attributes */
724
0
    if (!PKCS7_SIGNER_INFO_sign(si))
725
0
        return 0;
726
727
0
    return 1;
728
0
}
729
730
int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
731
0
{
732
0
    int ret = 0;
733
0
    int i, j;
734
0
    BIO *btmp;
735
0
    PKCS7_SIGNER_INFO *si;
736
0
    EVP_MD_CTX *mdc, *ctx_tmp;
737
0
    STACK_OF(X509_ATTRIBUTE) *sk;
738
0
    STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
739
0
    ASN1_OCTET_STRING *os = NULL;
740
0
    const PKCS7_CTX *p7_ctx;
741
742
0
    if (p7 == NULL) {
743
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
744
0
        return 0;
745
0
    }
746
747
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
748
749
0
    if (p7->d.ptr == NULL) {
750
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
751
0
        return 0;
752
0
    }
753
754
0
    ctx_tmp = EVP_MD_CTX_new();
755
0
    if (ctx_tmp == NULL) {
756
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
757
0
        return 0;
758
0
    }
759
760
0
    i = OBJ_obj2nid(p7->type);
761
0
    p7->state = PKCS7_S_HEADER;
762
763
0
    switch (i) {
764
0
    case NID_pkcs7_data:
765
0
        os = p7->d.data;
766
0
        break;
767
0
    case NID_pkcs7_signedAndEnveloped:
768
        /* XXXXXXXXXXXXXXXX */
769
0
        si_sk = p7->d.signed_and_enveloped->signer_info;
770
0
        os = p7->d.signed_and_enveloped->enc_data->enc_data;
771
0
        if (os == NULL) {
772
0
            os = ASN1_OCTET_STRING_new();
773
0
            if (os == NULL) {
774
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
775
0
                goto err;
776
0
            }
777
0
            p7->d.signed_and_enveloped->enc_data->enc_data = os;
778
0
        }
779
0
        break;
780
0
    case NID_pkcs7_enveloped:
781
        /* XXXXXXXXXXXXXXXX */
782
0
        os = p7->d.enveloped->enc_data->enc_data;
783
0
        if (os == NULL) {
784
0
            os = ASN1_OCTET_STRING_new();
785
0
            if (os == NULL) {
786
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
787
0
                goto err;
788
0
            }
789
0
            p7->d.enveloped->enc_data->enc_data = os;
790
0
        }
791
0
        break;
792
0
    case NID_pkcs7_signed:
793
0
        si_sk = p7->d.sign->signer_info;
794
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
795
        /* If detached data then the content is excluded */
796
0
        if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
797
0
            ASN1_OCTET_STRING_free(os);
798
0
            os = NULL;
799
0
            p7->d.sign->contents->d.data = NULL;
800
0
        }
801
0
        break;
802
803
0
    case NID_pkcs7_digest:
804
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
805
        /* If detached data then the content is excluded */
806
0
        if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
807
0
            ASN1_OCTET_STRING_free(os);
808
0
            os = NULL;
809
0
            p7->d.digest->contents->d.data = NULL;
810
0
        }
811
0
        break;
812
813
0
    default:
814
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
815
0
        goto err;
816
0
    }
817
818
0
    if (si_sk != NULL) {
819
0
        for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
820
0
            si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
821
0
            if (si->pkey == NULL)
822
0
                continue;
823
824
0
            j = OBJ_obj2nid(si->digest_alg->algorithm);
825
826
0
            btmp = bio;
827
828
0
            btmp = PKCS7_find_digest(&mdc, btmp, j);
829
830
0
            if (btmp == NULL)
831
0
                goto err;
832
833
            /*
834
             * We now have the EVP_MD_CTX, lets do the signing.
835
             */
836
0
            if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
837
0
                goto err;
838
839
0
            sk = si->auth_attr;
840
841
            /*
842
             * If there are attributes, we add the digest attribute and only
843
             * sign the attributes
844
             */
845
0
            if (sk_X509_ATTRIBUTE_num(sk) > 0) {
846
0
                if (!do_pkcs7_signed_attrib(si, ctx_tmp))
847
0
                    goto err;
848
0
            } else {
849
0
                unsigned char *abuf = NULL;
850
0
                unsigned int abuflen;
851
0
                abuflen = EVP_PKEY_get_size(si->pkey);
852
0
                abuf = OPENSSL_malloc(abuflen);
853
0
                if (abuf == NULL)
854
0
                    goto err;
855
856
0
                if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
857
0
                                      ossl_pkcs7_ctx_get0_libctx(p7_ctx),
858
0
                                      ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
859
0
                    OPENSSL_free(abuf);
860
0
                    ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
861
0
                    goto err;
862
0
                }
863
0
                ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
864
0
            }
865
0
        }
866
0
    } else if (i == NID_pkcs7_digest) {
867
0
        unsigned char md_data[EVP_MAX_MD_SIZE];
868
0
        unsigned int md_len;
869
0
        if (!PKCS7_find_digest(&mdc, bio,
870
0
                               OBJ_obj2nid(p7->d.digest->md->algorithm)))
871
0
            goto err;
872
0
        if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
873
0
            goto err;
874
0
        if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
875
0
            goto err;
876
0
    }
877
878
0
    if (!PKCS7_is_detached(p7)) {
879
        /*
880
         * NOTE(emilia): I think we only reach os == NULL here because detached
881
         * digested data support is broken.
882
         */
883
0
        if (os == NULL)
884
0
            goto err;
885
0
        if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
886
0
            char *cont;
887
0
            long contlen;
888
0
            btmp = BIO_find_type(bio, BIO_TYPE_MEM);
889
0
            if (btmp == NULL) {
890
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
891
0
                goto err;
892
0
            }
893
0
            contlen = BIO_get_mem_data(btmp, &cont);
894
            /*
895
             * Mark the BIO read only then we can use its copy of the data
896
             * instead of making an extra copy.
897
             */
898
0
            BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
899
0
            BIO_set_mem_eof_return(btmp, 0);
900
0
            ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
901
0
        }
902
0
    }
903
0
    ret = 1;
904
0
 err:
905
0
    EVP_MD_CTX_free(ctx_tmp);
906
0
    return ret;
907
0
}
908
909
int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
910
0
{
911
0
    EVP_MD_CTX *mctx;
912
0
    EVP_PKEY_CTX *pctx = NULL;
913
0
    unsigned char *abuf = NULL;
914
0
    int alen;
915
0
    size_t siglen;
916
0
    const EVP_MD *md = NULL;
917
0
    const PKCS7_CTX *ctx = si->ctx;
918
919
0
    md = EVP_get_digestbyobj(si->digest_alg->algorithm);
920
0
    if (md == NULL)
921
0
        return 0;
922
923
0
    mctx = EVP_MD_CTX_new();
924
0
    if (mctx == NULL) {
925
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
926
0
        goto err;
927
0
    }
928
929
0
    if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
930
0
                              ossl_pkcs7_ctx_get0_libctx(ctx),
931
0
                              ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
932
0
                              NULL) <= 0)
933
0
        goto err;
934
935
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
936
0
                         ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
937
0
    if (!abuf)
938
0
        goto err;
939
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
940
0
        goto err;
941
0
    OPENSSL_free(abuf);
942
0
    abuf = NULL;
943
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
944
0
        goto err;
945
0
    abuf = OPENSSL_malloc(siglen);
946
0
    if (abuf == NULL)
947
0
        goto err;
948
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
949
0
        goto err;
950
951
0
    EVP_MD_CTX_free(mctx);
952
953
0
    ASN1_STRING_set0(si->enc_digest, abuf, siglen);
954
955
0
    return 1;
956
957
0
 err:
958
0
    OPENSSL_free(abuf);
959
0
    EVP_MD_CTX_free(mctx);
960
0
    return 0;
961
0
}
962
963
int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
964
                     PKCS7 *p7, PKCS7_SIGNER_INFO *si)
965
0
{
966
0
    PKCS7_ISSUER_AND_SERIAL *ias;
967
0
    int ret = 0, i;
968
0
    STACK_OF(X509) *cert;
969
0
    X509 *x509;
970
971
0
    if (p7 == NULL) {
972
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
973
0
        return 0;
974
0
    }
975
976
0
    if (p7->d.ptr == NULL) {
977
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
978
0
        return 0;
979
0
    }
980
981
0
    if (PKCS7_type_is_signed(p7)) {
982
0
        cert = p7->d.sign->cert;
983
0
    } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
984
0
        cert = p7->d.signed_and_enveloped->cert;
985
0
    } else {
986
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
987
0
        goto err;
988
0
    }
989
    /* XXXXXXXXXXXXXXXXXXXXXXX */
990
0
    ias = si->issuer_and_serial;
991
992
0
    x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
993
994
    /* were we able to find the cert in passed to us */
995
0
    if (x509 == NULL) {
996
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
997
0
        goto err;
998
0
    }
999
1000
    /* Lets verify */
1001
0
    if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
1002
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1003
0
        goto err;
1004
0
    }
1005
0
    X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1006
0
    i = X509_verify_cert(ctx);
1007
0
    if (i <= 0) {
1008
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1009
0
        goto err;
1010
0
    }
1011
1012
0
    return PKCS7_signatureVerify(bio, p7, si, x509);
1013
0
 err:
1014
0
    return ret;
1015
0
}
1016
1017
int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1018
                          X509 *x509)
1019
0
{
1020
0
    ASN1_OCTET_STRING *os;
1021
0
    EVP_MD_CTX *mdc_tmp, *mdc;
1022
0
    const EVP_MD *md;
1023
0
    EVP_MD *fetched_md = NULL;
1024
0
    int ret = 0, i;
1025
0
    int md_type;
1026
0
    STACK_OF(X509_ATTRIBUTE) *sk;
1027
0
    BIO *btmp;
1028
0
    EVP_PKEY *pkey;
1029
0
    unsigned char *abuf = NULL;
1030
0
    const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1031
0
    OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1032
0
    const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1033
1034
0
    mdc_tmp = EVP_MD_CTX_new();
1035
0
    if (mdc_tmp == NULL) {
1036
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
1037
0
        goto err;
1038
0
    }
1039
1040
0
    if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1041
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1042
0
        goto err;
1043
0
    }
1044
1045
0
    md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1046
1047
0
    btmp = bio;
1048
0
    for (;;) {
1049
0
        if ((btmp == NULL) ||
1050
0
            ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1051
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1052
0
            goto err;
1053
0
        }
1054
0
        BIO_get_md_ctx(btmp, &mdc);
1055
0
        if (mdc == NULL) {
1056
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1057
0
            goto err;
1058
0
        }
1059
0
        if (EVP_MD_CTX_get_type(mdc) == md_type)
1060
0
            break;
1061
        /*
1062
         * Workaround for some broken clients that put the signature OID
1063
         * instead of the digest OID in digest_alg->algorithm
1064
         */
1065
0
        if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1066
0
            break;
1067
0
        btmp = BIO_next(btmp);
1068
0
    }
1069
1070
    /*
1071
     * mdc is the digest ctx that we want, unless there are attributes, in
1072
     * which case the digest is the signed attributes
1073
     */
1074
0
    if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1075
0
        goto err;
1076
1077
0
    sk = si->auth_attr;
1078
0
    if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1079
0
        unsigned char md_dat[EVP_MAX_MD_SIZE];
1080
0
        unsigned int md_len;
1081
0
        int alen;
1082
0
        ASN1_OCTET_STRING *message_digest;
1083
1084
0
        if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1085
0
            goto err;
1086
0
        message_digest = PKCS7_digest_from_attributes(sk);
1087
0
        if (!message_digest) {
1088
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1089
0
            goto err;
1090
0
        }
1091
0
        if ((message_digest->length != (int)md_len) ||
1092
0
            (memcmp(message_digest->data, md_dat, md_len))) {
1093
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1094
0
            ret = -1;
1095
0
            goto err;
1096
0
        }
1097
1098
0
        (void)ERR_set_mark();
1099
0
        fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1100
1101
0
        if (fetched_md != NULL)
1102
0
            md = fetched_md;
1103
0
        else
1104
0
            md = EVP_get_digestbynid(md_type);
1105
1106
0
        if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1107
0
            (void)ERR_clear_last_mark();
1108
0
            goto err;
1109
0
        }
1110
0
        (void)ERR_pop_to_mark();
1111
1112
0
        alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1113
0
                             ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1114
0
        if (alen <= 0) {
1115
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1116
0
            ret = -1;
1117
0
            goto err;
1118
0
        }
1119
0
        if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1120
0
            goto err;
1121
0
    }
1122
1123
0
    os = si->enc_digest;
1124
0
    pkey = X509_get0_pubkey(x509);
1125
0
    if (pkey == NULL) {
1126
0
        ret = -1;
1127
0
        goto err;
1128
0
    }
1129
1130
0
    i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1131
0
    if (i <= 0) {
1132
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1133
0
        ret = -1;
1134
0
        goto err;
1135
0
    }
1136
0
    ret = 1;
1137
0
 err:
1138
0
    OPENSSL_free(abuf);
1139
0
    EVP_MD_CTX_free(mdc_tmp);
1140
0
    EVP_MD_free(fetched_md);
1141
0
    return ret;
1142
0
}
1143
1144
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1145
0
{
1146
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk;
1147
0
    PKCS7_RECIP_INFO *ri;
1148
0
    int i;
1149
1150
0
    i = OBJ_obj2nid(p7->type);
1151
0
    if (i != NID_pkcs7_signedAndEnveloped)
1152
0
        return NULL;
1153
0
    if (p7->d.signed_and_enveloped == NULL)
1154
0
        return NULL;
1155
0
    rsk = p7->d.signed_and_enveloped->recipientinfo;
1156
0
    if (rsk == NULL)
1157
0
        return NULL;
1158
0
    if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1159
0
        return NULL;
1160
0
    ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1161
0
    return ri->issuer_and_serial;
1162
0
}
1163
1164
ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1165
248
{
1166
248
    return get_attribute(si->auth_attr, nid);
1167
248
}
1168
1169
ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1170
0
{
1171
0
    return get_attribute(si->unauth_attr, nid);
1172
0
}
1173
1174
static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1175
{
1176
    int idx;
1177
    X509_ATTRIBUTE *xa;
1178
    idx = X509at_get_attr_by_NID(sk, nid, -1);
1179
    xa = X509at_get_attr(sk, idx);
1180
    return X509_ATTRIBUTE_get0_type(xa, 0);
1181
}
1182
1183
ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1184
0
{
1185
0
    ASN1_TYPE *astype;
1186
0
    if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1187
0
        return NULL;
1188
0
    return astype->value.octet_string;
1189
0
}
1190
1191
int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1192
                                STACK_OF(X509_ATTRIBUTE) *sk)
1193
0
{
1194
0
    int i;
1195
1196
0
    sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1197
0
    p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1198
0
    if (p7si->auth_attr == NULL)
1199
0
        return 0;
1200
0
    for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1201
0
        if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1202
0
                                   X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1203
0
                                                      (sk, i))))
1204
0
            == NULL)
1205
0
            return 0;
1206
0
    }
1207
0
    return 1;
1208
0
}
1209
1210
int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1211
                         STACK_OF(X509_ATTRIBUTE) *sk)
1212
0
{
1213
0
    int i;
1214
1215
0
    sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1216
0
    p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1217
0
    if (p7si->unauth_attr == NULL)
1218
0
        return 0;
1219
0
    for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1220
0
        if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1221
0
                                   X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1222
0
                                                      (sk, i))))
1223
0
            == NULL)
1224
0
            return 0;
1225
0
    }
1226
0
    return 1;
1227
0
}
1228
1229
int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1230
                               void *value)
1231
0
{
1232
0
    return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1233
0
}
1234
1235
int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1236
                        void *value)
1237
0
{
1238
0
    return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1239
0
}
1240
1241
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1242
                         void *value)
1243
0
{
1244
0
    X509_ATTRIBUTE *attr = NULL;
1245
0
    int i, n;
1246
1247
0
    if (*sk == NULL) {
1248
0
        if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1249
0
            return 0;
1250
0
    }
1251
0
    n = sk_X509_ATTRIBUTE_num(*sk);
1252
0
    for (i = 0; i < n; i++) {
1253
0
        attr = sk_X509_ATTRIBUTE_value(*sk, i);
1254
0
        if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid)
1255
0
            goto end;
1256
0
    }
1257
0
    if (!sk_X509_ATTRIBUTE_push(*sk, NULL))
1258
0
        return 0;
1259
1260
0
 end:
1261
0
    attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1262
0
    if (attr == NULL) {
1263
0
        if (i == n)
1264
0
            sk_X509_ATTRIBUTE_pop(*sk);
1265
0
        return 0;
1266
0
    }
1267
0
    X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i));
1268
0
    (void) sk_X509_ATTRIBUTE_set(*sk, i, attr);
1269
0
    return 1;
1270
0
}