Coverage Report

Created: 2026-12-01 06:33

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
8.55k
#define HTTPSIG_MAX_SIG_BASE   CURL_MAX_HTTP_HEADER
46
4.41k
#define HTTPSIG_MAX_COMPONENTS 16
47
#define HTTPSIG_MAX_RAW_SIG    CURL_ED25519_SIGLEN
48
13.2k
#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
7.06k
{
58
7.06k
  switch(alg) {
59
5.19k
  case HTTPSIG_ALG_ED25519:
60
5.19k
    return "ed25519";
61
1.86k
  case HTTPSIG_ALG_HMAC_SHA256:
62
1.86k
    return "hmac-sha256";
63
0
  default:
64
0
    break;
65
7.06k
  }
66
0
  return NULL;
67
7.06k
}
68
69
static enum httpsig_alg id_to_alg(uint8_t val)
70
9.21k
{
71
9.21k
  switch(val) {
72
5.78k
  case CURLHTTPSIG_ED25519:
73
5.78k
    return HTTPSIG_ALG_ED25519;
74
3.38k
  case CURLHTTPSIG_HMAC_SHA256:
75
3.38k
    return HTTPSIG_ALG_HMAC_SHA256;
76
49
  default:
77
49
    break;
78
9.21k
  }
79
49
  return HTTPSIG_ALG_UNKNOWN;
80
9.21k
}
81
82
static CURLcode decode_hex_key(struct Curl_easy *data,
83
                               const char *hexstr,
84
                               uint8_t **keyout, size_t *keylen)
