| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "api/s2n.h" |
| 17 |
#include "crypto/s2n_dhe.h" |
| 18 |
#include "crypto/s2n_pkey.h" |
| 19 |
#include "error/s2n_errno.h" |
| 20 |
#include "stuffer/s2n_stuffer.h" |
| 21 |
#include "tls/s2n_async_pkey.h" |
| 22 |
#include "tls/s2n_cipher_suites.h" |
| 23 |
#include "tls/s2n_connection.h" |
| 24 |
#include "tls/s2n_handshake.h" |
| 25 |
#include "tls/s2n_kem.h" |
| 26 |
#include "tls/s2n_kex.h" |
| 27 |
#include "tls/s2n_key_log.h" |
| 28 |
#include "tls/s2n_resume.h" |
| 29 |
#include "utils/s2n_random.h" |
| 30 |
#include "utils/s2n_safety.h" |
| 31 |
|
| 32 |
typedef S2N_RESULT s2n_kex_client_key_method(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *shared_key); |
| 33 |
typedef void *s2n_stuffer_action(struct s2n_stuffer *stuffer, uint32_t data_len); |
| 34 |
|
| 35 |
static int s2n_rsa_client_key_recv_complete(struct s2n_connection *conn, bool rsa_failed, struct s2n_blob *shared_key); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static S2N_RESULT s2n_client_key_exchange_get_rsa_client_version(struct s2n_connection *conn, |
| 47 |
uint8_t client_version[S2N_TLS_PROTOCOL_VERSION_LEN]) |
| 48 |
{ |
| 49 |
RESULT_ENSURE_REF(conn); |
| 50 |
RESULT_ENSURE_REF(client_version); |
| 51 |
uint8_t client_version_for_rsa = S2N_MIN(conn->client_protocol_version, S2N_TLS12); |
| 52 |
client_version[0] = client_version_for_rsa / 10; |
| 53 |
client_version[1] = client_version_for_rsa % 10; |
| 54 |
return S2N_RESULT_OK; |
| 55 |
} |
| 56 |
|
| 57 |
static int s2n_hybrid_client_action(struct s2n_connection *conn, struct s2n_blob *combined_shared_key, |
| 58 |
s2n_kex_client_key_method kex_method, uint32_t *cursor, s2n_stuffer_action stuffer_action) |
| 59 |
{ |
| 60 |
POSIX_ENSURE_REF(conn); |
| 61 |
POSIX_ENSURE_REF(conn->secure); |
| 62 |
POSIX_ENSURE_REF(kex_method); |
| 63 |
POSIX_ENSURE_REF(stuffer_action); |
| 64 |
|
| 65 |
struct s2n_stuffer *io = &conn->handshake.io; |
| 66 |
const struct s2n_kex *hybrid_kex_0 = conn->secure->cipher_suite->key_exchange_alg->hybrid[0]; |
| 67 |
const struct s2n_kex *hybrid_kex_1 = conn->secure->cipher_suite->key_exchange_alg->hybrid[1]; |
| 68 |
|
| 69 |
|
| 70 |
struct s2n_blob *client_key_exchange_message = &conn->kex_params.client_key_exchange_message; |
| 71 |
client_key_exchange_message->data = stuffer_action(io, 0); |
| 72 |
POSIX_ENSURE_REF(client_key_exchange_message->data); |
| 73 |
const uint32_t start_cursor = *cursor; |
| 74 |
|
| 75 |
DEFER_CLEANUP(struct s2n_blob shared_key_0 = { 0 }, s2n_free); |
| 76 |
POSIX_GUARD_RESULT(kex_method(hybrid_kex_0, conn, &shared_key_0)); |
| 77 |
|
| 78 |
struct s2n_blob *shared_key_1 = &(conn->kex_params.kem_params.shared_secret); |
| 79 |
POSIX_GUARD_RESULT(kex_method(hybrid_kex_1, conn, shared_key_1)); |
| 80 |
|
| 81 |
const uint32_t end_cursor = *cursor; |
| 82 |
POSIX_ENSURE_GTE(end_cursor, start_cursor); |
| 83 |
client_key_exchange_message->size = end_cursor - start_cursor; |
| 84 |
|
| 85 |
POSIX_GUARD(s2n_alloc(combined_shared_key, shared_key_0.size + shared_key_1->size)); |
| 86 |
struct s2n_stuffer stuffer_combiner = { 0 }; |
| 87 |
POSIX_GUARD(s2n_stuffer_init(&stuffer_combiner, combined_shared_key)); |
| 88 |
POSIX_GUARD(s2n_stuffer_write(&stuffer_combiner, &shared_key_0)); |
| 89 |
POSIX_GUARD(s2n_stuffer_write(&stuffer_combiner, shared_key_1)); |
| 90 |
|
| 91 |
POSIX_GUARD(s2n_kem_free(&conn->kex_params.kem_params)); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
static int s2n_calculate_keys(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 97 |
{ |
| 98 |
POSIX_ENSURE_REF(conn); |
| 99 |
POSIX_ENSURE_REF(conn->secure); |
| 100 |
POSIX_ENSURE_REF(conn->secure->cipher_suite); |
| 101 |
|
| 102 |
|
| 103 |
POSIX_GUARD_RESULT(s2n_kex_tls_prf(conn->secure->cipher_suite->key_exchange_alg, conn, shared_key)); |
| 104 |
|
| 105 |
|
| 106 |
POSIX_GUARD(s2n_prf_key_expansion(conn)); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if (s2n_allowed_to_cache_connection(conn)) { |
| 111 |
s2n_result_ignore(s2n_store_to_cache(conn)); |
| 112 |
} |
| 113 |
|
| 114 |
s2n_result_ignore(s2n_key_log_tls12_secret(conn)); |
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
int s2n_rsa_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 119 |
{ |
| 120 |
|
| 121 |
POSIX_ENSURE_REF(shared_key); |
| 122 |
shared_key->data = conn->secrets.version.tls12.rsa_premaster_secret; |
| 123 |
shared_key->size = S2N_TLS_SECRET_LEN; |
| 124 |
|
| 125 |
S2N_ASYNC_PKEY_GUARD(conn); |
| 126 |
|
| 127 |
struct s2n_stuffer *in = &conn->handshake.io; |
| 128 |
uint16_t length = 0; |
| 129 |
|
| 130 |
if (conn->actual_protocol_version == S2N_SSLv3) { |
| 131 |
length = s2n_stuffer_data_available(in); |
| 132 |
} else { |
| 133 |
POSIX_GUARD(s2n_stuffer_read_uint16(in, &length)); |
| 134 |
} |
| 135 |
|
| 136 |
S2N_ERROR_IF(length > s2n_stuffer_data_available(in), S2N_ERR_BAD_MESSAGE); |
| 137 |
|
| 138 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 }; |
| 139 |
POSIX_GUARD_RESULT(s2n_client_key_exchange_get_rsa_client_version(conn, protocol_version)); |
| 140 |
|
| 141 |
|
| 142 |
struct s2n_blob encrypted = { 0 }; |
| 143 |
POSIX_GUARD(s2n_blob_init(&encrypted, s2n_stuffer_raw_read(in, length), length)); |
| 144 |
POSIX_ENSURE_REF(encrypted.data); |
| 145 |
POSIX_ENSURE_GT(encrypted.size, 0); |
| 146 |
|
| 147 |
|
| 148 |
POSIX_GUARD_RESULT(s2n_get_private_random_data(shared_key)); |
| 149 |
conn->secrets.version.tls12.rsa_premaster_secret[0] = protocol_version[0]; |
| 150 |
conn->secrets.version.tls12.rsa_premaster_secret[1] = protocol_version[1]; |
| 151 |
|
| 152 |
S2N_ASYNC_PKEY_DECRYPT(conn, &encrypted, shared_key, s2n_rsa_client_key_recv_complete); |
| 153 |
} |
| 154 |
|
| 155 |
int s2n_rsa_client_key_recv_complete(struct s2n_connection *conn, bool rsa_failed, struct s2n_blob *decrypted) |
| 156 |
{ |
| 157 |
S2N_ERROR_IF(decrypted->size != S2N_TLS_SECRET_LEN, S2N_ERR_SIZE_MISMATCH); |
| 158 |
|
| 159 |
|
| 160 |
if (conn->secrets.version.tls12.rsa_premaster_secret != decrypted->data) { |
| 161 |
|
| 162 |
POSIX_CHECKED_MEMCPY(conn->secrets.version.tls12.rsa_premaster_secret, decrypted->data, S2N_TLS_SECRET_LEN); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 }; |
| 167 |
POSIX_GUARD_RESULT(s2n_client_key_exchange_get_rsa_client_version(conn, protocol_version)); |
| 168 |
|
| 169 |
conn->handshake.rsa_failed = rsa_failed; |
| 170 |
|
| 171 |
|
| 172 |
conn->handshake.rsa_failed |= !s2n_constant_time_equals(protocol_version, |
| 173 |
conn->secrets.version.tls12.rsa_premaster_secret, S2N_TLS_PROTOCOL_VERSION_LEN); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
conn->secrets.version.tls12.rsa_premaster_secret[0] = protocol_version[0]; |
| 181 |
conn->secrets.version.tls12.rsa_premaster_secret[1] = protocol_version[1]; |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
int s2n_dhe_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 187 |
{ |
| 188 |
struct s2n_stuffer *in = &conn->handshake.io; |
| 189 |
|
| 190 |
|
| 191 |
POSIX_GUARD(s2n_dh_compute_shared_secret_as_server(&conn->kex_params.server_dh_params, in, shared_key)); |
| 192 |
|
| 193 |
POSIX_GUARD(s2n_dh_params_free(&conn->kex_params.server_dh_params)); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
int s2n_ecdhe_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 198 |
{ |
| 199 |
struct s2n_stuffer *in = &conn->handshake.io; |
| 200 |
|
| 201 |
|
| 202 |
POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_as_server(&conn->kex_params.server_ecc_evp_params, in, shared_key)); |
| 203 |
|
| 204 |
POSIX_GUARD(s2n_ecc_evp_params_free(&conn->kex_params.server_ecc_evp_params)); |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
int s2n_kem_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 209 |
{ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
POSIX_ENSURE_REF(shared_key); |
| 221 |
S2N_ERROR_IF(shared_key != &(conn->kex_params.kem_params.shared_secret), S2N_ERR_SAFETY); |
| 222 |
conn->kex_params.kem_params.len_prefixed = true; |
| 223 |
|
| 224 |
POSIX_GUARD(s2n_kem_recv_ciphertext(&(conn->handshake.io), &(conn->kex_params.kem_params))); |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
int s2n_hybrid_client_key_recv(struct s2n_connection *conn, struct s2n_blob *combined_shared_key) |
| 230 |
{ |
| 231 |
return s2n_hybrid_client_action(conn, combined_shared_key, &s2n_kex_client_key_recv, &conn->handshake.io.read_cursor, |
| 232 |
&s2n_stuffer_raw_read); |
| 233 |
} |
| 234 |
|
| 235 |
int s2n_client_key_recv(struct s2n_connection *conn) |
| 236 |
{ |
| 237 |
POSIX_ENSURE_REF(conn); |
| 238 |
POSIX_ENSURE_REF(conn->secure); |
| 239 |
POSIX_ENSURE_REF(conn->secure->cipher_suite); |
| 240 |
|
| 241 |
const struct s2n_kex *key_exchange = conn->secure->cipher_suite->key_exchange_alg; |
| 242 |
DEFER_CLEANUP(struct s2n_blob shared_key = { 0 }, s2n_free_or_wipe); |
| 243 |
POSIX_GUARD_RESULT(s2n_kex_client_key_recv(key_exchange, conn, &shared_key)); |
| 244 |
|
| 245 |
POSIX_GUARD(s2n_calculate_keys(conn, &shared_key)); |
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
int s2n_dhe_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 250 |
{ |
| 251 |
struct s2n_stuffer *out = &conn->handshake.io; |
| 252 |
POSIX_GUARD(s2n_dh_compute_shared_secret_as_client(&conn->kex_params.server_dh_params, out, shared_key)); |
| 253 |
|
| 254 |
|
| 255 |
POSIX_GUARD(s2n_dh_params_free(&conn->kex_params.server_dh_params)); |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
int s2n_ecdhe_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 260 |
{ |
| 261 |
struct s2n_stuffer *out = &conn->handshake.io; |
| 262 |
POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_as_client(&conn->kex_params.server_ecc_evp_params, out, shared_key)); |
| 263 |
|
| 264 |
|
| 265 |
POSIX_GUARD(s2n_ecc_evp_params_free(&conn->kex_params.server_ecc_evp_params)); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
int s2n_rsa_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 270 |
{ |
| 271 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 }; |
| 272 |
POSIX_GUARD_RESULT(s2n_client_key_exchange_get_rsa_client_version(conn, protocol_version)); |
| 273 |
|
| 274 |
shared_key->data = conn->secrets.version.tls12.rsa_premaster_secret; |
| 275 |
shared_key->size = S2N_TLS_SECRET_LEN; |
| 276 |
|
| 277 |
POSIX_GUARD_RESULT(s2n_get_private_random_data(shared_key)); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
POSIX_CHECKED_MEMCPY(conn->secrets.version.tls12.rsa_premaster_secret, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN); |
| 284 |
|
| 285 |
uint32_t encrypted_size = 0; |
| 286 |
POSIX_GUARD_RESULT(s2n_pkey_size(&conn->handshake_params.server_public_key, &encrypted_size)); |
| 287 |
S2N_ERROR_IF(encrypted_size > 0xffff, S2N_ERR_SIZE_MISMATCH); |
| 288 |
|
| 289 |
if (conn->actual_protocol_version > S2N_SSLv3) { |
| 290 |
POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, encrypted_size)); |
| 291 |
} |
| 292 |
|
| 293 |
struct s2n_blob encrypted = { 0 }; |
| 294 |
encrypted.data = s2n_stuffer_raw_write(&conn->handshake.io, encrypted_size); |
| 295 |
encrypted.size = encrypted_size; |
| 296 |
POSIX_ENSURE_REF(encrypted.data); |
| 297 |
|
| 298 |
|
| 299 |
POSIX_GUARD(s2n_pkey_encrypt(&conn->handshake_params.server_public_key, shared_key, &encrypted)); |
| 300 |
|
| 301 |
|
| 302 |
POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.server_public_key)); |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
int s2n_kem_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key) |
| 307 |
{ |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
POSIX_ENSURE_REF(shared_key); |
| 319 |
S2N_ERROR_IF(shared_key != &(conn->kex_params.kem_params.shared_secret), S2N_ERR_SAFETY); |
| 320 |
|
| 321 |
conn->kex_params.kem_params.len_prefixed = true; |
| 322 |
|
| 323 |
POSIX_GUARD(s2n_kem_send_ciphertext(&(conn->handshake.io), &(conn->kex_params.kem_params))); |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
int s2n_hybrid_client_key_send(struct s2n_connection *conn, struct s2n_blob *combined_shared_key) |
| 329 |
{ |
| 330 |
return s2n_hybrid_client_action(conn, combined_shared_key, &s2n_kex_client_key_send, &conn->handshake.io.write_cursor, |
| 331 |
s2n_stuffer_raw_write); |
| 332 |
} |
| 333 |
|
| 334 |
int s2n_client_key_send(struct s2n_connection *conn) |
| 335 |
{ |
| 336 |
POSIX_ENSURE_REF(conn); |
| 337 |
POSIX_ENSURE_REF(conn->secure); |
| 338 |
POSIX_ENSURE_REF(conn->secure->cipher_suite); |
| 339 |
|
| 340 |
const struct s2n_kex *key_exchange = conn->secure->cipher_suite->key_exchange_alg; |
| 341 |
DEFER_CLEANUP(struct s2n_blob shared_key = { 0 }, s2n_free_or_wipe); |
| 342 |
|
| 343 |
POSIX_GUARD_RESULT(s2n_kex_client_key_send(key_exchange, conn, &shared_key)); |
| 344 |
|
| 345 |
POSIX_GUARD(s2n_calculate_keys(conn, &shared_key)); |
| 346 |
return 0; |
| 347 |
} |