t1_enc.c 27 KB

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