t1_enc.c 28 KB

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