Coverage Report

Created: 2026-10-28 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/pkcs7/pk7_lib.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 "internal/cryptlib.h"
12
#include <openssl/objects.h>
13
#include <openssl/x509.h>
14
#include <openssl/pkcs7.h>
15
#include "crypto/asn1.h"
16
#include "crypto/evp.h"
17
#include "crypto/x509.h" /* for sk_X509_add1_cert() */
18
#include "pk7_local.h"
19
20
long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
21
0
{
22
0
    int nid;
23
0
    long ret;
24
25
0
    nid = OBJ_obj2nid(p7->type);
26
27
0
    switch (cmd) {
28
    /* NOTE(emilia): does not support detached digested data. */
29
0
    case PKCS7_OP_SET_DETACHED_SIGNATURE:
30
0
        if (nid == NID_pkcs7_signed) {
31
0
            if (p7->d.sign == NULL) {
32
0
                ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
33
0
                ret = 0;
34
0
                break;
35
0
            }
36
0
            ret = p7->detached = (int)larg;
37
0
            if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
38
0
                ASN1_OCTET_STRING *os;
39
0
                os = p7->d.sign->contents->d.data;
40
0
                ASN1_OCTET_STRING_free(os);
41
0
                p7->d.sign->contents->d.data = NULL;
42
0
            }
43
0
        } else {
44
0
            ERR_raise(ERR_LIB_PKCS7,
45
0
                PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
46
0
            ret = 0;
47
0
        }
48
0
        break;
49
0
    case PKCS7_OP_GET_DETACHED_SIGNATURE:
50
0
        if (nid == NID_pkcs7_signed) {
51
0
            if (p7->d.sign == NULL || p7->d.sign->contents == NULL
52
0
                || p7->d.sign->contents->d.ptr == NULL)
53
0
                ret = 1;
54
0
            else
55
0
                ret = 0;
56
57
0
            p7->detached = ret;
58
0
        } else {
59
0
            ERR_raise(ERR_LIB_PKCS7,
60
0
                PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
61
0
            ret = 0;
62
0
        }
63
64
0
        break;
65
0
    default:
66
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_OPERATION);
67
0
        ret = 0;
68
0
    }
69
0
    return ret;
70
0
}
71
72
int PKCS7_content_new(PKCS7 *p7, int type)
73
0
{
74
0
    PKCS7 *ret = NULL;
75
76
0
    if ((ret = PKCS7_new()) == NULL)
77
0
        goto err;
78
0
    if (!PKCS7_set_type(ret, type))
79
0
        goto err;
80
0
    if (!PKCS7_set_content(p7, ret))
81
0
        goto err;
82
83
0
    return 1;
84
0
err:
85
0
    PKCS7_free(ret);
86
0
    return 0;
87
0
}
88
89
int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
90
0
{
91
0
    int i;
92
93
0
    i = OBJ_obj2nid(p7->type);
94
0
    switch (i) {
95
0
    case NID_pkcs7_signed:
96
0
        PKCS7_free(p7->d.sign->contents);
97
0
        p7->d.sign->contents = p7_data;
98
0
        break;
99
0
    case NID_pkcs7_digest:
100
0
        PKCS7_free(p7->d.digest->contents);
101
0
        p7->d.digest->contents = p7_data;
102
0
        break;
103
0
    case NID_pkcs7_data:
104
0
    case NID_pkcs7_enveloped:
105
0
    case NID_pkcs7_signedAndEnveloped:
106
0
    case NID_pkcs7_encrypted:
107
0
    default:
108
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
109
0
        goto err;
110
0
    }
111
0
    return 1;
112
0
err:
113
0
    return 0;
114
0
}
115
116
int PKCS7_set_type(PKCS7 *p7, int type)
117
0
{
118
0
    ASN1_OBJECT *obj;
119
120
    /*
121
     * PKCS7_content_free(p7);
122
     */
123
0
    obj = OBJ_nid2obj(type); /* will not fail */
124
125
0
    switch (type) {
126
0
    case NID_pkcs7_signed:
127
0
        p7->type = obj;
128
0
        if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
129
0
            goto err;
130
0
        if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
131
0
            PKCS7_SIGNED_free(p7->d.sign);
132
0
            p7->d.sign = NULL;
133
0
            goto err;
134
0
        }
135
0
        break;
136
0
    case NID_pkcs7_data:
137
0
        p7->type = obj;
138
0
        if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL)
