Debian Codegraph @

crt/aws-c-auth/source/credentials.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/auth/credentials.h>
7
8 #include <aws/auth/private/sso_token_utils.h>
9 #include <aws/cal/ecc.h>
10 #include <aws/common/environment.h>
11 #include <aws/common/string.h>
12
13 /* aws ecc identity which contains the data needed to sign a Sigv4a AWS request */
14 struct aws_ecc_identity {
15 struct aws_string *access_key_id;
16 struct aws_string *session_token;
17 struct aws_ecc_key_pair *ecc_key;
18 };
19
20 /* aws credentials identity which contains the data needed to sign an authenticated AWS request */
21 struct aws_credentials_identity {
22 struct aws_string *access_key_id;
23 struct aws_string *secret_access_key;
24 struct aws_string *session_token;
25 struct aws_string *account_id;
26 };
27
28 /* aws_token identity contains only a token to represent token only identities like a bearer token. */
29 struct aws_token_identity {
30 struct aws_string *token;
31 };
32
33 enum aws_identity_type {
34 AWS_CREDENTIALS_IDENTITY,
35 TOKEN_IDENTITY,
36 ANONYMOUS_IDENTITY,
37 ECC_IDENTITY,
38 };
39
40 /*
41 * A structure that wraps the different types of credentials that the customer can provider to establish their
42 * identity.
43 */
44 struct aws_credentials {
45 struct aws_allocator *allocator;
46
47 struct aws_atomic_var ref_count;
48 /*
49 * A timepoint, in seconds since epoch, at which the credentials should no longer be used because they
50 * will have expired.
51 *
52 *
53 * The primary purpose of this value is to allow providers to communicate to the caching provider any
54 * additional constraints on how the sourced credentials should be used (STS). After refreshing the cached
55 * credentials, the caching provider uses the following calculation to determine the next requery time:
56 *
57 * next_requery_time = now + cached_expiration_config;
58 * if (cached_creds->expiration_timepoint_seconds < next_requery_time) {
59 * next_requery_time = cached_creds->expiration_timepoint_seconds;
60 *
61 * The cached provider may, at its discretion, use a smaller requery time to avoid edge-case scenarios where
62 * credential expiration becomes a race condition.
63 *
64 * The following leaf providers always set this value to UINT64_MAX (indefinite):
65 * static
66 * environment
67 * imds
68 * profile_config*
69 *
70 * * - profile_config may invoke sts which will use a non-max value
71 *
72 * The following leaf providers set this value to a sensible timepoint:
73 * sts - value is based on current time + options->duration_seconds
74 *
75 */
76 uint64_t expiration_timepoint_seconds;
77
78 enum aws_identity_type identity_type;
79 union {
80 struct aws_credentials_identity credentials_identity;
81 struct aws_token_identity token_identity;
82 struct aws_ecc_identity ecc_identity;
83 } identity;
84 };
85
86 /*
87 * Credentials API implementations
88 */
89
90 struct aws_credentials *aws_credentials_new(
91 struct aws_allocator *allocator,
92 struct aws_byte_cursor access_key_id_cursor,
93 struct aws_byte_cursor secret_access_key_cursor,
94 struct aws_byte_cursor session_token_cursor,
95 uint64_t expiration_timepoint_seconds) {
96
97 struct aws_byte_cursor account_id;
98 AWS_ZERO_STRUCT(account_id);
99 struct aws_credentials_options creds_option = {
100 .access_key_id_cursor = access_key_id_cursor,
101 .secret_access_key_cursor = secret_access_key_cursor,
102 .session_token_cursor = session_token_cursor,
103 .account_id_cursor = account_id,
104 .expiration_timepoint_seconds = expiration_timepoint_seconds,
105 };
106 return aws_credentials_new_with_options(allocator, &creds_option);
107 }
108 struct aws_credentials *aws_credentials_new_with_options(
109 struct aws_allocator *allocator,
110 const struct aws_credentials_options *options) {
111
112 if (options->access_key_id_cursor.ptr == NULL || options->access_key_id_cursor.len == 0) {
113 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
114 return NULL;
115 }
116
117 if (options->secret_access_key_cursor.ptr == NULL || options->secret_access_key_cursor.len == 0) {
118 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
119 return NULL;
120 }
121
122 struct aws_credentials *credentials = aws_mem_acquire(allocator, sizeof(struct aws_credentials));
123 if (credentials == NULL) {
124 return NULL;
125 }
126
127 AWS_ZERO_STRUCT(*credentials);
128
129 credentials->allocator = allocator;
130 aws_atomic_init_int(&credentials->ref_count, 1);
131 credentials->identity_type = AWS_CREDENTIALS_IDENTITY;
132 struct aws_credentials_identity *credentials_identity = &credentials->identity.credentials_identity;
133 credentials_identity->access_key_id =
134 aws_string_new_from_array(allocator, options->access_key_id_cursor.ptr, options->access_key_id_cursor.len);
135 if (credentials_identity->access_key_id == NULL) {
136 goto error;
137 }
138
139 credentials_identity->secret_access_key = aws_string_new_from_array(
140 allocator, options->secret_access_key_cursor.ptr, options->secret_access_key_cursor.len);
141 if (credentials_identity->secret_access_key == NULL) {
142 goto error;
143 }
144
145 if (options->session_token_cursor.ptr != NULL && options->session_token_cursor.len > 0) {
146 credentials_identity->session_token =
147 aws_string_new_from_array(allocator, options->session_token_cursor.ptr, options->session_token_cursor.len);
148 if (credentials_identity->session_token == NULL) {
149 goto error;
150 }
151 }
152
153 if (options->account_id_cursor.ptr != NULL && options->account_id_cursor.len > 0) {
154 credentials_identity->account_id =
155 aws_string_new_from_array(allocator, options->account_id_cursor.ptr, options->account_id_cursor.len);
156 if (credentials_identity->account_id == NULL) {
157 goto error;
158 }
159 }
160
161 credentials->expiration_timepoint_seconds = options->expiration_timepoint_seconds;
162
163 return credentials;
164
165 error:
166
167 aws_credentials_release(credentials);
168
169 return NULL;
170 }
171
172 struct aws_credentials *aws_credentials_new_anonymous(struct aws_allocator *allocator) {
173
174 struct aws_credentials *credentials = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials));
175
176 credentials->allocator = allocator;
177 credentials->identity_type = ANONYMOUS_IDENTITY;
178 aws_atomic_init_int(&credentials->ref_count, 1);
179
180 credentials->expiration_timepoint_seconds = UINT64_MAX;
181
182 return credentials;
183 }
184
185 static void s_aws_credentials_destroy(struct aws_credentials *credentials) {
186 if (credentials == NULL) {
187 return;
188 }
189 switch (credentials->identity_type) {
190 case AWS_CREDENTIALS_IDENTITY:
191 aws_string_destroy(credentials->identity.credentials_identity.access_key_id);
192 aws_string_destroy_secure(credentials->identity.credentials_identity.secret_access_key);
193 aws_string_destroy_secure(credentials->identity.credentials_identity.session_token);
194 aws_string_destroy_secure(credentials->identity.credentials_identity.account_id);
195 break;
196 case ECC_IDENTITY:
197 aws_string_destroy(credentials->identity.ecc_identity.access_key_id);
198 aws_string_destroy_secure(credentials->identity.ecc_identity.session_token);
199 aws_ecc_key_pair_release(credentials->identity.ecc_identity.ecc_key);
200 break;
201 case TOKEN_IDENTITY:
202 aws_string_destroy_secure(credentials->identity.token_identity.token);
203 break;
204 case ANONYMOUS_IDENTITY:
205 break;
206 }
207
208 aws_mem_release(credentials->allocator, credentials);
209 }
210
211 void aws_credentials_acquire(const struct aws_credentials *credentials) {
212 if (credentials == NULL) {
213 return;
214 }
215
216 aws_atomic_fetch_add((struct aws_atomic_var *)&credentials->ref_count, 1);
217 }
218
219 void aws_credentials_release(const struct aws_credentials *credentials) {
220 if (credentials == NULL) {
221 return;
222 }
223
224 size_t old_value = aws_atomic_fetch_sub((struct aws_atomic_var *)&credentials->ref_count, 1);
225 if (old_value == 1) {
226 s_aws_credentials_destroy((struct aws_credentials *)credentials);
227 }
228 }
229
230 static struct aws_byte_cursor s_empty_token_cursor = {
231 .ptr = NULL,
232 .len = 0,
233 };
234
235 struct aws_byte_cursor aws_credentials_get_access_key_id(const struct aws_credentials *credentials) {
236 switch (credentials->identity_type) {
237 case AWS_CREDENTIALS_IDENTITY:
238 if (credentials->identity.credentials_identity.access_key_id != NULL) {
239 return aws_byte_cursor_from_string(credentials->identity.credentials_identity.access_key_id);
240 }
241 break;
242 case ECC_IDENTITY:
243 if (credentials->identity.ecc_identity.access_key_id != NULL) {
244 return aws_byte_cursor_from_string(credentials->identity.ecc_identity.access_key_id);
245 }
246 break;
247 default:
248 break;
249 }
250 return s_empty_token_cursor;
251 }
252
253 struct aws_byte_cursor aws_credentials_get_secret_access_key(const struct aws_credentials *credentials) {
254 switch (credentials->identity_type) {
255 case AWS_CREDENTIALS_IDENTITY:
256 if (credentials->identity.credentials_identity.secret_access_key != NULL) {
257 return aws_byte_cursor_from_string(credentials->identity.credentials_identity.secret_access_key);
258 }
259 break;
260 default:
261 break;
262 }
263 return s_empty_token_cursor;
264 }
265
266 struct aws_byte_cursor aws_credentials_get_session_token(const struct aws_credentials *credentials) {
267 switch (credentials->identity_type) {
268 case AWS_CREDENTIALS_IDENTITY:
269 if (credentials->identity.credentials_identity.session_token != NULL) {
270 return aws_byte_cursor_from_string(credentials->identity.credentials_identity.session_token);
271 }
272 break;
273 case ECC_IDENTITY:
274 if (credentials->identity.ecc_identity.session_token != NULL) {
275 return aws_byte_cursor_from_string(credentials->identity.ecc_identity.session_token);
276 }
277 break;
278 default:
279 break;
280 }
281 return s_empty_token_cursor;
282 }
283
284 struct aws_byte_cursor aws_credentials_get_account_id(const struct aws_credentials *credentials) {
285 switch (credentials->identity_type) {
286 case AWS_CREDENTIALS_IDENTITY:
287 if (credentials->identity.credentials_identity.account_id != NULL) {
288 return aws_byte_cursor_from_string(credentials->identity.credentials_identity.account_id);
289 }
290 break;
291 default:
292 break;
293 }
294 return s_empty_token_cursor;
295 }
296
297 struct aws_byte_cursor aws_credentials_get_token(const struct aws_credentials *credentials) {
298 switch (credentials->identity_type) {
299 case TOKEN_IDENTITY:
300 if (credentials->identity.token_identity.token != NULL) {
301 return aws_byte_cursor_from_string(credentials->identity.token_identity.token);
302 }
303 break;
304 default:
305 break;
306 }
307 return s_empty_token_cursor;
308 }
309
310 uint64_t aws_credentials_get_expiration_timepoint_seconds(const struct aws_credentials *credentials) {
311 return credentials->expiration_timepoint_seconds;
312 }
313
314 struct aws_ecc_key_pair *aws_credentials_get_ecc_key_pair(const struct aws_credentials *credentials) {
315 if (credentials->identity_type == ECC_IDENTITY) {
316 return credentials->identity.ecc_identity.ecc_key;
317 }
318 return NULL;
319 }
320
321 bool aws_credentials_is_anonymous(const struct aws_credentials *credentials) {
322 AWS_PRECONDITION(credentials);
323 return credentials->identity_type == ANONYMOUS_IDENTITY;
324 }
325
326 struct aws_credentials *aws_credentials_new_from_string(
327 struct aws_allocator *allocator,
328 const struct aws_string *access_key_id,
329 const struct aws_string *secret_access_key,
330 const struct aws_string *session_token,
331 uint64_t expiration_timepoint_seconds) {
332
333 struct aws_byte_cursor access_key_cursor = aws_byte_cursor_from_string(access_key_id);
334 struct aws_byte_cursor secret_access_key_cursor = aws_byte_cursor_from_string(secret_access_key);
335 struct aws_byte_cursor session_token_cursor;
336 AWS_ZERO_STRUCT(session_token_cursor);
337
338 if (session_token) {
339 session_token_cursor = aws_byte_cursor_from_string(session_token);
340 }
341
342 return aws_credentials_new(
343 allocator, access_key_cursor, secret_access_key_cursor, session_token_cursor, expiration_timepoint_seconds);
344 }
345
346 struct aws_credentials *aws_credentials_new_ecc(
347 struct aws_allocator *allocator,
348 struct aws_byte_cursor access_key_id,
349 struct aws_ecc_key_pair *ecc_key,
350 struct aws_byte_cursor session_token,
351 uint64_t expiration_timepoint_in_seconds) {
352
353 if (access_key_id.len == 0 || ecc_key == NULL) {
354 AWS_LOGF_ERROR(AWS_LS_AUTH_GENERAL, "Provided credentials do not have a valid access_key_id or ecc_key");
355 return NULL;
356 }
357
358 struct aws_credentials *credentials = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials));
359 if (credentials == NULL) {
360 return NULL;
361 }
362
363 credentials->allocator = allocator;
364 credentials->expiration_timepoint_seconds = expiration_timepoint_in_seconds;
365 aws_atomic_init_int(&credentials->ref_count, 1);
366 aws_ecc_key_pair_acquire(ecc_key);
367 credentials->identity_type = ECC_IDENTITY;
368 credentials->identity.ecc_identity.ecc_key = ecc_key;
369
370 credentials->identity.ecc_identity.access_key_id =
371 aws_string_new_from_array(allocator, access_key_id.ptr, access_key_id.len);
372 if (credentials->identity.ecc_identity.access_key_id == NULL) {
373 goto on_error;
374 }
375
376 if (session_token.ptr != NULL && session_token.len > 0) {
377 credentials->identity.ecc_identity.session_token =
378 aws_string_new_from_array(allocator, session_token.ptr, session_token.len);
379 if (credentials->identity.ecc_identity.session_token == NULL) {
380 goto on_error;
381 }
382 }
383
384 return credentials;
385
386 on_error:
387
388 s_aws_credentials_destroy(credentials);
389
390 return NULL;
391 }
392
393 struct aws_credentials *aws_credentials_new_ecc_from_aws_credentials(
394 struct aws_allocator *allocator,
395 const struct aws_credentials *credentials) {
396
397 struct aws_ecc_key_pair *ecc_key = aws_ecc_key_pair_new_ecdsa_p256_key_from_aws_credentials(allocator, credentials);
398
399 if (ecc_key == NULL) {
400 return NULL;
401 }
402
403 struct aws_credentials *ecc_credentials = aws_credentials_new_ecc(
404 allocator,
405 aws_credentials_get_access_key_id(credentials),
406 ecc_key,
407 aws_credentials_get_session_token(credentials),
408 aws_credentials_get_expiration_timepoint_seconds(credentials));
409
410 aws_ecc_key_pair_release(ecc_key);
411
412 return ecc_credentials;
413 }
414
415 struct aws_credentials *aws_credentials_new_token(
416 struct aws_allocator *allocator,
417 struct aws_byte_cursor token,
418 uint64_t expiration_timepoint_in_seconds) {
419 if (token.ptr == NULL || token.len == 0) {
420 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
421 return NULL;
422 }
423 struct aws_credentials *credentials = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials));
424
425 credentials->allocator = allocator;
426 aws_atomic_init_int(&credentials->ref_count, 1);
427 credentials->identity_type = TOKEN_IDENTITY;
428 struct aws_token_identity *token_identity = &credentials->identity.token_identity;
429 token_identity->token = aws_string_new_from_array(allocator, token.ptr, token.len);
430 credentials->expiration_timepoint_seconds = expiration_timepoint_in_seconds;
431 return credentials;
432 }
433
434 /*
435 * global credentials provider APIs
436 */
437
438 void aws_credentials_provider_destroy(struct aws_credentials_provider *provider) {
439 if (provider != NULL) {
440 provider->vtable->destroy(provider);
441 }
442 }
443
444 struct aws_credentials_provider *aws_credentials_provider_release(struct aws_credentials_provider *provider) {
445 if (provider == NULL) {
446 return NULL;
447 }
448
449 size_t old_value = aws_atomic_fetch_sub(&provider->ref_count, 1);
450 if (old_value == 1) {
451 aws_credentials_provider_destroy(provider);
452 }
453
454 return NULL;
455 }
456
457 struct aws_credentials_provider *aws_credentials_provider_acquire(struct aws_credentials_provider *provider) {
458 if (provider == NULL) {
459 return NULL;
460 }
461
462 aws_atomic_fetch_add(&provider->ref_count, 1);
463
464 return provider;
465 }
466
467 int aws_credentials_provider_get_credentials(
468 struct aws_credentials_provider *provider,
469 aws_on_get_credentials_callback_fn callback,
470 void *user_data) {
471
472 AWS_ASSERT(provider->vtable->get_credentials);
473
474 return provider->vtable->get_credentials(provider, callback, user_data);
475 }