Coverage Report

Created: 2025-12-31 06:58

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