aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-auth/source/signing.c
blob: 1182de0fb55e1b4559708b5f6159944462889029 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/auth/signing.h>

#include <aws/auth/credentials.h>
#include <aws/auth/private/aws_signing.h>
#include <aws/io/uri.h>

/*
 * Aws signing implementation
 */

static int s_aws_last_error_or_unknown(void) {
    int last_error = aws_last_error();
    if (last_error == AWS_ERROR_SUCCESS) {
        last_error = AWS_ERROR_UNKNOWN;
    }

    return last_error;
}

static void s_perform_signing(struct aws_signing_state_aws *state) {
    struct aws_signing_result *result = NULL;

    if (state->error_code != AWS_ERROR_SUCCESS) {
        goto done;
    }

    if (aws_credentials_is_anonymous(state->config.credentials)) {
        result = &state->result;
        goto done;
    }

    if (aws_signing_build_canonical_request(state)) {
        state->error_code = s_aws_last_error_or_unknown();
        AWS_LOGF_ERROR(
            AWS_LS_AUTH_SIGNING,
            "(id=%p) Signing failed to build canonical request via algorithm %s, error %d(%s)",
            (void *)state->signable,
            aws_signing_algorithm_to_string(state->config.algorithm),
            state->error_code,
            aws_error_debug_str(state->error_code));
        goto done;
    }

    AWS_LOGF_INFO(
        AWS_LS_AUTH_SIGNING,
        "(id=%p) Signing successfully built canonical request for algorithm %s, with contents \n" PRInSTR "\n",
        (void *)state->signable,
        aws_signing_algorithm_to_string(state->config.algorithm),
        AWS_BYTE_BUF_PRI(state->canonical_request));

    if (aws_signing_build_string_to_sign(state)) {
        state->error_code = s_aws_last_error_or_unknown();
        AWS_LOGF_ERROR(
            AWS_LS_AUTH_SIGNING,
            "(id=%p) Signing failed to build string-to-sign via algorithm %s, error %d(%s)",
            (void *)state->signable,
            aws_signing_algorithm_to_string(state->config.algorithm),
            state->error_code,
            aws_error_debug_str(state->error_code));
        goto done;
    }

    AWS_LOGF_INFO(
        AWS_LS_AUTH_SIGNING,
        "(id=%p) Signing successfully built string-to-sign via algorithm %s, with contents \n" PRInSTR "\n",
        (void *)state->signable,
        aws_signing_algorithm_to_string(state->config.algorithm),
        AWS_BYTE_BUF_PRI(state->string_to_sign));

    if (aws_signing_build_authorization_value(state)) {
        state->error_code = s_aws_last_error_or_unknown();
        AWS_LOGF_ERROR(
            AWS_LS_AUTH_SIGNING,
            "(id=%p) Signing failed to build final authorization value via algorithm %s",
            (void *)state->signable,
            aws_signing_algorithm_to_string(state->config.algorithm));
        goto done;
    }

    result = &state->result;

done:

    state->on_complete(result, state->error_code, state->userdata);
    aws_signing_state_destroy(state);
}

static void s_aws_signing_on_get_credentials(struct aws_credentials *credentials, int error_code, void *user_data) {
    struct aws_signing_state_aws *state = user_data;

    if (!credentials) {
        if (error_code == AWS_ERROR_SUCCESS) {
            error_code = AWS_ERROR_UNKNOWN;
        }

        /* Log the credentials sourcing error */
        AWS_LOGF_ERROR(
            AWS_LS_AUTH_SIGNING,
            "(id=%p) Credentials Provider failed to source credentials with error %d(%s)",
            (void *)state->signable,
            error_code,
            aws_error_debug_str(error_code));

        state->error_code = AWS_AUTH_SIGNING_NO_CREDENTIALS;
    } else {
        if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC &&
            !aws_credentials_is_anonymous(credentials)) {

            state->config.credentials = aws_credentials_new_ecc_from_aws_credentials(state->allocator, credentials);
            if (state->config.credentials == NULL) {
                state->error_code = AWS_AUTH_SIGNING_NO_CREDENTIALS;
            }
        } else {
            state->config.credentials = credentials;
            aws_credentials_acquire(credentials);
        }
    }

    s_perform_signing(state);
}

int aws_sign_request_aws(
    struct aws_allocator *allocator,
    const struct aws_signable *signable,
    const struct aws_signing_config_base *base_config,
    aws_signing_complete_fn *on_complete,
    void *userdata) {

    AWS_PRECONDITION(base_config);

    if (base_config->config_type != AWS_SIGNING_CONFIG_AWS) {
        return aws_raise_error(AWS_AUTH_SIGNING_MISMATCHED_CONFIGURATION);
    }

    const struct aws_signing_config_aws *config = (void *)base_config;

    struct aws_signing_state_aws *signing_state =
        aws_signing_state_new(allocator, config, signable, on_complete, userdata);
    if (!signing_state) {
        return AWS_OP_ERR;
    }

    if (signing_state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) {
        if (signing_state->config.credentials != NULL &&
            !aws_credentials_is_anonymous(signing_state->config.credentials)) {
            /*
             * If these are regular credentials, try to derive ecc-based ones
             */
            if (aws_credentials_get_ecc_key_pair(signing_state->config.credentials) == NULL) {
                struct aws_credentials *ecc_credentials =
                    aws_credentials_new_ecc_from_aws_credentials(allocator, signing_state->config.credentials);
                aws_credentials_release(signing_state->config.credentials);
                signing_state->config.credentials = ecc_credentials;
                if (signing_state->config.credentials == NULL) {
                    goto on_error;
                }
            }
        }
    }

    bool can_sign_immediately = signing_state->config.credentials != NULL;

    if (can_sign_immediately) {
        s_perform_signing(signing_state);
    } else {
        if (aws_credentials_provider_get_credentials(
                signing_state->config.credentials_provider, s_aws_signing_on_get_credentials, signing_state)) {
            goto on_error;
        }
    }

    return AWS_OP_SUCCESS;

on_error:

    aws_signing_state_destroy(signing_state);
    return AWS_OP_ERR;
}