Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/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 "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
46
0
}
47
48
ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
49
0
{
50
0
    if (PKCS7_type_is_data(p7))
51
0
        return p7->d.data;
52
0
    if (PKCS7_type_is_other(p7) && p7->d.other
53
0
        && (p7->d.other->type == V_ASN1_OCTET_STRING))
54
0
        return p7->d.other->value.octet_string;
55
0
    return NULL;
56
0
}
57
58
static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
59
                                const PKCS7_CTX *ctx)
60
0
{
61
0
    BIO *btmp;
62
0
    char name[OSSL_MAX_NAME_SIZE];
63
0
    EVP_MD *fetched = NULL;
64
0
    const EVP_MD *md;
65
66
0
    if ((btmp = BIO_new(BIO_f_md())) == NULL) {
67
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
68
0
        goto err;
69
0
    }
70
71
0
    OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
72
73
0
    (void)ERR_set_mark();
74
0
    fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
75
0
                           ossl_pkcs7_ctx_get0_propq(ctx));
76
0
    if (fetched != NULL)
77
0
        md = fetched;
78
0
    else
79
0
        md = EVP_get_digestbyname(name);
80
81
0
    if (md == NULL) {
82
0
        (void)ERR_clear_last_mark();
83
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
84
0
        goto err;
85
0
    }
86
0
    (void)ERR_pop_to_mark();
87
88
0
    if (BIO_set_md(btmp, md) <= 0) {
89
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
90
0
        EVP_MD_free(fetched);
91
0
        goto err;
92
0
    }
93
0
    EVP_MD_free(fetched);
94
0
    if (*pbio == NULL)
95
0
        *pbio = btmp;
96
0
    else if (!BIO_push(*pbio, btmp)) {
97
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
98
0
        goto err;
99
0
    }
100
0
    btmp = NULL;
101
102
0
    return 1;
103
104
0
 err:
105
0
    BIO_free(btmp);
106
0
    return 0;
107
0
}
108
109
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
110
                              unsigned char *key, int keylen)
111
0
{
112
0
    EVP_PKEY_CTX *pctx = NULL;
113
0
    EVP_PKEY *pkey = NULL;
114
0
    unsigned char *ek = NULL;
115
0
    int ret = 0;
116
0
    size_t eklen;
117
0
    const PKCS7_CTX *ctx = ri->ctx;
118
119
0
    pkey = X509_get0_pubkey(ri->cert);
120
0
    if (pkey == NULL)
121
0
        return 0;
122
123
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
124
0
                                      ossl_pkcs7_ctx_get0_propq(ctx));
125
0
    if (pctx == NULL)
126
0
        return 0;
127
128
0
    if (EVP_PKEY_encrypt_init(pctx) <= 0)
129
0
        goto err;
130
131
0
    if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
132
0
        goto err;
133
134
0
    ek = OPENSSL_malloc(eklen);
135
0
    if (ek == NULL)
136
0
        goto err;
137
138
0
    if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
139
0
        goto err;
140
141
0
    ASN1_STRING_set0(ri->enc_key, ek, eklen);
142
0
    ek = NULL;
143
144
0
    ret = 1;
145
146
0
 err:
147
0
    EVP_PKEY_CTX_free(pctx);
148
0
    OPENSSL_free(ek);
149
0
    return ret;
150
151
0
}
152
153
static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
154
                               PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
155
                               size_t fixlen)
156
0
{
157
0
    EVP_PKEY_CTX *pctx = NULL;
158
0
    unsigned char *ek = NULL;
159
0
    size_t eklen;
160
0
    int ret = -1;
161
0
    const PKCS7_CTX *ctx = ri->ctx;
162
163
0
    pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
164
0
                                      ossl_pkcs7_ctx_get0_propq(ctx));
165
0
    if (pctx == NULL)
166
0
        return -1;
167
168
0
    if (EVP_PKEY_decrypt_init(pctx) <= 0)
169
0
        goto err;
170
171
0
    if (EVP_PKEY_is_a(pkey, "RSA"))
172
        /* upper layer pkcs7 code incorrectly assumes that a successful RSA
173
         * decryption means that the key matches ciphertext (which never
174
         * was the case, implicit rejection or not), so to make it work
175
         * disable implicit rejection for RSA keys */
