http_aws_sigv4.c 24 KB

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