]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pkcs7/pk7_lib.c
make EVP_PKEY opaque
[thirdparty/openssl.git] / crypto / pkcs7 / pk7_lib.c
CommitLineData
d02b48c6 1/* crypto/pkcs7/pk7_lib.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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.
0f113f3e 8 *
d02b48c6
RE
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).
0f113f3e 15 *
d02b48c6
RE
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.
0f113f3e 22 *
d02b48c6
RE
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 :-).
0f113f3e 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 40 *
d02b48c6
RE
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.
0f113f3e 52 *
d02b48c6
RE
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>
b39fc560 60#include "internal/cryptlib.h"
ec577822
BM
61#include <openssl/objects.h>
62#include <openssl/x509.h>
5fe736e5 63#include "internal/asn1_int.h"
3aeb9348 64#include "internal/evp_int.h"
d02b48c6 65
6b691a5c 66long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
0f113f3e
MC
67{
68 int nid;
69 long ret;
70
71 nid = OBJ_obj2nid(p7->type);
72
73 switch (cmd) {
c225c3cf 74 /* NOTE(emilia): does not support detached digested data. */
0f113f3e
MC
75 case PKCS7_OP_SET_DETACHED_SIGNATURE:
76 if (nid == NID_pkcs7_signed) {
77 ret = p7->detached = (int)larg;
78 if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
79 ASN1_OCTET_STRING *os;
80 os = p7->d.sign->contents->d.data;
81 ASN1_OCTET_STRING_free(os);
82 p7->d.sign->contents->d.data = NULL;
83 }
84 } else {
85 PKCS7err(PKCS7_F_PKCS7_CTRL,
86 PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
87 ret = 0;
88 }
89 break;
90 case PKCS7_OP_GET_DETACHED_SIGNATURE:
91 if (nid == NID_pkcs7_signed) {
92 if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
93 ret = 1;
94 else
95 ret = 0;
96
97 p7->detached = ret;
98 } else {
99 PKCS7err(PKCS7_F_PKCS7_CTRL,
100 PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
101 ret = 0;
102 }
103
104 break;
105 default:
106 PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
107 ret = 0;
108 }
109 return (ret);
110}
d02b48c6 111
6b691a5c 112int PKCS7_content_new(PKCS7 *p7, int type)
0f113f3e
MC
113{
114 PKCS7 *ret = NULL;
115
116 if ((ret = PKCS7_new()) == NULL)
117 goto err;
118 if (!PKCS7_set_type(ret, type))
119 goto err;
120 if (!PKCS7_set_content(p7, ret))
121 goto err;
122
123 return (1);
124 err:
e0e920b1 125 PKCS7_free(ret);
0f113f3e
MC
126 return (0);
127}
d02b48c6 128
6b691a5c 129int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
0f113f3e
MC
130{
131 int i;
132
133 i = OBJ_obj2nid(p7->type);
134 switch (i) {
135 case NID_pkcs7_signed:
e0e920b1 136 PKCS7_free(p7->d.sign->contents);
0f113f3e
MC
137 p7->d.sign->contents = p7_data;
138 break;
139 case NID_pkcs7_digest:
e0e920b1 140 PKCS7_free(p7->d.digest->contents);
0f113f3e
MC
141 p7->d.digest->contents = p7_data;
142 break;
143 case NID_pkcs7_data:
144 case NID_pkcs7_enveloped:
145 case NID_pkcs7_signedAndEnveloped:
146 case NID_pkcs7_encrypted:
147 default:
148 PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
149 goto err;
150 }
151 return (1);
152 err:
153 return (0);
154}
d02b48c6 155
6b691a5c 156int PKCS7_set_type(PKCS7 *p7, int type)
0f113f3e
MC
157{
158 ASN1_OBJECT *obj;
159
160 /*
161 * PKCS7_content_free(p7);
162 */
163 obj = OBJ_nid2obj(type); /* will not fail */
164
165 switch (type) {
166 case NID_pkcs7_signed:
167 p7->type = obj;
168 if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
169 goto err;
170 if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
171 PKCS7_SIGNED_free(p7->d.sign);
172 p7->d.sign = NULL;
173 goto err;
174 }
175 break;
176 case NID_pkcs7_data:
177 p7->type = obj;
f422a514 178 if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL)
0f113f3e
MC
179 goto err;
180 break;
181 case NID_pkcs7_signedAndEnveloped:
182 p7->type = obj;
183 if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
184 == NULL)
185 goto err;
186 ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1);
187 if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
188 goto err;
189 p7->d.signed_and_enveloped->enc_data->content_type
190 = OBJ_nid2obj(NID_pkcs7_data);
191 break;
192 case NID_pkcs7_enveloped:
193 p7->type = obj;
194 if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
195 == NULL)
196 goto err;
197 if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
198 goto err;
199 p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
200 break;
201 case NID_pkcs7_encrypted:
202 p7->type = obj;
203 if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
204 == NULL)
205 goto err;
206 if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
207 goto err;
208 p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
209 break;
210
211 case NID_pkcs7_digest:
212 p7->type = obj;
213 if ((p7->d.digest = PKCS7_DIGEST_new())
214 == NULL)
215 goto err;
216 if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
217 goto err;
218 break;
219 default:
220 PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
221 goto err;
222 }
223 return (1);
224 err:
225 return (0);
226}
d02b48c6 227
8d9086df 228int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
0f113f3e
MC
229{
230 p7->type = OBJ_nid2obj(type);
231 p7->d.other = other;
232 return 1;
233}
8d9086df 234
6b691a5c 235int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
0f113f3e
MC
236{
237 int i, j, nid;
238 X509_ALGOR *alg;
239 STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
240 STACK_OF(X509_ALGOR) *md_sk;
241
242 i = OBJ_obj2nid(p7->type);
243 switch (i) {
244 case NID_pkcs7_signed:
245 signer_sk = p7->d.sign->signer_info;
246 md_sk = p7->d.sign->md_algs;
247 break;
248 case NID_pkcs7_signedAndEnveloped:
249 signer_sk = p7->d.signed_and_enveloped->signer_info;
250 md_sk = p7->d.signed_and_enveloped->md_algs;
251 break;
252 default:
253 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
254 return (0);
255 }
256
257 nid = OBJ_obj2nid(psi->digest_alg->algorithm);
258
259 /* If the digest is not currently listed, add it */
260 j = 0;
261 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
262 alg = sk_X509_ALGOR_value(md_sk, i);
263 if (OBJ_obj2nid(alg->algorithm) == nid) {
264 j = 1;
265 break;
266 }
267 }
268 if (!j) { /* we need to add another algorithm */
75ebbd9a
RS
269 if ((alg = X509_ALGOR_new()) == NULL
270 || (alg->parameter = ASN1_TYPE_new()) == NULL) {
0f113f3e
MC
271 X509_ALGOR_free(alg);
272 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
273 return (0);
274 }
275 alg->algorithm = OBJ_nid2obj(nid);
276 alg->parameter->type = V_ASN1_NULL;
277 if (!sk_X509_ALGOR_push(md_sk, alg)) {
278 X509_ALGOR_free(alg);
279 return 0;
280 }
281 }
282
283 if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
284 return 0;
285 return (1);
286}
d02b48c6 287
6b691a5c 288int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
0f113f3e
MC
289{
290 int i;
291 STACK_OF(X509) **sk;
292
293 i = OBJ_obj2nid(p7->type);
294 switch (i) {
295 case NID_pkcs7_signed:
296 sk = &(p7->d.sign->cert);
297 break;
298 case NID_pkcs7_signedAndEnveloped:
299 sk = &(p7->d.signed_and_enveloped->cert);
300 break;
301 default:
302 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE);
303 return (0);
304 }
305
306 if (*sk == NULL)
307 *sk = sk_X509_new_null();
308 if (*sk == NULL) {
309 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
310 return 0;
311 }
05f0fb9f 312 X509_up_ref(x509);
0f113f3e
MC
313 if (!sk_X509_push(*sk, x509)) {
314 X509_free(x509);
315 return 0;
316 }
317 return (1);
318}
d02b48c6 319
6b691a5c 320int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
0f113f3e
MC
321{
322 int i;
323 STACK_OF(X509_CRL) **sk;
324
325 i = OBJ_obj2nid(p7->type);
326 switch (i) {
327 case NID_pkcs7_signed:
328 sk = &(p7->d.sign->crl);
329 break;
330 case NID_pkcs7_signedAndEnveloped:
331 sk = &(p7->d.signed_and_enveloped->crl);
332 break;
333 default:
334 PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
335 return (0);
336 }
337
338 if (*sk == NULL)
339 *sk = sk_X509_CRL_new_null();
340 if (*sk == NULL) {
341 PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
342 return 0;
343 }
344
65cbf983 345 X509_CRL_up_ref(crl);
0f113f3e
MC
346 if (!sk_X509_CRL_push(*sk, crl)) {
347 X509_CRL_free(crl);
348 return 0;
349 }
350 return (1);
351}
d02b48c6 352
6b691a5c 353int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
0f113f3e
MC
354 const EVP_MD *dgst)
355{
356 int ret;
357
358 /* We now need to add another PKCS7_SIGNER_INFO entry */
359 if (!ASN1_INTEGER_set(p7i->version, 1))
360 goto err;
361 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
362 X509_get_issuer_name(x509)))
363 goto err;
364
365 /*
366 * because ASN1_INTEGER_set is used to set a 'long' we will do things the
367 * ugly way.
368 */
f422a514 369 ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
0f113f3e 370 if (!(p7i->issuer_and_serial->serial =
f422a514 371 ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
0f113f3e
MC
372 goto err;
373
374 /* lets keep the pkey around for a while */
3aeb9348 375 EVP_PKEY_up_ref(pkey);
0f113f3e
MC
376 p7i->pkey = pkey;
377
378 /* Set the algorithms */
379
380 X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
381 V_ASN1_NULL, NULL);
382
383 if (pkey->ameth && pkey->ameth->pkey_ctrl) {
384 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
385 if (ret > 0)
386 return 1;
387 if (ret != -2) {
388 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
389 PKCS7_R_SIGNING_CTRL_FAILURE);
390 return 0;
391 }
392 }
393 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
394 PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
395 err:
396 return 0;
397}
d02b48c6 398
6b691a5c 399PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
0f113f3e
MC
400 const EVP_MD *dgst)
401{
402 PKCS7_SIGNER_INFO *si = NULL;
403
404 if (dgst == NULL) {
405 int def_nid;
406 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
407 goto err;
408 dgst = EVP_get_digestbynid(def_nid);
409 if (dgst == NULL) {
410 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, PKCS7_R_NO_DEFAULT_DIGEST);
411 goto err;
412 }
413 }
414
415 if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
416 goto err;
417 if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
418 goto err;
419 if (!PKCS7_add_signer(p7, si))
420 goto err;
421 return (si);
422 err:
e0e920b1 423 PKCS7_SIGNER_INFO_free(si);
0f113f3e
MC
424 return (NULL);
425}
d02b48c6 426
c5a55463 427int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
0f113f3e
MC
428{
429 if (PKCS7_type_is_digest(p7)) {
75ebbd9a 430 if ((p7->d.digest->md->parameter = ASN1_TYPE_new()) == NULL) {
0f113f3e
MC
431 PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
432 return 0;
433 }
434 p7->d.digest->md->parameter->type = V_ASN1_NULL;
435 p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
436 return 1;
437 }
438
439 PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
440 return 1;
441}
c5a55463 442
b05b50e6 443STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
0f113f3e 444{
c225c3cf
EK
445 if (p7 == NULL || p7->d.ptr == NULL)
446 return NULL;
0f113f3e
MC
447 if (PKCS7_type_is_signed(p7)) {
448 return (p7->d.sign->signer_info);
449 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
450 return (p7->d.signed_and_enveloped->signer_info);
451 } else
452 return (NULL);
453}
d02b48c6 454
492a9e24 455void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
0f113f3e
MC
456 X509_ALGOR **pdig, X509_ALGOR **psig)
457{
458 if (pk)
459 *pk = si->pkey;
460 if (pdig)
461 *pdig = si->digest_alg;
462 if (psig)
463 *psig = si->digest_enc_alg;
464}
492a9e24 465
e4b21c74 466void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
0f113f3e
MC
467{
468 if (penc)
469 *penc = ri->key_enc_algor;
470}
e4b21c74 471
6b691a5c 472PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
0f113f3e
MC
473{
474 PKCS7_RECIP_INFO *ri;
475
476 if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
477 goto err;
478 if (!PKCS7_RECIP_INFO_set(ri, x509))
479 goto err;
480 if (!PKCS7_add_recipient_info(p7, ri))
481 goto err;
482 return ri;
483 err:
e0e920b1 484 PKCS7_RECIP_INFO_free(ri);
0f113f3e
MC
485 return NULL;
486}
58964a49 487
6b691a5c 488int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
0f113f3e
MC
489{
490 int i;
491 STACK_OF(PKCS7_RECIP_INFO) *sk;
492
493 i = OBJ_obj2nid(p7->type);
494 switch (i) {
495 case NID_pkcs7_signedAndEnveloped:
496 sk = p7->d.signed_and_enveloped->recipientinfo;
497 break;
498 case NID_pkcs7_enveloped:
499 sk = p7->d.enveloped->recipientinfo;
500 break;
501 default:
502 PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
503 PKCS7_R_WRONG_CONTENT_TYPE);
504 return (0);
505 }
506
507 if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
508 return 0;
509 return (1);
510}
58964a49 511
6b691a5c 512int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
0f113f3e
MC
513{
514 int ret;
515 EVP_PKEY *pkey = NULL;
516 if (!ASN1_INTEGER_set(p7i->version, 0))
517 return 0;
518 if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
519 X509_get_issuer_name(x509)))
520 return 0;
521
f422a514 522 ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
0f113f3e 523 if (!(p7i->issuer_and_serial->serial =
f422a514 524 ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
0f113f3e
MC
525 return 0;
526
8382fd3a 527 pkey = X509_get0_pubkey(x509);
0f113f3e
MC
528
529 if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
530 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
531 PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
532 goto err;
533 }
534
535 ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
536 if (ret == -2) {
537 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
538 PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
539 goto err;
540 }
541 if (ret <= 0) {
542 PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
543 PKCS7_R_ENCRYPTION_CTRL_FAILURE);
544 goto err;
545 }
546
05f0fb9f 547 X509_up_ref(x509);
0f113f3e
MC
548 p7i->cert = x509;
549
550 return 1;
551
552 err:
0f113f3e
MC
553 return 0;
554}
58964a49 555
6b691a5c 556X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
0f113f3e
MC
557{
558 if (PKCS7_type_is_signed(p7))
559 return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
560 si->issuer_and_serial->issuer,
561 si->
562 issuer_and_serial->serial));
563 else
564 return (NULL);
565}
d02b48c6 566
84fa704c 567int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
0f113f3e
MC
568{
569 int i;
570 PKCS7_ENC_CONTENT *ec;
571
572 i = OBJ_obj2nid(p7->type);
573 switch (i) {
574 case NID_pkcs7_signedAndEnveloped:
575 ec = p7->d.signed_and_enveloped->enc_data;
576 break;
577 case NID_pkcs7_enveloped:
578 ec = p7->d.enveloped->enc_data;
579 break;
580 default:
581 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
582 return (0);
583 }
584
585 /* Check cipher OID exists and has data in it */
586 i = EVP_CIPHER_type(cipher);
587 if (i == NID_undef) {
588 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
589 PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
590 return (0);
591 }
592
593 ec->cipher = cipher;
594 return 1;
595}
58964a49 596
11d8cdc6 597int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
0f113f3e
MC
598{
599 ASN1_OCTET_STRING *os = NULL;
600
601 switch (OBJ_obj2nid(p7->type)) {
602 case NID_pkcs7_data:
603 os = p7->d.data;
604 break;
605
606 case NID_pkcs7_signedAndEnveloped:
607 os = p7->d.signed_and_enveloped->enc_data->enc_data;
608 if (os == NULL) {
f422a514 609 os = ASN1_OCTET_STRING_new();
0f113f3e
MC
610 p7->d.signed_and_enveloped->enc_data->enc_data = os;
611 }
612 break;
613
614 case NID_pkcs7_enveloped:
615 os = p7->d.enveloped->enc_data->enc_data;
616 if (os == NULL) {
f422a514 617 os = ASN1_OCTET_STRING_new();
0f113f3e
MC
618 p7->d.enveloped->enc_data->enc_data = os;
619 }
620 break;
621
622 case NID_pkcs7_signed:
623 os = p7->d.sign->contents->d.data;
624 break;
625
626 default:
627 os = NULL;
628 break;
629 }
630
631 if (os == NULL)
632 return 0;
633
634 os->flags |= ASN1_STRING_FLAG_NDEF;
635 *boundary = &os->data;
636
637 return 1;
638}