s3_enc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /* ssl/s3_enc.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2005 Nokia. All rights reserved.
  113. *
  114. * The portions of the attached software ("Contribution") is developed by
  115. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  116. * license.
  117. *
  118. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  119. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  120. * support (see RFC 4279) to OpenSSL.
  121. *
  122. * No patent licenses or other rights except those expressly stated in
  123. * the OpenSSL open source license shall be deemed granted or received
  124. * expressly, by implication, estoppel, or otherwise.
  125. *
  126. * No assurances are provided by Nokia that the Contribution does not
  127. * infringe the patent or other intellectual property rights of any third
  128. * party or that the license provides you with all the necessary rights
  129. * to make use of the Contribution.
  130. *
  131. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  132. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  133. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  134. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  135. * OTHERWISE.
  136. */
  137. #include <stdio.h>
  138. #include "ssl_locl.h"
  139. #include <openssl/evp.h>
  140. #include <openssl/md5.h>
  141. static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num)
  142. {
  143. EVP_MD_CTX *m5;
  144. EVP_MD_CTX *s1;
  145. unsigned char buf[16], smd[SHA_DIGEST_LENGTH];
  146. unsigned char c = 'A';
  147. unsigned int i, j, k;
  148. int ret = 0;
  149. #ifdef CHARSET_EBCDIC
  150. c = os_toascii[c]; /* 'A' in ASCII */
  151. #endif
  152. k = 0;
  153. m5 = EVP_MD_CTX_new();
  154. s1 = EVP_MD_CTX_new();
  155. if (m5 == NULL || s1 == NULL) {
  156. SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
  157. goto err;
  158. }
  159. EVP_MD_CTX_set_flags(m5, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  160. for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) {
  161. k++;
  162. if (k > sizeof buf) {
  163. /* bug: 'buf' is too small for this ciphersuite */
  164. SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR);
  165. return 0;
  166. }
  167. for (j = 0; j < k; j++)
  168. buf[j] = c;
  169. c++;
  170. EVP_DigestInit_ex(s1, EVP_sha1(), NULL);
  171. EVP_DigestUpdate(s1, buf, k);
  172. EVP_DigestUpdate(s1, s->session->master_key,
  173. s->session->master_key_length);
  174. EVP_DigestUpdate(s1, s->s3->server_random, SSL3_RANDOM_SIZE);
  175. EVP_DigestUpdate(s1, s->s3->client_random, SSL3_RANDOM_SIZE);
  176. EVP_DigestFinal_ex(s1, smd, NULL);
  177. EVP_DigestInit_ex(m5, EVP_md5(), NULL);
  178. EVP_DigestUpdate(m5, s->session->master_key,
  179. s->session->master_key_length);
  180. EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH);
  181. if ((int)(i + MD5_DIGEST_LENGTH) > num) {
  182. EVP_DigestFinal_ex(m5, smd, NULL);
  183. memcpy(km, smd, (num - i));
  184. } else
  185. EVP_DigestFinal_ex(m5, km, NULL);
  186. km += MD5_DIGEST_LENGTH;
  187. }
  188. OPENSSL_cleanse(smd, sizeof(smd));
  189. ret = 1;
  190. err:
  191. EVP_MD_CTX_free(m5);
  192. EVP_MD_CTX_free(s1);
  193. return ret;
  194. }
  195. int ssl3_change_cipher_state(SSL *s, int which)
  196. {
  197. unsigned char *p, *mac_secret;
  198. unsigned char exp_key[EVP_MAX_KEY_LENGTH];
  199. unsigned char exp_iv[EVP_MAX_IV_LENGTH];
  200. unsigned char *ms, *key, *iv;
  201. EVP_CIPHER_CTX *dd;
  202. const EVP_CIPHER *c;
  203. #ifndef OPENSSL_NO_COMP
  204. COMP_METHOD *comp;
  205. #endif
  206. const EVP_MD *m;
  207. int n, i, j, k, cl;
  208. int reuse_dd = 0;
  209. c = s->s3->tmp.new_sym_enc;
  210. m = s->s3->tmp.new_hash;
  211. /* m == NULL will lead to a crash later */
  212. OPENSSL_assert(m);
  213. #ifndef OPENSSL_NO_COMP
  214. if (s->s3->tmp.new_compression == NULL)
  215. comp = NULL;
  216. else
  217. comp = s->s3->tmp.new_compression->method;
  218. #endif
  219. if (which & SSL3_CC_READ) {
  220. if (s->enc_read_ctx != NULL)
  221. reuse_dd = 1;
  222. else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
  223. goto err;
  224. else
  225. /*
  226. * make sure it's intialized in case we exit later with an error
  227. */
  228. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  229. dd = s->enc_read_ctx;
  230. if (ssl_replace_hash(&s->read_hash, m) == NULL) {
  231. SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  232. goto err2;
  233. }
  234. #ifndef OPENSSL_NO_COMP
  235. /* COMPRESS */
  236. COMP_CTX_free(s->expand);
  237. s->expand = NULL;
  238. if (comp != NULL) {
  239. s->expand = COMP_CTX_new(comp);
  240. if (s->expand == NULL) {
  241. SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,
  242. SSL_R_COMPRESSION_LIBRARY_ERROR);
  243. goto err2;
  244. }
  245. if (!RECORD_LAYER_setup_comp_buffer(&s->rlayer))
  246. goto err;
  247. }
  248. #endif
  249. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  250. mac_secret = &(s->s3->read_mac_secret[0]);
  251. } else {
  252. if (s->enc_write_ctx != NULL)
  253. reuse_dd = 1;
  254. else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)
  255. goto err;
  256. else
  257. /*
  258. * make sure it's intialized in case we exit later with an error
  259. */
  260. EVP_CIPHER_CTX_reset(s->enc_write_ctx);
  261. dd = s->enc_write_ctx;
  262. if (ssl_replace_hash(&s->write_hash, m) == NULL) {
  263. SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  264. goto err2;
  265. }
  266. #ifndef OPENSSL_NO_COMP
  267. /* COMPRESS */
  268. COMP_CTX_free(s->compress);
  269. s->compress = NULL;
  270. if (comp != NULL) {
  271. s->compress = COMP_CTX_new(comp);
  272. if (s->compress == NULL) {
  273. SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,
  274. SSL_R_COMPRESSION_LIBRARY_ERROR);
  275. goto err2;
  276. }
  277. }
  278. #endif
  279. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  280. mac_secret = &(s->s3->write_mac_secret[0]);
  281. }
  282. if (reuse_dd)
  283. EVP_CIPHER_CTX_reset(dd);
  284. p = s->s3->tmp.key_block;
  285. i = EVP_MD_size(m);
  286. if (i < 0)
  287. goto err2;
  288. cl = EVP_CIPHER_key_length(c);
  289. j = cl;
  290. k = EVP_CIPHER_iv_length(c);
  291. if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  292. (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
  293. ms = &(p[0]);
  294. n = i + i;
  295. key = &(p[n]);
  296. n += j + j;
  297. iv = &(p[n]);
  298. n += k + k;
  299. } else {
  300. n = i;
  301. ms = &(p[n]);
  302. n += i + j;
  303. key = &(p[n]);
  304. n += j + k;
  305. iv = &(p[n]);
  306. n += k;
  307. }
  308. if (n > s->s3->tmp.key_block_length) {
  309. SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  310. goto err2;
  311. }
  312. memcpy(mac_secret, ms, i);
  313. EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE));
  314. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  315. if (s->msg_callback) {
  316. int wh = which & SSL3_CC_WRITE ?
  317. TLS1_RT_CRYPTO_WRITE : TLS1_RT_CRYPTO_READ;
  318. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
  319. mac_secret, EVP_MD_size(m), s, s->msg_callback_arg);
  320. if (c->key_len)
  321. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
  322. key, c->key_len, s, s->msg_callback_arg);
  323. if (k) {
  324. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_IV,
  325. iv, k, s, s->msg_callback_arg);
  326. }
  327. }
  328. #endif
  329. OPENSSL_cleanse(exp_key, sizeof(exp_key));
  330. OPENSSL_cleanse(exp_iv, sizeof(exp_iv));
  331. return (1);
  332. err:
  333. SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  334. err2:
  335. OPENSSL_cleanse(exp_key, sizeof(exp_key));
  336. OPENSSL_cleanse(exp_iv, sizeof(exp_iv));
  337. return (0);
  338. }
  339. int ssl3_setup_key_block(SSL *s)
  340. {
  341. unsigned char *p;
  342. const EVP_CIPHER *c;
  343. const EVP_MD *hash;
  344. int num;
  345. int ret = 0;
  346. SSL_COMP *comp;
  347. if (s->s3->tmp.key_block_length != 0)
  348. return (1);
  349. if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, &comp, 0)) {
  350. SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  351. return (0);
  352. }
  353. s->s3->tmp.new_sym_enc = c;
  354. s->s3->tmp.new_hash = hash;
  355. #ifdef OPENSSL_NO_COMP
  356. s->s3->tmp.new_compression = NULL;
  357. #else
  358. s->s3->tmp.new_compression = comp;
  359. #endif
  360. num = EVP_MD_size(hash);
  361. if (num < 0)
  362. return 0;
  363. num = EVP_CIPHER_key_length(c) + num + EVP_CIPHER_iv_length(c);
  364. num *= 2;
  365. ssl3_cleanup_key_block(s);
  366. if ((p = OPENSSL_malloc(num)) == NULL)
  367. goto err;
  368. s->s3->tmp.key_block_length = num;
  369. s->s3->tmp.key_block = p;
  370. ret = ssl3_generate_key_block(s, p, num);
  371. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) {
  372. /*
  373. * enable vulnerability countermeasure for CBC ciphers with known-IV
  374. * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  375. */
  376. s->s3->need_empty_fragments = 1;
  377. if (s->session->cipher != NULL) {
  378. if (s->session->cipher->algorithm_enc == SSL_eNULL)
  379. s->s3->need_empty_fragments = 0;
  380. #ifndef OPENSSL_NO_RC4
  381. if (s->session->cipher->algorithm_enc == SSL_RC4)
  382. s->s3->need_empty_fragments = 0;
  383. #endif
  384. }
  385. }
  386. return ret;
  387. err:
  388. SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
  389. return (0);
  390. }
  391. void ssl3_cleanup_key_block(SSL *s)
  392. {
  393. OPENSSL_clear_free(s->s3->tmp.key_block, s->s3->tmp.key_block_length);
  394. s->s3->tmp.key_block = NULL;
  395. s->s3->tmp.key_block_length = 0;
  396. }
  397. void ssl3_init_finished_mac(SSL *s)
  398. {
  399. ssl3_free_digest_list(s);
  400. s->s3->handshake_buffer = BIO_new(BIO_s_mem());
  401. (void)BIO_set_close(s->s3->handshake_buffer, BIO_CLOSE);
  402. }
  403. /*
  404. * Free digest list. Also frees handshake buffer since they are always freed
  405. * together.
  406. */
  407. void ssl3_free_digest_list(SSL *s)
  408. {
  409. BIO_free(s->s3->handshake_buffer);
  410. s->s3->handshake_buffer = NULL;
  411. EVP_MD_CTX_free(s->s3->handshake_dgst);
  412. s->s3->handshake_dgst = NULL;
  413. }
  414. void ssl3_finish_mac(SSL *s, const unsigned char *buf, int len)
  415. {
  416. if (s->s3->handshake_dgst == NULL)
  417. BIO_write(s->s3->handshake_buffer, (void *)buf, len);
  418. else
  419. EVP_DigestUpdate(s->s3->handshake_dgst, buf, len);
  420. }
  421. int ssl3_digest_cached_records(SSL *s, int keep)
  422. {
  423. const EVP_MD *md;
  424. long hdatalen;
  425. void *hdata;
  426. if (s->s3->handshake_dgst == NULL) {
  427. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  428. if (hdatalen <= 0) {
  429. SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, SSL_R_BAD_HANDSHAKE_LENGTH);
  430. return 0;
  431. }
  432. s->s3->handshake_dgst = EVP_MD_CTX_new();
  433. if (s->s3->handshake_dgst == NULL) {
  434. SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE);
  435. return 0;
  436. }
  437. md = ssl_handshake_md(s);
  438. if (md == NULL) {
  439. SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_INTERNAL_ERROR);
  440. return 0;
  441. }
  442. EVP_DigestInit_ex(s->s3->handshake_dgst, md, NULL);
  443. EVP_DigestUpdate(s->s3->handshake_dgst, hdata, hdatalen);
  444. }
  445. if (keep == 0) {
  446. BIO_free(s->s3->handshake_buffer);
  447. s->s3->handshake_buffer = NULL;
  448. }
  449. return 1;
  450. }
  451. int ssl3_final_finish_mac(SSL *s, const char *sender, int len, unsigned char *p)
  452. {
  453. int ret;
  454. EVP_MD_CTX *ctx = NULL;
  455. if (!ssl3_digest_cached_records(s, 0))
  456. return 0;
  457. if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) {
  458. SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST);
  459. return 0;
  460. }
  461. ctx = EVP_MD_CTX_new();
  462. if (ctx == NULL) {
  463. SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE);
  464. return 0;
  465. }
  466. EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst);
  467. ret = EVP_MD_CTX_size(ctx);
  468. if (ret < 0) {
  469. EVP_MD_CTX_reset(ctx);
  470. return 0;
  471. }
  472. if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0)
  473. || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,
  474. s->session->master_key_length,
  475. s->session->master_key) <= 0
  476. || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
  477. SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);
  478. ret = 0;
  479. }
  480. EVP_MD_CTX_free(ctx);
  481. return ret;
  482. }
  483. int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  484. int len)
  485. {
  486. static const unsigned char *salt[3] = {
  487. #ifndef CHARSET_EBCDIC
  488. (const unsigned char *)"A",
  489. (const unsigned char *)"BB",
  490. (const unsigned char *)"CCC",
  491. #else
  492. (const unsigned char *)"\x41",
  493. (const unsigned char *)"\x42\x42",
  494. (const unsigned char *)"\x43\x43\x43",
  495. #endif
  496. };
  497. unsigned char buf[EVP_MAX_MD_SIZE];
  498. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  499. int i, ret = 0;
  500. unsigned int n;
  501. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  502. unsigned char *tmpout = out;
  503. #endif
  504. if (ctx == NULL) {
  505. SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE);
  506. return 0;
  507. }
  508. for (i = 0; i < 3; i++) {
  509. if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0
  510. || EVP_DigestUpdate(ctx, salt[i],
  511. strlen((const char *)salt[i])) <= 0
  512. || EVP_DigestUpdate(ctx, p, len) <= 0
  513. || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]),
  514. SSL3_RANDOM_SIZE) <= 0
  515. || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]),
  516. SSL3_RANDOM_SIZE) <= 0
  517. || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
  518. || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
  519. || EVP_DigestUpdate(ctx, p, len) <= 0
  520. || EVP_DigestUpdate(ctx, buf, n) <= 0
  521. || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {
  522. SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR);
  523. ret = 0;
  524. break;
  525. }
  526. out += n;
  527. ret += n;
  528. }
  529. EVP_MD_CTX_free(ctx);
  530. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  531. if (ret > 0 && s->msg_callback) {
  532. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
  533. p, len, s, s->msg_callback_arg);
  534. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
  535. s->s3->client_random, SSL3_RANDOM_SIZE,
  536. s, s->msg_callback_arg);
  537. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
  538. s->s3->server_random, SSL3_RANDOM_SIZE,
  539. s, s->msg_callback_arg);
  540. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
  541. tmpout, SSL3_MASTER_SECRET_SIZE,
  542. s, s->msg_callback_arg);
  543. }
  544. #endif
  545. OPENSSL_cleanse(buf, sizeof(buf));
  546. return (ret);
  547. }
  548. int ssl3_alert_code(int code)
  549. {
  550. switch (code) {
  551. case SSL_AD_CLOSE_NOTIFY:
  552. return (SSL3_AD_CLOSE_NOTIFY);
  553. case SSL_AD_UNEXPECTED_MESSAGE:
  554. return (SSL3_AD_UNEXPECTED_MESSAGE);
  555. case SSL_AD_BAD_RECORD_MAC:
  556. return (SSL3_AD_BAD_RECORD_MAC);
  557. case SSL_AD_DECRYPTION_FAILED:
  558. return (SSL3_AD_BAD_RECORD_MAC);
  559. case SSL_AD_RECORD_OVERFLOW:
  560. return (SSL3_AD_BAD_RECORD_MAC);
  561. case SSL_AD_DECOMPRESSION_FAILURE:
  562. return (SSL3_AD_DECOMPRESSION_FAILURE);
  563. case SSL_AD_HANDSHAKE_FAILURE:
  564. return (SSL3_AD_HANDSHAKE_FAILURE);
  565. case SSL_AD_NO_CERTIFICATE:
  566. return (SSL3_AD_NO_CERTIFICATE);
  567. case SSL_AD_BAD_CERTIFICATE:
  568. return (SSL3_AD_BAD_CERTIFICATE);
  569. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  570. return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
  571. case SSL_AD_CERTIFICATE_REVOKED:
  572. return (SSL3_AD_CERTIFICATE_REVOKED);
  573. case SSL_AD_CERTIFICATE_EXPIRED:
  574. return (SSL3_AD_CERTIFICATE_EXPIRED);
  575. case SSL_AD_CERTIFICATE_UNKNOWN:
  576. return (SSL3_AD_CERTIFICATE_UNKNOWN);
  577. case SSL_AD_ILLEGAL_PARAMETER:
  578. return (SSL3_AD_ILLEGAL_PARAMETER);
  579. case SSL_AD_UNKNOWN_CA:
  580. return (SSL3_AD_BAD_CERTIFICATE);
  581. case SSL_AD_ACCESS_DENIED:
  582. return (SSL3_AD_HANDSHAKE_FAILURE);
  583. case SSL_AD_DECODE_ERROR:
  584. return (SSL3_AD_HANDSHAKE_FAILURE);
  585. case SSL_AD_DECRYPT_ERROR:
  586. return (SSL3_AD_HANDSHAKE_FAILURE);
  587. case SSL_AD_EXPORT_RESTRICTION:
  588. return (SSL3_AD_HANDSHAKE_FAILURE);
  589. case SSL_AD_PROTOCOL_VERSION:
  590. return (SSL3_AD_HANDSHAKE_FAILURE);
  591. case SSL_AD_INSUFFICIENT_SECURITY:
  592. return (SSL3_AD_HANDSHAKE_FAILURE);
  593. case SSL_AD_INTERNAL_ERROR:
  594. return (SSL3_AD_HANDSHAKE_FAILURE);
  595. case SSL_AD_USER_CANCELLED:
  596. return (SSL3_AD_HANDSHAKE_FAILURE);
  597. case SSL_AD_NO_RENEGOTIATION:
  598. return (-1); /* Don't send it :-) */
  599. case SSL_AD_UNSUPPORTED_EXTENSION:
  600. return (SSL3_AD_HANDSHAKE_FAILURE);
  601. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  602. return (SSL3_AD_HANDSHAKE_FAILURE);
  603. case SSL_AD_UNRECOGNIZED_NAME:
  604. return (SSL3_AD_HANDSHAKE_FAILURE);
  605. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  606. return (SSL3_AD_HANDSHAKE_FAILURE);
  607. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  608. return (SSL3_AD_HANDSHAKE_FAILURE);
  609. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  610. return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
  611. case SSL_AD_INAPPROPRIATE_FALLBACK:
  612. return (TLS1_AD_INAPPROPRIATE_FALLBACK);
  613. default:
  614. return (-1);
  615. }
  616. }