pk7_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* crypto/pkcs7/pk7_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. #include "asn1_locl.h"
  63. long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
  64. {
  65. int nid;
  66. long ret;
  67. nid = OBJ_obj2nid(p7->type);
  68. switch (cmd) {
  69. /* NOTE(emilia): does not support detached digested data. */
  70. case PKCS7_OP_SET_DETACHED_SIGNATURE:
  71. if (nid == NID_pkcs7_signed) {
  72. ret = p7->detached = (int)larg;
  73. if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
  74. ASN1_OCTET_STRING *os;
  75. os = p7->d.sign->contents->d.data;
  76. ASN1_OCTET_STRING_free(os);
  77. p7->d.sign->contents->d.data = NULL;
  78. }
  79. } else {
  80. PKCS7err(PKCS7_F_PKCS7_CTRL,
  81. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  82. ret = 0;
  83. }
  84. break;
  85. case PKCS7_OP_GET_DETACHED_SIGNATURE:
  86. if (nid == NID_pkcs7_signed) {
  87. if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
  88. ret = 1;
  89. else
  90. ret = 0;
  91. p7->detached = ret;
  92. } else {
  93. PKCS7err(PKCS7_F_PKCS7_CTRL,
  94. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  95. ret = 0;
  96. }
  97. break;
  98. default:
  99. PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
  100. ret = 0;
  101. }
  102. return (ret);
  103. }
  104. int PKCS7_content_new(PKCS7 *p7, int type)
  105. {
  106. PKCS7 *ret = NULL;
  107. if ((ret = PKCS7_new()) == NULL)
  108. goto err;
  109. if (!PKCS7_set_type(ret, type))
  110. goto err;
  111. if (!PKCS7_set_content(p7, ret))
  112. goto err;
  113. return (1);
  114. err:
  115. if (ret != NULL)
  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. if (p7->d.sign->contents != NULL)
  126. PKCS7_free(p7->d.sign->contents);
  127. p7->d.sign->contents = p7_data;
  128. break;
  129. case NID_pkcs7_digest:
  130. if (p7->d.digest->contents != NULL)
  131. PKCS7_free(p7->d.digest->contents);
  132. p7->d.digest->contents = p7_data;
  133. break;
  134. case NID_pkcs7_data:
  135. case NID_pkcs7_enveloped:
  136. case NID_pkcs7_signedAndEnveloped:
  137. case NID_pkcs7_encrypted:
  138. default:
  139. PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  140. goto err;
  141. }
  142. return (1);
  143. err:
  144. return (0);
  145. }
  146. int PKCS7_set_type(PKCS7 *p7, int type)
  147. {
  148. ASN1_OBJECT *obj;
  149. /*
  150. * PKCS7_content_free(p7);
  151. */
  152. obj = OBJ_nid2obj(type); /* will not fail */
  153. switch (type) {
  154. case NID_pkcs7_signed:
  155. p7->type = obj;
  156. if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
  157. goto err;
  158. if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
  159. PKCS7_SIGNED_free(p7->d.sign);
  160. p7->d.sign = NULL;
  161. goto err;
  162. }
  163. break;
  164. case NID_pkcs7_data:
  165. p7->type = obj;
  166. if ((p7->d.data = M_ASN1_OCTET_STRING_new()) == NULL)
  167. goto err;
  168. break;
  169. case NID_pkcs7_signedAndEnveloped:
  170. p7->type = obj;
  171. if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
  172. == NULL)
  173. goto err;
  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 ret;
  329. /* We now need to add another PKCS7_SIGNER_INFO entry */
  330. if (!ASN1_INTEGER_set(p7i->version, 1))
  331. goto err;
  332. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  333. X509_get_issuer_name(x509)))
  334. goto err;
  335. /*
  336. * because ASN1_INTEGER_set is used to set a 'long' we will do things the
  337. * ugly way.
  338. */
  339. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  340. if (!(p7i->issuer_and_serial->serial =
  341. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  342. goto err;
  343. /* lets keep the pkey around for a while */
  344. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  345. p7i->pkey = pkey;
  346. /* Set the algorithms */
  347. X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
  348. V_ASN1_NULL, NULL);
  349. if (pkey->ameth && pkey->ameth->pkey_ctrl) {
  350. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
  351. if (ret > 0)
  352. return 1;
  353. if (ret != -2) {
  354. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  355. PKCS7_R_SIGNING_CTRL_FAILURE);
  356. return 0;
  357. }
  358. }
  359. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  360. PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  361. err:
  362. return 0;
  363. }
  364. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  365. const EVP_MD *dgst)
  366. {
  367. PKCS7_SIGNER_INFO *si = NULL;
  368. if (dgst == NULL) {
  369. int def_nid;
  370. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
  371. goto err;
  372. dgst = EVP_get_digestbynid(def_nid);
  373. if (dgst == NULL) {
  374. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, PKCS7_R_NO_DEFAULT_DIGEST);
  375. goto err;
  376. }
  377. }
  378. if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
  379. goto err;
  380. if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
  381. goto err;
  382. if (!PKCS7_add_signer(p7, si))
  383. goto err;
  384. return (si);
  385. err:
  386. if (si)
  387. PKCS7_SIGNER_INFO_free(si);
  388. return (NULL);
  389. }
  390. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
  391. {
  392. if (PKCS7_type_is_digest(p7)) {
  393. if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
  394. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
  395. return 0;
  396. }
  397. p7->d.digest->md->parameter->type = V_ASN1_NULL;
  398. p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  399. return 1;
  400. }
  401. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
  402. return 1;
  403. }
  404. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  405. {
  406. if (p7 == NULL || p7->d.ptr == NULL)
  407. return NULL;
  408. if (PKCS7_type_is_signed(p7)) {
  409. return (p7->d.sign->signer_info);
  410. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  411. return (p7->d.signed_and_enveloped->signer_info);
  412. } else
  413. return (NULL);
  414. }
  415. void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
  416. X509_ALGOR **pdig, X509_ALGOR **psig)
  417. {
  418. if (pk)
  419. *pk = si->pkey;
  420. if (pdig)
  421. *pdig = si->digest_alg;
  422. if (psig)
  423. *psig = si->digest_enc_alg;
  424. }
  425. void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
  426. {
  427. if (penc)
  428. *penc = ri->key_enc_algor;
  429. }
  430. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  431. {
  432. PKCS7_RECIP_INFO *ri;
  433. if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
  434. goto err;
  435. if (!PKCS7_RECIP_INFO_set(ri, x509))
  436. goto err;
  437. if (!PKCS7_add_recipient_info(p7, ri))
  438. goto err;
  439. return ri;
  440. err:
  441. if (ri)
  442. PKCS7_RECIP_INFO_free(ri);
  443. return NULL;
  444. }
  445. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  446. {
  447. int i;
  448. STACK_OF(PKCS7_RECIP_INFO) *sk;
  449. i = OBJ_obj2nid(p7->type);
  450. switch (i) {
  451. case NID_pkcs7_signedAndEnveloped:
  452. sk = p7->d.signed_and_enveloped->recipientinfo;
  453. break;
  454. case NID_pkcs7_enveloped:
  455. sk = p7->d.enveloped->recipientinfo;
  456. break;
  457. default:
  458. PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
  459. PKCS7_R_WRONG_CONTENT_TYPE);
  460. return (0);
  461. }
  462. if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
  463. return 0;
  464. return (1);
  465. }
  466. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  467. {
  468. int ret;
  469. EVP_PKEY *pkey = NULL;
  470. if (!ASN1_INTEGER_set(p7i->version, 0))
  471. return 0;
  472. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  473. X509_get_issuer_name(x509)))
  474. return 0;
  475. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  476. if (!(p7i->issuer_and_serial->serial =
  477. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  478. return 0;
  479. pkey = X509_get_pubkey(x509);
  480. if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
  481. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  482. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  483. goto err;
  484. }
  485. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
  486. if (ret == -2) {
  487. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  488. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  489. goto err;
  490. }
  491. if (ret <= 0) {
  492. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  493. PKCS7_R_ENCRYPTION_CTRL_FAILURE);
  494. goto err;
  495. }
  496. EVP_PKEY_free(pkey);
  497. CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
  498. p7i->cert = x509;
  499. return 1;
  500. err:
  501. if (pkey)
  502. EVP_PKEY_free(pkey);
  503. return 0;
  504. }
  505. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  506. {
  507. if (PKCS7_type_is_signed(p7))
  508. return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
  509. si->issuer_and_serial->issuer,
  510. si->
  511. issuer_and_serial->serial));
  512. else
  513. return (NULL);
  514. }
  515. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  516. {
  517. int i;
  518. PKCS7_ENC_CONTENT *ec;
  519. i = OBJ_obj2nid(p7->type);
  520. switch (i) {
  521. case NID_pkcs7_signedAndEnveloped:
  522. ec = p7->d.signed_and_enveloped->enc_data;
  523. break;
  524. case NID_pkcs7_enveloped:
  525. ec = p7->d.enveloped->enc_data;
  526. break;
  527. default:
  528. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
  529. return (0);
  530. }
  531. /* Check cipher OID exists and has data in it */
  532. i = EVP_CIPHER_type(cipher);
  533. if (i == NID_undef) {
  534. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
  535. PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  536. return (0);
  537. }
  538. ec->cipher = cipher;
  539. return 1;
  540. }
  541. int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
  542. {
  543. ASN1_OCTET_STRING *os = NULL;
  544. switch (OBJ_obj2nid(p7->type)) {
  545. case NID_pkcs7_data:
  546. os = p7->d.data;
  547. break;
  548. case NID_pkcs7_signedAndEnveloped:
  549. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  550. if (os == NULL) {
  551. os = M_ASN1_OCTET_STRING_new();
  552. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  553. }
  554. break;
  555. case NID_pkcs7_enveloped:
  556. os = p7->d.enveloped->enc_data->enc_data;
  557. if (os == NULL) {
  558. os = M_ASN1_OCTET_STRING_new();
  559. p7->d.enveloped->enc_data->enc_data = os;
  560. }
  561. break;
  562. case NID_pkcs7_signed:
  563. os = p7->d.sign->contents->d.data;
  564. break;
  565. default:
  566. os = NULL;
  567. break;
  568. }
  569. if (os == NULL)
  570. return 0;
  571. os->flags |= ASN1_STRING_FLAG_NDEF;
  572. *boundary = &os->data;
  573. return 1;
  574. }