Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/hsts.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
 * The Strict-Transport-Security header is defined in RFC 6797:
26
 * https://datatracker.ietf.org/doc/html/rfc6797
27
 */
28
#include "curl_setup.h"
29
30
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HSTS)
31
#include "urldata.h"
32
#include "llist.h"
33
#include "hsts.h"
34
#include "curl_fopen.h"
35
#include "curl_get_line.h"
36
#include "parsedate.h"
37
#include "curl_share.h"
38
#include "curlx/strparse.h"
39
#include "curlx/strcopy.h"
40
41
6.36k
#define MAX_HSTS_LINE    4095
42
0
#define MAX_HSTS_HOSTLEN 2048
43
0
#define MAX_HSTS_DATELEN 17
44
0
#define UNLIMITED        "unlimited"
45
46
0
#define CAP_HSTS_MAX_AGE (2*365*24*3600) /* two years cap */
47
48
#if defined(DEBUGBUILD) || defined(UNITTESTS)
49
/* to play well with debug builds, we can *set* a fixed time this will
50
   return */
51
time_t deltatime; /* allow for "adjustments" for unit test purposes */
52
static time_t hsts_debugtime(void *unused)
53
0
{
54
0
  const char *timestr = getenv("CURL_TIME");
55
0
  (void)unused;
56
0
  if(timestr) {
57
0
    curl_off_t val;
58
0
    if(!curlx_str_number(&timestr, &val, TIME_T_MAX))
59
0
      val += (curl_off_t)deltatime;
60
0
    return (time_t)val;
61
0
  }
62
0
  return time(NULL);
63
0
}
64
#undef time
65
0
#define time(x) hsts_debugtime(x)
66
#endif
67
68
struct hsts *Curl_hsts_init(void)
69
6.54k
{
70
6.54k
  struct hsts *h = curlx_calloc(1, sizeof(struct hsts));
71
6.54k
  if(h) {
72
6.50k
    Curl_llist_init(&h->list, NULL);
73
6.50k
  }
74
6.54k
  return h;
75
6.54k
}
76
77
0
#define hsts_free(x) curlx_free(x)
78
79
void Curl_hsts_cleanup(struct hsts **hp)
80
13.0k
{
81
13.0k
  struct hsts *h = *hp;
82
13.0k
  if(h) {
83
6.50k
    struct Curl_llist_node *e;
84
6.50k
    struct Curl_llist_node *n;
85
6.50k
    for(e = Curl_llist_head(&h->list); e; e = n) {
86
0
      struct stsentry *sts = Curl_node_elem(e);
87
0
      n = Curl_node_next(e);
88
0
      hsts_free(sts);
89
0
    }
90
6.50k
    curlx_free(h->filename);
91
6.50k
    curlx_free(h);
92
6.50k
    *hp = NULL;
93
6.50k
  }
94
13.0k
}
95
96
/* append the new entry to the list after possibly removing an old entry
97
   first */
98
static void hsts_append(struct hsts *h, struct stsentry *sts)
99
0
{
100
0
  if(Curl_llist_count(&h->list) == MAX_HSTS_ENTRIES) {
101
    /* It is full. Remove the first entry in the list */
102
0
    struct Curl_llist_node *e = Curl_llist_head(&h->list);
103
0
    struct stsentry *oldsts = Curl_node_elem(e);
104
0
    Curl_node_remove(e);
105
0
    hsts_free(oldsts);
106
0
  }
107
0
  Curl_llist_append(&h->list, sts, &sts->node);
108
0
}
109
110
static CURLcode hsts_create(struct hsts *h,
111
                            const char *hostname,
112
                            size_t hlen,
113
                            bool subdomains,
114
                            curl_off_t expires)
115
0
{
116
0
  DEBUGASSERT(h);
117
0
  DEBUGASSERT(hostname);
118
119
0
  if(hlen && (hostname[hlen - 1] == '.'))
120
    /* strip off any trailing dot */
121
0
    --hlen;
122
0
  if(hlen) {
123
0
    struct stsentry *sts = curlx_calloc(1, sizeof(struct stsentry) + hlen);
124
0
    if(!sts)
125
0
      return CURLE_OUT_OF_MEMORY;
126
    /* the null-terminator is already there */
127
0
    memcpy(sts->host, hostname, hlen);
128
0
    sts->expires = expires;
129
0
    sts->includeSubDomains = subdomains;
130
0
    hsts_append(h, sts);
131
0
  }
132
0
  return CURLE_OK;
133
0
}
134
135
/*
136
 * Return the matching HSTS entry, or NULL if the given hostname is not
137
 * currently an HSTS one.
138
 *
139
 * The 'subdomain' argument tells the function if subdomain matching should be
140
 * attempted.
141
 *
142
 * @unittest 1660
143
 */
