Coverage Report

Created: 2026-12-01 06:24

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