| 1 | #ifndef AWS_AUTH_SIGNING_CONFIG_H |
| 2 | #define AWS_AUTH_SIGNING_CONFIG_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/auth/auth.h> |
| 10 | |
| 11 | #include <aws/common/byte_buf.h> |
| 12 | #include <aws/common/date_time.h> |
| 13 | |
| 14 | AWS_PUSH_SANE_WARNING_LEVEL |
| 15 | |
| 16 | struct aws_credentials; |
| 17 | |
| 18 | typedef bool(aws_should_sign_header_fn)(const struct aws_byte_cursor *name, void *userdata); |
| 19 | |
| 20 | /** |
| 21 | * A primitive RTTI indicator for signing configuration structs |
| 22 | * |
| 23 | * There must be one entry per config structure type and it's a fatal error |
| 24 | * to put the wrong value in the "config_type" member of your config structure. |
| 25 | */ |
| 26 | enum aws_signing_config_type { AWS_SIGNING_CONFIG_AWS = 1 }; |
| 27 | |
| 28 | /** |
| 29 | * All signing configuration structs must match this by having |
| 30 | * the config_type member as the first member. |
| 31 | */ |
| 32 | struct aws_signing_config_base { |
| 33 | enum aws_signing_config_type config_type; |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * What version of the AWS signing process should we use. |
| 38 | */ |
| 39 | enum aws_signing_algorithm { |
| 40 | AWS_SIGNING_ALGORITHM_V4, |
| 41 | AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC, |
| 42 | AWS_SIGNING_ALGORITHM_V4_S3EXPRESS, |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * What sort of signature should be computed from the signable? |
| 47 | */ |
| 48 | enum aws_signature_type { |
| 49 | /** |
| 50 | * A signature for a full http request should be computed, with header updates applied to the signing result. |
| 51 | */ |
| 52 | AWS_ST_HTTP_REQUEST_HEADERS, |
| 53 | |
| 54 | /** |
| 55 | * A signature for a full http request should be computed, with query param updates applied to the signing result. |
| 56 | */ |
| 57 | AWS_ST_HTTP_REQUEST_QUERY_PARAMS, |
| 58 | |
| 59 | /** |
| 60 | * Compute a signature for a payload chunk. The signable's input stream should be the chunk data and the |
| 61 | * signable should contain the most recent signature value (either the original http request or the most recent |
| 62 | * chunk) in the "previous-signature" property. |
| 63 | */ |
| 64 | AWS_ST_HTTP_REQUEST_CHUNK, |
| 65 | |
| 66 | /** |
| 67 | * Compute a signature for an event stream event. The signable's input stream should be the encoded event-stream |
| 68 | * message (headers + payload), the signable should contain the most recent signature value (either the original |
| 69 | * http request or the most recent event) in the "previous-signature" property. |
| 70 | * |
| 71 | * This option is only supported for Sigv4 for now. |
| 72 | */ |
| 73 | AWS_ST_HTTP_REQUEST_EVENT, |
| 74 | |
| 75 | /** |
| 76 | * Compute a signature for an http request via it's already-computed canonical request. Only the authorization |
| 77 | * signature header is added to the signing result. |
| 78 | */ |
| 79 | AWS_ST_CANONICAL_REQUEST_HEADERS, |
| 80 | |
| 81 | /** |
| 82 | * Compute a signature for an http request via it's already-computed canonical request. Only the authorization |
| 83 | * signature query param is added to the signing result. |
| 84 | */ |
| 85 | AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS, |
| 86 | |
| 87 | /** |
| 88 | * Compute a signature for the trailing headers. |
| 89 | * the signable should contain the most recent signature value (either the original http request or the most recent |
| 90 | * chunk) in the "previous-signature" property. |
| 91 | */ |
| 92 | AWS_ST_HTTP_REQUEST_TRAILING_HEADERS |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * The SHA-256 of an empty string: |
| 97 | * 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' |
| 98 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 99 | */ |
| 100 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_empty_sha256; |
| 101 | |
| 102 | /** |
| 103 | * 'UNSIGNED-PAYLOAD' |
| 104 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 105 | */ |
| 106 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_unsigned_payload; |
| 107 | |
| 108 | /** |
| 109 | * 'STREAMING-UNSIGNED-PAYLOAD-TRAILER' |
| 110 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 111 | */ |
| 112 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_streaming_unsigned_payload_trailer; |
| 113 | |
| 114 | /** |
| 115 | * 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD' |
| 116 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 117 | */ |
| 118 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_hmac_sha256_payload; |
| 119 | |
| 120 | /** |
| 121 | * 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER' |
| 122 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 123 | */ |
| 124 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_hmac_sha256_payload_trailer; |
| 125 | |
| 126 | /** |
| 127 | * 'STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD' |
| 128 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 129 | */ |
| 130 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload; |
| 131 | |
| 132 | /** |
| 133 | * 'STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER' |
| 134 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 135 | */ |
| 136 | AWS_AUTH_API extern const struct aws_byte_cursor |
| 137 | g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload_trailer; |
| 138 | |
| 139 | /** |
| 140 | * 'STREAMING-AWS4-HMAC-SHA256-EVENTS' |
| 141 | * For use with `aws_signing_config_aws.signed_body_value`. |
| 142 | * |
| 143 | * Event signing is only supported for Sigv4 for now. |
| 144 | */ |
| 145 | AWS_AUTH_API extern const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_hmac_sha256_events; |
| 146 | |
| 147 | /** |
| 148 | * Controls if signing adds a header containing the canonical request's body value |
| 149 | */ |
| 150 | enum aws_signed_body_header_type { |
| 151 | /** |
| 152 | * Do not add a header |
| 153 | */ |
| 154 | AWS_SBHT_NONE, |
| 155 | |
| 156 | /** |
| 157 | * Add the "x-amz-content-sha256" header with the canonical request's body value |
| 158 | */ |
| 159 | AWS_SBHT_X_AMZ_CONTENT_SHA256, |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * A configuration structure for use in AWS-related signing. Currently covers sigv4 only, but is not required to. |
| 164 | */ |
| 165 | struct aws_signing_config_aws { |
| 166 | |
| 167 | /** |
| 168 | * What kind of config structure is this? |
| 169 | */ |
| 170 | enum aws_signing_config_type config_type; |
| 171 | |
| 172 | /** |
| 173 | * What signing algorithm to use. |
| 174 | */ |
| 175 | enum aws_signing_algorithm algorithm; |
| 176 | |
| 177 | /** |
| 178 | * What sort of signature should be computed? |
| 179 | */ |
| 180 | enum aws_signature_type signature_type; |
| 181 | |
| 182 | /* |
| 183 | * Region-related configuration |
| 184 | * (1) If Sigv4, the region to sign against |
| 185 | * (2) If Sigv4a, the value of the X-amzn-region-set header (added in signing) |
| 186 | */ |
| 187 | struct aws_byte_cursor region; |
| 188 | |
| 189 | /** |
| 190 | * name of service to sign a request for |
| 191 | */ |
| 192 | struct aws_byte_cursor service; |
| 193 | |
| 194 | /** |
| 195 | * Raw date to use during the signing process. |
| 196 | */ |
| 197 | struct aws_date_time date; |
| 198 | |
| 199 | /** |
| 200 | * Optional function to control which headers are a part of the canonical request. |
| 201 | * Skipping auth-required headers will result in an unusable signature. Headers injected by the signing process |
| 202 | * are not skippable. |
| 203 | * |
| 204 | * This function does not override the internal check function (x-amzn-trace-id, user-agent), but rather |
| 205 | * supplements it. In particular, a header will get signed if and only if it returns true to both |
| 206 | * the internal check (skips x-amzn-trace-id, user-agent) and this function (if defined). |
| 207 | */ |
| 208 | aws_should_sign_header_fn *should_sign_header; |
| 209 | void *should_sign_header_ud; |
| 210 | |
| 211 | /* |
| 212 | * Put all flags in here at the end. If this grows, stay aware of bit-space overflow and ABI compatibility. |
| 213 | */ |
| 214 | struct { |
| 215 | /** |
| 216 | * We assume the uri will be encoded once in preparation for transmission. Certain services |
| 217 | * do not decode before checking signature, requiring us to actually double-encode the uri in the canonical |
| 218 | * request in order to pass a signature check. |
| 219 | */ |
| 220 | uint32_t use_double_uri_encode : 1; |
| 221 | |
| 222 | /** |
| 223 | * Controls whether or not the uri paths should be normalized when building the canonical request |
| 224 | */ |
| 225 | uint32_t should_normalize_uri_path : 1; |
| 226 | |
| 227 | /** |
| 228 | * Controls whether "X-Amz-Security-Token" is omitted from the canonical request. |
| 229 | * "X-Amz-Security-Token" is added during signing, as a header or |
| 230 | * query param, when credentials have a session token. |
| 231 | * If false (the default), this parameter is included in the canonical request. |
| 232 | * If true, this parameter is still added, but omitted from the canonical request. |
| 233 | */ |
| 234 | uint32_t omit_session_token : 1; |
| 235 | } flags; |
| 236 | |
| 237 | /** |
| 238 | * Optional string to use as the canonical request's body value. |
| 239 | * If string is empty, a value will be calculated from the payload during signing. |
| 240 | * Typically, this is the SHA-256 of the (request/chunk/event) payload, written as lowercase hex. |
| 241 | * If this has been precalculated, it can be set here. Special values used by certain services can also be set |
| 242 | * (e.g. "UNSIGNED-PAYLOAD" "STREAMING-AWS4-HMAC-SHA256-PAYLOAD" "STREAMING-AWS4-HMAC-SHA256-EVENTS"). |
| 243 | */ |
| 244 | struct aws_byte_cursor signed_body_value; |
| 245 | |
| 246 | /** |
| 247 | * Controls what body "hash" header, if any, should be added to the canonical request and the signed request: |
| 248 | * AWS_SBHT_NONE - no header should be added |
| 249 | * AWS_SBHT_X_AMZ_CONTENT_SHA256 - the body "hash" should be added in the X-Amz-Content-Sha256 header |
| 250 | */ |
| 251 | enum aws_signed_body_header_type signed_body_header; |
| 252 | |
| 253 | /* |
| 254 | * Signing key control: |
| 255 | * |
| 256 | * If "credentials" is valid: |
| 257 | * use it |
| 258 | * Else if "credentials_provider" is valid |
| 259 | * query credentials from the provider |
| 260 | * If sigv4a is being used |
| 261 | * use the ecc-based credentials derived from the query result |
| 262 | * Else |
| 263 | * use the query result |
| 264 | * Else |
| 265 | * fail |
| 266 | * |
| 267 | */ |
| 268 | |
| 269 | /* |
| 270 | * AWS Credentials to sign with. If Sigv4a is the algorithm and the credentials supplied are not ecc-based, |
| 271 | * a temporary ecc-based credentials object will be built and used instead. |
| 272 | */ |
| 273 | const struct aws_credentials *credentials; |
| 274 | |
| 275 | /* |
| 276 | * AWS credentials provider to fetch credentials from. If the signing algorithm is asymmetric sigv4, then the |
| 277 | * ecc-based credentials will be derived from the fetched credentials. |
| 278 | */ |
| 279 | struct aws_credentials_provider *credentials_provider; |
| 280 | |
| 281 | /** |
| 282 | * If non-zero and the signing transform is query param, then signing will add X-Amz-Expires to the query |
| 283 | * string, equal to the value specified here. If this value is zero or if header signing is being used then |
| 284 | * this parameter has no effect. |
| 285 | */ |
| 286 | uint64_t expiration_in_seconds; |
| 287 | }; |
| 288 | |
| 289 | AWS_EXTERN_C_BEGIN |
| 290 | |
| 291 | /** |
| 292 | * Returns a c-string that describes the supplied signing algorithm |
| 293 | * |
| 294 | * @param algorithm signing algorithm to get a friendly string name for |
| 295 | * |
| 296 | * @return friendly string name of the supplied algorithm, or "Unknown" if the algorithm is not recognized |
| 297 | */ |
| 298 | AWS_AUTH_API |
| 299 | const char *aws_signing_algorithm_to_string(enum aws_signing_algorithm algorithm); |
| 300 | |
| 301 | /** |
| 302 | * Checks a signing configuration for invalid settings combinations. |
| 303 | * |
| 304 | * @param config signing configuration to validate |
| 305 | * |
| 306 | * @return - AWS_OP_SUCCESS if the configuration is valid, AWS_OP_ERR otherwise |
| 307 | */ |
| 308 | AWS_AUTH_API |
| 309 | int aws_validate_aws_signing_config_aws(const struct aws_signing_config_aws *config); |
| 310 | |
| 311 | AWS_EXTERN_C_END |
| 312 | AWS_POP_SANE_WARNING_LEVEL |
| 313 | |
| 314 | #endif /* AWS_AUTH_SIGNING_CONFIG_H */ |