Coverage Report

Created: 2026-12-01 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http_httpsig.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_HTTPSIG)
27
28
#include "urldata.h"
29
#include "http_httpsig.h"
30
#include "curl_ed25519.h"
31
#include "curl_hmac.h"
32
#include "curl_sha256.h"
33
#include "http.h"
34
#include "transfer.h"
35
#include "curl_trc.h"
36
#include "slist.h"
37
#include "curlx/dynbuf.h"
38
#include "curlx/base64.h"
39
#include "curlx/strdup.h"
40
#include "curlx/strparse.h"
41
#include "strcase.h"
42
43
#include <time.h>
44
45
0
#define HTTPSIG_MAX_SIG_BASE   CURL_MAX_HTTP_HEADER
46
0
#define HTTPSIG_MAX_COMPONENTS 16
47
#define HTTPSIG_MAX_RAW_SIG    CURL_ED25519_SIGLEN
48
0
#define HTTPSIG_DEFAULT_LABEL  "sig1"
49
50
enum httpsig_alg {
51
  HTTPSIG_ALG_ED25519,
52
  HTTPSIG_ALG_HMAC_SHA256,
53
  HTTPSIG_ALG_UNKNOWN
54
};
55
56
static const char *alg_to_str(enum httpsig_alg alg)
57
0
{
58
0
  switch(alg) {
59
0
  case HTTPSIG_ALG_ED25519:
60
0
    return "ed25519";
61
0
  case HTTPSIG_ALG_HMAC_SHA256:
62
0
    return "hmac-sha256";
63
0
  default:
64
0
    break;
65
0
  }
66
0
  return NULL;
67
0
}
68
69
static enum httpsig_alg id_to_alg(uint8_t val)
70
0
{
71
0
  switch(val) {
72
0
  case CURLHTTPSIG_ED25519:
73
0
    return HTTPSIG_ALG_ED25519;
74
0
  case CURLHTTPSIG_HMAC_SHA256:
75
0
    return HTTPSIG_ALG_HMAC_SHA256;
76
0
  default:
77
0
    break;
78
0
  }
79
0
  return HTTPSIG_ALG_UNKNOWN;
80
0
}
81
82
static CURLcode decode_hex_key(struct Curl_easy *data,
83
                               const char *hexstr,
84
                               uint8_t **keyout, size_t *keylen)
85
0
{
86
0
  size_t len, i;
87
0
  uint8_t *keybuf;
88
89
0
  *keyout = NULL;
90
0
  *keylen = 0;
91
92
0
  len = strlen(hexstr);
93
0
  while(len > 0 && ISNEWLINE(hexstr[len - 1]))
94
0
    len--;
95
96
0
  if(len == 0 || (len & 1) != 0) {
97
0
    failf(data, "httpsig: invalid hex key (length %zu)", len);
98
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
99
0
  }
100
101
0
  if(len > CURL_MAX_INPUT_LENGTH) {
102
0
    failf(data, "httpsig: hex key too long");
103
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
104
0
  }
105
106
0
  keybuf = curlx_malloc(len / 2);
107
0
  if(!keybuf)
108
0
    return CURLE_OUT_OF_MEMORY;
109
110
0
  for(i = 0; i < len; i += 2) {
111
0
    if(!ISXDIGIT(hexstr[i]) || !ISXDIGIT(hexstr[i + 1])) {
112
0
      failf(data, "httpsig: invalid hex at position %zu ('%c%c')",
113
0
            i, hexstr[i], hexstr[i + 1]);
114
0
      curlx_free(keybuf);
115
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
116
0
    }
117
0
    keybuf[i / 2] = (uint8_t)((curlx_hexval(hexstr[i]) << 4) |
118
0
                               curlx_hexval(hexstr[i + 1]));
119
0
  }
120
121
0
  *keyout = keybuf;
122
0
  *keylen = len / 2;
123
0
  return CURLE_OK;
124
0
}
125
126
/* @authority matches the Host header field-value when available (RFC 9421).
127
   data->state.http_host is produced by http_set_aptr_host() before auth. */
128
static CURLcode httpsig_authority(struct Curl_easy *data,
129
                                  struct connectdata *conn,
130
                                  struct dynbuf *authority_buf)
