Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

563 řádky
20 KiB

  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 <openssl/err.h>
  139. #include <openssl/evp.h>
  140. #include <openssl/hmac.h>
  141. #include <openssl/md5.h>
  142. #include <openssl/mem.h>
  143. #include <openssl/nid.h>
  144. #include <openssl/rand.h>
  145. #include "internal.h"
  146. /* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
  147. * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
  148. * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
  149. * seed parameter. It returns one on success and zero on failure. */
  150. static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
  151. const uint8_t *secret, size_t secret_len,
  152. const uint8_t *seed1, size_t seed1_len,
  153. const uint8_t *seed2, size_t seed2_len,
  154. const uint8_t *seed3, size_t seed3_len) {
  155. HMAC_CTX ctx, ctx_tmp, ctx_init;
  156. uint8_t A1[EVP_MAX_MD_SIZE];
  157. unsigned A1_len;
  158. int ret = 0;
  159. size_t chunk = EVP_MD_size(md);
  160. HMAC_CTX_init(&ctx);
  161. HMAC_CTX_init(&ctx_tmp);
  162. HMAC_CTX_init(&ctx_init);
  163. if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
  164. !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
  165. !HMAC_Update(&ctx, seed1, seed1_len) ||
  166. !HMAC_Update(&ctx, seed2, seed2_len) ||
  167. !HMAC_Update(&ctx, seed3, seed3_len) ||
  168. !HMAC_Final(&ctx, 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, &ctx_init) ||
  175. !HMAC_Update(&ctx, A1, A1_len) ||
  176. /* Save a copy of |ctx| to compute the next A1 value below. */
  177. (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
  178. !HMAC_Update(&ctx, seed1, seed1_len) ||
  179. !HMAC_Update(&ctx, seed2, seed2_len) ||
  180. !HMAC_Update(&ctx, seed3, seed3_len) ||
  181. !HMAC_Final(&ctx, hmac, &len)) {
  182. goto err;
  183. }
  184. assert(len == chunk);
  185. /* XOR the result into |out|. */
  186. if (len > out_len) {
  187. len = out_len;
  188. }
  189. unsigned i;
  190. for (i = 0; i < len; i++) {
  191. out[i] ^= hmac[i];
  192. }
  193. out += len;
  194. out_len -= len;
  195. if (out_len == 0) {
  196. break;
  197. }
  198. /* Calculate the next A1 value. */
  199. if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
  200. goto err;
  201. }
  202. }
  203. ret = 1;
  204. err:
  205. HMAC_CTX_cleanup(&ctx);
  206. HMAC_CTX_cleanup(&ctx_tmp);
  207. HMAC_CTX_cleanup(&ctx_init);
  208. OPENSSL_cleanse(A1, sizeof(A1));
  209. return ret;
  210. }
  211. static int tls1_prf(const SSL *ssl, uint8_t *out, size_t out_len,
  212. const uint8_t *secret, size_t secret_len, const char *label,
  213. size_t label_len, const uint8_t *seed1, size_t seed1_len,
  214. const uint8_t *seed2, size_t seed2_len) {
  215. if (out_len == 0) {
  216. return 1;
  217. }
  218. memset(out, 0, out_len);
  219. uint32_t algorithm_prf = ssl_get_algorithm_prf(ssl);
  220. if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) {
  221. /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and
  222. * MD5, MD5 first. */
  223. size_t secret_half = secret_len - (secret_len / 2);
  224. if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half,
  225. (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
  226. seed2_len)) {
  227. return 0;
  228. }
  229. /* Note that, if |secret_len| is odd, the two halves share a byte. */
  230. secret = secret + (secret_len - secret_half);
  231. secret_len = secret_half;
  232. }
  233. if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf),
  234. secret, secret_len, (const uint8_t *)label, label_len,
  235. seed1, seed1_len, seed2, seed2_len)) {
  236. return 0;
  237. }
  238. return 1;
  239. }
  240. int tls1_change_cipher_state(SSL *ssl, int which) {
  241. /* Ensure the key block is set up. */
  242. if (!tls1_setup_key_block(ssl)) {
  243. return 0;
  244. }
  245. /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
  246. * need to update the read cipherspec. Otherwise we have just written one. */
  247. const char is_read = (which & SSL3_CC_READ) != 0;
  248. /* use_client_keys is true if we wish to use the keys for the "client write"
  249. * direction. This is the case if we're a client sending a ChangeCipherSpec,
  250. * or a server reading a client's ChangeCipherSpec. */
  251. const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
  252. which == SSL3_CHANGE_CIPHER_SERVER_READ;
  253. size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len;
  254. size_t key_len = ssl->s3->tmp.new_key_len;
  255. size_t iv_len = ssl->s3->tmp.new_fixed_iv_len;
  256. assert((mac_secret_len + key_len + iv_len) * 2 ==
  257. ssl->s3->tmp.key_block_length);
  258. const uint8_t *key_data = ssl->s3->tmp.key_block;
  259. const uint8_t *client_write_mac_secret = key_data;
  260. key_data += mac_secret_len;
  261. const uint8_t *server_write_mac_secret = key_data;
  262. key_data += mac_secret_len;
  263. const uint8_t *client_write_key = key_data;
  264. key_data += key_len;
  265. const uint8_t *server_write_key = key_data;
  266. key_data += key_len;
  267. const uint8_t *client_write_iv = key_data;
  268. key_data += iv_len;
  269. const uint8_t *server_write_iv = key_data;
  270. key_data += iv_len;
  271. const uint8_t *mac_secret, *key, *iv;
  272. if (use_client_keys) {
  273. mac_secret = client_write_mac_secret;
  274. key = client_write_key;
  275. iv = client_write_iv;
  276. } else {
  277. mac_secret = server_write_mac_secret;
  278. key = server_write_key;
  279. iv = server_write_iv;
  280. }
  281. SSL_AEAD_CTX *aead_ctx =
  282. SSL_AEAD_CTX_new(is_read ? evp_aead_open : evp_aead_seal,
  283. ssl3_protocol_version(ssl), ssl->s3->tmp.new_cipher, key,
  284. key_len, mac_secret, mac_secret_len, iv, iv_len);
  285. if (aead_ctx == NULL) {
  286. return 0;
  287. }
  288. if (is_read) {
  289. ssl_set_read_state(ssl, aead_ctx);
  290. } else {
  291. ssl_set_write_state(ssl, aead_ctx);
  292. }
  293. return 1;
  294. }
  295. size_t SSL_get_key_block_len(const SSL *ssl) {
  296. return 2 * ((size_t)ssl->s3->tmp.new_mac_secret_len +
  297. (size_t)ssl->s3->tmp.new_key_len +
  298. (size_t)ssl->s3->tmp.new_fixed_iv_len);
  299. }
  300. int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) {
  301. return ssl->s3->enc_method->prf(
  302. ssl, out, out_len, ssl->session->master_key,
  303. ssl->session->master_key_length, TLS_MD_KEY_EXPANSION_CONST,
  304. TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE,
  305. ssl->s3->client_random, SSL3_RANDOM_SIZE);
  306. }
  307. int tls1_setup_key_block(SSL *ssl) {
  308. if (ssl->s3->tmp.key_block_length != 0) {
  309. return 1;
  310. }
  311. const EVP_AEAD *aead = NULL;
  312. size_t mac_secret_len, fixed_iv_len;
  313. if (ssl->session->cipher == NULL ||
  314. !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
  315. ssl->session->cipher,
  316. ssl3_protocol_version(ssl))) {
  317. OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  318. return 0;
  319. }
  320. size_t key_len = EVP_AEAD_key_length(aead);
  321. if (mac_secret_len > 0) {
  322. /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
  323. * key length reported by |EVP_AEAD_key_length| will include the MAC key
  324. * bytes and initial implicit IV. */
  325. if (key_len < mac_secret_len + fixed_iv_len) {
  326. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  327. return 0;
  328. }
  329. key_len -= mac_secret_len + fixed_iv_len;
  330. }
  331. assert(mac_secret_len < 256);
  332. assert(key_len < 256);
  333. assert(fixed_iv_len < 256);
  334. ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
  335. ssl->s3->tmp.new_key_len = (uint8_t)key_len;
  336. ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
  337. size_t key_block_len = SSL_get_key_block_len(ssl);
  338. ssl3_cleanup_key_block(ssl);
  339. uint8_t *keyblock = OPENSSL_malloc(key_block_len);
  340. if (keyblock == NULL) {
  341. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  342. return 0;
  343. }
  344. if (!SSL_generate_key_block(ssl, keyblock, key_block_len)) {
  345. OPENSSL_free(keyblock);
  346. return 0;
  347. }
  348. assert(key_block_len < 256);
  349. ssl->s3->tmp.key_block_length = (uint8_t)key_block_len;
  350. ssl->s3->tmp.key_block = keyblock;
  351. return 1;
  352. }
  353. static int tls1_cert_verify_mac(SSL *ssl, int md_nid, uint8_t *out) {
  354. const EVP_MD_CTX *ctx_template;
  355. if (md_nid == NID_md5) {
  356. ctx_template = &ssl->s3->handshake_md5;
  357. } else if (md_nid == EVP_MD_CTX_type(&ssl->s3->handshake_hash)) {
  358. ctx_template = &ssl->s3->handshake_hash;
  359. } else {
  360. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
  361. return 0;
  362. }
  363. EVP_MD_CTX ctx;
  364. EVP_MD_CTX_init(&ctx);
  365. if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
  366. EVP_MD_CTX_cleanup(&ctx);
  367. return 0;
  368. }
  369. unsigned ret;
  370. EVP_DigestFinal_ex(&ctx, out, &ret);
  371. EVP_MD_CTX_cleanup(&ctx);
  372. return ret;
  373. }
  374. static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
  375. size_t max_out) {
  376. int ret = 0;
  377. EVP_MD_CTX ctx_copy;
  378. EVP_MD_CTX_init(&ctx_copy);
  379. if (EVP_MD_CTX_size(ctx) > max_out) {
  380. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  381. goto err;
  382. }
  383. unsigned len;
  384. if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) ||
  385. !EVP_DigestFinal_ex(&ctx_copy, out, &len)) {
  386. goto err;
  387. }
  388. assert(len == EVP_MD_CTX_size(ctx));
  389. *out_len = len;
  390. ret = 1;
  391. err:
  392. EVP_MD_CTX_cleanup(&ctx_copy);
  393. return ret;
  394. }
  395. /* tls1_handshake_digest calculates the current handshake hash and writes it to
  396. * |out|, which has space for |out_len| bytes. It returns the number of bytes
  397. * written or -1 in the event of an error. This function works on a copy of the
  398. * underlying digests so can be called multiple times and prior to the final
  399. * update etc. */
  400. int tls1_handshake_digest(SSL *ssl, uint8_t *out, size_t out_len) {
  401. size_t md5_len = 0;
  402. if (EVP_MD_CTX_md(&ssl->s3->handshake_md5) != NULL &&
  403. !append_digest(&ssl->s3->handshake_md5, out, &md5_len, out_len)) {
  404. return -1;
  405. }
  406. size_t len;
  407. if (!append_digest(&ssl->s3->handshake_hash, out + md5_len, &len,
  408. out_len - md5_len)) {
  409. return -1;
  410. }
  411. return (int)(md5_len + len);
  412. }
  413. static int tls1_final_finish_mac(SSL *ssl, int from_server, uint8_t *out) {
  414. /* At this point, the handshake should have released the handshake buffer on
  415. * its own. */
  416. assert(ssl->s3->handshake_buffer == NULL);
  417. const char *label = TLS_MD_CLIENT_FINISH_CONST;
  418. size_t label_len = TLS_MD_SERVER_FINISH_CONST_SIZE;
  419. if (from_server) {
  420. label = TLS_MD_SERVER_FINISH_CONST;
  421. label_len = TLS_MD_SERVER_FINISH_CONST_SIZE;
  422. }
  423. uint8_t buf[EVP_MAX_MD_SIZE];
  424. int digests_len = tls1_handshake_digest(ssl, buf, sizeof(buf));
  425. if (digests_len < 0) {
  426. return 0;
  427. }
  428. static const size_t kFinishedLen = 12;
  429. if (!ssl->s3->enc_method->prf(ssl, out, kFinishedLen,
  430. ssl->session->master_key,
  431. ssl->session->master_key_length, label,
  432. label_len, buf, digests_len, NULL, 0)) {
  433. return 0;
  434. }
  435. return (int)kFinishedLen;
  436. }
  437. int tls1_generate_master_secret(SSL *ssl, uint8_t *out,
  438. const uint8_t *premaster,
  439. size_t premaster_len) {
  440. if (ssl->s3->tmp.extended_master_secret) {
  441. uint8_t digests[EVP_MAX_MD_SIZE];
  442. int digests_len = tls1_handshake_digest(ssl, digests, sizeof(digests));
  443. if (digests_len == -1) {
  444. return 0;
  445. }
  446. if (!ssl->s3->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster,
  447. premaster_len,
  448. TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  449. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
  450. digests, digests_len, NULL, 0)) {
  451. return 0;
  452. }
  453. } else {
  454. if (!ssl->s3->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster,
  455. premaster_len, TLS_MD_MASTER_SECRET_CONST,
  456. TLS_MD_MASTER_SECRET_CONST_SIZE,
  457. ssl->s3->client_random, SSL3_RANDOM_SIZE,
  458. ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  459. return 0;
  460. }
  461. }
  462. return SSL3_MASTER_SECRET_SIZE;
  463. }
  464. int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
  465. const char *label, size_t label_len,
  466. const uint8_t *context, size_t context_len,
  467. int use_context) {
  468. if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) {
  469. return 0;
  470. }
  471. size_t seed_len = 2 * SSL3_RANDOM_SIZE;
  472. if (use_context) {
  473. if (context_len >= 1u << 16) {
  474. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  475. return 0;
  476. }
  477. seed_len += 2 + context_len;
  478. }
  479. uint8_t *seed = OPENSSL_malloc(seed_len);
  480. if (seed == NULL) {
  481. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  482. return 0;
  483. }
  484. memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE);
  485. memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE);
  486. if (use_context) {
  487. seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
  488. seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
  489. memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
  490. }
  491. int ret =
  492. ssl->s3->enc_method->prf(ssl, out, out_len, ssl->session->master_key,
  493. ssl->session->master_key_length, label,
  494. label_len, seed, seed_len, NULL, 0);
  495. OPENSSL_free(seed);
  496. return ret;
  497. }
  498. const SSL3_ENC_METHOD TLSv1_enc_data = {
  499. tls1_prf,
  500. tls1_final_finish_mac,
  501. tls1_cert_verify_mac,
  502. };