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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 "apr_dbd.h"
#include "apr_escape.h"
#include "apr_strings.h"
#include "httpd.h"
#include "http_log.h"
#include "http_main.h"
#include "ssl_ct_sct.h"
#include "ssl_ct_log_config.h"
#include "ssl_ct_util.h"
APLOG_USE_MODULE(ssl_ct);
int log_config_readable(apr_pool_t *p, const char *logconfig,
const char **msg)
{
const apr_dbd_driver_t *driver;
apr_dbd_t *handle;
apr_status_t rv;
apr_dbd_results_t *res;
int rc;
rv = apr_dbd_get_driver(p, "sqlite3", &driver);
if (rv != APR_SUCCESS) {
if (msg) {
*msg = "SQLite3 driver cannot be loaded";
}
return 0;
}
rv = apr_dbd_open(driver, p, logconfig, &handle);
if (rv != APR_SUCCESS) {
return 0;
}
/* is there a cheaper way? */
res = NULL;
rc = apr_dbd_select(driver, p, handle, &res,
"SELECT * FROM loginfo WHERE id = 0", 0);
apr_dbd_close(driver, handle);
if (rc != 0) {
return 0;
}
return 1;
}
static apr_status_t public_key_cleanup(void *data)
{
EVP_PKEY *pubkey = data;
EVP_PKEY_free(pubkey);
return APR_SUCCESS;
}
static apr_status_t read_public_key(apr_pool_t *p, const char *pubkey_fname,
EVP_PKEY **ppkey)
{
apr_status_t rv;
EVP_PKEY *pubkey;
FILE *pubkeyf;
*ppkey = NULL;
rv = ctutil_fopen(pubkey_fname, "r", &pubkeyf);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
APLOGNO(02751) "could not open log public key file %s",
pubkey_fname);
return rv;
}
pubkey = PEM_read_PUBKEY(pubkeyf, NULL, NULL, NULL);
if (!pubkey) {
fclose(pubkeyf);
rv = APR_EINVAL;
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02752) "PEM_read_PUBKEY() failed to process "
"public key file %s",
pubkey_fname);
return rv;
}
fclose(pubkeyf);
*ppkey = pubkey;
apr_pool_cleanup_register(p, (void *)pubkey, public_key_cleanup,
apr_pool_cleanup_null);
return APR_SUCCESS;
}
static void digest_public_key(EVP_PKEY *pubkey, unsigned char digest[LOG_ID_SIZE])
{
int len = i2d_PUBKEY(pubkey, NULL);
unsigned char *val = ap_malloc(len);
unsigned char *tmp = val;
ap_assert(LOG_ID_SIZE == SHA256_DIGEST_LENGTH);
i2d_PUBKEY(pubkey, &tmp);
#if OPENSSL_VERSION_NUMBER < 0x30000000
{
SHA256_CTX sha256ctx;
SHA256_Init(&sha256ctx);
SHA256_Update(&sha256ctx, (unsigned char *)val, len);
SHA256_Final(digest, &sha256ctx);
}
#else
{
EVP_MD_CTX *md_ctx;
unsigned int dlen = 0;
md_ctx = EVP_MD_CTX_create();
ap_assert(md_ctx != NULL);
ap_assert(EVP_DigestInit_ex(md_ctx, EVP_sha256(), NULL));
ap_assert(EVP_DigestUpdate(md_ctx, val, len));
ap_assert(EVP_DigestFinal_ex(md_ctx, digest, &dlen));
ap_assert(dlen == SHA256_DIGEST_LENGTH);
EVP_MD_CTX_destroy(md_ctx);
}
#endif
free(val);
}
static apr_status_t parse_log_url(apr_pool_t *p, const char *lu, apr_uri_t *puri)
{
apr_status_t rv;
apr_uri_t uri;
rv = apr_uri_parse(p, lu, &uri);
if (rv == APR_SUCCESS) {
if (!uri.scheme
|| !uri.hostname
|| !uri.path) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02753) "Error in log url \"%s\": URL can't be "
"parsed or is missing required elements", lu);
rv = APR_EINVAL;
}
if (strcmp(uri.scheme, "http")) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02754) "Error in log url \"%s\": Only scheme "
"\"http\" (instead of \"%s\") is currently "
"accepted",
lu, uri.scheme);
rv = APR_EINVAL;
}
if (strcmp(uri.path, "/")) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02755) "Error in log url \"%s\": Only path "
"\"/\" (instead of \"%s\") is currently accepted",
lu, uri.path);
rv = APR_EINVAL;
}
}
if (rv == APR_SUCCESS) {
*puri = uri;
}
return rv;
}
static apr_status_t parse_time_str(apr_pool_t *p, const char *time_str,
apr_time_t *time)
{
apr_int64_t val;
const char *end;
errno = 0;
val = apr_strtoi64(time_str, (char **)&end, 10);
if (errno || *end != '\0') {
return APR_EINVAL;
}
*time = apr_time_from_msec(val);
return APR_SUCCESS;
}
/* The log_config array should have already been allocated from p. */
apr_status_t save_log_config_entry(apr_array_header_t *log_config,
apr_pool_t *p,
const char *log_id,
const char *pubkey_fname,
const char *distrusted_str,
const char *min_time_str,
const char *max_time_str,
const char *url)
{
apr_size_t len;
apr_status_t rv;
apr_time_t min_time, max_time;
apr_uri_t uri;
char *computed_log_id = NULL, *log_id_bin = NULL;
ct_log_config *newconf, **pnewconf;
int distrusted;
EVP_PKEY *public_key;
if (!distrusted_str) {
distrusted = DISTRUSTED_UNSET;
}
else if (!strcmp(distrusted_str, "1")) {
distrusted = DISTRUSTED;
}
else if (!strcmp(distrusted_str, "0")) {
distrusted = TRUSTED;
}
else {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02756) "Trusted status \"%s\" not valid",
distrusted_str);
return APR_EINVAL;
}
if (log_id) {
rv = apr_unescape_hex(NULL, log_id, strlen(log_id), 0, &len);
if (rv != 0 || len != 32) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02757) "Log id \"%s\" not valid", log_id);
log_id_bin = apr_palloc(p, len);
apr_unescape_hex(log_id_bin, log_id, strlen(log_id), 0, NULL);
}
}
if (pubkey_fname) {
rv = read_public_key(p, pubkey_fname, &public_key);
if (rv != APR_SUCCESS) {
return rv;
}
}
else {
public_key = NULL;
}
if (min_time_str) {
rv = parse_time_str(p, min_time_str, &min_time);
if (rv) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02758) "Invalid min time \"%s\"", min_time_str);
return rv;
}
}
else {
min_time = 0;
}
if (max_time_str) {
rv = parse_time_str(p, max_time_str, &max_time);
if (rv) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02759) "Invalid max time \"%s\"", max_time_str);
return rv;
}
}
else {
max_time = 0;
}
if (url) {
rv = parse_log_url(p, url, &uri);
if (rv != APR_SUCCESS) {
return rv;
}
}
newconf = apr_pcalloc(p, sizeof(ct_log_config));
pnewconf = (ct_log_config **)apr_array_push(log_config);
*pnewconf = newconf;
newconf->distrusted = distrusted;
newconf->public_key = public_key;
if (newconf->public_key) {
computed_log_id = apr_palloc(p, LOG_ID_SIZE);
digest_public_key(newconf->public_key,
(unsigned char *)computed_log_id);
}
if (computed_log_id && log_id_bin) {
if (memcmp(computed_log_id, log_id_bin, LOG_ID_SIZE)) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
APLOGNO(02760) "Provided log id doesn't match digest "
"of public key");
return APR_EINVAL;
}
}
newconf->log_id = log_id_bin ? log_id_bin : computed_log_id;
newconf->min_valid_time = min_time;
newconf->max_valid_time = max_time;
newconf->url = url;
if (url) {
newconf->uri = uri;
newconf->uri_str = apr_uri_unparse(p, &uri, 0);
}
newconf->public_key_pem = pubkey_fname;
return APR_SUCCESS;
}
apr_status_t read_config_db(apr_pool_t *p, server_rec *s_main,
const char *log_config_fname,
apr_array_header_t *log_config)
{
apr_status_t rv;
const apr_dbd_driver_t *driver;
apr_dbd_t *handle;
apr_dbd_results_t *res;
apr_dbd_row_t *row;
int rc;
ap_assert(log_config);
rv = apr_dbd_get_driver(p, "sqlite3", &driver);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s_main,
APLOGNO(02761) "APR SQLite3 driver can't be loaded");
return rv;
}
rv = apr_dbd_open(driver, p, log_config_fname, &handle);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s_main,
APLOGNO(02762) "Can't open SQLite3 db %s",
log_config_fname);
return rv;
}
res = NULL;
rc = apr_dbd_select(driver, p, handle, &res,
"SELECT * FROM loginfo", 0);
if (rc != 0) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s_main,
APLOGNO(02763) "SELECT of loginfo records failed");
apr_dbd_close(driver, handle);
return APR_EINVAL;
}
rc = apr_dbd_num_tuples(driver, res);
switch (rc) {
case -1:
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s_main,
APLOGNO(02764) "Unexpected asynchronous result reading %s",
log_config_fname);
apr_dbd_close(driver, handle);
return APR_EINVAL;
case 0:
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s_main,
APLOGNO(02765) "Log configuration in %s is empty",
log_config_fname);
apr_dbd_close(driver, handle);
return APR_SUCCESS;
default:
/* quiet some lints */
break;
}
for (rv = apr_dbd_get_row(driver, p, res, &row, -1);
rv == APR_SUCCESS;
rv = apr_dbd_get_row(driver, p, res, &row, -1)) {
int cur = 0;
const char *id = apr_dbd_get_entry(driver, row, cur++);
const char *log_id = apr_dbd_get_entry(driver, row, cur++);
const char *public_key = apr_dbd_get_entry(driver, row, cur++);
const char *distrusted = apr_dbd_get_entry(driver, row, cur++);
const char *min_timestamp = apr_dbd_get_entry(driver, row, cur++);
const char *max_timestamp = apr_dbd_get_entry(driver, row, cur++);
const char *url = apr_dbd_get_entry(driver, row, cur++);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s_main, APLOGNO(03036)
"Log config: Record %s, log id %s, public key file %s,"
" distrusted %s, URL %s, time %s->%s",
id,
log_id ? log_id : "(unset)",
public_key ? public_key : "(unset)",
distrusted ? distrusted : "(unset, defaults to trusted)",
url ? url : "(unset)",
min_timestamp ? min_timestamp : "-INF",
max_timestamp ? max_timestamp : "+INF");
rv = save_log_config_entry(log_config, p, log_id,
public_key, distrusted,
min_timestamp, max_timestamp, url);
if (rv != APR_SUCCESS) {
apr_dbd_close(driver, handle);
return rv;
}
}
apr_dbd_close(driver, handle);
return APR_SUCCESS;
}
int log_valid_for_received_sct(const ct_log_config *l, apr_time_t to_check)
{
if (l->distrusted == DISTRUSTED) {
return 0;
}
if (l->max_valid_time && l->max_valid_time < to_check) {
return 0;
}
if (l->min_valid_time && l->min_valid_time < to_check) {
return 0;
}
return 1;
}
int log_valid_for_sent_sct(const ct_log_config *l)
{
/* The log could return us an SCT with an older timestamp which
* is within the trusted time interval for the log, but for
* simplicity let's just assume that if the log isn't still
* within a trusted interval we won't send SCTs from the log.
*/
return log_valid_for_received_sct(l, apr_time_now());
}
int log_configured_for_fetching_sct(const ct_log_config *l)
{
/* must have a url and a public key configured in order to obtain
* an SCT from the log
*/
return l->url != NULL && l->public_key != NULL;
}
|