tls13_enc.c 33 KB

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