p12_crt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/pkcs12.h>
  12. #include "p12_local.h"
  13. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
  14. PKCS12_SAFEBAG *bag);
  15. static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
  16. {
  17. int idx;
  18. X509_ATTRIBUTE *attr;
  19. idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
  20. if (idx < 0)
  21. return 1;
  22. attr = EVP_PKEY_get_attr(pkey, idx);
  23. if (!X509at_add1_attr(&bag->attrib, attr))
  24. return 0;
  25. return 1;
  26. }
  27. PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
  28. STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
  29. int mac_iter, int keytype)
  30. {
  31. PKCS12 *p12 = NULL;
  32. STACK_OF(PKCS7) *safes = NULL;
  33. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  34. PKCS12_SAFEBAG *bag = NULL;
  35. int i;
  36. unsigned char keyid[EVP_MAX_MD_SIZE];
  37. unsigned int keyidlen = 0;
  38. /* Set defaults */
  39. if (!nid_cert)
  40. #ifdef OPENSSL_NO_RC2
  41. nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  42. #else
  43. nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
  44. #endif
  45. if (!nid_key)
  46. nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  47. if (!iter)
  48. iter = PKCS12_DEFAULT_ITER;
  49. if (!mac_iter)
  50. mac_iter = 1;
  51. if (!pkey && !cert && !ca) {
  52. PKCS12err(PKCS12_F_PKCS12_CREATE, PKCS12_R_INVALID_NULL_ARGUMENT);
  53. return NULL;
  54. }
  55. if (pkey && cert) {
  56. if (!X509_check_private_key(cert, pkey))
  57. return NULL;
  58. if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen))
  59. return NULL;
  60. }
  61. if (cert) {
  62. bag = PKCS12_add_cert(&bags, cert);
  63. if (name && !PKCS12_add_friendlyname(bag, name, -1))
  64. goto err;
  65. if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  66. goto err;
  67. }
  68. /* Add all other certificates */
  69. for (i = 0; i < sk_X509_num(ca); i++) {
  70. if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
  71. goto err;
  72. }
  73. if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
  74. goto err;
  75. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  76. bags = NULL;
  77. if (pkey) {
  78. bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
  79. if (!bag)
  80. goto err;
  81. if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
  82. goto err;
  83. if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
  84. goto err;
  85. if (name && !PKCS12_add_friendlyname(bag, name, -1))
  86. goto err;
  87. if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  88. goto err;
  89. }
  90. if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
  91. goto err;
  92. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  93. bags = NULL;
  94. p12 = PKCS12_add_safes(safes, 0);
  95. if (!p12)
  96. goto err;
  97. sk_PKCS7_pop_free(safes, PKCS7_free);
  98. safes = NULL;
  99. if ((mac_iter != -1) &&
  100. !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
  101. goto err;
  102. return p12;
  103. err:
  104. PKCS12_free(p12);
  105. sk_PKCS7_pop_free(safes, PKCS7_free);
  106. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  107. return NULL;
  108. }
  109. PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
  110. {
  111. PKCS12_SAFEBAG *bag = NULL;
  112. char *name;
  113. int namelen = -1;
  114. unsigned char *keyid;
  115. int keyidlen = -1;
  116. /* Add user certificate */
  117. if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL)
  118. goto err;
  119. /*
  120. * Use friendlyName and localKeyID in certificate. (if present)
  121. */
  122. name = (char *)X509_alias_get0(cert, &namelen);
  123. if (name && !PKCS12_add_friendlyname(bag, name, namelen))
  124. goto err;
  125. keyid = X509_keyid_get0(cert, &keyidlen);
  126. if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  127. goto err;
  128. if (!pkcs12_add_bag(pbags, bag))
  129. goto err;
  130. return bag;
  131. err:
  132. PKCS12_SAFEBAG_free(bag);
  133. return NULL;
  134. }
  135. PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
  136. EVP_PKEY *key, int key_usage, int iter,
  137. int nid_key, const char *pass)
  138. {
  139. PKCS12_SAFEBAG *bag = NULL;
  140. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  141. /* Make a PKCS#8 structure */
  142. if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
  143. goto err;
  144. if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
  145. goto err;
  146. if (nid_key != -1) {
  147. bag = PKCS12_SAFEBAG_create_pkcs8_encrypt(nid_key, pass, -1, NULL, 0,
  148. iter, p8);
  149. PKCS8_PRIV_KEY_INFO_free(p8);
  150. } else
  151. bag = PKCS12_SAFEBAG_create0_p8inf(p8);
  152. if (!bag)
  153. goto err;
  154. if (!pkcs12_add_bag(pbags, bag))
  155. goto err;
  156. return bag;
  157. err:
  158. PKCS12_SAFEBAG_free(bag);
  159. return NULL;
  160. }
  161. int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
  162. int nid_safe, int iter, const char *pass)
  163. {
  164. PKCS7 *p7 = NULL;
  165. int free_safes = 0;
  166. if (!*psafes) {
  167. *psafes = sk_PKCS7_new_null();
  168. if (!*psafes)
  169. return 0;
  170. free_safes = 1;
  171. } else
  172. free_safes = 0;
  173. if (nid_safe == 0)
  174. #ifdef OPENSSL_NO_RC2
  175. nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  176. #else
  177. nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
  178. #endif
  179. if (nid_safe == -1)
  180. p7 = PKCS12_pack_p7data(bags);
  181. else
  182. p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0, iter, bags);
  183. if (!p7)
  184. goto err;
  185. if (!sk_PKCS7_push(*psafes, p7))
  186. goto err;
  187. return 1;
  188. err:
  189. if (free_safes) {
  190. sk_PKCS7_free(*psafes);
  191. *psafes = NULL;
  192. }
  193. PKCS7_free(p7);
  194. return 0;
  195. }
  196. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
  197. PKCS12_SAFEBAG *bag)
  198. {
  199. int free_bags;
  200. if (!pbags)
  201. return 1;
  202. if (!*pbags) {
  203. *pbags = sk_PKCS12_SAFEBAG_new_null();
  204. if (!*pbags)
  205. return 0;
  206. free_bags = 1;
  207. } else
  208. free_bags = 0;
  209. if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
  210. if (free_bags) {
  211. sk_PKCS12_SAFEBAG_free(*pbags);
  212. *pbags = NULL;
  213. }
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
  219. {
  220. PKCS12 *p12;
  221. if (nid_p7 <= 0)
  222. nid_p7 = NID_pkcs7_data;
  223. p12 = PKCS12_init(nid_p7);
  224. if (!p12)
  225. return NULL;
  226. if (!PKCS12_pack_authsafes(p12, safes)) {
  227. PKCS12_free(p12);
  228. return NULL;
  229. }
  230. return p12;
  231. }