Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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