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