tls1_prf.c 15 KB

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