p12_crt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 == NID_undef)
  40. nid_cert = NID_aes_256_cbc;
  41. if (nid_key == NID_undef)
  42. nid_key = NID_aes_256_cbc;
  43. if (!iter)
  44. iter = PKCS12_DEFAULT_ITER;
  45. if (!mac_iter)
  46. mac_iter = PKCS12_DEFAULT_ITER;
  47. if (pkey == NULL && cert == NULL && ca == NULL) {
  48. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_ARGUMENT);
  49. return NULL;
  50. }
  51. if (pkey && cert) {
  52. if (!X509_check_private_key(cert, pkey))
  53. return NULL;
  54. if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen))
  55. return NULL;
  56. }
  57. if (cert) {
  58. bag = PKCS12_add_cert(&bags, cert);
  59. if (name && !PKCS12_add_friendlyname(bag, name, -1))
  60. goto err;
  61. if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  62. goto err;
  63. }
  64. /* Add all other certificates */
  65. for (i = 0; i < sk_X509_num(ca); i++) {
  66. if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
  67. goto err;
  68. }
  69. if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
  70. goto err;
  71. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  72. bags = NULL;
  73. if (pkey) {
  74. bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
  75. if (!bag)
  76. goto err;
  77. if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
  78. goto err;
  79. if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
  80. goto err;
  81. if (name && !PKCS12_add_friendlyname(bag, name, -1))
  82. goto err;
  83. if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  84. goto err;
  85. }
  86. if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
  87. goto err;
  88. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  89. bags = NULL;
  90. p12 = PKCS12_add_safes(safes, 0);
  91. if (p12 == NULL)
  92. goto err;
  93. sk_PKCS7_pop_free(safes, PKCS7_free);
  94. safes = NULL;
  95. if ((mac_iter != -1) &&
  96. !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
  97. goto err;
  98. return p12;
  99. err:
  100. PKCS12_free(p12);
  101. sk_PKCS7_pop_free(safes, PKCS7_free);
  102. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  103. return NULL;
  104. }
  105. PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
  106. {
  107. PKCS12_SAFEBAG *bag = NULL;
  108. char *name;
  109. int namelen = -1;
  110. unsigned char *keyid;
  111. int keyidlen = -1;
  112. /* Add user certificate */
  113. if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL)
  114. goto err;
  115. /*
  116. * Use friendlyName and localKeyID in certificate. (if present)
  117. */
  118. name = (char *)X509_alias_get0(cert, &namelen);
  119. if (name && !PKCS12_add_friendlyname(bag, name, namelen))
  120. goto err;
  121. keyid = X509_keyid_get0(cert, &keyidlen);
  122. if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  123. goto err;
  124. if (!pkcs12_add_bag(pbags, bag))
  125. goto err;
  126. return bag;
  127. err:
  128. PKCS12_SAFEBAG_free(bag);
  129. return NULL;
  130. }
  131. PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
  132. EVP_PKEY *key, int key_usage, int iter,
  133. int nid_key, const char *pass)
  134. {
  135. PKCS12_SAFEBAG *bag = NULL;
  136. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  137. /* Make a PKCS#8 structure */
  138. if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
  139. goto err;
  140. if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
  141. goto err;
  142. if (nid_key != -1) {
  143. bag = PKCS12_SAFEBAG_create_pkcs8_encrypt(nid_key, pass, -1, NULL, 0,
  144. iter, p8);
  145. PKCS8_PRIV_KEY_INFO_free(p8);
  146. } else
  147. bag = PKCS12_SAFEBAG_create0_p8inf(p8);
  148. if (!bag)
  149. goto err;
  150. if (!pkcs12_add_bag(pbags, bag))
  151. goto err;
  152. return bag;
  153. err:
  154. PKCS12_SAFEBAG_free(bag);
  155. return NULL;
  156. }
  157. PKCS12_SAFEBAG *PKCS12_add_secret(STACK_OF(PKCS12_SAFEBAG) **pbags,
  158. int nid_type, const unsigned char *value, int len)
  159. {
  160. PKCS12_SAFEBAG *bag = NULL;
  161. /* Add secret, storing the value as an octet string */
  162. if ((bag = PKCS12_SAFEBAG_create_secret(nid_type, V_ASN1_OCTET_STRING, value, len)) == NULL)
  163. goto err;
  164. if (!pkcs12_add_bag(pbags, bag))
  165. goto err;
  166. return bag;
  167. err:
  168. PKCS12_SAFEBAG_free(bag);
  169. return NULL;
  170. }
  171. int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
  172. int nid_safe, int iter, const char *pass)
  173. {
  174. PKCS7 *p7 = NULL;
  175. int free_safes = 0;
  176. if (*psafes == NULL) {
  177. *psafes = sk_PKCS7_new_null();
  178. if (*psafes == NULL)
  179. return 0;
  180. free_safes = 1;
  181. }
  182. if (nid_safe == 0)
  183. #ifdef OPENSSL_NO_RC2
  184. nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  185. #else
  186. nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
  187. #endif
  188. if (nid_safe == -1)
  189. p7 = PKCS12_pack_p7data(bags);
  190. else
  191. p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0, iter, bags);
  192. if (p7 == NULL)
  193. goto err;
  194. if (!sk_PKCS7_push(*psafes, p7))
  195. goto err;
  196. return 1;
  197. err:
  198. if (free_safes) {
  199. sk_PKCS7_free(*psafes);
  200. *psafes = NULL;
  201. }
  202. PKCS7_free(p7);
  203. return 0;
  204. }
  205. static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
  206. PKCS12_SAFEBAG *bag)
  207. {
  208. int free_bags = 0;
  209. if (pbags == NULL)
  210. return 1;
  211. if (*pbags == NULL) {
  212. *pbags = sk_PKCS12_SAFEBAG_new_null();
  213. if (*pbags == NULL)
  214. return 0;
  215. free_bags = 1;
  216. }
  217. if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
  218. if (free_bags) {
  219. sk_PKCS12_SAFEBAG_free(*pbags);
  220. *pbags = NULL;
  221. }
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
  227. {
  228. PKCS12 *p12;
  229. if (nid_p7 <= 0)
  230. nid_p7 = NID_pkcs7_data;
  231. p12 = PKCS12_init(nid_p7);
  232. if (p12 == NULL)
  233. return NULL;
  234. if (!PKCS12_pack_authsafes(p12, safes)) {
  235. PKCS12_free(p12);
  236. return NULL;
  237. }
  238. return p12;
  239. }