176
0
        EVP_PKEY_CTX_ctrl_str(pctx, "rsa_pkcs1_implicit_rejection", "0");
177
178
0
    ret = evp_pkey_decrypt_alloc(pctx, &ek, &eklen, fixlen,
179
0
                                 ri->enc_key->data, ri->enc_key->length);
180
0
    if (ret <= 0)
181
0
        goto err;
182
183
0
    ret = 1;
184
185
0
    OPENSSL_clear_free(*pek, *peklen);
186
0
    *pek = ek;
187
0
    *peklen = eklen;
188
189
0
 err:
190
0
    EVP_PKEY_CTX_free(pctx);
191
0
    if (!ret)
192
0
        OPENSSL_free(ek);
193
194
0
    return ret;
195
0
}
196
197
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
198
0
{
199
0
    int i;
200
0
    BIO *out = NULL, *btmp = NULL;
201
0
    X509_ALGOR *xa = NULL;
202
0
    EVP_CIPHER *fetched_cipher = NULL;
203
0
    const EVP_CIPHER *cipher;
204
0
    const EVP_CIPHER *evp_cipher = NULL;
205
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
206
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
207
0
    X509_ALGOR *xalg = NULL;
208
0
    PKCS7_RECIP_INFO *ri = NULL;
209
0
    ASN1_OCTET_STRING *os = NULL;
210
0
    const PKCS7_CTX *p7_ctx;
211
0
    OSSL_LIB_CTX *libctx;
212
0
    const char *propq;
213
214
0
    if (p7 == NULL) {
215
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
216
0
        return NULL;
217
0
    }
218
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
219
0
    libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
220
0
    propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
221
222
    /*
223
     * The content field in the PKCS7 ContentInfo is optional, but that really
224
     * only applies to inner content (precisely, detached signatures).
225
     *
226
     * When reading content, missing outer content is therefore treated as an
227
     * error.
228
     *
229
     * When creating content, PKCS7_content_new() must be called before
230
     * calling this method, so a NULL p7->d is always an error.
231
     */
232
0
    if (p7->d.ptr == NULL) {
233
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
234
0
        return NULL;
235
0
    }
236
237
0
    i = OBJ_obj2nid(p7->type);
238
0
    p7->state = PKCS7_S_HEADER;
239
240
0
    switch (i) {
241
0
    case NID_pkcs7_signed:
242
0
        md_sk = p7->d.sign->md_algs;
243
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
244
0
        break;
245
0
    case NID_pkcs7_signedAndEnveloped:
246
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
247
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
248
0
        xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
249
0
        evp_cipher = p7->d.signed_and_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_enveloped:
256
0
        rsk = p7->d.enveloped->recipientinfo;
257
0
        xalg = p7->d.enveloped->enc_data->algorithm;
258
0
        evp_cipher = p7->d.enveloped->enc_data->cipher;
259
0
        if (evp_cipher == NULL) {
260
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
261
0
            goto err;
262
0
        }
263
0
        break;
264
0
    case NID_pkcs7_digest:
265
0
        xa = p7->d.digest->md;
266
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
267
0
        break;
268
0
    case NID_pkcs7_data:
269
0
        break;
270
0
    default:
271
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
272
0
        goto err;
273
0
    }
274
275
0
    for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
276
0
        if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
277
0
            goto err;
278
279
0
    if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
280
0
        goto err;
281
282
0
    if (evp_cipher != NULL) {
283
0
        unsigned char key[EVP_MAX_KEY_LENGTH];
284
0
        unsigned char iv[EVP_MAX_IV_LENGTH];
285
0
        int keylen, ivlen;
286
0
        EVP_CIPHER_CTX *ctx;
287
288
0
        if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
289
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
290
0
            goto err;
291
0
        }
292
0
        BIO_get_cipher_ctx(btmp, &ctx);
293
0
        keylen = EVP_CIPHER_get_key_length(evp_cipher);
294
0
        ivlen = EVP_CIPHER_get_iv_length(evp_cipher);
295
0
        xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher));
296
0
        if (ivlen > 0)
297
0
            if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
298
0
                goto err;
299
300
0
        (void)ERR_set_mark();
