pkcs12kdf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 1999-2023 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_EVP_LIB);
  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. goto end;
  80. for (i = 0; i < v; i++)
  81. D[i] = id;
  82. p = I;
  83. for (i = 0; i < Slen; i++)
  84. *p++ = salt[i % saltlen];
  85. for (i = 0; i < Plen; i++)
  86. *p++ = pass[i % passlen];
  87. for (;;) {
  88. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  89. || !EVP_DigestUpdate(ctx, D, v)
  90. || !EVP_DigestUpdate(ctx, I, Ilen)
  91. || !EVP_DigestFinal_ex(ctx, Ai, NULL))
  92. goto end;
  93. for (iter_cnt = 1; iter_cnt < iter; iter_cnt++) {
  94. if (!EVP_DigestInit_ex(ctx, md_type, NULL)
  95. || !EVP_DigestUpdate(ctx, Ai, u)
  96. || !EVP_DigestFinal_ex(ctx, Ai, NULL))
  97. goto end;
  98. }
  99. memcpy(out, Ai, n < u ? n : u);
  100. if (u >= n) {
  101. ret = 1;
  102. break;
  103. }
  104. n -= u;
  105. out += u;
  106. for (j = 0; j < v; j++)
  107. B[j] = Ai[j % u];
  108. for (j = 0; j < Ilen; j += v) {
  109. unsigned char *Ij = I + j;
  110. uint16_t c = 1;
  111. /* Work out Ij = Ij + B + 1 */
  112. for (k = v; k > 0;) {
  113. k--;
  114. c += Ij[k] + B[k];
  115. Ij[k] = (unsigned char)c;
  116. c >>= 8;
  117. }
  118. }
  119. }
  120. end:
  121. OPENSSL_free(Ai);
  122. OPENSSL_free(B);
  123. OPENSSL_free(D);
  124. OPENSSL_free(I);
  125. EVP_MD_CTX_free(ctx);
  126. return ret;
  127. }
  128. static void *kdf_pkcs12_new(void *provctx)
  129. {
  130. KDF_PKCS12 *ctx;
  131. if (!ossl_prov_is_running())
  132. return NULL;
  133. ctx = OPENSSL_zalloc(sizeof(*ctx));
  134. if (ctx == NULL)
  135. return NULL;
  136. ctx->provctx = provctx;
  137. return ctx;
  138. }
  139. static void kdf_pkcs12_cleanup(KDF_PKCS12 *ctx)
  140. {
  141. ossl_prov_digest_reset(&ctx->digest);
  142. OPENSSL_free(ctx->salt);
  143. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  144. memset(ctx, 0, sizeof(*ctx));
  145. }
  146. static void kdf_pkcs12_free(void *vctx)
  147. {
  148. KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx;
  149. if (ctx != NULL) {
  150. kdf_pkcs12_cleanup(ctx);
  151. OPENSSL_free(ctx);
  152. }
  153. }
  154. static void kdf_pkcs12_reset(void *vctx)
  155. {
  156. KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx;
  157. void *provctx = ctx->provctx;
  158. kdf_pkcs12_cleanup(ctx);
  159. ctx->provctx = provctx;
  160. }
  161. static void *kdf_pkcs12_dup(void *vctx)
  162. {
  163. const KDF_PKCS12 *src = (const KDF_PKCS12 *)vctx;
  164. KDF_PKCS12 *dest;
  165. dest = kdf_pkcs12_new(src->provctx);
  166. if (dest != NULL) {
  167. if (!ossl_prov_memdup(src->salt, src->salt_len,
  168. &dest->salt, &dest->salt_len)
  169. || !ossl_prov_memdup(src->pass, src->pass_len,
  170. &dest->pass , &dest->pass_len)
  171. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  172. goto err;
  173. dest->iter = src->iter;
  174. dest->id = src->id;
  175. }
  176. return dest;
  177. err:
  178. kdf_pkcs12_free(dest);
  179. return NULL;
  180. }
  181. static int pkcs12kdf_set_membuf(unsigned char **buffer, size_t *buflen,
  182. const OSSL_PARAM *p)
  183. {
  184. OPENSSL_clear_free(*buffer, *buflen);
  185. *buffer = NULL;
  186. *buflen = 0;
  187. if (p->data_size == 0) {
  188. if ((*buffer = OPENSSL_malloc(1)) == NULL)
  189. return 0;
  190. } else if (p->data != NULL) {
  191. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. static int kdf_pkcs12_derive(void *vctx, unsigned char *key, size_t keylen,
  197. const OSSL_PARAM params[])
  198. {
  199. KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx;
  200. const EVP_MD *md;
  201. if (!ossl_prov_is_running() || !kdf_pkcs12_set_ctx_params(ctx, params))
  202. return 0;
  203. if (ctx->pass == NULL) {
  204. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  205. return 0;
  206. }
  207. if (ctx->salt == NULL) {
  208. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  209. return 0;
  210. }
  211. md = ossl_prov_digest_md(&ctx->digest);
  212. return pkcs12kdf_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
  213. ctx->id, ctx->iter, md, key, keylen);
  214. }
  215. static int kdf_pkcs12_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  216. {
  217. const OSSL_PARAM *p;
  218. KDF_PKCS12 *ctx = vctx;
  219. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  220. if (params == NULL)
  221. return 1;
  222. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  223. return 0;
  224. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  225. if (!pkcs12kdf_set_membuf(&ctx->pass, &ctx->pass_len, p))
  226. return 0;
  227. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  228. if (!pkcs12kdf_set_membuf(&ctx->salt, &ctx->salt_len, p))
  229. return 0;
  230. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS12_ID)) != NULL)
  231. if (!OSSL_PARAM_get_int(p, &ctx->id))
  232. return 0;
  233. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
  234. if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
  235. return 0;
  236. return 1;
  237. }
  238. static const OSSL_PARAM *kdf_pkcs12_settable_ctx_params(
  239. ossl_unused void *ctx, ossl_unused void *provctx)
  240. {
  241. static const OSSL_PARAM known_settable_ctx_params[] = {
  242. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  243. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  244. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  245. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  246. OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
  247. OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS12_ID, NULL),
  248. OSSL_PARAM_END
  249. };
  250. return known_settable_ctx_params;
  251. }
  252. static int kdf_pkcs12_get_ctx_params(void *vctx, OSSL_PARAM params[])
  253. {
  254. OSSL_PARAM *p;
  255. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  256. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  257. return -2;
  258. }
  259. static const OSSL_PARAM *kdf_pkcs12_gettable_ctx_params(
  260. ossl_unused void *ctx, ossl_unused void *provctx)
  261. {
  262. static const OSSL_PARAM known_gettable_ctx_params[] = {
  263. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  264. OSSL_PARAM_END
  265. };
  266. return known_gettable_ctx_params;
  267. }
  268. const OSSL_DISPATCH ossl_kdf_pkcs12_functions[] = {
  269. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pkcs12_new },
  270. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pkcs12_dup },
  271. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pkcs12_free },
  272. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pkcs12_reset },
  273. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pkcs12_derive },
  274. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  275. (void(*)(void))kdf_pkcs12_settable_ctx_params },
  276. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pkcs12_set_ctx_params },
  277. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  278. (void(*)(void))kdf_pkcs12_gettable_ctx_params },
  279. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pkcs12_get_ctx_params },
  280. OSSL_DISPATCH_END
  281. };