2
0

tls13_enc.c 35 KB

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