p12_crt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* p12_crt.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/pkcs12.h>
  61. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag);
  62. PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
  63. STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter,
  64. int keytype)
  65. {
  66. PKCS12 *p12 = NULL;
  67. STACK_OF(PKCS7) *safes = NULL;
  68. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  69. PKCS12_SAFEBAG *bag = NULL;
  70. int i;
  71. unsigned char keyid[EVP_MAX_MD_SIZE];
  72. unsigned int keyidlen = 0;
  73. /* Set defaults */
  74. if (!nid_cert)
  75. nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
  76. if (!nid_key)
  77. nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  78. if (!iter)
  79. iter = PKCS12_DEFAULT_ITER;
  80. if (!mac_iter)
  81. mac_iter = 1;
  82. if(!pkey && !cert && !ca)
  83. {
  84. PKCS12err(PKCS12_F_PKCS12_CREATE,PKCS12_R_INVALID_NULL_ARGUMENT);
  85. return NULL;
  86. }
  87. if (pkey && cert)
  88. {
  89. if(!X509_check_private_key(cert, pkey))
  90. return NULL;
  91. X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
  92. }
  93. if (cert)
  94. {
  95. bag = PKCS12_add_cert(&bags, cert);
  96. if(name && !PKCS12_add_friendlyname(bag, name, -1))
  97. goto err;
  98. if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  99. goto err;
  100. }
  101. /* Add all other certificates */
  102. for(i = 0; i < sk_X509_num(ca); i++)
  103. {
  104. if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
  105. goto err;
  106. }
  107. if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
  108. goto err;
  109. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  110. bags = NULL;
  111. if (pkey)
  112. {
  113. int cspidx;
  114. bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
  115. if (!bag)
  116. goto err;
  117. cspidx = EVP_PKEY_get_attr_by_NID(pkey, NID_ms_csp_name, -1);
  118. if (cspidx >= 0)
  119. {
  120. X509_ATTRIBUTE *cspattr;
  121. cspattr = EVP_PKEY_get_attr(pkey, cspidx);
  122. if (!X509at_add1_attr(&bag->attrib, cspattr))
  123. goto err;
  124. }
  125. if(name && !PKCS12_add_friendlyname(bag, name, -1))
  126. goto err;
  127. if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  128. goto err;
  129. }
  130. if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
  131. goto err;
  132. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  133. bags = NULL;
  134. p12 = PKCS12_add_safes(safes, 0);
  135. sk_PKCS7_pop_free(safes, PKCS7_free);
  136. safes = NULL;
  137. if ((mac_iter != -1) &&
  138. !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
  139. goto err;
  140. return p12;
  141. err:
  142. if (p12)
  143. PKCS12_free(p12);
  144. if (safes)
  145. sk_PKCS7_pop_free(safes, PKCS7_free);
  146. if (bags)
  147. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  148. return NULL;
  149. }
  150. PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
  151. {
  152. PKCS12_SAFEBAG *bag = NULL;
  153. char *name;
  154. int namelen = -1;
  155. unsigned char *keyid;
  156. int keyidlen = -1;
  157. /* Add user certificate */
  158. if(!(bag = PKCS12_x5092certbag(cert)))
  159. goto err;
  160. /* Use friendlyName and localKeyID in certificate.
  161. * (if present)
  162. */
  163. name = (char *)X509_alias_get0(cert, &namelen);
  164. if(name && !PKCS12_add_friendlyname(bag, name, namelen))
  165. goto err;
  166. keyid = X509_keyid_get0(cert, &keyidlen);
  167. if(keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  168. goto err;
  169. if (!pkcs12_add_bag(pbags, bag))
  170. goto err;
  171. return bag;
  172. err:
  173. if (bag)
  174. PKCS12_SAFEBAG_free(bag);
  175. return NULL;
  176. }
  177. PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key,
  178. int key_usage, int iter,
  179. int nid_key, char *pass)
  180. {
  181. PKCS12_SAFEBAG *bag = NULL;
  182. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  183. /* Make a PKCS#8 structure */
  184. if(!(p8 = EVP_PKEY2PKCS8(key)))
  185. goto err;
  186. if(key_usage && !PKCS8_add_keyusage(p8, key_usage))
  187. goto err;
  188. if (nid_key != -1)
  189. {
  190. bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
  191. PKCS8_PRIV_KEY_INFO_free(p8);
  192. }
  193. else
  194. bag = PKCS12_MAKE_KEYBAG(p8);
  195. if(!bag)
  196. goto err;
  197. if (!pkcs12_add_bag(pbags, bag))
  198. goto err;
  199. return bag;
  200. err:
  201. if (bag)
  202. PKCS12_SAFEBAG_free(bag);
  203. return NULL;
  204. }
  205. int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
  206. int nid_safe, int iter, char *pass)
  207. {
  208. PKCS7 *p7 = NULL;
  209. int free_safes = 0;
  210. if (!*psafes)
  211. {
  212. *psafes = sk_PKCS7_new_null();
  213. if (!*psafes)
  214. return 0;
  215. free_safes = 1;
  216. }
  217. else
  218. free_safes = 0;
  219. if (nid_safe == 0)
  220. nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
  221. if (nid_safe == -1)
  222. p7 = PKCS12_pack_p7data(bags);
  223. else
  224. p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
  225. iter, bags);
  226. if (!p7)
  227. goto err;
  228. if (!sk_PKCS7_push(*psafes, p7))
  229. goto err;
  230. return 1;
  231. err:
  232. if (free_safes)
  233. {
  234. sk_PKCS7_free(*psafes);
  235. *psafes = NULL;
  236. }
  237. if (p7)
  238. PKCS7_free(p7);
  239. return 0;
  240. }
  241. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
  242. {
  243. int free_bags;
  244. if (!pbags)
  245. return 1;
  246. if (!*pbags)
  247. {
  248. *pbags = sk_PKCS12_SAFEBAG_new_null();
  249. if (!*pbags)
  250. return 0;
  251. free_bags = 1;
  252. }
  253. else
  254. free_bags = 0;
  255. if (!sk_PKCS12_SAFEBAG_push(*pbags, bag))
  256. {
  257. if (free_bags)
  258. {
  259. sk_PKCS12_SAFEBAG_free(*pbags);
  260. *pbags = NULL;
  261. }
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
  267. {
  268. PKCS12 *p12;
  269. if (nid_p7 <= 0)
  270. nid_p7 = NID_pkcs7_data;
  271. p12 = PKCS12_init(nid_p7);
  272. if (!p12)
  273. return NULL;
  274. if(!PKCS12_pack_authsafes(p12, safes))
  275. {
  276. PKCS12_free(p12);
  277. return NULL;
  278. }
  279. return p12;
  280. }