-
Notifications
You must be signed in to change notification settings - Fork 18
/
ngx_sxg_utils.c
409 lines (373 loc) · 11.5 KB
/
ngx_sxg_utils.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
#include "ngx_sxg_utils.h"
#include <ctype.h>
#include <ngx_core.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#ifdef __USE_THIRD_PARTY__
#include "third_party/openssl/ocsp.h"
#include "third_party/openssl/pem.h"
#else
#include "openssl/ocsp.h"
#include "openssl/pem.h"
#endif
#include "libsxg.h"
size_t get_term_length(const char* str, size_t len, char delimiter,
const char quotes[2]) {
bool inside_quote = false;
for (size_t i = 0; i < len; ++i) {
if (!inside_quote) {
if (str[i] == quotes[0]) {
inside_quote = true;
} else if (str[i] == delimiter) {
return i;
}
} else if (inside_quote && str[i] == quotes[1]) {
inside_quote = false;
}
}
return len;
}
// Returns value pointer for spefified |key|. Skips prefix whitespace or tab.
// |key| is case insensitive.
static const char* extract_prefix_with_whitespace(const char* key,
const char* str,
size_t strlength) {
if (strlength == 0) {
return NULL;
}
const size_t keylength = strlen(key);
// Omit optional white space prefix.
while (strlength > 0 && (*str == ' ' || *str == '\t')) {
++str;
--strlength;
}
if (keylength <= strlength && strncasecmp(key, str, keylength) == 0) {
return str + keylength;
}
return NULL;
}
// Returns true if the first word of |str| equals |expected| after optional
// double-quotation is removed. Whitespace means end of value.
static bool first_word_match(const char* expected, const char* str,
size_t len) {
const size_t expected_length = strlen(expected);
if (len == 0 || (str[0] == '"' && len == 1)) {
return false;
}
if (str[0] == '"' && str[len - 1] == '"') {
str += 1;
len -= 2;
}
if (len < expected_length) {
return false;
}
return strncmp(str, expected, expected_length) == 0 &&
(len == expected_length || str[expected_length] == ' ');
}
static bool term_is_sxg(const char* str, size_t len) {
static const char kSxg[] = "application/signed-exchange";
static const char kValueKey[] = "v=";
static const char kValidVersion[] = "b3";
const char* const end = str + len;
const size_t media_range_tail = get_term_length(str, len, ';', "<>");
if (len < strlen(kSxg) || strncmp(kSxg, str, strlen(kSxg)) != 0 ||
len == media_range_tail) { // The version information is missing.
return false;
}
str += media_range_tail + 1;
while (str < end) {
const size_t tail = get_term_length(str, end - str, ';', "\"\"");
const char* const value_ptr =
extract_prefix_with_whitespace(kValueKey, str, tail);
if (value_ptr != NULL) {
return first_word_match(kValidVersion, value_ptr,
tail - (value_ptr - str));
}
str += tail + 1;
}
return false;
}
// Returns qvalue * 1000.
// Returns 0 if qvalue does not match the RFC7231 spec.
static int parse_qvalue(const char* str, size_t len) {
// https://tools.ietf.org/html/rfc7231#section-5.3.1 says that
// qvalue MUST be between 0.000 and 1.000.
while (len > 0 && (str[len - 1] == ' ' || str[len - 1] == '\t')) {
--len;
}
if (len == 1 && *str == '1') {
return 1000;
}
if (len == 0 || len > 5) { // Contains no digits or extra digits.
return 0;
}
char data[6] = {}; // 1 digit + '.' + 3 digits + null
memcpy(data, str, len);
if (strspn(data, "1234567890.") != len) { // Contains non-digit word.
return 0;
}
memset(data + len, '0', sizeof(data) - len - 1);
int whole = 0, part = 0;
if (sscanf(data, "%d.%d", &whole, &part) != 2) {
return 0;
}
int qvalue = whole * 1000 + part;
return qvalue > 0 && qvalue <= 1000 ? qvalue : 0;
}
// Find and return qvalue.
// e.g. text/html;q=0.8 -> 800
// image/png; -> 1000 (Omittion means MAX)
static int get_priority(const char* str, size_t len) {
for (;;) {
const size_t tail = get_term_length(str, len, ';', "\"\"");
const char* value_ptr = extract_prefix_with_whitespace("q=", str, tail);
if (value_ptr != NULL) {
return parse_qvalue(value_ptr, tail - (value_ptr - str));
}
str += tail;
len -= tail;
if (len == 0) {
break;
}
++str;
--len;
}
return 1000;
}
bool sxg_qvalue_is_1(const char* str, size_t len) {
for (const char* const end = str + len; str < end;) {
const size_t tail = get_term_length(str, len, ',', "<>");
if (term_is_sxg(str, tail) && get_priority(str, tail) == 1000) {
return true;
}
str += tail + 1;
len -= tail + 1;
}
return false;
}
// Truncates optional white spaces at head and tail.
static void strip(const char** str, size_t* len) {
while (*len > 0 && (**str == ' ' || **str == '\t')) {
++*str;
--*len;
}
while (*len > 0 && ((*str)[*len - 1] == ' ' || (*str)[*len - 1] == '\t')) {
--*len;
}
}
// Finds |desired| word exists in quoted and space-separated string.
// The |desired| word must not contain space.
// |desired| must be null terminated.
static bool quoted_string_match(const char* str, size_t len,
const char* desired) {
if (len < 2 || str[0] != '"' || str[len - 1] != '"') {
return false;
}
++str; // Skip '"'.
len -= 2; // Omit first and last '"'.
strip(&str, &len);
const size_t desired_length = strlen(desired);
const char* const end = str + len;
while (str < end) {
size_t tail = get_term_length(str, len, ' ', "<>");
if (tail == desired_length && memcmp(str, desired, desired_length) == 0) {
return true;
}
str += tail + 1;
len -= tail + 1;
}
return false;
}
bool param_is_preload(const char* param, size_t len) {
strip(¶m, &len);
static const char kRel[] = "rel";
static const char kPreload[] = "preload";
if (len < sizeof(kRel) - 1 ||
memcmp((char*)param, kRel, sizeof(kRel) - 1) != 0) {
return false;
}
param += sizeof(kRel) - 1;
len -= sizeof(kRel) - 1;
strip(¶m, &len);
if (len < sizeof(kPreload) || param[0] != '=') {
return false;
}
param++;
len--;
strip(¶m, &len);
return quoted_string_match(param, len, kPreload) ||
(len == sizeof(kPreload) - 1 && memcmp(param, kPreload, len) == 0);
}
bool param_is_as(const char* param, size_t len, const char** value,
size_t* value_len) {
strip(¶m, &len);
static const char kAs[] = "as";
*value = NULL;
*value_len = 0;
if (len < sizeof(kAs) - 1 ||
memcmp((char*)param, kAs, sizeof(kAs) - 1) != 0) {
return false;
}
param += sizeof(kAs) - 1;
len -= sizeof(kAs) - 1;
strip(¶m, &len);
if (len == 0 || param[0] != '=') {
return false;
}
param++;
len--;
strip(¶m, &len);
if (len > 0 && *param == '"') {
const char* end = strchr(param + 1, '"');
if (end == NULL) {
return false;
}
param += 1;
len = end - param;
strip(¶m, &len);
}
*value = param;
*value_len = len;
return len > 0;
}
EVP_PKEY* load_private_key(const char* filepath) {
FILE* const fp = fopen(filepath, "r");
if (!fp) {
return NULL;
}
EVP_PKEY* private_key = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
return private_key;
}
X509* load_x509_cert(const char* filepath) {
FILE* certfile = fopen(filepath, "r");
if (!certfile) {
return NULL;
}
char passwd = 0;
X509* cert = PEM_read_X509(certfile, 0, 0, &passwd);
fclose(certfile);
return cert;
}
ngx_sxg_cert_chain_t ngx_sxg_empty_cert_chain() {
static ngx_sxg_cert_chain_t empty;
empty.serialized_cert_chain = sxg_empty_buffer();
empty.certificate = NULL;
empty.issuer = NULL;
empty.ocsp = NULL;
return empty;
}
void ngx_sxg_cert_chain_release(ngx_sxg_cert_chain_t* target) {
sxg_buffer_release(&target->serialized_cert_chain);
if (target->certificate != NULL) {
X509_free(target->certificate);
target->certificate = NULL;
}
if (target->issuer != NULL) {
X509_free(target->issuer);
target->issuer = NULL;
}
}
bool load_cert_chain(const char* cert_path, ngx_sxg_cert_chain_t* target) {
FILE* certfile = fopen(cert_path, "r");
if (!certfile) {
return false;
}
char passwd = 0;
X509* cert = PEM_read_X509(certfile, 0, 0, &passwd);
X509* issuer = PEM_read_X509(certfile, 0, 0, &passwd);
fclose(certfile);
target->certificate = cert;
target->issuer = issuer;
return cert != NULL && issuer != NULL;
}
bool write_cert_chain(const ngx_sxg_cert_chain_t* cert, sxg_buffer_t* dst) {
sxg_buffer_t empty_sct_list = sxg_empty_buffer();
sxg_cert_chain_t chain = sxg_empty_cert_chain();
bool success =
sxg_cert_chain_append_cert(cert->certificate, cert->ocsp, &empty_sct_list,
&chain) &&
sxg_cert_chain_append_cert(cert->issuer, NULL, &empty_sct_list, &chain) &&
sxg_write_cert_chain_cbor(&chain, dst);
// Don't call sxg_cert_chain_release, as it would also free
// cert->certificate, cert->ocsp, and cert->issuer.
if (chain.certs != NULL) {
OPENSSL_free(chain.certs);
}
sxg_buffer_release(&empty_sct_list);
return success;
}
static bool asn1_generalizedtime_to_unixtime(const ASN1_GENERALIZEDTIME* ag,
time_t* out) {
if (ag->type != V_ASN1_GENERALIZEDTIME) {
return false;
}
struct tm t;
if (ASN1_TIME_to_tm(ag, &t) != 1) {
return false;
}
*out = mktime(&t);
return true;
}
static bool check_refresh_needed(ngx_sxg_cert_chain_t* target) {
if (target->ocsp == NULL) {
return true;
}
OCSP_BASICRESP* br = OCSP_response_get1_basic(target->ocsp);
const int resps = OCSP_resp_count(br);
for (int i = 0; i < resps; ++i) {
OCSP_SINGLERESP* leaf = OCSP_resp_get0(br, i);
ASN1_GENERALIZEDTIME* this_update;
ASN1_GENERALIZEDTIME* next_update;
int status = OCSP_single_get0_status(leaf, /*revocation_reason=*/NULL,
/*revocation_time=*/NULL, &this_update,
&next_update);
if (status == -1) {
return true;
}
time_t since;
time_t until;
if (!asn1_generalizedtime_to_unixtime(this_update, &since) ||
!asn1_generalizedtime_to_unixtime(next_update, &until)) {
return true;
}
if (since >= until) {
// Unexpected error in the OCSP response; refresh immediately.
return true;
}
time_t middle_lifespan = since + ((until - since) / 2);
const time_t now = time(NULL);
if (middle_lifespan < now) {
return true;
}
}
return false;
}
bool refresh_if_needed(ngx_sxg_cert_chain_t* target) {
if (!check_refresh_needed(target)) {
return false;
}
if (target->ocsp != NULL) {
OCSP_RESPONSE_free(target->ocsp);
}
sxg_buffer_resize(0, &target->serialized_cert_chain);
return sxg_fetch_ocsp_response(target->certificate, target->issuer,
&target->ocsp) &&
write_cert_chain(target, &target->serialized_cert_chain);
}