85
8.55k
{
86
8.55k
  size_t len, i;
87
8.55k
  uint8_t *keybuf;
88
89
8.55k
  *keyout = NULL;
90
8.55k
  *keylen = 0;
91
92
8.55k
  len = strlen(hexstr);
93
12.0k
  while(len > 0 && ISNEWLINE(hexstr[len - 1]))
94
3.51k
    len--;
95
96
8.55k
  if(len == 0 || (len & 1) != 0) {
97
167
    failf(data, "httpsig: invalid hex key (length %zu)", len);
98
167
    return CURLE_BAD_FUNCTION_ARGUMENT;
99
167
  }
100
101
8.38k
  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
8.38k
  keybuf = curlx_malloc(len / 2);
107
8.38k
  if(!keybuf)
108
2
    return CURLE_OUT_OF_MEMORY;
109
110
209k
  for(i = 0; i < len; i += 2) {
111
201k
    if(!ISXDIGIT(hexstr[i]) || !ISXDIGIT(hexstr[i + 1])) {
112
306
      failf(data, "httpsig: invalid hex at position %zu ('%c%c')",
113
306
            i, hexstr[i], hexstr[i + 1]);
114
306
      curlx_free(keybuf);
115
306
      return CURLE_BAD_FUNCTION_ARGUMENT;
116
306
    }
117
200k
    keybuf[i / 2] = (uint8_t)((curlx_hexval(hexstr[i]) << 4) |
118
200k
                               curlx_hexval(hexstr[i + 1]));
119
200k
  }
120
121
8.08k
  *keyout = keybuf;
122
8.08k
  *keylen = len / 2;
123
8.08k
  return CURLE_OK;
124
8.38k
}
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
8.02k
{
132
8.02k
  const char *h = data->state.http_host;
133
134
8.02k
  if(h && curl_strnequal(h, "host:", 5)) {
135
7.76k
    const char *value = h + 5;
136
7.76k
    const char *end;
137
138
17.4k
    while(ISBLANK(*value))
139
9.67k
      value++;
140
7.76k
    if(*value) {
141
7.44k
      CURLcode result;
142
143
7.44k
      end = value;
144
365k
      while(*end && !ISNEWLINE(*end))
145
357k
        end++;
146
9.07k
      while(end > value && ISBLANK(end[-1]))
147
1.62k
        end--;
148
7.44k
      result = curlx_dyn_addn(authority_buf, value, (size_t)(end - value));
149
7.44k
      if(result)
150
10
        return result;
151
7.43k
      return CURLE_OK;
152
7.44k
    }
153
7.76k
  }
154
155
580
  {
156
580
    const char *hostname = conn->origin->hostname;
157
580
    uint16_t port = conn->origin->port;
158
159
580
    if((conn->origin->scheme->defport != port) && port)
160
150
      return curlx_dyn_addf(authority_buf, "%s:%u", hostname, port);
161
430
    return curlx_dyn_add(authority_buf, hostname);
162
580
  }
163
580
}
164
165
static CURLcode sf_append_quoted(struct dynbuf *buf, const char *str)
166
31.7k
{
167
31.7k
  CURLcode result = curlx_dyn_addn(buf, "\"", 1);
168
31.7k
  if(result)
169
0
    return result;
170
325k
  while(*str) {
171
294k
    if(ISCNTRL(*str))
172
359
      return CURLE_BAD_FUNCTION_ARGUMENT;
173
294k
    if(*str == '\\' || *str == '"') {
174
8.02k
      result = curlx_dyn_addn(buf, "\\", 1);
175
8.02k
      if(result)
176
1
        return result;
177
8.02k
    }
178
294k
    result = curlx_dyn_addn(buf, str, 1);
179
294k
    if(result)
180
10
      return result;
181
294k
    str++;
182
294k
  }
183
31.3k
  return curlx_dyn_addn(buf, "\"", 1);
184
31.7k
}
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
6.64k
{
190
6.64k
  CURLcode result;
191
6.64k
  size_t b64len;
192
6.64k
  char *b64;
193
194
6.64k
  result = curlx_base64_encode(raw, rawlen, &b64, &b64len);
195
6.64k
  if(result)
196
10
    return result;
197
198
6.63k
  result = curlx_dyn_addn(out, ":", 1);
199
6.63k
  if(!result)
200
6.63k
    result = curlx_dyn_addn(out, b64, b64len);
201
6.63k
  if(!result)
202
6.61k
    result = curlx_dyn_addn(out, ":", 1);
203
204
6.63k
  curlx_free(b64);
205
6.63k
  return result;
206
6.64k
}
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
7.47k
{
213
7.47k
  CURLcode result;
214
7.47k
  size_t i;
215
216
7.47k
  result = curlx_dyn_addn(params, "(", 1);
217
7.47k
  if(result)
218
14
    return result;
219
220
31.7k
  for(i = 0; i < count; i++) {
221
24.4k
    if(i > 0) {
222
16.9k
      result = curlx_dyn_addn(params, " ", 1);
223
16.9k
      if(result)
224
7
        return result;
225
16.9k
    }
226
24.4k
    result = sf_append_quoted(params, components[i]);
227
24.4k
    if(result)
228
97
      return result;
229
24.4k
  }
230
231
7.36k
  result = curlx_dyn_addn(params, ")", 1);
232
7.36k
  if(result)
233
10
    return result;
234
235
7.35k
  result = curlx_dyn_addf(params, ";created=%lld", (long long)created);
236
7.35k
  if(result)
237
0
    return result;
238
239
7.35k
  if(keyid && *keyid) {
240
7.35k
    result = curlx_dyn_add(params, ";keyid=");
241
7.35k
    if(result)
242
11
      return result;
243
7.34k
    result = sf_append_quoted(params, keyid);
244
7.34k
    if(result)
245
273
      return result;
246
7.34k
  }
247
248
7.06k
  result = curlx_dyn_addf(params, ";alg=\"%s\"", alg_to_str(alg));
249
7.06k
  return result;
250
7.35k
}
251
252
/* strings defined by RFC 9421 */
253
28.7k
#define SIG_METHOD    "@method"
254
22.0k
#define SIG_AUTHORITY "@authority"
255
15.4k
#define SIG_PATH      "@path"
256
3.68k
#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
22.8k
{
272
22.8k
  *out = NULL;
273
274
22.8k
  if(name[0] == '@') {
275
21.6k
    if(curl_strequal(name, SIG_METHOD))
276
6.62k
      *out = method;
277
15.0k
    else if(curl_strequal(name, SIG_AUTHORITY))
278
6.62k
      *out = authority;
279
8.38k
    else if(curl_strequal(name, SIG_PATH))
280
6.59k
      *out = path;
281
1.78k
    else if(curl_strequal(name, SIG_QUERY))
282
1.76k
      *out = query;
283
27
    else {
284
27
      failf(data, "httpsig: unsupported derived component '%s'", name);
285
27
      return CURLE_BAD_FUNCTION_ARGUMENT;
286
27
    }
287
21.6k
    if(!*out) {
288
0
      failf(data, "httpsig: derived component '%s' has no value", name);
289
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
290
0
    }
291
21.6k
    return CURLE_OK;
292
21.6k
  }
293
1.24k
  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
1.24k
    struct curl_slist *head;
297
1.24k
    size_t namelen = strlen(name);
298
1.24k
    bool found = FALSE;
299
300
1.24k
    curlx_dyn_reset(valbuf);
301
302
8.84k
    for(head = data->set.headers; head; head = head->next) {
303
7.60k
      if(curl_strnequal(head->data, name, namelen) &&
304
2.19k
         Curl_headersep(head->data[namelen])) {
305
1.89k
        const char *p = strchr(head->data, ':');
306
1.89k
        if(p) {
307
1.72k
          CURLcode result;
308
1.72k
          struct Curl_str content;
309
1.72k
          curlx_str_assign(&content, p + 1, strlen(p + 1));
310
1.72k
          curlx_str_trimblanks(&content);
311
1.72k
          if(found) {
312
782
            result = curlx_dyn_addn(valbuf, ", ", 2);
313
782
            if(result)
314
0
              return result;
315
782
          }
316
1.72k
          result = curlx_dyn_addn(valbuf, curlx_str(&content),
317
1.72k
                                  curlx_strlen(&content));
318
1.72k
          if(result)
319
6
            return result;
320
1.71k
          found = TRUE;
321
1.71k
        }
322
1.89k
      }
323
7.60k
    }
324
325
1.24k
    if(found) {
326
937
      *out = curlx_dyn_ptr(valbuf);
327
937
      return CURLE_OK;
328
937
    }
329
306
    failf(data, "httpsig: header '%s' not found in request", name);
330
306
    return CURLE_BAD_FUNCTION_ARGUMENT;
331
1.24k
  }