131
0
{
132
0
  const char *h = data->state.http_host;
133
134
0
  if(h && curl_strnequal(h, "host:", 5)) {
135
0
    const char *value = h + 5;
136
0
    const char *end;
137
138
0
    while(ISBLANK(*value))
139
0
      value++;
140
0
    if(*value) {
141
0
      CURLcode result;
142
143
0
      end = value;
144
0
      while(*end && !ISNEWLINE(*end))
145
0
        end++;
146
0
      while(end > value && ISBLANK(end[-1]))
147
0
        end--;
148
0
      result = curlx_dyn_addn(authority_buf, value, (size_t)(end - value));
149
0
      if(result)
150
0
        return result;
151
0
      return CURLE_OK;
152
0
    }
153
0
  }
154
155
0
  {
156
0
    const char *hostname = conn->origin->hostname;
157
0
    uint16_t port = conn->origin->port;
158
159
0
    if((conn->origin->scheme->defport != port) && port)
160
0
      return curlx_dyn_addf(authority_buf, "%s:%u", hostname, port);
161
0
    return curlx_dyn_add(authority_buf, hostname);
162
0
  }
163
0
}
164
165
static CURLcode sf_append_quoted(struct dynbuf *buf, const char *str)
166
0
{
167
0
  CURLcode result = curlx_dyn_addn(buf, "\"", 1);
168
0
  if(result)
169
0
    return result;
170
0
  while(*str) {
171
0
    if(ISCNTRL(*str))
172
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
173
0
    if(*str == '\\' || *str == '"') {
174
0
      result = curlx_dyn_addn(buf, "\\", 1);
175
0
      if(result)
176
0
        return result;
177
0
    }
178
0
    result = curlx_dyn_addn(buf, str, 1);
179
0
    if(result)
180
0
      return result;
181
0
    str++;
182
0
  }
183
0
  return curlx_dyn_addn(buf, "\"", 1);
184
0
}
185
186
/* base64-encode raw bytes into an RFC 8941 byte sequence (:base64:) */
187
static CURLcode sf_encode_byte_seq(const unsigned char *raw, size_t rawlen,
188
                                   struct dynbuf *out)
189
0
{
190
0
  CURLcode result;
191
0
  size_t b64len;
192
0
  char *b64;
193
194
0
  result = curlx_base64_encode(raw, rawlen, &b64, &b64len);
195
0
  if(result)
196
0
    return result;
197
198
0
  result = curlx_dyn_addn(out, ":", 1);
199
0
  if(!result)
200
0
    result = curlx_dyn_addn(out, b64, b64len);
201
0
  if(!result)
202
0
    result = curlx_dyn_addn(out, ":", 1);
203
204
0
  curlx_free(b64);
205
0
  return result;
206
0
}
207
208
static CURLcode build_sig_params(struct dynbuf *params,
209
                                 const char **components, size_t count,
210
                                 time_t created, const char *keyid,
211
                                 enum httpsig_alg alg)
212
0
{
213
0
  CURLcode result;
214
0
  size_t i;
215
216
0
  result = curlx_dyn_addn(params, "(", 1);
217
0
  if(result)
218
0
    return result;
219
220
0
  for(i = 0; i < count; i++) {
221
0
    if(i > 0) {
222
0
      result = curlx_dyn_addn(params, " ", 1);
223
0
      if(result)
224
0
        return result;
225
0
    }
226
0
    result = sf_append_quoted(params, components[i]);
227
0
    if(result)
228
0
      return result;
229
0
  }
230
231
0
  result = curlx_dyn_addn(params, ")", 1);
232
0
  if(result)
233
0
    return result;
234
235
0
  result = curlx_dyn_addf(params, ";created=%lld", (long long)created);
236
0
  if(result)
237
0
    return result;
238
239
0
  if(keyid && *keyid) {
240
0
    result = curlx_dyn_add(params, ";keyid=");
241
0
    if(result)
242
0
      return result;
243
0
    result = sf_append_quoted(params, keyid);
244
0
    if(result)
245
0
      return result;
246
0
  }
247
248
0
  result = curlx_dyn_addf(params, ";alg=\"%s\"", alg_to_str(alg));
249
0
  return result;
250
0
}
251
252
/* strings defined by RFC 9421 */
253
0
#define SIG_METHOD    "@method"
254
0
#define SIG_AUTHORITY "@authority"
255
0
#define SIG_PATH      "@path"
256
0
#define SIG_QUERY     "@query"
257
258
/* Resolve a component identifier to its value.
259
 * For headers, we walk the full user-supplied header list to combine
260
 * duplicate field values with ", " per RFC 9421 Section 2.1. Each
261
 * individual value is trimmed of leading/trailing OWS and the trailing
262
 * \r\n. The combined result is written into the caller-provided buffer. */
