| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/credentials.h> |
| 7 |
|
| 8 |
#include <aws/auth/private/credentials_utils.h> |
| 9 |
#include <aws/common/clock.h> |
| 10 |
#include <aws/common/date_time.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
#include <aws/http/connection.h> |
| 13 |
#include <aws/http/connection_manager.h> |
| 14 |
#include <aws/http/request_response.h> |
| 15 |
#include <aws/http/status_code.h> |
| 16 |
#include <aws/io/logging.h> |
| 17 |
#include <aws/io/socket.h> |
| 18 |
#include <aws/io/tls_channel_handler.h> |
| 19 |
#include <aws/io/uri.h> |
| 20 |
|
| 21 |
#include <aws/common/json.h> |
| 22 |
|
| 23 |
#if defined(_MSC_VER) |
| 24 |
# pragma warning(disable : 4204) |
| 25 |
# pragma warning(disable : 4232) |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
#define X509_RESPONSE_SIZE_INITIAL 1024 |
| 30 |
#define X509_RESPONSE_SIZE_LIMIT 2048 |
| 31 |
#define X509_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS 2 |
| 32 |
|
| 33 |
struct aws_credentials_provider_x509_impl { |
| 34 |
struct aws_http_connection_manager *connection_manager; |
| 35 |
const struct aws_auth_http_system_vtable *function_table; |
| 36 |
struct aws_byte_buf thing_name; |
| 37 |
struct aws_byte_buf role_alias_path; |
| 38 |
struct aws_byte_buf endpoint; |
| 39 |
struct aws_tls_connection_options tls_connection_options; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct aws_credentials_provider_x509_user_data { |
| 46 |
|
| 47 |
struct aws_allocator *allocator; |
| 48 |
struct aws_credentials_provider *x509_provider; |
| 49 |
aws_on_get_credentials_callback_fn *original_callback; |
| 50 |
void *original_user_data; |
| 51 |
|
| 52 |
|
| 53 |
struct aws_http_connection *connection; |
| 54 |
struct aws_http_message *request; |
| 55 |
struct aws_byte_buf response; |
| 56 |
int status_code; |
| 57 |
int error_code; |
| 58 |
}; |
| 59 |
|
| 60 |
static void s_aws_credentials_provider_x509_user_data_destroy( |
| 61 |
struct aws_credentials_provider_x509_user_data *user_data) { |
| 62 |
if (user_data == NULL) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
struct aws_credentials_provider_x509_impl *impl = user_data->x509_provider->impl; |
| 67 |
|
| 68 |
if (user_data->connection) { |
| 69 |
impl->function_table->aws_http_connection_manager_release_connection( |
| 70 |
impl->connection_manager, user_data->connection); |
| 71 |
} |
| 72 |
|
| 73 |
aws_byte_buf_clean_up(&user_data->response); |
| 74 |
|
| 75 |
if (user_data->request) { |
| 76 |
aws_http_message_destroy(user_data->request); |
| 77 |
} |
| 78 |
aws_credentials_provider_release(user_data->x509_provider); |
| 79 |
aws_mem_release(user_data->allocator, user_data); |
| 80 |
} |
| 81 |
|
| 82 |
static struct aws_credentials_provider_x509_user_data *s_aws_credentials_provider_x509_user_data_new( |
| 83 |
struct aws_credentials_provider *x509_provider, |
| 84 |
aws_on_get_credentials_callback_fn callback, |
| 85 |
void *user_data) { |
| 86 |
|
| 87 |
struct aws_credentials_provider_x509_user_data *wrapped_user_data = |
| 88 |
aws_mem_calloc(x509_provider->allocator, 1, sizeof(struct aws_credentials_provider_x509_user_data)); |
| 89 |
if (wrapped_user_data == NULL) { |
| 90 |
goto on_error; |
| 91 |
} |
| 92 |
|
| 93 |
wrapped_user_data->allocator = x509_provider->allocator; |
| 94 |
wrapped_user_data->x509_provider = x509_provider; |
| 95 |
aws_credentials_provider_acquire(x509_provider); |
| 96 |
wrapped_user_data->original_user_data = user_data; |
| 97 |
wrapped_user_data->original_callback = callback; |
| 98 |
|
| 99 |
if (aws_byte_buf_init(&wrapped_user_data->response, x509_provider->allocator, X509_RESPONSE_SIZE_INITIAL)) { |
| 100 |
goto on_error; |
| 101 |
} |
| 102 |
|
| 103 |
return wrapped_user_data; |
| 104 |
|
| 105 |
on_error: |
| 106 |
|
| 107 |
s_aws_credentials_provider_x509_user_data_destroy(wrapped_user_data); |
| 108 |
|
| 109 |
return NULL; |
| 110 |
} |
| 111 |
|
| 112 |
static void s_aws_credentials_provider_x509_user_data_reset_response( |
| 113 |
struct aws_credentials_provider_x509_user_data *x509_user_data) { |
| 114 |
x509_user_data->response.len = 0; |
| 115 |
x509_user_data->status_code = 0; |
| 116 |
|
| 117 |
if (x509_user_data->request) { |
| 118 |
aws_http_message_destroy(x509_user_data->request); |
| 119 |
x509_user_data->request = NULL; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static struct aws_credentials *s_parse_credentials_from_iot_core_document( |
| 135 |
struct aws_allocator *allocator, |
| 136 |
struct aws_byte_buf *document) { |
| 137 |
|
| 138 |
struct aws_credentials *credentials = NULL; |
| 139 |
struct aws_json_value *document_root = NULL; |
| 140 |
|
| 141 |
if (aws_byte_buf_append_null_terminator(document)) { |
| 142 |
goto done; |
| 143 |
} |
| 144 |
|
| 145 |
struct aws_byte_cursor document_cursor = aws_byte_cursor_from_buf(document); |
| 146 |
document_root = aws_json_value_new_from_string(allocator, document_cursor); |
| 147 |
if (document_root == NULL) { |
| 148 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse IoT Core response as Json document."); |
| 149 |
goto done; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
struct aws_json_value *creds = |
| 156 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("credentials")); |
| 157 |
if (!aws_json_value_is_object(creds)) { |
| 158 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse credentials from IoT Core response."); |
| 159 |
goto done; |
| 160 |
} |
| 161 |
|
| 162 |
struct aws_parse_credentials_from_json_doc_options parse_options = { |
| 163 |
.access_key_id_name = "accessKeyId", |
| 164 |
.secret_access_key_name = "secretAccessKey", |
| 165 |
.token_name = "sessionToken", |
| 166 |
.expiration_name = "expiration", |
| 167 |
.expiration_format = AWS_PCEF_STRING_ISO_8601_DATE, |
| 168 |
.token_required = true, |
| 169 |
.expiration_required = false, |
| 170 |
}; |
| 171 |
|
| 172 |
credentials = aws_parse_credentials_from_aws_json_object(allocator, creds, &parse_options); |
| 173 |
if (!credentials) { |
| 174 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "X509 credentials provider failed to parse credentials"); |
| 175 |
} |
| 176 |
|
| 177 |
done: |
| 178 |
|
| 179 |
if (document_root != NULL) { |
| 180 |
aws_json_value_destroy(document_root); |
| 181 |
} |
| 182 |
|
| 183 |
return credentials; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static void s_x509_finalize_get_credentials_query(struct aws_credentials_provider_x509_user_data *x509_user_data) { |
| 190 |
|
| 191 |
struct aws_credentials *credentials = |
| 192 |
s_parse_credentials_from_iot_core_document(x509_user_data->allocator, &x509_user_data->response); |
| 193 |
|
| 194 |
if (credentials != NULL) { |
| 195 |
AWS_LOGF_INFO( |
| 196 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 197 |
"(id=%p) X509 credentials provider successfully queried credentials", |
| 198 |
(void *)x509_user_data->x509_provider); |
| 199 |
} else { |
| 200 |
if (x509_user_data->error_code == AWS_ERROR_SUCCESS) { |
| 201 |
x509_user_data->error_code = aws_last_error(); |
| 202 |
if (x509_user_data->error_code == AWS_ERROR_SUCCESS) { |
| 203 |
x509_user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_X509_SOURCE_FAILURE; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
AWS_LOGF_WARN( |
| 208 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 209 |
"(id=%p) X509 credentials provider failed to query credentials with error %d(%s)", |
| 210 |
(void *)x509_user_data->x509_provider, |
| 211 |
x509_user_data->error_code, |
| 212 |
aws_error_str(x509_user_data->error_code)); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
x509_user_data->original_callback(credentials, x509_user_data->error_code, x509_user_data->original_user_data); |
| 217 |
|
| 218 |
|
| 219 |
s_aws_credentials_provider_x509_user_data_destroy(x509_user_data); |
| 220 |
aws_credentials_release(credentials); |
| 221 |
} |
| 222 |
|
| 223 |
static int s_x509_on_incoming_body_fn( |
| 224 |
struct aws_http_stream *stream, |
| 225 |
const struct aws_byte_cursor *data, |
| 226 |
void *user_data) { |
| 227 |
|
| 228 |
(void)stream; |
| 229 |
|
| 230 |
struct aws_credentials_provider_x509_user_data *x509_user_data = user_data; |
| 231 |
struct aws_credentials_provider_x509_impl *impl = x509_user_data->x509_provider->impl; |
| 232 |
|
| 233 |
AWS_LOGF_TRACE( |
| 234 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 235 |
"(id=%p) X509 credentials provider received %zu response bytes", |
| 236 |
(void *)x509_user_data->x509_provider, |
| 237 |
data->len); |
| 238 |
|
| 239 |
if (data->len + x509_user_data->response.len > X509_RESPONSE_SIZE_LIMIT) { |
| 240 |
impl->function_table->aws_http_connection_close(x509_user_data->connection); |
| 241 |
AWS_LOGF_ERROR( |
| 242 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 243 |
"(id=%p) X509 credentials provider query response exceeded maximum allowed length", |
| 244 |
(void *)x509_user_data->x509_provider); |
| 245 |
|
| 246 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 247 |
} |
| 248 |
|
| 249 |
if (aws_byte_buf_append_dynamic(&x509_user_data->response, data)) { |
| 250 |
impl->function_table->aws_http_connection_close(x509_user_data->connection); |
| 251 |
AWS_LOGF_ERROR( |
| 252 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 253 |
"(id=%p) X509 credentials provider query error appending response", |
| 254 |
(void *)x509_user_data->x509_provider); |
| 255 |
|
| 256 |
return AWS_OP_ERR; |
| 257 |
} |
| 258 |
|
| 259 |
return AWS_OP_SUCCESS; |
| 260 |
} |
| 261 |
|
| 262 |
static int s_x509_on_incoming_headers_fn( |
| 263 |
struct aws_http_stream *stream, |
| 264 |
enum aws_http_header_block header_block, |
| 265 |
const struct aws_http_header *header_array, |
| 266 |
size_t num_headers, |
| 267 |
void *user_data) { |
| 268 |
|
| 269 |
(void)header_array; |
| 270 |
(void)num_headers; |
| 271 |
|
| 272 |
if (header_block != AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 273 |
return AWS_OP_SUCCESS; |
| 274 |
} |
| 275 |
|
| 276 |
struct aws_credentials_provider_x509_user_data *x509_user_data = user_data; |
| 277 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 278 |
if (x509_user_data->status_code == 0) { |
| 279 |
struct aws_credentials_provider_x509_impl *impl = x509_user_data->x509_provider->impl; |
| 280 |
if (impl->function_table->aws_http_stream_get_incoming_response_status( |
| 281 |
stream, &x509_user_data->status_code)) { |
| 282 |
|
| 283 |
AWS_LOGF_ERROR( |
| 284 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 285 |
"(id=%p) X509 credentials provider failed to get http status code", |
| 286 |
(void *)x509_user_data->x509_provider); |
| 287 |
|
| 288 |
return AWS_OP_ERR; |
| 289 |
} |
| 290 |
AWS_LOGF_DEBUG( |
| 291 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 292 |
"(id=%p) X509 credentials provider query received http status code %d", |
| 293 |
(void *)x509_user_data->x509_provider, |
| 294 |
x509_user_data->status_code); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
return AWS_OP_SUCCESS; |
| 299 |
} |
| 300 |
|
| 301 |
static void s_x509_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 302 |
struct aws_credentials_provider_x509_user_data *x509_user_data = user_data; |
| 303 |
|
| 304 |
aws_http_message_destroy(x509_user_data->request); |
| 305 |
x509_user_data->request = NULL; |
| 306 |
|
| 307 |
struct aws_credentials_provider_x509_impl *impl = x509_user_data->x509_provider->impl; |
| 308 |
impl->function_table->aws_http_stream_release(stream); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
if (x509_user_data->status_code != AWS_HTTP_STATUS_CODE_200_OK || error_code != AWS_OP_SUCCESS) { |
| 315 |
x509_user_data->response.len = 0; |
| 316 |
|
| 317 |
if (error_code != AWS_OP_SUCCESS) { |
| 318 |
x509_user_data->error_code = error_code; |
| 319 |
} else { |
| 320 |
x509_user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_HTTP_STATUS_FAILURE; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
s_x509_finalize_get_credentials_query(x509_user_data); |
| 325 |
} |
| 326 |
|
| 327 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_accept_header, "Accept"); |
| 328 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_accept_header_value, "*/*"); |
| 329 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_user_agent_header, "User-Agent"); |
| 330 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_user_agent_header_value, "aws-sdk-crt/x509-credentials-provider"); |
| 331 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_h1_0_keep_alive_header, "Connection"); |
| 332 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_h1_0_keep_alive_header_value, "keep-alive"); |
| 333 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_thing_name_header, "x-amzn-iot-thingname"); |
| 334 |
AWS_STATIC_STRING_FROM_LITERAL(s_x509_host_header, "Host"); |
| 335 |
|
| 336 |
static int s_make_x509_http_query( |
| 337 |
struct aws_credentials_provider_x509_user_data *x509_user_data, |
| 338 |
struct aws_byte_cursor *request_path) { |
| 339 |
AWS_FATAL_ASSERT(x509_user_data->connection); |
| 340 |
|
| 341 |
struct aws_http_stream *stream = NULL; |
| 342 |
struct aws_http_message *request = aws_http_message_new_request(x509_user_data->allocator); |
| 343 |
if (request == NULL) { |
| 344 |
return AWS_OP_ERR; |
| 345 |
} |
| 346 |
|
| 347 |
struct aws_credentials_provider_x509_impl *impl = x509_user_data->x509_provider->impl; |
| 348 |
|
| 349 |
struct aws_http_header thing_name_header = { |
| 350 |
.name = aws_byte_cursor_from_string(s_x509_thing_name_header), |
| 351 |
.value = aws_byte_cursor_from_buf(&impl->thing_name), |
| 352 |
}; |
| 353 |
if (aws_http_message_add_header(request, thing_name_header)) { |
| 354 |
goto on_error; |
| 355 |
} |
| 356 |
|
| 357 |
struct aws_http_header accept_header = { |
| 358 |
.name = aws_byte_cursor_from_string(s_x509_accept_header), |
| 359 |
.value = aws_byte_cursor_from_string(s_x509_accept_header_value), |
| 360 |
}; |
| 361 |
if (aws_http_message_add_header(request, accept_header)) { |
| 362 |
goto on_error; |
| 363 |
} |
| 364 |
|
| 365 |
struct aws_http_header user_agent_header = { |
| 366 |
.name = aws_byte_cursor_from_string(s_x509_user_agent_header), |
| 367 |
.value = aws_byte_cursor_from_string(s_x509_user_agent_header_value), |
| 368 |
}; |
| 369 |
if (aws_http_message_add_header(request, user_agent_header)) { |
| 370 |
goto on_error; |
| 371 |
} |
| 372 |
|
| 373 |
struct aws_http_header keep_alive_header = { |
| 374 |
.name = aws_byte_cursor_from_string(s_x509_h1_0_keep_alive_header), |
| 375 |
.value = aws_byte_cursor_from_string(s_x509_h1_0_keep_alive_header_value), |
| 376 |
}; |
| 377 |
if (aws_http_message_add_header(request, keep_alive_header)) { |
| 378 |
goto on_error; |
| 379 |
} |
| 380 |
|
| 381 |
struct aws_http_header host_header = { |
| 382 |
.name = aws_byte_cursor_from_string(s_x509_host_header), |
| 383 |
.value = aws_byte_cursor_from_buf(&impl->endpoint), |
| 384 |
}; |
| 385 |
if (aws_http_message_add_header(request, host_header)) { |
| 386 |
goto on_error; |
| 387 |
} |
| 388 |
|
| 389 |
if (aws_http_message_set_request_path(request, *request_path)) { |
| 390 |
goto on_error; |
| 391 |
} |
| 392 |
|
| 393 |
if (aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET"))) { |
| 394 |
goto on_error; |
| 395 |
} |
| 396 |
|
| 397 |
x509_user_data->request = request; |
| 398 |
|
| 399 |
struct aws_http_make_request_options request_options = { |
| 400 |
.self_size = sizeof(request_options), |
| 401 |
.on_response_headers = s_x509_on_incoming_headers_fn, |
| 402 |
.on_response_header_block_done = NULL, |
| 403 |
.on_response_body = s_x509_on_incoming_body_fn, |
| 404 |
.on_complete = s_x509_on_stream_complete_fn, |
| 405 |
.user_data = x509_user_data, |
| 406 |
.request = request, |
| 407 |
}; |
| 408 |
|
| 409 |
stream = impl->function_table->aws_http_connection_make_request(x509_user_data->connection, &request_options); |
| 410 |
|
| 411 |
if (!stream) { |
| 412 |
goto on_error; |
| 413 |
} |
| 414 |
|
| 415 |
if (impl->function_table->aws_http_stream_activate(stream)) { |
| 416 |
goto on_error; |
| 417 |
} |
| 418 |
|
| 419 |
return AWS_OP_SUCCESS; |
| 420 |
|
| 421 |
on_error: |
| 422 |
impl->function_table->aws_http_stream_release(stream); |
| 423 |
aws_http_message_destroy(request); |
| 424 |
x509_user_data->request = NULL; |
| 425 |
return AWS_OP_ERR; |
| 426 |
} |
| 427 |
|
| 428 |
static void s_x509_query_credentials(struct aws_credentials_provider_x509_user_data *x509_user_data) { |
| 429 |
AWS_FATAL_ASSERT(x509_user_data->connection); |
| 430 |
|
| 431 |
struct aws_credentials_provider_x509_impl *impl = x509_user_data->x509_provider->impl; |
| 432 |
|
| 433 |
|
| 434 |
s_aws_credentials_provider_x509_user_data_reset_response(x509_user_data); |
| 435 |
|
| 436 |
struct aws_byte_cursor request_path_cursor = aws_byte_cursor_from_buf(&impl->role_alias_path); |
| 437 |
if (s_make_x509_http_query(x509_user_data, &request_path_cursor) == AWS_OP_ERR) { |
| 438 |
s_x509_finalize_get_credentials_query(x509_user_data); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
static void s_x509_on_acquire_connection(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 443 |
struct aws_credentials_provider_x509_user_data *x509_user_data = user_data; |
| 444 |
|
| 445 |
if (connection == NULL) { |
| 446 |
AWS_LOGF_WARN( |
| 447 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 448 |
"id=%p: X509 provider failed to acquire a connection, error code %d(%s)", |
| 449 |
(void *)x509_user_data->x509_provider, |
| 450 |
error_code, |
| 451 |
aws_error_str(error_code)); |
| 452 |
|
| 453 |
x509_user_data->error_code = error_code; |
| 454 |
|
| 455 |
s_x509_finalize_get_credentials_query(x509_user_data); |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
x509_user_data->connection = connection; |
| 460 |
|
| 461 |
s_x509_query_credentials(x509_user_data); |
| 462 |
} |
| 463 |
|
| 464 |
static int s_credentials_provider_x509_get_credentials_async( |
| 465 |
struct aws_credentials_provider *provider, |
| 466 |
aws_on_get_credentials_callback_fn callback, |
| 467 |
void *user_data) { |
| 468 |
|
| 469 |
struct aws_credentials_provider_x509_impl *impl = provider->impl; |
| 470 |
|
| 471 |
struct aws_credentials_provider_x509_user_data *wrapped_user_data = |
| 472 |
s_aws_credentials_provider_x509_user_data_new(provider, callback, user_data); |
| 473 |
if (wrapped_user_data == NULL) { |
| 474 |
goto error; |
| 475 |
} |
| 476 |
|
| 477 |
impl->function_table->aws_http_connection_manager_acquire_connection( |
| 478 |
impl->connection_manager, s_x509_on_acquire_connection, wrapped_user_data); |
| 479 |
|
| 480 |
return AWS_OP_SUCCESS; |
| 481 |
|
| 482 |
error: |
| 483 |
|
| 484 |
s_aws_credentials_provider_x509_user_data_destroy(wrapped_user_data); |
| 485 |
|
| 486 |
return AWS_OP_ERR; |
| 487 |
} |
| 488 |
|
| 489 |
static void s_credentials_provider_x509_destroy(struct aws_credentials_provider *provider) { |
| 490 |
struct aws_credentials_provider_x509_impl *impl = provider->impl; |
| 491 |
if (impl == NULL) { |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
aws_byte_buf_clean_up(&impl->thing_name); |
| 496 |
aws_byte_buf_clean_up(&impl->role_alias_path); |
| 497 |
aws_byte_buf_clean_up(&impl->endpoint); |
| 498 |
aws_tls_connection_options_clean_up(&impl->tls_connection_options); |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
impl->function_table->aws_http_connection_manager_release(impl->connection_manager); |
| 504 |
|
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_x509_vtable = { |
| 509 |
.get_credentials = s_credentials_provider_x509_get_credentials_async, |
| 510 |
.destroy = s_credentials_provider_x509_destroy, |
| 511 |
}; |
| 512 |
|
| 513 |
static void s_on_connection_manager_shutdown(void *user_data) { |
| 514 |
struct aws_credentials_provider *provider = user_data; |
| 515 |
|
| 516 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 517 |
|
| 518 |
aws_mem_release(provider->allocator, provider); |
| 519 |
} |
| 520 |
|
| 521 |
struct aws_credentials_provider *aws_credentials_provider_new_x509( |
| 522 |
struct aws_allocator *allocator, |
| 523 |
const struct aws_credentials_provider_x509_options *options) { |
| 524 |
|
| 525 |
struct aws_credentials_provider *provider = NULL; |
| 526 |
struct aws_credentials_provider_x509_impl *impl = NULL; |
| 527 |
|
| 528 |
if (options->tls_connection_options == NULL || options->thing_name.len == 0 || options->role_alias.len == 0) { |
| 529 |
AWS_LOGF_ERROR( |
| 530 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 531 |
"To create an X.509 creds provider, a tls_connection_options, an IoT thing name and an IAM role alias are " |
| 532 |
"required."); |
| 533 |
goto on_error; |
| 534 |
} |
| 535 |
|
| 536 |
aws_mem_acquire_many( |
| 537 |
allocator, |
| 538 |
2, |
| 539 |
&provider, |
| 540 |
sizeof(struct aws_credentials_provider), |
| 541 |
&impl, |
| 542 |
sizeof(struct aws_credentials_provider_x509_impl)); |
| 543 |
|
| 544 |
if (!provider) { |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
|
| 548 |
AWS_ZERO_STRUCT(*provider); |
| 549 |
AWS_ZERO_STRUCT(*impl); |
| 550 |
|
| 551 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_x509_vtable, impl); |
| 552 |
|
| 553 |
if (aws_tls_connection_options_copy(&impl->tls_connection_options, options->tls_connection_options)) { |
| 554 |
goto on_error; |
| 555 |
} |
| 556 |
|
| 557 |
struct aws_byte_cursor server_name = options->endpoint; |
| 558 |
if (aws_tls_connection_options_set_server_name(&impl->tls_connection_options, allocator, &(server_name))) { |
| 559 |
AWS_LOGF_ERROR( |
| 560 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 561 |
"(id=%p): failed to set tls connection options's server name with error %s", |
| 562 |
(void *)provider, |
| 563 |
aws_error_debug_str(aws_last_error())); |
| 564 |
goto on_error; |
| 565 |
} |
| 566 |
|
| 567 |
struct aws_socket_options socket_options; |
| 568 |
AWS_ZERO_STRUCT(socket_options); |
| 569 |
socket_options.type = AWS_SOCKET_STREAM; |
| 570 |
socket_options.domain = AWS_SOCKET_IPV4; |
| 571 |
socket_options.connect_timeout_ms = (uint32_t)aws_timestamp_convert( |
| 572 |
X509_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 573 |
|
| 574 |
struct aws_http_connection_manager_options manager_options; |
| 575 |
AWS_ZERO_STRUCT(manager_options); |
| 576 |
manager_options.bootstrap = options->bootstrap; |
| 577 |
manager_options.initial_window_size = X509_RESPONSE_SIZE_LIMIT; |
| 578 |
manager_options.socket_options = &socket_options; |
| 579 |
manager_options.host = options->endpoint; |
| 580 |
manager_options.port = 443; |
| 581 |
manager_options.max_connections = 2; |
| 582 |
manager_options.shutdown_complete_callback = s_on_connection_manager_shutdown; |
| 583 |
manager_options.shutdown_complete_user_data = provider; |
| 584 |
manager_options.tls_connection_options = &impl->tls_connection_options; |
| 585 |
manager_options.proxy_options = options->proxy_options; |
| 586 |
manager_options.proxy_ev_settings = options->proxy_ev_settings; |
| 587 |
|
| 588 |
impl->function_table = options->function_table; |
| 589 |
if (impl->function_table == NULL) { |
| 590 |
impl->function_table = g_aws_credentials_provider_http_function_table; |
| 591 |
} |
| 592 |
|
| 593 |
impl->connection_manager = impl->function_table->aws_http_connection_manager_new(allocator, &manager_options); |
| 594 |
if (impl->connection_manager == NULL) { |
| 595 |
goto on_error; |
| 596 |
} |
| 597 |
|
| 598 |
if (aws_byte_buf_init_copy_from_cursor(&impl->thing_name, allocator, options->thing_name)) { |
| 599 |
goto on_error; |
| 600 |
} |
| 601 |
|
| 602 |
if (aws_byte_buf_init_copy_from_cursor(&impl->endpoint, allocator, options->endpoint)) { |
| 603 |
goto on_error; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_c_str("/role-aliases/"); |
| 608 |
if (aws_byte_buf_init_copy_from_cursor(&impl->role_alias_path, allocator, prefix_cursor)) { |
| 609 |
goto on_error; |
| 610 |
} |
| 611 |
|
| 612 |
if (aws_byte_buf_append_dynamic(&impl->role_alias_path, &options->role_alias)) { |
| 613 |
goto on_error; |
| 614 |
} |
| 615 |
|
| 616 |
struct aws_byte_cursor creds_cursor = aws_byte_cursor_from_c_str("/credentials"); |
| 617 |
if (aws_byte_buf_append_dynamic(&impl->role_alias_path, &creds_cursor)) { |
| 618 |
goto on_error; |
| 619 |
} |
| 620 |
|
| 621 |
provider->shutdown_options = options->shutdown_options; |
| 622 |
|
| 623 |
return provider; |
| 624 |
|
| 625 |
on_error: |
| 626 |
|
| 627 |
aws_credentials_provider_destroy(provider); |
| 628 |
|
| 629 |
return NULL; |
| 630 |
} |