/src/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 | |
|
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 | strncpy(timestamp, value, TIMESTAMP_SIZE - 1); |
260 | 0 | timestamp[TIMESTAMP_SIZE - 1] = 0; |
261 | 0 | *date_header = NULL; |
262 | 0 | } |
263 | | |
264 | | /* alpha-sort in a case sensitive manner */ |
265 | 0 | do { |
266 | 0 | again = 0; |
267 | 0 | for(l = head; l; l = l->next) { |
268 | 0 | struct curl_slist *next = l->next; |
269 | |
|
270 | 0 | if(next && strcmp(l->data, next->data) > 0) { |
271 | 0 | char *tmp = l->data; |
272 | |
|
273 | 0 | l->data = next->data; |
274 | 0 | next->data = tmp; |
275 | 0 | again = 1; |
276 | 0 | } |
277 | 0 | } |
278 | 0 | } while(again); |
279 | |
|
280 | 0 | for(l = head; l; l = l->next) { |
281 | 0 | char *tmp; |
282 | |
|
283 | 0 | if(Curl_dyn_add(canonical_headers, l->data)) |
284 | 0 | goto fail; |
285 | 0 | if(Curl_dyn_add(canonical_headers, "\n")) |
286 | 0 | goto fail; |
287 | | |
288 | 0 | tmp = strchr(l->data, ':'); |
289 | 0 | if(tmp) |
290 | 0 | *tmp = 0; |
291 | |
|
292 | 0 | if(l != head) { |
293 | 0 | if(Curl_dyn_add(signed_headers, ";")) |
294 | 0 | goto fail; |
295 | 0 | } |
296 | 0 | if(Curl_dyn_add(signed_headers, l->data)) |
297 | 0 | goto fail; |
298 | 0 | } |
299 | | |
300 | 0 | ret = CURLE_OK; |
301 | 0 | fail: |
302 | 0 | curl_slist_free_all(head); |
303 | |
|
304 | 0 | return ret; |
305 | 0 | } |
306 | | |
307 | 0 | #define CONTENT_SHA256_KEY_LEN (MAX_SIGV4_LEN + sizeof("X--Content-Sha256")) |
308 | | /* add 2 for ": " between header name and value */ |
309 | 0 | #define CONTENT_SHA256_HDR_LEN (CONTENT_SHA256_KEY_LEN + 2 + \ |
310 | 0 | SHA256_HEX_LENGTH) |
311 | | |
312 | | /* try to parse a payload hash from the content-sha256 header */ |
313 | | static char *parse_content_sha_hdr(struct Curl_easy *data, |
314 | | const char *provider1, |
315 | | size_t *value_len) |
316 | 0 | { |
317 | 0 | char key[CONTENT_SHA256_KEY_LEN]; |
318 | 0 | size_t key_len; |
319 | 0 | char *value; |
320 | 0 | size_t len; |
321 | |
|
322 | 0 | key_len = msnprintf(key, sizeof(key), "x-%s-content-sha256", provider1); |
323 | |
|
324 | 0 | value = Curl_checkheaders(data, key, key_len); |
325 | 0 | if(!value) |
326 | 0 | return NULL; |
327 | | |
328 | 0 | value = strchr(value, ':'); |
329 | 0 | if(!value) |
330 | 0 | return NULL; |
331 | 0 | ++value; |
332 | |
|
333 | 0 | while(*value && ISBLANK(*value)) |
334 | 0 | ++value; |
335 | |
|
336 | 0 | len = strlen(value); |
337 | 0 | while(len > 0 && ISBLANK(value[len-1])) |
338 | 0 | --len; |
339 | |
|
340 | 0 | *value_len = len; |
341 | 0 | return value; |
342 | 0 | } |
343 | | |
344 | | static CURLcode calc_payload_hash(struct Curl_easy *data, |
345 | | unsigned char *sha_hash, char *sha_hex) |
346 | 0 | { |
347 | 0 | const char *post_data = data->set.postfields; |
348 | 0 | size_t post_data_len = 0; |
349 | 0 | CURLcode result; |
350 | |
|
351 | 0 | if(post_data) { |
352 | 0 | if(data->set.postfieldsize < 0) |
353 | 0 | post_data_len = strlen(post_data); |
354 | 0 | else |
355 | 0 | post_data_len = (size_t)data->set.postfieldsize; |
356 | 0 | } |
357 | 0 | result = Curl_sha256it(sha_hash, (const unsigned char *) post_data, |
358 | 0 | post_data_len); |
359 | 0 | if(!result) |
360 | 0 | sha256_to_hex(sha_hex, sha_hash); |
361 | 0 | return result; |
362 | 0 | } |
363 | | |
364 | 0 | #define S3_UNSIGNED_PAYLOAD "UNSIGNED-PAYLOAD" |
365 | | |
366 | | static CURLcode calc_s3_payload_hash(struct Curl_easy *data, |
367 | | Curl_HttpReq httpreq, char *provider1, |
368 | | unsigned char *sha_hash, |
369 | | char *sha_hex, char *header) |
370 | 0 | { |
371 | 0 | bool empty_method = (httpreq == HTTPREQ_GET || httpreq == HTTPREQ_HEAD); |
372 | | /* The request method or filesize indicate no request payload */ |
373 | 0 | bool empty_payload = (empty_method || data->set.filesize == 0); |
374 | | /* The POST payload is in memory */ |
375 | 0 | bool post_payload = (httpreq == HTTPREQ_POST && data->set.postfields); |
376 | 0 | CURLcode ret = CURLE_OUT_OF_MEMORY; |
377 | |
|
378 | 0 | if(empty_payload || post_payload) { |
379 | | /* Calculate a real hash when we know the request payload */ |
380 | 0 | ret = calc_payload_hash(data, sha_hash, sha_hex); |
381 | 0 | if(ret) |
382 | 0 | goto fail; |
383 | 0 | } |
384 | 0 | else { |
385 | | /* Fall back to s3's UNSIGNED-PAYLOAD */ |
386 | 0 | size_t len = sizeof(S3_UNSIGNED_PAYLOAD) - 1; |
387 | 0 | DEBUGASSERT(len < SHA256_HEX_LENGTH); /* 16 < 65 */ |
388 | 0 | memcpy(sha_hex, S3_UNSIGNED_PAYLOAD, len); |
389 | 0 | sha_hex[len] = 0; |
390 | 0 | } |
391 | | |
392 | | /* format the required content-sha256 header */ |
393 | 0 | msnprintf(header, CONTENT_SHA256_HDR_LEN, |
394 | 0 | "x-%s-content-sha256: %s", provider1, sha_hex); |
395 | |
|
396 | 0 | ret = CURLE_OK; |
397 | 0 | fail: |
398 | 0 | return ret; |
399 | 0 | } |
400 | | |
401 | | struct pair { |
402 | | const char *p; |
403 | | size_t len; |
404 | | }; |
405 | | |
406 | | static int compare_func(const void *a, const void *b) |
407 | 0 | { |
408 | 0 | const struct pair *aa = a; |
409 | 0 | const struct pair *bb = b; |
410 | | /* If one element is empty, the other is always sorted higher */ |
411 | 0 | if(aa->len == 0) |
412 | 0 | return -1; |
413 | 0 | if(bb->len == 0) |
414 | 0 | return 1; |
415 | 0 | return strncmp(aa->p, bb->p, aa->len < bb->len ? aa->len : bb->len); |
416 | 0 | } |
417 | | |
418 | 0 | #define MAX_QUERYPAIRS 64 |
419 | | |
420 | | static CURLcode canon_query(struct Curl_easy *data, |
421 | | const char *query, struct dynbuf *dq) |
422 | 0 | { |
423 | 0 | CURLcode result = CURLE_OK; |
424 | 0 | int entry = 0; |
425 | 0 | int i; |
426 | 0 | const char *p = query; |
427 | 0 | struct pair array[MAX_QUERYPAIRS]; |
428 | 0 | struct pair *ap = &array[0]; |
429 | 0 | if(!query) |
430 | 0 | return result; |
431 | | |
432 | | /* sort the name=value pairs first */ |
433 | 0 | do { |
434 | 0 | char *amp; |
435 | 0 | entry++; |
436 | 0 | ap->p = p; |
437 | 0 | amp = strchr(p, '&'); |
438 | 0 | if(amp) |
439 | 0 | ap->len = amp - p; /* excluding the ampersand */ |
440 | 0 | else { |
441 | 0 | ap->len = strlen(p); |
442 | 0 | break; |
443 | 0 | } |
444 | 0 | ap++; |
445 | 0 | p = amp + 1; |
446 | 0 | } while(entry < MAX_QUERYPAIRS); |
447 | 0 | if(entry == MAX_QUERYPAIRS) { |
448 | | /* too many query pairs for us */ |
449 | 0 | failf(data, "aws-sigv4: too many query pairs in URL"); |
450 | 0 | return CURLE_URL_MALFORMAT; |
451 | 0 | } |
452 | | |
453 | 0 | qsort(&array[0], entry, sizeof(struct pair), compare_func); |
454 | |
|
455 | 0 | ap = &array[0]; |
456 | 0 | for(i = 0; !result && (i < entry); i++, ap++) { |
457 | 0 | size_t len; |
458 | 0 | const char *q = ap->p; |
459 | 0 | bool found_equals = false; |
460 | 0 | if(!ap->len) |
461 | 0 | continue; |
462 | 0 | for(len = ap->len; len && !result; q++, len--) { |
463 | 0 | if(ISALNUM(*q)) |
464 | 0 | result = Curl_dyn_addn(dq, q, 1); |
465 | 0 | else { |
466 | 0 | switch(*q) { |
467 | 0 | case '-': |
468 | 0 | case '.': |
469 | 0 | case '_': |
470 | 0 | case '~': |
471 | | /* allowed as-is */ |
472 | 0 | result = Curl_dyn_addn(dq, q, 1); |
473 | 0 | break; |
474 | 0 | case '=': |
475 | | /* allowed as-is */ |
476 | 0 | result = Curl_dyn_addn(dq, q, 1); |
477 | 0 | found_equals = true; |
478 | 0 | break; |
479 | 0 | case '%': |
480 | | /* uppercase the following if hexadecimal */ |
481 | 0 | if(ISXDIGIT(q[1]) && ISXDIGIT(q[2])) { |
482 | 0 | char tmp[3]="%"; |
483 | 0 | tmp[1] = Curl_raw_toupper(q[1]); |
484 | 0 | tmp[2] = Curl_raw_toupper(q[2]); |
485 | 0 | result = Curl_dyn_addn(dq, tmp, 3); |
486 | 0 | q += 2; |
487 | 0 | len -= 2; |
488 | 0 | } |
489 | 0 | else |
490 | | /* '%' without a following two-digit hex, encode it */ |
491 | 0 | result = Curl_dyn_addn(dq, "%25", 3); |
492 | 0 | break; |
493 | 0 | default: { |
494 | | /* URL encode */ |
495 | 0 | const char hex[] = "0123456789ABCDEF"; |
496 | 0 | char out[3]={'%'}; |
497 | 0 | out[1] = hex[((unsigned char)*q)>>4]; |
498 | 0 | out[2] = hex[*q & 0xf]; |
499 | 0 | result = Curl_dyn_addn(dq, out, 3); |
500 | 0 | break; |
501 | 0 | } |
502 | 0 | } |
503 | 0 | } |
504 | 0 | } |
505 | 0 | if(!result && !found_equals) { |
506 | | /* queries without value still need an equals */ |
507 | 0 | result = Curl_dyn_addn(dq, "=", 1); |
508 | 0 | } |
509 | 0 | if(!result && i < entry - 1) { |
510 | | /* insert ampersands between query pairs */ |
511 | 0 | result = Curl_dyn_addn(dq, "&", 1); |
512 | 0 | } |
513 | 0 | } |
514 | 0 | return result; |
515 | 0 | } |
516 | | |
517 | | |
518 | | CURLcode Curl_output_aws_sigv4(struct Curl_easy *data, bool proxy) |
519 | 0 | { |
520 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
521 | 0 | struct connectdata *conn = data->conn; |
522 | 0 | size_t len; |
523 | 0 | const char *arg; |
524 | 0 | char provider0[MAX_SIGV4_LEN + 1]=""; |
525 | 0 | char provider1[MAX_SIGV4_LEN + 1]=""; |
526 | 0 | char region[MAX_SIGV4_LEN + 1]=""; |
527 | 0 | char service[MAX_SIGV4_LEN + 1]=""; |
528 | 0 | bool sign_as_s3 = false; |
529 | 0 | const char *hostname = conn->host.name; |
530 | 0 | time_t clock; |
531 | 0 | struct tm tm; |
532 | 0 | char timestamp[TIMESTAMP_SIZE]; |
533 | 0 | char date[9]; |
534 | 0 | struct dynbuf canonical_headers; |
535 | 0 | struct dynbuf signed_headers; |
536 | 0 | struct dynbuf canonical_query; |
537 | 0 | char *date_header = NULL; |
538 | 0 | Curl_HttpReq httpreq; |
539 | 0 | const char *method = NULL; |
540 | 0 | char *payload_hash = NULL; |
541 | 0 | size_t payload_hash_len = 0; |
542 | 0 | unsigned char sha_hash[SHA256_DIGEST_LENGTH]; |
543 | 0 | char sha_hex[SHA256_HEX_LENGTH]; |
544 | 0 | char content_sha256_hdr[CONTENT_SHA256_HDR_LEN + 2] = ""; /* add \r\n */ |
545 | 0 | char *canonical_request = NULL; |
546 | 0 | char *request_type = NULL; |
547 | 0 | char *credential_scope = NULL; |
548 | 0 | char *str_to_sign = NULL; |
549 | 0 | const char *user = data->state.aptr.user ? data->state.aptr.user : ""; |
550 | 0 | char *secret = NULL; |
551 | 0 | unsigned char sign0[SHA256_DIGEST_LENGTH] = {0}; |
552 | 0 | unsigned char sign1[SHA256_DIGEST_LENGTH] = {0}; |
553 | 0 | char *auth_headers = NULL; |
554 | |
|
555 | 0 | DEBUGASSERT(!proxy); |
556 | 0 | (void)proxy; |
557 | |
|
558 | 0 | if(Curl_checkheaders(data, STRCONST("Authorization"))) { |
559 | | /* Authorization already present, Bailing out */ |
560 | 0 | return CURLE_OK; |
561 | 0 | } |
562 | | |
563 | | /* we init those buffers here, so goto fail will free initialized dynbuf */ |
564 | 0 | Curl_dyn_init(&canonical_headers, CURL_MAX_HTTP_HEADER); |
565 | 0 | Curl_dyn_init(&canonical_query, CURL_MAX_HTTP_HEADER); |
566 | 0 | Curl_dyn_init(&signed_headers, CURL_MAX_HTTP_HEADER); |
567 | | |
568 | | /* |
569 | | * Parameters parsing |
570 | | * Google and Outscale use the same OSC or GOOG, |
571 | | * but Amazon uses AWS and AMZ for header arguments. |
572 | | * AWS is the default because most of non-amazon providers |
573 | | * are still using aws:amz as a prefix. |
574 | | */ |
575 | 0 | arg = data->set.str[STRING_AWS_SIGV4] ? |
576 | 0 | data->set.str[STRING_AWS_SIGV4] : "aws:amz"; |
577 | | |
578 | | /* provider1[:provider2[:region[:service]]] |
579 | | |
580 | | No string can be longer than N bytes of non-whitespace |
581 | | */ |
582 | 0 | (void)sscanf(arg, "%" MAX_SIGV4_LEN_TXT "[^:]" |
583 | 0 | ":%" MAX_SIGV4_LEN_TXT "[^:]" |
584 | 0 | ":%" MAX_SIGV4_LEN_TXT "[^:]" |
585 | 0 | ":%" MAX_SIGV4_LEN_TXT "s", |
586 | 0 | provider0, provider1, region, service); |
587 | 0 | if(!provider0[0]) { |
588 | 0 | failf(data, "first aws-sigv4 provider can't be empty"); |
589 | 0 | result = CURLE_BAD_FUNCTION_ARGUMENT; |
590 | 0 | goto fail; |
591 | 0 | } |
592 | 0 | else if(!provider1[0]) |
593 | 0 | strcpy(provider1, provider0); |
594 | | |
595 | 0 | if(!service[0]) { |
596 | 0 | char *hostdot = strchr(hostname, '.'); |
597 | 0 | if(!hostdot) { |
598 | 0 | failf(data, "aws-sigv4: service missing in parameters and hostname"); |
599 | 0 | result = CURLE_URL_MALFORMAT; |
600 | 0 | goto fail; |
601 | 0 | } |
602 | 0 | len = hostdot - hostname; |
603 | 0 | if(len > MAX_SIGV4_LEN) { |
604 | 0 | failf(data, "aws-sigv4: service too long in hostname"); |
605 | 0 | result = CURLE_URL_MALFORMAT; |
606 | 0 | goto fail; |
607 | 0 | } |
608 | 0 | strncpy(service, hostname, len); |
609 | 0 | service[len] = '\0'; |
610 | |
|
611 | 0 | infof(data, "aws_sigv4: picked service %s from host", service); |
612 | |
|
613 | 0 | if(!region[0]) { |
614 | 0 | const char *reg = hostdot + 1; |
615 | 0 | const char *hostreg = strchr(reg, '.'); |
616 | 0 | if(!hostreg) { |
617 | 0 | failf(data, "aws-sigv4: region missing in parameters and hostname"); |
618 | 0 | result = CURLE_URL_MALFORMAT; |
619 | 0 | goto fail; |
620 | 0 | } |
621 | 0 | len = hostreg - reg; |
622 | 0 | if(len > MAX_SIGV4_LEN) { |
623 | 0 | failf(data, "aws-sigv4: region too long in hostname"); |
624 | 0 | result = CURLE_URL_MALFORMAT; |
625 | 0 | goto fail; |
626 | 0 | } |
627 | 0 | strncpy(region, reg, len); |
628 | 0 | region[len] = '\0'; |
629 | 0 | infof(data, "aws_sigv4: picked region %s from host", region); |
630 | 0 | } |
631 | 0 | } |
632 | | |
633 | 0 | Curl_http_method(data, conn, &method, &httpreq); |
634 | | |
635 | | /* AWS S3 requires a x-amz-content-sha256 header, and supports special |
636 | | * values like UNSIGNED-PAYLOAD */ |
637 | 0 | sign_as_s3 = (strcasecompare(provider0, "aws") && |
638 | 0 | strcasecompare(service, "s3")); |
639 | |
|
640 | 0 | payload_hash = parse_content_sha_hdr(data, provider1, &payload_hash_len); |
641 | |
|
642 | 0 | if(!payload_hash) { |
643 | 0 | if(sign_as_s3) |
644 | 0 | result = calc_s3_payload_hash(data, httpreq, provider1, sha_hash, |
645 | 0 | sha_hex, content_sha256_hdr); |
646 | 0 | else |
647 | 0 | result = calc_payload_hash(data, sha_hash, sha_hex); |
648 | 0 | if(result) |
649 | 0 | goto fail; |
650 | | |
651 | 0 | payload_hash = sha_hex; |
652 | | /* may be shorter than SHA256_HEX_LENGTH, like S3_UNSIGNED_PAYLOAD */ |
653 | 0 | payload_hash_len = strlen(sha_hex); |
654 | 0 | } |
655 | | |
656 | 0 | #ifdef DEBUGBUILD |
657 | 0 | { |
658 | 0 | char *force_timestamp = getenv("CURL_FORCETIME"); |
659 | 0 | if(force_timestamp) |
660 | 0 | clock = 0; |
661 | 0 | else |
662 | 0 | time(&clock); |
663 | 0 | } |
664 | | #else |
665 | | time(&clock); |
666 | | #endif |
667 | 0 | result = Curl_gmtime(clock, &tm); |
668 | 0 | if(result) { |
669 | 0 | goto fail; |
670 | 0 | } |
671 | 0 | if(!strftime(timestamp, sizeof(timestamp), "%Y%m%dT%H%M%SZ", &tm)) { |
672 | 0 | result = CURLE_OUT_OF_MEMORY; |
673 | 0 | goto fail; |
674 | 0 | } |
675 | | |
676 | 0 | result = make_headers(data, hostname, timestamp, provider1, |
677 | 0 | &date_header, content_sha256_hdr, |
678 | 0 | &canonical_headers, &signed_headers); |
679 | 0 | if(result) |
680 | 0 | goto fail; |
681 | | |
682 | 0 | if(*content_sha256_hdr) { |
683 | | /* make_headers() needed this without the \r\n for canonicalization */ |
684 | 0 | size_t hdrlen = strlen(content_sha256_hdr); |
685 | 0 | DEBUGASSERT(hdrlen + 3 < sizeof(content_sha256_hdr)); |
686 | 0 | memcpy(content_sha256_hdr + hdrlen, "\r\n", 3); |
687 | 0 | } |
688 | | |
689 | 0 | memcpy(date, timestamp, sizeof(date)); |
690 | 0 | date[sizeof(date) - 1] = 0; |
691 | |
|
692 | 0 | result = canon_query(data, data->state.up.query, &canonical_query); |
693 | 0 | if(result) |
694 | 0 | goto fail; |
695 | 0 | result = CURLE_OUT_OF_MEMORY; |
696 | |
|
697 | 0 | canonical_request = |
698 | 0 | curl_maprintf("%s\n" /* HTTPRequestMethod */ |
699 | 0 | "%s\n" /* CanonicalURI */ |
700 | 0 | "%s\n" /* CanonicalQueryString */ |
701 | 0 | "%s\n" /* CanonicalHeaders */ |
702 | 0 | "%s\n" /* SignedHeaders */ |
703 | 0 | "%.*s", /* HashedRequestPayload in hex */ |
704 | 0 | method, |
705 | 0 | data->state.up.path, |
706 | 0 | Curl_dyn_ptr(&canonical_query) ? |
707 | 0 | Curl_dyn_ptr(&canonical_query) : "", |
708 | 0 | Curl_dyn_ptr(&canonical_headers), |
709 | 0 | Curl_dyn_ptr(&signed_headers), |
710 | 0 | (int)payload_hash_len, payload_hash); |
711 | 0 | if(!canonical_request) |
712 | 0 | goto fail; |
713 | | |
714 | 0 | DEBUGF(infof(data, "Canonical request: %s", canonical_request)); |
715 | | |
716 | | /* provider 0 lowercase */ |
717 | 0 | Curl_strntolower(provider0, provider0, strlen(provider0)); |
718 | 0 | request_type = curl_maprintf("%s4_request", provider0); |
719 | 0 | if(!request_type) |
720 | 0 | goto fail; |
721 | | |
722 | 0 | credential_scope = curl_maprintf("%s/%s/%s/%s", |
723 | 0 | date, region, service, request_type); |
724 | 0 | if(!credential_scope) |
725 | 0 | goto fail; |
726 | | |
727 | 0 | if(Curl_sha256it(sha_hash, (unsigned char *) canonical_request, |
728 | 0 | strlen(canonical_request))) |
729 | 0 | goto fail; |
730 | | |
731 | 0 | sha256_to_hex(sha_hex, sha_hash); |
732 | | |
733 | | /* provider 0 uppercase */ |
734 | 0 | Curl_strntoupper(provider0, provider0, strlen(provider0)); |
735 | | |
736 | | /* |
737 | | * Google allows using RSA key instead of HMAC, so this code might change |
738 | | * in the future. For now we only support HMAC. |
739 | | */ |
740 | 0 | str_to_sign = curl_maprintf("%s4-HMAC-SHA256\n" /* Algorithm */ |
741 | 0 | "%s\n" /* RequestDateTime */ |
742 | 0 | "%s\n" /* CredentialScope */ |
743 | 0 | "%s", /* HashedCanonicalRequest in hex */ |
744 | 0 | provider0, |
745 | 0 | timestamp, |
746 | 0 | credential_scope, |
747 | 0 | sha_hex); |
748 | 0 | if(!str_to_sign) { |
749 | 0 | goto fail; |
750 | 0 | } |
751 | | |
752 | | /* provider 0 uppercase */ |
753 | 0 | secret = curl_maprintf("%s4%s", provider0, |
754 | 0 | data->state.aptr.passwd ? |
755 | 0 | data->state.aptr.passwd : ""); |
756 | 0 | if(!secret) |
757 | 0 | goto fail; |
758 | | |
759 | 0 | HMAC_SHA256(secret, strlen(secret), date, strlen(date), sign0); |
760 | 0 | HMAC_SHA256(sign0, sizeof(sign0), region, strlen(region), sign1); |
761 | 0 | HMAC_SHA256(sign1, sizeof(sign1), service, strlen(service), sign0); |
762 | 0 | HMAC_SHA256(sign0, sizeof(sign0), request_type, strlen(request_type), sign1); |
763 | 0 | HMAC_SHA256(sign1, sizeof(sign1), str_to_sign, strlen(str_to_sign), sign0); |
764 | | |
765 | 0 | sha256_to_hex(sha_hex, sign0); |
766 | | |
767 | | /* provider 0 uppercase */ |
768 | 0 | auth_headers = curl_maprintf("Authorization: %s4-HMAC-SHA256 " |
769 | 0 | "Credential=%s/%s, " |
770 | 0 | "SignedHeaders=%s, " |
771 | 0 | "Signature=%s\r\n" |
772 | | /* |
773 | | * date_header is added here, only if it wasn't |
774 | | * user-specified (using CURLOPT_HTTPHEADER). |
775 | | * date_header includes \r\n |
776 | | */ |
777 | 0 | "%s" |
778 | 0 | "%s", /* optional sha256 header includes \r\n */ |
779 | 0 | provider0, |
780 | 0 | user, |
781 | 0 | credential_scope, |
782 | 0 | Curl_dyn_ptr(&signed_headers), |
783 | 0 | sha_hex, |
784 | 0 | date_header ? date_header : "", |
785 | 0 | content_sha256_hdr); |
786 | 0 | if(!auth_headers) { |
787 | 0 | goto fail; |
788 | 0 | } |
789 | | |
790 | 0 | Curl_safefree(data->state.aptr.userpwd); |
791 | 0 | data->state.aptr.userpwd = auth_headers; |
792 | 0 | data->state.authhost.done = TRUE; |
793 | 0 | result = CURLE_OK; |
794 | |
|
795 | 0 | fail: |
796 | 0 | Curl_dyn_free(&canonical_query); |
797 | 0 | Curl_dyn_free(&canonical_headers); |
798 | 0 | Curl_dyn_free(&signed_headers); |
799 | 0 | free(canonical_request); |
800 | 0 | free(request_type); |
801 | 0 | free(credential_scope); |
802 | 0 | free(str_to_sign); |
803 | 0 | free(secret); |
804 | 0 | free(date_header); |
805 | 0 | return result; |
806 | 0 | } |
807 | | |
808 | | #endif /* !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_AWS) */ |