summaryrefslogtreecommitdiff
path: root/utils/http/strict-transport-security.c
blob: 9de610c7396323137200ea5d1b859648a6950f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright 2018 John-Mark Bell <jmb@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <limits.h>
#include <stdlib.h>

#include "utils/corestrings.h"
#include "utils/http.h"

#include "utils/http/generics.h"
#include "utils/http/primitives.h"

/**
 * Representation of a Strict-Transport-Security
 */
struct http_strict_transport_security {
	uint32_t max_age;		/**< Max age (delta seconds) */
	bool include_sub_domains;	/**< Whether subdomains are included */
};

/**
 * Representation of a directive
 */
typedef struct http_directive {
	http__item base;

	lwc_string *name;		/**< Parameter name */
	lwc_string *value;		/**< Parameter value (optional) */
} http_directive;


static void http_destroy_directive(http_directive *self)
{
	lwc_string_unref(self->name);
	if (self->value != NULL) {
		lwc_string_unref(self->value);
	}
	free(self);
}

static nserror http__parse_directive(const char **input,
		http_directive **result)
{
	const char *pos = *input;
	lwc_string *name;
	lwc_string *value = NULL;
	http_directive *directive;
	nserror error;

	/* token [ "=" ( token | quoted-string ) ] */

	error = http__parse_token(&pos, &name);
	if (error != NSERROR_OK)
		return error;

	http__skip_LWS(&pos);

	if (*pos == '=') {
		pos++;

		http__skip_LWS(&pos);

		if (*pos == '"')
			error = http__parse_quoted_string(&pos, &value);
		else
			error = http__parse_token(&pos, &value);

		if (error != NSERROR_OK) {
			lwc_string_unref(name);
			return error;
		}
	}

	directive = malloc(sizeof(*directive));
	if (directive == NULL) {
		if (value != NULL) {
			lwc_string_unref(value);
		}
		lwc_string_unref(name);
		return NSERROR_NOMEM;
	}

	HTTP__ITEM_INIT(directive, NULL, http_destroy_directive);
	directive->name = name;
	directive->value = value;

	*result = directive;
	*input = pos;

	return NSERROR_OK;
}

static void http_directive_list_destroy(http_directive *list)
{
	http__item_list_destroy(list);
}

static nserror http_directive_list_find_item(const http_directive *list,
		lwc_string *name, lwc_string **value)
{
	bool match;

	while (list != NULL) {
		if (lwc_string_caseless_isequal(name, list->name,
				&match) == lwc_error_ok && match)
			break;

		list = (http_directive *) list->base.next;
	}

	if (list == NULL)
		return NSERROR_NOT_FOUND;

	if (list->value != NULL) {
		*value = lwc_string_ref(list->value);
	} else {
		*value = NULL;
	}

	return NSERROR_OK;
}

static const http_directive *http_directive_list_iterate(
		const http_directive *cur,
		lwc_string **name, lwc_string **value)
{
	if (cur == NULL)
		return NULL;

	*name = lwc_string_ref(cur->name);
	if (cur->value != NULL) {
		*value = lwc_string_ref(cur->value);
	} else {
		*value = NULL;
	}

	return (http_directive *) cur->base.next;
}

static uint32_t count(const http_directive *list, lwc_string *key)
{
	uint32_t count = 0;
	bool match;

	while (list != NULL) {
		if (lwc_string_caseless_isequal(key, list->name,
				&match) == lwc_error_ok && match) {
			count++;
		}

		list = (http_directive *) list->base.next;
	}

	return count;
}

static bool check_duplicates(const http_directive *directives)
{
	bool result = true;
	const http_directive *key = directives;

	if (key == NULL) {
		/* No directives, so there can't be any duplicates */
		return true;
	}

	do {
		lwc_string *name = NULL, *value = NULL;

		key = http_directive_list_iterate(key, &name, &value);

		result &= (count(directives, name) == 1);

		lwc_string_unref(name);
		if (value != NULL) {
			lwc_string_unref(value);
		}
	} while (key != NULL);

	return result;
}

