p12_crt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. 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. sk_PKCS7_pop_free(safes, PKCS7_free);
  143. safes = NULL;
  144. if ((mac_iter != -1) &&
  145. !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
  146. goto err;
  147. return p12;
  148. err:
  149. if (p12)
  150. PKCS12_free(p12);
  151. if (safes)
  152. sk_PKCS7_pop_free(safes, PKCS7_free);
  153. if (bags)
  154. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  155. return NULL;
  156. }
  157. PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
  158. {
  159. PKCS12_SAFEBAG *bag = NULL;
  160. char *name;
  161. int namelen = -1;
  162. unsigned char *keyid;
  163. int keyidlen = -1;
  164. /* Add user certificate */
  165. if(!(bag = PKCS12_x5092certbag(cert)))
  166. goto err;
  167. /* Use friendlyName and localKeyID in certificate.
  168. * (if present)
  169. */
  170. name = (char *)X509_alias_get0(cert, &namelen);
  171. if(name && !PKCS12_add_friendlyname(bag, name, namelen))
  172. goto err;
  173. keyid = X509_keyid_get0(cert, &keyidlen);
  174. if(keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  175. goto err;
  176. if (!pkcs12_add_bag(pbags, bag))
  177. goto err;
  178. return bag;
  179. err:
  180. if (bag)
  181. PKCS12_SAFEBAG_free(bag);
  182. return NULL;
  183. }
  184. PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key,
  185. int key_usage, int iter,
  186. int nid_key, char *pass)
  187. {
  188. PKCS12_SAFEBAG *bag = NULL;
  189. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  190. /* Make a PKCS#8 structure */
  191. if(!(p8 = EVP_PKEY2PKCS8(key)))
  192. goto err;
  193. if(key_usage && !PKCS8_add_keyusage(p8, key_usage))
  194. goto err;
  195. if (nid_key != -1)
  196. {
  197. bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
  198. PKCS8_PRIV_KEY_INFO_free(p8);
  199. }
  200. else
  201. bag = PKCS12_MAKE_KEYBAG(p8);
  202. if(!bag)
  203. goto err;
  204. if (!pkcs12_add_bag(pbags, bag))
  205. goto err;
  206. return bag;
  207. err:
  208. if (bag)
  209. PKCS12_SAFEBAG_free(bag);
  210. return NULL;
  211. }
  212. int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
  213. int nid_safe, int iter, char *pass)
  214. {
  215. PKCS7 *p7 = NULL;
  216. int free_safes = 0;
  217. if (!*psafes)
  218. {
  219. *psafes = sk_PKCS7_new_null();
  220. if (!*psafes)
  221. return 0;
  222. free_safes = 1;
  223. }
  224. else
  225. free_safes = 0;
  226. if (nid_safe == 0)
  227. nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
  228. if (nid_safe == -1)
  229. p7 = PKCS12_pack_p7data(bags);
  230. else
  231. p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
  232. iter, bags);
  233. if (!p7)
  234. goto err;
  235. if (!sk_PKCS7_push(*psafes, p7))
  236. goto err;
  237. return 1;
  238. err:
  239. if (free_safes)
  240. {
  241. sk_PKCS7_free(*psafes);
  242. *psafes = NULL;
  243. }
  244. if (p7)
  245. PKCS7_free(p7);
  246. return 0;
  247. }
  248. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
  249. {
  250. int free_bags;
  251. if (!pbags)
  252. return 1;
  253. if (!*pbags)
  254. {
  255. *pbags = sk_PKCS12_SAFEBAG_new_null();
  256. if (!*pbags)
  257. return 0;
  258. free_bags = 1;
  259. }
  260. else
  261. free_bags = 0;
  262. if (!sk_PKCS12_SAFEBAG_push(*pbags, bag))
  263. {
  264. if (free_bags)
  265. {
  266. sk_PKCS12_SAFEBAG_free(*pbags);
  267. *pbags = NULL;
  268. }
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
  274. {
  275. PKCS12 *p12;
  276. if (nid_p7 <= 0)
  277. nid_p7 = NID_pkcs7_data;
  278. p12 = PKCS12_init(nid_p7);
  279. if (!p12)
  280. return NULL;
  281. if(!PKCS12_pack_authsafes(p12, safes))
  282. {
  283. PKCS12_free(p12);
  284. return NULL;
  285. }
  286. return p12;
  287. }