s3_enc.c 30 KB

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