263
static CURLcode resolve_component(const char *name,
264
                                  const char *method,
265
                                  const char *authority,
266
                                  const char *path,
267
                                  const char *query,
268
                                  struct Curl_easy *data,
269
                                  struct dynbuf *valbuf,
270
                                  const char **out)
271
0
{
272
0
  *out = NULL;
273
274
0
  if(name[0] == '@') {
275
0
    if(curl_strequal(name, SIG_METHOD))
276
0
      *out = method;
277
0
    else if(curl_strequal(name, SIG_AUTHORITY))
278
0
      *out = authority;
279
0
    else if(curl_strequal(name, SIG_PATH))
280
0
      *out = path;
281
0
    else if(curl_strequal(name, SIG_QUERY))
282
0
      *out = query;
283
0
    else {
284
0
      failf(data, "httpsig: unsupported derived component '%s'", name);
285
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
286
0
    }
287
0
    if(!*out) {
288
0
      failf(data, "httpsig: derived component '%s' has no value", name);
289
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
290
0
    }
291
0
    return CURLE_OK;
292
0
  }
293
0
  else {
294
    /* RFC 9421 Section 2.1: walk all user-supplied headers and combine
295
       duplicate field values with ", " per HTTP field combination rules. */
296
0
    struct curl_slist *head;
297
0
    size_t namelen = strlen(name);
298
0
    bool found = FALSE;
299
300
0
    curlx_dyn_reset(valbuf);
301
302
0
    for(head = data->set.headers; head; head = head->next) {
303
0
      if(curl_strnequal(head->data, name, namelen) &&
304
0
         Curl_headersep(head->data[namelen])) {
305
0
        const char *p = strchr(head->data, ':');
306
0
        if(p) {
307
0
          CURLcode result;
308
0
          struct Curl_str content;
309
0
          curlx_str_assign(&content, p + 1, strlen(p + 1));
310
0
          curlx_str_trimblanks(&content);
311
0
          if(found) {
312
0
            result = curlx_dyn_addn(valbuf, ", ", 2);
313
0
            if(result)
314
0
              return result;
315
0
          }
316
0
          result = curlx_dyn_addn(valbuf, curlx_str(&content),
317
0
                                  curlx_strlen(&content));
318
0
          if(result)
319
0
            return result;
320
0
          found = TRUE;
321
0
        }
322
0
      }
323
0
    }
324
325
0
    if(found) {
326
0
      *out = curlx_dyn_ptr(valbuf);
327
0
      return CURLE_OK;
328
0
    }
329
0
    failf(data, "httpsig: header '%s' not found in request", name);
330
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
331
0
  }
332
0
}
333
334
static CURLcode build_sig_base(struct dynbuf *base,
335
                               const char **components, size_t count,
336
                               const char *method,
337
                               const char *authority,
338
                               const char *path,
339
                               const char *query,
340
                               struct Curl_easy *data,
341
                               const char *sig_params)
342
0
{
343
0
  CURLcode result;
344
0
  size_t i;
345
0
  struct dynbuf hdrvalbuf;
346
347
0
  curlx_dyn_init(&hdrvalbuf, CURL_MAX_HTTP_HEADER);
348
349
0
  for(i = 0; i < count; i++) {
350
0
    const char *val = NULL;
351
0
    result = resolve_component(components[i], method,
352
0
                               authority, path, query, data,
353
0
                               &hdrvalbuf, &val);
354
0
    if(result || !val) {
355
0
      failf(data, "httpsig: cannot resolve component '%s'", components[i]);
356
0
      curlx_dyn_free(&hdrvalbuf);
357
0
      return result ? result : CURLE_BAD_FUNCTION_ARGUMENT;
358
0
    }
359
360
0
    result = curlx_dyn_addf(base, "\"%s\": %s\n", components[i], val);
361
0
    if(result) {
362
0
      curlx_dyn_free(&hdrvalbuf);
363
0
      return result;
364
0
    }
365
0
  }
366
367
0
  curlx_dyn_free(&hdrvalbuf);
368
0
  result = curlx_dyn_addf(base, "\"@signature-params\": %s", sig_params);
369
0
  return result;
370
0
}
371
372
static CURLcode parse_components(struct Curl_easy *data,
373
                                 const char *query,
374
                                 const char **components,
375
                                 size_t *ncomp_out,
376
                                 char **hdrs_copy_out)
