Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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