tls13_enc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include "ssl_locl.h"
  11. #include "internal/cryptlib.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/kdf.h>
  14. #define TLS13_MAX_LABEL_LEN 246
  15. /* Always filled with zeros */
  16. static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
  17. /*
  18. * Given a |secret|; a |label| of length |labellen|; and |data| of length
  19. * |datalen| (e.g. typically a hash of the handshake messages), derive a new
  20. * secret |outlen| bytes long and store it in the location pointed to be |out|.
  21. * The |data| value may be zero length. Returns 1 on success 0 on failure.
  22. */
  23. int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
  24. const unsigned char *label, size_t labellen,
  25. const unsigned char *data, size_t datalen,
  26. unsigned char *out, size_t outlen)
  27. {
  28. const unsigned char label_prefix[] = "tls13 ";
  29. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  30. int ret;
  31. size_t hkdflabellen;
  32. size_t hashlen;
  33. /*
  34. * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
  35. * prefix and label + bytes for the label itself + bytes for the hash
  36. */
  37. unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
  38. + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
  39. + EVP_MAX_MD_SIZE];
  40. WPACKET pkt;
  41. if (pctx == NULL)
  42. return 0;
  43. hashlen = EVP_MD_size(md);
  44. if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
  45. || !WPACKET_put_bytes_u16(&pkt, outlen)
  46. || !WPACKET_start_sub_packet_u8(&pkt)
  47. || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
  48. || !WPACKET_memcpy(&pkt, label, labellen)
  49. || !WPACKET_close(&pkt)
  50. || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
  51. || !WPACKET_get_total_written(&pkt, &hkdflabellen)
  52. || !WPACKET_finish(&pkt)) {
  53. EVP_PKEY_CTX_free(pctx);
  54. WPACKET_cleanup(&pkt);
  55. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
  56. ERR_R_INTERNAL_ERROR);
  57. return 0;
  58. }
  59. ret = EVP_PKEY_derive_init(pctx) <= 0
  60. || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
  61. <= 0
  62. || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
  63. || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
  64. || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
  65. || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
  66. EVP_PKEY_CTX_free(pctx);
  67. if (ret != 0)
  68. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
  69. ERR_R_INTERNAL_ERROR);
  70. return ret == 0;
  71. }
  72. /*
  73. * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
  74. * success 0 on failure.
  75. */
  76. int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
  77. unsigned char *key, size_t keylen)
  78. {
  79. static const unsigned char keylabel[] = "key";
  80. return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
  81. NULL, 0, key, keylen);
  82. }
  83. /*
  84. * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
  85. * success 0 on failure.
  86. */
  87. int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
  88. unsigned char *iv, size_t ivlen)
  89. {
  90. static const unsigned char ivlabel[] = "iv";
  91. return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
  92. NULL, 0, iv, ivlen);
  93. }
  94. int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
  95. const unsigned char *secret,
  96. unsigned char *fin, size_t finlen)
  97. {
  98. static const unsigned char finishedlabel[] = "finished";
  99. return tls13_hkdf_expand(s, md, secret, finishedlabel,
  100. sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
  101. }
  102. /*
  103. * Given the previous secret |prevsecret| and a new input secret |insecret| of
  104. * length |insecretlen|, generate a new secret and store it in the location
  105. * pointed to by |outsecret|. Returns 1 on success 0 on failure.
  106. */
  107. int tls13_generate_secret(SSL *s, const EVP_MD *md,
  108. const unsigned char *prevsecret,
  109. const unsigned char *insecret,
  110. size_t insecretlen,
  111. unsigned char *outsecret)
  112. {
  113. size_t mdlen, prevsecretlen;
  114. int mdleni;
  115. int ret;
  116. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  117. static const char derived_secret_label[] = "derived";
  118. unsigned char preextractsec[EVP_MAX_MD_SIZE];
  119. if (pctx == NULL) {
  120. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
  121. ERR_R_INTERNAL_ERROR);
  122. return 0;
  123. }
  124. mdleni = EVP_MD_size(md);
  125. /* Ensure cast to size_t is safe */
  126. if (!ossl_assert(mdleni >= 0)) {
  127. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
  128. ERR_R_INTERNAL_ERROR);
  129. return 0;
  130. }
  131. mdlen = (size_t)mdleni;
  132. if (insecret == NULL) {
  133. insecret = default_zeros;
  134. insecretlen = mdlen;
  135. }
  136. if (prevsecret == NULL) {
  137. prevsecret = default_zeros;
  138. prevsecretlen = 0;
  139. } else {
  140. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  141. unsigned char hash[EVP_MAX_MD_SIZE];
  142. /* The pre-extract derive step uses a hash of no messages */
  143. if (mctx == NULL
  144. || EVP_DigestInit_ex(mctx, md, NULL) <= 0
  145. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  146. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
  147. ERR_R_INTERNAL_ERROR);
  148. EVP_MD_CTX_free(mctx);
  149. EVP_PKEY_CTX_free(pctx);
  150. return 0;
  151. }
  152. EVP_MD_CTX_free(mctx);
  153. /* Generate the pre-extract secret */
  154. if (!tls13_hkdf_expand(s, md, prevsecret,
  155. (unsigned char *)derived_secret_label,
  156. sizeof(derived_secret_label) - 1, hash, mdlen,
  157. preextractsec, mdlen)) {
  158. /* SSLfatal() already called */
  159. EVP_PKEY_CTX_free(pctx);
  160. return 0;
  161. }
  162. prevsecret = preextractsec;
  163. prevsecretlen = mdlen;
  164. }
  165. ret = EVP_PKEY_derive_init(pctx) <= 0
  166. || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
  167. <= 0
  168. || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
  169. || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
  170. || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
  171. <= 0
  172. || EVP_PKEY_derive(pctx, outsecret, &mdlen)
  173. <= 0;
  174. if (ret != 0)
  175. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
  176. ERR_R_INTERNAL_ERROR);
  177. EVP_PKEY_CTX_free(pctx);
  178. if (prevsecret == preextractsec)
  179. OPENSSL_cleanse(preextractsec, mdlen);
  180. return ret == 0;
  181. }
  182. /*
  183. * Given an input secret |insecret| of length |insecretlen| generate the
  184. * handshake secret. This requires the early secret to already have been
  185. * generated. Returns 1 on success 0 on failure.
  186. */
  187. int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
  188. size_t insecretlen)
  189. {
  190. /* Calls SSLfatal() if required */
  191. return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
  192. insecret, insecretlen,
  193. (unsigned char *)&s->handshake_secret);
  194. }
  195. /*
  196. * Given the handshake secret |prev| of length |prevlen| generate the master
  197. * secret and store its length in |*secret_size|. Returns 1 on success 0 on
  198. * failure.
  199. */
  200. int tls13_generate_master_secret(SSL *s, unsigned char *out,
  201. unsigned char *prev, size_t prevlen,
  202. size_t *secret_size)
  203. {
  204. const EVP_MD *md = ssl_handshake_md(s);
  205. *secret_size = EVP_MD_size(md);
  206. /* Calls SSLfatal() if required */
  207. return tls13_generate_secret(s, md, prev, NULL, 0, out);
  208. }
  209. /*
  210. * Generates the mac for the Finished message. Returns the length of the MAC or
  211. * 0 on error.
  212. */
  213. size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
  214. unsigned char *out)
  215. {
  216. const EVP_MD *md = ssl_handshake_md(s);
  217. unsigned char hash[EVP_MAX_MD_SIZE];
  218. size_t hashlen, ret = 0;
  219. EVP_PKEY *key = NULL;
  220. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  221. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  222. /* SSLfatal() already called */
  223. goto err;
  224. }
  225. if (str == s->method->ssl3_enc->server_finished_label) {
  226. key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  227. s->server_finished_secret, hashlen);
  228. } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
  229. key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  230. s->client_finished_secret, hashlen);
  231. } else {
  232. unsigned char finsecret[EVP_MAX_MD_SIZE];
  233. if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
  234. s->client_app_traffic_secret,
  235. finsecret, hashlen))
  236. goto err;
  237. key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
  238. hashlen);
  239. }
  240. if (key == NULL
  241. || ctx == NULL
  242. || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
  243. || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
  244. || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
  245. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
  246. ERR_R_INTERNAL_ERROR);
  247. goto err;
  248. }
  249. ret = hashlen;
  250. err:
  251. EVP_PKEY_free(key);
  252. EVP_MD_CTX_free(ctx);
  253. return ret;
  254. }
  255. /*
  256. * There isn't really a key block in TLSv1.3, but we still need this function
  257. * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
  258. */
  259. int tls13_setup_key_block(SSL *s)
  260. {
  261. const EVP_CIPHER *c;
  262. const EVP_MD *hash;
  263. int mac_type = NID_undef;
  264. s->session->cipher = s->s3->tmp.new_cipher;
  265. if (!ssl_cipher_get_evp
  266. (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
  267. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
  268. SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  269. return 0;
  270. }
  271. s->s3->tmp.new_sym_enc = c;
  272. s->s3->tmp.new_hash = hash;
  273. return 1;
  274. }
  275. static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
  276. const EVP_CIPHER *ciph,
  277. const unsigned char *insecret,
  278. const unsigned char *hash,
  279. const unsigned char *label,
  280. size_t labellen, unsigned char *secret,
  281. unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
  282. {
  283. unsigned char key[EVP_MAX_KEY_LENGTH];
  284. size_t ivlen, keylen, taglen;
  285. int hashleni = EVP_MD_size(md);
  286. size_t hashlen;
  287. /* Ensure cast to size_t is safe */
  288. if (!ossl_assert(hashleni >= 0)) {
  289. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
  290. ERR_R_EVP_LIB);
  291. goto err;
  292. }
  293. hashlen = (size_t)hashleni;
  294. if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
  295. secret, hashlen)) {
  296. /* SSLfatal() already called */
  297. goto err;
  298. }
  299. /* TODO(size_t): convert me */
  300. keylen = EVP_CIPHER_key_length(ciph);
  301. if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
  302. uint32_t algenc;
  303. ivlen = EVP_CCM_TLS_IV_LEN;
  304. if (s->s3->tmp.new_cipher == NULL) {
  305. /* We've not selected a cipher yet - we must be doing early data */
  306. algenc = s->session->cipher->algorithm_enc;
  307. } else {
  308. algenc = s->s3->tmp.new_cipher->algorithm_enc;
  309. }
  310. if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  311. taglen = EVP_CCM8_TLS_TAG_LEN;
  312. else
  313. taglen = EVP_CCM_TLS_TAG_LEN;
  314. } else {
  315. ivlen = EVP_CIPHER_iv_length(ciph);
  316. taglen = 0;
  317. }
  318. if (!tls13_derive_key(s, md, secret, key, keylen)
  319. || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
  320. /* SSLfatal() already called */
  321. goto err;
  322. }
  323. if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
  324. || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
  325. || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
  326. taglen, NULL))
  327. || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
  328. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
  329. ERR_R_EVP_LIB);
  330. goto err;
  331. }
  332. return 1;
  333. err:
  334. OPENSSL_cleanse(key, sizeof(key));
  335. return 0;
  336. }
  337. int tls13_change_cipher_state(SSL *s, int which)
  338. {
  339. static const unsigned char client_early_traffic[] = "c e traffic";
  340. static const unsigned char client_handshake_traffic[] = "c hs traffic";
  341. static const unsigned char client_application_traffic[] = "c ap traffic";
  342. static const unsigned char server_handshake_traffic[] = "s hs traffic";
  343. static const unsigned char server_application_traffic[] = "s ap traffic";
  344. static const unsigned char exporter_master_secret[] = "exp master";
  345. static const unsigned char resumption_master_secret[] = "res master";
  346. static const unsigned char early_exporter_master_secret[] = "e exp master";
  347. unsigned char *iv;
  348. unsigned char secret[EVP_MAX_MD_SIZE];
  349. unsigned char hashval[EVP_MAX_MD_SIZE];
  350. unsigned char *hash = hashval;
  351. unsigned char *insecret;
  352. unsigned char *finsecret = NULL;
  353. const char *log_label = NULL;
  354. EVP_CIPHER_CTX *ciph_ctx;
  355. size_t finsecretlen = 0;
  356. const unsigned char *label;
  357. size_t labellen, hashlen = 0;
  358. int ret = 0;
  359. const EVP_MD *md = NULL;
  360. const EVP_CIPHER *cipher = NULL;
  361. if (which & SSL3_CC_READ) {
  362. if (s->enc_read_ctx != NULL) {
  363. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  364. } else {
  365. s->enc_read_ctx = EVP_CIPHER_CTX_new();
  366. if (s->enc_read_ctx == NULL) {
  367. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  368. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  369. goto err;
  370. }
  371. }
  372. ciph_ctx = s->enc_read_ctx;
  373. iv = s->read_iv;
  374. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  375. } else {
  376. s->statem.invalid_enc_write_ctx = 1;
  377. if (s->enc_write_ctx != NULL) {
  378. EVP_CIPHER_CTX_reset(s->enc_write_ctx);
  379. } else {
  380. s->enc_write_ctx = EVP_CIPHER_CTX_new();
  381. if (s->enc_write_ctx == NULL) {
  382. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  383. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  384. goto err;
  385. }
  386. }
  387. ciph_ctx = s->enc_write_ctx;
  388. iv = s->write_iv;
  389. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  390. }
  391. if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
  392. || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
  393. if (which & SSL3_CC_EARLY) {
  394. EVP_MD_CTX *mdctx = NULL;
  395. long handlen;
  396. void *hdata;
  397. unsigned int hashlenui;
  398. const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
  399. insecret = s->early_secret;
  400. label = client_early_traffic;
  401. labellen = sizeof(client_early_traffic) - 1;
  402. log_label = CLIENT_EARLY_LABEL;
  403. handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  404. if (handlen <= 0) {
  405. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  406. SSL_F_TLS13_CHANGE_CIPHER_STATE,
  407. SSL_R_BAD_HANDSHAKE_LENGTH);
  408. goto err;
  409. }
  410. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  411. && s->max_early_data > 0
  412. && s->session->ext.max_early_data == 0) {
  413. /*
  414. * If we are attempting to send early data, and we've decided to
  415. * actually do it but max_early_data in s->session is 0 then we
  416. * must be using an external PSK.
  417. */
  418. if (!ossl_assert(s->psksession != NULL
  419. && s->max_early_data ==
  420. s->psksession->ext.max_early_data)) {
  421. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  422. SSL_F_TLS13_CHANGE_CIPHER_STATE,
  423. ERR_R_INTERNAL_ERROR);
  424. goto err;
  425. }
  426. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  427. }
  428. if (sslcipher == NULL) {
  429. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  430. SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
  431. goto err;
  432. }
  433. /*
  434. * We need to calculate the handshake digest using the digest from
  435. * the session. We haven't yet selected our ciphersuite so we can't
  436. * use ssl_handshake_md().
  437. */
  438. mdctx = EVP_MD_CTX_new();
  439. if (mdctx == NULL) {
  440. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  441. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  442. goto err;
  443. }
  444. cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
  445. md = ssl_md(sslcipher->algorithm2);
  446. if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
  447. || !EVP_DigestUpdate(mdctx, hdata, handlen)
  448. || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
  449. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  450. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  451. EVP_MD_CTX_free(mdctx);
  452. goto err;
  453. }
  454. hashlen = hashlenui;
  455. EVP_MD_CTX_free(mdctx);
  456. if (!tls13_hkdf_expand(s, md, insecret,
  457. early_exporter_master_secret,
  458. sizeof(early_exporter_master_secret) - 1,
  459. hashval, hashlen,
  460. s->early_exporter_master_secret, hashlen)) {
  461. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  462. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  463. goto err;
  464. }
  465. if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
  466. s->early_exporter_master_secret, hashlen)) {
  467. /* SSLfatal() already called */
  468. goto err;
  469. }
  470. } else if (which & SSL3_CC_HANDSHAKE) {
  471. insecret = s->handshake_secret;
  472. finsecret = s->client_finished_secret;
  473. finsecretlen = EVP_MD_size(ssl_handshake_md(s));
  474. label = client_handshake_traffic;
  475. labellen = sizeof(client_handshake_traffic) - 1;
  476. log_label = CLIENT_HANDSHAKE_LABEL;
  477. /*
  478. * The handshake hash used for the server read/client write handshake
  479. * traffic secret is the same as the hash for the server
  480. * write/client read handshake traffic secret. However, if we
  481. * processed early data then we delay changing the server
  482. * read/client write cipher state until later, and the handshake
  483. * hashes have moved on. Therefore we use the value saved earlier
  484. * when we did the server write/client read change cipher state.
  485. */
  486. hash = s->handshake_traffic_hash;
  487. } else {
  488. insecret = s->master_secret;
  489. label = client_application_traffic;
  490. labellen = sizeof(client_application_traffic) - 1;
  491. log_label = CLIENT_APPLICATION_LABEL;
  492. /*
  493. * For this we only use the handshake hashes up until the server
  494. * Finished hash. We do not include the client's Finished, which is
  495. * what ssl_handshake_hash() would give us. Instead we use the
  496. * previously saved value.
  497. */
  498. hash = s->server_finished_hash;
  499. }
  500. } else {
  501. /* Early data never applies to client-read/server-write */
  502. if (which & SSL3_CC_HANDSHAKE) {
  503. insecret = s->handshake_secret;
  504. finsecret = s->server_finished_secret;
  505. finsecretlen = EVP_MD_size(ssl_handshake_md(s));
  506. label = server_handshake_traffic;
  507. labellen = sizeof(server_handshake_traffic) - 1;
  508. log_label = SERVER_HANDSHAKE_LABEL;
  509. } else {
  510. insecret = s->master_secret;
  511. label = server_application_traffic;
  512. labellen = sizeof(server_application_traffic) - 1;
  513. log_label = SERVER_APPLICATION_LABEL;
  514. }
  515. }
  516. if (!(which & SSL3_CC_EARLY)) {
  517. md = ssl_handshake_md(s);
  518. cipher = s->s3->tmp.new_sym_enc;
  519. if (!ssl3_digest_cached_records(s, 1)
  520. || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
  521. /* SSLfatal() already called */;
  522. goto err;
  523. }
  524. }
  525. /*
  526. * Save the hash of handshakes up to now for use when we calculate the
  527. * client application traffic secret
  528. */
  529. if (label == server_application_traffic)
  530. memcpy(s->server_finished_hash, hashval, hashlen);
  531. if (label == server_handshake_traffic)
  532. memcpy(s->handshake_traffic_hash, hashval, hashlen);
  533. if (label == client_application_traffic) {
  534. /*
  535. * We also create the resumption master secret, but this time use the
  536. * hash for the whole handshake including the Client Finished
  537. */
  538. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  539. resumption_master_secret,
  540. sizeof(resumption_master_secret) - 1,
  541. hashval, hashlen, s->resumption_master_secret,
  542. hashlen)) {
  543. /* SSLfatal() already called */
  544. goto err;
  545. }
  546. }
  547. if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
  548. insecret, hash, label, labellen, secret, iv,
  549. ciph_ctx)) {
  550. /* SSLfatal() already called */
  551. goto err;
  552. }
  553. if (label == server_application_traffic) {
  554. memcpy(s->server_app_traffic_secret, secret, hashlen);
  555. /* Now we create the exporter master secret */
  556. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  557. exporter_master_secret,
  558. sizeof(exporter_master_secret) - 1,
  559. hash, hashlen, s->exporter_master_secret,
  560. hashlen)) {
  561. /* SSLfatal() already called */
  562. goto err;
  563. }
  564. if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
  565. hashlen)) {
  566. /* SSLfatal() already called */
  567. goto err;
  568. }
  569. } else if (label == client_application_traffic)
  570. memcpy(s->client_app_traffic_secret, secret, hashlen);
  571. if (!ssl_log_secret(s, log_label, secret, hashlen)) {
  572. /* SSLfatal() already called */
  573. goto err;
  574. }
  575. if (finsecret != NULL
  576. && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
  577. finsecret, finsecretlen)) {
  578. /* SSLfatal() already called */
  579. goto err;
  580. }
  581. s->statem.invalid_enc_write_ctx = 0;
  582. ret = 1;
  583. err:
  584. OPENSSL_cleanse(secret, sizeof(secret));
  585. return ret;
  586. }
  587. int tls13_update_key(SSL *s, int sending)
  588. {
  589. static const unsigned char application_traffic[] = "traffic upd";
  590. const EVP_MD *md = ssl_handshake_md(s);
  591. size_t hashlen = EVP_MD_size(md);
  592. unsigned char *insecret, *iv;
  593. unsigned char secret[EVP_MAX_MD_SIZE];
  594. EVP_CIPHER_CTX *ciph_ctx;
  595. int ret = 0;
  596. if (s->server == sending)
  597. insecret = s->server_app_traffic_secret;
  598. else
  599. insecret = s->client_app_traffic_secret;
  600. if (sending) {
  601. s->statem.invalid_enc_write_ctx = 1;
  602. iv = s->write_iv;
  603. ciph_ctx = s->enc_write_ctx;
  604. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  605. } else {
  606. iv = s->read_iv;
  607. ciph_ctx = s->enc_read_ctx;
  608. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  609. }
  610. if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
  611. s->s3->tmp.new_sym_enc, insecret, NULL,
  612. application_traffic,
  613. sizeof(application_traffic) - 1, secret, iv,
  614. ciph_ctx)) {
  615. /* SSLfatal() already called */
  616. goto err;
  617. }
  618. memcpy(insecret, secret, hashlen);
  619. s->statem.invalid_enc_write_ctx = 0;
  620. ret = 1;
  621. err:
  622. OPENSSL_cleanse(secret, sizeof(secret));
  623. return ret;
  624. }
  625. int tls13_alert_code(int code)
  626. {
  627. if (code == SSL_AD_MISSING_EXTENSION)
  628. return code;
  629. return tls1_alert_code(code);
  630. }
  631. int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  632. const char *label, size_t llen,
  633. const unsigned char *context,
  634. size_t contextlen, int use_context)
  635. {
  636. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  637. static const unsigned char exporterlabel[] = "exporter";
  638. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  639. const EVP_MD *md = ssl_handshake_md(s);
  640. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  641. unsigned int hashsize, datalen;
  642. int ret = 0;
  643. if (ctx == NULL || !ossl_statem_export_allowed(s))
  644. goto err;
  645. if (!use_context)
  646. contextlen = 0;
  647. if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  648. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  649. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  650. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  651. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  652. || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
  653. (const unsigned char *)label, llen,
  654. data, datalen, exportsecret, hashsize)
  655. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  656. sizeof(exporterlabel) - 1, hash, hashsize,
  657. out, olen))
  658. goto err;
  659. ret = 1;
  660. err:
  661. EVP_MD_CTX_free(ctx);
  662. return ret;
  663. }
  664. int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  665. const char *label, size_t llen,
  666. const unsigned char *context,
  667. size_t contextlen)
  668. {
  669. static const unsigned char exporterlabel[] = "exporter";
  670. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  671. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  672. const EVP_MD *md;
  673. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  674. unsigned int hashsize, datalen;
  675. int ret = 0;
  676. const SSL_CIPHER *sslcipher;
  677. if (ctx == NULL || !ossl_statem_export_early_allowed(s))
  678. goto err;
  679. if (!s->server && s->max_early_data > 0
  680. && s->session->ext.max_early_data == 0)
  681. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  682. else
  683. sslcipher = SSL_SESSION_get0_cipher(s->session);
  684. md = ssl_md(sslcipher->algorithm2);
  685. /*
  686. * Calculate the hash value and store it in |data|. The reason why
  687. * the empty string is used is that the definition of TLS-Exporter
  688. * is like so:
  689. *
  690. * TLS-Exporter(label, context_value, key_length) =
  691. * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
  692. * "exporter", Hash(context_value), key_length)
  693. *
  694. * Derive-Secret(Secret, Label, Messages) =
  695. * HKDF-Expand-Label(Secret, Label,
  696. * Transcript-Hash(Messages), Hash.length)
  697. *
  698. * Here Transcript-Hash is the cipher suite hash algorithm.
  699. */
  700. if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  701. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  702. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  703. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  704. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  705. || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
  706. (const unsigned char *)label, llen,
  707. data, datalen, exportsecret, hashsize)
  708. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  709. sizeof(exporterlabel) - 1, hash, hashsize,
  710. out, olen))
  711. goto err;
  712. ret = 1;
  713. err:
  714. EVP_MD_CTX_free(ctx);
  715. return ret;
  716. }