377
0
{
378
0
  const char *hdrs = CURL_EASY_STR(data, STRING_HTTPSIG_HEADERS);
379
0
  size_t ncomp = 0;
380
381
0
  *hdrs_copy_out = NULL;
382
0
  if(hdrs && *hdrs) {
383
0
    char *p;
384
0
    char *hdrs_copy = curlx_strdup(hdrs);
385
0
    if(!hdrs_copy)
386
0
      return CURLE_OUT_OF_MEMORY;
387
0
    *hdrs_copy_out = hdrs_copy;
388
0
    p = hdrs_copy;
389
0
    while(*p && ncomp < HTTPSIG_MAX_COMPONENTS) {
390
0
      char *start;
391
0
      size_t tlen = 0;
392
0
      const char *p2 = p;
393
394
0
      curlx_str_passblanks(&p2);
395
0
      if(!*p2)
396
0
        break;
397
0
      p = start = CURL_UNCONST(p2);
398
0
      while(*p && !ISBLANK(*p)) {
399
0
        if((*p == '\"') || (*p == '\\'))
400
0
          return CURLE_BAD_FUNCTION_ARGUMENT;
401
0
        p++;
402
0
        tlen++;
403
0
      }
404
0
      if(*p)
405
0
        *p++ = '\0';
406
407
0
      if(tlen && start[tlen - 1] == ':') {
408
        /* Header field: drop the trailing ':' marker. RFC 9421 field
409
           names are canonically lowercase (Section 2.1). */
410
0
        start[--tlen] = '\0';
411
0
        if(!tlen) {
412
0
          failf(data, "httpsig: empty header component name");
413
0
          return CURLE_BAD_FUNCTION_ARGUMENT;
414
0
        }
415
0
        Curl_strntolower(start, start, tlen);
416
0
        components[ncomp++] = start;
417
0
      }
418
0
      else {
419
        /* Derived component: map the bare name to its canonical RFC 9421
420
           '@'-prefixed identifier (Section 2.2). */
421
0
        Curl_strntolower(start, start, tlen);
422
0
        if(!strcmp(start, "method"))
423
0
          components[ncomp++] = SIG_METHOD;
424
0
        else if(!strcmp(start, "authority"))
425
0
          components[ncomp++] = SIG_AUTHORITY;
426
0
        else if(!strcmp(start, "path"))
427
0
          components[ncomp++] = SIG_PATH;
428
0
        else if(!strcmp(start, "query"))
429
0
          components[ncomp++] = SIG_QUERY;
430
0
        else {
431
0
          failf(data, "httpsig: unknown component '%s'", start);
432
0
          return CURLE_BAD_FUNCTION_ARGUMENT;
433
0
        }
434
0
      }
435
0
    }
436
0
    if(!ncomp) {
437
0
      failf(data, "httpsig: no signature components specified");
438
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
439
0
    }
440
0
    if(*p) {
441
0
      failf(data, "httpsig: too many signature components (max %u)",
442
0
            (unsigned int)HTTPSIG_MAX_COMPONENTS);
443
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
444
0
    }
445
446
    /* RFC 9421 Section 2: each covered component MUST occur only once */
447
0
    {
448
0
      size_t i, j;
449
450
0
      for(i = 0; i < ncomp; i++) {
451
0
        for(j = i + 1; j < ncomp; j++) {
452
0
          if(!strcmp(components[i], components[j])) {
453
0
            failf(data, "httpsig: duplicate signature component '%s'",
454
0
                  components[i]);
455
0
            return CURLE_BAD_FUNCTION_ARGUMENT;
456
0
          }
457
0
        }
458
0
      }
459
0
    }
460
0
  }
461
0
  else {
462
0
    components[ncomp++] = SIG_METHOD;
463
0
    components[ncomp++] = SIG_AUTHORITY;
464
0
    components[ncomp++] = SIG_PATH;
465
0
    if(query)
466
0
      components[ncomp++] = SIG_QUERY;
467
0
  }
468
469
0
  *ncomp_out = ncomp;
470
0
  return CURLE_OK;
471
0
}
472
473
static time_t httpsig_get_created(void)
474
0
{
475
0
#ifdef DEBUGBUILD
476
0
  char *force = getenv("CURL_FORCETIME");
477
0
  if(force && *force) {
478
0
    char *sigts = getenv("CURL_HTTPSIG_CREATED");
479
0
    if(sigts && *sigts) {
480
0
      const char *p = sigts;
481
0
      curl_off_t num;
482
0
      if(!curlx_str_number(&p, &num, CURL_OFF_T_MAX))
483
0
        return (time_t)num;
484
0
    }
485
0
    return 0;
486
0
  }
487
0
#endif
488
0
  return time(NULL);
489
0
}
490
491
static CURLcode httpsig_sign_base(struct Curl_easy *data,
492
                                  enum httpsig_alg alg,
493
                                  const void *keybuf, size_t keylen,
494
                                  const struct dynbuf *sig_base,
495
                                  uint8_t *raw_sig,
496
                                  size_t *raw_sig_len)