301
0
        fetched_cipher = EVP_CIPHER_fetch(libctx,
302
0
                                          EVP_CIPHER_get0_name(evp_cipher),
303
0
                                          propq);
304
0
        (void)ERR_pop_to_mark();
305
0
        if (fetched_cipher != NULL)
306
0
            cipher = fetched_cipher;
307
0
        else
308
0
            cipher = evp_cipher;
309
310
0
        if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
311
0
            goto err;
312
313
0
        EVP_CIPHER_free(fetched_cipher);
314
0
        fetched_cipher = NULL;
315
316
0
        if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
317
0
            goto err;
318
0
        if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
319
0
            goto err;
320
321
0
        if (ivlen > 0) {
322
0
            if (xalg->parameter == NULL) {
323
0
                xalg->parameter = ASN1_TYPE_new();
324
0
                if (xalg->parameter == NULL)
325
0
                    goto err;
326
0
            }
327
0
            if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0) {
328
0
                ASN1_TYPE_free(xalg->parameter);
329
0
                xalg->parameter = NULL;
330
0
                goto err;
331
0
            }
332
0
        }
333
334
        /* Lets do the pub key stuff :-) */
335
0
        for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
336
0
            ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
337
0
            if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
338
0
                goto err;
339
0
        }
340
0
        OPENSSL_cleanse(key, keylen);
341
342
0
        if (out == NULL)
343
0
            out = btmp;
344
0
        else
345
0
            BIO_push(out, btmp);
346
0
        btmp = NULL;
347
0
    }
348
349
0
    if (bio == NULL) {
350
0
        if (PKCS7_is_detached(p7)) {
351
0
            bio = BIO_new(BIO_s_null());
352
0
        } else if (os && os->length > 0) {
353
0
            bio = BIO_new_mem_buf(os->data, os->length);
354
0
        } else {
355
0
            bio = BIO_new(BIO_s_mem());
356
0
            if (bio == NULL)
357
0
                goto err;
358
0
            BIO_set_mem_eof_return(bio, 0);
359
0
        }
360
0
        if (bio == NULL)
361
0
            goto err;
362
0
    }
363
0
    if (out)
364
0
        BIO_push(out, bio);
365
0
    else
366
0
        out = bio;
367
0
    return out;
368
369
0
 err:
370
0
    EVP_CIPHER_free(fetched_cipher);
371
0
    BIO_free_all(out);
372
0
    BIO_free_all(btmp);
373
0
    return NULL;
374
0
}
375
376
static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
377
0
{
378
0
    int ret;
379
0
    ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
380
0
                        X509_get_issuer_name(pcert));
381
0
    if (ret)
382
0
        return ret;
383
0
    return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
384
0
                            ri->issuer_and_serial->serial);
