s2n_tls13_secrets.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "tls/s2n_tls13_secrets.h"
  16. #include "tls/s2n_connection.h"
  17. #include "tls/s2n_key_log.h"
  18. #include "tls/s2n_tls13_handshake.h"
  19. #include "utils/s2n_bitmap.h"
  20. #define S2N_MAX_HASHLEN SHA384_DIGEST_LENGTH
  21. #define CONN_HMAC_ALG(conn) ((conn)->secure->cipher_suite->prf_alg)
  22. #define CONN_SECRETS(conn) ((conn)->secrets.version.tls13)
  23. #define CONN_HASHES(conn) ((conn)->handshake.hashes)
  24. #define CONN_SECRET(conn, secret) ( \
  25. (struct s2n_blob){ .data = CONN_SECRETS(conn).secret, .size = s2n_get_hash_len(CONN_HMAC_ALG(conn)) })
  26. #define CONN_HASH(conn, hash) ( \
  27. (struct s2n_blob){ .data = CONN_HASHES(conn)->hash, .size = s2n_get_hash_len(CONN_HMAC_ALG(conn)) })
  28. #define CONN_FINISHED(conn, mode) ( \
  29. (struct s2n_blob){ .data = (conn)->handshake.mode##_finished, .size = s2n_get_hash_len(CONN_HMAC_ALG(conn)) })
  30. /**
  31. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  32. *# If a given secret is not available, then the 0-value consisting of a
  33. *# string of Hash.length bytes set to zeros is used.
  34. */
  35. static uint8_t zero_value_bytes[S2N_MAX_HASHLEN] = { 0 };
  36. #define ZERO_VALUE(hmac_alg) ( \
  37. (const struct s2n_blob){ .data = zero_value_bytes, .size = s2n_get_hash_len(hmac_alg) })
  38. /**
  39. * When an operation doesn't need an actual transcript hash,
  40. * it uses an empty transcript hash as an input instead.
  41. *
  42. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  43. *# Note that in some cases a zero-
  44. *# length Context (indicated by "") is passed to HKDF-Expand-Label
  45. */
  46. #define EMPTY_CONTEXT(hmac_alg) ( \
  47. (const struct s2n_blob){ .data = s2n_get_empty_context(hmac_alg), .size = s2n_get_hash_len(hmac_alg) })
  48. static uint8_t s2n_get_hash_len(s2n_hmac_algorithm hmac_alg)
  49. {
  50. uint8_t hash_size = 0;
  51. if (s2n_hmac_digest_size(hmac_alg, &hash_size) != S2N_SUCCESS) {
  52. return 0;
  53. }
  54. return hash_size;
  55. }
  56. static uint8_t *s2n_get_empty_context(s2n_hmac_algorithm hmac_alg)
  57. {
  58. static uint8_t sha256_empty_digest[S2N_MAX_HASHLEN] = { 0 };
  59. static uint8_t sha384_empty_digest[S2N_MAX_HASHLEN] = { 0 };
  60. switch (hmac_alg) {
  61. case S2N_HMAC_SHA256:
  62. return sha256_empty_digest;
  63. case S2N_HMAC_SHA384:
  64. return sha384_empty_digest;
  65. default:
  66. return NULL;
  67. }
  68. }
  69. static s2n_hmac_algorithm supported_hmacs[] = {
  70. S2N_HMAC_SHA256,
  71. S2N_HMAC_SHA384
  72. };
  73. S2N_RESULT s2n_tls13_empty_transcripts_init()
  74. {
  75. DEFER_CLEANUP(struct s2n_hash_state hash = { 0 }, s2n_hash_free);
  76. RESULT_GUARD_POSIX(s2n_hash_new(&hash));
  77. s2n_hash_algorithm hash_alg = S2N_HASH_NONE;
  78. for (size_t i = 0; i < s2n_array_len(supported_hmacs); i++) {
  79. s2n_hmac_algorithm hmac_alg = supported_hmacs[i];
  80. struct s2n_blob digest = EMPTY_CONTEXT(hmac_alg);
  81. RESULT_GUARD_POSIX(s2n_hmac_hash_alg(hmac_alg, &hash_alg));
  82. RESULT_GUARD_POSIX(s2n_hash_init(&hash, hash_alg));
  83. RESULT_GUARD_POSIX(s2n_hash_digest(&hash, digest.data, digest.size));
  84. }
  85. return S2N_RESULT_OK;
  86. }
  87. static S2N_RESULT s2n_calculate_transcript_digest(struct s2n_connection *conn)
  88. {
  89. RESULT_ENSURE_REF(conn);
  90. RESULT_ENSURE_REF(conn->handshake.hashes);
  91. s2n_hash_algorithm hash_algorithm = S2N_HASH_NONE;
  92. RESULT_GUARD_POSIX(s2n_hmac_hash_alg(CONN_HMAC_ALG(conn), &hash_algorithm));
  93. uint8_t digest_size = 0;
  94. RESULT_GUARD_POSIX(s2n_hash_digest_size(hash_algorithm, &digest_size));
  95. struct s2n_blob digest = { 0 };
  96. RESULT_GUARD_POSIX(s2n_blob_init(&digest, CONN_HASHES(conn)->transcript_hash_digest, digest_size));
  97. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  98. RESULT_GUARD(s2n_handshake_copy_hash_state(conn, hash_algorithm, hash_state));
  99. RESULT_GUARD_POSIX(s2n_hash_digest(hash_state, digest.data, digest.size));
  100. return S2N_RESULT_OK;
  101. }
  102. static S2N_RESULT s2n_extract_secret(s2n_hmac_algorithm hmac_alg,
  103. const struct s2n_blob *previous_secret_material, const struct s2n_blob *new_secret_material,
  104. struct s2n_blob *output)
  105. {
  106. /*
  107. * TODO: We should be able to reuse the prf_work_space rather
  108. * than allocating a new HMAC every time.
  109. * https://github.com/aws/s2n-tls/issues/3206
  110. */
  111. DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free);
  112. RESULT_GUARD_POSIX(s2n_hmac_new(&hmac_state));
  113. RESULT_GUARD_POSIX(s2n_hkdf_extract(&hmac_state, hmac_alg,
  114. previous_secret_material, new_secret_material, output));
  115. return S2N_RESULT_OK;
  116. }
  117. /**
  118. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  119. *# Derive-Secret(Secret, Label, Messages) =
  120. *# HKDF-Expand-Label(Secret, Label,
  121. *# Transcript-Hash(Messages), Hash.length)
  122. */
  123. static S2N_RESULT s2n_derive_secret(s2n_hmac_algorithm hmac_alg,
  124. const struct s2n_blob *previous_secret_material, const struct s2n_blob *label, const struct s2n_blob *context,
  125. struct s2n_blob *output)
  126. {
  127. /*
  128. * TODO: We should be able to reuse the prf_work_space rather
  129. * than allocating a new HMAC every time.
  130. * https://github.com/aws/s2n-tls/issues/3206
  131. */
  132. DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free);
  133. RESULT_GUARD_POSIX(s2n_hmac_new(&hmac_state));
  134. output->size = s2n_get_hash_len(hmac_alg);
  135. RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac_state, hmac_alg,
  136. previous_secret_material, label, context, output));
  137. return S2N_RESULT_OK;
  138. }
  139. static S2N_RESULT s2n_derive_secret_with_context(struct s2n_connection *conn,
  140. s2n_extract_secret_type_t input_secret_type, const struct s2n_blob *label, message_type_t transcript_end_msg,
  141. struct s2n_blob *output)
  142. {
  143. RESULT_ENSURE_REF(conn);
  144. RESULT_ENSURE_REF(label);
  145. RESULT_ENSURE_REF(output);
  146. RESULT_ENSURE(conn->secrets.extract_secret_type == input_secret_type, S2N_ERR_SECRET_SCHEDULE_STATE);
  147. RESULT_ENSURE(s2n_conn_get_current_message_type(conn) == transcript_end_msg, S2N_ERR_SECRET_SCHEDULE_STATE);
  148. RESULT_GUARD(s2n_derive_secret(CONN_HMAC_ALG(conn), &CONN_SECRET(conn, extract_secret),
  149. label, &CONN_HASH(conn, transcript_hash_digest), output));
  150. return S2N_RESULT_OK;
  151. }
  152. static S2N_RESULT s2n_derive_secret_without_context(struct s2n_connection *conn,
  153. s2n_extract_secret_type_t input_secret_type, struct s2n_blob *output)
  154. {
  155. RESULT_ENSURE_REF(conn);
  156. RESULT_ENSURE_REF(output);
  157. RESULT_ENSURE(conn->secrets.extract_secret_type == input_secret_type, S2N_ERR_SECRET_SCHEDULE_STATE);
  158. RESULT_GUARD(s2n_derive_secret(CONN_HMAC_ALG(conn), &CONN_SECRET(conn, extract_secret),
  159. &s2n_tls13_label_derived_secret, &EMPTY_CONTEXT(CONN_HMAC_ALG(conn)), output));
  160. return S2N_RESULT_OK;
  161. }
  162. /**
  163. *= https://tools.ietf.org/rfc/rfc8446#section-4.4.4
  164. *# The key used to compute the Finished message is computed from the
  165. *# Base Key defined in Section 4.4 using HKDF (see Section 7.1).
  166. *# Specifically:
  167. *#
  168. *# finished_key =
  169. *# HKDF-Expand-Label(BaseKey, "finished", "", Hash.length)
  170. **/
  171. static S2N_RESULT s2n_tls13_compute_finished_key(struct s2n_connection *conn,
  172. const struct s2n_blob *base_key, struct s2n_blob *output)
  173. {
  174. RESULT_ENSURE_REF(conn);
  175. RESULT_ENSURE_REF(base_key);
  176. RESULT_ENSURE_REF(output);
  177. RESULT_GUARD(s2n_handshake_set_finished_len(conn, output->size));
  178. /*
  179. * TODO: We should be able to reuse the prf_work_space rather
  180. * than allocating a new HMAC every time.
  181. */
  182. DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free);
  183. RESULT_GUARD_POSIX(s2n_hmac_new(&hmac_state));
  184. RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac_state, CONN_HMAC_ALG(conn),
  185. base_key, &s2n_tls13_label_finished, &(struct s2n_blob){ 0 }, output));
  186. return S2N_RESULT_OK;
  187. }
  188. static S2N_RESULT s2n_call_secret_callbacks(struct s2n_connection *conn,
  189. const struct s2n_blob *secret, s2n_secret_type_t secret_type)
  190. {
  191. RESULT_ENSURE_REF(conn);
  192. RESULT_ENSURE_REF(secret);
  193. if (conn->secret_cb && (s2n_connection_is_quic_enabled(conn) || s2n_in_unit_test())) {
  194. RESULT_GUARD_POSIX(conn->secret_cb(conn->secret_cb_context, conn, secret_type,
  195. secret->data, secret->size));
  196. }
  197. s2n_result_ignore(s2n_key_log_tls13_secret(conn, secret, secret_type));
  198. return S2N_RESULT_OK;
  199. }
  200. static S2N_RESULT s2n_trigger_secret_callbacks(struct s2n_connection *conn,
  201. const struct s2n_blob *secret, s2n_extract_secret_type_t secret_type, s2n_mode mode)
  202. {
  203. RESULT_ENSURE_REF(conn);
  204. RESULT_ENSURE_REF(secret);
  205. static const s2n_secret_type_t conversions[][2] = {
  206. [S2N_EARLY_SECRET] = { S2N_CLIENT_EARLY_TRAFFIC_SECRET, S2N_CLIENT_EARLY_TRAFFIC_SECRET },
  207. [S2N_HANDSHAKE_SECRET] = { S2N_SERVER_HANDSHAKE_TRAFFIC_SECRET, S2N_CLIENT_HANDSHAKE_TRAFFIC_SECRET },
  208. [S2N_MASTER_SECRET] = { S2N_SERVER_APPLICATION_TRAFFIC_SECRET, S2N_CLIENT_APPLICATION_TRAFFIC_SECRET },
  209. };
  210. s2n_secret_type_t callback_secret_type = conversions[secret_type][mode];
  211. RESULT_GUARD(s2n_call_secret_callbacks(conn, secret, callback_secret_type));
  212. return S2N_RESULT_OK;
  213. }
  214. /**
  215. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  216. *# 0
  217. *# |
  218. *# v
  219. *# PSK -> HKDF-Extract = Early Secret
  220. *
  221. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  222. *# There are multiple potential Early Secret values, depending on which
  223. *# PSK the server ultimately selects. The client will need to compute
  224. *# one for each potential PSK
  225. */
  226. S2N_RESULT s2n_extract_early_secret(struct s2n_psk *psk)
  227. {
  228. RESULT_ENSURE_REF(psk);
  229. RESULT_GUARD_POSIX(s2n_realloc(&psk->early_secret, s2n_get_hash_len(psk->hmac_alg)));
  230. RESULT_GUARD(s2n_extract_secret(psk->hmac_alg,
  231. &ZERO_VALUE(psk->hmac_alg),
  232. &psk->secret,
  233. &psk->early_secret));
  234. return S2N_RESULT_OK;
  235. }
  236. /*
  237. * When we require an early secret to derive other secrets,
  238. * either retrieve the early secret stored on the chosen / early data PSK
  239. * or calculate one using a "zero" PSK.
  240. */
  241. static S2N_RESULT s2n_extract_early_secret_for_schedule(struct s2n_connection *conn)
  242. {
  243. RESULT_ENSURE_REF(conn);
  244. struct s2n_psk *psk = conn->psk_params.chosen_psk;
  245. s2n_hmac_algorithm hmac_alg = CONN_HMAC_ALG(conn);
  246. /*
  247. * If the client is sending early data, then the PSK is always assumed
  248. * to be the first PSK offered.
  249. */
  250. if (conn->mode == S2N_CLIENT && conn->early_data_state == S2N_EARLY_DATA_REQUESTED) {
  251. RESULT_GUARD(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &psk));
  252. RESULT_ENSURE_REF(psk);
  253. }
  254. /**
  255. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  256. *# if no PSK is selected, it will then need
  257. *# to compute the Early Secret corresponding to the zero PSK.
  258. */
  259. if (psk == NULL) {
  260. RESULT_GUARD(s2n_extract_secret(hmac_alg,
  261. &ZERO_VALUE(hmac_alg),
  262. &ZERO_VALUE(hmac_alg),
  263. &CONN_SECRET(conn, extract_secret)));
  264. return S2N_RESULT_OK;
  265. }
  266. /*
  267. * The early secret is required to generate or verify a PSK's binder,
  268. * so must have already been calculated if a valid PSK exists.
  269. * Use the early secret stored on the PSK.
  270. */
  271. RESULT_ENSURE_EQ(hmac_alg, psk->hmac_alg);
  272. RESULT_CHECKED_MEMCPY(CONN_SECRETS(conn).extract_secret, psk->early_secret.data, psk->early_secret.size);
  273. return S2N_RESULT_OK;
  274. }
  275. /**
  276. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  277. *# |
  278. *# +-----> Derive-Secret(., "ext binder" | "res binder", "")
  279. *# | = binder_key
  280. */
  281. S2N_RESULT s2n_derive_binder_key(struct s2n_psk *psk, struct s2n_blob *output)
  282. {
  283. RESULT_ENSURE_REF(psk);
  284. RESULT_ENSURE_REF(output);
  285. const struct s2n_blob *label = &s2n_tls13_label_resumption_psk_binder_key;
  286. if (psk->type == S2N_PSK_TYPE_EXTERNAL) {
  287. label = &s2n_tls13_label_external_psk_binder_key;
  288. }
  289. RESULT_GUARD(s2n_extract_early_secret(psk));
  290. RESULT_GUARD(s2n_derive_secret(psk->hmac_alg,
  291. &psk->early_secret,
  292. label,
  293. &EMPTY_CONTEXT(psk->hmac_alg),
  294. output));
  295. return S2N_RESULT_OK;
  296. }
  297. /**
  298. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  299. *# |
  300. *# +-----> Derive-Secret(., "c e traffic", ClientHello)
  301. *# | = client_early_traffic_secret
  302. */
  303. static S2N_RESULT s2n_derive_client_early_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output)
  304. {
  305. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  306. S2N_EARLY_SECRET,
  307. &s2n_tls13_label_client_early_traffic_secret,
  308. CLIENT_HELLO,
  309. output));
  310. return S2N_RESULT_OK;
  311. }
  312. /**
  313. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  314. *# |
  315. *# v
  316. *# Derive-Secret(., "derived", "")
  317. *# |
  318. *# v
  319. *# (EC)DHE -> HKDF-Extract = Handshake Secret
  320. */
  321. static S2N_RESULT s2n_extract_handshake_secret(struct s2n_connection *conn)
  322. {
  323. RESULT_ENSURE_REF(conn);
  324. struct s2n_blob derived_secret = { 0 };
  325. uint8_t derived_secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
  326. RESULT_GUARD_POSIX(s2n_blob_init(&derived_secret, derived_secret_bytes, S2N_TLS13_SECRET_MAX_LEN));
  327. RESULT_GUARD(s2n_derive_secret_without_context(conn, S2N_EARLY_SECRET, &derived_secret));
  328. DEFER_CLEANUP(struct s2n_blob shared_secret = { 0 }, s2n_free_or_wipe);
  329. RESULT_GUARD_POSIX(s2n_tls13_compute_shared_secret(conn, &shared_secret));
  330. RESULT_GUARD(s2n_extract_secret(CONN_HMAC_ALG(conn),
  331. &derived_secret,
  332. &shared_secret,
  333. &CONN_SECRET(conn, extract_secret)));
  334. return S2N_RESULT_OK;
  335. }
  336. /**
  337. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  338. *# |
  339. *# +-----> Derive-Secret(., "c hs traffic",
  340. *# | ClientHello...ServerHello)
  341. *# | = client_handshake_traffic_secret
  342. */
  343. static S2N_RESULT s2n_derive_client_handshake_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output)
  344. {
  345. RESULT_ENSURE_REF(conn);
  346. RESULT_ENSURE_REF(output);
  347. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  348. S2N_HANDSHAKE_SECRET,
  349. &s2n_tls13_label_client_handshake_traffic_secret,
  350. SERVER_HELLO,
  351. output));
  352. /*
  353. * The client finished key needs to be calculated using the
  354. * same connection state as the client handshake secret.
  355. *
  356. *= https://tools.ietf.org/rfc/rfc8446#section-4.4.4
  357. *# The key used to compute the Finished message is computed from the
  358. *# Base Key defined in Section 4.4 using HKDF (see Section 7.1).
  359. */
  360. RESULT_GUARD(s2n_tls13_compute_finished_key(conn,
  361. output, &CONN_FINISHED(conn, client)));
  362. return S2N_RESULT_OK;
  363. }
  364. /**
  365. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  366. *# |
  367. *# +-----> Derive-Secret(., "s hs traffic",
  368. *# | ClientHello...ServerHello)
  369. *# | = server_handshake_traffic_secret
  370. */
  371. static S2N_RESULT s2n_derive_server_handshake_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output)
  372. {
  373. RESULT_ENSURE_REF(conn);
  374. RESULT_ENSURE_REF(output);
  375. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  376. S2N_HANDSHAKE_SECRET,
  377. &s2n_tls13_label_server_handshake_traffic_secret,
  378. SERVER_HELLO,
  379. output));
  380. /*
  381. * The server finished key needs to be calculated using the
  382. * same connection state as the server handshake secret.
  383. *
  384. *= https://tools.ietf.org/rfc/rfc8446#section-4.4.4
  385. *# The key used to compute the Finished message is computed from the
  386. *# Base Key defined in Section 4.4 using HKDF (see Section 7.1).
  387. */
  388. RESULT_GUARD(s2n_tls13_compute_finished_key(conn,
  389. output, &CONN_FINISHED(conn, server)));
  390. return S2N_RESULT_OK;
  391. }
  392. /**
  393. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  394. *# v
  395. *# Derive-Secret(., "derived", "")
  396. *# |
  397. *# v
  398. *# 0 -> HKDF-Extract = Master Secret
  399. */
  400. static S2N_RESULT s2n_extract_master_secret(struct s2n_connection *conn)
  401. {
  402. RESULT_ENSURE_REF(conn);
  403. struct s2n_blob derived_secret = { 0 };
  404. uint8_t derived_secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
  405. RESULT_GUARD_POSIX(s2n_blob_init(&derived_secret, derived_secret_bytes, S2N_TLS13_SECRET_MAX_LEN));
  406. RESULT_GUARD(s2n_derive_secret_without_context(conn, S2N_HANDSHAKE_SECRET, &derived_secret));
  407. RESULT_GUARD(s2n_extract_secret(CONN_HMAC_ALG(conn),
  408. &derived_secret,
  409. &ZERO_VALUE(CONN_HMAC_ALG(conn)),
  410. &CONN_SECRET(conn, extract_secret)));
  411. return S2N_RESULT_OK;
  412. }
  413. /**
  414. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  415. *# |
  416. *# +-----> Derive-Secret(., "c ap traffic",
  417. *# | ClientHello...server Finished)
  418. *# | = client_application_traffic_secret_0
  419. */
  420. static S2N_RESULT s2n_derive_client_application_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output)
  421. {
  422. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  423. S2N_MASTER_SECRET,
  424. &s2n_tls13_label_client_application_traffic_secret,
  425. SERVER_FINISHED,
  426. output));
  427. return S2N_RESULT_OK;
  428. }
  429. /**
  430. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  431. *# |
  432. *# +-----> Derive-Secret(., "s ap traffic",
  433. *# | ClientHello...server Finished)
  434. *# | = server_application_traffic_secret_0
  435. */
  436. static S2N_RESULT s2n_derive_server_application_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output)
  437. {
  438. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  439. S2N_MASTER_SECRET,
  440. &s2n_tls13_label_server_application_traffic_secret,
  441. SERVER_FINISHED,
  442. output));
  443. return S2N_RESULT_OK;
  444. }
  445. /**
  446. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  447. *# |
  448. *# +-----> Derive-Secret(., "res master",
  449. *# ClientHello...client Finished)
  450. *# = resumption_master_secret
  451. */
  452. S2N_RESULT s2n_derive_resumption_master_secret(struct s2n_connection *conn)
  453. {
  454. RESULT_ENSURE_REF(conn);
  455. /* Secret derivation requires these fields to be non-null. */
  456. RESULT_ENSURE_REF(conn->secure);
  457. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  458. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  459. S2N_MASTER_SECRET,
  460. &s2n_tls13_label_resumption_master_secret,
  461. CLIENT_FINISHED,
  462. &CONN_SECRET(conn, resumption_master_secret)));
  463. return S2N_RESULT_OK;
  464. }
  465. /**
  466. *= https://tools.ietf.org/rfc/rfc8446#section-7.1
  467. *# |
  468. *# +-----> Derive-Secret(., "exp master",
  469. *# | ClientHello...server Finished)
  470. *# | = exporter_master_secret
  471. */
  472. S2N_RESULT s2n_derive_exporter_master_secret(struct s2n_connection *conn, struct s2n_blob *secret)
  473. {
  474. RESULT_ENSURE_REF(conn);
  475. /* Secret derivation requires these fields to be non-null. */
  476. RESULT_ENSURE_REF(conn->secure);
  477. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  478. RESULT_GUARD(s2n_derive_secret_with_context(conn,
  479. S2N_MASTER_SECRET,
  480. &s2n_tls13_label_exporter_master_secret,
  481. SERVER_FINISHED,
  482. secret));
  483. RESULT_GUARD(s2n_call_secret_callbacks(conn, secret, S2N_EXPORTER_SECRET));
  484. return S2N_RESULT_OK;
  485. }
  486. static s2n_result (*extract_methods[])(struct s2n_connection *conn) = {
  487. [S2N_EARLY_SECRET] = &s2n_extract_early_secret_for_schedule,
  488. [S2N_HANDSHAKE_SECRET] = &s2n_extract_handshake_secret,
  489. [S2N_MASTER_SECRET] = &s2n_extract_master_secret,
  490. };
  491. S2N_RESULT s2n_tls13_extract_secret(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type)
  492. {
  493. RESULT_ENSURE_REF(conn);
  494. RESULT_ENSURE_REF(conn->secure);
  495. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  496. RESULT_ENSURE_REF(conn->handshake.hashes);
  497. RESULT_ENSURE_NE(secret_type, S2N_NONE_SECRET);
  498. RESULT_ENSURE_GTE(secret_type, 0);
  499. RESULT_ENSURE_LT(secret_type, s2n_array_len(extract_methods));
  500. s2n_extract_secret_type_t next_secret_type = conn->secrets.extract_secret_type + 1;
  501. for (s2n_extract_secret_type_t i = next_secret_type; i <= secret_type; i++) {
  502. RESULT_ENSURE_REF(extract_methods[i]);
  503. RESULT_GUARD(extract_methods[i](conn));
  504. conn->secrets.extract_secret_type = i;
  505. }
  506. return S2N_RESULT_OK;
  507. }
  508. static s2n_result (*derive_methods[][2])(struct s2n_connection *conn, struct s2n_blob *secret) = {
  509. [S2N_EARLY_SECRET] = { &s2n_derive_client_early_traffic_secret, &s2n_derive_client_early_traffic_secret },
  510. [S2N_HANDSHAKE_SECRET] = { &s2n_derive_server_handshake_traffic_secret, &s2n_derive_client_handshake_traffic_secret },
  511. [S2N_MASTER_SECRET] = { &s2n_derive_server_application_traffic_secret, &s2n_derive_client_application_traffic_secret },
  512. };
  513. S2N_RESULT s2n_tls13_derive_secret(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type,
  514. s2n_mode mode, struct s2n_blob *secret)
  515. {
  516. RESULT_ENSURE_REF(conn);
  517. RESULT_ENSURE_REF(secret);
  518. RESULT_ENSURE_REF(conn->secure);
  519. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  520. RESULT_ENSURE_REF(conn->handshake.hashes);
  521. RESULT_ENSURE_NE(secret_type, S2N_NONE_SECRET);
  522. RESULT_GUARD(s2n_tls13_extract_secret(conn, secret_type));
  523. RESULT_ENSURE_GTE(secret_type, 0);
  524. RESULT_ENSURE_LT(secret_type, s2n_array_len(derive_methods));
  525. RESULT_ENSURE_REF(derive_methods[secret_type][mode]);
  526. RESULT_GUARD(derive_methods[secret_type][mode](conn, secret));
  527. RESULT_GUARD(s2n_trigger_secret_callbacks(conn, secret, secret_type, mode));
  528. return S2N_RESULT_OK;
  529. }
  530. S2N_RESULT s2n_tls13_secrets_clean(struct s2n_connection *conn)
  531. {
  532. RESULT_ENSURE_REF(conn);
  533. /* Secret clean requires these fields to be non-null. */
  534. RESULT_ENSURE_REF(conn->secure);
  535. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  536. if (conn->actual_protocol_version < S2N_TLS13) {
  537. return S2N_RESULT_OK;
  538. }
  539. /*
  540. * Wipe base secrets.
  541. * Not strictly necessary, but probably safer than leaving them.
  542. * A compromised secret additionally compromises all secrets derived from it,
  543. * so these are the most sensitive secrets.
  544. */
  545. RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, extract_secret)));
  546. conn->secrets.extract_secret_type = S2N_NONE_SECRET;
  547. /* Wipe other secrets no longer needed */
  548. RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, client_early_secret)));
  549. RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, client_handshake_secret)));
  550. RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, server_handshake_secret)));
  551. return S2N_RESULT_OK;
  552. }
  553. S2N_RESULT s2n_tls13_secrets_update(struct s2n_connection *conn)
  554. {
  555. RESULT_ENSURE_REF(conn);
  556. if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
  557. return S2N_RESULT_OK;
  558. }
  559. /* Secret update requires these fields to be non-null. */
  560. RESULT_ENSURE_REF(conn->secure);
  561. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  562. message_type_t message_type = s2n_conn_get_current_message_type(conn);
  563. switch (message_type) {
  564. case CLIENT_HELLO:
  565. if (conn->early_data_state == S2N_EARLY_DATA_REQUESTED
  566. || conn->early_data_state == S2N_EARLY_DATA_ACCEPTED) {
  567. RESULT_GUARD(s2n_calculate_transcript_digest(conn));
  568. RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_EARLY_SECRET,
  569. S2N_CLIENT, &CONN_SECRET(conn, client_early_secret)));
  570. }
  571. break;
  572. case SERVER_HELLO:
  573. RESULT_GUARD(s2n_calculate_transcript_digest(conn));
  574. RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_HANDSHAKE_SECRET,
  575. S2N_CLIENT, &CONN_SECRET(conn, client_handshake_secret)));
  576. RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_HANDSHAKE_SECRET,
  577. S2N_SERVER, &CONN_SECRET(conn, server_handshake_secret)));
  578. break;
  579. case SERVER_FINISHED:
  580. RESULT_GUARD(s2n_calculate_transcript_digest(conn));
  581. RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_MASTER_SECRET,
  582. S2N_CLIENT, &CONN_SECRET(conn, client_app_secret)));
  583. RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_MASTER_SECRET,
  584. S2N_SERVER, &CONN_SECRET(conn, server_app_secret)));
  585. RESULT_GUARD(s2n_derive_exporter_master_secret(conn,
  586. &CONN_SECRET(conn, exporter_master_secret)));
  587. break;
  588. case CLIENT_FINISHED:
  589. RESULT_GUARD(s2n_calculate_transcript_digest(conn));
  590. RESULT_GUARD(s2n_derive_resumption_master_secret(conn));
  591. break;
  592. default:
  593. break;
  594. }
  595. return S2N_RESULT_OK;
  596. }
  597. S2N_RESULT s2n_tls13_secrets_get(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type,
  598. s2n_mode mode, struct s2n_blob *secret)
  599. {
  600. RESULT_ENSURE_REF(conn);
  601. RESULT_ENSURE_REF(secret);
  602. /* Getting secrets requires these fields to be non-null. */
  603. RESULT_ENSURE_REF(conn->secure);
  604. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  605. uint8_t *secrets[][2] = {
  606. [S2N_EARLY_SECRET] = { NULL, CONN_SECRETS(conn).client_early_secret },
  607. [S2N_HANDSHAKE_SECRET] = { CONN_SECRETS(conn).server_handshake_secret, CONN_SECRETS(conn).client_handshake_secret },
  608. [S2N_MASTER_SECRET] = { CONN_SECRETS(conn).server_app_secret, CONN_SECRETS(conn).client_app_secret },
  609. };
  610. RESULT_ENSURE_GT(secret_type, S2N_NONE_SECRET);
  611. RESULT_ENSURE_LT(secret_type, s2n_array_len(secrets));
  612. RESULT_ENSURE_LTE(secret_type, conn->secrets.extract_secret_type);
  613. RESULT_ENSURE_REF(secrets[secret_type][mode]);
  614. secret->size = s2n_get_hash_len(CONN_HMAC_ALG(conn));
  615. RESULT_CHECKED_MEMCPY(secret->data, secrets[secret_type][mode], secret->size);
  616. RESULT_ENSURE_GT(secret->size, 0);
  617. return S2N_RESULT_OK;
  618. }
  619. /*
  620. *= https://www.rfc-editor.org/rfc/rfc8446#section-7.5
  621. *# The exporter value is computed as:
  622. *#
  623. *# TLS-Exporter(label, context_value, key_length) =
  624. *# HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
  625. *# "exporter", Hash(context_value), key_length)
  626. */
  627. int s2n_connection_tls_exporter(struct s2n_connection *conn,
  628. const uint8_t *label_in, uint32_t label_length,
  629. const uint8_t *context, uint32_t context_length,
  630. uint8_t *output_in, uint32_t output_length)
  631. {
  632. POSIX_ENSURE_REF(conn);
  633. POSIX_ENSURE_REF(output_in);
  634. POSIX_ENSURE_REF(label_in);
  635. POSIX_ENSURE_REF(context);
  636. POSIX_ENSURE(s2n_connection_get_protocol_version(conn) == S2N_TLS13, S2N_ERR_INVALID_STATE);
  637. POSIX_ENSURE(is_handshake_complete(conn), S2N_ERR_HANDSHAKE_NOT_COMPLETE);
  638. POSIX_ENSURE_REF(conn->secure);
  639. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  640. s2n_hmac_algorithm hmac_alg = conn->secure->cipher_suite->prf_alg;
  641. uint8_t label_bytes[S2N_MAX_HKDF_EXPAND_LABEL_LENGTH] = { 0 };
  642. struct s2n_blob label = { 0 };
  643. POSIX_ENSURE_LTE(label_length, sizeof(label_bytes));
  644. POSIX_CHECKED_MEMCPY(label_bytes, label_in, label_length);
  645. POSIX_GUARD(s2n_blob_init(&label, label_bytes, label_length));
  646. uint8_t derived_secret_bytes[S2N_MAX_DIGEST_LEN] = { 0 };
  647. struct s2n_blob derived_secret = { 0 };
  648. POSIX_ENSURE_LTE(s2n_get_hash_len(CONN_HMAC_ALG(conn)), S2N_MAX_DIGEST_LEN);
  649. POSIX_GUARD(s2n_blob_init(&derived_secret,
  650. derived_secret_bytes, s2n_get_hash_len(CONN_HMAC_ALG(conn))));
  651. POSIX_GUARD_RESULT(s2n_derive_secret(hmac_alg, &CONN_SECRET(conn, exporter_master_secret),
  652. &label, &EMPTY_CONTEXT(hmac_alg), &derived_secret));
  653. DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free);
  654. POSIX_GUARD(s2n_hmac_new(&hmac_state));
  655. DEFER_CLEANUP(struct s2n_hash_state hash = { 0 }, s2n_hash_free);
  656. POSIX_GUARD(s2n_hash_new(&hash));
  657. s2n_hash_algorithm hash_alg = { 0 };
  658. POSIX_GUARD(s2n_hmac_hash_alg(hmac_alg, &hash_alg));
  659. uint8_t digest_bytes[S2N_MAX_DIGEST_LEN] = { 0 };
  660. struct s2n_blob digest = { 0 };
  661. POSIX_ENSURE_LTE(s2n_get_hash_len(CONN_HMAC_ALG(conn)), S2N_MAX_DIGEST_LEN);
  662. POSIX_GUARD(s2n_blob_init(&digest, digest_bytes, s2n_get_hash_len(CONN_HMAC_ALG(conn))));
  663. POSIX_GUARD(s2n_hash_init(&hash, hash_alg));
  664. POSIX_GUARD(s2n_hash_update(&hash, context, context_length));
  665. POSIX_GUARD(s2n_hash_digest(&hash, digest.data, digest.size));
  666. struct s2n_blob output = { 0 };
  667. POSIX_GUARD(s2n_blob_init(&output, output_in, output_length));
  668. POSIX_GUARD(s2n_hkdf_expand_label(&hmac_state, hmac_alg,
  669. &derived_secret, &s2n_tls13_label_exporter, &digest, &output));
  670. return S2N_SUCCESS;
  671. }