static nserror parse_max_age(lwc_string *value, uint32_t *result)
{
	const char *pos = lwc_string_data(value);
	const char *end = pos + lwc_string_length(value);
	uint32_t val = 0;

	/* 1*DIGIT */

	if (pos == end) {
		/* Blank value */
		return NSERROR_NOT_FOUND;
	}

	while (pos < end) {
		if ('0' <= *pos && *pos <= '9') {
			uint32_t nv = val * 10 + (*pos - '0');
			if (nv < val) {
				val = UINT_MAX;
			} else {
				val = nv;
			}
		} else {
			/* Non-digit */
			return NSERROR_NOT_FOUND;
		}

		pos++;
	}

	*result = val;

	return NSERROR_OK;
}

/* See strict-transport-security.h for documentation */
nserror http_parse_strict_transport_security(const char *header_value,
		http_strict_transport_security **result)
{
	const char *pos = header_value;
	http_strict_transport_security *sts;
	http_directive *first = NULL;
	http_directive *directives = NULL;
	lwc_string *max_age_str = NULL, *isd_str = NULL;
	uint32_t max_age;
	bool include_sub_domains = false;
	nserror error;

	/* directive *( ";" directive ) */

	http__skip_LWS(&pos);

	error = http__parse_directive(&pos, &first);
	if (error != NSERROR_OK) {
		return error;
	}

	http__skip_LWS(&pos);

	if (*pos == ';') {
		error = http__item_list_parse(&pos,
				http__parse_directive, first, &directives);
		if (error != NSERROR_OK) {
			if (directives != NULL) {
				http_directive_list_destroy(directives);
			}
			return error;
		}
	} else {
		directives = first;
	}

	/* Each directive must only appear once */
	if (check_duplicates(directives) == false) {
		http_directive_list_destroy(directives);
		return NSERROR_NOT_FOUND;
	}

	/* max-age is required */
	error = http_directive_list_find_item(directives,
			corestring_lwc_max_age, &max_age_str);
	if (error != NSERROR_OK || max_age_str == NULL) {
		http_directive_list_destroy(directives);
		return NSERROR_NOT_FOUND;
	}

	error = parse_max_age(max_age_str, &max_age);
	if (error != NSERROR_OK) {
		lwc_string_unref(max_age_str);
		http_directive_list_destroy(directives);
		return NSERROR_NOT_FOUND;
	}
	lwc_string_unref(max_age_str);

	/* includeSubDomains is optional and valueless */
	error = http_directive_list_find_item(directives,
			corestring_lwc_includesubdomains, &isd_str);
	if (error != NSERROR_OK && error != NSERROR_NOT_FOUND) {
		http_directive_list_destroy(directives);
		return NSERROR_NOT_FOUND;
	} else if (error == NSERROR_OK) {
		if (isd_str != NULL) {
			/* Present, but not valueless: invalid */
			lwc_string_unref(isd_str);
			http_directive_list_destroy(directives);
			return NSERROR_NOT_FOUND;
		}
		include_sub_domains = true;
	}
	http_directive_list_destroy(directives);

	sts = malloc(sizeof(*sts));
	if (sts == NULL) {
		return NSERROR_NOMEM;
	}

	sts->max_age = max_age;
	sts->include_sub_domains = include_sub_domains;

	*result = sts;

	return NSERROR_OK;
}

/* See strict-transport-security.h for documentation */
void http_strict_transport_security_destroy(
		http_strict_transport_security *victim)
{
	free(victim);
}

/* See strict-transport-security.h for documentation */
uint32_t http_strict_transport_security_max_age(
		http_strict_transport_security *sts)
{
	return sts->max_age;
}

/* See strict-transport-security.h for documentation */
bool http_strict_transport_security_include_subdomains(
		http_strict_transport_security *sts)
{
	return sts->include_sub_domains;
}