Coverage Report

Created: 2026-07-23 06:28

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