tls13_enc.c 30 KB

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