tls13_enc.c 37 KB

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