tls13_enc.c 35 KB

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