pk7_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. {
  70. case PKCS7_OP_SET_DETACHED_SIGNATURE:
  71. if (nid == NID_pkcs7_signed)
  72. {
  73. ret=p7->detached=(int)larg;
  74. if (ret && PKCS7_type_is_data(p7->d.sign->contents))
  75. {
  76. ASN1_OCTET_STRING *os;
  77. os=p7->d.sign->contents->d.data;
  78. ASN1_OCTET_STRING_free(os);
  79. p7->d.sign->contents->d.data = NULL;
  80. }
  81. }
  82. else
  83. {
  84. PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  85. ret=0;
  86. }
  87. break;
  88. case PKCS7_OP_GET_DETACHED_SIGNATURE:
  89. if (nid == NID_pkcs7_signed)
  90. {
  91. if(!p7->d.sign || !p7->d.sign->contents->d.ptr)
  92. ret = 1;
  93. else ret = 0;
  94. p7->detached = ret;
  95. }
  96. else
  97. {
  98. PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  99. ret=0;
  100. }
  101. break;
  102. default:
  103. PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_UNKNOWN_OPERATION);
  104. ret=0;
  105. }
  106. return(ret);
  107. }
  108. int PKCS7_content_new(PKCS7 *p7, int type)
  109. {
  110. PKCS7 *ret=NULL;
  111. if ((ret=PKCS7_new()) == NULL) goto err;
  112. if (!PKCS7_set_type(ret,type)) goto err;
  113. if (!PKCS7_set_content(p7,ret)) goto err;
  114. return(1);
  115. err:
  116. if (ret != NULL) 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. {
  125. case NID_pkcs7_signed:
  126. if (p7->d.sign->contents != NULL)
  127. PKCS7_free(p7->d.sign->contents);
  128. p7->d.sign->contents=p7_data;
  129. break;
  130. case NID_pkcs7_digest:
  131. if (p7->d.digest->contents != NULL)
  132. PKCS7_free(p7->d.digest->contents);
  133. p7->d.digest->contents=p7_data;
  134. break;
  135. case NID_pkcs7_data:
  136. case NID_pkcs7_enveloped:
  137. case NID_pkcs7_signedAndEnveloped:
  138. case NID_pkcs7_encrypted:
  139. default:
  140. PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  141. goto err;
  142. }
  143. return(1);
  144. err:
  145. return(0);
  146. }
  147. int PKCS7_set_type(PKCS7 *p7, int type)
  148. {
  149. ASN1_OBJECT *obj;
  150. /*PKCS7_content_free(p7);*/
  151. obj=OBJ_nid2obj(type); /* will not fail */
  152. switch (type)
  153. {
  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. {
  160. PKCS7_SIGNED_free(p7->d.sign);
  161. p7->d.sign=NULL;
  162. goto err;
  163. }
  164. break;
  165. case NID_pkcs7_data:
  166. p7->type=obj;
  167. if ((p7->d.data=M_ASN1_OCTET_STRING_new()) == NULL)
  168. goto err;
  169. break;
  170. case NID_pkcs7_signedAndEnveloped:
  171. p7->type=obj;
  172. if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new())
  173. == NULL) 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) goto err;
  184. if (!ASN1_INTEGER_set(p7->d.enveloped->version,0))
  185. goto err;
  186. p7->d.enveloped->enc_data->content_type
  187. = 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) goto err;
  193. if (!ASN1_INTEGER_set(p7->d.encrypted->version,0))
  194. goto err;
  195. p7->d.encrypted->enc_data->content_type
  196. = 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) 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. {
  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. {
  245. alg=sk_X509_ALGOR_value(md_sk,i);
  246. if (OBJ_obj2nid(alg->algorithm) == nid)
  247. {
  248. j=1;
  249. break;
  250. }
  251. }
  252. if (!j) /* we need to add another algorithm */
  253. {
  254. if(!(alg=X509_ALGOR_new())
  255. || !(alg->parameter = ASN1_TYPE_new()))
  256. {
  257. X509_ALGOR_free(alg);
  258. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE);
  259. return(0);
  260. }
  261. alg->algorithm=OBJ_nid2obj(nid);
  262. alg->parameter->type = V_ASN1_NULL;
  263. if (!sk_X509_ALGOR_push(md_sk,alg))
  264. {
  265. X509_ALGOR_free(alg);
  266. return 0;
  267. }
  268. }
  269. if (!sk_PKCS7_SIGNER_INFO_push(signer_sk,psi))
  270. return 0;
  271. return(1);
  272. }
  273. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
  274. {
  275. int i;
  276. STACK_OF(X509) **sk;
  277. i=OBJ_obj2nid(p7->type);
  278. switch (i)
  279. {
  280. case NID_pkcs7_signed:
  281. sk= &(p7->d.sign->cert);
  282. break;
  283. case NID_pkcs7_signedAndEnveloped:
  284. sk= &(p7->d.signed_and_enveloped->cert);
  285. break;
  286. default:
  287. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE);
  288. return(0);
  289. }
  290. if (*sk == NULL)
  291. *sk=sk_X509_new_null();
  292. if (*sk == NULL)
  293. {
  294. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  295. return 0;
  296. }
  297. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  298. if (!sk_X509_push(*sk,x509))
  299. {
  300. X509_free(x509);
  301. return 0;
  302. }
  303. return(1);
  304. }
  305. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
  306. {
  307. int i;
  308. STACK_OF(X509_CRL) **sk;
  309. i=OBJ_obj2nid(p7->type);
  310. switch (i)
  311. {
  312. case NID_pkcs7_signed:
  313. sk= &(p7->d.sign->crl);
  314. break;
  315. case NID_pkcs7_signedAndEnveloped:
  316. sk= &(p7->d.signed_and_enveloped->crl);
  317. break;
  318. default:
  319. PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE);
  320. return(0);
  321. }
  322. if (*sk == NULL)
  323. *sk=sk_X509_CRL_new_null();
  324. if (*sk == NULL)
  325. {
  326. PKCS7err(PKCS7_F_PKCS7_ADD_CRL,ERR_R_MALLOC_FAILURE);
  327. return 0;
  328. }
  329. CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL);
  330. if (!sk_X509_CRL_push(*sk,crl))
  331. {
  332. X509_CRL_free(crl);
  333. return 0;
  334. }
  335. return(1);
  336. }
  337. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  338. const EVP_MD *dgst)
  339. {
  340. int ret;
  341. /* We now need to add another PKCS7_SIGNER_INFO entry */
  342. if (!ASN1_INTEGER_set(p7i->version,1))
  343. goto err;
  344. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  345. X509_get_issuer_name(x509)))
  346. goto err;
  347. /* because ASN1_INTEGER_set is used to set a 'long' we will do
  348. * things the ugly way. */
  349. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  350. if (!(p7i->issuer_and_serial->serial=
  351. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  352. goto err;
  353. /* lets keep the pkey around for a while */
  354. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  355. p7i->pkey=pkey;
  356. /* Set the algorithms */
  357. X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
  358. V_ASN1_NULL, NULL);
  359. if (pkey->ameth && pkey->ameth->pkey_ctrl)
  360. {
  361. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN,
  362. 0, p7i);
  363. if (ret > 0)
  364. return 1;
  365. if (ret != -2)
  366. {
  367. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  368. PKCS7_R_SIGNING_CTRL_FAILURE);
  369. return 0;
  370. }
  371. }
  372. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  373. PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  374. err:
  375. return 0;
  376. }
  377. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  378. const EVP_MD *dgst)
  379. {
  380. PKCS7_SIGNER_INFO *si = NULL;
  381. if (dgst == NULL)
  382. {
  383. int def_nid;
  384. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
  385. goto err;
  386. dgst = EVP_get_digestbynid(def_nid);
  387. if (dgst == NULL)
  388. {
  389. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE,
  390. PKCS7_R_NO_DEFAULT_DIGEST);
  391. goto err;
  392. }
  393. }
  394. if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err;
  395. if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err;
  396. if (!PKCS7_add_signer(p7,si)) goto err;
  397. return(si);
  398. err:
  399. if (si)
  400. PKCS7_SIGNER_INFO_free(si);
  401. return(NULL);
  402. }
  403. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
  404. {
  405. if (PKCS7_type_is_digest(p7))
  406. {
  407. if(!(p7->d.digest->md->parameter = ASN1_TYPE_new()))
  408. {
  409. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,ERR_R_MALLOC_FAILURE);
  410. return 0;
  411. }
  412. p7->d.digest->md->parameter->type = V_ASN1_NULL;
  413. p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  414. return 1;
  415. }
  416. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,PKCS7_R_WRONG_CONTENT_TYPE);
  417. return 1;
  418. }
  419. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  420. {
  421. if (PKCS7_type_is_signed(p7))
  422. {
  423. return(p7->d.sign->signer_info);
  424. }
  425. else if (PKCS7_type_is_signedAndEnveloped(p7))
  426. {
  427. return(p7->d.signed_and_enveloped->signer_info);
  428. }
  429. else
  430. return(NULL);
  431. }
  432. void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
  433. X509_ALGOR **pdig, X509_ALGOR **psig)
  434. {
  435. if (pk)
  436. *pk = si->pkey;
  437. if (pdig)
  438. *pdig = si->digest_alg;
  439. if (psig)
  440. *psig = si->digest_enc_alg;
  441. }
  442. void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
  443. {
  444. if (penc)
  445. *penc = ri->key_enc_algor;
  446. }
  447. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  448. {
  449. PKCS7_RECIP_INFO *ri;
  450. if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err;
  451. if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err;
  452. if (!PKCS7_add_recipient_info(p7,ri)) goto err;
  453. return ri;
  454. err:
  455. if (ri)
  456. PKCS7_RECIP_INFO_free(ri);
  457. return NULL;
  458. }
  459. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  460. {
  461. int i;
  462. STACK_OF(PKCS7_RECIP_INFO) *sk;
  463. i=OBJ_obj2nid(p7->type);
  464. switch (i)
  465. {
  466. case NID_pkcs7_signedAndEnveloped:
  467. sk= p7->d.signed_and_enveloped->recipientinfo;
  468. break;
  469. case NID_pkcs7_enveloped:
  470. sk= p7->d.enveloped->recipientinfo;
  471. break;
  472. default:
  473. PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE);
  474. return(0);
  475. }
  476. if (!sk_PKCS7_RECIP_INFO_push(sk,ri))
  477. return 0;
  478. return(1);
  479. }
  480. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  481. {
  482. int ret;
  483. EVP_PKEY *pkey = NULL;
  484. if (!ASN1_INTEGER_set(p7i->version,0))
  485. return 0;
  486. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  487. X509_get_issuer_name(x509)))
  488. return 0;
  489. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  490. if (!(p7i->issuer_and_serial->serial=
  491. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  492. return 0;
  493. pkey = X509_get_pubkey(x509);
  494. if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl)
  495. {
  496. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  497. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  498. goto err;
  499. }
  500. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT,
  501. 0, p7i);
  502. if (ret == -2)
  503. {
  504. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  505. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  506. goto err;
  507. }
  508. if (ret <= 0)
  509. {
  510. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  511. PKCS7_R_ENCRYPTION_CTRL_FAILURE);
  512. goto err;
  513. }
  514. EVP_PKEY_free(pkey);
  515. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  516. p7i->cert=x509;
  517. return 1;
  518. err:
  519. if (pkey)
  520. EVP_PKEY_free(pkey);
  521. return 0;
  522. }
  523. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  524. {
  525. if (PKCS7_type_is_signed(p7))
  526. return(X509_find_by_issuer_and_serial(p7->d.sign->cert,
  527. si->issuer_and_serial->issuer,
  528. si->issuer_and_serial->serial));
  529. else
  530. return(NULL);
  531. }
  532. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  533. {
  534. int i;
  535. ASN1_OBJECT *objtmp;
  536. PKCS7_ENC_CONTENT *ec;
  537. i=OBJ_obj2nid(p7->type);
  538. switch (i)
  539. {
  540. case NID_pkcs7_signedAndEnveloped:
  541. ec=p7->d.signed_and_enveloped->enc_data;
  542. break;
  543. case NID_pkcs7_enveloped:
  544. ec=p7->d.enveloped->enc_data;
  545. break;
  546. default:
  547. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE);
  548. return(0);
  549. }
  550. /* Check cipher OID exists and has data in it*/
  551. i = EVP_CIPHER_type(cipher);
  552. if(i == NID_undef) {
  553. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  554. return(0);
  555. }
  556. objtmp = OBJ_nid2obj(i);
  557. ec->cipher = cipher;
  558. return 1;
  559. }
  560. int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
  561. {
  562. ASN1_OCTET_STRING *os = NULL;
  563. switch (OBJ_obj2nid(p7->type))
  564. {
  565. case NID_pkcs7_data:
  566. os = p7->d.data;
  567. break;
  568. case NID_pkcs7_signedAndEnveloped:
  569. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  570. if (os == NULL)
  571. {
  572. os=M_ASN1_OCTET_STRING_new();
  573. p7->d.signed_and_enveloped->enc_data->enc_data=os;
  574. }
  575. break;
  576. case NID_pkcs7_enveloped:
  577. os = p7->d.enveloped->enc_data->enc_data;
  578. if (os == NULL)
  579. {
  580. os=M_ASN1_OCTET_STRING_new();
  581. p7->d.enveloped->enc_data->enc_data=os;
  582. }
  583. break;
  584. case NID_pkcs7_signed:
  585. os=p7->d.sign->contents->d.data;
  586. break;
  587. default:
  588. os = NULL;
  589. break;
  590. }
  591. if (os == NULL)
  592. return 0;
  593. os->flags |= ASN1_STRING_FLAG_NDEF;
  594. *boundary = &os->data;
  595. return 1;
  596. }