t1_enc.c 28 KB

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