tls13_enc.c 33 KB

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