tls13_enc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright 2016-2022 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, int sending,
  295. const EVP_MD *md,
  296. const EVP_CIPHER *ciph,
  297. const unsigned char *insecret,
  298. const unsigned char *hash,
  299. const unsigned char *label,
  300. size_t labellen, unsigned char *secret,
  301. unsigned char *key, size_t *keylen,
  302. unsigned char *iv, size_t *ivlen,
  303. size_t *taglen)
  304. {
  305. int hashleni = EVP_MD_get_size(md);
  306. size_t hashlen;
  307. int mode;
  308. /* Ensure cast to size_t is safe */
  309. if (!ossl_assert(hashleni >= 0)) {
  310. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  311. return 0;
  312. }
  313. hashlen = (size_t)hashleni;
  314. if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
  315. secret, hashlen, 1)) {
  316. /* SSLfatal() already called */
  317. return 0;
  318. }
  319. *keylen = EVP_CIPHER_get_key_length(ciph);
  320. mode = EVP_CIPHER_get_mode(ciph);
  321. if (mode == EVP_CIPH_CCM_MODE) {
  322. uint32_t algenc;
  323. *ivlen = EVP_CCM_TLS_IV_LEN;
  324. if (s->s3.tmp.new_cipher != NULL) {
  325. algenc = s->s3.tmp.new_cipher->algorithm_enc;
  326. } else if (s->session->cipher != NULL) {
  327. /* We've not selected a cipher yet - we must be doing early data */
  328. algenc = s->session->cipher->algorithm_enc;
  329. } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
  330. /* We must be doing early data with out-of-band PSK */
  331. algenc = s->psksession->cipher->algorithm_enc;
  332. } else {
  333. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  334. return 0;
  335. }
  336. if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  337. *taglen = EVP_CCM8_TLS_TAG_LEN;
  338. else
  339. *taglen = EVP_CCM_TLS_TAG_LEN;
  340. } else {
  341. int iivlen;
  342. if (mode == EVP_CIPH_GCM_MODE) {
  343. *taglen = EVP_GCM_TLS_TAG_LEN;
  344. } else {
  345. /* CHACHA20P-POLY1305 */
  346. *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  347. }
  348. iivlen = EVP_CIPHER_get_iv_length(ciph);
  349. if (iivlen < 0) {
  350. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  351. return 0;
  352. }
  353. *ivlen = iivlen;
  354. }
  355. if (!tls13_derive_key(s, md, secret, key, *keylen)
  356. || !tls13_derive_iv(s, md, secret, iv, *ivlen)) {
  357. /* SSLfatal() already called */
  358. return 0;
  359. }
  360. return 1;
  361. }
  362. int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
  363. {
  364. /* ASCII: "c e traffic", in hex for EBCDIC compatibility */
  365. static const unsigned char client_early_traffic[] = "\x63\x20\x65\x20\x74\x72\x61\x66\x66\x69\x63";
  366. /* ASCII: "c hs traffic", in hex for EBCDIC compatibility */
  367. static const unsigned char client_handshake_traffic[] = "\x63\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
  368. /* ASCII: "c ap traffic", in hex for EBCDIC compatibility */
  369. static const unsigned char client_application_traffic[] = "\x63\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
  370. /* ASCII: "s hs traffic", in hex for EBCDIC compatibility */
  371. static const unsigned char server_handshake_traffic[] = "\x73\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
  372. /* ASCII: "s ap traffic", in hex for EBCDIC compatibility */
  373. static const unsigned char server_application_traffic[] = "\x73\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
  374. /* ASCII: "exp master", in hex for EBCDIC compatibility */
  375. static const unsigned char exporter_master_secret[] = "\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
  376. /* ASCII: "res master", in hex for EBCDIC compatibility */
  377. static const unsigned char resumption_master_secret[] = "\x72\x65\x73\x20\x6D\x61\x73\x74\x65\x72";
  378. /* ASCII: "e exp master", in hex for EBCDIC compatibility */
  379. static const unsigned char early_exporter_master_secret[] = "\x65\x20\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
  380. unsigned char iv[EVP_MAX_IV_LENGTH];
  381. unsigned char key[EVP_MAX_KEY_LENGTH];
  382. unsigned char secret[EVP_MAX_MD_SIZE];
  383. unsigned char hashval[EVP_MAX_MD_SIZE];
  384. unsigned char *hash = hashval;
  385. unsigned char *insecret;
  386. unsigned char *finsecret = NULL;
  387. const char *log_label = NULL;
  388. size_t finsecretlen = 0;
  389. const unsigned char *label;
  390. size_t labellen, hashlen = 0;
  391. int ret = 0;
  392. const EVP_MD *md = NULL;
  393. const EVP_CIPHER *cipher = NULL;
  394. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  395. size_t keylen, ivlen, taglen;
  396. int level;
  397. int direction = (which & SSL3_CC_READ) != 0 ? OSSL_RECORD_DIRECTION_READ
  398. : OSSL_RECORD_DIRECTION_WRITE;
  399. if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
  400. || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
  401. if (which & SSL3_CC_EARLY) {
  402. EVP_MD_CTX *mdctx = NULL;
  403. long handlen;
  404. void *hdata;
  405. unsigned int hashlenui;
  406. const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
  407. insecret = s->early_secret;
  408. label = client_early_traffic;
  409. labellen = sizeof(client_early_traffic) - 1;
  410. log_label = CLIENT_EARLY_LABEL;
  411. handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
  412. if (handlen <= 0) {
  413. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
  414. goto err;
  415. }
  416. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  417. && s->max_early_data > 0
  418. && s->session->ext.max_early_data == 0) {
  419. /*
  420. * If we are attempting to send early data, and we've decided to
  421. * actually do it but max_early_data in s->session is 0 then we
  422. * must be using an external PSK.
  423. */
  424. if (!ossl_assert(s->psksession != NULL
  425. && s->max_early_data ==
  426. s->psksession->ext.max_early_data)) {
  427. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  428. goto err;
  429. }
  430. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  431. }
  432. if (sslcipher == NULL) {
  433. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
  434. goto err;
  435. }
  436. /*
  437. * We need to calculate the handshake digest using the digest from
  438. * the session. We haven't yet selected our ciphersuite so we can't
  439. * use ssl_handshake_md().
  440. */
  441. mdctx = EVP_MD_CTX_new();
  442. if (mdctx == NULL) {
  443. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  444. goto err;
  445. }
  446. /*
  447. * This ups the ref count on cipher so we better make sure we free
  448. * it again
  449. */
  450. if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)) {
  451. /* Error is already recorded */
  452. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  453. EVP_MD_CTX_free(mdctx);
  454. goto err;
  455. }
  456. md = ssl_md(sctx, sslcipher->algorithm2);
  457. if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
  458. || !EVP_DigestUpdate(mdctx, hdata, handlen)
  459. || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
  460. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  461. EVP_MD_CTX_free(mdctx);
  462. goto err;
  463. }
  464. hashlen = hashlenui;
  465. EVP_MD_CTX_free(mdctx);
  466. if (!tls13_hkdf_expand(s, md, insecret,
  467. early_exporter_master_secret,
  468. sizeof(early_exporter_master_secret) - 1,
  469. hashval, hashlen,
  470. s->early_exporter_master_secret, hashlen,
  471. 1)) {
  472. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  473. goto err;
  474. }
  475. if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
  476. s->early_exporter_master_secret, hashlen)) {
  477. /* SSLfatal() already called */
  478. goto err;
  479. }
  480. } else if (which & SSL3_CC_HANDSHAKE) {
  481. insecret = s->handshake_secret;
  482. finsecret = s->client_finished_secret;
  483. finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
  484. label = client_handshake_traffic;
  485. labellen = sizeof(client_handshake_traffic) - 1;
  486. log_label = CLIENT_HANDSHAKE_LABEL;
  487. /*
  488. * The handshake hash used for the server read/client write handshake
  489. * traffic secret is the same as the hash for the server
  490. * write/client read handshake traffic secret. However, if we
  491. * processed early data then we delay changing the server
  492. * read/client write cipher state until later, and the handshake
  493. * hashes have moved on. Therefore we use the value saved earlier
  494. * when we did the server write/client read change cipher state.
  495. */
  496. hash = s->handshake_traffic_hash;
  497. } else {
  498. insecret = s->master_secret;
  499. label = client_application_traffic;
  500. labellen = sizeof(client_application_traffic) - 1;
  501. log_label = CLIENT_APPLICATION_LABEL;
  502. /*
  503. * For this we only use the handshake hashes up until the server
  504. * Finished hash. We do not include the client's Finished, which is
  505. * what ssl_handshake_hash() would give us. Instead we use the
  506. * previously saved value.
  507. */
  508. hash = s->server_finished_hash;
  509. }
  510. } else {
  511. /* Early data never applies to client-read/server-write */
  512. if (which & SSL3_CC_HANDSHAKE) {
  513. insecret = s->handshake_secret;
  514. finsecret = s->server_finished_secret;
  515. finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
  516. label = server_handshake_traffic;
  517. labellen = sizeof(server_handshake_traffic) - 1;
  518. log_label = SERVER_HANDSHAKE_LABEL;
  519. } else {
  520. insecret = s->master_secret;
  521. label = server_application_traffic;
  522. labellen = sizeof(server_application_traffic) - 1;
  523. log_label = SERVER_APPLICATION_LABEL;
  524. }
  525. }
  526. if (!(which & SSL3_CC_EARLY)) {
  527. md = ssl_handshake_md(s);
  528. cipher = s->s3.tmp.new_sym_enc;
  529. if (!ssl3_digest_cached_records(s, 1)
  530. || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
  531. /* SSLfatal() already called */;
  532. goto err;
  533. }
  534. }
  535. /*
  536. * Save the hash of handshakes up to now for use when we calculate the
  537. * client application traffic secret
  538. */
  539. if (label == server_application_traffic)
  540. memcpy(s->server_finished_hash, hashval, hashlen);
  541. if (label == server_handshake_traffic)
  542. memcpy(s->handshake_traffic_hash, hashval, hashlen);
  543. if (label == client_application_traffic) {
  544. /*
  545. * We also create the resumption master secret, but this time use the
  546. * hash for the whole handshake including the Client Finished
  547. */
  548. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  549. resumption_master_secret,
  550. sizeof(resumption_master_secret) - 1,
  551. hashval, hashlen, s->resumption_master_secret,
  552. hashlen, 1)) {
  553. /* SSLfatal() already called */
  554. goto err;
  555. }
  556. }
  557. /* check whether cipher is known */
  558. if (!ossl_assert(cipher != NULL))
  559. goto err;
  560. if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
  561. insecret, hash, label, labellen, secret, key,
  562. &keylen, iv, &ivlen, &taglen)) {
  563. /* SSLfatal() already called */
  564. goto err;
  565. }
  566. if (label == server_application_traffic) {
  567. memcpy(s->server_app_traffic_secret, secret, hashlen);
  568. /* Now we create the exporter master secret */
  569. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  570. exporter_master_secret,
  571. sizeof(exporter_master_secret) - 1,
  572. hash, hashlen, s->exporter_master_secret,
  573. hashlen, 1)) {
  574. /* SSLfatal() already called */
  575. goto err;
  576. }
  577. if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
  578. hashlen)) {
  579. /* SSLfatal() already called */
  580. goto err;
  581. }
  582. } else if (label == client_application_traffic)
  583. memcpy(s->client_app_traffic_secret, secret, hashlen);
  584. if (!ssl_log_secret(s, log_label, secret, hashlen)) {
  585. /* SSLfatal() already called */
  586. goto err;
  587. }
  588. if (finsecret != NULL
  589. && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
  590. finsecret, finsecretlen)) {
  591. /* SSLfatal() already called */
  592. goto err;
  593. }
  594. if ((which & SSL3_CC_WRITE) != 0) {
  595. if (!s->server && label == client_early_traffic)
  596. s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 1);
  597. else
  598. s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 0);
  599. }
  600. level = (which & SSL3_CC_EARLY) != 0
  601. ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
  602. : ((which &SSL3_CC_HANDSHAKE) != 0
  603. ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
  604. : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
  605. if (!ssl_set_new_record_layer(s, s->version,
  606. direction,
  607. level, key, keylen, iv, ivlen, NULL, 0,
  608. cipher, taglen, NID_undef, NULL, NULL)) {
  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, sending, 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. key, keylen, iv, ivlen, NULL, 0,
  659. s->s3.tmp.new_sym_enc, taglen, NID_undef, NULL,
  660. NULL)) {
  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. }