p12_key.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/pkcs12.h>
  12. #include <openssl/bn.h>
  13. /* Uncomment out this line to get debugging info about key generation */
  14. /*
  15. * #define OPENSSL_DEBUG_KEYGEN
  16. */
  17. #ifdef OPENSSL_DEBUG_KEYGEN
  18. # include <openssl/bio.h>
  19. extern BIO *bio_err;
  20. void h__dump(unsigned char *p, int len);
  21. #endif
  22. /* PKCS12 compatible key/IV generation */
  23. #ifndef min
  24. # define min(a,b) ((a) < (b) ? (a) : (b))
  25. #endif
  26. #if defined(WINSCP) && defined(PBE_UNICODE)
  27. #undef PKCS12_key_gen_uni
  28. int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
  29. int saltlen, int id, int iter, int n,
  30. unsigned char *out, const EVP_MD *md_type);
  31. int PKCS12_key_gen_wrap(unsigned char *pass, int passlen, unsigned char *salt,
  32. int saltlen, int id, int iter, int n,
  33. unsigned char *out, const EVP_MD *md_type)
  34. {
  35. if (pass == NULL)
  36. {
  37. // noop
  38. }
  39. // PKCS12_key_gen_uni cannot handle -1 length (contrary to PKCS12_key_gen_asc).
  40. // OPENSSL_asc2uni adds the trailing \0 to the length,
  41. // even if input ascii password length does not include it.
  42. else if (passlen < 0)
  43. {
  44. passlen = (wcslen((wchar_t*)pass) * sizeof(wchar_t)) + sizeof(wchar_t);
  45. }
  46. return PKCS12_key_gen_uni(pass, passlen, salt, saltlen, id, iter, n, out, md_type);
  47. }
  48. #endif
  49. int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
  50. int saltlen, int id, int iter, int n,
  51. unsigned char *out, const EVP_MD *md_type)
  52. {
  53. int ret;
  54. unsigned char *unipass;
  55. int uniplen;
  56. if (!pass) {
  57. unipass = NULL;
  58. uniplen = 0;
  59. } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
  60. PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE);
  61. return 0;
  62. }
  63. ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
  64. id, iter, n, out, md_type);
  65. if (ret <= 0)
  66. return 0;
  67. OPENSSL_clear_free(unipass, uniplen);
  68. return ret;
  69. }
  70. int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
  71. int saltlen, int id, int iter, int n,
  72. unsigned char *out, const EVP_MD *md_type)
  73. {
  74. int ret;
  75. unsigned char *unipass;
  76. int uniplen;
  77. if (!pass) {
  78. unipass = NULL;
  79. uniplen = 0;
  80. } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
  81. PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UTF8, ERR_R_MALLOC_FAILURE);
  82. return 0;
  83. }
  84. ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
  85. id, iter, n, out, md_type);
  86. if (ret <= 0)
  87. return 0;
  88. OPENSSL_clear_free(unipass, uniplen);
  89. return ret;
  90. }
  91. int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
  92. int saltlen, int id, int iter, int n,
  93. unsigned char *out, const EVP_MD *md_type)
  94. {
  95. unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
  96. int Slen, Plen, Ilen;
  97. int i, j, u, v;
  98. int ret = 0;
  99. EVP_MD_CTX *ctx = NULL;
  100. #ifdef OPENSSL_DEBUG_KEYGEN
  101. unsigned char *tmpout = out;
  102. int tmpn = n;
  103. #endif
  104. ctx = EVP_MD_CTX_new();
  105. if (ctx == NULL)
  106. goto err;
  107. #ifdef OPENSSL_DEBUG_KEYGEN
  108. fprintf(stderr, "KEYGEN DEBUG\n");
  109. fprintf(stderr, "ID %d, ITER %d\n", id, iter);
  110. fprintf(stderr, "Password (length %d):\n", passlen);
  111. h__dump(pass, passlen);
  112. fprintf(stderr, "Salt (length %d):\n", saltlen);
  113. h__dump(salt, saltlen);
  114. #endif
  115. v = EVP_MD_block_size(md_type);
  116. u = EVP_MD_size(md_type);
  117. if (u < 0 || v <= 0)
  118. goto err;
  119. D = OPENSSL_malloc(v);
  120. Ai = OPENSSL_malloc(u);
  121. B = OPENSSL_malloc(v + 1);
  122. Slen = v * ((saltlen + v - 1) / v);
  123. if (passlen)
  124. Plen = v * ((passlen + v - 1) / v);
  125. else
  126. Plen = 0;
  127. Ilen = Slen + Plen;
  128. I = OPENSSL_malloc(Ilen);
  129. if (D == NULL || Ai == NULL || B == NULL || I == NULL)
  130. goto err;
  131. for (i = 0; i < v; i++)
  132. D[i] = id;
  133. p = I;
  134. for (i = 0; i < Slen; i++)
  135. *p++ = salt[i % saltlen];
  136. for (i = 0; i < Plen; i++)
  137. *p++ = pass[i % passlen];
  138. for (;;) {
  139. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  140. || !EVP_DigestUpdate(ctx, D, v)
  141. || !EVP_DigestUpdate(ctx, I, Ilen)
  142. || !EVP_DigestFinal_ex(ctx, Ai, NULL))
  143. goto err;
  144. for (j = 1; j < iter; j++) {
  145. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  146. || !EVP_DigestUpdate(ctx, Ai, u)
  147. || !EVP_DigestFinal_ex(ctx, Ai, NULL))
  148. goto err;
  149. }
  150. memcpy(out, Ai, min(n, u));
  151. if (u >= n) {
  152. #ifdef OPENSSL_DEBUG_KEYGEN
  153. fprintf(stderr, "Output KEY (length %d)\n", tmpn);
  154. h__dump(tmpout, tmpn);
  155. #endif
  156. ret = 1;
  157. goto end;
  158. }
  159. n -= u;
  160. out += u;
  161. for (j = 0; j < v; j++)
  162. B[j] = Ai[j % u];
  163. for (j = 0; j < Ilen; j += v) {
  164. int k;
  165. unsigned char *Ij = I + j;
  166. uint16_t c = 1;
  167. /* Work out Ij = Ij + B + 1 */
  168. for (k = v - 1; k >= 0; k--) {
  169. c += Ij[k] + B[k];
  170. Ij[k] = (unsigned char)c;
  171. c >>= 8;
  172. }
  173. }
  174. }
  175. err:
  176. PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);
  177. end:
  178. OPENSSL_free(Ai);
  179. OPENSSL_free(B);
  180. OPENSSL_free(D);
  181. OPENSSL_free(I);
  182. EVP_MD_CTX_free(ctx);
  183. return ret;
  184. }
  185. #ifdef OPENSSL_DEBUG_KEYGEN
  186. void h__dump(unsigned char *p, int len)
  187. {
  188. for (; len--; p++)
  189. fprintf(stderr, "%02X", *p);
  190. fprintf(stderr, "\n");
  191. }
  192. #endif