http_aws_sigv4.c 24 KB

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