Coverage Report

Created: 2026-03-09 06:55

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