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.

t1_enc.c 24 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 (!SSL_IS_DTLS(s)) {
  270. memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8);
  271. }
  272. mac_secret_len = s->s3->tmp.new_mac_secret_len;
  273. iv_len = s->s3->tmp.new_fixed_iv_len;
  274. if (aead == NULL) {
  275. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  276. return 0;
  277. }
  278. key_len = EVP_AEAD_key_length(aead);
  279. if (mac_secret_len > 0) {
  280. /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher
  281. * suites) the key length reported by |EVP_AEAD_key_length| will
  282. * include the MAC and IV key bytes. */
  283. if (key_len < mac_secret_len + iv_len) {
  284. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  285. return 0;
  286. }
  287. key_len -= mac_secret_len + iv_len;
  288. }
  289. key_data = s->s3->tmp.key_block;
  290. client_write_mac_secret = key_data;
  291. key_data += mac_secret_len;
  292. server_write_mac_secret = key_data;
  293. key_data += mac_secret_len;
  294. client_write_key = key_data;
  295. key_data += key_len;
  296. server_write_key = key_data;
  297. key_data += key_len;
  298. client_write_iv = key_data;
  299. key_data += iv_len;
  300. server_write_iv = key_data;
  301. key_data += iv_len;
  302. if (use_client_keys) {
  303. mac_secret = client_write_mac_secret;
  304. key = client_write_key;
  305. iv = client_write_iv;
  306. } else {
  307. mac_secret = server_write_mac_secret;
  308. key = server_write_key;
  309. iv = server_write_iv;
  310. }
  311. if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
  312. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  313. return 0;
  314. }
  315. if (is_read) {
  316. SSL_AEAD_CTX_free(s->aead_read_ctx);
  317. s->aead_read_ctx = SSL_AEAD_CTX_new(
  318. evp_aead_open, ssl3_version_from_wire(s, s->version),
  319. s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
  320. iv_len);
  321. return s->aead_read_ctx != NULL;
  322. }
  323. SSL_AEAD_CTX_free(s->aead_write_ctx);
  324. s->aead_write_ctx = SSL_AEAD_CTX_new(
  325. evp_aead_seal, ssl3_version_from_wire(s, s->version),
  326. s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
  327. iv_len);
  328. if (s->aead_write_ctx == NULL) {
  329. return 0;
  330. }
  331. s->s3->need_record_splitting = 0;
  332. if (!SSL_USE_EXPLICIT_IV(s) &&
  333. (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
  334. SSL_CIPHER_is_block_cipher(s->s3->tmp.new_cipher)) {
  335. /* Enable 1/n-1 record-splitting to randomize the IV. See
  336. * https://www.openssl.org/~bodo/tls-cbc.txt and the BEAST attack. */
  337. s->s3->need_record_splitting = 1;
  338. }
  339. return 1;
  340. }
  341. int tls1_setup_key_block(SSL *s) {
  342. uint8_t *p;
  343. const EVP_AEAD *aead = NULL;
  344. int ret = 0;
  345. size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
  346. size_t key_block_len;
  347. if (s->s3->tmp.key_block_length != 0) {
  348. return 1;
  349. }
  350. if (s->session->cipher == NULL) {
  351. goto cipher_unavailable_err;
  352. }
  353. if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
  354. s->session->cipher,
  355. ssl3_version_from_wire(s, s->version))) {
  356. goto cipher_unavailable_err;
  357. }
  358. key_len = EVP_AEAD_key_length(aead);
  359. variable_iv_len = EVP_AEAD_nonce_length(aead);
  360. if (mac_secret_len > 0) {
  361. /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
  362. * key length reported by |EVP_AEAD_key_length| will include the MAC key
  363. * bytes and initial implicit IV. */
  364. if (key_len < mac_secret_len + fixed_iv_len) {
  365. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  366. return 0;
  367. }
  368. key_len -= mac_secret_len + fixed_iv_len;
  369. } else {
  370. /* The nonce is split into a fixed portion and a variable portion. */
  371. if (variable_iv_len < fixed_iv_len) {
  372. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  373. return 0;
  374. }
  375. variable_iv_len -= fixed_iv_len;
  376. }
  377. assert(mac_secret_len < 256);
  378. assert(fixed_iv_len < 256);
  379. assert(variable_iv_len < 256);
  380. s->s3->tmp.new_aead = aead;
  381. s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
  382. s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
  383. s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
  384. key_block_len = key_len + mac_secret_len + fixed_iv_len;
  385. key_block_len *= 2;
  386. ssl3_cleanup_key_block(s);
  387. p = (uint8_t *)OPENSSL_malloc(key_block_len);
  388. if (p == NULL) {
  389. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  390. goto err;
  391. }
  392. s->s3->tmp.key_block_length = key_block_len;
  393. s->s3->tmp.key_block = p;
  394. if (!tls1_generate_key_block(s, p, key_block_len)) {
  395. goto err;
  396. }
  397. ret = 1;
  398. err:
  399. return ret;
  400. cipher_unavailable_err:
  401. OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  402. return 0;
  403. }
  404. int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
  405. const EVP_MD_CTX *ctx_template;
  406. if (md_nid == NID_md5) {
  407. ctx_template = &s->s3->handshake_md5;
  408. } else if (md_nid == EVP_MD_CTX_type(&s->s3->handshake_hash)) {
  409. ctx_template = &s->s3->handshake_hash;
  410. } else {
  411. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
  412. return 0;
  413. }
  414. EVP_MD_CTX ctx;
  415. EVP_MD_CTX_init(&ctx);
  416. if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
  417. EVP_MD_CTX_cleanup(&ctx);
  418. return 0;
  419. }
  420. unsigned ret;
  421. EVP_DigestFinal_ex(&ctx, out, &ret);
  422. EVP_MD_CTX_cleanup(&ctx);
  423. return ret;
  424. }
  425. static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
  426. size_t max_out) {
  427. int ret = 0;
  428. EVP_MD_CTX ctx_copy;
  429. EVP_MD_CTX_init(&ctx_copy);
  430. if (EVP_MD_CTX_size(ctx) > max_out) {
  431. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  432. goto err;
  433. }
  434. unsigned len;
  435. if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) ||
  436. !EVP_DigestFinal_ex(&ctx_copy, out, &len)) {
  437. goto err;
  438. }
  439. assert(len == EVP_MD_CTX_size(ctx));
  440. *out_len = len;
  441. ret = 1;
  442. err:
  443. EVP_MD_CTX_cleanup(&ctx_copy);
  444. return ret;
  445. }
  446. /* tls1_handshake_digest calculates the current handshake hash and writes it to
  447. * |out|, which has space for |out_len| bytes. It returns the number of bytes
  448. * written or -1 in the event of an error. This function works on a copy of the
  449. * underlying digests so can be called multiple times and prior to the final
  450. * update etc. */
  451. int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
  452. size_t md5_len = 0;
  453. if (EVP_MD_CTX_md(&s->s3->handshake_md5) != NULL &&
  454. !append_digest(&s->s3->handshake_md5, out, &md5_len, out_len)) {
  455. return -1;
  456. }
  457. size_t len;
  458. if (!append_digest(&s->s3->handshake_hash, out + md5_len, &len,
  459. out_len - md5_len)) {
  460. return -1;
  461. }
  462. return (int)(md5_len + len);
  463. }
  464. int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
  465. uint8_t buf[2 * EVP_MAX_MD_SIZE];
  466. int err = 0;
  467. int digests_len;
  468. /* At this point, the handshake should have released the handshake buffer on
  469. * its own. */
  470. assert(s->s3->handshake_buffer == NULL);
  471. digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
  472. if (digests_len < 0) {
  473. err = 1;
  474. digests_len = 0;
  475. }
  476. if (!s->enc_method->prf(s, out, 12, s->session->master_key,
  477. s->session->master_key_length, str, slen, buf,
  478. digests_len, NULL, 0)) {
  479. err = 1;
  480. }
  481. if (err) {
  482. return 0;
  483. } else {
  484. return 12;
  485. }
  486. }
  487. int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
  488. size_t premaster_len) {
  489. if (s->s3->tmp.extended_master_secret) {
  490. uint8_t digests[2 * EVP_MAX_MD_SIZE];
  491. int digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
  492. if (digests_len == -1) {
  493. return 0;
  494. }
  495. if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
  496. premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  497. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
  498. digests_len, NULL, 0)) {
  499. return 0;
  500. }
  501. } else {
  502. if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
  503. premaster_len, TLS_MD_MASTER_SECRET_CONST,
  504. TLS_MD_MASTER_SECRET_CONST_SIZE,
  505. s->s3->client_random, SSL3_RANDOM_SIZE,
  506. s->s3->server_random, SSL3_RANDOM_SIZE)) {
  507. return 0;
  508. }
  509. }
  510. return SSL3_MASTER_SECRET_SIZE;
  511. }
  512. int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
  513. const char *label, size_t label_len,
  514. const uint8_t *context, size_t context_len,
  515. int use_context) {
  516. if (!s->s3->have_version || s->version == SSL3_VERSION) {
  517. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  518. return 0;
  519. }
  520. size_t seed_len = 2 * SSL3_RANDOM_SIZE;
  521. if (use_context) {
  522. if (context_len >= 1u << 16) {
  523. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  524. return 0;
  525. }
  526. seed_len += 2 + context_len;
  527. }
  528. uint8_t *seed = OPENSSL_malloc(seed_len);
  529. if (seed == NULL) {
  530. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  531. return 0;
  532. }
  533. memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE);
  534. memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
  535. if (use_context) {
  536. seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
  537. seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
  538. memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
  539. }
  540. int ret = s->enc_method->prf(s, out, out_len, s->session->master_key,
  541. s->session->master_key_length, label, label_len,
  542. seed, seed_len, NULL, 0);
  543. OPENSSL_free(seed);
  544. return ret;
  545. }
  546. int tls1_alert_code(int code) {
  547. switch (code) {
  548. case SSL_AD_CLOSE_NOTIFY:
  549. return SSL3_AD_CLOSE_NOTIFY;
  550. case SSL_AD_UNEXPECTED_MESSAGE:
  551. return SSL3_AD_UNEXPECTED_MESSAGE;
  552. case SSL_AD_BAD_RECORD_MAC:
  553. return SSL3_AD_BAD_RECORD_MAC;
  554. case SSL_AD_DECRYPTION_FAILED:
  555. return TLS1_AD_DECRYPTION_FAILED;
  556. case SSL_AD_RECORD_OVERFLOW:
  557. return TLS1_AD_RECORD_OVERFLOW;
  558. case SSL_AD_DECOMPRESSION_FAILURE:
  559. return SSL3_AD_DECOMPRESSION_FAILURE;
  560. case SSL_AD_HANDSHAKE_FAILURE:
  561. return SSL3_AD_HANDSHAKE_FAILURE;
  562. case SSL_AD_NO_CERTIFICATE:
  563. return -1;
  564. case SSL_AD_BAD_CERTIFICATE:
  565. return SSL3_AD_BAD_CERTIFICATE;
  566. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  567. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  568. case SSL_AD_CERTIFICATE_REVOKED:
  569. return SSL3_AD_CERTIFICATE_REVOKED;
  570. case SSL_AD_CERTIFICATE_EXPIRED:
  571. return SSL3_AD_CERTIFICATE_EXPIRED;
  572. case SSL_AD_CERTIFICATE_UNKNOWN:
  573. return SSL3_AD_CERTIFICATE_UNKNOWN;
  574. case SSL_AD_ILLEGAL_PARAMETER:
  575. return SSL3_AD_ILLEGAL_PARAMETER;
  576. case SSL_AD_UNKNOWN_CA:
  577. return TLS1_AD_UNKNOWN_CA;
  578. case SSL_AD_ACCESS_DENIED:
  579. return TLS1_AD_ACCESS_DENIED;
  580. case SSL_AD_DECODE_ERROR:
  581. return TLS1_AD_DECODE_ERROR;
  582. case SSL_AD_DECRYPT_ERROR:
  583. return TLS1_AD_DECRYPT_ERROR;
  584. case SSL_AD_EXPORT_RESTRICTION:
  585. return TLS1_AD_EXPORT_RESTRICTION;
  586. case SSL_AD_PROTOCOL_VERSION:
  587. return TLS1_AD_PROTOCOL_VERSION;
  588. case SSL_AD_INSUFFICIENT_SECURITY:
  589. return TLS1_AD_INSUFFICIENT_SECURITY;
  590. case SSL_AD_INTERNAL_ERROR:
  591. return TLS1_AD_INTERNAL_ERROR;
  592. case SSL_AD_USER_CANCELLED:
  593. return TLS1_AD_USER_CANCELLED;
  594. case SSL_AD_NO_RENEGOTIATION:
  595. return TLS1_AD_NO_RENEGOTIATION;
  596. case SSL_AD_UNSUPPORTED_EXTENSION:
  597. return TLS1_AD_UNSUPPORTED_EXTENSION;
  598. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  599. return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
  600. case SSL_AD_UNRECOGNIZED_NAME:
  601. return TLS1_AD_UNRECOGNIZED_NAME;
  602. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  603. return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  604. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  605. return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
  606. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  607. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  608. case SSL_AD_INAPPROPRIATE_FALLBACK:
  609. return SSL3_AD_INAPPROPRIATE_FALLBACK;
  610. default:
  611. return -1;
  612. }
  613. }