t1_enc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include "ssl_locl.h"
  12. #include "record/record_locl.h"
  13. #include "internal/ktls.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/comp.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/kdf.h>
  18. #include <openssl/rand.h>
  19. #include <openssl/obj_mac.h>
  20. #include <openssl/trace.h>
  21. /* seed1 through seed5 are concatenated */
  22. static int tls1_PRF(SSL *s,
  23. const void *seed1, size_t seed1_len,
  24. const void *seed2, size_t seed2_len,
  25. const void *seed3, size_t seed3_len,
  26. const void *seed4, size_t seed4_len,
  27. const void *seed5, size_t seed5_len,
  28. const unsigned char *sec, size_t slen,
  29. unsigned char *out, size_t olen, int fatal)
  30. {
  31. const EVP_MD *md = ssl_prf_md(s);
  32. EVP_KDF_CTX *kctx = NULL;
  33. int ret = 0;
  34. if (md == NULL) {
  35. /* Should never happen */
  36. if (fatal)
  37. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
  38. ERR_R_INTERNAL_ERROR);
  39. else
  40. SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
  41. return 0;
  42. }
  43. kctx = EVP_KDF_CTX_new_id(EVP_PKEY_TLS1_PRF);
  44. if (kctx == NULL
  45. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, md) <= 0
  46. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_TLS_SECRET,
  47. sec, (size_t)slen) <= 0
  48. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
  49. seed1, (size_t)seed1_len) <= 0
  50. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
  51. seed2, (size_t)seed2_len) <= 0
  52. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
  53. seed3, (size_t)seed3_len) <= 0
  54. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
  55. seed4, (size_t)seed4_len) <= 0
  56. || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
  57. seed5, (size_t)seed5_len) <= 0
  58. || EVP_KDF_derive(kctx, out, olen) <= 0) {
  59. if (fatal)
  60. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
  61. ERR_R_INTERNAL_ERROR);
  62. else
  63. SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
  64. goto err;
  65. }
  66. ret = 1;
  67. err:
  68. EVP_KDF_CTX_free(kctx);
  69. return ret;
  70. }
  71. static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
  72. {
  73. int ret;
  74. /* Calls SSLfatal() as required */
  75. ret = tls1_PRF(s,
  76. TLS_MD_KEY_EXPANSION_CONST,
  77. TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3.server_random,
  78. SSL3_RANDOM_SIZE, s->s3.client_random, SSL3_RANDOM_SIZE,
  79. NULL, 0, NULL, 0, s->session->master_key,
  80. s->session->master_key_length, km, num, 1);
  81. return ret;
  82. }
  83. #ifndef OPENSSL_NO_KTLS
  84. /*
  85. * Count the number of records that were not processed yet from record boundary.
  86. *
  87. * This function assumes that there are only fully formed records read in the
  88. * record layer. If read_ahead is enabled, then this might be false and this
  89. * function will fail.
  90. */
  91. static int count_unprocessed_records(SSL *s)
  92. {
  93. SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
  94. PACKET pkt, subpkt;
  95. int count = 0;
  96. if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))
  97. return -1;
  98. while (PACKET_remaining(&pkt) > 0) {
  99. /* Skip record type and version */
  100. if (!PACKET_forward(&pkt, 3))
  101. return -1;
  102. /* Read until next record */
  103. if (PACKET_get_length_prefixed_2(&pkt, &subpkt))
  104. return -1;
  105. count += 1;
  106. }
  107. return count;
  108. }
  109. #endif
  110. int tls1_change_cipher_state(SSL *s, int which)
  111. {
  112. unsigned char *p, *mac_secret;
  113. unsigned char *ms, *key, *iv;
  114. EVP_CIPHER_CTX *dd;
  115. const EVP_CIPHER *c;
  116. #ifndef OPENSSL_NO_COMP
  117. const SSL_COMP *comp;
  118. #endif
  119. const EVP_MD *m;
  120. int mac_type;
  121. size_t *mac_secret_size;
  122. EVP_MD_CTX *mac_ctx;
  123. EVP_PKEY *mac_key;
  124. size_t n, i, j, k, cl;
  125. int reuse_dd = 0;
  126. #ifndef OPENSSL_NO_KTLS
  127. struct tls12_crypto_info_aes_gcm_128 crypto_info;
  128. BIO *bio;
  129. unsigned char geniv[12];
  130. int count_unprocessed;
  131. int bit;
  132. #endif
  133. c = s->s3.tmp.new_sym_enc;
  134. m = s->s3.tmp.new_hash;
  135. mac_type = s->s3.tmp.new_mac_pkey_type;
  136. #ifndef OPENSSL_NO_COMP
  137. comp = s->s3.tmp.new_compression;
  138. #endif
  139. if (which & SSL3_CC_READ) {
  140. if (s->ext.use_etm)
  141. s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  142. else
  143. s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  144. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  145. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
  146. else
  147. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
  148. if (s->enc_read_ctx != NULL) {
  149. reuse_dd = 1;
  150. } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
  151. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  152. ERR_R_MALLOC_FAILURE);
  153. goto err;
  154. } else {
  155. /*
  156. * make sure it's initialised in case we exit later with an error
  157. */
  158. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  159. }
  160. dd = s->enc_read_ctx;
  161. mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
  162. if (mac_ctx == NULL) {
  163. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  164. ERR_R_INTERNAL_ERROR);
  165. goto err;
  166. }
  167. #ifndef OPENSSL_NO_COMP
  168. COMP_CTX_free(s->expand);
  169. s->expand = NULL;
  170. if (comp != NULL) {
  171. s->expand = COMP_CTX_new(comp->method);
  172. if (s->expand == NULL) {
  173. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  174. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  175. SSL_R_COMPRESSION_LIBRARY_ERROR);
  176. goto err;
  177. }
  178. }
  179. #endif
  180. /*
  181. * this is done by dtls1_reset_seq_numbers for DTLS
  182. */
  183. if (!SSL_IS_DTLS(s))
  184. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  185. mac_secret = &(s->s3.read_mac_secret[0]);
  186. mac_secret_size = &(s->s3.read_mac_secret_size);
  187. } else {
  188. s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
  189. if (s->ext.use_etm)
  190. s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  191. else
  192. s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  193. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  194. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
  195. else
  196. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
  197. if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
  198. reuse_dd = 1;
  199. } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
  200. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  201. ERR_R_MALLOC_FAILURE);
  202. goto err;
  203. }
  204. dd = s->enc_write_ctx;
  205. if (SSL_IS_DTLS(s)) {
  206. mac_ctx = EVP_MD_CTX_new();
  207. if (mac_ctx == NULL) {
  208. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  209. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  210. ERR_R_MALLOC_FAILURE);
  211. goto err;
  212. }
  213. s->write_hash = mac_ctx;
  214. } else {
  215. mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
  216. if (mac_ctx == NULL) {
  217. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  218. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  219. ERR_R_MALLOC_FAILURE);
  220. goto err;
  221. }
  222. }
  223. #ifndef OPENSSL_NO_COMP
  224. COMP_CTX_free(s->compress);
  225. s->compress = NULL;
  226. if (comp != NULL) {
  227. s->compress = COMP_CTX_new(comp->method);
  228. if (s->compress == NULL) {
  229. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  230. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  231. SSL_R_COMPRESSION_LIBRARY_ERROR);
  232. goto err;
  233. }
  234. }
  235. #endif
  236. /*
  237. * this is done by dtls1_reset_seq_numbers for DTLS
  238. */
  239. if (!SSL_IS_DTLS(s))
  240. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  241. mac_secret = &(s->s3.write_mac_secret[0]);
  242. mac_secret_size = &(s->s3.write_mac_secret_size);
  243. }
  244. if (reuse_dd)
  245. EVP_CIPHER_CTX_reset(dd);
  246. p = s->s3.tmp.key_block;
  247. i = *mac_secret_size = s->s3.tmp.new_mac_secret_size;
  248. /* TODO(size_t): convert me */
  249. cl = EVP_CIPHER_key_length(c);
  250. j = cl;
  251. /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
  252. /* If GCM/CCM mode only part of IV comes from PRF */
  253. if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
  254. k = EVP_GCM_TLS_FIXED_IV_LEN;
  255. else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
  256. k = EVP_CCM_TLS_FIXED_IV_LEN;
  257. else
  258. k = EVP_CIPHER_iv_length(c);
  259. if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  260. (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
  261. ms = &(p[0]);
  262. n = i + i;
  263. key = &(p[n]);
  264. n += j + j;
  265. iv = &(p[n]);
  266. n += k + k;
  267. } else {
  268. n = i;
  269. ms = &(p[n]);
  270. n += i + j;
  271. key = &(p[n]);
  272. n += j + k;
  273. iv = &(p[n]);
  274. n += k;
  275. }
  276. if (n > s->s3.tmp.key_block_length) {
  277. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  278. ERR_R_INTERNAL_ERROR);
  279. goto err;
  280. }
  281. memcpy(mac_secret, ms, i);
  282. if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  283. /* TODO(size_t): Convert this function */
  284. mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
  285. (int)*mac_secret_size);
  286. if (mac_key == NULL
  287. || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
  288. EVP_PKEY_free(mac_key);
  289. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  290. ERR_R_INTERNAL_ERROR);
  291. goto err;
  292. }
  293. EVP_PKEY_free(mac_key);
  294. }
  295. OSSL_TRACE_BEGIN(TLS) {
  296. BIO_printf(trc_out, "which = %04X, mac key:\n", which);
  297. BIO_dump_indent(trc_out, ms, i, 4);
  298. } OSSL_TRACE_END(TLS);
  299. if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
  300. if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
  301. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
  302. iv)) {
  303. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  304. ERR_R_INTERNAL_ERROR);
  305. goto err;
  306. }
  307. } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
  308. int taglen;
  309. if (s->s3.tmp.
  310. new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  311. taglen = EVP_CCM8_TLS_TAG_LEN;
  312. else
  313. taglen = EVP_CCM_TLS_TAG_LEN;
  314. if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
  315. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
  316. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
  317. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
  318. || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
  319. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  320. ERR_R_INTERNAL_ERROR);
  321. goto err;
  322. }
  323. } else {
  324. if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
  325. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  326. ERR_R_INTERNAL_ERROR);
  327. goto err;
  328. }
  329. }
  330. /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
  331. if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
  332. && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
  333. (int)*mac_secret_size, mac_secret)) {
  334. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  335. ERR_R_INTERNAL_ERROR);
  336. goto err;
  337. }
  338. #ifndef OPENSSL_NO_KTLS
  339. if (s->compress)
  340. goto skip_ktls;
  341. if (((which & SSL3_CC_READ) && (s->mode & SSL_MODE_NO_KTLS_RX))
  342. || ((which & SSL3_CC_WRITE) && (s->mode & SSL_MODE_NO_KTLS_TX)))
  343. goto skip_ktls;
  344. /* ktls supports only the maximum fragment size */
  345. if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
  346. goto skip_ktls;
  347. /* check that cipher is AES_GCM_128 */
  348. if (EVP_CIPHER_nid(c) != NID_aes_128_gcm
  349. || EVP_CIPHER_mode(c) != EVP_CIPH_GCM_MODE
  350. || EVP_CIPHER_key_length(c) != TLS_CIPHER_AES_GCM_128_KEY_SIZE)
  351. goto skip_ktls;
  352. /* check version is 1.2 */
  353. if (s->version != TLS1_2_VERSION)
  354. goto skip_ktls;
  355. if (which & SSL3_CC_WRITE)
  356. bio = s->wbio;
  357. else
  358. bio = s->rbio;
  359. if (!ossl_assert(bio != NULL)) {
  360. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  361. ERR_R_INTERNAL_ERROR);
  362. goto err;
  363. }
  364. /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
  365. if (which & SSL3_CC_WRITE) {
  366. if (BIO_flush(bio) <= 0)
  367. goto skip_ktls;
  368. }
  369. /* ktls doesn't support renegotiation */
  370. if ((BIO_get_ktls_send(s->wbio) && (which & SSL3_CC_WRITE)) ||
  371. (BIO_get_ktls_recv(s->rbio) && (which & SSL3_CC_READ))) {
  372. SSLfatal(s, SSL_AD_NO_RENEGOTIATION, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  373. ERR_R_INTERNAL_ERROR);
  374. goto err;
  375. }
  376. memset(&crypto_info, 0, sizeof(crypto_info));
  377. crypto_info.info.cipher_type = TLS_CIPHER_AES_GCM_128;
  378. crypto_info.info.version = s->version;
  379. EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GET_IV,
  380. EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN,
  381. geniv);
  382. memcpy(crypto_info.iv, geniv + EVP_GCM_TLS_FIXED_IV_LEN,
  383. TLS_CIPHER_AES_GCM_128_IV_SIZE);
  384. memcpy(crypto_info.salt, geniv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
  385. memcpy(crypto_info.key, key, EVP_CIPHER_key_length(c));
  386. if (which & SSL3_CC_WRITE)
  387. memcpy(crypto_info.rec_seq, &s->rlayer.write_sequence,
  388. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  389. else
  390. memcpy(crypto_info.rec_seq, &s->rlayer.read_sequence,
  391. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  392. if (which & SSL3_CC_READ) {
  393. count_unprocessed = count_unprocessed_records(s);
  394. if (count_unprocessed < 0)
  395. goto skip_ktls;
  396. /* increment the crypto_info record sequence */
  397. while (count_unprocessed) {
  398. for (bit = 7; bit >= 0; bit--) { /* increment */
  399. ++crypto_info.rec_seq[bit];
  400. if (crypto_info.rec_seq[bit] != 0)
  401. break;
  402. }
  403. count_unprocessed--;
  404. }
  405. }
  406. /* ktls works with user provided buffers directly */
  407. if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
  408. if (which & SSL3_CC_WRITE)
  409. ssl3_release_write_buffer(s);
  410. SSL_set_options(s, SSL_OP_NO_RENEGOTIATION);
  411. }
  412. skip_ktls:
  413. #endif /* OPENSSL_NO_KTLS */
  414. s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
  415. OSSL_TRACE_BEGIN(TLS) {
  416. BIO_printf(trc_out, "which = %04X, key:\n", which);
  417. BIO_dump_indent(trc_out, key, EVP_CIPHER_key_length(c), 4);
  418. BIO_printf(trc_out, "iv:\n");
  419. BIO_dump_indent(trc_out, iv, k, 4);
  420. } OSSL_TRACE_END(TLS);
  421. return 1;
  422. err:
  423. return 0;
  424. }
  425. int tls1_setup_key_block(SSL *s)
  426. {
  427. unsigned char *p;
  428. const EVP_CIPHER *c;
  429. const EVP_MD *hash;
  430. SSL_COMP *comp;
  431. int mac_type = NID_undef;
  432. size_t num, mac_secret_size = 0;
  433. int ret = 0;
  434. if (s->s3.tmp.key_block_length != 0)
  435. return 1;
  436. if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
  437. &comp, s->ext.use_etm)) {
  438. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
  439. SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  440. return 0;
  441. }
  442. s->s3.tmp.new_sym_enc = c;
  443. s->s3.tmp.new_hash = hash;
  444. s->s3.tmp.new_mac_pkey_type = mac_type;
  445. s->s3.tmp.new_mac_secret_size = mac_secret_size;
  446. num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
  447. num *= 2;
  448. ssl3_cleanup_key_block(s);
  449. if ((p = OPENSSL_malloc(num)) == NULL) {
  450. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
  451. ERR_R_MALLOC_FAILURE);
  452. goto err;
  453. }
  454. s->s3.tmp.key_block_length = num;
  455. s->s3.tmp.key_block = p;
  456. OSSL_TRACE_BEGIN(TLS) {
  457. BIO_printf(trc_out, "client random\n");
  458. BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
  459. BIO_printf(trc_out, "server random\n");
  460. BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
  461. BIO_printf(trc_out, "master key\n");
  462. BIO_dump_indent(trc_out,
  463. s->session->master_key,
  464. s->session->master_key_length, 4);
  465. } OSSL_TRACE_END(TLS);
  466. if (!tls1_generate_key_block(s, p, num)) {
  467. /* SSLfatal() already called */
  468. goto err;
  469. }
  470. OSSL_TRACE_BEGIN(TLS) {
  471. BIO_printf(trc_out, "key block\n");
  472. BIO_dump_indent(trc_out, p, num, 4);
  473. } OSSL_TRACE_END(TLS);
  474. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
  475. && s->method->version <= TLS1_VERSION) {
  476. /*
  477. * enable vulnerability countermeasure for CBC ciphers with known-IV
  478. * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  479. */
  480. s->s3.need_empty_fragments = 1;
  481. if (s->session->cipher != NULL) {
  482. if (s->session->cipher->algorithm_enc == SSL_eNULL)
  483. s->s3.need_empty_fragments = 0;
  484. #ifndef OPENSSL_NO_RC4
  485. if (s->session->cipher->algorithm_enc == SSL_RC4)
  486. s->s3.need_empty_fragments = 0;
  487. #endif
  488. }
  489. }
  490. ret = 1;
  491. err:
  492. return ret;
  493. }
  494. size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
  495. unsigned char *out)
  496. {
  497. size_t hashlen;
  498. unsigned char hash[EVP_MAX_MD_SIZE];
  499. if (!ssl3_digest_cached_records(s, 0)) {
  500. /* SSLfatal() already called */
  501. return 0;
  502. }
  503. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  504. /* SSLfatal() already called */
  505. return 0;
  506. }
  507. if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
  508. s->session->master_key, s->session->master_key_length,
  509. out, TLS1_FINISH_MAC_LENGTH, 1)) {
  510. /* SSLfatal() already called */
  511. return 0;
  512. }
  513. OPENSSL_cleanse(hash, hashlen);
  514. return TLS1_FINISH_MAC_LENGTH;
  515. }
  516. int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  517. size_t len, size_t *secret_size)
  518. {
  519. if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
  520. unsigned char hash[EVP_MAX_MD_SIZE * 2];
  521. size_t hashlen;
  522. /*
  523. * Digest cached records keeping record buffer (if present): this wont
  524. * affect client auth because we're freezing the buffer at the same
  525. * point (after client key exchange and before certificate verify)
  526. */
  527. if (!ssl3_digest_cached_records(s, 1)
  528. || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  529. /* SSLfatal() already called */
  530. return 0;
  531. }
  532. OSSL_TRACE_BEGIN(TLS) {
  533. BIO_printf(trc_out, "Handshake hashes:\n");
  534. BIO_dump(trc_out, (char *)hash, hashlen);
  535. } OSSL_TRACE_END(TLS);
  536. if (!tls1_PRF(s,
  537. TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  538. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
  539. hash, hashlen,
  540. NULL, 0,
  541. NULL, 0,
  542. NULL, 0, p, len, out,
  543. SSL3_MASTER_SECRET_SIZE, 1)) {
  544. /* SSLfatal() already called */
  545. return 0;
  546. }
  547. OPENSSL_cleanse(hash, hashlen);
  548. } else {
  549. if (!tls1_PRF(s,
  550. TLS_MD_MASTER_SECRET_CONST,
  551. TLS_MD_MASTER_SECRET_CONST_SIZE,
  552. s->s3.client_random, SSL3_RANDOM_SIZE,
  553. NULL, 0,
  554. s->s3.server_random, SSL3_RANDOM_SIZE,
  555. NULL, 0, p, len, out,
  556. SSL3_MASTER_SECRET_SIZE, 1)) {
  557. /* SSLfatal() already called */
  558. return 0;
  559. }
  560. }
  561. OSSL_TRACE_BEGIN(TLS) {
  562. BIO_printf(trc_out, "Premaster Secret:\n");
  563. BIO_dump_indent(trc_out, p, len, 4);
  564. BIO_printf(trc_out, "Client Random:\n");
  565. BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
  566. BIO_printf(trc_out, "Server Random:\n");
  567. BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
  568. BIO_printf(trc_out, "Master Secret:\n");
  569. BIO_dump_indent(trc_out,
  570. s->session->master_key,
  571. SSL3_MASTER_SECRET_SIZE, 4);
  572. } OSSL_TRACE_END(TLS);
  573. *secret_size = SSL3_MASTER_SECRET_SIZE;
  574. return 1;
  575. }
  576. int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  577. const char *label, size_t llen,
  578. const unsigned char *context,
  579. size_t contextlen, int use_context)
  580. {
  581. unsigned char *val = NULL;
  582. size_t vallen = 0, currentvalpos;
  583. int rv;
  584. /*
  585. * construct PRF arguments we construct the PRF argument ourself rather
  586. * than passing separate values into the TLS PRF to ensure that the
  587. * concatenation of values does not create a prohibited label.
  588. */
  589. vallen = llen + SSL3_RANDOM_SIZE * 2;
  590. if (use_context) {
  591. vallen += 2 + contextlen;
  592. }
  593. val = OPENSSL_malloc(vallen);
  594. if (val == NULL)
  595. goto err2;
  596. currentvalpos = 0;
  597. memcpy(val + currentvalpos, (unsigned char *)label, llen);
  598. currentvalpos += llen;
  599. memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);
  600. currentvalpos += SSL3_RANDOM_SIZE;
  601. memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);
  602. currentvalpos += SSL3_RANDOM_SIZE;
  603. if (use_context) {
  604. val[currentvalpos] = (contextlen >> 8) & 0xff;
  605. currentvalpos++;
  606. val[currentvalpos] = contextlen & 0xff;
  607. currentvalpos++;
  608. if ((contextlen > 0) || (context != NULL)) {
  609. memcpy(val + currentvalpos, context, contextlen);
  610. }
  611. }
  612. /*
  613. * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
  614. * label len) = 15, so size of val > max(prohibited label len) = 15 and
  615. * the comparisons won't have buffer overflow
  616. */
  617. if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
  618. TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
  619. goto err1;
  620. if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
  621. TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
  622. goto err1;
  623. if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
  624. TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
  625. goto err1;
  626. if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  627. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
  628. goto err1;
  629. if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
  630. TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
  631. goto err1;
  632. rv = tls1_PRF(s,
  633. val, vallen,
  634. NULL, 0,
  635. NULL, 0,
  636. NULL, 0,
  637. NULL, 0,
  638. s->session->master_key, s->session->master_key_length,
  639. out, olen, 0);
  640. goto ret;
  641. err1:
  642. SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
  643. rv = 0;
  644. goto ret;
  645. err2:
  646. SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
  647. rv = 0;
  648. ret:
  649. OPENSSL_clear_free(val, vallen);
  650. return rv;
  651. }
  652. int tls1_alert_code(int code)
  653. {
  654. switch (code) {
  655. case SSL_AD_CLOSE_NOTIFY:
  656. return SSL3_AD_CLOSE_NOTIFY;
  657. case SSL_AD_UNEXPECTED_MESSAGE:
  658. return SSL3_AD_UNEXPECTED_MESSAGE;
  659. case SSL_AD_BAD_RECORD_MAC:
  660. return SSL3_AD_BAD_RECORD_MAC;
  661. case SSL_AD_DECRYPTION_FAILED:
  662. return TLS1_AD_DECRYPTION_FAILED;
  663. case SSL_AD_RECORD_OVERFLOW:
  664. return TLS1_AD_RECORD_OVERFLOW;
  665. case SSL_AD_DECOMPRESSION_FAILURE:
  666. return SSL3_AD_DECOMPRESSION_FAILURE;
  667. case SSL_AD_HANDSHAKE_FAILURE:
  668. return SSL3_AD_HANDSHAKE_FAILURE;
  669. case SSL_AD_NO_CERTIFICATE:
  670. return -1;
  671. case SSL_AD_BAD_CERTIFICATE:
  672. return SSL3_AD_BAD_CERTIFICATE;
  673. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  674. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  675. case SSL_AD_CERTIFICATE_REVOKED:
  676. return SSL3_AD_CERTIFICATE_REVOKED;
  677. case SSL_AD_CERTIFICATE_EXPIRED:
  678. return SSL3_AD_CERTIFICATE_EXPIRED;
  679. case SSL_AD_CERTIFICATE_UNKNOWN:
  680. return SSL3_AD_CERTIFICATE_UNKNOWN;
  681. case SSL_AD_ILLEGAL_PARAMETER:
  682. return SSL3_AD_ILLEGAL_PARAMETER;
  683. case SSL_AD_UNKNOWN_CA:
  684. return TLS1_AD_UNKNOWN_CA;
  685. case SSL_AD_ACCESS_DENIED:
  686. return TLS1_AD_ACCESS_DENIED;
  687. case SSL_AD_DECODE_ERROR:
  688. return TLS1_AD_DECODE_ERROR;
  689. case SSL_AD_DECRYPT_ERROR:
  690. return TLS1_AD_DECRYPT_ERROR;
  691. case SSL_AD_EXPORT_RESTRICTION:
  692. return TLS1_AD_EXPORT_RESTRICTION;
  693. case SSL_AD_PROTOCOL_VERSION:
  694. return TLS1_AD_PROTOCOL_VERSION;
  695. case SSL_AD_INSUFFICIENT_SECURITY:
  696. return TLS1_AD_INSUFFICIENT_SECURITY;
  697. case SSL_AD_INTERNAL_ERROR:
  698. return TLS1_AD_INTERNAL_ERROR;
  699. case SSL_AD_USER_CANCELLED:
  700. return TLS1_AD_USER_CANCELLED;
  701. case SSL_AD_NO_RENEGOTIATION:
  702. return TLS1_AD_NO_RENEGOTIATION;
  703. case SSL_AD_UNSUPPORTED_EXTENSION:
  704. return TLS1_AD_UNSUPPORTED_EXTENSION;
  705. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  706. return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
  707. case SSL_AD_UNRECOGNIZED_NAME:
  708. return TLS1_AD_UNRECOGNIZED_NAME;
  709. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  710. return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  711. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  712. return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
  713. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  714. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  715. case SSL_AD_INAPPROPRIATE_FALLBACK:
  716. return TLS1_AD_INAPPROPRIATE_FALLBACK;
  717. case SSL_AD_NO_APPLICATION_PROTOCOL:
  718. return TLS1_AD_NO_APPLICATION_PROTOCOL;
  719. case SSL_AD_CERTIFICATE_REQUIRED:
  720. return SSL_AD_HANDSHAKE_FAILURE;
  721. default:
  722. return -1;
  723. }
  724. }