| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#define SECURITY_WIN32 |
| 6 |
|
| 7 |
#include <aws/io/tls_channel_handler.h> |
| 8 |
|
| 9 |
#include <aws/common/encoding.h> |
| 10 |
#include <aws/common/math.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
#include <aws/common/task_scheduler.h> |
| 13 |
|
| 14 |
#include <aws/io/channel.h> |
| 15 |
#include <aws/io/file_utils.h> |
| 16 |
#include <aws/io/logging.h> |
| 17 |
#include <aws/io/private/pki_utils.h> |
| 18 |
#include <aws/io/private/tls_channel_handler_private.h> |
| 19 |
#include <aws/io/private/tls_channel_handler_shared.h> |
| 20 |
#include <aws/io/statistics.h> |
| 21 |
|
| 22 |
|
| 23 |
#define SCHANNEL_USE_BLACKLISTS |
| 24 |
#include <windows.h> |
| 25 |
|
| 26 |
#include <subauth.h> |
| 27 |
|
| 28 |
#include <schannel.h> |
| 29 |
#include <security.h> |
| 30 |
#include <sspi.h> |
| 31 |
|
| 32 |
#include <errno.h> |
| 33 |
#include <inttypes.h> |
| 34 |
#include <math.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
|
| 38 |
#ifdef _MSC_VER |
| 39 |
# pragma warning(disable : 4221) |
| 40 |
# pragma warning(disable : 4204) |
| 41 |
# pragma warning(disable : 4306) |
| 42 |
#endif |
| 43 |
|
| 44 |
#define KB_1 1024 |
| 45 |
#define READ_OUT_SIZE (16 * KB_1) |
| 46 |
#define READ_IN_SIZE READ_OUT_SIZE |
| 47 |
#define EST_HANDSHAKE_SIZE (7 * KB_1) |
| 48 |
#define WINDOWS_BUILD_20348 20348 |
| 49 |
|
| 50 |
#define EST_TLS_RECORD_OVERHEAD 53 |
| 51 |
|
| 52 |
static bool s_use_schannel_creds = false; |
| 53 |
|
| 54 |
static struct aws_byte_buf s_tls_handler_server_name(struct aws_channel_handler *handler); |
| 55 |
|
| 56 |
static void s_aws_tls_init_static_state(struct aws_allocator *alloc) { |
| 57 |
AWS_LOGF_INFO(AWS_LS_IO_TLS, "static: Initializing TLS using SecureChannel (SSPI)."); |
| 58 |
(void)alloc; |
| 59 |
} |
| 60 |
|
| 61 |
static void s_tls_clean_up_static_state(void) {} |
| 62 |
|
| 63 |
struct common_credential_params { |
| 64 |
DWORD dwFlags; |
| 65 |
PCCERT_CONTEXT *paCred; |
| 66 |
DWORD cCreds; |
| 67 |
}; |
| 68 |
|
| 69 |
struct secure_channel_ctx { |
| 70 |
struct aws_tls_ctx ctx; |
| 71 |
struct aws_string *alpn_list; |
| 72 |
struct common_credential_params schannel_creds; |
| 73 |
SCHANNEL_CRED credentials; |
| 74 |
PCCERT_CONTEXT pcerts; |
| 75 |
HCERTSTORE cert_store; |
| 76 |
HCERTSTORE custom_trust_store; |
| 77 |
HCRYPTPROV crypto_provider; |
| 78 |
HCRYPTKEY private_key; |
| 79 |
bool verify_peer; |
| 80 |
bool should_free_pcerts; |
| 81 |
enum aws_tls_versions minimum_tls_version; |
| 82 |
bool disable_tls13; |
| 83 |
}; |
| 84 |
|
| 85 |
struct secure_channel_handler { |
| 86 |
struct aws_channel_handler handler; |
| 87 |
struct aws_tls_channel_handler_shared shared_state; |
| 88 |
CtxtHandle sec_handle; |
| 89 |
CredHandle creds; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
PCCERT_CONTEXT cert_context[1]; |
| 95 |
HCERTSTORE cert_store; |
| 96 |
HCERTSTORE custom_ca_store; |
| 97 |
SecPkgContext_StreamSizes stream_sizes; |
| 98 |
unsigned long ctx_req; |
| 99 |
unsigned long ctx_ret_flags; |
| 100 |
struct aws_channel_slot *slot; |
| 101 |
struct aws_byte_buf protocol; |
| 102 |
struct aws_byte_buf server_name; |
| 103 |
TimeStamp sspi_timestamp; |
| 104 |
int (*s_connection_state_fn)(struct aws_channel_handler *handler); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
uint8_t buffered_read_in_data[READ_IN_SIZE + KB_1]; |
| 109 |
struct aws_byte_buf buffered_read_in_data_buf; |
| 110 |
size_t estimated_incomplete_size; |
| 111 |
size_t read_extra; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
uint8_t buffered_read_out_data[READ_OUT_SIZE + KB_1]; |
| 116 |
struct aws_byte_buf buffered_read_out_data_buf; |
| 117 |
struct aws_channel_task sequential_task_storage; |
| 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 |
struct aws_string *alpn_list; |
| 122 |
void *user_data; |
| 123 |
bool advertise_alpn_message; |
| 124 |
bool negotiation_finished; |
| 125 |
bool verify_peer; |
| 126 |
struct aws_channel_task read_task; |
| 127 |
bool read_task_pending; |
| 128 |
enum aws_tls_handler_read_state read_state; |
| 129 |
int shutdown_error_code; |
| 130 |
}; |
| 131 |
|
| 132 |
static size_t s_message_overhead(struct aws_channel_handler *handler) { |
| 133 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 134 |
|
| 135 |
if (AWS_UNLIKELY(!sc_handler->stream_sizes.cbMaximumMessage)) { |
| 136 |
SECURITY_STATUS status = |
| 137 |
QueryContextAttributes(&sc_handler->sec_handle, SECPKG_ATTR_STREAM_SIZES, &sc_handler->stream_sizes); |
| 138 |
|
| 139 |
if (status != SEC_E_OK) { |
| 140 |
return EST_TLS_RECORD_OVERHEAD; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return sc_handler->stream_sizes.cbTrailer + sc_handler->stream_sizes.cbHeader; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static bool s_is_windows_equal_or_above_build_number(DWORD build_number) { |
| 152 |
NTSTATUS status = STATUS_DLL_NOT_FOUND; |
| 153 |
|
| 154 |
OSVERSIONINFOEX osvi; |
| 155 |
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); |
| 156 |
|
| 157 |
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
| 158 |
osvi.dwBuildNumber = build_number; |
| 159 |
|
| 160 |
ULONGLONG dwlConditionMask = 0; |
| 161 |
BYTE op = VER_GREATER_EQUAL; |
| 162 |
dwlConditionMask = VerSetConditionMask(dwlConditionMask, VER_BUILDNUMBER, op); |
| 163 |
typedef NTSTATUS(WINAPI * pRtlGetVersionInfo)( |
| 164 |
OSVERSIONINFOEX * lpVersionInformation, ULONG TypeMask, ULONGLONG ConditionMask); |
| 165 |
|
| 166 |
pRtlGetVersionInfo f; |
| 167 |
f = (pRtlGetVersionInfo)GetProcAddress(GetModuleHandle("ntdll"), "RtlVerifyVersionInfo"); |
| 168 |
|
| 169 |
if (f) { |
| 170 |
status = f(&osvi, VER_BUILDNUMBER, dwlConditionMask); |
| 171 |
} else { |
| 172 |
AWS_LOGF_ERROR( |
| 173 |
AWS_LS_IO_TLS, |
| 174 |
"Could not load ntdll: Falling back to earlier windows version before build %d", |
| 175 |
build_number); |
| 176 |
status = STATUS_DLL_NOT_FOUND; |
| 177 |
} |
| 178 |
if (status == STATUS_SUCCESS) { |
| 179 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "Checking Windows Version: running windows build %u or later", build_number); |
| 180 |
return true; |
| 181 |
} else if (status != STATUS_DLL_NOT_FOUND) { |
| 182 |
AWS_LOGF_DEBUG( |
| 183 |
AWS_LS_IO_TLS, |
| 184 |
"Checking Windows Version: Running earlier windows 10 version before build %u", |
| 185 |
build_number); |
| 186 |
return false; |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
static bool s_tls_is_alpn_available(void) { |
| 192 |
|
| 193 |
|
| 194 |
#ifdef SECBUFFER_APPLICATION_PROTOCOLS |
| 195 |
AWS_LOGF_DEBUG( |
| 196 |
AWS_LS_IO_TLS, |
| 197 |
"static: This library was built with Windows 8.1 or later, " |
| 198 |
"probing OS to see what we're actually running on."); |
| 199 |
|
| 200 |
OSVERSIONINFOEX os_version; |
| 201 |
DWORDLONG condition_mask = 0; |
| 202 |
VER_SET_CONDITION(condition_mask, VER_MAJORVERSION, VER_GREATER_EQUAL); |
| 203 |
VER_SET_CONDITION(condition_mask, VER_MINORVERSION, VER_GREATER_EQUAL); |
| 204 |
VER_SET_CONDITION(condition_mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); |
| 205 |
VER_SET_CONDITION(condition_mask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL); |
| 206 |
|
| 207 |
AWS_ZERO_STRUCT(os_version); |
| 208 |
os_version.dwMajorVersion = HIBYTE(_WIN32_WINNT_WIN8); |
| 209 |
os_version.dwMinorVersion = LOBYTE(_WIN32_WINNT_WIN8); |
| 210 |
os_version.wServicePackMajor = 0; |
| 211 |
os_version.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
| 212 |
|
| 213 |
if (VerifyVersionInfo( |
| 214 |
&os_version, |
| 215 |
VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, |
| 216 |
condition_mask)) { |
| 217 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: We're running on Windows 8.1 or later. ALPN is available."); |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
AWS_LOGF_WARN( |
| 222 |
AWS_LS_IO_TLS, |
| 223 |
"static: Running on older version of windows, ALPN is not supported. " |
| 224 |
"Please update your OS to take advantage of modern features."); |
| 225 |
#else |
| 226 |
AWS_LOGF_WARN( |
| 227 |
AWS_LS_IO_TLS, |
| 228 |
"static: This library was built using a Windows SDK prior to 8.1. " |
| 229 |
"Please build with a version of windows >= 8.1 to take advantage modern features. ALPN is not supported."); |
| 230 |
#endif |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
static bool s_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref) { |
| 235 |
switch (cipher_pref) { |
| 236 |
case AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT: |
| 237 |
return true; |
| 238 |
|
| 239 |
case AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2019_06: |
| 240 |
default: |
| 241 |
return false; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
#define MAX_HOST_LENGTH 255 |
| 247 |
|
| 248 |
|
| 249 |
static int s_manually_verify_peer_cert(struct aws_channel_handler *handler) { |
| 250 |
AWS_LOGF_DEBUG( |
| 251 |
AWS_LS_IO_TLS, |
| 252 |
"id=%p: manually verifying certifcate chain because a custom CA is configured.", |
| 253 |
(void *)handler); |
| 254 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 255 |
|
| 256 |
int result = AWS_OP_ERR; |
| 257 |
CERT_CONTEXT *peer_certificate = NULL; |
| 258 |
HCERTCHAINENGINE engine = NULL; |
| 259 |
PCCERT_CHAIN_CONTEXT cert_chain_ctx = NULL; |
| 260 |
|
| 261 |
|
| 262 |
SECURITY_STATUS status = |
| 263 |
QueryContextAttributes(&sc_handler->sec_handle, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &peer_certificate); |
| 264 |
|
| 265 |
if (status != SEC_E_OK || !peer_certificate) { |
| 266 |
AWS_LOGF_ERROR( |
| 267 |
AWS_LS_IO_TLS, |
| 268 |
"id=%p: failed to load peer's certificate with SECURITY_STATUS %d", |
| 269 |
(void *)handler, |
| 270 |
(int)status); |
| 271 |
return AWS_OP_ERR; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
CERT_CHAIN_ENGINE_CONFIG engine_config; |
| 277 |
AWS_ZERO_STRUCT(engine_config); |
| 278 |
engine_config.cbSize = sizeof(engine_config); |
| 279 |
engine_config.hExclusiveRoot = sc_handler->custom_ca_store; |
| 280 |
|
| 281 |
if (!CertCreateCertificateChainEngine(&engine_config, &engine)) { |
| 282 |
AWS_LOGF_ERROR( |
| 283 |
AWS_LS_IO_TLS, |
| 284 |
"id=%p: failed to load a certificate chain engine with SECURITY_STATUS %d. " |
| 285 |
"Most likely, the configured CA is corrupted.", |
| 286 |
(void *)handler, |
| 287 |
(int)status); |
| 288 |
goto done; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
DWORD get_chain_flags = 0; |
| 295 |
|
| 296 |
|
| 297 |
const LPCSTR usage_identifiers[] = { |
| 298 |
szOID_PKIX_KP_SERVER_AUTH, |
| 299 |
szOID_SERVER_GATED_CRYPTO, |
| 300 |
szOID_SGC_NETSCAPE, |
| 301 |
}; |
| 302 |
|
| 303 |
CERT_CHAIN_PARA chain_params; |
| 304 |
AWS_ZERO_STRUCT(chain_params); |
| 305 |
chain_params.cbSize = sizeof(chain_params); |
| 306 |
chain_params.RequestedUsage.dwType = USAGE_MATCH_TYPE_OR; |
| 307 |
chain_params.RequestedUsage.Usage.cUsageIdentifier = AWS_ARRAY_SIZE(usage_identifiers); |
| 308 |
chain_params.RequestedUsage.Usage.rgpszUsageIdentifier = (LPSTR *)usage_identifiers; |
| 309 |
|
| 310 |
if (!CertGetCertificateChain( |
| 311 |
engine, |
| 312 |
peer_certificate, |
| 313 |
NULL, |
| 314 |
peer_certificate->hCertStore, |
| 315 |
&chain_params, |
| 316 |
get_chain_flags, |
| 317 |
NULL, |
| 318 |
&cert_chain_ctx)) { |
| 319 |
AWS_LOGF_ERROR( |
| 320 |
AWS_LS_IO_TLS, |
| 321 |
"id=%p: unable to find certificate in chain with SECURITY_STATUS %d.", |
| 322 |
(void *)handler, |
| 323 |
(int)status); |
| 324 |
goto done; |
| 325 |
} |
| 326 |
|
| 327 |
struct aws_byte_buf host = s_tls_handler_server_name(handler); |
| 328 |
if (host.len > MAX_HOST_LENGTH) { |
| 329 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: host name too long (%d).", (void *)handler, (int)host.len); |
| 330 |
goto done; |
| 331 |
} |
| 332 |
|
| 333 |
wchar_t whost[MAX_HOST_LENGTH + 1]; |
| 334 |
AWS_ZERO_ARRAY(whost); |
| 335 |
|
| 336 |
int converted = MultiByteToWideChar( |
| 337 |
CP_UTF8, MB_ERR_INVALID_CHARS, (const char *)host.buffer, (int)host.len, whost, AWS_ARRAY_SIZE(whost)); |
| 338 |
if ((size_t)converted != host.len) { |
| 339 |
AWS_LOGF_ERROR( |
| 340 |
AWS_LS_IO_TLS, |
| 341 |
"id=%p: unable to convert host to wstr, %d -> %d, with last error 0x%x.", |
| 342 |
(void *)handler, |
| 343 |
(int)host.len, |
| 344 |
(int)converted, |
| 345 |
(int)GetLastError()); |
| 346 |
goto done; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
LPCSTR policyiod = CERT_CHAIN_POLICY_SSL; |
| 351 |
|
| 352 |
SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslpolicy; |
| 353 |
AWS_ZERO_STRUCT(sslpolicy); |
| 354 |
sslpolicy.cbSize = sizeof(sslpolicy); |
| 355 |
sslpolicy.dwAuthType = AUTHTYPE_SERVER; |
| 356 |
sslpolicy.fdwChecks = 0; |
| 357 |
sslpolicy.pwszServerName = whost; |
| 358 |
|
| 359 |
CERT_CHAIN_POLICY_PARA policypara; |
| 360 |
AWS_ZERO_STRUCT(policypara); |
| 361 |
policypara.cbSize = sizeof(policypara); |
| 362 |
policypara.dwFlags = 0; |
| 363 |
policypara.pvExtraPolicyPara = &sslpolicy; |
| 364 |
|
| 365 |
CERT_CHAIN_POLICY_STATUS policystatus; |
| 366 |
AWS_ZERO_STRUCT(policystatus); |
| 367 |
policystatus.cbSize = sizeof(policystatus); |
| 368 |
|
| 369 |
if (!CertVerifyCertificateChainPolicy(policyiod, cert_chain_ctx, &policypara, &policystatus)) { |
| 370 |
int error = GetLastError(); |
| 371 |
AWS_LOGF_ERROR( |
| 372 |
AWS_LS_IO_TLS, "id=%p: CertVerifyCertificateChainPolicy() failed, error 0x%x", (void *)handler, (int)error); |
| 373 |
goto done; |
| 374 |
} |
| 375 |
|
| 376 |
if (policystatus.dwError) { |
| 377 |
AWS_LOGF_ERROR( |
| 378 |
AWS_LS_IO_TLS, |
| 379 |
"id=%p: certificate verification failed, error 0x%x", |
| 380 |
(void *)handler, |
| 381 |
(int)policystatus.dwError); |
| 382 |
goto done; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
CERT_SIMPLE_CHAIN *simple_chain = cert_chain_ctx->rgpChain[0]; |
| 388 |
DWORD trust_mask = ~(DWORD)CERT_TRUST_IS_NOT_TIME_NESTED; |
| 389 |
trust_mask &= simple_chain->TrustStatus.dwErrorStatus; |
| 390 |
|
| 391 |
if (trust_mask != 0) { |
| 392 |
AWS_LOGF_ERROR( |
| 393 |
AWS_LS_IO_TLS, |
| 394 |
"id=%p: peer certificate is un-trusted with SECURITY_STATUS %d.", |
| 395 |
(void *)handler, |
| 396 |
(int)trust_mask); |
| 397 |
goto done; |
| 398 |
} |
| 399 |
|
| 400 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: peer certificate is trusted.", (void *)handler); |
| 401 |
result = AWS_OP_SUCCESS; |
| 402 |
|
| 403 |
done: |
| 404 |
|
| 405 |
if (cert_chain_ctx != NULL) { |
| 406 |
CertFreeCertificateChain(cert_chain_ctx); |
| 407 |
} |
| 408 |
|
| 409 |
if (engine != NULL) { |
| 410 |
CertFreeCertificateChainEngine(engine); |
| 411 |
} |
| 412 |
|
| 413 |
if (peer_certificate != NULL) { |
| 414 |
CertFreeCertificateContext(peer_certificate); |
| 415 |
} |
| 416 |
|
| 417 |
return result; |
| 418 |
} |
| 419 |
|
| 420 |
static void s_invoke_negotiation_error(struct aws_channel_handler *handler, int err) { |
| 421 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 422 |
|
| 423 |
aws_on_tls_negotiation_completed(&sc_handler->shared_state, err); |
| 424 |
|
| 425 |
if (sc_handler->on_negotiation_result) { |
| 426 |
sc_handler->on_negotiation_result(handler, sc_handler->slot, err, sc_handler->user_data); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
static void s_on_negotiation_success(struct aws_channel_handler *handler) { |
| 431 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 432 |
|
| 433 |
|
| 434 |
if (sc_handler->slot->adj_right && sc_handler->advertise_alpn_message && sc_handler->protocol.len) { |
| 435 |
struct aws_io_message *message = aws_channel_acquire_message_from_pool( |
| 436 |
sc_handler->slot->channel, |
| 437 |
AWS_IO_MESSAGE_APPLICATION_DATA, |
| 438 |
sizeof(struct aws_tls_negotiated_protocol_message)); |
| 439 |
message->message_tag = AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE; |
| 440 |
struct aws_tls_negotiated_protocol_message *protocol_message = |
| 441 |
(struct aws_tls_negotiated_protocol_message *)message->message_data.buffer; |
| 442 |
|
| 443 |
protocol_message->protocol = sc_handler->protocol; |
| 444 |
message->message_data.len = sizeof(struct aws_tls_negotiated_protocol_message); |
| 445 |
if (aws_channel_slot_send_message(sc_handler->slot, message, AWS_CHANNEL_DIR_READ)) { |
| 446 |
aws_mem_release(message->allocator, message); |
| 447 |
aws_channel_shutdown(sc_handler->slot->channel, aws_last_error()); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
aws_on_tls_negotiation_completed(&sc_handler->shared_state, AWS_ERROR_SUCCESS); |
| 452 |
|
| 453 |
if (sc_handler->on_negotiation_result) { |
| 454 |
sc_handler->on_negotiation_result(handler, sc_handler->slot, AWS_OP_SUCCESS, sc_handler->user_data); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
static int s_determine_sspi_error(int sspi_status) { |
| 459 |
switch (sspi_status) { |
| 460 |
case SEC_E_INSUFFICIENT_MEMORY: |
| 461 |
return AWS_ERROR_OOM; |
| 462 |
case SEC_I_CONTEXT_EXPIRED: |
| 463 |
return AWS_IO_TLS_ALERT_NOT_GRACEFUL; |
| 464 |
case SEC_E_WRONG_PRINCIPAL: |
| 465 |
return AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE; |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
default: |
| 478 |
return AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
#define CHECK_ALPN_BUFFER_SIZE(s, i, b) \ |
| 483 |
if (s <= i) { \ |
| 484 |
aws_array_list_clean_up(&b); \ |
| 485 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); \ |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
static int s_fillin_alpn_data( |
| 493 |
struct aws_channel_handler *handler, |
| 494 |
unsigned char *alpn_buffer_data, |
| 495 |
size_t buffer_size, |
| 496 |
size_t *written) { |
| 497 |
*written = 0; |
| 498 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 499 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, ""); |
| 500 |
|
| 501 |
struct aws_array_list alpn_buffers; |
| 502 |
struct aws_byte_cursor alpn_buffer_array[4]; |
| 503 |
aws_array_list_init_static(&alpn_buffers, alpn_buffer_array, 4, sizeof(struct aws_byte_cursor)); |
| 504 |
|
| 505 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "Setting ALPN extension with string %s.", aws_string_c_str(sc_handler->alpn_list)); |
| 506 |
struct aws_byte_cursor alpn_str_cur = aws_byte_cursor_from_string(sc_handler->alpn_list); |
| 507 |
if (aws_byte_cursor_split_on_char(&alpn_str_cur, ';', &alpn_buffers)) { |
| 508 |
return AWS_OP_ERR; |
| 509 |
} |
| 510 |
|
| 511 |
size_t protocols_count = aws_array_list_length(&alpn_buffers); |
| 512 |
|
| 513 |
size_t index = 0; |
| 514 |
CHECK_ALPN_BUFFER_SIZE(buffer_size, index + sizeof(uint32_t), alpn_buffers) |
| 515 |
uint32_t *extension_length = (uint32_t *)&alpn_buffer_data[index]; |
| 516 |
index += sizeof(uint32_t); |
| 517 |
CHECK_ALPN_BUFFER_SIZE(buffer_size, index + sizeof(uint32_t), alpn_buffers) |
| 518 |
uint32_t *extension_name = (uint32_t *)&alpn_buffer_data[index]; |
| 519 |
index += sizeof(uint32_t); |
| 520 |
CHECK_ALPN_BUFFER_SIZE(buffer_size, index + sizeof(uint32_t), alpn_buffers) |
| 521 |
uint16_t *protocols_byte_length = (uint16_t *)&alpn_buffer_data[index]; |
| 522 |
index += sizeof(uint16_t); |
| 523 |
CHECK_ALPN_BUFFER_SIZE(buffer_size, index + sizeof(uint16_t), alpn_buffers) |
| 524 |
|
| 525 |
*extension_length += sizeof(uint32_t) + sizeof(uint16_t); |
| 526 |
|
| 527 |
*extension_name = SecApplicationProtocolNegotiationExt_ALPN; |
| 528 |
|
| 529 |
for (size_t i = 0; i < protocols_count; ++i) { |
| 530 |
struct aws_byte_cursor *protocol_ptr = NULL; |
| 531 |
aws_array_list_get_at_ptr(&alpn_buffers, (void **)&protocol_ptr, i); |
| 532 |
AWS_ASSERT(protocol_ptr); |
| 533 |
*extension_length += (uint32_t)protocol_ptr->len + 1; |
| 534 |
*protocols_byte_length += (uint16_t)protocol_ptr->len + 1; |
| 535 |
CHECK_ALPN_BUFFER_SIZE(buffer_size, index + 1, alpn_buffers) |
| 536 |
alpn_buffer_data[index++] = (unsigned char)protocol_ptr->len; |
| 537 |
CHECK_ALPN_BUFFER_SIZE(buffer_size, index + protocol_ptr->len, alpn_buffers) |
| 538 |
memcpy(alpn_buffer_data + index, protocol_ptr->ptr, protocol_ptr->len); |
| 539 |
index += protocol_ptr->len; |
| 540 |
} |
| 541 |
|
| 542 |
aws_array_list_clean_up(&alpn_buffers); |
| 543 |
*written = *extension_length + sizeof(uint32_t); |
| 544 |
return AWS_OP_SUCCESS; |
| 545 |
} |
| 546 |
|
| 547 |
static int s_do_application_data_decrypt(struct aws_channel_handler *handler); |
| 548 |
|
| 549 |
static int s_do_server_side_negotiation_step_2(struct aws_channel_handler *handler); |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
static int s_do_server_side_negotiation_step_1(struct aws_channel_handler *handler) { |
| 554 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 555 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: server starting negotiation", (void *)handler); |
| 556 |
|
| 557 |
aws_on_drive_tls_negotiation(&sc_handler->shared_state); |
| 558 |
|
| 559 |
|
| 560 |
int aws_error = 0; |
| 561 |
|
| 562 |
unsigned char alpn_buffer_data[128] = {0}; |
| 563 |
SecBuffer input_bufs[] = { |
| 564 |
{ |
| 565 |
.pvBuffer = sc_handler->buffered_read_in_data_buf.buffer, |
| 566 |
.cbBuffer = (unsigned long)sc_handler->buffered_read_in_data_buf.len, |
| 567 |
.BufferType = SECBUFFER_TOKEN, |
| 568 |
}, |
| 569 |
{ |
| 570 |
.pvBuffer = NULL, |
| 571 |
.cbBuffer = 0, |
| 572 |
.BufferType = SECBUFFER_EMPTY, |
| 573 |
}, |
| 574 |
}; |
| 575 |
|
| 576 |
SecBufferDesc input_bufs_desc = { |
| 577 |
.ulVersion = SECBUFFER_VERSION, |
| 578 |
.cBuffers = 2, |
| 579 |
.pBuffers = input_bufs, |
| 580 |
}; |
| 581 |
|
| 582 |
SecBuffer output_buffer = { |
| 583 |
.pvBuffer = NULL, |
| 584 |
.cbBuffer = 0, |
| 585 |
.BufferType = SECBUFFER_TOKEN, |
| 586 |
}; |
| 587 |
|
| 588 |
SecBufferDesc output_buffer_desc = { |
| 589 |
.ulVersion = SECBUFFER_VERSION, |
| 590 |
.cBuffers = 1, |
| 591 |
.pBuffers = &output_buffer, |
| 592 |
}; |
| 593 |
|
| 594 |
#ifdef SECBUFFER_APPLICATION_PROTOCOLS |
| 595 |
if (sc_handler->alpn_list && s_tls_is_alpn_available()) { |
| 596 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Setting ALPN to %s", handler, aws_string_c_str(sc_handler->alpn_list)); |
| 597 |
size_t extension_length = 0; |
| 598 |
if (s_fillin_alpn_data(handler, alpn_buffer_data, sizeof(alpn_buffer_data), &extension_length)) { |
| 599 |
aws_error = aws_last_error(); |
| 600 |
goto cleanup; |
| 601 |
} |
| 602 |
|
| 603 |
input_bufs[1].pvBuffer = alpn_buffer_data, input_bufs[1].cbBuffer = (unsigned long)extension_length, |
| 604 |
input_bufs[1].BufferType = SECBUFFER_APPLICATION_PROTOCOLS; |
| 605 |
} |
| 606 |
#endif |
| 607 |
|
| 608 |
sc_handler->ctx_req = ASC_REQ_SEQUENCE_DETECT | ASC_REQ_REPLAY_DETECT | ASC_REQ_CONFIDENTIALITY | |
| 609 |
ASC_REQ_ALLOCATE_MEMORY | ASC_REQ_STREAM; |
| 610 |
|
| 611 |
if (sc_handler->verify_peer) { |
| 612 |
AWS_LOGF_DEBUG( |
| 613 |
AWS_LS_IO_TLS, |
| 614 |
"id=%p: server configured to use mutual tls, expecting a certficate from client.", |
| 615 |
(void *)handler); |
| 616 |
sc_handler->ctx_req |= ASC_REQ_MUTUAL_AUTH; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
SECURITY_STATUS status = AcceptSecurityContext( |
| 621 |
&sc_handler->creds, |
| 622 |
NULL, |
| 623 |
&input_bufs_desc, |
| 624 |
sc_handler->ctx_req, |
| 625 |
0, |
| 626 |
&sc_handler->sec_handle, |
| 627 |
&output_buffer_desc, |
| 628 |
&sc_handler->ctx_ret_flags, |
| 629 |
NULL); |
| 630 |
|
| 631 |
if (!(status == SEC_I_CONTINUE_NEEDED || status == SEC_E_OK)) { |
| 632 |
AWS_LOGF_ERROR( |
| 633 |
AWS_LS_IO_TLS, |
| 634 |
"id=%p: error during processing of the ClientHello. SECURITY_STATUS is %d", |
| 635 |
(void *)handler, |
| 636 |
(int)status); |
| 637 |
aws_error = s_determine_sspi_error(status); |
| 638 |
goto cleanup; |
| 639 |
} |
| 640 |
|
| 641 |
size_t data_to_write_len = output_buffer.cbBuffer; |
| 642 |
|
| 643 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Sending ServerHello. Data size %zu", (void *)handler, data_to_write_len); |
| 644 |
|
| 645 |
struct aws_io_message *outgoing_message = aws_channel_acquire_message_from_pool( |
| 646 |
sc_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, data_to_write_len); |
| 647 |
|
| 648 |
AWS_FATAL_ASSERT(outgoing_message->message_data.capacity >= data_to_write_len); |
| 649 |
memcpy(outgoing_message->message_data.buffer, output_buffer.pvBuffer, output_buffer.cbBuffer); |
| 650 |
outgoing_message->message_data.len = output_buffer.cbBuffer; |
| 651 |
|
| 652 |
if (aws_channel_slot_send_message(sc_handler->slot, outgoing_message, AWS_CHANNEL_DIR_WRITE)) { |
| 653 |
aws_error = aws_last_error(); |
| 654 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 655 |
goto cleanup; |
| 656 |
} |
| 657 |
|
| 658 |
sc_handler->s_connection_state_fn = s_do_server_side_negotiation_step_2; |
| 659 |
|
| 660 |
cleanup: |
| 661 |
FreeContextBuffer(output_buffer.pvBuffer); |
| 662 |
if (aws_error != 0) { |
| 663 |
s_invoke_negotiation_error(handler, aws_error); |
| 664 |
return aws_raise_error(aws_error); |
| 665 |
} |
| 666 |
return AWS_OP_SUCCESS; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
static int s_do_server_side_negotiation_step_2(struct aws_channel_handler *handler) { |
| 671 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 672 |
|
| 673 |
|
| 674 |
int aws_error = 0; |
| 675 |
|
| 676 |
AWS_LOGF_TRACE( |
| 677 |
AWS_LS_IO_TLS, "id=%p: running step 2 of negotiation (cipher change, key exchange etc...)", (void *)handler); |
| 678 |
SecBuffer input_buffers[] = { |
| 679 |
[0] = |
| 680 |
{ |
| 681 |
.pvBuffer = sc_handler->buffered_read_in_data_buf.buffer, |
| 682 |
.cbBuffer = (unsigned long)sc_handler->buffered_read_in_data_buf.len, |
| 683 |
.BufferType = SECBUFFER_TOKEN, |
| 684 |
}, |
| 685 |
[1] = |
| 686 |
{ |
| 687 |
.pvBuffer = NULL, |
| 688 |
.cbBuffer = 0, |
| 689 |
.BufferType = SECBUFFER_EMPTY, |
| 690 |
}, |
| 691 |
}; |
| 692 |
|
| 693 |
SecBufferDesc input_buffers_desc = { |
| 694 |
.ulVersion = SECBUFFER_VERSION, |
| 695 |
.cBuffers = 2, |
| 696 |
.pBuffers = input_buffers, |
| 697 |
}; |
| 698 |
|
| 699 |
SecBuffer output_buffers[3]; |
| 700 |
AWS_ZERO_ARRAY(output_buffers); |
| 701 |
output_buffers[0].BufferType = SECBUFFER_TOKEN; |
| 702 |
output_buffers[1].BufferType = SECBUFFER_ALERT; |
| 703 |
|
| 704 |
SecBufferDesc output_buffers_desc = { |
| 705 |
.ulVersion = SECBUFFER_VERSION, |
| 706 |
.cBuffers = 3, |
| 707 |
.pBuffers = output_buffers, |
| 708 |
}; |
| 709 |
|
| 710 |
sc_handler->read_extra = 0; |
| 711 |
sc_handler->estimated_incomplete_size = 0; |
| 712 |
|
| 713 |
SECURITY_STATUS status = AcceptSecurityContext( |
| 714 |
&sc_handler->creds, |
| 715 |
&sc_handler->sec_handle, |
| 716 |
&input_buffers_desc, |
| 717 |
sc_handler->ctx_req, |
| 718 |
0, |
| 719 |
NULL, |
| 720 |
&output_buffers_desc, |
| 721 |
&sc_handler->ctx_ret_flags, |
| 722 |
&sc_handler->sspi_timestamp); |
| 723 |
|
| 724 |
if (status != SEC_E_INCOMPLETE_MESSAGE && status != SEC_I_CONTINUE_NEEDED && status != SEC_E_OK) { |
| 725 |
AWS_LOGF_ERROR( |
| 726 |
AWS_LS_IO_TLS, "id=%p: Error during negotiation. SECURITY_STATUS is %d", (void *)handler, (int)status); |
| 727 |
aws_error = s_determine_sspi_error(status); |
| 728 |
goto cleanup; |
| 729 |
} |
| 730 |
|
| 731 |
if (status == SEC_E_INCOMPLETE_MESSAGE) { |
| 732 |
AWS_LOGF_TRACE( |
| 733 |
AWS_LS_IO_TLS, "id=%p: Last processed buffer was incomplete, waiting on more data.", (void *)handler); |
| 734 |
sc_handler->estimated_incomplete_size = input_buffers[1].cbBuffer; |
| 735 |
aws_error = AWS_IO_READ_WOULD_BLOCK; |
| 736 |
goto cleanup; |
| 737 |
}; |
| 738 |
|
| 739 |
|
| 740 |
if (status == SEC_I_CONTINUE_NEEDED || status == SEC_E_OK) { |
| 741 |
for (size_t i = 0; i < output_buffers_desc.cBuffers; ++i) { |
| 742 |
SecBuffer *buf_ptr = &output_buffers[i]; |
| 743 |
|
| 744 |
if (buf_ptr->BufferType == SECBUFFER_TOKEN && buf_ptr->cbBuffer) { |
| 745 |
struct aws_io_message *outgoing_message = aws_channel_acquire_message_from_pool( |
| 746 |
sc_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, buf_ptr->cbBuffer); |
| 747 |
|
| 748 |
AWS_FATAL_ASSERT(outgoing_message->message_data.capacity >= buf_ptr->cbBuffer); |
| 749 |
memcpy(outgoing_message->message_data.buffer, buf_ptr->pvBuffer, buf_ptr->cbBuffer); |
| 750 |
outgoing_message->message_data.len = buf_ptr->cbBuffer; |
| 751 |
|
| 752 |
if (aws_channel_slot_send_message(sc_handler->slot, outgoing_message, AWS_CHANNEL_DIR_WRITE)) { |
| 753 |
aws_error = aws_last_error(); |
| 754 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 755 |
goto cleanup; |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
if (input_buffers[1].BufferType == SECBUFFER_EXTRA && input_buffers[1].cbBuffer > 0) { |
| 761 |
AWS_LOGF_TRACE( |
| 762 |
AWS_LS_IO_TLS, |
| 763 |
"id=%p: Extra data recieved. Extra size is %lu", |
| 764 |
(void *)handler, |
| 765 |
input_buffers[1].cbBuffer); |
| 766 |
sc_handler->read_extra = input_buffers[1].cbBuffer; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
if (status == SEC_E_OK) { |
| 771 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: handshake completed", (void *)handler); |
| 772 |
|
| 773 |
if (sc_handler->custom_ca_store) { |
| 774 |
AWS_LOGF_TRACE( |
| 775 |
AWS_LS_IO_TLS, |
| 776 |
"id=%p: Custom CA was configured, evaluating trust before completing connection", |
| 777 |
(void *)handler); |
| 778 |
|
| 779 |
if (s_manually_verify_peer_cert(handler)) { |
| 780 |
aws_error = AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE; |
| 781 |
goto cleanup; |
| 782 |
} |
| 783 |
} |
| 784 |
sc_handler->negotiation_finished = true; |
| 785 |
|
| 786 |
|
| 787 |
s_message_overhead(handler); |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
#ifdef SECBUFFER_APPLICATION_PROTOCOLS |
| 793 |
if (sc_handler->alpn_list && s_tls_is_alpn_available()) { |
| 794 |
SecPkgContext_ApplicationProtocol alpn_result; |
| 795 |
status = QueryContextAttributes(&sc_handler->sec_handle, SECPKG_ATTR_APPLICATION_PROTOCOL, &alpn_result); |
| 796 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: ALPN is configured. Checking for negotiated protocol", handler); |
| 797 |
|
| 798 |
if (status == SEC_E_OK) { |
| 799 |
if (alpn_result.ProtoNegoStatus == SecApplicationProtocolNegotiationStatus_Success) { |
| 800 |
aws_byte_buf_init(&sc_handler->protocol, handler->alloc, alpn_result.ProtocolIdSize + 1); |
| 801 |
memset(sc_handler->protocol.buffer, 0, alpn_result.ProtocolIdSize + 1); |
| 802 |
memcpy(sc_handler->protocol.buffer, alpn_result.ProtocolId, alpn_result.ProtocolIdSize); |
| 803 |
sc_handler->protocol.len = alpn_result.ProtocolIdSize; |
| 804 |
AWS_LOGF_DEBUG( |
| 805 |
AWS_LS_IO_TLS, "id=%p: negotiated protocol %s", handler, (char *)sc_handler->protocol.buffer); |
| 806 |
} else { |
| 807 |
|
| 808 |
AWS_LOGF_INFO( |
| 809 |
AWS_LS_IO_TLS, "id=%p: ALPN - no protocol was negotiated during TLS handshake", handler); |
| 810 |
} |
| 811 |
} else { |
| 812 |
AWS_LOGF_WARN( |
| 813 |
AWS_LS_IO_TLS, |
| 814 |
"id=%p: Error retrieving negotiated protocol. SECURITY_STATUS is %d", |
| 815 |
handler, |
| 816 |
(int)status); |
| 817 |
aws_error = s_determine_sspi_error(status); |
| 818 |
goto cleanup; |
| 819 |
} |
| 820 |
} |
| 821 |
#endif |
| 822 |
sc_handler->s_connection_state_fn = s_do_application_data_decrypt; |
| 823 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: TLS handshake completed successfully.", (void *)handler); |
| 824 |
s_on_negotiation_success(handler); |
| 825 |
} |
| 826 |
|
| 827 |
cleanup: |
| 828 |
for (size_t i = 0; i < output_buffers_desc.cBuffers; ++i) { |
| 829 |
FreeContextBuffer(output_buffers[i].pvBuffer); |
| 830 |
} |
| 831 |
|
| 832 |
if (aws_error != 0) { |
| 833 |
if (aws_error != AWS_IO_READ_WOULD_BLOCK) { |
| 834 |
s_invoke_negotiation_error(handler, aws_error); |
| 835 |
} |
| 836 |
return aws_raise_error(aws_error); |
| 837 |
} |
| 838 |
return AWS_OP_SUCCESS; |
| 839 |
} |
| 840 |
|
| 841 |
static int s_do_client_side_negotiation_step_2(struct aws_channel_handler *handler); |
| 842 |
|
| 843 |
|
| 844 |
static int s_do_client_side_negotiation_step_1(struct aws_channel_handler *handler) { |
| 845 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 846 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: client starting negotiation", (void *)handler); |
| 847 |
|
| 848 |
aws_on_drive_tls_negotiation(&sc_handler->shared_state); |
| 849 |
|
| 850 |
|
| 851 |
int aws_error = 0; |
| 852 |
|
| 853 |
unsigned char alpn_buffer_data[128] = {0}; |
| 854 |
SecBuffer input_buf = { |
| 855 |
.pvBuffer = NULL, |
| 856 |
.cbBuffer = 0, |
| 857 |
.BufferType = SECBUFFER_EMPTY, |
| 858 |
}; |
| 859 |
|
| 860 |
SecBufferDesc input_buf_desc = { |
| 861 |
.ulVersion = SECBUFFER_VERSION, |
| 862 |
.cBuffers = 1, |
| 863 |
.pBuffers = &input_buf, |
| 864 |
}; |
| 865 |
|
| 866 |
SecBuffer output_buffer = { |
| 867 |
.pvBuffer = NULL, |
| 868 |
.cbBuffer = 0, |
| 869 |
.BufferType = SECBUFFER_EMPTY, |
| 870 |
}; |
| 871 |
|
| 872 |
SecBufferDesc output_buffer_desc = { |
| 873 |
.ulVersion = SECBUFFER_VERSION, |
| 874 |
.cBuffers = 1, |
| 875 |
.pBuffers = &output_buffer, |
| 876 |
}; |
| 877 |
|
| 878 |
SecBufferDesc *alpn_sspi_data = NULL; |
| 879 |
|
| 880 |
|
| 881 |
#ifdef SECBUFFER_APPLICATION_PROTOCOLS |
| 882 |
if (sc_handler->alpn_list && s_tls_is_alpn_available()) { |
| 883 |
AWS_LOGF_DEBUG( |
| 884 |
AWS_LS_IO_TLS, "id=%p: Setting ALPN data as %s", handler, aws_string_c_str(sc_handler->alpn_list)); |
| 885 |
size_t extension_length = 0; |
| 886 |
if (s_fillin_alpn_data(handler, alpn_buffer_data, sizeof(alpn_buffer_data), &extension_length)) { |
| 887 |
aws_error = aws_last_error(); |
| 888 |
goto cleanup; |
| 889 |
} |
| 890 |
|
| 891 |
input_buf.pvBuffer = alpn_buffer_data, input_buf.cbBuffer = (unsigned long)extension_length, |
| 892 |
input_buf.BufferType = SECBUFFER_APPLICATION_PROTOCOLS; |
| 893 |
|
| 894 |
alpn_sspi_data = &input_buf_desc; |
| 895 |
} |
| 896 |
#endif |
| 897 |
|
| 898 |
sc_handler->ctx_req = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONFIDENTIALITY | |
| 899 |
ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM; |
| 900 |
|
| 901 |
char server_name_cstr[256]; |
| 902 |
AWS_ZERO_ARRAY(server_name_cstr); |
| 903 |
AWS_ASSERT(sc_handler->server_name.len < 256); |
| 904 |
memcpy(server_name_cstr, sc_handler->server_name.buffer, sc_handler->server_name.len); |
| 905 |
|
| 906 |
SECURITY_STATUS status = InitializeSecurityContextA( |
| 907 |
&sc_handler->creds, |
| 908 |
NULL, |
| 909 |
(SEC_CHAR *)server_name_cstr, |
| 910 |
sc_handler->ctx_req, |
| 911 |
0, |
| 912 |
0, |
| 913 |
alpn_sspi_data, |
| 914 |
0, |
| 915 |
&sc_handler->sec_handle, |
| 916 |
&output_buffer_desc, |
| 917 |
&sc_handler->ctx_ret_flags, |
| 918 |
&sc_handler->sspi_timestamp); |
| 919 |
|
| 920 |
if (status != SEC_I_CONTINUE_NEEDED) { |
| 921 |
AWS_LOGF_ERROR( |
| 922 |
AWS_LS_IO_TLS, |
| 923 |
"id=%p: Error sending client/receiving server handshake data. SECURITY_STATUS is %d", |
| 924 |
(void *)handler, |
| 925 |
(int)status); |
| 926 |
aws_error = s_determine_sspi_error(status); |
| 927 |
goto cleanup; |
| 928 |
} |
| 929 |
|
| 930 |
size_t data_to_write_len = output_buffer.cbBuffer; |
| 931 |
AWS_LOGF_TRACE( |
| 932 |
AWS_LS_IO_TLS, "id=%p: Sending client handshake data of size %zu", (void *)handler, data_to_write_len); |
| 933 |
|
| 934 |
struct aws_io_message *outgoing_message = aws_channel_acquire_message_from_pool( |
| 935 |
sc_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, data_to_write_len); |
| 936 |
|
| 937 |
AWS_FATAL_ASSERT(outgoing_message->message_data.capacity >= data_to_write_len); |
| 938 |
memcpy(outgoing_message->message_data.buffer, output_buffer.pvBuffer, output_buffer.cbBuffer); |
| 939 |
outgoing_message->message_data.len = output_buffer.cbBuffer; |
| 940 |
|
| 941 |
if (aws_channel_slot_send_message(sc_handler->slot, outgoing_message, AWS_CHANNEL_DIR_WRITE)) { |
| 942 |
aws_error = aws_last_error(); |
| 943 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 944 |
goto cleanup; |
| 945 |
} |
| 946 |
|
| 947 |
sc_handler->s_connection_state_fn = s_do_client_side_negotiation_step_2; |
| 948 |
|
| 949 |
cleanup: |
| 950 |
FreeContextBuffer(output_buffer.pvBuffer); |
| 951 |
if (aws_error != 0) { |
| 952 |
s_invoke_negotiation_error(handler, aws_error); |
| 953 |
return aws_raise_error(aws_error); |
| 954 |
} |
| 955 |
return AWS_OP_SUCCESS; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
static int s_do_client_side_negotiation_step_2(struct aws_channel_handler *handler) { |
| 960 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 961 |
AWS_LOGF_TRACE( |
| 962 |
AWS_LS_IO_TLS, |
| 963 |
"id=%p: running step 2 of client-side negotiation (cipher change, key exchange etc...)", |
| 964 |
(void *)handler); |
| 965 |
|
| 966 |
|
| 967 |
int aws_error = 0; |
| 968 |
|
| 969 |
SecBuffer input_buffers[] = { |
| 970 |
[0] = |
| 971 |
{ |
| 972 |
.pvBuffer = sc_handler->buffered_read_in_data_buf.buffer, |
| 973 |
.cbBuffer = (unsigned long)sc_handler->buffered_read_in_data_buf.len, |
| 974 |
.BufferType = SECBUFFER_TOKEN, |
| 975 |
}, |
| 976 |
[1] = |
| 977 |
{ |
| 978 |
.pvBuffer = NULL, |
| 979 |
.cbBuffer = 0, |
| 980 |
.BufferType = SECBUFFER_EMPTY, |
| 981 |
}, |
| 982 |
}; |
| 983 |
|
| 984 |
SecBufferDesc input_buffers_desc = { |
| 985 |
.ulVersion = SECBUFFER_VERSION, |
| 986 |
.cBuffers = 2, |
| 987 |
.pBuffers = input_buffers, |
| 988 |
}; |
| 989 |
|
| 990 |
SecBuffer output_buffers[3]; |
| 991 |
AWS_ZERO_ARRAY(output_buffers); |
| 992 |
output_buffers[0].BufferType = SECBUFFER_TOKEN; |
| 993 |
output_buffers[1].BufferType = SECBUFFER_ALERT; |
| 994 |
|
| 995 |
SecBufferDesc output_buffers_desc = { |
| 996 |
.ulVersion = SECBUFFER_VERSION, |
| 997 |
.cBuffers = 3, |
| 998 |
.pBuffers = output_buffers, |
| 999 |
}; |
| 1000 |
|
| 1001 |
SECURITY_STATUS status = SEC_E_OK; |
| 1002 |
|
| 1003 |
sc_handler->read_extra = 0; |
| 1004 |
sc_handler->estimated_incomplete_size = 0; |
| 1005 |
|
| 1006 |
char server_name_cstr[256]; |
| 1007 |
AWS_ZERO_ARRAY(server_name_cstr); |
| 1008 |
AWS_FATAL_ASSERT(sc_handler->server_name.len < sizeof(server_name_cstr)); |
| 1009 |
memcpy(server_name_cstr, sc_handler->server_name.buffer, sc_handler->server_name.len); |
| 1010 |
|
| 1011 |
status = InitializeSecurityContextA( |
| 1012 |
&sc_handler->creds, |
| 1013 |
&sc_handler->sec_handle, |
| 1014 |
(SEC_CHAR *)server_name_cstr, |
| 1015 |
sc_handler->ctx_req, |
| 1016 |
0, |
| 1017 |
0, |
| 1018 |
&input_buffers_desc, |
| 1019 |
0, |
| 1020 |
NULL, |
| 1021 |
&output_buffers_desc, |
| 1022 |
&sc_handler->ctx_ret_flags, |
| 1023 |
&sc_handler->sspi_timestamp); |
| 1024 |
if (status != SEC_E_INCOMPLETE_MESSAGE && status != SEC_I_CONTINUE_NEEDED && status != SEC_E_OK) { |
| 1025 |
AWS_LOGF_ERROR( |
| 1026 |
AWS_LS_IO_TLS, "id=%p: Error during negotiation. SECURITY_STATUS is %d", (void *)handler, (int)status); |
| 1027 |
aws_error = s_determine_sspi_error(status); |
| 1028 |
goto cleanup; |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (status == SEC_E_INCOMPLETE_MESSAGE) { |
| 1032 |
sc_handler->estimated_incomplete_size = input_buffers[1].cbBuffer; |
| 1033 |
AWS_LOGF_TRACE( |
| 1034 |
AWS_LS_IO_TLS, |
| 1035 |
"id=%p: Incomplete buffer recieved. Incomplete size is %zu. Waiting for more data.", |
| 1036 |
(void *)handler, |
| 1037 |
sc_handler->estimated_incomplete_size); |
| 1038 |
aws_error = AWS_IO_READ_WOULD_BLOCK; |
| 1039 |
goto cleanup; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (status == SEC_I_CONTINUE_NEEDED || status == SEC_E_OK) { |
| 1043 |
for (size_t i = 0; i < output_buffers_desc.cBuffers; ++i) { |
| 1044 |
SecBuffer *buf_ptr = &output_buffers[i]; |
| 1045 |
|
| 1046 |
if (buf_ptr->BufferType == SECBUFFER_TOKEN && buf_ptr->cbBuffer) { |
| 1047 |
struct aws_io_message *outgoing_message = aws_channel_acquire_message_from_pool( |
| 1048 |
sc_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, buf_ptr->cbBuffer); |
| 1049 |
|
| 1050 |
AWS_FATAL_ASSERT(outgoing_message->message_data.capacity >= buf_ptr->cbBuffer); |
| 1051 |
memcpy(outgoing_message->message_data.buffer, buf_ptr->pvBuffer, buf_ptr->cbBuffer); |
| 1052 |
outgoing_message->message_data.len = buf_ptr->cbBuffer; |
| 1053 |
|
| 1054 |
if (aws_channel_slot_send_message(sc_handler->slot, outgoing_message, AWS_CHANNEL_DIR_WRITE)) { |
| 1055 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 1056 |
aws_error = aws_last_error(); |
| 1057 |
goto cleanup; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (input_buffers[1].BufferType == SECBUFFER_EXTRA && input_buffers[1].cbBuffer > 0) { |
| 1063 |
AWS_LOGF_TRACE( |
| 1064 |
AWS_LS_IO_TLS, |
| 1065 |
"id=%p: Extra data recieved. Extra data size is %lu.", |
| 1066 |
(void *)handler, |
| 1067 |
input_buffers[1].cbBuffer); |
| 1068 |
sc_handler->read_extra = input_buffers[1].cbBuffer; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (status == SEC_E_OK) { |
| 1073 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: handshake completed", handler); |
| 1074 |
|
| 1075 |
if (sc_handler->custom_ca_store) { |
| 1076 |
AWS_LOGF_TRACE( |
| 1077 |
AWS_LS_IO_TLS, |
| 1078 |
"id=%p: Custom CA was configured, evaluating trust before completing connection", |
| 1079 |
(void *)handler); |
| 1080 |
if (s_manually_verify_peer_cert(handler)) { |
| 1081 |
aws_error = AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE; |
| 1082 |
goto cleanup; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
sc_handler->negotiation_finished = true; |
| 1086 |
|
| 1087 |
s_message_overhead(handler); |
| 1088 |
|
| 1089 |
#ifdef SECBUFFER_APPLICATION_PROTOCOLS |
| 1090 |
if (sc_handler->alpn_list && s_tls_is_alpn_available()) { |
| 1091 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Retrieving negotiated protocol.", handler); |
| 1092 |
SecPkgContext_ApplicationProtocol alpn_result; |
| 1093 |
SECURITY_STATUS alpn_status = |
| 1094 |
QueryContextAttributes(&sc_handler->sec_handle, SECPKG_ATTR_APPLICATION_PROTOCOL, &alpn_result); |
| 1095 |
|
| 1096 |
if (alpn_status == SEC_E_OK) { |
| 1097 |
if (alpn_result.ProtoNegoStatus == SecApplicationProtocolNegotiationStatus_Success) { |
| 1098 |
aws_byte_buf_init(&sc_handler->protocol, handler->alloc, alpn_result.ProtocolIdSize + 1); |
| 1099 |
memset(sc_handler->protocol.buffer, 0, alpn_result.ProtocolIdSize + 1); |
| 1100 |
memcpy(sc_handler->protocol.buffer, alpn_result.ProtocolId, alpn_result.ProtocolIdSize); |
| 1101 |
sc_handler->protocol.len = alpn_result.ProtocolIdSize; |
| 1102 |
AWS_LOGF_DEBUG( |
| 1103 |
AWS_LS_IO_TLS, "id=%p: Negotiated protocol %s", handler, (char *)sc_handler->protocol.buffer); |
| 1104 |
} else { |
| 1105 |
|
| 1106 |
AWS_LOGF_INFO( |
| 1107 |
AWS_LS_IO_TLS, "id=%p: ALPN - no negotiated protocol was returned by remote endpoint", handler); |
| 1108 |
} |
| 1109 |
} else { |
| 1110 |
AWS_LOGF_WARN( |
| 1111 |
AWS_LS_IO_TLS, |
| 1112 |
"id=%p: Error retrieving negotiated protocol. SECURITY_STATUS is %d", |
| 1113 |
handler, |
| 1114 |
(int)alpn_status); |
| 1115 |
aws_error = s_determine_sspi_error(alpn_status); |
| 1116 |
goto cleanup; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
#endif |
| 1120 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: TLS handshake completed successfully.", (void *)handler); |
| 1121 |
sc_handler->s_connection_state_fn = s_do_application_data_decrypt; |
| 1122 |
s_on_negotiation_success(handler); |
| 1123 |
} |
| 1124 |
|
| 1125 |
cleanup: |
| 1126 |
for (size_t i = 0; i < output_buffers_desc.cBuffers; ++i) { |
| 1127 |
FreeContextBuffer(output_buffers[i].pvBuffer); |
| 1128 |
} |
| 1129 |
|
| 1130 |
if (aws_error != 0) { |
| 1131 |
if (aws_error != AWS_IO_READ_WOULD_BLOCK) { |
| 1132 |
s_invoke_negotiation_error(handler, aws_error); |
| 1133 |
} |
| 1134 |
return aws_raise_error(aws_error); |
| 1135 |
} |
| 1136 |
return AWS_OP_SUCCESS; |
| 1137 |
} |
| 1138 |
|
| 1139 |
static int s_do_application_data_decrypt(struct aws_channel_handler *handler) { |
| 1140 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1141 |
|
| 1142 |
|
| 1143 |
int error = AWS_OP_ERR; |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
do { |
| 1148 |
error = AWS_OP_ERR; |
| 1149 |
|
| 1150 |
SecBuffer input_buffers[4]; |
| 1151 |
AWS_ZERO_ARRAY(input_buffers); |
| 1152 |
|
| 1153 |
size_t read_len = sc_handler->read_extra ? sc_handler->read_extra : sc_handler->buffered_read_in_data_buf.len; |
| 1154 |
size_t offset = sc_handler->read_extra ? sc_handler->buffered_read_in_data_buf.len - sc_handler->read_extra : 0; |
| 1155 |
sc_handler->read_extra = 0; |
| 1156 |
|
| 1157 |
input_buffers[0] = (SecBuffer){ |
| 1158 |
.cbBuffer = (unsigned long)(read_len), |
| 1159 |
.pvBuffer = sc_handler->buffered_read_in_data_buf.buffer + offset, |
| 1160 |
.BufferType = SECBUFFER_DATA, |
| 1161 |
}; |
| 1162 |
|
| 1163 |
SecBufferDesc buffer_desc = { |
| 1164 |
.ulVersion = SECBUFFER_VERSION, |
| 1165 |
.cBuffers = 4, |
| 1166 |
.pBuffers = input_buffers, |
| 1167 |
}; |
| 1168 |
|
| 1169 |
SECURITY_STATUS status = DecryptMessage(&sc_handler->sec_handle, &buffer_desc, 0, NULL); |
| 1170 |
|
| 1171 |
if (status == SEC_E_OK) { |
| 1172 |
error = AWS_OP_SUCCESS; |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
if (input_buffers[1].BufferType == SECBUFFER_DATA) { |
| 1178 |
size_t decrypted_length = input_buffers[1].cbBuffer; |
| 1179 |
AWS_LOGF_TRACE( |
| 1180 |
AWS_LS_IO_TLS, "id=%p: Decrypted message with length %zu.", (void *)handler, decrypted_length); |
| 1181 |
|
| 1182 |
struct aws_byte_cursor to_append = |
| 1183 |
aws_byte_cursor_from_array(input_buffers[1].pvBuffer, decrypted_length); |
| 1184 |
int append_failed = aws_byte_buf_append(&sc_handler->buffered_read_out_data_buf, &to_append); |
| 1185 |
AWS_FATAL_ASSERT(!append_failed); |
| 1186 |
|
| 1187 |
if (input_buffers[3].BufferType == SECBUFFER_EXTRA && input_buffers[3].cbBuffer > 0) { |
| 1188 |
sc_handler->read_extra = input_buffers[3].cbBuffer; |
| 1189 |
AWS_LOGF_TRACE( |
| 1190 |
AWS_LS_IO_TLS, |
| 1191 |
"id=%p: Extra (incomplete) message received with length %zu.", |
| 1192 |
(void *)handler, |
| 1193 |
sc_handler->read_extra); |
| 1194 |
} else { |
| 1195 |
error = AWS_OP_SUCCESS; |
| 1196 |
|
| 1197 |
sc_handler->buffered_read_in_data_buf.len = 0; |
| 1198 |
AWS_LOGF_TRACE( |
| 1199 |
AWS_LS_IO_TLS, |
| 1200 |
"id=%p: Decrypt ended exactly on the end of the record, resetting buffer.", |
| 1201 |
(void *)handler); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
else if (status == SEC_I_RENEGOTIATE) { |
| 1208 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Renegotiation received", (void *)handler); |
| 1209 |
|
| 1210 |
|
| 1211 |
if (input_buffers[1].BufferType == SECBUFFER_DATA) { |
| 1212 |
size_t decrypted_length = input_buffers[1].cbBuffer; |
| 1213 |
AWS_LOGF_TRACE( |
| 1214 |
AWS_LS_IO_TLS, "id=%p: Decrypted message with length %zu.", (void *)handler, decrypted_length); |
| 1215 |
|
| 1216 |
struct aws_byte_cursor to_append = |
| 1217 |
aws_byte_cursor_from_array(input_buffers[1].pvBuffer, decrypted_length); |
| 1218 |
int append_failed = aws_byte_buf_append(&sc_handler->buffered_read_out_data_buf, &to_append); |
| 1219 |
AWS_FATAL_ASSERT(!append_failed); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
size_t extra_data_offset = offset; |
| 1244 |
if (input_buffers[3].BufferType == SECBUFFER_EXTRA && input_buffers[3].cbBuffer > 0 && |
| 1245 |
input_buffers[3].cbBuffer < read_len) { |
| 1246 |
extra_data_offset = offset + read_len - input_buffers[3].cbBuffer; |
| 1247 |
} |
| 1248 |
|
| 1249 |
SecBuffer input_buffers2[] = { |
| 1250 |
[0] = |
| 1251 |
{ |
| 1252 |
.pvBuffer = sc_handler->buffered_read_in_data_buf.buffer + extra_data_offset, |
| 1253 |
.cbBuffer = (unsigned long)(sc_handler->buffered_read_in_data_buf.len - extra_data_offset), |
| 1254 |
.BufferType = SECBUFFER_TOKEN, |
| 1255 |
}, |
| 1256 |
[1] = |
| 1257 |
{ |
| 1258 |
.pvBuffer = NULL, |
| 1259 |
.cbBuffer = 0, |
| 1260 |
.BufferType = SECBUFFER_EMPTY, |
| 1261 |
}, |
| 1262 |
}; |
| 1263 |
|
| 1264 |
SecBufferDesc input_bufs_desc = { |
| 1265 |
.ulVersion = SECBUFFER_VERSION, |
| 1266 |
.cBuffers = 2, |
| 1267 |
.pBuffers = input_buffers2, |
| 1268 |
}; |
| 1269 |
|
| 1270 |
SecBuffer output_buffers[3]; |
| 1271 |
AWS_ZERO_ARRAY(output_buffers); |
| 1272 |
output_buffers[0].BufferType = SECBUFFER_TOKEN; |
| 1273 |
output_buffers[1].BufferType = SECBUFFER_ALERT; |
| 1274 |
output_buffers[2].BufferType = SECBUFFER_EMPTY; |
| 1275 |
|
| 1276 |
SecBufferDesc output_buffers_desc = { |
| 1277 |
.ulVersion = SECBUFFER_VERSION, |
| 1278 |
.cBuffers = 3, |
| 1279 |
.pBuffers = output_buffers, |
| 1280 |
}; |
| 1281 |
char server_name_cstr[256]; |
| 1282 |
AWS_ZERO_ARRAY(server_name_cstr); |
| 1283 |
AWS_FATAL_ASSERT(sc_handler->server_name.len < sizeof(server_name_cstr)); |
| 1284 |
memcpy(server_name_cstr, sc_handler->server_name.buffer, sc_handler->server_name.len); |
| 1285 |
|
| 1286 |
status = InitializeSecurityContextA( |
| 1287 |
&sc_handler->creds, |
| 1288 |
&sc_handler->sec_handle, |
| 1289 |
(SEC_CHAR *)server_name_cstr, |
| 1290 |
sc_handler->ctx_req, |
| 1291 |
0, |
| 1292 |
0, |
| 1293 |
&input_bufs_desc, |
| 1294 |
0, |
| 1295 |
NULL, |
| 1296 |
&output_buffers_desc, |
| 1297 |
&sc_handler->ctx_ret_flags, |
| 1298 |
NULL); |
| 1299 |
if (status == SEC_E_OK) { |
| 1300 |
error = AWS_OP_SUCCESS; |
| 1301 |
|
| 1302 |
|
| 1303 |
if (input_buffers2[1].BufferType == SECBUFFER_EXTRA) { |
| 1304 |
sc_handler->read_extra = input_buffers2[1].cbBuffer; |
| 1305 |
} |
| 1306 |
AWS_LOGF_TRACE( |
| 1307 |
AWS_LS_IO_TLS, |
| 1308 |
"id=%p: Successfully renegotiated; got %zu bytes of extra data to decrypt", |
| 1309 |
(void *)handler, |
| 1310 |
sc_handler->read_extra); |
| 1311 |
|
| 1312 |
} else { |
| 1313 |
AWS_LOGF_ERROR( |
| 1314 |
AWS_LS_IO_TLS, |
| 1315 |
"id=%p: Error InitializeSecurityContext after renegotiation. status %d", |
| 1316 |
(void *)handler, |
| 1317 |
(int)status); |
| 1318 |
error = AWS_OP_ERR; |
| 1319 |
break; |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
else if (status == SEC_E_INCOMPLETE_MESSAGE) { |
| 1325 |
sc_handler->estimated_incomplete_size = input_buffers[1].cbBuffer; |
| 1326 |
AWS_LOGF_TRACE( |
| 1327 |
AWS_LS_IO_TLS, |
| 1328 |
"id=%p: (incomplete) message received. Expecting remaining portion of size %zu.", |
| 1329 |
(void *)handler, |
| 1330 |
sc_handler->estimated_incomplete_size); |
| 1331 |
memmove( |
| 1332 |
sc_handler->buffered_read_in_data_buf.buffer, |
| 1333 |
sc_handler->buffered_read_in_data_buf.buffer + offset, |
| 1334 |
read_len); |
| 1335 |
sc_handler->buffered_read_in_data_buf.len = read_len; |
| 1336 |
aws_raise_error(AWS_IO_READ_WOULD_BLOCK); |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
else if (status == SEC_I_CONTEXT_EXPIRED) { |
| 1341 |
AWS_LOGF_TRACE( |
| 1342 |
AWS_LS_IO_TLS, |
| 1343 |
"id=%p: Alert received. Message sender has shut down the connection. SECURITY_STATUS is %d.", |
| 1344 |
(void *)handler, |
| 1345 |
(int)status); |
| 1346 |
|
| 1347 |
struct aws_channel_slot *slot = handler->slot; |
| 1348 |
aws_channel_shutdown(slot->channel, AWS_OP_SUCCESS); |
| 1349 |
error = AWS_OP_SUCCESS; |
| 1350 |
} else { |
| 1351 |
AWS_LOGF_ERROR( |
| 1352 |
AWS_LS_IO_TLS, "id=%p: Error decrypting message. SECURITY_STATUS is %d.", (void *)handler, (int)status); |
| 1353 |
aws_raise_error(AWS_IO_TLS_ERROR_READ_FAILURE); |
| 1354 |
} |
| 1355 |
} while (sc_handler->read_extra); |
| 1356 |
|
| 1357 |
return error; |
| 1358 |
} |
| 1359 |
|
| 1360 |
static int s_process_pending_output_messages(struct aws_channel_handler *handler) { |
| 1361 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1362 |
if (sc_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 1363 |
return AWS_OP_SUCCESS; |
| 1364 |
} |
| 1365 |
|
| 1366 |
size_t downstream_window = SIZE_MAX; |
| 1367 |
int error_code = 0; |
| 1368 |
|
| 1369 |
if (sc_handler->slot->adj_right) { |
| 1370 |
downstream_window = aws_channel_slot_downstream_read_window(sc_handler->slot); |
| 1371 |
} |
| 1372 |
|
| 1373 |
AWS_LOGF_TRACE( |
| 1374 |
AWS_LS_IO_TLS, |
| 1375 |
"id=%p: Processing incomming messages. Downstream window is %zu", |
| 1376 |
(void *)handler, |
| 1377 |
downstream_window); |
| 1378 |
while (sc_handler->buffered_read_out_data_buf.len && downstream_window) { |
| 1379 |
size_t requested_message_size = sc_handler->buffered_read_out_data_buf.len > downstream_window |
| 1380 |
? downstream_window |
| 1381 |
: sc_handler->buffered_read_out_data_buf.len; |
| 1382 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Requested message size is %zu", (void *)handler, requested_message_size); |
| 1383 |
|
| 1384 |
if (sc_handler->slot->adj_right) { |
| 1385 |
struct aws_io_message *read_out_msg = aws_channel_acquire_message_from_pool( |
| 1386 |
sc_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, requested_message_size); |
| 1387 |
|
| 1388 |
size_t copy_size = read_out_msg->message_data.capacity < requested_message_size |
| 1389 |
? read_out_msg->message_data.capacity |
| 1390 |
: requested_message_size; |
| 1391 |
|
| 1392 |
memcpy(read_out_msg->message_data.buffer, sc_handler->buffered_read_out_data_buf.buffer, copy_size); |
| 1393 |
read_out_msg->message_data.len = copy_size; |
| 1394 |
|
| 1395 |
memmove( |
| 1396 |
sc_handler->buffered_read_out_data_buf.buffer, |
| 1397 |
sc_handler->buffered_read_out_data_buf.buffer + copy_size, |
| 1398 |
sc_handler->buffered_read_out_data_buf.len - copy_size); |
| 1399 |
sc_handler->buffered_read_out_data_buf.len -= copy_size; |
| 1400 |
|
| 1401 |
if (sc_handler->on_data_read) { |
| 1402 |
sc_handler->on_data_read(handler, sc_handler->slot, &read_out_msg->message_data, sc_handler->user_data); |
| 1403 |
} |
| 1404 |
if (aws_channel_slot_send_message(sc_handler->slot, read_out_msg, AWS_CHANNEL_DIR_READ)) { |
| 1405 |
aws_mem_release(read_out_msg->allocator, read_out_msg); |
| 1406 |
error_code = aws_last_error(); |
| 1407 |
goto done; |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (sc_handler->slot->adj_right) { |
| 1411 |
downstream_window = aws_channel_slot_downstream_read_window(sc_handler->slot); |
| 1412 |
} |
| 1413 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Downstream window is %zu", (void *)handler, downstream_window); |
| 1414 |
} else { |
| 1415 |
if (sc_handler->on_data_read) { |
| 1416 |
sc_handler->on_data_read( |
| 1417 |
handler, sc_handler->slot, &sc_handler->buffered_read_out_data_buf, sc_handler->user_data); |
| 1418 |
} |
| 1419 |
sc_handler->buffered_read_out_data_buf.len = 0; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (sc_handler->buffered_read_out_data_buf.len > 0) { |
| 1424 |
|
| 1425 |
return AWS_OP_SUCCESS; |
| 1426 |
} |
| 1427 |
|
| 1428 |
done: |
| 1429 |
if (sc_handler->read_state == AWS_TLS_HANDLER_READ_SHUTTING_DOWN) { |
| 1430 |
sc_handler->read_state = AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE; |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
int shutdown_error_code = sc_handler->shutdown_error_code ? sc_handler->shutdown_error_code : error_code; |
| 1435 |
|
| 1436 |
aws_channel_slot_on_handler_shutdown_complete( |
| 1437 |
sc_handler->slot, AWS_CHANNEL_DIR_READ, shutdown_error_code, false); |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
return error_code ? aws_raise_error(error_code) : AWS_OP_SUCCESS; |
| 1442 |
} |
| 1443 |
|
| 1444 |
static void s_process_pending_output_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 1445 |
(void)task; |
| 1446 |
struct aws_channel_handler *handler = arg; |
| 1447 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1448 |
sc_handler->read_task_pending = false; |
| 1449 |
|
| 1450 |
aws_channel_task_init(task, NULL, NULL, "secure_channel_handler_process_pending_output"); |
| 1451 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 1452 |
if (s_process_pending_output_messages(handler)) { |
| 1453 |
aws_channel_shutdown(sc_handler->slot->channel, aws_last_error()); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
static int s_process_read_message( |
| 1459 |
struct aws_channel_handler *handler, |
| 1460 |
struct aws_channel_slot *slot, |
| 1461 |
struct aws_io_message *message) { |
| 1462 |
|
| 1463 |
AWS_ASSERT(message); |
| 1464 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1465 |
|
| 1466 |
if (sc_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 1467 |
aws_mem_release(message->allocator, message); |
| 1468 |
return AWS_OP_SUCCESS; |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
AWS_LOGF_TRACE( |
| 1473 |
AWS_LS_IO_TLS, "id=%p: processing incoming message of size %zu", (void *)handler, message->message_data.len); |
| 1474 |
|
| 1475 |
struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message->message_data); |
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
int err = AWS_OP_SUCCESS; |
| 1484 |
while (!err && message_cursor.len) { |
| 1485 |
|
| 1486 |
aws_byte_buf_write_to_capacity(&sc_handler->buffered_read_in_data_buf, &message_cursor); |
| 1487 |
|
| 1488 |
|
| 1489 |
bool record_is_incomplete = false; |
| 1490 |
err = sc_handler->s_connection_state_fn(handler); |
| 1491 |
if (err) { |
| 1492 |
|
| 1493 |
if (aws_last_error() == AWS_IO_READ_WOULD_BLOCK) { |
| 1494 |
err = AWS_OP_SUCCESS; |
| 1495 |
record_is_incomplete = true; |
| 1496 |
} else { |
| 1497 |
break; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
if (sc_handler->buffered_read_out_data_buf.len) { |
| 1503 |
err = s_process_pending_output_messages(handler); |
| 1504 |
if (err) { |
| 1505 |
break; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
if (record_is_incomplete) { |
| 1510 |
|
| 1511 |
if (sc_handler->buffered_read_in_data_buf.len == sc_handler->buffered_read_in_data_buf.capacity) { |
| 1512 |
err = aws_raise_error(AWS_IO_TLS_ERROR_WRITE_FAILURE); |
| 1513 |
break; |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
size_t downstream_window = |
| 1519 |
sc_handler->slot->adj_right ? aws_channel_slot_downstream_read_window(sc_handler->slot) : SIZE_MAX; |
| 1520 |
if (downstream_window > 0 && slot->window_size == 0) { |
| 1521 |
err = aws_channel_slot_increment_read_window(slot, sc_handler->estimated_incomplete_size); |
| 1522 |
if (err) { |
| 1523 |
break; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
} else { |
| 1527 |
|
| 1528 |
if (sc_handler->read_extra) { |
| 1529 |
size_t move_pos = sc_handler->buffered_read_in_data_buf.len - sc_handler->read_extra; |
| 1530 |
memmove( |
| 1531 |
sc_handler->buffered_read_in_data_buf.buffer, |
| 1532 |
sc_handler->buffered_read_in_data_buf.buffer + move_pos, |
| 1533 |
sc_handler->read_extra); |
| 1534 |
sc_handler->buffered_read_in_data_buf.len = sc_handler->read_extra; |
| 1535 |
sc_handler->read_extra = 0; |
| 1536 |
} else { |
| 1537 |
sc_handler->buffered_read_in_data_buf.len = 0; |
| 1538 |
} |
| 1539 |
} |
| 1540 |
} |
| 1541 |
|
| 1542 |
if (err) { |
| 1543 |
aws_channel_shutdown(slot->channel, aws_last_error()); |
| 1544 |
return AWS_OP_ERR; |
| 1545 |
} |
| 1546 |
|
| 1547 |
aws_mem_release(message->allocator, message); |
| 1548 |
return AWS_OP_SUCCESS; |
| 1549 |
} |
| 1550 |
|
| 1551 |
static int s_process_write_message( |
| 1552 |
struct aws_channel_handler *handler, |
| 1553 |
struct aws_channel_slot *slot, |
| 1554 |
struct aws_io_message *message) { |
| 1555 |
|
| 1556 |
struct secure_channel_handler *sc_handler = (struct secure_channel_handler *)handler->impl; |
| 1557 |
AWS_ASSERT(sc_handler->negotiation_finished); |
| 1558 |
SECURITY_STATUS status = SEC_E_OK; |
| 1559 |
|
| 1560 |
if (message) { |
| 1561 |
AWS_LOGF_TRACE( |
| 1562 |
AWS_LS_IO_TLS, "id=%p: processing ougoing message of size %zu", (void *)handler, message->message_data.len); |
| 1563 |
|
| 1564 |
struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message->message_data); |
| 1565 |
|
| 1566 |
while (message_cursor.len) { |
| 1567 |
AWS_LOGF_TRACE( |
| 1568 |
AWS_LS_IO_TLS, "id=%p: processing message fragment of size %zu", (void *)handler, message_cursor.len); |
| 1569 |
|
| 1570 |
size_t upstream_overhead = aws_channel_slot_upstream_message_overhead(sc_handler->slot); |
| 1571 |
upstream_overhead += sc_handler->stream_sizes.cbHeader + sc_handler->stream_sizes.cbTrailer; |
| 1572 |
size_t requested_length = message_cursor.len + upstream_overhead; |
| 1573 |
size_t to_write = sc_handler->stream_sizes.cbMaximumMessage < requested_length |
| 1574 |
? sc_handler->stream_sizes.cbMaximumMessage |
| 1575 |
: requested_length; |
| 1576 |
struct aws_io_message *outgoing_message = |
| 1577 |
aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, to_write); |
| 1578 |
|
| 1579 |
if (outgoing_message->message_data.capacity <= upstream_overhead) { |
| 1580 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 1581 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1582 |
} |
| 1583 |
|
| 1584 |
|
| 1585 |
size_t original_message_fragment_to_process = outgoing_message->message_data.capacity - upstream_overhead; |
| 1586 |
memcpy( |
| 1587 |
outgoing_message->message_data.buffer + sc_handler->stream_sizes.cbHeader, |
| 1588 |
message_cursor.ptr, |
| 1589 |
original_message_fragment_to_process); |
| 1590 |
|
| 1591 |
if (original_message_fragment_to_process == message_cursor.len) { |
| 1592 |
outgoing_message->on_completion = message->on_completion; |
| 1593 |
outgoing_message->user_data = message->user_data; |
| 1594 |
} |
| 1595 |
|
| 1596 |
SecBuffer buffers[4] = { |
| 1597 |
[0] = |
| 1598 |
{ |
| 1599 |
.BufferType = SECBUFFER_STREAM_HEADER, |
| 1600 |
.pvBuffer = outgoing_message->message_data.buffer, |
| 1601 |
.cbBuffer = sc_handler->stream_sizes.cbHeader, |
| 1602 |
}, |
| 1603 |
[1] = |
| 1604 |
{ |
| 1605 |
.BufferType = SECBUFFER_DATA, |
| 1606 |
.pvBuffer = outgoing_message->message_data.buffer + sc_handler->stream_sizes.cbHeader, |
| 1607 |
.cbBuffer = (unsigned long)original_message_fragment_to_process, |
| 1608 |
}, |
| 1609 |
[2] = |
| 1610 |
{ |
| 1611 |
.BufferType = SECBUFFER_STREAM_TRAILER, |
| 1612 |
.pvBuffer = outgoing_message->message_data.buffer + sc_handler->stream_sizes.cbHeader + |
| 1613 |
original_message_fragment_to_process, |
| 1614 |
.cbBuffer = sc_handler->stream_sizes.cbTrailer, |
| 1615 |
}, |
| 1616 |
[3] = |
| 1617 |
{ |
| 1618 |
.BufferType = SECBUFFER_EMPTY, |
| 1619 |
.pvBuffer = NULL, |
| 1620 |
.cbBuffer = 0, |
| 1621 |
}, |
| 1622 |
}; |
| 1623 |
|
| 1624 |
SecBufferDesc buffer_desc = { |
| 1625 |
.ulVersion = SECBUFFER_VERSION, |
| 1626 |
.cBuffers = 4, |
| 1627 |
.pBuffers = buffers, |
| 1628 |
}; |
| 1629 |
|
| 1630 |
status = EncryptMessage(&sc_handler->sec_handle, 0, &buffer_desc, 0); |
| 1631 |
|
| 1632 |
if (status == SEC_E_OK) { |
| 1633 |
outgoing_message->message_data.len = buffers[0].cbBuffer + buffers[1].cbBuffer + buffers[2].cbBuffer; |
| 1634 |
AWS_LOGF_TRACE( |
| 1635 |
AWS_LS_IO_TLS, |
| 1636 |
"id=%p:message fragment encrypted successfully: size is %zu", |
| 1637 |
(void *)handler, |
| 1638 |
outgoing_message->message_data.len); |
| 1639 |
|
| 1640 |
if (aws_channel_slot_send_message(slot, outgoing_message, AWS_CHANNEL_DIR_WRITE)) { |
| 1641 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 1642 |
return AWS_OP_ERR; |
| 1643 |
} |
| 1644 |
|
| 1645 |
aws_byte_cursor_advance(&message_cursor, original_message_fragment_to_process); |
| 1646 |
} else { |
| 1647 |
AWS_LOGF_TRACE( |
| 1648 |
AWS_LS_IO_TLS, |
| 1649 |
"id=%p: Error encrypting message. SECURITY_STATUS is %d", |
| 1650 |
(void *)handler, |
| 1651 |
(int)status); |
| 1652 |
return aws_raise_error(AWS_IO_TLS_ERROR_WRITE_FAILURE); |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
aws_mem_release(message->allocator, message); |
| 1657 |
} |
| 1658 |
|
| 1659 |
return AWS_OP_SUCCESS; |
| 1660 |
} |
| 1661 |
|
| 1662 |
static int s_increment_read_window(struct aws_channel_handler *handler, struct aws_channel_slot *slot, size_t size) { |
| 1663 |
(void)size; |
| 1664 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1665 |
if (sc_handler->read_state == AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE) { |
| 1666 |
return AWS_OP_SUCCESS; |
| 1667 |
} |
| 1668 |
AWS_LOGF_TRACE(AWS_LS_IO_TLS, "id=%p: Increment read window message received %zu", (void *)handler, size); |
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
if (sc_handler->negotiation_finished && !sc_handler->stream_sizes.cbMaximumMessage) { |
| 1673 |
SECURITY_STATUS status = |
| 1674 |
QueryContextAttributes(&sc_handler->sec_handle, SECPKG_ATTR_STREAM_SIZES, &sc_handler->stream_sizes); |
| 1675 |
|
| 1676 |
if (status != SEC_E_OK) { |
| 1677 |
AWS_LOGF_ERROR( |
| 1678 |
AWS_LS_IO_TLS, "id=%p: QueryContextAttributes failed with error %d", (void *)handler, (int)status); |
| 1679 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 1680 |
aws_channel_shutdown(slot->channel, AWS_ERROR_SYS_CALL_FAILURE); |
| 1681 |
return AWS_OP_ERR; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
|
| 1685 |
size_t total_desired_size = size; |
| 1686 |
size_t downstream_size = aws_channel_slot_downstream_read_window(slot); |
| 1687 |
size_t current_window_size = slot->window_size; |
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
if (sc_handler->stream_sizes.cbMaximumMessage) { |
| 1692 |
size_t likely_records_count = (size_t)ceil((double)(downstream_size) / (double)(READ_IN_SIZE)); |
| 1693 |
size_t offset_size = aws_mul_size_saturating( |
| 1694 |
likely_records_count, sc_handler->stream_sizes.cbTrailer + sc_handler->stream_sizes.cbHeader); |
| 1695 |
total_desired_size = aws_add_size_saturating(offset_size, downstream_size); |
| 1696 |
} |
| 1697 |
|
| 1698 |
if (total_desired_size > current_window_size) { |
| 1699 |
size_t window_update_size = total_desired_size - current_window_size; |
| 1700 |
AWS_LOGF_TRACE( |
| 1701 |
AWS_LS_IO_TLS, "id=%p: Propagating read window increment of size %zu", (void *)handler, window_update_size); |
| 1702 |
aws_channel_slot_increment_read_window(slot, window_update_size); |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (sc_handler->negotiation_finished && !sc_handler->read_task_pending) { |
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
sc_handler->read_task_pending = true; |
| 1710 |
aws_channel_task_init( |
| 1711 |
&sc_handler->read_task, |
| 1712 |
s_process_pending_output_task, |
| 1713 |
handler, |
| 1714 |
"secure_channel_handler_process_pending_output_on_window_increment"); |
| 1715 |
aws_channel_schedule_task_now(slot->channel, &sc_handler->read_task); |
| 1716 |
} |
| 1717 |
return AWS_OP_SUCCESS; |
| 1718 |
} |
| 1719 |
|
| 1720 |
static size_t s_initial_window_size(struct aws_channel_handler *handler) { |
| 1721 |
(void)handler; |
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
return EST_HANDSHAKE_SIZE; |
| 1726 |
} |
| 1727 |
|
| 1728 |
static void s_initialize_read_delay_shutdown( |
| 1729 |
struct aws_channel_handler *handler, |
| 1730 |
struct aws_channel_slot *slot, |
| 1731 |
int error_code) { |
| 1732 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
AWS_LOGF_DEBUG( |
| 1738 |
AWS_LS_IO_TLS, |
| 1739 |
"id=%p: TLS handler still have pending data to be delivered during shutdown. Wait until downstream " |
| 1740 |
"reads the data.", |
| 1741 |
(void *)handler); |
| 1742 |
if (aws_channel_slot_downstream_read_window(slot) == 0) { |
| 1743 |
AWS_LOGF_WARN( |
| 1744 |
AWS_LS_IO_TLS, |
| 1745 |
"id=%p: TLS shutdown delayed. Pending data cannot be processed until the flow-control window opens. " |
| 1746 |
" Your application may hang if the read window never opens", |
| 1747 |
(void *)handler); |
| 1748 |
} |
| 1749 |
sc_handler->read_state = AWS_TLS_HANDLER_READ_SHUTTING_DOWN; |
| 1750 |
sc_handler->shutdown_error_code = error_code; |
| 1751 |
if (!sc_handler->read_task_pending) { |
| 1752 |
|
| 1753 |
|
| 1754 |
sc_handler->read_task_pending = true; |
| 1755 |
aws_channel_task_init( |
| 1756 |
&sc_handler->read_task, |
| 1757 |
s_process_pending_output_task, |
| 1758 |
handler, |
| 1759 |
"secure_channel_handler_read_on_delay_shutdown"); |
| 1760 |
|
| 1761 |
aws_channel_schedule_task_now(slot->channel, &sc_handler->read_task); |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
static int s_handler_shutdown( |
| 1766 |
struct aws_channel_handler *handler, |
| 1767 |
struct aws_channel_slot *slot, |
| 1768 |
enum aws_channel_direction dir, |
| 1769 |
int error_code, |
| 1770 |
bool abort_immediately) { |
| 1771 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1772 |
|
| 1773 |
SecBuffer output_buffer = { |
| 1774 |
.pvBuffer = NULL, |
| 1775 |
.cbBuffer = 0, |
| 1776 |
.BufferType = SECBUFFER_EMPTY, |
| 1777 |
}; |
| 1778 |
|
| 1779 |
SecBufferDesc output_buffer_desc = { |
| 1780 |
.ulVersion = SECBUFFER_VERSION, |
| 1781 |
.cBuffers = 1, |
| 1782 |
.pBuffers = &output_buffer, |
| 1783 |
}; |
| 1784 |
|
| 1785 |
if (dir == AWS_CHANNEL_DIR_READ) { |
| 1786 |
AWS_LOGF_DEBUG( |
| 1787 |
AWS_LS_IO_TLS, "id=%p: shutting down read direction with error %d.", (void *)handler, error_code); |
| 1788 |
if (!abort_immediately && sc_handler->negotiation_finished && sc_handler->buffered_read_out_data_buf.len && |
| 1789 |
slot->adj_right) { |
| 1790 |
s_initialize_read_delay_shutdown(handler, slot, error_code); |
| 1791 |
return AWS_OP_SUCCESS; |
| 1792 |
} |
| 1793 |
sc_handler->read_state = AWS_TLS_HANDLER_READ_SHUT_DOWN_COMPLETE; |
| 1794 |
} else { |
| 1795 |
|
| 1796 |
if (!abort_immediately && error_code != AWS_IO_SOCKET_CLOSED) { |
| 1797 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Shutting down the write direction", (void *)handler); |
| 1798 |
|
| 1799 |
|
| 1800 |
SECURITY_STATUS status; |
| 1801 |
|
| 1802 |
DWORD shutdown_code = SCHANNEL_SHUTDOWN; |
| 1803 |
SecBuffer shutdown_buffer = { |
| 1804 |
.pvBuffer = &shutdown_code, |
| 1805 |
.cbBuffer = sizeof(shutdown_code), |
| 1806 |
.BufferType = SECBUFFER_TOKEN, |
| 1807 |
}; |
| 1808 |
|
| 1809 |
SecBufferDesc shutdown_buffer_desc = { |
| 1810 |
.ulVersion = SECBUFFER_VERSION, |
| 1811 |
.cBuffers = 1, |
| 1812 |
.pBuffers = &shutdown_buffer, |
| 1813 |
}; |
| 1814 |
|
| 1815 |
|
| 1816 |
status = ApplyControlToken(&sc_handler->sec_handle, &shutdown_buffer_desc); |
| 1817 |
|
| 1818 |
if (status != SEC_E_OK) { |
| 1819 |
error_code = AWS_ERROR_SYS_CALL_FAILURE; |
| 1820 |
goto cleanup; |
| 1821 |
} |
| 1822 |
|
| 1823 |
struct aws_byte_buf server_name = s_tls_handler_server_name(handler); |
| 1824 |
char server_name_cstr[256]; |
| 1825 |
AWS_ZERO_ARRAY(server_name_cstr); |
| 1826 |
AWS_FATAL_ASSERT(server_name.len < sizeof(server_name_cstr)); |
| 1827 |
memcpy(server_name_cstr, server_name.buffer, server_name.len); |
| 1828 |
|
| 1829 |
status = InitializeSecurityContextA( |
| 1830 |
&sc_handler->creds, |
| 1831 |
&sc_handler->sec_handle, |
| 1832 |
(SEC_CHAR *)server_name_cstr, |
| 1833 |
sc_handler->ctx_req, |
| 1834 |
0, |
| 1835 |
0, |
| 1836 |
NULL, |
| 1837 |
0, |
| 1838 |
NULL, |
| 1839 |
&output_buffer_desc, |
| 1840 |
&sc_handler->ctx_ret_flags, |
| 1841 |
NULL); |
| 1842 |
|
| 1843 |
if (status == SEC_E_OK || status == SEC_I_CONTEXT_EXPIRED) { |
| 1844 |
struct aws_io_message *outgoing_message = aws_channel_acquire_message_from_pool( |
| 1845 |
slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, output_buffer.cbBuffer); |
| 1846 |
|
| 1847 |
if (outgoing_message->message_data.capacity < output_buffer.cbBuffer) { |
| 1848 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 1849 |
error_code = AWS_IO_TLS_ERROR_WRITE_FAILURE; |
| 1850 |
abort_immediately = true; |
| 1851 |
goto cleanup; |
| 1852 |
} |
| 1853 |
memcpy(outgoing_message->message_data.buffer, output_buffer.pvBuffer, output_buffer.cbBuffer); |
| 1854 |
outgoing_message->message_data.len = output_buffer.cbBuffer; |
| 1855 |
|
| 1856 |
|
| 1857 |
if (aws_channel_slot_send_message(slot, outgoing_message, AWS_CHANNEL_DIR_WRITE)) { |
| 1858 |
aws_mem_release(outgoing_message->allocator, outgoing_message); |
| 1859 |
goto cleanup; |
| 1860 |
} |
| 1861 |
} |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
cleanup: |
| 1866 |
FreeContextBuffer(output_buffer.pvBuffer); |
| 1867 |
return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, abort_immediately); |
| 1868 |
} |
| 1869 |
|
| 1870 |
static void s_do_negotiation_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 1871 |
(void)task; |
| 1872 |
|
| 1873 |
struct aws_channel_handler *handler = arg; |
| 1874 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1875 |
|
| 1876 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 1877 |
int err = sc_handler->s_connection_state_fn(handler); |
| 1878 |
if (err) { |
| 1879 |
aws_channel_shutdown(sc_handler->slot->channel, aws_last_error()); |
| 1880 |
} |
| 1881 |
} |
| 1882 |
} |
| 1883 |
|
| 1884 |
static void s_secure_channel_handler_destroy( |
| 1885 |
struct aws_allocator *allocator, |
| 1886 |
struct secure_channel_handler *sc_handler) { |
| 1887 |
|
| 1888 |
if (sc_handler == NULL) { |
| 1889 |
return; |
| 1890 |
} |
| 1891 |
|
| 1892 |
if (sc_handler->protocol.buffer) { |
| 1893 |
aws_byte_buf_clean_up(&sc_handler->protocol); |
| 1894 |
} |
| 1895 |
|
| 1896 |
if (sc_handler->alpn_list) { |
| 1897 |
aws_string_destroy(sc_handler->alpn_list); |
| 1898 |
} |
| 1899 |
|
| 1900 |
if (sc_handler->server_name.buffer) { |
| 1901 |
aws_byte_buf_clean_up(&sc_handler->server_name); |
| 1902 |
} |
| 1903 |
|
| 1904 |
if (sc_handler->sec_handle.dwLower || sc_handler->sec_handle.dwUpper) { |
| 1905 |
DeleteSecurityContext(&sc_handler->sec_handle); |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (sc_handler->creds.dwLower || sc_handler->creds.dwUpper) { |
| 1909 |
DeleteSecurityContext(&sc_handler->creds); |
| 1910 |
} |
| 1911 |
|
| 1912 |
aws_tls_channel_handler_shared_clean_up(&sc_handler->shared_state); |
| 1913 |
|
| 1914 |
aws_mem_release(allocator, sc_handler); |
| 1915 |
} |
| 1916 |
|
| 1917 |
static void s_handler_destroy(struct aws_channel_handler *handler) { |
| 1918 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: destroying handler", (void *)handler); |
| 1919 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1920 |
|
| 1921 |
s_secure_channel_handler_destroy(handler->alloc, sc_handler); |
| 1922 |
} |
| 1923 |
|
| 1924 |
static void s_reset_statistics(struct aws_channel_handler *handler) { |
| 1925 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1926 |
|
| 1927 |
aws_crt_statistics_tls_reset(&sc_handler->shared_state.stats); |
| 1928 |
} |
| 1929 |
|
| 1930 |
static void s_gather_statistics(struct aws_channel_handler *handler, struct aws_array_list *stats) { |
| 1931 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1932 |
|
| 1933 |
void *stats_base = &sc_handler->shared_state.stats; |
| 1934 |
aws_array_list_push_back(stats, &stats_base); |
| 1935 |
} |
| 1936 |
|
| 1937 |
static int s_tls_client_handler_start_negotiation(struct aws_channel_handler *handler) { |
| 1938 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "id=%p: Kicking off TLS negotiation", (void *)handler); |
| 1939 |
|
| 1940 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1941 |
|
| 1942 |
if (aws_channel_thread_is_callers_thread(sc_handler->slot->channel)) { |
| 1943 |
int err = sc_handler->s_connection_state_fn(handler); |
| 1944 |
if (err) { |
| 1945 |
aws_channel_shutdown(sc_handler->slot->channel, aws_last_error()); |
| 1946 |
} |
| 1947 |
return err; |
| 1948 |
} |
| 1949 |
|
| 1950 |
aws_channel_task_init( |
| 1951 |
&sc_handler->sequential_task_storage, |
| 1952 |
s_do_negotiation_task, |
| 1953 |
handler, |
| 1954 |
"secure_channel_handler_start_negotation"); |
| 1955 |
aws_channel_schedule_task_now(sc_handler->slot->channel, &sc_handler->sequential_task_storage); |
| 1956 |
return AWS_OP_SUCCESS; |
| 1957 |
} |
| 1958 |
|
| 1959 |
static struct aws_byte_buf s_tls_handler_protocol(struct aws_channel_handler *handler) { |
| 1960 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1961 |
return sc_handler->protocol; |
| 1962 |
} |
| 1963 |
|
| 1964 |
static struct aws_byte_buf s_tls_handler_server_name(struct aws_channel_handler *handler) { |
| 1965 |
struct secure_channel_handler *sc_handler = handler->impl; |
| 1966 |
return sc_handler->server_name; |
| 1967 |
} |
| 1968 |
|
| 1969 |
static struct aws_channel_handler_vtable s_handler_vtable = { |
| 1970 |
.destroy = s_handler_destroy, |
| 1971 |
.process_read_message = s_process_read_message, |
| 1972 |
.process_write_message = s_process_write_message, |
| 1973 |
.shutdown = s_handler_shutdown, |
| 1974 |
.increment_read_window = s_increment_read_window, |
| 1975 |
.initial_window_size = s_initial_window_size, |
| 1976 |
.message_overhead = s_message_overhead, |
| 1977 |
.reset_statistics = s_reset_statistics, |
| 1978 |
.gather_statistics = s_gather_statistics, |
| 1979 |
}; |
| 1980 |
|
| 1981 |
static struct aws_channel_handler *s_tls_handler_new_common( |
| 1982 |
struct aws_allocator *alloc, |
| 1983 |
struct aws_tls_connection_options *options, |
| 1984 |
struct aws_channel_slot *slot, |
| 1985 |
bool is_client_mode, |
| 1986 |
struct secure_channel_handler *sc_handler) { |
| 1987 |
|
| 1988 |
struct secure_channel_ctx *sc_ctx = options->ctx->impl; |
| 1989 |
sc_handler->advertise_alpn_message = options->advertise_alpn_message; |
| 1990 |
sc_handler->on_data_read = options->on_data_read; |
| 1991 |
sc_handler->on_error = options->on_error; |
| 1992 |
sc_handler->on_negotiation_result = options->on_negotiation_result; |
| 1993 |
sc_handler->user_data = options->user_data; |
| 1994 |
|
| 1995 |
if (!options->alpn_list && sc_ctx->alpn_list) { |
| 1996 |
sc_handler->alpn_list = aws_string_new_from_string(alloc, sc_ctx->alpn_list); |
| 1997 |
if (!sc_handler->alpn_list) { |
| 1998 |
goto on_error; |
| 1999 |
} |
| 2000 |
} else if (options->alpn_list) { |
| 2001 |
sc_handler->alpn_list = aws_string_new_from_string(alloc, options->alpn_list); |
| 2002 |
if (!sc_handler->alpn_list) { |
| 2003 |
goto on_error; |
| 2004 |
} |
| 2005 |
} |
| 2006 |
sc_handler->handler.alloc = alloc; |
| 2007 |
sc_handler->handler.impl = sc_handler; |
| 2008 |
sc_handler->handler.vtable = &s_handler_vtable; |
| 2009 |
sc_handler->handler.slot = slot; |
| 2010 |
|
| 2011 |
if (options->server_name) { |
| 2012 |
AWS_LOGF_DEBUG( |
| 2013 |
AWS_LS_IO_TLS, |
| 2014 |
"id=%p: Setting SNI to %s", |
| 2015 |
(void *)&sc_handler->handler, |
| 2016 |
aws_string_c_str(options->server_name)); |
| 2017 |
struct aws_byte_cursor server_name_crsr = aws_byte_cursor_from_string(options->server_name); |
| 2018 |
if (aws_byte_buf_init_copy_from_cursor(&sc_handler->server_name, alloc, server_name_crsr)) { |
| 2019 |
goto on_error; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
sc_handler->slot = slot; |
| 2024 |
|
| 2025 |
if (is_client_mode) { |
| 2026 |
sc_handler->s_connection_state_fn = s_do_client_side_negotiation_step_1; |
| 2027 |
} else { |
| 2028 |
sc_handler->s_connection_state_fn = s_do_server_side_negotiation_step_1; |
| 2029 |
} |
| 2030 |
|
| 2031 |
sc_handler->custom_ca_store = sc_ctx->custom_trust_store; |
| 2032 |
sc_handler->buffered_read_in_data_buf = |
| 2033 |
aws_byte_buf_from_array(sc_handler->buffered_read_in_data, sizeof(sc_handler->buffered_read_in_data)); |
| 2034 |
sc_handler->buffered_read_in_data_buf.len = 0; |
| 2035 |
sc_handler->buffered_read_out_data_buf = |
| 2036 |
aws_byte_buf_from_array(sc_handler->buffered_read_out_data, sizeof(sc_handler->buffered_read_out_data)); |
| 2037 |
sc_handler->buffered_read_out_data_buf.len = 0; |
| 2038 |
sc_handler->verify_peer = sc_ctx->verify_peer; |
| 2039 |
|
| 2040 |
return &sc_handler->handler; |
| 2041 |
|
| 2042 |
on_error: |
| 2043 |
|
| 2044 |
s_secure_channel_handler_destroy(alloc, sc_handler); |
| 2045 |
|
| 2046 |
return NULL; |
| 2047 |
} |
| 2048 |
|
| 2049 |
static DWORD s_get_disabled_protocols( |
| 2050 |
enum aws_tls_versions minimum_tls_version, |
| 2051 |
bool is_client_mode, |
| 2052 |
bool disable_tls13) { |
| 2053 |
DWORD bit_disabled_protocols = 0; |
| 2054 |
if (is_client_mode) { |
| 2055 |
switch (minimum_tls_version) { |
| 2056 |
case AWS_IO_TLSv1_3: |
| 2057 |
#if defined(SP_PROT_TLS1_2_CLIENT) |
| 2058 |
bit_disabled_protocols |= SP_PROT_TLS1_2_CLIENT; |
| 2059 |
#endif |
| 2060 |
case AWS_IO_TLSv1_2: |
| 2061 |
bit_disabled_protocols |= SP_PROT_TLS1_1_CLIENT; |
| 2062 |
case AWS_IO_TLSv1_1: |
| 2063 |
bit_disabled_protocols |= SP_PROT_TLS1_0_CLIENT; |
| 2064 |
case AWS_IO_TLSv1: |
| 2065 |
bit_disabled_protocols |= SP_PROT_SSL3_CLIENT; |
| 2066 |
case AWS_IO_SSLv3: |
| 2067 |
break; |
| 2068 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 2069 |
bit_disabled_protocols = 0; |
| 2070 |
break; |
| 2071 |
} |
| 2072 |
#if defined(SP_PROT_TLS1_3_CLIENT) |
| 2073 |
if (disable_tls13) { |
| 2074 |
bit_disabled_protocols |= SP_PROT_TLS1_3_CLIENT; |
| 2075 |
} |
| 2076 |
#endif |
| 2077 |
} else { |
| 2078 |
switch (minimum_tls_version) { |
| 2079 |
case AWS_IO_TLSv1_3: |
| 2080 |
#if defined(SP_PROT_TLS1_2_SERVER) |
| 2081 |
bit_disabled_protocols |= SP_PROT_TLS1_2_SERVER; |
| 2082 |
#endif |
| 2083 |
case AWS_IO_TLSv1_2: |
| 2084 |
bit_disabled_protocols |= SP_PROT_TLS1_1_SERVER; |
| 2085 |
case AWS_IO_TLSv1_1: |
| 2086 |
bit_disabled_protocols |= SP_PROT_TLS1_0_SERVER; |
| 2087 |
case AWS_IO_TLSv1: |
| 2088 |
bit_disabled_protocols |= SP_PROT_SSL3_SERVER; |
| 2089 |
case AWS_IO_SSLv3: |
| 2090 |
break; |
| 2091 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 2092 |
bit_disabled_protocols = 0; |
| 2093 |
break; |
| 2094 |
} |
| 2095 |
#if defined(SP_PROT_TLS1_3_SERVER) |
| 2096 |
if (disable_tls13) { |
| 2097 |
bit_disabled_protocols |= SP_PROT_TLS1_3_SERVER; |
| 2098 |
} |
| 2099 |
#endif |
| 2100 |
} |
| 2101 |
return bit_disabled_protocols; |
| 2102 |
} |
| 2103 |
|
| 2104 |
static DWORD s_get_enabled_protocols(enum aws_tls_versions minimum_tls_version, bool is_client_mode) { |
| 2105 |
DWORD bit_enabled_protocols = 0; |
| 2106 |
if (is_client_mode) { |
| 2107 |
switch (minimum_tls_version) { |
| 2108 |
case AWS_IO_SSLv3: |
| 2109 |
bit_enabled_protocols |= SP_PROT_SSL3_CLIENT; |
| 2110 |
case AWS_IO_TLSv1: |
| 2111 |
bit_enabled_protocols |= SP_PROT_TLS1_0_CLIENT; |
| 2112 |
case AWS_IO_TLSv1_1: |
| 2113 |
bit_enabled_protocols |= SP_PROT_TLS1_1_CLIENT; |
| 2114 |
case AWS_IO_TLSv1_2: |
| 2115 |
#if defined(SP_PROT_TLS1_2_CLIENT) |
| 2116 |
bit_enabled_protocols |= SP_PROT_TLS1_2_CLIENT; |
| 2117 |
#endif |
| 2118 |
case AWS_IO_TLSv1_3: |
| 2119 |
|
| 2120 |
break; |
| 2121 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 2122 |
bit_enabled_protocols = 0; |
| 2123 |
break; |
| 2124 |
} |
| 2125 |
} else { |
| 2126 |
switch (minimum_tls_version) { |
| 2127 |
case AWS_IO_SSLv3: |
| 2128 |
bit_enabled_protocols |= SP_PROT_SSL3_SERVER; |
| 2129 |
case AWS_IO_TLSv1: |
| 2130 |
bit_enabled_protocols |= SP_PROT_TLS1_0_SERVER; |
| 2131 |
case AWS_IO_TLSv1_1: |
| 2132 |
bit_enabled_protocols |= SP_PROT_TLS1_1_SERVER; |
| 2133 |
case AWS_IO_TLSv1_2: |
| 2134 |
#if defined(SP_PROT_TLS1_2_SERVER) |
| 2135 |
bit_enabled_protocols |= SP_PROT_TLS1_2_SERVER; |
| 2136 |
#endif |
| 2137 |
case AWS_IO_TLSv1_3: |
| 2138 |
|
| 2139 |
break; |
| 2140 |
case AWS_IO_TLS_VER_SYS_DEFAULTS: |
| 2141 |
bit_enabled_protocols = 0; |
| 2142 |
break; |
| 2143 |
} |
| 2144 |
} |
| 2145 |
return bit_enabled_protocols; |
| 2146 |
} |
| 2147 |
|
| 2148 |
#if NTDDI_VERSION >= 0x0A000006 |
| 2149 |
|
| 2150 |
|
| 2151 |
static struct aws_channel_handler *s_tls_handler_sch_credentials_new( |
| 2152 |
struct aws_allocator *alloc, |
| 2153 |
struct aws_tls_connection_options *options, |
| 2154 |
struct aws_channel_slot *slot, |
| 2155 |
bool is_client_mode) { |
| 2156 |
|
| 2157 |
AWS_ASSERT(options->ctx); |
| 2158 |
|
| 2159 |
struct secure_channel_handler *sc_handler = aws_mem_calloc(alloc, 1, sizeof(struct secure_channel_handler)); |
| 2160 |
if (!sc_handler) { |
| 2161 |
return NULL; |
| 2162 |
} |
| 2163 |
struct secure_channel_ctx *sc_ctx = options->ctx->impl; |
| 2164 |
|
| 2165 |
SCH_CREDENTIALS credentials = {0}; |
| 2166 |
|
| 2167 |
ZeroMemory(&credentials, sizeof(SCH_CREDENTIALS)); |
| 2168 |
|
| 2169 |
TLS_PARAMETERS tls_params = {0}; |
| 2170 |
tls_params.grbitDisabledProtocols = |
| 2171 |
s_get_disabled_protocols(sc_ctx->minimum_tls_version, is_client_mode, sc_ctx->disable_tls13); |
| 2172 |
|
| 2173 |
credentials.pTlsParameters = &tls_params; |
| 2174 |
credentials.cTlsParameters = 1; |
| 2175 |
credentials.dwSessionLifespan = 0; |
| 2176 |
credentials.dwVersion = SCH_CREDENTIALS_VERSION; |
| 2177 |
credentials.dwCredFormat = 0; |
| 2178 |
credentials.dwFlags = sc_ctx->schannel_creds.dwFlags; |
| 2179 |
credentials.paCred = sc_ctx->schannel_creds.paCred; |
| 2180 |
credentials.cCreds = sc_ctx->schannel_creds.cCreds; |
| 2181 |
|
| 2182 |
aws_tls_channel_handler_shared_init(&sc_handler->shared_state, &sc_handler->handler, options); |
| 2183 |
|
| 2184 |
unsigned long credential_use = SECPKG_CRED_INBOUND; |
| 2185 |
if (is_client_mode) { |
| 2186 |
credential_use = SECPKG_CRED_OUTBOUND; |
| 2187 |
} |
| 2188 |
|
| 2189 |
SECURITY_STATUS status = AcquireCredentialsHandleA( |
| 2190 |
NULL, |
| 2191 |
UNISP_NAME, |
| 2192 |
credential_use, |
| 2193 |
NULL, |
| 2194 |
&credentials, |
| 2195 |
NULL, |
| 2196 |
NULL, |
| 2197 |
&sc_handler->creds, |
| 2198 |
&sc_handler->sspi_timestamp); |
| 2199 |
|
| 2200 |
if (status != SEC_E_OK) { |
| 2201 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Error on AcquireCredentialsHandle. SECURITY_STATUS is 0x%X", (int)status); |
| 2202 |
int aws_error = s_determine_sspi_error(status); |
| 2203 |
aws_raise_error(aws_error); |
| 2204 |
goto on_error; |
| 2205 |
} |
| 2206 |
|
| 2207 |
return s_tls_handler_new_common(alloc, options, slot, is_client_mode, sc_handler); |
| 2208 |
|
| 2209 |
on_error: |
| 2210 |
|
| 2211 |
s_secure_channel_handler_destroy(alloc, sc_handler); |
| 2212 |
|
| 2213 |
return NULL; |
| 2214 |
} |
| 2215 |
|
| 2216 |
#endif |
| 2217 |
|
| 2218 |
static struct aws_channel_handler *s_tls_handler_schannel_cred_new( |
| 2219 |
struct aws_allocator *alloc, |
| 2220 |
struct aws_tls_connection_options *options, |
| 2221 |
struct aws_channel_slot *slot, |
| 2222 |
bool is_client_mode) { |
| 2223 |
AWS_ASSERT(options->ctx); |
| 2224 |
|
| 2225 |
struct secure_channel_handler *sc_handler = aws_mem_calloc(alloc, 1, sizeof(struct secure_channel_handler)); |
| 2226 |
if (!sc_handler) { |
| 2227 |
return NULL; |
| 2228 |
} |
| 2229 |
|
| 2230 |
struct secure_channel_ctx *sc_ctx = options->ctx->impl; |
| 2231 |
|
| 2232 |
|
| 2233 |
if (sc_ctx->minimum_tls_version == AWS_IO_TLSv1_3) { |
| 2234 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Minimum TLS version is set to 1.3, but SCHANNEL_CRED can't support it"); |
| 2235 |
return NULL; |
| 2236 |
} |
| 2237 |
|
| 2238 |
SCHANNEL_CRED credentials = {0}; |
| 2239 |
credentials.dwVersion = SCHANNEL_CRED_VERSION; |
| 2240 |
credentials.dwCredFormat = 0; |
| 2241 |
credentials.dwFlags = sc_ctx->schannel_creds.dwFlags; |
| 2242 |
credentials.paCred = sc_ctx->schannel_creds.paCred; |
| 2243 |
credentials.cCreds = sc_ctx->schannel_creds.cCreds; |
| 2244 |
sc_ctx->disable_tls13 = true; |
| 2245 |
credentials.grbitEnabledProtocols = s_get_enabled_protocols(sc_ctx->minimum_tls_version, is_client_mode); |
| 2246 |
|
| 2247 |
aws_tls_channel_handler_shared_init(&sc_handler->shared_state, &sc_handler->handler, options); |
| 2248 |
|
| 2249 |
unsigned long credential_use = SECPKG_CRED_INBOUND; |
| 2250 |
if (is_client_mode) { |
| 2251 |
credential_use = SECPKG_CRED_OUTBOUND; |
| 2252 |
} |
| 2253 |
|
| 2254 |
SECURITY_STATUS status = AcquireCredentialsHandleA( |
| 2255 |
NULL, |
| 2256 |
UNISP_NAME, |
| 2257 |
credential_use, |
| 2258 |
NULL, |
| 2259 |
&credentials, |
| 2260 |
NULL, |
| 2261 |
NULL, |
| 2262 |
&sc_handler->creds, |
| 2263 |
&sc_handler->sspi_timestamp); |
| 2264 |
|
| 2265 |
if (status != SEC_E_OK) { |
| 2266 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Error on AcquireCredentialsHandle. SECURITY_STATUS is %d", (int)status); |
| 2267 |
int aws_error = s_determine_sspi_error(status); |
| 2268 |
aws_raise_error(aws_error); |
| 2269 |
goto on_error; |
| 2270 |
} |
| 2271 |
|
| 2272 |
return s_tls_handler_new_common(alloc, options, slot, is_client_mode, sc_handler); |
| 2273 |
|
| 2274 |
on_error: |
| 2275 |
|
| 2276 |
s_secure_channel_handler_destroy(alloc, sc_handler); |
| 2277 |
|
| 2278 |
return NULL; |
| 2279 |
} |
| 2280 |
|
| 2281 |
void aws_windows_force_schannel_creds(bool use_schannel_creds) { |
| 2282 |
s_use_schannel_creds = use_schannel_creds; |
| 2283 |
} |
| 2284 |
|
| 2285 |
static struct aws_channel_handler *s_tls_handler_new( |
| 2286 |
struct aws_allocator *alloc, |
| 2287 |
struct aws_tls_connection_options *options, |
| 2288 |
struct aws_channel_slot *slot, |
| 2289 |
bool is_client_mode) { |
| 2290 |
|
| 2291 |
#if NTDDI_VERSION >= 0x0A000006 |
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
if (s_is_windows_equal_or_above_build_number(WINDOWS_BUILD_20348) && !s_use_schannel_creds) { |
| 2302 |
return s_tls_handler_sch_credentials_new(alloc, options, slot, is_client_mode); |
| 2303 |
} |
| 2304 |
#endif |
| 2305 |
return s_tls_handler_schannel_cred_new(alloc, options, slot, is_client_mode); |
| 2306 |
} |
| 2307 |
|
| 2308 |
static struct aws_channel_handler *s_tls_client_handler_new( |
| 2309 |
struct aws_allocator *allocator, |
| 2310 |
struct aws_tls_connection_options *options, |
| 2311 |
struct aws_channel_slot *slot) { |
| 2312 |
|
| 2313 |
return s_tls_handler_new(allocator, options, slot, true); |
| 2314 |
} |
| 2315 |
|
| 2316 |
static struct aws_channel_handler *s_tls_server_handler_new( |
| 2317 |
struct aws_allocator *allocator, |
| 2318 |
struct aws_tls_connection_options *options, |
| 2319 |
struct aws_channel_slot *slot) { |
| 2320 |
|
| 2321 |
return s_tls_handler_new(allocator, options, slot, false); |
| 2322 |
} |
| 2323 |
|
| 2324 |
static void s_secure_channel_ctx_destroy(struct secure_channel_ctx *secure_channel_ctx) { |
| 2325 |
if (secure_channel_ctx == NULL) { |
| 2326 |
return; |
| 2327 |
} |
| 2328 |
|
| 2329 |
if (secure_channel_ctx->private_key) { |
| 2330 |
CryptDestroyKey(secure_channel_ctx->private_key); |
| 2331 |
} |
| 2332 |
|
| 2333 |
if (secure_channel_ctx->crypto_provider) { |
| 2334 |
CryptReleaseContext(secure_channel_ctx->crypto_provider, 0); |
| 2335 |
} |
| 2336 |
|
| 2337 |
if (secure_channel_ctx->custom_trust_store) { |
| 2338 |
aws_close_cert_store(secure_channel_ctx->custom_trust_store); |
| 2339 |
} |
| 2340 |
|
| 2341 |
if (secure_channel_ctx->pcerts) { |
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
if (secure_channel_ctx->should_free_pcerts == true) { |
| 2349 |
CertFreeCertificateContext(secure_channel_ctx->pcerts); |
| 2350 |
} |
| 2351 |
} |
| 2352 |
|
| 2353 |
if (secure_channel_ctx->cert_store) { |
| 2354 |
aws_close_cert_store(secure_channel_ctx->cert_store); |
| 2355 |
} |
| 2356 |
|
| 2357 |
if (secure_channel_ctx->alpn_list) { |
| 2358 |
aws_string_destroy(secure_channel_ctx->alpn_list); |
| 2359 |
} |
| 2360 |
|
| 2361 |
aws_mem_release(secure_channel_ctx->ctx.alloc, secure_channel_ctx); |
| 2362 |
} |
| 2363 |
|
| 2364 |
struct aws_tls_ctx *s_ctx_new( |
| 2365 |
struct aws_allocator *alloc, |
| 2366 |
const struct aws_tls_ctx_options *options, |
| 2367 |
bool is_client_mode) { |
| 2368 |
|
| 2369 |
DWORD dw_flags = 0; |
| 2370 |
PCCERT_CONTEXT *pa_cred = NULL; |
| 2371 |
DWORD creds = 0; |
| 2372 |
|
| 2373 |
if (!s_tls_is_cipher_pref_supported(options->cipher_pref)) { |
| 2374 |
aws_raise_error(AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED); |
| 2375 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: TLS Cipher Preference is not supported: %d.", options->cipher_pref); |
| 2376 |
return NULL; |
| 2377 |
} |
| 2378 |
|
| 2379 |
struct secure_channel_ctx *secure_channel_ctx = aws_mem_calloc(alloc, 1, sizeof(struct secure_channel_ctx)); |
| 2380 |
if (!secure_channel_ctx) { |
| 2381 |
return NULL; |
| 2382 |
} |
| 2383 |
|
| 2384 |
secure_channel_ctx->ctx.alloc = alloc; |
| 2385 |
secure_channel_ctx->ctx.impl = secure_channel_ctx; |
| 2386 |
aws_ref_count_init( |
| 2387 |
&secure_channel_ctx->ctx.ref_count, |
| 2388 |
secure_channel_ctx, |
| 2389 |
(aws_simple_completion_callback *)s_secure_channel_ctx_destroy); |
| 2390 |
|
| 2391 |
if (options->alpn_list) { |
| 2392 |
secure_channel_ctx->alpn_list = aws_string_new_from_string(alloc, options->alpn_list); |
| 2393 |
if (!secure_channel_ctx->alpn_list) { |
| 2394 |
goto clean_up; |
| 2395 |
} |
| 2396 |
} |
| 2397 |
|
| 2398 |
secure_channel_ctx->verify_peer = options->verify_peer; |
| 2399 |
secure_channel_ctx->should_free_pcerts = true; |
| 2400 |
secure_channel_ctx->disable_tls13 = false; |
| 2401 |
secure_channel_ctx->minimum_tls_version = options->minimum_tls_version; |
| 2402 |
|
| 2403 |
if (options->verify_peer && aws_tls_options_buf_is_set(&options->ca_file)) { |
| 2404 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: loading custom CA file."); |
| 2405 |
dw_flags |= SCH_CRED_MANUAL_CRED_VALIDATION; |
| 2406 |
|
| 2407 |
struct aws_byte_cursor ca_blob_cur = aws_byte_cursor_from_buf(&options->ca_file); |
| 2408 |
int error = aws_import_trusted_certificates(alloc, &ca_blob_cur, &secure_channel_ctx->custom_trust_store); |
| 2409 |
|
| 2410 |
if (error) { |
| 2411 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import custom CA with error %d", aws_last_error()); |
| 2412 |
goto clean_up; |
| 2413 |
} |
| 2414 |
} else if (is_client_mode) { |
| 2415 |
dw_flags |= SCH_CRED_AUTO_CRED_VALIDATION; |
| 2416 |
} |
| 2417 |
|
| 2418 |
if (is_client_mode && !options->verify_peer) { |
| 2419 |
AWS_LOGF_WARN( |
| 2420 |
AWS_LS_IO_TLS, |
| 2421 |
"static: x.509 validation has been disabled. " |
| 2422 |
"If this is not running in a test environment, this is likely a security vulnerability."); |
| 2423 |
dw_flags &= ~(SCH_CRED_AUTO_CRED_VALIDATION); |
| 2424 |
dw_flags |= SCH_CRED_IGNORE_NO_REVOCATION_CHECK | SCH_CRED_IGNORE_REVOCATION_OFFLINE | |
| 2425 |
SCH_CRED_NO_SERVERNAME_CHECK | SCH_CRED_MANUAL_CRED_VALIDATION; |
| 2426 |
} else if (is_client_mode) { |
| 2427 |
if (!options->no_certificate_revocation) { |
| 2428 |
dw_flags |= SCH_CRED_REVOCATION_CHECK_CHAIN | SCH_CRED_IGNORE_REVOCATION_OFFLINE; |
| 2429 |
} |
| 2430 |
} |
| 2431 |
|
| 2432 |
|
| 2433 |
dw_flags |= SCH_USE_STRONG_CRYPTO; |
| 2434 |
dw_flags |= SCH_CRED_NO_DEFAULT_CREDS; |
| 2435 |
|
| 2436 |
|
| 2437 |
if (options->system_certificate_path) { |
| 2438 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: assuming certificate is in a system store, loading now."); |
| 2439 |
|
| 2440 |
if (aws_load_cert_from_system_cert_store( |
| 2441 |
options->system_certificate_path, &secure_channel_ctx->cert_store, &secure_channel_ctx->pcerts)) { |
| 2442 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to load %s", options->system_certificate_path); |
| 2443 |
goto clean_up; |
| 2444 |
} |
| 2445 |
pa_cred = &secure_channel_ctx->pcerts; |
| 2446 |
creds = 1; |
| 2447 |
|
| 2448 |
} else if (aws_tls_options_buf_is_set(&options->certificate) && aws_tls_options_buf_is_set(&options->private_key)) { |
| 2449 |
|
| 2450 |
AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: certificate and key have been set, setting them up now."); |
| 2451 |
|
| 2452 |
if (!aws_text_is_utf8(options->certificate.buffer, options->certificate.len)) { |
| 2453 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import certificate, must be ASCII/UTF-8 encoded"); |
| 2454 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 2455 |
goto clean_up; |
| 2456 |
} |
| 2457 |
|
| 2458 |
if (!aws_text_is_utf8(options->private_key.buffer, options->private_key.len)) { |
| 2459 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: failed to import private key, must be ASCII/UTF-8 encoded"); |
| 2460 |
aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE); |
| 2461 |
goto clean_up; |
| 2462 |
} |
| 2463 |
|
| 2464 |
struct aws_byte_cursor cert_chain_cur = aws_byte_cursor_from_buf(&options->certificate); |
| 2465 |
struct aws_byte_cursor pk_cur = aws_byte_cursor_from_buf(&options->private_key); |
| 2466 |
int err = aws_import_key_pair_to_cert_context( |
| 2467 |
alloc, |
| 2468 |
&cert_chain_cur, |
| 2469 |
&pk_cur, |
| 2470 |
is_client_mode, |
| 2471 |
&secure_channel_ctx->cert_store, |
| 2472 |
&secure_channel_ctx->pcerts, |
| 2473 |
&secure_channel_ctx->crypto_provider, |
| 2474 |
&secure_channel_ctx->private_key, |
| 2475 |
&secure_channel_ctx->disable_tls13); |
| 2476 |
|
| 2477 |
if (err) { |
| 2478 |
AWS_LOGF_ERROR( |
| 2479 |
AWS_LS_IO_TLS, "static: failed to import certificate and private key with error %d.", aws_last_error()); |
| 2480 |
goto clean_up; |
| 2481 |
} |
| 2482 |
|
| 2483 |
if (secure_channel_ctx->disable_tls13 && options->minimum_tls_version == AWS_IO_TLSv1_3) { |
| 2484 |
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: minimum TLS version is set to 1.3, but it can't be supported"); |
| 2485 |
goto clean_up; |
| 2486 |
} |
| 2487 |
|
| 2488 |
pa_cred = &secure_channel_ctx->pcerts; |
| 2489 |
creds = 1; |
| 2490 |
secure_channel_ctx->should_free_pcerts = false; |
| 2491 |
} |
| 2492 |
|
| 2493 |
secure_channel_ctx->schannel_creds.dwFlags = dw_flags; |
| 2494 |
secure_channel_ctx->schannel_creds.paCred = pa_cred; |
| 2495 |
secure_channel_ctx->schannel_creds.cCreds = creds; |
| 2496 |
|
| 2497 |
return &secure_channel_ctx->ctx; |
| 2498 |
|
| 2499 |
clean_up: |
| 2500 |
s_secure_channel_ctx_destroy(secure_channel_ctx); |
| 2501 |
return NULL; |
| 2502 |
} |
| 2503 |
|
| 2504 |
static struct aws_tls_ctx *s_tls_server_ctx_new( |
| 2505 |
struct aws_allocator *alloc, |
| 2506 |
const struct aws_tls_ctx_options *options) { |
| 2507 |
return s_ctx_new(alloc, options, false); |
| 2508 |
} |
| 2509 |
|
| 2510 |
static struct aws_tls_ctx *s_tls_client_ctx_new( |
| 2511 |
struct aws_allocator *alloc, |
| 2512 |
const struct aws_tls_ctx_options *options) { |
| 2513 |
return s_ctx_new(alloc, options, true); |
| 2514 |
} |
| 2515 |
|
| 2516 |
static struct aws_tls_vtable s_vtable = { |
| 2517 |
.init_static_state = s_aws_tls_init_static_state, |
| 2518 |
.clean_up_static_state = s_tls_clean_up_static_state, |
| 2519 |
.is_alpn_available = s_tls_is_alpn_available, |
| 2520 |
.is_cipher_pref_supported = s_tls_is_cipher_pref_supported, |
| 2521 |
.client_handler_start_negotiation = s_tls_client_handler_start_negotiation, |
| 2522 |
.key_operation_complete = NULL, |
| 2523 |
.key_operation_complete_with_error = NULL, |
| 2524 |
.key_operation_get_input = NULL, |
| 2525 |
.key_operation_get_type = NULL, |
| 2526 |
.key_operation_get_signature_algorithm = NULL, |
| 2527 |
.key_operation_get_digest_algorithm = NULL, |
| 2528 |
.handler_protocol = s_tls_handler_protocol, |
| 2529 |
.handler_server_name = s_tls_handler_server_name, |
| 2530 |
.client_handler_new = s_tls_client_handler_new, |
| 2531 |
.server_handler_new = s_tls_server_handler_new, |
| 2532 |
.server_ctx_new = s_tls_server_ctx_new, |
| 2533 |
.client_ctx_new = s_tls_client_ctx_new, |
| 2534 |
}; |
| 2535 |
|
| 2536 |
void secure_channel_init_tls_vtable(struct aws_tls_vtable *vtable) { |
| 2537 |
*vtable = s_vtable; |
| 2538 |
} |