t1_enc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /* ssl/t1_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-2002 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. #include <stdio.h>
  112. #include "ssl_locl.h"
  113. #include <openssl/comp.h>
  114. #include <openssl/evp.h>
  115. #include <openssl/hmac.h>
  116. #include <openssl/md5.h>
  117. #include <openssl/fips.h>
  118. static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
  119. int sec_len, unsigned char *seed, int seed_len,
  120. unsigned char *out, int olen)
  121. {
  122. int chunk,n;
  123. unsigned int j;
  124. HMAC_CTX ctx;
  125. HMAC_CTX ctx_tmp;
  126. unsigned char A1[EVP_MAX_MD_SIZE];
  127. unsigned int A1_len;
  128. chunk=EVP_MD_size(md);
  129. HMAC_CTX_init(&ctx);
  130. HMAC_CTX_init(&ctx_tmp);
  131. HMAC_Init_ex(&ctx,sec,sec_len,md, NULL);
  132. HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL);
  133. HMAC_Update(&ctx,seed,seed_len);
  134. HMAC_Final(&ctx,A1,&A1_len);
  135. n=0;
  136. for (;;)
  137. {
  138. HMAC_Init_ex(&ctx,NULL,0,NULL,NULL); /* re-init */
  139. HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL); /* re-init */
  140. HMAC_Update(&ctx,A1,A1_len);
  141. HMAC_Update(&ctx_tmp,A1,A1_len);
  142. HMAC_Update(&ctx,seed,seed_len);
  143. if (olen > chunk)
  144. {
  145. HMAC_Final(&ctx,out,&j);
  146. out+=j;
  147. olen-=j;
  148. HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */
  149. }
  150. else /* last one */
  151. {
  152. HMAC_Final(&ctx,A1,&A1_len);
  153. memcpy(out,A1,olen);
  154. break;
  155. }
  156. }
  157. HMAC_CTX_cleanup(&ctx);
  158. HMAC_CTX_cleanup(&ctx_tmp);
  159. OPENSSL_cleanse(A1,sizeof(A1));
  160. }
  161. static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1,
  162. unsigned char *label, int label_len,
  163. const unsigned char *sec, int slen, unsigned char *out1,
  164. unsigned char *out2, int olen)
  165. {
  166. int len,i;
  167. const unsigned char *S1,*S2;
  168. len=slen/2;
  169. S1=sec;
  170. S2= &(sec[len]);
  171. len+=(slen&1); /* add for odd, make longer */
  172. #ifdef OPENSSL_FIPS
  173. FIPS_allow_md5(1);
  174. #endif
  175. tls1_P_hash(md5 ,S1,len,label,label_len,out1,olen);
  176. #ifdef OPENSSL_FIPS
  177. FIPS_allow_md5(0);
  178. #endif
  179. tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);
  180. for (i=0; i<olen; i++)
  181. out1[i]^=out2[i];
  182. }
  183. static void tls1_generate_key_block(SSL *s, unsigned char *km,
  184. unsigned char *tmp, int num)
  185. {
  186. unsigned char *p;
  187. unsigned char buf[SSL3_RANDOM_SIZE*2+
  188. TLS_MD_MAX_CONST_SIZE];
  189. p=buf;
  190. memcpy(p,TLS_MD_KEY_EXPANSION_CONST,
  191. TLS_MD_KEY_EXPANSION_CONST_SIZE);
  192. p+=TLS_MD_KEY_EXPANSION_CONST_SIZE;
  193. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  194. p+=SSL3_RANDOM_SIZE;
  195. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  196. p+=SSL3_RANDOM_SIZE;
  197. tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),
  198. s->session->master_key,s->session->master_key_length,
  199. km,tmp,num);
  200. #ifdef KSSL_DEBUG
  201. printf("tls1_generate_key_block() ==> %d byte master_key =\n\t",
  202. s->session->master_key_length);
  203. {
  204. int i;
  205. for (i=0; i < s->session->master_key_length; i++)
  206. {
  207. printf("%02X", s->session->master_key[i]);
  208. }
  209. printf("\n"); }
  210. #endif /* KSSL_DEBUG */
  211. }
  212. int tls1_change_cipher_state(SSL *s, int which)
  213. {
  214. static const unsigned char empty[]="";
  215. unsigned char *p,*key_block,*mac_secret;
  216. unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+
  217. SSL3_RANDOM_SIZE*2];
  218. unsigned char tmp1[EVP_MAX_KEY_LENGTH];
  219. unsigned char tmp2[EVP_MAX_KEY_LENGTH];
  220. unsigned char iv1[EVP_MAX_IV_LENGTH*2];
  221. unsigned char iv2[EVP_MAX_IV_LENGTH*2];
  222. unsigned char *ms,*key,*iv,*er1,*er2;
  223. int client_write;
  224. EVP_CIPHER_CTX *dd;
  225. const EVP_CIPHER *c;
  226. const SSL_COMP *comp;
  227. const EVP_MD *m;
  228. int is_export,n,i,j,k,exp_label_len,cl;
  229. int reuse_dd = 0;
  230. is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
  231. c=s->s3->tmp.new_sym_enc;
  232. m=s->s3->tmp.new_hash;
  233. comp=s->s3->tmp.new_compression;
  234. key_block=s->s3->tmp.key_block;
  235. #ifdef KSSL_DEBUG
  236. printf("tls1_change_cipher_state(which= %d) w/\n", which);
  237. printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms,
  238. comp);
  239. printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c);
  240. printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",
  241. c->nid,c->block_size,c->key_len,c->iv_len);
  242. printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length);
  243. {
  244. int i;
  245. for (i=0; i<s->s3->tmp.key_block_length; i++)
  246. printf("%02x", key_block[i]); printf("\n");
  247. }
  248. #endif /* KSSL_DEBUG */
  249. if (which & SSL3_CC_READ)
  250. {
  251. if (s->enc_read_ctx != NULL)
  252. reuse_dd = 1;
  253. else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
  254. goto err;
  255. dd= s->enc_read_ctx;
  256. s->read_hash=m;
  257. if (s->expand != NULL)
  258. {
  259. COMP_CTX_free(s->expand);
  260. s->expand=NULL;
  261. }
  262. if (comp != NULL)
  263. {
  264. s->expand=COMP_CTX_new(comp->method);
  265. if (s->expand == NULL)
  266. {
  267. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
  268. goto err2;
  269. }
  270. if (s->s3->rrec.comp == NULL)
  271. s->s3->rrec.comp=(unsigned char *)
  272. OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
  273. if (s->s3->rrec.comp == NULL)
  274. goto err;
  275. }
  276. memset(&(s->s3->read_sequence[0]),0,8);
  277. mac_secret= &(s->s3->read_mac_secret[0]);
  278. }
  279. else
  280. {
  281. if (s->enc_write_ctx != NULL)
  282. reuse_dd = 1;
  283. else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
  284. goto err;
  285. if ((s->enc_write_ctx == NULL) &&
  286. ((s->enc_write_ctx=(EVP_CIPHER_CTX *)
  287. OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
  288. goto err;
  289. dd= s->enc_write_ctx;
  290. s->write_hash=m;
  291. if (s->compress != NULL)
  292. {
  293. COMP_CTX_free(s->compress);
  294. s->compress=NULL;
  295. }
  296. if (comp != NULL)
  297. {
  298. s->compress=COMP_CTX_new(comp->method);
  299. if (s->compress == NULL)
  300. {
  301. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
  302. goto err2;
  303. }
  304. }
  305. memset(&(s->s3->write_sequence[0]),0,8);
  306. mac_secret= &(s->s3->write_mac_secret[0]);
  307. }
  308. if (reuse_dd)
  309. EVP_CIPHER_CTX_cleanup(dd);
  310. EVP_CIPHER_CTX_init(dd);
  311. p=s->s3->tmp.key_block;
  312. i=EVP_MD_size(m);
  313. cl=EVP_CIPHER_key_length(c);
  314. j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
  315. cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
  316. /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
  317. k=EVP_CIPHER_iv_length(c);
  318. er1= &(s->s3->client_random[0]);
  319. er2= &(s->s3->server_random[0]);
  320. if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  321. (which == SSL3_CHANGE_CIPHER_SERVER_READ))
  322. {
  323. ms= &(p[ 0]); n=i+i;
  324. key= &(p[ n]); n+=j+j;
  325. iv= &(p[ n]); n+=k+k;
  326. exp_label=(unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;
  327. exp_label_len=TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;
  328. client_write=1;
  329. }
  330. else
  331. {
  332. n=i;
  333. ms= &(p[ n]); n+=i+j;
  334. key= &(p[ n]); n+=j+k;
  335. iv= &(p[ n]); n+=k;
  336. exp_label=(unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;
  337. exp_label_len=TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;
  338. client_write=0;
  339. }
  340. if (n > s->s3->tmp.key_block_length)
  341. {
  342. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_INTERNAL_ERROR);
  343. goto err2;
  344. }
  345. memcpy(mac_secret,ms,i);
  346. #ifdef TLS_DEBUG
  347. printf("which = %04X\nmac key=",which);
  348. { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
  349. #endif
  350. if (is_export)
  351. {
  352. /* In here I set both the read and write key/iv to the
  353. * same value since only the correct one will be used :-).
  354. */
  355. p=buf;
  356. memcpy(p,exp_label,exp_label_len);
  357. p+=exp_label_len;
  358. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  359. p+=SSL3_RANDOM_SIZE;
  360. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  361. p+=SSL3_RANDOM_SIZE;
  362. tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,
  363. tmp1,tmp2,EVP_CIPHER_key_length(c));
  364. key=tmp1;
  365. if (k > 0)
  366. {
  367. p=buf;
  368. memcpy(p,TLS_MD_IV_BLOCK_CONST,
  369. TLS_MD_IV_BLOCK_CONST_SIZE);
  370. p+=TLS_MD_IV_BLOCK_CONST_SIZE;
  371. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  372. p+=SSL3_RANDOM_SIZE;
  373. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  374. p+=SSL3_RANDOM_SIZE;
  375. tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0,
  376. iv1,iv2,k*2);
  377. if (client_write)
  378. iv=iv1;
  379. else
  380. iv= &(iv1[k]);
  381. }
  382. }
  383. s->session->key_arg_length=0;
  384. #ifdef KSSL_DEBUG
  385. {
  386. int i;
  387. printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");
  388. printf("\tkey= "); for (i=0; i<c->key_len; i++) printf("%02x", key[i]);
  389. printf("\n");
  390. printf("\t iv= "); for (i=0; i<c->iv_len; i++) printf("%02x", iv[i]);
  391. printf("\n");
  392. }
  393. #endif /* KSSL_DEBUG */
  394. EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));
  395. #ifdef TLS_DEBUG
  396. printf("which = %04X\nkey=",which);
  397. { int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }
  398. printf("\niv=");
  399. { int z; for (z=0; z<k; z++) printf("%02X%c",iv[z],((z+1)%16)?' ':'\n'); }
  400. printf("\n");
  401. #endif
  402. OPENSSL_cleanse(tmp1,sizeof(tmp1));
  403. OPENSSL_cleanse(tmp2,sizeof(tmp1));
  404. OPENSSL_cleanse(iv1,sizeof(iv1));
  405. OPENSSL_cleanse(iv2,sizeof(iv2));
  406. return(1);
  407. err:
  408. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_MALLOC_FAILURE);
  409. err2:
  410. return(0);
  411. }
  412. int tls1_setup_key_block(SSL *s)
  413. {
  414. unsigned char *p1,*p2;
  415. const EVP_CIPHER *c;
  416. const EVP_MD *hash;
  417. int num;
  418. SSL_COMP *comp;
  419. #ifdef KSSL_DEBUG
  420. printf ("tls1_setup_key_block()\n");
  421. #endif /* KSSL_DEBUG */
  422. if (s->s3->tmp.key_block_length != 0)
  423. return(1);
  424. if (!ssl_cipher_get_evp(s->session,&c,&hash,&comp))
  425. {
  426. SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  427. return(0);
  428. }
  429. s->s3->tmp.new_sym_enc=c;
  430. s->s3->tmp.new_hash=hash;
  431. num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c);
  432. num*=2;
  433. ssl3_cleanup_key_block(s);
  434. if ((p1=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  435. goto err;
  436. if ((p2=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  437. goto err;
  438. s->s3->tmp.key_block_length=num;
  439. s->s3->tmp.key_block=p1;
  440. #ifdef TLS_DEBUG
  441. printf("client random\n");
  442. { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->client_random[z],((z+1)%16)?' ':'\n'); }
  443. printf("server random\n");
  444. { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->server_random[z],((z+1)%16)?' ':'\n'); }
  445. printf("pre-master\n");
  446. { int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); }
  447. #endif
  448. tls1_generate_key_block(s,p1,p2,num);
  449. OPENSSL_cleanse(p2,num);
  450. OPENSSL_free(p2);
  451. #ifdef TLS_DEBUG
  452. printf("\nkey block\n");
  453. { int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); }
  454. #endif
  455. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  456. {
  457. /* enable vulnerability countermeasure for CBC ciphers with
  458. * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  459. */
  460. s->s3->need_empty_fragments = 1;
  461. if (s->session->cipher != NULL)
  462. {
  463. if ((s->session->cipher->algorithms & SSL_ENC_MASK) == SSL_eNULL)
  464. s->s3->need_empty_fragments = 0;
  465. #ifndef OPENSSL_NO_RC4
  466. if ((s->session->cipher->algorithms & SSL_ENC_MASK) == SSL_RC4)
  467. s->s3->need_empty_fragments = 0;
  468. #endif
  469. }
  470. }
  471. return(1);
  472. err:
  473. SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE);
  474. return(0);
  475. }
  476. int tls1_enc(SSL *s, int send)
  477. {
  478. SSL3_RECORD *rec;
  479. EVP_CIPHER_CTX *ds;
  480. unsigned long l;
  481. int bs,i,ii,j,k,n=0;
  482. const EVP_CIPHER *enc;
  483. if (send)
  484. {
  485. if (s->write_hash != NULL)
  486. n=EVP_MD_size(s->write_hash);
  487. ds=s->enc_write_ctx;
  488. rec= &(s->s3->wrec);
  489. if (s->enc_write_ctx == NULL)
  490. enc=NULL;
  491. else
  492. enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  493. }
  494. else
  495. {
  496. if (s->read_hash != NULL)
  497. n=EVP_MD_size(s->read_hash);
  498. ds=s->enc_read_ctx;
  499. rec= &(s->s3->rrec);
  500. if (s->enc_read_ctx == NULL)
  501. enc=NULL;
  502. else
  503. enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  504. }
  505. #ifdef KSSL_DEBUG
  506. printf("tls1_enc(%d)\n", send);
  507. #endif /* KSSL_DEBUG */
  508. if ((s->session == NULL) || (ds == NULL) ||
  509. (enc == NULL))
  510. {
  511. memmove(rec->data,rec->input,rec->length);
  512. rec->input=rec->data;
  513. }
  514. else
  515. {
  516. l=rec->length;
  517. bs=EVP_CIPHER_block_size(ds->cipher);
  518. if ((bs != 1) && send)
  519. {
  520. i=bs-((int)l%bs);
  521. /* Add weird padding of upto 256 bytes */
  522. /* we need to add 'i' padding bytes of value j */
  523. j=i-1;
  524. if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG)
  525. {
  526. if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
  527. j++;
  528. }
  529. for (k=(int)l; k<(int)(l+i); k++)
  530. rec->input[k]=j;
  531. l+=i;
  532. rec->length+=i;
  533. }
  534. #ifdef KSSL_DEBUG
  535. {
  536. unsigned long ui;
  537. printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
  538. ds,rec->data,rec->input,l);
  539. printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n",
  540. ds->buf_len, ds->cipher->key_len,
  541. DES_KEY_SZ, DES_SCHEDULE_SZ,
  542. ds->cipher->iv_len);
  543. printf("\t\tIV: ");
  544. for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]);
  545. printf("\n");
  546. printf("\trec->input=");
  547. for (ui=0; ui<l; ui++) printf(" %02x", rec->input[ui]);
  548. printf("\n");
  549. }
  550. #endif /* KSSL_DEBUG */
  551. if (!send)
  552. {
  553. if (l == 0 || l%bs != 0)
  554. {
  555. SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
  556. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
  557. return 0;
  558. }
  559. }
  560. EVP_Cipher(ds,rec->data,rec->input,l);
  561. #ifdef KSSL_DEBUG
  562. {
  563. unsigned long i;
  564. printf("\trec->data=");
  565. for (i=0; i<l; i++)
  566. printf(" %02x", rec->data[i]); printf("\n");
  567. }
  568. #endif /* KSSL_DEBUG */
  569. if ((bs != 1) && !send)
  570. {
  571. ii=i=rec->data[l-1]; /* padding_length */
  572. i++;
  573. if (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG)
  574. {
  575. /* First packet is even in size, so check */
  576. if ((memcmp(s->s3->read_sequence,
  577. "\0\0\0\0\0\0\0\0",8) == 0) && !(ii & 1))
  578. s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG;
  579. if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
  580. i--;
  581. }
  582. /* TLS 1.0 does not bound the number of padding bytes by the block size.
  583. * All of them must have value 'padding_length'. */
  584. if (i > (int)rec->length)
  585. {
  586. /* Incorrect padding. SSLerr() and ssl3_alert are done
  587. * by caller: we don't want to reveal whether this is
  588. * a decryption error or a MAC verification failure
  589. * (see http://www.openssl.org/~bodo/tls-cbc.txt) */
  590. return -1;
  591. }
  592. for (j=(int)(l-i); j<(int)l; j++)
  593. {
  594. if (rec->data[j] != ii)
  595. {
  596. /* Incorrect padding */
  597. return -1;
  598. }
  599. }
  600. rec->length-=i;
  601. }
  602. }
  603. return(1);
  604. }
  605. int tls1_cert_verify_mac(SSL *s, EVP_MD_CTX *in_ctx, unsigned char *out)
  606. {
  607. unsigned int ret;
  608. EVP_MD_CTX ctx;
  609. EVP_MD_CTX_init(&ctx);
  610. EVP_MD_CTX_copy_ex(&ctx,in_ctx);
  611. #ifdef OPENSSL_FIPS
  612. FIPS_allow_md5(1);
  613. #endif
  614. EVP_DigestFinal_ex(&ctx,out,&ret);
  615. #ifdef OPENSSL_FIPS
  616. FIPS_allow_md5(0);
  617. #endif
  618. EVP_MD_CTX_cleanup(&ctx);
  619. return((int)ret);
  620. }
  621. int tls1_final_finish_mac(SSL *s, EVP_MD_CTX *in1_ctx, EVP_MD_CTX *in2_ctx,
  622. const char *str, int slen, unsigned char *out)
  623. {
  624. unsigned int i;
  625. EVP_MD_CTX ctx;
  626. unsigned char buf[TLS_MD_MAX_CONST_SIZE+MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  627. unsigned char *q,buf2[12];
  628. q=buf;
  629. memcpy(q,str,slen);
  630. q+=slen;
  631. EVP_MD_CTX_init(&ctx);
  632. EVP_MD_CTX_copy_ex(&ctx,in1_ctx);
  633. #ifdef OPENSSL_FIPS
  634. FIPS_allow_md5(1);
  635. #endif
  636. EVP_DigestFinal_ex(&ctx,q,&i);
  637. #ifdef OPENSSL_FIPS
  638. FIPS_allow_md5(0);
  639. #endif
  640. q+=i;
  641. EVP_MD_CTX_copy_ex(&ctx,in2_ctx);
  642. EVP_DigestFinal_ex(&ctx,q,&i);
  643. q+=i;
  644. tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(q-buf),
  645. s->session->master_key,s->session->master_key_length,
  646. out,buf2,sizeof buf2);
  647. EVP_MD_CTX_cleanup(&ctx);
  648. return sizeof buf2;
  649. }
  650. int tls1_mac(SSL *ssl, unsigned char *md, int send)
  651. {
  652. SSL3_RECORD *rec;
  653. unsigned char *mac_sec,*seq;
  654. const EVP_MD *hash;
  655. unsigned int md_size;
  656. int i;
  657. HMAC_CTX hmac;
  658. unsigned char buf[5];
  659. if (send)
  660. {
  661. rec= &(ssl->s3->wrec);
  662. mac_sec= &(ssl->s3->write_mac_secret[0]);
  663. seq= &(ssl->s3->write_sequence[0]);
  664. hash=ssl->write_hash;
  665. }
  666. else
  667. {
  668. rec= &(ssl->s3->rrec);
  669. mac_sec= &(ssl->s3->read_mac_secret[0]);
  670. seq= &(ssl->s3->read_sequence[0]);
  671. hash=ssl->read_hash;
  672. }
  673. md_size=EVP_MD_size(hash);
  674. buf[0]=rec->type;
  675. buf[1]=TLS1_VERSION_MAJOR;
  676. buf[2]=TLS1_VERSION_MINOR;
  677. buf[3]=rec->length>>8;
  678. buf[4]=rec->length&0xff;
  679. /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
  680. HMAC_CTX_init(&hmac);
  681. HMAC_Init_ex(&hmac,mac_sec,EVP_MD_size(hash),hash,NULL);
  682. HMAC_Update(&hmac,seq,8);
  683. HMAC_Update(&hmac,buf,5);
  684. HMAC_Update(&hmac,rec->input,rec->length);
  685. HMAC_Final(&hmac,md,&md_size);
  686. HMAC_CTX_cleanup(&hmac);
  687. #ifdef TLS_DEBUG
  688. printf("sec=");
  689. {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); }
  690. printf("seq=");
  691. {int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); }
  692. printf("buf=");
  693. {int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); }
  694. printf("rec=");
  695. {unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); }
  696. #endif
  697. for (i=7; i>=0; i--)
  698. {
  699. ++seq[i];
  700. if (seq[i] != 0) break;
  701. }
  702. #ifdef TLS_DEBUG
  703. {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); }
  704. #endif
  705. return(md_size);
  706. }
  707. int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  708. int len)
  709. {
  710. unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE];
  711. unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
  712. #ifdef KSSL_DEBUG
  713. printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len);
  714. #endif /* KSSL_DEBUG */
  715. /* Setup the stuff to munge */
  716. memcpy(buf,TLS_MD_MASTER_SECRET_CONST,
  717. TLS_MD_MASTER_SECRET_CONST_SIZE);
  718. memcpy(&(buf[TLS_MD_MASTER_SECRET_CONST_SIZE]),
  719. s->s3->client_random,SSL3_RANDOM_SIZE);
  720. memcpy(&(buf[SSL3_RANDOM_SIZE+TLS_MD_MASTER_SECRET_CONST_SIZE]),
  721. s->s3->server_random,SSL3_RANDOM_SIZE);
  722. tls1_PRF(s->ctx->md5,s->ctx->sha1,
  723. buf,TLS_MD_MASTER_SECRET_CONST_SIZE+SSL3_RANDOM_SIZE*2,p,len,
  724. s->session->master_key,buff,sizeof buff);
  725. #ifdef KSSL_DEBUG
  726. printf ("tls1_generate_master_secret() complete\n");
  727. #endif /* KSSL_DEBUG */
  728. return(SSL3_MASTER_SECRET_SIZE);
  729. }
  730. int tls1_alert_code(int code)
  731. {
  732. switch (code)
  733. {
  734. case SSL_AD_CLOSE_NOTIFY: return(SSL3_AD_CLOSE_NOTIFY);
  735. case SSL_AD_UNEXPECTED_MESSAGE: return(SSL3_AD_UNEXPECTED_MESSAGE);
  736. case SSL_AD_BAD_RECORD_MAC: return(SSL3_AD_BAD_RECORD_MAC);
  737. case SSL_AD_DECRYPTION_FAILED: return(TLS1_AD_DECRYPTION_FAILED);
  738. case SSL_AD_RECORD_OVERFLOW: return(TLS1_AD_RECORD_OVERFLOW);
  739. case SSL_AD_DECOMPRESSION_FAILURE:return(SSL3_AD_DECOMPRESSION_FAILURE);
  740. case SSL_AD_HANDSHAKE_FAILURE: return(SSL3_AD_HANDSHAKE_FAILURE);
  741. case SSL_AD_NO_CERTIFICATE: return(-1);
  742. case SSL_AD_BAD_CERTIFICATE: return(SSL3_AD_BAD_CERTIFICATE);
  743. case SSL_AD_UNSUPPORTED_CERTIFICATE:return(SSL3_AD_UNSUPPORTED_CERTIFICATE);
  744. case SSL_AD_CERTIFICATE_REVOKED:return(SSL3_AD_CERTIFICATE_REVOKED);
  745. case SSL_AD_CERTIFICATE_EXPIRED:return(SSL3_AD_CERTIFICATE_EXPIRED);
  746. case SSL_AD_CERTIFICATE_UNKNOWN:return(SSL3_AD_CERTIFICATE_UNKNOWN);
  747. case SSL_AD_ILLEGAL_PARAMETER: return(SSL3_AD_ILLEGAL_PARAMETER);
  748. case SSL_AD_UNKNOWN_CA: return(TLS1_AD_UNKNOWN_CA);
  749. case SSL_AD_ACCESS_DENIED: return(TLS1_AD_ACCESS_DENIED);
  750. case SSL_AD_DECODE_ERROR: return(TLS1_AD_DECODE_ERROR);
  751. case SSL_AD_DECRYPT_ERROR: return(TLS1_AD_DECRYPT_ERROR);
  752. case SSL_AD_EXPORT_RESTRICTION: return(TLS1_AD_EXPORT_RESTRICTION);
  753. case SSL_AD_PROTOCOL_VERSION: return(TLS1_AD_PROTOCOL_VERSION);
  754. case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY);
  755. case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR);
  756. case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED);
  757. case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION);
  758. default: return(-1);
  759. }
  760. }