| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_kex.h" |
| 17 |
|
| 18 |
#include "crypto/s2n_pq.h" |
| 19 |
#include "tls/s2n_cipher_preferences.h" |
| 20 |
#include "tls/s2n_cipher_suites.h" |
| 21 |
#include "tls/s2n_client_key_exchange.h" |
| 22 |
#include "tls/s2n_kem.h" |
| 23 |
#include "tls/s2n_security_policies.h" |
| 24 |
#include "tls/s2n_server_key_exchange.h" |
| 25 |
#include "tls/s2n_tls.h" |
| 26 |
#include "utils/s2n_safety.h" |
| 27 |
|
| 28 |
static S2N_RESULT s2n_check_tls13(const struct s2n_cipher_suite *cipher_suite, |
| 29 |
struct s2n_connection *conn, bool *is_supported) |
| 30 |
{ |
| 31 |
RESULT_ENSURE_REF(is_supported); |
| 32 |
*is_supported = (s2n_connection_get_protocol_version(conn) >= S2N_TLS13); |
| 33 |
return S2N_RESULT_OK; |
| 34 |
} |
| 35 |
|
| 36 |
static S2N_RESULT s2n_check_rsa_key(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn, bool *is_supported) |
| 37 |
{ |
| 38 |
RESULT_ENSURE_REF(cipher_suite); |
| 39 |
RESULT_ENSURE_REF(conn); |
| 40 |
RESULT_ENSURE_REF(is_supported); |
| 41 |
|
| 42 |
*is_supported = s2n_get_compatible_cert_chain_and_key(conn, S2N_PKEY_TYPE_RSA) != NULL; |
| 43 |
|
| 44 |
return S2N_RESULT_OK; |
| 45 |
} |
| 46 |
|
| 47 |
static S2N_RESULT s2n_check_dhe(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn, bool *is_supported) |
| 48 |
{ |
| 49 |
RESULT_ENSURE_REF(cipher_suite); |
| 50 |
RESULT_ENSURE_REF(conn); |
| 51 |
RESULT_ENSURE_REF(conn->config); |
| 52 |
RESULT_ENSURE_REF(is_supported); |
| 53 |
|
| 54 |
*is_supported = conn->config->dhparams != NULL; |
| 55 |
|
| 56 |
return S2N_RESULT_OK; |
| 57 |
} |
| 58 |
|
| 59 |
static S2N_RESULT s2n_check_ecdhe(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn, bool *is_supported) |
| 60 |
{ |
| 61 |
RESULT_ENSURE_REF(cipher_suite); |
| 62 |
RESULT_ENSURE_REF(conn); |
| 63 |
RESULT_ENSURE_REF(is_supported); |
| 64 |
|
| 65 |
*is_supported = conn->kex_params.server_ecc_evp_params.negotiated_curve != NULL; |
| 66 |
|
| 67 |
return S2N_RESULT_OK; |
| 68 |
} |
| 69 |
|
| 70 |
static S2N_RESULT s2n_check_kem(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn, bool *is_supported) |
| 71 |
{ |
| 72 |
RESULT_ENSURE_REF(cipher_suite); |
| 73 |
RESULT_ENSURE_REF(conn); |
| 74 |
RESULT_ENSURE_REF(is_supported); |
| 75 |
|
| 76 |
|
| 77 |
*is_supported = false; |
| 78 |
|
| 79 |
const struct s2n_kem_preferences *kem_preferences = NULL; |
| 80 |
RESULT_GUARD_POSIX(s2n_connection_get_kem_preferences(conn, &kem_preferences)); |
| 81 |
RESULT_ENSURE_REF(kem_preferences); |
| 82 |
|
| 83 |
if (!s2n_pq_is_enabled() || kem_preferences->kem_count == 0) { |
| 84 |
return S2N_RESULT_OK; |
| 85 |
} |
| 86 |
|
| 87 |
const struct s2n_iana_to_kem *supported_params = NULL; |
| 88 |
if (s2n_cipher_suite_to_kem(cipher_suite->iana_value, &supported_params) != S2N_SUCCESS) { |
| 89 |
return S2N_RESULT_OK; |
| 90 |
} |
| 91 |
|
| 92 |
RESULT_ENSURE_REF(supported_params); |
| 93 |
if (supported_params->kem_count == 0) { |
| 94 |
return S2N_RESULT_OK; |
| 95 |
} |
| 96 |
|
| 97 |
struct s2n_blob *client_kem_pref_list = &(conn->kex_params.client_pq_kem_extension); |
| 98 |
const struct s2n_kem *chosen_kem = NULL; |
| 99 |
if (client_kem_pref_list == NULL || client_kem_pref_list->data == NULL) { |
| 100 |
|
| 101 |
if (s2n_choose_kem_without_peer_pref_list( |
| 102 |
cipher_suite->iana_value, kem_preferences->kems, kem_preferences->kem_count, &chosen_kem) |
| 103 |
!= S2N_SUCCESS) { |
| 104 |
return S2N_RESULT_OK; |
| 105 |
} |
| 106 |
} else { |
| 107 |
|
| 108 |
if (s2n_choose_kem_with_peer_pref_list( |
| 109 |
cipher_suite->iana_value, client_kem_pref_list, kem_preferences->kems, kem_preferences->kem_count, &chosen_kem) |
| 110 |
!= S2N_SUCCESS) { |
| 111 |
return S2N_RESULT_OK; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
*is_supported = chosen_kem != NULL; |
| 116 |
return S2N_RESULT_OK; |
| 117 |
} |
| 118 |
|
| 119 |
static S2N_RESULT s2n_configure_kem(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn) |
| 120 |
{ |
| 121 |
RESULT_ENSURE_REF(cipher_suite); |
| 122 |
RESULT_ENSURE_REF(conn); |
| 123 |
|
| 124 |
RESULT_ENSURE(s2n_pq_is_enabled(), S2N_ERR_UNIMPLEMENTED); |
| 125 |
|
| 126 |
const struct s2n_kem_preferences *kem_preferences = NULL; |
| 127 |
RESULT_GUARD_POSIX(s2n_connection_get_kem_preferences(conn, &kem_preferences)); |
| 128 |
RESULT_ENSURE_REF(kem_preferences); |
| 129 |
|
| 130 |
struct s2n_blob *proposed_kems = &(conn->kex_params.client_pq_kem_extension); |
| 131 |
const struct s2n_kem *chosen_kem = NULL; |
| 132 |
if (proposed_kems == NULL || proposed_kems->data == NULL) { |
| 133 |
|
| 134 |
RESULT_GUARD_POSIX(s2n_choose_kem_without_peer_pref_list(cipher_suite->iana_value, kem_preferences->kems, |
| 135 |
kem_preferences->kem_count, &chosen_kem)); |
| 136 |
} else { |
| 137 |
|
| 138 |
RESULT_GUARD_POSIX(s2n_choose_kem_with_peer_pref_list(cipher_suite->iana_value, proposed_kems, kem_preferences->kems, |
| 139 |
kem_preferences->kem_count, &chosen_kem)); |
| 140 |
} |
| 141 |
|
| 142 |
conn->kex_params.kem_params.kem = chosen_kem; |
| 143 |
return S2N_RESULT_OK; |
| 144 |
} |
| 145 |
|
| 146 |
static S2N_RESULT s2n_check_hybrid_ecdhe_kem(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn, bool *is_supported) |
| 147 |
{ |
| 148 |
RESULT_ENSURE_REF(cipher_suite); |
| 149 |
RESULT_ENSURE_REF(conn); |
| 150 |
RESULT_ENSURE_REF(is_supported); |
| 151 |
|
| 152 |
bool ecdhe_supported = false; |
| 153 |
bool kem_supported = false; |
| 154 |
RESULT_GUARD(s2n_check_ecdhe(cipher_suite, conn, &ecdhe_supported)); |
| 155 |
RESULT_GUARD(s2n_check_kem(cipher_suite, conn, &kem_supported)); |
| 156 |
|
| 157 |
*is_supported = ecdhe_supported && kem_supported; |
| 158 |
|
| 159 |
return S2N_RESULT_OK; |
| 160 |
} |
| 161 |
|
| 162 |
static S2N_RESULT s2n_kex_configure_noop(const struct s2n_cipher_suite *cipher_suite, |
| 163 |
struct s2n_connection *conn) |
| 164 |
{ |
| 165 |
return S2N_RESULT_OK; |
| 166 |
} |
| 167 |
|
| 168 |
static int s2n_kex_server_key_recv_read_data_unimplemented(struct s2n_connection *conn, |
| 169 |
struct s2n_blob *data_to_verify, struct s2n_kex_raw_server_data *kex_data) |
| 170 |
{ |
| 171 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 172 |
} |
| 173 |
|
| 174 |
static int s2n_kex_server_key_recv_parse_data_unimplemented(struct s2n_connection *conn, |
| 175 |
struct s2n_kex_raw_server_data *kex_data) |
| 176 |
{ |
| 177 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 178 |
} |
| 179 |
|
| 180 |
static int s2n_kex_io_unimplemented(struct s2n_connection *conn, struct s2n_blob *data_to_sign) |
| 181 |
{ |
| 182 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 183 |
} |
| 184 |
|
| 185 |
static int s2n_kex_prf_unimplemented(struct s2n_connection *conn, struct s2n_blob *premaster_secret) |
| 186 |
{ |
| 187 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 188 |
} |
| 189 |
|
| 190 |
const struct s2n_kex s2n_kem = { |
| 191 |
.is_ephemeral = true, |
| 192 |
.connection_supported = &s2n_check_kem, |
| 193 |
.configure_connection = &s2n_configure_kem, |
| 194 |
.server_key_recv_read_data = &s2n_kem_server_key_recv_read_data, |
| 195 |
.server_key_recv_parse_data = &s2n_kem_server_key_recv_parse_data, |
| 196 |
.server_key_send = &s2n_kem_server_key_send, |
| 197 |
.client_key_recv = &s2n_kem_client_key_recv, |
| 198 |
.client_key_send = &s2n_kem_client_key_send, |
| 199 |
.prf = &s2n_kex_prf_unimplemented, |
| 200 |
}; |
| 201 |
|
| 202 |
const struct s2n_kex s2n_rsa = { |
| 203 |
.is_ephemeral = false, |
| 204 |
.connection_supported = &s2n_check_rsa_key, |
| 205 |
.configure_connection = &s2n_kex_configure_noop, |
| 206 |
.server_key_recv_read_data = &s2n_kex_server_key_recv_read_data_unimplemented, |
| 207 |
.server_key_recv_parse_data = &s2n_kex_server_key_recv_parse_data_unimplemented, |
| 208 |
.server_key_send = &s2n_kex_io_unimplemented, |
| 209 |
.client_key_recv = &s2n_rsa_client_key_recv, |
| 210 |
.client_key_send = &s2n_rsa_client_key_send, |
| 211 |
.prf = &s2n_prf_calculate_master_secret, |
| 212 |
}; |
| 213 |
|
| 214 |
const struct s2n_kex s2n_dhe = { |
| 215 |
.is_ephemeral = true, |
| 216 |
.connection_supported = &s2n_check_dhe, |
| 217 |
.configure_connection = &s2n_kex_configure_noop, |
| 218 |
.server_key_recv_read_data = &s2n_dhe_server_key_recv_read_data, |
| 219 |
.server_key_recv_parse_data = &s2n_dhe_server_key_recv_parse_data, |
| 220 |
.server_key_send = &s2n_dhe_server_key_send, |
| 221 |
.client_key_recv = &s2n_dhe_client_key_recv, |
| 222 |
.client_key_send = &s2n_dhe_client_key_send, |
| 223 |
.prf = &s2n_prf_calculate_master_secret, |
| 224 |
}; |
| 225 |
|
| 226 |
const struct s2n_kex s2n_ecdhe = { |
| 227 |
.is_ephemeral = true, |
| 228 |
.connection_supported = &s2n_check_ecdhe, |
| 229 |
.configure_connection = &s2n_kex_configure_noop, |
| 230 |
.server_key_recv_read_data = &s2n_ecdhe_server_key_recv_read_data, |
| 231 |
.server_key_recv_parse_data = &s2n_ecdhe_server_key_recv_parse_data, |
| 232 |
.server_key_send = &s2n_ecdhe_server_key_send, |
| 233 |
.client_key_recv = &s2n_ecdhe_client_key_recv, |
| 234 |
.client_key_send = &s2n_ecdhe_client_key_send, |
| 235 |
.prf = &s2n_prf_calculate_master_secret, |
| 236 |
}; |
| 237 |
|
| 238 |
const struct s2n_kex s2n_hybrid_ecdhe_kem = { |
| 239 |
.is_ephemeral = true, |
| 240 |
.hybrid = { &s2n_ecdhe, &s2n_kem }, |
| 241 |
.connection_supported = &s2n_check_hybrid_ecdhe_kem, |
| 242 |
.configure_connection = &s2n_configure_kem, |
| 243 |
.server_key_recv_read_data = &s2n_hybrid_server_key_recv_read_data, |
| 244 |
.server_key_recv_parse_data = &s2n_hybrid_server_key_recv_parse_data, |
| 245 |
.server_key_send = &s2n_hybrid_server_key_send, |
| 246 |
.client_key_recv = &s2n_hybrid_client_key_recv, |
| 247 |
.client_key_send = &s2n_hybrid_client_key_send, |
| 248 |
.prf = &s2n_prf_hybrid_master_secret, |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
const struct s2n_kex s2n_tls13_kex = { |
| 257 |
.is_ephemeral = true, |
| 258 |
.connection_supported = &s2n_check_tls13, |
| 259 |
.configure_connection = &s2n_kex_configure_noop, |
| 260 |
.server_key_recv_read_data = &s2n_kex_server_key_recv_read_data_unimplemented, |
| 261 |
.server_key_recv_parse_data = &s2n_kex_server_key_recv_parse_data_unimplemented, |
| 262 |
.server_key_send = &s2n_kex_io_unimplemented, |
| 263 |
.client_key_recv = &s2n_kex_io_unimplemented, |
| 264 |
.client_key_send = &s2n_kex_io_unimplemented, |
| 265 |
.prf = &s2n_kex_prf_unimplemented, |
| 266 |
}; |
| 267 |
|
| 268 |
S2N_RESULT s2n_kex_supported(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn, bool *is_supported) |
| 269 |
{ |
| 270 |
RESULT_ENSURE_REF(cipher_suite); |
| 271 |
RESULT_ENSURE_REF(cipher_suite->key_exchange_alg); |
| 272 |
RESULT_ENSURE_REF(cipher_suite->key_exchange_alg->connection_supported); |
| 273 |
RESULT_ENSURE_REF(conn); |
| 274 |
RESULT_ENSURE_REF(is_supported); |
| 275 |
|
| 276 |
RESULT_GUARD(cipher_suite->key_exchange_alg->connection_supported(cipher_suite, conn, is_supported)); |
| 277 |
|
| 278 |
return S2N_RESULT_OK; |
| 279 |
} |
| 280 |
|
| 281 |
S2N_RESULT s2n_configure_kex(const struct s2n_cipher_suite *cipher_suite, struct s2n_connection *conn) |
| 282 |
{ |
| 283 |
RESULT_ENSURE_REF(cipher_suite); |
| 284 |
RESULT_ENSURE_REF(cipher_suite->key_exchange_alg); |
| 285 |
RESULT_ENSURE_REF(cipher_suite->key_exchange_alg->configure_connection); |
| 286 |
RESULT_ENSURE_REF(conn); |
| 287 |
|
| 288 |
RESULT_GUARD(cipher_suite->key_exchange_alg->configure_connection(cipher_suite, conn)); |
| 289 |
|
| 290 |
return S2N_RESULT_OK; |
| 291 |
} |
| 292 |
|
| 293 |
S2N_RESULT s2n_kex_is_ephemeral(const struct s2n_kex *kex, bool *is_ephemeral) |
| 294 |
{ |
| 295 |
RESULT_ENSURE_REF(kex); |
| 296 |
RESULT_ENSURE_REF(is_ephemeral); |
| 297 |
|
| 298 |
*is_ephemeral = kex->is_ephemeral; |
| 299 |
|
| 300 |
return S2N_RESULT_OK; |
| 301 |
} |
| 302 |
|
| 303 |
S2N_RESULT s2n_kex_server_key_recv_parse_data(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_kex_raw_server_data *raw_server_data) |
| 304 |
{ |
| 305 |
RESULT_ENSURE_REF(kex); |
| 306 |
RESULT_ENSURE_REF(kex->server_key_recv_parse_data); |
| 307 |
RESULT_ENSURE_REF(conn); |
| 308 |
RESULT_ENSURE_REF(raw_server_data); |
| 309 |
|
| 310 |
RESULT_GUARD_POSIX(kex->server_key_recv_parse_data(conn, raw_server_data)); |
| 311 |
|
| 312 |
return S2N_RESULT_OK; |
| 313 |
} |
| 314 |
|
| 315 |
S2N_RESULT s2n_kex_server_key_recv_read_data(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *data_to_verify, |
| 316 |
struct s2n_kex_raw_server_data *raw_server_data) |
| 317 |
{ |
| 318 |
RESULT_ENSURE_REF(kex); |
| 319 |
RESULT_ENSURE_REF(kex->server_key_recv_read_data); |
| 320 |
RESULT_ENSURE_REF(conn); |
| 321 |
RESULT_ENSURE_REF(data_to_verify); |
| 322 |
|
| 323 |
RESULT_GUARD_POSIX(kex->server_key_recv_read_data(conn, data_to_verify, raw_server_data)); |
| 324 |
|
| 325 |
return S2N_RESULT_OK; |
| 326 |
} |
| 327 |
|
| 328 |
S2N_RESULT s2n_kex_server_key_send(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *data_to_sign) |
| 329 |
{ |
| 330 |
RESULT_ENSURE_REF(kex); |
| 331 |
RESULT_ENSURE_REF(kex->server_key_send); |
| 332 |
RESULT_ENSURE_REF(conn); |
| 333 |
RESULT_ENSURE_REF(data_to_sign); |
| 334 |
|
| 335 |
RESULT_GUARD_POSIX(kex->server_key_send(conn, data_to_sign)); |
| 336 |
|
| 337 |
return S2N_RESULT_OK; |
| 338 |
} |
| 339 |
|
| 340 |
S2N_RESULT s2n_kex_client_key_recv(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 341 |
{ |
| 342 |
RESULT_ENSURE_REF(kex); |
| 343 |
RESULT_ENSURE_REF(kex->client_key_recv); |
| 344 |
RESULT_ENSURE_REF(conn); |
| 345 |
RESULT_ENSURE_REF(shared_key); |
| 346 |
|
| 347 |
RESULT_GUARD_POSIX(kex->client_key_recv(conn, shared_key)); |
| 348 |
|
| 349 |
return S2N_RESULT_OK; |
| 350 |
} |
| 351 |
|
| 352 |
S2N_RESULT s2n_kex_client_key_send(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 353 |
{ |
| 354 |
RESULT_ENSURE_REF(kex); |
| 355 |
RESULT_ENSURE_REF(kex->client_key_send); |
| 356 |
RESULT_ENSURE_REF(conn); |
| 357 |
RESULT_ENSURE_REF(shared_key); |
| 358 |
|
| 359 |
RESULT_GUARD_POSIX(kex->client_key_send(conn, shared_key)); |
| 360 |
|
| 361 |
return S2N_RESULT_OK; |
| 362 |
} |
| 363 |
|
| 364 |
S2N_RESULT s2n_kex_tls_prf(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *premaster_secret) |
| 365 |
{ |
| 366 |
RESULT_ENSURE_REF(kex); |
| 367 |
RESULT_ENSURE_REF(kex->prf); |
| 368 |
RESULT_ENSURE_REF(conn); |
| 369 |
RESULT_ENSURE_REF(premaster_secret); |
| 370 |
|
| 371 |
RESULT_GUARD_POSIX(kex->prf(conn, premaster_secret)); |
| 372 |
|
| 373 |
return S2N_RESULT_OK; |
| 374 |
} |
| 375 |
|
| 376 |
bool s2n_kex_includes(const struct s2n_kex *kex, const struct s2n_kex *query) |
| 377 |
{ |
| 378 |
if (kex == query) { |
| 379 |
return true; |
| 380 |
} |
| 381 |
|
| 382 |
if (kex == NULL || query == NULL) { |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
return query == kex->hybrid[0] || query == kex->hybrid[1]; |
| 387 |
} |