| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/io/tls_channel_handler.h> |
| 6 |
|
| 7 |
|
| 8 |
#ifdef USE_S2N |
| 9 |
|
| 10 |
# include <aws/common/clock.h> |
| 11 |
# include <aws/common/encoding.h> |
| 12 |
# include <aws/common/mutex.h> |
| 13 |
# include <aws/common/string.h> |
| 14 |
# include <aws/common/task_scheduler.h> |
| 15 |
# include <aws/common/thread.h> |
| 16 |
# include <aws/io/channel.h> |
| 17 |
# include <aws/io/event_loop.h> |
| 18 |
# include <aws/io/file_utils.h> |
| 19 |
# include <aws/io/logging.h> |
| 20 |
# include <aws/io/private/event_loop_impl.h> |
| 21 |
# include <aws/io/private/pki_utils.h> |
| 22 |
# include <aws/io/private/tls_channel_handler_private.h> |
| 23 |
# include <aws/io/private/tls_channel_handler_shared.h> |
| 24 |
# include <aws/io/statistics.h> |
| 25 |
|
| 26 |
# include <s2n.h> |
| 27 |
# ifdef AWS_S2N_INSOURCE_PATH |
| 28 |
# include <api/unstable/cleanup.h> |
| 29 |
# else |
| 30 |
# include <s2n/unstable/cleanup.h> |
| 31 |
# endif |
| 32 |
|
| 33 |
# include <errno.h> |
| 34 |
# include <inttypes.h> |
| 35 |
# include <math.h> |
| 36 |
# include <stdio.h> |
| 37 |
# include <stdlib.h> |
| 38 |
|
| 39 |
# define EST_TLS_RECORD_OVERHEAD 53 |
| 40 |
# define KB_1 1024 |
| 41 |
# define MAX_RECORD_SIZE (KB_1 * 16) |
| 42 |
# define EST_HANDSHAKE_SIZE (7 * KB_1) |
| 43 |
|
| 44 |
static const char *s_default_ca_dir = NULL; |
| 45 |
static const char *s_default_ca_file = NULL; |
| 46 |
|
| 47 |
struct s2n_handler { |
| 48 |
struct aws_channel_handler handler; |
| 49 |
struct aws_tls_channel_handler_shared shared_state; |
| 50 |
struct s2n_connection *connection; |
| 51 |
struct s2n_ctx *s2n_ctx; |
| 52 |
struct aws_channel_slot *slot; |
| 53 |
struct aws_linked_list input_queue; |
| 54 |
struct aws_byte_buf protocol; |
| 55 |
struct aws_byte_buf server_name; |
| 56 |
aws_channel_on_message_write_completed_fn *latest_message_on_completion; |
| 57 |
struct aws_channel_task sequential_tasks; |
| 58 |
void *latest_message_completion_user_data; |
| 59 |
aws_tls_on_negotiation_result_fn *on_negotiation_result; |
| 60 |
aws_tls_on_data_read_fn *on_data_read; |
| 61 |
aws_tls_on_error_fn *on_error; |
| 62 |
void *user_data; |
| 63 |
bool advertise_alpn_message; |
| 64 |
enum { |
| 65 |
NEGOTIATION_ONGOING, |
| 66 |
NEGOTIATION_FAILED, |
| 67 |
NEGOTIATION_SUCCEEDED, |
| 68 |
} state; |
| 69 |
struct aws_channel_task read_task; |
| 70 |
bool read_task_pending; |
| 71 |
enum aws_tls_handler_read_state read_state; |
| 72 |
int shutdown_error_code; |
| 73 |
struct aws_channel_task delayed_shutdown_task; |
| 74 |
}; |
| 75 |
|
| 76 |
struct s2n_ctx { |
| 77 |
struct aws_tls_ctx ctx; |
| 78 |
struct s2n_config *s2n_config; |
| 79 |
|
| 80 |
|
| 81 |
struct s2n_cert_chain_and_key *custom_cert_chain_and_key; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
struct aws_custom_key_op_handler *custom_key_handler; |
| 92 |
}; |
| 93 |
|
| 94 |
struct aws_tls_key_operation { |
| 95 |
struct aws_allocator *alloc; |
| 96 |
struct s2n_async_pkey_op *s2n_op; |
| 97 |
struct s2n_handler *s2n_handler; |
| 98 |
enum aws_tls_key_operation_type operation_type; |
| 99 |
enum aws_tls_signature_algorithm signature_algorithm; |
| 100 |
enum aws_tls_hash_algorithm digest_algorithm; |
| 101 |
struct aws_byte_buf input_data; |
| 102 |
struct aws_channel_task completion_task; |
| 103 |
int completion_error_code; |
| 104 |
|
| 105 |
struct aws_atomic_var complete_count; |
| 106 |
}; |
| 107 |
|
| 108 |
AWS_STATIC_STRING_FROM_LITERAL(s_debian_path, "/etc/ssl/certs"); |
| 109 |
AWS_STATIC_STRING_FROM_LITERAL(s_rhel_path, "/etc/pki/tls/certs"); |
| 110 |
AWS_STATIC_STRING_FROM_LITERAL(s_android_path, "/system/etc/security/cacerts"); |
| 111 |
AWS_STATIC_STRING_FROM_LITERAL(s_free_bsd_path, "/usr/local/share/certs"); |
| 112 |
AWS_STATIC_STRING_FROM_LITERAL(s_net_bsd_path, "/etc/openssl/certs"); |
| 113 |
|
| 114 |
# include "s2n_apple_keychain.h" |
| 115 |
|
| 116 |
static void s_tls_init_static_state(struct aws_allocator *alloc); |
| 117 |
static void s_tls_clean_up_static_state(void); |
| 118 |
static bool s_tls_is_alpn_available(void); |
| 119 |
static bool s_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref); |
| 120 |
static int s_tls_client_handler_start_negotiation(struct aws_channel_handler *handler); |
| 121 |
static void s_tls_key_operation_complete(struct aws_tls_key_operation *operation, struct aws_byte_cursor output); |
| 122 |
static void s_tls_key_operation_complete_with_error(struct aws_tls_key_operation *operation, int error_code); |
| 123 |
static struct aws_byte_cursor s_tls_key_operation_get_input(const struct aws_tls_key_operation *operation); |
| 124 |
static enum aws_tls_key_operation_type s_tls_key_operation_get_type(const struct aws_tls_key_operation *operation); |
| 125 |
static enum aws_tls_signature_algorithm s_tls_key_operation_get_signature_algorithm( |
| 126 |
const struct aws_tls_key_operation *operation); |
| 127 |
static enum aws_tls_hash_algorithm s_tls_key_operation_get_digest_algorithm( |
| 128 |
const struct aws_tls_key_operation *operation); |
| 129 |
static struct aws_byte_buf s_tls_handler_protocol(struct aws_channel_handler *handler); |
| 130 |
static struct aws_byte_buf s_tls_handler_server_name(struct aws_channel_handler *handler); |
| 131 |
static struct aws_channel_handler *s_tls_client_handler_new( |
| 132 |
struct aws_allocator *allocator, |
| 133 |
struct aws_tls_connection_options *options, |
| 134 |
struct aws_channel_slot *slot); |
| 135 |
static struct aws_channel_handler *s_tls_server_handler_new( |
| 136 |
struct aws_allocator *allocator, |
| 137 |
struct aws_tls_connection_options *options, |
| 138 |
struct aws_channel_slot *slot); |
| 139 |
static struct aws_tls_ctx *s_tls_server_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options); |
| 140 |
static struct aws_tls_ctx *s_tls_client_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options); |
| 141 |
|
| 142 |
static struct aws_tls_vtable s_vtable = { |
| 143 |
.init_static_state = s_tls_init_static_state, |
| 144 |
.clean_up_static_state = s_tls_clean_up_static_state, |
| 145 |
.is_alpn_available = s_tls_is_alpn_available, |
| 146 |
.is_cipher_pref_supported = s_tls_is_cipher_pref_supported, |
| 147 |
.client_handler_start_negotiation = s_tls_client_handler_start_negotiation, |
| 148 |
.key_operation_complete = s_tls_key_operation_complete, |
| 149 |
.key_operation_complete_with_error = s_tls_key_operation_complete_with_error, |
| 150 |
.key_operation_get_input = s_tls_key_operation_get_input, |
| 151 |
.key_operation_get_type = s_tls_key_operation_get_type, |
| 152 |
.key_operation_get_signature_algorithm = s_tls_key_operation_get_signature_algorithm, |
| 153 |
.key_operation_get_digest_algorithm = s_tls_key_operation_get_digest_algorithm, |
| 154 |
.handler_protocol = s_tls_handler_protocol, |
| 155 |
.handler_server_name = s_tls_handler_server_name, |
| 156 |
.client_handler_new = s_tls_client_handler_new, |
| 157 |
.server_handler_new = s_tls_server_handler_new, |
| 158 |
.server_ctx_new = s_tls_server_ctx_new, |
| 159 |
.client_ctx_new = s_tls_client_ctx_new, |
| 160 |
}; |
| 161 |
|
| 162 |
void s2n_init_tls_vtable(struct aws_tls_vtable *vtable) { |
| 163 |
*vtable = s_vtable; |
| 164 |
} |
| 165 |
|
| 166 |
const char *aws_determine_default_pki_dir(void) { |
| 167 |
|
| 168 |
if (aws_path_exists(s_debian_path)) { |
| 169 |
return aws_string_c_str(s_debian_path); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
if (aws_path_exists(s_rhel_path)) { |
| 174 |
return aws_string_c_str(s_rhel_path); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if (aws_path_exists(s_android_path)) { |
| 179 |
return aws_string_c_str(s_android_path); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
if (aws_path_exists(s_free_bsd_path)) { |
| 184 |
return aws_string_c_str(s_free_bsd_path); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if (aws_path_exists(s_net_bsd_path)) { |
| 189 |
return aws_string_c_str(s_net_bsd_path); |
| 190 |
} |
| 191 |
|
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
AWS_STATIC_STRING_FROM_LITERAL(s_debian_ca_file_path, "/etc/ssl/certs/ca-certificates.crt"); |
| 196 |
AWS_STATIC_STRING_FROM_LITERAL(s_old_rhel_ca_file_path, "/etc/pki/tls/certs/ca-bundle.crt"); |
| 197 |
AWS_STATIC_STRING_FROM_LITERAL(s_open_suse_ca_file_path, "/etc/ssl/ca-bundle.pem"); |
| 198 |
AWS_STATIC_STRING_FROM_LITERAL(s_open_elec_ca_file_path, "/etc/pki/tls/cacert.pem"); |
| 199 |
AWS_STATIC_STRING_FROM_LITERAL(s_modern_rhel_ca_file_path, "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"); |
| 200 |
AWS_STATIC_STRING_FROM_LITERAL(s_openbsd_ca_file_path, "/etc/ssl/cert.pem"); |
| 201 |
|
| 202 |
const char *aws_determine_default_pki_ca_file(void) { |
| 203 |
|
| 204 |
if (aws_path_exists(s_debian_ca_file_path)) { |
| 205 |
return aws_string_c_str(s_debian_ca_file_path); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
if (aws_path_exists(s_old_rhel_ca_file_path)) { |
| 210 |
return aws_string_c_str(s_old_rhel_ca_file_path); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
if (aws_path_exists(s_open_suse_ca_file_path)) { |
| 215 |
return aws_string_c_str(s_open_suse_ca_file_path); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
if (aws_path_exists(s_open_elec_ca_file_path)) { |
| 220 |
return aws_string_c_str(s_open_elec_ca_file_path); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if (aws_path_exists(s_modern_rhel_ca_file_path)) { |
| 225 |
return aws_string_c_str(s_modern_rhel_ca_file_path); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
if (aws_path_exists(s_openbsd_ca_file_path)) { |
| 230 |
return aws_string_c_str(s_openbsd_ca_file_path); |
| 231 |
} |
| 232 |
|
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
static struct aws_allocator *s_library_allocator = NULL; |
| 237 |
|
| 238 |
static int s_s2n_mem_init(void) { |
| 239 |
return S2N_SUCCESS; |
| 240 |
} |
| 241 |
|
| 242 |
static int s_s2n_mem_cleanup(void) { |
| 243 |
return S2N_SUCCESS; |
| 244 |
} |
| 245 |
|
| 246 |
static int s_s2n_mem_malloc(void **ptr, uint32_t requested, uint32_t *allocated) { |
| 247 |
*ptr = aws_mem_acquire(s_library_allocator, requested); |
| 248 |
*allocated = requested; |
| 249 |
|
| 250 |
return S2N_SUCCESS; |
| 251 |
} |
| 252 |
|
| 253 |
static int s_s2n_mem_free(void *ptr, uint32_t size) { |
| 254 |
(void)size; |
| 255 |
aws_mem_release(s_library_allocator, ptr); |
| 256 |
return S2N_SUCCESS; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
static bool s_s2n_initialized_externally = false; |
| 261 |
|
| 262 |
static void s_tls_init_static_state(struct aws_allocator *alloc) { |
| 263 |
AWS_FATAL_ASSERT(alloc); |
| 264 |
AWS_LOGF_INFO(AWS_LS_IO_TLS, "static: Initializing TLS using s2n."); |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if (s2n_disable_atexit() != S2N_SUCCESS) { |
| 272 |
|
| 273 |
|
| 274 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: s2n is already initialized"); |
| 275 |
s_s2n_initialized_externally = true; |
| 276 |
} else { |
| 277 |
s_s2n_initialized_externally = false; |
| 278 |
} |
| 279 |
|
| 280 |
if (!s_s2n_initialized_externally) { |
| 281 |
s_library_allocator = alloc; |
| 282 |
if (S2N_SUCCESS != s2n_mem_set_callbacks(s_s2n_mem_init, s_s2n_mem_cleanup, s_s2n_mem_malloc, s_s2n_mem_free)) { |
| 283 |
fprintf(stderr, "s2n_mem_set_callbacks() failed: %d (%s)\n", s2n_errno, s2n_strerror(s2n_errno, "EN")); |
| 284 |
AWS_FATAL_ASSERT(0 && "s2n_mem_set_callbacks() failed"); |
| 285 |
} |
| 286 |
|
| 287 |
if (s2n_init() != S2N_SUCCESS) { |
| 288 |
fprintf(stderr, "s2n_init() failed: %d (%s)\n", s2n_errno, s2n_strerror(s2n_errno, "EN")); |
| 289 |
AWS_FATAL_ASSERT(0 && "s2n_init() failed"); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
s_default_ca_dir = aws_determine_default_pki_dir(); |
| 294 |
s_default_ca_file = aws_determine_default_pki_ca_file(); |
| 295 |
if (s_default_ca_dir || s_default_ca_file) { |
| 296 |
AWS_LOGF_DEBUG( |
| 297 |
AWS_LS_IO_TLS, |
| 298 |
"ctx: Based on OS, we detected the default PKI path as %s, and ca file as %s", |
| 299 |
s_default_ca_dir, |
| 300 |
s_default_ca_file); |
| 301 |
} else { |
| 302 |
AWS_LOGF_WARN( |
| 303 |
AWS_LS_IO_TLS, |
| 304 |
"Default TLS trust store not found on this system." |
| 305 |
" TLS connections will fail unless trusted CA certificates are installed," |
| 306 |
" or \"override default trust store\" is used while creating the TLS context."); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
static void s_tls_clean_up_static_state(void) { |
| 311 |
|
| 312 |
if (!s_s2n_initialized_externally) { |
| 313 |
s2n_cleanup_final(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
static bool s_tls_is_alpn_available(void) { |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
static bool s_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref) { |
| 322 |
switch (cipher_pref) { |
| 323 |
case AWS_IO_TLS_CIPHER_PREF_PQ_DEFAULT: |
| 324 |
case AWS_IO_TLS_CIPHER_PREF_PQ_TLSV1_2_2024_10: |
| 325 |
case AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT: |
| 326 |
case AWS_IO_TLS_CIPHER_PREF_TLSV1_0_2023_06: |
| 327 |
case AWS_IO_TLS_CIPHER_PREF_TLSV1_2_2025_07: |
| 328 |
case AWS_IO_TLS_CIPHER_PREF_NON_PQ_DEFAULT: |
| 329 |
return true; |
| 330 |
default: |
| 331 |
return false; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static int s_generic_read(struct s2n_handler *handler, struct aws_byte_buf *buf) { |
| 336 |
|
| 337 |
size_t written = 0; |
| 338 |
|
| 339 |
while (!aws_linked_list_empty(&handler->input_queue) && written < buf->len) { |
| 340 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&handler->input_queue); |
| 341 |
struct aws_io_message *message = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle); |
| 342 |
|
| 343 |
size_t remaining_message_len = message->message_data.len - message->copy_mark; |
| 344 |
size_t remaining_buf_len = buf->len - written; |
| 345 |
|
| 346 |
size_t to_write = remaining_message_len < remaining_buf_len ? remaining_message_len : remaining_buf_len; |
| 347 |
|
| 348 |
struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message->message_data); |
| 349 |
aws_byte_cursor_advance(&message_cursor, message->copy_mark); |
| 350 |
aws_byte_cursor_read(&message_cursor, buf->buffer + written, to_write); |
| 351 |
|
| 352 |
written += to_write; |
| 353 |
|
| 354 |
message->copy_mark += to_write; |
| 355 |
|
| 356 |
if (message->copy_mark == message->message_data.len) { |
| 357 |
aws_mem_release(message->allocator, message); |
| 358 |
} else { |
| 359 |
aws_linked_list_push_front(&handler->input_queue, &message->queueing_handle); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if (written) { |
| 364 |
return (int)written; |
| 365 |
} |
| 366 |
|
| 367 |
errno = EAGAIN; |
| 368 |
return -1; |
| 369 |
} |
| 370 |
|
| 371 |
static int s_s2n_handler_recv(void *io_context, uint8_t *buf, uint32_t len) { |
| 372 |
struct s2n_handler *handler = (struct s2n_handler *)io_context; |
| 373 |
|
| 374 |
struct aws_byte_buf read_buffer = aws_byte_buf_from_array(buf, len); |
| 375 |
return s_generic_read(handler, &read_buffer); |
| 376 |
} |
| 377 |
|
| 378 |
static int s_generic_send(struct s2n_handler *handler, struct aws_byte_buf *buf) { |
| 379 |
|
| 380 |
struct aws_byte_cursor buffer_cursor = aws_byte_cursor_from_buf(buf); |
| 381 |
|
| 382 |
size_t processed = 0; |
| 383 |
while (processed < buf->len) { |
| 384 |
const size_t overhead = aws_channel_slot_upstream_message_overhead(handler->slot); |
| 385 |
const size_t message_size_hint = (buf->len - processed) + overhead; |
| 386 |
struct aws_io_message *message = aws_channel_acquire_message_from_pool( |
| 387 |
handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, message_size_hint); |
| 388 |
|
| 389 |
if (message->message_data.capacity <= overhead) { |
| 390 |
aws_mem_release(message->allocator, message); |
| 391 |
errno = ENOMEM; |
| 392 |
return -1; |
| 393 |
} |
| 394 |
|
| 395 |
const size_t available_msg_write_capacity = message->message_data.capacity - overhead; |
| 396 |
const size_t to_write = |
| 397 |
available_msg_write_capacity >= buffer_cursor.len ? buffer_cursor.len : available_msg_write_capacity; |
| 398 |
|
| 399 |
struct aws_byte_cursor chunk = aws_byte_cursor_advance(&buffer_cursor, to_write); |
| 400 |
if (aws_byte_buf_append(&message->message_data, &chunk)) { |
| 401 |
aws_mem_release(message->allocator, message); |
| 402 |
return -1; |
| 403 |
} |
| 404 |
processed += message->message_data.len; |
| 405 |
|
| 406 |
if (processed == buf->len) { |
| 407 |
message->on_completion = handler->latest_message_on_completion; |
| 408 |
message->user_data = handler->latest_message_completion_user_data; |
| 409 |
handler->latest_message_on_completion = NULL; |
| 410 |
handler->latest_message_completion_user_data = NULL; |
| 411 |
} |
| 412 |
|
| 413 |
if (aws_channel_slot_send_message(handler->slot, message, AWS_CHANNEL_DIR_WRITE)) { |
| 414 |
aws_mem_release(message->allocator, message); |
| 415 |
errno = EPIPE; |
| 416 |
return -1; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
if (processed) { |
| 421 |
return (int)processed; |
| 422 |
} |
| 423 |
|
| 424 |
errno = EAGAIN; |
| 425 |
return -1; |
| 426 |
} |
| 427 |
|
| 428 |
static int s_s2n_handler_send(void *io_context, const uint8_t *buf, uint32_t len) { |
| 429 |
struct s2n_handler *handler = (struct s2n_handler *)io_context; |
| 430 |
struct aws_byte_buf send_buf = aws_byte_buf_from_array(buf, len); |
| 431 |
|
| 432 |
return s_generic_send(handler, &send_buf); |
| 433 |
} |
| 434 |
|
| 435 |
static void s_s2n_handler_destroy(struct aws_channel_handler *handler) { |
| 436 |
if (handler) { |
| 437 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 438 |
aws_tls_channel_handler_shared_clean_up(&s2n_handler->shared_state); |
| 439 |
if (s2n_handler->connection) { |
| 440 |
s2n_connection_free(s2n_handler->connection); |
| 441 |
} |
| 442 |
if (s2n_handler->s2n_ctx) { |
| 443 |
aws_tls_ctx_release(&s2n_handler->s2n_ctx->ctx); |
| 444 |
} |
| 445 |
aws_mem_release(handler->alloc, (void *)s2n_handler); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
static void s_on_negotiation_result( |
| 450 |
struct aws_channel_handler *handler, |
| 451 |
struct aws_channel_slot *slot, |
| 452 |
int error_code, |
| 453 |
void *user_data) { |
| 454 |
|
| 455 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 456 |
|
| 457 |
aws_on_tls_negotiation_completed(&s2n_handler->shared_state, error_code); |
| 458 |
|
| 459 |
if (s2n_handler->on_negotiation_result) { |
| 460 |
s2n_handler->on_negotiation_result(handler, slot, error_code, user_data); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
static int s_drive_negotiation(struct aws_channel_handler *handler) { |
| 465 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 466 |
|
| 467 |
AWS_ASSERT(s2n_handler->state == NEGOTIATION_ONGOING); |
| 468 |
|
| 469 |
aws_on_drive_tls_negotiation(&s2n_handler->shared_state); |
| 470 |
|
| 471 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 472 |
do { |
| 473 |
int negotiation_code = s2n_negotiate(s2n_handler->connection, &blocked); |
| 474 |
|
| 475 |
int s2n_error = s2n_errno; |
| 476 |
if (negotiation_code == S2N_ERR_T_OK) { |
| 477 |
s2n_handler->state = NEGOTIATION_SUCCEEDED; |
| 478 |
|
| 479 |
const char *protocol = s2n_get_application_protocol(s2n_handler->connection); |
| 480 |
if (protocol) { |
| 481 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Alpn protocol negotiated as %s", (void *)handler, protocol); |
| 482 |
s2n_handler->protocol = aws_byte_buf_from_c_str(protocol); |
| 483 |
} |
| 484 |
|
| 485 |
const char *server_name = s2n_get_server_name(s2n_handler->connection); |
| 486 |
|
| 487 |
if (server_name) { |
| 488 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Remote server name is %s", (void *)handler, server_name); |
| 489 |
s2n_handler->server_name = aws_byte_buf_from_c_str(server_name); |
| 490 |
} |
| 491 |
|
| 492 |
if (s2n_handler->slot->adj_right && s2n_handler->advertise_alpn_message && protocol) { |
| 493 |
struct aws_io_message *message = aws_channel_acquire_message_from_pool( |
| 494 |
s2n_handler->slot->channel, |
| 495 |
AWS_IO_MESSAGE_APPLICATION_DATA, |
| 496 |
sizeof(struct aws_tls_negotiated_protocol_message)); |
| 497 |
message->message_tag = AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE; |
| 498 |
struct aws_tls_negotiated_protocol_message *protocol_message = |
| 499 |
(struct aws_tls_negotiated_protocol_message *)message->message_data.buffer; |
| 500 |
|
| 501 |
protocol_message->protocol = s2n_handler->protocol; |
| 502 |
message->message_data.len = sizeof(struct aws_tls_negotiated_protocol_message); |
| 503 |
if (aws_channel_slot_send_message(s2n_handler->slot, message, AWS_CHANNEL_DIR_READ)) { |
| 504 |
aws_mem_release(message->allocator, message); |
| 505 |
aws_channel_shutdown(s2n_handler->slot->channel, aws_last_error()); |
| 506 |
return AWS_OP_SUCCESS; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
s_on_negotiation_result(handler, s2n_handler->slot, AWS_OP_SUCCESS, s2n_handler->user_data); |
| 511 |
|
| 512 |
break; |
| 513 |
} |
| 514 |
if (s2n_error_get_type(s2n_error) != S2N_ERR_T_BLOCKED) { |
| 515 |
AWS_LOGF_WARN( |
| 516 |
AWS_LS_IO_TLS, |
| 517 |
"id=%p: negotiation failed with error %s (%s)", |
| 518 |
(void *)handler, |
| 519 |
s2n_strerror(s2n_error, "EN"), |
| 520 |
s2n_strerror_debug(s2n_error, "EN")); |
| 521 |
|
| 522 |
if (s2n_error_get_type(s2n_error) == S2N_ERR_T_ALERT) { |
| 523 |
AWS_LOGF_DEBUG( |
| 524 |
AWS_LS_IO_TLS, |
| 525 |
"id=%p: Alert code %d", |
| 526 |
(void *)handler, |
| 527 |
s2n_connection_get_alert(s2n_handler->connection)); |
| 528 |
} |
| 529 |
|
| 530 |
const char *err_str = s2n_strerror_debug(s2n_error, NULL); |
| 531 |
(void)err_str; |
| 532 |
s2n_handler->state = NEGOTIATION_FAILED; |
| 533 |
|
| 534 |
aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 535 |
|
| 536 |
s_on_negotiation_result( |
| 537 |
handler, s2n_handler->slot, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE, s2n_handler->user_data); |
| 538 |
|
| 539 |
return AWS_OP_ERR; |
| 540 |
} |
| 541 |
} while (blocked == S2N_NOT_BLOCKED); |
| 542 |
|
| 543 |
return AWS_OP_SUCCESS; |
| 544 |
} |
| 545 |
|
| 546 |
static void s_negotiation_task(struct aws_channel_task *task, void *arg, aws_task_status status) { |
| 547 |
task->task_fn = NULL; |
| 548 |
task->arg = NULL; |
| 549 |
|
| 550 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 551 |
struct aws_channel_handler *handler = arg; |
| 552 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 553 |
if (s2n_handler->state == NEGOTIATION_ONGOING) { |
| 554 |
s_drive_negotiation(handler); |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static int s_tls_client_handler_start_negotiation(struct aws_channel_handler *handler) { |
| 560 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 561 |
|
| 562 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Kicking off TLS negotiation.", (void *)handler); |
| 563 |
if (aws_channel_thread_is_callers_thread(s2n_handler->slot->channel)) { |
| 564 |
if (s2n_handler->state == NEGOTIATION_ONGOING) { |
| 565 |
s_drive_negotiation(handler); |
| 566 |
} |
| 567 |
return AWS_OP_SUCCESS; |
| 568 |
} |
| 569 |
|
| 570 |
aws_channel_task_init( |
| 571 |
&s2n_handler->sequential_tasks, s_negotiation_task, handler, "s2n_channel_handler_negotiation"); |
| 572 |
aws_channel_schedule_task_now(s2n_handler->slot->channel, &s2n_handler->sequential_tasks); |
| 573 |
|
| 574 |
return AWS_OP_SUCCESS; |
| 575 |
} |
| 576 |
|
| 577 |
static int s_s2n_handler_process_read_message( |
| 578 |
struct aws_channel_handler *handler, |
| 579 |
struct aws_channel_slot *slot, |
| 580 |
struct aws_io_message *message) { |
| 581 |
|
| 582 |
struct s2n_handler *s2n_handler = handler->impl; |
| 583 |
|
| 584 |
if (s2n_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 585 |
if (message) { |
| 586 |
aws_mem_release(message->allocator, message); |
| 587 |
} |
| 588 |
return AWS_OP_SUCCESS; |
| 589 |
} |
| 590 |
|
| 591 |
if (AWS_UNLIKELY(s2n_handler->state == NEGOTIATION_FAILED)) { |
| 592 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 593 |
} |
| 594 |
|
| 595 |
if (message) { |
| 596 |
aws_linked_list_push_back(&s2n_handler->input_queue, &message->queueing_handle); |
| 597 |
|
| 598 |
if (s2n_handler->state == NEGOTIATION_ONGOING) { |
| 599 |
size_t message_len = message->message_data.len; |
| 600 |
if (s_drive_negotiation(handler) == AWS_OP_SUCCESS) { |
| 601 |
aws_channel_slot_increment_read_window(slot, message_len); |
| 602 |
} else { |
| 603 |
aws_channel_shutdown(s2n_handler->slot->channel, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 604 |
} |
| 605 |
return AWS_OP_SUCCESS; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 610 |
size_t downstream_window = SIZE_MAX; |
| 611 |
if (slot->adj_right) { |
| 612 |
downstream_window = aws_channel_slot_downstream_read_window(slot); |
| 613 |
} |
| 614 |
int shutdown_error_code = 0; |
| 615 |
|
| 616 |
size_t processed = 0; |
| 617 |
AWS_LOGF_TRACE( |
| 618 |
AWS_LS_IO_TLS, "id=%p: Downstream window %llu", (void *)handler, (unsigned long long)downstream_window); |
| 619 |
|
| 620 |
while (processed < downstream_window) { |
| 621 |
|
| 622 |
struct aws_io_message *outgoing_read_message = aws_channel_acquire_message_from_pool( |
| 623 |
slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, downstream_window - processed); |
| 624 |
|
| 625 |
ssize_t read = s2n_recv( |
| 626 |
s2n_handler->connection, |
| 627 |
outgoing_read_message->message_data.buffer, |
| 628 |
outgoing_read_message->message_data.capacity, |
| 629 |
&blocked); |
| 630 |
|
| 631 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Bytes read %lld", (void *)handler, (long long)read); |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
if (read == 0) { |
| 640 |
AWS_LOGF_DEBUG( |
| 641 |
AWS_LS_IO_TLS, |
| 642 |
"id=%p: Alert code %d", |
| 643 |
(void *)handler, |
| 644 |
s2n_connection_get_alert(s2n_handler->connection)); |
| 645 |
aws_mem_release(outgoing_read_message->allocator, outgoing_read_message); |
| 646 |
goto shutdown_channel; |
| 647 |
} |
| 648 |
|
| 649 |
if (read < 0) { |
| 650 |
aws_mem_release(outgoing_read_message->allocator, outgoing_read_message); |
| 651 |
|
| 652 |
|
| 653 |
if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED) { |
| 654 |
if (s2n_handler->read_state == AWS_TLS_HANDLER_READ_SHUTTING_DOWN) { |
| 655 |
|
| 656 |
goto shutdown_channel; |
| 657 |
} |
| 658 |
break; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
AWS_LOGF_ERROR( |
| 663 |
AWS_LS_IO_TLS, |
| 664 |
"id=%p: S2N failed to read with error: %s (%s)", |
| 665 |
(void *)handler, |
| 666 |
s2n_strerror(s2n_errno, "EN"), |
| 667 |
s2n_strerror_debug(s2n_errno, "EN")); |
| 668 |
shutdown_error_code = AWS_IO_TLS_ERROR_READ_FAILURE; |
| 669 |
goto shutdown_channel; |
| 670 |
}; |
| 671 |
|
| 672 |
|
| 673 |
processed += read; |
| 674 |
outgoing_read_message->message_data.len = (size_t)read; |
| 675 |
|
| 676 |
if (s2n_handler->on_data_read) { |
| 677 |
s2n_handler->on_data_read(handler, slot, &outgoing_read_message->message_data, s2n_handler->user_data); |
| 678 |
} |
| 679 |
|
| 680 |
if (slot->adj_right) { |
| 681 |
aws_channel_slot_send_message(slot, outgoing_read_message, AWS_CHANNEL_DIR_READ); |
| 682 |
} else { |
| 683 |
aws_mem_release(outgoing_read_message->allocator, outgoing_read_message); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
AWS_LOGF_TRACE( |
| 688 |
AWS_LS_IO_TLS, |
| 689 |
"id=%p: Remaining window for this event-loop tick: %llu", |
| 690 |
(void *)handler, |
| 691 |
(unsigned long long)downstream_window - processed); |
| 692 |
|
| 693 |
return AWS_OP_SUCCESS; |
| 694 |
|
| 695 |
shutdown_channel: |
| 696 |
if (s2n_handler->read_state == AWS_TLS_HANDLER_READ_SHUTTING_DOWN) { |
| 697 |
if (s2n_handler->shutdown_error_code != 0) { |
| 698 |
|
| 699 |
shutdown_error_code = s2n_handler->shutdown_error_code; |
| 700 |
} |
| 701 |
s2n_handler->read_state = AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE; |
| 702 |
aws_channel_slot_on_handler_shutdown_complete(slot, AWS_CHANNEL_DIR_READ, shutdown_error_code, false); |
| 703 |
} else { |
| 704 |
|
| 705 |
aws_channel_shutdown(slot->channel, shutdown_error_code); |
| 706 |
} |
| 707 |
return AWS_OP_SUCCESS; |
| 708 |
} |
| 709 |
|
| 710 |
static int s_s2n_handler_process_write_message( |
| 711 |
struct aws_channel_handler *handler, |
| 712 |
struct aws_channel_slot *slot, |
| 713 |
struct aws_io_message *message) { |
| 714 |
(void)slot; |
| 715 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 716 |
|
| 717 |
if (AWS_UNLIKELY(s2n_handler->state != NEGOTIATION_SUCCEEDED)) { |
| 718 |
return aws_raise_error(AWS_IO_TLS_ERROR_NOT_NEGOTIATED); |
| 719 |
} |
| 720 |
|
| 721 |
s2n_handler->latest_message_on_completion = message->on_completion; |
| 722 |
s2n_handler->latest_message_completion_user_data = message->user_data; |
| 723 |
|
| 724 |
s2n_blocked_status blocked; |
| 725 |
ssize_t write_code = |
| 726 |
s2n_send(s2n_handler->connection, message->message_data.buffer, (ssize_t)message->message_data.len, &blocked); |
| 727 |
|
| 728 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Bytes written: %llu", (void *)handler, (unsigned long long)write_code); |
| 729 |
|
| 730 |
ssize_t message_len = (ssize_t)message->message_data.len; |
| 731 |
|
| 732 |
if (write_code < message_len) { |
| 733 |
return aws_raise_error(AWS_IO_TLS_ERROR_WRITE_FAILURE); |
| 734 |
} |
| 735 |
|
| 736 |
aws_mem_release(message->allocator, message); |
| 737 |
|
| 738 |
return AWS_OP_SUCCESS; |
| 739 |
} |
| 740 |
|
| 741 |
static void s_delayed_shutdown_task_fn(struct aws_channel_task *channel_task, void *arg, enum aws_task_status status) { |
| 742 |
(void)channel_task; |
| 743 |
|
| 744 |
struct aws_channel_handler *handler = arg; |
| 745 |
struct s2n_handler *s2n_handler = handler->impl; |
| 746 |
|
| 747 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 748 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Delayed shut down in write direction", (void *)handler); |
| 749 |
s2n_blocked_status blocked; |
| 750 |
|
| 751 |
s2n_shutdown(s2n_handler->connection, &blocked); |
| 752 |
} |
| 753 |
aws_channel_slot_on_handler_shutdown_complete( |
| 754 |
s2n_handler->slot, AWS_CHANNEL_DIR_WRITE, s2n_handler->shutdown_error_code, false); |
| 755 |
} |
| 756 |
|
| 757 |
static enum aws_tls_signature_algorithm s_s2n_to_aws_signature_algorithm(s2n_tls_signature_algorithm s2n_alg) { |
| 758 |
switch (s2n_alg) { |
| 759 |
case S2N_TLS_SIGNATURE_RSA: |
| 760 |
return AWS_TLS_SIGNATURE_RSA; |
| 761 |
case S2N_TLS_SIGNATURE_ECDSA: |
| 762 |
return AWS_TLS_SIGNATURE_ECDSA; |
| 763 |
default: |
| 764 |
return AWS_TLS_SIGNATURE_UNKNOWN; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
static enum aws_tls_hash_algorithm s_s2n_to_aws_hash_algorithm(s2n_tls_hash_algorithm s2n_alg) { |
| 769 |
switch (s2n_alg) { |
| 770 |
case (S2N_TLS_HASH_SHA1): |
| 771 |
return AWS_TLS_HASH_SHA1; |
| 772 |
case (S2N_TLS_HASH_SHA224): |
| 773 |
return AWS_TLS_HASH_SHA224; |
| 774 |
case (S2N_TLS_HASH_SHA256): |
| 775 |
return AWS_TLS_HASH_SHA256; |
| 776 |
case (S2N_TLS_HASH_SHA384): |
| 777 |
return AWS_TLS_HASH_SHA384; |
| 778 |
case (S2N_TLS_HASH_SHA512): |
| 779 |
return AWS_TLS_HASH_SHA512; |
| 780 |
default: |
| 781 |
return AWS_TLS_HASH_UNKNOWN; |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
static void s_tls_key_operation_destroy(struct aws_tls_key_operation *operation) { |
| 786 |
if (operation->s2n_op) { |
| 787 |
s2n_async_pkey_op_free(operation->s2n_op); |
| 788 |
} |
| 789 |
if (operation->s2n_handler) { |
| 790 |
aws_channel_release_hold(operation->s2n_handler->slot->channel); |
| 791 |
} |
| 792 |
aws_byte_buf_clean_up(&operation->input_data); |
| 793 |
aws_mem_release(operation->alloc, operation); |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
static void s_tls_key_operation_completion_task( |
| 800 |
struct aws_channel_task *channel_task, |
| 801 |
void *arg, |
| 802 |
enum aws_task_status status) { |
| 803 |
|
| 804 |
(void)channel_task; |
| 805 |
struct aws_tls_key_operation *operation = arg; |
| 806 |
struct s2n_handler *s2n_handler = operation->s2n_handler; |
| 807 |
struct aws_channel_handler *handler = &s2n_handler->handler; |
| 808 |
|
| 809 |
|
| 810 |
if (status != AWS_TASK_STATUS_RUN_READY || s2n_handler->state != NEGOTIATION_ONGOING) { |
| 811 |
goto clean_up; |
| 812 |
} |
| 813 |
|
| 814 |
if (operation->completion_error_code == 0) { |
| 815 |
if (s2n_async_pkey_op_apply(operation->s2n_op, s2n_handler->connection)) { |
| 816 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed applying s2n async pkey op", (void *)handler); |
| 817 |
operation->completion_error_code = AWS_ERROR_INVALID_STATE; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
if (operation->completion_error_code == 0) { |
| 822 |
s_drive_negotiation(handler); |
| 823 |
} else { |
| 824 |
aws_channel_shutdown(s2n_handler->slot->channel, operation->completion_error_code); |
| 825 |
} |
| 826 |
|
| 827 |
clean_up: |
| 828 |
s_tls_key_operation_destroy(operation); |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
static void s_tls_key_operation_complete_common( |
| 834 |
struct aws_tls_key_operation *operation, |
| 835 |
int error_code, |
| 836 |
const struct aws_byte_cursor *output) { |
| 837 |
|
| 838 |
AWS_ASSERT((error_code != 0) ^ (output != NULL)); |
| 839 |
|
| 840 |
|
| 841 |
size_t complete_count = aws_atomic_fetch_add(&operation->complete_count, 1); |
| 842 |
AWS_FATAL_ASSERT(complete_count == 0 && "TLS key operation marked complete multiple times"); |
| 843 |
|
| 844 |
struct s2n_handler *s2n_handler = operation->s2n_handler; |
| 845 |
struct aws_channel_handler *handler = &s2n_handler->handler; |
| 846 |
|
| 847 |
if (output != NULL) { |
| 848 |
|
| 849 |
if (s2n_async_pkey_op_set_output(operation->s2n_op, output->ptr, output->len)) { |
| 850 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed setting output on s2n async pkey op", (void *)handler); |
| 851 |
error_code = AWS_ERROR_INVALID_STATE; |
| 852 |
goto done; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
done: |
| 857 |
operation->completion_error_code = error_code; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
aws_channel_task_init( |
| 865 |
&operation->completion_task, |
| 866 |
s_tls_key_operation_completion_task, |
| 867 |
operation, |
| 868 |
"tls_key_operation_completion_task"); |
| 869 |
aws_channel_schedule_task_now(s2n_handler->slot->channel, &operation->completion_task); |
| 870 |
} |
| 871 |
|
| 872 |
static void s_tls_key_operation_complete(struct aws_tls_key_operation *operation, struct aws_byte_cursor output) { |
| 873 |
if (operation == NULL) { |
| 874 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Operation complete: operation is null and therefore cannot be set to complete!"); |
| 875 |
return; |
| 876 |
} |
| 877 |
|
| 878 |
AWS_LOGF_DEBUG( |
| 879 |
AWS_LS_IO_TLS, |
| 880 |
"id=%p: TLS key operation complete with %zu bytes of output data", |
| 881 |
(void *)operation->s2n_handler, |
| 882 |
output.len); |
| 883 |
s_tls_key_operation_complete_common(operation, 0, &output); |
| 884 |
} |
| 885 |
|
| 886 |
static void s_tls_key_operation_complete_with_error(struct aws_tls_key_operation *operation, int error_code) { |
| 887 |
if (operation == NULL) { |
| 888 |
AWS_LOGF_ERROR( |
| 889 |
AWS_LS_IO_TLS, "Operation complete with error: operation is null and therefore cannot be set to complete!"); |
| 890 |
return; |
| 891 |
} |
| 892 |
|
| 893 |
if (error_code == 0) { |
| 894 |
error_code = AWS_ERROR_UNKNOWN; |
| 895 |
AWS_LOGF_ERROR( |
| 896 |
AWS_LS_IO_TLS, |
| 897 |
"id=%p: TLS key operation completed with error, but no error-code set. Using %s", |
| 898 |
(void *)operation->s2n_handler, |
| 899 |
aws_error_name(error_code)); |
| 900 |
} |
| 901 |
|
| 902 |
AWS_LOGF_ERROR( |
| 903 |
AWS_LS_IO_TLS, |
| 904 |
"id=%p: TLS key operation complete with error %s", |
| 905 |
(void *)operation->s2n_handler, |
| 906 |
aws_error_name(error_code)); |
| 907 |
|
| 908 |
s_tls_key_operation_complete_common(operation, error_code, NULL); |
| 909 |
} |
| 910 |
|
| 911 |
static struct aws_tls_key_operation *s_tls_key_operation_new( |
| 912 |
struct aws_channel_handler *handler, |
| 913 |
struct s2n_async_pkey_op *s2n_op) { |
| 914 |
|
| 915 |
struct s2n_handler *s2n_handler = handler->impl; |
| 916 |
|
| 917 |
struct aws_tls_key_operation *operation = aws_mem_calloc(handler->alloc, 1, sizeof(struct aws_tls_key_operation)); |
| 918 |
operation->alloc = handler->alloc; |
| 919 |
|
| 920 |
|
| 921 |
uint32_t input_size = 0; |
| 922 |
if (s2n_async_pkey_op_get_input_size(s2n_op, &input_size)) { |
| 923 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed querying s2n async pkey op size", (void *)handler); |
| 924 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 925 |
goto error; |
| 926 |
} |
| 927 |
|
| 928 |
aws_byte_buf_init(&operation->input_data, operation->alloc, input_size); |
| 929 |
if (s2n_async_pkey_op_get_input(s2n_op, operation->input_data.buffer, input_size)) { |
| 930 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed querying s2n async pkey input", (void *)handler); |
| 931 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 932 |
goto error; |
| 933 |
} |
| 934 |
operation->input_data.len = input_size; |
| 935 |
|
| 936 |
|
| 937 |
s2n_async_pkey_op_type s2n_op_type = 0; |
| 938 |
if (s2n_async_pkey_op_get_op_type(s2n_op, &s2n_op_type)) { |
| 939 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed querying s2n async pkey op type", (void *)handler); |
| 940 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 941 |
goto error; |
| 942 |
} |
| 943 |
|
| 944 |
if (s2n_op_type == S2N_ASYNC_SIGN) { |
| 945 |
operation->operation_type = AWS_TLS_KEY_OPERATION_SIGN; |
| 946 |
|
| 947 |
|
| 948 |
s2n_tls_signature_algorithm s2n_sign_alg = 0; |
| 949 |
if (s2n_connection_get_selected_client_cert_signature_algorithm(s2n_handler->connection, &s2n_sign_alg)) { |
| 950 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed getting s2n client cert signature algorithm", (void *)handler); |
| 951 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 952 |
goto error; |
| 953 |
} |
| 954 |
|
| 955 |
operation->signature_algorithm = s_s2n_to_aws_signature_algorithm(s2n_sign_alg); |
| 956 |
if (operation->signature_algorithm == AWS_TLS_SIGNATURE_UNKNOWN) { |
| 957 |
AWS_LOGF_ERROR( |
| 958 |
AWS_LS_IO_TLS, |
| 959 |
"id=%p: Cannot sign with s2n_tls_signature_algorithm=%d. Algorithm currently unsupported", |
| 960 |
(void *)handler, |
| 961 |
s2n_sign_alg); |
| 962 |
aws_raise_error(AWS_IO_TLS_SIGNATURE_ALGORITHM_UNSUPPORTED); |
| 963 |
goto error; |
| 964 |
} |
| 965 |
|
| 966 |
s2n_tls_hash_algorithm s2n_digest_alg = 0; |
| 967 |
if (s2n_connection_get_selected_client_cert_digest_algorithm(s2n_handler->connection, &s2n_digest_alg)) { |
| 968 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed getting s2n client cert digest algorithm", (void *)handler); |
| 969 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 970 |
goto error; |
| 971 |
} |
| 972 |
|
| 973 |
operation->digest_algorithm = s_s2n_to_aws_hash_algorithm(s2n_digest_alg); |
| 974 |
if (operation->digest_algorithm == AWS_TLS_HASH_UNKNOWN) { |
| 975 |
AWS_LOGF_ERROR( |
| 976 |
AWS_LS_IO_TLS, |
| 977 |
"id=%p: Cannot sign digest created with s2n_tls_hash_algorithm=%d. Algorithm currently unsupported", |
| 978 |
(void *)handler, |
| 979 |
s2n_digest_alg); |
| 980 |
aws_raise_error(AWS_IO_TLS_DIGEST_ALGORITHM_UNSUPPORTED); |
| 981 |
goto error; |
| 982 |
} |
| 983 |
|
| 984 |
} else if (s2n_op_type == S2N_ASYNC_DECRYPT) { |
| 985 |
operation->operation_type = AWS_TLS_KEY_OPERATION_DECRYPT; |
| 986 |
|
| 987 |
} else { |
| 988 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Unknown s2n async pkey op type:%d", (void *)handler, (int)s2n_op_type); |
| 989 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 990 |
goto error; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
operation->s2n_handler = s2n_handler; |
| 995 |
aws_channel_acquire_hold(s2n_handler->slot->channel); |
| 996 |
|
| 997 |
|
| 998 |
aws_atomic_init_int(&operation->complete_count, 0); |
| 999 |
|
| 1000 |
|
| 1001 |
operation->s2n_op = s2n_op; |
| 1002 |
|
| 1003 |
return operation; |
| 1004 |
error: |
| 1005 |
s_tls_key_operation_destroy(operation); |
| 1006 |
return NULL; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static struct aws_byte_cursor s_tls_key_operation_get_input(const struct aws_tls_key_operation *operation) { |
| 1010 |
return aws_byte_cursor_from_buf(&operation->input_data); |
| 1011 |
} |
| 1012 |
|
| 1013 |
static enum aws_tls_key_operation_type s_tls_key_operation_get_type(const struct aws_tls_key_operation *operation) { |
| 1014 |
return operation->operation_type; |
| 1015 |
} |
| 1016 |
|
| 1017 |
static enum aws_tls_signature_algorithm s_tls_key_operation_get_signature_algorithm( |
| 1018 |
const struct aws_tls_key_operation *operation) { |
| 1019 |
return operation->signature_algorithm; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static enum aws_tls_hash_algorithm s_tls_key_operation_get_digest_algorithm( |
| 1023 |
const struct aws_tls_key_operation *operation) { |
| 1024 |
return operation->digest_algorithm; |
| 1025 |
} |
| 1026 |
|
| 1027 |
static int s_s2n_async_pkey_callback(struct s2n_connection *conn, struct s2n_async_pkey_op *s2n_op) { |
| 1028 |
struct s2n_handler *s2n_handler = s2n_connection_get_ctx(conn); |
| 1029 |
struct aws_channel_handler *handler = &s2n_handler->handler; |
| 1030 |
|
| 1031 |
AWS_ASSERT(conn == s2n_handler->connection); |
| 1032 |
(void)conn; |
| 1033 |
|
| 1034 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: s2n async pkey callback received", (void *)handler); |
| 1035 |
|
| 1036 |
|
| 1037 |
struct aws_tls_key_operation *operation = s_tls_key_operation_new(handler, s2n_op); |
| 1038 |
if (operation == NULL) { |
| 1039 |
s2n_async_pkey_op_free(s2n_op); |
| 1040 |
return S2N_FAILURE; |
| 1041 |
} |
| 1042 |
|
| 1043 |
AWS_LOGF_DEBUG( |
| 1044 |
AWS_LS_IO_TLS, |
| 1045 |
"id=%p: Begin TLS key operation. type=%s input_data.len=%zu signature=%s digest=%s", |
| 1046 |
(void *)operation, |
| 1047 |
aws_tls_key_operation_type_str(operation->operation_type), |
| 1048 |
operation->input_data.len, |
| 1049 |
aws_tls_signature_algorithm_str(operation->signature_algorithm), |
| 1050 |
aws_tls_hash_algorithm_str(operation->digest_algorithm)); |
| 1051 |
|
| 1052 |
aws_custom_key_op_handler_perform_operation(s2n_handler->s2n_ctx->custom_key_handler, operation); |
| 1053 |
|
| 1054 |
return S2N_SUCCESS; |
| 1055 |
} |
| 1056 |
|
| 1057 |
static int s_s2n_do_delayed_shutdown( |
| 1058 |
struct aws_channel_handler *handler, |
| 1059 |
struct aws_channel_slot *slot, |
| 1060 |
int error_code) { |
| 1061 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 1062 |
|
| 1063 |
s2n_handler->shutdown_error_code = error_code; |
| 1064 |
|
| 1065 |
uint64_t shutdown_delay = s2n_connection_get_delay(s2n_handler->connection); |
| 1066 |
uint64_t now = 0; |
| 1067 |
|
| 1068 |
if (aws_channel_current_clock_time(slot->channel, &now)) { |
| 1069 |
return AWS_OP_ERR; |
| 1070 |
} |
| 1071 |
|
| 1072 |
uint64_t shutdown_time = aws_add_u64_saturating(shutdown_delay, now); |
| 1073 |
aws_channel_schedule_task_future(slot->channel, &s2n_handler->delayed_shutdown_task, shutdown_time); |
| 1074 |
|
| 1075 |
return AWS_OP_SUCCESS; |
| 1076 |
} |
| 1077 |
|
| 1078 |
static void s_run_read(struct aws_channel_task *task, void *arg, aws_task_status status) { |
| 1079 |
task->task_fn = NULL; |
| 1080 |
task->arg = NULL; |
| 1081 |
|
| 1082 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 1083 |
struct aws_channel_handler *handler = (struct aws_channel_handler *)arg; |
| 1084 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 1085 |
s2n_handler->read_task_pending = false; |
| 1086 |
s_s2n_handler_process_read_message(handler, s2n_handler->slot, NULL); |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
static void s_initialize_read_delay_shutdown( |
| 1091 |
struct aws_channel_handler *handler, |
| 1092 |
struct aws_channel_slot *slot, |
| 1093 |
int error_code) { |
| 1094 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
AWS_LOGF_DEBUG( |
| 1100 |
AWS_LS_IO_TLS, |
| 1101 |
"id=%p: TLS handler still have pending data to be delivered during shutdown. Wait until downstream " |
| 1102 |
"reads the data.", |
| 1103 |
(void *)handler); |
| 1104 |
if (aws_channel_slot_downstream_read_window(slot) == 0) { |
| 1105 |
AWS_LOGF_WARN( |
| 1106 |
AWS_LS_IO_TLS, |
| 1107 |
"id=%p: TLS shutdown delayed. Pending data cannot be processed until the flow-control window opens. " |
| 1108 |
" Your application may hang if the read window never opens", |
| 1109 |
(void *)handler); |
| 1110 |
} |
| 1111 |
s2n_handler->read_state = AWS_TLS_HANDLER_READ_SHUTTING_DOWN; |
| 1112 |
s2n_handler->shutdown_error_code = error_code; |
| 1113 |
if (!s2n_handler->read_task_pending) { |
| 1114 |
|
| 1115 |
|
| 1116 |
s2n_handler->read_task_pending = true; |
| 1117 |
aws_channel_task_init( |
| 1118 |
&s2n_handler->read_task, s_run_read, handler, "s2n_channel_handler_read_on_delay_shutdown"); |
| 1119 |
aws_channel_schedule_task_now(slot->channel, &s2n_handler->read_task); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
static int s_s2n_handler_shutdown( |
| 1124 |
struct aws_channel_handler *handler, |
| 1125 |
struct aws_channel_slot *slot, |
| 1126 |
enum aws_channel_direction dir, |
| 1127 |
int error_code, |
| 1128 |
bool abort_immediately) { |
| 1129 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 1130 |
|
| 1131 |
if (dir == AWS_CHANNEL_DIR_READ) { |
| 1132 |
AWS_LOGF_DEBUG( |
| 1133 |
AWS_LS_IO_TLS, "id=%p: Shutting down read direction with error code %d", (void *)handler, error_code); |
| 1134 |
|
| 1135 |
|
| 1136 |
if (s2n_handler->state == NEGOTIATION_ONGOING) { |
| 1137 |
s2n_handler->state = NEGOTIATION_FAILED; |
| 1138 |
} |
| 1139 |
|
| 1140 |
if (!abort_immediately && s2n_handler->state == NEGOTIATION_SUCCEEDED && |
| 1141 |
!aws_linked_list_empty(&s2n_handler->input_queue) && slot->adj_right) { |
| 1142 |
s_initialize_read_delay_shutdown(handler, slot, error_code); |
| 1143 |
return AWS_OP_SUCCESS; |
| 1144 |
} |
| 1145 |
s2n_handler->read_state = AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE; |
| 1146 |
} else { |
| 1147 |
|
| 1148 |
if (!abort_immediately && error_code != AWS_IO_SOCKET_CLOSED) { |
| 1149 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Scheduling delayed write direction shutdown", (void *)handler); |
| 1150 |
if (s_s2n_do_delayed_shutdown(handler, slot, error_code) == AWS_OP_SUCCESS) { |
| 1151 |
return AWS_OP_SUCCESS; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
while (!aws_linked_list_empty(&s2n_handler->input_queue)) { |
| 1156 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&s2n_handler->input_queue); |
| 1157 |
struct aws_io_message *message = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle); |
| 1158 |
aws_mem_release(message->allocator, message); |
| 1159 |
} |
| 1160 |
|
| 1161 |
return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, abort_immediately); |
| 1162 |
} |
| 1163 |
|
| 1164 |
static int s_s2n_handler_increment_read_window( |
| 1165 |
struct aws_channel_handler *handler, |
| 1166 |
struct aws_channel_slot *slot, |
| 1167 |
size_t size) { |
| 1168 |
(void)size; |
| 1169 |
struct s2n_handler *s2n_handler = handler->impl; |
| 1170 |
if (s2n_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 1171 |
return AWS_OP_SUCCESS; |
| 1172 |
} |
| 1173 |
|
| 1174 |
size_t downstream_size = aws_channel_slot_downstream_read_window(slot); |
| 1175 |
size_t current_window_size = slot->window_size; |
| 1176 |
|
| 1177 |
AWS_LOGF_TRACE( |
| 1178 |
AWS_LS_IO_TLS, "id=%p: Increment read window message received %llu", (void *)handler, (unsigned long long)size); |
| 1179 |
|
| 1180 |
size_t likely_records_count = (size_t)ceil((double)(downstream_size) / (double)(MAX_RECORD_SIZE)); |
| 1181 |
size_t offset_size = aws_mul_size_saturating(likely_records_count, EST_TLS_RECORD_OVERHEAD); |
| 1182 |
size_t total_desired_size = aws_add_size_saturating(offset_size, downstream_size); |
| 1183 |
|
| 1184 |
if (total_desired_size > current_window_size) { |
| 1185 |
size_t window_update_size = total_desired_size - current_window_size; |
| 1186 |
AWS_LOGF_TRACE( |
| 1187 |
AWS_LS_IO_TLS, |
| 1188 |
"id=%p: Propagating read window increment of size %llu", |
| 1189 |
(void *)handler, |
| 1190 |
(unsigned long long)window_update_size); |
| 1191 |
aws_channel_slot_increment_read_window(slot, window_update_size); |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (s2n_handler->state == NEGOTIATION_SUCCEEDED && !s2n_handler->read_task_pending) { |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
s2n_handler->read_task_pending = true; |
| 1202 |
aws_channel_task_init( |
| 1203 |
&s2n_handler->read_task, s_run_read, handler, "s2n_channel_handler_read_on_window_increment"); |
| 1204 |
aws_channel_schedule_task_now(slot->channel, &s2n_handler->read_task); |
| 1205 |
} |
| 1206 |
|
| 1207 |
return AWS_OP_SUCCESS; |
| 1208 |
} |
| 1209 |
|
| 1210 |
static size_t s_s2n_handler_message_overhead(struct aws_channel_handler *handler) { |
| 1211 |
(void)handler; |
| 1212 |
return EST_TLS_RECORD_OVERHEAD; |
| 1213 |
} |
| 1214 |
|
| 1215 |
static size_t s_s2n_handler_initial_window_size(struct aws_channel_handler *handler) { |
| 1216 |
(void)handler; |
| 1217 |
|
| 1218 |
return EST_HANDSHAKE_SIZE; |
| 1219 |
} |
| 1220 |
|
| 1221 |
static void s_s2n_handler_reset_statistics(struct aws_channel_handler *handler) { |
| 1222 |
struct s2n_handler *s2n_handler = handler->impl; |
| 1223 |
|
| 1224 |
aws_crt_statistics_tls_reset(&s2n_handler->shared_state.stats); |
| 1225 |
} |
| 1226 |
|
| 1227 |
static void s_s2n_handler_gather_statistics(struct aws_channel_handler *handler, struct aws_array_list *stats) { |
| 1228 |
struct s2n_handler *s2n_handler = handler->impl; |
| 1229 |
|
| 1230 |
void *stats_base = &s2n_handler->shared_state.stats; |
| 1231 |
aws_array_list_push_back(stats, &stats_base); |
| 1232 |
} |
| 1233 |
|
| 1234 |
static struct aws_byte_buf s_tls_handler_protocol(struct aws_channel_handler *handler) { |
| 1235 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 1236 |
return s2n_handler->protocol; |
| 1237 |
} |
| 1238 |
|
| 1239 |
static struct aws_byte_buf s_tls_handler_server_name(struct aws_channel_handler *handler) { |
| 1240 |
struct s2n_handler *s2n_handler = (struct s2n_handler *)handler->impl; |
| 1241 |
return s2n_handler->server_name; |
| 1242 |
} |
| 1243 |
|
| 1244 |
static struct aws_channel_handler_vtable s_handler_vtable = { |
| 1245 |
.destroy = s_s2n_handler_destroy, |
| 1246 |
.process_read_message = s_s2n_handler_process_read_message, |
| 1247 |
.process_write_message = s_s2n_handler_process_write_message, |
| 1248 |
.shutdown = s_s2n_handler_shutdown, |
| 1249 |
.increment_read_window = s_s2n_handler_increment_read_window, |
| 1250 |
.initial_window_size = s_s2n_handler_initial_window_size, |
| 1251 |
.message_overhead = s_s2n_handler_message_overhead, |
| 1252 |
.reset_statistics = s_s2n_handler_reset_statistics, |
| 1253 |
.gather_statistics = s_s2n_handler_gather_statistics, |
| 1254 |
}; |
| 1255 |
|
| 1256 |
static int s_parse_protocol_preferences( |
| 1257 |
struct aws_string *alpn_list_str, |
| 1258 |
const char protocol_output[4][128], |
| 1259 |
size_t *protocol_count) { |
| 1260 |
size_t max_count = *protocol_count; |
| 1261 |
*protocol_count = 0; |
| 1262 |
|
| 1263 |
struct aws_byte_cursor alpn_list_buffer[4]; |
| 1264 |
AWS_ZERO_ARRAY(alpn_list_buffer); |
| 1265 |
struct aws_array_list alpn_list; |
| 1266 |
struct aws_byte_cursor user_alpn_str = aws_byte_cursor_from_string(alpn_list_str); |
| 1267 |
|
| 1268 |
aws_array_list_init_static(&alpn_list, alpn_list_buffer, 4, sizeof(struct aws_byte_cursor)); |
| 1269 |
|
| 1270 |
if (aws_byte_cursor_split_on_char(&user_alpn_str, ';', &alpn_list)) { |
| 1271 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1272 |
return AWS_OP_ERR; |
| 1273 |
} |
| 1274 |
|
| 1275 |
size_t protocols_list_len = aws_array_list_length(&alpn_list); |
| 1276 |
if (protocols_list_len < 1) { |
| 1277 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1278 |
return AWS_OP_ERR; |
| 1279 |
} |
| 1280 |
|
| 1281 |
for (size_t i = 0; i < protocols_list_len && i < max_count; ++i) { |
| 1282 |
struct aws_byte_cursor cursor; |
| 1283 |
AWS_ZERO_STRUCT(cursor); |
| 1284 |
if (aws_array_list_get_at(&alpn_list, (void *)&cursor, (size_t)i)) { |
| 1285 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1286 |
return AWS_OP_ERR; |
| 1287 |
} |
| 1288 |
AWS_FATAL_ASSERT(cursor.ptr && cursor.len > 0); |
| 1289 |
memcpy((void *)protocol_output[i], cursor.ptr, cursor.len); |
| 1290 |
*protocol_count += 1; |
| 1291 |
} |
| 1292 |
|
| 1293 |
return AWS_OP_SUCCESS; |
| 1294 |
} |
| 1295 |
|
| 1296 |
static size_t s_tl_cleanup_key = 0; |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
static struct aws_event_loop_local_object s_tl_cleanup_object = { |
| 1303 |
.key = &s_tl_cleanup_key, |
| 1304 |
.object = NULL, |
| 1305 |
.on_object_removed = NULL, |
| 1306 |
}; |
| 1307 |
|
| 1308 |
static void s_aws_cleanup_s2n_thread_local_state(void *user_data) { |
| 1309 |
(void)user_data; |
| 1310 |
|
| 1311 |
s2n_cleanup_thread(); |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
static int s_s2n_tls_channel_handler_schedule_thread_local_cleanup(struct aws_channel_slot *slot) { |
| 1316 |
struct aws_channel *channel = slot->channel; |
| 1317 |
|
| 1318 |
struct aws_event_loop_local_object existing_marker; |
| 1319 |
AWS_ZERO_STRUCT(existing_marker); |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
if (aws_channel_fetch_local_object(channel, &s_tl_cleanup_key, &existing_marker)) { |
| 1325 |
|
| 1326 |
if (aws_channel_put_local_object(channel, &s_tl_cleanup_key, &s_tl_cleanup_object)) { |
| 1327 |
return AWS_OP_ERR; |
| 1328 |
} |
| 1329 |
|
| 1330 |
aws_thread_current_at_exit(s_aws_cleanup_s2n_thread_local_state, NULL); |
| 1331 |
} |
| 1332 |
|
| 1333 |
return AWS_OP_SUCCESS; |
| 1334 |
} |
| 1335 |
|
| 1336 |
static struct aws_channel_handler *s_new_tls_handler( |
| 1337 |
struct aws_allocator *allocator, |
| 1338 |
struct aws_tls_connection_options *options, |
| 1339 |
struct aws_channel_slot *slot, |
| 1340 |
s2n_mode mode) { |
| 1341 |
|
| 1342 |
AWS_ASSERT(options->ctx); |
| 1343 |
struct s2n_handler *s2n_handler = aws_mem_calloc(allocator, 1, sizeof(struct s2n_handler)); |
| 1344 |
s2n_handler->handler.impl = s2n_handler; |
| 1345 |
s2n_handler->handler.alloc = allocator; |
| 1346 |
s2n_handler->handler.vtable = &s_handler_vtable; |
| 1347 |
s2n_handler->handler.slot = slot; |
| 1348 |
|
| 1349 |
aws_tls_ctx_acquire(options->ctx); |
| 1350 |
s2n_handler->s2n_ctx = options->ctx->impl; |
| 1351 |
|
| 1352 |
s2n_handler->connection = s2n_connection_new(mode); |
| 1353 |
|
| 1354 |
if (!s2n_handler->connection) { |
| 1355 |
goto cleanup_conn; |
| 1356 |
} |
| 1357 |
|
| 1358 |
aws_tls_channel_handler_shared_init(&s2n_handler->shared_state, &s2n_handler->handler, options); |
| 1359 |
|
| 1360 |
s2n_handler->user_data = options->user_data; |
| 1361 |
s2n_handler->on_data_read = options->on_data_read; |
| 1362 |
s2n_handler->on_error = options->on_error; |
| 1363 |
s2n_handler->on_negotiation_result = options->on_negotiation_result; |
| 1364 |
s2n_handler->advertise_alpn_message = options->advertise_alpn_message; |
| 1365 |
|
| 1366 |
s2n_handler->latest_message_completion_user_data = NULL; |
| 1367 |
s2n_handler->latest_message_on_completion = NULL; |
| 1368 |
s2n_handler->slot = slot; |
| 1369 |
aws_linked_list_init(&s2n_handler->input_queue); |
| 1370 |
|
| 1371 |
s2n_handler->protocol = aws_byte_buf_from_array(NULL, 0); |
| 1372 |
|
| 1373 |
if (options->server_name) { |
| 1374 |
|
| 1375 |
if (s2n_set_server_name(s2n_handler->connection, aws_string_c_str(options->server_name))) { |
| 1376 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1377 |
goto cleanup_conn; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
s2n_handler->state = NEGOTIATION_ONGOING; |
| 1382 |
|
| 1383 |
s2n_connection_set_recv_cb(s2n_handler->connection, s_s2n_handler_recv); |
| 1384 |
s2n_connection_set_recv_ctx(s2n_handler->connection, s2n_handler); |
| 1385 |
s2n_connection_set_send_cb(s2n_handler->connection, s_s2n_handler_send); |
| 1386 |
s2n_connection_set_send_ctx(s2n_handler->connection, s2n_handler); |
| 1387 |
s2n_connection_set_ctx(s2n_handler->connection, s2n_handler); |
| 1388 |
s2n_connection_set_blinding(s2n_handler->connection, S2N_SELF_SERVICE_BLINDING); |
| 1389 |
|
| 1390 |
if (options->alpn_list) { |
| 1391 |
AWS_LOGF_DEBUG( |
| 1392 |
AWS_LS_IO_TLS, |
| 1393 |
"id=%p: Setting ALPN list %s", |
| 1394 |
(void *)&s2n_handler->handler, |
| 1395 |
aws_string_c_str(options->alpn_list)); |
| 1396 |
|
| 1397 |
const char protocols_cpy[4][128] = {{0}}; |
| 1398 |
AWS_ZERO_ARRAY(protocols_cpy); |
| 1399 |
size_t protocols_size = 4; |
| 1400 |
if (s_parse_protocol_preferences(options->alpn_list, protocols_cpy, &protocols_size)) { |
| 1401 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1402 |
goto cleanup_conn; |
| 1403 |
} |
| 1404 |
|
| 1405 |
const char *protocols[4]; |
| 1406 |
AWS_ZERO_ARRAY(protocols); |
| 1407 |
for (size_t i = 0; i < protocols_size; ++i) { |
| 1408 |
protocols[i] = protocols_cpy[i]; |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (s2n_connection_set_protocol_preferences( |
| 1412 |
s2n_handler->connection, (const char *const *)protocols, (int)protocols_size)) { |
| 1413 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1414 |
goto cleanup_conn; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
if (s2n_connection_set_config(s2n_handler->connection, s2n_handler->s2n_ctx->s2n_config)) { |
| 1419 |
AWS_LOGF_WARN( |
| 1420 |
AWS_LS_IO_TLS, |
| 1421 |
"id=%p: configuration error %s (%s)", |
| 1422 |
(void *)&s2n_handler->handler, |
| 1423 |
s2n_strerror(s2n_errno, "EN"), |
| 1424 |
s2n_strerror_debug(s2n_errno, "EN")); |
| 1425 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1426 |
goto cleanup_conn; |
| 1427 |
} |
| 1428 |
|
| 1429 |
aws_channel_task_init( |
| 1430 |
&s2n_handler->delayed_shutdown_task, s_delayed_shutdown_task_fn, &s2n_handler->handler, "s2n_delayed_shutdown"); |
| 1431 |
|
| 1432 |
if (s_s2n_tls_channel_handler_schedule_thread_local_cleanup(slot)) { |
| 1433 |
goto cleanup_conn; |
| 1434 |
} |
| 1435 |
|
| 1436 |
return &s2n_handler->handler; |
| 1437 |
|
| 1438 |
cleanup_conn: |
| 1439 |
s_s2n_handler_destroy(&s2n_handler->handler); |
| 1440 |
|
| 1441 |
return NULL; |
| 1442 |
} |
| 1443 |
|
| 1444 |
static struct aws_channel_handler *s_tls_client_handler_new( |
| 1445 |
struct aws_allocator *allocator, |
| 1446 |
struct aws_tls_connection_options *options, |
| 1447 |
struct aws_channel_slot *slot) { |
| 1448 |
|
| 1449 |
return s_new_tls_handler(allocator, options, slot, S2N_CLIENT); |
| 1450 |
} |
| 1451 |
|
| 1452 |
static struct aws_channel_handler *s_tls_server_handler_new( |
| 1453 |
struct aws_allocator *allocator, |
| 1454 |
struct aws_tls_connection_options *options, |
| 1455 |
struct aws_channel_slot *slot) { |
| 1456 |
|
| 1457 |
return s_new_tls_handler(allocator, options, slot, S2N_SERVER); |
| 1458 |
} |
| 1459 |
|
| 1460 |
static void s_s2n_ctx_destroy(void *user_data) { |
| 1461 |
struct s2n_ctx *s2n_ctx = user_data; |
| 1462 |
if (s2n_ctx != NULL) { |
| 1463 |
if (s2n_ctx->s2n_config) { |
| 1464 |
s2n_config_free(s2n_ctx->s2n_config); |
| 1465 |
} |
| 1466 |
if (s2n_ctx->custom_cert_chain_and_key) { |
| 1467 |
s2n_cert_chain_and_key_free(s2n_ctx->custom_cert_chain_and_key); |
| 1468 |
} |
| 1469 |
s2n_ctx->custom_key_handler = aws_custom_key_op_handler_release(s2n_ctx->custom_key_handler); |
| 1470 |
|
| 1471 |
aws_mem_release(s2n_ctx->ctx.alloc, s2n_ctx); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
static int s2n_wall_clock_time_nanoseconds(void *context, uint64_t *time_in_ns) { |
| 1476 |
(void)context; |
| 1477 |
if (aws_sys_clock_get_ticks(time_in_ns)) { |
| 1478 |
*time_in_ns = 0; |
| 1479 |
return -1; |
| 1480 |
} |
| 1481 |
|
| 1482 |
return 0; |
| 1483 |
} |
| 1484 |
|
| 1485 |
static int s2n_monotonic_clock_time_nanoseconds(void *context, uint64_t *time_in_ns) { |
| 1486 |
(void)context; |
| 1487 |
if (aws_high_res_clock_get_ticks(time_in_ns)) { |
| 1488 |
*time_in_ns = 0; |
| 1489 |
return -1; |
| 1490 |
} |
| 1491 |
|
| 1492 |
return 0; |
| 1493 |
} |
| 1494 |
|
| 1495 |
static void s_log_and_raise_s2n_errno(const char *msg) { |
| 1496 |
AWS_LOGF_ERROR( |
| 1497 |
AWS_LS_IO_TLS, "%s: %s (%s)", msg, s2n_strerror(s2n_errno, "EN"), s2n_strerror_debug(s2n_errno, "EN")); |
| 1498 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1499 |
} |
| 1500 |
|
| 1501 |
static struct aws_tls_ctx *s_tls_ctx_new( |
| 1502 |
struct aws_allocator *alloc, |
| 1503 |
const struct aws_tls_ctx_options *options, |
| 1504 |
s2n_mode mode) { |
| 1505 |
|
| 1506 |
if (!s_tls_is_cipher_pref_supported(options->cipher_pref)) { |
| 1507 |
aws_raise_error(AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED); |
| 1508 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: TLS Cipher Preference is not supported: %d.", options->cipher_pref); |
| 1509 |
return NULL; |
| 1510 |
} |
| 1511 |
|
| 1512 |
struct s2n_ctx *s2n_ctx = aws_mem_calloc(alloc, 1, sizeof(struct s2n_ctx)); |
| 1513 |
|
| 1514 |
s2n_ctx->ctx.alloc = alloc; |
| 1515 |
s2n_ctx->ctx.impl = s2n_ctx; |
| 1516 |
aws_ref_count_init(&s2n_ctx->ctx.ref_count, s2n_ctx, (aws_simple_completion_callback *)s_s2n_ctx_destroy); |
| 1517 |
|
| 1518 |
s2n_ctx->s2n_config = s2n_config_new(); |
| 1519 |
if (!s2n_ctx->s2n_config) { |
| 1520 |
s_log_and_raise_s2n_errno("ctx: creation failed"); |
| 1521 |
goto cleanup_s2n_config; |
| 1522 |
} |
| 1523 |
|
| 1524 |
int set_clock_result = s2n_config_set_wall_clock(s2n_ctx->s2n_config, s2n_wall_clock_time_nanoseconds, NULL); |
| 1525 |
if (set_clock_result != S2N_ERR_T_OK) { |
| 1526 |
s_log_and_raise_s2n_errno("ctx: failed to set wall clock"); |
| 1527 |
goto cleanup_s2n_config; |
| 1528 |
} |
| 1529 |
|
| 1530 |
set_clock_result = s2n_config_set_monotonic_clock(s2n_ctx->s2n_config, s2n_monotonic_clock_time_nanoseconds, NULL); |
| 1531 |
if (set_clock_result != S2N_ERR_T_OK) { |
| 1532 |
s_log_and_raise_s2n_errno("ctx: failed to set monotonic clock"); |
| 1533 |
goto cleanup_s2n_config; |
| 1534 |
} |
| 1535 |
|
| 1536 |
const char *security_policy = NULL; |
| 1537 |
if (options->custom_key_op_handler != NULL) { |
| 1538 |
|
| 1539 |
|
| 1540 |
switch (options->minimum_tls_version) { |
| 1541 |
case AWS_IO_SSLv3: |
| 1542 |
security_policy = "CloudFront-SSL-v-3"; |
| 1543 |
break; |
| 1544 |
case AWS_IO_TLSv1: |
| 1545 |
security_policy = "CloudFront-TLS-1-0-2014"; |
| 1546 |
break; |
| 1547 |
case AWS_IO_TLSv1_1: |
| 1548 |
security_policy = "ELBSecurityPolicy-TLS-1-1-2017-01"; |
| 1549 |
break; |
| 1550 |
case AWS_IO_TLSv1_2: |
| 1551 |
security_policy = "ELBSecurityPolicy-TLS-1-2-Ext-2018-06"; |
| 1552 |
break; |
| 1553 |
case AWS_IO_TLSv1_3: |
| 1554 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "TLS 1.3 with PKCS#11 is not supported yet."); |
| 1555 |
aws_raise_error(AWS_IO_TLS_VERSION_UNSUPPORTED); |
| 1556 |
goto cleanup_s2n_config; |
| 1557 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 1558 |
default: |
| 1559 |
security_policy = "ELBSecurityPolicy-TLS-1-1-2017-01"; |
| 1560 |
} |
| 1561 |
} else { |
| 1562 |
|
| 1563 |
switch (options->minimum_tls_version) { |
| 1564 |
case AWS_IO_SSLv3: |
| 1565 |
security_policy = "AWS-CRT-SDK-SSLv3.0-2023"; |
| 1566 |
break; |
| 1567 |
case AWS_IO_TLSv1: |
| 1568 |
security_policy = "AWS-CRT-SDK-TLSv1.0-2023"; |
| 1569 |
break; |
| 1570 |
case AWS_IO_TLSv1_1: |
| 1571 |
security_policy = "AWS-CRT-SDK-TLSv1.1-2023"; |
| 1572 |
break; |
| 1573 |
case AWS_IO_TLSv1_2: |
| 1574 |
security_policy = "AWS-CRT-SDK-TLSv1.2-2025-PQ"; |
| 1575 |
break; |
| 1576 |
case AWS_IO_TLSv1_3: |
| 1577 |
security_policy = "AWS-CRT-SDK-TLSv1.3-2025-PQ"; |
| 1578 |
break; |
| 1579 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 1580 |
default: |
| 1581 |
security_policy = "AWS-CRT-SDK-TLSv1.0-2025-PQ"; |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
switch (options->cipher_pref) { |
| 1586 |
case AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT: |
| 1587 |
|
| 1588 |
|
| 1589 |
break; |
| 1590 |
case AWS_IO_TLS_CIPHER_PREF_PQ_DEFAULT: |
| 1591 |
|
| 1592 |
security_policy = "AWS-CRT-SDK-TLSv1.2-2025-PQ"; |
| 1593 |
break; |
| 1594 |
case AWS_IO_TLS_CIPHER_PREF_PQ_TLSV1_2_2024_10: |
| 1595 |
security_policy = "AWS-CRT-SDK-TLSv1.2-2023-PQ"; |
| 1596 |
break; |
| 1597 |
case AWS_IO_TLS_CIPHER_PREF_TLSV1_2_2025_07: |
| 1598 |
security_policy = "AWS-CRT-SDK-TLSv1.2-2025"; |
| 1599 |
break; |
| 1600 |
case AWS_IO_TLS_CIPHER_PREF_TLSV1_0_2023_06: |
| 1601 |
security_policy = "AWS-CRT-SDK-TLSv1.0-2023"; |
| 1602 |
break; |
| 1603 |
case AWS_IO_TLS_CIPHER_PREF_NON_PQ_DEFAULT: |
| 1604 |
|
| 1605 |
security_policy = "AWS-CRT-SDK-TLSv1.0-2023"; |
| 1606 |
break; |
| 1607 |
default: |
| 1608 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Unrecognized TLS Cipher Preference: %d", options->cipher_pref); |
| 1609 |
aws_raise_error(AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED); |
| 1610 |
goto cleanup_s2n_config; |
| 1611 |
} |
| 1612 |
|
| 1613 |
AWS_ASSERT(security_policy != NULL); |
| 1614 |
|
| 1615 |
AWS_LOGF_DEBUG( |
| 1616 |
AWS_LS_IO_TLS, |
| 1617 |
"Set security policy to %s (minimum_tls_version: %d; cipher_pref: %d)", |
| 1618 |
security_policy, |
| 1619 |
(int)options->minimum_tls_version, |
| 1620 |
(int)options->cipher_pref); |
| 1621 |
|
| 1622 |
if (s2n_config_set_cipher_preferences(s2n_ctx->s2n_config, security_policy)) { |
| 1623 |
AWS_LOGF_ERROR( |
| 1624 |
AWS_LS_IO_TLS, |
| 1625 |
"ctx: Failed setting security policy '%s' (newer S2N required?): %s (%s)", |
| 1626 |
security_policy, |
| 1627 |
s2n_strerror(s2n_errno, "EN"), |
| 1628 |
s2n_strerror_debug(s2n_errno, "EN")); |
| 1629 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 1630 |
goto cleanup_s2n_config; |
| 1631 |
} |
| 1632 |
|
| 1633 |
if (aws_tls_options_buf_is_set(&options->certificate) && aws_tls_options_buf_is_set(&options->private_key)) { |
| 1634 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "ctx: Certificate and key have been set, setting them up now."); |
| 1635 |
|
| 1636 |
if (!aws_text_is_utf8(options->certificate.buffer, options->certificate.len)) { |
| 1637 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import certificate, must be ASCII/UTF-8 encoded"); |
| 1638 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 1639 |
goto cleanup_s2n_config; |
| 1640 |
} |
| 1641 |
|
| 1642 |
if (!aws_text_is_utf8(options->private_key.buffer, options->private_key.len)) { |
| 1643 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import private key, must be ASCII/UTF-8 encoded"); |
| 1644 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 1645 |
goto cleanup_s2n_config; |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
struct aws_string *certificate_string = aws_string_new_from_buf(alloc, &options->certificate); |
| 1650 |
struct aws_string *private_key_string = aws_string_new_from_buf(alloc, &options->private_key); |
| 1651 |
|
| 1652 |
int err_code = s2n_config_add_cert_chain_and_key( |
| 1653 |
s2n_ctx->s2n_config, (const char *)certificate_string->bytes, (const char *)private_key_string->bytes); |
| 1654 |
|
| 1655 |
aws_string_destroy(certificate_string); |
| 1656 |
aws_string_destroy_secure(private_key_string); |
| 1657 |
|
| 1658 |
if (mode == S2N_CLIENT) { |
| 1659 |
s2n_config_set_client_auth_type(s2n_ctx->s2n_config, S2N_CERT_AUTH_REQUIRED); |
| 1660 |
} |
| 1661 |
|
| 1662 |
if (err_code != S2N_ERR_T_OK) { |
| 1663 |
s_log_and_raise_s2n_errno("ctx: Failed to add certificate and private key"); |
| 1664 |
goto cleanup_s2n_config; |
| 1665 |
} |
| 1666 |
} else if (options->custom_key_op_handler != NULL) { |
| 1667 |
|
| 1668 |
s2n_ctx->custom_key_handler = aws_custom_key_op_handler_acquire(options->custom_key_op_handler); |
| 1669 |
|
| 1670 |
|
| 1671 |
if (s2n_config_set_async_pkey_callback(s2n_ctx->s2n_config, s_s2n_async_pkey_callback)) { |
| 1672 |
s_log_and_raise_s2n_errno("ctx: failed to set private key callback"); |
| 1673 |
goto cleanup_s2n_config; |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
s2n_ctx->custom_cert_chain_and_key = s2n_cert_chain_and_key_new(); |
| 1679 |
if (!s2n_ctx->custom_cert_chain_and_key) { |
| 1680 |
s_log_and_raise_s2n_errno("ctx: creation failed"); |
| 1681 |
goto cleanup_s2n_config; |
| 1682 |
} |
| 1683 |
|
| 1684 |
if (s2n_cert_chain_and_key_load_public_pem_bytes( |
| 1685 |
s2n_ctx->custom_cert_chain_and_key, options->certificate.buffer, options->certificate.len)) { |
| 1686 |
s_log_and_raise_s2n_errno("ctx: failed to load certificate"); |
| 1687 |
goto cleanup_s2n_config; |
| 1688 |
} |
| 1689 |
|
| 1690 |
if (s2n_config_add_cert_chain_and_key_to_store(s2n_ctx->s2n_config, s2n_ctx->custom_cert_chain_and_key)) { |
| 1691 |
s_log_and_raise_s2n_errno("ctx: failed to add certificate to store"); |
| 1692 |
goto cleanup_s2n_config; |
| 1693 |
} |
| 1694 |
|
| 1695 |
if (mode == S2N_CLIENT) { |
| 1696 |
s2n_config_set_client_auth_type(s2n_ctx->s2n_config, S2N_CERT_AUTH_REQUIRED); |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (options->verify_peer) { |
| 1701 |
|
| 1702 |
if (!options->no_certificate_revocation) { |
| 1703 |
if (s2n_config_set_check_stapled_ocsp_response(s2n_ctx->s2n_config, 1) == S2N_SUCCESS) { |
| 1704 |
if (s2n_config_set_status_request_type(s2n_ctx->s2n_config, S2N_STATUS_REQUEST_OCSP) != S2N_SUCCESS) { |
| 1705 |
s_log_and_raise_s2n_errno("ctx: ocsp status request cannot be set"); |
| 1706 |
goto cleanup_s2n_config; |
| 1707 |
} |
| 1708 |
} else { |
| 1709 |
if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_USAGE) { |
| 1710 |
AWS_LOGF_INFO(AWS_LS_IO_TLS, "ctx: cannot enable ocsp stapling: %s", s2n_strerror(s2n_errno, "EN")); |
| 1711 |
} else { |
| 1712 |
s_log_and_raise_s2n_errno("ctx: cannot enable ocsp stapling"); |
| 1713 |
goto cleanup_s2n_config; |
| 1714 |
} |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
if (options->ca_path || aws_tls_options_buf_is_set(&options->ca_file)) { |
| 1719 |
|
| 1720 |
|
| 1721 |
if (s2n_config_wipe_trust_store(s2n_ctx->s2n_config)) { |
| 1722 |
s_log_and_raise_s2n_errno("ctx: failed to wipe default trust store"); |
| 1723 |
goto cleanup_s2n_config; |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (options->ca_path) { |
| 1727 |
if (s2n_config_set_verification_ca_location( |
| 1728 |
s2n_ctx->s2n_config, NULL, aws_string_c_str(options->ca_path))) { |
| 1729 |
s_log_and_raise_s2n_errno("ctx: configuration error"); |
| 1730 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Failed to set ca_path %s\n", aws_string_c_str(options->ca_path)); |
| 1731 |
goto cleanup_s2n_config; |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
if (aws_tls_options_buf_is_set(&options->ca_file)) { |
| 1736 |
|
| 1737 |
struct aws_string *ca_file_string = aws_string_new_from_buf(alloc, &options->ca_file); |
| 1738 |
int set_ca_result = |
| 1739 |
s2n_config_add_pem_to_trust_store(s2n_ctx->s2n_config, (const char *)ca_file_string->bytes); |
| 1740 |
aws_string_destroy(ca_file_string); |
| 1741 |
|
| 1742 |
if (set_ca_result) { |
| 1743 |
s_log_and_raise_s2n_errno("ctx: configuration error"); |
| 1744 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Failed to set ca_file %s\n", (const char *)options->ca_file.buffer); |
| 1745 |
goto cleanup_s2n_config; |
| 1746 |
} |
| 1747 |
} |
| 1748 |
} else if (s_default_ca_file || s_default_ca_dir) { |
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
if (s2n_config_set_verification_ca_location(s2n_ctx->s2n_config, s_default_ca_file, s_default_ca_dir)) { |
| 1758 |
s_log_and_raise_s2n_errno("ctx: configuration error"); |
| 1759 |
AWS_LOGF_ERROR( |
| 1760 |
AWS_LS_IO_TLS, "Failed to set ca_path: %s and ca_file %s\n", s_default_ca_dir, s_default_ca_file); |
| 1761 |
goto cleanup_s2n_config; |
| 1762 |
} |
| 1763 |
# ifdef __APPLE__ |
| 1764 |
|
| 1765 |
aws_tls_s2n_load_macos_keychain_root_cas(s2n_ctx->s2n_config, alloc); |
| 1766 |
# endif |
| 1767 |
} else { |
| 1768 |
|
| 1769 |
aws_raise_error(AWS_IO_TLS_ERROR_DEFAULT_TRUST_STORE_NOT_FOUND); |
| 1770 |
AWS_LOGF_ERROR( |
| 1771 |
AWS_LS_IO_TLS, |
| 1772 |
"Default TLS trust store not found on this system." |
| 1773 |
" Install CA certificates, or \"override default trust store\"."); |
| 1774 |
goto cleanup_s2n_config; |
| 1775 |
} |
| 1776 |
|
| 1777 |
if (mode == S2N_SERVER && s2n_config_set_client_auth_type(s2n_ctx->s2n_config, S2N_CERT_AUTH_REQUIRED)) { |
| 1778 |
s_log_and_raise_s2n_errno("ctx: failed to set client auth type"); |
| 1779 |
goto cleanup_s2n_config; |
| 1780 |
} |
| 1781 |
} else if (mode != S2N_SERVER) { |
| 1782 |
AWS_LOGF_WARN( |
| 1783 |
AWS_LS_IO_TLS, |
| 1784 |
"ctx: X.509 validation has been disabled. " |
| 1785 |
"If this is not running in a test environment, this is likely a security vulnerability."); |
| 1786 |
if (s2n_config_disable_x509_verification(s2n_ctx->s2n_config)) { |
| 1787 |
s_log_and_raise_s2n_errno("ctx: failed to disable x509 verification"); |
| 1788 |
goto cleanup_s2n_config; |
| 1789 |
} |
| 1790 |
} |
| 1791 |
|
| 1792 |
if (options->alpn_list) { |
| 1793 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "ctx: Setting ALPN list %s", aws_string_c_str(options->alpn_list)); |
| 1794 |
const char protocols_cpy[4][128] = {{0}}; |
| 1795 |
AWS_ZERO_ARRAY(protocols_cpy); |
| 1796 |
size_t protocols_size = 4; |
| 1797 |
if (s_parse_protocol_preferences(options->alpn_list, protocols_cpy, &protocols_size)) { |
| 1798 |
s_log_and_raise_s2n_errno("ctx: Failed to parse ALPN list"); |
| 1799 |
goto cleanup_s2n_config; |
| 1800 |
} |
| 1801 |
|
| 1802 |
const char *protocols[4]; |
| 1803 |
AWS_ZERO_ARRAY(protocols); |
| 1804 |
for (size_t i = 0; i < protocols_size; ++i) { |
| 1805 |
protocols[i] = protocols_cpy[i]; |
| 1806 |
} |
| 1807 |
|
| 1808 |
if (s2n_config_set_protocol_preferences(s2n_ctx->s2n_config, protocols, (int)protocols_size)) { |
| 1809 |
s_log_and_raise_s2n_errno("ctx: Failed to set protocol preferences"); |
| 1810 |
goto cleanup_s2n_config; |
| 1811 |
} |
| 1812 |
} |
| 1813 |
|
| 1814 |
if (options->max_fragment_size == 512) { |
| 1815 |
s2n_config_send_max_fragment_length(s2n_ctx->s2n_config, S2N_TLS_MAX_FRAG_LEN_512); |
| 1816 |
} else if (options->max_fragment_size == 1024) { |
| 1817 |
s2n_config_send_max_fragment_length(s2n_ctx->s2n_config, S2N_TLS_MAX_FRAG_LEN_1024); |
| 1818 |
} else if (options->max_fragment_size == 2048) { |
| 1819 |
s2n_config_send_max_fragment_length(s2n_ctx->s2n_config, S2N_TLS_MAX_FRAG_LEN_2048); |
| 1820 |
} else if (options->max_fragment_size == 4096) { |
| 1821 |
s2n_config_send_max_fragment_length(s2n_ctx->s2n_config, S2N_TLS_MAX_FRAG_LEN_4096); |
| 1822 |
} |
| 1823 |
|
| 1824 |
return &s2n_ctx->ctx; |
| 1825 |
|
| 1826 |
cleanup_s2n_config: |
| 1827 |
s_s2n_ctx_destroy(s2n_ctx); |
| 1828 |
|
| 1829 |
return NULL; |
| 1830 |
} |
| 1831 |
|
| 1832 |
static struct aws_tls_ctx *s_tls_server_ctx_new( |
| 1833 |
struct aws_allocator *alloc, |
| 1834 |
const struct aws_tls_ctx_options *options) { |
| 1835 |
aws_io_fatal_assert_library_initialized(); |
| 1836 |
return s_tls_ctx_new(alloc, options, S2N_SERVER); |
| 1837 |
} |
| 1838 |
|
| 1839 |
static struct aws_tls_ctx *s_tls_client_ctx_new( |
| 1840 |
struct aws_allocator *alloc, |
| 1841 |
const struct aws_tls_ctx_options *options) { |
| 1842 |
aws_io_fatal_assert_library_initialized(); |
| 1843 |
return s_tls_ctx_new(alloc, options, S2N_CLIENT); |
| 1844 |
} |
| 1845 |
|
| 1846 |
#endif |