You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1156 lines
33 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 1999.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/pkcs8.h>
  56. #include <assert.h>
  57. #include <limits.h>
  58. #include <string.h>
  59. #include <openssl/asn1.h>
  60. #include <openssl/bn.h>
  61. #include <openssl/buf.h>
  62. #include <openssl/cipher.h>
  63. #include <openssl/digest.h>
  64. #include <openssl/err.h>
  65. #include <openssl/hmac.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/x509.h>
  68. #include "../bytestring/internal.h"
  69. #include "../evp/internal.h"
  70. #define PKCS12_KEY_ID 1
  71. #define PKCS12_IV_ID 2
  72. #define PKCS12_MAC_ID 3
  73. static int ascii_to_ucs2(const char *ascii, size_t ascii_len,
  74. uint8_t **out, size_t *out_len) {
  75. uint8_t *unitmp;
  76. size_t ulen, i;
  77. ulen = ascii_len * 2 + 2;
  78. if (ulen < ascii_len) {
  79. return 0;
  80. }
  81. unitmp = OPENSSL_malloc(ulen);
  82. if (unitmp == NULL) {
  83. return 0;
  84. }
  85. for (i = 0; i < ulen - 2; i += 2) {
  86. unitmp[i] = 0;
  87. unitmp[i + 1] = ascii[i >> 1];
  88. }
  89. /* Make result double null terminated */
  90. unitmp[ulen - 2] = 0;
  91. unitmp[ulen - 1] = 0;
  92. *out_len = ulen;
  93. *out = unitmp;
  94. return 1;
  95. }
  96. static int pkcs12_key_gen_raw(const uint8_t *pass_raw, size_t pass_raw_len,
  97. const uint8_t *salt, size_t salt_len,
  98. int id, int iterations,
  99. size_t out_len, uint8_t *out,
  100. const EVP_MD *md_type) {
  101. uint8_t *B, *D, *I, *p, *Ai;
  102. int Slen, Plen, Ilen, Ijlen;
  103. int i, j, v;
  104. size_t u;
  105. int ret = 0;
  106. BIGNUM *Ij, *Bpl1; /* These hold Ij and B + 1 */
  107. EVP_MD_CTX ctx;
  108. EVP_MD_CTX_init(&ctx);
  109. v = EVP_MD_block_size(md_type);
  110. u = EVP_MD_size(md_type);
  111. D = OPENSSL_malloc(v);
  112. Ai = OPENSSL_malloc(u);
  113. B = OPENSSL_malloc(v + 1);
  114. Slen = v * ((salt_len + v - 1) / v);
  115. if (pass_raw_len)
  116. Plen = v * ((pass_raw_len + v - 1) / v);
  117. else
  118. Plen = 0;
  119. Ilen = Slen + Plen;
  120. I = OPENSSL_malloc(Ilen);
  121. Ij = BN_new();
  122. Bpl1 = BN_new();
  123. if (!D || !Ai || !B || !I || !Ij || !Bpl1)
  124. goto err;
  125. for (i = 0; i < v; i++)
  126. D[i] = id;
  127. p = I;
  128. for (i = 0; i < Slen; i++)
  129. *p++ = salt[i % salt_len];
  130. for (i = 0; i < Plen; i++)
  131. *p++ = pass_raw[i % pass_raw_len];
  132. for (;;) {
  133. if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
  134. !EVP_DigestUpdate(&ctx, D, v) ||
  135. !EVP_DigestUpdate(&ctx, I, Ilen) ||
  136. !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
  137. goto err;
  138. }
  139. for (j = 1; j < iterations; j++) {
  140. if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
  141. !EVP_DigestUpdate(&ctx, Ai, u) ||
  142. !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
  143. goto err;
  144. }
  145. }
  146. memcpy(out, Ai, out_len < u ? out_len : u);
  147. if (u >= out_len) {
  148. ret = 1;
  149. goto end;
  150. }
  151. out_len -= u;
  152. out += u;
  153. for (j = 0; j < v; j++)
  154. B[j] = Ai[j % u];
  155. /* Work out B + 1 first then can use B as tmp space */
  156. if (!BN_bin2bn(B, v, Bpl1))
  157. goto err;
  158. if (!BN_add_word(Bpl1, 1))
  159. goto err;
  160. for (j = 0; j < Ilen; j += v) {
  161. if (!BN_bin2bn(I + j, v, Ij))
  162. goto err;
  163. if (!BN_add(Ij, Ij, Bpl1))
  164. goto err;
  165. if (!BN_bn2bin(Ij, B))
  166. goto err;
  167. Ijlen = BN_num_bytes(Ij);
  168. /* If more than 2^(v*8) - 1 cut off MSB */
  169. if (Ijlen > v) {
  170. if (!BN_bn2bin(Ij, B))
  171. goto err;
  172. memcpy(I + j, B + 1, v);
  173. /* If less than v bytes pad with zeroes */
  174. } else if (Ijlen < v) {
  175. memset(I + j, 0, v - Ijlen);
  176. if (!BN_bn2bin(Ij, I + j + v - Ijlen))
  177. goto err;
  178. } else if (!BN_bn2bin(Ij, I + j)) {
  179. goto err;
  180. }
  181. }
  182. }
  183. err:
  184. OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_raw, ERR_R_MALLOC_FAILURE);
  185. end:
  186. OPENSSL_free(Ai);
  187. OPENSSL_free(B);
  188. OPENSSL_free(D);
  189. OPENSSL_free(I);
  190. BN_free(Ij);
  191. BN_free(Bpl1);
  192. EVP_MD_CTX_cleanup(&ctx);
  193. return ret;
  194. }
  195. static int pkcs12_pbe_keyivgen(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
  196. size_t pass_raw_len, ASN1_TYPE *param,
  197. const EVP_CIPHER *cipher, const EVP_MD *md,
  198. int is_encrypt) {
  199. PBEPARAM *pbe;
  200. int salt_len, iterations, ret;
  201. uint8_t *salt;
  202. const uint8_t *pbuf;
  203. uint8_t key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  204. /* Extract useful info from parameter */
  205. if (param == NULL || param->type != V_ASN1_SEQUENCE ||
  206. param->value.sequence == NULL) {
  207. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_DECODE_ERROR);
  208. return 0;
  209. }
  210. pbuf = param->value.sequence->data;
  211. pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length);
  212. if (pbe == NULL) {
  213. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_DECODE_ERROR);
  214. return 0;
  215. }
  216. if (!pbe->iter) {
  217. iterations = 1;
  218. } else {
  219. iterations = ASN1_INTEGER_get(pbe->iter);
  220. }
  221. salt = pbe->salt->data;
  222. salt_len = pbe->salt->length;
  223. if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_KEY_ID,
  224. iterations, EVP_CIPHER_key_length(cipher), key, md)) {
  225. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_KEY_GEN_ERROR);
  226. PBEPARAM_free(pbe);
  227. return 0;
  228. }
  229. if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_IV_ID,
  230. iterations, EVP_CIPHER_iv_length(cipher), iv, md)) {
  231. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_KEY_GEN_ERROR);
  232. PBEPARAM_free(pbe);
  233. return 0;
  234. }
  235. PBEPARAM_free(pbe);
  236. ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt);
  237. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  238. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  239. return ret;
  240. }
  241. typedef int (*keygen_func)(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
  242. size_t pass_raw_len, ASN1_TYPE *param,
  243. const EVP_CIPHER *cipher, const EVP_MD *md,
  244. int is_encrypt);
  245. struct pbe_suite {
  246. int pbe_nid;
  247. const EVP_CIPHER* (*cipher_func)(void);
  248. const EVP_MD* (*md_func)(void);
  249. keygen_func keygen;
  250. };
  251. static const struct pbe_suite kBuiltinPBE[] = {
  252. {
  253. NID_pbe_WithSHA1And40BitRC2_CBC, EVP_rc2_40_cbc, EVP_sha1, pkcs12_pbe_keyivgen,
  254. },
  255. {
  256. NID_pbe_WithSHA1And128BitRC4, EVP_rc4, EVP_sha1, pkcs12_pbe_keyivgen,
  257. },
  258. {
  259. NID_pbe_WithSHA1And3_Key_TripleDES_CBC, EVP_des_ede3_cbc, EVP_sha1,
  260. pkcs12_pbe_keyivgen,
  261. },
  262. };
  263. static int pbe_cipher_init(ASN1_OBJECT *pbe_obj,
  264. const uint8_t *pass_raw, size_t pass_raw_len,
  265. ASN1_TYPE *param,
  266. EVP_CIPHER_CTX *ctx, int is_encrypt) {
  267. const EVP_CIPHER *cipher;
  268. const EVP_MD *md;
  269. unsigned i;
  270. const struct pbe_suite *suite = NULL;
  271. const int pbe_nid = OBJ_obj2nid(pbe_obj);
  272. for (i = 0; i < sizeof(kBuiltinPBE) / sizeof(struct pbe_suite); i++) {
  273. if (kBuiltinPBE[i].pbe_nid == pbe_nid) {
  274. suite = &kBuiltinPBE[i];
  275. break;
  276. }
  277. }
  278. if (suite == NULL) {
  279. char obj_str[80];
  280. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_ALGORITHM);
  281. if (!pbe_obj) {
  282. strncpy(obj_str, "NULL", sizeof(obj_str));
  283. } else {
  284. i2t_ASN1_OBJECT(obj_str, sizeof(obj_str), pbe_obj);
  285. }
  286. ERR_add_error_data(2, "TYPE=", obj_str);
  287. return 0;
  288. }
  289. if (suite->cipher_func == NULL) {
  290. cipher = NULL;
  291. } else {
  292. cipher = suite->cipher_func();
  293. if (!cipher) {
  294. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_CIPHER);
  295. return 0;
  296. }
  297. }
  298. if (suite->md_func == NULL) {
  299. md = NULL;
  300. } else {
  301. md = suite->md_func();
  302. if (!md) {
  303. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_DIGEST);
  304. return 0;
  305. }
  306. }
  307. if (!suite->keygen(ctx, pass_raw, pass_raw_len, param, cipher, md,
  308. is_encrypt)) {
  309. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_KEYGEN_FAILURE);
  310. return 0;
  311. }
  312. return 1;
  313. }
  314. static int pbe_crypt(const X509_ALGOR *algor,
  315. const uint8_t *pass_raw, size_t pass_raw_len,
  316. const uint8_t *in, size_t in_len,
  317. uint8_t **out, size_t *out_len,
  318. int is_encrypt) {
  319. uint8_t *buf;
  320. int n, ret = 0;
  321. EVP_CIPHER_CTX ctx;
  322. unsigned block_size;
  323. EVP_CIPHER_CTX_init(&ctx);
  324. if (!pbe_cipher_init(algor->algorithm, pass_raw, pass_raw_len,
  325. algor->parameter, &ctx, is_encrypt)) {
  326. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_UNKNOWN_CIPHER_ALGORITHM);
  327. return 0;
  328. }
  329. block_size = EVP_CIPHER_CTX_block_size(&ctx);
  330. if (in_len + block_size < in_len) {
  331. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_TOO_LONG);
  332. goto err;
  333. }
  334. buf = OPENSSL_malloc(in_len + block_size);
  335. if (buf == NULL) {
  336. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_MALLOC_FAILURE);
  337. goto err;
  338. }
  339. if (!EVP_CipherUpdate(&ctx, buf, &n, in, in_len)) {
  340. OPENSSL_free(buf);
  341. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
  342. goto err;
  343. }
  344. *out_len = n;
  345. if (!EVP_CipherFinal_ex(&ctx, buf + n, &n)) {
  346. OPENSSL_free(buf);
  347. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
  348. goto err;
  349. }
  350. *out_len += n;
  351. *out = buf;
  352. ret = 1;
  353. err:
  354. EVP_CIPHER_CTX_cleanup(&ctx);
  355. return ret;
  356. }
  357. static void *pkcs12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,
  358. const uint8_t *pass_raw,
  359. size_t pass_raw_len,
  360. ASN1_OCTET_STRING *oct) {
  361. uint8_t *out;
  362. const uint8_t *p;
  363. void *ret;
  364. size_t out_len;
  365. if (!pbe_crypt(algor, pass_raw, pass_raw_len, oct->data, oct->length,
  366. &out, &out_len, 0 /* decrypt */)) {
  367. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_decrypt_d2i, PKCS8_R_CRYPT_ERROR);
  368. return NULL;
  369. }
  370. p = out;
  371. ret = ASN1_item_d2i(NULL, &p, out_len, it);
  372. OPENSSL_cleanse(out, out_len);
  373. if (!ret) {
  374. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_decrypt_d2i, PKCS8_R_DECODE_ERROR);
  375. }
  376. OPENSSL_free(out);
  377. return ret;
  378. }
  379. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass,
  380. int pass_len) {
  381. uint8_t *pass_raw = NULL;
  382. size_t pass_raw_len = 0;
  383. PKCS8_PRIV_KEY_INFO *ret;
  384. if (pass) {
  385. if (pass_len == -1) {
  386. pass_len = strlen(pass);
  387. }
  388. if (!ascii_to_ucs2(pass, pass_len, &pass_raw, &pass_raw_len)) {
  389. OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_asc, PKCS8_R_DECODE_ERROR);
  390. return NULL;
  391. }
  392. }
  393. ret = PKCS8_decrypt_pbe(pkcs8, pass_raw, pass_raw_len);
  394. if (pass_raw) {
  395. OPENSSL_cleanse(pass_raw, pass_raw_len);
  396. OPENSSL_free(pass_raw);
  397. }
  398. return ret;
  399. }
  400. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt_pbe(X509_SIG *pkcs8, const uint8_t *pass_raw,
  401. size_t pass_raw_len) {
  402. return pkcs12_item_decrypt_d2i(pkcs8->algor,
  403. ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw,
  404. pass_raw_len, pkcs8->digest);
  405. }
  406. static ASN1_OCTET_STRING *pkcs12_item_i2d_encrypt(X509_ALGOR *algor,
  407. const ASN1_ITEM *it,
  408. const uint8_t *pass_raw,
  409. size_t pass_raw_len, void *obj) {
  410. ASN1_OCTET_STRING *oct;
  411. uint8_t *in = NULL;
  412. int in_len;
  413. size_t crypt_len;
  414. oct = M_ASN1_OCTET_STRING_new();
  415. if (oct == NULL) {
  416. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, ERR_R_MALLOC_FAILURE);
  417. return NULL;
  418. }
  419. in_len = ASN1_item_i2d(obj, &in, it);
  420. if (!in) {
  421. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, PKCS8_R_ENCODE_ERROR);
  422. return NULL;
  423. }
  424. if (!pbe_crypt(algor, pass_raw, pass_raw_len, in, in_len, &oct->data, &crypt_len,
  425. 1 /* encrypt */)) {
  426. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, PKCS8_R_ENCRYPT_ERROR);
  427. OPENSSL_free(in);
  428. return NULL;
  429. }
  430. oct->length = crypt_len;
  431. OPENSSL_cleanse(in, in_len);
  432. OPENSSL_free(in);
  433. return oct;
  434. }
  435. X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass,
  436. int pass_len, uint8_t *salt, size_t salt_len,
  437. int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
  438. uint8_t *pass_raw = NULL;
  439. size_t pass_raw_len = 0;
  440. X509_SIG *ret;
  441. if (pass) {
  442. if (pass_len == -1) {
  443. pass_len = strlen(pass);
  444. }
  445. if (!ascii_to_ucs2(pass, pass_len, &pass_raw, &pass_raw_len)) {
  446. OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_asc, PKCS8_R_DECODE_ERROR);
  447. return NULL;
  448. }
  449. }
  450. ret = PKCS8_encrypt_pbe(pbe_nid, pass_raw, pass_raw_len,
  451. salt, salt_len, iterations, p8inf);
  452. if (pass_raw) {
  453. OPENSSL_cleanse(pass_raw, pass_raw_len);
  454. OPENSSL_free(pass_raw);
  455. }
  456. return ret;
  457. }
  458. X509_SIG *PKCS8_encrypt_pbe(int pbe_nid,
  459. const uint8_t *pass_raw, size_t pass_raw_len,
  460. uint8_t *salt, size_t salt_len,
  461. int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
  462. X509_SIG *pkcs8 = NULL;
  463. X509_ALGOR *pbe;
  464. pkcs8 = X509_SIG_new();
  465. if (pkcs8 == NULL) {
  466. OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt_pbe, ERR_R_MALLOC_FAILURE);
  467. goto err;
  468. }
  469. pbe = PKCS5_pbe_set(pbe_nid, iterations, salt, salt_len);
  470. if (!pbe) {
  471. OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt_pbe, ERR_R_ASN1_LIB);
  472. goto err;
  473. }
  474. X509_ALGOR_free(pkcs8->algor);
  475. pkcs8->algor = pbe;
  476. M_ASN1_OCTET_STRING_free(pkcs8->digest);
  477. pkcs8->digest = pkcs12_item_i2d_encrypt(
  478. pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw, pass_raw_len, p8inf);
  479. if (!pkcs8->digest) {
  480. OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt_pbe, PKCS8_R_ENCRYPT_ERROR);
  481. goto err;
  482. }
  483. return pkcs8;
  484. err:
  485. X509_SIG_free(pkcs8);
  486. return NULL;
  487. }
  488. EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
  489. EVP_PKEY *pkey = NULL;
  490. ASN1_OBJECT *algoid;
  491. char obj_tmp[80];
  492. if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
  493. return NULL;
  494. pkey = EVP_PKEY_new();
  495. if (pkey == NULL) {
  496. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  497. return NULL;
  498. }
  499. if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
  500. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY,
  501. PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  502. i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
  503. ERR_add_error_data(2, "TYPE=", obj_tmp);
  504. goto error;
  505. }
  506. if (pkey->ameth->priv_decode) {
  507. if (!pkey->ameth->priv_decode(pkey, p8)) {
  508. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, PKCS8_R_PRIVATE_KEY_DECODE_ERROR);
  509. goto error;
  510. }
  511. } else {
  512. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, PKCS8_R_METHOD_NOT_SUPPORTED);
  513. goto error;
  514. }
  515. return pkey;
  516. error:
  517. EVP_PKEY_free(pkey);
  518. return NULL;
  519. }
  520. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) {
  521. PKCS8_PRIV_KEY_INFO *p8;
  522. p8 = PKCS8_PRIV_KEY_INFO_new();
  523. if (p8 == NULL) {
  524. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  525. return NULL;
  526. }
  527. p8->broken = PKCS8_OK;
  528. if (pkey->ameth) {
  529. if (pkey->ameth->priv_encode) {
  530. if (!pkey->ameth->priv_encode(p8, pkey)) {
  531. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8,
  532. PKCS8_R_PRIVATE_KEY_ENCODE_ERROR);
  533. goto error;
  534. }
  535. } else {
  536. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8, PKCS8_R_METHOD_NOT_SUPPORTED);
  537. goto error;
  538. }
  539. } else {
  540. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8,
  541. PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  542. goto error;
  543. }
  544. return p8;
  545. error:
  546. PKCS8_PRIV_KEY_INFO_free(p8);
  547. return NULL;
  548. }
  549. struct pkcs12_context {
  550. EVP_PKEY **out_key;
  551. STACK_OF(X509) *out_certs;
  552. uint8_t *password;
  553. size_t password_len;
  554. };
  555. static int PKCS12_handle_content_info(CBS *content_info, unsigned depth,
  556. struct pkcs12_context *ctx);
  557. /* PKCS12_handle_content_infos parses a series of PKCS#7 ContentInfos in a
  558. * SEQUENCE. */
  559. static int PKCS12_handle_content_infos(CBS *content_infos,
  560. unsigned depth,
  561. struct pkcs12_context *ctx) {
  562. uint8_t *der_bytes = NULL;
  563. size_t der_len;
  564. CBS in;
  565. int ret = 0;
  566. /* Generally we only expect depths 0 (the top level, with a
  567. * pkcs7-encryptedData and a pkcs7-data) and depth 1 (the various PKCS#12
  568. * bags). */
  569. if (depth > 3) {
  570. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_infos,
  571. PKCS8_R_PKCS12_TOO_DEEPLY_NESTED);
  572. return 0;
  573. }
  574. /* Although a BER->DER conversion is done at the beginning of |PKCS12_parse|,
  575. * the ASN.1 data gets wrapped in OCTETSTRINGs and/or encrypted and the
  576. * conversion cannot see through those wrappings. So each time we step
  577. * through one we need to convert to DER again. */
  578. if (!CBS_asn1_ber_to_der(content_infos, &der_bytes, &der_len)) {
  579. return 0;
  580. }
  581. if (der_bytes != NULL) {
  582. CBS_init(&in, der_bytes, der_len);
  583. } else {
  584. CBS_init(&in, CBS_data(content_infos), CBS_len(content_infos));
  585. }
  586. if (!CBS_get_asn1(&in, &in, CBS_ASN1_SEQUENCE)) {
  587. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_infos,
  588. PKCS8_R_BAD_PKCS12_DATA);
  589. goto err;
  590. }
  591. while (CBS_len(&in) > 0) {
  592. CBS content_info;
  593. if (!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE)) {
  594. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_infos,
  595. PKCS8_R_BAD_PKCS12_DATA);
  596. goto err;
  597. }
  598. if (!PKCS12_handle_content_info(&content_info, depth + 1, ctx)) {
  599. goto err;
  600. }
  601. }
  602. /* NSS includes additional data after the SEQUENCE, but it's an (unwrapped)
  603. * copy of the same encrypted private key (with the same IV and
  604. * ciphertext)! */
  605. ret = 1;
  606. err:
  607. if (der_bytes != NULL) {
  608. OPENSSL_free(der_bytes);
  609. }
  610. return ret;
  611. }
  612. /* PKCS12_handle_content_info parses a single PKCS#7 ContentInfo element in a
  613. * PKCS#12 structure. */
  614. static int PKCS12_handle_content_info(CBS *content_info, unsigned depth,
  615. struct pkcs12_context *ctx) {
  616. CBS content_type, wrapped_contents, contents, content_infos;
  617. int nid, ret = 0;
  618. if (!CBS_get_asn1(content_info, &content_type, CBS_ASN1_OBJECT) ||
  619. !CBS_get_asn1(content_info, &wrapped_contents,
  620. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  621. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  622. goto err;
  623. }
  624. nid = OBJ_cbs2nid(&content_type);
  625. if (nid == NID_pkcs7_encrypted) {
  626. /* See https://tools.ietf.org/html/rfc2315#section-13.
  627. *
  628. * PKCS#7 encrypted data inside a PKCS#12 structure is generally an
  629. * encrypted certificate bag and it's generally encrypted with 40-bit
  630. * RC2-CBC. */
  631. CBS version_bytes, eci, contents_type, ai, encrypted_contents;
  632. X509_ALGOR *algor = NULL;
  633. const uint8_t *inp;
  634. uint8_t *out;
  635. size_t out_len;
  636. if (!CBS_get_asn1(&wrapped_contents, &contents, CBS_ASN1_SEQUENCE) ||
  637. !CBS_get_asn1(&contents, &version_bytes, CBS_ASN1_INTEGER) ||
  638. /* EncryptedContentInfo, see
  639. * https://tools.ietf.org/html/rfc2315#section-10.1 */
  640. !CBS_get_asn1(&contents, &eci, CBS_ASN1_SEQUENCE) ||
  641. !CBS_get_asn1(&eci, &contents_type, CBS_ASN1_OBJECT) ||
  642. /* AlgorithmIdentifier, see
  643. * https://tools.ietf.org/html/rfc5280#section-4.1.1.2 */
  644. !CBS_get_asn1_element(&eci, &ai, CBS_ASN1_SEQUENCE) ||
  645. !CBS_get_asn1(&eci, &encrypted_contents,
  646. CBS_ASN1_CONTEXT_SPECIFIC | 0)) {
  647. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  648. PKCS8_R_BAD_PKCS12_DATA);
  649. goto err;
  650. }
  651. if (OBJ_cbs2nid(&contents_type) != NID_pkcs7_data) {
  652. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  653. PKCS8_R_BAD_PKCS12_DATA);
  654. goto err;
  655. }
  656. inp = CBS_data(&ai);
  657. algor = d2i_X509_ALGOR(NULL, &inp, CBS_len(&ai));
  658. if (algor == NULL) {
  659. goto err;
  660. }
  661. if (inp != CBS_data(&ai) + CBS_len(&ai)) {
  662. X509_ALGOR_free(algor);
  663. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  664. PKCS8_R_BAD_PKCS12_DATA);
  665. goto err;
  666. }
  667. if (!pbe_crypt(algor, ctx->password, ctx->password_len,
  668. CBS_data(&encrypted_contents), CBS_len(&encrypted_contents),
  669. &out, &out_len, 0 /* decrypt */)) {
  670. X509_ALGOR_free(algor);
  671. goto err;
  672. }
  673. X509_ALGOR_free(algor);
  674. CBS_init(&content_infos, out, out_len);
  675. ret = PKCS12_handle_content_infos(&content_infos, depth + 1, ctx);
  676. OPENSSL_free(out);
  677. } else if (nid == NID_pkcs7_data) {
  678. CBS octet_string_contents;
  679. if (!CBS_get_asn1(&wrapped_contents, &octet_string_contents,
  680. CBS_ASN1_OCTETSTRING)) {
  681. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  682. PKCS8_R_BAD_PKCS12_DATA);
  683. goto err;
  684. }
  685. ret = PKCS12_handle_content_infos(&octet_string_contents, depth + 1, ctx);
  686. } else if (nid == NID_pkcs8ShroudedKeyBag) {
  687. /* See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section
  688. * 4.2.2. */
  689. const uint8_t *inp = CBS_data(&wrapped_contents);
  690. PKCS8_PRIV_KEY_INFO *pki = NULL;
  691. X509_SIG *encrypted = NULL;
  692. if (*ctx->out_key) {
  693. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  694. PKCS8_R_MULTIPLE_PRIVATE_KEYS_IN_PKCS12);
  695. goto err;
  696. }
  697. /* encrypted isn't actually an X.509 signature, but it has the same
  698. * structure as one and so |X509_SIG| is reused to store it. */
  699. encrypted = d2i_X509_SIG(NULL, &inp, CBS_len(&wrapped_contents));
  700. if (encrypted == NULL) {
  701. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  702. PKCS8_R_BAD_PKCS12_DATA);
  703. goto err;
  704. }
  705. if (inp != CBS_data(&wrapped_contents) + CBS_len(&wrapped_contents)) {
  706. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  707. PKCS8_R_BAD_PKCS12_DATA);
  708. X509_SIG_free(encrypted);
  709. goto err;
  710. }
  711. pki = PKCS8_decrypt_pbe(encrypted, ctx->password, ctx->password_len);
  712. X509_SIG_free(encrypted);
  713. if (pki == NULL) {
  714. goto err;
  715. }
  716. *ctx->out_key = EVP_PKCS82PKEY(pki);
  717. PKCS8_PRIV_KEY_INFO_free(pki);
  718. if (ctx->out_key == NULL) {
  719. goto err;
  720. }
  721. ret = 1;
  722. } else if (nid == NID_certBag) {
  723. CBS cert_bag, cert_type, wrapped_cert, cert;
  724. if (!CBS_get_asn1(&wrapped_contents, &cert_bag, CBS_ASN1_SEQUENCE) ||
  725. !CBS_get_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) ||
  726. !CBS_get_asn1(&cert_bag, &wrapped_cert,
  727. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  728. !CBS_get_asn1(&wrapped_cert, &cert, CBS_ASN1_OCTETSTRING)) {
  729. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  730. PKCS8_R_BAD_PKCS12_DATA);
  731. goto err;
  732. }
  733. if (OBJ_cbs2nid(&cert_type) == NID_x509Certificate) {
  734. const uint8_t *inp = CBS_data(&cert);
  735. X509 *x509 = d2i_X509(NULL, &inp, CBS_len(&cert));
  736. if (!x509) {
  737. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  738. PKCS8_R_BAD_PKCS12_DATA);
  739. goto err;
  740. }
  741. if (inp != CBS_data(&cert) + CBS_len(&cert)) {
  742. OPENSSL_PUT_ERROR(PKCS8, PKCS12_handle_content_info,
  743. PKCS8_R_BAD_PKCS12_DATA);
  744. X509_free(x509);
  745. goto err;
  746. }
  747. if (0 == sk_X509_push(ctx->out_certs, x509)) {
  748. X509_free(x509);
  749. goto err;
  750. }
  751. }
  752. ret = 1;
  753. } else {
  754. /* Unknown element type - ignore it. */
  755. ret = 1;
  756. }
  757. err:
  758. return ret;
  759. }
  760. int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs,
  761. CBS *ber_in, const char *password) {
  762. uint8_t *der_bytes = NULL;
  763. size_t der_len;
  764. CBS in, pfx, mac_data, authsafe, content_type, wrapped_authsafes, authsafes;
  765. uint64_t version;
  766. int ret = 0;
  767. struct pkcs12_context ctx;
  768. const size_t original_out_certs_len = sk_X509_num(out_certs);
  769. /* The input may be in BER format. */
  770. if (!CBS_asn1_ber_to_der(ber_in, &der_bytes, &der_len)) {
  771. return 0;
  772. }
  773. if (der_bytes != NULL) {
  774. CBS_init(&in, der_bytes, der_len);
  775. } else {
  776. CBS_init(&in, CBS_data(ber_in), CBS_len(ber_in));
  777. }
  778. *out_key = NULL;
  779. memset(&ctx, 0, sizeof(ctx));
  780. /* See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section
  781. * four. */
  782. if (!CBS_get_asn1(&in, &pfx, CBS_ASN1_SEQUENCE) ||
  783. CBS_len(&in) != 0 ||
  784. !CBS_get_asn1_uint64(&pfx, &version)) {
  785. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  786. goto err;
  787. }
  788. if (version < 3) {
  789. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_VERSION);
  790. goto err;
  791. }
  792. if (!CBS_get_asn1(&pfx, &authsafe, CBS_ASN1_SEQUENCE)) {
  793. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  794. goto err;
  795. }
  796. if (CBS_len(&pfx) == 0) {
  797. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_MISSING_MAC);
  798. goto err;
  799. }
  800. if (!CBS_get_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE)) {
  801. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  802. goto err;
  803. }
  804. /* authsafe is a PKCS#7 ContentInfo. See
  805. * https://tools.ietf.org/html/rfc2315#section-7. */
  806. if (!CBS_get_asn1(&authsafe, &content_type, CBS_ASN1_OBJECT) ||
  807. !CBS_get_asn1(&authsafe, &wrapped_authsafes,
  808. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  809. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  810. goto err;
  811. }
  812. /* The content type can either be |NID_pkcs7_data| or |NID_pkcs7_signed|. The
  813. * latter indicates that it's signed by a public key, which isn't
  814. * supported. */
  815. if (OBJ_cbs2nid(&content_type) != NID_pkcs7_data) {
  816. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse,
  817. PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED);
  818. goto err;
  819. }
  820. if (!CBS_get_asn1(&wrapped_authsafes, &authsafes, CBS_ASN1_OCTETSTRING)) {
  821. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  822. goto err;
  823. }
  824. ctx.out_key = out_key;
  825. ctx.out_certs = out_certs;
  826. if (!ascii_to_ucs2(password, strlen(password), &ctx.password,
  827. &ctx.password_len)) {
  828. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_DECODE_ERROR);
  829. goto err;
  830. }
  831. /* Verify the MAC. */
  832. {
  833. CBS mac, hash_type_seq, hash_oid, salt, expected_mac;
  834. uint64_t iterations;
  835. int hash_nid;
  836. const EVP_MD *md;
  837. uint8_t hmac_key[EVP_MAX_MD_SIZE];
  838. uint8_t hmac[EVP_MAX_MD_SIZE];
  839. unsigned hmac_len;
  840. if (!CBS_get_asn1(&mac_data, &mac, CBS_ASN1_SEQUENCE) ||
  841. !CBS_get_asn1(&mac, &hash_type_seq, CBS_ASN1_SEQUENCE) ||
  842. !CBS_get_asn1(&hash_type_seq, &hash_oid, CBS_ASN1_OBJECT) ||
  843. !CBS_get_asn1(&mac, &expected_mac, CBS_ASN1_OCTETSTRING) ||
  844. !CBS_get_asn1(&mac_data, &salt, CBS_ASN1_OCTETSTRING)) {
  845. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  846. goto err;
  847. }
  848. /* The iteration count is optional and the default is one. */
  849. iterations = 1;
  850. if (CBS_len(&mac_data) > 0) {
  851. if (!CBS_get_asn1_uint64(&mac_data, &iterations) ||
  852. iterations > INT_MAX) {
  853. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_BAD_PKCS12_DATA);
  854. goto err;
  855. }
  856. }
  857. hash_nid = OBJ_cbs2nid(&hash_oid);
  858. if (hash_nid == NID_undef ||
  859. (md = EVP_get_digestbynid(hash_nid)) == NULL) {
  860. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_UNKNOWN_HASH);
  861. goto err;
  862. }
  863. if (!pkcs12_key_gen_raw(ctx.password, ctx.password_len, CBS_data(&salt),
  864. CBS_len(&salt), PKCS12_MAC_ID, iterations,
  865. EVP_MD_size(md), hmac_key, md)) {
  866. goto err;
  867. }
  868. if (NULL == HMAC(md, hmac_key, EVP_MD_size(md), CBS_data(&authsafes),
  869. CBS_len(&authsafes), hmac, &hmac_len)) {
  870. goto err;
  871. }
  872. if (!CBS_mem_equal(&expected_mac, hmac, hmac_len)) {
  873. OPENSSL_PUT_ERROR(PKCS8, PKCS12_parse, PKCS8_R_INCORRECT_PASSWORD);
  874. goto err;
  875. }
  876. }
  877. /* authsafes contains a series of PKCS#7 ContentInfos. */
  878. if (!PKCS12_handle_content_infos(&authsafes, 0, &ctx)) {
  879. goto err;
  880. }
  881. ret = 1;
  882. err:
  883. if (ctx.password) {
  884. OPENSSL_free(ctx.password);
  885. }
  886. if (der_bytes) {
  887. OPENSSL_free(der_bytes);
  888. }
  889. if (!ret) {
  890. if (*out_key) {
  891. EVP_PKEY_free(*out_key);
  892. *out_key = NULL;
  893. }
  894. while (sk_X509_num(out_certs) > original_out_certs_len) {
  895. X509 *x509 = sk_X509_pop(out_certs);
  896. X509_free(x509);
  897. }
  898. }
  899. return ret;
  900. }
  901. void PKCS12_PBE_add(void) {}
  902. struct pkcs12_st {
  903. uint8_t *ber_bytes;
  904. size_t ber_len;
  905. };
  906. PKCS12* d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes, size_t ber_len) {
  907. PKCS12 *p12;
  908. /* out_p12 must be NULL because we don't export the PKCS12 structure. */
  909. assert(out_p12 == NULL);
  910. p12 = OPENSSL_malloc(sizeof(PKCS12));
  911. if (!p12) {
  912. return NULL;
  913. }
  914. p12->ber_bytes = OPENSSL_malloc(ber_len);
  915. if (!p12->ber_bytes) {
  916. OPENSSL_free(p12);
  917. return NULL;
  918. }
  919. memcpy(p12->ber_bytes, *ber_bytes, ber_len);
  920. p12->ber_len = ber_len;
  921. *ber_bytes += ber_len;
  922. return p12;
  923. }
  924. PKCS12* d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12) {
  925. size_t used = 0;
  926. BUF_MEM *buf;
  927. const uint8_t *dummy;
  928. static const size_t kMaxSize = 256 * 1024;
  929. PKCS12 *ret = NULL;
  930. buf = BUF_MEM_new();
  931. if (buf == NULL) {
  932. return NULL;
  933. }
  934. if (BUF_MEM_grow(buf, 8192) == 0) {
  935. goto out;
  936. }
  937. for (;;) {
  938. int n = BIO_read(bio, &buf->data[used], buf->length - used);
  939. if (n < 0) {
  940. goto out;
  941. }
  942. if (n == 0) {
  943. break;
  944. }
  945. used += n;
  946. if (used < buf->length) {
  947. continue;
  948. }
  949. if (buf->length > kMaxSize ||
  950. BUF_MEM_grow(buf, buf->length * 2) == 0) {
  951. goto out;
  952. }
  953. }
  954. dummy = (uint8_t*) buf->data;
  955. ret = d2i_PKCS12(out_p12, &dummy, used);
  956. out:
  957. BUF_MEM_free(buf);
  958. return ret;
  959. }
  960. PKCS12* d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12) {
  961. BIO *bio;
  962. PKCS12 *ret;
  963. bio = BIO_new_fp(fp, 0 /* don't take ownership */);
  964. if (!bio) {
  965. return NULL;
  966. }
  967. ret = d2i_PKCS12_bio(bio, out_p12);
  968. BIO_free(bio);
  969. return ret;
  970. }
  971. int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey,
  972. X509 **out_cert, STACK_OF(X509) **out_ca_certs) {
  973. CBS ber_bytes;
  974. STACK_OF(X509) *ca_certs = NULL;
  975. char ca_certs_alloced = 0;
  976. if (out_ca_certs != NULL && *out_ca_certs != NULL) {
  977. ca_certs = *out_ca_certs;
  978. }
  979. if (!ca_certs) {
  980. ca_certs = sk_X509_new_null();
  981. if (ca_certs == NULL) {
  982. return 0;
  983. }
  984. ca_certs_alloced = 1;
  985. }
  986. CBS_init(&ber_bytes, p12->ber_bytes, p12->ber_len);
  987. if (!PKCS12_get_key_and_certs(out_pkey, ca_certs, &ber_bytes, password)) {
  988. if (ca_certs_alloced) {
  989. sk_X509_free(ca_certs);
  990. }
  991. return 0;
  992. }
  993. *out_cert = NULL;
  994. if (sk_X509_num(ca_certs) > 0) {
  995. *out_cert = sk_X509_shift(ca_certs);
  996. }
  997. if (out_ca_certs) {
  998. *out_ca_certs = ca_certs;
  999. } else {
  1000. sk_X509_pop_free(ca_certs, X509_free);
  1001. }
  1002. return 1;
  1003. }
  1004. void PKCS12_free(PKCS12 *p12) {
  1005. OPENSSL_free(p12->ber_bytes);
  1006. OPENSSL_free(p12);
  1007. }