| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_hmac.h" |
| 17 |
#include "s2n_test.h" |
| 18 |
#include "testlib/s2n_testlib.h" |
| 19 |
#include "tls/extensions/s2n_server_psk.h" |
| 20 |
#include "tls/s2n_tls.h" |
| 21 |
#include "utils/s2n_bitmap.h" |
| 22 |
|
| 23 |
#define TEST_PSK_WIRE_INDEX 1 |
| 24 |
#define TEST_PSK_HMAC S2N_HMAC_SHA384 |
| 25 |
|
| 26 |
uint8_t test_identity[] = "test identity"; |
| 27 |
uint8_t test_secret[] = "test secret"; |
| 28 |
|
| 29 |
static s2n_result setup_client_psks(struct s2n_connection *client_conn) |
| 30 |
{ |
| 31 |
RESULT_ENSURE_REF(client_conn); |
| 32 |
|
| 33 |
|
| 34 |
uint8_t other_client_data[] = "other client data"; |
| 35 |
struct s2n_psk *other_client_psk = NULL; |
| 36 |
RESULT_GUARD(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &other_client_psk)); |
| 37 |
RESULT_GUARD(s2n_psk_init(other_client_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 38 |
RESULT_GUARD_POSIX(s2n_psk_set_identity(other_client_psk, other_client_data, sizeof(other_client_data))); |
| 39 |
RESULT_GUARD_POSIX(s2n_psk_set_secret(other_client_psk, other_client_data, sizeof(other_client_data))); |
| 40 |
other_client_psk->hmac_alg = S2N_HMAC_SHA256; |
| 41 |
|
| 42 |
|
| 43 |
struct s2n_psk *shared_psk = NULL; |
| 44 |
RESULT_GUARD(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 45 |
RESULT_GUARD(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 46 |
RESULT_GUARD_POSIX(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity))); |
| 47 |
RESULT_GUARD_POSIX(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret))); |
| 48 |
shared_psk->hmac_alg = TEST_PSK_HMAC; |
| 49 |
|
| 50 |
return S2N_RESULT_OK; |
| 51 |
} |
| 52 |
|
| 53 |
static s2n_result setup_server_psks(struct s2n_connection *server_conn) |
| 54 |
{ |
| 55 |
RESULT_ENSURE_REF(server_conn); |
| 56 |
|
| 57 |
EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL)); |
| 58 |
|
| 59 |
|
| 60 |
struct s2n_psk *shared_psk = NULL; |
| 61 |
RESULT_GUARD(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 62 |
RESULT_GUARD(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 63 |
RESULT_GUARD_POSIX(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity))); |
| 64 |
RESULT_GUARD_POSIX(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret))); |
| 65 |
shared_psk->hmac_alg = TEST_PSK_HMAC; |
| 66 |
|
| 67 |
|
| 68 |
uint8_t other_server_data[] = "other server data"; |
| 69 |
struct s2n_psk *other_server_psk = NULL; |
| 70 |
RESULT_GUARD(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &other_server_psk)); |
| 71 |
RESULT_GUARD(s2n_psk_init(other_server_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 72 |
RESULT_GUARD_POSIX(s2n_psk_set_identity(other_server_psk, other_server_data, sizeof(other_server_data))); |
| 73 |
RESULT_GUARD_POSIX(s2n_psk_set_secret(other_server_psk, other_server_data, sizeof(other_server_data))); |
| 74 |
other_server_psk->hmac_alg = S2N_HMAC_SHA224; |
| 75 |
|
| 76 |
return S2N_RESULT_OK; |
| 77 |
} |
| 78 |
|
| 79 |
int main(int argc, char **argv) |
| 80 |
{ |
| 81 |
BEGIN_TEST(); |
| 82 |
|
| 83 |
|
| 84 |
{ |
| 85 |
struct s2n_psk *psk = NULL; |
| 86 |
|
| 87 |
struct s2n_connection *conn = NULL; |
| 88 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 89 |
|
| 90 |
EXPECT_FALSE(s2n_server_psk_extension.should_send(NULL)); |
| 91 |
|
| 92 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 93 |
EXPECT_FALSE(s2n_server_psk_extension.should_send(conn)); |
| 94 |
|
| 95 |
conn->psk_params.chosen_psk_wire_index = 0; |
| 96 |
EXPECT_OK(s2n_array_get(&conn->psk_params.psk_list, conn->psk_params.chosen_psk_wire_index, |
| 97 |
(void **) &conn->psk_params.chosen_psk)); |
| 98 |
EXPECT_TRUE(s2n_server_psk_extension.should_send(conn)); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(conn)); |
| 104 |
conn->actual_protocol_version = S2N_TLS12; |
| 105 |
EXPECT_SUCCESS(s2n_extension_send(&s2n_server_psk_extension, conn, NULL)); |
| 106 |
conn->actual_protocol_version = S2N_TLS13; |
| 107 |
EXPECT_FAILURE(s2n_extension_send(&s2n_server_psk_extension, conn, NULL)); |
| 108 |
|
| 109 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
{ |
| 114 |
|
| 115 |
{ |
| 116 |
struct s2n_stuffer out = { 0 }; |
| 117 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 118 |
|
| 119 |
struct s2n_connection *server_conn = NULL; |
| 120 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 121 |
|
| 122 |
EXPECT_OK(setup_server_psks(server_conn)); |
| 123 |
|
| 124 |
server_conn->psk_params.chosen_psk_wire_index = TEST_PSK_WIRE_INDEX; |
| 125 |
EXPECT_SUCCESS(s2n_server_psk_extension.send(server_conn, &out)); |
| 126 |
|
| 127 |
uint16_t chosen_psk_wire_index = 0; |
| 128 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &chosen_psk_wire_index)); |
| 129 |
EXPECT_EQUAL(chosen_psk_wire_index, server_conn->psk_params.chosen_psk_wire_index); |
| 130 |
|
| 131 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 132 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 133 |
}; |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
{ |
| 138 |
s2n_extension_type_id key_share_ext_id = 0; |
| 139 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_ext_id)); |
| 140 |
|
| 141 |
|
| 142 |
{ |
| 143 |
struct s2n_stuffer out = { 0 }; |
| 144 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 145 |
|
| 146 |
struct s2n_connection *conn = NULL; |
| 147 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 148 |
|
| 149 |
EXPECT_OK(setup_client_psks(conn)); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
S2N_CBIT_SET(conn->extension_requests_received, key_share_ext_id); |
| 155 |
|
| 156 |
uint16_t chosen_psk_wire_index = 1; |
| 157 |
|
| 158 |
conn->actual_protocol_version = S2N_TLS12; |
| 159 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&out, chosen_psk_wire_index)); |
| 160 |
|
| 161 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 162 |
EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(conn)); |
| 163 |
EXPECT_SUCCESS(s2n_extension_recv(&s2n_server_psk_extension, conn, &out)); |
| 164 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 165 |
|
| 166 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 167 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
{ |
| 172 |
struct s2n_stuffer out = { 0 }; |
| 173 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 174 |
|
| 175 |
struct s2n_connection *conn = NULL; |
| 176 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 177 |
|
| 178 |
EXPECT_OK(setup_client_psks(conn)); |
| 179 |
|
| 180 |
uint16_t chosen_psk_wire_index = TEST_PSK_WIRE_INDEX; |
| 181 |
conn->actual_protocol_version = S2N_TLS13; |
| 182 |
|
| 183 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&out, chosen_psk_wire_index)); |
| 184 |
|
| 185 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 186 |
EXPECT_FAILURE_WITH_ERRNO(s2n_server_psk_extension.recv(conn, &out), S2N_ERR_MISSING_EXTENSION); |
| 187 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 188 |
|
| 189 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 190 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
{ |
| 195 |
struct s2n_stuffer out = { 0 }; |
| 196 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 197 |
|
| 198 |
struct s2n_connection *conn = NULL; |
| 199 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 200 |
|
| 201 |
EXPECT_OK(setup_client_psks(conn)); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
S2N_CBIT_SET(conn->extension_responses_received, key_share_ext_id); |
| 207 |
|
| 208 |
|
| 209 |
uint16_t chosen_psk_wire_index = 10; |
| 210 |
conn->actual_protocol_version = S2N_TLS13; |
| 211 |
|
| 212 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&out, chosen_psk_wire_index)); |
| 213 |
|
| 214 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 215 |
EXPECT_FAILURE_WITH_ERRNO(s2n_server_psk_extension.recv(conn, &out), S2N_ERR_INVALID_ARGUMENT); |
| 216 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 217 |
|
| 218 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 219 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
{ |
| 224 |
struct s2n_stuffer out = { 0 }; |
| 225 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 226 |
|
| 227 |
struct s2n_connection *conn = NULL; |
| 228 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 229 |
|
| 230 |
EXPECT_OK(setup_client_psks(conn)); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
S2N_CBIT_SET(conn->extension_responses_received, key_share_ext_id); |
| 236 |
|
| 237 |
uint16_t chosen_psk_wire_index = TEST_PSK_WIRE_INDEX; |
| 238 |
conn->actual_protocol_version = S2N_TLS13; |
| 239 |
|
| 240 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&out, chosen_psk_wire_index)); |
| 241 |
|
| 242 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 243 |
EXPECT_SUCCESS(s2n_server_psk_extension.recv(conn, &out)); |
| 244 |
|
| 245 |
|
| 246 |
EXPECT_EQUAL(conn->psk_params.psk_ke_mode, S2N_PSK_DHE_KE); |
| 247 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, TEST_PSK_WIRE_INDEX); |
| 248 |
EXPECT_EQUAL(conn->psk_params.chosen_psk->identity.size, sizeof(test_identity)); |
| 249 |
EXPECT_BYTEARRAY_EQUAL(conn->psk_params.chosen_psk->identity.data, test_identity, sizeof(test_identity)); |
| 250 |
EXPECT_EQUAL(conn->psk_params.chosen_psk->secret.size, sizeof(test_secret)); |
| 251 |
EXPECT_BYTEARRAY_EQUAL(conn->psk_params.chosen_psk->secret.data, test_secret, sizeof(test_secret)); |
| 252 |
EXPECT_EQUAL(conn->psk_params.chosen_psk->hmac_alg, TEST_PSK_HMAC); |
| 253 |
|
| 254 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 255 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 256 |
}; |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
if (s2n_is_tls13_fully_supported()) { |
| 261 |
|
| 262 |
EXPECT_SUCCESS(s2n_enable_tls13_in_test()); |
| 263 |
struct s2n_connection *client_conn = NULL, *server_conn = NULL; |
| 264 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 265 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 266 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13")); |
| 267 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13")); |
| 268 |
|
| 269 |
EXPECT_OK(setup_client_psks(client_conn)); |
| 270 |
EXPECT_OK(setup_server_psks(server_conn)); |
| 271 |
|
| 272 |
EXPECT_SUCCESS(s2n_client_hello_send(client_conn)); |
| 273 |
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io, |
| 274 |
s2n_stuffer_data_available(&client_conn->handshake.io))); |
| 275 |
EXPECT_SUCCESS(s2n_client_hello_recv(server_conn)); |
| 276 |
|
| 277 |
|
| 278 |
EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, TEST_PSK_WIRE_INDEX); |
| 279 |
|
| 280 |
EXPECT_SUCCESS(s2n_server_hello_send(server_conn)); |
| 281 |
|
| 282 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io)); |
| 283 |
EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, |
| 284 |
s2n_stuffer_data_available(&server_conn->handshake.io))); |
| 285 |
|
| 286 |
EXPECT_NULL(client_conn->psk_params.chosen_psk); |
| 287 |
EXPECT_SUCCESS(s2n_server_hello_recv(client_conn)); |
| 288 |
|
| 289 |
|
| 290 |
EXPECT_EQUAL(client_conn->psk_params.psk_ke_mode, S2N_PSK_DHE_KE); |
| 291 |
EXPECT_EQUAL(client_conn->psk_params.chosen_psk_wire_index, TEST_PSK_WIRE_INDEX); |
| 292 |
EXPECT_EQUAL(client_conn->psk_params.chosen_psk->identity.size, sizeof(test_identity)); |
| 293 |
EXPECT_BYTEARRAY_EQUAL(client_conn->psk_params.chosen_psk->identity.data, test_identity, sizeof(test_identity)); |
| 294 |
EXPECT_EQUAL(client_conn->psk_params.chosen_psk->secret.size, sizeof(test_secret)); |
| 295 |
EXPECT_BYTEARRAY_EQUAL(client_conn->psk_params.chosen_psk->secret.data, test_secret, sizeof(test_secret)); |
| 296 |
EXPECT_EQUAL(client_conn->psk_params.chosen_psk->hmac_alg, TEST_PSK_HMAC); |
| 297 |
|
| 298 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 299 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 300 |
} |
| 301 |
|
| 302 |
END_TEST(); |
| 303 |
} |