p12_crt.c 8.2 KB

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