| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/proxy.h> |
| 7 |
|
| 8 |
#include <aws/common/encoding.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
#include <aws/http/private/proxy_impl.h> |
| 11 |
|
| 12 |
#if defined(_MSC_VER) |
| 13 |
# pragma warning(push) |
| 14 |
# pragma warning(disable : 4221) |
| 15 |
#endif |
| 16 |
|
| 17 |
struct aws_http_proxy_negotiator *aws_http_proxy_negotiator_acquire( |
| 18 |
struct aws_http_proxy_negotiator *proxy_negotiator) { |
| 19 |
if (proxy_negotiator != NULL) { |
| 20 |
aws_ref_count_acquire(&proxy_negotiator->ref_count); |
| 21 |
} |
| 22 |
|
| 23 |
return proxy_negotiator; |
| 24 |
} |
| 25 |
|
| 26 |
void aws_http_proxy_negotiator_release(struct aws_http_proxy_negotiator *proxy_negotiator) { |
| 27 |
if (proxy_negotiator != NULL) { |
| 28 |
aws_ref_count_release(&proxy_negotiator->ref_count); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
struct aws_http_proxy_negotiator *aws_http_proxy_strategy_create_negotiator( |
| 33 |
struct aws_http_proxy_strategy *strategy, |
| 34 |
struct aws_allocator *allocator) { |
| 35 |
if (strategy == NULL || allocator == NULL) { |
| 36 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
|
| 40 |
return strategy->vtable->create_negotiator(strategy, allocator); |
| 41 |
} |
| 42 |
|
| 43 |
enum aws_http_proxy_negotiation_retry_directive aws_http_proxy_negotiator_get_retry_directive( |
| 44 |
struct aws_http_proxy_negotiator *proxy_negotiator) { |
| 45 |
if (proxy_negotiator != NULL) { |
| 46 |
if (proxy_negotiator->strategy_vtable.tunnelling_vtable->get_retry_directive != NULL) { |
| 47 |
return proxy_negotiator->strategy_vtable.tunnelling_vtable->get_retry_directive(proxy_negotiator); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return AWS_HPNRD_STOP; |
| 52 |
} |
| 53 |
|
| 54 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_acquire(struct aws_http_proxy_strategy *proxy_strategy) { |
| 55 |
if (proxy_strategy != NULL) { |
| 56 |
aws_ref_count_acquire(&proxy_strategy->ref_count); |
| 57 |
} |
| 58 |
|
| 59 |
return proxy_strategy; |
| 60 |
} |
| 61 |
|
| 62 |
void aws_http_proxy_strategy_release(struct aws_http_proxy_strategy *proxy_strategy) { |
| 63 |
if (proxy_strategy != NULL) { |
| 64 |
aws_ref_count_release(&proxy_strategy->ref_count); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
enum proxy_negotiator_connect_state { |
| 71 |
AWS_PNCS_READY, |
| 72 |
AWS_PNCS_IN_PROGRESS, |
| 73 |
AWS_PNCS_SUCCESS, |
| 74 |
AWS_PNCS_FAILURE, |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
struct aws_http_proxy_strategy_basic_auth { |
| 80 |
struct aws_allocator *allocator; |
| 81 |
struct aws_string *user_name; |
| 82 |
struct aws_string *password; |
| 83 |
struct aws_http_proxy_strategy strategy_base; |
| 84 |
}; |
| 85 |
|
| 86 |
static void s_destroy_basic_auth_strategy(void *user_data) { |
| 87 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 88 |
struct aws_http_proxy_strategy_basic_auth *basic_auth_strategy = proxy_strategy->impl; |
| 89 |
|
| 90 |
aws_string_destroy(basic_auth_strategy->user_name); |
| 91 |
aws_string_destroy(basic_auth_strategy->password); |
| 92 |
|
| 93 |
aws_mem_release(basic_auth_strategy->allocator, basic_auth_strategy); |
| 94 |
} |
| 95 |
|
| 96 |
struct aws_http_proxy_negotiator_basic_auth { |
| 97 |
struct aws_allocator *allocator; |
| 98 |
|
| 99 |
struct aws_http_proxy_strategy *strategy; |
| 100 |
|
| 101 |
enum proxy_negotiator_connect_state connect_state; |
| 102 |
|
| 103 |
struct aws_http_proxy_negotiator negotiator_base; |
| 104 |
}; |
| 105 |
|
| 106 |
static void s_destroy_basic_auth_negotiator(void *user_data) { |
| 107 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 108 |
struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl; |
| 109 |
|
| 110 |
aws_http_proxy_strategy_release(basic_auth_negotiator->strategy); |
| 111 |
|
| 112 |
aws_mem_release(basic_auth_negotiator->allocator, basic_auth_negotiator); |
| 113 |
} |
| 114 |
|
| 115 |
AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_name, "Proxy-Authorization"); |
| 116 |
AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_basic_prefix, "Basic "); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static int s_add_basic_proxy_authentication_header( |
| 122 |
struct aws_allocator *allocator, |
| 123 |
struct aws_http_message *request, |
| 124 |
struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator) { |
| 125 |
|
| 126 |
struct aws_byte_buf base64_input_value; |
| 127 |
AWS_ZERO_STRUCT(base64_input_value); |
| 128 |
|
| 129 |
struct aws_byte_buf header_value; |
| 130 |
AWS_ZERO_STRUCT(header_value); |
| 131 |
|
| 132 |
int result = AWS_OP_ERR; |
| 133 |
|
| 134 |
struct aws_http_proxy_strategy_basic_auth *basic_auth_strategy = basic_auth_negotiator->strategy->impl; |
| 135 |
|
| 136 |
if (aws_byte_buf_init( |
| 137 |
&base64_input_value, |
| 138 |
allocator, |
| 139 |
basic_auth_strategy->user_name->len + basic_auth_strategy->password->len + 1)) { |
| 140 |
goto done; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
struct aws_byte_cursor username_cursor = aws_byte_cursor_from_string(basic_auth_strategy->user_name); |
| 145 |
if (aws_byte_buf_append(&base64_input_value, &username_cursor)) { |
| 146 |
goto done; |
| 147 |
} |
| 148 |
|
| 149 |
struct aws_byte_cursor colon_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":"); |
| 150 |
if (aws_byte_buf_append(&base64_input_value, &colon_cursor)) { |
| 151 |
goto done; |
| 152 |
} |
| 153 |
|
| 154 |
struct aws_byte_cursor password_cursor = aws_byte_cursor_from_string(basic_auth_strategy->password); |
| 155 |
if (aws_byte_buf_append(&base64_input_value, &password_cursor)) { |
| 156 |
goto done; |
| 157 |
} |
| 158 |
|
| 159 |
struct aws_byte_cursor base64_source_cursor = |
| 160 |
aws_byte_cursor_from_array(base64_input_value.buffer, base64_input_value.len); |
| 161 |
|
| 162 |
|
| 163 |
size_t required_size = 0; |
| 164 |
if (aws_base64_compute_encoded_len(base64_source_cursor.len, &required_size)) { |
| 165 |
goto done; |
| 166 |
} |
| 167 |
|
| 168 |
required_size += s_proxy_authorization_header_basic_prefix->len + 1; |
| 169 |
if (aws_byte_buf_init(&header_value, allocator, required_size)) { |
| 170 |
goto done; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
struct aws_byte_cursor basic_prefix = aws_byte_cursor_from_string(s_proxy_authorization_header_basic_prefix); |
| 175 |
if (aws_byte_buf_append_dynamic(&header_value, &basic_prefix)) { |
| 176 |
goto done; |
| 177 |
} |
| 178 |
|
| 179 |
if (aws_base64_encode(&base64_source_cursor, &header_value)) { |
| 180 |
goto done; |
| 181 |
} |
| 182 |
|
| 183 |
struct aws_http_header header = { |
| 184 |
.name = aws_byte_cursor_from_string(s_proxy_authorization_header_name), |
| 185 |
.value = aws_byte_cursor_from_array(header_value.buffer, header_value.len), |
| 186 |
}; |
| 187 |
|
| 188 |
if (aws_http_message_add_header(request, header)) { |
| 189 |
goto done; |
| 190 |
} |
| 191 |
|
| 192 |
result = AWS_OP_SUCCESS; |
| 193 |
|
| 194 |
done: |
| 195 |
|
| 196 |
aws_byte_buf_clean_up(&header_value); |
| 197 |
aws_byte_buf_clean_up(&base64_input_value); |
| 198 |
|
| 199 |
return result; |
| 200 |
} |
| 201 |
|
| 202 |
int s_basic_auth_forward_add_header( |
| 203 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 204 |
struct aws_http_message *message) { |
| 205 |
struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl; |
| 206 |
|
| 207 |
return s_add_basic_proxy_authentication_header(basic_auth_negotiator->allocator, message, basic_auth_negotiator); |
| 208 |
} |
| 209 |
|
| 210 |
void s_basic_auth_tunnel_add_header( |
| 211 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 212 |
struct aws_http_message *message, |
| 213 |
aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback, |
| 214 |
aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback, |
| 215 |
void *internal_proxy_user_data) { |
| 216 |
|
| 217 |
struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl; |
| 218 |
if (basic_auth_negotiator->connect_state != AWS_PNCS_READY) { |
| 219 |
negotiation_termination_callback(message, AWS_ERROR_HTTP_PROXY_CONNECT_FAILED, internal_proxy_user_data); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
basic_auth_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 224 |
|
| 225 |
if (s_add_basic_proxy_authentication_header(basic_auth_negotiator->allocator, message, basic_auth_negotiator)) { |
| 226 |
negotiation_termination_callback(message, aws_last_error(), internal_proxy_user_data); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
negotiation_http_request_forward_callback(message, internal_proxy_user_data); |
| 231 |
} |
| 232 |
|
| 233 |
static int s_basic_auth_on_connect_status( |
| 234 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 235 |
enum aws_http_status_code status_code) { |
| 236 |
struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl; |
| 237 |
|
| 238 |
if (basic_auth_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) { |
| 239 |
if (AWS_HTTP_STATUS_CODE_200_OK != status_code) { |
| 240 |
basic_auth_negotiator->connect_state = AWS_PNCS_FAILURE; |
| 241 |
} else { |
| 242 |
basic_auth_negotiator->connect_state = AWS_PNCS_SUCCESS; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return AWS_OP_SUCCESS; |
| 247 |
} |
| 248 |
|
| 249 |
static struct aws_http_proxy_negotiator_forwarding_vtable s_basic_auth_proxy_negotiator_forwarding_vtable = { |
| 250 |
.forward_request_transform = s_basic_auth_forward_add_header, |
| 251 |
}; |
| 252 |
|
| 253 |
static struct aws_http_proxy_negotiator_tunnelling_vtable s_basic_auth_proxy_negotiator_tunneling_vtable = { |
| 254 |
.on_status_callback = s_basic_auth_on_connect_status, |
| 255 |
.connect_request_transform = s_basic_auth_tunnel_add_header, |
| 256 |
}; |
| 257 |
|
| 258 |
static struct aws_http_proxy_negotiator *s_create_basic_auth_negotiator( |
| 259 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 260 |
struct aws_allocator *allocator) { |
| 261 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 262 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = |
| 267 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_basic_auth)); |
| 268 |
if (basic_auth_negotiator == NULL) { |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
basic_auth_negotiator->allocator = allocator; |
| 273 |
basic_auth_negotiator->connect_state = AWS_PNCS_READY; |
| 274 |
basic_auth_negotiator->negotiator_base.impl = basic_auth_negotiator; |
| 275 |
aws_ref_count_init( |
| 276 |
&basic_auth_negotiator->negotiator_base.ref_count, |
| 277 |
&basic_auth_negotiator->negotiator_base, |
| 278 |
(aws_simple_completion_callback *)s_destroy_basic_auth_negotiator); |
| 279 |
|
| 280 |
if (proxy_strategy->proxy_connection_type == AWS_HPCT_HTTP_FORWARD) { |
| 281 |
basic_auth_negotiator->negotiator_base.strategy_vtable.forwarding_vtable = |
| 282 |
&s_basic_auth_proxy_negotiator_forwarding_vtable; |
| 283 |
} else { |
| 284 |
basic_auth_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable = |
| 285 |
&s_basic_auth_proxy_negotiator_tunneling_vtable; |
| 286 |
} |
| 287 |
|
| 288 |
basic_auth_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy); |
| 289 |
|
| 290 |
return &basic_auth_negotiator->negotiator_base; |
| 291 |
} |
| 292 |
|
| 293 |
static struct aws_http_proxy_strategy_vtable s_basic_auth_proxy_strategy_vtable = { |
| 294 |
.create_negotiator = s_create_basic_auth_negotiator, |
| 295 |
}; |
| 296 |
|
| 297 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_basic_auth( |
| 298 |
struct aws_allocator *allocator, |
| 299 |
struct aws_http_proxy_strategy_basic_auth_options *config) { |
| 300 |
if (config == NULL || allocator == NULL) { |
| 301 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
|
| 305 |
if (config->proxy_connection_type != AWS_HPCT_HTTP_FORWARD && |
| 306 |
config->proxy_connection_type != AWS_HPCT_HTTP_TUNNEL) { |
| 307 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
struct aws_http_proxy_strategy_basic_auth *basic_auth_strategy = |
| 312 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_basic_auth)); |
| 313 |
if (basic_auth_strategy == NULL) { |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
|
| 317 |
basic_auth_strategy->strategy_base.impl = basic_auth_strategy; |
| 318 |
basic_auth_strategy->strategy_base.vtable = &s_basic_auth_proxy_strategy_vtable; |
| 319 |
basic_auth_strategy->allocator = allocator; |
| 320 |
basic_auth_strategy->strategy_base.proxy_connection_type = config->proxy_connection_type; |
| 321 |
aws_ref_count_init( |
| 322 |
&basic_auth_strategy->strategy_base.ref_count, |
| 323 |
&basic_auth_strategy->strategy_base, |
| 324 |
(aws_simple_completion_callback *)s_destroy_basic_auth_strategy); |
| 325 |
|
| 326 |
basic_auth_strategy->user_name = aws_string_new_from_cursor(allocator, &config->user_name); |
| 327 |
if (basic_auth_strategy->user_name == NULL) { |
| 328 |
goto on_error; |
| 329 |
} |
| 330 |
|
| 331 |
basic_auth_strategy->password = aws_string_new_from_cursor(allocator, &config->password); |
| 332 |
if (basic_auth_strategy->password == NULL) { |
| 333 |
goto on_error; |
| 334 |
} |
| 335 |
|
| 336 |
return &basic_auth_strategy->strategy_base; |
| 337 |
|
| 338 |
on_error: |
| 339 |
|
| 340 |
aws_http_proxy_strategy_release(&basic_auth_strategy->strategy_base); |
| 341 |
|
| 342 |
return NULL; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
struct aws_http_proxy_strategy_one_time_identity { |
| 348 |
struct aws_allocator *allocator; |
| 349 |
|
| 350 |
struct aws_http_proxy_strategy strategy_base; |
| 351 |
}; |
| 352 |
|
| 353 |
struct aws_http_proxy_negotiator_one_time_identity { |
| 354 |
struct aws_allocator *allocator; |
| 355 |
|
| 356 |
enum proxy_negotiator_connect_state connect_state; |
| 357 |
|
| 358 |
struct aws_http_proxy_negotiator negotiator_base; |
| 359 |
}; |
| 360 |
|
| 361 |
static void s_destroy_one_time_identity_negotiator(void *user_data) { |
| 362 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 363 |
struct aws_http_proxy_negotiator_one_time_identity *identity_negotiator = proxy_negotiator->impl; |
| 364 |
|
| 365 |
aws_mem_release(identity_negotiator->allocator, identity_negotiator); |
| 366 |
} |
| 367 |
|
| 368 |
void s_one_time_identity_connect_transform( |
| 369 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 370 |
struct aws_http_message *message, |
| 371 |
aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback, |
| 372 |
aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback, |
| 373 |
void *internal_proxy_user_data) { |
| 374 |
|
| 375 |
struct aws_http_proxy_negotiator_one_time_identity *one_time_identity_negotiator = proxy_negotiator->impl; |
| 376 |
if (one_time_identity_negotiator->connect_state != AWS_PNCS_READY) { |
| 377 |
negotiation_termination_callback(message, AWS_ERROR_HTTP_PROXY_CONNECT_FAILED, internal_proxy_user_data); |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
one_time_identity_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 382 |
negotiation_http_request_forward_callback(message, internal_proxy_user_data); |
| 383 |
} |
| 384 |
|
| 385 |
static int s_one_time_identity_on_connect_status( |
| 386 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 387 |
enum aws_http_status_code status_code) { |
| 388 |
struct aws_http_proxy_negotiator_one_time_identity *one_time_identity_negotiator = proxy_negotiator->impl; |
| 389 |
|
| 390 |
if (one_time_identity_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) { |
| 391 |
if (AWS_HTTP_STATUS_CODE_200_OK != status_code) { |
| 392 |
one_time_identity_negotiator->connect_state = AWS_PNCS_FAILURE; |
| 393 |
} else { |
| 394 |
one_time_identity_negotiator->connect_state = AWS_PNCS_SUCCESS; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
return AWS_OP_SUCCESS; |
| 399 |
} |
| 400 |
|
| 401 |
static struct aws_http_proxy_negotiator_tunnelling_vtable s_one_time_identity_proxy_negotiator_tunneling_vtable = { |
| 402 |
.on_status_callback = s_one_time_identity_on_connect_status, |
| 403 |
.connect_request_transform = s_one_time_identity_connect_transform, |
| 404 |
}; |
| 405 |
|
| 406 |
static struct aws_http_proxy_negotiator *s_create_one_time_identity_negotiator( |
| 407 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 408 |
struct aws_allocator *allocator) { |
| 409 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 410 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 411 |
return NULL; |
| 412 |
} |
| 413 |
|
| 414 |
struct aws_http_proxy_negotiator_one_time_identity *identity_negotiator = |
| 415 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_one_time_identity)); |
| 416 |
if (identity_negotiator == NULL) { |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
|
| 420 |
identity_negotiator->allocator = allocator; |
| 421 |
identity_negotiator->connect_state = AWS_PNCS_READY; |
| 422 |
identity_negotiator->negotiator_base.impl = identity_negotiator; |
| 423 |
aws_ref_count_init( |
| 424 |
&identity_negotiator->negotiator_base.ref_count, |
| 425 |
&identity_negotiator->negotiator_base, |
| 426 |
(aws_simple_completion_callback *)s_destroy_one_time_identity_negotiator); |
| 427 |
|
| 428 |
identity_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable = |
| 429 |
&s_one_time_identity_proxy_negotiator_tunneling_vtable; |
| 430 |
|
| 431 |
return &identity_negotiator->negotiator_base; |
| 432 |
} |
| 433 |
|
| 434 |
static struct aws_http_proxy_strategy_vtable s_one_time_identity_proxy_strategy_vtable = { |
| 435 |
.create_negotiator = s_create_one_time_identity_negotiator, |
| 436 |
}; |
| 437 |
|
| 438 |
static void s_destroy_one_time_identity_strategy(void *user_data) { |
| 439 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 440 |
struct aws_http_proxy_strategy_one_time_identity *identity_strategy = proxy_strategy->impl; |
| 441 |
|
| 442 |
aws_mem_release(identity_strategy->allocator, identity_strategy); |
| 443 |
} |
| 444 |
|
| 445 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_one_time_identity( |
| 446 |
struct aws_allocator *allocator) { |
| 447 |
if (allocator == NULL) { |
| 448 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 449 |
return NULL; |
| 450 |
} |
| 451 |
|
| 452 |
struct aws_http_proxy_strategy_one_time_identity *identity_strategy = |
| 453 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_one_time_identity)); |
| 454 |
if (identity_strategy == NULL) { |
| 455 |
return NULL; |
| 456 |
} |
| 457 |
|
| 458 |
identity_strategy->strategy_base.impl = identity_strategy; |
| 459 |
identity_strategy->strategy_base.vtable = &s_one_time_identity_proxy_strategy_vtable; |
| 460 |
identity_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL; |
| 461 |
identity_strategy->allocator = allocator; |
| 462 |
|
| 463 |
aws_ref_count_init( |
| 464 |
&identity_strategy->strategy_base.ref_count, |
| 465 |
&identity_strategy->strategy_base, |
| 466 |
(aws_simple_completion_callback *)s_destroy_one_time_identity_strategy); |
| 467 |
|
| 468 |
return &identity_strategy->strategy_base; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
struct aws_http_proxy_strategy_forwarding_identity { |
| 474 |
struct aws_allocator *allocator; |
| 475 |
|
| 476 |
struct aws_http_proxy_strategy strategy_base; |
| 477 |
}; |
| 478 |
|
| 479 |
struct aws_http_proxy_negotiator_forwarding_identity { |
| 480 |
struct aws_allocator *allocator; |
| 481 |
|
| 482 |
struct aws_http_proxy_negotiator negotiator_base; |
| 483 |
}; |
| 484 |
|
| 485 |
static void s_destroy_forwarding_identity_negotiator(void *user_data) { |
| 486 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 487 |
struct aws_http_proxy_negotiator_forwarding_identity *identity_negotiator = proxy_negotiator->impl; |
| 488 |
|
| 489 |
aws_mem_release(identity_negotiator->allocator, identity_negotiator); |
| 490 |
} |
| 491 |
|
| 492 |
int s_forwarding_identity_connect_transform( |
| 493 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 494 |
struct aws_http_message *message) { |
| 495 |
|
| 496 |
(void)message; |
| 497 |
(void)proxy_negotiator; |
| 498 |
|
| 499 |
return AWS_OP_SUCCESS; |
| 500 |
} |
| 501 |
|
| 502 |
static struct aws_http_proxy_negotiator_forwarding_vtable s_forwarding_identity_proxy_negotiator_tunneling_vtable = { |
| 503 |
.forward_request_transform = s_forwarding_identity_connect_transform, |
| 504 |
}; |
| 505 |
|
| 506 |
static struct aws_http_proxy_negotiator *s_create_forwarding_identity_negotiator( |
| 507 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 508 |
struct aws_allocator *allocator) { |
| 509 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 510 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 511 |
return NULL; |
| 512 |
} |
| 513 |
|
| 514 |
struct aws_http_proxy_negotiator_forwarding_identity *identity_negotiator = |
| 515 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_forwarding_identity)); |
| 516 |
if (identity_negotiator == NULL) { |
| 517 |
return NULL; |
| 518 |
} |
| 519 |
|
| 520 |
identity_negotiator->allocator = allocator; |
| 521 |
identity_negotiator->negotiator_base.impl = identity_negotiator; |
| 522 |
aws_ref_count_init( |
| 523 |
&identity_negotiator->negotiator_base.ref_count, |
| 524 |
&identity_negotiator->negotiator_base, |
| 525 |
(aws_simple_completion_callback *)s_destroy_forwarding_identity_negotiator); |
| 526 |
|
| 527 |
identity_negotiator->negotiator_base.strategy_vtable.forwarding_vtable = |
| 528 |
&s_forwarding_identity_proxy_negotiator_tunneling_vtable; |
| 529 |
|
| 530 |
return &identity_negotiator->negotiator_base; |
| 531 |
} |
| 532 |
|
| 533 |
static struct aws_http_proxy_strategy_vtable s_forwarding_identity_strategy_vtable = { |
| 534 |
.create_negotiator = s_create_forwarding_identity_negotiator, |
| 535 |
}; |
| 536 |
|
| 537 |
static void s_destroy_forwarding_identity_strategy(void *user_data) { |
| 538 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 539 |
struct aws_http_proxy_strategy_forwarding_identity *identity_strategy = proxy_strategy->impl; |
| 540 |
|
| 541 |
aws_mem_release(identity_strategy->allocator, identity_strategy); |
| 542 |
} |
| 543 |
|
| 544 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_forwarding_identity(struct aws_allocator *allocator) { |
| 545 |
if (allocator == NULL) { |
| 546 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 547 |
return NULL; |
| 548 |
} |
| 549 |
|
| 550 |
struct aws_http_proxy_strategy_forwarding_identity *identity_strategy = |
| 551 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_forwarding_identity)); |
| 552 |
if (identity_strategy == NULL) { |
| 553 |
return NULL; |
| 554 |
} |
| 555 |
|
| 556 |
identity_strategy->strategy_base.impl = identity_strategy; |
| 557 |
identity_strategy->strategy_base.vtable = &s_forwarding_identity_strategy_vtable; |
| 558 |
identity_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_FORWARD; |
| 559 |
identity_strategy->allocator = allocator; |
| 560 |
|
| 561 |
aws_ref_count_init( |
| 562 |
&identity_strategy->strategy_base.ref_count, |
| 563 |
&identity_strategy->strategy_base, |
| 564 |
(aws_simple_completion_callback *)s_destroy_forwarding_identity_strategy); |
| 565 |
|
| 566 |
return &identity_strategy->strategy_base; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_kerberos_prefix, "Negotiate "); |
| 573 |
|
| 574 |
struct aws_http_proxy_strategy_tunneling_kerberos { |
| 575 |
struct aws_allocator *allocator; |
| 576 |
|
| 577 |
aws_http_proxy_negotiation_get_token_sync_fn *get_token; |
| 578 |
|
| 579 |
void *get_token_user_data; |
| 580 |
|
| 581 |
struct aws_http_proxy_strategy strategy_base; |
| 582 |
}; |
| 583 |
|
| 584 |
struct aws_http_proxy_negotiator_tunneling_kerberos { |
| 585 |
struct aws_allocator *allocator; |
| 586 |
|
| 587 |
struct aws_http_proxy_strategy *strategy; |
| 588 |
|
| 589 |
enum proxy_negotiator_connect_state connect_state; |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
struct aws_http_proxy_negotiator negotiator_base; |
| 599 |
}; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
static int s_add_kerberos_proxy_usertoken_authentication_header( |
| 606 |
struct aws_allocator *allocator, |
| 607 |
struct aws_http_message *request, |
| 608 |
struct aws_byte_cursor user_token) { |
| 609 |
|
| 610 |
struct aws_byte_buf header_value; |
| 611 |
AWS_ZERO_STRUCT(header_value); |
| 612 |
|
| 613 |
int result = AWS_OP_ERR; |
| 614 |
|
| 615 |
if (aws_byte_buf_init( |
| 616 |
&header_value, allocator, s_proxy_authorization_header_kerberos_prefix->len + user_token.len)) { |
| 617 |
goto done; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
struct aws_byte_cursor auth_header_cursor = |
| 622 |
aws_byte_cursor_from_string(s_proxy_authorization_header_kerberos_prefix); |
| 623 |
if (aws_byte_buf_append(&header_value, &auth_header_cursor)) { |
| 624 |
goto done; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
if (aws_byte_buf_append(&header_value, &user_token)) { |
| 629 |
goto done; |
| 630 |
} |
| 631 |
|
| 632 |
struct aws_http_header header = { |
| 633 |
.name = aws_byte_cursor_from_string(s_proxy_authorization_header_name), |
| 634 |
.value = aws_byte_cursor_from_array(header_value.buffer, header_value.len), |
| 635 |
}; |
| 636 |
|
| 637 |
if (aws_http_message_add_header(request, header)) { |
| 638 |
goto done; |
| 639 |
} |
| 640 |
|
| 641 |
result = AWS_OP_SUCCESS; |
| 642 |
|
| 643 |
done: |
| 644 |
|
| 645 |
aws_byte_buf_clean_up(&header_value); |
| 646 |
return result; |
| 647 |
} |
| 648 |
|
| 649 |
static void s_kerberos_tunnel_transform_connect( |
| 650 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 651 |
struct aws_http_message *message, |
| 652 |
aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback, |
| 653 |
aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback, |
| 654 |
void *internal_proxy_user_data) { |
| 655 |
struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl; |
| 656 |
struct aws_http_proxy_strategy_tunneling_kerberos *kerberos_strategy = kerberos_negotiator->strategy->impl; |
| 657 |
|
| 658 |
int result = AWS_OP_ERR; |
| 659 |
int error_code = AWS_ERROR_SUCCESS; |
| 660 |
struct aws_string *kerberos_token = NULL; |
| 661 |
|
| 662 |
if (kerberos_negotiator->connect_state == AWS_PNCS_FAILURE) { |
| 663 |
error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED; |
| 664 |
goto done; |
| 665 |
} |
| 666 |
|
| 667 |
if (kerberos_negotiator->connect_state != AWS_PNCS_READY) { |
| 668 |
error_code = AWS_ERROR_INVALID_STATE; |
| 669 |
goto done; |
| 670 |
} |
| 671 |
|
| 672 |
kerberos_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 673 |
|
| 674 |
kerberos_token = kerberos_strategy->get_token(kerberos_strategy->get_token_user_data, &error_code); |
| 675 |
if (kerberos_token == NULL || error_code != AWS_ERROR_SUCCESS) { |
| 676 |
goto done; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
if (s_add_kerberos_proxy_usertoken_authentication_header( |
| 681 |
kerberos_negotiator->allocator, message, aws_byte_cursor_from_string(kerberos_token))) { |
| 682 |
error_code = aws_last_error(); |
| 683 |
goto done; |
| 684 |
} |
| 685 |
|
| 686 |
kerberos_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 687 |
result = AWS_OP_SUCCESS; |
| 688 |
|
| 689 |
done: |
| 690 |
|
| 691 |
if (result != AWS_OP_SUCCESS) { |
| 692 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 693 |
error_code = AWS_ERROR_UNKNOWN; |
| 694 |
} |
| 695 |
negotiation_termination_callback(message, error_code, internal_proxy_user_data); |
| 696 |
} else { |
| 697 |
negotiation_http_request_forward_callback(message, internal_proxy_user_data); |
| 698 |
} |
| 699 |
|
| 700 |
aws_string_destroy(kerberos_token); |
| 701 |
} |
| 702 |
|
| 703 |
static int s_kerberos_on_incoming_header_adaptive( |
| 704 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 705 |
enum aws_http_header_block header_block, |
| 706 |
const struct aws_http_header *header_array, |
| 707 |
size_t num_headers) { |
| 708 |
|
| 709 |
struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl; |
| 710 |
(void)kerberos_negotiator; |
| 711 |
(void)header_block; |
| 712 |
(void)header_array; |
| 713 |
(void)num_headers; |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
return AWS_OP_SUCCESS; |
| 718 |
} |
| 719 |
|
| 720 |
static int s_kerberos_on_connect_status( |
| 721 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 722 |
enum aws_http_status_code status_code) { |
| 723 |
|
| 724 |
struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl; |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
if (kerberos_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) { |
| 729 |
if (AWS_HTTP_STATUS_CODE_200_OK != status_code) { |
| 730 |
kerberos_negotiator->connect_state = AWS_PNCS_FAILURE; |
| 731 |
} else { |
| 732 |
kerberos_negotiator->connect_state = AWS_PNCS_SUCCESS; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
return AWS_OP_SUCCESS; |
| 737 |
} |
| 738 |
|
| 739 |
static int s_kerberos_on_incoming_body( |
| 740 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 741 |
const struct aws_byte_cursor *data) { |
| 742 |
|
| 743 |
struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl; |
| 744 |
(void)kerberos_negotiator; |
| 745 |
(void)data; |
| 746 |
|
| 747 |
return AWS_OP_SUCCESS; |
| 748 |
} |
| 749 |
|
| 750 |
static struct aws_http_proxy_negotiator_tunnelling_vtable s_tunneling_kerberos_proxy_negotiator_tunneling_vtable = { |
| 751 |
.on_incoming_body_callback = s_kerberos_on_incoming_body, |
| 752 |
.on_incoming_headers_callback = s_kerberos_on_incoming_header_adaptive, |
| 753 |
.on_status_callback = s_kerberos_on_connect_status, |
| 754 |
.connect_request_transform = s_kerberos_tunnel_transform_connect, |
| 755 |
}; |
| 756 |
|
| 757 |
static void s_destroy_tunneling_kerberos_negotiator(void *user_data) { |
| 758 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 759 |
struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl; |
| 760 |
|
| 761 |
aws_http_proxy_strategy_release(kerberos_negotiator->strategy); |
| 762 |
|
| 763 |
aws_mem_release(kerberos_negotiator->allocator, kerberos_negotiator); |
| 764 |
} |
| 765 |
|
| 766 |
static struct aws_http_proxy_negotiator *s_create_tunneling_kerberos_negotiator( |
| 767 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 768 |
struct aws_allocator *allocator) { |
| 769 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 770 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
|
| 774 |
struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = |
| 775 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_kerberos)); |
| 776 |
if (kerberos_negotiator == NULL) { |
| 777 |
return NULL; |
| 778 |
} |
| 779 |
|
| 780 |
kerberos_negotiator->allocator = allocator; |
| 781 |
kerberos_negotiator->negotiator_base.impl = kerberos_negotiator; |
| 782 |
aws_ref_count_init( |
| 783 |
&kerberos_negotiator->negotiator_base.ref_count, |
| 784 |
&kerberos_negotiator->negotiator_base, |
| 785 |
(aws_simple_completion_callback *)s_destroy_tunneling_kerberos_negotiator); |
| 786 |
|
| 787 |
kerberos_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable = |
| 788 |
&s_tunneling_kerberos_proxy_negotiator_tunneling_vtable; |
| 789 |
|
| 790 |
kerberos_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy); |
| 791 |
|
| 792 |
return &kerberos_negotiator->negotiator_base; |
| 793 |
} |
| 794 |
|
| 795 |
static struct aws_http_proxy_strategy_vtable s_tunneling_kerberos_strategy_vtable = { |
| 796 |
.create_negotiator = s_create_tunneling_kerberos_negotiator, |
| 797 |
}; |
| 798 |
|
| 799 |
static void s_destroy_tunneling_kerberos_strategy(void *user_data) { |
| 800 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 801 |
struct aws_http_proxy_strategy_tunneling_kerberos *kerberos_strategy = proxy_strategy->impl; |
| 802 |
|
| 803 |
aws_mem_release(kerberos_strategy->allocator, kerberos_strategy); |
| 804 |
} |
| 805 |
|
| 806 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_kerberos( |
| 807 |
struct aws_allocator *allocator, |
| 808 |
struct aws_http_proxy_strategy_tunneling_kerberos_options *config) { |
| 809 |
|
| 810 |
if (allocator == NULL || config == NULL || config->get_token == NULL) { |
| 811 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 812 |
return NULL; |
| 813 |
} |
| 814 |
|
| 815 |
struct aws_http_proxy_strategy_tunneling_kerberos *kerberos_strategy = |
| 816 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_kerberos)); |
| 817 |
if (kerberos_strategy == NULL) { |
| 818 |
return NULL; |
| 819 |
} |
| 820 |
|
| 821 |
kerberos_strategy->strategy_base.impl = kerberos_strategy; |
| 822 |
kerberos_strategy->strategy_base.vtable = &s_tunneling_kerberos_strategy_vtable; |
| 823 |
kerberos_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL; |
| 824 |
kerberos_strategy->allocator = allocator; |
| 825 |
|
| 826 |
aws_ref_count_init( |
| 827 |
&kerberos_strategy->strategy_base.ref_count, |
| 828 |
&kerberos_strategy->strategy_base, |
| 829 |
(aws_simple_completion_callback *)s_destroy_tunneling_kerberos_strategy); |
| 830 |
|
| 831 |
kerberos_strategy->get_token = config->get_token; |
| 832 |
kerberos_strategy->get_token_user_data = config->get_token_user_data; |
| 833 |
|
| 834 |
return &kerberos_strategy->strategy_base; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
struct aws_http_proxy_strategy_tunneling_ntlm { |
| 840 |
struct aws_allocator *allocator; |
| 841 |
|
| 842 |
aws_http_proxy_negotiation_get_token_sync_fn *get_token; |
| 843 |
|
| 844 |
aws_http_proxy_negotiation_get_challenge_token_sync_fn *get_challenge_token; |
| 845 |
|
| 846 |
void *get_challenge_token_user_data; |
| 847 |
|
| 848 |
struct aws_http_proxy_strategy strategy_base; |
| 849 |
}; |
| 850 |
|
| 851 |
struct aws_http_proxy_negotiator_tunneling_ntlm { |
| 852 |
struct aws_allocator *allocator; |
| 853 |
|
| 854 |
struct aws_http_proxy_strategy *strategy; |
| 855 |
|
| 856 |
enum proxy_negotiator_connect_state connect_state; |
| 857 |
|
| 858 |
struct aws_string *challenge_token; |
| 859 |
|
| 860 |
struct aws_http_proxy_negotiator negotiator_base; |
| 861 |
}; |
| 862 |
|
| 863 |
AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_ntlm_prefix, "NTLM "); |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
static int s_add_ntlm_proxy_usertoken_authentication_header( |
| 869 |
struct aws_allocator *allocator, |
| 870 |
struct aws_http_message *request, |
| 871 |
struct aws_byte_cursor credential_response) { |
| 872 |
|
| 873 |
struct aws_byte_buf header_value; |
| 874 |
AWS_ZERO_STRUCT(header_value); |
| 875 |
|
| 876 |
int result = AWS_OP_ERR; |
| 877 |
|
| 878 |
if (aws_byte_buf_init( |
| 879 |
&header_value, allocator, s_proxy_authorization_header_ntlm_prefix->len + credential_response.len)) { |
| 880 |
goto done; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
struct aws_byte_cursor auth_header_cursor = aws_byte_cursor_from_string(s_proxy_authorization_header_ntlm_prefix); |
| 885 |
if (aws_byte_buf_append(&header_value, &auth_header_cursor)) { |
| 886 |
goto done; |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
if (aws_byte_buf_append(&header_value, &credential_response)) { |
| 891 |
goto done; |
| 892 |
} |
| 893 |
|
| 894 |
struct aws_http_header header = { |
| 895 |
.name = aws_byte_cursor_from_string(s_proxy_authorization_header_name), |
| 896 |
.value = aws_byte_cursor_from_array(header_value.buffer, header_value.len), |
| 897 |
}; |
| 898 |
|
| 899 |
if (aws_http_message_add_header(request, header)) { |
| 900 |
goto done; |
| 901 |
} |
| 902 |
|
| 903 |
result = AWS_OP_SUCCESS; |
| 904 |
|
| 905 |
done: |
| 906 |
|
| 907 |
aws_byte_buf_clean_up(&header_value); |
| 908 |
return result; |
| 909 |
} |
| 910 |
|
| 911 |
static void s_ntlm_tunnel_transform_connect( |
| 912 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 913 |
struct aws_http_message *message, |
| 914 |
aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback, |
| 915 |
aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback, |
| 916 |
void *internal_proxy_user_data) { |
| 917 |
|
| 918 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl; |
| 919 |
struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_strategy = ntlm_negotiator->strategy->impl; |
| 920 |
|
| 921 |
int result = AWS_OP_ERR; |
| 922 |
int error_code = AWS_ERROR_SUCCESS; |
| 923 |
struct aws_string *challenge_answer_token = NULL; |
| 924 |
struct aws_byte_cursor challenge_token; |
| 925 |
AWS_ZERO_STRUCT(challenge_token); |
| 926 |
|
| 927 |
if (ntlm_negotiator->connect_state == AWS_PNCS_FAILURE) { |
| 928 |
error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED; |
| 929 |
goto done; |
| 930 |
} |
| 931 |
|
| 932 |
if (ntlm_negotiator->connect_state != AWS_PNCS_READY) { |
| 933 |
error_code = AWS_ERROR_INVALID_STATE; |
| 934 |
goto done; |
| 935 |
} |
| 936 |
|
| 937 |
if (ntlm_negotiator->challenge_token == NULL) { |
| 938 |
error_code = AWS_ERROR_HTTP_PROXY_STRATEGY_NTLM_CHALLENGE_TOKEN_MISSING; |
| 939 |
goto done; |
| 940 |
} |
| 941 |
|
| 942 |
ntlm_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 943 |
challenge_token = aws_byte_cursor_from_string(ntlm_negotiator->challenge_token); |
| 944 |
challenge_answer_token = |
| 945 |
ntlm_strategy->get_challenge_token(ntlm_strategy->get_challenge_token_user_data, &challenge_token, &error_code); |
| 946 |
|
| 947 |
if (challenge_answer_token == NULL || error_code != AWS_ERROR_SUCCESS) { |
| 948 |
goto done; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
if (s_add_ntlm_proxy_usertoken_authentication_header( |
| 953 |
ntlm_negotiator->allocator, message, aws_byte_cursor_from_string(challenge_answer_token))) { |
| 954 |
error_code = aws_last_error(); |
| 955 |
goto done; |
| 956 |
} |
| 957 |
|
| 958 |
ntlm_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 959 |
result = AWS_OP_SUCCESS; |
| 960 |
|
| 961 |
done: |
| 962 |
|
| 963 |
if (result != AWS_OP_SUCCESS) { |
| 964 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 965 |
error_code = AWS_ERROR_UNKNOWN; |
| 966 |
} |
| 967 |
negotiation_termination_callback(message, error_code, internal_proxy_user_data); |
| 968 |
} else { |
| 969 |
negotiation_http_request_forward_callback(message, internal_proxy_user_data); |
| 970 |
} |
| 971 |
|
| 972 |
aws_string_destroy(challenge_answer_token); |
| 973 |
} |
| 974 |
|
| 975 |
AWS_STATIC_STRING_FROM_LITERAL(s_ntlm_challenge_token_header, "Proxy-Authenticate"); |
| 976 |
|
| 977 |
static int s_ntlm_on_incoming_header_adaptive( |
| 978 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 979 |
enum aws_http_header_block header_block, |
| 980 |
const struct aws_http_header *header_array, |
| 981 |
size_t num_headers) { |
| 982 |
|
| 983 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl; |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
if (ntlm_negotiator->connect_state == AWS_PNCS_READY) { |
| 992 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 993 |
struct aws_byte_cursor proxy_authenticate_header_name = |
| 994 |
aws_byte_cursor_from_string(s_ntlm_challenge_token_header); |
| 995 |
for (size_t i = 0; i < num_headers; ++i) { |
| 996 |
struct aws_byte_cursor header_name_cursor = header_array[i].name; |
| 997 |
if (aws_byte_cursor_eq_ignore_case(&proxy_authenticate_header_name, &header_name_cursor)) { |
| 998 |
aws_string_destroy(ntlm_negotiator->challenge_token); |
| 999 |
|
| 1000 |
struct aws_byte_cursor challenge_value_cursor = header_array[i].value; |
| 1001 |
ntlm_negotiator->challenge_token = |
| 1002 |
aws_string_new_from_cursor(ntlm_negotiator->allocator, &challenge_value_cursor); |
| 1003 |
break; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
return AWS_OP_SUCCESS; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static int s_ntlm_on_connect_status( |
| 1013 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1014 |
enum aws_http_status_code status_code) { |
| 1015 |
|
| 1016 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl; |
| 1017 |
|
| 1018 |
if (ntlm_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) { |
| 1019 |
if (AWS_HTTP_STATUS_CODE_200_OK != status_code) { |
| 1020 |
ntlm_negotiator->connect_state = AWS_PNCS_FAILURE; |
| 1021 |
} else { |
| 1022 |
ntlm_negotiator->connect_state = AWS_PNCS_SUCCESS; |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
return AWS_OP_SUCCESS; |
| 1027 |
} |
| 1028 |
|
| 1029 |
static int s_ntlm_on_incoming_body( |
| 1030 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1031 |
const struct aws_byte_cursor *data) { |
| 1032 |
|
| 1033 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl; |
| 1034 |
(void)ntlm_negotiator; |
| 1035 |
(void)data; |
| 1036 |
|
| 1037 |
return AWS_OP_SUCCESS; |
| 1038 |
} |
| 1039 |
|
| 1040 |
static enum aws_http_proxy_negotiation_retry_directive s_ntlm_tunnel_get_retry_directive( |
| 1041 |
struct aws_http_proxy_negotiator *proxy_negotiator) { |
| 1042 |
(void)proxy_negotiator; |
| 1043 |
|
| 1044 |
return AWS_HPNRD_CURRENT_CONNECTION; |
| 1045 |
} |
| 1046 |
|
| 1047 |
static struct aws_http_proxy_negotiator_tunnelling_vtable s_tunneling_ntlm_proxy_negotiator_tunneling_vtable = { |
| 1048 |
.on_incoming_body_callback = s_ntlm_on_incoming_body, |
| 1049 |
.on_incoming_headers_callback = s_ntlm_on_incoming_header_adaptive, |
| 1050 |
.on_status_callback = s_ntlm_on_connect_status, |
| 1051 |
.connect_request_transform = s_ntlm_tunnel_transform_connect, |
| 1052 |
.get_retry_directive = s_ntlm_tunnel_get_retry_directive, |
| 1053 |
}; |
| 1054 |
|
| 1055 |
static void s_destroy_tunneling_ntlm_negotiator(void *user_data) { |
| 1056 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 1057 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl; |
| 1058 |
|
| 1059 |
aws_string_destroy(ntlm_negotiator->challenge_token); |
| 1060 |
aws_http_proxy_strategy_release(ntlm_negotiator->strategy); |
| 1061 |
|
| 1062 |
aws_mem_release(ntlm_negotiator->allocator, ntlm_negotiator); |
| 1063 |
} |
| 1064 |
|
| 1065 |
static struct aws_http_proxy_negotiator *s_create_tunneling_ntlm_negotiator( |
| 1066 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 1067 |
struct aws_allocator *allocator) { |
| 1068 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 1069 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1070 |
return NULL; |
| 1071 |
} |
| 1072 |
|
| 1073 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = |
| 1074 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_ntlm)); |
| 1075 |
if (ntlm_negotiator == NULL) { |
| 1076 |
return NULL; |
| 1077 |
} |
| 1078 |
|
| 1079 |
ntlm_negotiator->allocator = allocator; |
| 1080 |
ntlm_negotiator->negotiator_base.impl = ntlm_negotiator; |
| 1081 |
aws_ref_count_init( |
| 1082 |
&ntlm_negotiator->negotiator_base.ref_count, |
| 1083 |
&ntlm_negotiator->negotiator_base, |
| 1084 |
(aws_simple_completion_callback *)s_destroy_tunneling_ntlm_negotiator); |
| 1085 |
|
| 1086 |
ntlm_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable = |
| 1087 |
&s_tunneling_ntlm_proxy_negotiator_tunneling_vtable; |
| 1088 |
|
| 1089 |
ntlm_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy); |
| 1090 |
|
| 1091 |
return &ntlm_negotiator->negotiator_base; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static struct aws_http_proxy_strategy_vtable s_tunneling_ntlm_strategy_vtable = { |
| 1095 |
.create_negotiator = s_create_tunneling_ntlm_negotiator, |
| 1096 |
}; |
| 1097 |
|
| 1098 |
static void s_destroy_tunneling_ntlm_strategy(void *user_data) { |
| 1099 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 1100 |
struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_strategy = proxy_strategy->impl; |
| 1101 |
|
| 1102 |
aws_mem_release(ntlm_strategy->allocator, ntlm_strategy); |
| 1103 |
} |
| 1104 |
|
| 1105 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_ntlm( |
| 1106 |
struct aws_allocator *allocator, |
| 1107 |
struct aws_http_proxy_strategy_tunneling_ntlm_options *config) { |
| 1108 |
|
| 1109 |
if (allocator == NULL || config == NULL || config->get_challenge_token == NULL) { |
| 1110 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1111 |
return NULL; |
| 1112 |
} |
| 1113 |
|
| 1114 |
struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_strategy = |
| 1115 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_ntlm)); |
| 1116 |
if (ntlm_strategy == NULL) { |
| 1117 |
return NULL; |
| 1118 |
} |
| 1119 |
|
| 1120 |
ntlm_strategy->strategy_base.impl = ntlm_strategy; |
| 1121 |
ntlm_strategy->strategy_base.vtable = &s_tunneling_ntlm_strategy_vtable; |
| 1122 |
ntlm_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL; |
| 1123 |
|
| 1124 |
ntlm_strategy->allocator = allocator; |
| 1125 |
|
| 1126 |
aws_ref_count_init( |
| 1127 |
&ntlm_strategy->strategy_base.ref_count, |
| 1128 |
&ntlm_strategy->strategy_base, |
| 1129 |
(aws_simple_completion_callback *)s_destroy_tunneling_ntlm_strategy); |
| 1130 |
|
| 1131 |
ntlm_strategy->get_challenge_token = config->get_challenge_token; |
| 1132 |
ntlm_strategy->get_challenge_token_user_data = config->get_challenge_token_user_data; |
| 1133 |
|
| 1134 |
return &ntlm_strategy->strategy_base; |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
static void s_ntlm_credential_tunnel_transform_connect( |
| 1139 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1140 |
struct aws_http_message *message, |
| 1141 |
aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback, |
| 1142 |
aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback, |
| 1143 |
void *internal_proxy_user_data) { |
| 1144 |
|
| 1145 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_credential_negotiator = proxy_negotiator->impl; |
| 1146 |
struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_credential_strategy = |
| 1147 |
ntlm_credential_negotiator->strategy->impl; |
| 1148 |
|
| 1149 |
int result = AWS_OP_ERR; |
| 1150 |
int error_code = AWS_ERROR_SUCCESS; |
| 1151 |
struct aws_string *token = NULL; |
| 1152 |
|
| 1153 |
if (ntlm_credential_negotiator->connect_state == AWS_PNCS_FAILURE) { |
| 1154 |
error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED; |
| 1155 |
goto done; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (ntlm_credential_negotiator->connect_state != AWS_PNCS_READY) { |
| 1159 |
error_code = AWS_ERROR_INVALID_STATE; |
| 1160 |
goto done; |
| 1161 |
} |
| 1162 |
|
| 1163 |
ntlm_credential_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 1164 |
token = ntlm_credential_strategy->get_token(ntlm_credential_strategy->get_challenge_token_user_data, &error_code); |
| 1165 |
|
| 1166 |
if (token == NULL || error_code != AWS_ERROR_SUCCESS) { |
| 1167 |
goto done; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
if (s_add_ntlm_proxy_usertoken_authentication_header( |
| 1172 |
ntlm_credential_negotiator->allocator, message, aws_byte_cursor_from_string(token))) { |
| 1173 |
error_code = aws_last_error(); |
| 1174 |
goto done; |
| 1175 |
} |
| 1176 |
|
| 1177 |
ntlm_credential_negotiator->connect_state = AWS_PNCS_IN_PROGRESS; |
| 1178 |
result = AWS_OP_SUCCESS; |
| 1179 |
|
| 1180 |
done: |
| 1181 |
|
| 1182 |
if (result != AWS_OP_SUCCESS) { |
| 1183 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 1184 |
error_code = AWS_ERROR_UNKNOWN; |
| 1185 |
} |
| 1186 |
negotiation_termination_callback(message, error_code, internal_proxy_user_data); |
| 1187 |
} else { |
| 1188 |
negotiation_http_request_forward_callback(message, internal_proxy_user_data); |
| 1189 |
} |
| 1190 |
|
| 1191 |
aws_string_destroy(token); |
| 1192 |
} |
| 1193 |
|
| 1194 |
static struct aws_http_proxy_negotiator_tunnelling_vtable |
| 1195 |
s_tunneling_ntlm_proxy_credential_negotiator_tunneling_vtable = { |
| 1196 |
.on_incoming_body_callback = s_ntlm_on_incoming_body, |
| 1197 |
.on_incoming_headers_callback = s_ntlm_on_incoming_header_adaptive, |
| 1198 |
.on_status_callback = s_ntlm_on_connect_status, |
| 1199 |
.connect_request_transform = s_ntlm_credential_tunnel_transform_connect, |
| 1200 |
}; |
| 1201 |
|
| 1202 |
static void s_destroy_tunneling_ntlm_credential_negotiator(void *user_data) { |
| 1203 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 1204 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_credential_negotiator = proxy_negotiator->impl; |
| 1205 |
|
| 1206 |
aws_string_destroy(ntlm_credential_negotiator->challenge_token); |
| 1207 |
aws_http_proxy_strategy_release(ntlm_credential_negotiator->strategy); |
| 1208 |
|
| 1209 |
aws_mem_release(ntlm_credential_negotiator->allocator, ntlm_credential_negotiator); |
| 1210 |
} |
| 1211 |
|
| 1212 |
static struct aws_http_proxy_negotiator *s_create_tunneling_ntlm_credential_negotiator( |
| 1213 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 1214 |
struct aws_allocator *allocator) { |
| 1215 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 1216 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1217 |
return NULL; |
| 1218 |
} |
| 1219 |
|
| 1220 |
struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_credential_negotiator = |
| 1221 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_ntlm)); |
| 1222 |
if (ntlm_credential_negotiator == NULL) { |
| 1223 |
return NULL; |
| 1224 |
} |
| 1225 |
|
| 1226 |
ntlm_credential_negotiator->allocator = allocator; |
| 1227 |
ntlm_credential_negotiator->negotiator_base.impl = ntlm_credential_negotiator; |
| 1228 |
aws_ref_count_init( |
| 1229 |
&ntlm_credential_negotiator->negotiator_base.ref_count, |
| 1230 |
&ntlm_credential_negotiator->negotiator_base, |
| 1231 |
(aws_simple_completion_callback *)s_destroy_tunneling_ntlm_credential_negotiator); |
| 1232 |
|
| 1233 |
ntlm_credential_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable = |
| 1234 |
&s_tunneling_ntlm_proxy_credential_negotiator_tunneling_vtable; |
| 1235 |
|
| 1236 |
ntlm_credential_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy); |
| 1237 |
|
| 1238 |
return &ntlm_credential_negotiator->negotiator_base; |
| 1239 |
} |
| 1240 |
|
| 1241 |
static struct aws_http_proxy_strategy_vtable s_tunneling_ntlm_credential_strategy_vtable = { |
| 1242 |
.create_negotiator = s_create_tunneling_ntlm_credential_negotiator, |
| 1243 |
}; |
| 1244 |
|
| 1245 |
static void s_destroy_tunneling_ntlm_credential_strategy(void *user_data) { |
| 1246 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 1247 |
struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_credential_strategy = proxy_strategy->impl; |
| 1248 |
|
| 1249 |
aws_mem_release(ntlm_credential_strategy->allocator, ntlm_credential_strategy); |
| 1250 |
} |
| 1251 |
|
| 1252 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_ntlm_credential( |
| 1253 |
struct aws_allocator *allocator, |
| 1254 |
struct aws_http_proxy_strategy_tunneling_ntlm_options *config) { |
| 1255 |
|
| 1256 |
if (allocator == NULL || config == NULL || config->get_token == NULL) { |
| 1257 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1258 |
return NULL; |
| 1259 |
} |
| 1260 |
|
| 1261 |
struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_credential_strategy = |
| 1262 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_ntlm)); |
| 1263 |
if (ntlm_credential_strategy == NULL) { |
| 1264 |
return NULL; |
| 1265 |
} |
| 1266 |
|
| 1267 |
ntlm_credential_strategy->strategy_base.impl = ntlm_credential_strategy; |
| 1268 |
ntlm_credential_strategy->strategy_base.vtable = &s_tunneling_ntlm_credential_strategy_vtable; |
| 1269 |
ntlm_credential_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL; |
| 1270 |
|
| 1271 |
ntlm_credential_strategy->allocator = allocator; |
| 1272 |
|
| 1273 |
aws_ref_count_init( |
| 1274 |
&ntlm_credential_strategy->strategy_base.ref_count, |
| 1275 |
&ntlm_credential_strategy->strategy_base, |
| 1276 |
(aws_simple_completion_callback *)s_destroy_tunneling_ntlm_credential_strategy); |
| 1277 |
|
| 1278 |
ntlm_credential_strategy->get_token = config->get_token; |
| 1279 |
ntlm_credential_strategy->get_challenge_token_user_data = config->get_challenge_token_user_data; |
| 1280 |
|
| 1281 |
return &ntlm_credential_strategy->strategy_base; |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
#define PROXY_STRATEGY_MAX_ADAPTIVE_STRATEGIES 4 |
| 1287 |
|
| 1288 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_adaptive( |
| 1289 |
struct aws_allocator *allocator, |
| 1290 |
struct aws_http_proxy_strategy_tunneling_adaptive_options *config) { |
| 1291 |
|
| 1292 |
if (allocator == NULL || config == NULL) { |
| 1293 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1294 |
return NULL; |
| 1295 |
} |
| 1296 |
|
| 1297 |
struct aws_http_proxy_strategy *strategies[PROXY_STRATEGY_MAX_ADAPTIVE_STRATEGIES]; |
| 1298 |
|
| 1299 |
uint32_t strategy_count = 0; |
| 1300 |
struct aws_http_proxy_strategy *identity_strategy = NULL; |
| 1301 |
struct aws_http_proxy_strategy *kerberos_strategy = NULL; |
| 1302 |
struct aws_http_proxy_strategy *ntlm_credential_strategy = NULL; |
| 1303 |
struct aws_http_proxy_strategy *ntlm_strategy = NULL; |
| 1304 |
struct aws_http_proxy_strategy *adaptive_sequence_strategy = NULL; |
| 1305 |
|
| 1306 |
identity_strategy = aws_http_proxy_strategy_new_tunneling_one_time_identity(allocator); |
| 1307 |
if (identity_strategy == NULL) { |
| 1308 |
goto done; |
| 1309 |
} |
| 1310 |
strategies[strategy_count++] = identity_strategy; |
| 1311 |
|
| 1312 |
if (config->kerberos_options != NULL) { |
| 1313 |
kerberos_strategy = aws_http_proxy_strategy_new_tunneling_kerberos(allocator, config->kerberos_options); |
| 1314 |
if (kerberos_strategy == NULL) { |
| 1315 |
goto done; |
| 1316 |
} |
| 1317 |
|
| 1318 |
strategies[strategy_count++] = kerberos_strategy; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (config->ntlm_options != NULL) { |
| 1322 |
ntlm_credential_strategy = |
| 1323 |
aws_http_proxy_strategy_new_tunneling_ntlm_credential(allocator, config->ntlm_options); |
| 1324 |
if (ntlm_credential_strategy == NULL) { |
| 1325 |
goto done; |
| 1326 |
} |
| 1327 |
|
| 1328 |
strategies[strategy_count++] = ntlm_credential_strategy; |
| 1329 |
|
| 1330 |
ntlm_strategy = aws_http_proxy_strategy_new_tunneling_ntlm(allocator, config->ntlm_options); |
| 1331 |
if (ntlm_strategy == NULL) { |
| 1332 |
goto done; |
| 1333 |
} |
| 1334 |
|
| 1335 |
strategies[strategy_count++] = ntlm_strategy; |
| 1336 |
} |
| 1337 |
|
| 1338 |
AWS_FATAL_ASSERT(strategy_count <= PROXY_STRATEGY_MAX_ADAPTIVE_STRATEGIES); |
| 1339 |
|
| 1340 |
struct aws_http_proxy_strategy_tunneling_sequence_options sequence_config = { |
| 1341 |
.strategies = strategies, |
| 1342 |
.strategy_count = strategy_count, |
| 1343 |
}; |
| 1344 |
|
| 1345 |
adaptive_sequence_strategy = aws_http_proxy_strategy_new_tunneling_sequence(allocator, &sequence_config); |
| 1346 |
if (adaptive_sequence_strategy == NULL) { |
| 1347 |
goto done; |
| 1348 |
} |
| 1349 |
|
| 1350 |
done: |
| 1351 |
|
| 1352 |
aws_http_proxy_strategy_release(identity_strategy); |
| 1353 |
aws_http_proxy_strategy_release(kerberos_strategy); |
| 1354 |
aws_http_proxy_strategy_release(ntlm_credential_strategy); |
| 1355 |
aws_http_proxy_strategy_release(ntlm_strategy); |
| 1356 |
|
| 1357 |
return adaptive_sequence_strategy; |
| 1358 |
} |
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
struct aws_http_proxy_strategy_tunneling_sequence { |
| 1363 |
struct aws_allocator *allocator; |
| 1364 |
|
| 1365 |
struct aws_array_list strategies; |
| 1366 |
|
| 1367 |
struct aws_http_proxy_strategy strategy_base; |
| 1368 |
}; |
| 1369 |
|
| 1370 |
struct aws_http_proxy_negotiator_tunneling_sequence { |
| 1371 |
struct aws_allocator *allocator; |
| 1372 |
|
| 1373 |
struct aws_array_list negotiators; |
| 1374 |
size_t current_negotiator_transform_index; |
| 1375 |
void *original_internal_proxy_user_data; |
| 1376 |
aws_http_proxy_negotiation_terminate_fn *original_negotiation_termination_callback; |
| 1377 |
aws_http_proxy_negotiation_http_request_forward_fn *original_negotiation_http_request_forward_callback; |
| 1378 |
|
| 1379 |
struct aws_http_proxy_negotiator negotiator_base; |
| 1380 |
}; |
| 1381 |
|
| 1382 |
static void s_sequence_tunnel_iteration_termination_callback( |
| 1383 |
struct aws_http_message *message, |
| 1384 |
int error_code, |
| 1385 |
void *user_data) { |
| 1386 |
|
| 1387 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 1388 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1389 |
|
| 1390 |
AWS_LOGF_WARN( |
| 1391 |
AWS_LS_HTTP_PROXY_NEGOTIATION, |
| 1392 |
"(id=%p) Proxy negotiation step failed with error %d", |
| 1393 |
(void *)proxy_negotiator, |
| 1394 |
error_code); |
| 1395 |
|
| 1396 |
int connection_error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED_RETRYABLE; |
| 1397 |
if (sequence_negotiator->current_negotiator_transform_index >= |
| 1398 |
aws_array_list_length(&sequence_negotiator->negotiators)) { |
| 1399 |
connection_error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED; |
| 1400 |
} |
| 1401 |
|
| 1402 |
sequence_negotiator->original_negotiation_termination_callback( |
| 1403 |
message, connection_error_code, sequence_negotiator->original_internal_proxy_user_data); |
| 1404 |
} |
| 1405 |
|
| 1406 |
static void s_sequence_tunnel_iteration_forward_callback(struct aws_http_message *message, void *user_data) { |
| 1407 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 1408 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1409 |
|
| 1410 |
sequence_negotiator->original_negotiation_http_request_forward_callback( |
| 1411 |
message, sequence_negotiator->original_internal_proxy_user_data); |
| 1412 |
} |
| 1413 |
|
| 1414 |
static void s_sequence_tunnel_try_next_negotiator( |
| 1415 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1416 |
struct aws_http_message *message) { |
| 1417 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1418 |
|
| 1419 |
size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators); |
| 1420 |
if (sequence_negotiator->current_negotiator_transform_index >= negotiator_count) { |
| 1421 |
goto on_error; |
| 1422 |
} |
| 1423 |
|
| 1424 |
struct aws_http_proxy_negotiator *current_negotiator = NULL; |
| 1425 |
if (aws_array_list_get_at( |
| 1426 |
&sequence_negotiator->negotiators, |
| 1427 |
¤t_negotiator, |
| 1428 |
sequence_negotiator->current_negotiator_transform_index++)) { |
| 1429 |
goto on_error; |
| 1430 |
} |
| 1431 |
|
| 1432 |
current_negotiator->strategy_vtable.tunnelling_vtable->connect_request_transform( |
| 1433 |
current_negotiator, |
| 1434 |
message, |
| 1435 |
s_sequence_tunnel_iteration_termination_callback, |
| 1436 |
s_sequence_tunnel_iteration_forward_callback, |
| 1437 |
proxy_negotiator); |
| 1438 |
|
| 1439 |
return; |
| 1440 |
|
| 1441 |
on_error: |
| 1442 |
|
| 1443 |
sequence_negotiator->original_negotiation_termination_callback( |
| 1444 |
message, AWS_ERROR_HTTP_PROXY_CONNECT_FAILED, sequence_negotiator->original_internal_proxy_user_data); |
| 1445 |
} |
| 1446 |
|
| 1447 |
static void s_sequence_tunnel_transform_connect( |
| 1448 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1449 |
struct aws_http_message *message, |
| 1450 |
aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback, |
| 1451 |
aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback, |
| 1452 |
void *internal_proxy_user_data) { |
| 1453 |
|
| 1454 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1455 |
|
| 1456 |
sequence_negotiator->original_internal_proxy_user_data = internal_proxy_user_data; |
| 1457 |
sequence_negotiator->original_negotiation_termination_callback = negotiation_termination_callback; |
| 1458 |
sequence_negotiator->original_negotiation_http_request_forward_callback = negotiation_http_request_forward_callback; |
| 1459 |
|
| 1460 |
s_sequence_tunnel_try_next_negotiator(proxy_negotiator, message); |
| 1461 |
} |
| 1462 |
|
| 1463 |
static int s_sequence_on_incoming_headers( |
| 1464 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1465 |
enum aws_http_header_block header_block, |
| 1466 |
const struct aws_http_header *header_array, |
| 1467 |
size_t num_headers) { |
| 1468 |
|
| 1469 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1470 |
size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators); |
| 1471 |
for (size_t i = 0; i < negotiator_count; ++i) { |
| 1472 |
struct aws_http_proxy_negotiator *negotiator = NULL; |
| 1473 |
if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) { |
| 1474 |
continue; |
| 1475 |
} |
| 1476 |
|
| 1477 |
aws_http_proxy_negotiation_connect_on_incoming_headers_fn *on_incoming_headers = |
| 1478 |
negotiator->strategy_vtable.tunnelling_vtable->on_incoming_headers_callback; |
| 1479 |
if (on_incoming_headers != NULL) { |
| 1480 |
(*on_incoming_headers)(negotiator, header_block, header_array, num_headers); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
return AWS_OP_SUCCESS; |
| 1485 |
} |
| 1486 |
|
| 1487 |
static int s_sequence_on_connect_status( |
| 1488 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1489 |
enum aws_http_status_code status_code) { |
| 1490 |
|
| 1491 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1492 |
size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators); |
| 1493 |
for (size_t i = 0; i < negotiator_count; ++i) { |
| 1494 |
struct aws_http_proxy_negotiator *negotiator = NULL; |
| 1495 |
if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) { |
| 1496 |
continue; |
| 1497 |
} |
| 1498 |
|
| 1499 |
aws_http_proxy_negotiator_connect_status_fn *on_status = |
| 1500 |
negotiator->strategy_vtable.tunnelling_vtable->on_status_callback; |
| 1501 |
if (on_status != NULL) { |
| 1502 |
(*on_status)(negotiator, status_code); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
return AWS_OP_SUCCESS; |
| 1507 |
} |
| 1508 |
|
| 1509 |
static int s_sequence_on_incoming_body( |
| 1510 |
struct aws_http_proxy_negotiator *proxy_negotiator, |
| 1511 |
const struct aws_byte_cursor *data) { |
| 1512 |
|
| 1513 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1514 |
size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators); |
| 1515 |
for (size_t i = 0; i < negotiator_count; ++i) { |
| 1516 |
struct aws_http_proxy_negotiator *negotiator = NULL; |
| 1517 |
if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) { |
| 1518 |
continue; |
| 1519 |
} |
| 1520 |
|
| 1521 |
aws_http_proxy_negotiator_connect_on_incoming_body_fn *on_incoming_body = |
| 1522 |
negotiator->strategy_vtable.tunnelling_vtable->on_incoming_body_callback; |
| 1523 |
if (on_incoming_body != NULL) { |
| 1524 |
(*on_incoming_body)(negotiator, data); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
return AWS_OP_SUCCESS; |
| 1529 |
} |
| 1530 |
|
| 1531 |
static enum aws_http_proxy_negotiation_retry_directive s_sequence_get_retry_directive( |
| 1532 |
struct aws_http_proxy_negotiator *proxy_negotiator) { |
| 1533 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1534 |
|
| 1535 |
if (sequence_negotiator->current_negotiator_transform_index < |
| 1536 |
aws_array_list_length(&sequence_negotiator->negotiators)) { |
| 1537 |
struct aws_http_proxy_negotiator *next_negotiator = NULL; |
| 1538 |
aws_array_list_get_at( |
| 1539 |
&sequence_negotiator->negotiators, |
| 1540 |
&next_negotiator, |
| 1541 |
sequence_negotiator->current_negotiator_transform_index); |
| 1542 |
|
| 1543 |
enum aws_http_proxy_negotiation_retry_directive next_negotiator_directive = |
| 1544 |
aws_http_proxy_negotiator_get_retry_directive(next_negotiator); |
| 1545 |
if (next_negotiator_directive == AWS_HPNRD_CURRENT_CONNECTION) { |
| 1546 |
return AWS_HPNRD_CURRENT_CONNECTION; |
| 1547 |
} else { |
| 1548 |
return AWS_HPNRD_NEW_CONNECTION; |
| 1549 |
} |
| 1550 |
} |
| 1551 |
|
| 1552 |
return AWS_HPNRD_STOP; |
| 1553 |
} |
| 1554 |
|
| 1555 |
static struct aws_http_proxy_negotiator_tunnelling_vtable s_tunneling_sequence_proxy_negotiator_tunneling_vtable = { |
| 1556 |
.on_incoming_body_callback = s_sequence_on_incoming_body, |
| 1557 |
.on_incoming_headers_callback = s_sequence_on_incoming_headers, |
| 1558 |
.on_status_callback = s_sequence_on_connect_status, |
| 1559 |
.connect_request_transform = s_sequence_tunnel_transform_connect, |
| 1560 |
.get_retry_directive = s_sequence_get_retry_directive, |
| 1561 |
}; |
| 1562 |
|
| 1563 |
static void s_destroy_tunneling_sequence_negotiator(void *user_data) { |
| 1564 |
struct aws_http_proxy_negotiator *proxy_negotiator = user_data; |
| 1565 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl; |
| 1566 |
|
| 1567 |
size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators); |
| 1568 |
for (size_t i = 0; i < negotiator_count; ++i) { |
| 1569 |
struct aws_http_proxy_negotiator *negotiator = NULL; |
| 1570 |
if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) { |
| 1571 |
continue; |
| 1572 |
} |
| 1573 |
|
| 1574 |
aws_http_proxy_negotiator_release(negotiator); |
| 1575 |
} |
| 1576 |
|
| 1577 |
aws_array_list_clean_up(&sequence_negotiator->negotiators); |
| 1578 |
|
| 1579 |
aws_mem_release(sequence_negotiator->allocator, sequence_negotiator); |
| 1580 |
} |
| 1581 |
|
| 1582 |
static struct aws_http_proxy_negotiator *s_create_tunneling_sequence_negotiator( |
| 1583 |
struct aws_http_proxy_strategy *proxy_strategy, |
| 1584 |
struct aws_allocator *allocator) { |
| 1585 |
if (proxy_strategy == NULL || allocator == NULL) { |
| 1586 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1587 |
return NULL; |
| 1588 |
} |
| 1589 |
|
| 1590 |
struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = |
| 1591 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_sequence)); |
| 1592 |
if (sequence_negotiator == NULL) { |
| 1593 |
return NULL; |
| 1594 |
} |
| 1595 |
|
| 1596 |
sequence_negotiator->allocator = allocator; |
| 1597 |
sequence_negotiator->negotiator_base.impl = sequence_negotiator; |
| 1598 |
aws_ref_count_init( |
| 1599 |
&sequence_negotiator->negotiator_base.ref_count, |
| 1600 |
&sequence_negotiator->negotiator_base, |
| 1601 |
(aws_simple_completion_callback *)s_destroy_tunneling_sequence_negotiator); |
| 1602 |
|
| 1603 |
sequence_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable = |
| 1604 |
&s_tunneling_sequence_proxy_negotiator_tunneling_vtable; |
| 1605 |
|
| 1606 |
struct aws_http_proxy_strategy_tunneling_sequence *sequence_strategy = proxy_strategy->impl; |
| 1607 |
size_t strategy_count = aws_array_list_length(&sequence_strategy->strategies); |
| 1608 |
|
| 1609 |
if (aws_array_list_init_dynamic( |
| 1610 |
&sequence_negotiator->negotiators, allocator, strategy_count, sizeof(struct aws_http_proxy_negotiator *))) { |
| 1611 |
goto on_error; |
| 1612 |
} |
| 1613 |
|
| 1614 |
for (size_t i = 0; i < strategy_count; ++i) { |
| 1615 |
struct aws_http_proxy_strategy *strategy = NULL; |
| 1616 |
if (aws_array_list_get_at(&sequence_strategy->strategies, &strategy, i)) { |
| 1617 |
goto on_error; |
| 1618 |
} |
| 1619 |
|
| 1620 |
struct aws_http_proxy_negotiator *negotiator = aws_http_proxy_strategy_create_negotiator(strategy, allocator); |
| 1621 |
if (negotiator == NULL) { |
| 1622 |
goto on_error; |
| 1623 |
} |
| 1624 |
|
| 1625 |
if (aws_array_list_push_back(&sequence_negotiator->negotiators, &negotiator)) { |
| 1626 |
aws_http_proxy_negotiator_release(negotiator); |
| 1627 |
goto on_error; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
return &sequence_negotiator->negotiator_base; |
| 1632 |
|
| 1633 |
on_error: |
| 1634 |
|
| 1635 |
aws_http_proxy_negotiator_release(&sequence_negotiator->negotiator_base); |
| 1636 |
|
| 1637 |
return NULL; |
| 1638 |
} |
| 1639 |
|
| 1640 |
static struct aws_http_proxy_strategy_vtable s_tunneling_sequence_strategy_vtable = { |
| 1641 |
.create_negotiator = s_create_tunneling_sequence_negotiator, |
| 1642 |
}; |
| 1643 |
|
| 1644 |
static void s_destroy_tunneling_sequence_strategy(void *user_data) { |
| 1645 |
struct aws_http_proxy_strategy *proxy_strategy = user_data; |
| 1646 |
struct aws_http_proxy_strategy_tunneling_sequence *sequence_strategy = proxy_strategy->impl; |
| 1647 |
|
| 1648 |
size_t strategy_count = aws_array_list_length(&sequence_strategy->strategies); |
| 1649 |
for (size_t i = 0; i < strategy_count; ++i) { |
| 1650 |
struct aws_http_proxy_strategy *strategy = NULL; |
| 1651 |
if (aws_array_list_get_at(&sequence_strategy->strategies, &strategy, i)) { |
| 1652 |
continue; |
| 1653 |
} |
| 1654 |
|
| 1655 |
aws_http_proxy_strategy_release(strategy); |
| 1656 |
} |
| 1657 |
|
| 1658 |
aws_array_list_clean_up(&sequence_strategy->strategies); |
| 1659 |
|
| 1660 |
aws_mem_release(sequence_strategy->allocator, sequence_strategy); |
| 1661 |
} |
| 1662 |
|
| 1663 |
struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_sequence( |
| 1664 |
struct aws_allocator *allocator, |
| 1665 |
struct aws_http_proxy_strategy_tunneling_sequence_options *config) { |
| 1666 |
|
| 1667 |
if (allocator == NULL || config == NULL) { |
| 1668 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1669 |
return NULL; |
| 1670 |
} |
| 1671 |
|
| 1672 |
struct aws_http_proxy_strategy_tunneling_sequence *sequence_strategy = |
| 1673 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_sequence)); |
| 1674 |
if (sequence_strategy == NULL) { |
| 1675 |
return NULL; |
| 1676 |
} |
| 1677 |
|
| 1678 |
sequence_strategy->strategy_base.impl = sequence_strategy; |
| 1679 |
sequence_strategy->strategy_base.vtable = &s_tunneling_sequence_strategy_vtable; |
| 1680 |
sequence_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL; |
| 1681 |
sequence_strategy->allocator = allocator; |
| 1682 |
|
| 1683 |
aws_ref_count_init( |
| 1684 |
&sequence_strategy->strategy_base.ref_count, |
| 1685 |
&sequence_strategy->strategy_base, |
| 1686 |
(aws_simple_completion_callback *)s_destroy_tunneling_sequence_strategy); |
| 1687 |
|
| 1688 |
if (aws_array_list_init_dynamic( |
| 1689 |
&sequence_strategy->strategies, |
| 1690 |
allocator, |
| 1691 |
config->strategy_count, |
| 1692 |
sizeof(struct aws_http_proxy_strategy *))) { |
| 1693 |
goto on_error; |
| 1694 |
} |
| 1695 |
|
| 1696 |
for (size_t i = 0; i < config->strategy_count; ++i) { |
| 1697 |
struct aws_http_proxy_strategy *strategy = config->strategies[i]; |
| 1698 |
|
| 1699 |
if (aws_array_list_push_back(&sequence_strategy->strategies, &strategy)) { |
| 1700 |
goto on_error; |
| 1701 |
} |
| 1702 |
|
| 1703 |
aws_http_proxy_strategy_acquire(strategy); |
| 1704 |
} |
| 1705 |
|
| 1706 |
return &sequence_strategy->strategy_base; |
| 1707 |
|
| 1708 |
on_error: |
| 1709 |
|
| 1710 |
aws_http_proxy_strategy_release(&sequence_strategy->strategy_base); |
| 1711 |
|
| 1712 |
return NULL; |
| 1713 |
} |
| 1714 |
|
| 1715 |
#if defined(_MSC_VER) |
| 1716 |
# pragma warning(pop) |
| 1717 |
#endif |