/src/PROJ/curl/lib/http_aws_sigv4.c
Line | Count | Source (jump to first uncovered line) |
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.haxx.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 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_AWS) |
28 | | |
29 | | #include "urldata.h" |
30 | | #include "strcase.h" |
31 | | #include "strdup.h" |
32 | | #include "http_aws_sigv4.h" |
33 | | #include "curl_sha256.h" |
34 | | #include "transfer.h" |
35 | | #include "parsedate.h" |
36 | | #include "sendf.h" |
37 | | #include "escape.h" |
38 | | |
39 | | #include <time.h> |
40 | | |
41 | | /* The last 3 #include files should be in this order */ |
42 | | #include "curl_printf.h" |
43 | | #include "curl_memory.h" |
44 | | #include "memdebug.h" |
45 | | |
46 | | #include "slist.h" |
47 | | |
48 | | #define HMAC_SHA256(k, kl, d, dl, o) \ |
49 | 0 | do { \ |
50 | 0 | result = Curl_hmacit(Curl_HMAC_SHA256, \ |
51 | 0 | (unsigned char *)k, \ |
52 | 0 | kl, \ |
53 | 0 | (unsigned char *)d, \ |
54 | 0 | dl, o); \ |
55 | 0 | if(result) { \ |
56 | 0 | goto fail; \ |
57 | 0 | } \ |
58 | 0 | } while(0) |
59 | | |
60 | 0 | #define TIMESTAMP_SIZE 17 |
61 | | |
62 | | /* hex-encoded with trailing null */ |
63 | 0 | #define SHA256_HEX_LENGTH (2 * SHA256_DIGEST_LENGTH + 1) |
64 | | |
65 | | static void sha256_to_hex(char *dst, unsigned char *sha) |
66 | 0 | { |
67 | 0 | Curl_hexencode(sha, SHA256_DIGEST_LENGTH, |
68 | 0 | (unsigned char *)dst, SHA256_HEX_LENGTH); |
69 | 0 | } |
70 | | |
71 | | static char *find_date_hdr(struct Curl_easy *data, const char *sig_hdr) |
72 | 0 | { |
73 | 0 | char *tmp = Curl_checkheaders(data, sig_hdr, strlen(sig_hdr)); |
74 | |
|
75 | 0 | if(tmp) |
76 | 0 | return tmp; |
77 | 0 | return Curl_checkheaders(data, STRCONST("Date")); |
78 | 0 | } |
79 | | |
80 | | /* remove whitespace, and lowercase all headers */ |
81 | | static void trim_headers(struct curl_slist *head) |
82 | 0 | { |
83 | 0 | struct curl_slist *l; |
84 | 0 | for(l = head; l; l = l->next) { |
85 | 0 | char *value; /* to read from */ |
86 | 0 | char *store; |
87 | 0 | size_t colon = strcspn(l->data, ":"); |
88 | 0 | Curl_strntolower(l->data, l->data, colon); |
89 | |
|
90 | 0 | value = &l->data[colon]; |
91 | 0 | if(!*value) |
92 | 0 | continue; |
93 | 0 | ++value; |
94 | 0 | store = value; |
95 | | |
96 | | /* skip leading whitespace */ |
97 | 0 | while(*value && ISBLANK(*value)) |
98 | 0 | value++; |
99 | |
|
100 | 0 | while(*value) { |
101 | 0 | int space = 0; |
102 | 0 | while(*value && ISBLANK(*value)) { |
103 | 0 | value++; |
104 | 0 | space++; |
105 | 0 | } |
106 | 0 | if(space) { |
107 | | /* replace any number of consecutive whitespace with a single space, |
108 | | unless at the end of the string, then nothing */ |
109 | 0 | if(*value) |
110 | 0 | *store++ = ' '; |
111 | 0 | } |
112 | 0 | else |
113 | 0 | *store++ = *value++; |
114 | 0 | } |
115 | 0 | *store = 0; /* null terminate */ |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | /* maximum length for the aws sivg4 parts */ |
120 | 0 | #define MAX_SIGV4_LEN 64 |
121 | | #define MAX_SIGV4_LEN_TXT "64" |
122 | | |
123 | 0 | #define DATE_HDR_KEY_LEN (MAX_SIGV4_LEN + sizeof("X--Date")) |
124 | | |
125 | 0 | #define MAX_HOST_LEN 255 |
126 | | /* FQDN + host: */ |
127 | 0 | #define FULL_HOST_LEN (MAX_HOST_LEN + sizeof("host:")) |
128 | | |
129 | | /* string been x-PROVIDER-date:TIMESTAMP, I need +1 for ':' */ |
130 | 0 | #define DATE_FULL_HDR_LEN (DATE_HDR_KEY_LEN + TIMESTAMP_SIZE + 1) |
131 | | |
132 | | /* timestamp should point to a buffer of at last TIMESTAMP_SIZE bytes */ |
133 | | static CURLcode make_headers(struct Curl_easy *data, |
134 | | const char *hostname, |
135 | | char *timestamp, |
136 | | char *provider1, |
137 | | char **date_header, |
138 | | char *content_sha256_header, |
139 | | struct dynbuf *canonical_headers, |
140 | | struct dynbuf *signed_headers) |
141 | 0 | { |
142 | 0 | char date_hdr_key[DATE_HDR_KEY_LEN]; |
143 | 0 | char date_full_hdr[DATE_FULL_HDR_LEN]; |
144 | 0 | struct curl_slist *head = NULL; |
145 | 0 | struct curl_slist *tmp_head = NULL; |
146 | 0 | CURLcode ret = CURLE_OUT_OF_MEMORY; |
147 | 0 | struct curl_slist *l; |
148 | 0 | int again = 1; |
149 | | |
150 | | /* provider1 mid */ |
151 | 0 | Curl_strntolower(provider1, provider1, strlen(provider1)); |
152 | 0 | provider1[0] = Curl_raw_toupper(provider1[0]); |
153 | |
|
154 | 0 | msnprintf(date_hdr_key, DATE_HDR_KEY_LEN, "X-%s-Date", provider1); |
155 | | |
156 | | /* provider1 lowercase */ |
157 | 0 | Curl_strntolower(provider1, provider1, 1); /* first byte only */ |
158 | 0 | msnprintf(date_full_hdr, DATE_FULL_HDR_LEN, |
159 | 0 | "x-%s-date:%s", provider1, timestamp); |
160 | |
|
161 | 0 | if(Curl_checkheaders(data, STRCONST("Host"))) { |
162 | 0 | head = NULL; |
163 | 0 | } |
164 | 0 | else { |
165 | 0 | char full_host[FULL_HOST_LEN + 1]; |
166 | |
|
167 | 0 | if(data->state.aptr.host) { |
168 | 0 | size_t pos; |
169 | |
|
170 | 0 | if(strlen(data->state.aptr.host) > FULL_HOST_LEN) { |
171 | 0 | ret = CURLE_URL_MALFORMAT; |
172 | 0 | goto fail; |
173 | 0 | } |
174 | 0 | strcpy(full_host, data->state.aptr.host); |
175 | | /* remove /r/n as the separator for canonical request must be '\n' */ |
176 | 0 | pos = strcspn(full_host, "\n\r"); |
177 | 0 | full_host[pos] = 0; |
178 | 0 | } |
179 | 0 | else { |
180 | 0 | if(strlen(hostname) > MAX_HOST_LEN) { |
181 | 0 | ret = CURLE_URL_MALFORMAT; |
182 | 0 | goto fail; |
183 | 0 | } |
184 | 0 | msnprintf(full_host, FULL_HOST_LEN, "host:%s", hostname); |
185 | 0 | } |
186 | | |
187 | 0 | head = curl_slist_append(NULL, full_host); |
188 | 0 | if(!head) |
189 | 0 | goto fail; |
190 | 0 | } |
191 | | |
192 | | |
193 | 0 | if(*content_sha256_header) { |
194 | 0 | tmp_head = curl_slist_append(head, content_sha256_header); |
195 | 0 | if(!tmp_head) |
196 | 0 | goto fail; |
197 | 0 | head = tmp_head; |
198 | 0 | } |
199 | | |
200 | | /* copy user headers to our header list. the logic is based on how http.c |
201 | | handles user headers. |
202 | | |
203 | | user headers in format 'name:' with no value are used to signal that an |
204 | | internal header of that name should be removed. those user headers are not |
205 | | added to this list. |
206 | | |
207 | | user headers in format 'name;' with no value are used to signal that a |
208 | | header of that name with no value should be sent. those user headers are |
209 | | added to this list but in the format that they will be sent, ie the |
210 | | semi-colon is changed to a colon for format 'name:'. |
211 | | |
212 | | user headers with a value of whitespace only, or without a colon or |
213 | | semi-colon, are not added to this list. |
214 | | */ |
215 | 0 | for(l = data->set.headers; l; l = l->next) { |
216 | 0 | char *dupdata, *ptr; |
217 | 0 | char *sep = strchr(l->data, ':'); |
218 | 0 | if(!sep) |
219 | 0 | sep = strchr(l->data, ';'); |
220 | 0 | if(!sep || (*sep == ':' && !*(sep + 1))) |
221 | 0 | continue; |
222 | 0 | for(ptr = sep + 1; ISSPACE(*ptr); ++ptr) |
223 | 0 | ; |
224 | 0 | if(!*ptr && ptr != sep + 1) /* a value of whitespace only */ |
225 | 0 | continue; |
226 | 0 | dupdata = strdup(l->data); |
227 | 0 | if(!dupdata) |
228 | 0 | goto fail; |
229 | 0 | dupdata[sep - l->data] = ':'; |
230 | 0 | tmp_head = Curl_slist_append_nodup(head, dupdata); |
231 | 0 | if(!tmp_head) { |
232 | 0 | free(dupdata); |
233 | 0 | goto fail; |
234 | 0 | } |
235 | 0 | head = tmp_head; |
236 | 0 | } |
237 | | |
238 | 0 | trim_headers(head); |
239 | |
|
240 | 0 | *date_header = find_date_hdr(data, date_hdr_key); |
241 | 0 | if(!*date_header) { |
242 | 0 | tmp_head = curl_slist_append(head, date_full_hdr); |
243 | 0 | if(!tmp_head) |
244 | 0 | goto fail; |
245 | 0 | head = tmp_head; |
246 | 0 | *date_header = curl_maprintf("%s: %s\r\n", date_hdr_key, timestamp); |
247 | 0 | } |
248 | 0 | else { |
249 | 0 | char *value; |
250 | 0 | char *endp; |
251 | 0 | value = strchr(*date_header, ':'); |
252 | 0 | if(!value) { |
253 | 0 | *date_header = NULL; |
254 | 0 | goto fail; |
255 | 0 | } |
256 | 0 | ++value; |
257 | 0 | while(ISBLANK(*value)) |
258 | 0 | ++value; |
259 | 0 | endp = value; |
260 | 0 | while(*endp && ISALNUM(*endp)) |
261 | 0 | ++endp; |
262 | | /* 16 bytes => "19700101T000000Z" */ |
263 | 0 | if((endp - value) == TIMESTAMP_SIZE - 1) { |
264 | 0 | memcpy(timestamp, value, TIMESTAMP_SIZE - 1); |
265 | 0 | timestamp[TIMESTAMP_SIZE - 1] = 0; |
266 | 0 | } |
267 | 0 | else |
268 | | /* bad timestamp length */ |
269 | 0 | timestamp[0] = 0; |
270 | 0 | *date_header = NULL; |
271 | 0 | } |
272 | | |
273 | | /* alpha-sort in a case sensitive manner */ |
274 | 0 | do { |
275 | 0 | again = 0; |
276 | 0 | for(l = head; l; l = l->next) { |
277 | 0 | struct curl_slist *next = l->next; |
278 | |
|
279 | 0 | if(next && strcmp(l->data, next->data) > 0) { |
280 | 0 | char *tmp = l->data; |
281 | |
|
282 | 0 | l->data = next->data; |
283 | 0 | next->data = tmp; |
284 | 0 | again = 1; |
285 | 0 | } |
286 | 0 | } |
287 | 0 | } while(again); |
288 | |
|
289 | 0 | for(l = head; l; l = l->next) { |
290 | 0 | char *tmp; |
291 | |
|
292 | 0 | if(Curl_dyn_add(canonical_headers, l->data)) |
293 | 0 | goto fail; |
294 | 0 | if(Curl_dyn_add(canonical_headers, "\n")) |
295 | 0 | goto fail; |
296 | | |
297 | 0 | tmp = strchr(l->data, ':'); |
298 | 0 | if(tmp) |
299 | 0 | *tmp = 0; |
300 | |
|
301 | 0 | if(l != head) { |
302 | 0 | if(Curl_dyn_add(signed_headers, ";")) |
303 | 0 | goto fail; |
304 | 0 | } |
305 | 0 | if(Curl_dyn_add(signed_headers, l->data)) |
306 | 0 | goto fail; |
307 | 0 | } |
308 | | |
309 | 0 | ret = CURLE_OK; |
310 | 0 | fail: |
311 | 0 | curl_slist_free_all(head); |
312 | |
|
313 | 0 | return ret; |
314 | 0 | } |
315 | | |
316 | 0 | #define CONTENT_SHA256_KEY_LEN (MAX_SIGV4_LEN + sizeof("X--Content-Sha256")) |
317 | | /* add 2 for ": " between header name and value */ |
318 | 0 | #define CONTENT_SHA256_HDR_LEN (CONTENT_SHA256_KEY_LEN + 2 + \ |
319 | 0 | SHA256_HEX_LENGTH) |
320 | | |
321 | | /* try to parse a payload hash from the content-sha256 header */ |
322 | | static char *parse_content_sha_hdr(struct Curl_easy *data, |
323 | | const char *provider1, |
324 | | size_t *value_len) |
325 | 0 | { |
326 | 0 | char key[CONTENT_SHA256_KEY_LEN]; |
327 | 0 | size_t key_len; |
328 | 0 | char *value; |
329 | 0 | size_t len; |
330 | |
|
331 | 0 | key_len = msnprintf(key, sizeof(key), "x-%s-content-sha256", provider1); |
332 | |
|
333 | 0 | value = Curl_checkheaders(data, key, key_len); |
334 | 0 | if(!value) |
335 | 0 | return NULL; |
336 | | |
337 | 0 | value = strchr(value, ':'); |
338 | 0 | if(!value) |
339 | 0 | return NULL; |
340 | 0 | ++value; |
341 | |
|
342 | 0 | while(*value && ISBLANK(*value)) |
343 | 0 | ++value; |
344 | |
|
345 | 0 | len = strlen(value); |
346 | 0 | while(len > 0 && ISBLANK(value[len-1])) |
347 | 0 | --len; |
348 | |
|
349 | 0 | *value_len = len; |
350 | 0 | return value; |
351 | 0 | } |
352 | | |
353 | | static CURLcode calc_payload_hash(struct Curl_easy *data, |
354 | | unsigned char *sha_hash, char *sha_hex) |
355 | 0 | { |
356 | 0 | const char *post_data = data->set.postfields; |
357 | 0 | size_t post_data_len = 0; |
358 | 0 | CURLcode result; |
359 | |
|
360 | 0 | if(post_data) { |
361 | 0 | if(data->set.postfieldsize < 0) |
362 | 0 | post_data_len = strlen(post_data); |
363 | 0 | else |
364 | 0 | post_data_len = (size_t)data->set.postfieldsize; |
365 | 0 | } |
366 | 0 | result = Curl_sha256it(sha_hash, (const unsigned char *) post_data, |
367 | 0 | post_data_len); |
368 | 0 | if(!result) |
369 | 0 | sha256_to_hex(sha_hex, sha_hash); |
370 | 0 | return result; |
371 | 0 | } |
372 | | |
373 | 0 | #define S3_UNSIGNED_PAYLOAD "UNSIGNED-PAYLOAD" |
374 | | |
375 | | static CURLcode calc_s3_payload_hash(struct Curl_easy *data, |
376 | | Curl_HttpReq httpreq, char *provider1, |
377 | | unsigned char *sha_hash, |
378 | | char *sha_hex, char *header) |
379 | 0 | { |
380 | 0 | bool empty_method = (httpreq == HTTPREQ_GET || httpreq == HTTPREQ_HEAD); |
381 | | /* The request method or filesize indicate no request payload */ |
382 | 0 | bool empty_payload = (empty_method || data->set.filesize == 0); |
383 | | /* The POST payload is in memory */ |
384 | 0 | bool post_payload = (httpreq == HTTPREQ_POST && data->set.postfields); |
385 | 0 | CURLcode ret = CURLE_OUT_OF_MEMORY; |
386 | |
|
387 | 0 | if(empty_payload || post_payload) { |
388 | | /* Calculate a real hash when we know the request payload */ |
389 | 0 | ret = calc_payload_hash(data, sha_hash, sha_hex); |
390 | 0 | if(ret) |
391 | 0 | goto fail; |
392 | 0 | } |
393 | 0 | else { |
394 | | /* Fall back to s3's UNSIGNED-PAYLOAD */ |
395 | 0 | size_t len = sizeof(S3_UNSIGNED_PAYLOAD) - 1; |
396 | 0 | DEBUGASSERT(len < SHA256_HEX_LENGTH); /* 16 < 65 */ |
397 | 0 | memcpy(sha_hex, S3_UNSIGNED_PAYLOAD, len); |
398 | 0 | sha_hex[len] = 0; |
399 | 0 | } |
400 | | |
401 | | /* format the required content-sha256 header */ |
402 | 0 | msnprintf(header, CONTENT_SHA256_HDR_LEN, |
403 | 0 | "x-%s-content-sha256: %s", provider1, sha_hex); |
404 | |
|
405 | 0 | ret = CURLE_OK; |
406 | 0 | fail: |
407 | 0 | return ret; |
408 | 0 | } |
409 | | |
410 | | struct pair { |
411 | | const char *p; |
412 | | size_t len; |
413 | | }; |
414 | | |
415 | | static int compare_func(const void *a, const void *b) |
416 | 0 | { |
417 | 0 | const struct pair *aa = a; |
418 | 0 | const struct pair *bb = b; |
419 | | /* If one element is empty, the other is always sorted higher */ |
420 | 0 | if(aa->len == 0) |
421 | 0 | return -1; |
422 | 0 | if(bb->len == 0) |
423 | 0 | return 1; |
424 | 0 | return strncmp(aa->p, bb->p, aa->len < bb->len ? aa->len : bb->len); |
425 | 0 | } |
426 | | |
427 | 0 | #define MAX_QUERYPAIRS 64 |
428 | | |
429 | | static CURLcode canon_query(struct Curl_easy *data, |
430 | | const char *query, struct dynbuf *dq) |
431 | 0 | { |
432 | 0 | CURLcode result = CURLE_OK; |
433 | 0 | int entry = 0; |
434 | 0 | int i; |
435 | 0 | const char *p = query; |
436 | 0 | struct pair array[MAX_QUERYPAIRS]; |
437 | 0 | struct pair *ap = &array[0]; |
438 | 0 | if(!query) |
439 | 0 | return result; |
440 | | |
441 | | /* sort the name=value pairs first */ |
442 | 0 | do { |
443 | 0 | char *amp; |
444 | 0 | entry++; |
445 | 0 | ap->p = p; |
446 | 0 | amp = strchr(p, '&'); |
447 | 0 | if(amp) |
448 | 0 | ap->len = amp - p; /* excluding the ampersand */ |
449 | 0 | else { |
450 | 0 | ap->len = strlen(p); |
451 | 0 | break; |
452 | 0 | } |
453 | 0 | ap++; |
454 | 0 | p = amp + 1; |
455 | 0 | } while(entry < MAX_QUERYPAIRS); |
456 | 0 | if(entry == MAX_QUERYPAIRS) { |
457 | | /* too many query pairs for us */ |
458 | 0 | failf(data, "aws-sigv4: too many query pairs in URL"); |
459 | 0 | return CURLE_URL_MALFORMAT; |
460 | 0 | } |
461 | | |
462 | 0 | qsort(&array[0], entry, sizeof(struct pair), compare_func); |
463 | |
|
464 | 0 | ap = &array[0]; |
465 | 0 | for(i = 0; !result && (i < entry); i++, ap++) { |
466 | 0 | size_t len; |
467 | 0 | const char *q = ap->p; |
468 | 0 | bool found_equals = false; |
469 | 0 | if(!ap->len) |
470 | 0 | continue; |
471 | 0 | for(len = ap->len; len && !result; q++, len--) { |
472 | 0 | if(ISALNUM(*q)) |
473 | 0 | result = Curl_dyn_addn(dq, q, 1); |
474 | 0 | else { |
475 | 0 | switch(*q) { |
476 | 0 | case '-': |
477 | 0 | case '.': |
478 | 0 | case '_': |
479 | 0 | case '~': |
480 | | /* allowed as-is */ |
481 | 0 | result = Curl_dyn_addn(dq, q, 1); |
482 | 0 | break; |
483 | 0 | case '=': |
484 | | /* allowed as-is */ |
485 | 0 | result = Curl_dyn_addn(dq, q, 1); |
486 | 0 | found_equals = true; |
487 | 0 | break; |
488 | 0 | case '%': |
489 | | /* uppercase the following if hexadecimal */ |
490 | 0 | if(ISXDIGIT(q[1]) && ISXDIGIT(q[2])) { |
491 | 0 | char tmp[3]="%"; |
492 | 0 | tmp[1] = Curl_raw_toupper(q[1]); |
493 | 0 | tmp[2] = Curl_raw_toupper(q[2]); |
494 | 0 | result = Curl_dyn_addn(dq, tmp, 3); |
495 | 0 | q += 2; |
496 | 0 | len -= 2; |
497 | 0 | } |
498 | 0 | else |
499 | | /* '%' without a following two-digit hex, encode it */ |
500 | 0 | result = Curl_dyn_addn(dq, "%25", 3); |
501 | 0 | break; |
502 | 0 | default: { |
503 | | /* URL encode */ |
504 | 0 | const char hex[] = "0123456789ABCDEF"; |
505 | 0 | char out[3]={'%'}; |
506 | 0 | out[1] = hex[((unsigned char)*q)>>4]; |
507 | 0 | out[2] = hex[*q & 0xf]; |
508 | 0 | result = Curl_dyn_addn(dq, out, 3); |
509 | 0 | break; |
510 | 0 | } |
511 | 0 | } |
512 | 0 | } |
513 | 0 | } |
514 | 0 | if(!result && !found_equals) { |
515 | | /* queries without value still need an equals */ |
516 | 0 | result = Curl_dyn_addn(dq, "=", 1); |
517 | 0 | } |
518 | 0 | if(!result && i < entry - 1) { |
519 | | /* insert ampersands between query pairs */ |
520 | 0 | result = Curl_dyn_addn(dq, "&", 1); |
521 | 0 | } |
522 | 0 | } |
523 | 0 | return result; |
524 | 0 | } |
525 | | |
526 | | |
527 | | CURLcode Curl_output_aws_sigv4(struct Curl_easy *data, bool proxy) |
528 | 0 | { |
529 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
530 | 0 | struct connectdata *conn = data->conn; |
531 | 0 | size_t len; |
532 | 0 | const char *arg; |
533 | 0 | char provider0[MAX_SIGV4_LEN + 1]=""; |
534 | 0 | char provider1[MAX_SIGV4_LEN + 1]=""; |
535 | 0 | char region[MAX_SIGV4_LEN + 1]=""; |
536 | 0 | char service[MAX_SIGV4_LEN + 1]=""; |
537 | 0 | bool sign_as_s3 = false; |
538 | 0 | const char *hostname = conn->host.name; |
539 | 0 | time_t clock; |
540 | 0 | struct tm tm; |
541 | 0 | char timestamp[TIMESTAMP_SIZE]; |
542 | 0 | char date[9]; |
543 | 0 | struct dynbuf canonical_headers; |
544 | 0 | struct dynbuf signed_headers; |
545 | 0 | struct dynbuf canonical_query; |
546 | 0 | char *date_header = NULL; |
547 | 0 | Curl_HttpReq httpreq; |
548 | 0 | const char *method = NULL; |
549 | 0 | char *payload_hash = NULL; |
550 | 0 | size_t payload_hash_len = 0; |
551 | 0 | unsigned char sha_hash[SHA256_DIGEST_LENGTH]; |
552 | 0 | char sha_hex[SHA256_HEX_LENGTH]; |
553 | 0 | char content_sha256_hdr[CONTENT_SHA256_HDR_LEN + 2] = ""; /* add \r\n */ |
554 | 0 | char *canonical_request = NULL; |
555 | 0 | char *request_type = NULL; |
556 | 0 | char *credential_scope = NULL; |
557 | 0 | char *str_to_sign = NULL; |
558 | 0 | const char *user = data->state.aptr.user ? data->state.aptr.user : ""; |
559 | 0 | char *secret = NULL; |
560 | 0 | unsigned char sign0[SHA256_DIGEST_LENGTH] = {0}; |
561 | 0 | unsigned char sign1[SHA256_DIGEST_LENGTH] = {0}; |
562 | 0 | char *auth_headers = NULL; |
563 | |
|
564 | 0 | DEBUGASSERT(!proxy); |
565 | 0 | (void)proxy; |
566 | |
|
567 | 0 | if(Curl_checkheaders(data, STRCONST("Authorization"))) { |
568 | | /* Authorization already present, Bailing out */ |
569 | 0 | return CURLE_OK; |
570 | 0 | } |
571 | | |
572 | | /* we init those buffers here, so goto fail will free initialized dynbuf */ |
573 | 0 | Curl_dyn_init(&canonical_headers, CURL_MAX_HTTP_HEADER); |
574 | 0 | Curl_dyn_init(&canonical_query, CURL_MAX_HTTP_HEADER); |
575 | 0 | Curl_dyn_init(&signed_headers, CURL_MAX_HTTP_HEADER); |
576 | | |
577 | | /* |
578 | | * Parameters parsing |
579 | | * Google and Outscale use the same OSC or GOOG, |
580 | | * but Amazon uses AWS and AMZ for header arguments. |
581 | | * AWS is the default because most of non-amazon providers |
582 | | * are still using aws:amz as a prefix. |
583 | | */ |
584 | 0 | arg = data->set.str[STRING_AWS_SIGV4] ? |
585 | 0 | data->set.str[STRING_AWS_SIGV4] : "aws:amz"; |
586 | | |
587 | | /* provider1[:provider2[:region[:service]]] |
588 | | |
589 | | No string can be longer than N bytes of non-whitespace |
590 | | */ |
591 | 0 | (void)sscanf(arg, "%" MAX_SIGV4_LEN_TXT "[^:]" |
592 | 0 | ":%" MAX_SIGV4_LEN_TXT "[^:]" |
593 | 0 | ":%" MAX_SIGV4_LEN_TXT "[^:]" |
594 | 0 | ":%" MAX_SIGV4_LEN_TXT "s", |
595 | 0 | provider0, provider1, region, service); |
596 | 0 | if(!provider0[0]) { |
597 | 0 | failf(data, "first aws-sigv4 provider can't be empty"); |
598 | 0 | result = CURLE_BAD_FUNCTION_ARGUMENT; |
599 | 0 | goto fail; |
600 | 0 | } |
601 | 0 | else if(!provider1[0]) |
602 | 0 | strcpy(provider1, provider0); |
603 | | |
604 | 0 | if(!service[0]) { |
605 | 0 | char *hostdot = strchr(hostname, '.'); |
606 | 0 | if(!hostdot) { |
607 | 0 | failf(data, "aws-sigv4: service missing in parameters and hostname"); |
608 | 0 | result = CURLE_URL_MALFORMAT; |
609 | 0 | goto fail; |
610 | 0 | } |
611 | 0 | len = hostdot - hostname; |
612 | 0 | if(len > MAX_SIGV4_LEN) { |
613 | 0 | failf(data, "aws-sigv4: service too long in hostname"); |
614 | 0 | result = CURLE_URL_MALFORMAT; |
615 | 0 | goto fail; |
616 | 0 | } |
617 | 0 | memcpy(service, hostname, len); |
618 | 0 | service[len] = '\0'; |
619 | |
|
620 | 0 | infof(data, "aws_sigv4: picked service %s from host", service); |
621 | |
|
622 | 0 | if(!region[0]) { |
623 | 0 | const char *reg = hostdot + 1; |
624 | 0 | const char *hostreg = strchr(reg, '.'); |
625 | 0 | if(!hostreg) { |
626 | 0 | failf(data, "aws-sigv4: region missing in parameters and hostname"); |
627 | 0 | result = CURLE_URL_MALFORMAT; |
628 | 0 | goto fail; |
629 | 0 | } |
630 | 0 | len = hostreg - reg; |
631 | 0 | if(len > MAX_SIGV4_LEN) { |
632 | 0 | failf(data, "aws-sigv4: region too long in hostname"); |
633 | 0 | result = CURLE_URL_MALFORMAT; |
634 | 0 | goto fail; |
635 | 0 | } |
636 | 0 | memcpy(region, reg, len); |
637 | 0 | region[len] = '\0'; |
638 | 0 | infof(data, "aws_sigv4: picked region %s from host", region); |
639 | 0 | } |
640 | 0 | } |
641 | | |
642 | 0 | Curl_http_method(data, conn, &method, &httpreq); |
643 | | |
644 | | /* AWS S3 requires a x-amz-content-sha256 header, and supports special |
645 | | * values like UNSIGNED-PAYLOAD */ |
646 | 0 | sign_as_s3 = (strcasecompare(provider0, "aws") && |
647 | 0 | strcasecompare(service, "s3")); |
648 | |
|
649 | 0 | payload_hash = parse_content_sha_hdr(data, provider1, &payload_hash_len); |
650 | |
|
651 | 0 | if(!payload_hash) { |
652 | 0 | if(sign_as_s3) |
653 | 0 | result = calc_s3_payload_hash(data, httpreq, provider1, sha_hash, |
654 | 0 | sha_hex, content_sha256_hdr); |
655 | 0 | else |
656 | 0 | result = calc_payload_hash(data, sha_hash, sha_hex); |
657 | 0 | if(result) |
658 | 0 | goto fail; |
659 | | |
660 | 0 | payload_hash = sha_hex; |
661 | | /* may be shorter than SHA256_HEX_LENGTH, like S3_UNSIGNED_PAYLOAD */ |
662 | 0 | payload_hash_len = strlen(sha_hex); |
663 | 0 | } |
664 | | |
665 | | #ifdef DEBUGBUILD |
666 | | { |
667 | | char *force_timestamp = getenv("CURL_FORCETIME"); |
668 | | if(force_timestamp) |
669 | | clock = 0; |
670 | | else |
671 | | time(&clock); |
672 | | } |
673 | | #else |
674 | 0 | time(&clock); |
675 | 0 | #endif |
676 | 0 | result = Curl_gmtime(clock, &tm); |
677 | 0 | if(result) { |
678 | 0 | goto fail; |
679 | 0 | } |
680 | 0 | if(!strftime(timestamp, sizeof(timestamp), "%Y%m%dT%H%M%SZ", &tm)) { |
681 | 0 | result = CURLE_OUT_OF_MEMORY; |
682 | 0 | goto fail; |
683 | 0 | } |
684 | | |
685 | 0 | result = make_headers(data, hostname, timestamp, provider1, |
686 | 0 | &date_header, content_sha256_hdr, |
687 | 0 | &canonical_headers, &signed_headers); |
688 | 0 | if(result) |
689 | 0 | goto fail; |
690 | | |
691 | 0 | if(*content_sha256_hdr) { |
692 | | /* make_headers() needed this without the \r\n for canonicalization */ |
693 | 0 | size_t hdrlen = strlen(content_sha256_hdr); |
694 | 0 | DEBUGASSERT(hdrlen + 3 < sizeof(content_sha256_hdr)); |
695 | 0 | memcpy(content_sha256_hdr + hdrlen, "\r\n", 3); |
696 | 0 | } |
697 | |
|
698 | 0 | memcpy(date, timestamp, sizeof(date)); |
699 | 0 | date[sizeof(date) - 1] = 0; |
700 | |
|
701 | 0 | result = canon_query(data, data->state.up.query, &canonical_query); |
702 | 0 | if(result) |
703 | 0 | goto fail; |
704 | 0 | result = CURLE_OUT_OF_MEMORY; |
705 | |
|
706 | 0 | canonical_request = |
707 | 0 | curl_maprintf("%s\n" /* HTTPRequestMethod */ |
708 | 0 | "%s\n" /* CanonicalURI */ |
709 | 0 | "%s\n" /* CanonicalQueryString */ |
710 | 0 | "%s\n" /* CanonicalHeaders */ |
711 | 0 | "%s\n" /* SignedHeaders */ |
712 | 0 | "%.*s", /* HashedRequestPayload in hex */ |
713 | 0 | method, |
714 | 0 | data->state.up.path, |
715 | 0 | Curl_dyn_ptr(&canonical_query) ? |
716 | 0 | Curl_dyn_ptr(&canonical_query) : "", |
717 | 0 | Curl_dyn_ptr(&canonical_headers), |
718 | 0 | Curl_dyn_ptr(&signed_headers), |
719 | 0 | (int)payload_hash_len, payload_hash); |
720 | 0 | if(!canonical_request) |
721 | 0 | goto fail; |
722 | | |
723 | 0 | DEBUGF(infof(data, "Canonical request: %s", canonical_request)); |
724 | | |
725 | | /* provider 0 lowercase */ |
726 | 0 | Curl_strntolower(provider0, provider0, strlen(provider0)); |
727 | 0 | request_type = curl_maprintf("%s4_request", provider0); |
728 | 0 | if(!request_type) |
729 | 0 | goto fail; |
730 | | |
731 | 0 | credential_scope = curl_maprintf("%s/%s/%s/%s", |
732 | 0 | date, region, service, request_type); |
733 | 0 | if(!credential_scope) |
734 | 0 | goto fail; |
735 | | |
736 | 0 | if(Curl_sha256it(sha_hash, (unsigned char *) canonical_request, |
737 | 0 | strlen(canonical_request))) |
738 | 0 | goto fail; |
739 | | |
740 | 0 | sha256_to_hex(sha_hex, sha_hash); |
741 | | |
742 | | /* provider 0 uppercase */ |
743 | 0 | Curl_strntoupper(provider0, provider0, strlen(provider0)); |
744 | | |
745 | | /* |
746 | | * Google allows using RSA key instead of HMAC, so this code might change |
747 | | * in the future. For now we only support HMAC. |
748 | | */ |
749 | 0 | str_to_sign = curl_maprintf("%s4-HMAC-SHA256\n" /* Algorithm */ |
750 | 0 | "%s\n" /* RequestDateTime */ |
751 | 0 | "%s\n" /* CredentialScope */ |
752 | 0 | "%s", /* HashedCanonicalRequest in hex */ |
753 | 0 | provider0, |
754 | 0 | timestamp, |
755 | 0 | credential_scope, |
756 | 0 | sha_hex); |
757 | 0 | if(!str_to_sign) { |
758 | 0 | goto fail; |
759 | 0 | } |
760 | | |
761 | | /* provider 0 uppercase */ |
762 | 0 | secret = curl_maprintf("%s4%s", provider0, |
763 | 0 | data->state.aptr.passwd ? |
764 | 0 | data->state.aptr.passwd : ""); |
765 | 0 | if(!secret) |
766 | 0 | goto fail; |
767 | | |
768 | 0 | HMAC_SHA256(secret, strlen(secret), date, strlen(date), sign0); |
769 | 0 | HMAC_SHA256(sign0, sizeof(sign0), region, strlen(region), sign1); |
770 | 0 | HMAC_SHA256(sign1, sizeof(sign1), service, strlen(service), sign0); |
771 | 0 | HMAC_SHA256(sign0, sizeof(sign0), request_type, strlen(request_type), sign1); |
772 | 0 | HMAC_SHA256(sign1, sizeof(sign1), str_to_sign, strlen(str_to_sign), sign0); |
773 | | |
774 | 0 | sha256_to_hex(sha_hex, sign0); |
775 | | |
776 | | /* provider 0 uppercase */ |
777 | 0 | auth_headers = curl_maprintf("Authorization: %s4-HMAC-SHA256 " |
778 | 0 | "Credential=%s/%s, " |
779 | 0 | "SignedHeaders=%s, " |
780 | 0 | "Signature=%s\r\n" |
781 | | /* |
782 | | * date_header is added here, only if it wasn't |
783 | | * user-specified (using CURLOPT_HTTPHEADER). |
784 | | * date_header includes \r\n |
785 | | */ |
786 | 0 | "%s" |
787 | 0 | "%s", /* optional sha256 header includes \r\n */ |
788 | 0 | provider0, |
789 | 0 | user, |
790 | 0 | credential_scope, |
791 | 0 | Curl_dyn_ptr(&signed_headers), |
792 | 0 | sha_hex, |
793 | 0 | date_header ? date_header : "", |
794 | 0 | content_sha256_hdr); |
795 | 0 | if(!auth_headers) { |
796 | 0 | goto fail; |
797 | 0 | } |
798 | | |
799 | 0 | Curl_safefree(data->state.aptr.userpwd); |
800 | 0 | data->state.aptr.userpwd = auth_headers; |
801 | 0 | data->state.authhost.done = TRUE; |
802 | 0 | result = CURLE_OK; |
803 | |
|
804 | 0 | fail: |
805 | 0 | Curl_dyn_free(&canonical_query); |
806 | 0 | Curl_dyn_free(&canonical_headers); |
807 | 0 | Curl_dyn_free(&signed_headers); |
808 | 0 | free(canonical_request); |
809 | 0 | free(request_type); |
810 | 0 | free(credential_scope); |
811 | 0 | free(str_to_sign); |
812 | 0 | free(secret); |
813 | 0 | free(date_header); |
814 | 0 | return result; |
815 | 0 | } |
816 | | |
817 | | #endif /* !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_AWS) */ |