144
UNITTEST struct stsentry *hsts_check(struct hsts *h, const char *hostname,
145
                                     size_t hlen, bool subdomain);
146
147
/* Copy all live entries from src into dst. Used by curl_easy_duphandle so the
148
 * clone inherits entries learned at runtime. E.g. Strict-Transport-Security.
149
 */
150
CURLcode Curl_hsts_copy(struct hsts *dst, struct hsts *src)
151
0
{
152
0
  struct Curl_llist_node *e;
153
0
  time_t now = time(NULL);
154
155
0
  for(e = Curl_llist_head(&src->list); e; e = Curl_node_next(e)) {
156
0
    struct stsentry *sts = Curl_node_elem(e);
157
0
    if(sts->expires > now) {
158
0
      size_t hlen = strlen(sts->host);
159
0
      struct stsentry *existing = hsts_check(dst, sts->host, hlen, FALSE);
160
161
0
      if(existing) {
162
0
        if(sts->expires > existing->expires)
163
0
          existing->expires = sts->expires;
164
0
        if(sts->includeSubDomains)
165
0
          existing->includeSubDomains = TRUE;
166
0
      }
167
0
      else {
168
0
        CURLcode result = hsts_create(dst, sts->host, hlen,
169
0
                                      sts->includeSubDomains != 0,
170
0
                                      sts->expires);
171
0
        if(result)
172
0
          return result;
173
0
      }
174
0
    }
175
0
  }
176
0
  return CURLE_OK;
177
0
}
178
179
UNITTEST struct stsentry *hsts_check(struct hsts *h, const char *hostname,
180
                                     size_t hlen, bool subdomain)
181
0
{
182
0
  struct stsentry *bestsub = NULL;
183
0
  if(h) {
184
0
    time_t now = time(NULL);
185
0
    struct Curl_llist_node *e;
186
0
    struct Curl_llist_node *n;
187
0
    size_t blen = 0;
188
189
0
    if(!hlen)
190
0
      return NULL;
191
0
    if(hostname[hlen - 1] == '.')
192
      /* remove the trailing dot */
193
0
      --hlen;
194
195
0
    for(e = Curl_llist_head(&h->list); e; e = n) {
196
0
      struct stsentry *sts = Curl_node_elem(e);
197
0
      size_t ntail;
198
0
      n = Curl_node_next(e);
199
0
      if(sts->expires <= now) {
200
        /* remove expired entries */
201
0
        Curl_node_remove(&sts->node);
202
0
        hsts_free(sts);
203
0
        continue;
204
0
      }
205
0
      ntail = strlen(sts->host);
206
0
      if((subdomain && sts->includeSubDomains) && (ntail < hlen)) {
207
0
        size_t offs = hlen - ntail;
208
0
        if((hostname[offs - 1] == '.') &&
209
0
           curl_strnequal(&hostname[offs], sts->host, ntail) &&
210
0
           (ntail > blen)) {
211
          /* save the tail match with the longest tail */
212
0
          bestsub = sts;
213
0
          blen = ntail;
214
0
        }
215
0
      }
216
      /* avoid curl_strequal because the hostname is not null-terminated */
217
0
      if((hlen == ntail) && curl_strnequal(hostname, sts->host, hlen))
218
0
        return sts;
219
0
    }
220
0
  }
221
0
  return bestsub;
222
0
}
223
224
CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
225
                         const char *header)
