Coverage Report

Created: 2025-06-13 06:58

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