Coverage Report

Created: 2025-12-31 06:58

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