credentials.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/credentials.h>
  6. #include <aws/cal/ecc.h>
  7. #include <aws/common/environment.h>
  8. #include <aws/common/string.h>
  9. /*
  10. * A structure that wraps the public/private data needed to sign an authenticated AWS request
  11. */
  12. struct aws_credentials {
  13. struct aws_allocator *allocator;
  14. struct aws_atomic_var ref_count;
  15. struct aws_string *access_key_id;
  16. struct aws_string *secret_access_key;
  17. struct aws_string *session_token;
  18. /*
  19. * A timepoint, in seconds since epoch, at which the credentials should no longer be used because they
  20. * will have expired.
  21. *
  22. *
  23. * The primary purpose of this value is to allow providers to communicate to the caching provider any
  24. * additional constraints on how the sourced credentials should be used (STS). After refreshing the cached
  25. * credentials, the caching provider uses the following calculation to determine the next requery time:
  26. *
  27. * next_requery_time = now + cached_expiration_config;
  28. * if (cached_creds->expiration_timepoint_seconds < next_requery_time) {
  29. * next_requery_time = cached_creds->expiration_timepoint_seconds;
  30. *
  31. * The cached provider may, at its discretion, use a smaller requery time to avoid edge-case scenarios where
  32. * credential expiration becomes a race condition.
  33. *
  34. * The following leaf providers always set this value to UINT64_MAX (indefinite):
  35. * static
  36. * environment
  37. * imds
  38. * profile_config*
  39. *
  40. * * - profile_config may invoke sts which will use a non-max value
  41. *
  42. * The following leaf providers set this value to a sensible timepoint:
  43. * sts - value is based on current time + options->duration_seconds
  44. *
  45. */
  46. uint64_t expiration_timepoint_seconds;
  47. struct aws_ecc_key_pair *ecc_key;
  48. };
  49. /*
  50. * Credentials API implementations
  51. */
  52. struct aws_credentials *aws_credentials_new(
  53. struct aws_allocator *allocator,
  54. struct aws_byte_cursor access_key_id_cursor,
  55. struct aws_byte_cursor secret_access_key_cursor,
  56. struct aws_byte_cursor session_token_cursor,
  57. uint64_t expiration_timepoint_seconds) {
  58. if (access_key_id_cursor.ptr == NULL || access_key_id_cursor.len == 0) {
  59. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  60. return NULL;
  61. }
  62. if (secret_access_key_cursor.ptr == NULL || secret_access_key_cursor.len == 0) {
  63. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  64. return NULL;
  65. }
  66. struct aws_credentials *credentials = aws_mem_acquire(allocator, sizeof(struct aws_credentials));
  67. if (credentials == NULL) {
  68. return NULL;
  69. }
  70. AWS_ZERO_STRUCT(*credentials);
  71. credentials->allocator = allocator;
  72. aws_atomic_init_int(&credentials->ref_count, 1);
  73. credentials->access_key_id =
  74. aws_string_new_from_array(allocator, access_key_id_cursor.ptr, access_key_id_cursor.len);
  75. if (credentials->access_key_id == NULL) {
  76. goto error;
  77. }
  78. credentials->secret_access_key =
  79. aws_string_new_from_array(allocator, secret_access_key_cursor.ptr, secret_access_key_cursor.len);
  80. if (credentials->secret_access_key == NULL) {
  81. goto error;
  82. }
  83. if (session_token_cursor.ptr != NULL && session_token_cursor.len > 0) {
  84. credentials->session_token =
  85. aws_string_new_from_array(allocator, session_token_cursor.ptr, session_token_cursor.len);
  86. if (credentials->session_token == NULL) {
  87. goto error;
  88. }
  89. }
  90. credentials->expiration_timepoint_seconds = expiration_timepoint_seconds;
  91. return credentials;
  92. error:
  93. aws_credentials_release(credentials);
  94. return NULL;
  95. }
  96. struct aws_credentials *aws_credentials_new_anonymous(struct aws_allocator *allocator) {
  97. struct aws_credentials *credentials = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials));
  98. credentials->allocator = allocator;
  99. aws_atomic_init_int(&credentials->ref_count, 1);
  100. credentials->expiration_timepoint_seconds = UINT64_MAX;
  101. return credentials;
  102. }
  103. static void s_aws_credentials_destroy(struct aws_credentials *credentials) {
  104. if (credentials == NULL) {
  105. return;
  106. }
  107. if (credentials->access_key_id != NULL) {
  108. aws_string_destroy(credentials->access_key_id);
  109. }
  110. if (credentials->secret_access_key != NULL) {
  111. aws_string_destroy_secure(credentials->secret_access_key);
  112. }
  113. if (credentials->session_token != NULL) {
  114. aws_string_destroy_secure(credentials->session_token);
  115. }
  116. aws_ecc_key_pair_release(credentials->ecc_key);
  117. aws_mem_release(credentials->allocator, credentials);
  118. }
  119. void aws_credentials_acquire(const struct aws_credentials *credentials) {
  120. if (credentials == NULL) {
  121. return;
  122. }
  123. aws_atomic_fetch_add((struct aws_atomic_var *)&credentials->ref_count, 1);
  124. }
  125. void aws_credentials_release(const struct aws_credentials *credentials) {
  126. if (credentials == NULL) {
  127. return;
  128. }
  129. size_t old_value = aws_atomic_fetch_sub((struct aws_atomic_var *)&credentials->ref_count, 1);
  130. if (old_value == 1) {
  131. s_aws_credentials_destroy((struct aws_credentials *)credentials);
  132. }
  133. }
  134. static struct aws_byte_cursor s_empty_token_cursor = {
  135. .ptr = NULL,
  136. .len = 0,
  137. };
  138. struct aws_byte_cursor aws_credentials_get_access_key_id(const struct aws_credentials *credentials) {
  139. if (credentials->access_key_id == NULL) {
  140. return s_empty_token_cursor;
  141. }
  142. return aws_byte_cursor_from_string(credentials->access_key_id);
  143. }
  144. struct aws_byte_cursor aws_credentials_get_secret_access_key(const struct aws_credentials *credentials) {
  145. if (credentials->secret_access_key == NULL) {
  146. return s_empty_token_cursor;
  147. }
  148. return aws_byte_cursor_from_string(credentials->secret_access_key);
  149. }
  150. struct aws_byte_cursor aws_credentials_get_session_token(const struct aws_credentials *credentials) {
  151. if (credentials->session_token != NULL) {
  152. return aws_byte_cursor_from_string(credentials->session_token);
  153. }
  154. return s_empty_token_cursor;
  155. }
  156. uint64_t aws_credentials_get_expiration_timepoint_seconds(const struct aws_credentials *credentials) {
  157. return credentials->expiration_timepoint_seconds;
  158. }
  159. struct aws_ecc_key_pair *aws_credentials_get_ecc_key_pair(const struct aws_credentials *credentials) {
  160. return credentials->ecc_key;
  161. }
  162. bool aws_credentials_is_anonymous(const struct aws_credentials *credentials) {
  163. AWS_PRECONDITION(credentials);
  164. return credentials->access_key_id == NULL && credentials->secret_access_key == NULL;
  165. }
  166. struct aws_credentials *aws_credentials_new_from_string(
  167. struct aws_allocator *allocator,
  168. const struct aws_string *access_key_id,
  169. const struct aws_string *secret_access_key,
  170. const struct aws_string *session_token,
  171. uint64_t expiration_timepoint_seconds) {
  172. struct aws_byte_cursor access_key_cursor = aws_byte_cursor_from_string(access_key_id);
  173. struct aws_byte_cursor secret_access_key_cursor = aws_byte_cursor_from_string(secret_access_key);
  174. struct aws_byte_cursor session_token_cursor;
  175. AWS_ZERO_STRUCT(session_token_cursor);
  176. if (session_token) {
  177. session_token_cursor = aws_byte_cursor_from_string(session_token);
  178. }
  179. return aws_credentials_new(
  180. allocator, access_key_cursor, secret_access_key_cursor, session_token_cursor, expiration_timepoint_seconds);
  181. }
  182. struct aws_credentials *aws_credentials_new_ecc(
  183. struct aws_allocator *allocator,
  184. struct aws_byte_cursor access_key_id,
  185. struct aws_ecc_key_pair *ecc_key,
  186. struct aws_byte_cursor session_token,
  187. uint64_t expiration_timepoint_in_seconds) {
  188. if (access_key_id.len == 0 || ecc_key == NULL) {
  189. AWS_LOGF_ERROR(AWS_LS_AUTH_GENERAL, "Provided credentials do not have a valid access_key_id or ecc_key");
  190. return NULL;
  191. }
  192. struct aws_credentials *credentials = aws_mem_calloc(allocator, 1, sizeof(struct aws_credentials));
  193. if (credentials == NULL) {
  194. return NULL;
  195. }
  196. credentials->allocator = allocator;
  197. credentials->expiration_timepoint_seconds = expiration_timepoint_in_seconds;
  198. aws_atomic_init_int(&credentials->ref_count, 1);
  199. aws_ecc_key_pair_acquire(ecc_key);
  200. credentials->ecc_key = ecc_key;
  201. credentials->access_key_id = aws_string_new_from_array(allocator, access_key_id.ptr, access_key_id.len);
  202. if (credentials->access_key_id == NULL) {
  203. goto on_error;
  204. }
  205. if (session_token.ptr != NULL && session_token.len > 0) {
  206. credentials->session_token = aws_string_new_from_array(allocator, session_token.ptr, session_token.len);
  207. if (credentials->session_token == NULL) {
  208. goto on_error;
  209. }
  210. }
  211. return credentials;
  212. on_error:
  213. s_aws_credentials_destroy(credentials);
  214. return NULL;
  215. }
  216. struct aws_credentials *aws_credentials_new_ecc_from_aws_credentials(
  217. struct aws_allocator *allocator,
  218. const struct aws_credentials *credentials) {
  219. struct aws_ecc_key_pair *ecc_key = aws_ecc_key_pair_new_ecdsa_p256_key_from_aws_credentials(allocator, credentials);
  220. if (ecc_key == NULL) {
  221. return NULL;
  222. }
  223. struct aws_credentials *ecc_credentials = aws_credentials_new_ecc(
  224. allocator,
  225. aws_credentials_get_access_key_id(credentials),
  226. ecc_key,
  227. aws_credentials_get_session_token(credentials),
  228. aws_credentials_get_expiration_timepoint_seconds(credentials));
  229. aws_ecc_key_pair_release(ecc_key);
  230. return ecc_credentials;
  231. }
  232. /*
  233. * global credentials provider APIs
  234. */
  235. void aws_credentials_provider_destroy(struct aws_credentials_provider *provider) {
  236. if (provider != NULL) {
  237. provider->vtable->destroy(provider);
  238. }
  239. }
  240. struct aws_credentials_provider *aws_credentials_provider_release(struct aws_credentials_provider *provider) {
  241. if (provider == NULL) {
  242. return NULL;
  243. }
  244. size_t old_value = aws_atomic_fetch_sub(&provider->ref_count, 1);
  245. if (old_value == 1) {
  246. aws_credentials_provider_destroy(provider);
  247. }
  248. return NULL;
  249. }
  250. struct aws_credentials_provider *aws_credentials_provider_acquire(struct aws_credentials_provider *provider) {
  251. if (provider == NULL) {
  252. return NULL;
  253. }
  254. aws_atomic_fetch_add(&provider->ref_count, 1);
  255. return provider;
  256. }
  257. int aws_credentials_provider_get_credentials(
  258. struct aws_credentials_provider *provider,
  259. aws_on_get_credentials_callback_fn callback,
  260. void *user_data) {
  261. AWS_ASSERT(provider->vtable->get_credentials);
  262. return provider->vtable->get_credentials(provider, callback, user_data);
  263. }