385
0
}
386
387
/* int */
388
BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
389
0
{
390
0
    int i, len;
391
0
    BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
392
0
    X509_ALGOR *xa;
393
0
    ASN1_OCTET_STRING *data_body = NULL;
394
0
    EVP_MD *evp_md = NULL;
395
0
    const EVP_MD *md;
396
0
    EVP_CIPHER *evp_cipher = NULL;
397
0
    const EVP_CIPHER *cipher = NULL;
398
0
    EVP_CIPHER_CTX *evp_ctx = NULL;
399
0
    X509_ALGOR *enc_alg = NULL;
400
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
401
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
402
0
    PKCS7_RECIP_INFO *ri = NULL;
403
0
    unsigned char *ek = NULL, *tkey = NULL;
404
0
    int eklen = 0, tkeylen = 0;
405
0
    char name[OSSL_MAX_NAME_SIZE];
406
0
    const PKCS7_CTX *p7_ctx;
407
0
    OSSL_LIB_CTX *libctx;
408
0
    const char *propq;
409
410
0
    if (p7 == NULL) {
411
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
412
0
        return NULL;
413
0
    }
414
415
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
416
0
    libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
417
0
    propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
418
419
0
    if (p7->d.ptr == NULL) {
420
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
421
0
        return NULL;
422
0
    }
423
424
0
    i = OBJ_obj2nid(p7->type);
425
0
    p7->state = PKCS7_S_HEADER;
426
427
0
    switch (i) {
428
0
    case NID_pkcs7_signed:
429
        /*
430
         * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
431
         * field and optional content.
432
         * data_body is NULL if that structure has no (=detached) content
433
         * or if the contentType is wrong (i.e., not "data").
434
         */
435
0
        data_body = PKCS7_get_octet_string(p7->d.sign->contents);
436
0
        if (!PKCS7_is_detached(p7) && data_body == NULL) {
437
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE);
438
0
            goto err;
439
0
        }
440
0
        md_sk = p7->d.sign->md_algs;
441
0
        break;
442
0
    case NID_pkcs7_signedAndEnveloped:
443
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
444
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
445
        /* data_body is NULL if the optional EncryptedContent is missing. */
446
0
        data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
447
0
        enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
448
449
0
        OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
450
451
0
        (void)ERR_set_mark();
452
0
        evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
453
0
        if (evp_cipher != NULL)
454
0
            cipher = evp_cipher;
455
0
        else
456
0
            cipher = EVP_get_cipherbyname(name);
457
458
0
        if (cipher == NULL) {
459
0
            (void)ERR_clear_last_mark();
460
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
461
0
            goto err;
462
0
        }
463
0
        (void)ERR_pop_to_mark();
464
0
        break;
465
0
    case NID_pkcs7_enveloped:
466
0
        rsk = p7->d.enveloped->recipientinfo;
467
0
        enc_alg = p7->d.enveloped->enc_data->algorithm;
468
        /* data_body is NULL if the optional EncryptedContent is missing. */
469
0
        data_body = p7->d.enveloped->enc_data->enc_data;
470
0
        OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
471
472
0
        (void)ERR_set_mark();
473
0
        evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
474
0
        if (evp_cipher != NULL)
475
0
            cipher = evp_cipher;
476
0
        else
477
0
            cipher = EVP_get_cipherbyname(name);
478
479
0
        if (cipher == NULL) {
480
0
            (void)ERR_clear_last_mark();
481
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
482
0
            goto err;
483
0
        }
484
0
        (void)ERR_pop_to_mark();
485
0
        break;
486
0
    default:
487
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
488
0
        goto err;
489
0
    }
490
491
    /* Detached content must be supplied via in_bio instead. */
492
0
    if (data_body == NULL && in_bio == NULL) {
493
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
494
0
        goto err;
495
0
    }
496
497
    /* We will be checking the signature */
498
0
    if (md_sk != NULL) {
499
0
        for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
500
0
            xa = sk_X509_ALGOR_value(md_sk, i);
501
0
            if ((btmp = BIO_new(BIO_f_md())) == NULL) {
502
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
503
0
                goto err;
504
0
            }
505
506
0
            OBJ_obj2txt(name, sizeof(name), xa->algorithm, 0);
507
508
0
            (void)ERR_set_mark();
509
0
            evp_md = EVP_MD_fetch(libctx, name, propq);
510
0
            if (evp_md != NULL)
511
0
                md = evp_md;
512
0
            else
513
0
                md = EVP_get_digestbyname(name);
514
515
0
            if (md == NULL) {
516
0
                (void)ERR_clear_last_mark();
517
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
518
0
                goto err;
519
0
            }
520
0
            (void)ERR_pop_to_mark();
521
522
0
            if (BIO_set_md(btmp, md) <= 0) {
523
0
                EVP_MD_free(evp_md);
524
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
525
0
                goto err;
526
0
            }
527
0
            EVP_MD_free(evp_md);
528
0
            if (out == NULL)
529
0
                out = btmp;
530
0
            else
531
0
                BIO_push(out, btmp);
532
0
            btmp = NULL;
533
0
        }
534
0
    }
535
536
0
    if (cipher != NULL) {
537
0
        if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
538
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
539
0
            goto err;
540
0
        }
541
542
        /*
543
         * It was encrypted, we need to decrypt the secret key with the
544
         * private key
545
         */
546
547
        /*
548
         * Find the recipientInfo which matches the passed certificate (if
549
         * any)
550
         */
551
552
0
        if (pcert) {
553
0
            for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
554
0
                ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
555
0
                if (!pkcs7_cmp_ri(ri, pcert))
556
0
                    break;
557
0
                ri = NULL;
558
0
            }
