Coverage Report

Created: 2026-04-08 06:20

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-2026 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
        if (p7->d.sign->contents == NULL) {
806
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
807
0
            goto err;
808
0
        }
809
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
810
        /* If detached data then the content is excluded */
811
0
        if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
812
0
            ASN1_OCTET_STRING_free(os);
813
0
            os = NULL;
814
0
            p7->d.sign->contents->d.data = NULL;
815
0
        }
816
0
        break;
817
818
0
    case NID_pkcs7_digest:
819
0
        if (p7->d.digest->contents == NULL) {
820
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
821
0
            goto err;
822
0
        }
823
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
824
        /* If detached data then the content is excluded */
825
0
        if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
826
0
            ASN1_OCTET_STRING_free(os);
827
0
            os = NULL;
828
0
            p7->d.digest->contents->d.data = NULL;
829
0
        }
830
0
        break;
831
832
0
    default:
833
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
834
0
        goto err;
835
0
    }
836
837
0
    if (si_sk != NULL) {
838
0
        for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
839
0
            si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
840
0
            if (si->pkey == NULL)
841
0
                continue;
842
843
0
            j = OBJ_obj2nid(si->digest_alg->algorithm);
844
845
0
            btmp = bio;
846
847
0
            btmp = PKCS7_find_digest(&mdc, btmp, j);
848
849
0
            if (btmp == NULL)
850
0
                goto err;
851
852
            /*
853
             * We now have the EVP_MD_CTX, lets do the signing.
854
             */
855
0
            if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
856
0
                goto err;
857
858
0
            sk = si->auth_attr;
859
860
            /*
861
             * If there are attributes, we add the digest attribute and only
862
             * sign the attributes
863
             */
864
0
            if (sk_X509_ATTRIBUTE_num(sk) > 0) {
865
0
                if (!do_pkcs7_signed_attrib(si, ctx_tmp))
866
0
                    goto err;
867
0
            } else {
868
0
                unsigned char *abuf = NULL;
869
0
                unsigned int abuflen = EVP_PKEY_get_size(si->pkey);
870
871
0
                if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL)
872
0
                    goto err;
873
874
0
                if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
875
0
                        ossl_pkcs7_ctx_get0_libctx(p7_ctx),
876
0
                        ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
877
0
                    OPENSSL_free(abuf);
878
0
                    ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
879
0
                    goto err;
880
0
                }
881
0
                ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
882
0
            }
883
0
        }
884
0
    } else if (i == NID_pkcs7_digest) {
885
0
        unsigned char md_data[EVP_MAX_MD_SIZE];
886
0
        unsigned int md_len;
887
0
        if (!PKCS7_find_digest(&mdc, bio,
888
0
                OBJ_obj2nid(p7->d.digest->md->algorithm)))
889
0
            goto err;
890
0
        if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
891
0
            goto err;
892
0
        if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
893
0
            goto err;
894
0
    }
895
896
0
    if (!PKCS7_is_detached(p7)) {
897
        /*
898
         * NOTE(emilia): I think we only reach os == NULL here because detached
899
         * digested data support is broken.
900
         */
901
0
        if (os == NULL)
902
0
            goto err;
903
0
        if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
904
0
            char *cont;
905
0
            long contlen;
906
0
            btmp = BIO_find_type(bio, BIO_TYPE_MEM);
907
0
            if (btmp == NULL) {
908
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
909
0
                goto err;
910
0
            }
911
0
            contlen = BIO_get_mem_data(btmp, &cont);
912
            /*
913
             * Mark the BIO read only then we can use its copy of the data
914
             * instead of making an extra copy.
915
             */
916
0
            BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
917
0
            BIO_set_mem_eof_return(btmp, 0);
918
0
            ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
919
0
        }
920
0
    }
921
0
    ret = 1;
922
0
err:
923
0
    EVP_MD_CTX_free(ctx_tmp);
924
0
    return ret;
925
0
}
926
927
int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
928
0
{
929
0
    EVP_MD_CTX *mctx;
930
0
    EVP_PKEY_CTX *pctx = NULL;
931
0
    unsigned char *abuf = NULL;
932
0
    int alen;
933
0
    size_t siglen;
934
0
    const EVP_MD *md = NULL;
935
0
    const PKCS7_CTX *ctx = si->ctx;
936
937
0
    md = EVP_get_digestbyobj(si->digest_alg->algorithm);
938
0
    if (md == NULL)
939
0
        return 0;
940
941
0
    mctx = EVP_MD_CTX_new();
942
0
    if (mctx == NULL) {
943
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
944
0
        goto err;
945
0
    }
946
947
0
    if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
948
0
            ossl_pkcs7_ctx_get0_libctx(ctx),
949
0
            ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
950
0
            NULL)
951
0
        <= 0)
952
0
        goto err;
953
954
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
955
0
        ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
956
0
    if (alen < 0 || abuf == NULL)