139
0
            goto err;
140
0
        break;
141
0
    case NID_pkcs7_signedAndEnveloped:
142
0
        p7->type = obj;
143
0
        if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
144
0
            == NULL)
145
0
            goto err;
146
0
        if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
147
0
            goto err;
148
0
        p7->d.signed_and_enveloped->enc_data->content_type
149
0
            = OBJ_nid2obj(NID_pkcs7_data);
150
0
        break;
151
0
    case NID_pkcs7_enveloped:
152
0
        p7->type = obj;
153
0
        if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
154
0
            == NULL)
155
0
            goto err;
156
0
        if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
157
0
            goto err;
158
0
        p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
159
0
        break;
160
0
    case NID_pkcs7_encrypted:
161
0
        p7->type = obj;
162
0
        if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
163
0
            == NULL)
164
0
            goto err;
165
0
        if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
166
0
            goto err;
167
0
        p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
168
0
        break;
169
170
0
    case NID_pkcs7_digest:
171
0
        p7->type = obj;
172
0
        if ((p7->d.digest = PKCS7_DIGEST_new())
173
0
            == NULL)
174
0
            goto err;
175
0
        if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
176
0
            goto err;
177
0
        break;
178
0
    default:
179
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
180
0
        goto err;
181
0
    }
182
0
    return 1;
183
0
err:
184
0
    return 0;
185
0
}
186
187
int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
188
0
{
189
0
    p7->type = OBJ_nid2obj(type);
190
0
    p7->d.other = other;
191
0
    return 1;
192
0
}
193
194
int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
195
0
{
196
0
    int i, j;
197
0
    ASN1_OBJECT *obj;
198
0
    X509_ALGOR *alg;
199
0
    STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
200
0
    STACK_OF(X509_ALGOR) *md_sk;
201
202
0
    i = OBJ_obj2nid(p7->type);
203
0
    switch (i) {
204
0
    case NID_pkcs7_signed:
205
0
        signer_sk = p7->d.sign->signer_info;
206
0
        md_sk = p7->d.sign->md_algs;
207
0
        break;
208
0
    case NID_pkcs7_signedAndEnveloped:
209
0
        signer_sk = p7->d.signed_and_enveloped->signer_info;
210
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
211
0
        break;
212
0
    default:
213
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
214
0
        return 0;
215
0
    }
216
217
0
    obj = psi->digest_alg->algorithm;
218
    /* If the digest is not currently listed, add it */
219
0
    j = 0;
220
0
    for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
221
0
        alg = sk_X509_ALGOR_value(md_sk, i);
222
0
        if (OBJ_cmp(obj, alg->algorithm) == 0) {
223
0
            j = 1;
224
0
            break;
225
0
        }
226
0
    }
227
0
    if (!j) { /* we need to add another algorithm */
228
0
        int nid;
229
230
0
        if ((alg = X509_ALGOR_new()) == NULL
231
0
            || (alg->parameter = ASN1_TYPE_new()) == NULL) {
232
0
            X509_ALGOR_free(alg);
233
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
234
0
            return 0;
235
0
        }
236
        /*
237
         * If there is a constant copy of the ASN1 OBJECT in libcrypto, then
238
         * use that.  Otherwise, use a dynamically duplicated copy
239
         */
240
0
        if ((nid = OBJ_obj2nid(obj)) != NID_undef)
241
0
            alg->algorithm = OBJ_nid2obj(nid);
242
0
        else
243
0
            alg->algorithm = OBJ_dup(obj);
244
0
        alg->parameter->type = V_ASN1_NULL;
245
0
        if (alg->algorithm == NULL || !sk_X509_ALGOR_push(md_sk, alg)) {
246
0
            X509_ALGOR_free(alg);
247
0
            return 0;
248
0
        }
249
0
    }
250
251
0
    psi->ctx = ossl_pkcs7_get0_ctx(p7);
252
0
    if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
253
0
        return 0;
254
0
    return 1;
