t1_enc.c 27 KB

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