| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <stdbool.h> |
| 16 |
|
| 17 |
#include "crypto/s2n_pq.h" |
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "tls/s2n_cipher_suites.h" |
| 20 |
#include "tls/s2n_server_extensions.h" |
| 21 |
#include "tls/s2n_tls.h" |
| 22 |
#include "tls/s2n_tls13.h" |
| 23 |
#include "tls/s2n_tls13_handshake.h" |
| 24 |
#include "utils/s2n_blob.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
|
| 27 |
|
| 28 |
#define S2N_TLS_COMPRESSION_METHOD_NULL 0 |
| 29 |
|
| 30 |
|
| 31 |
uint8_t hello_retry_req_random[S2N_TLS_RANDOM_DATA_LEN] = { |
| 32 |
0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
| 33 |
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
| 34 |
}; |
| 35 |
|
| 36 |
int s2n_server_hello_retry_send(struct s2n_connection *conn) |
| 37 |
{ |
| 38 |
POSIX_ENSURE_REF(conn); |
| 39 |
|
| 40 |
POSIX_CHECKED_MEMCPY(conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN); |
| 41 |
|
| 42 |
POSIX_GUARD(s2n_server_hello_write_message(conn)); |
| 43 |
|
| 44 |
|
| 45 |
POSIX_GUARD(s2n_server_extensions_send(conn, &conn->handshake.io)); |
| 46 |
|
| 47 |
|
| 48 |
POSIX_GUARD(s2n_server_hello_retry_recreate_transcript(conn)); |
| 49 |
|
| 50 |
|
| 51 |
conn->handshake.client_hello_received = 0; |
| 52 |
conn->client_hello.parsed = 0; |
| 53 |
POSIX_CHECKED_MEMSET((uint8_t *) conn->extension_requests_received, 0, sizeof(s2n_extension_bitfield)); |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
int s2n_server_hello_retry_recv(struct s2n_connection *conn) |
| 59 |
{ |
| 60 |
POSIX_ENSURE_REF(conn); |
| 61 |
POSIX_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_INVALID_HELLO_RETRY); |
| 62 |
|
| 63 |
const struct s2n_ecc_preferences *ecc_pref = NULL; |
| 64 |
POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref)); |
| 65 |
POSIX_ENSURE_REF(ecc_pref); |
| 66 |
|
| 67 |
const struct s2n_kem_preferences *kem_pref = NULL; |
| 68 |
POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref)); |
| 69 |
POSIX_ENSURE_REF(kem_pref); |
| 70 |
|
| 71 |
const struct s2n_ecc_named_curve *named_curve = conn->kex_params.server_ecc_evp_params.negotiated_curve; |
| 72 |
const struct s2n_kem_group *server_preferred_kem_group = conn->kex_params.server_kem_group_params.kem_group; |
| 73 |
const struct s2n_kem_group *client_preferred_kem_group = conn->kex_params.client_kem_group_params.kem_group; |
| 74 |
|
| 75 |
|
| 76 |
POSIX_ENSURE((named_curve != NULL) != (server_preferred_kem_group != NULL), S2N_ERR_INVALID_HELLO_RETRY); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
bool selected_group_in_supported_groups = false; |
| 86 |
if (named_curve != NULL && s2n_ecc_preferences_includes_curve(ecc_pref, named_curve->iana_id)) { |
| 87 |
selected_group_in_supported_groups = true; |
| 88 |
} |
| 89 |
if (server_preferred_kem_group != NULL |
| 90 |
&& s2n_kem_group_is_available(server_preferred_kem_group) |
| 91 |
&& s2n_kem_preferences_includes_tls13_kem_group(kem_pref, server_preferred_kem_group->iana_id)) { |
| 92 |
selected_group_in_supported_groups = true; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
bool new_key_share_requested = false; |
| 102 |
|
| 103 |
if (named_curve != NULL) { |
| 104 |
new_key_share_requested = (named_curve != conn->kex_params.client_ecc_evp_params.negotiated_curve); |
| 105 |
} |
| 106 |
|
| 107 |
if (server_preferred_kem_group != NULL) { |
| 108 |
|
| 109 |
|
| 110 |
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_INVALID_HELLO_RETRY); |
| 111 |
new_key_share_requested = (server_preferred_kem_group != client_preferred_kem_group); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
POSIX_ENSURE(new_key_share_requested, S2N_ERR_INVALID_HELLO_RETRY); |
| 126 |
POSIX_ENSURE(selected_group_in_supported_groups, S2N_ERR_INVALID_HELLO_RETRY); |
| 127 |
|
| 128 |
|
| 129 |
POSIX_GUARD(s2n_server_hello_retry_recreate_transcript(conn)); |
| 130 |
|
| 131 |
|
| 132 |
POSIX_CHECKED_MEMSET((uint8_t *) conn->extension_requests_sent, 0, sizeof(s2n_extension_bitfield)); |
| 133 |
|
| 134 |
return S2N_SUCCESS; |
| 135 |
} |