226
0
{
227
0
  const char *p = header;
228
0
  curl_off_t expires = 0;
229
0
  bool gotma = FALSE;
230
0
  bool gotinc = FALSE;
231
0
  bool subdomains = FALSE;
232
0
  struct stsentry *sts;
233
0
  time_t now = time(NULL);
234
0
  size_t hlen = strlen(hostname);
235
236
0
  if(Curl_host_is_ipnum(hostname))
237
    /* "explicit IP address identification of all forms is excluded."
238
       / RFC 6797 */
239
0
    return CURLE_OK;
240
241
0
  do {
242
0
    struct Curl_str word;
243
0
    struct Curl_str val = { 0 };
244
0
    int rc;
245
0
    bool assign = FALSE;
246
247
0
    do {
248
0
      curlx_str_passblanks(&p);
249
0
      if(*p == ';')
250
0
        p++;
251
0
      else
252
0
        break;
253
0
    } while(1);
254
0
    if(curlx_str_cspn(&p, &word, ";=\r\n \t"))
255
0
      break;
256
257
0
    curlx_str_passblanks(&p);
258
0
    if(!curlx_str_single(&p, '=')) {
259
0
      assign = TRUE;
260
0
      curlx_str_passblanks(&p);
261
262
0
      if(*p == '\"') {
263
0
        if(curlx_str_quotedword(&p, &val, MAX_HSTS_LINE))
264
0
          break;
265
0
      }
266
0
      else {
267
0
        if(curlx_str_cspn(&p, &val, ", ;\r\n"))
268
0
          break;
269
0
      }
270
0
    }
271
272
0
    if(assign && curlx_str_casecompare2(&word, STRCONST("max-age"))) {
273
0
      const char *vp = curlx_str(&val);
274
0
      if(gotma)
275
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
276
0
      rc = curlx_str_number(&vp, &expires, CAP_HSTS_MAX_AGE);
277
0
      if(rc == STRE_OVERFLOW)
278
0
        expires = CAP_HSTS_MAX_AGE;
279
0
      else if(rc)
280
        /* invalid max-age */
281
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
282
283
0
      gotma = TRUE;
284
0
    }
285
0
    else if(curlx_str_casecompare2(&word, STRCONST("includesubdomains"))) {
286
0
      if(gotinc)
287
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
288
0
      subdomains = TRUE;
289
0
      gotinc = TRUE;
290
0
    }
291
0
  } while(*p);
292
293
0
  if(!gotma)
294
    /* max-age is mandatory */
295
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
296
297
0
  if(!expires) {
298
    /* remove the entry if present verbatim (without subdomain match) */
299
0
    sts = hsts_check(h, hostname, hlen, FALSE);
300
0
    if(sts) {
301
0
      Curl_node_remove(&sts->node);
302
0
      hsts_free(sts);
303
0
    }
304
0
    return CURLE_OK;
305
0
  }
306
307
0
  if(CURL_OFF_T_MAX - now < expires)
308
    /* would overflow, use maximum value */
309
0
    expires = CURL_OFF_T_MAX;
310
0
  else
311
0
    expires += now;
312
313
  /* check if it already exists */
314
0
  sts = hsts_check(h, hostname, hlen, FALSE);
315
0
  if(sts) {
316
    /* update these fields */
317
0
    sts->expires = expires;
318
0
    sts->includeSubDomains = subdomains;
319
0
  }
320
0
  else
321
0
    return hsts_create(h, hostname, hlen, subdomains, expires);
322
323
0
  return CURLE_OK;
324
0
}
325
326
/*
327
 * Send this HSTS entry to the write callback.
328
 */
329
static CURLcode hsts_push(struct Curl_easy *data,
330
                          struct curl_index *i,
331
                          struct stsentry *sts,
332
                          bool *stop)
333
0
{
334
0
  struct curl_hstsentry e;
335
0
  CURLSTScode sc;
336
0
  struct tm stamp;
337
0
  CURLcode result;
338
339
0
  e.name = (char *)sts->host;
340
0
  e.namelen = strlen(sts->host);
341
0
  e.includeSubDomains = sts->includeSubDomains;
342
343
0
  if(sts->expires != TIME_T_MAX) {
344
0
    result = curlx_gmtime((time_t)sts->expires, &stamp);
345
0
    if(result)
346
0
      return result;
347
348
0
    curl_msnprintf(e.expire, sizeof(e.expire), "%d%02d%02d %02d:%02d:%02d",
349
0
                   stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
350
0
                   stamp.tm_hour, stamp.tm_min, stamp.tm_sec);
351
0
  }
352
0
  else
353
0
    curlx_strcopy(e.expire, sizeof(e.expire), STRCONST(UNLIMITED));
354
355
0
  sc = data->set.hsts_write(data, &e, i, data->set.hsts_write_userp);
356
0
  *stop = (sc != CURLSTS_OK);
357
0
  return sc == CURLSTS_FAIL ? CURLE_BAD_FUNCTION_ARGUMENT : CURLE_OK;
358
0
}
359
360
/*
361
 * Write this single hsts entry to a single output line
362
 */
