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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
/* $OpenBSD: ocsp.c,v 1.20 2020/09/03 14:50:40 tobhe Exp $ */
/*
* Copyright (c) 2014 Markus Friedl
* Copyright (c) 2005 Marco Pfatschbacher
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <openssl/pem.h>
#include <openssl/ocsp.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <event.h>
#include "iked.h"
struct iked_ocsp {
struct iked *ocsp_env; /* back pointer to env */
struct iked_sahdr ocsp_sh; /* ike sa */
uint8_t ocsp_type; /* auth type */
struct iked_socket *ocsp_sock; /* socket to ocsp responder */
BIO *ocsp_cbio; /* matching OpenSSL obj */
OCSP_CERTID *ocsp_id; /* ocsp-id for cert */
OCSP_REQUEST *ocsp_req; /* ocsp-request */
OCSP_REQ_CTX *ocsp_req_ctx; /* async ocsp-request */
};
struct ocsp_connect {
struct iked_sahdr oc_sh;
struct iked_socket oc_sock;
char *oc_path;
char *oc_url;
};
#define OCSP_TIMEOUT 30
/* priv */
void ocsp_connect_cb(int, short, void *);
int ocsp_connect_finish(struct iked *, int, struct ocsp_connect *);
/* unpriv */
void ocsp_free(struct iked_ocsp *);
void ocsp_callback(int, short, void *);
void ocsp_parse_response(struct iked_ocsp *, OCSP_RESPONSE *);
STACK_OF(X509) *ocsp_load_certs(const char *);
int ocsp_validate_finish(struct iked_ocsp *, int);
/* priv */
/* async connect to configure ocsp-responder */
int
ocsp_connect(struct iked *env, struct imsg *imsg)
{
struct ocsp_connect *oc = NULL;
struct iked_sahdr sh;
struct addrinfo hints, *res0 = NULL, *res;
struct timeval tv;
uint8_t *ptr;
size_t len;
char *host = NULL, *port = NULL, *path = NULL;
char *url, *freeme = NULL;
int use_ssl, fd = -1, ret = -1, error;
IMSG_SIZE_CHECK(imsg, &sh);
ptr = (uint8_t *)imsg->data;
len = IMSG_DATA_SIZE(imsg);
memcpy(&sh, ptr, sizeof(sh));
ptr += sizeof(sh);
len -= sizeof(sh);
if (len > 0)
url = freeme = get_string(ptr, len);
else if (env->sc_ocsp_url)
url = env->sc_ocsp_url;
else {
log_warnx("%s: no ocsp url", __func__);
goto done;
}
if (!OCSP_parse_url(url, &host, &port, &path, &use_ssl)) {
log_warnx("%s: error parsing OCSP-request-URL: %s", __func__,
url);
goto done;
}
if (use_ssl) {
log_warnx("%s: OCSP over SSL not supported: %s", __func__,
url);
goto done;
}
if ((fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
log_debug("%s: socket failed", __func__);
goto done;
}
if ((oc = calloc(1, sizeof(*oc))) == NULL) {
log_debug("%s: calloc failed", __func__);
goto done;
}
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(host, port, &hints, &res0);
if (error) {
log_warn("%s: getaddrinfo(%s, %s) failed",
SPI_SH(&sh, __func__), host, port);
goto done;
}
/* XXX just pick the first answer. we could loop instead */
for (res = res0; res; res = res->ai_next)
if (res->ai_family == AF_INET)
break;
if (res == NULL) {
log_debug("%s: no addr to connect to for %s:%s",
__func__, host, port);
goto done;
}
oc->oc_sock.sock_fd = fd;
oc->oc_sock.sock_env = env;
oc->oc_sh = sh;
oc->oc_path = path;
oc->oc_url = strdup(url);
path = NULL;
log_debug("%s: connect(%s, %s)", __func__, host, port);
if (connect(fd, res->ai_addr, res->ai_addrlen) == -1) {
/* register callback for ansync connect */
if (errno == EINPROGRESS) {
tv.tv_sec = OCSP_TIMEOUT;
tv.tv_usec = 0;
event_set(&oc->oc_sock.sock_ev, fd, EV_WRITE,
ocsp_connect_cb, oc);
event_add(&oc->oc_sock.sock_ev, &tv);
ret = 0;
} else
log_warn("%s: connect(%s, %s)",
SPI_SH(&oc->oc_sh, __func__), host, port);
} else {
ocsp_connect_finish(env, fd, oc);
ret = 0;
}
done:
if (res0)
freeaddrinfo(res0);
free(freeme);
free(host);
free(port);
free(path);
if (ret == -1) {
ocsp_connect_finish(env, -1, oc);
if (fd >= 0)
close(fd);
}
return (ret);
}
/* callback triggered if connection to ocsp-responder completes/fails */
void
ocsp_connect_cb(int fd, short event, void *arg)
{
struct ocsp_connect *oc = arg;
int error, send_fd = -1;
socklen_t len;
if (event == EV_TIMEOUT) {
log_info("%s: timeout, giving up",
SPI_SH(&oc->oc_sh, __func__));
goto done;
}
len = sizeof(error);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1) {
log_warn("%s: getsockopt SOL_SOCKET SO_ERROR", __func__);
} else if (error) {
log_warnx("%s: error while connecting: %s",
SPI_SH(&oc->oc_sh, __func__), strerror(error));
} else {
send_fd = fd;
}
done:
ocsp_connect_finish(oc->oc_sock.sock_env, send_fd, oc);
/* if we did not send the fd, we need to close it ourself */
if (send_fd == -1)
close(fd);
}
/* send FD+path or error back to CA process */
int
ocsp_connect_finish(struct iked *env, int fd, struct ocsp_connect *oc)
{
struct iovec iov[2];
int iovcnt = 0, ret;
iov[iovcnt].iov_base = &oc->oc_sh;
iov[iovcnt].iov_len = sizeof(oc->oc_sh);
iovcnt++;
if (oc && fd >= 0) {
/* the imsg framework will close the FD after send */
iov[iovcnt].iov_base = oc->oc_path;
iov[iovcnt].iov_len = strlen(oc->oc_path);
iovcnt++;
ret = proc_composev_imsg(&env->sc_ps, PROC_CERT, -1,
IMSG_OCSP_FD, -1, fd, iov, iovcnt);
} else {
if (oc)
log_info("%s: connect failed for %s",
SPI_SH(&oc->oc_sh, __func__),
oc->oc_url ? oc->oc_url : "unknown");
else
log_info("%s: connect failed", __func__);
ret = proc_composev_imsg(&env->sc_ps, PROC_CERT, -1,
IMSG_OCSP_FD, -1, -1, iov, iovcnt);
if (fd >= 0)
close(fd);
}
if (oc) {
free(oc->oc_url);
free(oc->oc_path);
free(oc);
}
return (ret);
}
/* unpriv */
/* validate the certifcate stored in 'data' by querying the ocsp-responder */
int
ocsp_validate_cert(struct iked *env, void *data, size_t len,
struct iked_sahdr sh, u_int8_t type, X509 *issuer)
{
struct iovec iov[2];
STACK_OF(OPENSSL_STRING) *aia; /* Authority Information Access */
struct iked_ocsp_entry *ioe;
struct iked_ocsp *ocsp;
OCSP_CERTID *id = NULL;
char *url;
BIO *rawcert = NULL;
X509 *cert = NULL;
int ret, iovcnt = 0;
if (issuer == NULL)
return (-1);
if ((ioe = calloc(1, sizeof(*ioe))) == NULL)
return (-1);
if ((ocsp = calloc(1, sizeof(*ocsp))) == NULL) {
free(ioe);
return (-1);
}
ocsp->ocsp_env = env;
ocsp->ocsp_sh = sh;
ocsp->ocsp_type = type;
if ((rawcert = BIO_new_mem_buf(data, len)) == NULL ||
(cert = d2i_X509_bio(rawcert, NULL)) == NULL ||
(ocsp->ocsp_cbio = BIO_new(BIO_s_socket())) == NULL ||
(ocsp->ocsp_req = OCSP_REQUEST_new()) == NULL ||
(id = OCSP_cert_to_id(NULL, cert, issuer)) == NULL ||
!OCSP_request_add0_id(ocsp->ocsp_req, id))
goto err;
/* id is owned by and freed together with ocsp_req */
ocsp->ocsp_id = id;
BIO_free(rawcert);
X509_free(cert);
ioe->ioe_ocsp = ocsp;
TAILQ_INSERT_TAIL(&env->sc_ocsp, ioe, ioe_entry);
/* pass SA header */
iov[iovcnt].iov_base = &ocsp->ocsp_sh;
iov[iovcnt].iov_len = sizeof(ocsp->ocsp_sh);
iovcnt++;
/* pass optional ocsp-url from issuer */
if ((aia = X509_get1_ocsp(issuer)) != NULL) {
url = sk_OPENSSL_STRING_value(aia, 0);
log_debug("%s: aia %s", __func__, url);
iov[iovcnt].iov_base = url;
iov[iovcnt].iov_len = strlen(url);
iovcnt++;
}
/* request connection to ocsp-responder */
ret = proc_composev(&env->sc_ps, PROC_PARENT, IMSG_OCSP_FD,
iov, iovcnt);
if (aia)
X509_email_free(aia); /* free stack of openssl strings */
return (ret);
err:
ca_sslerror(__func__);
free(ioe);
if (rawcert != NULL)
BIO_free(rawcert);
if (cert != NULL)
X509_free(cert);
if (id != NULL)
OCSP_CERTID_free(id);
ocsp_validate_finish(ocsp, 0); /* failed */
return (-1);
}
/* free ocsp query context */
void
ocsp_free(struct iked_ocsp *ocsp)
{
if (ocsp != NULL) {
if (ocsp->ocsp_sock != NULL) {
close(ocsp->ocsp_sock->sock_fd);
free(ocsp->ocsp_sock);
}
if (ocsp->ocsp_cbio != NULL)
BIO_free_all(ocsp->ocsp_cbio);
if (ocsp->ocsp_req_ctx != NULL)
OCSP_REQ_CTX_free(ocsp->ocsp_req_ctx);
if (ocsp->ocsp_req != NULL)
OCSP_REQUEST_free(ocsp->ocsp_req);
free(ocsp);
}
}
/* we got a connection to the ocsp responder */
int
ocsp_receive_fd(struct iked *env, struct imsg *imsg)
{
struct iked_ocsp_entry *ioe = NULL;
struct iked_ocsp *ocsp = NULL, *ocsp_tmp;
struct iked_socket *sock;
struct iked_sahdr sh;
struct timeval tv;
uint8_t *ptr;
char *path = NULL;
size_t len;
int ret = -1;
log_debug("%s: received socket fd %d", __func__, imsg->fd);
IMSG_SIZE_CHECK(imsg, &sh);
ptr = (uint8_t *)imsg->data;
len = IMSG_DATA_SIZE(imsg);
memcpy(&sh, ptr, sizeof(sh));
ptr += sizeof(sh);
len -= sizeof(sh);
TAILQ_FOREACH(ioe, &env->sc_ocsp, ioe_entry) {
ocsp_tmp = ioe->ioe_ocsp;
if (memcmp(&ocsp_tmp->ocsp_sh, &sh, sizeof(sh)) == 0)
break;
}
if (ioe == NULL) {
log_debug("%s: no pending request found", __func__);
if (imsg->fd != -1)
close(imsg->fd);
return (-1);
}
TAILQ_REMOVE(&env->sc_ocsp, ioe, ioe_entry);
ocsp = ioe->ioe_ocsp;
free(ioe);
if (imsg->fd == -1)
goto done;
if ((sock = calloc(1, sizeof(*sock))) == NULL)
fatal("ocsp_receive_fd: calloc sock");
/* note that sock_addr is not set */
sock->sock_fd = imsg->fd;
sock->sock_env = env;
ocsp->ocsp_sock = sock;
/* fetch 'path' and 'fd' from imsg */
if ((path = get_string(ptr, len)) == NULL)
goto done;
BIO_set_fd(ocsp->ocsp_cbio, imsg->fd, BIO_NOCLOSE);
if ((ocsp->ocsp_req_ctx = OCSP_sendreq_new(ocsp->ocsp_cbio,
path, NULL, -1)) == NULL)
goto done;
if (!OCSP_REQ_CTX_set1_req(ocsp->ocsp_req_ctx, ocsp->ocsp_req))
goto done;
tv.tv_sec = OCSP_TIMEOUT;
tv.tv_usec = 0;
event_set(&sock->sock_ev, sock->sock_fd, EV_WRITE, ocsp_callback, ocsp);
event_add(&sock->sock_ev, &tv);
ret = 0;
done:
if (ret == -1)
ocsp_validate_finish(ocsp, 0); /* failed */
free(path);
return (ret);
}
/* load a stack of x509 certificates */
STACK_OF(X509)*
ocsp_load_certs(const char *file)
{
BIO *bio = NULL;
STACK_OF(X509) *certs = NULL;
STACK_OF(X509_INFO) *xis = NULL;
X509_INFO *xi;
int i;
if ((bio = BIO_new_file(file, "r")) == NULL) {
log_warn("%s: BIO_new_file failed for %s",
__func__, file);
return (NULL);
}
if ((xis = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL)) == NULL) {
ca_sslerror(__func__);
goto done;
}
if ((certs = sk_X509_new_null()) == NULL) {
log_debug("%s: sk_X509_new_null failed for %s", __func__, file);
goto done;
}
for (i = 0; i < sk_X509_INFO_num(xis); i++) {
xi = sk_X509_INFO_value(xis, i);
if (xi->x509) {
if (!sk_X509_push(certs, xi->x509))
goto done;
xi->x509 = NULL;
}
}
done:
if (bio)
BIO_free(bio);
if (xis)
sk_X509_INFO_pop_free(xis, X509_INFO_free);
if (certs && sk_X509_num(certs) <= 0) {
sk_X509_pop_free(certs, X509_free);
certs = NULL;
}
return (certs);
}
/* read/write callback that sends the requests and reads the ocsp response */
void
ocsp_callback(int fd, short event, void *arg)
{
struct iked_ocsp *ocsp = arg;
struct iked_socket *sock = ocsp->ocsp_sock;
struct timeval tv;
OCSP_RESPONSE *resp = NULL;
if (event == EV_TIMEOUT) {
log_info("%s: timeout, giving up",
SPI_SH(&ocsp->ocsp_sh, __func__));
ocsp_validate_finish(ocsp, 0);
return;
}
/*
* Only call OCSP_sendreq_nbio() if should_read/write is
* either not requested or read/write can be called.
*/
if ((!BIO_should_read(ocsp->ocsp_cbio) || (event & EV_READ)) &&
(!BIO_should_write(ocsp->ocsp_cbio) || (event & EV_WRITE)) &&
OCSP_sendreq_nbio(&resp, ocsp->ocsp_req_ctx) != -1 ) {
ocsp_parse_response(ocsp, resp);
return;
}
if (BIO_should_read(ocsp->ocsp_cbio))
event_set(&sock->sock_ev, sock->sock_fd, EV_READ,
ocsp_callback, ocsp);
else if (BIO_should_write(ocsp->ocsp_cbio))
event_set(&sock->sock_ev, sock->sock_fd, EV_WRITE,
ocsp_callback, ocsp);
tv.tv_sec = OCSP_TIMEOUT;
tv.tv_usec = 0;
event_add(&sock->sock_ev, &tv);
}
/* parse the actual OCSP response */
void
ocsp_parse_response(struct iked_ocsp *ocsp, OCSP_RESPONSE *resp)
{
struct iked *env = ocsp->ocsp_env;
X509_STORE *store = NULL;
STACK_OF(X509) *verify_other = NULL;
OCSP_BASICRESP *bs = NULL;
ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
const char *errstr;
int reason = 0, valid = 0, verify_flags = 0;
int status;
if (!resp) {
errstr = "error querying OCSP responder";
goto done;
}
status = OCSP_response_status(resp);
if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
errstr = OCSP_response_status_str(status);
goto done;
}
verify_other = ocsp_load_certs(IKED_OCSP_RESPCERT);
verify_flags |= OCSP_TRUSTOTHER;
if (!verify_other) {
errstr = "no verify_other";
goto done;
}
bs = OCSP_response_get1_basic(resp);
if (!bs) {
errstr = "error parsing response";
goto done;
}
status = OCSP_check_nonce(ocsp->ocsp_req, bs);
if (status <= 0) {
if (status == -1)
log_warnx("%s: no nonce in response",
SPI_SH(&ocsp->ocsp_sh, __func__));
else {
errstr = "nonce verify error";
goto done;
}
}
store = X509_STORE_new();
status = OCSP_basic_verify(bs, verify_other, store, verify_flags);
if (status < 0)
status = OCSP_basic_verify(bs, NULL, store, 0);
if (status <= 0) {
ca_sslerror(__func__);
errstr = "response verify failure";
goto done;
}
log_debug("%s: response verify ok", SPI_SH(&ocsp->ocsp_sh, __func__));
if (!OCSP_resp_find_status(bs, ocsp->ocsp_id, &status, &reason,
&rev, &thisupd, &nextupd)) {
errstr = "no status found";
goto done;
}
if (env->sc_ocsp_tolerate &&
!OCSP_check_validity(thisupd, nextupd, env->sc_ocsp_tolerate,
env->sc_ocsp_maxage)) {
ca_sslerror(SPI_SH(&ocsp->ocsp_sh, __func__));
errstr = "status times invalid";
goto done;
}
errstr = OCSP_cert_status_str(status);
if (status == V_OCSP_CERTSTATUS_GOOD) {
log_debug("%s: status: %s", SPI_SH(&ocsp->ocsp_sh, __func__),
errstr);
valid = 1;
}
done:
if (!valid) {
log_debug("%s: status: %s", __func__, errstr);
}
if (store)
X509_STORE_free(store);
if (verify_other)
sk_X509_pop_free(verify_other, X509_free);
if (resp)
OCSP_RESPONSE_free(resp);
if (bs)
OCSP_BASICRESP_free(bs);
ocsp_validate_finish(ocsp, valid);
}
/*
* finish the ocsp_validate_cert() RPC by sending the appropriate
* message back to the IKEv2 process
*/
int
ocsp_validate_finish(struct iked_ocsp *ocsp, int valid)
{
struct iked *env = ocsp->ocsp_env;
struct iovec iov[2];
int iovcnt = 2, ret, cmd;
iov[0].iov_base = &ocsp->ocsp_sh;
iov[0].iov_len = sizeof(ocsp->ocsp_sh);
iov[1].iov_base = &ocsp->ocsp_type;
iov[1].iov_len = sizeof(ocsp->ocsp_type);
cmd = valid ? IMSG_CERTVALID : IMSG_CERTINVALID;
ret = proc_composev(&env->sc_ps, PROC_IKEV2, cmd, iov, iovcnt);
ocsp_free(ocsp);
return (ret);
}
|