/src/curl/lib/http_aws_sigv4.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_AWS) |
27 | | |
28 | | #include "urldata.h" |
29 | | #include "strcase.h" |
30 | | #include "curlx/strdup.h" |
31 | | #include "http_aws_sigv4.h" |
32 | | #include "curl_sha256.h" |
33 | | #include "transfer.h" |
34 | | #include "curl_trc.h" |
35 | | #include "escape.h" |
36 | | #include "curlx/strparse.h" |
37 | | #include "slist.h" |
38 | | |
39 | | #include <time.h> |
40 | | |
41 | | #define HMAC_SHA256(k, kl, d, dl, o) \ |
42 | 12.6k | do { \ |
43 | 12.6k | result = Curl_hmacit(&Curl_HMAC_SHA256, \ |
44 | 12.6k | (const unsigned char *)(k), \ |
45 | 12.6k | kl, \ |
46 | 12.6k | (const unsigned char *)(d), \ |
47 | 12.6k | dl, o); \ |
48 | 12.6k | if(result) { \ |
49 | 0 | goto fail; \ |
50 | 0 | } \ |
51 | 12.6k | } while(0) |
52 | | |
53 | 2.74k | #define TIMESTAMP_SIZE 17 |
54 | | |
55 | | /* hex-encoded with null-terminator */ |
56 | 7.63k | #define SHA256_HEX_LENGTH ((2 * CURL_SHA256_DIGEST_LENGTH) + 1) |
57 | | |
58 | 2.51k | #define MAX_QUERY_COMPONENTS 128 |
59 | | |
60 | | struct pair { |
61 | | struct dynbuf key; |
62 | | struct dynbuf value; |
63 | | }; |
64 | | |
65 | | static void sha256_to_hex(char *dst, unsigned char *sha) |
66 | 7.53k | { |
67 | 7.53k | Curl_hexencode(sha, CURL_SHA256_DIGEST_LENGTH, |
68 | 7.53k | (unsigned char *)dst, SHA256_HEX_LENGTH); |
69 | 7.53k | } |
70 | | |
71 | | static char *find_date_hdr(struct Curl_easy *data, const char *sig_hdr) |
72 | 2.52k | { |
73 | 2.52k | char *tmp = Curl_checkheaders(data, sig_hdr, strlen(sig_hdr)); |
74 | | |
75 | 2.52k | if(tmp) |
76 | 21 | return tmp; |
77 | 2.50k | return Curl_checkheaders(data, STRCONST("Date")); |
78 | 2.52k | } |
79 | | |
80 | | /* remove whitespace, and lowercase all headers */ |
81 | | static void trim_headers(struct curl_slist *head) |
82 | 2.52k | { |
83 | 2.52k | struct curl_slist *l; |
84 | 18.1k | for(l = head; l; l = l->next) { |
85 | 15.6k | const char *value; /* to read from */ |
86 | 15.6k | char *store; |
87 | 15.6k | size_t colon = strcspn(l->data, ":"); |
88 | 15.6k | Curl_strntolower(l->data, l->data, colon); |
89 | | |
90 | 15.6k | value = &l->data[colon]; |
91 | 15.6k | if(!*value) |
92 | 0 | continue; |
93 | 15.6k | ++value; |
94 | 15.6k | store = (char *)CURL_UNCONST(value); |
95 | | |
96 | | /* skip leading whitespace */ |
97 | 15.6k | curlx_str_passblanks(&value); |
98 | | |
99 | 95.5k | while(*value) { |
100 | 79.8k | int space = 0; |
101 | 84.1k | while(ISBLANK(*value)) { |
102 | 4.33k | value++; |
103 | 4.33k | space++; |
104 | 4.33k | } |
105 | 79.8k | if(space) { |
106 | | /* replace any number of consecutive whitespace with a single space, |
107 | | unless at the end of the string, then nothing */ |
108 | 3.70k | if(*value) |
109 | 2.39k | *store++ = ' '; |
110 | 3.70k | } |
111 | 76.1k | else |
112 | 76.1k | *store++ = *value++; |
113 | 79.8k | } |
114 | 15.6k | *store = 0; /* null-terminate */ |
115 | 15.6k | } |
116 | 2.52k | } |
117 | | |
118 | | /* |
119 | | * Frees all allocated strings in a dynbuf pair array, and the dynbuf itself |
120 | | */ |
121 | | static void pair_array_free(struct pair *pair_array, size_t num_elements) |
122 | 501 | { |
123 | 501 | size_t index; |
124 | | |
125 | 2.88k | for(index = 0; index != num_elements; index++) { |
126 | 2.38k | curlx_dyn_free(&pair_array[index].key); |
127 | 2.38k | curlx_dyn_free(&pair_array[index].value); |
128 | 2.38k | } |
129 | 501 | } |
130 | | |
131 | | /* |
132 | | * Frees all allocated strings in a split dynbuf, and the dynbuf itself |
133 | | */ |
134 | | static void dyn_array_free(struct dynbuf *db, size_t num_elements) |
135 | 521 | { |
136 | 521 | size_t index; |
137 | | |
138 | 3.03k | for(index = 0; index < num_elements; index++) |
139 | 2.51k | curlx_dyn_free((&db[index])); |
140 | 521 | } |
141 | | |
142 | | /* |
143 | | * Splits source string by SPLIT_BY, and creates an array of dynbuf in db. |
144 | | * db is initialized by this function. |
145 | | * Caller is responsible for freeing the array elements with dyn_array_free |
146 | | */ |
147 | | |
148 | 149k | #define SPLIT_BY '&' |
149 | | |
150 | | static CURLcode split_to_dyn_array(const char *source, |
151 | | struct dynbuf db[MAX_QUERY_COMPONENTS], |
152 | | size_t *num_splits_out) |
153 | 521 | { |
154 | 521 | CURLcode result = CURLE_OK; |
155 | 521 | size_t len = strlen(source); |
156 | 521 | size_t pos; /* Position in result buffer */ |
157 | 521 | size_t start = 0; /* Start of current segment */ |
158 | 521 | size_t segment_length = 0; |
159 | 521 | size_t index = 0; |
160 | 521 | size_t num_splits = 0; |
161 | | |
162 | | /* Split source_ptr on SPLIT_BY and store the segment offsets and length in |
163 | | * array */ |
164 | 150k | for(pos = 0; pos < len; pos++) { |
165 | 149k | if(source[pos] == SPLIT_BY) { |
166 | 2.97k | if(segment_length) { |
167 | 2.03k | curlx_dyn_init(&db[index], segment_length + 1); |
168 | 2.03k | result = curlx_dyn_addn(&db[index], &source[start], segment_length); |
169 | 2.03k | if(result) |
170 | 0 | goto fail; |
171 | | |
172 | 2.03k | segment_length = 0; |
173 | 2.03k | index++; |
174 | 2.03k | if(++num_splits == MAX_QUERY_COMPONENTS) { |
175 | 1 | result = CURLE_TOO_LARGE; |
176 | 1 | goto fail; |
177 | 1 | } |
178 | 2.03k | } |
179 | 2.97k | start = pos + 1; |
180 | 2.97k | } |
181 | 146k | else { |
182 | 146k | segment_length++; |
183 | 146k | } |
184 | 149k | } |
185 | | |
186 | 520 | if(segment_length) { |
187 | 474 | curlx_dyn_init(&db[index], segment_length + 1); |
188 | 474 | result = curlx_dyn_addn(&db[index], &source[start], segment_length); |
189 | 474 | if(!result && ++num_splits == MAX_QUERY_COMPONENTS) |
190 | 0 | result = CURLE_TOO_LARGE; |
191 | 474 | } |
192 | 521 | fail: |
193 | 521 | *num_splits_out = num_splits; |
194 | 521 | return result; |
195 | 520 | } |
196 | | |
197 | | static bool is_reserved_char(const char c) |
198 | 1.42M | { |
199 | 1.42M | return (ISALNUM(c) || ISURLPUNTCS(c)); |
200 | 1.42M | } |
201 | | |
202 | | static CURLcode uri_encode_path(struct Curl_str *original_path, |
203 | | struct dynbuf *new_path) |
204 | 2.49k | { |
205 | 2.49k | const char *p = curlx_str(original_path); |
206 | 2.49k | size_t i; |
207 | | |
208 | 1.32M | for(i = 0; i < curlx_strlen(original_path); i++) { |
209 | | /* Do not encode slashes or unreserved chars from RFC 3986 */ |
210 | 1.31M | CURLcode result = CURLE_OK; |
211 | 1.31M | unsigned char c = p[i]; |
212 | 1.31M | if(is_reserved_char(c) || c == '/') |
213 | 889k | result = curlx_dyn_addn(new_path, &c, 1); |
214 | 429k | else |
215 | 429k | result = curlx_dyn_addf(new_path, "%%%02X", c); |
216 | 1.31M | if(result) |
217 | 0 | return result; |
218 | 1.31M | } |
219 | | |
220 | 2.49k | return CURLE_OK; |
221 | 2.49k | } |
222 | | |
223 | | /* Normalize the query part. Make sure %2B is left percent encoded, and not |
224 | | decoded to plus, then encoded to space. */ |
225 | | static CURLcode normalize_query(const char *string, size_t len, |
226 | | struct dynbuf *db) |
227 | 2.71k | { |
228 | 2.71k | CURLcode result = CURLE_OK; |
229 | | |
230 | 104k | while(len && !result) { |
231 | 101k | unsigned char in = (unsigned char)*string; |
232 | 101k | if(('%' == in) && (len > 2) && |
233 | 26.0k | ISXDIGIT(string[1]) && ISXDIGIT(string[2])) { |
234 | | /* this is two hexadecimal digits following a '%' */ |
235 | 21.3k | in = (unsigned char)((curlx_hexval(string[1]) << 4) | |
236 | 21.3k | curlx_hexval(string[2])); |
237 | 21.3k | string += 3; |
238 | 21.3k | len -= 3; |
239 | 21.3k | if(in == '+') { |
240 | | /* decodes to plus, so leave this encoded */ |
241 | 39 | result = curlx_dyn_addn(db, "%2B", 3); |
242 | 39 | continue; |
243 | 39 | } |
244 | 21.3k | } |
245 | 80.1k | else { |
246 | 80.1k | string++; |
247 | 80.1k | len--; |
248 | 80.1k | } |
249 | | |
250 | 101k | if(is_reserved_char(in)) |
251 | | /* Escape unreserved chars from RFC 3986 */ |
252 | 35.9k | result = curlx_dyn_addn(db, &in, 1); |
253 | 65.5k | else if(in == '+') |
254 | | /* Encode '+' as space */ |
255 | 632 | result = curlx_dyn_add(db, "%20"); |
256 | 64.9k | else |
257 | 64.9k | result = curlx_dyn_addf(db, "%%%02X", in); |
258 | 101k | } |
259 | | |
260 | 2.71k | return result; |
261 | 2.71k | } |
262 | | |
263 | | static bool should_urlencode(struct Curl_str *service_name) |
264 | 2.52k | { |
265 | | /* |
266 | | * These services require unmodified (not additionally URL-encoded) URL |
267 | | * paths. They are the S3 signing names, which have disableDoubleEncoding |
268 | | * set in the AWS S3 endpoint rules. Urls are already normalized by the curl |
269 | | * URL parser |
270 | | */ |
271 | 2.52k | if(curlx_str_cmp(service_name, "s3") || |
272 | 2.49k | curlx_str_cmp(service_name, "s3express") || |
273 | 2.49k | curlx_str_cmp(service_name, "s3-outposts") || |
274 | 2.49k | curlx_str_cmp(service_name, "s3-object-lambda")) { |
275 | 35 | return FALSE; |
276 | 35 | } |
277 | 2.49k | return TRUE; |
278 | 2.52k | } |
279 | | |
280 | | /* maximum length for the aws sivg4 parts */ |
281 | 13.8k | #define MAX_SIGV4_LEN 64 |
282 | 5.05k | #define DATE_HDR_KEY_LEN (MAX_SIGV4_LEN + sizeof("X--Date")) |
283 | | |
284 | | /* string been x-PROVIDER-date:TIMESTAMP, I need +1 for ':' */ |
285 | 2.52k | #define DATE_FULL_HDR_LEN (DATE_HDR_KEY_LEN + TIMESTAMP_SIZE + 1) |
286 | | |
287 | | /* alphabetically compare two headers by their name, expecting |
288 | | headers to use ':' at this point */ |
289 | | static int compare_header_names(const char *a, const char *b) |
290 | 752k | { |
291 | 752k | const char *colon_a; |
292 | 752k | const char *colon_b; |
293 | 752k | size_t len_a; |
294 | 752k | size_t len_b; |
295 | 752k | size_t min_len; |
296 | 752k | int cmp; |
297 | | |
298 | 752k | colon_a = strchr(a, ':'); |
299 | 752k | colon_b = strchr(b, ':'); |
300 | | |
301 | 752k | DEBUGASSERT(colon_a); |
302 | 752k | DEBUGASSERT(colon_b); |
303 | | |
304 | 752k | len_a = colon_a ? (size_t)(colon_a - a) : strlen(a); |
305 | 752k | len_b = colon_b ? (size_t)(colon_b - b) : strlen(b); |
306 | | |
307 | 752k | min_len = (len_a < len_b) ? len_a : len_b; |
308 | | |
309 | 752k | cmp = strncmp(a, b, min_len); |
310 | | |
311 | | /* return the shorter of the two if one is shorter */ |
312 | 752k | if(!cmp) |
313 | 509k | return (int)(len_a - len_b); |
314 | | |
315 | 242k | return cmp; |
316 | 752k | } |
317 | | |
318 | | /* Merge duplicate header definitions by comma delimiting their values |
319 | | in the order defined the headers are defined, expecting headers to |
320 | | be alpha-sorted and use ':' at this point */ |
321 | | static CURLcode merge_duplicate_headers(struct curl_slist *head) |
322 | 2.52k | { |
323 | 2.52k | struct curl_slist *curr = head; |
324 | 2.52k | CURLcode result = CURLE_OK; |
325 | | |
326 | 18.0k | while(curr) { |
327 | 17.9k | struct curl_slist *next = curr->next; |
328 | 17.9k | if(!next) |
329 | 2.50k | break; |
330 | | |
331 | 15.4k | if(compare_header_names(curr->data, next->data) == 0) { |
332 | 8.28k | struct dynbuf buf; |
333 | 8.28k | const char *colon_next; |
334 | 8.28k | const char *val_next; |
335 | | |
336 | 8.28k | curlx_dyn_init(&buf, CURL_MAX_HTTP_HEADER); |
337 | | |
338 | 8.28k | result = curlx_dyn_add(&buf, curr->data); |
339 | 8.28k | if(result) |
340 | 0 | return result; |
341 | | |
342 | 8.28k | colon_next = strchr(next->data, ':'); |
343 | 8.28k | DEBUGASSERT(colon_next); |
344 | 8.28k | val_next = colon_next + 1; |
345 | | |
346 | 8.28k | result = curlx_dyn_addn(&buf, ",", 1); |
347 | 8.28k | if(result) |
348 | 0 | return result; |
349 | | |
350 | 8.28k | result = curlx_dyn_add(&buf, val_next); |
351 | 8.28k | if(result) |
352 | 0 | return result; |
353 | | |
354 | 8.28k | curlx_free(curr->data); |
355 | 8.28k | curr->data = curlx_dyn_ptr(&buf); |
356 | | |
357 | 8.28k | curr->next = next->next; |
358 | 8.28k | curlx_free(next->data); |
359 | 8.28k | curlx_free(next); |
360 | 8.28k | } |
361 | 7.20k | else { |
362 | 7.20k | curr = curr->next; |
363 | 7.20k | } |
364 | 15.4k | } |
365 | | |
366 | 2.52k | return CURLE_OK; |
367 | 2.52k | } |
368 | | |
369 | | /* timestamp should point to a buffer of at last TIMESTAMP_SIZE bytes */ |
370 | | static CURLcode make_headers(struct Curl_easy *data, |
371 | | char *timestamp, |
372 | | const char *provider1, |
373 | | size_t plen, /* length of provider1 */ |
374 | | char **date_header, |
375 | | char *content_sha256_header, |
376 | | struct dynbuf *canonical_headers, |
377 | | struct dynbuf *signed_headers) |
378 | 2.52k | { |
379 | 2.52k | char date_hdr_key[DATE_HDR_KEY_LEN]; |
380 | 2.52k | char date_full_hdr[DATE_FULL_HDR_LEN]; |
381 | 2.52k | struct curl_slist *head = NULL; |
382 | 2.52k | struct curl_slist *tmp_head = NULL; |
383 | 2.52k | CURLcode result = CURLE_OUT_OF_MEMORY; |
384 | 2.52k | struct curl_slist *l; |
385 | 2.52k | bool again = TRUE; |
386 | | |
387 | 2.52k | curl_msnprintf(date_hdr_key, DATE_HDR_KEY_LEN, "X-%.*s-Date", |
388 | 2.52k | (int)plen, provider1); |
389 | | /* provider1 ucfirst */ |
390 | 2.52k | Curl_strntolower(&date_hdr_key[2], provider1, plen); |
391 | 2.52k | date_hdr_key[2] = Curl_raw_toupper(provider1[0]); |
392 | | |
393 | 2.52k | curl_msnprintf(date_full_hdr, DATE_FULL_HDR_LEN, |
394 | 2.52k | "x-%.*s-date:%s", (int)plen, provider1, timestamp); |
395 | | /* provider1 lowercase */ |
396 | 2.52k | Curl_strntolower(&date_full_hdr[2], provider1, plen); |
397 | | |
398 | 2.52k | if(!Curl_checkheaders(data, STRCONST("Host")) && |
399 | 2.33k | data->state.http_host) { |
400 | | /* Host: [host]:[port] */ |
401 | 2.23k | char *fullhost = curlx_strdup(data->state.http_host); |
402 | | |
403 | 2.23k | if(fullhost) |
404 | 2.23k | head = Curl_slist_append_nodup(NULL, fullhost); |
405 | 2.23k | if(!head) { |
406 | 0 | curlx_free(fullhost); |
407 | 0 | goto fail; |
408 | 0 | } |
409 | 2.23k | } |
410 | | |
411 | 2.52k | if(*content_sha256_header) { |
412 | 100 | tmp_head = curl_slist_append(head, content_sha256_header); |
413 | 100 | if(!tmp_head) |
414 | 0 | goto fail; |
415 | 100 | head = tmp_head; |
416 | 100 | } |
417 | | |
418 | | /* copy user headers to our header list. the logic is based on how http.c |
419 | | handles user headers. |
420 | | |
421 | | user headers in format 'name:' with no value are used to signal that an |
422 | | internal header of that name should be removed. those user headers are not |
423 | | added to this list. |
424 | | |
425 | | user headers in format 'name;' with no value are used to signal that a |
426 | | header of that name with no value should be sent. those user headers are |
427 | | added to this list but in the format that they will be sent, ie the |
428 | | semi-colon is changed to a colon for format 'name:'. |
429 | | |
430 | | user headers with a value of whitespace only, or without a colon or |
431 | | semi-colon, are not added to this list. |
432 | | */ |
433 | 20.0k | for(l = data->set.headers; l; l = l->next) { |
434 | 17.4k | char *dupdata; |
435 | 17.4k | const char *ptr; |
436 | 17.4k | const char *sep = strchr(l->data, ':'); |
437 | 17.4k | if(!sep) |
438 | 3.94k | sep = strchr(l->data, ';'); |
439 | 17.4k | if(!sep || (*sep == ':' && !*(sep + 1))) |
440 | 3.90k | continue; |
441 | 17.4k | for(ptr = sep + 1; ISBLANK(*ptr); ++ptr) |
442 | 3.83k | ; |
443 | 13.5k | if(!*ptr && ptr != sep + 1) /* a value of whitespace only */ |
444 | 255 | continue; |
445 | 13.3k | dupdata = curlx_strdup(l->data); |
446 | 13.3k | if(!dupdata) |
447 | 0 | goto fail; |
448 | 13.3k | dupdata[sep - l->data] = ':'; |
449 | 13.3k | tmp_head = Curl_slist_append_nodup(head, dupdata); |
450 | 13.3k | if(!tmp_head) { |
451 | 0 | curlx_free(dupdata); |
452 | 0 | goto fail; |
453 | 0 | } |
454 | 13.3k | head = tmp_head; |
455 | 13.3k | } |
456 | | |
457 | 2.52k | trim_headers(head); |
458 | | |
459 | 2.52k | *date_header = find_date_hdr(data, date_hdr_key); |
460 | 2.52k | if(!*date_header) { |
461 | 2.34k | tmp_head = curl_slist_append(head, date_full_hdr); |
462 | 2.34k | if(!tmp_head) |
463 | 0 | goto fail; |
464 | 2.34k | head = tmp_head; |
465 | 2.34k | *date_header = curl_maprintf("%s: %s\r\n", date_hdr_key, timestamp); |
466 | 2.34k | if(!*date_header) |
467 | 0 | goto fail; |
468 | 2.34k | } |
469 | 183 | else { |
470 | 183 | const char *value; |
471 | 183 | const char *endp; |
472 | 183 | value = strchr(*date_header, ':'); |
473 | 183 | if(!value) { |
474 | 3 | *date_header = NULL; |
475 | 3 | goto fail; |
476 | 3 | } |
477 | 180 | ++value; |
478 | 180 | curlx_str_passblanks(&value); |
479 | 180 | endp = value; |
480 | 1.66k | while(*endp && ISALNUM(*endp)) |
481 | 1.48k | ++endp; |
482 | | /* 16 bytes => "19700101T000000Z" */ |
483 | 180 | if((endp - value) == TIMESTAMP_SIZE - 1) { |
484 | 18 | memcpy(timestamp, value, TIMESTAMP_SIZE - 1); |
485 | 18 | timestamp[TIMESTAMP_SIZE - 1] = 0; |
486 | 18 | } |
487 | 162 | else |
488 | | /* bad timestamp length */ |
489 | 162 | timestamp[0] = 0; |
490 | 180 | *date_header = NULL; |
491 | 180 | } |
492 | | |
493 | | /* alpha-sort by header name in a case sensitive manner */ |
494 | 11.9k | do { |
495 | 11.9k | again = FALSE; |
496 | 760k | for(l = head; l; l = l->next) { |
497 | 748k | struct curl_slist *next = l->next; |
498 | | |
499 | 748k | if(next && compare_header_names(l->data, next->data) > 0) { |
500 | 166k | char *tmp = l->data; |
501 | | |
502 | 166k | l->data = next->data; |
503 | 166k | next->data = tmp; |
504 | 166k | again = TRUE; |
505 | 166k | } |
506 | 748k | } |
507 | 11.9k | } while(again); |
508 | | |
509 | 2.52k | result = merge_duplicate_headers(head); |
510 | 2.52k | if(result) |
511 | 0 | goto fail; |
512 | | |
513 | 12.2k | for(l = head; l; l = l->next) { |
514 | 9.70k | char *tmp; |
515 | | |
516 | 9.70k | if(curlx_dyn_add(canonical_headers, l->data)) |
517 | 0 | goto fail; |
518 | 9.70k | if(curlx_dyn_add(canonical_headers, "\n")) |
519 | 0 | goto fail; |
520 | | |
521 | 9.70k | tmp = strchr(l->data, ':'); |
522 | 9.70k | if(tmp) |
523 | 9.70k | *tmp = 0; |
524 | | |
525 | 9.70k | if(l != head && curlx_dyn_add(signed_headers, ";")) |
526 | 0 | goto fail; |
527 | 9.70k | if(curlx_dyn_add(signed_headers, l->data)) |
528 | 0 | goto fail; |
529 | 9.70k | } |
530 | | |
531 | 2.52k | result = CURLE_OK; |
532 | 2.52k | fail: |
533 | 2.52k | curl_slist_free_all(head); |
534 | | |
535 | 2.52k | return result; |
536 | 2.52k | } |
537 | | |
538 | 100 | #define CONTENT_SHA256_KEY_LEN (MAX_SIGV4_LEN + sizeof("X--Content-Sha256")) |
539 | | /* add 2 for ": " between header name and value */ |
540 | 100 | #define CONTENT_SHA256_HDR_LEN (CONTENT_SHA256_KEY_LEN + 2 + SHA256_HEX_LENGTH) |
541 | | |
542 | | /* try to parse a payload hash from the content-sha256 header */ |
543 | | static const char *parse_content_sha_hdr(struct Curl_easy *data, |
544 | | const char *provider1, |
545 | | size_t plen, |
546 | | size_t *value_len) |
547 | 2.52k | { |
548 | 2.52k | char key[CONTENT_SHA256_KEY_LEN]; |
549 | 2.52k | size_t key_len; |
550 | 2.52k | const char *value; |
551 | 2.52k | size_t len; |
552 | | |
553 | 2.52k | key_len = curl_msnprintf(key, sizeof(key), "x-%.*s-content-sha256", |
554 | 2.52k | (int)plen, provider1); |
555 | | |
556 | 2.52k | value = Curl_checkheaders(data, key, key_len); |
557 | 2.52k | if(!value) |
558 | 2.52k | return NULL; |
559 | | |
560 | 0 | value = strchr(value, ':'); |
561 | 0 | if(!value) |
562 | 0 | return NULL; |
563 | 0 | ++value; |
564 | |
|
565 | 0 | curlx_str_passblanks(&value); |
566 | |
|
567 | 0 | len = strlen(value); |
568 | 0 | while(len > 0 && ISBLANK(value[len - 1])) |
569 | 0 | --len; |
570 | |
|
571 | 0 | *value_len = len; |
572 | 0 | return value; |
573 | 0 | } |
574 | | |
575 | | static CURLcode calc_payload_hash(struct Curl_easy *data, |
576 | | unsigned char *sha_hash, char *sha_hex) |
577 | 2.48k | { |
578 | 2.48k | const char *post_data = data->set.postfields; |
579 | 2.48k | size_t post_data_len = 0; |
580 | 2.48k | CURLcode result; |
581 | | |
582 | 2.48k | if(post_data) { |
583 | 262 | if(data->set.postfieldsize < 0) |
584 | 262 | post_data_len = strlen(post_data); |
585 | 0 | else |
586 | 0 | post_data_len = (size_t)data->set.postfieldsize; |
587 | 262 | } |
588 | 2.48k | result = Curl_sha256it(sha_hash, (const unsigned char *)post_data, |
589 | 2.48k | post_data_len); |
590 | 2.48k | if(!result) |
591 | 2.48k | sha256_to_hex(sha_hex, sha_hash); |
592 | 2.48k | return result; |
593 | 2.48k | } |
594 | | |
595 | 45 | #define S3_UNSIGNED_PAYLOAD "UNSIGNED-PAYLOAD" |
596 | | |
597 | | static CURLcode calc_s3_payload_hash(struct Curl_easy *data, |
598 | | Curl_HttpReq httpreq, |
599 | | const char *provider1, |
600 | | size_t plen, |
601 | | unsigned char *sha_hash, |
602 | | char *sha_hex, char *header) |
603 | 100 | { |
604 | 100 | bool empty_method = (httpreq == HTTPREQ_GET || httpreq == HTTPREQ_HEAD); |
605 | | /* The request method or filesize indicate no request payload */ |
606 | 100 | bool empty_payload = (empty_method || data->set.filesize == 0); |
607 | | /* The POST payload is in memory */ |
608 | 100 | bool post_payload = (httpreq == HTTPREQ_POST && data->set.postfields); |
609 | 100 | CURLcode result = CURLE_OUT_OF_MEMORY; |
610 | | |
611 | 100 | if(empty_payload || post_payload) { |
612 | | /* Calculate a real hash when we know the request payload */ |
613 | 55 | result = calc_payload_hash(data, sha_hash, sha_hex); |
614 | 55 | if(result) |
615 | 0 | goto fail; |
616 | 55 | } |
617 | 45 | else { |
618 | | /* Fall back to s3's UNSIGNED-PAYLOAD */ |
619 | 45 | size_t len = CURL_CSTRLEN(S3_UNSIGNED_PAYLOAD); |
620 | 45 | DEBUGASSERT(len < SHA256_HEX_LENGTH); /* 16 < 65 */ |
621 | 45 | memcpy(sha_hex, S3_UNSIGNED_PAYLOAD, len); |
622 | 45 | sha_hex[len] = 0; |
623 | 45 | } |
624 | | |
625 | | /* format the required content-sha256 header */ |
626 | 100 | curl_msnprintf(header, CONTENT_SHA256_HDR_LEN, |
627 | 100 | "x-%.*s-content-sha256: %s", (int)plen, provider1, sha_hex); |
628 | | |
629 | 100 | result = CURLE_OK; |
630 | 100 | fail: |
631 | 100 | return result; |
632 | 100 | } |
633 | | |
634 | | static int compare_func(const void *a, const void *b) |
635 | 7.50k | { |
636 | 7.50k | const struct pair *aa = a; |
637 | 7.50k | const struct pair *bb = b; |
638 | 7.50k | const size_t aa_key_len = curlx_dyn_len(&aa->key); |
639 | 7.50k | const size_t bb_key_len = curlx_dyn_len(&bb->key); |
640 | 7.50k | const size_t aa_value_len = curlx_dyn_len(&aa->value); |
641 | 7.50k | const size_t bb_value_len = curlx_dyn_len(&bb->value); |
642 | 7.50k | int compare; |
643 | | |
644 | | /* If one element is empty, the other is always sorted higher */ |
645 | | |
646 | | /* Compare keys */ |
647 | 7.50k | if((aa_key_len == 0) && (bb_key_len == 0)) |
648 | 977 | return 0; |
649 | 6.53k | if(aa_key_len == 0) |
650 | 259 | return -1; |
651 | 6.27k | if(bb_key_len == 0) |
652 | 1.39k | return 1; |
653 | 4.88k | compare = strcmp(curlx_dyn_ptr(&aa->key), curlx_dyn_ptr(&bb->key)); |
654 | 4.88k | if(compare) { |
655 | 4.43k | return compare; |
656 | 4.43k | } |
657 | | |
658 | | /* Compare values */ |
659 | 441 | if((aa_value_len == 0) && (bb_value_len == 0)) |
660 | 0 | return 0; |
661 | 441 | if(aa_value_len == 0) |
662 | 0 | return -1; |
663 | 441 | if(bb_value_len == 0) |
664 | 0 | return 1; |
665 | 441 | compare = strcmp(curlx_dyn_ptr(&aa->value), curlx_dyn_ptr(&bb->value)); |
666 | | |
667 | 441 | return compare; |
668 | 441 | } |
669 | | |
670 | | /* @unittest 1979 */ |
671 | | UNITTEST CURLcode canon_path(const char *q, size_t len, |
672 | | struct dynbuf *new_path, |
673 | | bool do_uri_encode); |
674 | | UNITTEST CURLcode canon_path(const char *q, size_t len, |
675 | | struct dynbuf *new_path, |
676 | | bool do_uri_encode) |
677 | 2.52k | { |
678 | 2.52k | CURLcode result = CURLE_OK; |
679 | | |
680 | 2.52k | struct Curl_str original_path; |
681 | | |
682 | 2.52k | curlx_str_assign(&original_path, q, len); |
683 | | |
684 | | /* Normalized path will be either the same or shorter than the original |
685 | | * path, plus trailing slash */ |
686 | | |
687 | 2.52k | if(do_uri_encode) |
688 | 2.49k | result = uri_encode_path(&original_path, new_path); |
689 | 35 | else |
690 | 35 | result = curlx_dyn_addn(new_path, q, len); |
691 | | |
692 | 2.52k | if(!result && curlx_dyn_len(new_path) == 0) |
693 | 0 | result = curlx_dyn_add(new_path, "/"); |
694 | | |
695 | 2.52k | return result; |
696 | 2.52k | } |
697 | | |
698 | | /* @unittest 1980 */ |
699 | | UNITTEST CURLcode canon_query(const char *query, struct dynbuf *dq); |
700 | | UNITTEST CURLcode canon_query(const char *query, struct dynbuf *dq) |
701 | 2.52k | { |
702 | 2.52k | CURLcode result = CURLE_OK; |
703 | | |
704 | 2.52k | struct dynbuf query_array[MAX_QUERY_COMPONENTS]; |
705 | 2.52k | struct pair encoded_query_array[MAX_QUERY_COMPONENTS]; |
706 | 2.52k | size_t num_query_components; |
707 | 2.52k | size_t counted_query_components = 0; |
708 | 2.52k | size_t index; |
709 | | |
710 | 2.52k | if(!query) |
711 | 2.00k | return result; |
712 | | |
713 | 521 | result = split_to_dyn_array(query, &query_array[0], &num_query_components); |
714 | 521 | if(result) { |
715 | 1 | goto fail; |
716 | 1 | } |
717 | | |
718 | | /* Create list of pairs, each pair containing an encoded query |
719 | | * component */ |
720 | | |
721 | 2.90k | for(index = 0; index < num_query_components; index++) { |
722 | 2.38k | const char *in_key; |
723 | 2.38k | size_t in_key_len; |
724 | 2.38k | const char *offset; |
725 | 2.38k | size_t query_part_len = curlx_dyn_len(&query_array[index]); |
726 | 2.38k | const char *query_part = curlx_dyn_ptr(&query_array[index]); |
727 | | |
728 | 2.38k | in_key = query_part; |
729 | | |
730 | 2.38k | offset = strchr(query_part, '='); |
731 | | /* If there is no equals, this key has no value */ |
732 | 2.38k | if(!offset) { |
733 | 1.62k | in_key_len = strlen(in_key); |
734 | 1.62k | } |
735 | 756 | else { |
736 | 756 | in_key_len = offset - in_key; |
737 | 756 | } |
738 | | |
739 | 2.38k | curlx_dyn_init(&encoded_query_array[index].key, |
740 | 2.38k | (query_part_len * 3) + 1); |
741 | 2.38k | curlx_dyn_init(&encoded_query_array[index].value, |
742 | 2.38k | (query_part_len * 3) + 1); |
743 | 2.38k | counted_query_components++; |
744 | | |
745 | | /* Decode/encode the key */ |
746 | 2.38k | result = normalize_query(in_key, in_key_len, |
747 | 2.38k | &encoded_query_array[index].key); |
748 | 2.38k | if(result) { |
749 | 0 | goto fail; |
750 | 0 | } |
751 | | |
752 | | /* Decode/encode the value if it exists */ |
753 | 2.38k | if(offset && offset != (query_part + query_part_len - 1)) { |
754 | 328 | size_t in_value_len; |
755 | 328 | const char *in_value = offset + 1; |
756 | 328 | in_value_len = query_part + query_part_len - (offset + 1); |
757 | 328 | result = normalize_query(in_value, in_value_len, |
758 | 328 | &encoded_query_array[index].value); |
759 | 328 | if(result) { |
760 | 0 | goto fail; |
761 | 0 | } |
762 | 328 | } |
763 | 2.05k | else { |
764 | | /* If there is no value, the value is an empty string */ |
765 | 2.05k | curlx_dyn_init(&encoded_query_array[index].value, 2); |
766 | 2.05k | result = curlx_dyn_addn(&encoded_query_array[index].value, "", 1); |
767 | 2.05k | } |
768 | | |
769 | 2.38k | if(result) { |
770 | 0 | goto fail; |
771 | 0 | } |
772 | 2.38k | } |
773 | | |
774 | | /* Sort the encoded query components by key and value */ |
775 | 520 | qsort(&encoded_query_array, num_query_components, |
776 | 520 | sizeof(struct pair), compare_func); |
777 | | |
778 | | /* Append the query components together to make a full query string */ |
779 | 2.90k | for(index = 0; index < num_query_components; index++) { |
780 | | |
781 | 2.38k | if(index) |
782 | 1.88k | result = curlx_dyn_addn(dq, "&", 1); |
783 | 2.38k | if(!result) { |
784 | 2.38k | const char *key_ptr = curlx_dyn_ptr(&encoded_query_array[index].key); |
785 | 2.38k | const char *value_ptr = curlx_dyn_ptr(&encoded_query_array[index].value); |
786 | 2.38k | size_t vlen = curlx_dyn_len(&encoded_query_array[index].value); |
787 | 2.38k | if(value_ptr && vlen) { |
788 | 2.38k | result = curlx_dyn_addf(dq, "%s=%s", key_ptr, value_ptr); |
789 | 2.38k | } |
790 | 0 | else { |
791 | | /* Empty value is always encoded to key= */ |
792 | 0 | result = curlx_dyn_addf(dq, "%s=", key_ptr); |
793 | 0 | } |
794 | 2.38k | } |
795 | 2.38k | if(result) |
796 | 0 | break; |
797 | 2.38k | } |
798 | | |
799 | 521 | fail: |
800 | 521 | if(counted_query_components) |
801 | | /* the encoded_query_array might not be initialized yet */ |
802 | 501 | pair_array_free(&encoded_query_array[0], counted_query_components); |
803 | 521 | dyn_array_free(&query_array[0], num_query_components); |
804 | 521 | return result; |
805 | 520 | } |
806 | | |
807 | | static CURLcode parse_sigv4_params(struct Curl_easy *data, |
808 | | const char *hostname, |
809 | | struct Curl_str *provider0, |
810 | | struct Curl_str *provider1, |
811 | | struct Curl_str *region, |
812 | | struct Curl_str *service) |
813 | 2.54k | { |
814 | 2.54k | const char *line = CURL_EASY_STR(data, STRING_AWS_SIGV4); |
815 | 2.54k | if(!line || !*line) |
816 | 457 | line = "aws:amz"; |
817 | | |
818 | | /* provider0[:provider1[:region[:service]]] |
819 | | |
820 | | No string can be longer than N bytes of non-whitespace */ |
821 | 2.54k | if(curlx_str_until(&line, provider0, MAX_SIGV4_LEN, ':')) { |
822 | 1 | failf(data, "first aws-sigv4 provider cannot be empty"); |
823 | 1 | return CURLE_BAD_FUNCTION_ARGUMENT; |
824 | 1 | } |
825 | 2.54k | if(curlx_str_single(&line, ':') || |
826 | 1.64k | curlx_str_until(&line, provider1, MAX_SIGV4_LEN, ':')) { |
827 | 1.64k | *provider1 = *provider0; |
828 | 1.64k | } |
829 | 900 | else if(curlx_str_single(&line, ':') || |
830 | 285 | curlx_str_until(&line, region, MAX_SIGV4_LEN, ':') || |
831 | 216 | curlx_str_single(&line, ':') || |
832 | 737 | curlx_str_until(&line, service, MAX_SIGV4_LEN, ':')) { |
833 | | /* nothing to do */ |
834 | 737 | } |
835 | | |
836 | 2.54k | if(!curlx_strlen(service)) { |
837 | 2.37k | const char *p = hostname; |
838 | 2.37k | if(curlx_str_until(&p, service, MAX_SIGV4_LEN, '.') || |
839 | 2.37k | curlx_str_single(&p, '.')) { |
840 | 9 | failf(data, "aws-sigv4: service missing in parameters and hostname"); |
841 | 9 | return CURLE_URL_MALFORMAT; |
842 | 9 | } |
843 | | |
844 | 2.36k | infof(data, "aws_sigv4: picked service %.*s from host", |
845 | 2.36k | (int)curlx_strlen(service), curlx_str(service)); |
846 | | |
847 | 2.36k | if(!curlx_strlen(region)) { |
848 | 2.31k | if(curlx_str_until(&p, region, MAX_SIGV4_LEN, '.') || |
849 | 2.31k | curlx_str_single(&p, '.')) { |
850 | 2 | failf(data, "aws-sigv4: region missing in parameters and hostname"); |
851 | 2 | return CURLE_URL_MALFORMAT; |
852 | 2 | } |
853 | 2.31k | infof(data, "aws_sigv4: picked region %.*s from host", |
854 | 2.31k | (int)curlx_strlen(region), curlx_str(region)); |
855 | 2.31k | } |
856 | 2.36k | } |
857 | | |
858 | 2.52k | return CURLE_OK; |
859 | 2.54k | } |
860 | | |
861 | | static CURLcode get_payload_hash(struct Curl_easy *data, |
862 | | Curl_HttpReq httpreq, |
863 | | struct Curl_str *provider0, |
864 | | struct Curl_str *provider1, |
865 | | struct Curl_str *service, |
866 | | unsigned char *sha_hash, |
867 | | char *sha_hex, |
868 | | char *content_sha256_hdr, |
869 | | const char **payload_hash_out, |
870 | | size_t *payload_hash_len_out) |
871 | 2.52k | { |
872 | 2.52k | *payload_hash_out = |
873 | 2.52k | parse_content_sha_hdr(data, curlx_str(provider1), |
874 | 2.52k | curlx_strlen(provider1), payload_hash_len_out); |
875 | | |
876 | 2.52k | if(!*payload_hash_out) { |
877 | 2.52k | CURLcode result; |
878 | | /* AWS S3 requires a x-amz-content-sha256 header, and supports special |
879 | | * values like UNSIGNED-PAYLOAD */ |
880 | 2.52k | bool sign_as_s3 = curlx_str_casecompare(provider0, "aws") && |
881 | 454 | curlx_str_casecompare(service, "s3"); |
882 | | |
883 | 2.52k | if(sign_as_s3) |
884 | 100 | result = calc_s3_payload_hash(data, httpreq, curlx_str(provider1), |
885 | 100 | curlx_strlen(provider1), sha_hash, |
886 | 100 | sha_hex, content_sha256_hdr); |
887 | 2.42k | else |
888 | 2.42k | result = calc_payload_hash(data, sha_hash, sha_hex); |
889 | 2.52k | if(result) |
890 | 0 | return result; |
891 | | |
892 | 2.52k | *payload_hash_out = sha_hex; |
893 | | /* may be shorter than SHA256_HEX_LENGTH, like S3_UNSIGNED_PAYLOAD */ |
894 | 2.52k | *payload_hash_len_out = strlen(sha_hex); |
895 | 2.52k | } |
896 | 2.52k | return CURLE_OK; |
897 | 2.52k | } |
898 | | |
899 | | static CURLcode get_timestamp(char *timestamp, size_t stampsize) |
900 | 2.52k | { |
901 | 2.52k | time_t clock; |
902 | 2.52k | struct tm tm; |
903 | 2.52k | CURLcode result; |
904 | | |
905 | 2.52k | #ifdef DEBUGBUILD |
906 | 2.52k | { |
907 | 2.52k | char *force_timestamp = getenv("CURL_FORCETIME"); |
908 | 2.52k | if(force_timestamp) |
909 | 0 | clock = 0; |
910 | 2.52k | else |
911 | 2.52k | clock = time(NULL); |
912 | 2.52k | } |
913 | | #else |
914 | | clock = time(NULL); |
915 | | #endif |
916 | 2.52k | result = curlx_gmtime(clock, &tm); |
917 | 2.52k | if(result) |
918 | 0 | return result; |
919 | | |
920 | 2.52k | if(!strftime(timestamp, stampsize, "%Y%m%dT%H%M%SZ", &tm)) |
921 | 0 | return CURLE_OUT_OF_MEMORY; |
922 | | |
923 | 2.52k | return CURLE_OK; |
924 | 2.52k | } |
925 | | |
926 | | static CURLcode make_canonical_request(struct Curl_easy *data, |
927 | | char *timestamp, |
928 | | struct Curl_str *provider1, |
929 | | struct Curl_str *service, |
930 | | const char *method, |
931 | | const char *payload_hash, |
932 | | size_t payload_hash_len, |
933 | | char **date_header_out, |
934 | | char *content_sha256_hdr, |
935 | | struct dynbuf *canonical_headers, |
936 | | struct dynbuf *signed_headers, |
937 | | char **canonical_request_out) |
938 | 2.52k | { |
939 | 2.52k | struct dynbuf canonical_query; |
940 | 2.52k | struct dynbuf canonical_path; |
941 | 2.52k | CURLcode result; |
942 | | |
943 | 2.52k | curlx_dyn_init(&canonical_query, CURL_MAX_HTTP_HEADER); |
944 | 2.52k | curlx_dyn_init(&canonical_path, CURL_MAX_HTTP_HEADER); |
945 | | |
946 | 2.52k | result = make_headers(data, timestamp, |
947 | 2.52k | curlx_str(provider1), curlx_strlen(provider1), |
948 | 2.52k | date_header_out, content_sha256_hdr, |
949 | 2.52k | canonical_headers, signed_headers); |
950 | 2.52k | if(result) |
951 | 3 | goto fail; |
952 | | |
953 | 2.52k | result = canon_query(data->state.up.query, &canonical_query); |
954 | 2.52k | if(result) |
955 | 1 | goto fail; |
956 | | |
957 | 2.52k | result = canon_path(data->state.up.path, strlen(data->state.up.path), |
958 | 2.52k | &canonical_path, |
959 | 2.52k | should_urlencode(service)); |
960 | 2.52k | if(result) |
961 | 0 | goto fail; |
962 | | |
963 | 2.52k | *canonical_request_out = |
964 | 2.52k | curl_maprintf("%s\n" /* HTTPRequestMethod */ |
965 | 2.52k | "%s\n" /* CanonicalURI */ |
966 | 2.52k | "%s\n" /* CanonicalQueryString */ |
967 | 2.52k | "%s\n" /* CanonicalHeaders */ |
968 | 2.52k | "%s\n" /* SignedHeaders */ |
969 | 2.52k | "%.*s", /* HashedRequestPayload in hex */ |
970 | 2.52k | method, |
971 | 2.52k | curlx_dyn_ptr(&canonical_path), |
972 | 2.52k | curlx_dyn_ptr(&canonical_query) ? |
973 | 2.02k | curlx_dyn_ptr(&canonical_query) : "", |
974 | 2.52k | curlx_dyn_ptr(canonical_headers), |
975 | 2.52k | curlx_dyn_ptr(signed_headers), |
976 | 2.52k | (int)payload_hash_len, payload_hash); |
977 | 2.52k | if(!*canonical_request_out) { |
978 | 0 | result = CURLE_OUT_OF_MEMORY; |
979 | 0 | goto fail; |
980 | 0 | } |
981 | | |
982 | 2.52k | result = CURLE_OK; |
983 | 2.52k | fail: |
984 | 2.52k | curlx_dyn_free(&canonical_query); |
985 | 2.52k | curlx_dyn_free(&canonical_path); |
986 | 2.52k | return result; |
987 | 2.52k | } |
988 | | |
989 | | static CURLcode make_string_to_sign(struct Curl_easy *data, |
990 | | struct Curl_str *provider0, |
991 | | struct Curl_str *region, |
992 | | struct Curl_str *service, |
993 | | const char *date, |
994 | | const char *timestamp, |
995 | | const char *canonical_request, |
996 | | char **request_type_out, |
997 | | char **credential_scope_out, |
998 | | char **str_to_sign_out) |
999 | 2.52k | { |
1000 | 2.52k | char *request_type; |
1001 | 2.52k | char *credential_scope; |
1002 | 2.52k | char *str_to_sign; |
1003 | 2.52k | unsigned char sha_hash[CURL_SHA256_DIGEST_LENGTH]; |
1004 | 2.52k | char sha_hex[SHA256_HEX_LENGTH]; |
1005 | | |
1006 | 2.52k | request_type = curl_maprintf("%.*s4_request", |
1007 | 2.52k | (int)curlx_strlen(provider0), |
1008 | 2.52k | curlx_str(provider0)); |
1009 | 2.52k | if(!request_type) |
1010 | 0 | return CURLE_OUT_OF_MEMORY; |
1011 | | |
1012 | | /* provider0 is lowercased *after* curl_maprintf() so that the buffer |
1013 | | can be written to */ |
1014 | 2.52k | Curl_strntolower(request_type, request_type, curlx_strlen(provider0)); |
1015 | | |
1016 | 2.52k | credential_scope = curl_maprintf("%s/%.*s/%.*s/%s", date, |
1017 | 2.52k | (int)curlx_strlen(region), |
1018 | 2.52k | curlx_str(region), |
1019 | 2.52k | (int)curlx_strlen(service), |
1020 | 2.52k | curlx_str(service), |
1021 | 2.52k | request_type); |
1022 | 2.52k | if(!credential_scope) { |
1023 | 0 | curlx_free(request_type); |
1024 | 0 | return CURLE_OUT_OF_MEMORY; |
1025 | 0 | } |
1026 | | |
1027 | 2.52k | if(Curl_sha256it(sha_hash, (const unsigned char *)canonical_request, |
1028 | 2.52k | strlen(canonical_request))) { |
1029 | 0 | curlx_free(request_type); |
1030 | 0 | curlx_free(credential_scope); |
1031 | 0 | return CURLE_OUT_OF_MEMORY; |
1032 | 0 | } |
1033 | | |
1034 | 2.52k | sha256_to_hex(sha_hex, sha_hash); |
1035 | | |
1036 | | /* |
1037 | | * Google allows using RSA key instead of HMAC, so this code might change |
1038 | | * in the future. For now we only support HMAC. |
1039 | | */ |
1040 | 2.52k | str_to_sign = curl_maprintf("%.*s4-HMAC-SHA256\n" /* Algorithm */ |
1041 | 2.52k | "%s\n" /* RequestDateTime */ |
1042 | 2.52k | "%s\n" /* CredentialScope */ |
1043 | 2.52k | "%s", /* HashedCanonicalRequest in hex */ |
1044 | 2.52k | (int)curlx_strlen(provider0), |
1045 | 2.52k | curlx_str(provider0), |
1046 | 2.52k | timestamp, |
1047 | 2.52k | credential_scope, |
1048 | 2.52k | sha_hex); |
1049 | 2.52k | if(!str_to_sign) { |
1050 | 0 | curlx_free(request_type); |
1051 | 0 | curlx_free(credential_scope); |
1052 | 0 | return CURLE_OUT_OF_MEMORY; |
1053 | 0 | } |
1054 | | |
1055 | | /* make provider0 part done uppercase */ |
1056 | 2.52k | Curl_strntoupper(str_to_sign, curlx_str(provider0), |
1057 | 2.52k | curlx_strlen(provider0)); |
1058 | | |
1059 | 2.52k | infof(data, "aws_sigv4: String to sign (enclosed in []) - [%s]", |
1060 | 2.52k | str_to_sign); |
1061 | | |
1062 | 2.52k | *request_type_out = request_type; |
1063 | 2.52k | *credential_scope_out = credential_scope; |
1064 | 2.52k | *str_to_sign_out = str_to_sign; |
1065 | 2.52k | return CURLE_OK; |
1066 | 2.52k | } |
1067 | | |
1068 | | static CURLcode sign_and_set_auth_headers(struct Curl_easy *data, |
1069 | | struct Curl_str *provider0, |
1070 | | struct Curl_str *region, |
1071 | | struct Curl_str *service, |
1072 | | const char *request_type, |
1073 | | const char *credential_scope, |
1074 | | const char *date, |
1075 | | const char *str_to_sign, |
1076 | | const char *date_header, |
1077 | | const char *content_sha256_hdr, |
1078 | | struct dynbuf *signed_headers) |
1079 | 2.52k | { |
1080 | 2.52k | CURLcode result = CURLE_OUT_OF_MEMORY; |
1081 | 2.52k | const char *passwd = Curl_creds_passwd(data->state.creds); |
1082 | 2.52k | char *secret = NULL; |
1083 | 2.52k | unsigned char sign0[CURL_SHA256_DIGEST_LENGTH] = { 0 }; |
1084 | 2.52k | unsigned char sign1[CURL_SHA256_DIGEST_LENGTH] = { 0 }; |
1085 | 2.52k | char sha_hex[SHA256_HEX_LENGTH]; |
1086 | 2.52k | char *auth_headers = NULL; |
1087 | 2.52k | char *user = curl_escape(Curl_creds_user(data->state.creds), 0); |
1088 | 2.52k | if(!user) |
1089 | 0 | return CURLE_OUT_OF_MEMORY; |
1090 | | |
1091 | 2.52k | secret = curl_maprintf("%.*s4%s", (int)curlx_strlen(provider0), |
1092 | 2.52k | curlx_str(provider0), passwd); |
1093 | 2.52k | if(!secret) |
1094 | 0 | goto fail; |
1095 | | /* make provider0 part done uppercase */ |
1096 | 2.52k | Curl_strntoupper(secret, curlx_str(provider0), curlx_strlen(provider0)); |
1097 | | |
1098 | 2.52k | HMAC_SHA256(secret, strlen(secret), date, strlen(date), sign0); |
1099 | 2.52k | HMAC_SHA256(sign0, sizeof(sign0), |
1100 | 2.52k | curlx_str(region), curlx_strlen(region), sign1); |
1101 | 2.52k | HMAC_SHA256(sign1, sizeof(sign1), |
1102 | 2.52k | curlx_str(service), curlx_strlen(service), sign0); |
1103 | 2.52k | HMAC_SHA256(sign0, sizeof(sign0), |
1104 | 2.52k | request_type, strlen(request_type), sign1); |
1105 | 2.52k | HMAC_SHA256(sign1, sizeof(sign1), |
1106 | 2.52k | str_to_sign, strlen(str_to_sign), sign0); |
1107 | | |
1108 | 2.52k | sha256_to_hex(sha_hex, sign0); |
1109 | | |
1110 | 2.52k | infof(data, "aws_sigv4: Signature - %s", sha_hex); |
1111 | | |
1112 | 2.52k | auth_headers = curl_maprintf("Authorization: %.*s4-HMAC-SHA256 " |
1113 | 2.52k | "Credential=%s/%s, " |
1114 | 2.52k | "SignedHeaders=%s, " |
1115 | 2.52k | "Signature=%s\r\n" |
1116 | 2.52k | "%s" |
1117 | 2.52k | "%s%s", |
1118 | 2.52k | (int)curlx_strlen(provider0), |
1119 | 2.52k | curlx_str(provider0), |
1120 | 2.52k | user, |
1121 | 2.52k | credential_scope, |
1122 | 2.52k | curlx_dyn_ptr(signed_headers), |
1123 | 2.52k | sha_hex, |
1124 | | /* |
1125 | | * date_header is added here, only if it was not |
1126 | | * user-specified (using CURLOPT_HTTPHEADER). |
1127 | | * date_header includes \r\n |
1128 | | */ |
1129 | 2.52k | date_header ? date_header : "", |
1130 | 2.52k | content_sha256_hdr, |
1131 | 2.52k | content_sha256_hdr[0] ? "\r\n": ""); |
1132 | 2.52k | if(!auth_headers) |
1133 | 0 | goto fail; |
1134 | | |
1135 | | /* provider 0 uppercase */ |
1136 | 2.52k | Curl_strntoupper(&auth_headers[CURL_CSTRLEN("Authorization: ")], |
1137 | 2.52k | curlx_str(provider0), curlx_strlen(provider0)); |
1138 | | |
1139 | 2.52k | curlx_free(data->req.hd_auth); |
1140 | 2.52k | data->req.hd_auth = auth_headers; |
1141 | 2.52k | data->state.authhost.done = TRUE; |
1142 | 2.52k | result = CURLE_OK; |
1143 | | |
1144 | 2.52k | fail: |
1145 | 2.52k | curlx_free(user); |
1146 | 2.52k | curlx_free(secret); |
1147 | 2.52k | return result; |
1148 | 2.52k | } |
1149 | | |
1150 | | CURLcode Curl_output_aws_sigv4(struct Curl_easy *data) |
1151 | 2.58k | { |
1152 | 2.58k | CURLcode result = CURLE_OUT_OF_MEMORY; |
1153 | 2.58k | struct Curl_str provider0 = { NULL, 0 }; |
1154 | 2.58k | struct Curl_str provider1 = { NULL, 0 }; |
1155 | 2.58k | struct Curl_str region = { NULL, 0 }; |
1156 | 2.58k | struct Curl_str service = { NULL, 0 }; |
1157 | 2.58k | const char *hostname = data->state.origin->hostname; |
1158 | 2.58k | char timestamp[TIMESTAMP_SIZE]; |
1159 | 2.58k | char date[9]; |
1160 | 2.58k | struct dynbuf canonical_headers; |
1161 | 2.58k | struct dynbuf signed_headers; |
1162 | 2.58k | char *date_header = NULL; |
1163 | 2.58k | Curl_HttpReq httpreq; |
1164 | 2.58k | const char *method = NULL; |
1165 | 2.58k | const char *payload_hash = NULL; |
1166 | 2.58k | size_t payload_hash_len = 0; |
1167 | 2.58k | unsigned char sha_hash[CURL_SHA256_DIGEST_LENGTH]; |
1168 | 2.58k | char sha_hex[SHA256_HEX_LENGTH]; |
1169 | 2.58k | char content_sha256_hdr[CONTENT_SHA256_HDR_LEN + 2] = ""; /* add \r\n */ |
1170 | 2.58k | char *canonical_request = NULL; |
1171 | 2.58k | char *request_type = NULL; |
1172 | 2.58k | char *credential_scope = NULL; |
1173 | 2.58k | char *str_to_sign = NULL; |
1174 | | |
1175 | 2.58k | if(data->set.path_as_is) { |
1176 | 2 | failf(data, "Cannot use sigv4 authentication with path-as-is flag"); |
1177 | 2 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1178 | 2 | } |
1179 | | |
1180 | 2.57k | if(Curl_checkheaders(data, STRCONST("Authorization"))) |
1181 | | /* Authorization already present, Bailing out */ |
1182 | 38 | return CURLE_OK; |
1183 | | |
1184 | | /* we init those buffers here, so goto fail will free initialized dynbuf */ |
1185 | 2.54k | curlx_dyn_init(&canonical_headers, CURL_MAX_HTTP_HEADER); |
1186 | 2.54k | curlx_dyn_init(&signed_headers, CURL_MAX_HTTP_HEADER); |
1187 | | |
1188 | 2.54k | result = parse_sigv4_params(data, hostname, &provider0, &provider1, |
1189 | 2.54k | ®ion, &service); |
1190 | 2.54k | if(!result) { |
1191 | 2.52k | Curl_http_method(data, &method, &httpreq); |
1192 | 2.52k | result = get_payload_hash(data, httpreq, &provider0, &provider1, &service, |
1193 | 2.52k | sha_hash, sha_hex, content_sha256_hdr, |
1194 | 2.52k | &payload_hash, &payload_hash_len); |
1195 | 2.52k | } |
1196 | | |
1197 | 2.54k | if(!result) |
1198 | 2.52k | result = get_timestamp(timestamp, sizeof(timestamp)); |
1199 | | |
1200 | 2.54k | if(!result) |
1201 | 2.52k | result = make_canonical_request(data, timestamp, |
1202 | 2.52k | &provider1, &service, |
1203 | 2.52k | method, payload_hash, payload_hash_len, |
1204 | 2.52k | &date_header, content_sha256_hdr, |
1205 | 2.52k | &canonical_headers, &signed_headers, |
1206 | 2.52k | &canonical_request); |
1207 | 2.54k | if(!result) { |
1208 | | /* the timestamp might have been updated in make_canonical_request */ |
1209 | 2.52k | memcpy(date, timestamp, sizeof(date) - 1); |
1210 | 2.52k | date[sizeof(date) - 1] = 0; |
1211 | | |
1212 | 2.52k | result = make_string_to_sign(data, &provider0, ®ion, &service, |
1213 | 2.52k | date, timestamp, canonical_request, |
1214 | 2.52k | &request_type, &credential_scope, |
1215 | 2.52k | &str_to_sign); |
1216 | 2.52k | } |
1217 | 2.54k | if(!result) |
1218 | 2.52k | result = sign_and_set_auth_headers(data, &provider0, ®ion, &service, |
1219 | 2.52k | request_type, credential_scope, |
1220 | 2.52k | date, str_to_sign, date_header, |
1221 | 2.52k | content_sha256_hdr, &signed_headers); |
1222 | | |
1223 | 2.54k | curlx_dyn_free(&canonical_headers); |
1224 | 2.54k | curlx_dyn_free(&signed_headers); |
1225 | 2.54k | curlx_free(canonical_request); |
1226 | 2.54k | curlx_free(request_type); |
1227 | 2.54k | curlx_free(credential_scope); |
1228 | 2.54k | curlx_free(str_to_sign); |
1229 | 2.54k | curlx_free(date_header); |
1230 | 2.54k | return result; |
1231 | 2.57k | } |
1232 | | |
1233 | | #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_AWS */ |