363
static void hsts_out(struct stsentry *sts, FILE *fp)
364
0
{
365
0
  struct tm stamp;
366
0
  if(sts->expires != TIME_T_MAX) {
367
0
    CURLcode result = curlx_gmtime((time_t)sts->expires, &stamp);
368
0
    if(!result)
369
      /* skip the entry if the date function fails */
370
0
      curl_mfprintf(fp, "%s%s \"%d%02d%02d %02d:%02d:%02d\"\n",
371
0
                    sts->includeSubDomains ? "." : "", sts->host,
372
0
                    stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
373
0
                    stamp.tm_hour, stamp.tm_min, stamp.tm_sec);
374
0
  }
375
0
  else
376
0
    curl_mfprintf(fp, "%s%s \"%s\"\n",
377
0
                  sts->includeSubDomains ? "." : "", sts->host, UNLIMITED);
378
0
}
379
380
/*
381
 * Curl_https_save() writes the HSTS cache to file and callback.
382
 */
383
CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h,
384
                        const char *file)
385
13.0k
{
386
13.0k
  struct Curl_llist_node *e;
387
13.0k
  struct Curl_llist_node *n;
388
13.0k
  CURLcode result = CURLE_OK;
389
13.0k
  FILE *out;
390
13.0k
  char *tempstore = NULL;
391
392
13.0k
  if(!h)
393
    /* no cache activated */
394
6.51k
    return CURLE_OK;
395
396
  /* if no new name is given, use the one we stored from the load */
397
6.50k
  if(!file && h->filename)
398
0
    file = h->filename;
399
400
6.50k
  if((h->flags & CURLHSTS_READONLYFILE) || !file || !file[0])
401
    /* marked as read-only, no file or zero length filename */
402
10
    goto skipsave;
403
404
6.49k
  result = Curl_fopen(data, file, &out, &tempstore);
405
6.49k
  if(!result) {
406
6.49k
    fputs("# Your HSTS cache. https://curl.se/docs/hsts.html\n"
407
6.49k
          "# This file was generated by libcurl! Edit at your own risk.\n",
408
6.49k
          out);
409
6.49k
    for(e = Curl_llist_head(&h->list); e; e = n) {
410
0
      struct stsentry *sts = Curl_node_elem(e);
411
0
      n = Curl_node_next(e);
412
0
      hsts_out(sts, out);
413
0
    }
414
6.49k
    if(fflush(out) || ferror(out))
415
0
      result = CURLE_WRITE_ERROR;
416
6.49k
    if(curlx_fclose(out))
417
0
      result = CURLE_WRITE_ERROR;
418
6.49k
    if(!result && tempstore && curlx_rename(tempstore, file))
419
0
      result = CURLE_WRITE_ERROR;
420
421
6.49k
    if(result && tempstore)
422
0
      unlink(tempstore);
423
6.49k
  }
424
6.49k
  curlx_free(tempstore);
425
6.50k
skipsave:
426
6.50k
  if(data->set.hsts_write) {
427
    /* if there is a write callback */
428
0
    struct curl_index i; /* count */
429
0
    i.total = Curl_llist_count(&h->list);
430
0
    i.index = 0;
431
0
    for(e = Curl_llist_head(&h->list); e; e = n) {
432
0
      struct stsentry *sts = Curl_node_elem(e);
433
0
      bool stop;
434
0
      n = Curl_node_next(e);
435
0
      result = hsts_push(data, &i, sts, &stop);
436
0
      if(result || stop)
437
0
        break;
438
0
      i.index++;
439
0
    }
440
0
  }
441
6.50k
  return result;
442
6.49k
}
443
444
/* only returns SERIOUS errors */
445
static CURLcode hsts_add_host_expire(struct hsts *h,
446
                                     const char *host, size_t hostlen,
447
                                     const char *expire, size_t explen,
448
                                     bool subdomain) /* default */
