t1_enc.c 31 KB

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