tls13_enc.c 34 KB

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