449
0
{
450
0
  CURLcode result = CURLE_OK;
451
0
  struct stsentry *e;
452
0
  char dbuf[MAX_HSTS_DATELEN + 1];
453
0
  time_t expires = 0;
454
0
  time_t now = time(NULL);
455
456
  /* The date parser works on a null-terminated string. */
457
0
  if(explen > MAX_HSTS_DATELEN)
458
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
459
0
  memcpy(dbuf, expire, explen);
460
0
  dbuf[explen] = 0;
461
462
0
  if(!strcmp(dbuf, UNLIMITED))
463
0
    expires = TIME_T_MAX;
464
0
  else
465
0
    Curl_getdate_capped(dbuf, &expires);
466
467
0
  if(expires <= now)
468
    /* this entry already expired */
469
0
    return CURLE_OK;
470
471
0
  if(host[0] == '.') {
472
0
    host++;
473
0
    hostlen--;
474
0
    subdomain = TRUE;
475
0
  }
476
0
  if(hostlen && (host[hostlen - 1] == '.'))
477
    /* strip off any trailing dot */
478
0
    hostlen--;
479
480
0
  if(hostlen) {
481
    /* only add it if not already present */
482
0
    e = hsts_check(h, host, hostlen, FALSE);
483
0
    if(!e)
484
0
      result = hsts_create(h, host, hostlen, subdomain, expires);
485
    /* 'host' is not necessarily null-terminated */
486
0
    else if((hostlen == strlen(e->host) &&
487
0
             curl_strnequal(host, e->host, hostlen))) {
488
      /* the same hostname, use the largest expire time and keep the strictest
489
         subdomain policy */
490
0
      if(expires > e->expires)
491
0
        e->expires = expires;
492
0
      if(subdomain)
493
0
        e->includeSubDomains = TRUE;
494
0
    }
495
0
  }
496
0
  return result;
497
0
}
498
499
/* only returns SERIOUS errors */
500
static CURLcode hsts_add(struct hsts *h, const char *line)
501
0
{
502
  /* Example lines:
503
     example.com "20191231 10:00:00"
504
     .example.net "20191231 10:00:00"
505
   */
506
0
  struct Curl_str host;
507
0
  struct Curl_str date;
508
509
0
  if(curlx_str_word(&line, &host, MAX_HSTS_HOSTLEN) ||
510
0
     curlx_str_singlespace(&line) ||
511
0
     curlx_str_quotedword(&line, &date, MAX_HSTS_DATELEN) ||
512
0
     curlx_str_newline(&line))
513
0
    ;
514
0
  else {
515
0
    return hsts_add_host_expire(h, curlx_str(&host), curlx_strlen(&host),
516
0
                                curlx_str(&date), curlx_strlen(&date),
517
0
                                FALSE);
518
0
  }
519
520
0
  return CURLE_OK;
521
0
}
522
523
/*
524
 * Load HSTS data from callback.
525
 *
526
 */
527
static CURLcode hsts_pull(struct Curl_easy *data, struct hsts *h)
528
6.37k
{
529
  /* if the HSTS read callback is set, use it */
530
6.37k
  if(data->set.hsts_read) {
531
0
    CURLSTScode sc;
532
0
    DEBUGASSERT(h);
533
0
    do {
534
0
      char buffer[MAX_HSTS_HOSTLEN + 1];
535
0
      struct curl_hstsentry e;
536
0
      e.name = buffer;
537
0
      e.namelen = sizeof(buffer) - 1;
538
0
      e.includeSubDomains = FALSE; /* default */
539
0
      e.expire[0] = 0;
540
0
      e.expire[MAX_HSTS_DATELEN] = 0;
541
0
      e.name[0] = 0; /* to make it clean */
542
0
      e.name[MAX_HSTS_HOSTLEN] = 0;
543
0
      sc = data->set.hsts_read(data, &e, data->set.hsts_read_userp);
544
0
      if(sc == CURLSTS_OK) {
545
0
        CURLcode result;
546
0
        const char *date = e.expire;
547
0
        if(!e.name[0] || e.expire[MAX_HSTS_DATELEN] ||
548
0
           e.name[MAX_HSTS_HOSTLEN])
549
          /* bail out if no name was stored or if a null-terminator is gone */
550
0
          return CURLE_BAD_FUNCTION_ARGUMENT;
551
0
        if(!date[0])
552
0
          date = UNLIMITED;
553
0
        result = hsts_add_host_expire(h, e.name, strlen(e.name),
554
0
                                      date, strlen(date),
555
                                      /* bitfield to bool conversion: */
556
0
                                      e.includeSubDomains ? TRUE : FALSE);
557
0
        if(result)
558
0
          return result;
559
0
      }
560
0
      else if(sc == CURLSTS_FAIL)
561
0
        return CURLE_ABORTED_BY_CALLBACK;
562
0
    } while(sc == CURLSTS_OK);
563
0
  }
564
6.37k
  return CURLE_OK;
565
6.37k
}
566
567
/*
568
 * Load the HSTS cache from the given file. The text based line-oriented file
569
 * format is documented here: https://curl.se/docs/hsts.html
570
 *
571
 * This function only returns error on major problems that prevent hsts
572
 * handling to work completely. It will ignore individual syntactical errors
573
 * etc.
574
 */
