tls13_enc.c 33 KB

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