pk7_lib.c 18 KB

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