tls13_enc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. #ifdef CHARSET_EBCDIC
  388. static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
  389. static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
  390. static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
  391. static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
  392. static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
  393. static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
  394. static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
  395. static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
  396. #else
  397. static const unsigned char client_early_traffic[] = "c e traffic";
  398. static const unsigned char client_handshake_traffic[] = "c hs traffic";
  399. static const unsigned char client_application_traffic[] = "c ap traffic";
  400. static const unsigned char server_handshake_traffic[] = "s hs traffic";
  401. static const unsigned char server_application_traffic[] = "s ap traffic";
  402. static const unsigned char exporter_master_secret[] = "exp master";
  403. static const unsigned char resumption_master_secret[] = "res master";
  404. static const unsigned char early_exporter_master_secret[] = "e exp master";
  405. #endif
  406. #ifndef OPENSSL_NO_QUIC
  407. static int quic_change_cipher_state(SSL *s, int which)
  408. {
  409. unsigned char hash[EVP_MAX_MD_SIZE];
  410. size_t hashlen = 0;
  411. int hashleni;
  412. int ret = 0;
  413. const EVP_MD *md = NULL;
  414. OSSL_ENCRYPTION_LEVEL level;
  415. int is_handshake = ((which & SSL3_CC_HANDSHAKE) == SSL3_CC_HANDSHAKE);
  416. int is_client_read = ((which & SSL3_CHANGE_CIPHER_CLIENT_READ) == SSL3_CHANGE_CIPHER_CLIENT_READ);
  417. int is_server_write = ((which & SSL3_CHANGE_CIPHER_SERVER_WRITE) == SSL3_CHANGE_CIPHER_SERVER_WRITE);
  418. int is_early = (which & SSL3_CC_EARLY);
  419. if (is_early) {
  420. EVP_MD_CTX *mdctx = NULL;
  421. long handlen;
  422. void *hdata;
  423. unsigned int hashlenui;
  424. const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
  425. handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  426. if (handlen <= 0) {
  427. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_CHANGE_CIPHER_STATE,
  428. SSL_R_BAD_HANDSHAKE_LENGTH);
  429. goto err;
  430. }
  431. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  432. && s->max_early_data > 0 && s->session->ext.max_early_data == 0) {
  433. /*
  434. * If we are attempting to send early data, and we've decided to
  435. * actually do it but max_early_data in s->session is 0 then we
  436. * must be using an external PSK.
  437. */
  438. if (!ossl_assert(s->psksession != NULL
  439. && s->max_early_data
  440. == s->psksession->ext.max_early_data)) {
  441. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  442. SSL_F_QUIC_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  443. goto err;
  444. }
  445. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  446. }
  447. if (sslcipher == NULL) {
  448. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_CHANGE_CIPHER_STATE,
  449. SSL_R_BAD_PSK);
  450. goto err;
  451. }
  452. /*
  453. * We need to calculate the handshake digest using the digest from
  454. * the session. We haven't yet selected our ciphersuite so we can't
  455. * use ssl_handshake_md().
  456. */
  457. mdctx = EVP_MD_CTX_new();
  458. if (mdctx == NULL) {
  459. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_CHANGE_CIPHER_STATE,
  460. ERR_R_MALLOC_FAILURE);
  461. goto err;
  462. }
  463. md = ssl_md(sslcipher->algorithm2);
  464. if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
  465. || !EVP_DigestUpdate(mdctx, hdata, handlen)
  466. || !EVP_DigestFinal_ex(mdctx, hash, &hashlenui)) {
  467. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_CHANGE_CIPHER_STATE,
  468. ERR_R_INTERNAL_ERROR);
  469. EVP_MD_CTX_free(mdctx);
  470. goto err;
  471. }
  472. hashlen = hashlenui;
  473. EVP_MD_CTX_free(mdctx);
  474. } else {
  475. md = ssl_handshake_md(s);
  476. if (!ssl3_digest_cached_records(s, 1)
  477. || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  478. /* SSLfatal() already called */;
  479. goto err;
  480. }
  481. /* Ensure cast to size_t is safe */
  482. hashleni = EVP_MD_size(md);
  483. if (!ossl_assert(hashleni >= 0)) {
  484. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_CHANGE_CIPHER_STATE,
  485. ERR_R_EVP_LIB);
  486. goto err;
  487. }
  488. hashlen = (size_t)hashleni;
  489. }
  490. if (is_client_read || is_server_write) {
  491. if (is_handshake) {
  492. /*
  493. * This looks a bit weird, since the condition is basically "the
  494. * server is writing" but we set both the server *and* client
  495. * handshake traffic keys here. That's because there's only a fixed
  496. * number of change-cipher-state events in the TLS 1.3 handshake,
  497. * and in particular there's not an event in between when the server
  498. * writes encrypted handshake messages and when the client writes
  499. * encrypted handshake messages, so we generate both here.
  500. */
  501. level = ssl_encryption_handshake;
  502. if (!tls13_hkdf_expand(s, md, s->handshake_secret,
  503. client_handshake_traffic,
  504. sizeof(client_handshake_traffic)-1, hash,
  505. hashlen, s->client_hand_traffic_secret,
  506. hashlen, 1)
  507. || !ssl_log_secret(s, CLIENT_HANDSHAKE_LABEL,
  508. s->client_hand_traffic_secret, hashlen)
  509. || !tls13_derive_finishedkey(s, md,
  510. s->client_hand_traffic_secret,
  511. s->client_finished_secret, hashlen)
  512. || !tls13_hkdf_expand(s, md, s->handshake_secret,
  513. server_handshake_traffic,
  514. sizeof(server_handshake_traffic)-1, hash,
  515. hashlen, s->server_hand_traffic_secret,
  516. hashlen, 1)
  517. || !ssl_log_secret(s, SERVER_HANDSHAKE_LABEL,
  518. s->server_hand_traffic_secret, hashlen)
  519. || !tls13_derive_finishedkey(s, md,
  520. s->server_hand_traffic_secret,
  521. s->server_finished_secret,
  522. hashlen)) {
  523. /* SSLfatal() already called */
  524. goto err;
  525. }
  526. } else {
  527. /*
  528. * As above, we generate both sets of application traffic keys at
  529. * the same time.
  530. */
  531. level = ssl_encryption_application;
  532. if (!tls13_hkdf_expand(s, md, s->master_secret,
  533. client_application_traffic,
  534. sizeof(client_application_traffic)-1, hash,
  535. hashlen, s->client_app_traffic_secret,
  536. hashlen, 1)
  537. || !ssl_log_secret(s, CLIENT_APPLICATION_LABEL,
  538. s->client_app_traffic_secret, hashlen)
  539. || !tls13_hkdf_expand(s, md, s->master_secret,
  540. server_application_traffic,
  541. sizeof(server_application_traffic)-1,
  542. hash, hashlen,
  543. s->server_app_traffic_secret, hashlen, 1)
  544. || !ssl_log_secret(s, SERVER_APPLICATION_LABEL,
  545. s->server_app_traffic_secret, hashlen)) {
  546. /* SSLfatal() already called */
  547. goto err;
  548. }
  549. }
  550. if (!quic_set_encryption_secrets(s, level)) {
  551. /* SSLfatal() already called */
  552. goto err;
  553. }
  554. if (s->server)
  555. s->quic_write_level = level;
  556. else
  557. s->quic_read_level = level;
  558. } else {
  559. /* is_client_write || is_server_read */
  560. if (is_early) {
  561. level = ssl_encryption_early_data;
  562. if (!tls13_hkdf_expand(s, md, s->early_secret, client_early_traffic,
  563. sizeof(client_early_traffic)-1, hash,
  564. hashlen, s->client_early_traffic_secret,
  565. hashlen, 1)
  566. || !ssl_log_secret(s, CLIENT_EARLY_LABEL,
  567. s->client_early_traffic_secret, hashlen)
  568. || !quic_set_encryption_secrets(s, level)) {
  569. /* SSLfatal() already called */
  570. goto err;
  571. }
  572. } else if (is_handshake) {
  573. level = ssl_encryption_handshake;
  574. } else {
  575. level = ssl_encryption_application;
  576. /*
  577. * We also create the resumption master secret, but this time use the
  578. * hash for the whole handshake including the Client Finished
  579. */
  580. if (!tls13_hkdf_expand(s, md, s->master_secret,
  581. resumption_master_secret,
  582. sizeof(resumption_master_secret)-1, hash,
  583. hashlen, s->resumption_master_secret,
  584. hashlen, 1)) {
  585. /* SSLfatal() already called */
  586. goto err;
  587. }
  588. }
  589. if (level != ssl_encryption_early_data) {
  590. if (s->server)
  591. s->quic_read_level = level;
  592. else
  593. s->quic_write_level = level;
  594. }
  595. }
  596. ret = 1;
  597. err:
  598. return ret;
  599. }
  600. #endif /* OPENSSL_NO_QUIC */
  601. int tls13_change_cipher_state(SSL *s, int which)
  602. {
  603. unsigned char *iv;
  604. unsigned char secret[EVP_MAX_MD_SIZE];
  605. unsigned char hashval[EVP_MAX_MD_SIZE];
  606. unsigned char *hash = hashval;
  607. unsigned char *insecret;
  608. unsigned char *finsecret = NULL;
  609. const char *log_label = NULL;
  610. EVP_CIPHER_CTX *ciph_ctx;
  611. size_t finsecretlen = 0;
  612. const unsigned char *label;
  613. size_t labellen, hashlen = 0;
  614. int ret = 0;
  615. const EVP_MD *md = NULL;
  616. const EVP_CIPHER *cipher = NULL;
  617. #ifndef OPENSSL_NO_QUIC
  618. if (SSL_IS_QUIC(s))
  619. return quic_change_cipher_state(s, which);
  620. #endif
  621. if (which & SSL3_CC_READ) {
  622. if (s->enc_read_ctx != NULL) {
  623. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  624. } else {
  625. s->enc_read_ctx = EVP_CIPHER_CTX_new();
  626. if (s->enc_read_ctx == NULL) {
  627. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  628. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  629. goto err;
  630. }
  631. }
  632. ciph_ctx = s->enc_read_ctx;
  633. iv = s->read_iv;
  634. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  635. } else {
  636. s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
  637. if (s->enc_write_ctx != NULL) {
  638. EVP_CIPHER_CTX_reset(s->enc_write_ctx);
  639. } else {
  640. s->enc_write_ctx = EVP_CIPHER_CTX_new();
  641. if (s->enc_write_ctx == NULL) {
  642. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  643. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  644. goto err;
  645. }
  646. }
  647. ciph_ctx = s->enc_write_ctx;
  648. iv = s->write_iv;
  649. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  650. }
  651. if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
  652. || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
  653. if (which & SSL3_CC_EARLY) {
  654. EVP_MD_CTX *mdctx = NULL;
  655. long handlen;
  656. void *hdata;
  657. unsigned int hashlenui;
  658. const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
  659. insecret = s->early_secret;
  660. label = client_early_traffic;
  661. labellen = sizeof(client_early_traffic) - 1;
  662. log_label = CLIENT_EARLY_LABEL;
  663. handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  664. if (handlen <= 0) {
  665. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  666. SSL_F_TLS13_CHANGE_CIPHER_STATE,
  667. SSL_R_BAD_HANDSHAKE_LENGTH);
  668. goto err;
  669. }
  670. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  671. && s->max_early_data > 0
  672. && s->session->ext.max_early_data == 0) {
  673. /*
  674. * If we are attempting to send early data, and we've decided to
  675. * actually do it but max_early_data in s->session is 0 then we
  676. * must be using an external PSK.
  677. */
  678. if (!ossl_assert(s->psksession != NULL
  679. && s->max_early_data ==
  680. s->psksession->ext.max_early_data)) {
  681. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  682. SSL_F_TLS13_CHANGE_CIPHER_STATE,
  683. ERR_R_INTERNAL_ERROR);
  684. goto err;
  685. }
  686. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  687. }
  688. if (sslcipher == NULL) {
  689. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  690. SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
  691. goto err;
  692. }
  693. /*
  694. * We need to calculate the handshake digest using the digest from
  695. * the session. We haven't yet selected our ciphersuite so we can't
  696. * use ssl_handshake_md().
  697. */
  698. mdctx = EVP_MD_CTX_new();
  699. if (mdctx == NULL) {
  700. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  701. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  702. goto err;
  703. }
  704. cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
  705. md = ssl_md(sslcipher->algorithm2);
  706. if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
  707. || !EVP_DigestUpdate(mdctx, hdata, handlen)
  708. || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
  709. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  710. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  711. EVP_MD_CTX_free(mdctx);
  712. goto err;
  713. }
  714. hashlen = hashlenui;
  715. EVP_MD_CTX_free(mdctx);
  716. if (!tls13_hkdf_expand(s, md, insecret,
  717. early_exporter_master_secret,
  718. sizeof(early_exporter_master_secret) - 1,
  719. hashval, hashlen,
  720. s->early_exporter_master_secret, hashlen,
  721. 1)) {
  722. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  723. SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  724. goto err;
  725. }
  726. if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
  727. s->early_exporter_master_secret, hashlen)) {
  728. /* SSLfatal() already called */
  729. goto err;
  730. }
  731. } else if (which & SSL3_CC_HANDSHAKE) {
  732. insecret = s->handshake_secret;
  733. finsecret = s->client_finished_secret;
  734. finsecretlen = EVP_MD_size(ssl_handshake_md(s));
  735. label = client_handshake_traffic;
  736. labellen = sizeof(client_handshake_traffic) - 1;
  737. log_label = CLIENT_HANDSHAKE_LABEL;
  738. /*
  739. * The handshake hash used for the server read/client write handshake
  740. * traffic secret is the same as the hash for the server
  741. * write/client read handshake traffic secret. However, if we
  742. * processed early data then we delay changing the server
  743. * read/client write cipher state until later, and the handshake
  744. * hashes have moved on. Therefore we use the value saved earlier
  745. * when we did the server write/client read change cipher state.
  746. */
  747. hash = s->handshake_traffic_hash;
  748. } else {
  749. insecret = s->master_secret;
  750. label = client_application_traffic;
  751. labellen = sizeof(client_application_traffic) - 1;
  752. log_label = CLIENT_APPLICATION_LABEL;
  753. /*
  754. * For this we only use the handshake hashes up until the server
  755. * Finished hash. We do not include the client's Finished, which is
  756. * what ssl_handshake_hash() would give us. Instead we use the
  757. * previously saved value.
  758. */
  759. hash = s->server_finished_hash;
  760. }
  761. } else {
  762. /* Early data never applies to client-read/server-write */
  763. if (which & SSL3_CC_HANDSHAKE) {
  764. insecret = s->handshake_secret;
  765. finsecret = s->server_finished_secret;
  766. finsecretlen = EVP_MD_size(ssl_handshake_md(s));
  767. label = server_handshake_traffic;
  768. labellen = sizeof(server_handshake_traffic) - 1;
  769. log_label = SERVER_HANDSHAKE_LABEL;
  770. } else {
  771. insecret = s->master_secret;
  772. label = server_application_traffic;
  773. labellen = sizeof(server_application_traffic) - 1;
  774. log_label = SERVER_APPLICATION_LABEL;
  775. }
  776. }
  777. if (!(which & SSL3_CC_EARLY)) {
  778. md = ssl_handshake_md(s);
  779. cipher = s->s3->tmp.new_sym_enc;
  780. if (!ssl3_digest_cached_records(s, 1)
  781. || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
  782. /* SSLfatal() already called */;
  783. goto err;
  784. }
  785. }
  786. /*
  787. * Save the hash of handshakes up to now for use when we calculate the
  788. * client application traffic secret
  789. */
  790. if (label == server_application_traffic)
  791. memcpy(s->server_finished_hash, hashval, hashlen);
  792. if (label == server_handshake_traffic)
  793. memcpy(s->handshake_traffic_hash, hashval, hashlen);
  794. if (label == client_application_traffic) {
  795. /*
  796. * We also create the resumption master secret, but this time use the
  797. * hash for the whole handshake including the Client Finished
  798. */
  799. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  800. resumption_master_secret,
  801. sizeof(resumption_master_secret) - 1,
  802. hashval, hashlen, s->resumption_master_secret,
  803. hashlen, 1)) {
  804. /* SSLfatal() already called */
  805. goto err;
  806. }
  807. }
  808. if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
  809. insecret, hash, label, labellen, secret, iv,
  810. ciph_ctx)) {
  811. /* SSLfatal() already called */
  812. goto err;
  813. }
  814. if (label == server_application_traffic) {
  815. memcpy(s->server_app_traffic_secret, secret, hashlen);
  816. /* Now we create the exporter master secret */
  817. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  818. exporter_master_secret,
  819. sizeof(exporter_master_secret) - 1,
  820. hash, hashlen, s->exporter_master_secret,
  821. hashlen, 1)) {
  822. /* SSLfatal() already called */
  823. goto err;
  824. }
  825. if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
  826. hashlen)) {
  827. /* SSLfatal() already called */
  828. goto err;
  829. }
  830. } else if (label == client_application_traffic)
  831. memcpy(s->client_app_traffic_secret, secret, hashlen);
  832. if (!ssl_log_secret(s, log_label, secret, hashlen)) {
  833. /* SSLfatal() already called */
  834. goto err;
  835. }
  836. if (finsecret != NULL
  837. && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
  838. finsecret, finsecretlen)) {
  839. /* SSLfatal() already called */
  840. goto err;
  841. }
  842. if (!s->server && label == client_early_traffic)
  843. s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
  844. else
  845. s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
  846. ret = 1;
  847. err:
  848. OPENSSL_cleanse(secret, sizeof(secret));
  849. return ret;
  850. }
  851. int tls13_update_key(SSL *s, int sending)
  852. {
  853. #ifdef CHARSET_EBCDIC
  854. static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
  855. #else
  856. static const unsigned char application_traffic[] = "traffic upd";
  857. #endif
  858. const EVP_MD *md = ssl_handshake_md(s);
  859. size_t hashlen = EVP_MD_size(md);
  860. unsigned char *insecret, *iv;
  861. unsigned char secret[EVP_MAX_MD_SIZE];
  862. EVP_CIPHER_CTX *ciph_ctx;
  863. int ret = 0;
  864. if (s->server == sending)
  865. insecret = s->server_app_traffic_secret;
  866. else
  867. insecret = s->client_app_traffic_secret;
  868. if (sending) {
  869. s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
  870. iv = s->write_iv;
  871. ciph_ctx = s->enc_write_ctx;
  872. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  873. } else {
  874. iv = s->read_iv;
  875. ciph_ctx = s->enc_read_ctx;
  876. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  877. }
  878. if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
  879. s->s3->tmp.new_sym_enc, insecret, NULL,
  880. application_traffic,
  881. sizeof(application_traffic) - 1, secret, iv,
  882. ciph_ctx)) {
  883. /* SSLfatal() already called */
  884. goto err;
  885. }
  886. memcpy(insecret, secret, hashlen);
  887. s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
  888. ret = 1;
  889. err:
  890. OPENSSL_cleanse(secret, sizeof(secret));
  891. return ret;
  892. }
  893. int tls13_alert_code(int code)
  894. {
  895. /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
  896. if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
  897. return code;
  898. return tls1_alert_code(code);
  899. }
  900. int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  901. const char *label, size_t llen,
  902. const unsigned char *context,
  903. size_t contextlen, int use_context)
  904. {
  905. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  906. #ifdef CHARSET_EBCDIC
  907. static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
  908. #else
  909. static const unsigned char exporterlabel[] = "exporter";
  910. #endif
  911. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  912. const EVP_MD *md = ssl_handshake_md(s);
  913. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  914. unsigned int hashsize, datalen;
  915. int ret = 0;
  916. if (ctx == NULL || !ossl_statem_export_allowed(s))
  917. goto err;
  918. if (!use_context)
  919. contextlen = 0;
  920. if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  921. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  922. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  923. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  924. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  925. || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
  926. (const unsigned char *)label, llen,
  927. data, datalen, exportsecret, hashsize, 0)
  928. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  929. sizeof(exporterlabel) - 1, hash, hashsize,
  930. out, olen, 0))
  931. goto err;
  932. ret = 1;
  933. err:
  934. EVP_MD_CTX_free(ctx);
  935. return ret;
  936. }
  937. int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  938. const char *label, size_t llen,
  939. const unsigned char *context,
  940. size_t contextlen)
  941. {
  942. #ifdef CHARSET_EBCDIC
  943. static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
  944. #else
  945. static const unsigned char exporterlabel[] = "exporter";
  946. #endif
  947. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  948. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  949. const EVP_MD *md;
  950. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  951. unsigned int hashsize, datalen;
  952. int ret = 0;
  953. const SSL_CIPHER *sslcipher;
  954. if (ctx == NULL || !ossl_statem_export_early_allowed(s))
  955. goto err;
  956. if (!s->server && s->max_early_data > 0
  957. && s->session->ext.max_early_data == 0)
  958. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  959. else
  960. sslcipher = SSL_SESSION_get0_cipher(s->session);
  961. md = ssl_md(sslcipher->algorithm2);
  962. /*
  963. * Calculate the hash value and store it in |data|. The reason why
  964. * the empty string is used is that the definition of TLS-Exporter
  965. * is like so:
  966. *
  967. * TLS-Exporter(label, context_value, key_length) =
  968. * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
  969. * "exporter", Hash(context_value), key_length)
  970. *
  971. * Derive-Secret(Secret, Label, Messages) =
  972. * HKDF-Expand-Label(Secret, Label,
  973. * Transcript-Hash(Messages), Hash.length)
  974. *
  975. * Here Transcript-Hash is the cipher suite hash algorithm.
  976. */
  977. if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  978. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  979. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  980. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  981. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  982. || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
  983. (const unsigned char *)label, llen,
  984. data, datalen, exportsecret, hashsize, 0)
  985. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  986. sizeof(exporterlabel) - 1, hash, hashsize,
  987. out, olen, 0))
  988. goto err;
  989. ret = 1;
  990. err:
  991. EVP_MD_CTX_free(ctx);
  992. return ret;
  993. }