tls13_enc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright 2016 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 <openssl/evp.h>
  12. #include <openssl/kdf.h>
  13. #define TLS13_MAX_LABEL_LEN 246
  14. /* Always filled with zeros */
  15. static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
  16. /*
  17. * Given a |secret|; a |label| of length |labellen|; and |data| of length
  18. * |datalen| (e.g. typically a hash of the handshake messages), derive a new
  19. * secret |outlen| bytes long and store it in the location pointed to be |out|.
  20. * The |data| value may be zero length. Returns 1 on success 0 on failure.
  21. */
  22. int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
  23. const unsigned char *label, size_t labellen,
  24. const unsigned char *data, size_t datalen,
  25. unsigned char *out, size_t outlen)
  26. {
  27. const unsigned char label_prefix[] = "tls13 ";
  28. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  29. int ret;
  30. size_t hkdflabellen;
  31. size_t hashlen;
  32. /*
  33. * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
  34. * prefix and label + bytes for the label itself + bytes for the hash
  35. */
  36. unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
  37. + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
  38. + EVP_MAX_MD_SIZE];
  39. WPACKET pkt;
  40. if (pctx == NULL)
  41. return 0;
  42. hashlen = EVP_MD_size(md);
  43. if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
  44. || !WPACKET_put_bytes_u16(&pkt, outlen)
  45. || !WPACKET_start_sub_packet_u8(&pkt)
  46. || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
  47. || !WPACKET_memcpy(&pkt, label, labellen)
  48. || !WPACKET_close(&pkt)
  49. || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
  50. || !WPACKET_get_total_written(&pkt, &hkdflabellen)
  51. || !WPACKET_finish(&pkt)) {
  52. EVP_PKEY_CTX_free(pctx);
  53. WPACKET_cleanup(&pkt);
  54. return 0;
  55. }
  56. ret = EVP_PKEY_derive_init(pctx) <= 0
  57. || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
  58. <= 0
  59. || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
  60. || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
  61. || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
  62. || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
  63. EVP_PKEY_CTX_free(pctx);
  64. return ret == 0;
  65. }
  66. /*
  67. * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
  68. * success 0 on failure.
  69. */
  70. int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
  71. unsigned char *key, size_t keylen)
  72. {
  73. static const unsigned char keylabel[] = "key";
  74. return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
  75. NULL, 0, key, keylen);
  76. }
  77. /*
  78. * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
  79. * success 0 on failure.
  80. */
  81. int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
  82. unsigned char *iv, size_t ivlen)
  83. {
  84. static const unsigned char ivlabel[] = "iv";
  85. return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
  86. NULL, 0, iv, ivlen);
  87. }
  88. int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
  89. const unsigned char *secret,
  90. unsigned char *fin, size_t finlen)
  91. {
  92. static const unsigned char finishedlabel[] = "finished";
  93. return tls13_hkdf_expand(s, md, secret, finishedlabel,
  94. sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
  95. }
  96. /*
  97. * Given the previous secret |prevsecret| and a new input secret |insecret| of
  98. * length |insecretlen|, generate a new secret and store it in the location
  99. * pointed to by |outsecret|. Returns 1 on success 0 on failure.
  100. */
  101. int tls13_generate_secret(SSL *s, const EVP_MD *md,
  102. const unsigned char *prevsecret,
  103. const unsigned char *insecret,
  104. size_t insecretlen,
  105. unsigned char *outsecret)
  106. {
  107. size_t mdlen, prevsecretlen;
  108. int ret;
  109. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  110. static const char derived_secret_label[] = "derived";
  111. unsigned char preextractsec[EVP_MAX_MD_SIZE];
  112. if (pctx == NULL)
  113. return 0;
  114. mdlen = EVP_MD_size(md);
  115. if (insecret == NULL) {
  116. insecret = default_zeros;
  117. insecretlen = mdlen;
  118. }
  119. if (prevsecret == NULL) {
  120. prevsecret = default_zeros;
  121. prevsecretlen = 0;
  122. } else {
  123. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  124. unsigned char hash[EVP_MAX_MD_SIZE];
  125. /* The pre-extract derive step uses a hash of no messages */
  126. if (mctx == NULL
  127. || EVP_DigestInit_ex(mctx, md, NULL) <= 0
  128. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  129. EVP_MD_CTX_free(mctx);
  130. EVP_PKEY_CTX_free(pctx);
  131. return 0;
  132. }
  133. EVP_MD_CTX_free(mctx);
  134. /* Generate the pre-extract secret */
  135. if (!tls13_hkdf_expand(s, md, prevsecret,
  136. (unsigned char *)derived_secret_label,
  137. sizeof(derived_secret_label) - 1, hash, mdlen,
  138. preextractsec, mdlen)) {
  139. EVP_PKEY_CTX_free(pctx);
  140. return 0;
  141. }
  142. prevsecret = preextractsec;
  143. prevsecretlen = mdlen;
  144. }
  145. ret = EVP_PKEY_derive_init(pctx) <= 0
  146. || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
  147. <= 0
  148. || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
  149. || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
  150. || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
  151. <= 0
  152. || EVP_PKEY_derive(pctx, outsecret, &mdlen)
  153. <= 0;
  154. EVP_PKEY_CTX_free(pctx);
  155. if (prevsecret == preextractsec)
  156. OPENSSL_cleanse(preextractsec, mdlen);
  157. return ret == 0;
  158. }
  159. /*
  160. * Given an input secret |insecret| of length |insecretlen| generate the
  161. * handshake secret. This requires the early secret to already have been
  162. * generated. Returns 1 on success 0 on failure.
  163. */
  164. int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
  165. size_t insecretlen)
  166. {
  167. return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
  168. insecret, insecretlen,
  169. (unsigned char *)&s->handshake_secret);
  170. }
  171. /*
  172. * Given the handshake secret |prev| of length |prevlen| generate the master
  173. * secret and store its length in |*secret_size|. Returns 1 on success 0 on
  174. * failure.
  175. */
  176. int tls13_generate_master_secret(SSL *s, unsigned char *out,
  177. unsigned char *prev, size_t prevlen,
  178. size_t *secret_size)
  179. {
  180. const EVP_MD *md = ssl_handshake_md(s);
  181. *secret_size = EVP_MD_size(md);
  182. return tls13_generate_secret(s, md, prev, NULL, 0, out);
  183. }
  184. /*
  185. * Generates the mac for the Finished message. Returns the length of the MAC or
  186. * 0 on error.
  187. */
  188. size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
  189. unsigned char *out)
  190. {
  191. const EVP_MD *md = ssl_handshake_md(s);
  192. unsigned char hash[EVP_MAX_MD_SIZE];
  193. size_t hashlen, ret = 0;
  194. EVP_PKEY *key = NULL;
  195. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  196. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
  197. goto err;
  198. if (str == s->method->ssl3_enc->server_finished_label)
  199. key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  200. s->server_finished_secret, hashlen);
  201. else
  202. key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  203. s->client_finished_secret, hashlen);
  204. if (key == NULL
  205. || ctx == NULL
  206. || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
  207. || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
  208. || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0)
  209. goto err;
  210. ret = hashlen;
  211. err:
  212. EVP_PKEY_free(key);
  213. EVP_MD_CTX_free(ctx);
  214. return ret;
  215. }
  216. /*
  217. * There isn't really a key block in TLSv1.3, but we still need this function
  218. * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
  219. */
  220. int tls13_setup_key_block(SSL *s)
  221. {
  222. const EVP_CIPHER *c;
  223. const EVP_MD *hash;
  224. int mac_type = NID_undef;
  225. s->session->cipher = s->s3->tmp.new_cipher;
  226. if (!ssl_cipher_get_evp
  227. (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
  228. SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  229. return 0;
  230. }
  231. s->s3->tmp.new_sym_enc = c;
  232. s->s3->tmp.new_hash = hash;
  233. return 1;
  234. }
  235. static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
  236. const EVP_CIPHER *ciph,
  237. const unsigned char *insecret,
  238. const unsigned char *hash,
  239. const unsigned char *label,
  240. size_t labellen, unsigned char *secret,
  241. unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
  242. {
  243. unsigned char key[EVP_MAX_KEY_LENGTH];
  244. size_t ivlen, keylen, taglen;
  245. size_t hashlen = EVP_MD_size(md);
  246. if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
  247. secret, hashlen)) {
  248. SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
  249. goto err;
  250. }
  251. /* TODO(size_t): convert me */
  252. keylen = EVP_CIPHER_key_length(ciph);
  253. if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
  254. uint32_t algenc;
  255. ivlen = EVP_CCM_TLS_IV_LEN;
  256. if (s->s3->tmp.new_cipher == NULL) {
  257. /* We've not selected a cipher yet - we must be doing early data */
  258. algenc = s->session->cipher->algorithm_enc;
  259. } else {
  260. algenc = s->s3->tmp.new_cipher->algorithm_enc;
  261. }
  262. if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  263. taglen = EVP_CCM8_TLS_TAG_LEN;
  264. else
  265. taglen = EVP_CCM_TLS_TAG_LEN;
  266. } else {
  267. ivlen = EVP_CIPHER_iv_length(ciph);
  268. taglen = 0;
  269. }
  270. if (!tls13_derive_key(s, md, secret, key, keylen)
  271. || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
  272. SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
  273. goto err;
  274. }
  275. if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
  276. || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
  277. || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
  278. taglen, NULL))
  279. || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
  280. SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_EVP_LIB);
  281. goto err;
  282. }
  283. return 1;
  284. err:
  285. OPENSSL_cleanse(key, sizeof(key));
  286. return 0;
  287. }
  288. int tls13_change_cipher_state(SSL *s, int which)
  289. {
  290. static const unsigned char client_early_traffic[] = "c e traffic";
  291. static const unsigned char client_handshake_traffic[] = "c hs traffic";
  292. static const unsigned char client_application_traffic[] = "c ap traffic";
  293. static const unsigned char server_handshake_traffic[] = "s hs traffic";
  294. static const unsigned char server_application_traffic[] = "s ap traffic";
  295. static const unsigned char exporter_master_secret[] = "exp master";
  296. static const unsigned char resumption_master_secret[] = "res master";
  297. unsigned char *iv;
  298. unsigned char secret[EVP_MAX_MD_SIZE];
  299. unsigned char hashval[EVP_MAX_MD_SIZE];
  300. unsigned char *hash = hashval;
  301. unsigned char *insecret;
  302. unsigned char *finsecret = NULL;
  303. const char *log_label = NULL;
  304. EVP_CIPHER_CTX *ciph_ctx;
  305. size_t finsecretlen = 0;
  306. const unsigned char *label;
  307. size_t labellen, hashlen = 0;
  308. int ret = 0;
  309. const EVP_MD *md = NULL;
  310. const EVP_CIPHER *cipher = NULL;
  311. if (which & SSL3_CC_READ) {
  312. if (s->enc_read_ctx != NULL) {
  313. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  314. } else {
  315. s->enc_read_ctx = EVP_CIPHER_CTX_new();
  316. if (s->enc_read_ctx == NULL) {
  317. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  318. goto err;
  319. }
  320. }
  321. ciph_ctx = s->enc_read_ctx;
  322. iv = s->read_iv;
  323. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  324. } else {
  325. if (s->enc_write_ctx != NULL) {
  326. EVP_CIPHER_CTX_reset(s->enc_write_ctx);
  327. } else {
  328. s->enc_write_ctx = EVP_CIPHER_CTX_new();
  329. if (s->enc_write_ctx == NULL) {
  330. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  331. goto err;
  332. }
  333. }
  334. ciph_ctx = s->enc_write_ctx;
  335. iv = s->write_iv;
  336. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  337. }
  338. if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
  339. || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
  340. if (which & SSL3_CC_EARLY) {
  341. EVP_MD_CTX *mdctx = NULL;
  342. long handlen;
  343. void *hdata;
  344. unsigned int hashlenui;
  345. const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
  346. insecret = s->early_secret;
  347. label = client_early_traffic;
  348. labellen = sizeof(client_early_traffic) - 1;
  349. log_label = CLIENT_EARLY_LABEL;
  350. handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  351. if (handlen <= 0) {
  352. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE,
  353. SSL_R_BAD_HANDSHAKE_LENGTH);
  354. goto err;
  355. }
  356. if (sslcipher == NULL) {
  357. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  358. goto err;
  359. }
  360. /*
  361. * We need to calculate the handshake digest using the digest from
  362. * the session. We haven't yet selected our ciphersuite so we can't
  363. * use ssl_handshake_md().
  364. */
  365. mdctx = EVP_MD_CTX_new();
  366. if (mdctx == NULL) {
  367. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  368. goto err;
  369. }
  370. cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
  371. md = ssl_md(sslcipher->algorithm2);
  372. if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
  373. || !EVP_DigestUpdate(mdctx, hdata, handlen)
  374. || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
  375. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  376. EVP_MD_CTX_free(mdctx);
  377. goto err;
  378. }
  379. hashlen = hashlenui;
  380. EVP_MD_CTX_free(mdctx);
  381. } else if (which & SSL3_CC_HANDSHAKE) {
  382. insecret = s->handshake_secret;
  383. finsecret = s->client_finished_secret;
  384. finsecretlen = EVP_MD_size(ssl_handshake_md(s));
  385. label = client_handshake_traffic;
  386. labellen = sizeof(client_handshake_traffic) - 1;
  387. log_label = CLIENT_HANDSHAKE_LABEL;
  388. /*
  389. * The handshake hash used for the server read/client write handshake
  390. * traffic secret is the same as the hash for the server
  391. * write/client read handshake traffic secret. However, if we
  392. * processed early data then we delay changing the server
  393. * read/client write cipher state until later, and the handshake
  394. * hashes have moved on. Therefore we use the value saved earlier
  395. * when we did the server write/client read change cipher state.
  396. */
  397. hash = s->handshake_traffic_hash;
  398. } else {
  399. insecret = s->master_secret;
  400. label = client_application_traffic;
  401. labellen = sizeof(client_application_traffic) - 1;
  402. log_label = CLIENT_APPLICATION_LABEL;
  403. /*
  404. * For this we only use the handshake hashes up until the server
  405. * Finished hash. We do not include the client's Finished, which is
  406. * what ssl_handshake_hash() would give us. Instead we use the
  407. * previously saved value.
  408. */
  409. hash = s->server_finished_hash;
  410. }
  411. } else {
  412. /* Early data never applies to client-read/server-write */
  413. if (which & SSL3_CC_HANDSHAKE) {
  414. insecret = s->handshake_secret;
  415. finsecret = s->server_finished_secret;
  416. finsecretlen = EVP_MD_size(ssl_handshake_md(s));
  417. label = server_handshake_traffic;
  418. labellen = sizeof(server_handshake_traffic) - 1;
  419. log_label = SERVER_HANDSHAKE_LABEL;
  420. } else {
  421. insecret = s->master_secret;
  422. label = server_application_traffic;
  423. labellen = sizeof(server_application_traffic) - 1;
  424. log_label = SERVER_APPLICATION_LABEL;
  425. }
  426. }
  427. if (!(which & SSL3_CC_EARLY)) {
  428. md = ssl_handshake_md(s);
  429. cipher = s->s3->tmp.new_sym_enc;
  430. if (!ssl3_digest_cached_records(s, 1)
  431. || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
  432. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  433. goto err;
  434. }
  435. }
  436. /*
  437. * Save the hash of handshakes up to now for use when we calculate the
  438. * client application traffic secret
  439. */
  440. if (label == server_application_traffic)
  441. memcpy(s->server_finished_hash, hashval, hashlen);
  442. if (label == server_handshake_traffic)
  443. memcpy(s->handshake_traffic_hash, hashval, hashlen);
  444. if (label == client_application_traffic) {
  445. /*
  446. * We also create the resumption master secret, but this time use the
  447. * hash for the whole handshake including the Client Finished
  448. */
  449. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  450. resumption_master_secret,
  451. sizeof(resumption_master_secret) - 1,
  452. hashval, hashlen, s->session->master_key,
  453. hashlen)) {
  454. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  455. goto err;
  456. }
  457. s->session->master_key_length = hashlen;
  458. /* Now we create the exporter master secret */
  459. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  460. exporter_master_secret,
  461. sizeof(exporter_master_secret) - 1,
  462. hash, hashlen, s->exporter_master_secret,
  463. hashlen)) {
  464. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  465. goto err;
  466. }
  467. }
  468. if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
  469. insecret, hash, label, labellen, secret, iv,
  470. ciph_ctx)) {
  471. goto err;
  472. }
  473. if (label == server_application_traffic)
  474. memcpy(s->server_app_traffic_secret, secret, hashlen);
  475. else if (label == client_application_traffic)
  476. memcpy(s->client_app_traffic_secret, secret, hashlen);
  477. if (!ssl_log_secret(s, log_label, secret, hashlen)) {
  478. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  479. goto err;
  480. }
  481. if (finsecret != NULL
  482. && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
  483. finsecret, finsecretlen)) {
  484. SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  485. goto err;
  486. }
  487. ret = 1;
  488. err:
  489. OPENSSL_cleanse(secret, sizeof(secret));
  490. return ret;
  491. }
  492. int tls13_update_key(SSL *s, int sending)
  493. {
  494. static const unsigned char application_traffic[] = "traffic upd";
  495. const EVP_MD *md = ssl_handshake_md(s);
  496. size_t hashlen = EVP_MD_size(md);
  497. unsigned char *insecret, *iv;
  498. unsigned char secret[EVP_MAX_MD_SIZE];
  499. EVP_CIPHER_CTX *ciph_ctx;
  500. int ret = 0;
  501. if (s->server == sending)
  502. insecret = s->server_app_traffic_secret;
  503. else
  504. insecret = s->client_app_traffic_secret;
  505. if (sending) {
  506. iv = s->write_iv;
  507. ciph_ctx = s->enc_write_ctx;
  508. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  509. } else {
  510. iv = s->read_iv;
  511. ciph_ctx = s->enc_read_ctx;
  512. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  513. }
  514. if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
  515. s->s3->tmp.new_sym_enc, insecret, NULL,
  516. application_traffic,
  517. sizeof(application_traffic) - 1, secret, iv,
  518. ciph_ctx))
  519. goto err;
  520. memcpy(insecret, secret, hashlen);
  521. ret = 1;
  522. err:
  523. OPENSSL_cleanse(secret, sizeof(secret));
  524. return ret;
  525. }
  526. int tls13_alert_code(int code)
  527. {
  528. if (code == SSL_AD_MISSING_EXTENSION)
  529. return code;
  530. return tls1_alert_code(code);
  531. }
  532. int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  533. const char *label, size_t llen,
  534. const unsigned char *context,
  535. size_t contextlen, int use_context)
  536. {
  537. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  538. static const unsigned char exporterlabel[] = "exporter";
  539. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  540. const EVP_MD *md = ssl_handshake_md(s);
  541. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  542. unsigned int hashsize, datalen;
  543. int ret = 0;
  544. if (ctx == NULL || !SSL_is_init_finished(s))
  545. goto err;
  546. if (!use_context)
  547. contextlen = 0;
  548. if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  549. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  550. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  551. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  552. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  553. || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
  554. (const unsigned char *)label, llen,
  555. data, datalen, exportsecret, hashsize)
  556. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  557. sizeof(exporterlabel) - 1, hash, hashsize,
  558. out, olen))
  559. goto err;
  560. ret = 1;
  561. err:
  562. EVP_MD_CTX_free(ctx);
  563. return ret;
  564. }