2
0

tls13_enc.c 33 KB

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