559
0
            if (ri == NULL) {
560
0
                ERR_raise(ERR_LIB_PKCS7,
561
0
                          PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
562
0
                goto err;
563
0
            }
564
0
        }
565
566
        /* If we haven't got a certificate try each ri in turn */
567
0
        if (pcert == NULL) {
568
            /*
569
             * Always attempt to decrypt all rinfo even after success as a
570
             * defence against MMA timing attacks.
571
             */
572
0
            for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
573
0
                ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
574
0
                ri->ctx = p7_ctx;
575
0
                if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
576
0
                        EVP_CIPHER_get_key_length(cipher)) < 0)
577
0
                    goto err;
578
0
                ERR_clear_error();
579
0
            }
580
0
        } else {
581
0
            ri->ctx = p7_ctx;
582
            /* Only exit on fatal errors, not decrypt failure */
583
0
            if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
584
0
                goto err;
585
0
            ERR_clear_error();
586
0
        }
587
588
0
        evp_ctx = NULL;
589
0
        BIO_get_cipher_ctx(etmp, &evp_ctx);
590
0
        if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
591
0
            goto err;
592
0
        if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0)
593
0
            goto err;
594
        /* Generate random key as MMA defence */
595
0
        len = EVP_CIPHER_CTX_get_key_length(evp_ctx);
596
0
        if (len <= 0)
597
0
            goto err;
598
0
        tkeylen = (size_t)len;
599
0
        tkey = OPENSSL_malloc(tkeylen);
600
0
        if (tkey == NULL)
601
0
            goto err;
602
0
        if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
603
0
            goto err;
604
0
        if (ek == NULL) {
605
0
            ek = tkey;
606
0
            eklen = tkeylen;
607
0
            tkey = NULL;
608
0
        }
609
610
0
        if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) {
611
            /*
612
             * Some S/MIME clients don't use the same key and effective key
613
             * length. The key length is determined by the size of the
614
             * decrypted RSA key.
615
             */
616
0
            if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) {
617
                /* Use random key as MMA defence */
618
0
                OPENSSL_clear_free(ek, eklen);
619
0
                ek = tkey;
620
0
                eklen = tkeylen;
621
0
                tkey = NULL;
622
0
            }
623
0
        }
624
        /* Clear errors so we don't leak information useful in MMA */
625
0
        ERR_clear_error();
626
0
        if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
627
0
            goto err;
628
629
0
        OPENSSL_clear_free(ek, eklen);
630
0
        ek = NULL;
631
0
        OPENSSL_clear_free(tkey, tkeylen);
632
0
        tkey = NULL;
633
634
0
        if (out == NULL)
635
0
            out = etmp;
636
0
        else
637
0
            BIO_push(out, etmp);
638
0
        etmp = NULL;
639
0
    }
640
0
    if (in_bio != NULL) {
641
0
        bio = in_bio;
642
0
    } else {
643
0
        if (data_body->length > 0)
644
0
            bio = BIO_new_mem_buf(data_body->data, data_body->length);
645
0
        else {
646
0
            bio = BIO_new(BIO_s_mem());
647
0
            if (bio == NULL)
648
0
                goto err;
649
0
            BIO_set_mem_eof_return(bio, 0);
650
0
        }
651
0
        if (bio == NULL)
652
0
            goto err;
653
0
    }
654
0
    BIO_push(out, bio);
655
0
    bio = NULL;
656
0
    EVP_CIPHER_free(evp_cipher);
657
0
    return out;
658
659
0
 err:
660
0
    EVP_CIPHER_free(evp_cipher);
661
0
    OPENSSL_clear_free(ek, eklen);
662
0
    OPENSSL_clear_free(tkey, tkeylen);
663
0
    BIO_free_all(out);
664
0
    BIO_free_all(btmp);
665
0
    BIO_free_all(etmp);
666
0
    BIO_free_all(bio);
667
0
    return NULL;
