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