s2n_key_update.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_key_update.h"
  16. #include "crypto/s2n_sequence.h"
  17. #include "error/s2n_errno.h"
  18. #include "tls/s2n_connection.h"
  19. #include "tls/s2n_record.h"
  20. #include "tls/s2n_tls.h"
  21. #include "tls/s2n_tls13_handshake.h"
  22. #include "utils/s2n_atomic.h"
  23. #include "utils/s2n_safety.h"
  24. static keyupdate_request key_update_request_val = S2N_KEY_UPDATE_NOT_REQUESTED;
  25. int s2n_key_update_write(struct s2n_blob *out);
  26. int s2n_check_record_limit(struct s2n_connection *conn, struct s2n_blob *sequence_number);
  27. S2N_RESULT s2n_set_key_update_request_for_testing(keyupdate_request request)
  28. {
  29. RESULT_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
  30. key_update_request_val = request;
  31. return S2N_RESULT_OK;
  32. }
  33. int s2n_key_update_recv(struct s2n_connection *conn, struct s2n_stuffer *request)
  34. {
  35. POSIX_ENSURE_REF(conn);
  36. POSIX_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_BAD_MESSAGE);
  37. POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_BAD_MESSAGE);
  38. uint8_t key_update_request;
  39. POSIX_GUARD(s2n_stuffer_read_uint8(request, &key_update_request));
  40. S2N_ERROR_IF(key_update_request != S2N_KEY_UPDATE_NOT_REQUESTED && key_update_request != S2N_KEY_UPDATE_REQUESTED,
  41. S2N_ERR_BAD_MESSAGE);
  42. if (key_update_request == S2N_KEY_UPDATE_REQUESTED) {
  43. s2n_atomic_flag_set(&conn->key_update_pending);
  44. }
  45. /* Update peer's key since a key_update was received */
  46. if (conn->mode == S2N_CLIENT) {
  47. POSIX_GUARD(s2n_update_application_traffic_keys(conn, S2N_SERVER, RECEIVING));
  48. } else {
  49. POSIX_GUARD(s2n_update_application_traffic_keys(conn, S2N_CLIENT, RECEIVING));
  50. }
  51. return S2N_SUCCESS;
  52. }
  53. int s2n_key_update_send(struct s2n_connection *conn, s2n_blocked_status *blocked)
  54. {
  55. POSIX_ENSURE_REF(conn);
  56. POSIX_ENSURE_REF(conn->secure);
  57. POSIX_ENSURE_GTE(conn->actual_protocol_version, S2N_TLS13);
  58. struct s2n_blob sequence_number = { 0 };
  59. if (conn->mode == S2N_CLIENT) {
  60. POSIX_GUARD(s2n_blob_init(&sequence_number, conn->secure->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
  61. } else {
  62. POSIX_GUARD(s2n_blob_init(&sequence_number, conn->secure->server_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
  63. }
  64. POSIX_GUARD(s2n_check_record_limit(conn, &sequence_number));
  65. if (s2n_atomic_flag_test(&conn->key_update_pending)) {
  66. /* Flush any buffered records to ensure an empty output buffer.
  67. *
  68. * This is important when buffering multiple records because we don't:
  69. * 1) Respect max fragment length for handshake messages
  70. * 2) Check if there is sufficient space in the output buffer for
  71. * post-handshake messages.
  72. */
  73. POSIX_GUARD(s2n_flush(conn, blocked));
  74. uint8_t key_update_data[S2N_KEY_UPDATE_MESSAGE_SIZE];
  75. struct s2n_blob key_update_blob = { 0 };
  76. POSIX_GUARD(s2n_blob_init(&key_update_blob, key_update_data, sizeof(key_update_data)));
  77. /* Write key update message */
  78. POSIX_GUARD(s2n_key_update_write(&key_update_blob));
  79. /* Encrypt the message */
  80. POSIX_GUARD_RESULT(s2n_record_write(conn, TLS_HANDSHAKE, &key_update_blob));
  81. /* Update encryption key */
  82. POSIX_GUARD(s2n_update_application_traffic_keys(conn, conn->mode, SENDING));
  83. s2n_atomic_flag_clear(&conn->key_update_pending);
  84. POSIX_GUARD(s2n_flush(conn, blocked));
  85. }
  86. return S2N_SUCCESS;
  87. }
  88. int s2n_key_update_write(struct s2n_blob *out)
  89. {
  90. POSIX_ENSURE_REF(out);
  91. struct s2n_stuffer key_update_stuffer = { 0 };
  92. POSIX_GUARD(s2n_stuffer_init(&key_update_stuffer, out));
  93. POSIX_GUARD(s2n_stuffer_write_uint8(&key_update_stuffer, TLS_KEY_UPDATE));
  94. POSIX_GUARD(s2n_stuffer_write_uint24(&key_update_stuffer, S2N_KEY_UPDATE_LENGTH));
  95. /* s2n currently does not require peers to update their encryption keys. */
  96. POSIX_GUARD(s2n_stuffer_write_uint8(&key_update_stuffer, key_update_request_val));
  97. return S2N_SUCCESS;
  98. }
  99. int s2n_check_record_limit(struct s2n_connection *conn, struct s2n_blob *sequence_number)
  100. {
  101. POSIX_ENSURE_REF(conn);
  102. POSIX_ENSURE_REF(sequence_number);
  103. POSIX_ENSURE_REF(conn->secure);
  104. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  105. POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg);
  106. /*
  107. * This is the sequence number that will be used for the next record,
  108. * because we incremented the sequence number after sending the last record.
  109. */
  110. uint64_t next_seq_num = 0;
  111. POSIX_GUARD(s2n_sequence_number_to_uint64(sequence_number, &next_seq_num));
  112. /*
  113. * If the next record is the last record we can send, then the next record needs
  114. * to contain a KeyUpdate message.
  115. *
  116. * This should always trigger on "==", but we use ">=" just in case.
  117. */
  118. if (next_seq_num >= conn->secure->cipher_suite->record_alg->encryption_limit) {
  119. s2n_atomic_flag_set(&conn->key_update_pending);
  120. }
  121. return S2N_SUCCESS;
  122. }