Coverage Report

Created: 2025-08-25 06:30

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