tls1_prf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright 2016-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. /*
  10. * Refer to "The TLS Protocol Version 1.0" Section 5
  11. * (https://tools.ietf.org/html/rfc2246#section-5) and
  12. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  13. * (https://tools.ietf.org/html/rfc5246#section-5).
  14. *
  15. * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
  16. *
  17. * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
  18. * P_SHA-1(S2, label + seed)
  19. *
  20. * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
  21. * two halves of the secret (with the possibility of one shared byte, in the
  22. * case where the length of the original secret is odd). S1 is taken from the
  23. * first half of the secret, S2 from the second half.
  24. *
  25. * For TLS v1.2 the TLS PRF algorithm is given by:
  26. *
  27. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  28. *
  29. * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
  30. * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
  31. * unless defined otherwise by the cipher suite.
  32. *
  33. * P_<hash> is an expansion function that uses a single hash function to expand
  34. * a secret and seed into an arbitrary quantity of output:
  35. *
  36. * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
  37. * HMAC_<hash>(secret, A(2) + seed) +
  38. * HMAC_<hash>(secret, A(3) + seed) + ...
  39. *
  40. * where + indicates concatenation. P_<hash> can be iterated as many times as
  41. * is necessary to produce the required quantity of data.
  42. *
  43. * A(i) is defined as:
  44. * A(0) = seed
  45. * A(i) = HMAC_<hash>(secret, A(i-1))
  46. */
  47. /*
  48. * Low level APIs (such as DH) are deprecated for public use, but still ok for
  49. * internal use.
  50. */
  51. #include "internal/deprecated.h"
  52. #include <stdio.h>
  53. #include <stdarg.h>
  54. #include <string.h>
  55. #include <openssl/evp.h>
  56. #include <openssl/kdf.h>
  57. #include <openssl/core_names.h>
  58. #include <openssl/params.h>
  59. #include <openssl/proverr.h>
  60. #include "internal/cryptlib.h"
  61. #include "internal/numbers.h"
  62. #include "crypto/evp.h"
  63. #include "prov/provider_ctx.h"
  64. #include "prov/providercommon.h"
  65. #include "prov/implementations.h"
  66. #include "prov/provider_util.h"
  67. #include "prov/securitycheck.h"
  68. #include "internal/e_os.h"
  69. #include "internal/safe_math.h"
  70. OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
  71. static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
  72. static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup;
  73. static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
  74. static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
  75. static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
  76. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
  77. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
  78. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
  79. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
  80. static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
  81. const unsigned char *sec, size_t slen,
  82. const unsigned char *seed, size_t seed_len,
  83. unsigned char *out, size_t olen);
  84. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"
  85. #define TLS_MD_MASTER_SECRET_CONST_SIZE 13
  86. /* TLS KDF kdf context structure */
  87. typedef struct {
  88. void *provctx;
  89. /* MAC context for the main digest */
  90. EVP_MAC_CTX *P_hash;
  91. /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
  92. EVP_MAC_CTX *P_sha1;
  93. /* Secret value to use for PRF */
  94. unsigned char *sec;
  95. size_t seclen;
  96. /* Concatenated seed data */
  97. unsigned char *seed;
  98. size_t seedlen;
  99. } TLS1_PRF;
  100. static void *kdf_tls1_prf_new(void *provctx)
  101. {
  102. TLS1_PRF *ctx;
  103. if (!ossl_prov_is_running())
  104. return NULL;
  105. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL)
  106. ctx->provctx = provctx;
  107. return ctx;
  108. }
  109. static void kdf_tls1_prf_free(void *vctx)
  110. {
  111. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  112. if (ctx != NULL) {
  113. kdf_tls1_prf_reset(ctx);
  114. OPENSSL_free(ctx);
  115. }
  116. }
  117. static void kdf_tls1_prf_reset(void *vctx)
  118. {
  119. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  120. void *provctx = ctx->provctx;
  121. EVP_MAC_CTX_free(ctx->P_hash);
  122. EVP_MAC_CTX_free(ctx->P_sha1);
  123. OPENSSL_clear_free(ctx->sec, ctx->seclen);
  124. OPENSSL_clear_free(ctx->seed, ctx->seedlen);
  125. memset(ctx, 0, sizeof(*ctx));
  126. ctx->provctx = provctx;
  127. }
  128. static void *kdf_tls1_prf_dup(void *vctx)
  129. {
  130. const TLS1_PRF *src = (const TLS1_PRF *)vctx;
  131. TLS1_PRF *dest;
  132. dest = kdf_tls1_prf_new(src->provctx);
  133. if (dest != NULL) {
  134. if (src->P_hash != NULL
  135. && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
  136. goto err;
  137. if (src->P_sha1 != NULL
  138. && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
  139. goto err;
  140. if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
  141. goto err;
  142. if (!ossl_prov_memdup(src->seed, src->seedlen, &dest->seed,
  143. &dest->seedlen))
  144. goto err;
  145. }
  146. return dest;
  147. err:
  148. kdf_tls1_prf_free(dest);
  149. return NULL;
  150. }
  151. static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
  152. const OSSL_PARAM params[])
  153. {
  154. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  155. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  156. if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
  157. return 0;
  158. if (ctx->P_hash == NULL) {
  159. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  160. return 0;
  161. }
  162. if (ctx->sec == NULL) {
  163. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
  164. return 0;
  165. }
  166. if (ctx->seedlen == 0) {
  167. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
  168. return 0;
  169. }
  170. if (keylen == 0) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  172. return 0;
  173. }
  174. /*
  175. * The seed buffer is prepended with a label.
  176. * If EMS mode is enforced then the label "master secret" is not allowed,
  177. * We do the check this way since the PRF is used for other purposes, as well
  178. * as "extended master secret".
  179. */
  180. if (ossl_tls1_prf_ems_check_enabled(libctx)) {
  181. if (ctx->seedlen >= TLS_MD_MASTER_SECRET_CONST_SIZE
  182. && memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,
  183. TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) {
  184. ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);
  185. return 0;
  186. }
  187. }
  188. return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
  189. ctx->sec, ctx->seclen,
  190. ctx->seed, ctx->seedlen,
  191. key, keylen);
  192. }
  193. static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  194. {
  195. const OSSL_PARAM *p;
  196. TLS1_PRF *ctx = vctx;
  197. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  198. if (params == NULL)
  199. return 1;
  200. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
  201. if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
  202. if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
  203. OSSL_MAC_NAME_HMAC,
  204. NULL, SN_md5, libctx)
  205. || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
  206. OSSL_MAC_NAME_HMAC,
  207. NULL, SN_sha1, libctx))
  208. return 0;
  209. } else {
  210. EVP_MAC_CTX_free(ctx->P_sha1);
  211. if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
  212. OSSL_MAC_NAME_HMAC,
  213. NULL, NULL, libctx))
  214. return 0;
  215. }
  216. }
  217. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
  218. OPENSSL_clear_free(ctx->sec, ctx->seclen);
  219. ctx->sec = NULL;
  220. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
  221. return 0;
  222. }
  223. /* The seed fields concatenate, so process them all */
  224. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
  225. for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
  226. OSSL_KDF_PARAM_SEED)) {
  227. if (p->data_size != 0 && p->data != NULL) {
  228. const void *val = NULL;
  229. size_t sz = 0;
  230. unsigned char *seed;
  231. size_t seedlen;
  232. int err = 0;
  233. if (!OSSL_PARAM_get_octet_string_ptr(p, &val, &sz))
  234. return 0;
  235. seedlen = safe_add_size_t(ctx->seedlen, sz, &err);
  236. if (err)
  237. return 0;
  238. seed = OPENSSL_clear_realloc(ctx->seed, ctx->seedlen, seedlen);
  239. if (!seed)
  240. return 0;
  241. ctx->seed = seed;
  242. if (ossl_assert(sz != 0))
  243. memcpy(ctx->seed + ctx->seedlen, val, sz);
  244. ctx->seedlen = seedlen;
  245. }
  246. }
  247. }
  248. return 1;
  249. }
  250. static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
  251. ossl_unused void *ctx, ossl_unused void *provctx)
  252. {
  253. static const OSSL_PARAM known_settable_ctx_params[] = {
  254. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  255. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  256. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  257. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
  258. OSSL_PARAM_END
  259. };
  260. return known_settable_ctx_params;
  261. }
  262. static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  263. {
  264. OSSL_PARAM *p;
  265. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  266. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  267. return -2;
  268. }
  269. static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
  270. ossl_unused void *ctx, ossl_unused void *provctx)
  271. {
  272. static const OSSL_PARAM known_gettable_ctx_params[] = {
  273. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  274. OSSL_PARAM_END
  275. };
  276. return known_gettable_ctx_params;
  277. }
  278. const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
  279. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
  280. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup },
  281. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
  282. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
  283. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
  284. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  285. (void(*)(void))kdf_tls1_prf_settable_ctx_params },
  286. { OSSL_FUNC_KDF_SET_CTX_PARAMS,
  287. (void(*)(void))kdf_tls1_prf_set_ctx_params },
  288. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  289. (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
  290. { OSSL_FUNC_KDF_GET_CTX_PARAMS,
  291. (void(*)(void))kdf_tls1_prf_get_ctx_params },
  292. OSSL_DISPATCH_END
  293. };
  294. /*
  295. * Refer to "The TLS Protocol Version 1.0" Section 5
  296. * (https://tools.ietf.org/html/rfc2246#section-5) and
  297. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  298. * (https://tools.ietf.org/html/rfc5246#section-5).
  299. *
  300. * P_<hash> is an expansion function that uses a single hash function to expand
  301. * a secret and seed into an arbitrary quantity of output:
  302. *
  303. * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
  304. * HMAC_<hash>(secret, A(2) + seed) +
  305. * HMAC_<hash>(secret, A(3) + seed) + ...
  306. *
  307. * where + indicates concatenation. P_<hash> can be iterated as many times as
  308. * is necessary to produce the required quantity of data.
  309. *
  310. * A(i) is defined as:
  311. * A(0) = seed
  312. * A(i) = HMAC_<hash>(secret, A(i-1))
  313. */
  314. static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
  315. const unsigned char *sec, size_t sec_len,
  316. const unsigned char *seed, size_t seed_len,
  317. unsigned char *out, size_t olen)
  318. {
  319. size_t chunk;
  320. EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
  321. unsigned char Ai[EVP_MAX_MD_SIZE];
  322. size_t Ai_len;
  323. int ret = 0;
  324. if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
  325. goto err;
  326. chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
  327. if (chunk == 0)
  328. goto err;
  329. /* A(0) = seed */
  330. ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
  331. if (ctx_Ai == NULL)
  332. goto err;
  333. if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
  334. goto err;
  335. for (;;) {
  336. /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
  337. if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
  338. goto err;
  339. EVP_MAC_CTX_free(ctx_Ai);
  340. ctx_Ai = NULL;
  341. /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
  342. ctx = EVP_MAC_CTX_dup(ctx_init);
  343. if (ctx == NULL)
  344. goto err;
  345. if (!EVP_MAC_update(ctx, Ai, Ai_len))
  346. goto err;
  347. /* save state for calculating next A(i) value */
  348. if (olen > chunk) {
  349. ctx_Ai = EVP_MAC_CTX_dup(ctx);
  350. if (ctx_Ai == NULL)
  351. goto err;
  352. }
  353. if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
  354. goto err;
  355. if (olen <= chunk) {
  356. /* last chunk - use Ai as temp bounce buffer */
  357. if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
  358. goto err;
  359. memcpy(out, Ai, olen);
  360. break;
  361. }
  362. if (!EVP_MAC_final(ctx, out, NULL, olen))
  363. goto err;
  364. EVP_MAC_CTX_free(ctx);
  365. ctx = NULL;
  366. out += chunk;
  367. olen -= chunk;
  368. }
  369. ret = 1;
  370. err:
  371. EVP_MAC_CTX_free(ctx);
  372. EVP_MAC_CTX_free(ctx_Ai);
  373. OPENSSL_cleanse(Ai, sizeof(Ai));
  374. return ret;
  375. }
  376. /*
  377. * Refer to "The TLS Protocol Version 1.0" Section 5
  378. * (https://tools.ietf.org/html/rfc2246#section-5) and
  379. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  380. * (https://tools.ietf.org/html/rfc5246#section-5).
  381. *
  382. * For TLS v1.0 and TLS v1.1:
  383. *
  384. * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
  385. * P_SHA-1(S2, label + seed)
  386. *
  387. * S1 is taken from the first half of the secret, S2 from the second half.
  388. *
  389. * L_S = length in bytes of secret;
  390. * L_S1 = L_S2 = ceil(L_S / 2);
  391. *
  392. * For TLS v1.2:
  393. *
  394. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  395. */
  396. static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
  397. const unsigned char *sec, size_t slen,
  398. const unsigned char *seed, size_t seed_len,
  399. unsigned char *out, size_t olen)
  400. {
  401. if (sha1ctx != NULL) {
  402. /* TLS v1.0 and TLS v1.1 */
  403. size_t i;
  404. unsigned char *tmp;
  405. /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
  406. size_t L_S1 = (slen + 1) / 2;
  407. size_t L_S2 = L_S1;
  408. if (!tls1_prf_P_hash(mdctx, sec, L_S1,
  409. seed, seed_len, out, olen))
  410. return 0;
  411. if ((tmp = OPENSSL_malloc(olen)) == NULL)
  412. return 0;
  413. if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
  414. seed, seed_len, tmp, olen)) {
  415. OPENSSL_clear_free(tmp, olen);
  416. return 0;
  417. }
  418. for (i = 0; i < olen; i++)
  419. out[i] ^= tmp[i];
  420. OPENSSL_clear_free(tmp, olen);
  421. return 1;
  422. }
  423. /* TLS v1.2 */
  424. if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
  425. return 0;
  426. return 1;
  427. }