| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/io/tls_channel_handler.h> |
| 6 |
|
| 7 |
#include <aws/io/channel.h> |
| 8 |
#include <aws/io/file_utils.h> |
| 9 |
#include <aws/io/private/pki_utils.h> |
| 10 |
#include <aws/io/private/socket_impl.h> |
| 11 |
#include <aws/io/private/tls_channel_handler_private.h> |
| 12 |
#include <aws/io/private/tls_channel_handler_shared.h> |
| 13 |
#include <aws/io/socket_channel_handler.h> |
| 14 |
#include <aws/io/statistics.h> |
| 15 |
|
| 16 |
#include <aws/io/logging.h> |
| 17 |
|
| 18 |
#include <aws/common/encoding.h> |
| 19 |
#include <aws/common/string.h> |
| 20 |
#include <aws/common/task_scheduler.h> |
| 21 |
|
| 22 |
#include <AvailabilityMacros.h> |
| 23 |
#include <CoreFoundation/CoreFoundation.h> |
| 24 |
#include <Security/SecCertificate.h> |
| 25 |
#include <Security/SecureTransport.h> |
| 26 |
#include <Security/Security.h> |
| 27 |
#include <dlfcn.h> |
| 28 |
#include <math.h> |
| 29 |
|
| 30 |
#include "./dispatch_queue_event_loop_private.h" |
| 31 |
|
| 32 |
#pragma clang diagnostic push |
| 33 |
#pragma clang diagnostic ignored "-Wunused-variable" |
| 34 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 35 |
#pragma clang diagnostic ignored "-Wunused-function" |
| 36 |
|
| 37 |
static OSStatus (*s_SSLSetALPNProtocols)(SSLContextRef context, CFArrayRef protocols) = NULL; |
| 38 |
static OSStatus (*s_SSLCopyALPNProtocols)(SSLContextRef context, CFArrayRef *protocols) = NULL; |
| 39 |
|
| 40 |
#define EST_TLS_RECORD_OVERHEAD 53 |
| 41 |
#define KB_1 1024 |
| 42 |
#define MAX_RECORD_SIZE (KB_1 * 16) |
| 43 |
#define EST_HANDSHAKE_SIZE (7 * KB_1) |
| 44 |
|
| 45 |
|
| 46 |
#if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || \ |
| 47 |
(TARGET_OS_TV && __TV_OS_VERSION_MAX_ALLOWED >= 110000) || \ |
| 48 |
(TARGET_OS_WATCH && __WATCH_OS_VERSION_MAX_ALLOWED >= 40000) |
| 49 |
# define ALPN_AVAILABLE true |
| 50 |
# define TLS13_AVAILABLE true |
| 51 |
#elif (TARGET_OS_MAC && MAC_OS_X_VERSION_MAX_ALLOWED >= 101302) |
| 52 |
# define ALPN_AVAILABLE true |
| 53 |
|
| 54 |
# define TLS13_AVAILABLE false |
| 55 |
#else |
| 56 |
# define ALPN_AVAILABLE false |
| 57 |
# define TLS13_AVAILABLE false |
| 58 |
#endif |
| 59 |
|
| 60 |
static bool s_tls_is_alpn_available(void) { |
| 61 |
#if ALPN_AVAILABLE |
| 62 |
return s_SSLCopyALPNProtocols != NULL; |
| 63 |
#endif |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
static bool s_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref) { |
| 68 |
switch (cipher_pref) { |
| 69 |
case AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT: |
| 70 |
return true; |
| 71 |
|
| 72 |
case AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2019_06: |
| 73 |
default: |
| 74 |
return false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void s_aws_tls_init_static_state(struct aws_allocator *allocator) { |
| 79 |
(void)allocator; |
| 80 |
|
| 81 |
|
| 82 |
s_SSLSetALPNProtocols = (OSStatus(*)(SSLContextRef, CFArrayRef))dlsym(RTLD_DEFAULT, "SSLSetALPNProtocols"); |
| 83 |
s_SSLCopyALPNProtocols = (OSStatus(*)(SSLContextRef, CFArrayRef *))dlsym(RTLD_DEFAULT, "SSLCopyALPNProtocols"); |
| 84 |
|
| 85 |
if (aws_is_using_secitem()) { |
| 86 |
AWS_LOGF_INFO(AWS_LS_IO_TLS, "static: initializing TLS implementation as Apple SecItem."); |
| 87 |
} else { |
| 88 |
AWS_LOGF_INFO(AWS_LS_IO_TLS, "static: initializing TLS implementation as Apple SecureTransport."); |
| 89 |
} |
| 90 |
|
| 91 |
if (s_SSLSetALPNProtocols) { |
| 92 |
AWS_LOGF_INFO(AWS_LS_IO_TLS, "static: ALPN support detected."); |
| 93 |
} else { |
| 94 |
AWS_LOGF_WARN( |
| 95 |
AWS_LS_IO_TLS, |
| 96 |
"static: ALPN isn't supported on your apple device, you can improve support and performance by upgrading."); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
static void s_tls_clean_up_static_state(void) { } |
| 101 |
|
| 102 |
struct secure_transport_handler { |
| 103 |
struct aws_channel_handler handler; |
| 104 |
struct aws_tls_channel_handler_shared shared_state; |
| 105 |
SSLContextRef ctx; |
| 106 |
CFAllocatorRef wrapped_allocator; |
| 107 |
struct aws_linked_list input_queue; |
| 108 |
struct aws_channel_slot *parent_slot; |
| 109 |
struct aws_byte_buf protocol; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
struct aws_string *server_name; |
| 114 |
aws_channel_on_message_write_completed_fn *latest_message_on_completion; |
| 115 |
void *latest_message_completion_user_data; |
| 116 |
CFArrayRef ca_certs; |
| 117 |
struct aws_channel_task read_task; |
| 118 |
aws_tls_on_negotiation_result_fn *on_negotiation_result; |
| 119 |
aws_tls_on_data_read_fn *on_data_read; |
| 120 |
aws_tls_on_error_fn *on_error; |
| 121 |
void *user_data; |
| 122 |
bool advertise_alpn_message; |
| 123 |
bool negotiation_finished; |
| 124 |
bool verify_peer; |
| 125 |
bool read_task_pending; |
| 126 |
enum aws_tls_handler_read_state read_state; |
| 127 |
int delay_shutdown_error_code; |
| 128 |
}; |
| 129 |
|
| 130 |
static OSStatus s_read_cb(SSLConnectionRef conn, void *data, size_t *len) { |
| 131 |
struct secure_transport_handler *handler = (struct secure_transport_handler *)conn; |
| 132 |
|
| 133 |
size_t written = 0; |
| 134 |
struct aws_byte_buf buf = aws_byte_buf_from_array((const uint8_t *)data, *len); |
| 135 |
buf.len = 0; |
| 136 |
|
| 137 |
while (!aws_linked_list_empty(&handler->input_queue) && written < buf.capacity) { |
| 138 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&handler->input_queue); |
| 139 |
struct aws_io_message *message = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle); |
| 140 |
|
| 141 |
size_t remaining_message_len = message->message_data.len - message->copy_mark; |
| 142 |
size_t remaining_buf_len = buf.capacity - written; |
| 143 |
|
| 144 |
size_t to_write = remaining_message_len < remaining_buf_len ? remaining_message_len : remaining_buf_len; |
| 145 |
|
| 146 |
struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message->message_data); |
| 147 |
aws_byte_cursor_advance(&message_cursor, message->copy_mark); |
| 148 |
aws_byte_cursor_read(&message_cursor, buf.buffer + written, to_write); |
| 149 |
|
| 150 |
written += to_write; |
| 151 |
|
| 152 |
message->copy_mark += to_write; |
| 153 |
|
| 154 |
if (message->copy_mark == message->message_data.len) { |
| 155 |
|
| 156 |
aws_mem_release(message->allocator, message); |
| 157 |
} else { |
| 158 |
aws_linked_list_push_front(&handler->input_queue, &message->queueing_handle); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (*len == written) { |
| 163 |
return noErr; |
| 164 |
} |
| 165 |
|
| 166 |
*len = written; |
| 167 |
return errSSLWouldBlock; |
| 168 |
} |
| 169 |
|
| 170 |
static OSStatus s_write_cb(SSLConnectionRef conn, const void *data, size_t *len) { |
| 171 |
struct secure_transport_handler *handler = (struct secure_transport_handler *)conn; |
| 172 |
|
| 173 |
struct aws_byte_buf buf = aws_byte_buf_from_array((const uint8_t *)data, *len); |
| 174 |
struct aws_byte_cursor buffer_cursor = aws_byte_cursor_from_buf(&buf); |
| 175 |
|
| 176 |
size_t processed = 0; |
| 177 |
while (processed < buf.len) { |
| 178 |
const size_t overhead = aws_channel_slot_upstream_message_overhead(handler->parent_slot); |
| 179 |
const size_t message_size_hint = (buf.len - processed) + overhead; |
| 180 |
struct aws_io_message *message = aws_channel_acquire_message_from_pool( |
| 181 |
handler->parent_slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, message_size_hint); |
| 182 |
|
| 183 |
if (message->message_data.capacity <= overhead) { |
| 184 |
aws_mem_release(message->allocator, message); |
| 185 |
return errSecMemoryError; |
| 186 |
} |
| 187 |
|
| 188 |
const size_t available_msg_write_capacity = message->message_data.capacity - overhead; |
| 189 |
const size_t to_write = |
| 190 |
available_msg_write_capacity >= buffer_cursor.len ? buffer_cursor.len : available_msg_write_capacity; |
| 191 |
|
| 192 |
struct aws_byte_cursor chunk = aws_byte_cursor_advance(&buffer_cursor, to_write); |
| 193 |
if (aws_byte_buf_append(&message->message_data, &chunk)) { |
| 194 |
aws_mem_release(message->allocator, message); |
| 195 |
return errSecBufferTooSmall; |
| 196 |
} |
| 197 |
processed += message->message_data.len; |
| 198 |
|
| 199 |
if (processed == buf.len) { |
| 200 |
message->on_completion = handler->latest_message_on_completion; |
| 201 |
message->user_data = handler->latest_message_completion_user_data; |
| 202 |
handler->latest_message_on_completion = NULL; |
| 203 |
handler->latest_message_completion_user_data = NULL; |
| 204 |
} |
| 205 |
|
| 206 |
if (aws_channel_slot_send_message(handler->parent_slot, message, AWS_CHANNEL_DIR_WRITE)) { |
| 207 |
aws_mem_release(message->allocator, message); |
| 208 |
return errSSLClosedNoNotify; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (*len == processed) { |
| 213 |
return noErr; |
| 214 |
} |
| 215 |
|
| 216 |
*len = processed; |
| 217 |
return errSSLWouldBlock; |
| 218 |
} |
| 219 |
|
| 220 |
static void s_destroy(struct aws_channel_handler *handler) { |
| 221 |
if (handler) { |
| 222 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 223 |
CFRelease(secure_transport_handler->ctx); |
| 224 |
|
| 225 |
if (secure_transport_handler->protocol.buffer) { |
| 226 |
aws_byte_buf_clean_up(&secure_transport_handler->protocol); |
| 227 |
} |
| 228 |
|
| 229 |
aws_tls_channel_handler_shared_clean_up(&secure_transport_handler->shared_state); |
| 230 |
|
| 231 |
aws_string_destroy(secure_transport_handler->server_name); |
| 232 |
|
| 233 |
aws_mem_release(handler->alloc, secure_transport_handler); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static CFStringRef s_get_protocol(struct secure_transport_handler *handler) { |
| 238 |
#if ALPN_AVAILABLE |
| 239 |
if (s_SSLCopyALPNProtocols) { |
| 240 |
CFArrayRef protocols = NULL; |
| 241 |
|
| 242 |
OSStatus status = s_SSLCopyALPNProtocols(handler->ctx, &protocols); |
| 243 |
(void)status; |
| 244 |
|
| 245 |
if (!protocols) { |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
|
| 249 |
CFIndex count = CFArrayGetCount(protocols); |
| 250 |
|
| 251 |
if (count <= 0) { |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
|
| 255 |
CFStringRef alpn_value = CFArrayGetValueAtIndex(protocols, 0); |
| 256 |
CFRetain(alpn_value); |
| 257 |
CFRelease(protocols); |
| 258 |
|
| 259 |
return alpn_value; |
| 260 |
} |
| 261 |
|
| 262 |
return NULL; |
| 263 |
#else |
| 264 |
(void)handler; |
| 265 |
return NULL; |
| 266 |
#endif |
| 267 |
} |
| 268 |
|
| 269 |
static void s_set_protocols( |
| 270 |
struct secure_transport_handler *handler, |
| 271 |
struct aws_allocator *alloc, |
| 272 |
struct aws_string *alpn_list) { |
| 273 |
|
| 274 |
(void)handler; |
| 275 |
(void)alloc; |
| 276 |
(void)alpn_list; |
| 277 |
#if ALPN_AVAILABLE |
| 278 |
if (s_SSLSetALPNProtocols) { |
| 279 |
struct aws_byte_cursor alpn_data = aws_byte_cursor_from_string(alpn_list); |
| 280 |
struct aws_array_list alpn_list_array; |
| 281 |
if (aws_array_list_init_dynamic(&alpn_list_array, alloc, 2, sizeof(struct aws_byte_cursor))) { |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
if (aws_byte_cursor_split_on_char(&alpn_data, ';', &alpn_list_array)) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
CFMutableArrayRef alpn_array = CFArrayCreateMutable( |
| 290 |
handler->wrapped_allocator, aws_array_list_length(&alpn_list_array), &kCFTypeArrayCallBacks); |
| 291 |
|
| 292 |
if (!alpn_array) { |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
for (size_t i = 0; i < aws_array_list_length(&alpn_list_array); ++i) { |
| 297 |
struct aws_byte_cursor protocol_cursor; |
| 298 |
aws_array_list_get_at(&alpn_list_array, &protocol_cursor, i); |
| 299 |
CFStringRef protocol = CFStringCreateWithBytes( |
| 300 |
handler->wrapped_allocator, protocol_cursor.ptr, protocol_cursor.len, kCFStringEncodingASCII, false); |
| 301 |
|
| 302 |
if (!protocol) { |
| 303 |
CFRelease(alpn_array); |
| 304 |
alpn_array = NULL; |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
CFArrayAppendValue(alpn_array, protocol); |
| 309 |
CFRelease(protocol); |
| 310 |
} |
| 311 |
|
| 312 |
if (alpn_array) { |
| 313 |
OSStatus status = s_SSLSetALPNProtocols(handler->ctx, alpn_array); |
| 314 |
(void)status; |
| 315 |
|
| 316 |
CFRelease(alpn_array); |
| 317 |
} |
| 318 |
|
| 319 |
aws_array_list_clean_up(&alpn_list_array); |
| 320 |
} |
| 321 |
#endif |
| 322 |
} |
| 323 |
|
| 324 |
static void s_invoke_negotiation_callback(struct aws_channel_handler *handler, int err_code) { |
| 325 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 326 |
|
| 327 |
aws_on_tls_negotiation_completed(&secure_transport_handler->shared_state, err_code); |
| 328 |
|
| 329 |
if (secure_transport_handler->on_negotiation_result) { |
| 330 |
secure_transport_handler->on_negotiation_result( |
| 331 |
handler, secure_transport_handler->parent_slot, err_code, secure_transport_handler->user_data); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static int s_drive_negotiation(struct aws_channel_handler *handler) { |
| 336 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 337 |
|
| 338 |
aws_on_drive_tls_negotiation(&secure_transport_handler->shared_state); |
| 339 |
|
| 340 |
OSStatus status = SSLHandshake(secure_transport_handler->ctx); |
| 341 |
|
| 342 |
if (status == noErr) { |
| 343 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: negotiation succeeded", (void *)handler); |
| 344 |
secure_transport_handler->negotiation_finished = true; |
| 345 |
CFStringRef protocol = s_get_protocol(secure_transport_handler); |
| 346 |
|
| 347 |
if (protocol) { |
| 348 |
if (aws_byte_buf_init( |
| 349 |
&secure_transport_handler->protocol, handler->alloc, (size_t)CFStringGetLength(protocol) + 1)) { |
| 350 |
CFRelease(protocol); |
| 351 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 352 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 353 |
} |
| 354 |
|
| 355 |
memset(secure_transport_handler->protocol.buffer, 0, secure_transport_handler->protocol.capacity); |
| 356 |
|
| 357 |
CFRange byte_range = CFRangeMake(0, CFStringGetLength(protocol)); |
| 358 |
CFStringGetBytes( |
| 359 |
protocol, |
| 360 |
byte_range, |
| 361 |
kCFStringEncodingASCII, |
| 362 |
0, |
| 363 |
false, |
| 364 |
secure_transport_handler->protocol.buffer, |
| 365 |
secure_transport_handler->protocol.capacity, |
| 366 |
NULL); |
| 367 |
secure_transport_handler->protocol.len = secure_transport_handler->protocol.capacity - 1; |
| 368 |
CFRelease(protocol); |
| 369 |
AWS_LOGF_DEBUG( |
| 370 |
AWS_LS_IO_TLS, |
| 371 |
"id=%p: negotiated protocol: %s", |
| 372 |
(void *)handler, |
| 373 |
secure_transport_handler->protocol.buffer); |
| 374 |
} |
| 375 |
|
| 376 |
if (secure_transport_handler->server_name) { |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
AWS_LOGF_DEBUG( |
| 381 |
AWS_LS_IO_TLS, |
| 382 |
"id=%p: Remote Server Name: %s", |
| 383 |
(void *)handler, |
| 384 |
aws_string_c_str(secure_transport_handler->server_name)); |
| 385 |
} |
| 386 |
|
| 387 |
if (secure_transport_handler->parent_slot->adj_right && secure_transport_handler->advertise_alpn_message && |
| 388 |
protocol) { |
| 389 |
struct aws_io_message *message = aws_channel_acquire_message_from_pool( |
| 390 |
secure_transport_handler->parent_slot->channel, |
| 391 |
AWS_IO_MESSAGE_APPLICATION_DATA, |
| 392 |
sizeof(struct aws_tls_negotiated_protocol_message)); |
| 393 |
message->message_tag = AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE; |
| 394 |
struct aws_tls_negotiated_protocol_message *protocol_message = |
| 395 |
(struct aws_tls_negotiated_protocol_message *)message->message_data.buffer; |
| 396 |
|
| 397 |
protocol_message->protocol = secure_transport_handler->protocol; |
| 398 |
|
| 399 |
message->message_data.len = sizeof(struct aws_tls_negotiated_protocol_message); |
| 400 |
if (aws_channel_slot_send_message(secure_transport_handler->parent_slot, message, AWS_CHANNEL_DIR_READ)) { |
| 401 |
aws_mem_release(message->allocator, message); |
| 402 |
aws_channel_shutdown(secure_transport_handler->parent_slot->channel, aws_last_error()); |
| 403 |
return AWS_OP_SUCCESS; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
s_invoke_negotiation_callback(handler, AWS_ERROR_SUCCESS); |
| 408 |
|
| 409 |
} else if (status == errSSLPeerAuthCompleted) { |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
if (secure_transport_handler->verify_peer) { |
| 414 |
if (!secure_transport_handler->ca_certs) { |
| 415 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 416 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 417 |
} |
| 418 |
|
| 419 |
SecTrustRef trust; |
| 420 |
status = SSLCopyPeerTrust(secure_transport_handler->ctx, &trust); |
| 421 |
|
| 422 |
if (status != errSecSuccess) { |
| 423 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 424 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 425 |
} |
| 426 |
|
| 427 |
SecPolicyRef policy; |
| 428 |
if (secure_transport_handler->server_name) { |
| 429 |
CFStringRef server_name = CFStringCreateWithCString( |
| 430 |
secure_transport_handler->wrapped_allocator, |
| 431 |
aws_string_c_str(secure_transport_handler->server_name), |
| 432 |
kCFStringEncodingUTF8); |
| 433 |
policy = SecPolicyCreateSSL(true, server_name); |
| 434 |
CFRelease(server_name); |
| 435 |
} else { |
| 436 |
policy = SecPolicyCreateBasicX509(); |
| 437 |
} |
| 438 |
status = SecTrustSetPolicies(trust, policy); |
| 439 |
CFRelease(policy); |
| 440 |
|
| 441 |
if (status != errSecSuccess) { |
| 442 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed to set trust policy %d\n", (void *)handler, (int)status); |
| 443 |
CFRelease(trust); |
| 444 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 445 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 446 |
} |
| 447 |
|
| 448 |
status = SecTrustSetAnchorCertificates(trust, secure_transport_handler->ca_certs); |
| 449 |
if (status != errSecSuccess) { |
| 450 |
AWS_LOGF_ERROR( |
| 451 |
AWS_LS_IO_TLS, |
| 452 |
"id=%p: Failed to set anchor certificate with OSStatus %d\n", |
| 453 |
(void *)handler, |
| 454 |
(int)status); |
| 455 |
CFRelease(trust); |
| 456 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 457 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
status = SecTrustSetAnchorCertificatesOnly(trust, true); |
| 462 |
if (status != errSecSuccess) { |
| 463 |
AWS_LOGF_ERROR( |
| 464 |
AWS_LS_IO_TLS, |
| 465 |
"id=%p: Failed to ignore system anchors with OSStatus %d\n", |
| 466 |
(void *)handler, |
| 467 |
(int)status); |
| 468 |
CFRelease(trust); |
| 469 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 470 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 471 |
} |
| 472 |
|
| 473 |
SecTrustResultType trust_eval = 0; |
| 474 |
status = SecTrustEvaluate(trust, &trust_eval); |
| 475 |
CFRelease(trust); |
| 476 |
|
| 477 |
if (status == errSecSuccess && |
| 478 |
(trust_eval == kSecTrustResultProceed || trust_eval == kSecTrustResultUnspecified)) { |
| 479 |
return s_drive_negotiation(handler); |
| 480 |
} |
| 481 |
|
| 482 |
AWS_LOGF_WARN( |
| 483 |
AWS_LS_IO_TLS, |
| 484 |
"id=%p: Using custom CA, certificate validation failed with OSStatus %d and Trust Eval %d.", |
| 485 |
(void *)handler, |
| 486 |
(int)status, |
| 487 |
(int)trust_eval); |
| 488 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 489 |
} |
| 490 |
return s_drive_negotiation(handler); |
| 491 |
|
| 492 |
} else if (status != errSSLWouldBlock) { |
| 493 |
secure_transport_handler->negotiation_finished = false; |
| 494 |
|
| 495 |
AWS_LOGF_WARN(AWS_LS_IO_TLS, "id=%p: negotiation failed with OSStatus %d.", (void *)handler, (int)status); |
| 496 |
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 497 |
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 498 |
} |
| 499 |
|
| 500 |
return AWS_OP_SUCCESS; |
| 501 |
} |
| 502 |
|
| 503 |
static void s_negotiation_task(struct aws_channel_task *task, void *arg, aws_task_status status) { |
| 504 |
struct aws_channel_handler *handler = arg; |
| 505 |
|
| 506 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 507 |
s_drive_negotiation(handler); |
| 508 |
} |
| 509 |
|
| 510 |
aws_mem_release(handler->alloc, task); |
| 511 |
} |
| 512 |
|
| 513 |
static int s_tls_client_handler_start_negotiation(struct aws_channel_handler *handler) { |
| 514 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 515 |
|
| 516 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p, starting TLS negotiation", (void *)handler); |
| 517 |
if (aws_channel_thread_is_callers_thread(secure_transport_handler->parent_slot->channel)) { |
| 518 |
return s_drive_negotiation(handler); |
| 519 |
} |
| 520 |
|
| 521 |
struct aws_channel_task *negotiation_task = aws_mem_acquire(handler->alloc, sizeof(struct aws_channel_task)); |
| 522 |
|
| 523 |
if (!negotiation_task) { |
| 524 |
return AWS_OP_ERR; |
| 525 |
} |
| 526 |
|
| 527 |
aws_channel_task_init( |
| 528 |
negotiation_task, s_negotiation_task, handler, "secure_transport_channel_handler_start_negotiation"); |
| 529 |
aws_channel_schedule_task_now(secure_transport_handler->parent_slot->channel, negotiation_task); |
| 530 |
return AWS_OP_SUCCESS; |
| 531 |
} |
| 532 |
|
| 533 |
static int s_process_write_message( |
| 534 |
struct aws_channel_handler *handler, |
| 535 |
struct aws_channel_slot *slot, |
| 536 |
struct aws_io_message *message) { |
| 537 |
(void)slot; |
| 538 |
|
| 539 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 540 |
|
| 541 |
if (AWS_UNLIKELY(!secure_transport_handler->negotiation_finished)) { |
| 542 |
return aws_raise_error(AWS_IO_TLS_ERROR_NOT_NEGOTIATED); |
| 543 |
} |
| 544 |
|
| 545 |
secure_transport_handler->latest_message_on_completion = message->on_completion; |
| 546 |
secure_transport_handler->latest_message_completion_user_data = message->user_data; |
| 547 |
|
| 548 |
size_t processed = 0; |
| 549 |
OSStatus status = |
| 550 |
SSLWrite(secure_transport_handler->ctx, message->message_data.buffer, message->message_data.len, &processed); |
| 551 |
|
| 552 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: bytes written: %llu", (void *)handler, (unsigned long long)processed); |
| 553 |
|
| 554 |
if (status != noErr) { |
| 555 |
AWS_LOGF_DEBUG( |
| 556 |
AWS_LS_IO_TLS, "id=%p: SSLWrite failed with OSStatus error code %d.", (void *)handler, (int)status); |
| 557 |
return aws_raise_error(AWS_IO_TLS_ERROR_WRITE_FAILURE); |
| 558 |
} |
| 559 |
|
| 560 |
aws_mem_release(message->allocator, message); |
| 561 |
|
| 562 |
return AWS_OP_SUCCESS; |
| 563 |
} |
| 564 |
|
| 565 |
static void s_run_read(struct aws_channel_task *task, void *arg, aws_task_status status); |
| 566 |
|
| 567 |
static void s_initialize_read_delay_shutdown( |
| 568 |
struct aws_channel_handler *handler, |
| 569 |
struct aws_channel_slot *slot, |
| 570 |
int error_code) { |
| 571 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
AWS_LOGF_DEBUG( |
| 577 |
AWS_LS_IO_TLS, |
| 578 |
"id=%p: TLS handler still have pending data to be delivered during shutdown. Wait until downstream " |
| 579 |
"reads the data.", |
| 580 |
(void *)handler); |
| 581 |
if (aws_channel_slot_downstream_read_window(slot) == 0) { |
| 582 |
AWS_LOGF_WARN( |
| 583 |
AWS_LS_IO_TLS, |
| 584 |
"id=%p: TLS shutdown delayed. Pending data cannot be processed until the flow-control window opens. " |
| 585 |
" Your application may hang if the read window never opens", |
| 586 |
(void *)handler); |
| 587 |
} |
| 588 |
secure_transport_handler->read_state = AWS_TLS_HANDLER_READ_SHUTTING_DOWN; |
| 589 |
secure_transport_handler->delay_shutdown_error_code = error_code; |
| 590 |
if (!secure_transport_handler->read_task_pending) { |
| 591 |
|
| 592 |
|
| 593 |
secure_transport_handler->read_task_pending = true; |
| 594 |
aws_channel_task_init( |
| 595 |
&secure_transport_handler->read_task, s_run_read, handler, "darwin_channel_handler_read_on_delay_shutdown"); |
| 596 |
aws_channel_schedule_task_now(slot->channel, &secure_transport_handler->read_task); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
static int s_handle_shutdown( |
| 601 |
struct aws_channel_handler *handler, |
| 602 |
struct aws_channel_slot *slot, |
| 603 |
enum aws_channel_direction dir, |
| 604 |
int error_code, |
| 605 |
bool abort_immediately) { |
| 606 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 607 |
|
| 608 |
if (dir == AWS_CHANNEL_DIR_READ) { |
| 609 |
AWS_LOGF_DEBUG( |
| 610 |
AWS_LS_IO_TLS, "id=%p: shutting down read direction with error %d.", (void *)handler, error_code); |
| 611 |
if (!abort_immediately && secure_transport_handler->negotiation_finished && |
| 612 |
!aws_linked_list_empty(&secure_transport_handler->input_queue) && slot->adj_right) { |
| 613 |
s_initialize_read_delay_shutdown(handler, slot, error_code); |
| 614 |
|
| 615 |
|
| 616 |
return AWS_OP_SUCCESS; |
| 617 |
} |
| 618 |
secure_transport_handler->read_state = AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE; |
| 619 |
} else { |
| 620 |
|
| 621 |
if (!abort_immediately && error_code != AWS_IO_SOCKET_CLOSED) { |
| 622 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: shutting down write direction.", (void *)handler); |
| 623 |
SSLClose(secure_transport_handler->ctx); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
while (!aws_linked_list_empty(&secure_transport_handler->input_queue)) { |
| 628 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&secure_transport_handler->input_queue); |
| 629 |
struct aws_io_message *message = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle); |
| 630 |
aws_mem_release(message->allocator, message); |
| 631 |
} |
| 632 |
return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, abort_immediately); |
| 633 |
} |
| 634 |
|
| 635 |
static int s_process_read_message( |
| 636 |
struct aws_channel_handler *handler, |
| 637 |
struct aws_channel_slot *slot, |
| 638 |
struct aws_io_message *message) { |
| 639 |
|
| 640 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 641 |
if (secure_transport_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 642 |
if (message) { |
| 643 |
aws_mem_release(message->allocator, message); |
| 644 |
} |
| 645 |
return AWS_OP_SUCCESS; |
| 646 |
} |
| 647 |
|
| 648 |
if (message) { |
| 649 |
aws_linked_list_push_back(&secure_transport_handler->input_queue, &message->queueing_handle); |
| 650 |
|
| 651 |
if (!secure_transport_handler->negotiation_finished) { |
| 652 |
size_t message_len = message->message_data.len; |
| 653 |
if (!s_drive_negotiation(handler)) { |
| 654 |
aws_channel_slot_increment_read_window(slot, message_len); |
| 655 |
} else { |
| 656 |
aws_channel_shutdown( |
| 657 |
secure_transport_handler->parent_slot->channel, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE); |
| 658 |
} |
| 659 |
return AWS_OP_SUCCESS; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
size_t downstream_window = SIZE_MAX; |
| 664 |
|
| 665 |
if (slot->adj_right) { |
| 666 |
downstream_window = aws_channel_slot_downstream_read_window(slot); |
| 667 |
} |
| 668 |
AWS_LOGF_TRACE( |
| 669 |
AWS_LS_IO_TLS, "id=%p: downstream window is %llu", (void *)handler, (unsigned long long)downstream_window); |
| 670 |
size_t processed = 0; |
| 671 |
|
| 672 |
int shutdown_error_code = 0; |
| 673 |
while (processed < downstream_window) { |
| 674 |
|
| 675 |
struct aws_io_message *outgoing_read_message = aws_channel_acquire_message_from_pool( |
| 676 |
slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, downstream_window - processed); |
| 677 |
|
| 678 |
size_t read = 0; |
| 679 |
OSStatus status = SSLRead( |
| 680 |
secure_transport_handler->ctx, |
| 681 |
outgoing_read_message->message_data.buffer, |
| 682 |
outgoing_read_message->message_data.capacity, |
| 683 |
&read); |
| 684 |
|
| 685 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: bytes read %llu", (void *)handler, (unsigned long long)read); |
| 686 |
if (read > 0) { |
| 687 |
processed += read; |
| 688 |
outgoing_read_message->message_data.len = read; |
| 689 |
|
| 690 |
if (secure_transport_handler->on_data_read) { |
| 691 |
secure_transport_handler->on_data_read( |
| 692 |
handler, slot, &outgoing_read_message->message_data, secure_transport_handler->user_data); |
| 693 |
} |
| 694 |
|
| 695 |
if (slot->adj_right) { |
| 696 |
if (aws_channel_slot_send_message(slot, outgoing_read_message, AWS_CHANNEL_DIR_READ)) { |
| 697 |
aws_mem_release(outgoing_read_message->allocator, outgoing_read_message); |
| 698 |
shutdown_error_code = aws_last_error(); |
| 699 |
goto shutdown_channel; |
| 700 |
} |
| 701 |
|
| 702 |
} else { |
| 703 |
aws_mem_release(outgoing_read_message->allocator, outgoing_read_message); |
| 704 |
} |
| 705 |
} else { |
| 706 |
|
| 707 |
aws_mem_release(outgoing_read_message->allocator, outgoing_read_message); |
| 708 |
} |
| 709 |
|
| 710 |
switch (status) { |
| 711 |
case errSSLWouldBlock: |
| 712 |
if (secure_transport_handler->read_state == AWS_TLS_HANDLER_READ_SHUTTING_DOWN) { |
| 713 |
|
| 714 |
goto shutdown_channel; |
| 715 |
} else { |
| 716 |
break; |
| 717 |
} |
| 718 |
case errSSLClosedGraceful: |
| 719 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: connection shutting down gracefully.", (void *)handler); |
| 720 |
goto shutdown_channel; |
| 721 |
case noErr: |
| 722 |
|
| 723 |
continue; |
| 724 |
default: |
| 725 |
AWS_LOGF_TRACE( |
| 726 |
AWS_LS_IO_TLS, "id=%p: read message processed with OSStatus %d.", (void *)handler, status); |
| 727 |
|
| 728 |
aws_raise_error(AWS_IO_TLS_ERROR_READ_FAILURE); |
| 729 |
shutdown_error_code = AWS_IO_TLS_ERROR_READ_FAILURE; |
| 730 |
goto shutdown_channel; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
break; |
| 735 |
} |
| 736 |
AWS_LOGF_TRACE( |
| 737 |
AWS_LS_IO_TLS, |
| 738 |
"id=%p, Remaining window for this event-loop tick: %llu", |
| 739 |
(void *)handler, |
| 740 |
(unsigned long long)downstream_window - processed); |
| 741 |
|
| 742 |
return AWS_OP_SUCCESS; |
| 743 |
|
| 744 |
shutdown_channel: |
| 745 |
if (secure_transport_handler->read_state == AWS_TLS_HANDLER_READ_SHUTTING_DOWN) { |
| 746 |
if (secure_transport_handler->delay_shutdown_error_code != 0) { |
| 747 |
|
| 748 |
shutdown_error_code = secure_transport_handler->delay_shutdown_error_code; |
| 749 |
} |
| 750 |
|
| 751 |
secure_transport_handler->read_state = AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE; |
| 752 |
aws_channel_slot_on_handler_shutdown_complete(slot, AWS_CHANNEL_DIR_READ, shutdown_error_code, false); |
| 753 |
} else { |
| 754 |
|
| 755 |
aws_channel_shutdown(slot->channel, shutdown_error_code); |
| 756 |
} |
| 757 |
return AWS_OP_SUCCESS; |
| 758 |
} |
| 759 |
|
| 760 |
static void s_run_read(struct aws_channel_task *task, void *arg, aws_task_status status) { |
| 761 |
(void)task; |
| 762 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 763 |
struct aws_channel_handler *handler = arg; |
| 764 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 765 |
secure_transport_handler->read_task_pending = false; |
| 766 |
s_process_read_message(handler, secure_transport_handler->parent_slot, NULL); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
static int s_increment_read_window(struct aws_channel_handler *handler, struct aws_channel_slot *slot, size_t size) { |
| 771 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 772 |
if (secure_transport_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 773 |
return AWS_OP_SUCCESS; |
| 774 |
} |
| 775 |
|
| 776 |
AWS_LOGF_TRACE( |
| 777 |
AWS_LS_IO_TLS, "id=%p: increment read window message received %llu", (void *)handler, (unsigned long long)size); |
| 778 |
|
| 779 |
size_t downstream_size = aws_channel_slot_downstream_read_window(slot); |
| 780 |
size_t current_window_size = slot->window_size; |
| 781 |
|
| 782 |
size_t likely_records_count = (size_t)ceil((double)(downstream_size) / (double)(MAX_RECORD_SIZE)); |
| 783 |
size_t offset_size = aws_mul_size_saturating(likely_records_count, EST_TLS_RECORD_OVERHEAD); |
| 784 |
size_t total_desired_size = aws_add_size_saturating(offset_size, downstream_size); |
| 785 |
|
| 786 |
if (total_desired_size > current_window_size) { |
| 787 |
size_t window_update_size = total_desired_size - current_window_size; |
| 788 |
AWS_LOGF_TRACE( |
| 789 |
AWS_LS_IO_TLS, |
| 790 |
"id=%p: Propagating read window increment of size %llu", |
| 791 |
(void *)handler, |
| 792 |
(unsigned long long)window_update_size); |
| 793 |
aws_channel_slot_increment_read_window(slot, window_update_size); |
| 794 |
} |
| 795 |
|
| 796 |
if (secure_transport_handler->negotiation_finished && !secure_transport_handler->read_task_pending) { |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
secure_transport_handler->read_task_pending = true; |
| 801 |
aws_channel_task_init( |
| 802 |
&secure_transport_handler->read_task, |
| 803 |
s_run_read, |
| 804 |
handler, |
| 805 |
"secure_transport_channel_handler_read_on_window_increment"); |
| 806 |
aws_channel_schedule_task_now(slot->channel, &secure_transport_handler->read_task); |
| 807 |
} |
| 808 |
|
| 809 |
return AWS_OP_SUCCESS; |
| 810 |
} |
| 811 |
|
| 812 |
static size_t s_message_overhead(struct aws_channel_handler *handler) { |
| 813 |
(void)handler; |
| 814 |
return EST_TLS_RECORD_OVERHEAD; |
| 815 |
} |
| 816 |
|
| 817 |
static size_t s_initial_window_size(struct aws_channel_handler *handler) { |
| 818 |
(void)handler; |
| 819 |
return EST_HANDSHAKE_SIZE; |
| 820 |
} |
| 821 |
|
| 822 |
static void s_reset_statistics(struct aws_channel_handler *handler) { |
| 823 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 824 |
|
| 825 |
aws_crt_statistics_tls_reset(&secure_transport_handler->shared_state.stats); |
| 826 |
} |
| 827 |
|
| 828 |
static void s_gather_statistics(struct aws_channel_handler *handler, struct aws_array_list *stats) { |
| 829 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 830 |
|
| 831 |
void *stats_base = &secure_transport_handler->shared_state.stats; |
| 832 |
aws_array_list_push_back(stats, &stats_base); |
| 833 |
} |
| 834 |
|
| 835 |
static struct aws_byte_buf s_tls_handler_protocol(struct aws_channel_handler *handler) { |
| 836 |
if (aws_is_using_secitem()) { |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
const struct aws_socket *socket = aws_socket_handler_get_socket(handler); |
| 841 |
return socket->vtable->socket_get_protocol_fn(socket); |
| 842 |
} |
| 843 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 844 |
return secure_transport_handler->protocol; |
| 845 |
} |
| 846 |
|
| 847 |
static struct aws_byte_buf s_tls_handler_server_name(struct aws_channel_handler *handler) { |
| 848 |
struct aws_string *server_name = NULL; |
| 849 |
if (aws_is_using_secitem()) { |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
const struct aws_socket *socket = aws_socket_handler_get_socket(handler); |
| 854 |
server_name = socket->vtable->socket_get_server_name_fn(socket); |
| 855 |
} else { |
| 856 |
struct secure_transport_handler *secure_transport_handler = handler->impl; |
| 857 |
server_name = secure_transport_handler->server_name; |
| 858 |
} |
| 859 |
|
| 860 |
const uint8_t *bytes = NULL; |
| 861 |
size_t len = 0; |
| 862 |
if (server_name) { |
| 863 |
bytes = server_name->bytes; |
| 864 |
len = server_name->len; |
| 865 |
} |
| 866 |
return aws_byte_buf_from_array(bytes, len); |
| 867 |
} |
| 868 |
|
| 869 |
static struct aws_channel_handler_vtable s_handler_vtable = { |
| 870 |
.destroy = s_destroy, |
| 871 |
.process_read_message = s_process_read_message, |
| 872 |
.process_write_message = s_process_write_message, |
| 873 |
.shutdown = s_handle_shutdown, |
| 874 |
.increment_read_window = s_increment_read_window, |
| 875 |
.initial_window_size = s_initial_window_size, |
| 876 |
.message_overhead = s_message_overhead, |
| 877 |
.reset_statistics = s_reset_statistics, |
| 878 |
.gather_statistics = s_gather_statistics, |
| 879 |
}; |
| 880 |
|
| 881 |
static struct aws_channel_handler *s_tls_handler_new( |
| 882 |
struct aws_allocator *allocator, |
| 883 |
struct aws_tls_connection_options *options, |
| 884 |
struct aws_channel_slot *slot, |
| 885 |
SSLProtocolSide protocol_side) { |
| 886 |
AWS_ASSERT(options->ctx); |
| 887 |
struct secure_transport_ctx *secure_transport_ctx = options->ctx->impl; |
| 888 |
|
| 889 |
struct secure_transport_handler *secure_transport_handler = |
| 890 |
(struct secure_transport_handler *)aws_mem_calloc(allocator, 1, sizeof(struct secure_transport_handler)); |
| 891 |
|
| 892 |
secure_transport_handler->handler.alloc = allocator; |
| 893 |
secure_transport_handler->handler.impl = secure_transport_handler; |
| 894 |
secure_transport_handler->handler.vtable = &s_handler_vtable; |
| 895 |
secure_transport_handler->handler.slot = slot; |
| 896 |
secure_transport_handler->wrapped_allocator = secure_transport_ctx->wrapped_allocator; |
| 897 |
secure_transport_handler->advertise_alpn_message = options->advertise_alpn_message; |
| 898 |
secure_transport_handler->on_data_read = options->on_data_read; |
| 899 |
secure_transport_handler->on_error = options->on_error; |
| 900 |
secure_transport_handler->on_negotiation_result = options->on_negotiation_result; |
| 901 |
secure_transport_handler->user_data = options->user_data; |
| 902 |
|
| 903 |
aws_tls_channel_handler_shared_init( |
| 904 |
&secure_transport_handler->shared_state, &secure_transport_handler->handler, options); |
| 905 |
|
| 906 |
secure_transport_handler->ctx = |
| 907 |
SSLCreateContext(secure_transport_handler->wrapped_allocator, protocol_side, kSSLStreamType); |
| 908 |
|
| 909 |
if (!secure_transport_handler->ctx) { |
| 910 |
AWS_LOGF_FATAL( |
| 911 |
AWS_LS_IO_TLS, "id=%p: failed to initialize an SSL Context.", (void *)&secure_transport_handler->handler); |
| 912 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 913 |
goto cleanup_st_handler; |
| 914 |
} |
| 915 |
|
| 916 |
switch (secure_transport_ctx->minimum_tls_version) { |
| 917 |
case AWS_IO_SSLv3: |
| 918 |
SSLSetProtocolVersionMin(secure_transport_handler->ctx, kSSLProtocol3); |
| 919 |
break; |
| 920 |
case AWS_IO_TLSv1: |
| 921 |
SSLSetProtocolVersionMin(secure_transport_handler->ctx, kTLSProtocol1); |
| 922 |
break; |
| 923 |
case AWS_IO_TLSv1_1: |
| 924 |
SSLSetProtocolVersionMin(secure_transport_handler->ctx, kTLSProtocol12); |
| 925 |
break; |
| 926 |
case AWS_IO_TLSv1_2: |
| 927 |
SSLSetProtocolVersionMin(secure_transport_handler->ctx, kTLSProtocol12); |
| 928 |
break; |
| 929 |
case AWS_IO_TLSv1_3: |
| 930 |
#if TLS13_AVAILABLE |
| 931 |
SSLSetProtocolVersionMin(secure_transport_handler->ctx, kTLSProtocol13); |
| 932 |
#else |
| 933 |
AWS_LOGF_ERROR( |
| 934 |
AWS_LS_IO_TLS, |
| 935 |
"static: Minimum TLS version has been set to TLS 1.3, which is not supported on this device. If " |
| 936 |
"minimum TLS version is not set or AWS_IO_TLS_VER_SYS_DEFAULTS is used, the latest supported version " |
| 937 |
"of TLS will be used automatically."); |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 944 |
goto cleanup_ssl_ctx; |
| 945 |
#endif |
| 946 |
break; |
| 947 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 948 |
default: |
| 949 |
|
| 950 |
SSLSetProtocolVersionMin(secure_transport_handler->ctx, kSSLProtocolUnknown); |
| 951 |
|
| 952 |
break; |
| 953 |
} |
| 954 |
|
| 955 |
if (SSLSetIOFuncs(secure_transport_handler->ctx, s_read_cb, s_write_cb) != noErr || |
| 956 |
SSLSetConnection(secure_transport_handler->ctx, secure_transport_handler) != noErr) { |
| 957 |
AWS_LOGF_FATAL( |
| 958 |
AWS_LS_IO_TLS, "id=%p: failed to initialize an SSL Context.", (void *)&secure_transport_handler->handler); |
| 959 |
aws_raise_error(AWS_IO_TLS_CTX_ERROR); |
| 960 |
goto cleanup_ssl_ctx; |
| 961 |
} |
| 962 |
|
| 963 |
OSStatus status = noErr; |
| 964 |
secure_transport_handler->verify_peer = secure_transport_ctx->verify_peer; |
| 965 |
|
| 966 |
if (!secure_transport_ctx->verify_peer && protocol_side == kSSLClientSide) { |
| 967 |
AWS_LOGF_WARN( |
| 968 |
AWS_LS_IO_TLS, |
| 969 |
"id=%p: x.509 validation has been disabled. " |
| 970 |
"If this is not running in a test environment, this is likely a security vulnerability.", |
| 971 |
(void *)&secure_transport_handler->handler); |
| 972 |
SSLSetSessionOption(secure_transport_handler->ctx, kSSLSessionOptionBreakOnServerAuth, true); |
| 973 |
} |
| 974 |
|
| 975 |
if (secure_transport_ctx->certs) { |
| 976 |
status = SSLSetCertificate(secure_transport_handler->ctx, secure_transport_ctx->certs); |
| 977 |
} |
| 978 |
|
| 979 |
secure_transport_handler->ca_certs = NULL; |
| 980 |
if (secure_transport_ctx->ca_cert) { |
| 981 |
secure_transport_handler->ca_certs = secure_transport_ctx->ca_cert; |
| 982 |
if (protocol_side == kSSLServerSide && secure_transport_ctx->verify_peer) { |
| 983 |
SSLSetSessionOption(secure_transport_handler->ctx, kSSLSessionOptionBreakOnClientAuth, true); |
| 984 |
} else if (secure_transport_ctx->verify_peer) { |
| 985 |
SSLSetSessionOption(secure_transport_handler->ctx, kSSLSessionOptionBreakOnServerAuth, true); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
(void)status; |
| 990 |
|
| 991 |
aws_linked_list_init(&secure_transport_handler->input_queue); |
| 992 |
secure_transport_handler->parent_slot = slot; |
| 993 |
secure_transport_handler->latest_message_completion_user_data = NULL; |
| 994 |
secure_transport_handler->negotiation_finished = false; |
| 995 |
secure_transport_handler->latest_message_on_completion = NULL; |
| 996 |
|
| 997 |
if (options->server_name) { |
| 998 |
secure_transport_handler->server_name = aws_string_new_from_string(allocator, options->server_name); |
| 999 |
size_t server_name_len = options->server_name->len; |
| 1000 |
SSLSetPeerDomainName(secure_transport_handler->ctx, aws_string_c_str(options->server_name), server_name_len); |
| 1001 |
} |
| 1002 |
|
| 1003 |
struct aws_string *alpn_list = NULL; |
| 1004 |
if (options->alpn_list) { |
| 1005 |
AWS_LOGF_DEBUG( |
| 1006 |
AWS_LS_IO_TLS, |
| 1007 |
"id=%p: setting ALPN list %s", |
| 1008 |
(void *)&secure_transport_handler->handler, |
| 1009 |
aws_string_c_str(options->alpn_list)); |
| 1010 |
alpn_list = options->alpn_list; |
| 1011 |
|
| 1012 |
} else if (secure_transport_ctx->alpn_list) { |
| 1013 |
alpn_list = secure_transport_ctx->alpn_list; |
| 1014 |
} |
| 1015 |
|
| 1016 |
if (alpn_list) { |
| 1017 |
s_set_protocols(secure_transport_handler, allocator, alpn_list); |
| 1018 |
} |
| 1019 |
|
| 1020 |
return &secure_transport_handler->handler; |
| 1021 |
|
| 1022 |
cleanup_ssl_ctx: |
| 1023 |
CFRelease(secure_transport_handler->ctx); |
| 1024 |
|
| 1025 |
cleanup_st_handler: |
| 1026 |
aws_mem_release(allocator, secure_transport_handler); |
| 1027 |
|
| 1028 |
return NULL; |
| 1029 |
} |
| 1030 |
|
| 1031 |
static struct aws_channel_handler *s_tls_client_handler_new( |
| 1032 |
struct aws_allocator *allocator, |
| 1033 |
struct aws_tls_connection_options *options, |
| 1034 |
struct aws_channel_slot *slot) { |
| 1035 |
return s_tls_handler_new(allocator, options, slot, kSSLClientSide); |
| 1036 |
} |
| 1037 |
|
| 1038 |
static struct aws_channel_handler *s_tls_server_handler_new( |
| 1039 |
struct aws_allocator *allocator, |
| 1040 |
struct aws_tls_connection_options *options, |
| 1041 |
struct aws_channel_slot *slot) { |
| 1042 |
return s_tls_handler_new(allocator, options, slot, kSSLServerSide); |
| 1043 |
} |
| 1044 |
|
| 1045 |
void s_aws_release_cert(const void *val, void *context) { |
| 1046 |
(void)context; |
| 1047 |
CFTypeRef cert = (CFTypeRef)val; |
| 1048 |
CFRelease(cert); |
| 1049 |
} |
| 1050 |
|
| 1051 |
static void s_aws_secure_transport_ctx_destroy(struct secure_transport_ctx *secure_transport_ctx) { |
| 1052 |
if (secure_transport_ctx == NULL) { |
| 1053 |
return; |
| 1054 |
} |
| 1055 |
|
| 1056 |
if (secure_transport_ctx->certs) { |
| 1057 |
if (secure_transport_ctx->cleanup_cert) { |
| 1058 |
CFRange range = CFRangeMake(0, CFArrayGetCount(secure_transport_ctx->certs)); |
| 1059 |
CFArrayApplyFunction(secure_transport_ctx->certs, range, s_aws_release_cert, NULL); |
| 1060 |
} |
| 1061 |
CFRelease(secure_transport_ctx->certs); |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (secure_transport_ctx->secitem_identity) { |
| 1065 |
CFRelease(secure_transport_ctx->secitem_identity); |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (secure_transport_ctx->ca_cert) { |
| 1069 |
CFRelease(secure_transport_ctx->ca_cert); |
| 1070 |
} |
| 1071 |
|
| 1072 |
aws_string_destroy(secure_transport_ctx->alpn_list); |
| 1073 |
|
| 1074 |
CFRelease(secure_transport_ctx->wrapped_allocator); |
| 1075 |
aws_mem_release(secure_transport_ctx->ctx.alloc, secure_transport_ctx); |
| 1076 |
} |
| 1077 |
|
| 1078 |
static struct aws_tls_ctx *s_tls_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options) { |
| 1079 |
if (!s_tls_is_cipher_pref_supported(options->cipher_pref)) { |
| 1080 |
aws_raise_error(AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED); |
| 1081 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: TLS Cipher Preference is not supported: %d.", options->cipher_pref); |
| 1082 |
return NULL; |
| 1083 |
} |
| 1084 |
|
| 1085 |
struct secure_transport_ctx *secure_transport_ctx = aws_mem_calloc(alloc, 1, sizeof(struct secure_transport_ctx)); |
| 1086 |
|
| 1087 |
secure_transport_ctx->wrapped_allocator = aws_wrapped_cf_allocator_new(alloc); |
| 1088 |
if (!secure_transport_ctx->wrapped_allocator) { |
| 1089 |
goto cleanup_secure_transport_ctx; |
| 1090 |
} |
| 1091 |
|
| 1092 |
secure_transport_ctx->minimum_tls_version = options->minimum_tls_version; |
| 1093 |
|
| 1094 |
if (options->alpn_list) { |
| 1095 |
secure_transport_ctx->alpn_list = aws_string_new_from_string(alloc, options->alpn_list); |
| 1096 |
|
| 1097 |
if (!secure_transport_ctx->alpn_list) { |
| 1098 |
goto cleanup_secure_transport_ctx; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
secure_transport_ctx->verify_peer = options->verify_peer; |
| 1103 |
secure_transport_ctx->ca_cert = NULL; |
| 1104 |
secure_transport_ctx->certs = NULL; |
| 1105 |
secure_transport_ctx->cleanup_cert = false; |
| 1106 |
secure_transport_ctx->secitem_identity = NULL; |
| 1107 |
secure_transport_ctx->ctx.alloc = alloc; |
| 1108 |
secure_transport_ctx->ctx.impl = secure_transport_ctx; |
| 1109 |
aws_ref_count_init( |
| 1110 |
&secure_transport_ctx->ctx.ref_count, |
| 1111 |
secure_transport_ctx, |
| 1112 |
(aws_simple_completion_callback *)s_aws_secure_transport_ctx_destroy); |
| 1113 |
|
| 1114 |
if (aws_tls_options_buf_is_set(&options->certificate) && aws_tls_options_buf_is_set(&options->private_key)) { |
| 1115 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: certificate and key have been set, setting them up now."); |
| 1116 |
|
| 1117 |
if (!aws_text_is_utf8(options->certificate.buffer, options->certificate.len)) { |
| 1118 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import certificate, must be ASCII/UTF-8 encoded"); |
| 1119 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 1120 |
goto cleanup_wrapped_allocator; |
| 1121 |
} |
| 1122 |
|
| 1123 |
if (!aws_text_is_utf8(options->private_key.buffer, options->private_key.len)) { |
| 1124 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import private key, must be ASCII/UTF-8 encoded"); |
| 1125 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 1126 |
goto cleanup_wrapped_allocator; |
| 1127 |
} |
| 1128 |
|
| 1129 |
struct aws_byte_cursor cert_chain_cur = aws_byte_cursor_from_buf(&options->certificate); |
| 1130 |
struct aws_byte_cursor private_key_cur = aws_byte_cursor_from_buf(&options->private_key); |
| 1131 |
if (aws_is_using_secitem()) { |
| 1132 |
if (aws_secitem_import_cert_and_key( |
| 1133 |
alloc, |
| 1134 |
secure_transport_ctx->wrapped_allocator, |
| 1135 |
&cert_chain_cur, |
| 1136 |
&private_key_cur, |
| 1137 |
&secure_transport_ctx->secitem_identity, |
| 1138 |
&options->secitem_options)) { |
| 1139 |
AWS_LOGF_ERROR( |
| 1140 |
AWS_LS_IO_TLS, |
| 1141 |
"static: failed to import certificate and private key with error %d.", |
| 1142 |
aws_last_error()); |
| 1143 |
goto cleanup_wrapped_allocator; |
| 1144 |
} |
| 1145 |
} else { |
| 1146 |
if (aws_import_public_and_private_keys_to_identity( |
| 1147 |
alloc, |
| 1148 |
secure_transport_ctx->wrapped_allocator, |
| 1149 |
&cert_chain_cur, |
| 1150 |
&private_key_cur, |
| 1151 |
&secure_transport_ctx->certs, |
| 1152 |
options->keychain_path)) { |
| 1153 |
AWS_LOGF_ERROR( |
| 1154 |
AWS_LS_IO_TLS, |
| 1155 |
"static: failed to import certificate and private key with error %d.", |
| 1156 |
aws_last_error()); |
| 1157 |
goto cleanup_wrapped_allocator; |
| 1158 |
} |
| 1159 |
secure_transport_ctx->cleanup_cert = true; |
| 1160 |
} |
| 1161 |
} else if (aws_tls_options_buf_is_set(&options->pkcs12)) { |
| 1162 |
|
| 1163 |
struct aws_byte_cursor pkcs12_blob_cur = aws_byte_cursor_from_buf(&options->pkcs12); |
| 1164 |
struct aws_byte_cursor password_cur = aws_byte_cursor_from_buf(&options->pkcs12_password); |
| 1165 |
if (aws_is_using_secitem()) { |
| 1166 |
AWS_LOGF_DEBUG( |
| 1167 |
AWS_LS_IO_TLS, "static: a pkcs#12 certificate and key has been set, setting up for secitem now."); |
| 1168 |
if (aws_secitem_import_pkcs12( |
| 1169 |
secure_transport_ctx->wrapped_allocator, |
| 1170 |
&pkcs12_blob_cur, |
| 1171 |
&password_cur, |
| 1172 |
&secure_transport_ctx->secitem_identity)) { |
| 1173 |
AWS_LOGF_ERROR( |
| 1174 |
AWS_LS_IO_TLS, "static: failed to import pkcs#12 certificate with error %d.", aws_last_error()); |
| 1175 |
goto cleanup_wrapped_allocator; |
| 1176 |
} |
| 1177 |
} else { |
| 1178 |
AWS_LOGF_DEBUG( |
| 1179 |
AWS_LS_IO_TLS, "static: a pkcs#12 certificate and key has been set, setting up for secKeychain now."); |
| 1180 |
if (aws_import_pkcs12_to_identity( |
| 1181 |
secure_transport_ctx->wrapped_allocator, |
| 1182 |
&pkcs12_blob_cur, |
| 1183 |
&password_cur, |
| 1184 |
&secure_transport_ctx->certs)) { |
| 1185 |
AWS_LOGF_ERROR( |
| 1186 |
AWS_LS_IO_TLS, "static: failed to import pkcs#12 certificate with error %d.", aws_last_error()); |
| 1187 |
goto cleanup_wrapped_allocator; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (aws_tls_options_buf_is_set(&options->ca_file)) { |
| 1193 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: loading custom CA file."); |
| 1194 |
|
| 1195 |
struct aws_byte_cursor ca_cursor = aws_byte_cursor_from_buf(&options->ca_file); |
| 1196 |
if (aws_import_trusted_certificates( |
| 1197 |
alloc, secure_transport_ctx->wrapped_allocator, &ca_cursor, &secure_transport_ctx->ca_cert)) { |
| 1198 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import custom CA with error %d", aws_last_error()); |
| 1199 |
goto cleanup_wrapped_allocator; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
return &secure_transport_ctx->ctx; |
| 1204 |
|
| 1205 |
cleanup_wrapped_allocator: |
| 1206 |
aws_wrapped_cf_allocator_destroy(secure_transport_ctx->wrapped_allocator); |
| 1207 |
|
| 1208 |
if (secure_transport_ctx->alpn_list) { |
| 1209 |
aws_string_destroy(secure_transport_ctx->alpn_list); |
| 1210 |
} |
| 1211 |
|
| 1212 |
cleanup_secure_transport_ctx: |
| 1213 |
aws_mem_release(alloc, secure_transport_ctx); |
| 1214 |
|
| 1215 |
return NULL; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static struct aws_tls_ctx *s_tls_server_ctx_new( |
| 1219 |
struct aws_allocator *alloc, |
| 1220 |
const struct aws_tls_ctx_options *options) { |
| 1221 |
return s_tls_ctx_new(alloc, options); |
| 1222 |
} |
| 1223 |
|
| 1224 |
static struct aws_tls_ctx *s_tls_client_ctx_new( |
| 1225 |
struct aws_allocator *alloc, |
| 1226 |
const struct aws_tls_ctx_options *options) { |
| 1227 |
return s_tls_ctx_new(alloc, options); |
| 1228 |
} |
| 1229 |
|
| 1230 |
static struct aws_tls_vtable s_vtable = { |
| 1231 |
.init_static_state = s_aws_tls_init_static_state, |
| 1232 |
.clean_up_static_state = s_tls_clean_up_static_state, |
| 1233 |
.is_alpn_available = s_tls_is_alpn_available, |
| 1234 |
.is_cipher_pref_supported = s_tls_is_cipher_pref_supported, |
| 1235 |
.client_handler_start_negotiation = s_tls_client_handler_start_negotiation, |
| 1236 |
.key_operation_complete = NULL, |
| 1237 |
.key_operation_complete_with_error = NULL, |
| 1238 |
.key_operation_get_input = NULL, |
| 1239 |
.key_operation_get_type = NULL, |
| 1240 |
.key_operation_get_signature_algorithm = NULL, |
| 1241 |
.key_operation_get_digest_algorithm = NULL, |
| 1242 |
.handler_protocol = s_tls_handler_protocol, |
| 1243 |
.handler_server_name = s_tls_handler_server_name, |
| 1244 |
.client_handler_new = s_tls_client_handler_new, |
| 1245 |
.server_handler_new = s_tls_server_handler_new, |
| 1246 |
.server_ctx_new = s_tls_server_ctx_new, |
| 1247 |
.client_ctx_new = s_tls_client_ctx_new, |
| 1248 |
}; |
| 1249 |
|
| 1250 |
void secure_transport_init_tls_vtable(struct aws_tls_vtable *vtable) { |
| 1251 |
*vtable = s_vtable; |
| 1252 |
} |
| 1253 |
|
| 1254 |
#pragma clang diagnostic pop |