34#include "../dlib/dlib.h"
61 for (i = 0; i < n; i++) {
74 const HstsData_t *node = v1;
75 const char *host = v2;
88 _MSG(
"HSTS: removed policy for %s\n", policy->host);
99 time_t ret, now = time(NULL);
100 struct tm *tm = gmtime(&now);
102 if (seconds_from_now > INT_MAX - tm->tm_sec)
103 tm->tm_sec = INT_MAX;
105 tm->tm_sec += seconds_from_now;
108 if (ret == (time_t) -1)
119 const HstsData_t *node1 = v1, *node2 = v2;
129 _MSG(
"HSTS: %s %s%s: until %s", (policy ?
"modify" :
"add"), host,
130 (subdomains ?
" and subdomains" :
""), ctime(&exp));
132 if (policy == NULL) {
133 policy =
dNew0(HstsData_t, 1);
137 policy->subdomains = subdomains;
138 policy->expires_at = exp;
154 len = strcspn(str,
"=;");
157 while (len && (str[len - 1] ==
' ' || str[len - 1] ==
'\t'))
170 if (**header_str ==
'=') {
177 len = strcspn(str,
";");
180 while (len && (str[len - 1] ==
' ' || str[len - 1] ==
'\t'))
195 *str += strcspn(*str,
";");
207 _MSG(
"HSTS header for %s: %s\n", host, header);
211 _MSG(
"But there were certificate warnings, so ignore it (!)\n");
226 if (isdigit(*value)) {
228 max_age = strtol(value, NULL, 10);
231 max_age_valid =
TRUE;
243 MSG(
"HSTS: header contains unknown attribute: '%s'\n", attr);
262 time_t now = time(NULL);
266 _MSG(
"HSTS: expired\n");
279 _MSG(
"HSTS: matched host %s\n", host);
286 const char *domain_str;
288 for (domain_str = strchr(host+1,
'.');
289 domain_str != NULL && *domain_str;
290 domain_str = strchr(domain_str+1,
'.')) {
293 if (policy && policy->subdomains) {
294 _MSG(
"HSTS: matched %s under %s subdomain rule\n", host,
312 const long ONE_YEAR = 60 * 60 * 24 * 365;
314 char *rc, *subdomains;
319 while (!feof(stream)) {
322 if (!rc && ferror(stream)) {
323 MSG_WARN(
"HSTS: Error while reading preload entries: %s\n",
331 if (line[0] !=
'\0' && line[0] !=
'#') {
335 while (line[i] !=
'\0' && !
dIsspace(line[i]))
336 domain[j++] = line[i++];
343 subdomains = line + i;
350 MSG_WARN(
"HSTS: format of line not recognized. Ignoring '%s'.\n",
360 struct tm future_tm = {7, 14, 3, 19, 0, 138, 0, 0, 0, 0, 0};
367 fclose(preload_file);
void dList_insert_sorted(Dlist *lp, void *data, dCompareFunc func)
Insert an element into a sorted list.
int dStrAsciiCasecmp(const char *s1, const char *s2)
char * dStrstrip(char *s)
Remove leading and trailing whitespace.
char * dStrdup(const char *s)
Dlist * dList_new(int size)
Create a new empty list.
int dList_length(Dlist *lp)
For completing the ADT.
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
char * dStrndup(const char *s, size_t sz)
void * dList_find_sorted(Dlist *lp, const void *data, dCompareFunc func)
Search a sorted list.
void dList_free(Dlist *lp)
Free a list (not its elements)
void dList_remove(Dlist *lp, const void *data)
#define dNew0(type, count)
static void Hsts_preload(FILE *stream)
bool_t a_Hsts_require_https(const char *host)
static time_t Hsts_future_time(long seconds_from_now)
Return the time_t for a future time.
static int Domain_node_cmp(const void *v1, const void *v2)
Compare function for searching domains.
static void Hsts_eat_value(const char **str)
Advance past any value.
static time_t hsts_latest_representable_time
void a_Hsts_freeall(void)
void a_Hsts_set(const char *header, const DilloUrl *url)
The reponse for this url had an HSTS header, so let's take action.
static void Hsts_set_policy(const char *host, long max_age, bool_t subdomains)
static char * Hsts_parse_value(const char **header_str)
Get the value in *header_str.
static void Hsts_free_policy(HstsData_t *p)
static HstsData_t * Hsts_get_policy(const char *host)
static void Hsts_remove_policy(HstsData_t *policy)
static char * Hsts_parse_attr(const char **header_str)
Read the next attribute.
static bool_t Hsts_expired(HstsData_t *policy)
static int Domain_node_domain_str_cmp(const void *v1, const void *v2)
Compare function for searching a domain node by domain string.
void a_Hsts_init(FILE *preload_file)
DilloPrefs prefs
Global Data.
bool_t http_strict_transport_security
int a_Tls_certificate_is_clean(const DilloUrl *url)
Did everything seem proper with the certificate – no warnings to click through?.