http_aws_sigv4.c 23 KB

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