s2n_server_hello_retry.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <stdbool.h>
  16. #include "error/s2n_errno.h"
  17. #include "pq-crypto/s2n_pq.h"
  18. #include "tls/s2n_cipher_suites.h"
  19. #include "tls/s2n_server_extensions.h"
  20. #include "tls/s2n_tls.h"
  21. #include "tls/s2n_tls13.h"
  22. #include "tls/s2n_tls13_handshake.h"
  23. #include "utils/s2n_blob.h"
  24. #include "utils/s2n_safety.h"
  25. /* From RFC5246 7.4.1.2. */
  26. #define S2N_TLS_COMPRESSION_METHOD_NULL 0
  27. /* from RFC: https://tools.ietf.org/html/rfc8446#section-4.1.3*/
  28. uint8_t hello_retry_req_random[S2N_TLS_RANDOM_DATA_LEN] = {
  29. 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
  30. 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C
  31. };
  32. int s2n_server_hello_retry_send(struct s2n_connection *conn)
  33. {
  34. POSIX_ENSURE_REF(conn);
  35. POSIX_CHECKED_MEMCPY(conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN);
  36. POSIX_GUARD(s2n_server_hello_write_message(conn));
  37. /* Write the extensions */
  38. POSIX_GUARD(s2n_server_extensions_send(conn, &conn->handshake.io));
  39. /* Update transcript */
  40. POSIX_GUARD(s2n_server_hello_retry_recreate_transcript(conn));
  41. /* Reset handshake values */
  42. conn->handshake.client_hello_received = 0;
  43. conn->client_hello.parsed = 0;
  44. POSIX_CHECKED_MEMSET((uint8_t *) conn->extension_requests_received, 0, sizeof(s2n_extension_bitfield));
  45. return 0;
  46. }
  47. int s2n_server_hello_retry_recv(struct s2n_connection *conn)
  48. {
  49. POSIX_ENSURE_REF(conn);
  50. POSIX_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_INVALID_HELLO_RETRY);
  51. const struct s2n_ecc_preferences *ecc_pref = NULL;
  52. POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
  53. POSIX_ENSURE_REF(ecc_pref);
  54. const struct s2n_kem_preferences *kem_pref = NULL;
  55. POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
  56. POSIX_ENSURE_REF(kem_pref);
  57. const struct s2n_ecc_named_curve *named_curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
  58. const struct s2n_kem_group *kem_group = conn->kex_params.server_kem_group_params.kem_group;
  59. /* Boolean XOR check: exactly one of {named_curve, kem_group} should be non-null. */
  60. POSIX_ENSURE((named_curve != NULL) != (kem_group != NULL), S2N_ERR_INVALID_HELLO_RETRY);
  61. /**
  62. *= https://tools.ietf.org/rfc/rfc8446#4.2.8
  63. *# Upon receipt of this extension in a HelloRetryRequest, the client
  64. *# MUST verify that (1) the selected_group field corresponds to a group
  65. *# which was provided in the "supported_groups" extension in the
  66. *# original ClientHello
  67. **/
  68. bool selected_group_in_supported_groups = false;
  69. if (named_curve != NULL && s2n_ecc_preferences_includes_curve(ecc_pref, named_curve->iana_id)) {
  70. selected_group_in_supported_groups = true;
  71. }
  72. if (kem_group != NULL && s2n_kem_preferences_includes_tls13_kem_group(kem_pref, kem_group->iana_id)) {
  73. selected_group_in_supported_groups = true;
  74. }
  75. /**
  76. *= https://tools.ietf.org/rfc/rfc8446#4.2.8
  77. *# and (2) the selected_group field does not
  78. *# correspond to a group which was provided in the "key_share" extension
  79. *# in the original ClientHello.
  80. **/
  81. bool new_key_share_requested = false;
  82. if (named_curve != NULL) {
  83. new_key_share_requested = (named_curve != conn->kex_params.client_ecc_evp_params.negotiated_curve);
  84. }
  85. if (kem_group != NULL) {
  86. /* If PQ is disabled, the client should not have sent any PQ IDs
  87. * in the supported_groups list of the initial ClientHello */
  88. POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
  89. new_key_share_requested = (kem_group != conn->kex_params.client_kem_group_params.kem_group);
  90. }
  91. /**
  92. *= https://tools.ietf.org/rfc/rfc8446#4.2.8
  93. *# If either of these checks fails, then
  94. *# the client MUST abort the handshake with an "illegal_parameter"
  95. *# alert.
  96. *
  97. *= https://tools.ietf.org/rfc/rfc8446#section-4.1.4
  98. *# Clients MUST abort the handshake with an
  99. *# "illegal_parameter" alert if the HelloRetryRequest would not result
  100. *# in any change in the ClientHello.
  101. **/
  102. POSIX_ENSURE(new_key_share_requested, S2N_ERR_INVALID_HELLO_RETRY);
  103. POSIX_ENSURE(selected_group_in_supported_groups, S2N_ERR_INVALID_HELLO_RETRY);
  104. /* Update transcript hash */
  105. POSIX_GUARD(s2n_server_hello_retry_recreate_transcript(conn));
  106. /* Reset handshake values */
  107. POSIX_CHECKED_MEMSET((uint8_t *) conn->extension_requests_sent, 0, sizeof(s2n_extension_bitfield));
  108. return S2N_SUCCESS;
  109. }