s2n_tls13_handshake.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "tls/s2n_cipher_suites.h"
  17. #include "tls/s2n_key_log.h"
  18. #include "tls/s2n_security_policies.h"
  19. static int s2n_zero_sequence_number(struct s2n_connection *conn, s2n_mode mode)
  20. {
  21. POSIX_ENSURE_REF(conn);
  22. POSIX_ENSURE_REF(conn->secure);
  23. struct s2n_blob sequence_number = { 0 };
  24. if (mode == S2N_CLIENT) {
  25. POSIX_GUARD(s2n_blob_init(&sequence_number, conn->secure->client_sequence_number, sizeof(conn->secure->client_sequence_number)));
  26. } else {
  27. POSIX_GUARD(s2n_blob_init(&sequence_number, conn->secure->server_sequence_number, sizeof(conn->secure->server_sequence_number)));
  28. }
  29. POSIX_GUARD(s2n_blob_zero(&sequence_number));
  30. return S2N_SUCCESS;
  31. }
  32. int s2n_tls13_mac_verify(struct s2n_tls13_keys *keys, struct s2n_blob *finished_verify, struct s2n_blob *wire_verify)
  33. {
  34. POSIX_ENSURE_REF(wire_verify->data);
  35. POSIX_ENSURE_EQ(wire_verify->size, keys->size);
  36. S2N_ERROR_IF(!s2n_constant_time_equals(finished_verify->data, wire_verify->data, keys->size), S2N_ERR_BAD_MESSAGE);
  37. return S2N_SUCCESS;
  38. }
  39. int s2n_tls13_keys_from_conn(struct s2n_tls13_keys *keys, struct s2n_connection *conn)
  40. {
  41. POSIX_GUARD(s2n_tls13_keys_init(keys, conn->secure->cipher_suite->prf_alg));
  42. return S2N_SUCCESS;
  43. }
  44. int s2n_tls13_compute_ecc_shared_secret(struct s2n_connection *conn, struct s2n_blob *shared_secret)
  45. {
  46. POSIX_ENSURE_REF(conn);
  47. const struct s2n_ecc_preferences *ecc_preferences = NULL;
  48. POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
  49. POSIX_ENSURE_REF(ecc_preferences);
  50. struct s2n_ecc_evp_params *server_key = &conn->kex_params.server_ecc_evp_params;
  51. POSIX_ENSURE_REF(server_key);
  52. POSIX_ENSURE_REF(server_key->negotiated_curve);
  53. struct s2n_ecc_evp_params *client_key = &conn->kex_params.client_ecc_evp_params;
  54. POSIX_ENSURE_REF(client_key);
  55. POSIX_ENSURE_REF(client_key->negotiated_curve);
  56. POSIX_ENSURE_EQ(server_key->negotiated_curve, client_key->negotiated_curve);
  57. if (conn->mode == S2N_CLIENT) {
  58. POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(client_key, server_key, shared_secret));
  59. } else {
  60. POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(server_key, client_key, shared_secret));
  61. }
  62. return S2N_SUCCESS;
  63. }
  64. /* Computes the ECDHE+PQKEM hybrid shared secret as defined in
  65. * https://tools.ietf.org/html/draft-stebila-tls-hybrid-design */
  66. int s2n_tls13_compute_pq_hybrid_shared_secret(struct s2n_connection *conn, struct s2n_blob *shared_secret)
  67. {
  68. POSIX_ENSURE_REF(conn);
  69. POSIX_ENSURE_REF(shared_secret);
  70. /* conn->kex_params.server_ecc_evp_params should be set only during a classic/non-hybrid handshake */
  71. POSIX_ENSURE_EQ(NULL, conn->kex_params.server_ecc_evp_params.negotiated_curve);
  72. POSIX_ENSURE_EQ(NULL, conn->kex_params.server_ecc_evp_params.evp_pkey);
  73. struct s2n_kem_group_params *server_kem_group_params = &conn->kex_params.server_kem_group_params;
  74. POSIX_ENSURE_REF(server_kem_group_params);
  75. struct s2n_ecc_evp_params *server_ecc_params = &server_kem_group_params->ecc_params;
  76. POSIX_ENSURE_REF(server_ecc_params);
  77. struct s2n_kem_group_params *client_kem_group_params = &conn->kex_params.client_kem_group_params;
  78. POSIX_ENSURE_REF(client_kem_group_params);
  79. struct s2n_ecc_evp_params *client_ecc_params = &client_kem_group_params->ecc_params;
  80. POSIX_ENSURE_REF(client_ecc_params);
  81. DEFER_CLEANUP(struct s2n_blob ecdhe_shared_secret = { 0 }, s2n_free_or_wipe);
  82. /* Compute the ECDHE shared secret, and retrieve the PQ shared secret. */
  83. if (conn->mode == S2N_CLIENT) {
  84. POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(client_ecc_params, server_ecc_params, &ecdhe_shared_secret));
  85. } else {
  86. POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_from_params(server_ecc_params, client_ecc_params, &ecdhe_shared_secret));
  87. }
  88. struct s2n_blob *pq_shared_secret = &client_kem_group_params->kem_params.shared_secret;
  89. POSIX_ENSURE_REF(pq_shared_secret);
  90. POSIX_ENSURE_REF(pq_shared_secret->data);
  91. const struct s2n_kem_group *negotiated_kem_group = conn->kex_params.server_kem_group_params.kem_group;
  92. POSIX_ENSURE_REF(negotiated_kem_group);
  93. POSIX_ENSURE_REF(negotiated_kem_group->kem);
  94. POSIX_ENSURE_EQ(pq_shared_secret->size, negotiated_kem_group->kem->shared_secret_key_length);
  95. /* Construct the concatenated/hybrid shared secret */
  96. uint32_t hybrid_shared_secret_size = ecdhe_shared_secret.size + negotiated_kem_group->kem->shared_secret_key_length;
  97. POSIX_GUARD(s2n_alloc(shared_secret, hybrid_shared_secret_size));
  98. struct s2n_stuffer stuffer_combiner = { 0 };
  99. POSIX_GUARD(s2n_stuffer_init(&stuffer_combiner, shared_secret));
  100. POSIX_GUARD(s2n_stuffer_write(&stuffer_combiner, &ecdhe_shared_secret));
  101. POSIX_GUARD(s2n_stuffer_write(&stuffer_combiner, pq_shared_secret));
  102. return S2N_SUCCESS;
  103. }
  104. static int s2n_tls13_pq_hybrid_supported(struct s2n_connection *conn)
  105. {
  106. return conn->kex_params.server_kem_group_params.kem_group != NULL;
  107. }
  108. int s2n_tls13_compute_shared_secret(struct s2n_connection *conn, struct s2n_blob *shared_secret)
  109. {
  110. POSIX_ENSURE_REF(conn);
  111. if (s2n_tls13_pq_hybrid_supported(conn)) {
  112. POSIX_GUARD(s2n_tls13_compute_pq_hybrid_shared_secret(conn, shared_secret));
  113. } else {
  114. POSIX_GUARD(s2n_tls13_compute_ecc_shared_secret(conn, shared_secret));
  115. }
  116. POSIX_GUARD_RESULT(s2n_connection_wipe_all_keyshares(conn));
  117. /* It would make more sense to wipe the PSK secrets in s2n_tls13_handle_early_secret,
  118. * but at that point we don't know whether or not the server will request a HRR request
  119. * and we'll have to use the secrets again.
  120. *
  121. * Instead, wipe them here when we wipe all the other connection secrets. */
  122. POSIX_GUARD_RESULT(s2n_psk_parameters_wipe_secrets(&conn->psk_params));
  123. return S2N_SUCCESS;
  124. }
  125. int s2n_update_application_traffic_keys(struct s2n_connection *conn, s2n_mode mode, keyupdate_status status)
  126. {
  127. POSIX_ENSURE_REF(conn);
  128. POSIX_ENSURE_REF(conn->secure);
  129. POSIX_ENSURE_GTE(conn->actual_protocol_version, S2N_TLS13);
  130. /* get tls13 key context */
  131. s2n_tls13_connection_keys(keys, conn);
  132. struct s2n_session_key *old_key;
  133. struct s2n_blob old_app_secret = { 0 };
  134. struct s2n_blob app_iv = { 0 };
  135. if (mode == S2N_CLIENT) {
  136. old_key = &conn->secure->client_key;
  137. POSIX_GUARD(s2n_blob_init(&old_app_secret, conn->secrets.version.tls13.client_app_secret, keys.size));
  138. POSIX_GUARD(s2n_blob_init(&app_iv, conn->secure->client_implicit_iv, S2N_TLS13_FIXED_IV_LEN));
  139. } else {
  140. old_key = &conn->secure->server_key;
  141. POSIX_GUARD(s2n_blob_init(&old_app_secret, conn->secrets.version.tls13.server_app_secret, keys.size));
  142. POSIX_GUARD(s2n_blob_init(&app_iv, conn->secure->server_implicit_iv, S2N_TLS13_FIXED_IV_LEN));
  143. }
  144. /* Produce new application secret */
  145. s2n_stack_blob(app_secret_update, keys.size, S2N_TLS13_SECRET_MAX_LEN);
  146. /* Derives next generation of traffic secret */
  147. POSIX_GUARD(s2n_tls13_update_application_traffic_secret(&keys, &old_app_secret, &app_secret_update));
  148. s2n_tls13_key_blob(app_key, conn->secure->cipher_suite->record_alg->cipher->key_material_size);
  149. /* Derives next generation of traffic key */
  150. POSIX_GUARD(s2n_tls13_derive_traffic_keys(&keys, &app_secret_update, &app_key, &app_iv));
  151. if (status == RECEIVING) {
  152. POSIX_GUARD(conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(old_key, &app_key));
  153. } else {
  154. POSIX_GUARD(conn->secure->cipher_suite->record_alg->cipher->set_encryption_key(old_key, &app_key));
  155. }
  156. /* According to https://tools.ietf.org/html/rfc8446#section-5.3:
  157. * Each sequence number is set to zero at the beginning of a connection and
  158. * whenever the key is changed; the first record transmitted under a particular traffic key
  159. * MUST use sequence number 0.
  160. */
  161. POSIX_GUARD(s2n_zero_sequence_number(conn, mode));
  162. /* Save updated secret */
  163. struct s2n_stuffer old_secret_stuffer = { 0 };
  164. POSIX_GUARD(s2n_stuffer_init(&old_secret_stuffer, &old_app_secret));
  165. POSIX_GUARD(s2n_stuffer_write_bytes(&old_secret_stuffer, app_secret_update.data, keys.size));
  166. return S2N_SUCCESS;
  167. }