s2n_tls13_key_schedule.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_handshake.h"
  16. #include "utils/s2n_result.h"
  17. /* The state machine refers to the "master" secret as the "application" secret.
  18. * Let's use that terminology here to match.
  19. */
  20. #define S2N_APPLICATION_SECRET S2N_MASTER_SECRET
  21. /**
  22. *= https://tools.ietf.org/rfc/rfc8446#appendix-A
  23. *# The notation "K_{send,recv} = foo" means "set
  24. *# the send/recv key to the given key".
  25. */
  26. #define K_send(conn, secret_type) RESULT_GUARD(s2n_set_key(conn, secret_type, (conn)->mode))
  27. #define K_recv(conn, secret_type) RESULT_GUARD(s2n_set_key(conn, secret_type, S2N_PEER_MODE((conn)->mode)))
  28. static const struct s2n_blob s2n_zero_length_context = { 0 };
  29. static S2N_RESULT s2n_zero_sequence_number(struct s2n_connection *conn, s2n_mode mode)
  30. {
  31. RESULT_ENSURE_REF(conn);
  32. RESULT_ENSURE_REF(conn->secure);
  33. struct s2n_blob sequence_number = { 0 };
  34. if (mode == S2N_CLIENT) {
  35. RESULT_GUARD_POSIX(s2n_blob_init(&sequence_number,
  36. conn->secure->client_sequence_number, sizeof(conn->secure->client_sequence_number)));
  37. } else {
  38. RESULT_GUARD_POSIX(s2n_blob_init(&sequence_number,
  39. conn->secure->server_sequence_number, sizeof(conn->secure->server_sequence_number)));
  40. }
  41. RESULT_GUARD_POSIX(s2n_blob_zero(&sequence_number));
  42. return S2N_RESULT_OK;
  43. }
  44. static S2N_RESULT s2n_set_key(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type, s2n_mode mode)
  45. {
  46. RESULT_ENSURE_REF(conn);
  47. RESULT_ENSURE_REF(conn->secure);
  48. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  49. const struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
  50. RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg);
  51. RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg->cipher);
  52. const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
  53. uint8_t *implicit_iv_data = NULL;
  54. struct s2n_session_key *session_key = NULL;
  55. if (mode == S2N_CLIENT) {
  56. implicit_iv_data = conn->secure->client_implicit_iv;
  57. session_key = &conn->secure->client_key;
  58. conn->client = conn->secure;
  59. } else {
  60. implicit_iv_data = conn->secure->server_implicit_iv;
  61. session_key = &conn->secure->server_key;
  62. conn->server = conn->secure;
  63. }
  64. /**
  65. *= https://tools.ietf.org/rfc/rfc8446#section-7.3
  66. *# The traffic keying material is generated from the following input
  67. *# values:
  68. *#
  69. *# - A secret value
  70. **/
  71. struct s2n_blob secret = { 0 };
  72. uint8_t secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
  73. RESULT_GUARD_POSIX(s2n_blob_init(&secret, secret_bytes, S2N_TLS13_SECRET_MAX_LEN));
  74. RESULT_GUARD(s2n_tls13_secrets_get(conn, secret_type, mode, &secret));
  75. /**
  76. *= https://tools.ietf.org/rfc/rfc8446#section-7.3
  77. *#
  78. *# - A purpose value indicating the specific value being generated
  79. **/
  80. const struct s2n_blob *key_purpose = &s2n_tls13_label_traffic_secret_key;
  81. const struct s2n_blob *iv_purpose = &s2n_tls13_label_traffic_secret_iv;
  82. /**
  83. *= https://tools.ietf.org/rfc/rfc8446#section-7.3
  84. *#
  85. *# - The length of the key being generated
  86. **/
  87. const uint32_t key_size = cipher->key_material_size;
  88. const uint32_t iv_size = S2N_TLS13_FIXED_IV_LEN;
  89. /*
  90. * TODO: We should be able to reuse the prf_work_space rather
  91. * than allocating a new HMAC every time.
  92. * https://github.com/aws/s2n-tls/issues/3206
  93. */
  94. s2n_hmac_algorithm hmac_alg = cipher_suite->prf_alg;
  95. DEFER_CLEANUP(struct s2n_hmac_state hmac = { 0 }, s2n_hmac_free);
  96. RESULT_GUARD_POSIX(s2n_hmac_new(&hmac));
  97. /**
  98. *= https://tools.ietf.org/rfc/rfc8446#section-7.3
  99. *#
  100. *# The traffic keying material is generated from an input traffic secret
  101. *# value using:
  102. *#
  103. *# [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
  104. **/
  105. struct s2n_blob key = { 0 };
  106. uint8_t key_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
  107. RESULT_GUARD_POSIX(s2n_blob_init(&key, key_bytes, key_size));
  108. RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac, hmac_alg,
  109. &secret, key_purpose, &s2n_zero_length_context, &key));
  110. /**
  111. *= https://tools.ietf.org/rfc/rfc8446#section-7.3
  112. *# [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)
  113. **/
  114. struct s2n_blob iv = { 0 };
  115. RESULT_GUARD_POSIX(s2n_blob_init(&iv, implicit_iv_data, iv_size));
  116. RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac, hmac_alg,
  117. &secret, iv_purpose, &s2n_zero_length_context, &iv));
  118. bool is_sending_secret = (mode == conn->mode);
  119. if (is_sending_secret) {
  120. RESULT_GUARD_POSIX(cipher->set_encryption_key(session_key, &key));
  121. } else {
  122. RESULT_GUARD_POSIX(cipher->set_decryption_key(session_key, &key));
  123. }
  124. /**
  125. *= https://tools.ietf.org/rfc/rfc8446#section-5.3
  126. *# Each sequence number is
  127. *# set to zero at the beginning of a connection and whenever the key is
  128. *# changed; the first record transmitted under a particular traffic key
  129. *# MUST use sequence number 0.
  130. */
  131. RESULT_GUARD(s2n_zero_sequence_number(conn, mode));
  132. return S2N_RESULT_OK;
  133. }
  134. static S2N_RESULT s2n_client_key_schedule(struct s2n_connection *conn)
  135. {
  136. RESULT_ENSURE_REF(conn);
  137. message_type_t message_type = s2n_conn_get_current_message_type(conn);
  138. /**
  139. * How client keys are set varies depending on early data state.
  140. *
  141. *= https://tools.ietf.org/rfc/rfc8446#appendix-A
  142. *# Actions which are taken only in certain circumstances
  143. *# are indicated in [].
  144. */
  145. /**
  146. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
  147. *# START <----+
  148. *# Send ClientHello | | Recv HelloRetryRequest
  149. *# [K_send = early data] | |
  150. */
  151. if (message_type == CLIENT_HELLO
  152. && conn->early_data_state == S2N_EARLY_DATA_REQUESTED) {
  153. K_send(conn, S2N_EARLY_SECRET);
  154. }
  155. /**
  156. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
  157. *# v |
  158. *# / WAIT_SH ----+
  159. *# | | Recv ServerHello
  160. *# | | K_recv = handshake
  161. */
  162. if (message_type == SERVER_HELLO) {
  163. K_recv(conn, S2N_HANDSHAKE_SECRET);
  164. }
  165. /**
  166. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
  167. *# Can | V
  168. *# send | WAIT_EE
  169. *# early | | Recv EncryptedExtensions
  170. *# data | +--------+--------+
  171. *# | Using | | Using certificate
  172. *# | PSK | v
  173. *# | | WAIT_CERT_CR
  174. *# | | Recv | | Recv CertificateRequest
  175. *# | | Certificate | v
  176. *# | | | WAIT_CERT
  177. *# | | | | Recv Certificate
  178. *# | | v v
  179. *# | | WAIT_CV
  180. *# | | | Recv CertificateVerify
  181. *# | +> WAIT_FINISHED <+
  182. *# | | Recv Finished
  183. *# \ | [Send EndOfEarlyData]
  184. *# | K_send = handshake
  185. */
  186. if ((message_type == SERVER_FINISHED && !WITH_EARLY_DATA(conn))
  187. || (message_type == END_OF_EARLY_DATA)) {
  188. K_send(conn, S2N_HANDSHAKE_SECRET);
  189. }
  190. /**
  191. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
  192. *# | [Send Certificate [+ CertificateVerify]]
  193. *# Can send | Send Finished
  194. *# app data --> | K_send = K_recv = application
  195. */
  196. if (message_type == CLIENT_FINISHED) {
  197. K_send(conn, S2N_APPLICATION_SECRET);
  198. K_recv(conn, S2N_APPLICATION_SECRET);
  199. }
  200. /**
  201. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
  202. *# after here v
  203. *# CONNECTED
  204. */
  205. return S2N_RESULT_OK;
  206. }
  207. static S2N_RESULT s2n_server_key_schedule(struct s2n_connection *conn)
  208. {
  209. RESULT_ENSURE_REF(conn);
  210. message_type_t message_type = s2n_conn_get_current_message_type(conn);
  211. /**
  212. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
  213. *# START <-----+
  214. *# Recv ClientHello | | Send HelloRetryRequest
  215. *# v |
  216. *# RECVD_CH ----+
  217. *# | Select parameters
  218. *# v
  219. *# NEGOTIATED
  220. *# | Send ServerHello
  221. *# | K_send = handshake
  222. */
  223. if (message_type == SERVER_HELLO) {
  224. K_send(conn, S2N_HANDSHAKE_SECRET);
  225. }
  226. /**
  227. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
  228. *# | Send EncryptedExtensions
  229. *# | [Send CertificateRequest]
  230. *# Can send | [Send Certificate + CertificateVerify]
  231. *# app data | Send Finished
  232. *# after --> | K_send = application
  233. */
  234. if (message_type == SERVER_FINISHED) {
  235. K_send(conn, S2N_APPLICATION_SECRET);
  236. /* clang-format off */
  237. /**
  238. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
  239. *# here +--------+--------+
  240. *# No 0-RTT | | 0-RTT
  241. *# | |
  242. *# K_recv = handshake | | K_recv = early data
  243. */
  244. /* clang-format on */
  245. if (WITH_EARLY_DATA(conn)) {
  246. K_recv(conn, S2N_EARLY_SECRET);
  247. } else {
  248. K_recv(conn, S2N_HANDSHAKE_SECRET);
  249. }
  250. }
  251. /**
  252. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
  253. *# [Skip decrypt errors] | +------> WAIT_EOED -+
  254. *# | | Recv | | Recv EndOfEarlyData
  255. *# | | early data | | K_recv = handshake
  256. *# | +------------+ |
  257. */
  258. if (message_type == END_OF_EARLY_DATA) {
  259. K_recv(conn, S2N_HANDSHAKE_SECRET);
  260. }
  261. /**
  262. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
  263. *# | |
  264. *# +> WAIT_FLIGHT2 <--------+
  265. *# |
  266. *# +--------+--------+
  267. *# No auth | | Client auth
  268. *# | |
  269. *# | v
  270. *# | WAIT_CERT
  271. *# | Recv | | Recv Certificate
  272. *# | empty | v
  273. *# | Certificate | WAIT_CV
  274. *# | | | Recv
  275. *# | v | CertificateVerify
  276. *# +-> WAIT_FINISHED <---+
  277. *# | Recv Finished
  278. *# | K_recv = application
  279. */
  280. if (message_type == CLIENT_FINISHED) {
  281. K_recv(conn, S2N_APPLICATION_SECRET);
  282. }
  283. /**
  284. *= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
  285. *# v
  286. *# CONNECTED
  287. */
  288. return S2N_RESULT_OK;
  289. }
  290. s2n_result (*key_schedules[])(struct s2n_connection *) = {
  291. [S2N_CLIENT] = &s2n_client_key_schedule,
  292. [S2N_SERVER] = &s2n_server_key_schedule,
  293. };
  294. S2N_RESULT s2n_tls13_key_schedule_update(struct s2n_connection *conn)
  295. {
  296. RESULT_ENSURE_REF(conn);
  297. if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
  298. return S2N_RESULT_OK;
  299. }
  300. RESULT_ENSURE_REF(key_schedules[conn->mode]);
  301. RESULT_GUARD(key_schedules[conn->mode](conn));
  302. return S2N_RESULT_OK;
  303. }
  304. S2N_RESULT s2n_tls13_key_schedule_reset(struct s2n_connection *conn)
  305. {
  306. RESULT_ENSURE_REF(conn);
  307. RESULT_ENSURE_REF(conn->initial);
  308. conn->client = conn->initial;
  309. conn->server = conn->initial;
  310. conn->secrets.extract_secret_type = S2N_NONE_SECRET;
  311. return S2N_RESULT_OK;
  312. }