tls13_enc.c 33 KB

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