Debian Codegraph @

src/hsts.c

raw
1 /* HTTP Strict Transport Security (HSTS) support.
2 Copyright (C) 1996-2012, 2015, 2018-2024 Free Software Foundation,
3 Inc.
4
5 This file is part of GNU Wget.
6
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
30 #include "wget.h"
31
32 #ifdef HAVE_HSTS
33 #include "hsts.h"
34 #include "utils.h"
35 #include "host.h" /* for is_valid_ip_address() */
36 #include "hash.h"
37 #include "c-ctype.h"
38 #ifdef TESTING
39 #include "init.h" /* for ajoin_dir_file() */
40 #include "../tests/unit-tests.h"
41 #endif
42
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <stdlib.h>
46 #include <time.h>
47 #include <sys/stat.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <sys/file.h>
51
52 struct hsts_store {
53 struct hash_table *table;
54 time_t last_mtime;
55 bool changed;
56 };
57
58 struct hsts_kh {
59 char *host;
60 int explicit_port;
61 };
62
63 struct hsts_kh_info {
64 int64_t created;
65 int64_t max_age;
66 bool include_subdomains;
67 };
68
69 enum hsts_kh_match {
70 NO_MATCH,
71 SUPERDOMAIN_MATCH,
72 CONGRUENT_MATCH
73 };
74
75 #define hsts_is_host_name_valid(host) (!is_valid_ip_address (host))
76 #define hsts_is_scheme_valid(scheme) (scheme == SCHEME_HTTPS)
77 #define hsts_is_host_eligible(scheme, host) \
78 (hsts_is_scheme_valid (scheme) && hsts_is_host_name_valid (host))
79
80 #define DEFAULT_HTTP_PORT 80
81 #define DEFAULT_SSL_PORT 443
82 #define MAKE_EXPLICIT_PORT(s, p) (s == SCHEME_HTTPS ? (p == DEFAULT_SSL_PORT ? 0 : p) \
83 : (p == DEFAULT_HTTP_PORT ? 0 : p))
84
85 /* Hashing and comparison functions for the hash table */
86
87 #ifdef __clang__
88 __attribute__((no_sanitize("integer")))
89 #endif
90 static unsigned long
91 hsts_hash_func (const void *key)
92 {
93 struct hsts_kh *k = (struct hsts_kh *) key;
94 const char *h = NULL;
95 unsigned int hash = k->explicit_port;
96
97 for (h = k->host; *h; h++)
98 hash = hash * 31 + *h;
99
100 return hash;
101 }
102
103 static int
104 hsts_cmp_func (const void *h1, const void *h2)
105 {
106 struct hsts_kh *kh1 = (struct hsts_kh *) h1,
107 *kh2 = (struct hsts_kh *) h2;
108
109 return (!strcmp (kh1->host, kh2->host)) && (kh1->explicit_port == kh2->explicit_port);
110 }
111
112 /* Private functions. Feel free to make some of these public when needed. */
113
114 static struct hsts_kh_info *
115 hsts_find_entry (hsts_store_t store,
116 const char *host, int explicit_port,
117 enum hsts_kh_match *match_type,
118 struct hsts_kh *kh)
119 {
120 struct hsts_kh *k = NULL;
121 struct hsts_kh_info *khi = NULL;
122 enum hsts_kh_match match = NO_MATCH;
123 char *org_ptr = NULL;
124
125 k = (struct hsts_kh *) xnew (struct hsts_kh);
126 k->host = xstrdup_lower (host);
127 k->explicit_port = explicit_port;
128
129 /* save pointer so that we don't get into trouble later when freeing */
130 org_ptr = k->host;
131
132 khi = (struct hsts_kh_info *) hash_table_get (store->table, k);
133 if (khi)
134 {
135 match = CONGRUENT_MATCH;
136 goto end;
137 }
138
139 for (char *p = k->host; (p = strchr(p, '.')); )
140 {
141 k->host = ++p;
142 khi = (struct hsts_kh_info *) hash_table_get (store->table, k);
143 if (khi && khi->include_subdomains)
144 {
145 match = SUPERDOMAIN_MATCH;
146 break;
147 }
148 }
149
150 end:
151 /* restore pointer or we'll get a SEGV */
152 k->host = org_ptr;
153
154 /* copy parameters to previous frame */
155 if (match_type)
156 *match_type = match;
157 if (kh)
158 memcpy (kh, k, sizeof (struct hsts_kh));
159 else
160 xfree (k->host);
161
162 xfree (k);
163 return khi;
164 }
165
166 static bool
167 hsts_new_entry_internal (hsts_store_t store,
168 const char *host, int port,
169 int64_t created, int64_t max_age,
170 bool include_subdomains,
171 bool check_validity,
172 bool check_expired,
173 bool check_duplicates)
174 {
175 struct hsts_kh *kh = xnew (struct hsts_kh);
176 struct hsts_kh_info *khi = xnew0 (struct hsts_kh_info);
177 bool success = false;
178
179 kh->host = xstrdup_lower (host);
180 kh->explicit_port = MAKE_EXPLICIT_PORT (SCHEME_HTTPS, port);
181
182 khi->created = created;
183 khi->max_age = max_age;
184 khi->include_subdomains = include_subdomains;
185
186 /* Check validity */
187 if (check_validity && !hsts_is_host_name_valid (host))
188 goto bail;
189
190 if (check_expired && ((khi->created + khi->max_age) < khi->created))
191 goto bail;
192
193 if (check_duplicates && hash_table_contains (store->table, kh))
194 goto bail;
195
196 /* Now store the new entry */
197 hash_table_put (store->table, kh, khi);
198 success = true;
199
200 bail:
201 if (!success)
202 {
203 /* abort! */
204 xfree (kh->host);
205 xfree (kh);
206 xfree (khi);
207 }
208
209 return success;
210 }
211
212 /*
213 Creates a new entry, but does not check whether that entry already exists.
214 This function assumes that check has already been done by the caller.
215 */
216 static bool
217 hsts_add_entry (hsts_store_t store,
218 const char *host, int port,
219 int64_t max_age, bool include_subdomains)
220 {
221 int64_t t = (int64_t) time (NULL);
222
223 /* It might happen time() returned -1 */
224 return (t == -1) ?
225 false :
226 hsts_new_entry_internal (store, host, port, t, max_age, include_subdomains, false, true, false);
227 }
228
229 /* Creates a new entry, unless an identical one already exists. */
230 static bool
231 hsts_new_entry (hsts_store_t store,
232 const char *host, int port,
233 int64_t created, int64_t max_age,
234 bool include_subdomains)
235 {
236 return hsts_new_entry_internal (store, host, port, created, max_age, include_subdomains, true, true, true);
237 }
238
239 static void
240 hsts_remove_entry (hsts_store_t store, struct hsts_kh *kh)
241 {
242 hash_table_remove (store->table, kh);
243 }
244
245 static bool
246 hsts_store_merge (hsts_store_t store,
247 const char *host, int port,
248 int64_t created, int64_t max_age,
249 bool include_subdomains)
250 {
251 enum hsts_kh_match match_type = NO_MATCH;
252 struct hsts_kh_info *khi = NULL;
253 bool success = false;
254
255 port = MAKE_EXPLICIT_PORT (SCHEME_HTTPS, port);
256 khi = hsts_find_entry (store, host, port, &match_type, NULL);
257 if (khi && match_type == CONGRUENT_MATCH && created > khi->created)
258 {
259 /* update the entry with the new info */
260 khi->created = created;
261 khi->max_age = max_age;
262 khi->include_subdomains = include_subdomains;
263
264 success = true;
265 }
266 else if (!khi)
267 success = hsts_new_entry (store, host, port, created, max_age, include_subdomains);
268
269 return success;
270 }
271
272 static bool
273 hsts_read_database (hsts_store_t store, FILE *fp, bool merge_with_existing_entries)
274 {
275 char *line = NULL, *p;
276 size_t len = 0;
277 int items_read;
278 bool result = false;
279 bool (*func)(hsts_store_t, const char *, int, int64_t, int64_t, bool);
280
281 char host[256];
282 int port;
283 int64_t created, max_age;
284 int include_subdomains;
285
286 func = (merge_with_existing_entries ? hsts_store_merge : hsts_new_entry);
287
288 while (getline (&line, &len, fp) > 0)
289 {
290 for (p = line; c_isspace (*p); p++)
291 ;
292
293 if (*p == '#')
294 continue;
295
296 items_read = sscanf (p, "%255s %d %d %" SCNd64 " %" SCNd64,
297 host,
298 &port,
299 &include_subdomains,
300 &created,
301 &max_age);
302
303 if (items_read == 5)
304 func (store, host, port, created, max_age, !!include_subdomains);
305 }
306
307 xfree (line);
308 result = true;
309
310 return result;
311 }
312
313 static void
314 hsts_store_dump (hsts_store_t store, FILE *fp)
315 {
316 hash_table_iterator it;
317
318 /* Print preliminary comments. We don't care if any of these fail. */
319 fputs ("# HSTS 1.0 Known Hosts database for GNU Wget.\n", fp);
320 fputs ("# Edit at your own risk.\n", fp);
321 fputs ("# <hostname>\t<port>\t<incl. subdomains>\t<created>\t<max-age>\n", fp);
322
323 /* Now cycle through the HSTS store in memory and dump the entries */
324 for (hash_table_iterate (store->table, &it); hash_table_iter_next (&it);)
325 {
326 struct hsts_kh *kh = (struct hsts_kh *) it.key;
327 struct hsts_kh_info *khi = (struct hsts_kh_info *) it.value;
328
329 if (fprintf (fp, "%s\t%d\t%d\t%" PRId64 "\t%" PRId64 "\n",
330 kh->host, kh->explicit_port, khi->include_subdomains,
331 khi->created, khi->max_age) < 0)
332 {
333 logprintf (LOG_ALWAYS, "Could not write the HSTS database correctly.\n");
334 break;
335 }
336 }
337 }
338
339 /*
340 * Test:
341 * - The file is a regular file (ie. not a symlink), and
342 * - The file is not world-writable.
343 */
344 static bool
345 hsts_file_access_valid (const char *filename)
346 {
347 struct stat st;
348
349 if (stat (filename, &st) == -1)
350 return false;
351
352 return
353 #ifndef WINDOWS
354 /*
355 * The world-writable concept is a Unix-centric notion.
356 * We bypass this test on Windows.
357 */
358 !(st.st_mode & S_IWOTH) &&
359 #endif
360 S_ISREG (st.st_mode);
361 }
362
363 /* HSTS API */
364
365 /*
366 Changes the given URLs according to the HSTS policy.
367
368 If there's no host in the store that either congruently
369 or not, matches the given URL, no changes are made.
370 Returns true if the URL was changed, or false
371 if it was left intact.
372 */
373 bool
374 hsts_match (hsts_store_t store, struct url *u)
375 {
376 bool url_changed = false;
377 struct hsts_kh_info *entry = NULL;
378 struct hsts_kh *kh = xnew(struct hsts_kh);
379 enum hsts_kh_match match = NO_MATCH;
380 int port = MAKE_EXPLICIT_PORT (u->scheme, u->port);
381
382 /* avoid doing any computation if we're already in HTTPS */
383 if (!hsts_is_scheme_valid (u->scheme))
384 {
385 entry = hsts_find_entry (store, u->host, port, &match, kh);
386 if (entry)
387 {
388 if ((entry->created + entry->max_age) >= time(NULL))
389 {
390 if ((match == CONGRUENT_MATCH) ||
391 (match == SUPERDOMAIN_MATCH && entry->include_subdomains))
392 {
393 /* we found a matching Known HSTS Host
394 rewrite the URL */
395 u->scheme = SCHEME_HTTPS;
396 if (u->port == 80)
397 u->port = 443;
398 url_changed = true;
399 store->changed = true;
400 }
401 }
402 else
403 {
404 hsts_remove_entry (store, kh);
405 store->changed = true;
406 }
407 }
408 xfree (kh->host);
409 }
410
411 xfree (kh);
412
413 return url_changed;
414 }
415
416 /*
417 Add a new HSTS Known Host to the HSTS store.
418
419 If the host already exists, its information is updated,
420 or it'll be removed from the store if max_age is zero.
421
422 Bear in mind that the store is kept in memory, and will not
423 be written to disk until hsts_store_save is called.
424 This function regrows the in-memory HSTS store if necessary.
425
426 Currently, for a host to be taken into consideration,
427 two conditions have to be met:
428 - Connection must be through a secure channel (HTTPS).
429 - The host must not be an IPv4 or IPv6 address.
430
431 The RFC 6797 states that hosts that match IPv4 or IPv6 format
432 should be discarded at URI rewrite time. But we short-circuit
433 that check here, since there's no point in storing a host that
434 will never be matched.
435
436 Returns true if a new entry was actually created, or false
437 if an existing entry was updated/deleted. */
438 bool
439 hsts_store_entry (hsts_store_t store,
440 enum url_scheme scheme, const char *host, int port,
441 int64_t max_age, bool include_subdomains)
442 {
443 bool result = false;
444 enum hsts_kh_match match = NO_MATCH;
445 struct hsts_kh *kh = xnew(struct hsts_kh);
446 struct hsts_kh_info *entry = NULL;
447
448 if (hsts_is_host_eligible (scheme, host))
449 {
450 port = MAKE_EXPLICIT_PORT (scheme, port);
451 entry = hsts_find_entry (store, host, port, &match, kh);
452 if (entry && match == CONGRUENT_MATCH)
453 {
454 if (max_age == 0)
455 {
456 hsts_remove_entry (store, kh);
457 store->changed = true;
458 }
459 else if (max_age > 0)
460 {
461 /* RFC 6797 states that 'max_age' is a TTL relative to the
462 * reception of the STS header so we have to update the
463 * 'created' field too. The RFC also states that we have to
464 * update the entry each time we see HSTS header.
465 * See also Section 11.2. */
466 int64_t t = (int64_t) time (NULL);
467
468 if (t != -1 && t != entry->created)
469 {
470 entry->created = t;
471 entry->max_age = max_age;
472 entry->include_subdomains = include_subdomains;
473 store->changed = true;
474 }
475 }
476 /* we ignore negative max_ages */
477 }
478 else if (entry == NULL || match == SUPERDOMAIN_MATCH)
479 {
480 /* Either we didn't find a matching host,
481 or we got a superdomain match.
482 In either case, we create a new entry.
483
484 We have to perform an explicit check because it might
485 happen we got a non-existent entry with max_age == 0.
486 */
487 result = hsts_add_entry (store, host, port, max_age, include_subdomains);
488 if (result)
489 store->changed = true;
490 }
491 /* we ignore new entries with max_age == 0 */
492 xfree (kh->host);
493 }
494
495 xfree (kh);
496
497 return result;
498 }
499
500 hsts_store_t
501 hsts_store_open (const char *filename)
502 {
503 hsts_store_t store = NULL;
504 file_stats_t fstats;
505
506 store = xnew0 (struct hsts_store);
507 store->table = hash_table_new (0, hsts_hash_func, hsts_cmp_func);
508 store->last_mtime = 0;
509 store->changed = false;
510
511 if (file_exists_p (filename, &fstats))
512 {
513 if (hsts_file_access_valid (filename))
514 {
515 struct stat st;
516 FILE *fp = fopen_stat (filename, "r", &fstats);
517
518 if (!fp || !hsts_read_database (store, fp, false))
519 {
520 /* abort! */
521 hsts_store_close (store);
522 xfree (store);
523 if (fp)
524 fclose (fp);
525 goto out;
526 }
527
528 if (fstat (fileno (fp), &st) == 0)
529 store->last_mtime = st.st_mtime;
530
531 fclose (fp);
532 }
533 else
534 {
535 /*
536 * If we're not reading the HSTS database,
537 * then by all means act as if HSTS was disabled.
538 */
539 hsts_store_close (store);
540 xfree (store);
541
542 logprintf (LOG_NOTQUIET, "Will not apply HSTS. "
543 "The HSTS database must be a regular and non-world-writable file.\n");
544 }
545 }
546
547 out:
548 return store;
549 }
550
551 void
552 hsts_store_save (hsts_store_t store, const char *filename)
553 {
554 struct stat st;
555 FILE *fp = NULL;
556 int fd = 0;
557
558 if (filename && hash_table_count (store->table) > 0)
559 {
560 fp = fopen (filename, "a+");
561 if (fp)
562 {
563 /* Lock the file to avoid potential race conditions */
564 fd = fileno (fp);
565 flock (fd, LOCK_EX);
566
567 /* If the file has changed, merge the changes with our in-memory data
568 before dumping them to the file.
569 Otherwise we could potentially overwrite the data stored by other Wget processes.
570 */
571 if (store->last_mtime && stat (filename, &st) == 0 && st.st_mtime > store->last_mtime)
572 hsts_read_database (store, fp, true);
573
574 /* We've merged the latest changes so we can now truncate the file
575 and dump everything. */
576 fseek (fp, 0, SEEK_SET);
577 ftruncate (fd, 0);
578
579 /* now dump to the file */
580 hsts_store_dump (store, fp);
581
582 /* fclose is expected to unlock the file for us */
583 fclose (fp);
584 }
585 }
586 }
587
588 bool
589 hsts_store_has_changed (hsts_store_t store)
590 {
591 return (store ? store->changed : false);
592 }
593
594 void
595 hsts_store_close (hsts_store_t store)
596 {
597 hash_table_iterator it;
598
599 /* free all the host fields */
600 for (hash_table_iterate (store->table, &it); hash_table_iter_next (&it);)
601 {
602 xfree (((struct hsts_kh *) it.key)->host);
603 xfree (it.key);
604 xfree (it.value);
605 }
606
607 hash_table_destroy (store->table);
608 }
609
610 #ifdef TESTING
611 /* I know I'm really evil because I'm writing macros
612 that change control flow. But we're testing, who will tell? :D
613 */
614 #define TEST_URL_RW(s, u, p) do { \
615 if (test_url_rewrite (s, u, p, true)) \
616 return test_url_rewrite (s, u, p, true); \
617 } while (0)
618
619 #define TEST_URL_NORW(s, u, p) do { \
620 if (test_url_rewrite (s, u, p, false)) \
621 return test_url_rewrite (s, u, p, false); \
622 } while (0)
623
624 static char *
625 get_hsts_store_filename (void)
626 {
627 char *filename = NULL;
628 FILE *fp = NULL;
629
630 if (opt.homedir)
631 {
632 filename = ajoin_dir_file (opt.homedir, ".wget-hsts-test");
633 fp = fopen (filename, "w");
634 if (fp)
635 fclose (fp);
636 }
637
638 return filename;
639 }
640
641 static hsts_store_t
642 open_hsts_test_store (void)
643 {
644 char *filename = NULL;
645 hsts_store_t table = NULL;
646
647 filename = get_hsts_store_filename ();
648 table = hsts_store_open (filename);
649 xfree (filename);
650
651 return table;
652 }
653
654 static void
655 close_hsts_test_store (hsts_store_t store)
656 {
657 char *filename;
658
659 if ((filename = get_hsts_store_filename ()))
660 {
661 unlink (filename);
662 xfree (filename);
663 }
664 xfree (store);
665 }
666
667 static const char*
668 test_url_rewrite (hsts_store_t s, const char *url, int port, bool rewrite)
669 {
670 bool result;
671 struct url u;
672
673 u.host = xstrdup (url);
674 u.port = port;
675 u.scheme = SCHEME_HTTP;
676
677 result = hsts_match (s, &u);
678
679 if (rewrite)
680 {
681 if (port == 80)
682 mu_assert("URL: port should've been rewritten to 443", u.port == 443);
683 else
684 mu_assert("URL: port should've been left intact", u.port == port);
685 mu_assert("URL: scheme should've been rewritten to HTTPS", u.scheme == SCHEME_HTTPS);
686 mu_assert("result should've been true", result == true);
687 }
688 else
689 {
690 mu_assert("URL: port should've been left intact", u.port == port);
691 mu_assert("URL: scheme should've been left intact", u.scheme == SCHEME_HTTP);
692 mu_assert("result should've been false", result == false);
693 }
694
695 xfree (u.host);
696 return NULL;
697 }
698
699 const char *
700 test_hsts_new_entry (void)
701 {
702 enum hsts_kh_match match = NO_MATCH;
703 struct hsts_kh_info *khi;
704 hsts_store_t s;
705 bool created;
706
707 s = open_hsts_test_store ();
708 mu_assert("Could not open the HSTS store. This could be due to lack of memory.", s != NULL);
709
710 created = hsts_store_entry (s, SCHEME_HTTP, "www.foo.com", 80, 1234, true);
711 mu_assert("No entry should have been created.", created == false);
712
713 created = hsts_store_entry (s, SCHEME_HTTPS, "www.foo.com", 443, 1234, true);
714 mu_assert("A new entry should have been created", created == true);
715
716 khi = hsts_find_entry (s, "www.foo.com", MAKE_EXPLICIT_PORT (SCHEME_HTTPS, 443), &match, NULL);
717 mu_assert("Should've been a congruent match", match == CONGRUENT_MATCH);
718 mu_assert("No valid HSTS info was returned", khi != NULL);
719 mu_assert("Variable 'max_age' should be 1234", khi->max_age == 1234);
720 mu_assert("Variable 'include_subdomains' should be asserted", khi->include_subdomains == true);
721
722 khi = hsts_find_entry (s, "b.www.foo.com", MAKE_EXPLICIT_PORT (SCHEME_HTTPS, 443), &match, NULL);
723 mu_assert("Should've been a superdomain match", match == SUPERDOMAIN_MATCH);
724 mu_assert("No valid HSTS info was returned", khi != NULL);
725 mu_assert("Variable 'max_age' should be 1234", khi->max_age == 1234);
726 mu_assert("Variable 'include_subdomains' should be asserted", khi->include_subdomains == true);
727
728 khi = hsts_find_entry (s, "ww.foo.com", MAKE_EXPLICIT_PORT (SCHEME_HTTPS, 443), &match, NULL);
729 mu_assert("Should've been no match", match == NO_MATCH);
730
731 khi = hsts_find_entry (s, "foo.com", MAKE_EXPLICIT_PORT (SCHEME_HTTPS, 443), &match, NULL);
732 mu_assert("Should've been no match", match == NO_MATCH);
733
734 khi = hsts_find_entry (s, ".foo.com", MAKE_EXPLICIT_PORT (SCHEME_HTTPS, 443), &match, NULL);
735 mu_assert("Should've been no match", match == NO_MATCH);
736
737 khi = hsts_find_entry (s, ".www.foo.com", MAKE_EXPLICIT_PORT (SCHEME_HTTPS, 443), &match, NULL);
738 mu_assert("Should've been no match", match == SUPERDOMAIN_MATCH);
739
740 hsts_store_close (s);
741 close_hsts_test_store (s);
742
743 return NULL;
744 }
745
746 const char*
747 test_hsts_url_rewrite_superdomain (void)
748 {
749 hsts_store_t s;
750 bool created;
751
752 s = open_hsts_test_store ();
753 mu_assert("Could not open the HSTS store", s != NULL);
754
755 created = hsts_store_entry (s, SCHEME_HTTPS, "example.com", 443, 1234, true);
756 mu_assert("A new entry should've been created", created == true);
757
758 created = hsts_store_entry (s, SCHEME_HTTPS, "rep.example.com", 443, 1234, false);
759 mu_assert("A new entry should've been created", created == true);
760
761 TEST_URL_RW (s, "example.com", 80);
762 TEST_URL_RW (s, "rep.example.com", 80);
763 TEST_URL_RW (s, "rep.rep.example.com", 80);
764
765 hsts_store_close (s);
766 close_hsts_test_store (s);
767
768 return NULL;
769 }
770
771 const char*
772 test_hsts_url_rewrite_congruent (void)
773 {
774 hsts_store_t s;
775 bool created;
776
777 s = open_hsts_test_store ();
778 mu_assert("Could not open the HSTS store", s != NULL);
779
780 created = hsts_store_entry (s, SCHEME_HTTPS, "foo.com", 443, 1234, false);
781 mu_assert("A new entry should've been created", created == true);
782
783 TEST_URL_RW (s, "foo.com", 80);
784 TEST_URL_NORW (s, "www.foo.com", 80);
785
786 hsts_store_close (s);
787 close_hsts_test_store (s);
788
789 return NULL;
790 }
791
792 const char*
793 test_hsts_read_database (void)
794 {
795 hsts_store_t table;
796 char *file = NULL;
797 FILE *fp = NULL;
798 int64_t created = time(NULL) - 10;
799
800 if (opt.homedir)
801 {
802 file = ajoin_dir_file (opt.homedir, ".wget-hsts-testing");
803 fp = fopen (file, "w");
804 if (fp)
805 {
806 fputs ("# dummy comment\n", fp);
807 fprintf (fp, "foo.example.com\t0\t1\t%" PRId64 "\t123\n", created);
808 fprintf (fp, "bar.example.com\t0\t0\t%" PRId64 "\t456\n", created);
809 fprintf (fp, "test.example.com\t8080\t0\t%" PRId64 "\t789\n", created);
810 fclose (fp);
811
812 table = hsts_store_open (file);
813
814 TEST_URL_RW (table, "foo.example.com", 80);
815 TEST_URL_RW (table, "www.foo.example.com", 80);
816 TEST_URL_RW (table, "bar.example.com", 80);
817
818 TEST_URL_NORW(table, "www.bar.example.com", 80);
819
820 TEST_URL_RW (table, "test.example.com", 8080);
821
822 hsts_store_close (table);
823 close_hsts_test_store (table);
824 unlink (file);
825 }
826 xfree (file);
827 }
828
829 return NULL;
830 }
831 #endif /* TESTING */
832 #endif /* HAVE_HSTS */