tls13_enc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. EVP_CIPHER_CTX *ciph_ctx)
  305. {
  306. int hashleni = EVP_MD_get_size(md);
  307. size_t hashlen;
  308. int mode;
  309. /* Ensure cast to size_t is safe */
  310. if (!ossl_assert(hashleni >= 0)) {
  311. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  312. return 0;
  313. }
  314. hashlen = (size_t)hashleni;
  315. if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
  316. secret, hashlen, 1)) {
  317. /* SSLfatal() already called */
  318. return 0;
  319. }
  320. *keylen = EVP_CIPHER_get_key_length(ciph);
  321. mode = EVP_CIPHER_get_mode(ciph);
  322. if (mode == EVP_CIPH_CCM_MODE) {
  323. uint32_t algenc;
  324. *ivlen = EVP_CCM_TLS_IV_LEN;
  325. if (s->s3.tmp.new_cipher != NULL) {
  326. algenc = s->s3.tmp.new_cipher->algorithm_enc;
  327. } else if (s->session->cipher != NULL) {
  328. /* We've not selected a cipher yet - we must be doing early data */
  329. algenc = s->session->cipher->algorithm_enc;
  330. } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
  331. /* We must be doing early data with out-of-band PSK */
  332. algenc = s->psksession->cipher->algorithm_enc;
  333. } else {
  334. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  335. return 0;
  336. }
  337. if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  338. *taglen = EVP_CCM8_TLS_TAG_LEN;
  339. else
  340. *taglen = EVP_CCM_TLS_TAG_LEN;
  341. } else {
  342. int iivlen;
  343. if (mode == EVP_CIPH_GCM_MODE) {
  344. *taglen = EVP_GCM_TLS_TAG_LEN;
  345. } else {
  346. /* CHACHA20P-POLY1305 */
  347. *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  348. }
  349. iivlen = EVP_CIPHER_get_iv_length(ciph);
  350. if (iivlen < 0) {
  351. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  352. return 0;
  353. }
  354. *ivlen = iivlen;
  355. }
  356. if (!tls13_derive_key(s, md, secret, key, *keylen)
  357. || !tls13_derive_iv(s, md, secret, iv, *ivlen)) {
  358. /* SSLfatal() already called */
  359. return 0;
  360. }
  361. if (sending) {
  362. if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
  363. || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, *ivlen, NULL) <= 0
  364. || (mode == EVP_CIPH_CCM_MODE
  365. && EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, *taglen, NULL) <= 0)
  366. || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
  367. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  368. return 0;
  369. }
  370. }
  371. return 1;
  372. }
  373. int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
  374. {
  375. /* ASCII: "c e traffic", in hex for EBCDIC compatibility */
  376. static const unsigned char client_early_traffic[] = "\x63\x20\x65\x20\x74\x72\x61\x66\x66\x69\x63";
  377. /* ASCII: "c hs traffic", in hex for EBCDIC compatibility */
  378. static const unsigned char client_handshake_traffic[] = "\x63\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
  379. /* ASCII: "c ap traffic", in hex for EBCDIC compatibility */
  380. static const unsigned char client_application_traffic[] = "\x63\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
  381. /* ASCII: "s hs traffic", in hex for EBCDIC compatibility */
  382. static const unsigned char server_handshake_traffic[] = "\x73\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
  383. /* ASCII: "s ap traffic", in hex for EBCDIC compatibility */
  384. static const unsigned char server_application_traffic[] = "\x73\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
  385. /* ASCII: "exp master", in hex for EBCDIC compatibility */
  386. static const unsigned char exporter_master_secret[] = "\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
  387. /* ASCII: "res master", in hex for EBCDIC compatibility */
  388. static const unsigned char resumption_master_secret[] = "\x72\x65\x73\x20\x6D\x61\x73\x74\x65\x72";
  389. /* ASCII: "e exp master", in hex for EBCDIC compatibility */
  390. static const unsigned char early_exporter_master_secret[] = "\x65\x20\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
  391. unsigned char *iv;
  392. unsigned char key[EVP_MAX_KEY_LENGTH];
  393. unsigned char secret[EVP_MAX_MD_SIZE];
  394. unsigned char hashval[EVP_MAX_MD_SIZE];
  395. unsigned char *hash = hashval;
  396. unsigned char *insecret;
  397. unsigned char *finsecret = NULL;
  398. const char *log_label = NULL;
  399. EVP_CIPHER_CTX *ciph_ctx = NULL;
  400. size_t finsecretlen = 0;
  401. const unsigned char *label;
  402. size_t labellen, hashlen = 0;
  403. int ret = 0;
  404. const EVP_MD *md = NULL;
  405. const EVP_CIPHER *cipher = NULL;
  406. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  407. size_t keylen, ivlen, taglen;
  408. #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
  409. ktls_crypto_info_t crypto_info;
  410. void *rl_sequence;
  411. BIO *bio;
  412. #endif
  413. if (which & SSL3_CC_READ) {
  414. iv = s->read_iv;
  415. } else {
  416. s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
  417. if (s->enc_write_ctx != NULL) {
  418. EVP_CIPHER_CTX_reset(s->enc_write_ctx);
  419. } else {
  420. s->enc_write_ctx = EVP_CIPHER_CTX_new();
  421. if (s->enc_write_ctx == NULL) {
  422. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  423. goto err;
  424. }
  425. }
  426. ciph_ctx = s->enc_write_ctx;
  427. iv = s->write_iv;
  428. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  429. }
  430. if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
  431. || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
  432. if (which & SSL3_CC_EARLY) {
  433. EVP_MD_CTX *mdctx = NULL;
  434. long handlen;
  435. void *hdata;
  436. unsigned int hashlenui;
  437. const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
  438. insecret = s->early_secret;
  439. label = client_early_traffic;
  440. labellen = sizeof(client_early_traffic) - 1;
  441. log_label = CLIENT_EARLY_LABEL;
  442. handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
  443. if (handlen <= 0) {
  444. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
  445. goto err;
  446. }
  447. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  448. && s->max_early_data > 0
  449. && s->session->ext.max_early_data == 0) {
  450. /*
  451. * If we are attempting to send early data, and we've decided to
  452. * actually do it but max_early_data in s->session is 0 then we
  453. * must be using an external PSK.
  454. */
  455. if (!ossl_assert(s->psksession != NULL
  456. && s->max_early_data ==
  457. s->psksession->ext.max_early_data)) {
  458. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  459. goto err;
  460. }
  461. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  462. }
  463. if (sslcipher == NULL) {
  464. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
  465. goto err;
  466. }
  467. /*
  468. * We need to calculate the handshake digest using the digest from
  469. * the session. We haven't yet selected our ciphersuite so we can't
  470. * use ssl_handshake_md().
  471. */
  472. mdctx = EVP_MD_CTX_new();
  473. if (mdctx == NULL) {
  474. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  475. goto err;
  476. }
  477. /*
  478. * This ups the ref count on cipher so we better make sure we free
  479. * it again
  480. */
  481. if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)) {
  482. /* Error is already recorded */
  483. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  484. EVP_MD_CTX_free(mdctx);
  485. goto err;
  486. }
  487. md = ssl_md(sctx, sslcipher->algorithm2);
  488. if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
  489. || !EVP_DigestUpdate(mdctx, hdata, handlen)
  490. || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
  491. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  492. EVP_MD_CTX_free(mdctx);
  493. goto err;
  494. }
  495. hashlen = hashlenui;
  496. EVP_MD_CTX_free(mdctx);
  497. if (!tls13_hkdf_expand(s, md, insecret,
  498. early_exporter_master_secret,
  499. sizeof(early_exporter_master_secret) - 1,
  500. hashval, hashlen,
  501. s->early_exporter_master_secret, hashlen,
  502. 1)) {
  503. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  504. goto err;
  505. }
  506. if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
  507. s->early_exporter_master_secret, hashlen)) {
  508. /* SSLfatal() already called */
  509. goto err;
  510. }
  511. } else if (which & SSL3_CC_HANDSHAKE) {
  512. insecret = s->handshake_secret;
  513. finsecret = s->client_finished_secret;
  514. finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
  515. label = client_handshake_traffic;
  516. labellen = sizeof(client_handshake_traffic) - 1;
  517. log_label = CLIENT_HANDSHAKE_LABEL;
  518. /*
  519. * The handshake hash used for the server read/client write handshake
  520. * traffic secret is the same as the hash for the server
  521. * write/client read handshake traffic secret. However, if we
  522. * processed early data then we delay changing the server
  523. * read/client write cipher state until later, and the handshake
  524. * hashes have moved on. Therefore we use the value saved earlier
  525. * when we did the server write/client read change cipher state.
  526. */
  527. hash = s->handshake_traffic_hash;
  528. } else {
  529. insecret = s->master_secret;
  530. label = client_application_traffic;
  531. labellen = sizeof(client_application_traffic) - 1;
  532. log_label = CLIENT_APPLICATION_LABEL;
  533. /*
  534. * For this we only use the handshake hashes up until the server
  535. * Finished hash. We do not include the client's Finished, which is
  536. * what ssl_handshake_hash() would give us. Instead we use the
  537. * previously saved value.
  538. */
  539. hash = s->server_finished_hash;
  540. }
  541. } else {
  542. /* Early data never applies to client-read/server-write */
  543. if (which & SSL3_CC_HANDSHAKE) {
  544. insecret = s->handshake_secret;
  545. finsecret = s->server_finished_secret;
  546. finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
  547. label = server_handshake_traffic;
  548. labellen = sizeof(server_handshake_traffic) - 1;
  549. log_label = SERVER_HANDSHAKE_LABEL;
  550. } else {
  551. insecret = s->master_secret;
  552. label = server_application_traffic;
  553. labellen = sizeof(server_application_traffic) - 1;
  554. log_label = SERVER_APPLICATION_LABEL;
  555. }
  556. }
  557. if (!(which & SSL3_CC_EARLY)) {
  558. md = ssl_handshake_md(s);
  559. cipher = s->s3.tmp.new_sym_enc;
  560. if (!ssl3_digest_cached_records(s, 1)
  561. || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
  562. /* SSLfatal() already called */;
  563. goto err;
  564. }
  565. }
  566. /*
  567. * Save the hash of handshakes up to now for use when we calculate the
  568. * client application traffic secret
  569. */
  570. if (label == server_application_traffic)
  571. memcpy(s->server_finished_hash, hashval, hashlen);
  572. if (label == server_handshake_traffic)
  573. memcpy(s->handshake_traffic_hash, hashval, hashlen);
  574. if (label == client_application_traffic) {
  575. /*
  576. * We also create the resumption master secret, but this time use the
  577. * hash for the whole handshake including the Client Finished
  578. */
  579. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  580. resumption_master_secret,
  581. sizeof(resumption_master_secret) - 1,
  582. hashval, hashlen, s->resumption_master_secret,
  583. hashlen, 1)) {
  584. /* SSLfatal() already called */
  585. goto err;
  586. }
  587. }
  588. /* check whether cipher is known */
  589. if (!ossl_assert(cipher != NULL))
  590. goto err;
  591. if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
  592. insecret, hash, label, labellen, secret, key,
  593. &keylen, iv, &ivlen, &taglen, ciph_ctx)) {
  594. /* SSLfatal() already called */
  595. goto err;
  596. }
  597. if (label == server_application_traffic) {
  598. memcpy(s->server_app_traffic_secret, secret, hashlen);
  599. /* Now we create the exporter master secret */
  600. if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
  601. exporter_master_secret,
  602. sizeof(exporter_master_secret) - 1,
  603. hash, hashlen, s->exporter_master_secret,
  604. hashlen, 1)) {
  605. /* SSLfatal() already called */
  606. goto err;
  607. }
  608. if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
  609. hashlen)) {
  610. /* SSLfatal() already called */
  611. goto err;
  612. }
  613. } else if (label == client_application_traffic)
  614. memcpy(s->client_app_traffic_secret, secret, hashlen);
  615. if (!ssl_log_secret(s, log_label, secret, hashlen)) {
  616. /* SSLfatal() already called */
  617. goto err;
  618. }
  619. if (finsecret != NULL
  620. && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
  621. finsecret, finsecretlen)) {
  622. /* SSLfatal() already called */
  623. goto err;
  624. }
  625. if (!s->server && label == client_early_traffic)
  626. s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
  627. else
  628. s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
  629. if ((which & SSL3_CC_READ) != 0) {
  630. int level = (which & SSL3_CC_EARLY) != 0
  631. ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
  632. : ((which &SSL3_CC_HANDSHAKE) != 0
  633. ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
  634. : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
  635. if (!ssl_set_new_record_layer(s, s->version,
  636. OSSL_RECORD_DIRECTION_READ,
  637. level, key, keylen, iv, ivlen, NULL, 0,
  638. cipher, taglen, NID_undef, NULL, NULL)) {
  639. /* SSLfatal already called */
  640. goto err;
  641. }
  642. /* TODO(RECLAYER): Remove me when write rlayer done */
  643. goto skip_ktls;
  644. }
  645. #ifndef OPENSSL_NO_KTLS
  646. # if defined(OPENSSL_KTLS_TLS13)
  647. if (!(which & SSL3_CC_APPLICATION)
  648. || (s->options & SSL_OP_ENABLE_KTLS) == 0)
  649. goto skip_ktls;
  650. /* ktls supports only the maximum fragment size */
  651. if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
  652. goto skip_ktls;
  653. /* ktls does not support record padding */
  654. if (s->record_padding_cb != NULL)
  655. goto skip_ktls;
  656. /* check that cipher is supported */
  657. if (!ktls_check_supported_cipher(s, cipher, NULL, taglen))
  658. goto skip_ktls;
  659. if (which & SSL3_CC_WRITE)
  660. bio = s->wbio;
  661. else
  662. bio = s->rbio;
  663. if (!ossl_assert(bio != NULL)) {
  664. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  665. goto err;
  666. }
  667. /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
  668. if (which & SSL3_CC_WRITE) {
  669. if (BIO_flush(bio) <= 0)
  670. goto skip_ktls;
  671. }
  672. /* configure kernel crypto structure */
  673. /*
  674. * If we get here we are only doing the write side. The read side goes
  675. * through the new record layer code.
  676. */
  677. rl_sequence = RECORD_LAYER_get_write_sequence(&s->rlayer);
  678. if (!ktls_configure_crypto(sctx->libctx, s->version, cipher, NULL,
  679. rl_sequence, &crypto_info, which & SSL3_CC_WRITE,
  680. iv, ivlen, key, keylen, NULL, 0))
  681. goto skip_ktls;
  682. /* ktls works with user provided buffers directly */
  683. if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
  684. if (which & SSL3_CC_WRITE)
  685. ssl3_release_write_buffer(s);
  686. }
  687. # endif
  688. #endif
  689. skip_ktls:
  690. ret = 1;
  691. err:
  692. if ((which & SSL3_CC_EARLY) != 0) {
  693. /* We up-refed this so now we need to down ref */
  694. ssl_evp_cipher_free(cipher);
  695. }
  696. OPENSSL_cleanse(key, sizeof(key));
  697. OPENSSL_cleanse(secret, sizeof(secret));
  698. return ret;
  699. }
  700. int tls13_update_key(SSL_CONNECTION *s, int sending)
  701. {
  702. /* ASCII: "traffic upd", in hex for EBCDIC compatibility */
  703. static const unsigned char application_traffic[] = "\x74\x72\x61\x66\x66\x69\x63\x20\x75\x70\x64";
  704. const EVP_MD *md = ssl_handshake_md(s);
  705. size_t hashlen;
  706. unsigned char key[EVP_MAX_KEY_LENGTH];
  707. unsigned char *insecret, *iv;
  708. unsigned char secret[EVP_MAX_MD_SIZE];
  709. EVP_CIPHER_CTX *ciph_ctx;
  710. size_t keylen, ivlen, taglen;
  711. int ret = 0, l;
  712. if ((l = EVP_MD_get_size(md)) <= 0) {
  713. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  714. return 0;
  715. }
  716. hashlen = (size_t)l;
  717. if (s->server == sending)
  718. insecret = s->server_app_traffic_secret;
  719. else
  720. insecret = s->client_app_traffic_secret;
  721. if (sending) {
  722. s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
  723. iv = s->write_iv;
  724. ciph_ctx = s->enc_write_ctx;
  725. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  726. } else {
  727. iv = s->read_iv;
  728. ciph_ctx = s->enc_read_ctx;
  729. }
  730. if (!derive_secret_key_and_iv(s, sending, md,
  731. s->s3.tmp.new_sym_enc, insecret, NULL,
  732. application_traffic,
  733. sizeof(application_traffic) - 1, secret, key,
  734. &keylen, iv, &ivlen, &taglen, ciph_ctx)) {
  735. /* SSLfatal() already called */
  736. goto err;
  737. }
  738. memcpy(insecret, secret, hashlen);
  739. if (!sending) {
  740. if (!ssl_set_new_record_layer(s, s->version,
  741. OSSL_RECORD_DIRECTION_READ,
  742. OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
  743. key, keylen, iv, ivlen, NULL, 0,
  744. s->s3.tmp.new_sym_enc, taglen, NID_undef, NULL,
  745. NULL)) {
  746. /* SSLfatal already called */
  747. goto err;
  748. }
  749. }
  750. s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
  751. ret = 1;
  752. err:
  753. OPENSSL_cleanse(key, sizeof(key));
  754. OPENSSL_cleanse(secret, sizeof(secret));
  755. return ret;
  756. }
  757. int tls13_alert_code(int code)
  758. {
  759. /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
  760. if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
  761. return code;
  762. return tls1_alert_code(code);
  763. }
  764. int tls13_export_keying_material(SSL_CONNECTION *s,
  765. unsigned char *out, size_t olen,
  766. const char *label, size_t llen,
  767. const unsigned char *context,
  768. size_t contextlen, int use_context)
  769. {
  770. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  771. /* ASCII: "exporter", in hex for EBCDIC compatibility */
  772. static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
  773. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  774. const EVP_MD *md = ssl_handshake_md(s);
  775. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  776. unsigned int hashsize, datalen;
  777. int ret = 0;
  778. if (ctx == NULL || md == NULL || !ossl_statem_export_allowed(s))
  779. goto err;
  780. if (!use_context)
  781. contextlen = 0;
  782. if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  783. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  784. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  785. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  786. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  787. || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
  788. (const unsigned char *)label, llen,
  789. data, datalen, exportsecret, hashsize, 0)
  790. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  791. sizeof(exporterlabel) - 1, hash, hashsize,
  792. out, olen, 0))
  793. goto err;
  794. ret = 1;
  795. err:
  796. EVP_MD_CTX_free(ctx);
  797. return ret;
  798. }
  799. int tls13_export_keying_material_early(SSL_CONNECTION *s,
  800. unsigned char *out, size_t olen,
  801. const char *label, size_t llen,
  802. const unsigned char *context,
  803. size_t contextlen)
  804. {
  805. /* ASCII: "exporter", in hex for EBCDIC compatibility */
  806. static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
  807. unsigned char exportsecret[EVP_MAX_MD_SIZE];
  808. unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  809. const EVP_MD *md;
  810. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  811. unsigned int hashsize, datalen;
  812. int ret = 0;
  813. const SSL_CIPHER *sslcipher;
  814. if (ctx == NULL || !ossl_statem_export_early_allowed(s))
  815. goto err;
  816. if (!s->server && s->max_early_data > 0
  817. && s->session->ext.max_early_data == 0)
  818. sslcipher = SSL_SESSION_get0_cipher(s->psksession);
  819. else
  820. sslcipher = SSL_SESSION_get0_cipher(s->session);
  821. md = ssl_md(SSL_CONNECTION_GET_CTX(s), sslcipher->algorithm2);
  822. /*
  823. * Calculate the hash value and store it in |data|. The reason why
  824. * the empty string is used is that the definition of TLS-Exporter
  825. * is like so:
  826. *
  827. * TLS-Exporter(label, context_value, key_length) =
  828. * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
  829. * "exporter", Hash(context_value), key_length)
  830. *
  831. * Derive-Secret(Secret, Label, Messages) =
  832. * HKDF-Expand-Label(Secret, Label,
  833. * Transcript-Hash(Messages), Hash.length)
  834. *
  835. * Here Transcript-Hash is the cipher suite hash algorithm.
  836. */
  837. if (md == NULL
  838. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  839. || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  840. || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  841. || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  842. || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  843. || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
  844. (const unsigned char *)label, llen,
  845. data, datalen, exportsecret, hashsize, 0)
  846. || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
  847. sizeof(exporterlabel) - 1, hash, hashsize,
  848. out, olen, 0))
  849. goto err;
  850. ret = 1;
  851. err:
  852. EVP_MD_CTX_free(ctx);
  853. return ret;
  854. }