497
0
{
498
0
  CURLcode result;
499
500
0
  switch(alg) {
501
0
  case HTTPSIG_ALG_ED25519:
502
0
    result = Curl_ed25519_sign(keybuf, keylen,
503
0
                               curlx_dyn_ptr(sig_base),
504
0
                               curlx_dyn_len(sig_base),
505
0
                               raw_sig, raw_sig_len);
506
0
    break;
507
0
  case HTTPSIG_ALG_HMAC_SHA256:
508
0
    result = Curl_hmacit(&Curl_HMAC_SHA256, keybuf, keylen,
509
0
                         curlx_dyn_ptr(sig_base), curlx_dyn_len(sig_base),
510
0
                         raw_sig);
511
0
    if(!result)
512
0
      *raw_sig_len = CURL_SHA256_DIGEST_LEN;
513
0
    break;
514
0
  default:
515
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
516
0
    break;
517
0
  }
518
519
0
  if(result && result == CURLE_NOT_BUILT_IN) {
520
0
    failf(data, "httpsig: algorithm '%s' not supported by TLS backend",
521
0
          alg_to_str(alg));
522
0
  }
523
0
  return result;
524
0
}
525
526
CURLcode Curl_output_httpsig(struct Curl_easy *data)
527
0
{
528
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
529
0
  struct connectdata *conn = data->conn;
530
0
  const char *path;
531
0
  const char *query;
532
0
  Curl_HttpReq httpreq;
533
0
  const char *method = NULL;
534
0
  const char *hexkey = CURL_EASY_STR(data, STRING_HTTPSIG_KEY);
535
0
  const char *keyid = CURL_EASY_STR(data, STRING_HTTPSIG_KEYID);
536
0
  enum httpsig_alg alg;
537
0
  time_t created;
538
0
  struct dynbuf sig_params;
539
0
  struct dynbuf sig_base;
540
0
  struct dynbuf sig_hdr;
541
0
  struct dynbuf input_hdr;
542
0
  struct dynbuf authority_buf;
543
0
  const char *authority;
544
0
  const char *components[HTTPSIG_MAX_COMPONENTS];
545
0
  size_t ncomp = 0;
546
0
  uint8_t *keybuf = NULL;
547
0
  size_t keylen = 0;
548
0
  uint8_t raw_sig[HTTPSIG_MAX_RAW_SIG];
549
0
  size_t raw_sig_len = 0;
550
0
  char *auth_headers = NULL;
551
0
  char *hdrs_copy = NULL;
552
0
  struct dynbuf query_dyn;
553
554
0
  alg = id_to_alg(data->set.httpsig_algorithm);
555
0
  if(alg == HTTPSIG_ALG_UNKNOWN) {
556
0
    failf(data, "httpsig: CURLOPT_HTTPSIG_ALGORITHM is required");
557
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
558
0
  }
559
560
0
  if(!hexkey || !*hexkey) {
561
0
    failf(data, "httpsig: CURLOPT_HTTPSIG_KEY is required");
562
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
563
0
  }
564
0
  if(!keyid || !*keyid) {
565
0
    failf(data, "httpsig: CURLOPT_HTTPSIG_KEYID is required");
566
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
567
0
  }
568
569
0
  curlx_dyn_init(&sig_params, CURL_MAX_HTTP_HEADER);
570
0
  curlx_dyn_init(&sig_base, HTTPSIG_MAX_SIG_BASE);
571
0
  curlx_dyn_init(&sig_hdr, CURL_MAX_HTTP_HEADER);
572
0
  curlx_dyn_init(&input_hdr, CURL_MAX_HTTP_HEADER);
573
0
  curlx_dyn_init(&authority_buf, CURL_MAX_HTTP_HEADER);
574
0
  curlx_dyn_init(&query_dyn, CURL_MAX_HTTP_HEADER);
575
576
0
  if(Curl_checkheaders(data, STRCONST("Signature")) ||
577
0
     Curl_checkheaders(data, STRCONST("Signature-Input"))) {
578
    /* user provides their own Signature / Signature-Input headers, consider
579
       this done */
580
0
    goto done;
581
0
  }
582
583
0
  result = decode_hex_key(data, hexkey, &keybuf, &keylen);
584
0
  if(result)
585
0
    goto fail;
586
587
0
  if(alg == HTTPSIG_ALG_ED25519 && keylen != 32) {
588
0
    failf(data, "httpsig: ed25519 requires a 32-byte key (got %zu)", keylen);
589
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
590
0
    goto fail;
591
0
  }
592
593
0
  Curl_http_method(data, &method, &httpreq);
594
595
0
  path = data->state.up.path;
596
0
  if(!path || !*path)
597
0
    path = "/";
598
599
0
  query = data->state.up.query;
600
601
0
  result = httpsig_authority(data, conn, &authority_buf);
602
0
  if(result)
603
0
    goto fail;
604
0
  authority = curlx_dyn_ptr(&authority_buf);
605
606
  /* Build @query value: RFC 9421 Section 2.2.7 - always starts with "?" */
607
0
  if(query && *query)
608
0
    result = curlx_dyn_addf(&query_dyn, "?%s", query);
609
0
  else
610
0
    result = curlx_dyn_add(&query_dyn, "?");
611
0
  if(result)
612
0
    goto fail;
613
614
0
  result = parse_components(data, query, components, &ncomp, &hdrs_copy);
615
0
  if(result)
616
0
    goto fail;
617
618
0
  created = httpsig_get_created();
619
620
0
  result = build_sig_params(&sig_params, components, ncomp,
621
0
                            created, keyid, alg);
622
0
  if(result)
623
0
    goto fail;
624
625
0
  infof(data, "httpsig: Signature-Input params: %s",
626
0
        curlx_dyn_ptr(&sig_params));
627
628
0
  result = build_sig_base(&sig_base, components, ncomp,
629
0
                          method, authority, path,
630
0
                          curlx_dyn_ptr(&query_dyn),
631
0
                          data, curlx_dyn_ptr(&sig_params));
632
0
  if(result)
633
0
    goto fail;
634
635
0
  infof(data, "httpsig: Signature base: [%s]",
636
0
        curlx_dyn_ptr(&sig_base));
637
638
0
  result = httpsig_sign_base(data, alg, keybuf, keylen, &sig_base,
639
0
                             raw_sig, &raw_sig_len);
640
0
  if(result)
641
0
    goto fail;
642
643
0
  result = curlx_dyn_add(&sig_hdr, HTTPSIG_DEFAULT_LABEL "=");
644
0
  if(result)
645
0
    goto fail;
646
0
  result = sf_encode_byte_seq(raw_sig, raw_sig_len, &sig_hdr);
647
0
  if(result)
648
0
    goto fail;
649
650
0
  result = curlx_dyn_addf(&input_hdr, "%s=%s", HTTPSIG_DEFAULT_LABEL,
651
0
                          curlx_dyn_ptr(&sig_params));
652
0
  if(result)
653
0
    goto fail;
654
655
0
  auth_headers = curl_maprintf("Signature-Input: %s\r\n"
656
0
                               "Signature: %s\r\n",
657
0
                               curlx_dyn_ptr(&input_hdr),
658
0
                               curlx_dyn_ptr(&sig_hdr));
659
0
  if(!auth_headers)
660
0
    goto fail;
661
662
0
  infof(data, "httpsig: Signature-Input: %s", curlx_dyn_ptr(&input_hdr));
663
0
  infof(data, "httpsig: Signature: %s", curlx_dyn_ptr(&sig_hdr));
664
0
done:
665
0
  curlx_free(data->req.hd_auth);
666
0
  data->req.hd_auth = auth_headers;
667
0
  data->state.authhost.done = TRUE;
668
0
  result = CURLE_OK;
669
670
0
fail:
671
0
  if(keybuf) {
672
0
    memset(keybuf, 0, keylen);
673
0
    curlx_free(keybuf);
674
0
  }
675
0
  memset(raw_sig, 0, sizeof(raw_sig));
676
0
  curlx_free(hdrs_copy);
677
0
  curlx_dyn_free(&sig_params);
678
0
  curlx_dyn_free(&sig_base);
679
0
  curlx_dyn_free(&sig_hdr);
680
0
  curlx_dyn_free(&input_hdr);
681
0
  curlx_dyn_free(&authority_buf);
682
0
  curlx_dyn_free(&query_dyn);
683
0
  return result;
684
0
}
685
686
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_HTTPSIG */