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.
 
 
 
 
 
 

886 lines
26 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. *g
  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. *g
  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. *g
  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) fromg
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *g
  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. *g
  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.g
  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 <stdio.h>
  136. #include <assert.h>
  137. #include <openssl/err.h>
  138. #include <openssl/evp.h>
  139. #include <openssl/mem.h>
  140. #include <openssl/md5.h>
  141. #include <openssl/obj.h>
  142. #include "ssl_locl.h"
  143. static unsigned char ssl3_pad_1[48]={
  144. 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  145. 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  146. 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  147. 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  148. 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  149. 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36 };
  150. static unsigned char ssl3_pad_2[48]={
  151. 0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,
  152. 0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,
  153. 0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,
  154. 0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,
  155. 0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,
  156. 0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c,0x5c };
  157. static int ssl3_handshake_mac(SSL *s, int md_nid,
  158. const char *sender, int len, unsigned char *p);
  159. static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num)
  160. {
  161. EVP_MD_CTX m5;
  162. EVP_MD_CTX s1;
  163. unsigned char buf[16],smd[SHA_DIGEST_LENGTH];
  164. unsigned char c='A';
  165. unsigned int i,j,k;
  166. #ifdef CHARSET_EBCDIC
  167. c = os_toascii[c]; /*'A' in ASCII */
  168. #endif
  169. k=0;
  170. EVP_MD_CTX_init(&m5);
  171. EVP_MD_CTX_init(&s1);
  172. for (i=0; (int)i<num; i+=MD5_DIGEST_LENGTH)
  173. {
  174. k++;
  175. if (k > sizeof buf)
  176. {
  177. /* bug: 'buf' is too small for this ciphersuite */
  178. OPENSSL_PUT_ERROR(SSL, ssl3_generate_key_block, ERR_R_INTERNAL_ERROR);
  179. return 0;
  180. }
  181. for (j=0; j<k; j++)
  182. buf[j]=c;
  183. c++;
  184. EVP_DigestInit_ex(&s1,EVP_sha1(), NULL);
  185. EVP_DigestUpdate(&s1,buf,k);
  186. EVP_DigestUpdate(&s1,s->session->master_key,
  187. s->session->master_key_length);
  188. EVP_DigestUpdate(&s1,s->s3->server_random,SSL3_RANDOM_SIZE);
  189. EVP_DigestUpdate(&s1,s->s3->client_random,SSL3_RANDOM_SIZE);
  190. EVP_DigestFinal_ex(&s1,smd,NULL);
  191. EVP_DigestInit_ex(&m5,EVP_md5(), NULL);
  192. EVP_DigestUpdate(&m5,s->session->master_key,
  193. s->session->master_key_length);
  194. EVP_DigestUpdate(&m5,smd,SHA_DIGEST_LENGTH);
  195. if ((int)(i+MD5_DIGEST_LENGTH) > num)
  196. {
  197. EVP_DigestFinal_ex(&m5,smd,NULL);
  198. memcpy(km,smd,(num-i));
  199. }
  200. else
  201. EVP_DigestFinal_ex(&m5,km,NULL);
  202. km+=MD5_DIGEST_LENGTH;
  203. }
  204. OPENSSL_cleanse(smd,SHA_DIGEST_LENGTH);
  205. EVP_MD_CTX_cleanup(&m5);
  206. EVP_MD_CTX_cleanup(&s1);
  207. return 1;
  208. }
  209. int ssl3_change_cipher_state(SSL *s, int which)
  210. {
  211. unsigned char *p,*mac_secret;
  212. unsigned char exp_key[EVP_MAX_KEY_LENGTH];
  213. unsigned char exp_iv[EVP_MAX_IV_LENGTH];
  214. unsigned char *ms,*key,*iv,*er1,*er2;
  215. EVP_CIPHER_CTX *dd;
  216. const EVP_CIPHER *c;
  217. const EVP_MD *m;
  218. EVP_MD_CTX md;
  219. int is_exp,n,i,j,k,cl;
  220. int reuse_dd = 0;
  221. is_exp=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
  222. c=s->s3->tmp.new_sym_enc;
  223. m=s->s3->tmp.new_hash;
  224. /* m == NULL will lead to a crash later */
  225. assert(m);
  226. if (which & SSL3_CC_READ)
  227. {
  228. if (s->enc_read_ctx != NULL)
  229. reuse_dd = 1;
  230. else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
  231. goto err;
  232. else
  233. /* make sure it's intialized in case we exit later with an error */
  234. EVP_CIPHER_CTX_init(s->enc_read_ctx);
  235. dd= s->enc_read_ctx;
  236. ssl_replace_hash(&s->read_hash,m);
  237. memset(&(s->s3->read_sequence[0]),0,8);
  238. mac_secret= &(s->s3->read_mac_secret[0]);
  239. }
  240. else
  241. {
  242. if (s->enc_write_ctx != NULL)
  243. reuse_dd = 1;
  244. else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
  245. goto err;
  246. else
  247. /* make sure it's intialized in case we exit later with an error */
  248. EVP_CIPHER_CTX_init(s->enc_write_ctx);
  249. dd= s->enc_write_ctx;
  250. ssl_replace_hash(&s->write_hash,m);
  251. memset(&(s->s3->write_sequence[0]),0,8);
  252. mac_secret= &(s->s3->write_mac_secret[0]);
  253. }
  254. if (reuse_dd)
  255. EVP_CIPHER_CTX_cleanup(dd);
  256. p=s->s3->tmp.key_block;
  257. i=EVP_MD_size(m);
  258. cl=EVP_CIPHER_key_length(c);
  259. j=is_exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
  260. cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
  261. /* Was j=(is_exp)?5:EVP_CIPHER_key_length(c); */
  262. k=EVP_CIPHER_iv_length(c);
  263. if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  264. (which == SSL3_CHANGE_CIPHER_SERVER_READ))
  265. {
  266. ms= &(p[ 0]); n=i+i;
  267. key= &(p[ n]); n+=j+j;
  268. iv= &(p[ n]); n+=k+k;
  269. er1= &(s->s3->client_random[0]);
  270. er2= &(s->s3->server_random[0]);
  271. }
  272. else
  273. {
  274. n=i;
  275. ms= &(p[ n]); n+=i+j;
  276. key= &(p[ n]); n+=j+k;
  277. iv= &(p[ n]); n+=k;
  278. er1= &(s->s3->server_random[0]);
  279. er2= &(s->s3->client_random[0]);
  280. }
  281. if (n > s->s3->tmp.key_block_length)
  282. {
  283. OPENSSL_PUT_ERROR(SSL, ssl3_change_cipher_state, ERR_R_INTERNAL_ERROR);
  284. goto err2;
  285. }
  286. EVP_MD_CTX_init(&md);
  287. memcpy(mac_secret,ms,i);
  288. if (is_exp)
  289. {
  290. /* In here I set both the read and write key/iv to the
  291. * same value since only the correct one will be used :-).
  292. */
  293. EVP_DigestInit_ex(&md,EVP_md5(), NULL);
  294. EVP_DigestUpdate(&md,key,j);
  295. EVP_DigestUpdate(&md,er1,SSL3_RANDOM_SIZE);
  296. EVP_DigestUpdate(&md,er2,SSL3_RANDOM_SIZE);
  297. EVP_DigestFinal_ex(&md,&(exp_key[0]),NULL);
  298. key= &(exp_key[0]);
  299. if (k > 0)
  300. {
  301. EVP_DigestInit_ex(&md,EVP_md5(), NULL);
  302. EVP_DigestUpdate(&md,er1,SSL3_RANDOM_SIZE);
  303. EVP_DigestUpdate(&md,er2,SSL3_RANDOM_SIZE);
  304. EVP_DigestFinal_ex(&md,&(exp_iv[0]),NULL);
  305. iv= &(exp_iv[0]);
  306. }
  307. }
  308. s->session->key_arg_length=0;
  309. EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));
  310. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  311. if (s->msg_callback)
  312. {
  313. int wh = which & SSL3_CC_WRITE ?
  314. TLS1_RT_CRYPTO_WRITE : TLS1_RT_CRYPTO_READ;
  315. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
  316. mac_secret, EVP_MD_size(m),
  317. s, s->msg_callback_arg);
  318. if (c->key_len)
  319. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
  320. key, c->key_len,
  321. s, s->msg_callback_arg);
  322. if (k)
  323. {
  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[0]),sizeof(exp_key));
  330. OPENSSL_cleanse(&(exp_iv[0]),sizeof(exp_iv));
  331. EVP_MD_CTX_cleanup(&md);
  332. return(1);
  333. err:
  334. OPENSSL_PUT_ERROR(SSL, ssl3_change_cipher_state, ERR_R_MALLOC_FAILURE);
  335. err2:
  336. return(0);
  337. }
  338. int ssl3_setup_key_block(SSL *s)
  339. {
  340. unsigned char *p;
  341. const EVP_CIPHER *c;
  342. const EVP_MD *hash;
  343. size_t num;
  344. int ret = 0;
  345. SSL_COMP *comp;
  346. if (s->s3->tmp.key_block_length != 0)
  347. return(1);
  348. if (!ssl_cipher_get_comp(s->session, &comp))
  349. {
  350. OPENSSL_PUT_ERROR(SSL, ssl3_setup_key_block, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  351. return(0);
  352. }
  353. if (!ssl_cipher_get_evp(s->session,&c,&hash,NULL,NULL))
  354. {
  355. OPENSSL_PUT_ERROR(SSL, ssl3_setup_key_block, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  356. return(0);
  357. }
  358. s->s3->tmp.new_sym_enc=c;
  359. s->s3->tmp.new_hash=hash;
  360. s->s3->tmp.new_compression=NULL;
  361. num=EVP_MD_size(hash);
  362. num=EVP_CIPHER_key_length(c)+num+EVP_CIPHER_iv_length(c);
  363. num*=2;
  364. ssl3_cleanup_key_block(s);
  365. if ((p=OPENSSL_malloc(num)) == NULL)
  366. goto err;
  367. s->s3->tmp.key_block_length=num;
  368. s->s3->tmp.key_block=p;
  369. ret = ssl3_generate_key_block(s,p,num);
  370. /* enable vulnerability countermeasure for CBC ciphers with
  371. * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt) */
  372. if ((s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0)
  373. {
  374. s->s3->need_record_splitting = 1;
  375. if (s->session->cipher != NULL)
  376. {
  377. if (s->session->cipher->algorithm_enc == SSL_eNULL)
  378. s->s3->need_record_splitting = 0;
  379. #ifndef OPENSSL_NO_RC4
  380. if (s->session->cipher->algorithm_enc == SSL_RC4)
  381. s->s3->need_record_splitting = 0;
  382. #endif
  383. }
  384. }
  385. return ret;
  386. err:
  387. OPENSSL_PUT_ERROR(SSL, ssl3_setup_key_block, ERR_R_MALLOC_FAILURE);
  388. return(0);
  389. }
  390. void ssl3_cleanup_key_block(SSL *s)
  391. {
  392. if (s->s3->tmp.key_block != NULL)
  393. {
  394. OPENSSL_cleanse(s->s3->tmp.key_block,
  395. s->s3->tmp.key_block_length);
  396. OPENSSL_free(s->s3->tmp.key_block);
  397. s->s3->tmp.key_block=NULL;
  398. }
  399. s->s3->tmp.key_block_length=0;
  400. }
  401. /* ssl3_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
  402. *
  403. * Returns:
  404. * 0: (in non-constant time) if the record is publically invalid (i.e. too
  405. * short etc).
  406. * 1: if the record's padding is valid / the encryption was successful.
  407. * -1: if the record's padding is invalid or, if sending, an internal error
  408. * occured.
  409. */
  410. int ssl3_enc(SSL *s, int send)
  411. {
  412. SSL3_RECORD *rec;
  413. EVP_CIPHER_CTX *ds;
  414. unsigned long l;
  415. int bs,i,mac_size=0;
  416. const EVP_CIPHER *enc;
  417. if (send)
  418. {
  419. ds=s->enc_write_ctx;
  420. rec= &(s->s3->wrec);
  421. if (s->enc_write_ctx == NULL)
  422. enc=NULL;
  423. else
  424. enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  425. }
  426. else
  427. {
  428. ds=s->enc_read_ctx;
  429. rec= &(s->s3->rrec);
  430. if (s->enc_read_ctx == NULL)
  431. enc=NULL;
  432. else
  433. enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  434. }
  435. if ((s->session == NULL) || (ds == NULL) ||
  436. (enc == NULL))
  437. {
  438. memmove(rec->data,rec->input,rec->length);
  439. rec->input=rec->data;
  440. }
  441. else
  442. {
  443. l=rec->length;
  444. bs=EVP_CIPHER_block_size(ds->cipher);
  445. /* COMPRESS */
  446. if ((bs != 1) && send)
  447. {
  448. i=bs-((int)l%bs);
  449. /* we need to add 'i-1' padding bytes */
  450. l+=i;
  451. /* the last of these zero bytes will be overwritten
  452. * with the padding length. */
  453. memset(&rec->input[rec->length], 0, i);
  454. rec->length+=i;
  455. rec->input[l-1]=(i-1);
  456. }
  457. if (!send)
  458. {
  459. if (l == 0 || l%bs != 0)
  460. return 0;
  461. /* otherwise, rec->length >= bs */
  462. }
  463. EVP_Cipher(ds,rec->data,rec->input,l);
  464. if (EVP_MD_CTX_md(s->read_hash) != NULL)
  465. mac_size = EVP_MD_CTX_size(s->read_hash);
  466. if ((bs != 1) && !send)
  467. return ssl3_cbc_remove_padding(s, rec, bs, mac_size);
  468. }
  469. return(1);
  470. }
  471. void ssl3_init_finished_mac(SSL *s)
  472. {
  473. if (s->s3->handshake_buffer) BIO_free(s->s3->handshake_buffer);
  474. if (s->s3->handshake_dgst) ssl3_free_digest_list(s);
  475. s->s3->handshake_buffer=BIO_new(BIO_s_mem());
  476. (void)BIO_set_close(s->s3->handshake_buffer,BIO_CLOSE);
  477. }
  478. void ssl3_free_digest_list(SSL *s)
  479. {
  480. int i;
  481. if (!s->s3->handshake_dgst) return;
  482. for (i=0;i<SSL_MAX_DIGEST;i++)
  483. {
  484. if (s->s3->handshake_dgst[i])
  485. EVP_MD_CTX_destroy(s->s3->handshake_dgst[i]);
  486. }
  487. OPENSSL_free(s->s3->handshake_dgst);
  488. s->s3->handshake_dgst=NULL;
  489. }
  490. void ssl3_finish_mac(SSL *s, const unsigned char *buf, int len)
  491. {
  492. if (s->s3->handshake_buffer && !(s->s3->flags & TLS1_FLAGS_KEEP_HANDSHAKE))
  493. {
  494. BIO_write (s->s3->handshake_buffer,(void *)buf,len);
  495. }
  496. else
  497. {
  498. int i;
  499. for (i=0;i< SSL_MAX_DIGEST;i++)
  500. {
  501. if (s->s3->handshake_dgst[i]!= NULL)
  502. EVP_DigestUpdate(s->s3->handshake_dgst[i],buf,len);
  503. }
  504. }
  505. }
  506. int ssl3_digest_cached_records(SSL *s)
  507. {
  508. int i;
  509. long mask;
  510. const EVP_MD *md;
  511. long hdatalen;
  512. char *hdata;
  513. /* Allocate handshake_dgst array */
  514. ssl3_free_digest_list(s);
  515. s->s3->handshake_dgst = OPENSSL_malloc(SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
  516. memset(s->s3->handshake_dgst,0,SSL_MAX_DIGEST *sizeof(EVP_MD_CTX *));
  517. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,&hdata);
  518. if (hdatalen <= 0)
  519. {
  520. OPENSSL_PUT_ERROR(SSL, ssl3_digest_cached_records, SSL_R_BAD_HANDSHAKE_LENGTH);
  521. return 0;
  522. }
  523. /* Loop through bitso of algorithm2 field and create MD_CTX-es */
  524. for (i=0;ssl_get_handshake_digest(i,&mask,&md); i++)
  525. {
  526. if ((mask & ssl_get_algorithm2(s)) && md)
  527. {
  528. s->s3->handshake_dgst[i]=EVP_MD_CTX_create();
  529. EVP_DigestInit_ex(s->s3->handshake_dgst[i],md,NULL);
  530. EVP_DigestUpdate(s->s3->handshake_dgst[i],hdata,hdatalen);
  531. }
  532. else
  533. {
  534. s->s3->handshake_dgst[i]=NULL;
  535. }
  536. }
  537. if (!(s->s3->flags & TLS1_FLAGS_KEEP_HANDSHAKE))
  538. {
  539. /* Free handshake_buffer BIO */
  540. BIO_free(s->s3->handshake_buffer);
  541. s->s3->handshake_buffer = NULL;
  542. }
  543. return 1;
  544. }
  545. int ssl3_cert_verify_mac(SSL *s, int md_nid, unsigned char *p)
  546. {
  547. return(ssl3_handshake_mac(s,md_nid,NULL,0,p));
  548. }
  549. int ssl3_final_finish_mac(SSL *s,
  550. const char *sender, int len, unsigned char *p)
  551. {
  552. int ret;
  553. ret=ssl3_handshake_mac(s,NID_md5,sender,len,p);
  554. p+=ret;
  555. ret+=ssl3_handshake_mac(s,NID_sha1,sender,len,p);
  556. return(ret);
  557. }
  558. static int ssl3_handshake_mac(SSL *s, int md_nid,
  559. const char *sender, int len, unsigned char *p)
  560. {
  561. unsigned int ret;
  562. int npad,n;
  563. unsigned int i;
  564. unsigned char md_buf[EVP_MAX_MD_SIZE];
  565. EVP_MD_CTX ctx,*d=NULL;
  566. if (s->s3->handshake_buffer)
  567. if (!ssl3_digest_cached_records(s))
  568. return 0;
  569. /* Search for digest of specified type in the handshake_dgst
  570. * array*/
  571. for (i=0;i<SSL_MAX_DIGEST;i++)
  572. {
  573. if (s->s3->handshake_dgst[i]&&EVP_MD_CTX_type(s->s3->handshake_dgst[i])==md_nid)
  574. {
  575. d=s->s3->handshake_dgst[i];
  576. break;
  577. }
  578. }
  579. if (!d) {
  580. OPENSSL_PUT_ERROR(SSL, ssl3_handshake_mac, SSL_R_NO_REQUIRED_DIGEST);
  581. return 0;
  582. }
  583. EVP_MD_CTX_init(&ctx);
  584. EVP_MD_CTX_copy_ex(&ctx,d);
  585. n=EVP_MD_CTX_size(&ctx);
  586. if (n < 0)
  587. return 0;
  588. npad=(48/n)*n;
  589. if (sender != NULL)
  590. EVP_DigestUpdate(&ctx,sender,len);
  591. EVP_DigestUpdate(&ctx,s->session->master_key,
  592. s->session->master_key_length);
  593. EVP_DigestUpdate(&ctx,ssl3_pad_1,npad);
  594. EVP_DigestFinal_ex(&ctx,md_buf,&i);
  595. EVP_DigestInit_ex(&ctx,EVP_MD_CTX_md(&ctx), NULL);
  596. EVP_DigestUpdate(&ctx,s->session->master_key,
  597. s->session->master_key_length);
  598. EVP_DigestUpdate(&ctx,ssl3_pad_2,npad);
  599. EVP_DigestUpdate(&ctx,md_buf,i);
  600. EVP_DigestFinal_ex(&ctx,p,&ret);
  601. EVP_MD_CTX_cleanup(&ctx);
  602. return((int)ret);
  603. }
  604. int n_ssl3_mac(SSL *ssl, unsigned char *md, int send)
  605. {
  606. SSL3_RECORD *rec;
  607. unsigned char *mac_sec,*seq;
  608. EVP_MD_CTX md_ctx;
  609. const EVP_MD_CTX *hash;
  610. unsigned char *p,rec_char;
  611. size_t md_size, orig_len;
  612. int npad;
  613. int t;
  614. if (send)
  615. {
  616. rec= &(ssl->s3->wrec);
  617. mac_sec= &(ssl->s3->write_mac_secret[0]);
  618. seq= &(ssl->s3->write_sequence[0]);
  619. hash=ssl->write_hash;
  620. }
  621. else
  622. {
  623. rec= &(ssl->s3->rrec);
  624. mac_sec= &(ssl->s3->read_mac_secret[0]);
  625. seq= &(ssl->s3->read_sequence[0]);
  626. hash=ssl->read_hash;
  627. }
  628. t=EVP_MD_CTX_size(hash);
  629. if (t < 0 || t > 20)
  630. return -1;
  631. md_size=t;
  632. npad=(48/md_size)*md_size;
  633. /* kludge: ssl3_cbc_remove_padding passes padding length in rec->type */
  634. orig_len = rec->length+md_size+((unsigned int)rec->type>>8);
  635. rec->type &= 0xff;
  636. if (!send &&
  637. EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  638. ssl3_cbc_record_digest_supported(hash))
  639. {
  640. /* This is a CBC-encrypted record. We must avoid leaking any
  641. * timing-side channel information about how many blocks of
  642. * data we are hashing because that gives an attacker a
  643. * timing-oracle. */
  644. /* npad is, at most, 48 bytes and that's with MD5:
  645. * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
  646. *
  647. * With SHA-1 (the largest hash speced for SSLv3) the hash size
  648. * goes up 4, but npad goes down by 8, resulting in a smaller
  649. * total size. */
  650. unsigned char header[75];
  651. unsigned j = 0;
  652. memcpy(header+j, mac_sec, md_size);
  653. j += md_size;
  654. memcpy(header+j, ssl3_pad_1, npad);
  655. j += npad;
  656. memcpy(header+j, seq, 8);
  657. j += 8;
  658. header[j++] = rec->type;
  659. header[j++] = rec->length >> 8;
  660. header[j++] = rec->length & 0xff;
  661. ssl3_cbc_digest_record(
  662. hash,
  663. md, &md_size,
  664. header, rec->input,
  665. rec->length + md_size, orig_len,
  666. mac_sec, md_size,
  667. 1 /* is SSLv3 */);
  668. }
  669. else
  670. {
  671. unsigned int md_size_u;
  672. /* Chop the digest off the end :-) */
  673. EVP_MD_CTX_init(&md_ctx);
  674. EVP_MD_CTX_copy_ex( &md_ctx,hash);
  675. EVP_DigestUpdate(&md_ctx,mac_sec,md_size);
  676. EVP_DigestUpdate(&md_ctx,ssl3_pad_1,npad);
  677. EVP_DigestUpdate(&md_ctx,seq,8);
  678. rec_char=rec->type;
  679. EVP_DigestUpdate(&md_ctx,&rec_char,1);
  680. p=md;
  681. s2n(rec->length,p);
  682. EVP_DigestUpdate(&md_ctx,md,2);
  683. EVP_DigestUpdate(&md_ctx,rec->input,rec->length);
  684. EVP_DigestFinal_ex( &md_ctx,md,NULL);
  685. EVP_MD_CTX_copy_ex( &md_ctx,hash);
  686. EVP_DigestUpdate(&md_ctx,mac_sec,md_size);
  687. EVP_DigestUpdate(&md_ctx,ssl3_pad_2,npad);
  688. EVP_DigestUpdate(&md_ctx,md,md_size);
  689. EVP_DigestFinal_ex( &md_ctx,md,&md_size_u);
  690. md_size = md_size_u;
  691. EVP_MD_CTX_cleanup(&md_ctx);
  692. }
  693. ssl3_record_sequence_update(seq);
  694. return(md_size);
  695. }
  696. void ssl3_record_sequence_update(unsigned char *seq)
  697. {
  698. int i;
  699. for (i=7; i>=0; i--)
  700. {
  701. ++seq[i];
  702. if (seq[i] != 0) break;
  703. }
  704. }
  705. int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  706. int len)
  707. {
  708. static const unsigned char *salt[3]={
  709. #ifndef CHARSET_EBCDIC
  710. (const unsigned char *)"A",
  711. (const unsigned char *)"BB",
  712. (const unsigned char *)"CCC",
  713. #else
  714. (const unsigned char *)"\x41",
  715. (const unsigned char *)"\x42\x42",
  716. (const unsigned char *)"\x43\x43\x43",
  717. #endif
  718. };
  719. unsigned char buf[EVP_MAX_MD_SIZE];
  720. EVP_MD_CTX ctx;
  721. int i,ret=0;
  722. unsigned int n;
  723. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  724. unsigned char *tmpout = out;
  725. #endif
  726. EVP_MD_CTX_init(&ctx);
  727. for (i=0; i<3; i++)
  728. {
  729. EVP_DigestInit_ex(&ctx,s->ctx->sha1, NULL);
  730. EVP_DigestUpdate(&ctx,salt[i],strlen((const char *)salt[i]));
  731. EVP_DigestUpdate(&ctx,p,len);
  732. EVP_DigestUpdate(&ctx,&(s->s3->client_random[0]),
  733. SSL3_RANDOM_SIZE);
  734. EVP_DigestUpdate(&ctx,&(s->s3->server_random[0]),
  735. SSL3_RANDOM_SIZE);
  736. EVP_DigestFinal_ex(&ctx,buf,&n);
  737. EVP_DigestInit_ex(&ctx,s->ctx->md5, NULL);
  738. EVP_DigestUpdate(&ctx,p,len);
  739. EVP_DigestUpdate(&ctx,buf,n);
  740. EVP_DigestFinal_ex(&ctx,out,&n);
  741. out+=n;
  742. ret+=n;
  743. }
  744. EVP_MD_CTX_cleanup(&ctx);
  745. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  746. if (s->msg_callback)
  747. {
  748. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
  749. p, len, s, s->msg_callback_arg);
  750. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
  751. s->s3->client_random, SSL3_RANDOM_SIZE,
  752. s, s->msg_callback_arg);
  753. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
  754. s->s3->server_random, SSL3_RANDOM_SIZE,
  755. s, s->msg_callback_arg);
  756. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
  757. tmpout, SSL3_MASTER_SECRET_SIZE,
  758. s, s->msg_callback_arg);
  759. }
  760. #endif
  761. return(ret);
  762. }
  763. int ssl3_alert_code(int code)
  764. {
  765. switch (code)
  766. {
  767. case SSL_AD_CLOSE_NOTIFY: return(SSL3_AD_CLOSE_NOTIFY);
  768. case SSL_AD_UNEXPECTED_MESSAGE: return(SSL3_AD_UNEXPECTED_MESSAGE);
  769. case SSL_AD_BAD_RECORD_MAC: return(SSL3_AD_BAD_RECORD_MAC);
  770. case SSL_AD_DECRYPTION_FAILED: return(SSL3_AD_BAD_RECORD_MAC);
  771. case SSL_AD_RECORD_OVERFLOW: return(SSL3_AD_BAD_RECORD_MAC);
  772. case SSL_AD_DECOMPRESSION_FAILURE:return(SSL3_AD_DECOMPRESSION_FAILURE);
  773. case SSL_AD_HANDSHAKE_FAILURE: return(SSL3_AD_HANDSHAKE_FAILURE);
  774. case SSL_AD_NO_CERTIFICATE: return(SSL3_AD_NO_CERTIFICATE);
  775. case SSL_AD_BAD_CERTIFICATE: return(SSL3_AD_BAD_CERTIFICATE);
  776. case SSL_AD_UNSUPPORTED_CERTIFICATE:return(SSL3_AD_UNSUPPORTED_CERTIFICATE);
  777. case SSL_AD_CERTIFICATE_REVOKED:return(SSL3_AD_CERTIFICATE_REVOKED);
  778. case SSL_AD_CERTIFICATE_EXPIRED:return(SSL3_AD_CERTIFICATE_EXPIRED);
  779. case SSL_AD_CERTIFICATE_UNKNOWN:return(SSL3_AD_CERTIFICATE_UNKNOWN);
  780. case SSL_AD_ILLEGAL_PARAMETER: return(SSL3_AD_ILLEGAL_PARAMETER);
  781. case SSL_AD_UNKNOWN_CA: return(SSL3_AD_BAD_CERTIFICATE);
  782. case SSL_AD_ACCESS_DENIED: return(SSL3_AD_HANDSHAKE_FAILURE);
  783. case SSL_AD_DECODE_ERROR: return(SSL3_AD_HANDSHAKE_FAILURE);
  784. case SSL_AD_DECRYPT_ERROR: return(SSL3_AD_HANDSHAKE_FAILURE);
  785. case SSL_AD_EXPORT_RESTRICTION: return(SSL3_AD_HANDSHAKE_FAILURE);
  786. case SSL_AD_PROTOCOL_VERSION: return(SSL3_AD_HANDSHAKE_FAILURE);
  787. case SSL_AD_INSUFFICIENT_SECURITY:return(SSL3_AD_HANDSHAKE_FAILURE);
  788. case SSL_AD_INTERNAL_ERROR: return(SSL3_AD_HANDSHAKE_FAILURE);
  789. case SSL_AD_USER_CANCELLED: return(SSL3_AD_HANDSHAKE_FAILURE);
  790. case SSL_AD_NO_RENEGOTIATION: return(-1); /* Don't send it :-) */
  791. case SSL_AD_UNSUPPORTED_EXTENSION: return(SSL3_AD_HANDSHAKE_FAILURE);
  792. case SSL_AD_CERTIFICATE_UNOBTAINABLE: return(SSL3_AD_HANDSHAKE_FAILURE);
  793. case SSL_AD_UNRECOGNIZED_NAME: return(SSL3_AD_HANDSHAKE_FAILURE);
  794. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: return(SSL3_AD_HANDSHAKE_FAILURE);
  795. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: return(SSL3_AD_HANDSHAKE_FAILURE);
  796. case SSL_AD_UNKNOWN_PSK_IDENTITY:return(TLS1_AD_UNKNOWN_PSK_IDENTITY);
  797. case SSL_AD_INAPPROPRIATE_FALLBACK:return(SSL3_AD_INAPPROPRIATE_FALLBACK);
  798. default: return(-1);
  799. }
  800. }