s3_enc.c 25 KB

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