255
0
}
256
257
int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
258
0
{
259
0
    int i;
260
0
    STACK_OF(X509) **sk;
261
262
0
    i = OBJ_obj2nid(p7->type);
263
0
    switch (i) {
264
0
    case NID_pkcs7_signed:
265
0
        sk = &(p7->d.sign->cert);
266
0
        break;
267
0
    case NID_pkcs7_signedAndEnveloped:
268
0
        sk = &(p7->d.signed_and_enveloped->cert);
269
0
        break;
270
0
    default:
271
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
272
0
        return 0;
273
0
    }
274
275
0
    return ossl_x509_add_cert_new(sk, x509, X509_ADD_FLAG_UP_REF);
276
0
}
277
278
int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
279
0
{
280
0
    int i;
281
0
    STACK_OF(X509_CRL) **sk;
282
283
0
    i = OBJ_obj2nid(p7->type);
284
0
    switch (i) {
285
0
    case NID_pkcs7_signed:
286
0
        sk = &(p7->d.sign->crl);
287
0
        break;
288
0
    case NID_pkcs7_signedAndEnveloped:
289
0
        sk = &(p7->d.signed_and_enveloped->crl);
290
0
        break;
291
0
    default:
292
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
293
0
        return 0;
294
0
    }
295
296
0
    if (*sk == NULL)
297
0
        *sk = sk_X509_CRL_new_null();
298
0
    if (*sk == NULL) {
299
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
300
0
        return 0;
301
0
    }
302
303
0
    if (!X509_CRL_up_ref(crl))
304
0
        return 0;
305
0
    if (!sk_X509_CRL_push(*sk, crl)) {
306
0
        X509_CRL_free(crl);
307
0
        return 0;
308
0
    }
309
0
    return 1;
310
0
}
311
312
static int pkcs7_ecdsa_or_dsa_sign_verify_setup(PKCS7_SIGNER_INFO *si,
313
    int verify)
314
0
{
315
0
    if (!verify) {
316
0
        int snid, hnid;
317
0
        X509_ALGOR *alg1, *alg2;
318
0
        EVP_PKEY *pkey = si->pkey;
319
320
0
        PKCS7_SIGNER_INFO_get0_algs(si, NULL, &alg1, &alg2);
321
0
        if (alg1 == NULL || alg1->algorithm == NULL)
322
0
            return -1;
323
0
        hnid = OBJ_obj2nid(alg1->algorithm);
324
0
        if (hnid == NID_undef)
325
0
            return -1;
326
0
        if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_get_id(pkey)))
327
0
            return -1;
328
0
        return X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL);
329
0
    }
330
0
    return 1;
331
0
}
332
333
static int pkcs7_rsa_sign_verify_setup(PKCS7_SIGNER_INFO *si, int verify)
334
0
{
335
0
    if (!verify) {
336
0
        X509_ALGOR *alg = NULL;
337
338
0
        PKCS7_SIGNER_INFO_get0_algs(si, NULL, NULL, &alg);
339
0
        if (alg != NULL)
340
0
            return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
341
0
                V_ASN1_NULL, NULL);
342
0
    }
343
0
    return 1;
344
0
}
345
346
int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
347
    const EVP_MD *dgst)
348
0
{
349
0
    int ret;
350
351
    /* We now need to add another PKCS7_SIGNER_INFO entry */
352
0
    if (!ASN1_INTEGER_set(p7i->version, 1))
353
0
        return 0;
354
0
    if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
355
0
            X509_get_issuer_name(x509)))
356
0
        return 0;
357
358
    /*
359
     * because ASN1_INTEGER_set is used to set a 'long' we will do things the
360
     * ugly way.
361
     */
362
0
    ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
363
0
    if (!(p7i->issuer_and_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(x509))))
364
0
        return 0;
365
366
    /* lets keep the pkey around for a while */
367
0
    if (!EVP_PKEY_up_ref(pkey))
368
0
        return 0;
369
370
0
    p7i->pkey = pkey;
371
372
    /* Set the algorithms */
373
374
0
    if (!X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_get_type(dgst)),
375
0
            V_ASN1_NULL, NULL))
376
0
        return 0;
377
378
0
    if (EVP_PKEY_is_a(pkey, "EC") || EVP_PKEY_is_a(pkey, "DSA"))
379
0
        return pkcs7_ecdsa_or_dsa_sign_verify_setup(p7i, 0);
380
0
    if (EVP_PKEY_is_a(pkey, "RSA"))
381
0
        return pkcs7_rsa_sign_verify_setup(p7i, 0);
