You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

725 lines
24 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 <stdio.h>
  138. #include <string.h>
  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/obj.h>
  145. #include <openssl/rand.h>
  146. #include "internal.h"
  147. /* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
  148. * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
  149. * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
  150. * seed parameter. It returns one on success and zero on failure. */
  151. static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
  152. const uint8_t *secret, size_t secret_len,
  153. const uint8_t *seed1, size_t seed1_len,
  154. const uint8_t *seed2, size_t seed2_len,
  155. const uint8_t *seed3, size_t seed3_len) {
  156. size_t chunk;
  157. HMAC_CTX ctx, ctx_tmp, ctx_init;
  158. uint8_t A1[EVP_MAX_MD_SIZE];
  159. unsigned A1_len;
  160. int ret = 0;
  161. chunk = EVP_MD_size(md);
  162. HMAC_CTX_init(&ctx);
  163. HMAC_CTX_init(&ctx_tmp);
  164. HMAC_CTX_init(&ctx_init);
  165. if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
  166. !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
  167. (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
  168. (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
  169. (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len)) ||
  170. !HMAC_Final(&ctx, A1, &A1_len)) {
  171. goto err;
  172. }
  173. for (;;) {
  174. /* Reinit mac contexts. */
  175. if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
  176. !HMAC_Update(&ctx, A1, A1_len) ||
  177. (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
  178. (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
  179. (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
  180. (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len))) {
  181. goto err;
  182. }
  183. unsigned len;
  184. uint8_t hmac[EVP_MAX_MD_SIZE];
  185. if (!HMAC_Final(&ctx, hmac, &len)) {
  186. goto err;
  187. }
  188. assert(len == chunk);
  189. /* XOR the result into |out|. */
  190. if (len > out_len) {
  191. len = out_len;
  192. }
  193. unsigned i;
  194. for (i = 0; i < len; i++) {
  195. out[i] ^= hmac[i];
  196. }
  197. out += len;
  198. out_len -= len;
  199. if (out_len == 0) {
  200. break;
  201. }
  202. /* Calculate the next A1 value. */
  203. if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
  204. goto err;
  205. }
  206. }
  207. ret = 1;
  208. err:
  209. HMAC_CTX_cleanup(&ctx);
  210. HMAC_CTX_cleanup(&ctx_tmp);
  211. HMAC_CTX_cleanup(&ctx_init);
  212. OPENSSL_cleanse(A1, sizeof(A1));
  213. return ret;
  214. }
  215. int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
  216. size_t secret_len, const char *label, size_t label_len,
  217. const uint8_t *seed1, size_t seed1_len,
  218. const uint8_t *seed2, size_t seed2_len) {
  219. if (out_len == 0) {
  220. return 1;
  221. }
  222. memset(out, 0, out_len);
  223. uint32_t algorithm_prf = ssl_get_algorithm_prf(s);
  224. if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) {
  225. /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and
  226. * MD5, MD5 first. */
  227. size_t secret_half = secret_len - (secret_len / 2);
  228. if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half,
  229. (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
  230. seed2_len)) {
  231. return 0;
  232. }
  233. /* Note that, if |secret_len| is odd, the two halves share a byte. */
  234. secret = secret + (secret_len - secret_half);
  235. secret_len = secret_half;
  236. }
  237. if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf),
  238. secret, secret_len, (const uint8_t *)label, label_len,
  239. seed1, seed1_len, seed2, seed2_len)) {
  240. return 0;
  241. }
  242. return 1;
  243. }
  244. static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) {
  245. return s->enc_method->prf(s, out, out_len, s->session->master_key,
  246. s->session->master_key_length,
  247. TLS_MD_KEY_EXPANSION_CONST,
  248. TLS_MD_KEY_EXPANSION_CONST_SIZE,
  249. s->s3->server_random, SSL3_RANDOM_SIZE,
  250. s->s3->client_random,
  251. SSL3_RANDOM_SIZE);
  252. }
  253. int tls1_change_cipher_state(SSL *s, int which) {
  254. /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
  255. * need to update the read cipherspec. Otherwise we have just written one. */
  256. const char is_read = (which & SSL3_CC_READ) != 0;
  257. /* use_client_keys is true if we wish to use the keys for the "client write"
  258. * direction. This is the case if we're a client sending a ChangeCipherSpec,
  259. * or a server reading a client's ChangeCipherSpec. */
  260. const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
  261. which == SSL3_CHANGE_CIPHER_SERVER_READ;
  262. const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret;
  263. const uint8_t *client_write_key, *server_write_key, *key;
  264. const uint8_t *client_write_iv, *server_write_iv, *iv;
  265. const EVP_AEAD *aead = s->s3->tmp.new_aead;
  266. size_t key_len, iv_len, mac_secret_len;
  267. const uint8_t *key_data;
  268. /* Reset sequence number to zero. */
  269. if (is_read) {
  270. if (SSL_IS_DTLS(s)) {
  271. s->d1->r_epoch++;
  272. memset(&s->d1->bitmap, 0, sizeof(s->d1->bitmap));
  273. }
  274. memset(s->s3->read_sequence, 0, sizeof(s->s3->read_sequence));
  275. } else {
  276. if (SSL_IS_DTLS(s)) {
  277. s->d1->w_epoch++;
  278. memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
  279. sizeof(s->s3->write_sequence));
  280. }
  281. memset(s->s3->write_sequence, 0, sizeof(s->s3->write_sequence));
  282. }
  283. mac_secret_len = s->s3->tmp.new_mac_secret_len;
  284. iv_len = s->s3->tmp.new_fixed_iv_len;
  285. if (aead == NULL) {
  286. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  287. return 0;
  288. }
  289. key_len = EVP_AEAD_key_length(aead);
  290. if (mac_secret_len > 0) {
  291. /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher
  292. * suites) the key length reported by |EVP_AEAD_key_length| will
  293. * include the MAC and IV key bytes. */
  294. if (key_len < mac_secret_len + iv_len) {
  295. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  296. return 0;
  297. }
  298. key_len -= mac_secret_len + iv_len;
  299. }
  300. key_data = s->s3->tmp.key_block;
  301. client_write_mac_secret = key_data;
  302. key_data += mac_secret_len;
  303. server_write_mac_secret = key_data;
  304. key_data += mac_secret_len;
  305. client_write_key = key_data;
  306. key_data += key_len;
  307. server_write_key = key_data;
  308. key_data += key_len;
  309. client_write_iv = key_data;
  310. key_data += iv_len;
  311. server_write_iv = key_data;
  312. key_data += iv_len;
  313. if (use_client_keys) {
  314. mac_secret = client_write_mac_secret;
  315. key = client_write_key;
  316. iv = client_write_iv;
  317. } else {
  318. mac_secret = server_write_mac_secret;
  319. key = server_write_key;
  320. iv = server_write_iv;
  321. }
  322. if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
  323. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  324. return 0;
  325. }
  326. if (is_read) {
  327. SSL_AEAD_CTX_free(s->aead_read_ctx);
  328. s->aead_read_ctx = SSL_AEAD_CTX_new(
  329. evp_aead_open, ssl3_version_from_wire(s, s->version),
  330. s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
  331. iv_len);
  332. return s->aead_read_ctx != NULL;
  333. }
  334. SSL_AEAD_CTX_free(s->aead_write_ctx);
  335. s->aead_write_ctx = SSL_AEAD_CTX_new(
  336. evp_aead_seal, ssl3_version_from_wire(s, s->version),
  337. s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
  338. iv_len);
  339. if (s->aead_write_ctx == NULL) {
  340. return 0;
  341. }
  342. s->s3->need_record_splitting = 0;
  343. if (!SSL_USE_EXPLICIT_IV(s) &&
  344. (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
  345. SSL_CIPHER_is_block_cipher(s->s3->tmp.new_cipher)) {
  346. /* Enable 1/n-1 record-splitting to randomize the IV. See
  347. * https://www.openssl.org/~bodo/tls-cbc.txt and the BEAST attack. */
  348. s->s3->need_record_splitting = 1;
  349. }
  350. return 1;
  351. }
  352. int tls1_setup_key_block(SSL *s) {
  353. uint8_t *p;
  354. const EVP_AEAD *aead = NULL;
  355. int ret = 0;
  356. size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
  357. size_t key_block_len;
  358. if (s->s3->tmp.key_block_length != 0) {
  359. return 1;
  360. }
  361. if (s->session->cipher == NULL) {
  362. goto cipher_unavailable_err;
  363. }
  364. if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
  365. s->session->cipher,
  366. ssl3_version_from_wire(s, s->version))) {
  367. goto cipher_unavailable_err;
  368. }
  369. key_len = EVP_AEAD_key_length(aead);
  370. variable_iv_len = EVP_AEAD_nonce_length(aead);
  371. if (mac_secret_len > 0) {
  372. /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
  373. * key length reported by |EVP_AEAD_key_length| will include the MAC key
  374. * bytes and initial implicit IV. */
  375. if (key_len < mac_secret_len + fixed_iv_len) {
  376. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  377. return 0;
  378. }
  379. key_len -= mac_secret_len + fixed_iv_len;
  380. } else {
  381. /* The nonce is split into a fixed portion and a variable portion. */
  382. if (variable_iv_len < fixed_iv_len) {
  383. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  384. return 0;
  385. }
  386. variable_iv_len -= fixed_iv_len;
  387. }
  388. assert(mac_secret_len < 256);
  389. assert(fixed_iv_len < 256);
  390. assert(variable_iv_len < 256);
  391. s->s3->tmp.new_aead = aead;
  392. s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
  393. s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
  394. s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
  395. key_block_len = key_len + mac_secret_len + fixed_iv_len;
  396. key_block_len *= 2;
  397. ssl3_cleanup_key_block(s);
  398. p = (uint8_t *)OPENSSL_malloc(key_block_len);
  399. if (p == NULL) {
  400. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  401. goto err;
  402. }
  403. s->s3->tmp.key_block_length = key_block_len;
  404. s->s3->tmp.key_block = p;
  405. if (!tls1_generate_key_block(s, p, key_block_len)) {
  406. goto err;
  407. }
  408. ret = 1;
  409. err:
  410. return ret;
  411. cipher_unavailable_err:
  412. OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  413. return 0;
  414. }
  415. int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
  416. const EVP_MD_CTX *ctx_template;
  417. if (md_nid == NID_md5) {
  418. ctx_template = &s->s3->handshake_md5;
  419. } else if (md_nid == EVP_MD_CTX_type(&s->s3->handshake_hash)) {
  420. ctx_template = &s->s3->handshake_hash;
  421. } else {
  422. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
  423. return 0;
  424. }
  425. EVP_MD_CTX ctx;
  426. EVP_MD_CTX_init(&ctx);
  427. if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
  428. EVP_MD_CTX_cleanup(&ctx);
  429. return 0;
  430. }
  431. unsigned ret;
  432. EVP_DigestFinal_ex(&ctx, out, &ret);
  433. EVP_MD_CTX_cleanup(&ctx);
  434. return ret;
  435. }
  436. static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
  437. size_t max_out) {
  438. int ret = 0;
  439. EVP_MD_CTX ctx_copy;
  440. EVP_MD_CTX_init(&ctx_copy);
  441. if (EVP_MD_CTX_size(ctx) > max_out) {
  442. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  443. goto err;
  444. }
  445. unsigned len;
  446. if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) ||
  447. !EVP_DigestFinal_ex(&ctx_copy, out, &len)) {
  448. goto err;
  449. }
  450. assert(len == EVP_MD_CTX_size(ctx));
  451. *out_len = len;
  452. ret = 1;
  453. err:
  454. EVP_MD_CTX_cleanup(&ctx_copy);
  455. return ret;
  456. }
  457. /* tls1_handshake_digest calculates the current handshake hash and writes it to
  458. * |out|, which has space for |out_len| bytes. It returns the number of bytes
  459. * written or -1 in the event of an error. This function works on a copy of the
  460. * underlying digests so can be called multiple times and prior to the final
  461. * update etc. */
  462. int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
  463. size_t md5_len = 0;
  464. if (EVP_MD_CTX_md(&s->s3->handshake_md5) != NULL &&
  465. !append_digest(&s->s3->handshake_md5, out, &md5_len, out_len)) {
  466. return -1;
  467. }
  468. size_t len;
  469. if (!append_digest(&s->s3->handshake_hash, out + md5_len, &len,
  470. out_len - md5_len)) {
  471. return -1;
  472. }
  473. return (int)(md5_len + len);
  474. }
  475. int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
  476. uint8_t buf[2 * EVP_MAX_MD_SIZE];
  477. int err = 0;
  478. int digests_len;
  479. /* At this point, the handshake should have released the handshake buffer on
  480. * its own. */
  481. assert(s->s3->handshake_buffer == NULL);
  482. digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
  483. if (digests_len < 0) {
  484. err = 1;
  485. digests_len = 0;
  486. }
  487. if (!s->enc_method->prf(s, out, 12, s->session->master_key,
  488. s->session->master_key_length, str, slen, buf,
  489. digests_len, NULL, 0)) {
  490. err = 1;
  491. }
  492. if (err) {
  493. return 0;
  494. } else {
  495. return 12;
  496. }
  497. }
  498. int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
  499. size_t premaster_len) {
  500. if (s->s3->tmp.extended_master_secret) {
  501. uint8_t digests[2 * EVP_MAX_MD_SIZE];
  502. int digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
  503. if (digests_len == -1) {
  504. return 0;
  505. }
  506. if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
  507. premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  508. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
  509. digests_len, NULL, 0)) {
  510. return 0;
  511. }
  512. } else {
  513. if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
  514. premaster_len, TLS_MD_MASTER_SECRET_CONST,
  515. TLS_MD_MASTER_SECRET_CONST_SIZE,
  516. s->s3->client_random, SSL3_RANDOM_SIZE,
  517. s->s3->server_random, SSL3_RANDOM_SIZE)) {
  518. return 0;
  519. }
  520. }
  521. return SSL3_MASTER_SECRET_SIZE;
  522. }
  523. int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
  524. const char *label, size_t label_len,
  525. const uint8_t *context, size_t context_len,
  526. int use_context) {
  527. if (!s->s3->have_version || s->version == SSL3_VERSION) {
  528. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  529. return 0;
  530. }
  531. size_t seed_len = 2 * SSL3_RANDOM_SIZE;
  532. if (use_context) {
  533. if (context_len >= 1u << 16) {
  534. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  535. return 0;
  536. }
  537. seed_len += 2 + context_len;
  538. }
  539. uint8_t *seed = OPENSSL_malloc(seed_len);
  540. if (seed == NULL) {
  541. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  542. return 0;
  543. }
  544. memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE);
  545. memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
  546. if (use_context) {
  547. seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
  548. seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
  549. memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
  550. }
  551. int ret = s->enc_method->prf(s, out, out_len, s->session->master_key,
  552. s->session->master_key_length, label, label_len,
  553. seed, seed_len, NULL, 0);
  554. OPENSSL_free(seed);
  555. return ret;
  556. }
  557. int tls1_alert_code(int code) {
  558. switch (code) {
  559. case SSL_AD_CLOSE_NOTIFY:
  560. return SSL3_AD_CLOSE_NOTIFY;
  561. case SSL_AD_UNEXPECTED_MESSAGE:
  562. return SSL3_AD_UNEXPECTED_MESSAGE;
  563. case SSL_AD_BAD_RECORD_MAC:
  564. return SSL3_AD_BAD_RECORD_MAC;
  565. case SSL_AD_DECRYPTION_FAILED:
  566. return TLS1_AD_DECRYPTION_FAILED;
  567. case SSL_AD_RECORD_OVERFLOW:
  568. return TLS1_AD_RECORD_OVERFLOW;
  569. case SSL_AD_DECOMPRESSION_FAILURE:
  570. return SSL3_AD_DECOMPRESSION_FAILURE;
  571. case SSL_AD_HANDSHAKE_FAILURE:
  572. return SSL3_AD_HANDSHAKE_FAILURE;
  573. case SSL_AD_NO_CERTIFICATE:
  574. return -1;
  575. case SSL_AD_BAD_CERTIFICATE:
  576. return SSL3_AD_BAD_CERTIFICATE;
  577. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  578. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  579. case SSL_AD_CERTIFICATE_REVOKED:
  580. return SSL3_AD_CERTIFICATE_REVOKED;
  581. case SSL_AD_CERTIFICATE_EXPIRED:
  582. return SSL3_AD_CERTIFICATE_EXPIRED;
  583. case SSL_AD_CERTIFICATE_UNKNOWN:
  584. return SSL3_AD_CERTIFICATE_UNKNOWN;
  585. case SSL_AD_ILLEGAL_PARAMETER:
  586. return SSL3_AD_ILLEGAL_PARAMETER;
  587. case SSL_AD_UNKNOWN_CA:
  588. return TLS1_AD_UNKNOWN_CA;
  589. case SSL_AD_ACCESS_DENIED:
  590. return TLS1_AD_ACCESS_DENIED;
  591. case SSL_AD_DECODE_ERROR:
  592. return TLS1_AD_DECODE_ERROR;
  593. case SSL_AD_DECRYPT_ERROR:
  594. return TLS1_AD_DECRYPT_ERROR;
  595. case SSL_AD_EXPORT_RESTRICTION:
  596. return TLS1_AD_EXPORT_RESTRICTION;
  597. case SSL_AD_PROTOCOL_VERSION:
  598. return TLS1_AD_PROTOCOL_VERSION;
  599. case SSL_AD_INSUFFICIENT_SECURITY:
  600. return TLS1_AD_INSUFFICIENT_SECURITY;
  601. case SSL_AD_INTERNAL_ERROR:
  602. return TLS1_AD_INTERNAL_ERROR;
  603. case SSL_AD_USER_CANCELLED:
  604. return TLS1_AD_USER_CANCELLED;
  605. case SSL_AD_NO_RENEGOTIATION:
  606. return TLS1_AD_NO_RENEGOTIATION;
  607. case SSL_AD_UNSUPPORTED_EXTENSION:
  608. return TLS1_AD_UNSUPPORTED_EXTENSION;
  609. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  610. return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
  611. case SSL_AD_UNRECOGNIZED_NAME:
  612. return TLS1_AD_UNRECOGNIZED_NAME;
  613. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  614. return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  615. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  616. return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
  617. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  618. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  619. case SSL_AD_INAPPROPRIATE_FALLBACK:
  620. return SSL3_AD_INAPPROPRIATE_FALLBACK;
  621. default:
  622. return -1;
  623. }
  624. }