1 /* $OpenBSD: pk7_lib.c,v 1.28 2023/11/09 19:08:07 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59 #include <stdio.h>
60
61 #include <openssl/err.h>
62 #include <openssl/objects.h>
63 #include <openssl/x509.h>
64
65 #include "asn1_local.h"
66 #include "evp_local.h"
67 #include "x509_local.h"
68
69 long
PKCS7_ctrl(PKCS7 * p7,int cmd,long larg,char * parg)70 PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
71 {
72 int nid;
73 long ret;
74
75 nid = OBJ_obj2nid(p7->type);
76
77 switch (cmd) {
78 case PKCS7_OP_SET_DETACHED_SIGNATURE:
79 if (nid == NID_pkcs7_signed) {
80 ret = p7->detached = (int)larg;
81 if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
82 ASN1_OCTET_STRING *os;
83 os = p7->d.sign->contents->d.data;
84 ASN1_OCTET_STRING_free(os);
85 p7->d.sign->contents->d.data = NULL;
86 }
87 } else {
88 PKCS7error(PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
89 ret = 0;
90 }
91 break;
92 case PKCS7_OP_GET_DETACHED_SIGNATURE:
93 if (nid == NID_pkcs7_signed) {
94 if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
95 ret = 1;
96 else
97 ret = 0;
98
99 p7->detached = ret;
100 } else {
101 PKCS7error(PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
102 ret = 0;
103 }
104
105 break;
106 default:
107 PKCS7error(PKCS7_R_UNKNOWN_OPERATION);
108 ret = 0;
109 }
110 return (ret);
111 }
112 LCRYPTO_ALIAS(PKCS7_ctrl);
113
114 int
PKCS7_content_new(PKCS7 * p7,int type)115 PKCS7_content_new(PKCS7 *p7, int type)
116 {
117 PKCS7 *ret = NULL;
118
119 if ((ret = PKCS7_new()) == NULL)
120 goto err;
121 if (!PKCS7_set_type(ret, type))
122 goto err;
123 if (!PKCS7_set_content(p7, ret))
124 goto err;
125
126 return (1);
127 err:
128 if (ret != NULL)
129 PKCS7_free(ret);
130 return (0);
131 }
132 LCRYPTO_ALIAS(PKCS7_content_new);
133
134 int
PKCS7_set_content(PKCS7 * p7,PKCS7 * p7_data)135 PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
136 {
137 int i;
138
139 i = OBJ_obj2nid(p7->type);
140 switch (i) {
141 case NID_pkcs7_signed:
142 if (p7->d.sign->contents != NULL)
143 PKCS7_free(p7->d.sign->contents);
144 p7->d.sign->contents = p7_data;
145 break;
146 case NID_pkcs7_digest:
147 if (p7->d.digest->contents != NULL)
148 PKCS7_free(p7->d.digest->contents);
149 p7->d.digest->contents = p7_data;
150 break;
151 case NID_pkcs7_data:
152 case NID_pkcs7_enveloped:
153 case NID_pkcs7_signedAndEnveloped:
154 case NID_pkcs7_encrypted:
155 default:
156 PKCS7error(PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
157 goto err;
158 }
159 return (1);
160 err:
161 return (0);
162 }
163 LCRYPTO_ALIAS(PKCS7_set_content);
164
165 int
PKCS7_set_type(PKCS7 * p7,int type)166 PKCS7_set_type(PKCS7 *p7, int type)
167 {
168 ASN1_OBJECT *obj;
169
170 /*PKCS7_content_free(p7);*/
171 obj=OBJ_nid2obj(type); /* will not fail */
172
173 switch (type) {
174 case NID_pkcs7_signed:
175 p7->type = obj;
176 if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
177 goto err;
178 if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
179 PKCS7_SIGNED_free(p7->d.sign);
180 p7->d.sign = NULL;
181 goto err;
182 }
183 break;
184 case NID_pkcs7_data:
185 p7->type = obj;
186 if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL)
187 goto err;
188 break;
189 case NID_pkcs7_signedAndEnveloped:
190 p7->type = obj;
191 if ((p7->d.signed_and_enveloped =
192 PKCS7_SIGN_ENVELOPE_new()) == NULL)
193 goto err;
194 if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
195 goto err;
196 p7->d.signed_and_enveloped->enc_data->content_type =
197 OBJ_nid2obj(NID_pkcs7_data);
198 break;
199 case NID_pkcs7_enveloped:
200 p7->type = obj;
201 if ((p7->d.enveloped = PKCS7_ENVELOPE_new()) == NULL)
202 goto err;
203 if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
204 goto err;
205 p7->d.enveloped->enc_data->content_type =
206 OBJ_nid2obj(NID_pkcs7_data);
207 break;
208 case NID_pkcs7_encrypted:
209 p7->type = obj;
210 if ((p7->d.encrypted = PKCS7_ENCRYPT_new()) == NULL)
211 goto err;
212 if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
213 goto err;
214 p7->d.encrypted->enc_data->content_type =
215 OBJ_nid2obj(NID_pkcs7_data);
216 break;
217
218 case NID_pkcs7_digest:
219 p7->type = obj;
220 if ((p7->d.digest = PKCS7_DIGEST_new()) == NULL)
221 goto err;
222 if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
223 goto err;
224 break;
225 default:
226 PKCS7error(PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
227 goto err;
228 }
229 return (1);
230 err:
231 return (0);
232 }
233 LCRYPTO_ALIAS(PKCS7_set_type);
234
235 int
PKCS7_set0_type_other(PKCS7 * p7,int type,ASN1_TYPE * other)236 PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
237 {
238 p7->type = OBJ_nid2obj(type);
239 p7->d.other = other;
240 return 1;
241 }
242 LCRYPTO_ALIAS(PKCS7_set0_type_other);
243
244 int
PKCS7_add_signer(PKCS7 * p7,PKCS7_SIGNER_INFO * psi)245 PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
246 {
247 int i, j, nid;
248 X509_ALGOR *alg;
249 STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
250 STACK_OF(X509_ALGOR) *md_sk;
251
252 i = OBJ_obj2nid(p7->type);
253 switch (i) {
254 case NID_pkcs7_signed:
255 signer_sk = p7->d.sign->signer_info;
256 md_sk = p7->d.sign->md_algs;
257 break;
258 case NID_pkcs7_signedAndEnveloped:
259 signer_sk = p7->d.signed_and_enveloped->signer_info;
260 md_sk = p7->d.signed_and_enveloped->md_algs;
261 break;
262 default:
263 PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
264 return (0);
265 }
266
267 nid = OBJ_obj2nid(psi->digest_alg->algorithm);
268
269 /* If the digest is not currently listed, add it */
270 j = 0;
271 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
272 alg = sk_X509_ALGOR_value(md_sk, i);
273 if (OBJ_obj2nid(alg->algorithm) == nid) {
274 j = 1;
275 break;
276 }
277 }
278 if (!j) /* we need to add another algorithm */
279 {
280 if (!(alg = X509_ALGOR_new()) ||
281 !(alg->parameter = ASN1_TYPE_new())) {
282 X509_ALGOR_free(alg);
283 PKCS7error(ERR_R_MALLOC_FAILURE);
284 return (0);
285 }
286 alg->algorithm = OBJ_nid2obj(nid);
287 alg->parameter->type = V_ASN1_NULL;
288 if (!sk_X509_ALGOR_push(md_sk, alg)) {
289 X509_ALGOR_free(alg);
290 return 0;
291 }
292 }
293
294 if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
295 return 0;
296 return (1);
297 }
298 LCRYPTO_ALIAS(PKCS7_add_signer);
299
300 int
PKCS7_add_certificate(PKCS7 * p7,X509 * x509)301 PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
302 {
303 int i;
304 STACK_OF(X509) **sk;
305
306 i = OBJ_obj2nid(p7->type);
307 switch (i) {
308 case NID_pkcs7_signed:
309 sk = &(p7->d.sign->cert);
310 break;
311 case NID_pkcs7_signedAndEnveloped:
312 sk = &(p7->d.signed_and_enveloped->cert);
313 break;
314 default:
315 PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
316 return (0);
317 }
318
319 if (*sk == NULL)
320 *sk = sk_X509_new_null();
321 if (*sk == NULL) {
322 PKCS7error(ERR_R_MALLOC_FAILURE);
323 return 0;
324 }
325 CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
326 if (!sk_X509_push(*sk, x509)) {
327 X509_free(x509);
328 return 0;
329 }
330 return (1);
331 }
332 LCRYPTO_ALIAS(PKCS7_add_certificate);
333
334 int
PKCS7_add_crl(PKCS7 * p7,X509_CRL * crl)335 PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
336 {
337 int i;
338 STACK_OF(X509_CRL) **sk;
339
340 i = OBJ_obj2nid(p7->type);
341 switch (i) {
342 case NID_pkcs7_signed:
343 sk = &(p7->d.sign->crl);
344 break;
345 case NID_pkcs7_signedAndEnveloped:
346 sk = &(p7->d.signed_and_enveloped->crl);
347 break;
348 default:
349 PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
350 return (0);
351 }
352
353 if (*sk == NULL)
354 *sk = sk_X509_CRL_new_null();
355 if (*sk == NULL) {
356 PKCS7error(ERR_R_MALLOC_FAILURE);
357 return 0;
358 }
359
360 CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
361 if (!sk_X509_CRL_push(*sk, crl)) {
362 X509_CRL_free(crl);
363 return 0;
364 }
365 return (1);
366 }
367 LCRYPTO_ALIAS(PKCS7_add_crl);
368
369 int
PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO * p7i,X509 * x509,EVP_PKEY * pkey,const EVP_MD * dgst)370 PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
371 const EVP_MD *dgst)
372 {
373 int nid;
374 int ret;
375
376 /* We now need to add another PKCS7_SIGNER_INFO entry */
377 if (!ASN1_INTEGER_set(p7i->version, 1))
378 goto err;
379 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
380 X509_get_issuer_name(x509)))
381 goto err;
382
383 /* because ASN1_INTEGER_set is used to set a 'long' we will do
384 * things the ugly way. */
385 ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
386 if (!(p7i->issuer_and_serial->serial =
387 ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
388 goto err;
389
390 /* lets keep the pkey around for a while */
391 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
392 p7i->pkey = pkey;
393
394 /*
395 * Do not use X509_ALGOR_set_evp_md() to match historical behavior.
396 * A mistranslation of the ASN.1 from 1988 to 1997 syntax lost the
397 * OPTIONAL field, cf. the NOTE above RFC 5754, 2.1.
398 * Using X509_ALGOR_set_evp_md() would change encoding of the SHAs.
399 */
400 nid = EVP_MD_type(dgst);
401 if (!X509_ALGOR_set0_by_nid(p7i->digest_alg, nid, V_ASN1_NULL, NULL))
402 return 0;
403
404 if (pkey->ameth && pkey->ameth->pkey_ctrl) {
405 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN,
406 0, p7i);
407 if (ret > 0)
408 return 1;
409 if (ret != -2) {
410 PKCS7error(PKCS7_R_SIGNING_CTRL_FAILURE);
411 return 0;
412 }
413 }
414 PKCS7error(PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
415 err:
416 return 0;
417 }
418 LCRYPTO_ALIAS(PKCS7_SIGNER_INFO_set);
419
420 PKCS7_SIGNER_INFO *
PKCS7_add_signature(PKCS7 * p7,X509 * x509,EVP_PKEY * pkey,const EVP_MD * dgst)421 PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, const EVP_MD *dgst)
422 {
423 PKCS7_SIGNER_INFO *si = NULL;
424
425 if (dgst == NULL) {
426 int def_nid;
427 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
428 goto err;
429 dgst = EVP_get_digestbynid(def_nid);
430 if (dgst == NULL) {
431 PKCS7error(PKCS7_R_NO_DEFAULT_DIGEST);
432 goto err;
433 }
434 }
435
436 if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
437 goto err;
438 if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
439 goto err;
440 if (!PKCS7_add_signer(p7, si))
441 goto err;
442 return (si);
443 err:
444 if (si)
445 PKCS7_SIGNER_INFO_free(si);
446 return (NULL);
447 }
448 LCRYPTO_ALIAS(PKCS7_add_signature);
449
450 int
PKCS7_set_digest(PKCS7 * p7,const EVP_MD * md)451 PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
452 {
453 if (PKCS7_type_is_digest(p7)) {
454 if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
455 PKCS7error(ERR_R_MALLOC_FAILURE);
456 return 0;
457 }
458 p7->d.digest->md->parameter->type = V_ASN1_NULL;
459 p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
460 return 1;
461 }
462
463 PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
464 return 1;
465 }
466 LCRYPTO_ALIAS(PKCS7_set_digest);
467
STACK_OF(PKCS7_SIGNER_INFO)468 STACK_OF(PKCS7_SIGNER_INFO) *
469 PKCS7_get_signer_info(PKCS7 *p7)
470 {
471 if (p7 == NULL || p7->d.ptr == NULL)
472 return (NULL);
473 if (PKCS7_type_is_signed(p7)) {
474 return (p7->d.sign->signer_info);
475 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
476 return (p7->d.signed_and_enveloped->signer_info);
477 } else
478 return (NULL);
479 }
480 LCRYPTO_ALIAS(PKCS7_get_signer_info);
481
482 void
PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO * si,EVP_PKEY ** pk,X509_ALGOR ** pdig,X509_ALGOR ** psig)483 PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
484 X509_ALGOR **pdig, X509_ALGOR **psig)
485 {
486 if (pk)
487 *pk = si->pkey;
488 if (pdig)
489 *pdig = si->digest_alg;
490 if (psig)
491 *psig = si->digest_enc_alg;
492 }
493 LCRYPTO_ALIAS(PKCS7_SIGNER_INFO_get0_algs);
494
495 void
PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO * ri,X509_ALGOR ** penc)496 PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
497 {
498 if (penc)
499 *penc = ri->key_enc_algor;
500 }
501 LCRYPTO_ALIAS(PKCS7_RECIP_INFO_get0_alg);
502
503 PKCS7_RECIP_INFO *
PKCS7_add_recipient(PKCS7 * p7,X509 * x509)504 PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
505 {
506 PKCS7_RECIP_INFO *ri;
507
508 if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
509 goto err;
510 if (!PKCS7_RECIP_INFO_set(ri, x509))
511 goto err;
512 if (!PKCS7_add_recipient_info(p7, ri))
513 goto err;
514 return ri;
515 err:
516 if (ri)
517 PKCS7_RECIP_INFO_free(ri);
518 return NULL;
519 }
520 LCRYPTO_ALIAS(PKCS7_add_recipient);
521
522 int
PKCS7_add_recipient_info(PKCS7 * p7,PKCS7_RECIP_INFO * ri)523 PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
524 {
525 int i;
526 STACK_OF(PKCS7_RECIP_INFO) *sk;
527
528 i = OBJ_obj2nid(p7->type);
529 switch (i) {
530 case NID_pkcs7_signedAndEnveloped:
531 sk = p7->d.signed_and_enveloped->recipientinfo;
532 break;
533 case NID_pkcs7_enveloped:
534 sk = p7->d.enveloped->recipientinfo;
535 break;
536 default:
537 PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
538 return (0);
539 }
540
541 if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
542 return 0;
543 return (1);
544 }
545 LCRYPTO_ALIAS(PKCS7_add_recipient_info);
546
547 int
PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO * p7i,X509 * x509)548 PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
549 {
550 int ret;
551 EVP_PKEY *pkey = NULL;
552 if (!ASN1_INTEGER_set(p7i->version, 0))
553 return 0;
554 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
555 X509_get_issuer_name(x509)))
556 return 0;
557
558 ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
559 if (!(p7i->issuer_and_serial->serial =
560 ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
561 return 0;
562
563 pkey = X509_get_pubkey(x509);
564
565 if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
566 PKCS7error(PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
567 goto err;
568 }
569
570 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT,
571 0, p7i);
572 if (ret == -2) {
573 PKCS7error(PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
574 goto err;
575 }
576 if (ret <= 0) {
577 PKCS7error(PKCS7_R_ENCRYPTION_CTRL_FAILURE);
578 goto err;
579 }
580
581 EVP_PKEY_free(pkey);
582
583 CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
584 p7i->cert = x509;
585
586 return 1;
587
588 err:
589 EVP_PKEY_free(pkey);
590 return 0;
591 }
592 LCRYPTO_ALIAS(PKCS7_RECIP_INFO_set);
593
594 X509 *
PKCS7_cert_from_signer_info(PKCS7 * p7,PKCS7_SIGNER_INFO * si)595 PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
596 {
597 if (PKCS7_type_is_signed(p7))
598 return(X509_find_by_issuer_and_serial(p7->d.sign->cert,
599 si->issuer_and_serial->issuer,
600 si->issuer_and_serial->serial));
601 else
602 return (NULL);
603 }
604 LCRYPTO_ALIAS(PKCS7_cert_from_signer_info);
605
606 int
PKCS7_set_cipher(PKCS7 * p7,const EVP_CIPHER * cipher)607 PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
608 {
609 int i;
610 PKCS7_ENC_CONTENT *ec;
611
612 i = OBJ_obj2nid(p7->type);
613 switch (i) {
614 case NID_pkcs7_signedAndEnveloped:
615 ec = p7->d.signed_and_enveloped->enc_data;
616 break;
617 case NID_pkcs7_enveloped:
618 ec = p7->d.enveloped->enc_data;
619 break;
620 default:
621 PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
622 return (0);
623 }
624
625 /* Check cipher OID exists and has data in it*/
626 i = EVP_CIPHER_type(cipher);
627 if (i == NID_undef) {
628 PKCS7error(PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
629 return (0);
630 }
631
632 ec->cipher = cipher;
633 return 1;
634 }
635 LCRYPTO_ALIAS(PKCS7_set_cipher);
636
637 int
PKCS7_stream(unsigned char *** boundary,PKCS7 * p7)638 PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
639 {
640 ASN1_OCTET_STRING *os = NULL;
641
642 switch (OBJ_obj2nid(p7->type)) {
643 case NID_pkcs7_data:
644 os = p7->d.data;
645 break;
646
647 case NID_pkcs7_signedAndEnveloped:
648 os = p7->d.signed_and_enveloped->enc_data->enc_data;
649 if (os == NULL) {
650 os = ASN1_OCTET_STRING_new();
651 p7->d.signed_and_enveloped->enc_data->enc_data = os;
652 }
653 break;
654
655 case NID_pkcs7_enveloped:
656 os = p7->d.enveloped->enc_data->enc_data;
657 if (os == NULL) {
658 os = ASN1_OCTET_STRING_new();
659 p7->d.enveloped->enc_data->enc_data = os;
660 }
661 break;
662
663 case NID_pkcs7_signed:
664 os = p7->d.sign->contents->d.data;
665 break;
666
667 default:
668 os = NULL;
669 break;
670 }
671
672 if (os == NULL)
673 return 0;
674
675 os->flags |= ASN1_STRING_FLAG_NDEF;
676 *boundary = &os->data;
677
678 return 1;
679 }
680 LCRYPTO_ALIAS(PKCS7_stream);
681