668
0
}
669
670
static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
671
0
{
672
0
    for (;;) {
673
0
        bio = BIO_find_type(bio, BIO_TYPE_MD);
674
0
        if (bio == NULL) {
675
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
676
0
            return NULL;
677
0
        }
678
0
        BIO_get_md_ctx(bio, pmd);
679
0
        if (*pmd == NULL) {
680
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
681
0
            return NULL;
682
0
        }
683
0
        if (EVP_MD_CTX_get_type(*pmd) == nid)
684
0
            return bio;
685
0
        bio = BIO_next(bio);
686
0
    }
687
0
    return NULL;
688
0
}
689
690
static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
691
0
{
692
0
    unsigned char md_data[EVP_MAX_MD_SIZE];
693
0
    unsigned int md_len;
694
695
    /* Add signing time if not already present */
696
0
    if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
697
0
        if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
698
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
699
0
            return 0;
700
0
        }
701
0
    }
702
703
    /* Add digest */
704
0
    if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
705
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
706
0
        return 0;
707
0
    }
708
0
    if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
709
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
710
0
        return 0;
711
0
    }
712
713
    /* Now sign the attributes */
714
0
    if (!PKCS7_SIGNER_INFO_sign(si))
715
0
        return 0;
716
717
0
    return 1;
718
0
}
719
720
int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
721
0
{
722
0
    int ret = 0;
723
0
    int i, j;
724
0
    BIO *btmp;
725
0
    PKCS7_SIGNER_INFO *si;
726
0
    EVP_MD_CTX *mdc, *ctx_tmp;
727
0
    STACK_OF(X509_ATTRIBUTE) *sk;
728
0
    STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
729
0
    ASN1_OCTET_STRING *os = NULL;
730
0
    const PKCS7_CTX *p7_ctx;
731
732
0
    if (p7 == NULL) {
733
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
734
0
        return 0;
735
0
    }
736
737
0
    p7_ctx = ossl_pkcs7_get0_ctx(p7);
738
739
0
    if (p7->d.ptr == NULL) {
740
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
741
0
        return 0;
742
0
    }
743
744
0
    ctx_tmp = EVP_MD_CTX_new();
745
0
    if (ctx_tmp == NULL) {
746
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
747
0
        return 0;
748
0
    }
749
750
0
    i = OBJ_obj2nid(p7->type);
751
0
    p7->state = PKCS7_S_HEADER;
752
753
0
    switch (i) {
754
0
    case NID_pkcs7_data:
755
0
        os = p7->d.data;
756
0
        break;
757
0
    case NID_pkcs7_signedAndEnveloped:
758
        /* XXXXXXXXXXXXXXXX */
759
0
        si_sk = p7->d.signed_and_enveloped->signer_info;
760
0
        os = p7->d.signed_and_enveloped->enc_data->enc_data;
761
0
        if (os == NULL) {
762
0
            os = ASN1_OCTET_STRING_new();
763
0
            if (os == NULL) {
764
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
765
0
                goto err;
766
0
            }
767
0
            p7->d.signed_and_enveloped->enc_data->enc_data = os;
768
0
        }
769
0
        break;
770
0
    case NID_pkcs7_enveloped:
771
        /* XXXXXXXXXXXXXXXX */
772
0
        os = p7->d.enveloped->enc_data->enc_data;
773
0
        if (os == NULL) {
774
0
            os = ASN1_OCTET_STRING_new();
775
0
            if (os == NULL) {
776
0
                ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
777
0
                goto err;
778
0
            }
779
0
            p7->d.enveloped->enc_data->enc_data = os;
780
0
        }
781
0
        break;
782
0
    case NID_pkcs7_signed:
783
0
        si_sk = p7->d.sign->signer_info;
784
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
785
        /* If detached data then the content is excluded */
786
0
        if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
787
0
            ASN1_OCTET_STRING_free(os);
788
0
            os = NULL;
789
0
            p7->d.sign->contents->d.data = NULL;
790
0
        }
791
0
        break;
792
793
0
    case NID_pkcs7_digest:
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) <= 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 (!abuf)
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) ||
1045
0
            ((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) ||
1087
0
            (memcmp(message_digest->data, md_dat, md_len))) {
1088
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1089
0
            ret = -1;
1090
0
            goto err;
1091
0
        }
1092
1093
0
        (void)ERR_set_mark();
1094
0
        fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1095
1096
0
        if (fetched_md != NULL)
1097
0
            md = fetched_md;
1098
0
        else
1099
0
            md = EVP_get_digestbynid(md_type);
1100
1101
0
        if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1102
0
            (void)ERR_clear_last_mark();
1103
0
            goto err;
