Coverage Report

Created: 2025-06-13 06:58

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