Coverage Report

Created: 2025-12-10 06:24

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