1104
0
        }
1105
0
        (void)ERR_pop_to_mark();
1106
1107
0
        alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1108
0
                             ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1109
0
        if (alen <= 0) {
1110
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1111
0
            ret = -1;
1112
0
            goto err;
1113
0
        }
1114
0
        if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1115
0
            goto err;
1116
0
    }
1117
1118
0
    os = si->enc_digest;
1119
0
    pkey = X509_get0_pubkey(signer);
1120
0
    if (pkey == NULL) {
1121
0
        ret = -1;
1122
0
        goto err;
1123
0
    }
1124
1125
0
    i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1126
0
    if (i <= 0) {
1127
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1128
0
        ret = -1;
1129
0
        goto err;
1130
0
    }
1131
0
    ret = 1;
1132
0
 err:
1133
0
    OPENSSL_free(abuf);
1134
0
    EVP_MD_CTX_free(mdc_tmp);
1135
0
    EVP_MD_free(fetched_md);
1136
0
    return ret;
1137
0
}
1138
1139
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1140
0
{
1141
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk;
1142
0
    PKCS7_RECIP_INFO *ri;
1143
0
    int i;
1144
1145
0
    i = OBJ_obj2nid(p7->type);
1146
0
    if (i != NID_pkcs7_signedAndEnveloped)
1147
0
        return NULL;
1148
0
    if (p7->d.signed_and_enveloped == NULL)
1149
0
        return NULL;
1150
0
    rsk = p7->d.signed_and_enveloped->recipientinfo;
1151
0
    if (rsk == NULL)
1152
0
        return NULL;
1153
0
    if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1154
0
        return NULL;
1155
0
    ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1156
0
    return ri->issuer_and_serial;
1157
0
}
1158
1159
ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1160
248
{
1161
248
    return get_attribute(si->auth_attr, nid);
1162
248
}
1163
1164
ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1165
0
{
1166
0
    return get_attribute(si->unauth_attr, nid);
1167
0
}
1168
1169
static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1170
248
{
1171
248
    int idx = X509at_get_attr_by_NID(sk, nid, -1);
1172
1173
248
    if (idx < 0)
1174
206
        return NULL;
1175
42
    return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1176
248
}
1177
1178
ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1179
0
{
1180
0
    ASN1_TYPE *astype;
1181
0
    if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1182
0
        return NULL;
1183
0
    return astype->value.octet_string;
1184
0
}
1185
1186
int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1187
                                STACK_OF(X509_ATTRIBUTE) *sk)
1188
0
{
1189
0
    int i;
1190
1191
0
    sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1192
0
    p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1193
0
    if (p7si->auth_attr == NULL)
1194
0
        return 0;
1195
0
    for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1196
0
        if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1197
0
                                   X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1198
0
                                                      (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
1217
0
                                                      (sk, i))))
1218
0
            == NULL)
1219
0
            return 0;
1220
0
    }
1221
0
    return 1;
1222
0
}
1223
1224
int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1225
                               void *value)
1226
0
{
1227
0
    return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1228
0
}
1229
1230
int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1231
                        void *value)
1232
0
{
1233
0
    return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1234
0
}
1235
1236
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1237
                         void *value)
1238
0
{
1239
0
    X509_ATTRIBUTE *attr = NULL;
1240
0
    int i, n;
1241
1242
0
    if (*sk == NULL) {
1243
0
        if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1244
0
            return 0;
1245
0
    }
1246
0
    n = sk_X509_ATTRIBUTE_num(*sk);
1247
0
    for (i = 0; i < n; i++) {
1248
0
        attr = sk_X509_ATTRIBUTE_value(*sk, i);
1249
0
        if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid)
1250
0
            goto end;
1251
0
    }
1252
0
    if (!sk_X509_ATTRIBUTE_push(*sk, NULL))
1253
0
        return 0;
1254
1255
0
 end:
1256
0
    attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1257
0
    if (attr == NULL) {
1258
0
        if (i == n)
1259
0
            sk_X509_ATTRIBUTE_pop(*sk);
1260
0
        return 0;
1261
0
    }
1262
0
    X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i));
1263
0
    (void) sk_X509_ATTRIBUTE_set(*sk, i, attr);
1264
0
    return 1;
1265
0
}