pk7_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. {
  69. case PKCS7_OP_SET_DETACHED_SIGNATURE:
  70. if (nid == NID_pkcs7_signed)
  71. {
  72. ret=p7->detached=(int)larg;
  73. }
  74. else
  75. {
  76. PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  77. ret=0;
  78. }
  79. break;
  80. case PKCS7_OP_GET_DETACHED_SIGNATURE:
  81. if (nid == NID_pkcs7_signed)
  82. {
  83. if(!p7->d.sign || !p7->d.sign->contents->d.ptr)
  84. ret = 1;
  85. else ret = 0;
  86. p7->detached = ret;
  87. }
  88. else
  89. {
  90. PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  91. ret=0;
  92. }
  93. break;
  94. default:
  95. PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_UNKNOWN_OPERATION);
  96. ret=0;
  97. }
  98. return(ret);
  99. }
  100. int PKCS7_content_new(PKCS7 *p7, int type)
  101. {
  102. PKCS7 *ret=NULL;
  103. if ((ret=PKCS7_new()) == NULL) goto err;
  104. if (!PKCS7_set_type(ret,type)) goto err;
  105. if (!PKCS7_set_content(p7,ret)) goto err;
  106. return(1);
  107. err:
  108. if (ret != NULL) PKCS7_free(ret);
  109. return(0);
  110. }
  111. int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
  112. {
  113. int i;
  114. i=OBJ_obj2nid(p7->type);
  115. switch (i)
  116. {
  117. case NID_pkcs7_signed:
  118. if (p7->d.sign->contents != NULL)
  119. PKCS7_free(p7->d.sign->contents);
  120. p7->d.sign->contents=p7_data;
  121. break;
  122. case NID_pkcs7_digest:
  123. case NID_pkcs7_data:
  124. case NID_pkcs7_enveloped:
  125. case NID_pkcs7_signedAndEnveloped:
  126. case NID_pkcs7_encrypted:
  127. default:
  128. PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  129. goto err;
  130. }
  131. return(1);
  132. err:
  133. return(0);
  134. }
  135. int PKCS7_set_type(PKCS7 *p7, int type)
  136. {
  137. ASN1_OBJECT *obj;
  138. /*PKCS7_content_free(p7);*/
  139. obj=OBJ_nid2obj(type); /* will not fail */
  140. switch (type)
  141. {
  142. case NID_pkcs7_signed:
  143. p7->type=obj;
  144. if ((p7->d.sign=PKCS7_SIGNED_new()) == NULL)
  145. goto err;
  146. ASN1_INTEGER_set(p7->d.sign->version,1);
  147. break;
  148. case NID_pkcs7_data:
  149. p7->type=obj;
  150. if ((p7->d.data=M_ASN1_OCTET_STRING_new()) == NULL)
  151. goto err;
  152. break;
  153. case NID_pkcs7_signedAndEnveloped:
  154. p7->type=obj;
  155. if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new())
  156. == NULL) goto err;
  157. ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1);
  158. p7->d.signed_and_enveloped->enc_data->content_type
  159. = OBJ_nid2obj(NID_pkcs7_data);
  160. break;
  161. case NID_pkcs7_enveloped:
  162. p7->type=obj;
  163. if ((p7->d.enveloped=PKCS7_ENVELOPE_new())
  164. == NULL) goto err;
  165. ASN1_INTEGER_set(p7->d.enveloped->version,0);
  166. p7->d.enveloped->enc_data->content_type
  167. = OBJ_nid2obj(NID_pkcs7_data);
  168. break;
  169. case NID_pkcs7_encrypted:
  170. p7->type=obj;
  171. if ((p7->d.encrypted=PKCS7_ENCRYPT_new())
  172. == NULL) goto err;
  173. ASN1_INTEGER_set(p7->d.encrypted->version,0);
  174. p7->d.encrypted->enc_data->content_type
  175. = OBJ_nid2obj(NID_pkcs7_data);
  176. break;
  177. case NID_pkcs7_digest:
  178. default:
  179. PKCS7err(PKCS7_F_PKCS7_SET_TYPE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  180. goto err;
  181. }
  182. return(1);
  183. err:
  184. return(0);
  185. }
  186. int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
  187. {
  188. int i,j,nid;
  189. X509_ALGOR *alg;
  190. STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
  191. STACK_OF(X509_ALGOR) *md_sk;
  192. i=OBJ_obj2nid(p7->type);
  193. switch (i)
  194. {
  195. case NID_pkcs7_signed:
  196. signer_sk= p7->d.sign->signer_info;
  197. md_sk= p7->d.sign->md_algs;
  198. break;
  199. case NID_pkcs7_signedAndEnveloped:
  200. signer_sk= p7->d.signed_and_enveloped->signer_info;
  201. md_sk= p7->d.signed_and_enveloped->md_algs;
  202. break;
  203. default:
  204. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,PKCS7_R_WRONG_CONTENT_TYPE);
  205. return(0);
  206. }
  207. nid=OBJ_obj2nid(psi->digest_alg->algorithm);
  208. /* If the digest is not currently listed, add it */
  209. j=0;
  210. for (i=0; i<sk_X509_ALGOR_num(md_sk); i++)
  211. {
  212. alg=sk_X509_ALGOR_value(md_sk,i);
  213. if (OBJ_obj2nid(alg->algorithm) == nid)
  214. {
  215. j=1;
  216. break;
  217. }
  218. }
  219. if (!j) /* we need to add another algorithm */
  220. {
  221. if(!(alg=X509_ALGOR_new())
  222. || !(alg->parameter = ASN1_TYPE_new())) {
  223. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE);
  224. return(0);
  225. }
  226. alg->algorithm=OBJ_nid2obj(nid);
  227. alg->parameter->type = V_ASN1_NULL;
  228. sk_X509_ALGOR_push(md_sk,alg);
  229. }
  230. sk_PKCS7_SIGNER_INFO_push(signer_sk,psi);
  231. return(1);
  232. }
  233. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
  234. {
  235. int i;
  236. STACK_OF(X509) **sk;
  237. i=OBJ_obj2nid(p7->type);
  238. switch (i)
  239. {
  240. case NID_pkcs7_signed:
  241. sk= &(p7->d.sign->cert);
  242. break;
  243. case NID_pkcs7_signedAndEnveloped:
  244. sk= &(p7->d.signed_and_enveloped->cert);
  245. break;
  246. default:
  247. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE);
  248. return(0);
  249. }
  250. if (*sk == NULL)
  251. *sk=sk_X509_new_null();
  252. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  253. sk_X509_push(*sk,x509);
  254. return(1);
  255. }
  256. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
  257. {
  258. int i;
  259. STACK_OF(X509_CRL) **sk;
  260. i=OBJ_obj2nid(p7->type);
  261. switch (i)
  262. {
  263. case NID_pkcs7_signed:
  264. sk= &(p7->d.sign->crl);
  265. break;
  266. case NID_pkcs7_signedAndEnveloped:
  267. sk= &(p7->d.signed_and_enveloped->crl);
  268. break;
  269. default:
  270. PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE);
  271. return(0);
  272. }
  273. if (*sk == NULL)
  274. *sk=sk_X509_CRL_new_null();
  275. CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL);
  276. sk_X509_CRL_push(*sk,crl);
  277. return(1);
  278. }
  279. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  280. const EVP_MD *dgst)
  281. {
  282. char is_dsa;
  283. if (pkey->type == EVP_PKEY_DSA) is_dsa = 1;
  284. else is_dsa = 0;
  285. /* We now need to add another PKCS7_SIGNER_INFO entry */
  286. ASN1_INTEGER_set(p7i->version,1);
  287. X509_NAME_set(&p7i->issuer_and_serial->issuer,
  288. X509_get_issuer_name(x509));
  289. /* because ASN1_INTEGER_set is used to set a 'long' we will do
  290. * things the ugly way. */
  291. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  292. p7i->issuer_and_serial->serial=
  293. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509));
  294. /* lets keep the pkey around for a while */
  295. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  296. p7i->pkey=pkey;
  297. /* Set the algorithms */
  298. if (is_dsa) p7i->digest_alg->algorithm=OBJ_nid2obj(NID_sha1);
  299. else
  300. p7i->digest_alg->algorithm=OBJ_nid2obj(EVP_MD_type(dgst));
  301. if (p7i->digest_alg->parameter != NULL)
  302. ASN1_TYPE_free(p7i->digest_alg->parameter);
  303. if ((p7i->digest_alg->parameter=ASN1_TYPE_new()) == NULL)
  304. goto err;
  305. p7i->digest_alg->parameter->type=V_ASN1_NULL;
  306. p7i->digest_enc_alg->algorithm=OBJ_nid2obj(EVP_PKEY_type(pkey->type));
  307. if (p7i->digest_enc_alg->parameter != NULL)
  308. ASN1_TYPE_free(p7i->digest_enc_alg->parameter);
  309. if(is_dsa) p7i->digest_enc_alg->parameter = NULL;
  310. else {
  311. if (!(p7i->digest_enc_alg->parameter=ASN1_TYPE_new()))
  312. goto err;
  313. p7i->digest_enc_alg->parameter->type=V_ASN1_NULL;
  314. }
  315. return(1);
  316. err:
  317. return(0);
  318. }
  319. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  320. const EVP_MD *dgst)
  321. {
  322. PKCS7_SIGNER_INFO *si;
  323. if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err;
  324. if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err;
  325. if (!PKCS7_add_signer(p7,si)) goto err;
  326. return(si);
  327. err:
  328. return(NULL);
  329. }
  330. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  331. {
  332. if (PKCS7_type_is_signed(p7))
  333. {
  334. return(p7->d.sign->signer_info);
  335. }
  336. else if (PKCS7_type_is_signedAndEnveloped(p7))
  337. {
  338. return(p7->d.signed_and_enveloped->signer_info);
  339. }
  340. else
  341. return(NULL);
  342. }
  343. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  344. {
  345. PKCS7_RECIP_INFO *ri;
  346. if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err;
  347. if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err;
  348. if (!PKCS7_add_recipient_info(p7,ri)) goto err;
  349. return(ri);
  350. err:
  351. return(NULL);
  352. }
  353. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  354. {
  355. int i;
  356. STACK_OF(PKCS7_RECIP_INFO) *sk;
  357. i=OBJ_obj2nid(p7->type);
  358. switch (i)
  359. {
  360. case NID_pkcs7_signedAndEnveloped:
  361. sk= p7->d.signed_and_enveloped->recipientinfo;
  362. break;
  363. case NID_pkcs7_enveloped:
  364. sk= p7->d.enveloped->recipientinfo;
  365. break;
  366. default:
  367. PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE);
  368. return(0);
  369. }
  370. sk_PKCS7_RECIP_INFO_push(sk,ri);
  371. return(1);
  372. }
  373. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  374. {
  375. ASN1_INTEGER_set(p7i->version,0);
  376. X509_NAME_set(&p7i->issuer_and_serial->issuer,
  377. X509_get_issuer_name(x509));
  378. M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  379. p7i->issuer_and_serial->serial=
  380. M_ASN1_INTEGER_dup(X509_get_serialNumber(x509));
  381. X509_ALGOR_free(p7i->key_enc_algor);
  382. p7i->key_enc_algor= X509_ALGOR_dup(x509->cert_info->key->algor);
  383. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  384. p7i->cert=x509;
  385. return(1);
  386. }
  387. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  388. {
  389. if (PKCS7_type_is_signed(p7))
  390. return(X509_find_by_issuer_and_serial(p7->d.sign->cert,
  391. si->issuer_and_serial->issuer,
  392. si->issuer_and_serial->serial));
  393. else
  394. return(NULL);
  395. }
  396. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  397. {
  398. int i;
  399. ASN1_OBJECT *objtmp;
  400. PKCS7_ENC_CONTENT *ec;
  401. i=OBJ_obj2nid(p7->type);
  402. switch (i)
  403. {
  404. case NID_pkcs7_signedAndEnveloped:
  405. ec=p7->d.signed_and_enveloped->enc_data;
  406. break;
  407. case NID_pkcs7_enveloped:
  408. ec=p7->d.enveloped->enc_data;
  409. break;
  410. default:
  411. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE);
  412. return(0);
  413. }
  414. /* Check cipher OID exists and has data in it*/
  415. i = EVP_CIPHER_type(cipher);
  416. if(i == NID_undef) {
  417. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  418. return(0);
  419. }
  420. objtmp = OBJ_nid2obj(i);
  421. ec->cipher = cipher;
  422. return 1;
  423. }