tls13_enc.c 30 KB

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