25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

524 lines
16 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/bytestring.h>
  60. #include <openssl/cipher.h>
  61. #include <openssl/digest.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/nid.h>
  65. #include <openssl/rand.h>
  66. #include "internal.h"
  67. #include "../internal.h"
  68. static int ascii_to_ucs2(const char *ascii, size_t ascii_len,
  69. uint8_t **out, size_t *out_len) {
  70. size_t ulen = ascii_len * 2 + 2;
  71. if (ascii_len * 2 < ascii_len || ulen < ascii_len * 2) {
  72. return 0;
  73. }
  74. uint8_t *unitmp = OPENSSL_malloc(ulen);
  75. if (unitmp == NULL) {
  76. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  77. return 0;
  78. }
  79. for (size_t i = 0; i < ulen - 2; i += 2) {
  80. unitmp[i] = 0;
  81. unitmp[i + 1] = ascii[i >> 1];
  82. }
  83. /* Terminate the result with a UCS-2 NUL. */
  84. unitmp[ulen - 2] = 0;
  85. unitmp[ulen - 1] = 0;
  86. *out_len = ulen;
  87. *out = unitmp;
  88. return 1;
  89. }
  90. int pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt,
  91. size_t salt_len, uint8_t id, unsigned iterations,
  92. size_t out_len, uint8_t *out, const EVP_MD *md) {
  93. /* See https://tools.ietf.org/html/rfc7292#appendix-B. Quoted parts of the
  94. * specification have errata applied and other typos fixed. */
  95. if (iterations < 1) {
  96. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
  97. return 0;
  98. }
  99. int ret = 0;
  100. EVP_MD_CTX ctx;
  101. EVP_MD_CTX_init(&ctx);
  102. uint8_t *pass_raw = NULL, *I = NULL;
  103. size_t pass_raw_len = 0, I_len = 0;
  104. /* If |pass| is NULL, we use the empty string rather than {0, 0} as the raw
  105. * password. */
  106. if (pass != NULL &&
  107. !ascii_to_ucs2(pass, pass_len, &pass_raw, &pass_raw_len)) {
  108. goto err;
  109. }
  110. /* In the spec, |block_size| is called "v", but measured in bits. */
  111. size_t block_size = EVP_MD_block_size(md);
  112. /* 1. Construct a string, D (the "diversifier"), by concatenating v/8 copies
  113. * of ID. */
  114. uint8_t D[EVP_MAX_MD_BLOCK_SIZE];
  115. OPENSSL_memset(D, id, block_size);
  116. /* 2. Concatenate copies of the salt together to create a string S of length
  117. * v(ceiling(s/v)) bits (the final copy of the salt may be truncated to
  118. * create S). Note that if the salt is the empty string, then so is S.
  119. *
  120. * 3. Concatenate copies of the password together to create a string P of
  121. * length v(ceiling(p/v)) bits (the final copy of the password may be
  122. * truncated to create P). Note that if the password is the empty string,
  123. * then so is P.
  124. *
  125. * 4. Set I=S||P to be the concatenation of S and P. */
  126. if (salt_len + block_size - 1 < salt_len ||
  127. pass_raw_len + block_size - 1 < pass_raw_len) {
  128. OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
  129. goto err;
  130. }
  131. size_t S_len = block_size * ((salt_len + block_size - 1) / block_size);
  132. size_t P_len = block_size * ((pass_raw_len + block_size - 1) / block_size);
  133. I_len = S_len + P_len;
  134. if (I_len < S_len) {
  135. OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
  136. goto err;
  137. }
  138. I = OPENSSL_malloc(I_len);
  139. if (I_len != 0 && I == NULL) {
  140. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  141. goto err;
  142. }
  143. for (size_t i = 0; i < S_len; i++) {
  144. I[i] = salt[i % salt_len];
  145. }
  146. for (size_t i = 0; i < P_len; i++) {
  147. I[i + S_len] = pass_raw[i % pass_raw_len];
  148. }
  149. while (out_len != 0) {
  150. /* A. Set A_i=H^r(D||I). (i.e., the r-th hash of D||I,
  151. * H(H(H(... H(D||I)))) */
  152. uint8_t A[EVP_MAX_MD_SIZE];
  153. unsigned A_len;
  154. if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
  155. !EVP_DigestUpdate(&ctx, D, block_size) ||
  156. !EVP_DigestUpdate(&ctx, I, I_len) ||
  157. !EVP_DigestFinal_ex(&ctx, A, &A_len)) {
  158. goto err;
  159. }
  160. for (unsigned iter = 1; iter < iterations; iter++) {
  161. if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
  162. !EVP_DigestUpdate(&ctx, A, A_len) ||
  163. !EVP_DigestFinal_ex(&ctx, A, &A_len)) {
  164. goto err;
  165. }
  166. }
  167. size_t todo = out_len < A_len ? out_len : A_len;
  168. OPENSSL_memcpy(out, A, todo);
  169. out += todo;
  170. out_len -= todo;
  171. if (out_len == 0) {
  172. break;
  173. }
  174. /* B. Concatenate copies of A_i to create a string B of length v bits (the
  175. * final copy of A_i may be truncated to create B). */
  176. uint8_t B[EVP_MAX_MD_BLOCK_SIZE];
  177. for (size_t i = 0; i < block_size; i++) {
  178. B[i] = A[i % A_len];
  179. }
  180. /* C. Treating I as a concatenation I_0, I_1, ..., I_(k-1) of v-bit blocks,
  181. * where k=ceiling(s/v)+ceiling(p/v), modify I by setting I_j=(I_j+B+1) mod
  182. * 2^v for each j. */
  183. assert(I_len % block_size == 0);
  184. for (size_t i = 0; i < I_len; i += block_size) {
  185. unsigned carry = 1;
  186. for (size_t j = block_size - 1; j < block_size; j--) {
  187. carry += I[i + j] + B[j];
  188. I[i + j] = (uint8_t)carry;
  189. carry >>= 8;
  190. }
  191. }
  192. }
  193. ret = 1;
  194. err:
  195. if (I != NULL) {
  196. OPENSSL_cleanse(I, I_len);
  197. OPENSSL_free(I);
  198. }
  199. if (pass_raw != NULL) {
  200. OPENSSL_cleanse(pass_raw, pass_raw_len);
  201. OPENSSL_free(pass_raw);
  202. }
  203. EVP_MD_CTX_cleanup(&ctx);
  204. return ret;
  205. }
  206. static int pkcs12_pbe_cipher_init(const struct pbe_suite *suite,
  207. EVP_CIPHER_CTX *ctx, unsigned iterations,
  208. const char *pass, size_t pass_len,
  209. const uint8_t *salt, size_t salt_len,
  210. int is_encrypt) {
  211. const EVP_CIPHER *cipher = suite->cipher_func();
  212. const EVP_MD *md = suite->md_func();
  213. uint8_t key[EVP_MAX_KEY_LENGTH];
  214. uint8_t iv[EVP_MAX_IV_LENGTH];
  215. if (!pkcs12_key_gen(pass, pass_len, salt, salt_len, PKCS12_KEY_ID, iterations,
  216. EVP_CIPHER_key_length(cipher), key, md) ||
  217. !pkcs12_key_gen(pass, pass_len, salt, salt_len, PKCS12_IV_ID, iterations,
  218. EVP_CIPHER_iv_length(cipher), iv, md)) {
  219. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR);
  220. return 0;
  221. }
  222. int ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt);
  223. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  224. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  225. return ret;
  226. }
  227. static int pkcs12_pbe_decrypt_init(const struct pbe_suite *suite,
  228. EVP_CIPHER_CTX *ctx, const char *pass,
  229. size_t pass_len, CBS *param) {
  230. CBS pbe_param, salt;
  231. uint64_t iterations;
  232. if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) ||
  233. !CBS_get_asn1(&pbe_param, &salt, CBS_ASN1_OCTETSTRING) ||
  234. !CBS_get_asn1_uint64(&pbe_param, &iterations) ||
  235. CBS_len(&pbe_param) != 0 ||
  236. CBS_len(param) != 0) {
  237. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  238. return 0;
  239. }
  240. if (iterations == 0 || iterations > UINT_MAX) {
  241. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
  242. return 0;
  243. }
  244. return pkcs12_pbe_cipher_init(suite, ctx, (unsigned)iterations, pass,
  245. pass_len, CBS_data(&salt), CBS_len(&salt),
  246. 0 /* decrypt */);
  247. }
  248. static const struct pbe_suite kBuiltinPBE[] = {
  249. {
  250. NID_pbe_WithSHA1And40BitRC2_CBC,
  251. /* 1.2.840.113549.1.12.1.6 */
  252. {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x06},
  253. 10,
  254. EVP_rc2_40_cbc,
  255. EVP_sha1,
  256. pkcs12_pbe_decrypt_init,
  257. },
  258. {
  259. NID_pbe_WithSHA1And128BitRC4,
  260. /* 1.2.840.113549.1.12.1.1 */
  261. {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x01},
  262. 10,
  263. EVP_rc4,
  264. EVP_sha1,
  265. pkcs12_pbe_decrypt_init,
  266. },
  267. {
  268. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  269. /* 1.2.840.113549.1.12.1.3 */
  270. {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03},
  271. 10,
  272. EVP_des_ede3_cbc,
  273. EVP_sha1,
  274. pkcs12_pbe_decrypt_init,
  275. },
  276. {
  277. NID_pbes2,
  278. /* 1.2.840.113549.1.5.13 */
  279. {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0d},
  280. 9,
  281. NULL,
  282. NULL,
  283. PKCS5_pbe2_decrypt_init,
  284. },
  285. };
  286. static const struct pbe_suite *get_pbe_suite(int pbe_nid) {
  287. for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) {
  288. if (kBuiltinPBE[i].pbe_nid == pbe_nid) {
  289. return &kBuiltinPBE[i];
  290. }
  291. }
  292. return NULL;
  293. }
  294. static int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg,
  295. unsigned iterations, const char *pass,
  296. size_t pass_len, const uint8_t *salt,
  297. size_t salt_len) {
  298. const struct pbe_suite *suite = get_pbe_suite(alg);
  299. if (suite == NULL) {
  300. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM);
  301. return 0;
  302. }
  303. /* See RFC 2898, appendix A.3. */
  304. CBB algorithm, oid, param, salt_cbb;
  305. if (!CBB_add_asn1(out, &algorithm, CBS_ASN1_SEQUENCE) ||
  306. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  307. !CBB_add_bytes(&oid, suite->oid, suite->oid_len) ||
  308. !CBB_add_asn1(&algorithm, &param, CBS_ASN1_SEQUENCE) ||
  309. !CBB_add_asn1(&param, &salt_cbb, CBS_ASN1_OCTETSTRING) ||
  310. !CBB_add_bytes(&salt_cbb, salt, salt_len) ||
  311. !CBB_add_asn1_uint64(&param, iterations) ||
  312. !CBB_flush(out)) {
  313. return 0;
  314. }
  315. return pkcs12_pbe_cipher_init(suite, ctx, iterations, pass, pass_len, salt,
  316. salt_len, 1 /* encrypt */);
  317. }
  318. int pkcs8_pbe_decrypt(uint8_t **out, size_t *out_len, CBS *algorithm,
  319. const char *pass, size_t pass_len, const uint8_t *in,
  320. size_t in_len) {
  321. int ret = 0;
  322. uint8_t *buf = NULL;;
  323. EVP_CIPHER_CTX ctx;
  324. EVP_CIPHER_CTX_init(&ctx);
  325. CBS obj;
  326. if (!CBS_get_asn1(algorithm, &obj, CBS_ASN1_OBJECT)) {
  327. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  328. goto err;
  329. }
  330. const struct pbe_suite *suite = NULL;
  331. for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) {
  332. if (CBS_mem_equal(&obj, kBuiltinPBE[i].oid, kBuiltinPBE[i].oid_len)) {
  333. suite = &kBuiltinPBE[i];
  334. break;
  335. }
  336. }
  337. if (suite == NULL) {
  338. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM);
  339. goto err;
  340. }
  341. if (!suite->decrypt_init(suite, &ctx, pass, pass_len, algorithm)) {
  342. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEYGEN_FAILURE);
  343. goto err;
  344. }
  345. buf = OPENSSL_malloc(in_len);
  346. if (buf == NULL) {
  347. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  348. goto err;
  349. }
  350. if (in_len > INT_MAX) {
  351. OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
  352. goto err;
  353. }
  354. int n1, n2;
  355. if (!EVP_DecryptUpdate(&ctx, buf, &n1, in, (int)in_len) ||
  356. !EVP_DecryptFinal_ex(&ctx, buf + n1, &n2)) {
  357. goto err;
  358. }
  359. *out = buf;
  360. *out_len = n1 + n2;
  361. ret = 1;
  362. buf = NULL;
  363. err:
  364. OPENSSL_free(buf);
  365. EVP_CIPHER_CTX_cleanup(&ctx);
  366. return ret;
  367. }
  368. EVP_PKEY *PKCS8_parse_encrypted_private_key(CBS *cbs, const char *pass,
  369. size_t pass_len) {
  370. /* See RFC 5208, section 6. */
  371. CBS epki, algorithm, ciphertext;
  372. if (!CBS_get_asn1(cbs, &epki, CBS_ASN1_SEQUENCE) ||
  373. !CBS_get_asn1(&epki, &algorithm, CBS_ASN1_SEQUENCE) ||
  374. !CBS_get_asn1(&epki, &ciphertext, CBS_ASN1_OCTETSTRING) ||
  375. CBS_len(&epki) != 0) {
  376. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  377. return 0;
  378. }
  379. uint8_t *out;
  380. size_t out_len;
  381. if (!pkcs8_pbe_decrypt(&out, &out_len, &algorithm, pass, pass_len,
  382. CBS_data(&ciphertext), CBS_len(&ciphertext))) {
  383. return 0;
  384. }
  385. CBS pki;
  386. CBS_init(&pki, out, out_len);
  387. EVP_PKEY *ret = EVP_parse_private_key(&pki);
  388. OPENSSL_cleanse(out, out_len);
  389. OPENSSL_free(out);
  390. return ret;
  391. }
  392. int PKCS8_marshal_encrypted_private_key(CBB *out, int pbe_nid,
  393. const EVP_CIPHER *cipher,
  394. const char *pass, size_t pass_len,
  395. const uint8_t *salt, size_t salt_len,
  396. int iterations, const EVP_PKEY *pkey) {
  397. int ret = 0;
  398. uint8_t *plaintext = NULL, *salt_buf = NULL;
  399. size_t plaintext_len = 0;
  400. EVP_CIPHER_CTX ctx;
  401. EVP_CIPHER_CTX_init(&ctx);
  402. /* Generate a random salt if necessary. */
  403. if (salt == NULL) {
  404. if (salt_len == 0) {
  405. salt_len = PKCS5_SALT_LEN;
  406. }
  407. salt_buf = OPENSSL_malloc(salt_len);
  408. if (salt_buf == NULL ||
  409. !RAND_bytes(salt_buf, salt_len)) {
  410. goto err;
  411. }
  412. salt = salt_buf;
  413. }
  414. if (iterations <= 0) {
  415. iterations = PKCS5_DEFAULT_ITERATIONS;
  416. }
  417. /* Serialize the input key. */
  418. CBB plaintext_cbb;
  419. if (!CBB_init(&plaintext_cbb, 128) ||
  420. !EVP_marshal_private_key(&plaintext_cbb, pkey) ||
  421. !CBB_finish(&plaintext_cbb, &plaintext, &plaintext_len)) {
  422. CBB_cleanup(&plaintext_cbb);
  423. goto err;
  424. }
  425. CBB epki;
  426. if (!CBB_add_asn1(out, &epki, CBS_ASN1_SEQUENCE)) {
  427. goto err;
  428. }
  429. int alg_ok;
  430. if (pbe_nid == -1) {
  431. alg_ok = PKCS5_pbe2_encrypt_init(&epki, &ctx, cipher, (unsigned)iterations,
  432. pass, pass_len, salt, salt_len);
  433. } else {
  434. alg_ok = pkcs12_pbe_encrypt_init(&epki, &ctx, pbe_nid, (unsigned)iterations,
  435. pass, pass_len, salt, salt_len);
  436. }
  437. if (!alg_ok) {
  438. goto err;
  439. }
  440. size_t max_out = plaintext_len + EVP_CIPHER_CTX_block_size(&ctx);
  441. if (max_out < plaintext_len) {
  442. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_TOO_LONG);
  443. goto err;
  444. }
  445. CBB ciphertext;
  446. uint8_t *ptr;
  447. int n1, n2;
  448. if (!CBB_add_asn1(&epki, &ciphertext, CBS_ASN1_OCTETSTRING) ||
  449. !CBB_reserve(&ciphertext, &ptr, max_out) ||
  450. !EVP_CipherUpdate(&ctx, ptr, &n1, plaintext, plaintext_len) ||
  451. !EVP_CipherFinal_ex(&ctx, ptr + n1, &n2) ||
  452. !CBB_did_write(&ciphertext, n1 + n2) ||
  453. !CBB_flush(out)) {
  454. goto err;
  455. }
  456. ret = 1;
  457. err:
  458. if (plaintext != NULL) {
  459. OPENSSL_cleanse(plaintext, plaintext_len);
  460. OPENSSL_free(plaintext);
  461. }
  462. OPENSSL_free(salt_buf);
  463. EVP_CIPHER_CTX_cleanup(&ctx);
  464. return ret;
  465. }