Coverage Report

Created: 2026-06-15 07:03

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