p12_crt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* p12_crt.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) 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. static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
  63. {
  64. int idx;
  65. X509_ATTRIBUTE *attr;
  66. idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
  67. if (idx < 0)
  68. return 1;
  69. attr = EVP_PKEY_get_attr(pkey, idx);
  70. if (!X509at_add1_attr(&bag->attrib, attr))
  71. return 0;
  72. return 1;
  73. }
  74. PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
  75. STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter,
  76. int keytype)
  77. {
  78. PKCS12 *p12 = NULL;
  79. STACK_OF(PKCS7) *safes = NULL;
  80. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  81. PKCS12_SAFEBAG *bag = NULL;
  82. int i;
  83. unsigned char keyid[EVP_MAX_MD_SIZE];
  84. unsigned int keyidlen = 0;
  85. /* Set defaults */
  86. if (!nid_cert)
  87. nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
  88. if (!nid_key)
  89. nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  90. if (!iter)
  91. iter = PKCS12_DEFAULT_ITER;
  92. if (!mac_iter)
  93. mac_iter = 1;
  94. if(!pkey && !cert && !ca)
  95. {
  96. PKCS12err(PKCS12_F_PKCS12_CREATE,PKCS12_R_INVALID_NULL_ARGUMENT);
  97. return NULL;
  98. }
  99. if (pkey && cert)
  100. {
  101. if(!X509_check_private_key(cert, pkey))
  102. return NULL;
  103. X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
  104. }
  105. if (cert)
  106. {
  107. bag = PKCS12_add_cert(&bags, cert);
  108. if(name && !PKCS12_add_friendlyname(bag, name, -1))
  109. goto err;
  110. if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  111. goto err;
  112. }
  113. /* Add all other certificates */
  114. for(i = 0; i < sk_X509_num(ca); i++)
  115. {
  116. if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
  117. goto err;
  118. }
  119. if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
  120. goto err;
  121. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  122. bags = NULL;
  123. if (pkey)
  124. {
  125. bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
  126. if (!bag)
  127. goto err;
  128. if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
  129. goto err;
  130. if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
  131. goto err;
  132. if(name && !PKCS12_add_friendlyname(bag, name, -1))
  133. goto err;
  134. if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  135. goto err;
  136. }
  137. if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
  138. goto err;
  139. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  140. bags = NULL;
  141. p12 = PKCS12_add_safes(safes, 0);
  142. if (!p12)
  143. goto err;
  144. sk_PKCS7_pop_free(safes, PKCS7_free);
  145. safes = NULL;
  146. if ((mac_iter != -1) &&
  147. !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
  148. goto err;
  149. return p12;
  150. err:
  151. if (p12)
  152. PKCS12_free(p12);
  153. if (safes)
  154. sk_PKCS7_pop_free(safes, PKCS7_free);
  155. if (bags)
  156. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  157. return NULL;
  158. }
  159. PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
  160. {
  161. PKCS12_SAFEBAG *bag = NULL;
  162. char *name;
  163. int namelen = -1;
  164. unsigned char *keyid;
  165. int keyidlen = -1;
  166. /* Add user certificate */
  167. if(!(bag = PKCS12_x5092certbag(cert)))
  168. goto err;
  169. /* Use friendlyName and localKeyID in certificate.
  170. * (if present)
  171. */
  172. name = (char *)X509_alias_get0(cert, &namelen);
  173. if(name && !PKCS12_add_friendlyname(bag, name, namelen))
  174. goto err;
  175. keyid = X509_keyid_get0(cert, &keyidlen);
  176. if(keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  177. goto err;
  178. if (!pkcs12_add_bag(pbags, bag))
  179. goto err;
  180. return bag;
  181. err:
  182. if (bag)
  183. PKCS12_SAFEBAG_free(bag);
  184. return NULL;
  185. }
  186. PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key,
  187. int key_usage, int iter,
  188. int nid_key, char *pass)
  189. {
  190. PKCS12_SAFEBAG *bag = NULL;
  191. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  192. /* Make a PKCS#8 structure */
  193. if(!(p8 = EVP_PKEY2PKCS8(key)))
  194. goto err;
  195. if(key_usage && !PKCS8_add_keyusage(p8, key_usage))
  196. goto err;
  197. if (nid_key != -1)
  198. {
  199. bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
  200. PKCS8_PRIV_KEY_INFO_free(p8);
  201. }
  202. else
  203. bag = PKCS12_MAKE_KEYBAG(p8);
  204. if(!bag)
  205. goto err;
  206. if (!pkcs12_add_bag(pbags, bag))
  207. goto err;
  208. return bag;
  209. err:
  210. if (bag)
  211. PKCS12_SAFEBAG_free(bag);
  212. return NULL;
  213. }
  214. int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
  215. int nid_safe, int iter, char *pass)
  216. {
  217. PKCS7 *p7 = NULL;
  218. int free_safes = 0;
  219. if (!*psafes)
  220. {
  221. *psafes = sk_PKCS7_new_null();
  222. if (!*psafes)
  223. return 0;
  224. free_safes = 1;
  225. }
  226. else
  227. free_safes = 0;
  228. if (nid_safe == 0)
  229. nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
  230. if (nid_safe == -1)
  231. p7 = PKCS12_pack_p7data(bags);
  232. else
  233. p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
  234. iter, bags);
  235. if (!p7)
  236. goto err;
  237. if (!sk_PKCS7_push(*psafes, p7))
  238. goto err;
  239. return 1;
  240. err:
  241. if (free_safes)
  242. {
  243. sk_PKCS7_free(*psafes);
  244. *psafes = NULL;
  245. }
  246. if (p7)
  247. PKCS7_free(p7);
  248. return 0;
  249. }
  250. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
  251. {
  252. int free_bags;
  253. if (!pbags)
  254. return 1;
  255. if (!*pbags)
  256. {
  257. *pbags = sk_PKCS12_SAFEBAG_new_null();
  258. if (!*pbags)
  259. return 0;
  260. free_bags = 1;
  261. }
  262. else
  263. free_bags = 0;
  264. if (!sk_PKCS12_SAFEBAG_push(*pbags, bag))
  265. {
  266. if (free_bags)
  267. {
  268. sk_PKCS12_SAFEBAG_free(*pbags);
  269. *pbags = NULL;
  270. }
  271. return 0;
  272. }
  273. return 1;
  274. }
  275. PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
  276. {
  277. PKCS12 *p12;
  278. if (nid_p7 <= 0)
  279. nid_p7 = NID_pkcs7_data;
  280. p12 = PKCS12_init(nid_p7);
  281. if (!p12)
  282. return NULL;
  283. if(!PKCS12_pack_authsafes(p12, safes))
  284. {
  285. PKCS12_free(p12);
  286. return NULL;
  287. }
  288. return p12;
  289. }