332
22.8k
}
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
7.06k
{
343
7.06k
  CURLcode result;
344
7.06k
  size_t i;
345
7.06k
  struct dynbuf hdrvalbuf;
346
347
7.06k
  curlx_dyn_init(&hdrvalbuf, CURL_MAX_HTTP_HEADER);
348
349
29.5k
  for(i = 0; i < count; i++) {
350
22.8k
    const char *val = NULL;
351
22.8k
    result = resolve_component(components[i], method,
352
22.8k
                               authority, path, query, data,
353
22.8k
                               &hdrvalbuf, &val);
354
22.8k
    if(result || !val) {
355
339
      failf(data, "httpsig: cannot resolve component '%s'", components[i]);
356
339
      curlx_dyn_free(&hdrvalbuf);
357
339
      return result ? result : CURLE_BAD_FUNCTION_ARGUMENT;
358
339
    }
359
360
22.5k
    result = curlx_dyn_addf(base, "\"%s\": %s\n", components[i], val);
361
22.5k
    if(result) {
362
46
      curlx_dyn_free(&hdrvalbuf);
363
46
      return result;
364
46
    }
365
22.5k
  }
366
367
6.67k
  curlx_dyn_free(&hdrvalbuf);
368
6.67k
  result = curlx_dyn_addf(base, "\"@signature-params\": %s", sig_params);
369
6.67k
  return result;
370
7.06k
}
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
8.00k
{
378
8.00k
  const char *hdrs = CURL_EASY_STR(data, STRING_HTTPSIG_HEADERS);
379
8.00k
  size_t ncomp = 0;
380
381
8.00k
  *hdrs_copy_out = NULL;
382
8.00k
  if(hdrs && *hdrs) {
383
1.35k
    char *p;
384
1.35k
    char *hdrs_copy = curlx_strdup(hdrs);
385
1.35k
    if(!hdrs_copy)
386
4
      return CURLE_OUT_OF_MEMORY;
387
1.34k
    *hdrs_copy_out = hdrs_copy;
388
1.34k
    p = hdrs_copy;
389
5.26k
    while(*p && ncomp < HTTPSIG_MAX_COMPONENTS) {
390
4.38k
      char *start;
391
4.38k
      size_t tlen = 0;
392
4.38k
      const char *p2 = p;
393
394
4.38k
      curlx_str_passblanks(&p2);
395
4.38k
      if(!*p2)
396
53
        break;
397
4.32k
      p = start = CURL_UNCONST(p2);
398
48.1k
      while(*p && !ISBLANK(*p)) {
399
43.8k
        if((*p == '\"') || (*p == '\\'))
400
60
          return CURLE_BAD_FUNCTION_ARGUMENT;
401
43.8k
        p++;
402
43.8k
        tlen++;
403
43.8k
      }
404
4.26k
      if(*p)
405
3.22k
        *p++ = '\0';
406
407
4.26k
      if(tlen && start[tlen - 1] == ':') {
408
        /* Header field: drop the trailing ':' marker. RFC 9421 field
409
           names are canonically lowercase (Section 2.1). */
410
2.44k
        start[--tlen] = '\0';
411
2.44k
        if(!tlen) {
412
34
          failf(data, "httpsig: empty header component name");
413
34
          return CURLE_BAD_FUNCTION_ARGUMENT;
414
34
        }
415
2.40k
        Curl_strntolower(start, start, tlen);
416
2.40k
        components[ncomp++] = start;
417
2.40k
      }
418
1.82k
      else {
419
        /* Derived component: map the bare name to its canonical RFC 9421
420
           '@'-prefixed identifier (Section 2.2). */
421
1.82k
        Curl_strntolower(start, start, tlen);
422
1.82k
        if(!strcmp(start, "method"))
423
408
          components[ncomp++] = SIG_METHOD;
424
1.41k
        else if(!strcmp(start, "authority"))
425
369
          components[ncomp++] = SIG_AUTHORITY;
426
1.04k
        else if(!strcmp(start, "path"))
427
368
          components[ncomp++] = SIG_PATH;
428
681
        else if(!strcmp(start, "query"))
429
366
          components[ncomp++] = SIG_QUERY;
430
315
        else {
431
315
          failf(data, "httpsig: unknown component '%s'", start);
432
315
          return CURLE_BAD_FUNCTION_ARGUMENT;
433
315
        }
434
1.82k
      }
435
4.26k
    }
436
940
    if(!ncomp) {
437
27
      failf(data, "httpsig: no signature components specified");
438
27
      return CURLE_BAD_FUNCTION_ARGUMENT;
439
27
    }
440
913
    if(*p) {
441
28
      failf(data, "httpsig: too many signature components (max %u)",
442
28
            (unsigned int)HTTPSIG_MAX_COMPONENTS);
443
28
      return CURLE_BAD_FUNCTION_ARGUMENT;
444
28
    }
445
446
    /* RFC 9421 Section 2: each covered component MUST occur only once */
447
885
    {
448
885
      size_t i, j;
449
450
4.19k
      for(i = 0; i < ncomp; i++) {
451
12.4k
        for(j = i + 1; j < ncomp; j++) {
452
9.17k
          if(!strcmp(components[i], components[j])) {
453
59
            failf(data, "httpsig: duplicate signature component '%s'",
454
59
                  components[i]);
455
59
            return CURLE_BAD_FUNCTION_ARGUMENT;
456
59
          }
457
9.17k
        }
458
3.36k
      }
459
885
    }
460
885
  }
461
6.65k
  else {
462
6.65k
    components[ncomp++] = SIG_METHOD;
463
6.65k
    components[ncomp++] = SIG_AUTHORITY;
464
6.65k
    components[ncomp++] = SIG_PATH;
465
6.65k
    if(query)
466
1.53k
      components[ncomp++] = SIG_QUERY;
467
6.65k
  }
468
469
7.47k
  *ncomp_out = ncomp;
470
7.47k
  return CURLE_OK;
471
8.00k
}
472
473
static time_t httpsig_get_created(void)
474
7.47k
{
475
7.47k
#ifdef DEBUGBUILD
476
7.47k
  char *force = getenv("CURL_FORCETIME");
477
7.47k
  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
7.47k
#endif
488
7.47k
  return time(NULL);
489
7.47k
}
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
6.66k
{
498
6.66k
  CURLcode result;
499
500
6.66k
  switch(alg) {
501
5.06k
  case HTTPSIG_ALG_ED25519:
502
5.06k
    result = Curl_ed25519_sign(keybuf, keylen,
503
5.06k
                               curlx_dyn_ptr(sig_base),
504
5.06k
                               curlx_dyn_len(sig_base),
505
5.06k
                               raw_sig, raw_sig_len);
506
5.06k
    break;
507
1.60k
  case HTTPSIG_ALG_HMAC_SHA256:
508
1.60k
    result = Curl_hmacit(&Curl_HMAC_SHA256, keybuf, keylen,
509
1.60k
                         curlx_dyn_ptr(sig_base), curlx_dyn_len(sig_base),
510
1.60k
                         raw_sig);
511
1.60k
    if(!result)
512
1.59k
      *raw_sig_len = CURL_SHA256_DIGEST_LEN;
513
1.60k
    break;
514
0
  default:
515
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
516
0
    break;
517
6.66k
  }
518
519
6.66k
  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
6.66k
  return result;
524
6.66k
}
525
526
CURLcode Curl_output_httpsig(struct Curl_easy *data)
527
9.21k
{
528
9.21k
  CURLcode result = CURLE_OUT_OF_MEMORY;
529
9.21k
  struct connectdata *conn = data->conn;
530
9.21k
  const char *path;
531
9.21k
  const char *query;
532
9.21k
  Curl_HttpReq httpreq;
533
9.21k
  const char *method = NULL;
534
9.21k
  const char *hexkey = CURL_EASY_STR(data, STRING_HTTPSIG_KEY);
535
9.21k
  const char *keyid = CURL_EASY_STR(data, STRING_HTTPSIG_KEYID);
536
9.21k
  enum httpsig_alg alg;
537
9.21k
  time_t created;
538
9.21k
  struct dynbuf sig_params;
539
9.21k
  struct dynbuf sig_base;
540
9.21k
  struct dynbuf sig_hdr;
541
9.21k
  struct dynbuf input_hdr;
542
9.21k
  struct dynbuf authority_buf;
543
9.21k
  const char *authority;
544
9.21k
  const char *components[HTTPSIG_MAX_COMPONENTS];
545
9.21k
  size_t ncomp = 0;
546
9.21k
  uint8_t *keybuf = NULL;
547
9.21k
  size_t keylen = 0;
548
9.21k
  uint8_t raw_sig[HTTPSIG_MAX_RAW_SIG];
549
9.21k
  size_t raw_sig_len = 0;
550
9.21k
  char *auth_headers = NULL;
551
9.21k
  char *hdrs_copy = NULL;
552
9.21k
  struct dynbuf query_dyn;
553
554
9.21k
  alg = id_to_alg(data->set.httpsig_algorithm);
555
9.21k
  if(alg == HTTPSIG_ALG_UNKNOWN) {
556
49
    failf(data, "httpsig: CURLOPT_HTTPSIG_ALGORITHM is required");
557
49
    return CURLE_BAD_FUNCTION_ARGUMENT;
558
49
  }
559
560
9.16k
  if(!hexkey || !*hexkey) {
561
529
    failf(data, "httpsig: CURLOPT_HTTPSIG_KEY is required");
562
529
    return CURLE_BAD_FUNCTION_ARGUMENT;
563
529
  }
564
8.63k
  if(!keyid || !*keyid) {
565
80
    failf(data, "httpsig: CURLOPT_HTTPSIG_KEYID is required");
566
80
    return CURLE_BAD_FUNCTION_ARGUMENT;
567
80
  }
568
569
8.55k
  curlx_dyn_init(&sig_params, CURL_MAX_HTTP_HEADER);
570
8.55k
  curlx_dyn_init(&sig_base, HTTPSIG_MAX_SIG_BASE);
571
8.55k
  curlx_dyn_init(&sig_hdr, CURL_MAX_HTTP_HEADER);
572
8.55k
  curlx_dyn_init(&input_hdr, CURL_MAX_HTTP_HEADER);
573
8.55k
  curlx_dyn_init(&authority_buf, CURL_MAX_HTTP_HEADER);
574
8.55k
  curlx_dyn_init(&query_dyn, CURL_MAX_HTTP_HEADER);
575
576
8.55k
  if(Curl_checkheaders(data, STRCONST("Signature")) ||
577
8.55k
     Curl_checkheaders(data, STRCONST("Signature-Input"))) {
578
    /* user provides their own Signature / Signature-Input headers, consider
579
       this done */
580
2
    goto done;
581
2
  }
582
583
8.55k
  result = decode_hex_key(data, hexkey, &keybuf, &keylen);
584
8.55k
  if(result)
585
475
    goto fail;
586
587
8.08k
  if(alg == HTTPSIG_ALG_ED25519 && keylen != 32) {
588
53
    failf(data, "httpsig: ed25519 requires a 32-byte key (got %zu)", keylen);
589
53
    result = CURLE_BAD_FUNCTION_ARGUMENT;
590
53
    goto fail;
591
53
  }
592
593
8.02k
  Curl_http_method(data, &method, &httpreq);
594
595
8.02k
  path = data->state.up.path;
596
8.02k
  if(!path || !*path)
597
0
    path = "/";
598
599
8.02k
  query = data->state.up.query;
600
601
8.02k
  result = httpsig_authority(data, conn, &authority_buf);
602
8.02k
  if(result)
603
10
    goto fail;
604
8.01k
  authority = curlx_dyn_ptr(&authority_buf);
605
606
  /* Build @query value: RFC 9421 Section 2.2.7 - always starts with "?" */
607
8.01k
  if(query && *query)
608
1.53k
    result = curlx_dyn_addf(&query_dyn, "?%s", query);
609
6.48k
  else
610
6.48k
    result = curlx_dyn_add(&query_dyn, "?");
611
8.01k
  if(result)
612
12
    goto fail;
613
614
8.00k
  result = parse_components(data, query, components, &ncomp, &hdrs_copy);
615
8.00k
  if(result)
616
527
    goto fail;
617
618
7.47k
  created = httpsig_get_created();
619
620
7.47k
  result = build_sig_params(&sig_params, components, ncomp,
621
7.47k
                            created, keyid, alg);
622
7.47k
  if(result)
623
417
    goto fail;
624
625
7.06k
  infof(data, "httpsig: Signature-Input params: %s",
626
7.06k
        curlx_dyn_ptr(&sig_params));
627
628
7.06k
  result = build_sig_base(&sig_base, components, ncomp,
629
7.06k
                          method, authority, path,
630
7.06k
                          curlx_dyn_ptr(&query_dyn),
631
7.06k
                          data, curlx_dyn_ptr(&sig_params));
632
7.06k
  if(result)
633
397
    goto fail;
634
635
6.66k
  infof(data, "httpsig: Signature base: [%s]",
636
6.66k
        curlx_dyn_ptr(&sig_base));
637
638
6.66k
  result = httpsig_sign_base(data, alg, keybuf, keylen, &sig_base,
639
6.66k
                             raw_sig, &raw_sig_len);
640
6.66k
  if(result)
641
7
    goto fail;
642
643
6.65k
  result = curlx_dyn_add(&sig_hdr, HTTPSIG_DEFAULT_LABEL "=");
644
6.65k
  if(result)
645
13
    goto fail;
646
6.64k
  result = sf_encode_byte_seq(raw_sig, raw_sig_len, &sig_hdr);
647
6.64k
  if(result)
648
26
    goto fail;
649
650
6.61k
  result = curlx_dyn_addf(&input_hdr, "%s=%s", HTTPSIG_DEFAULT_LABEL,
651
6.61k
                          curlx_dyn_ptr(&sig_params));
652
6.61k
  if(result)
653
15
    goto fail;
654
655
6.60k
  auth_headers = curl_maprintf("Signature-Input: %s\r\n"
656
6.60k
                               "Signature: %s\r\n",
657
6.60k
                               curlx_dyn_ptr(&input_hdr),
658
6.60k
                               curlx_dyn_ptr(&sig_hdr));
659
6.60k
  if(!auth_headers)
660
8
    goto fail;
661
662
6.59k
  infof(data, "httpsig: Signature-Input: %s", curlx_dyn_ptr(&input_hdr));
663
6.59k
  infof(data, "httpsig: Signature: %s", curlx_dyn_ptr(&sig_hdr));
664
6.59k
done:
665
6.59k
  curlx_free(data->req.hd_auth);
666
6.59k
  data->req.hd_auth = auth_headers;
667
6.59k
  data->state.authhost.done = TRUE;
668
6.59k
  result = CURLE_OK;
669
670
8.55k
fail:
671
8.55k
  if(keybuf) {
672
8.08k
    memset(keybuf, 0, keylen);
673
8.08k
    curlx_free(keybuf);
674
8.08k
  }
675
8.55k
  memset(raw_sig, 0, sizeof(raw_sig));
676
8.55k
  curlx_free(hdrs_copy);
677
8.55k
  curlx_dyn_free(&sig_params);
678
8.55k
  curlx_dyn_free(&sig_base);
679
8.55k
  curlx_dyn_free(&sig_hdr);
680
8.55k
  curlx_dyn_free(&input_hdr);
681
8.55k
  curlx_dyn_free(&authority_buf);
682
8.55k
  curlx_dyn_free(&query_dyn);
683
8.55k
  return result;
684
6.59k
}
685
686
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_HTTPSIG */