tls1_prf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright 2016-2024 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. OSSL_FIPS_IND_DECLARE
  100. } TLS1_PRF;
  101. static void *kdf_tls1_prf_new(void *provctx)
  102. {
  103. TLS1_PRF *ctx;
  104. if (!ossl_prov_is_running())
  105. return NULL;
  106. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) {
  107. ctx->provctx = provctx;
  108. OSSL_FIPS_IND_INIT(ctx)
  109. }
  110. return ctx;
  111. }
  112. static void kdf_tls1_prf_free(void *vctx)
  113. {
  114. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  115. if (ctx != NULL) {
  116. kdf_tls1_prf_reset(ctx);
  117. OPENSSL_free(ctx);
  118. }
  119. }
  120. static void kdf_tls1_prf_reset(void *vctx)
  121. {
  122. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  123. void *provctx = ctx->provctx;
  124. EVP_MAC_CTX_free(ctx->P_hash);
  125. EVP_MAC_CTX_free(ctx->P_sha1);
  126. OPENSSL_clear_free(ctx->sec, ctx->seclen);
  127. OPENSSL_clear_free(ctx->seed, ctx->seedlen);
  128. memset(ctx, 0, sizeof(*ctx));
  129. ctx->provctx = provctx;
  130. }
  131. static void *kdf_tls1_prf_dup(void *vctx)
  132. {
  133. const TLS1_PRF *src = (const TLS1_PRF *)vctx;
  134. TLS1_PRF *dest;
  135. dest = kdf_tls1_prf_new(src->provctx);
  136. if (dest != NULL) {
  137. if (src->P_hash != NULL
  138. && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
  139. goto err;
  140. if (src->P_sha1 != NULL
  141. && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
  142. goto err;
  143. if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
  144. goto err;
  145. if (!ossl_prov_memdup(src->seed, src->seedlen, &dest->seed,
  146. &dest->seedlen))
  147. goto err;
  148. OSSL_FIPS_IND_COPY(dest, src)
  149. }
  150. return dest;
  151. err:
  152. kdf_tls1_prf_free(dest);
  153. return NULL;
  154. }
  155. #ifdef FIPS_MODULE
  156. static int fips_ems_check_passed(TLS1_PRF *ctx)
  157. {
  158. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  159. /*
  160. * Check that TLS is using EMS.
  161. *
  162. * The seed buffer is prepended with a label.
  163. * If EMS mode is enforced then the label "master secret" is not allowed,
  164. * We do the check this way since the PRF is used for other purposes, as well
  165. * as "extended master secret".
  166. */
  167. int ems_approved = (ctx->seedlen < TLS_MD_MASTER_SECRET_CONST_SIZE
  168. || memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,
  169. TLS_MD_MASTER_SECRET_CONST_SIZE) != 0);
  170. if (!ems_approved) {
  171. if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
  172. libctx, "TLS_PRF", "EMS",
  173. ossl_fips_config_tls1_prf_ems_check)) {
  174. ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);
  175. return 0;
  176. }
  177. }
  178. return 1;
  179. }
  180. static int fips_digest_check_passed(TLS1_PRF *ctx, const EVP_MD *md)
  181. {
  182. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  183. /*
  184. * Perform digest check
  185. *
  186. * According to NIST SP 800-135r1 section 5.2, the valid hash functions are
  187. * specified in FIPS 180-3. ACVP also only lists the same set of hash
  188. * functions.
  189. */
  190. int digest_unapproved = !EVP_MD_is_a(md, SN_sha256)
  191. && !EVP_MD_is_a(md, SN_sha384)
  192. && !EVP_MD_is_a(md, SN_sha512);
  193. if (digest_unapproved) {
  194. if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1,
  195. libctx, "TLS_PRF", "Digest",
  196. ossl_fips_config_tls1_prf_digest_check)) {
  197. ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
  198. return 0;
  199. }
  200. }
  201. return 1;
  202. }
  203. static int fips_key_check_passed(TLS1_PRF *ctx)
  204. {
  205. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  206. int key_approved = ossl_kdf_check_key_size(ctx->seclen);
  207. if (!key_approved) {
  208. if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,
  209. libctx, "TLS_PRF", "Key size",
  210. ossl_fips_config_tls1_prf_key_check)) {
  211. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  212. return 0;
  213. }
  214. }
  215. return 1;
  216. }
  217. #endif
  218. static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
  219. const OSSL_PARAM params[])
  220. {
  221. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  222. if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
  223. return 0;
  224. if (ctx->P_hash == NULL) {
  225. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  226. return 0;
  227. }
  228. if (ctx->sec == NULL) {
  229. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
  230. return 0;
  231. }
  232. if (ctx->seedlen == 0) {
  233. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
  234. return 0;
  235. }
  236. if (keylen == 0) {
  237. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  238. return 0;
  239. }
  240. #ifdef FIPS_MODULE
  241. if (!fips_ems_check_passed(ctx))
  242. return 0;
  243. #endif
  244. return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
  245. ctx->sec, ctx->seclen,
  246. ctx->seed, ctx->seedlen,
  247. key, keylen);
  248. }
  249. static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  250. {
  251. const OSSL_PARAM *p;
  252. TLS1_PRF *ctx = vctx;
  253. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  254. if (ossl_param_is_empty(params))
  255. return 1;
  256. if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
  257. OSSL_KDF_PARAM_FIPS_EMS_CHECK))
  258. return 0;
  259. if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, params,
  260. OSSL_KDF_PARAM_FIPS_DIGEST_CHECK))
  261. return 0;
  262. if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, params,
  263. OSSL_KDF_PARAM_FIPS_KEY_CHECK))
  264. return 0;
  265. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
  266. PROV_DIGEST digest;
  267. const EVP_MD *md = NULL;
  268. if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
  269. if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
  270. OSSL_MAC_NAME_HMAC,
  271. NULL, SN_md5, libctx)
  272. || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
  273. OSSL_MAC_NAME_HMAC,
  274. NULL, SN_sha1, libctx))
  275. return 0;
  276. } else {
  277. EVP_MAC_CTX_free(ctx->P_sha1);
  278. if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
  279. OSSL_MAC_NAME_HMAC,
  280. NULL, NULL, libctx))
  281. return 0;
  282. }
  283. memset(&digest, 0, sizeof(digest));
  284. if (!ossl_prov_digest_load_from_params(&digest, params, libctx))
  285. return 0;
  286. md = ossl_prov_digest_md(&digest);
  287. if (EVP_MD_xof(md)) {
  288. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  289. ossl_prov_digest_reset(&digest);
  290. return 0;
  291. }
  292. #ifdef FIPS_MODULE
  293. if (!fips_digest_check_passed(ctx, md)) {
  294. ossl_prov_digest_reset(&digest);
  295. return 0;
  296. }
  297. #endif
  298. ossl_prov_digest_reset(&digest);
  299. }
  300. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
  301. OPENSSL_clear_free(ctx->sec, ctx->seclen);
  302. ctx->sec = NULL;
  303. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
  304. return 0;
  305. #ifdef FIPS_MODULE
  306. if (!fips_key_check_passed(ctx))
  307. return 0;
  308. #endif
  309. }
  310. /* The seed fields concatenate, so process them all */
  311. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
  312. for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
  313. OSSL_KDF_PARAM_SEED)) {
  314. if (p->data_size != 0 && p->data != NULL) {
  315. const void *val = NULL;
  316. size_t sz = 0;
  317. unsigned char *seed;
  318. size_t seedlen;
  319. int err = 0;
  320. if (!OSSL_PARAM_get_octet_string_ptr(p, &val, &sz))
  321. return 0;
  322. seedlen = safe_add_size_t(ctx->seedlen, sz, &err);
  323. if (err)
  324. return 0;
  325. seed = OPENSSL_clear_realloc(ctx->seed, ctx->seedlen, seedlen);
  326. if (!seed)
  327. return 0;
  328. ctx->seed = seed;
  329. if (ossl_assert(sz != 0))
  330. memcpy(ctx->seed + ctx->seedlen, val, sz);
  331. ctx->seedlen = seedlen;
  332. }
  333. }
  334. }
  335. return 1;
  336. }
  337. static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
  338. ossl_unused void *ctx, ossl_unused void *provctx)
  339. {
  340. static const OSSL_PARAM known_settable_ctx_params[] = {
  341. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  342. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  343. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  344. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
  345. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_EMS_CHECK)
  346. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK)
  347. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)
  348. OSSL_PARAM_END
  349. };
  350. return known_settable_ctx_params;
  351. }
  352. static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  353. {
  354. OSSL_PARAM *p;
  355. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) {
  356. if (!OSSL_PARAM_set_size_t(p, SIZE_MAX))
  357. return 0;
  358. }
  359. if (!OSSL_FIPS_IND_GET_CTX_PARAM(((TLS1_PRF *)vctx), params))
  360. return 0;
  361. return 1;
  362. }
  363. static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
  364. ossl_unused void *ctx, ossl_unused void *provctx)
  365. {
  366. static const OSSL_PARAM known_gettable_ctx_params[] = {
  367. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  368. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  369. OSSL_PARAM_END
  370. };
  371. return known_gettable_ctx_params;
  372. }
  373. const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
  374. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
  375. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup },
  376. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
  377. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
  378. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
  379. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  380. (void(*)(void))kdf_tls1_prf_settable_ctx_params },
  381. { OSSL_FUNC_KDF_SET_CTX_PARAMS,
  382. (void(*)(void))kdf_tls1_prf_set_ctx_params },
  383. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  384. (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
  385. { OSSL_FUNC_KDF_GET_CTX_PARAMS,
  386. (void(*)(void))kdf_tls1_prf_get_ctx_params },
  387. OSSL_DISPATCH_END
  388. };
  389. /*
  390. * Refer to "The TLS Protocol Version 1.0" Section 5
  391. * (https://tools.ietf.org/html/rfc2246#section-5) and
  392. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  393. * (https://tools.ietf.org/html/rfc5246#section-5).
  394. *
  395. * P_<hash> is an expansion function that uses a single hash function to expand
  396. * a secret and seed into an arbitrary quantity of output:
  397. *
  398. * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
  399. * HMAC_<hash>(secret, A(2) + seed) +
  400. * HMAC_<hash>(secret, A(3) + seed) + ...
  401. *
  402. * where + indicates concatenation. P_<hash> can be iterated as many times as
  403. * is necessary to produce the required quantity of data.
  404. *
  405. * A(i) is defined as:
  406. * A(0) = seed
  407. * A(i) = HMAC_<hash>(secret, A(i-1))
  408. */
  409. static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
  410. const unsigned char *sec, size_t sec_len,
  411. const unsigned char *seed, size_t seed_len,
  412. unsigned char *out, size_t olen)
  413. {
  414. size_t chunk;
  415. EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
  416. unsigned char Ai[EVP_MAX_MD_SIZE];
  417. size_t Ai_len;
  418. int ret = 0;
  419. if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
  420. goto err;
  421. chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
  422. if (chunk == 0)
  423. goto err;
  424. /* A(0) = seed */
  425. ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
  426. if (ctx_Ai == NULL)
  427. goto err;
  428. if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
  429. goto err;
  430. for (;;) {
  431. /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
  432. if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
  433. goto err;
  434. EVP_MAC_CTX_free(ctx_Ai);
  435. ctx_Ai = NULL;
  436. /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
  437. ctx = EVP_MAC_CTX_dup(ctx_init);
  438. if (ctx == NULL)
  439. goto err;
  440. if (!EVP_MAC_update(ctx, Ai, Ai_len))
  441. goto err;
  442. /* save state for calculating next A(i) value */
  443. if (olen > chunk) {
  444. ctx_Ai = EVP_MAC_CTX_dup(ctx);
  445. if (ctx_Ai == NULL)
  446. goto err;
  447. }
  448. if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
  449. goto err;
  450. if (olen <= chunk) {
  451. /* last chunk - use Ai as temp bounce buffer */
  452. if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
  453. goto err;
  454. memcpy(out, Ai, olen);
  455. break;
  456. }
  457. if (!EVP_MAC_final(ctx, out, NULL, olen))
  458. goto err;
  459. EVP_MAC_CTX_free(ctx);
  460. ctx = NULL;
  461. out += chunk;
  462. olen -= chunk;
  463. }
  464. ret = 1;
  465. err:
  466. EVP_MAC_CTX_free(ctx);
  467. EVP_MAC_CTX_free(ctx_Ai);
  468. OPENSSL_cleanse(Ai, sizeof(Ai));
  469. return ret;
  470. }
  471. /*
  472. * Refer to "The TLS Protocol Version 1.0" Section 5
  473. * (https://tools.ietf.org/html/rfc2246#section-5) and
  474. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  475. * (https://tools.ietf.org/html/rfc5246#section-5).
  476. *
  477. * For TLS v1.0 and TLS v1.1:
  478. *
  479. * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
  480. * P_SHA-1(S2, label + seed)
  481. *
  482. * S1 is taken from the first half of the secret, S2 from the second half.
  483. *
  484. * L_S = length in bytes of secret;
  485. * L_S1 = L_S2 = ceil(L_S / 2);
  486. *
  487. * For TLS v1.2:
  488. *
  489. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  490. */
  491. static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
  492. const unsigned char *sec, size_t slen,
  493. const unsigned char *seed, size_t seed_len,
  494. unsigned char *out, size_t olen)
  495. {
  496. if (sha1ctx != NULL) {
  497. /* TLS v1.0 and TLS v1.1 */
  498. size_t i;
  499. unsigned char *tmp;
  500. /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
  501. size_t L_S1 = (slen + 1) / 2;
  502. size_t L_S2 = L_S1;
  503. if (!tls1_prf_P_hash(mdctx, sec, L_S1,
  504. seed, seed_len, out, olen))
  505. return 0;
  506. if ((tmp = OPENSSL_malloc(olen)) == NULL)
  507. return 0;
  508. if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
  509. seed, seed_len, tmp, olen)) {
  510. OPENSSL_clear_free(tmp, olen);
  511. return 0;
  512. }
  513. for (i = 0; i < olen; i++)
  514. out[i] ^= tmp[i];
  515. OPENSSL_clear_free(tmp, olen);
  516. return 1;
  517. }
  518. /* TLS v1.2 */
  519. if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
  520. return 0;
  521. return 1;
  522. }