| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/auth/credentials.h> |
| 6 |
#include <aws/auth/private/login_token_utils.h> |
| 7 |
#include <aws/cal/ecc.h> |
| 8 |
#include <aws/cal/hash.h> |
| 9 |
#include <aws/common/encoding.h> |
| 10 |
#include <aws/common/file.h> |
| 11 |
#include <aws/common/json.h> |
| 12 |
#include <aws/common/uuid.h> |
| 13 |
#include <aws/io/pem.h> |
| 14 |
|
| 15 |
#if defined(_MSC_VER) |
| 16 |
# pragma warning(disable : 4232) |
| 17 |
#endif |
| 18 |
|
| 19 |
static int TOKEN_BUFFER_SIZE = 2500; |
| 20 |
static int PADDING_LENGTH = 32; |
| 21 |
|
| 22 |
|
| 23 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_access_token_key, "accessToken"); |
| 24 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_access_key_id_key, "accessKeyId"); |
| 25 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_secret_access_key, "secretAccessKey"); |
| 26 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_session_token_key, "sessionToken"); |
| 27 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_expires_at_key, "expiresAt"); |
| 28 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_expires_in_key, "expiresIn"); |
| 29 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_account_id_key, "accountId"); |
| 30 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_token_type_key, "tokenType"); |
| 31 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_refresh_token_key, "refreshToken"); |
| 32 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_id_token_key, "idToken"); |
| 33 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_client_id_key, "clientId"); |
| 34 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_dpop_key, "dpopKey"); |
| 35 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_grant_type_key, "grantType"); |
| 36 |
AWS_STATIC_STRING_FROM_LITERAL(s_login_grant_type_value, "refresh_token"); |
| 37 |
|
| 38 |
|
| 39 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_typ_key, "typ"); |
| 40 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_alg_key, "alg"); |
| 41 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_kty_key, "kty"); |
| 42 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_x_nums_key, "x"); |
| 43 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_y_nums_key, "y"); |
| 44 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_crv_key, "crv"); |
| 45 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_jti_key, "jti"); |
| 46 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_htm_key, "htm"); |
| 47 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_htu_key, "htu"); |
| 48 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_iat_key, "iat"); |
| 49 |
|
| 50 |
|
| 51 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_typ_value, "dpop+jwt"); |
| 52 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_alg_value, "ES256"); |
| 53 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_kty_value, "EC"); |
| 54 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_crv_value, "P-256"); |
| 55 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_header_htm_value, "POST"); |
| 56 |
|
| 57 |
|
| 58 |
AWS_STATIC_STRING_FROM_LITERAL(s_jwt_message_delim, "."); |
| 59 |
|
| 60 |
struct aws_login_token { |
| 61 |
struct aws_json_value *serialized_token; |
| 62 |
struct aws_byte_cursor access_key_id; |
| 63 |
struct aws_byte_cursor secret_access_key; |
| 64 |
struct aws_byte_cursor session_token; |
| 65 |
struct aws_byte_cursor account_id; |
| 66 |
struct aws_date_time expires_at; |
| 67 |
struct aws_byte_cursor token_type; |
| 68 |
struct aws_byte_cursor refresh_token; |
| 69 |
struct aws_byte_cursor id_token; |
| 70 |
struct aws_byte_cursor client_id; |
| 71 |
struct aws_byte_cursor dpop_key; |
| 72 |
}; |
| 73 |
|
| 74 |
struct aws_login_refresh_response { |
| 75 |
struct aws_json_value *serialized_token; |
| 76 |
struct aws_byte_cursor access_key_id; |
| 77 |
struct aws_byte_cursor secret_access_key; |
| 78 |
struct aws_byte_cursor session_token; |
| 79 |
struct aws_date_time expires_at; |
| 80 |
struct aws_byte_cursor refresh_token; |
| 81 |
}; |
| 82 |
|
| 83 |
struct aws_byte_cursor aws_login_token_get_account_id(struct aws_login_token *token) { |
| 84 |
return token->account_id; |
| 85 |
} |
| 86 |
|
| 87 |
void aws_login_token_set_access_key_id(struct aws_login_token *token, struct aws_byte_cursor value) { |
| 88 |
token->access_key_id = value; |
| 89 |
} |
| 90 |
|
| 91 |
void aws_login_token_set_secret_access_key(struct aws_login_token *token, struct aws_byte_cursor value) { |
| 92 |
token->secret_access_key = value; |
| 93 |
} |
| 94 |
|
| 95 |
void aws_login_token_set_session_token(struct aws_login_token *token, struct aws_byte_cursor value) { |
| 96 |
token->session_token = value; |
| 97 |
} |
| 98 |
|
| 99 |
void aws_login_token_set_expires_at(struct aws_login_token *token, struct aws_date_time value) { |
| 100 |
token->expires_at = value; |
| 101 |
} |
| 102 |
|
| 103 |
void aws_login_token_set_refresh_token(struct aws_login_token *token, struct aws_byte_cursor value) { |
| 104 |
token->refresh_token = value; |
| 105 |
} |
| 106 |
|
| 107 |
struct aws_login_token *aws_login_token_destroy(struct aws_login_token *login_token) { |
| 108 |
if (login_token == NULL) { |
| 109 |
return login_token; |
| 110 |
} |
| 111 |
aws_json_value_destroy(login_token->serialized_token); |
| 112 |
return login_token; |
| 113 |
} |
| 114 |
struct aws_byte_cursor aws_login_refresh_get_access_key_id(struct aws_login_refresh_response *token) { |
| 115 |
return token->access_key_id; |
| 116 |
} |
| 117 |
|
| 118 |
struct aws_byte_cursor aws_login_refresh_get_secret_access_key(struct aws_login_refresh_response *token) { |
| 119 |
return token->secret_access_key; |
| 120 |
} |
| 121 |
|
| 122 |
struct aws_byte_cursor aws_login_refresh_get_session_token(struct aws_login_refresh_response *token) { |
| 123 |
return token->session_token; |
| 124 |
} |
| 125 |
|
| 126 |
struct aws_date_time aws_login_refresh_get_expires_at(struct aws_login_refresh_response *token) { |
| 127 |
return token->expires_at; |
| 128 |
} |
| 129 |
|
| 130 |
struct aws_byte_cursor aws_login_refresh_get_refresh_token(struct aws_login_refresh_response *token) { |
| 131 |
return token->refresh_token; |
| 132 |
} |
| 133 |
|
| 134 |
struct aws_login_refresh_response *aws_login_refresh_destroy(struct aws_login_refresh_response *refresh_token) { |
| 135 |
if (refresh_token == NULL) { |
| 136 |
return refresh_token; |
| 137 |
} |
| 138 |
aws_json_value_destroy(refresh_token->serialized_token); |
| 139 |
return refresh_token; |
| 140 |
} |
| 141 |
|
| 142 |
int aws_login_token_construct_token_path( |
| 143 |
struct aws_allocator *allocator, |
| 144 |
const struct aws_string *input, |
| 145 |
const struct aws_string *directory_override, |
| 146 |
struct aws_byte_buf *login_token_path_buf) { |
| 147 |
bool success = false; |
| 148 |
AWS_PRECONDITION(input); |
| 149 |
|
| 150 |
struct aws_string *home_directory = aws_get_home_directory(allocator); |
| 151 |
if (!home_directory) { |
| 152 |
goto on_error; |
| 153 |
} |
| 154 |
|
| 155 |
struct aws_byte_buf sha256_buf; |
| 156 |
AWS_ZERO_STRUCT(sha256_buf); |
| 157 |
|
| 158 |
if (directory_override) { |
| 159 |
struct aws_byte_cursor override_dir = aws_byte_cursor_from_string(directory_override); |
| 160 |
if (aws_byte_buf_init_copy_from_cursor(login_token_path_buf, allocator, override_dir)) { |
| 161 |
goto on_error; |
| 162 |
} |
| 163 |
} else { |
| 164 |
|
| 165 |
struct aws_byte_cursor home_dir_cursor = aws_byte_cursor_from_string(home_directory); |
| 166 |
if (aws_byte_buf_init_copy_from_cursor(login_token_path_buf, allocator, home_dir_cursor)) { |
| 167 |
goto on_error; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
struct aws_byte_cursor login_cache_dir_cursor = aws_byte_cursor_from_c_str("/.aws/login/cache/"); |
| 172 |
if (aws_byte_buf_append_dynamic(login_token_path_buf, &login_cache_dir_cursor)) { |
| 173 |
goto on_error; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_string(input); |
| 178 |
struct aws_byte_cursor json_cursor = aws_byte_cursor_from_c_str(".json"); |
| 179 |
|
| 180 |
|
| 181 |
if (aws_byte_buf_init(&sha256_buf, allocator, AWS_SHA256_LEN) || |
| 182 |
aws_sha256_compute(allocator, &input_cursor, &sha256_buf, 0)) { |
| 183 |
goto on_error; |
| 184 |
} |
| 185 |
struct aws_byte_cursor sha256_cursor = aws_byte_cursor_from_buf(&sha256_buf); |
| 186 |
if (aws_hex_encode_append_dynamic(&sha256_cursor, login_token_path_buf)) { |
| 187 |
goto on_error; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
if (aws_byte_buf_append_dynamic(login_token_path_buf, &json_cursor)) { |
| 192 |
goto on_error; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
aws_normalize_directory_separator(login_token_path_buf); |
| 197 |
|
| 198 |
AWS_LOGF_INFO( |
| 199 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 200 |
"successfully constructed token path: " PRInSTR, |
| 201 |
AWS_BYTE_BUF_PRI(*login_token_path_buf)); |
| 202 |
success = true; |
| 203 |
goto on_finish; |
| 204 |
on_error: |
| 205 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_CREATE_TOKEN_PATH); |
| 206 |
on_finish: |
| 207 |
aws_byte_buf_clean_up(&sha256_buf); |
| 208 |
aws_string_destroy(home_directory); |
| 209 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 210 |
} |
| 211 |
|
| 212 |
struct aws_login_token *aws_login_token_new_from_file(struct aws_allocator *allocator, struct aws_byte_buf *file_path) { |
| 213 |
AWS_PRECONDITION(allocator); |
| 214 |
struct aws_login_token *token = NULL; |
| 215 |
struct aws_byte_buf file_contents_buf; |
| 216 |
AWS_ZERO_STRUCT(file_contents_buf); |
| 217 |
struct aws_string *file_path_str = aws_string_new_from_buf(allocator, file_path); |
| 218 |
if (aws_byte_buf_init_from_file(&file_contents_buf, allocator, aws_string_c_str(file_path_str))) { |
| 219 |
goto on_finish; |
| 220 |
} |
| 221 |
|
| 222 |
struct aws_byte_cursor document_cursor = aws_byte_cursor_from_buf(&file_contents_buf); |
| 223 |
token = aws_login_token_new_from_json_document(allocator, document_cursor); |
| 224 |
on_finish: |
| 225 |
aws_string_destroy(file_path_str); |
| 226 |
aws_byte_buf_clean_up_secure(&file_contents_buf); |
| 227 |
return token; |
| 228 |
} |
| 229 |
|
| 230 |
typedef bool(validate_json_object_fn)(struct aws_json_value *value); |
| 231 |
typedef int(fetch_json_object_fn)(struct aws_json_value *value, void *raw_value); |
| 232 |
|
| 233 |
static int s_set_json_on_token( |
| 234 |
struct aws_json_value *json_parent, |
| 235 |
struct aws_byte_cursor json_value, |
| 236 |
validate_json_object_fn *validate_fn, |
| 237 |
fetch_json_object_fn *fetch_fn, |
| 238 |
void *json_raw_value) { |
| 239 |
struct aws_json_value *json_node = aws_json_value_get_from_object(json_parent, json_value); |
| 240 |
if (!validate_fn(json_node) || fetch_fn(json_node, json_raw_value)) { |
| 241 |
return AWS_OP_ERR; |
| 242 |
} |
| 243 |
return AWS_OP_SUCCESS; |
| 244 |
} |
| 245 |
|
| 246 |
static bool s_is_string_json_value(struct aws_json_value *value) { |
| 247 |
return aws_json_value_is_string(value); |
| 248 |
} |
| 249 |
|
| 250 |
static int s_get_string_json_value(struct aws_json_value *value, void *out_value) { |
| 251 |
struct aws_byte_cursor *out_cursor = out_value; |
| 252 |
return aws_json_value_get_string(value, out_cursor); |
| 253 |
} |
| 254 |
|
| 255 |
static int s_get_date_from_string_value(struct aws_json_value *value, void *out_value) { |
| 256 |
struct aws_date_time *expiration = out_value; |
| 257 |
struct aws_byte_cursor expires_at_cursor; |
| 258 |
aws_json_value_get_string(value, &expires_at_cursor); |
| 259 |
if (aws_date_time_init_from_str_cursor(expiration, &expires_at_cursor, AWS_DATE_FORMAT_ISO_8601)) { |
| 260 |
AWS_LOGF_ERROR( |
| 261 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 262 |
"login token: expiresAt '" PRInSTR "' is not a valid ISO-8601 date string", |
| 263 |
AWS_BYTE_CURSOR_PRI(expires_at_cursor)); |
| 264 |
return AWS_OP_ERR; |
| 265 |
} |
| 266 |
return AWS_OP_SUCCESS; |
| 267 |
} |
| 268 |
|
| 269 |
static bool s_is_number_json_value(struct aws_json_value *value) { |
| 270 |
return aws_json_value_is_number(value); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
static int s_get_expiration_from_expires_in(struct aws_json_value *value, void *out_value) { |
| 278 |
bool success = false; |
| 279 |
struct aws_date_time *date_time = out_value; |
| 280 |
|
| 281 |
double number = 0; |
| 282 |
if (aws_json_value_get_number(value, &number)) { |
| 283 |
goto on_finish; |
| 284 |
} |
| 285 |
|
| 286 |
struct aws_date_time now; |
| 287 |
aws_date_time_init_now(&now); |
| 288 |
double expiration_seconds = aws_date_time_as_epoch_secs(&now) + number; |
| 289 |
aws_date_time_init_epoch_secs(date_time, expiration_seconds); |
| 290 |
success = true; |
| 291 |
on_finish: |
| 292 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 293 |
} |
| 294 |
|
| 295 |
struct aws_login_token *aws_login_token_new_from_json_document( |
| 296 |
struct aws_allocator *allocator, |
| 297 |
struct aws_byte_cursor document_cursor) { |
| 298 |
AWS_PRECONDITION(allocator); |
| 299 |
bool success = false; |
| 300 |
|
| 301 |
struct aws_login_token *token = aws_mem_acquire(allocator, sizeof(struct aws_login_token)); |
| 302 |
struct aws_json_value *document_root = NULL; |
| 303 |
|
| 304 |
document_root = aws_json_value_new_from_string(allocator, document_cursor); |
| 305 |
if (document_root == NULL) { |
| 306 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to parse login token"); |
| 307 |
goto on_error; |
| 308 |
} |
| 309 |
|
| 310 |
struct aws_json_value *access_token = |
| 311 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_login_access_token_key)); |
| 312 |
if (!aws_json_value_is_object(access_token)) { |
| 313 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to parse accessToken"); |
| 314 |
goto on_error; |
| 315 |
} |
| 316 |
|
| 317 |
if (s_set_json_on_token( |
| 318 |
access_token, |
| 319 |
aws_byte_cursor_from_string(s_login_access_key_id_key), |
| 320 |
s_is_string_json_value, |
| 321 |
s_get_string_json_value, |
| 322 |
&token->access_key_id) || |
| 323 |
s_set_json_on_token( |
| 324 |
access_token, |
| 325 |
aws_byte_cursor_from_string(s_login_secret_access_key), |
| 326 |
s_is_string_json_value, |
| 327 |
s_get_string_json_value, |
| 328 |
&token->secret_access_key) || |
| 329 |
s_set_json_on_token( |
| 330 |
access_token, |
| 331 |
aws_byte_cursor_from_string(s_login_session_token_key), |
| 332 |
s_is_string_json_value, |
| 333 |
s_get_string_json_value, |
| 334 |
&token->session_token) || |
| 335 |
s_set_json_on_token( |
| 336 |
access_token, |
| 337 |
aws_byte_cursor_from_string(s_login_expires_at_key), |
| 338 |
s_is_string_json_value, |
| 339 |
s_get_date_from_string_value, |
| 340 |
&token->expires_at) || |
| 341 |
s_set_json_on_token( |
| 342 |
access_token, |
| 343 |
aws_byte_cursor_from_string(s_login_account_id_key), |
| 344 |
s_is_string_json_value, |
| 345 |
s_get_string_json_value, |
| 346 |
&token->account_id) || |
| 347 |
s_set_json_on_token( |
| 348 |
document_root, |
| 349 |
aws_byte_cursor_from_string(s_login_token_type_key), |
| 350 |
s_is_string_json_value, |
| 351 |
s_get_string_json_value, |
| 352 |
&token->token_type) || |
| 353 |
s_set_json_on_token( |
| 354 |
document_root, |
| 355 |
aws_byte_cursor_from_string(s_login_refresh_token_key), |
| 356 |
s_is_string_json_value, |
| 357 |
s_get_string_json_value, |
| 358 |
&token->refresh_token) || |
| 359 |
s_set_json_on_token( |
| 360 |
document_root, |
| 361 |
aws_byte_cursor_from_string(s_login_id_token_key), |
| 362 |
s_is_string_json_value, |
| 363 |
s_get_string_json_value, |
| 364 |
&token->id_token) || |
| 365 |
s_set_json_on_token( |
| 366 |
document_root, |
| 367 |
aws_byte_cursor_from_string(s_login_client_id_key), |
| 368 |
s_is_string_json_value, |
| 369 |
s_get_string_json_value, |
| 370 |
&token->client_id) || |
| 371 |
s_set_json_on_token( |
| 372 |
document_root, |
| 373 |
aws_byte_cursor_from_string(s_login_dpop_key), |
| 374 |
s_is_string_json_value, |
| 375 |
s_get_string_json_value, |
| 376 |
&token->dpop_key)) { |
| 377 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to parse login token"); |
| 378 |
goto on_error; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
struct aws_date_time now; |
| 383 |
aws_date_time_init_now(&now); |
| 384 |
if (aws_date_time_as_millis(&token->expires_at) < aws_date_time_as_millis(&now)) { |
| 385 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: token is already expired"); |
| 386 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_TOKEN_EXPIRED); |
| 387 |
goto on_finish; |
| 388 |
; |
| 389 |
} |
| 390 |
|
| 391 |
token->serialized_token = document_root; |
| 392 |
success = true; |
| 393 |
goto on_finish; |
| 394 |
on_error: |
| 395 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_CREATE_TOKEN); |
| 396 |
on_finish: |
| 397 |
if (!success) { |
| 398 |
aws_mem_release(allocator, token); |
| 399 |
token = NULL; |
| 400 |
aws_json_value_destroy(document_root); |
| 401 |
} |
| 402 |
return token; |
| 403 |
} |
| 404 |
|
| 405 |
struct aws_login_refresh_response *aws_login_refresh_new_from_json_document( |
| 406 |
struct aws_allocator *allocator, |
| 407 |
struct aws_byte_cursor payload_cursor) { |
| 408 |
bool success = false; |
| 409 |
struct aws_login_refresh_response *refresh_token = |
| 410 |
aws_mem_acquire(allocator, sizeof(struct aws_login_refresh_response)); |
| 411 |
struct aws_json_value *document_root = NULL; |
| 412 |
|
| 413 |
document_root = aws_json_value_new_from_string(allocator, payload_cursor); |
| 414 |
if (document_root == NULL) { |
| 415 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to parse login token"); |
| 416 |
goto on_error; |
| 417 |
} |
| 418 |
|
| 419 |
struct aws_json_value *access_token = |
| 420 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_login_access_token_key)); |
| 421 |
if (!aws_json_value_is_object(access_token)) { |
| 422 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to parse accessToken"); |
| 423 |
goto on_error; |
| 424 |
} |
| 425 |
|
| 426 |
if (s_set_json_on_token( |
| 427 |
access_token, |
| 428 |
aws_byte_cursor_from_string(s_login_access_key_id_key), |
| 429 |
s_is_string_json_value, |
| 430 |
s_get_string_json_value, |
| 431 |
&refresh_token->access_key_id) || |
| 432 |
s_set_json_on_token( |
| 433 |
access_token, |
| 434 |
aws_byte_cursor_from_string(s_login_secret_access_key), |
| 435 |
s_is_string_json_value, |
| 436 |
s_get_string_json_value, |
| 437 |
&refresh_token->secret_access_key) || |
| 438 |
s_set_json_on_token( |
| 439 |
access_token, |
| 440 |
aws_byte_cursor_from_string(s_login_session_token_key), |
| 441 |
s_is_string_json_value, |
| 442 |
s_get_string_json_value, |
| 443 |
&refresh_token->session_token) || |
| 444 |
s_set_json_on_token( |
| 445 |
document_root, |
| 446 |
aws_byte_cursor_from_string(s_login_expires_in_key), |
| 447 |
s_is_number_json_value, |
| 448 |
s_get_expiration_from_expires_in, |
| 449 |
&refresh_token->expires_at) || |
| 450 |
s_set_json_on_token( |
| 451 |
document_root, |
| 452 |
aws_byte_cursor_from_string(s_login_refresh_token_key), |
| 453 |
s_is_string_json_value, |
| 454 |
s_get_string_json_value, |
| 455 |
&refresh_token->refresh_token)) { |
| 456 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: failed to parse refresh token"); |
| 457 |
goto on_error; |
| 458 |
} |
| 459 |
|
| 460 |
refresh_token->serialized_token = document_root; |
| 461 |
success = true; |
| 462 |
goto on_finish; |
| 463 |
on_error: |
| 464 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_CREATE_REFRESH_TOKEN); |
| 465 |
on_finish: |
| 466 |
if (!success) { |
| 467 |
aws_mem_release(allocator, refresh_token); |
| 468 |
refresh_token = NULL; |
| 469 |
aws_json_value_destroy(document_root); |
| 470 |
} |
| 471 |
return refresh_token; |
| 472 |
} |
| 473 |
|
| 474 |
typedef int(new_node_fn)(struct aws_allocator *allocator, struct aws_json_value **node, const void *value); |
| 475 |
typedef int(add_node_to_parent_fn)(struct aws_json_value *parent, const char *key, struct aws_json_value *node); |
| 476 |
|
| 477 |
static int s_write_to_json_node( |
| 478 |
struct aws_allocator *allocator, |
| 479 |
struct aws_json_value *node, |
| 480 |
new_node_fn *new_node_fn, |
| 481 |
add_node_to_parent_fn *add_node_to_parent_fn, |
| 482 |
const char *key, |
| 483 |
const void *value) { |
| 484 |
bool success = false; |
| 485 |
struct aws_json_value *new_node = NULL; |
| 486 |
if (new_node_fn(allocator, &new_node, value)) { |
| 487 |
goto on_finish; |
| 488 |
} |
| 489 |
|
| 490 |
if (add_node_to_parent_fn(node, key, new_node)) { |
| 491 |
aws_json_value_destroy(new_node); |
| 492 |
} |
| 493 |
success = true; |
| 494 |
on_finish: |
| 495 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 496 |
} |
| 497 |
|
| 498 |
static int s_new_string_from_cursor_json_value( |
| 499 |
struct aws_allocator *allocator, |
| 500 |
struct aws_json_value **node, |
| 501 |
const void *value) { |
| 502 |
const struct aws_byte_cursor *string_value = value; |
| 503 |
*node = aws_json_value_new_string(allocator, *string_value); |
| 504 |
return *node ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 505 |
} |
| 506 |
|
| 507 |
static int s_new_string_json_value(struct aws_allocator *allocator, struct aws_json_value **node, const void *value) { |
| 508 |
const struct aws_string *string_value = value; |
| 509 |
*node = aws_json_value_new_string(allocator, aws_byte_cursor_from_string(string_value)); |
| 510 |
return *node ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 511 |
} |
| 512 |
|
| 513 |
static int s_new_number_json_value(struct aws_allocator *allocator, struct aws_json_value **node, const void *value) { |
| 514 |
const double *number_value = value; |
| 515 |
*node = aws_json_value_new_number(allocator, *number_value); |
| 516 |
return *node ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 517 |
} |
| 518 |
|
| 519 |
static int s_add_json_value_to_parent(struct aws_json_value *parent, const char *key, struct aws_json_value *node) { |
| 520 |
if (aws_json_value_add_to_object(parent, aws_byte_cursor_from_c_str(key), node)) { |
| 521 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: failed to add string json value to parent"); |
| 522 |
aws_json_value_destroy(node); |
| 523 |
return AWS_OP_ERR; |
| 524 |
} |
| 525 |
return AWS_OP_SUCCESS; |
| 526 |
} |
| 527 |
|
| 528 |
static int s_new_string_json_value_from_date_time( |
| 529 |
struct aws_allocator *allocator, |
| 530 |
struct aws_json_value **node, |
| 531 |
const void *value) { |
| 532 |
const struct aws_date_time *date_time = value; |
| 533 |
struct aws_byte_buf expires_buf; |
| 534 |
AWS_ZERO_STRUCT(expires_buf); |
| 535 |
if (aws_byte_buf_init(&expires_buf, allocator, AWS_DATE_TIME_STR_MAX_LEN)) { |
| 536 |
goto on_finish; |
| 537 |
} |
| 538 |
|
| 539 |
if (aws_date_time_to_utc_time_str(date_time, AWS_DATE_FORMAT_ISO_8601, &expires_buf)) { |
| 540 |
goto on_finish; |
| 541 |
} |
| 542 |
*node = aws_json_value_new_string(allocator, aws_byte_cursor_from_buf(&expires_buf)); |
| 543 |
on_finish: |
| 544 |
aws_byte_buf_clean_up_secure(&expires_buf); |
| 545 |
return *node ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 546 |
} |
| 547 |
|
| 548 |
int aws_login_token_write_token_file( |
| 549 |
const struct aws_login_token *token, |
| 550 |
struct aws_allocator *allocator, |
| 551 |
const struct aws_byte_buf *file_path) { |
| 552 |
bool success = false; |
| 553 |
struct aws_byte_buf output_buffer; |
| 554 |
AWS_ZERO_STRUCT(output_buffer); |
| 555 |
struct aws_json_value *login_json = NULL; |
| 556 |
struct aws_json_value *access_token = NULL; |
| 557 |
|
| 558 |
struct aws_string *mode = aws_string_new_from_c_str(allocator, "wb"); |
| 559 |
struct aws_string *file_path_string = aws_string_new_from_buf(allocator, file_path); |
| 560 |
|
| 561 |
FILE *cache_file = aws_fopen_safe(file_path_string, mode); |
| 562 |
if (!cache_file) { |
| 563 |
goto on_error; |
| 564 |
} |
| 565 |
|
| 566 |
access_token = aws_json_value_new_object(allocator); |
| 567 |
|
| 568 |
if (s_write_to_json_node( |
| 569 |
allocator, |
| 570 |
access_token, |
| 571 |
s_new_string_from_cursor_json_value, |
| 572 |
s_add_json_value_to_parent, |
| 573 |
aws_string_c_str(s_login_access_key_id_key), |
| 574 |
&token->access_key_id) || |
| 575 |
s_write_to_json_node( |
| 576 |
allocator, |
| 577 |
access_token, |
| 578 |
s_new_string_from_cursor_json_value, |
| 579 |
s_add_json_value_to_parent, |
| 580 |
aws_string_c_str(s_login_secret_access_key), |
| 581 |
&token->secret_access_key) || |
| 582 |
s_write_to_json_node( |
| 583 |
allocator, |
| 584 |
access_token, |
| 585 |
s_new_string_from_cursor_json_value, |
| 586 |
s_add_json_value_to_parent, |
| 587 |
aws_string_c_str(s_login_session_token_key), |
| 588 |
&token->session_token) || |
| 589 |
s_write_to_json_node( |
| 590 |
allocator, |
| 591 |
access_token, |
| 592 |
s_new_string_from_cursor_json_value, |
| 593 |
s_add_json_value_to_parent, |
| 594 |
aws_string_c_str(s_login_account_id_key), |
| 595 |
&token->account_id) || |
| 596 |
s_write_to_json_node( |
| 597 |
allocator, |
| 598 |
access_token, |
| 599 |
s_new_string_json_value_from_date_time, |
| 600 |
s_add_json_value_to_parent, |
| 601 |
aws_string_c_str(s_login_expires_at_key), |
| 602 |
&token->expires_at)) { |
| 603 |
aws_json_value_destroy(access_token); |
| 604 |
goto on_error; |
| 605 |
} |
| 606 |
|
| 607 |
login_json = aws_json_value_new_object(allocator); |
| 608 |
|
| 609 |
if (aws_json_value_add_to_object(login_json, aws_byte_cursor_from_string(s_login_access_token_key), access_token) || |
| 610 |
s_write_to_json_node( |
| 611 |
allocator, |
| 612 |
login_json, |
| 613 |
s_new_string_from_cursor_json_value, |
| 614 |
s_add_json_value_to_parent, |
| 615 |
aws_string_c_str(s_login_token_type_key), |
| 616 |
&token->token_type) || |
| 617 |
s_write_to_json_node( |
| 618 |
allocator, |
| 619 |
login_json, |
| 620 |
s_new_string_from_cursor_json_value, |
| 621 |
s_add_json_value_to_parent, |
| 622 |
aws_string_c_str(s_login_refresh_token_key), |
| 623 |
&token->refresh_token) || |
| 624 |
s_write_to_json_node( |
| 625 |
allocator, |
| 626 |
login_json, |
| 627 |
s_new_string_from_cursor_json_value, |
| 628 |
s_add_json_value_to_parent, |
| 629 |
aws_string_c_str(s_login_id_token_key), |
| 630 |
&token->id_token) || |
| 631 |
s_write_to_json_node( |
| 632 |
allocator, |
| 633 |
login_json, |
| 634 |
s_new_string_from_cursor_json_value, |
| 635 |
s_add_json_value_to_parent, |
| 636 |
aws_string_c_str(s_login_client_id_key), |
| 637 |
&token->client_id) || |
| 638 |
s_write_to_json_node( |
| 639 |
allocator, |
| 640 |
login_json, |
| 641 |
s_new_string_from_cursor_json_value, |
| 642 |
s_add_json_value_to_parent, |
| 643 |
aws_string_c_str(s_login_dpop_key), |
| 644 |
&token->dpop_key)) { |
| 645 |
aws_json_value_destroy(login_json); |
| 646 |
aws_json_value_destroy(access_token); |
| 647 |
goto on_error; |
| 648 |
} |
| 649 |
|
| 650 |
if (aws_byte_buf_init(&output_buffer, allocator, TOKEN_BUFFER_SIZE) || |
| 651 |
aws_byte_buf_append_json_string_formatted(login_json, &output_buffer)) { |
| 652 |
aws_byte_buf_clean_up_secure(&output_buffer); |
| 653 |
goto on_error; |
| 654 |
} |
| 655 |
|
| 656 |
int chars_written = fprintf(cache_file, PRInSTR, AWS_BYTE_BUF_PRI(output_buffer)); |
| 657 |
if (chars_written < 0) { |
| 658 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to write to cache file"); |
| 659 |
goto on_error; |
| 660 |
} |
| 661 |
|
| 662 |
if (fflush(cache_file) != 0) { |
| 663 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to flush cache file"); |
| 664 |
goto on_error; |
| 665 |
} |
| 666 |
|
| 667 |
success = true; |
| 668 |
goto on_finish; |
| 669 |
on_error: |
| 670 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_WRITE_TOKEN); |
| 671 |
on_finish: |
| 672 |
aws_byte_buf_clean_up_secure(&output_buffer); |
| 673 |
if (success) { |
| 674 |
aws_json_value_destroy(login_json); |
| 675 |
} |
| 676 |
if (cache_file) { |
| 677 |
fclose(cache_file); |
| 678 |
} |
| 679 |
aws_string_destroy(mode); |
| 680 |
aws_string_destroy(file_path_string); |
| 681 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 682 |
} |
| 683 |
|
| 684 |
typedef void(calculate_size_fn)(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output); |
| 685 |
typedef int(encode_fn)(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output); |
| 686 |
|
| 687 |
void s_base_64_url_length(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output) { |
| 688 |
struct aws_byte_cursor *input_cursor = input; |
| 689 |
size_t encoded_length; |
| 690 |
aws_base64_url_compute_encoded_len(input_cursor->len, &encoded_length); |
| 691 |
aws_byte_buf_init(output, allocator, encoded_length); |
| 692 |
} |
| 693 |
|
| 694 |
static int s_encode_base64_url(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output) { |
| 695 |
(void)allocator; |
| 696 |
struct aws_byte_cursor *input_cursor = input; |
| 697 |
return aws_base64_url_encode(input_cursor, output); |
| 698 |
} |
| 699 |
|
| 700 |
static void s_sha256_length(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output) { |
| 701 |
(void)input; |
| 702 |
aws_byte_buf_init(output, allocator, AWS_SHA256_LEN); |
| 703 |
} |
| 704 |
|
| 705 |
static int s_sha256_encode(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output) { |
| 706 |
struct aws_byte_cursor *input_cursor = input; |
| 707 |
return aws_sha256_compute(allocator, input_cursor, output, 0); |
| 708 |
} |
| 709 |
|
| 710 |
struct ecc_sign_input { |
| 711 |
struct aws_byte_cursor *message; |
| 712 |
struct aws_ecc_key_pair *private_key; |
| 713 |
}; |
| 714 |
|
| 715 |
static void s_ecc_signature_length(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output) { |
| 716 |
struct ecc_sign_input *ecc_input = input; |
| 717 |
size_t signature_length = aws_ecc_key_pair_signature_length(ecc_input->private_key); |
| 718 |
aws_byte_buf_init(output, allocator, signature_length); |
| 719 |
} |
| 720 |
|
| 721 |
static int s_ecc_sign(struct aws_allocator *allocator, void *input, struct aws_byte_buf *output) { |
| 722 |
(void)allocator; |
| 723 |
struct ecc_sign_input *ecc_input = input; |
| 724 |
return aws_ecc_key_pair_sign_message(ecc_input->private_key, ecc_input->message, output); |
| 725 |
} |
| 726 |
|
| 727 |
static int s_encode_buff( |
| 728 |
struct aws_allocator *allocator, |
| 729 |
void *input, |
| 730 |
struct aws_byte_buf *output, |
| 731 |
calculate_size_fn *size_fn, |
| 732 |
encode_fn encode_fn) { |
| 733 |
|
| 734 |
size_fn(allocator, input, output); |
| 735 |
if (encode_fn(allocator, input, output)) { |
| 736 |
aws_byte_buf_clean_up_secure(output); |
| 737 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to encode buffer"); |
| 738 |
return AWS_OP_ERR; |
| 739 |
} |
| 740 |
return AWS_OP_SUCCESS; |
| 741 |
} |
| 742 |
|
| 743 |
static struct aws_ecc_key_pair *s_get_ecc_key_pair( |
| 744 |
struct aws_allocator *allocator, |
| 745 |
struct aws_byte_cursor private_token_cursor) { |
| 746 |
struct aws_ecc_key_pair *private_key = NULL; |
| 747 |
|
| 748 |
struct aws_array_list pem_objects; |
| 749 |
AWS_ZERO_STRUCT(pem_objects); |
| 750 |
if (aws_pem_objects_init_from_file_contents(&pem_objects, allocator, private_token_cursor)) { |
| 751 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to initialize pem objects"); |
| 752 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_INVALID_PEM); |
| 753 |
goto on_finish; |
| 754 |
} |
| 755 |
|
| 756 |
size_t pem_objects_length = aws_array_list_length(&pem_objects); |
| 757 |
if (pem_objects_length != 1) { |
| 758 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: pem object should only have one entry"); |
| 759 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_INVALID_PEM); |
| 760 |
goto on_finish; |
| 761 |
} |
| 762 |
|
| 763 |
for (size_t i = 0; i < pem_objects_length; ++i) { |
| 764 |
struct aws_pem_object pem_object; |
| 765 |
AWS_ZERO_STRUCT(pem_object); |
| 766 |
aws_array_list_get_at(&pem_objects, &pem_object, i); |
| 767 |
if (pem_object.type == AWS_PEM_TYPE_EC_PRIVATE) { |
| 768 |
struct aws_byte_cursor private_key_cursor = aws_byte_cursor_from_buf(&pem_object.data); |
| 769 |
private_key = aws_ecc_key_pair_new_from_asn1(allocator, &private_key_cursor); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
if (!private_key) { |
| 774 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_INVALID_PEM); |
| 775 |
} |
| 776 |
on_finish: |
| 777 |
aws_pem_objects_clean_up(&pem_objects); |
| 778 |
return private_key; |
| 779 |
} |
| 780 |
|
| 781 |
static int build_header( |
| 782 |
struct aws_allocator *allocator, |
| 783 |
struct aws_ecc_key_pair *private_key, |
| 784 |
struct aws_byte_buf *header_buf) { |
| 785 |
|
| 786 |
struct aws_json_value *header = NULL; |
| 787 |
struct aws_json_value *jwk = NULL; |
| 788 |
|
| 789 |
struct aws_byte_cursor x_cursor = aws_byte_cursor_from_buf(&private_key->pub_x); |
| 790 |
struct aws_byte_buf x_byte_buf; |
| 791 |
AWS_ZERO_STRUCT(x_byte_buf); |
| 792 |
|
| 793 |
struct aws_byte_cursor y_cursor = aws_byte_cursor_from_buf(&private_key->pub_y); |
| 794 |
struct aws_byte_buf y_byte_buf; |
| 795 |
AWS_ZERO_STRUCT(y_byte_buf); |
| 796 |
|
| 797 |
if (s_encode_buff(allocator, &x_cursor, &x_byte_buf, s_base_64_url_length, s_encode_base64_url) || |
| 798 |
s_encode_buff(allocator, &y_cursor, &y_byte_buf, s_base_64_url_length, s_encode_base64_url)) { |
| 799 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to build dpop header"); |
| 800 |
goto on_finish; |
| 801 |
} |
| 802 |
|
| 803 |
struct aws_byte_cursor x_encoded_cursor = aws_byte_cursor_from_buf(&x_byte_buf); |
| 804 |
struct aws_byte_cursor y_encoded_cursor = aws_byte_cursor_from_buf(&y_byte_buf); |
| 805 |
|
| 806 |
header = aws_json_value_new_object(allocator); |
| 807 |
jwk = aws_json_value_new_object(allocator); |
| 808 |
if (!header || !jwk || aws_json_value_add_to_object(header, aws_byte_cursor_from_c_str("jwk"), jwk) || |
| 809 |
s_write_to_json_node( |
| 810 |
allocator, |
| 811 |
header, |
| 812 |
s_new_string_json_value, |
| 813 |
s_add_json_value_to_parent, |
| 814 |
aws_string_c_str(s_jwt_header_typ_key), |
| 815 |
s_jwt_header_typ_value) || |
| 816 |
s_write_to_json_node( |
| 817 |
allocator, |
| 818 |
header, |
| 819 |
s_new_string_json_value, |
| 820 |
s_add_json_value_to_parent, |
| 821 |
aws_string_c_str(s_jwt_header_alg_key), |
| 822 |
s_jwt_header_alg_value) || |
| 823 |
s_write_to_json_node( |
| 824 |
allocator, |
| 825 |
jwk, |
| 826 |
s_new_string_json_value, |
| 827 |
s_add_json_value_to_parent, |
| 828 |
aws_string_c_str(s_jwt_header_kty_key), |
| 829 |
s_jwt_header_kty_value) || |
| 830 |
s_write_to_json_node( |
| 831 |
allocator, |
| 832 |
jwk, |
| 833 |
s_new_string_from_cursor_json_value, |
| 834 |
s_add_json_value_to_parent, |
| 835 |
aws_string_c_str(s_jwt_header_x_nums_key), |
| 836 |
&x_encoded_cursor) || |
| 837 |
s_write_to_json_node( |
| 838 |
allocator, |
| 839 |
jwk, |
| 840 |
s_new_string_from_cursor_json_value, |
| 841 |
s_add_json_value_to_parent, |
| 842 |
aws_string_c_str(s_jwt_header_y_nums_key), |
| 843 |
&y_encoded_cursor) || |
| 844 |
s_write_to_json_node( |
| 845 |
allocator, |
| 846 |
jwk, |
| 847 |
s_new_string_json_value, |
| 848 |
s_add_json_value_to_parent, |
| 849 |
aws_string_c_str(s_jwt_header_crv_key), |
| 850 |
s_jwt_header_crv_value)) { |
| 851 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to build dpop header"); |
| 852 |
goto on_finish; |
| 853 |
} |
| 854 |
|
| 855 |
aws_byte_buf_init(header_buf, allocator, TOKEN_BUFFER_SIZE); |
| 856 |
aws_byte_buf_append_json_string(header, header_buf); |
| 857 |
on_finish: |
| 858 |
aws_byte_buf_clean_up(&x_byte_buf); |
| 859 |
aws_byte_buf_clean_up(&y_byte_buf); |
| 860 |
if (header) { |
| 861 |
aws_json_value_destroy(header); |
| 862 |
} |
| 863 |
return AWS_OP_SUCCESS; |
| 864 |
} |
| 865 |
|
| 866 |
static int build_payload( |
| 867 |
struct aws_allocator *allocator, |
| 868 |
const struct aws_byte_cursor *host, |
| 869 |
struct aws_byte_buf *payload_buf) { |
| 870 |
bool success = false; |
| 871 |
struct aws_json_value *payload = NULL; |
| 872 |
|
| 873 |
struct aws_uuid jwk_id; |
| 874 |
AWS_ZERO_STRUCT(jwk_id); |
| 875 |
aws_uuid_init(&jwk_id); |
| 876 |
struct aws_byte_buf jwk_id_buf; |
| 877 |
AWS_ZERO_STRUCT(jwk_id_buf); |
| 878 |
aws_byte_buf_init(&jwk_id_buf, allocator, AWS_UUID_STR_LEN); |
| 879 |
aws_uuid_to_str(&jwk_id, &jwk_id_buf); |
| 880 |
struct aws_byte_cursor jwk_id_cursor = aws_byte_cursor_from_buf(&jwk_id_buf); |
| 881 |
|
| 882 |
struct aws_date_time dt; |
| 883 |
AWS_ZERO_STRUCT(dt); |
| 884 |
aws_date_time_init_now(&dt); |
| 885 |
double iat_seconds = (int)aws_date_time_as_epoch_secs(&dt); |
| 886 |
|
| 887 |
payload = aws_json_value_new_object(allocator); |
| 888 |
if (!payload || |
| 889 |
s_write_to_json_node( |
| 890 |
allocator, |
| 891 |
payload, |
| 892 |
s_new_string_from_cursor_json_value, |
| 893 |
s_add_json_value_to_parent, |
| 894 |
aws_string_c_str(s_jwt_header_jti_key), |
| 895 |
&jwk_id_cursor) || |
| 896 |
s_write_to_json_node( |
| 897 |
allocator, |
| 898 |
payload, |
| 899 |
s_new_string_json_value, |
| 900 |
s_add_json_value_to_parent, |
| 901 |
aws_string_c_str(s_jwt_header_htm_key), |
| 902 |
s_jwt_header_htm_value) || |
| 903 |
s_write_to_json_node( |
| 904 |
allocator, |
| 905 |
payload, |
| 906 |
s_new_string_from_cursor_json_value, |
| 907 |
s_add_json_value_to_parent, |
| 908 |
aws_string_c_str(s_jwt_header_htu_key), |
| 909 |
host) || |
| 910 |
s_write_to_json_node( |
| 911 |
allocator, |
| 912 |
payload, |
| 913 |
s_new_number_json_value, |
| 914 |
s_add_json_value_to_parent, |
| 915 |
aws_string_c_str(s_jwt_header_iat_key), |
| 916 |
&iat_seconds)) { |
| 917 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to build dpop header"); |
| 918 |
goto on_finish; |
| 919 |
} |
| 920 |
|
| 921 |
aws_byte_buf_init(payload_buf, allocator, TOKEN_BUFFER_SIZE); |
| 922 |
aws_byte_buf_append_json_string(payload, payload_buf); |
| 923 |
success = true; |
| 924 |
on_finish: |
| 925 |
aws_byte_buf_clean_up(&jwk_id_buf); |
| 926 |
if (payload) { |
| 927 |
aws_json_value_destroy(payload); |
| 928 |
} |
| 929 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 930 |
} |
| 931 |
|
| 932 |
static int get_message( |
| 933 |
struct aws_allocator *allocator, |
| 934 |
struct aws_byte_cursor *header_cursor, |
| 935 |
struct aws_byte_cursor *payload_cursor, |
| 936 |
struct aws_byte_buf *message) { |
| 937 |
bool success = false; |
| 938 |
|
| 939 |
struct aws_byte_buf header_encoded_byte_buf; |
| 940 |
struct aws_byte_buf payload_encoded_byte_buf; |
| 941 |
if (s_encode_buff(allocator, header_cursor, &header_encoded_byte_buf, s_base_64_url_length, s_encode_base64_url) || |
| 942 |
s_encode_buff( |
| 943 |
allocator, payload_cursor, &payload_encoded_byte_buf, s_base_64_url_length, s_encode_base64_url)) { |
| 944 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to create message"); |
| 945 |
goto on_finish; |
| 946 |
} |
| 947 |
|
| 948 |
struct aws_byte_cursor period_delim = aws_byte_cursor_from_string(s_jwt_message_delim); |
| 949 |
struct aws_byte_cursor payload_encoded_cursor = aws_byte_cursor_from_buf(&payload_encoded_byte_buf); |
| 950 |
if (aws_byte_buf_init_copy_from_cursor(message, allocator, aws_byte_cursor_from_buf(&header_encoded_byte_buf)) || |
| 951 |
aws_byte_buf_append_dynamic(message, &period_delim) || |
| 952 |
aws_byte_buf_append_dynamic(message, &payload_encoded_cursor)) { |
| 953 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to create message"); |
| 954 |
goto on_finish; |
| 955 |
} |
| 956 |
|
| 957 |
success = true; |
| 958 |
on_finish: |
| 959 |
aws_byte_buf_clean_up_secure(&header_encoded_byte_buf); |
| 960 |
aws_byte_buf_clean_up_secure(&payload_encoded_byte_buf); |
| 961 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 962 |
} |
| 963 |
|
| 964 |
static int sign_message( |
| 965 |
struct aws_allocator *allocator, |
| 966 |
struct aws_byte_cursor *message_cursor, |
| 967 |
struct aws_ecc_key_pair *private_key, |
| 968 |
struct aws_byte_buf *signature_encoded) { |
| 969 |
bool success = false; |
| 970 |
|
| 971 |
struct aws_byte_buf message_sha256; |
| 972 |
AWS_ZERO_STRUCT(message_sha256); |
| 973 |
if (s_encode_buff(allocator, message_cursor, &message_sha256, s_sha256_length, s_sha256_encode)) { |
| 974 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed calculate SHA256 Digest"); |
| 975 |
goto on_finish; |
| 976 |
} |
| 977 |
|
| 978 |
struct aws_byte_cursor message_sha256_cursor = aws_byte_cursor_from_buf(&message_sha256); |
| 979 |
struct ecc_sign_input ecc_input = {.message = &message_sha256_cursor, .private_key = private_key}; |
| 980 |
struct aws_byte_buf signature; |
| 981 |
AWS_ZERO_STRUCT(signature); |
| 982 |
if (s_encode_buff(allocator, &ecc_input, &signature, s_ecc_signature_length, s_ecc_sign)) { |
| 983 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed sign digest"); |
| 984 |
goto on_finish; |
| 985 |
} |
| 986 |
|
| 987 |
struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_buf(&signature); |
| 988 |
uint8_t decoded[64] = {0}; |
| 989 |
struct aws_byte_buf decoded_buf = aws_byte_buf_from_empty_array(decoded, 64); |
| 990 |
if (aws_ecc_decode_signature_der_to_raw_padded(allocator, signature_cursor, &decoded_buf, PADDING_LENGTH)) { |
| 991 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to decode signature"); |
| 992 |
goto on_finish; |
| 993 |
} |
| 994 |
|
| 995 |
struct aws_byte_cursor rs_combination_cursor = aws_byte_cursor_from_buf(&decoded_buf); |
| 996 |
s_encode_buff(allocator, &rs_combination_cursor, signature_encoded, s_base_64_url_length, s_encode_base64_url); |
| 997 |
|
| 998 |
success = true; |
| 999 |
on_finish: |
| 1000 |
aws_byte_buf_clean_up_secure(&message_sha256); |
| 1001 |
aws_byte_buf_clean_up_secure(&signature); |
| 1002 |
aws_byte_buf_clean_up_secure(&decoded_buf); |
| 1003 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 1004 |
} |
| 1005 |
|
| 1006 |
int aws_login_token_get_dpop_header( |
| 1007 |
struct aws_allocator *allocator, |
| 1008 |
const struct aws_login_token *token, |
| 1009 |
struct aws_byte_cursor host, |
| 1010 |
struct aws_byte_buf *header_value) { |
| 1011 |
bool success = false; |
| 1012 |
|
| 1013 |
struct aws_byte_cursor private_token_cursor = token->dpop_key; |
| 1014 |
struct aws_ecc_key_pair *private_key = NULL; |
| 1015 |
struct aws_byte_buf header_buf; |
| 1016 |
struct aws_byte_buf payload_buf; |
| 1017 |
struct aws_byte_buf message; |
| 1018 |
struct aws_byte_buf signature; |
| 1019 |
AWS_ZERO_STRUCT(header_buf); |
| 1020 |
AWS_ZERO_STRUCT(payload_buf); |
| 1021 |
AWS_ZERO_STRUCT(message); |
| 1022 |
AWS_ZERO_STRUCT(signature); |
| 1023 |
|
| 1024 |
private_key = s_get_ecc_key_pair(allocator, private_token_cursor); |
| 1025 |
if (!private_key) { |
| 1026 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to initialize ecc key pair"); |
| 1027 |
goto on_error; |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (build_header(allocator, private_key, &header_buf) || build_payload(allocator, &host, &payload_buf)) { |
| 1031 |
goto on_error; |
| 1032 |
} |
| 1033 |
|
| 1034 |
struct aws_byte_cursor header_cursor = aws_byte_cursor_from_buf(&header_buf); |
| 1035 |
struct aws_byte_cursor payload_cursor = aws_byte_cursor_from_buf(&payload_buf); |
| 1036 |
get_message(allocator, &header_cursor, &payload_cursor, &message); |
| 1037 |
|
| 1038 |
struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message); |
| 1039 |
sign_message(allocator, &message_cursor, private_key, &signature); |
| 1040 |
|
| 1041 |
struct aws_byte_cursor period_delim = aws_byte_cursor_from_string(s_jwt_message_delim); |
| 1042 |
struct aws_byte_cursor signature_encoded_cursor = aws_byte_cursor_from_buf(&signature); |
| 1043 |
aws_byte_buf_init_copy_from_cursor(header_value, allocator, aws_byte_cursor_from_buf(&message)); |
| 1044 |
aws_byte_buf_append_dynamic(header_value, &period_delim); |
| 1045 |
aws_byte_buf_append_dynamic(header_value, &signature_encoded_cursor); |
| 1046 |
|
| 1047 |
success = true; |
| 1048 |
goto on_finish; |
| 1049 |
on_error: |
| 1050 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_CREATE_DPOP_HEADER); |
| 1051 |
on_finish: |
| 1052 |
if (private_key) { |
| 1053 |
aws_ecc_key_pair_release(private_key); |
| 1054 |
} |
| 1055 |
aws_byte_buf_clean_up_secure(&header_buf); |
| 1056 |
aws_byte_buf_clean_up_secure(&payload_buf); |
| 1057 |
aws_byte_buf_clean_up_secure(&message); |
| 1058 |
aws_byte_buf_clean_up_secure(&signature); |
| 1059 |
|
| 1060 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 1061 |
} |
| 1062 |
|
| 1063 |
int aws_login_token_get_body( |
| 1064 |
struct aws_allocator *allocator, |
| 1065 |
const struct aws_login_token *token, |
| 1066 |
struct aws_byte_buf *body_buf) { |
| 1067 |
bool success = false; |
| 1068 |
struct aws_json_value *body = aws_json_value_new_object(allocator); |
| 1069 |
|
| 1070 |
if (s_write_to_json_node( |
| 1071 |
allocator, |
| 1072 |
body, |
| 1073 |
s_new_string_from_cursor_json_value, |
| 1074 |
s_add_json_value_to_parent, |
| 1075 |
aws_string_c_str(s_login_client_id_key), |
| 1076 |
&token->client_id) || |
| 1077 |
s_write_to_json_node( |
| 1078 |
allocator, |
| 1079 |
body, |
| 1080 |
s_new_string_from_cursor_json_value, |
| 1081 |
s_add_json_value_to_parent, |
| 1082 |
aws_string_c_str(s_login_refresh_token_key), |
| 1083 |
&token->refresh_token) || |
| 1084 |
s_write_to_json_node( |
| 1085 |
allocator, |
| 1086 |
body, |
| 1087 |
s_new_string_json_value, |
| 1088 |
s_add_json_value_to_parent, |
| 1089 |
aws_string_c_str(s_login_grant_type_key), |
| 1090 |
s_login_grant_type_value)) { |
| 1091 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login token: failed to build body"); |
| 1092 |
goto on_error; |
| 1093 |
} |
| 1094 |
|
| 1095 |
aws_byte_buf_init(body_buf, allocator, TOKEN_BUFFER_SIZE); |
| 1096 |
aws_byte_buf_append_json_string(body, body_buf); |
| 1097 |
success = true; |
| 1098 |
goto on_finish; |
| 1099 |
on_error: |
| 1100 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_CREATE_REQUEST_BODY); |
| 1101 |
on_finish: |
| 1102 |
aws_json_value_destroy(body); |
| 1103 |
return success ? AWS_OP_SUCCESS : AWS_OP_ERR; |
| 1104 |
} |