382
383
0
    if (pkey->ameth != NULL && pkey->ameth->pkey_ctrl != NULL) {
384
0
        ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
385
0
        if (ret > 0)
386
0
            return 1;
387
0
        if (ret != -2) {
388
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNING_CTRL_FAILURE);
389
0
            return 0;
390
0
        }
391
0
    }
392
0
    ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
393
0
    return 0;
394
0
}
395
396
PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
397
    const EVP_MD *dgst)
398
0
{
399
0
    PKCS7_SIGNER_INFO *si = NULL;
400
401
0
    if (dgst == NULL) {
402
0
        int def_nid;
403
0
        if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
404
0
            goto err;
405
0
        dgst = EVP_get_digestbynid(def_nid);
406
0
        if (dgst == NULL) {
407
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_DEFAULT_DIGEST);
408
0
            goto err;
409
0
        }
410
0
    }
411
412
0
    if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
413
0
        goto err;
414
0
    if (PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst) <= 0)
415
0
        goto err;
416
0
    if (!PKCS7_add_signer(p7, si))
417
0
        goto err;
418
0
    return si;
419
0
err:
420
0
    PKCS7_SIGNER_INFO_free(si);
421
0
    return NULL;
422
0
}
423
424
STACK_OF(X509) *pkcs7_get0_certificates(const PKCS7 *p7)
425
0
{
426
0
    if (p7->d.ptr == NULL)
427
0
        return NULL;
428
0
    if (PKCS7_type_is_signed(p7))
429
0
        return p7->d.sign->cert;
430
0
    if (PKCS7_type_is_signedAndEnveloped(p7))
431
0
        return p7->d.signed_and_enveloped->cert;
432
0
    return NULL;
433
0
}
434
435
/* Point the signer infos of a PKCS7 at its context */
436
void ossl_pkcs7_SignerInfos_set_ctx(PKCS7 *p7)
437
0
{
438
0
    int i;
439
0
    const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
440
0
    STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
441
442
0
    if (ctx == NULL || p7->d.ptr == NULL)
443
0
        return;
444
445
0
    sinfos = PKCS7_get_signer_info(p7);
446
447
0
    for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
448
0
        PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
449
450
0
        if (si != NULL)
451
0
            si->ctx = ctx;
452
0
    }
453
0
}
454
455
const PKCS7_CTX *ossl_pkcs7_get0_ctx(const PKCS7 *p7)
456
0
{
457
0
    return p7 != NULL ? &p7->ctx : NULL;
458
0
}
459
460
void ossl_pkcs7_set0_libctx(PKCS7 *p7, OSSL_LIB_CTX *ctx)
461
0
{
462
0
    p7->ctx.libctx = ctx;
463
0
}
464
465
int ossl_pkcs7_set1_propq(PKCS7 *p7, const char *propq)
466
0
{
467
0
    if (p7->ctx.propq != NULL) {
468
0
        OPENSSL_free(p7->ctx.propq);
469
0
        p7->ctx.propq = NULL;
470
0
    }
471
0
    if (propq != NULL) {
472
0
        p7->ctx.propq = OPENSSL_strdup(propq);
473
0
        if (p7->ctx.propq == NULL)
474
0
            return 0;
475
0
    }
476
0
    return 1;
477
0
}
478
479
int ossl_pkcs7_ctx_propagate(const PKCS7 *from, PKCS7 *to)
480
0
{
481
0
    ossl_pkcs7_set0_libctx(to, from->ctx.libctx);
482
0
    if (!ossl_pkcs7_set1_propq(to, from->ctx.propq))
483
0
        return 0;
484
485
0
    ossl_pkcs7_SignerInfos_set_ctx(to);
486
0
    return 1;
487
0
}
488
489
OSSL_LIB_CTX *ossl_pkcs7_ctx_get0_libctx(const PKCS7_CTX *ctx)
490
0
{
491
0
    return ctx != NULL ? ctx->libctx : NULL;
492
0
}
493
const char *ossl_pkcs7_ctx_get0_propq(const PKCS7_CTX *ctx)
494
0
{
495
0
    return ctx != NULL ? ctx->propq : NULL;
496
0
}
497
498
int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
499
0
{
500
0
    if (PKCS7_type_is_digest(p7)) {
501
0
        if ((p7->d.digest->md->parameter = ASN1_TYPE_new()) == NULL) {
502
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
503
0
            return 0;
504
0
        }
505
0
        p7->d.digest->md->parameter->type = V_ASN1_NULL;
506
0
        p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
507
0
        return 1;
508
0
    }
509
510
0
    ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
511
0
    return 0;
512
0
}
513
514
STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
515
0
{
516
0
    if (p7 == NULL || p7->d.ptr == NULL)
517
0
        return NULL;
518
0
    if (PKCS7_type_is_signed(p7)) {
519
0
        return p7->d.sign->signer_info;
520
0
    } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
521
0
        return p7->d.signed_and_enveloped->signer_info;
522
0
    } else
