pk7_lib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /* crypto/pkcs7/pk7_lib.c */
  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. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/objects.h>
  61. #include <openssl/x509.h>
  62. long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
  63. {
  64. int nid;
  65. long ret;
  66. nid = OBJ_obj2nid(p7->type);
  67. switch (cmd) {
  68. /* NOTE(emilia): does not support detached digested data. */
  69. case PKCS7_OP_SET_DETACHED_SIGNATURE:
  70. if (nid == NID_pkcs7_signed) {
  71. ret = p7->detached = (int)larg;
  72. if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
  73. ASN1_OCTET_STRING *os;
  74. os = p7->d.sign->contents->d.data;
  75. ASN1_OCTET_STRING_free(os);
  76. p7->d.sign->contents->d.data = NULL;
  77. }
  78. } else {
  79. PKCS7err(PKCS7_F_PKCS7_CTRL,
  80. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  81. ret = 0;
  82. }
  83. break;
  84. case PKCS7_OP_GET_DETACHED_SIGNATURE:
  85. if (nid == NID_pkcs7_signed) {
  86. if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
  87. ret = 1;
  88. else
  89. ret = 0;
  90. p7->detached = ret;
  91. } else {
  92. PKCS7err(PKCS7_F_PKCS7_CTRL,
  93. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  94. ret = 0;
  95. }
  96. break;
  97. default:
  98. PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
  99. ret = 0;
  100. }
  101. return (ret);
  102. }
  103. int PKCS7_content_new(PKCS7 *p7, int type)
  104. {
  105. PKCS7 *ret = NULL;
  106. if ((ret = PKCS7_new()) == NULL)
  107. goto err;
  108. if (!PKCS7_set_type(ret, type))
  109. goto err;
  110. if (!PKCS7_set_content(p7, ret))
  111. goto err;
  112. return (1);
  113. err:
  114. if (ret != NULL)
  115. PKCS7_free(ret);
  116. return (0);
  117. }
  118. int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
  119. {
  120. int i;
  121. i = OBJ_obj2nid(p7->type);
  122. switch (i) {
  123. case NID_pkcs7_signed:
  124. if (p7->d.sign->contents != NULL)
  125. PKCS7_free(p7->d.sign->contents);
  126. p7->d.sign->contents = p7_data;
  127. break;
  128. case NID_pkcs7_digest:
  129. if (p7->d.digest->contents != NULL)
  130. PKCS7_free(p7->d.digest->contents);
  131. p7->d.digest->contents = p7_data;
  132. break;
  133. case NID_pkcs7_data:
  134. case NID_pkcs7_enveloped:
  135. case NID_pkcs7_signedAndEnveloped:
  136. case NID_pkcs7_encrypted:
  137. default:
  138. PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  139. goto err;
  140. }
  141. return (1);
  142. err:
  143. return (0);
  144. }
  145. int PKCS7_set_type(PKCS7 *p7, int type)
  146. {
  147. ASN1_OBJECT *obj;
  148. /*
  149. * PKCS7_content_free(p7);
  150. */
  151. obj = OBJ_nid2obj(type); /* will not fail */
  152. switch (type) {
  153. case NID_pkcs7_signed:
  154. p7->type = obj;
  155. if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
  156. goto err;
  157. if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
  158. PKCS7_SIGNED_free(p7->d.sign);
  159. p7->d.sign = NULL;
  160. goto err;
  161. }
  162. break;
  163. case NID_pkcs7_data:
  164. p7->type = obj;
  165. if ((p7->d.data = M_ASN1_OCTET_STRING_new()) == NULL)
  166. goto err;
  167. break;
  168. case NID_pkcs7_signedAndEnveloped:
  169. p7->type = obj;
  170. if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
  171. == NULL)
  172. goto err;
  173. ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1);
  174. if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
  175. goto err;
  176. p7->d.signed_and_enveloped->enc_data->content_type
  177. = OBJ_nid2obj(NID_pkcs7_data);
  178. break;
  179. case NID_pkcs7_enveloped:
  180. p7->type = obj;
  181. if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
  182. == NULL)
  183. goto err;
  184. if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
  185. goto err;
  186. p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  187. break;
  188. case NID_pkcs7_encrypted:
  189. p7->type = obj;
  190. if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
  191. == NULL)
  192. goto err;
  193. if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
  194. goto err;
  195. p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  196. break;
  197. case NID_pkcs7_digest:
  198. p7->type = obj;
  199. if ((p7->d.digest = PKCS7_DIGEST_new())
  200. == NULL)
  201. goto err;
  202. if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
  203. goto err;
  204. break;
  205. default:
  206. PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  207. goto err;
  208. }
  209. return (1);
  210. err:
  211. return (0);
  212. }
  213. int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
  214. {
  215. p7->type = OBJ_nid2obj(type);
  216. p7->d.other = other;
  217. return 1;
  218. }
  219. int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
  220. {
  221. int i, j, nid;
  222. X509_ALGOR *alg;
  223. STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
  224. STACK_OF(X509_ALGOR) *md_sk;
  225. i = OBJ_obj2nid(p7->type);
  226. switch (i) {
  227. case NID_pkcs7_signed:
  228. signer_sk = p7->d.sign->signer_info;
  229. md_sk = p7->d.sign->md_algs;
  230. break;
  231. case NID_pkcs7_signedAndEnveloped:
  232. signer_sk = p7->d.signed_and_enveloped->signer_info;
  233. md_sk = p7->d.signed_and_enveloped->md_algs;
  234. break;
  235. default:
  236. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
  237. return (0);
  238. }
  239. nid = OBJ_obj2nid(psi->digest_alg->algorithm);
  240. /* If the digest is not currently listed, add it */
  241. j = 0;
  242. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  243. alg = sk_X509_ALGOR_value(md_sk, i);
  244. if (OBJ_obj2nid(alg->algorithm) == nid) {
  245. j = 1;
  246. break;
  247. }
  248. }
  249. if (!j) { /* we need to add another algorithm */
  250. if (!(alg = X509_ALGOR_new())
  251. || !(alg->parameter = ASN1_TYPE_new())) {
  252. X509_ALGOR_free(alg);
  253. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
  254. return (0);
  255. }
  256. alg->algorithm = OBJ_nid2obj(nid);
  257. alg->parameter->type = V_ASN1_NULL;
  258. if (!sk_X509_ALGOR_push(md_sk, alg)) {
  259. X509_ALGOR_free(alg);
  260. return 0;
  261. }
  262. }
  263. if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
  264. return 0;
  265. return (1);
  266. }
  267. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
  268. {
  269. int i;
  270. STACK_OF(X509) **sk;
  271. i = OBJ_obj2nid(p7->type);
  272. switch (i) {
  273. case NID_pkcs7_signed:
  274. sk = &(p7->d.sign->cert);
  275. break;
  276. case NID_pkcs7_signedAndEnveloped:
  277. sk = &(p7->d.signed_and_enveloped->cert);
  278. break;
  279. default:
  280. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE);
  281. return (0);
  282. }
  283. if (*sk == NULL)
  284. *sk = sk_X509_new_null();
  285. if (*sk == NULL) {
  286. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  287. return 0;
  288. }
  289. CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
  290. if (!sk_X509_push(*sk, x509)) {
  291. X509_free(x509);
  292. return 0;
  293. }
  294. return (1);
  295. }
  296. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
  297. {
  298. int i;
  299. STACK_OF(X509_CRL) **sk;
  300. i = OBJ_obj2nid(p7->type);
  301. switch (i) {
  302. case NID_pkcs7_signed:
  303. sk = &(p7->d.sign->crl);
  304. break;
  305. case NID_pkcs7_signedAndEnveloped:
  306. sk = &(p7->d.signed_and_enveloped->crl);
  307. break;
  308. default:
  309. PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
  310. return (0);
  311. }
  312. if (*sk == NULL)
  313. *sk = sk_X509_CRL_new_null();
  314. if (*sk == NULL) {
  315. PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
  316. return 0;
  317. }
  318. CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
  319. if (!sk_X509_CRL_push(*sk, crl)) {
  320. X509_CRL_free(crl);
  321. return 0;
  322. }
  323. return (1);
  324. }
  325. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  326. const EVP_MD *dgst)
  327. {
  328. int nid;
  329. char is_dsa;
  330. if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
  331. is_dsa = 1;
  332. else
  333. is_dsa = 0;
  334. /* We now need to add another PKCS7_SIGNER_INFO entry */
  335. if (!ASN1_INTEGER_set(p7i->version, 1))
  336. goto err;
  337. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  338. X509_get_issuer_name(x509)))
  339. goto err;
  340. /*
  341. * because ASN1_INTEGER_set is used to set a 'long' we will do things the
  342. * ugly way.
  343. */
  344. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  345. if (!(p7i->issuer_and_serial->serial =
  346. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  347. goto err;
  348. /* lets keep the pkey around for a while */
  349. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  350. p7i->pkey = pkey;
  351. /* Set the algorithms */
  352. if (is_dsa)
  353. p7i->digest_alg->algorithm = OBJ_nid2obj(NID_sha1);
  354. else
  355. p7i->digest_alg->algorithm = OBJ_nid2obj(EVP_MD_type(dgst));
  356. if (p7i->digest_alg->parameter != NULL)
  357. ASN1_TYPE_free(p7i->digest_alg->parameter);
  358. if ((p7i->digest_alg->parameter = ASN1_TYPE_new()) == NULL)
  359. goto err;
  360. p7i->digest_alg->parameter->type = V_ASN1_NULL;
  361. if (p7i->digest_enc_alg->parameter != NULL)
  362. ASN1_TYPE_free(p7i->digest_enc_alg->parameter);
  363. nid = EVP_PKEY_type(pkey->type);
  364. if (nid == EVP_PKEY_RSA) {
  365. p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
  366. if (!(p7i->digest_enc_alg->parameter = ASN1_TYPE_new()))
  367. goto err;
  368. p7i->digest_enc_alg->parameter->type = V_ASN1_NULL;
  369. } else if (nid == EVP_PKEY_DSA) {
  370. #if 1
  371. /*
  372. * use 'dsaEncryption' OID for compatibility with other software
  373. * (PKCS #7 v1.5 does specify how to handle DSA) ...
  374. */
  375. p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_dsa);
  376. #else
  377. /*
  378. * ... although the 'dsaWithSHA1' OID (as required by RFC 2630 for
  379. * CMS) would make more sense.
  380. */
  381. p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_dsaWithSHA1);
  382. #endif
  383. p7i->digest_enc_alg->parameter = NULL; /* special case for DSA: omit
  384. * 'parameter'! */
  385. } else if (nid == EVP_PKEY_EC) {
  386. p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_ecdsa_with_SHA1);
  387. if (!(p7i->digest_enc_alg->parameter = ASN1_TYPE_new()))
  388. goto err;
  389. p7i->digest_enc_alg->parameter->type = V_ASN1_NULL;
  390. } else
  391. return (0);
  392. return (1);
  393. err:
  394. return (0);
  395. }
  396. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  397. const EVP_MD *dgst)
  398. {
  399. PKCS7_SIGNER_INFO *si;
  400. if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
  401. goto err;
  402. if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
  403. goto err;
  404. if (!PKCS7_add_signer(p7, si))
  405. goto err;
  406. return (si);
  407. err:
  408. PKCS7_SIGNER_INFO_free(si);
  409. return (NULL);
  410. }
  411. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
  412. {
  413. if (PKCS7_type_is_digest(p7)) {
  414. if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
  415. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
  416. return 0;
  417. }
  418. p7->d.digest->md->parameter->type = V_ASN1_NULL;
  419. p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  420. return 1;
  421. }
  422. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
  423. return 1;
  424. }
  425. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  426. {
  427. if (p7 == NULL || p7->d.ptr == NULL)
  428. return NULL;
  429. if (PKCS7_type_is_signed(p7)) {
  430. return (p7->d.sign->signer_info);
  431. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  432. return (p7->d.signed_and_enveloped->signer_info);
  433. } else
  434. return (NULL);
  435. }
  436. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  437. {
  438. PKCS7_RECIP_INFO *ri;
  439. if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
  440. goto err;
  441. if (!PKCS7_RECIP_INFO_set(ri, x509))
  442. goto err;
  443. if (!PKCS7_add_recipient_info(p7, ri))
  444. goto err;
  445. return (ri);
  446. err:
  447. PKCS7_RECIP_INFO_free(ri);
  448. return (NULL);
  449. }
  450. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  451. {
  452. int i;
  453. STACK_OF(PKCS7_RECIP_INFO) *sk;
  454. i = OBJ_obj2nid(p7->type);
  455. switch (i) {
  456. case NID_pkcs7_signedAndEnveloped:
  457. sk = p7->d.signed_and_enveloped->recipientinfo;
  458. break;
  459. case NID_pkcs7_enveloped:
  460. sk = p7->d.enveloped->recipientinfo;
  461. break;
  462. default:
  463. PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
  464. PKCS7_R_WRONG_CONTENT_TYPE);
  465. return (0);
  466. }
  467. if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
  468. return 0;
  469. return (1);
  470. }
  471. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  472. {
  473. if (!ASN1_INTEGER_set(p7i->version, 0))
  474. return 0;
  475. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  476. X509_get_issuer_name(x509)))
  477. return 0;
  478. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  479. if (!(p7i->issuer_and_serial->serial =
  480. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  481. return 0;
  482. X509_ALGOR_free(p7i->key_enc_algor);
  483. if (!(p7i->key_enc_algor = X509_ALGOR_dup(x509->cert_info->key->algor)))
  484. return 0;
  485. CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
  486. p7i->cert = x509;
  487. return (1);
  488. }
  489. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  490. {
  491. if (PKCS7_type_is_signed(p7))
  492. return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
  493. si->issuer_and_serial->issuer,
  494. si->
  495. issuer_and_serial->serial));
  496. else
  497. return (NULL);
  498. }
  499. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  500. {
  501. int i;
  502. PKCS7_ENC_CONTENT *ec;
  503. i = OBJ_obj2nid(p7->type);
  504. switch (i) {
  505. case NID_pkcs7_signedAndEnveloped:
  506. ec = p7->d.signed_and_enveloped->enc_data;
  507. break;
  508. case NID_pkcs7_enveloped:
  509. ec = p7->d.enveloped->enc_data;
  510. break;
  511. default:
  512. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
  513. return (0);
  514. }
  515. /* Check cipher OID exists and has data in it */
  516. i = EVP_CIPHER_type(cipher);
  517. if (i == NID_undef) {
  518. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
  519. PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  520. return (0);
  521. }
  522. ec->cipher = cipher;
  523. return 1;
  524. }