957
0
        goto err;
958
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
959
0
        goto err;
960
0
    OPENSSL_free(abuf);
961
0
    abuf = NULL;
962
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
963
0
        goto err;
964
0
    abuf = OPENSSL_malloc(siglen);
965
0
    if (abuf == NULL)
966
0
        goto err;
967
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
968
0
        goto err;
969
970
0
    EVP_MD_CTX_free(mctx);
971
972
0
    ASN1_STRING_set0(si->enc_digest, abuf, (int)siglen);
973
974
0
    return 1;
975
976
0
err:
977
0
    OPENSSL_free(abuf);
978
0
    EVP_MD_CTX_free(mctx);
979
0
    return 0;
980
0
}
981
982
/* This partly overlaps with PKCS7_verify(). It does not support flags. */
983
int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
984
    PKCS7 *p7, PKCS7_SIGNER_INFO *si)
985
0
{
986
0
    PKCS7_ISSUER_AND_SERIAL *ias;
987
0
    int ret = 0, i;
988
0
    STACK_OF(X509) *untrusted;
989
0
    STACK_OF(X509_CRL) *crls;
990
0
    X509 *signer;
991
992
0
    if (p7 == NULL) {
993
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
994
0
        return 0;
995
0
    }
996
997
0
    if (p7->d.ptr == NULL) {
998
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
999
0
        return 0;
1000
0
    }
1001
1002
0
    if (PKCS7_type_is_signed(p7)) {
1003
0
        untrusted = p7->d.sign->cert;
1004
0
        crls = p7->d.sign->crl;
1005
0
    } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
1006
0
        untrusted = p7->d.signed_and_enveloped->cert;
1007
0
        crls = p7->d.signed_and_enveloped->crl;
1008
0
    } else {
1009
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1010
0
        goto err;
1011
0
    }
1012
0
    X509_STORE_CTX_set0_crls(ctx, crls);
1013
1014
    /* XXXXXXXXXXXXXXXXXXXXXXX */
1015
0
    ias = si->issuer_and_serial;
1016
1017
0
    signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
1018
1019
    /* Were we able to find the signer certificate in passed to us? */
1020
0
    if (signer == NULL) {
1021
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
1022
0
        goto err;
1023
0
    }
1024
1025
    /* Lets verify */
1026
0
    if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
1027
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1028
0
        goto err;
1029
0
    }
1030
0
    X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1031
0
    i = X509_verify_cert(ctx);
1032
0
    if (i <= 0) {
1033
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1034
0
        goto err;
1035
0
    }
1036
1037
0
    if (PKCS7_signatureVerify(bio, p7, si, signer) <= 0)
1038
0
        goto err;
1039
0
    ret = 1;
1040
0
err:
1041
0
    return ret;
1042
0
}
1043
1044
int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1045
    const X509 *signer)
1046
0
{
1047
0
    ASN1_OCTET_STRING *os;
1048
0
    EVP_MD_CTX *mdc_tmp, *mdc;
1049
0
    EVP_MD *md = NULL;
1050
0
    int ret = 0, i;
1051
0
    int md_type;
1052
0
    STACK_OF(X509_ATTRIBUTE) *sk;
1053
0
    BIO *btmp;
1054
0
    EVP_PKEY *pkey;
1055
0
    unsigned char *abuf = NULL;
1056
0
    const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1057
0
    OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1058
0
    const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1059
1060
0
    mdc_tmp = EVP_MD_CTX_new();
1061
0
    if (mdc_tmp == NULL) {
1062
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
1063
0
        goto err;
1064
0
    }
1065
1066
0
    if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1067
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1068
0
        goto err;
1069
0
    }
1070
1071
0
    md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1072
1073
0
    btmp = bio;
1074
0
    for (;;) {
1075
0
        if ((btmp == NULL) || ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1076
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1077
0
            goto err;
1078
0
        }
1079
0
        BIO_get_md_ctx(btmp, &mdc);
1080
0
        if (mdc == NULL) {
1081
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1082
0
            goto err;
1083
0
        }
1084
0
        if (EVP_MD_CTX_get_type(mdc) == md_type)
1085
0
            break;
1086
        /*
1087
         * Workaround for some broken clients that put the signature OID
1088
         * instead of the digest OID in digest_alg->algorithm
1089
         */
1090
0
        if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1091
0
            break;
1092
0
        btmp = BIO_next(btmp);
1093
0
    }
1094
1095
    /*
1096
     * mdc is the digest ctx that we want, unless there are attributes, in
1097
     * which case the digest is the signed attributes
1098
     */
1099
0
    if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1100
0
        goto err;
1101
1102
0
    sk = si->auth_attr;
1103
0
    if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1104
0
        unsigned char md_dat[EVP_MAX_MD_SIZE];
1105
0
        unsigned int md_len;
1106
0
        int alen;
1107
0
        const ASN1_OCTET_STRING *message_digest;
