pk7_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. #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. ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1);
  175. if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
  176. goto err;
  177. p7->d.signed_and_enveloped->enc_data->content_type
  178. = OBJ_nid2obj(NID_pkcs7_data);
  179. break;
  180. case NID_pkcs7_enveloped:
  181. p7->type = obj;
  182. if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
  183. == NULL)
  184. goto err;
  185. if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
  186. goto err;
  187. p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  188. break;
  189. case NID_pkcs7_encrypted:
  190. p7->type = obj;
  191. if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
  192. == NULL)
  193. goto err;
  194. if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
  195. goto err;
  196. p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  197. break;
  198. case NID_pkcs7_digest:
  199. p7->type = obj;
  200. if ((p7->d.digest = PKCS7_DIGEST_new())
  201. == NULL)
  202. goto err;
  203. if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
  204. goto err;
  205. break;
  206. default:
  207. PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  208. goto err;
  209. }
  210. return (1);
  211. err:
  212. return (0);
  213. }
  214. int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
  215. {
  216. p7->type = OBJ_nid2obj(type);
  217. p7->d.other = other;
  218. return 1;
  219. }
  220. int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
  221. {
  222. int i, j, nid;
  223. X509_ALGOR *alg;
  224. STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
  225. STACK_OF(X509_ALGOR) *md_sk;
  226. i = OBJ_obj2nid(p7->type);
  227. switch (i) {
  228. case NID_pkcs7_signed:
  229. signer_sk = p7->d.sign->signer_info;
  230. md_sk = p7->d.sign->md_algs;
  231. break;
  232. case NID_pkcs7_signedAndEnveloped:
  233. signer_sk = p7->d.signed_and_enveloped->signer_info;
  234. md_sk = p7->d.signed_and_enveloped->md_algs;
  235. break;
  236. default:
  237. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
  238. return (0);
  239. }
  240. nid = OBJ_obj2nid(psi->digest_alg->algorithm);
  241. /* If the digest is not currently listed, add it */
  242. j = 0;
  243. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  244. alg = sk_X509_ALGOR_value(md_sk, i);
  245. if (OBJ_obj2nid(alg->algorithm) == nid) {
  246. j = 1;
  247. break;
  248. }
  249. }
  250. if (!j) { /* we need to add another algorithm */
  251. if (!(alg = X509_ALGOR_new())
  252. || !(alg->parameter = ASN1_TYPE_new())) {
  253. X509_ALGOR_free(alg);
  254. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
  255. return (0);
  256. }
  257. alg->algorithm = OBJ_nid2obj(nid);
  258. alg->parameter->type = V_ASN1_NULL;
  259. if (!sk_X509_ALGOR_push(md_sk, alg)) {
  260. X509_ALGOR_free(alg);
  261. return 0;
  262. }
  263. }
  264. if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
  265. return 0;
  266. return (1);
  267. }
  268. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
  269. {
  270. int i;
  271. STACK_OF(X509) **sk;
  272. i = OBJ_obj2nid(p7->type);
  273. switch (i) {
  274. case NID_pkcs7_signed:
  275. sk = &(p7->d.sign->cert);
  276. break;
  277. case NID_pkcs7_signedAndEnveloped:
  278. sk = &(p7->d.signed_and_enveloped->cert);
  279. break;
  280. default:
  281. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE);
  282. return (0);
  283. }
  284. if (*sk == NULL)
  285. *sk = sk_X509_new_null();
  286. if (*sk == NULL) {
  287. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  288. return 0;
  289. }
  290. CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
  291. if (!sk_X509_push(*sk, x509)) {
  292. X509_free(x509);
  293. return 0;
  294. }
  295. return (1);
  296. }
  297. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
  298. {
  299. int i;
  300. STACK_OF(X509_CRL) **sk;
  301. i = OBJ_obj2nid(p7->type);
  302. switch (i) {
  303. case NID_pkcs7_signed:
  304. sk = &(p7->d.sign->crl);
  305. break;
  306. case NID_pkcs7_signedAndEnveloped:
  307. sk = &(p7->d.signed_and_enveloped->crl);
  308. break;
  309. default:
  310. PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
  311. return (0);
  312. }
  313. if (*sk == NULL)
  314. *sk = sk_X509_CRL_new_null();
  315. if (*sk == NULL) {
  316. PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
  317. return 0;
  318. }
  319. CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
  320. if (!sk_X509_CRL_push(*sk, crl)) {
  321. X509_CRL_free(crl);
  322. return 0;
  323. }
  324. return (1);
  325. }
  326. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  327. const EVP_MD *dgst)
  328. {
  329. int ret;
  330. /* We now need to add another PKCS7_SIGNER_INFO entry */
  331. if (!ASN1_INTEGER_set(p7i->version, 1))
  332. goto err;
  333. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  334. X509_get_issuer_name(x509)))
  335. goto err;
  336. /*
  337. * because ASN1_INTEGER_set is used to set a 'long' we will do things the
  338. * ugly way.
  339. */
  340. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  341. if (!(p7i->issuer_and_serial->serial =
  342. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  343. goto err;
  344. /* lets keep the pkey around for a while */
  345. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  346. p7i->pkey = pkey;
  347. /* Set the algorithms */
  348. X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
  349. V_ASN1_NULL, NULL);
  350. if (pkey->ameth && pkey->ameth->pkey_ctrl) {
  351. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
  352. if (ret > 0)
  353. return 1;
  354. if (ret != -2) {
  355. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  356. PKCS7_R_SIGNING_CTRL_FAILURE);
  357. return 0;
  358. }
  359. }
  360. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  361. PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  362. err:
  363. return 0;
  364. }
  365. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  366. const EVP_MD *dgst)
  367. {
  368. PKCS7_SIGNER_INFO *si = NULL;
  369. if (dgst == NULL) {
  370. int def_nid;
  371. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
  372. goto err;
  373. dgst = EVP_get_digestbynid(def_nid);
  374. if (dgst == NULL) {
  375. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, PKCS7_R_NO_DEFAULT_DIGEST);
  376. goto err;
  377. }
  378. }
  379. if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
  380. goto err;
  381. if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
  382. goto err;
  383. if (!PKCS7_add_signer(p7, si))
  384. goto err;
  385. return (si);
  386. err:
  387. if (si)
  388. PKCS7_SIGNER_INFO_free(si);
  389. return (NULL);
  390. }
  391. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
  392. {
  393. if (PKCS7_type_is_digest(p7)) {
  394. if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
  395. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
  396. return 0;
  397. }
  398. p7->d.digest->md->parameter->type = V_ASN1_NULL;
  399. p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  400. return 1;
  401. }
  402. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
  403. return 1;
  404. }
  405. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  406. {
  407. if (p7 == NULL || p7->d.ptr == NULL)
  408. return NULL;
  409. if (PKCS7_type_is_signed(p7)) {
  410. return (p7->d.sign->signer_info);
  411. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  412. return (p7->d.signed_and_enveloped->signer_info);
  413. } else
  414. return (NULL);
  415. }
  416. void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
  417. X509_ALGOR **pdig, X509_ALGOR **psig)
  418. {
  419. if (pk)
  420. *pk = si->pkey;
  421. if (pdig)
  422. *pdig = si->digest_alg;
  423. if (psig)
  424. *psig = si->digest_enc_alg;
  425. }
  426. void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
  427. {
  428. if (penc)
  429. *penc = ri->key_enc_algor;
  430. }
  431. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  432. {
  433. PKCS7_RECIP_INFO *ri;
  434. if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
  435. goto err;
  436. if (!PKCS7_RECIP_INFO_set(ri, x509))
  437. goto err;
  438. if (!PKCS7_add_recipient_info(p7, ri))
  439. goto err;
  440. return ri;
  441. err:
  442. if (ri)
  443. PKCS7_RECIP_INFO_free(ri);
  444. return NULL;
  445. }
  446. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  447. {
  448. int i;
  449. STACK_OF(PKCS7_RECIP_INFO) *sk;
  450. i = OBJ_obj2nid(p7->type);
  451. switch (i) {
  452. case NID_pkcs7_signedAndEnveloped:
  453. sk = p7->d.signed_and_enveloped->recipientinfo;
  454. break;
  455. case NID_pkcs7_enveloped:
  456. sk = p7->d.enveloped->recipientinfo;
  457. break;
  458. default:
  459. PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
  460. PKCS7_R_WRONG_CONTENT_TYPE);
  461. return (0);
  462. }
  463. if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
  464. return 0;
  465. return (1);
  466. }
  467. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  468. {
  469. int ret;
  470. EVP_PKEY *pkey = NULL;
  471. if (!ASN1_INTEGER_set(p7i->version, 0))
  472. return 0;
  473. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  474. X509_get_issuer_name(x509)))
  475. return 0;
  476. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  477. if (!(p7i->issuer_and_serial->serial =
  478. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  479. return 0;
  480. pkey = X509_get_pubkey(x509);
  481. if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
  482. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  483. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  484. goto err;
  485. }
  486. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
  487. if (ret == -2) {
  488. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  489. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  490. goto err;
  491. }
  492. if (ret <= 0) {
  493. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  494. PKCS7_R_ENCRYPTION_CTRL_FAILURE);
  495. goto err;
  496. }
  497. EVP_PKEY_free(pkey);
  498. CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
  499. p7i->cert = x509;
  500. return 1;
  501. err:
  502. if (pkey)
  503. EVP_PKEY_free(pkey);
  504. return 0;
  505. }
  506. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  507. {
  508. if (PKCS7_type_is_signed(p7))
  509. return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
  510. si->issuer_and_serial->issuer,
  511. si->
  512. issuer_and_serial->serial));
  513. else
  514. return (NULL);
  515. }
  516. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  517. {
  518. int i;
  519. PKCS7_ENC_CONTENT *ec;
  520. i = OBJ_obj2nid(p7->type);
  521. switch (i) {
  522. case NID_pkcs7_signedAndEnveloped:
  523. ec = p7->d.signed_and_enveloped->enc_data;
  524. break;
  525. case NID_pkcs7_enveloped:
  526. ec = p7->d.enveloped->enc_data;
  527. break;
  528. default:
  529. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
  530. return (0);
  531. }
  532. /* Check cipher OID exists and has data in it */
  533. i = EVP_CIPHER_type(cipher);
  534. if (i == NID_undef) {
  535. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
  536. PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  537. return (0);
  538. }
  539. ec->cipher = cipher;
  540. return 1;
  541. }
  542. int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
  543. {
  544. ASN1_OCTET_STRING *os = NULL;
  545. switch (OBJ_obj2nid(p7->type)) {
  546. case NID_pkcs7_data:
  547. os = p7->d.data;
  548. break;
  549. case NID_pkcs7_signedAndEnveloped:
  550. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  551. if (os == NULL) {
  552. os = M_ASN1_OCTET_STRING_new();
  553. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  554. }
  555. break;
  556. case NID_pkcs7_enveloped:
  557. os = p7->d.enveloped->enc_data->enc_data;
  558. if (os == NULL) {
  559. os = M_ASN1_OCTET_STRING_new();
  560. p7->d.enveloped->enc_data->enc_data = os;
  561. }
  562. break;
  563. case NID_pkcs7_signed:
  564. os = p7->d.sign->contents->d.data;
  565. break;
  566. default:
  567. os = NULL;
  568. break;
  569. }
  570. if (os == NULL)
  571. return 0;
  572. os->flags |= ASN1_STRING_FLAG_NDEF;
  573. *boundary = &os->data;
  574. return 1;
  575. }