523
0
        return NULL;
524
0
}
525
526
void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
527
    X509_ALGOR **pdig, X509_ALGOR **psig)
528
0
{
529
0
    if (pk)
530
0
        *pk = si->pkey;
531
0
    if (pdig)
532
0
        *pdig = si->digest_alg;
533
0
    if (psig)
534
0
        *psig = si->digest_enc_alg;
535
0
}
536
537
void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
538
0
{
539
0
    if (penc)
540
0
        *penc = ri->key_enc_algor;
541
0
}
542
543
PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
544
0
{
545
0
    PKCS7_RECIP_INFO *ri;
546
547
0
    if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
548
0
        goto err;
549
0
    if (PKCS7_RECIP_INFO_set(ri, x509) <= 0)
550
0
        goto err;
551
0
    if (!PKCS7_add_recipient_info(p7, ri))
552
0
        goto err;
553
0
    ri->ctx = ossl_pkcs7_get0_ctx(p7);
554
0
    return ri;
555
0
err:
556
0
    PKCS7_RECIP_INFO_free(ri);
557
0
    return NULL;
558
0
}
559
560
int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
561
0
{
562
0
    int i;
563
0
    STACK_OF(PKCS7_RECIP_INFO) *sk;
564
565
0
    i = OBJ_obj2nid(p7->type);
566
0
    switch (i) {
567
0
    case NID_pkcs7_signedAndEnveloped:
568
0
        sk = p7->d.signed_and_enveloped->recipientinfo;
569
0
        break;
570
0
    case NID_pkcs7_enveloped:
571
0
        sk = p7->d.enveloped->recipientinfo;
572
0
        break;
573
0
    default:
574
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
575
0
        return 0;
576
0
    }
577
578
0
    if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
579
0
        return 0;
580
0
    return 1;
581
0
}
582
583
static int pkcs7_rsa_encrypt_decrypt_setup(PKCS7_RECIP_INFO *ri, int decrypt)
584
0
{
585
0
    X509_ALGOR *alg = NULL;
586
587
0
    if (!decrypt) {
588
0
        PKCS7_RECIP_INFO_get0_alg(ri, &alg);
589
0
        if (alg != NULL)
590
0
            return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
591
0
                V_ASN1_NULL, NULL);
592
0
    }
593
0
    return 1;
594
0
}
595
596
int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
597
0
{
598
0
    int ret;
599
0
    EVP_PKEY *pkey = NULL;
600
0
    if (!ASN1_INTEGER_set(p7i->version, 0))
601
0
        return 0;
602
0
    if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
603
0
            X509_get_issuer_name(x509)))
604
0
        return 0;
605
606
0
    ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
607
0
    if (!(p7i->issuer_and_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(x509))))
608
0
        return 0;
609
610
0
    pkey = X509_get0_pubkey(x509);
611
0
    if (pkey == NULL)
612
0
        return 0;
613
614
0
    if (EVP_PKEY_is_a(pkey, "RSA-PSS"))
615
0
        return -2;
616
0
    if (EVP_PKEY_is_a(pkey, "RSA")) {
617
0
        if (pkcs7_rsa_encrypt_decrypt_setup(p7i, 0) <= 0)
618
0
            goto err;
619
0
        goto finished;
620
0
    }