1108
1109
0
        if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1110
0
            goto err;
1111
0
        message_digest = PKCS7_digest_from_attributes(sk);
1112
0
        if (!message_digest) {
1113
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1114
0
            goto err;
1115
0
        }
1116
0
        if ((message_digest->length != (int)md_len) || (memcmp(message_digest->data, md_dat, md_len))) {
1117
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1118
0
            ret = -1;
1119
0
            goto err;
1120
0
        }
1121
1122
0
        md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1123
1124
0
        if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1125
0
            goto err;
1126
0
        }
1127
1128
0
        alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1129
0
            ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1130
0
        if (alen <= 0 || abuf == NULL) {
1131
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1132
0
            ret = -1;
1133
0
            goto err;
1134
0
        }
1135
0
        if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1136
0
            goto err;
1137
0
    }
1138
1139
0
    os = si->enc_digest;
1140
0
    pkey = X509_get0_pubkey(signer);
1141
0
    if (pkey == NULL) {
1142
0
        ret = -1;
1143
0
        goto err;
1144
0
    }
1145
1146
0
    i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1147
0
    if (i <= 0) {
1148
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1149
0
        ret = -1;
1150
0
        goto err;
1151
0
    }
1152
0
    ret = 1;
1153
0
err:
1154
0
    OPENSSL_free(abuf);
1155
0
    EVP_MD_CTX_free(mdc_tmp);
1156
0
    EVP_MD_free(md);
1157
0
    return ret;
1158
0
}
1159
1160
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1161
0
{
1162
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk;
1163
0
    PKCS7_RECIP_INFO *ri;
1164
0
    int i;
1165
1166
0
    i = OBJ_obj2nid(p7->type);
1167
0
    if (i != NID_pkcs7_signedAndEnveloped)
1168
0
        return NULL;
1169
0
    if (p7->d.signed_and_enveloped == NULL)
1170
0
        return NULL;
1171
0
    rsk = p7->d.signed_and_enveloped->recipientinfo;
1172
0
    if (rsk == NULL)
1173
0
        return NULL;
1174
0
    if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1175
0
        return NULL;
1176
0
    ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1177
0
    return ri->issuer_and_serial;
1178
0
}
1179
1180
const ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1181
0
{
1182
0
    return get_attribute(si->auth_attr, nid);
1183
0
}
1184
1185
const ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1186
0
{
1187
0
    return get_attribute(si->unauth_attr, nid);
1188
0
}
1189
1190
static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1191
0
{
1192
0
    int idx = X509at_get_attr_by_NID(sk, nid, -1);
1193
1194
0
    if (idx < 0)
1195
0
        return NULL;
1196
0
    return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1197
0
}
1198
1199
const ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1200
0
{
1201
0
    const ASN1_TYPE *astype;
1202
0
    if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1203
0
        return NULL;
1204
0
    if (astype->type != V_ASN1_OCTET_STRING)
1205
0
        return NULL;
1206
0
    return astype->value.octet_string;
1207
0
}
1208
1209
int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1210
    STACK_OF(X509_ATTRIBUTE) *sk)
1211
0
{
1212
0
    sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1213
0
    p7si->auth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free);
1214
0
    if (p7si->auth_attr == NULL)
1215
0
        return 0;
1216
0
    return 1;
1217
0
}
1218
1219
int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1220
    STACK_OF(X509_ATTRIBUTE) *sk)
1221
0
{
1222
0
    sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1223
0
    p7si->unauth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free);
1224
0
    if (p7si->unauth_attr == NULL)
1225
0
        return 0;
1226
0
    return 1;
1227
0
}
1228
1229
int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1230
    void *value)
1231
0
{
1232
0
    return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1233
0
}
1234
1235
int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1236
    void *value)
1237
0
{
1238
0
    return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1239
0
}
1240
1241
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1242
    void *value)
1243
0
{
1244
0
    X509_ATTRIBUTE *attr = NULL;
1245
0
    int i, n;
1246
1247
0
    if (*sk == NULL) {
1248
0
        if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1249
0
            return 0;
1250
0
    }
1251
0
    n = sk_X509_ATTRIBUTE_num(*sk);
1252
0
    for (i = 0; i < n; i++) {
1253
0
        attr = sk_X509_ATTRIBUTE_value(*sk, i);
1254
0
        if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid)
1255
0
            goto end;
1256
0
    }
1257
0
    if (!sk_X509_ATTRIBUTE_push(*sk, NULL))
1258
0
        return 0;
1259
1260
0
end:
1261
0
    attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1262
0
    if (attr == NULL) {
1263
0
        if (i == n)
1264
0
            sk_X509_ATTRIBUTE_pop(*sk);
1265
0
        return 0;
1266
0
    }
1267
0
    X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i));
1268
0
    (void)sk_X509_ATTRIBUTE_set(*sk, i, attr);
1269
0
    return 1;
1270
0
}