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.
 
 
 
 
 
 

790 lines
22 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 <limits.h>
  57. #include <openssl/asn1t.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/buf.h>
  61. #include <openssl/bytestring.h>
  62. #include <openssl/err.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/digest.h>
  65. #include <openssl/hmac.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/x509.h>
  68. #include "internal.h"
  69. #include "../bytestring/internal.h"
  70. #include "../internal.h"
  71. // Minor tweak to operation: zero private key data
  72. static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  73. void *exarg) {
  74. // Since the structure must still be valid use ASN1_OP_FREE_PRE
  75. if (operation == ASN1_OP_FREE_PRE) {
  76. PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
  77. if (key->pkey && key->pkey->type == V_ASN1_OCTET_STRING &&
  78. key->pkey->value.octet_string) {
  79. OPENSSL_cleanse(key->pkey->value.octet_string->data,
  80. key->pkey->value.octet_string->length);
  81. }
  82. }
  83. return 1;
  84. }
  85. ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
  86. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
  87. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
  88. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_ANY),
  89. ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0)
  90. } ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
  91. IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
  92. EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
  93. uint8_t *der = NULL;
  94. int der_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &der);
  95. if (der_len < 0) {
  96. return NULL;
  97. }
  98. CBS cbs;
  99. CBS_init(&cbs, der, (size_t)der_len);
  100. EVP_PKEY *ret = EVP_parse_private_key(&cbs);
  101. if (ret == NULL || CBS_len(&cbs) != 0) {
  102. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  103. EVP_PKEY_free(ret);
  104. OPENSSL_free(der);
  105. return NULL;
  106. }
  107. OPENSSL_free(der);
  108. return ret;
  109. }
  110. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) {
  111. CBB cbb;
  112. uint8_t *der = NULL;
  113. size_t der_len;
  114. if (!CBB_init(&cbb, 0) ||
  115. !EVP_marshal_private_key(&cbb, pkey) ||
  116. !CBB_finish(&cbb, &der, &der_len) ||
  117. der_len > LONG_MAX) {
  118. CBB_cleanup(&cbb);
  119. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCODE_ERROR);
  120. goto err;
  121. }
  122. const uint8_t *p = der;
  123. PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, (long)der_len);
  124. if (p8 == NULL || p != der + der_len) {
  125. PKCS8_PRIV_KEY_INFO_free(p8);
  126. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  127. goto err;
  128. }
  129. OPENSSL_free(der);
  130. return p8;
  131. err:
  132. OPENSSL_free(der);
  133. return NULL;
  134. }
  135. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass,
  136. int pass_len_in) {
  137. size_t pass_len;
  138. if (pass_len_in == -1 && pass != NULL) {
  139. pass_len = strlen(pass);
  140. } else {
  141. pass_len = (size_t)pass_len_in;
  142. }
  143. PKCS8_PRIV_KEY_INFO *ret = NULL;
  144. EVP_PKEY *pkey = NULL;
  145. uint8_t *in = NULL;
  146. // Convert the legacy ASN.1 object to a byte string.
  147. int in_len = i2d_X509_SIG(pkcs8, &in);
  148. if (in_len < 0) {
  149. goto err;
  150. }
  151. CBS cbs;
  152. CBS_init(&cbs, in, in_len);
  153. pkey = PKCS8_parse_encrypted_private_key(&cbs, pass, pass_len);
  154. if (pkey == NULL || CBS_len(&cbs) != 0) {
  155. goto err;
  156. }
  157. ret = EVP_PKEY2PKCS8(pkey);
  158. err:
  159. OPENSSL_free(in);
  160. EVP_PKEY_free(pkey);
  161. return ret;
  162. }
  163. X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass,
  164. int pass_len_in, const uint8_t *salt, size_t salt_len,
  165. int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
  166. size_t pass_len;
  167. if (pass_len_in == -1 && pass != NULL) {
  168. pass_len = strlen(pass);
  169. } else {
  170. pass_len = (size_t)pass_len_in;
  171. }
  172. // Parse out the private key.
  173. EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf);
  174. if (pkey == NULL) {
  175. return NULL;
  176. }
  177. X509_SIG *ret = NULL;
  178. uint8_t *der = NULL;
  179. size_t der_len;
  180. CBB cbb;
  181. if (!CBB_init(&cbb, 128) ||
  182. !PKCS8_marshal_encrypted_private_key(&cbb, pbe_nid, cipher, pass,
  183. pass_len, salt, salt_len, iterations,
  184. pkey) ||
  185. !CBB_finish(&cbb, &der, &der_len)) {
  186. CBB_cleanup(&cbb);
  187. goto err;
  188. }
  189. // Convert back to legacy ASN.1 objects.
  190. const uint8_t *ptr = der;
  191. ret = d2i_X509_SIG(NULL, &ptr, der_len);
  192. if (ret == NULL || ptr != der + der_len) {
  193. OPENSSL_PUT_ERROR(PKCS8, ERR_R_INTERNAL_ERROR);
  194. X509_SIG_free(ret);
  195. ret = NULL;
  196. }
  197. err:
  198. OPENSSL_free(der);
  199. EVP_PKEY_free(pkey);
  200. return ret;
  201. }
  202. struct pkcs12_context {
  203. EVP_PKEY **out_key;
  204. STACK_OF(X509) *out_certs;
  205. const char *password;
  206. size_t password_len;
  207. };
  208. // PKCS12_handle_sequence parses a BER-encoded SEQUENCE of elements in a PKCS#12
  209. // structure.
  210. static int PKCS12_handle_sequence(
  211. CBS *sequence, struct pkcs12_context *ctx,
  212. int (*handle_element)(CBS *cbs, struct pkcs12_context *ctx)) {
  213. uint8_t *der_bytes = NULL;
  214. size_t der_len;
  215. CBS in;
  216. int ret = 0;
  217. // Although a BER->DER conversion is done at the beginning of |PKCS12_parse|,
  218. // the ASN.1 data gets wrapped in OCTETSTRINGs and/or encrypted and the
  219. // conversion cannot see through those wrappings. So each time we step
  220. // through one we need to convert to DER again.
  221. if (!CBS_asn1_ber_to_der(sequence, &der_bytes, &der_len)) {
  222. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  223. return 0;
  224. }
  225. if (der_bytes != NULL) {
  226. CBS_init(&in, der_bytes, der_len);
  227. } else {
  228. CBS_init(&in, CBS_data(sequence), CBS_len(sequence));
  229. }
  230. CBS child;
  231. if (!CBS_get_asn1(&in, &child, CBS_ASN1_SEQUENCE) ||
  232. CBS_len(&in) != 0) {
  233. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  234. goto err;
  235. }
  236. while (CBS_len(&child) > 0) {
  237. CBS element;
  238. if (!CBS_get_asn1(&child, &element, CBS_ASN1_SEQUENCE)) {
  239. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  240. goto err;
  241. }
  242. if (!handle_element(&element, ctx)) {
  243. goto err;
  244. }
  245. }
  246. ret = 1;
  247. err:
  248. OPENSSL_free(der_bytes);
  249. return ret;
  250. }
  251. // 1.2.840.113549.1.12.10.1.2
  252. static const uint8_t kPKCS8ShroudedKeyBag[] = {
  253. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02};
  254. // 1.2.840.113549.1.12.10.1.3
  255. static const uint8_t kCertBag[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  256. 0x01, 0x0c, 0x0a, 0x01, 0x03};
  257. // 1.2.840.113549.1.9.22.1
  258. static const uint8_t kX509Certificate[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  259. 0x0d, 0x01, 0x09, 0x16, 0x01};
  260. // PKCS12_handle_safe_bag parses a single SafeBag element in a PKCS#12
  261. // structure.
  262. static int PKCS12_handle_safe_bag(CBS *safe_bag, struct pkcs12_context *ctx) {
  263. CBS bag_id, wrapped_value;
  264. if (!CBS_get_asn1(safe_bag, &bag_id, CBS_ASN1_OBJECT) ||
  265. !CBS_get_asn1(safe_bag, &wrapped_value,
  266. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)
  267. /* Ignore the bagAttributes field. */) {
  268. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  269. return 0;
  270. }
  271. if (CBS_mem_equal(&bag_id, kPKCS8ShroudedKeyBag,
  272. sizeof(kPKCS8ShroudedKeyBag))) {
  273. // See RFC 7292, section 4.2.2.
  274. if (*ctx->out_key) {
  275. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MULTIPLE_PRIVATE_KEYS_IN_PKCS12);
  276. return 0;
  277. }
  278. EVP_PKEY *pkey = PKCS8_parse_encrypted_private_key(
  279. &wrapped_value, ctx->password, ctx->password_len);
  280. if (pkey == NULL) {
  281. return 0;
  282. }
  283. if (CBS_len(&wrapped_value) != 0) {
  284. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  285. EVP_PKEY_free(pkey);
  286. return 0;
  287. }
  288. *ctx->out_key = pkey;
  289. return 1;
  290. }
  291. if (CBS_mem_equal(&bag_id, kCertBag, sizeof(kCertBag))) {
  292. // See RFC 7292, section 4.2.3.
  293. CBS cert_bag, cert_type, wrapped_cert, cert;
  294. if (!CBS_get_asn1(&wrapped_value, &cert_bag, CBS_ASN1_SEQUENCE) ||
  295. !CBS_get_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) ||
  296. !CBS_get_asn1(&cert_bag, &wrapped_cert,
  297. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  298. !CBS_get_asn1(&wrapped_cert, &cert, CBS_ASN1_OCTETSTRING)) {
  299. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  300. return 0;
  301. }
  302. // Skip unknown certificate types.
  303. if (!CBS_mem_equal(&cert_type, kX509Certificate,
  304. sizeof(kX509Certificate))) {
  305. return 1;
  306. }
  307. if (CBS_len(&cert) > LONG_MAX) {
  308. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  309. return 0;
  310. }
  311. const uint8_t *inp = CBS_data(&cert);
  312. X509 *x509 = d2i_X509(NULL, &inp, (long)CBS_len(&cert));
  313. if (!x509) {
  314. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  315. return 0;
  316. }
  317. if (inp != CBS_data(&cert) + CBS_len(&cert)) {
  318. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  319. X509_free(x509);
  320. return 0;
  321. }
  322. if (0 == sk_X509_push(ctx->out_certs, x509)) {
  323. X509_free(x509);
  324. return 0;
  325. }
  326. return 1;
  327. }
  328. // Unknown element type - ignore it.
  329. return 1;
  330. }
  331. // 1.2.840.113549.1.7.1
  332. static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  333. 0x0d, 0x01, 0x07, 0x01};
  334. // 1.2.840.113549.1.7.6
  335. static const uint8_t kPKCS7EncryptedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  336. 0x0d, 0x01, 0x07, 0x06};
  337. // PKCS12_handle_content_info parses a single PKCS#7 ContentInfo element in a
  338. // PKCS#12 structure.
  339. static int PKCS12_handle_content_info(CBS *content_info,
  340. struct pkcs12_context *ctx) {
  341. CBS content_type, wrapped_contents, contents;
  342. int ret = 0;
  343. uint8_t *storage = NULL;
  344. if (!CBS_get_asn1(content_info, &content_type, CBS_ASN1_OBJECT) ||
  345. !CBS_get_asn1(content_info, &wrapped_contents,
  346. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  347. CBS_len(content_info) != 0) {
  348. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  349. goto err;
  350. }
  351. if (CBS_mem_equal(&content_type, kPKCS7EncryptedData,
  352. sizeof(kPKCS7EncryptedData))) {
  353. // See https://tools.ietf.org/html/rfc2315#section-13.
  354. //
  355. // PKCS#7 encrypted data inside a PKCS#12 structure is generally an
  356. // encrypted certificate bag and it's generally encrypted with 40-bit
  357. // RC2-CBC.
  358. CBS version_bytes, eci, contents_type, ai, encrypted_contents;
  359. uint8_t *out;
  360. size_t out_len;
  361. if (!CBS_get_asn1(&wrapped_contents, &contents, CBS_ASN1_SEQUENCE) ||
  362. !CBS_get_asn1(&contents, &version_bytes, CBS_ASN1_INTEGER) ||
  363. // EncryptedContentInfo, see
  364. // https://tools.ietf.org/html/rfc2315#section-10.1
  365. !CBS_get_asn1(&contents, &eci, CBS_ASN1_SEQUENCE) ||
  366. !CBS_get_asn1(&eci, &contents_type, CBS_ASN1_OBJECT) ||
  367. // AlgorithmIdentifier, see
  368. // https://tools.ietf.org/html/rfc5280#section-4.1.1.2
  369. !CBS_get_asn1(&eci, &ai, CBS_ASN1_SEQUENCE) ||
  370. !CBS_get_asn1_implicit_string(
  371. &eci, &encrypted_contents, &storage,
  372. CBS_ASN1_CONTEXT_SPECIFIC | 0, CBS_ASN1_OCTETSTRING)) {
  373. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  374. goto err;
  375. }
  376. if (!CBS_mem_equal(&contents_type, kPKCS7Data, sizeof(kPKCS7Data))) {
  377. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  378. goto err;
  379. }
  380. if (!pkcs8_pbe_decrypt(&out, &out_len, &ai, ctx->password,
  381. ctx->password_len, CBS_data(&encrypted_contents),
  382. CBS_len(&encrypted_contents))) {
  383. goto err;
  384. }
  385. CBS safe_contents;
  386. CBS_init(&safe_contents, out, out_len);
  387. ret = PKCS12_handle_sequence(&safe_contents, ctx, PKCS12_handle_safe_bag);
  388. OPENSSL_free(out);
  389. } else if (CBS_mem_equal(&content_type, kPKCS7Data, sizeof(kPKCS7Data))) {
  390. CBS octet_string_contents;
  391. if (!CBS_get_asn1(&wrapped_contents, &octet_string_contents,
  392. CBS_ASN1_OCTETSTRING)) {
  393. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  394. goto err;
  395. }
  396. ret = PKCS12_handle_sequence(&octet_string_contents, ctx,
  397. PKCS12_handle_safe_bag);
  398. } else {
  399. // Unknown element type - ignore it.
  400. ret = 1;
  401. }
  402. err:
  403. OPENSSL_free(storage);
  404. return ret;
  405. }
  406. int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs,
  407. CBS *ber_in, const char *password) {
  408. uint8_t *der_bytes = NULL;
  409. size_t der_len;
  410. CBS in, pfx, mac_data, authsafe, content_type, wrapped_authsafes, authsafes;
  411. uint64_t version;
  412. int ret = 0;
  413. struct pkcs12_context ctx;
  414. const size_t original_out_certs_len = sk_X509_num(out_certs);
  415. // The input may be in BER format.
  416. if (!CBS_asn1_ber_to_der(ber_in, &der_bytes, &der_len)) {
  417. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  418. return 0;
  419. }
  420. if (der_bytes != NULL) {
  421. CBS_init(&in, der_bytes, der_len);
  422. } else {
  423. CBS_init(&in, CBS_data(ber_in), CBS_len(ber_in));
  424. }
  425. *out_key = NULL;
  426. OPENSSL_memset(&ctx, 0, sizeof(ctx));
  427. // See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section
  428. // four.
  429. if (!CBS_get_asn1(&in, &pfx, CBS_ASN1_SEQUENCE) ||
  430. CBS_len(&in) != 0 ||
  431. !CBS_get_asn1_uint64(&pfx, &version)) {
  432. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  433. goto err;
  434. }
  435. if (version < 3) {
  436. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_VERSION);
  437. goto err;
  438. }
  439. if (!CBS_get_asn1(&pfx, &authsafe, CBS_ASN1_SEQUENCE)) {
  440. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  441. goto err;
  442. }
  443. if (CBS_len(&pfx) == 0) {
  444. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MISSING_MAC);
  445. goto err;
  446. }
  447. if (!CBS_get_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE)) {
  448. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  449. goto err;
  450. }
  451. // authsafe is a PKCS#7 ContentInfo. See
  452. // https://tools.ietf.org/html/rfc2315#section-7.
  453. if (!CBS_get_asn1(&authsafe, &content_type, CBS_ASN1_OBJECT) ||
  454. !CBS_get_asn1(&authsafe, &wrapped_authsafes,
  455. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  456. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  457. goto err;
  458. }
  459. // The content type can either be data or signedData. The latter indicates
  460. // that it's signed by a public key, which isn't supported.
  461. if (!CBS_mem_equal(&content_type, kPKCS7Data, sizeof(kPKCS7Data))) {
  462. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED);
  463. goto err;
  464. }
  465. if (!CBS_get_asn1(&wrapped_authsafes, &authsafes, CBS_ASN1_OCTETSTRING)) {
  466. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  467. goto err;
  468. }
  469. ctx.out_key = out_key;
  470. ctx.out_certs = out_certs;
  471. ctx.password = password;
  472. ctx.password_len = password != NULL ? strlen(password) : 0;
  473. // Verify the MAC.
  474. {
  475. CBS mac, salt, expected_mac;
  476. if (!CBS_get_asn1(&mac_data, &mac, CBS_ASN1_SEQUENCE)) {
  477. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  478. goto err;
  479. }
  480. const EVP_MD *md = EVP_parse_digest_algorithm(&mac);
  481. if (md == NULL) {
  482. goto err;
  483. }
  484. if (!CBS_get_asn1(&mac, &expected_mac, CBS_ASN1_OCTETSTRING) ||
  485. !CBS_get_asn1(&mac_data, &salt, CBS_ASN1_OCTETSTRING)) {
  486. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  487. goto err;
  488. }
  489. // The iteration count is optional and the default is one.
  490. uint64_t iterations = 1;
  491. if (CBS_len(&mac_data) > 0) {
  492. if (!CBS_get_asn1_uint64(&mac_data, &iterations) ||
  493. iterations > UINT_MAX) {
  494. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  495. goto err;
  496. }
  497. }
  498. uint8_t hmac_key[EVP_MAX_MD_SIZE];
  499. if (!pkcs12_key_gen(ctx.password, ctx.password_len, CBS_data(&salt),
  500. CBS_len(&salt), PKCS12_MAC_ID, iterations,
  501. EVP_MD_size(md), hmac_key, md)) {
  502. goto err;
  503. }
  504. uint8_t hmac[EVP_MAX_MD_SIZE];
  505. unsigned hmac_len;
  506. if (NULL == HMAC(md, hmac_key, EVP_MD_size(md), CBS_data(&authsafes),
  507. CBS_len(&authsafes), hmac, &hmac_len)) {
  508. goto err;
  509. }
  510. if (!CBS_mem_equal(&expected_mac, hmac, hmac_len)) {
  511. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INCORRECT_PASSWORD);
  512. goto err;
  513. }
  514. }
  515. // authsafes contains a series of PKCS#7 ContentInfos.
  516. if (!PKCS12_handle_sequence(&authsafes, &ctx, PKCS12_handle_content_info)) {
  517. goto err;
  518. }
  519. ret = 1;
  520. err:
  521. OPENSSL_free(der_bytes);
  522. if (!ret) {
  523. EVP_PKEY_free(*out_key);
  524. *out_key = NULL;
  525. while (sk_X509_num(out_certs) > original_out_certs_len) {
  526. X509 *x509 = sk_X509_pop(out_certs);
  527. X509_free(x509);
  528. }
  529. }
  530. return ret;
  531. }
  532. void PKCS12_PBE_add(void) {}
  533. struct pkcs12_st {
  534. uint8_t *ber_bytes;
  535. size_t ber_len;
  536. };
  537. PKCS12 *d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes,
  538. size_t ber_len) {
  539. PKCS12 *p12;
  540. p12 = OPENSSL_malloc(sizeof(PKCS12));
  541. if (!p12) {
  542. return NULL;
  543. }
  544. p12->ber_bytes = OPENSSL_malloc(ber_len);
  545. if (!p12->ber_bytes) {
  546. OPENSSL_free(p12);
  547. return NULL;
  548. }
  549. OPENSSL_memcpy(p12->ber_bytes, *ber_bytes, ber_len);
  550. p12->ber_len = ber_len;
  551. *ber_bytes += ber_len;
  552. if (out_p12) {
  553. PKCS12_free(*out_p12);
  554. *out_p12 = p12;
  555. }
  556. return p12;
  557. }
  558. PKCS12* d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12) {
  559. size_t used = 0;
  560. BUF_MEM *buf;
  561. const uint8_t *dummy;
  562. static const size_t kMaxSize = 256 * 1024;
  563. PKCS12 *ret = NULL;
  564. buf = BUF_MEM_new();
  565. if (buf == NULL) {
  566. return NULL;
  567. }
  568. if (BUF_MEM_grow(buf, 8192) == 0) {
  569. goto out;
  570. }
  571. for (;;) {
  572. int n = BIO_read(bio, &buf->data[used], buf->length - used);
  573. if (n < 0) {
  574. if (used == 0) {
  575. goto out;
  576. }
  577. // Workaround a bug in node.js. It uses a memory BIO for this in the wrong
  578. // mode.
  579. n = 0;
  580. }
  581. if (n == 0) {
  582. break;
  583. }
  584. used += n;
  585. if (used < buf->length) {
  586. continue;
  587. }
  588. if (buf->length > kMaxSize ||
  589. BUF_MEM_grow(buf, buf->length * 2) == 0) {
  590. goto out;
  591. }
  592. }
  593. dummy = (uint8_t*) buf->data;
  594. ret = d2i_PKCS12(out_p12, &dummy, used);
  595. out:
  596. BUF_MEM_free(buf);
  597. return ret;
  598. }
  599. PKCS12* d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12) {
  600. BIO *bio;
  601. PKCS12 *ret;
  602. bio = BIO_new_fp(fp, 0 /* don't take ownership */);
  603. if (!bio) {
  604. return NULL;
  605. }
  606. ret = d2i_PKCS12_bio(bio, out_p12);
  607. BIO_free(bio);
  608. return ret;
  609. }
  610. int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey,
  611. X509 **out_cert, STACK_OF(X509) **out_ca_certs) {
  612. CBS ber_bytes;
  613. STACK_OF(X509) *ca_certs = NULL;
  614. char ca_certs_alloced = 0;
  615. if (out_ca_certs != NULL && *out_ca_certs != NULL) {
  616. ca_certs = *out_ca_certs;
  617. }
  618. if (!ca_certs) {
  619. ca_certs = sk_X509_new_null();
  620. if (ca_certs == NULL) {
  621. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  622. return 0;
  623. }
  624. ca_certs_alloced = 1;
  625. }
  626. CBS_init(&ber_bytes, p12->ber_bytes, p12->ber_len);
  627. if (!PKCS12_get_key_and_certs(out_pkey, ca_certs, &ber_bytes, password)) {
  628. if (ca_certs_alloced) {
  629. sk_X509_free(ca_certs);
  630. }
  631. return 0;
  632. }
  633. *out_cert = NULL;
  634. if (sk_X509_num(ca_certs) > 0) {
  635. *out_cert = sk_X509_shift(ca_certs);
  636. }
  637. if (out_ca_certs) {
  638. *out_ca_certs = ca_certs;
  639. } else {
  640. sk_X509_pop_free(ca_certs, X509_free);
  641. }
  642. return 1;
  643. }
  644. int PKCS12_verify_mac(const PKCS12 *p12, const char *password,
  645. int password_len) {
  646. if (password == NULL) {
  647. if (password_len != 0) {
  648. return 0;
  649. }
  650. } else if (password_len != -1 &&
  651. (password[password_len] != 0 ||
  652. OPENSSL_memchr(password, 0, password_len) != NULL)) {
  653. return 0;
  654. }
  655. EVP_PKEY *pkey = NULL;
  656. X509 *cert = NULL;
  657. if (!PKCS12_parse(p12, password, &pkey, &cert, NULL)) {
  658. ERR_clear_error();
  659. return 0;
  660. }
  661. EVP_PKEY_free(pkey);
  662. X509_free(cert);
  663. return 1;
  664. }
  665. void PKCS12_free(PKCS12 *p12) {
  666. if (p12 == NULL) {
  667. return;
  668. }
  669. OPENSSL_free(p12->ber_bytes);
  670. OPENSSL_free(p12);
  671. }