621
622
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) {
623
0
        ERR_raise(ERR_LIB_PKCS7,
624
0
            PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
625
0
        goto err;
626
0
    }
627
628
0
    ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
629
0
    if (ret == -2) {
630
0
        ERR_raise(ERR_LIB_PKCS7,
631
0
            PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
632
0
        goto err;
633
0
    }
634
0
    if (ret <= 0) {
635
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ENCRYPTION_CTRL_FAILURE);
636
0
        goto err;
637
0
    }
638
0
finished:
639
0
    if (!X509_up_ref(x509))
640
0
        goto err;
641
642
0
    p7i->cert = x509;
643
644
0
    return 1;
645
646
0
err:
647
0
    return 0;
648
0
}
649
650
X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
651
0
{
652
0
    if (PKCS7_type_is_signed(p7))
653
0
        return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
654
0
            si->issuer_and_serial->issuer,
655
0
            si->issuer_and_serial->serial));
656
0
    else
657
0
        return NULL;
658
0
}
659
660
int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
661
0
{
662
0
    int i;
663
0
    PKCS7_ENC_CONTENT *ec;
664
665
0
    i = OBJ_obj2nid(p7->type);
666
0
    switch (i) {
667
0
    case NID_pkcs7_signedAndEnveloped:
668
0
        ec = p7->d.signed_and_enveloped->enc_data;
669
0
        break;
670
0
    case NID_pkcs7_enveloped:
671
0
        ec = p7->d.enveloped->enc_data;
672
0
        break;
673
0
    default:
674
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
675
0
        return 0;
676
0
    }
677
678
    /* Check cipher OID exists and has data in it */
679
0
    i = EVP_CIPHER_get_type(cipher);
680
0
    if (i == NID_undef) {
681
0
        ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
682
0
        return 0;
683
0
    }
684
685
0
    ec->cipher = cipher;
686
0
    ec->ctx = ossl_pkcs7_get0_ctx(p7);
687
0
    return 1;
688
0
}
689
690
/* Return the content octet string that indefinite-length streaming encodes */
691
ASN1_OCTET_STRING *ossl_pkcs7_get0_stream_content(PKCS7 *p7)
692
0
{
693
0
    ASN1_OCTET_STRING *os = NULL;
694
695
0
    switch (OBJ_obj2nid(p7->type)) {
696
0
    case NID_pkcs7_data:
697
0
        os = p7->d.data;
698
0
        break;
699
700
0
    case NID_pkcs7_signedAndEnveloped:
701
0
        if (p7->d.signed_and_enveloped == NULL || p7->d.signed_and_enveloped->enc_data == NULL) {
702
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
703
0
            break;
704
0
        }
705
0
        os = p7->d.signed_and_enveloped->enc_data->enc_data;
706
0
        if (os == NULL) {
707
0
            os = ASN1_OCTET_STRING_new();
708
0
            p7->d.signed_and_enveloped->enc_data->enc_data = os;
709
0
        }
710
0
        break;
711
712
0
    case NID_pkcs7_enveloped:
713
0
        if (p7->d.enveloped == NULL || p7->d.enveloped->enc_data == NULL) {
714
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
715
0
            break;
716
0
        }
717
0
        os = p7->d.enveloped->enc_data->enc_data;
718
0
        if (os == NULL) {
719
0
            os = ASN1_OCTET_STRING_new();
720
0
            p7->d.enveloped->enc_data->enc_data = os;
721
0
        }
722
0
        break;
723
724
0
    case NID_pkcs7_signed:
725
0
        if (p7->d.sign == NULL || p7->d.sign->contents == NULL) {
726
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
727
0
            break;
728
0
        }
729
730
0
        if (!PKCS7_type_is_data(p7->d.sign->contents)) {
731
0
            ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
732
0
            break;
733
0
        }
734
735
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
736
0
        break;
737
738
0
    default:
739
0
        os = NULL;
740
0
        break;
741
0
    }
742
743
0
    return os;
744
0
}
745
746
int ossl_pkcs7_stream(PKCS7 *p7)
747
0
{
748
0
    ASN1_OCTET_STRING *os = ossl_pkcs7_get0_stream_content(p7);
749
750
0
    if (os == NULL)
751
0
        return 0;
752
753
0
    p7->state |= PKCS7_STATE_STREAMING;
754
755
0
    return 1;
756
0
}
757
758
#if !defined(OPENSSL_NO_DEPRECATED_4_1)
759
int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
760
0
{
761
0
    if (boundary != NULL)
762
0
        *boundary = NULL;
763
0
    return ossl_pkcs7_stream(p7);
764
0
}
765
#endif /* !defined(OPENSSL_NO_DEPRECATED_4_1) */