t1_enc.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2005 Nokia. All rights reserved.
  112. *
  113. * The portions of the attached software ("Contribution") is developed by
  114. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  115. * license.
  116. *
  117. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  118. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  119. * support (see RFC 4279) to OpenSSL.
  120. *
  121. * No patent licenses or other rights except those expressly stated in
  122. * the OpenSSL open source license shall be deemed granted or received
  123. * expressly, by implication, estoppel, or otherwise.
  124. *
  125. * No assurances are provided by Nokia that the Contribution does not
  126. * infringe the patent or other intellectual property rights of any third
  127. * party or that the license provides you with all the necessary rights
  128. * to make use of the Contribution.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  131. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  132. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  133. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  134. * OTHERWISE. */
  135. #include <openssl/ssl.h>
  136. #include <assert.h>
  137. #include <string.h>
  138. #include <utility>
  139. #include <openssl/err.h>
  140. #include <openssl/evp.h>
  141. #include <openssl/hmac.h>
  142. #include <openssl/md5.h>
  143. #include <openssl/mem.h>
  144. #include <openssl/nid.h>
  145. #include <openssl/rand.h>
  146. #include "../crypto/internal.h"
  147. #include "internal.h"
  148. namespace bssl {
  149. // tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
  150. // section 5. It XORs |out.size()| bytes to |out|, using |md| as the hash and
  151. // |secret| as the secret. |label|, |seed1|, and |seed2| are concatenated to
  152. // form the seed parameter. It returns true on success and false on failure.
  153. static bool tls1_P_hash(Span<uint8_t> out, const EVP_MD *md,
  154. Span<const uint8_t> secret, Span<const char> label,
  155. Span<const uint8_t> seed1, Span<const uint8_t> seed2) {
  156. ScopedHMAC_CTX ctx, ctx_tmp, ctx_init;
  157. uint8_t A1[EVP_MAX_MD_SIZE];
  158. unsigned A1_len;
  159. bool ret = false;
  160. size_t chunk = EVP_MD_size(md);
  161. if (!HMAC_Init_ex(ctx_init.get(), secret.data(), secret.size(), md,
  162. nullptr) ||
  163. !HMAC_CTX_copy_ex(ctx.get(), ctx_init.get()) ||
  164. !HMAC_Update(ctx.get(), reinterpret_cast<const uint8_t *>(label.data()),
  165. label.size()) ||
  166. !HMAC_Update(ctx.get(), seed1.data(), seed1.size()) ||
  167. !HMAC_Update(ctx.get(), seed2.data(), seed2.size()) ||
  168. !HMAC_Final(ctx.get(), A1, &A1_len)) {
  169. goto err;
  170. }
  171. for (;;) {
  172. unsigned len;
  173. uint8_t hmac[EVP_MAX_MD_SIZE];
  174. if (!HMAC_CTX_copy_ex(ctx.get(), ctx_init.get()) ||
  175. !HMAC_Update(ctx.get(), A1, A1_len) ||
  176. // Save a copy of |ctx| to compute the next A1 value below.
  177. (out.size() > chunk && !HMAC_CTX_copy_ex(ctx_tmp.get(), ctx.get())) ||
  178. !HMAC_Update(ctx.get(), reinterpret_cast<const uint8_t *>(label.data()),
  179. label.size()) ||
  180. !HMAC_Update(ctx.get(), seed1.data(), seed1.size()) ||
  181. !HMAC_Update(ctx.get(), seed2.data(), seed2.size()) ||
  182. !HMAC_Final(ctx.get(), hmac, &len)) {
  183. goto err;
  184. }
  185. assert(len == chunk);
  186. // XOR the result into |out|.
  187. if (len > out.size()) {
  188. len = out.size();
  189. }
  190. for (unsigned i = 0; i < len; i++) {
  191. out[i] ^= hmac[i];
  192. }
  193. out = out.subspan(len);
  194. if (out.empty()) {
  195. break;
  196. }
  197. // Calculate the next A1 value.
  198. if (!HMAC_Final(ctx_tmp.get(), A1, &A1_len)) {
  199. goto err;
  200. }
  201. }
  202. ret = true;
  203. err:
  204. OPENSSL_cleanse(A1, sizeof(A1));
  205. return ret;
  206. }
  207. bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
  208. Span<const uint8_t> secret, Span<const char> label,
  209. Span<const uint8_t> seed1, Span<const uint8_t> seed2) {
  210. if (out.empty()) {
  211. return true;
  212. }
  213. OPENSSL_memset(out.data(), 0, out.size());
  214. if (digest == EVP_md5_sha1()) {
  215. // If using the MD5/SHA1 PRF, |secret| is partitioned between MD5 and SHA-1.
  216. size_t secret_half = secret.size() - (secret.size() / 2);
  217. if (!tls1_P_hash(out, EVP_md5(), secret.subspan(0, secret_half), label,
  218. seed1, seed2)) {
  219. return false;
  220. }
  221. // Note that, if |secret.size()| is odd, the two halves share a byte.
  222. secret = secret.subspan(secret.size() - secret_half);
  223. digest = EVP_sha1();
  224. }
  225. return tls1_P_hash(out, digest, secret, label, seed1, seed2);
  226. }
  227. static bool ssl3_prf(Span<uint8_t> out, Span<const uint8_t> secret,
  228. Span<const char> label, Span<const uint8_t> seed1,
  229. Span<const uint8_t> seed2) {
  230. ScopedEVP_MD_CTX md5;
  231. ScopedEVP_MD_CTX sha1;
  232. uint8_t buf[16], smd[SHA_DIGEST_LENGTH];
  233. uint8_t c = 'A';
  234. size_t k = 0;
  235. while (!out.empty()) {
  236. k++;
  237. if (k > sizeof(buf)) {
  238. // bug: 'buf' is too small for this ciphersuite
  239. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  240. return false;
  241. }
  242. for (size_t j = 0; j < k; j++) {
  243. buf[j] = c;
  244. }
  245. c++;
  246. if (!EVP_DigestInit_ex(sha1.get(), EVP_sha1(), NULL)) {
  247. OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
  248. return false;
  249. }
  250. EVP_DigestUpdate(sha1.get(), buf, k);
  251. EVP_DigestUpdate(sha1.get(), secret.data(), secret.size());
  252. // |label| is ignored for SSLv3.
  253. EVP_DigestUpdate(sha1.get(), seed1.data(), seed1.size());
  254. EVP_DigestUpdate(sha1.get(), seed2.data(), seed2.size());
  255. EVP_DigestFinal_ex(sha1.get(), smd, NULL);
  256. if (!EVP_DigestInit_ex(md5.get(), EVP_md5(), NULL)) {
  257. OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
  258. return false;
  259. }
  260. EVP_DigestUpdate(md5.get(), secret.data(), secret.size());
  261. EVP_DigestUpdate(md5.get(), smd, SHA_DIGEST_LENGTH);
  262. if (out.size() < MD5_DIGEST_LENGTH) {
  263. EVP_DigestFinal_ex(md5.get(), smd, NULL);
  264. OPENSSL_memcpy(out.data(), smd, out.size());
  265. break;
  266. }
  267. EVP_DigestFinal_ex(md5.get(), out.data(), NULL);
  268. out = out.subspan(MD5_DIGEST_LENGTH);
  269. }
  270. OPENSSL_cleanse(smd, SHA_DIGEST_LENGTH);
  271. return true;
  272. }
  273. static bool get_key_block_lengths(const SSL *ssl, size_t *out_mac_secret_len,
  274. size_t *out_key_len, size_t *out_iv_len,
  275. const SSL_CIPHER *cipher) {
  276. const EVP_AEAD *aead = NULL;
  277. if (!ssl_cipher_get_evp_aead(&aead, out_mac_secret_len, out_iv_len, cipher,
  278. ssl_protocol_version(ssl), SSL_is_dtls(ssl))) {
  279. OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  280. return false;
  281. }
  282. *out_key_len = EVP_AEAD_key_length(aead);
  283. if (*out_mac_secret_len > 0) {
  284. // For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
  285. // key length reported by |EVP_AEAD_key_length| will include the MAC key
  286. // bytes and initial implicit IV.
  287. if (*out_key_len < *out_mac_secret_len + *out_iv_len) {
  288. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  289. return false;
  290. }
  291. *out_key_len -= *out_mac_secret_len + *out_iv_len;
  292. }
  293. return true;
  294. }
  295. static bool setup_key_block(SSL_HANDSHAKE *hs) {
  296. SSL *const ssl = hs->ssl;
  297. if (!hs->key_block.empty()) {
  298. return true;
  299. }
  300. size_t mac_secret_len, key_len, fixed_iv_len;
  301. Array<uint8_t> key_block;
  302. if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &fixed_iv_len,
  303. hs->new_cipher) ||
  304. !key_block.Init(2 * (mac_secret_len + key_len + fixed_iv_len)) ||
  305. !SSL_generate_key_block(ssl, key_block.data(), key_block.size())) {
  306. return false;
  307. }
  308. hs->key_block = std::move(key_block);
  309. return true;
  310. }
  311. int tls1_change_cipher_state(SSL_HANDSHAKE *hs,
  312. evp_aead_direction_t direction) {
  313. SSL *const ssl = hs->ssl;
  314. // Ensure the key block is set up.
  315. size_t mac_secret_len, key_len, iv_len;
  316. if (!setup_key_block(hs) ||
  317. !get_key_block_lengths(ssl, &mac_secret_len, &key_len, &iv_len,
  318. hs->new_cipher)) {
  319. return 0;
  320. }
  321. if ((mac_secret_len + key_len + iv_len) * 2 != hs->key_block.size()) {
  322. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  323. return 0;
  324. }
  325. Span<const uint8_t> key_block = hs->key_block;
  326. Span<const uint8_t> mac_secret, key, iv;
  327. if (direction == (ssl->server ? evp_aead_open : evp_aead_seal)) {
  328. // Use the client write (server read) keys.
  329. mac_secret = key_block.subspan(0, mac_secret_len);
  330. key = key_block.subspan(2 * mac_secret_len, key_len);
  331. iv = key_block.subspan(2 * mac_secret_len + 2 * key_len, iv_len);
  332. } else {
  333. // Use the server write (client read) keys.
  334. mac_secret = key_block.subspan(mac_secret_len, mac_secret_len);
  335. key = key_block.subspan(2 * mac_secret_len + key_len, key_len);
  336. iv = key_block.subspan(2 * mac_secret_len + 2 * key_len + iv_len, iv_len);
  337. }
  338. UniquePtr<SSLAEADContext> aead_ctx =
  339. SSLAEADContext::Create(direction, ssl->version, SSL_is_dtls(ssl),
  340. hs->new_cipher, key, mac_secret, iv);
  341. if (!aead_ctx) {
  342. return 0;
  343. }
  344. if (direction == evp_aead_open) {
  345. return ssl->method->set_read_state(ssl, std::move(aead_ctx));
  346. }
  347. return ssl->method->set_write_state(ssl, std::move(aead_ctx));
  348. }
  349. int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
  350. Span<const uint8_t> premaster) {
  351. static const char kMasterSecretLabel[] = "master secret";
  352. static const char kExtendedMasterSecretLabel[] = "extended master secret";
  353. const SSL *ssl = hs->ssl;
  354. auto out_span = MakeSpan(out, SSL3_MASTER_SECRET_SIZE);
  355. if (hs->extended_master_secret) {
  356. auto label = MakeConstSpan(kExtendedMasterSecretLabel,
  357. sizeof(kExtendedMasterSecretLabel) - 1);
  358. uint8_t digests[EVP_MAX_MD_SIZE];
  359. size_t digests_len;
  360. if (!hs->transcript.GetHash(digests, &digests_len) ||
  361. !tls1_prf(hs->transcript.Digest(), out_span, premaster, label,
  362. MakeConstSpan(digests, digests_len), {})) {
  363. return 0;
  364. }
  365. } else {
  366. auto label =
  367. MakeConstSpan(kMasterSecretLabel, sizeof(kMasterSecretLabel) - 1);
  368. if (ssl_protocol_version(ssl) == SSL3_VERSION) {
  369. if (!ssl3_prf(out_span, premaster, label, ssl->s3->client_random,
  370. ssl->s3->server_random)) {
  371. return 0;
  372. }
  373. } else {
  374. if (!tls1_prf(hs->transcript.Digest(), out_span, premaster, label,
  375. ssl->s3->client_random, ssl->s3->server_random)) {
  376. return 0;
  377. }
  378. }
  379. }
  380. return SSL3_MASTER_SECRET_SIZE;
  381. }
  382. } // namespace bssl
  383. using namespace bssl;
  384. size_t SSL_get_key_block_len(const SSL *ssl) {
  385. size_t mac_secret_len, key_len, fixed_iv_len;
  386. if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &fixed_iv_len,
  387. SSL_get_current_cipher(ssl))) {
  388. ERR_clear_error();
  389. return 0;
  390. }
  391. return 2 * (mac_secret_len + key_len + fixed_iv_len);
  392. }
  393. int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) {
  394. const SSL_SESSION *session = SSL_get_session(ssl);
  395. auto out_span = MakeSpan(out, out_len);
  396. auto master_key =
  397. MakeConstSpan(session->master_key, session->master_key_length);
  398. static const char kLabel[] = "key expansion";
  399. auto label = MakeConstSpan(kLabel, sizeof(kLabel) - 1);
  400. if (ssl_protocol_version(ssl) == SSL3_VERSION) {
  401. return ssl3_prf(out_span, master_key, label, ssl->s3->server_random,
  402. ssl->s3->client_random);
  403. }
  404. const EVP_MD *digest = ssl_session_get_digest(session);
  405. return tls1_prf(digest, out_span, master_key, label, ssl->s3->server_random,
  406. ssl->s3->client_random);
  407. }
  408. int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
  409. const char *label, size_t label_len,
  410. const uint8_t *context, size_t context_len,
  411. int use_context) {
  412. if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) {
  413. return 0;
  414. }
  415. // Exporters may not be used in the middle of a renegotiation.
  416. if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  417. return 0;
  418. }
  419. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  420. return tls13_export_keying_material(ssl, out, out_len, label, label_len,
  421. context, context_len, use_context);
  422. }
  423. size_t seed_len = 2 * SSL3_RANDOM_SIZE;
  424. if (use_context) {
  425. if (context_len >= 1u << 16) {
  426. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  427. return 0;
  428. }
  429. seed_len += 2 + context_len;
  430. }
  431. Array<uint8_t> seed;
  432. if (!seed.Init(seed_len)) {
  433. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  434. return 0;
  435. }
  436. OPENSSL_memcpy(seed.data(), ssl->s3->client_random, SSL3_RANDOM_SIZE);
  437. OPENSSL_memcpy(seed.data() + SSL3_RANDOM_SIZE, ssl->s3->server_random,
  438. SSL3_RANDOM_SIZE);
  439. if (use_context) {
  440. seed[2 * SSL3_RANDOM_SIZE] = static_cast<uint8_t>(context_len >> 8);
  441. seed[2 * SSL3_RANDOM_SIZE + 1] = static_cast<uint8_t>(context_len);
  442. OPENSSL_memcpy(seed.data() + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
  443. }
  444. const SSL_SESSION *session = SSL_get_session(ssl);
  445. const EVP_MD *digest = ssl_session_get_digest(session);
  446. return tls1_prf(
  447. digest, MakeSpan(out, out_len),
  448. MakeConstSpan(session->master_key, session->master_key_length),
  449. MakeConstSpan(label, label_len), seed, {});
  450. }