tls13_enc.c 33 KB

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