Coverage Report

Created: 2025-10-10 06:09

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