575
static CURLcode hsts_load(struct hsts *h, const char *file)
576
6.36k
{
577
6.36k
  CURLcode result = CURLE_OK;
578
6.36k
  FILE *fp;
579
580
  /* we need a private copy of the filename so that the hsts cache file
581
     name survives an easy handle reset */
582
6.36k
  curlx_free(h->filename);
583
6.36k
  h->filename = curlx_strdup(file);
584
6.36k
  if(!h->filename)
585
9
    return CURLE_OUT_OF_MEMORY;
586
587
6.36k
  fp = curlx_fopen(file, FOPEN_READTEXT);
588
6.36k
  if(fp) {
589
6.36k
    curlx_struct_stat stat;
590
6.36k
    if((curlx_fstat(fileno(fp), &stat) == -1) || !S_ISDIR(stat.st_mode)) {
591
6.36k
      struct dynbuf buf;
592
6.36k
      bool eof = FALSE;
593
6.36k
      curlx_dyn_init(&buf, MAX_HSTS_LINE);
594
6.36k
      do {
595
6.36k
        result = Curl_get_line(&buf, fp, &eof);
596
6.36k
        if(!result) {
597
6.35k
          const char *lineptr = curlx_dyn_ptr(&buf);
598
6.35k
          curlx_str_passblanks(&lineptr);
599
600
          /* Skip empty or commented lines, since we know the line will have
601
             a trailing newline from Curl_get_line we can treat length 1 as
602
             empty. */
603
6.35k
          if((*lineptr == '#') || strlen(lineptr) <= 1)
604
6.35k
            continue;
605
606
0
          hsts_add(h, lineptr);
607
0
        }
608
6.36k
      } while(!result && !eof);
609
6.36k
      curlx_dyn_free(&buf); /* free the line buffer */
610
6.36k
    }
611
6.36k
    curlx_fclose(fp);
612
6.36k
  }
613
6.36k
  return result;
614
6.36k
}
615
616
/*
617
 * Curl_hsts_loadfile() loads HSTS from file
618
 */
619
CURLcode Curl_hsts_loadfile(struct Curl_easy *data,
620
                            struct hsts *h, const char *file)
621
6.36k
{
622
6.36k
  DEBUGASSERT(h);
623
6.36k
  (void)data;
624
6.36k
  return hsts_load(h, file);
625
6.36k
}
626
627
/*
628
 * Curl_hsts_loadcb() loads HSTS from callback
629
 */
630
CURLcode Curl_hsts_loadcb(struct Curl_easy *data, struct hsts *h)
631
6.39k
{
632
6.39k
  if(h)
633
6.37k
    return hsts_pull(data, h);
634
27
  return CURLE_OK;
635
6.39k
}
636
637
CURLcode Curl_hsts_loadfiles(struct Curl_easy *data,
638
                             struct Curl_frzn_strs *files)
639
6.41k
{
640
6.41k
  CURLcode result = CURLE_OK;
641
6.41k
  size_t i;
642
6.41k
  if(Curl_frzn_strs_count(files)) {
643
6.36k
    Curl_share_lock(data, CURL_LOCK_DATA_HSTS, CURL_LOCK_ACCESS_SINGLE);
644
12.7k
    for(i = 0; i < Curl_frzn_strs_count(files); ++i) {
645
6.36k
      const char *filepath = Curl_frzn_strs_get_str(files, i);
646
6.36k
      if(filepath) {
647
6.36k
        result = Curl_hsts_loadfile(data, data->hsts, filepath);
648
6.36k
        if(result)
649
19
          break;
650
6.36k
      }
651
6.36k
    }
652
6.36k
    Curl_share_unlock(data, CURL_LOCK_DATA_HSTS);
653
6.36k
  }
654
6.41k
  return result;
655
6.41k
}
656
657
bool Curl_hsts_applies(struct hsts *h, const struct Curl_peer *dest)
658
0
{
659
  return !!hsts_check(h, dest->hostname, strlen(dest->hostname), TRUE);
660
0
}
661
662
#if defined(DEBUGBUILD) || defined(UNITTESTS)
663
#undef time
664
#endif
665
666
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_HSTS */