pkcs12kdf.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <openssl/trace.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include <string.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/proverr.h>
  17. #include "internal/cryptlib.h"
  18. #include "internal/numbers.h"
  19. #include "crypto/evp.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/providercommon.h"
  22. #include "prov/implementations.h"
  23. #include "prov/provider_util.h"
  24. static OSSL_FUNC_kdf_newctx_fn kdf_pkcs12_new;
  25. static OSSL_FUNC_kdf_dupctx_fn kdf_pkcs12_dup;
  26. static OSSL_FUNC_kdf_freectx_fn kdf_pkcs12_free;
  27. static OSSL_FUNC_kdf_reset_fn kdf_pkcs12_reset;
  28. static OSSL_FUNC_kdf_derive_fn kdf_pkcs12_derive;
  29. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pkcs12_settable_ctx_params;
  30. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pkcs12_set_ctx_params;
  31. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pkcs12_gettable_ctx_params;
  32. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pkcs12_get_ctx_params;
  33. typedef struct {
  34. void *provctx;
  35. PROV_DIGEST digest;
  36. unsigned char *pass;
  37. size_t pass_len;
  38. unsigned char *salt;
  39. size_t salt_len;
  40. uint64_t iter;
  41. int id;
  42. } KDF_PKCS12;
  43. /* PKCS12 compatible key/IV generation */
  44. static int pkcs12kdf_derive(const unsigned char *pass, size_t passlen,
  45. const unsigned char *salt, size_t saltlen,
  46. int id, uint64_t iter, const EVP_MD *md_type,
  47. unsigned char *out, size_t n)
  48. {
  49. unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
  50. size_t Slen, Plen, Ilen;
  51. size_t i, j, k, u, v;
  52. uint64_t iter_cnt;
  53. int ret = 0, ui, vi;
  54. EVP_MD_CTX *ctx = NULL;
  55. ctx = EVP_MD_CTX_new();
  56. if (ctx == NULL) {
  57. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  58. goto end;
  59. }
  60. vi = EVP_MD_get_block_size(md_type);
  61. ui = EVP_MD_get_size(md_type);
  62. if (ui <= 0 || vi <= 0) {
  63. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);
  64. goto end;
  65. }
  66. u = (size_t)ui;
  67. v = (size_t)vi;
  68. D = OPENSSL_malloc(v);
  69. Ai = OPENSSL_malloc(u);
  70. B = OPENSSL_malloc(v + 1);
  71. Slen = v * ((saltlen + v - 1) / v);
  72. if (passlen != 0)
  73. Plen = v * ((passlen + v - 1) / v);
  74. else
  75. Plen = 0;
  76. Ilen = Slen + Plen;
  77. I = OPENSSL_malloc(Ilen);
  78. if (D == NULL || Ai == NULL || B == NULL || I == NULL) {
  79. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  80. goto end;
  81. }
  82. for (i = 0; i < v; i++)
  83. D[i] = id;
  84. p = I;
  85. for (i = 0; i < Slen; i++)
  86. *p++ = salt[i % saltlen];
  87. for (i = 0; i < Plen; i++)
  88. *p++ = pass[i % passlen];
  89. for (;;) {
  90. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  91. || !EVP_DigestUpdate(ctx, D, v)
  92. || !EVP_DigestUpdate(ctx, I, Ilen)
  93. || !EVP_DigestFinal_ex(ctx, Ai, NULL))
  94. goto end;
  95. for (iter_cnt = 1; iter_cnt < iter; iter_cnt++) {
  96. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  97. || !EVP_DigestUpdate(ctx, Ai, u)
  98. || !EVP_DigestFinal_ex(ctx, Ai, NULL))
  99. goto end;
  100. }
  101. memcpy(out, Ai, n < u ? n : u);
  102. if (u >= n) {
  103. ret = 1;
  104. break;
  105. }
  106. n -= u;
  107. out += u;
  108. for (j = 0; j < v; j++)
  109. B[j] = Ai[j % u];
  110. for (j = 0; j < Ilen; j += v) {
  111. unsigned char *Ij = I + j;
  112. uint16_t c = 1;
  113. /* Work out Ij = Ij + B + 1 */
  114. for (k = v; k > 0;) {
  115. k--;
  116. c += Ij[k] + B[k];
  117. Ij[k] = (unsigned char)c;
  118. c >>= 8;
  119. }
  120. }
  121. }
  122. end:
  123. OPENSSL_free(Ai);
  124. OPENSSL_free(B);
  125. OPENSSL_free(D);
  126. OPENSSL_free(I);
  127. EVP_MD_CTX_free(ctx);
  128. return ret;
  129. }
  130. static void *kdf_pkcs12_new(void *provctx)
  131. {
  132. KDF_PKCS12 *ctx;
  133. if (!ossl_prov_is_running())
  134. return NULL;
  135. ctx = OPENSSL_zalloc(sizeof(*ctx));
  136. if (ctx == NULL) {
  137. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  138. return NULL;
  139. }
  140. ctx->provctx = provctx;
  141. return ctx;
  142. }
  143. static void kdf_pkcs12_cleanup(KDF_PKCS12 *ctx)
  144. {
  145. ossl_prov_digest_reset(&ctx->digest);
  146. OPENSSL_free(ctx->salt);
  147. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  148. memset(ctx, 0, sizeof(*ctx));
  149. }
  150. static void kdf_pkcs12_free(void *vctx)
  151. {
  152. KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx;
  153. if (ctx != NULL) {
  154. kdf_pkcs12_cleanup(ctx);
  155. OPENSSL_free(ctx);
  156. }
  157. }
  158. static void kdf_pkcs12_reset(void *vctx)
  159. {
  160. KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx;
  161. void *provctx = ctx->provctx;
  162. kdf_pkcs12_cleanup(ctx);
  163. ctx->provctx = provctx;
  164. }
  165. static void *kdf_pkcs12_dup(void *vctx)
  166. {
  167. const KDF_PKCS12 *src = (const KDF_PKCS12 *)vctx;
  168. KDF_PKCS12 *dest;
  169. dest = kdf_pkcs12_new(src->provctx);
  170. if (dest != NULL) {
  171. if (!ossl_prov_memdup(src->salt, src->salt_len,
  172. &dest->salt, &dest->salt_len)
  173. || !ossl_prov_memdup(src->pass, src->pass_len,
  174. &dest->pass , &dest->pass_len)
  175. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  176. goto err;
  177. dest->iter = src->iter;
  178. dest->id = src->id;
  179. }
  180. return dest;
  181. err:
  182. kdf_pkcs12_free(dest);
  183. return NULL;
  184. }
  185. static int pkcs12kdf_set_membuf(unsigned char **buffer, size_t *buflen,
  186. const OSSL_PARAM *p)
  187. {
  188. OPENSSL_clear_free(*buffer, *buflen);
  189. *buffer = NULL;
  190. *buflen = 0;
  191. if (p->data_size == 0) {
  192. if ((*buffer = OPENSSL_malloc(1)) == NULL) {
  193. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  194. return 0;
  195. }
  196. } else if (p->data != NULL) {
  197. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. static int kdf_pkcs12_derive(void *vctx, unsigned char *key, size_t keylen,
  203. const OSSL_PARAM params[])
  204. {
  205. KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx;
  206. const EVP_MD *md;
  207. if (!ossl_prov_is_running() || !kdf_pkcs12_set_ctx_params(ctx, params))
  208. return 0;
  209. if (ctx->pass == NULL) {
  210. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  211. return 0;
  212. }
  213. if (ctx->salt == NULL) {
  214. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  215. return 0;
  216. }
  217. md = ossl_prov_digest_md(&ctx->digest);
  218. return pkcs12kdf_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
  219. ctx->id, ctx->iter, md, key, keylen);
  220. }
  221. static int kdf_pkcs12_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  222. {
  223. const OSSL_PARAM *p;
  224. KDF_PKCS12 *ctx = vctx;
  225. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  226. if (params == NULL)
  227. return 1;
  228. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  229. return 0;
  230. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  231. if (!pkcs12kdf_set_membuf(&ctx->pass, &ctx->pass_len, p))
  232. return 0;
  233. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  234. if (!pkcs12kdf_set_membuf(&ctx->salt, &ctx->salt_len, p))
  235. return 0;
  236. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS12_ID)) != NULL)
  237. if (!OSSL_PARAM_get_int(p, &ctx->id))
  238. return 0;
  239. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
  240. if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
  241. return 0;
  242. return 1;
  243. }
  244. static const OSSL_PARAM *kdf_pkcs12_settable_ctx_params(
  245. ossl_unused void *ctx, ossl_unused void *provctx)
  246. {
  247. static const OSSL_PARAM known_settable_ctx_params[] = {
  248. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  249. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  250. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  251. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  252. OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
  253. OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS12_ID, NULL),
  254. OSSL_PARAM_END
  255. };
  256. return known_settable_ctx_params;
  257. }
  258. static int kdf_pkcs12_get_ctx_params(void *vctx, OSSL_PARAM params[])
  259. {
  260. OSSL_PARAM *p;
  261. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  262. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  263. return -2;
  264. }
  265. static const OSSL_PARAM *kdf_pkcs12_gettable_ctx_params(
  266. ossl_unused void *ctx, ossl_unused void *provctx)
  267. {
  268. static const OSSL_PARAM known_gettable_ctx_params[] = {
  269. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  270. OSSL_PARAM_END
  271. };
  272. return known_gettable_ctx_params;
  273. }
  274. const OSSL_DISPATCH ossl_kdf_pkcs12_functions[] = {
  275. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pkcs12_new },
  276. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pkcs12_dup },
  277. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pkcs12_free },
  278. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pkcs12_reset },
  279. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pkcs12_derive },
  280. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  281. (void(*)(void))kdf_pkcs12_settable_ctx_params },
  282. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pkcs12_set_ctx_params },
  283. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  284. (void(*)(void))kdf_pkcs12_gettable_ctx_params },
  285. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pkcs12_get_ctx_params },
  286. { 0, NULL }
  287. };