GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/openssl/ocsp.c Lines: 435 710 61.3 %
Date: 2017-11-07 Branches: 235 498 47.2 %

Line Branch Exec Source
1
/* $OpenBSD: ocsp.c,v 1.12 2017/01/21 09:29:09 deraadt Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 2000.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
#ifndef OPENSSL_NO_OCSP
59
60
#include <sys/types.h>
61
62
#include <stdio.h>
63
#include <stdlib.h>
64
#include <limits.h>
65
#include <string.h>
66
#include <poll.h>
67
#include <time.h>
68
69
/* Needs to be included before the openssl headers! */
70
#include "apps.h"
71
72
#include <openssl/bn.h>
73
#include <openssl/crypto.h>
74
#include <openssl/err.h>
75
#include <openssl/evp.h>
76
#include <openssl/ssl.h>
77
#include <openssl/x509v3.h>
78
79
/* Maximum leeway in validity period: default 5 minutes */
80
#define MAX_VALIDITY_PERIOD	(5 * 60)
81
82
static int
83
add_ocsp_cert(OCSP_REQUEST ** req, X509 * cert, const EVP_MD * cert_id_md, X509 * issuer,
84
    STACK_OF(OCSP_CERTID) * ids);
85
static int add_ocsp_serial(OCSP_REQUEST ** req, char *serial, const EVP_MD * cert_id_md, X509 * issuer,
86
    STACK_OF(OCSP_CERTID) * ids);
87
static int print_ocsp_summary(BIO * out, OCSP_BASICRESP * bs, OCSP_REQUEST * req,
88
    STACK_OF(OPENSSL_STRING) * names,
89
    STACK_OF(OCSP_CERTID) * ids, long nsec,
90
    long maxage);
91
92
static int make_ocsp_response(OCSP_RESPONSE ** resp, OCSP_REQUEST * req, CA_DB * db,
93
    X509 * ca, X509 * rcert, EVP_PKEY * rkey,
94
    STACK_OF(X509) * rother, unsigned long flags,
95
    int nmin, int ndays);
96
97
static char **lookup_serial(CA_DB * db, ASN1_INTEGER * ser);
98
static BIO *init_responder(char *port);
99
static int do_responder(OCSP_REQUEST ** preq, BIO ** pcbio, BIO * acbio, char *port);
100
static int send_ocsp_response(BIO * cbio, OCSP_RESPONSE * resp);
101
static OCSP_RESPONSE *query_responder(BIO * err, BIO * cbio, char *path,
102
    STACK_OF(CONF_VALUE) * headers,
103
    OCSP_REQUEST * req, int req_timeout);
104
105
int
106
ocsp_main(int argc, char **argv)
107
{
108
	char **args;
109
80
	char *host = NULL, *port = NULL, *path = "/";
110
	char *reqin = NULL, *respin = NULL;
111
	char *reqout = NULL, *respout = NULL;
112
	char *signfile = NULL, *keyfile = NULL;
113
	char *rsignfile = NULL, *rkeyfile = NULL;
114
	char *outfile = NULL;
115
40
	int add_nonce = 1, noverify = 0, use_ssl = -1;
116
40
	STACK_OF(CONF_VALUE) * headers = NULL;
117
40
	OCSP_REQUEST *req = NULL;
118
40
	OCSP_RESPONSE *resp = NULL;
119
	OCSP_BASICRESP *bs = NULL;
120
	X509 *issuer = NULL, *cert = NULL;
121
	X509 *signer = NULL, *rsigner = NULL;
122
	EVP_PKEY *key = NULL, *rkey = NULL;
123
40
	BIO *acbio = NULL, *cbio = NULL;
124
	BIO *derbio = NULL;
125
	BIO *out = NULL;
126
	int req_timeout = -1;
127
	int req_text = 0, resp_text = 0;
128
	long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
129
	char *CAfile = NULL, *CApath = NULL;
130
	X509_STORE *store = NULL;
131
	STACK_OF(X509) * sign_other = NULL, *verify_other = NULL, *rother = NULL;
132
	char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
133
	unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
134
	int ret = 1;
135
	int accept_count = -1;
136
	int badarg = 0;
137
	int i;
138
	int ignore_err = 0;
139
	STACK_OF(OPENSSL_STRING) * reqnames = NULL;
140
	STACK_OF(OCSP_CERTID) * ids = NULL;
141
	X509 *rca_cert = NULL;
142
	char *ridx_filename = NULL;
143
	char *rca_filename = NULL;
144
	CA_DB *rdb = NULL;
145
	int nmin = 0, ndays = -1;
146
	const EVP_MD *cert_id_md = NULL;
147
40
	const char *errstr = NULL;
148
149
40
	if (single_execution) {
150
40
		if (pledge("stdio cpath wpath rpath inet dns tty flock", NULL) == -1) {
151
			perror("pledge");
152
			exit(1);
153
		}
154
	}
155
156
40
	args = argv + 1;
157
40
	reqnames = sk_OPENSSL_STRING_new_null();
158
40
	ids = sk_OCSP_CERTID_new_null();
159

1008
	while (!badarg && *args && *args[0] == '-') {
160
224
		if (!strcmp(*args, "-out")) {
161
			if (args[1]) {
162
				args++;
163
				outfile = *args;
164
			} else
165
				badarg = 1;
166
224
		} else if (!strcmp(*args, "-timeout")) {
167
			if (args[1]) {
168
				args++;
169
				req_timeout = strtonum(*args, 0,
170
				    INT_MAX, &errstr);
171
				if (errstr) {
172
					BIO_printf(bio_err,
173
					    "Illegal timeout value %s: %s\n",
174
					    *args, errstr);
175
					badarg = 1;
176
				}
177
			} else
178
				badarg = 1;
179
224
		} else if (!strcmp(*args, "-url")) {
180
8
			if (args[1]) {
181
8
				args++;
182
8
				if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl)) {
183
					BIO_printf(bio_err, "Error parsing URL\n");
184
					badarg = 1;
185
				}
186
			} else
187
				badarg = 1;
188
216
		} else if (!strcmp(*args, "-host")) {
189
			if (args[1]) {
190
				args++;
191
				host = *args;
192
			} else
193
				badarg = 1;
194
216
		} else if (!strcmp(*args, "-port")) {
195
8
			if (args[1]) {
196
8
				args++;
197
8
				port = *args;
198
8
			} else
199
				badarg = 1;
200
208
		} else if (!strcmp(*args, "-header")) {
201
			if (args[1] && args[2]) {
202
				if (!X509V3_add_value(args[1], args[2], &headers))
203
					goto end;
204
				args += 2;
205
			} else
206
				badarg = 1;
207
208
		} else if (!strcmp(*args, "-ignore_err"))
208
			ignore_err = 1;
209
208
		else if (!strcmp(*args, "-noverify"))
210
			noverify = 1;
211
208
		else if (!strcmp(*args, "-nonce"))
212
			add_nonce = 2;
213
208
		else if (!strcmp(*args, "-no_nonce"))
214
			add_nonce = 0;
215
208
		else if (!strcmp(*args, "-resp_no_certs"))
216
			rflags |= OCSP_NOCERTS;
217
208
		else if (!strcmp(*args, "-resp_key_id"))
218
			rflags |= OCSP_RESPID_KEY;
219
208
		else if (!strcmp(*args, "-no_certs"))
220
			sign_flags |= OCSP_NOCERTS;
221
208
		else if (!strcmp(*args, "-no_signature_verify"))
222
			verify_flags |= OCSP_NOSIGS;
223
208
		else if (!strcmp(*args, "-no_cert_verify"))
224
			verify_flags |= OCSP_NOVERIFY;
225
208
		else if (!strcmp(*args, "-no_chain"))
226
			verify_flags |= OCSP_NOCHAIN;
227
208
		else if (!strcmp(*args, "-no_cert_checks"))
228
			verify_flags |= OCSP_NOCHECKS;
229
208
		else if (!strcmp(*args, "-no_explicit"))
230
			verify_flags |= OCSP_NOEXPLICIT;
231
208
		else if (!strcmp(*args, "-trust_other"))
232
			verify_flags |= OCSP_TRUSTOTHER;
233
208
		else if (!strcmp(*args, "-no_intern"))
234
			verify_flags |= OCSP_NOINTERN;
235
208
		else if (!strcmp(*args, "-text")) {
236
			req_text = 1;
237
			resp_text = 1;
238
208
		} else if (!strcmp(*args, "-req_text"))
239
			req_text = 1;
240
200
		else if (!strcmp(*args, "-resp_text"))
241
8
			resp_text = 1;
242
192
		else if (!strcmp(*args, "-reqin")) {
243
8
			if (args[1]) {
244
8
				args++;
245
8
				reqin = *args;
246
8
			} else
247
				badarg = 1;
248
184
		} else if (!strcmp(*args, "-respin")) {
249
			if (args[1]) {
250
				args++;
251
				respin = *args;
252
			} else
253
				badarg = 1;
254
184
		} else if (!strcmp(*args, "-signer")) {
255
			if (args[1]) {
256
				args++;
257
				signfile = *args;
258
			} else
259
				badarg = 1;
260
184
		} else if (!strcmp(*args, "-VAfile")) {
261
			if (args[1]) {
262
				args++;
263
				verify_certfile = *args;
264
				verify_flags |= OCSP_TRUSTOTHER;
265
			} else
266
				badarg = 1;
267
184
		} else if (!strcmp(*args, "-sign_other")) {
268
			if (args[1]) {
269
				args++;
270
				sign_certfile = *args;
271
			} else
272
				badarg = 1;
273
184
		} else if (!strcmp(*args, "-verify_other")) {
274
			if (args[1]) {
275
				args++;
276
				verify_certfile = *args;
277
			} else
278
				badarg = 1;
279
184
		} else if (!strcmp(*args, "-CAfile")) {
280
32
			if (args[1]) {
281
32
				args++;
282
32
				CAfile = *args;
283
32
			} else
284
				badarg = 1;
285
152
		} else if (!strcmp(*args, "-CApath")) {
286
			if (args[1]) {
287
				args++;
288
				CApath = *args;
289
			} else
290
				badarg = 1;
291
152
		} else if (!strcmp(*args, "-validity_period")) {
292
			if (args[1]) {
293
				args++;
294
				nsec = strtonum(*args, 0, LONG_MAX, &errstr);
295
				if (errstr) {
296
					BIO_printf(bio_err,
297
					    "Illegal validity period %s: %s\n",
298
					    *args, errstr);
299
					badarg = 1;
300
				}
301
			} else
302
				badarg = 1;
303
152
		} else if (!strcmp(*args, "-status_age")) {
304
			if (args[1]) {
305
				args++;
306
				maxage = strtonum(*args, 0, LONG_MAX, &errstr);
307
				if (errstr) {
308
					BIO_printf(bio_err,
309
					    "Illegal validity age %s: %s\n",
310
					    *args, errstr);
311
					badarg = 1;
312
				}
313
			} else
314
				badarg = 1;
315
152
		} else if (!strcmp(*args, "-signkey")) {
316
			if (args[1]) {
317
				args++;
318
				keyfile = *args;
319
			} else
320
				badarg = 1;
321
152
		} else if (!strcmp(*args, "-reqout")) {
322
8
			if (args[1]) {
323
8
				args++;
324
8
				reqout = *args;
325
8
			} else
326
				badarg = 1;
327
144
		} else if (!strcmp(*args, "-respout")) {
328
16
			if (args[1]) {
329
16
				args++;
330
16
				respout = *args;
331
16
			} else
332
				badarg = 1;
333
128
		} else if (!strcmp(*args, "-path")) {
334
			if (args[1]) {
335
				args++;
336
				path = *args;
337
			} else
338
				badarg = 1;
339
128
		} else if (!strcmp(*args, "-issuer")) {
340
16
			if (args[1]) {
341
16
				args++;
342
16
				X509_free(issuer);
343
16
				issuer = load_cert(bio_err, *args, FORMAT_PEM,
344
				    NULL, "issuer certificate");
345
16
				if (!issuer)
346
					goto end;
347
			} else
348
				badarg = 1;
349
112
		} else if (!strcmp(*args, "-cert")) {
350
32
			if (args[1]) {
351
32
				args++;
352
32
				X509_free(cert);
353
32
				cert = load_cert(bio_err, *args, FORMAT_PEM,
354
				    NULL, "certificate");
355
32
				if (!cert)
356
					goto end;
357
32
				if (!cert_id_md)
358
16
					cert_id_md = EVP_sha1();
359
32
				if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
360
					goto end;
361
32
				if (!sk_OPENSSL_STRING_push(reqnames, *args))
362
					goto end;
363
			} else
364
				badarg = 1;
365
80
		} else if (!strcmp(*args, "-serial")) {
366
			if (args[1]) {
367
				args++;
368
				if (!cert_id_md)
369
					cert_id_md = EVP_sha1();
370
				if (!add_ocsp_serial(&req, *args, cert_id_md, issuer, ids))
371
					goto end;
372
				if (!sk_OPENSSL_STRING_push(reqnames, *args))
373
					goto end;
374
			} else
375
				badarg = 1;
376
80
		} else if (!strcmp(*args, "-index")) {
377
16
			if (args[1]) {
378
16
				args++;
379
16
				ridx_filename = *args;
380
16
			} else
381
				badarg = 1;
382
64
		} else if (!strcmp(*args, "-CA")) {
383
16
			if (args[1]) {
384
16
				args++;
385
16
				rca_filename = *args;
386
16
			} else
387
				badarg = 1;
388
48
		} else if (!strcmp(*args, "-nmin")) {
389
			if (args[1]) {
390
				args++;
391
				nmin = strtonum(*args, 0, INT_MAX, &errstr);
392
				if (errstr) {
393
					BIO_printf(bio_err,
394
					    "Illegal update period %s: %s\n",
395
					    *args, errstr);
396
					badarg = 1;
397
				}
398
			}
399
			if (ndays == -1)
400
				ndays = 0;
401
			else
402
				badarg = 1;
403
48
		} else if (!strcmp(*args, "-nrequest")) {
404
8
			if (args[1]) {
405
8
				args++;
406
8
				accept_count = strtonum(*args, 0, INT_MAX, &errstr);
407
8
				if (errstr) {
408
					BIO_printf(bio_err,
409
					    "Illegal accept count %s: %s\n",
410
					    *args, errstr);
411
					badarg = 1;
412
				}
413
			} else
414
				badarg = 1;
415
40
		} else if (!strcmp(*args, "-ndays")) {
416
			if (args[1]) {
417
				args++;
418
				ndays = strtonum(*args, 0, INT_MAX, &errstr);
419
				if (errstr) {
420
					BIO_printf(bio_err,
421
					    "Illegal update period %s: %s\n",
422
					    *args, errstr);
423
					badarg = 1;
424
				}
425
			} else
426
				badarg = 1;
427
40
		} else if (!strcmp(*args, "-rsigner")) {
428
16
			if (args[1]) {
429
16
				args++;
430
16
				rsignfile = *args;
431
16
			} else
432
				badarg = 1;
433
24
		} else if (!strcmp(*args, "-rkey")) {
434
16
			if (args[1]) {
435
16
				args++;
436
16
				rkeyfile = *args;
437
16
			} else
438
				badarg = 1;
439
8
		} else if (!strcmp(*args, "-rother")) {
440
			if (args[1]) {
441
				args++;
442
				rcertfile = *args;
443
			} else
444
				badarg = 1;
445
8
		} else if ((cert_id_md = EVP_get_digestbyname((*args) + 1)) == NULL) {
446
			badarg = 1;
447
8
		}
448
224
		args++;
449
	}
450
451
	/* Have we anything to do? */
452

56
	if (!req && !reqin && !respin && !(port && ridx_filename))
453
8
		badarg = 1;
454
455
40
	if (badarg) {
456
8
		BIO_printf(bio_err, "OCSP utility\n");
457
8
		BIO_printf(bio_err, "Usage ocsp [options]\n");
458
8
		BIO_printf(bio_err, "where options are\n");
459
8
		BIO_printf(bio_err, "-out file          output filename\n");
460
8
		BIO_printf(bio_err, "-issuer file       issuer certificate\n");
461
8
		BIO_printf(bio_err, "-cert file         certificate to check\n");
462
8
		BIO_printf(bio_err, "-serial n          serial number to check\n");
463
8
		BIO_printf(bio_err, "-signer file       certificate to sign OCSP request with\n");
464
8
		BIO_printf(bio_err, "-signkey file      private key to sign OCSP request with\n");
465
8
		BIO_printf(bio_err, "-sign_other file   additional certificates to include in signed request\n");
466
8
		BIO_printf(bio_err, "-no_certs          don't include any certificates in signed request\n");
467
8
		BIO_printf(bio_err, "-req_text          print text form of request\n");
468
8
		BIO_printf(bio_err, "-resp_text         print text form of response\n");
469
8
		BIO_printf(bio_err, "-text              print text form of request and response\n");
470
8
		BIO_printf(bio_err, "-reqout file       write DER encoded OCSP request to \"file\"\n");
471
8
		BIO_printf(bio_err, "-respout file      write DER encoded OCSP reponse to \"file\"\n");
472
8
		BIO_printf(bio_err, "-reqin file        read DER encoded OCSP request from \"file\"\n");
473
8
		BIO_printf(bio_err, "-respin file       read DER encoded OCSP reponse from \"file\"\n");
474
8
		BIO_printf(bio_err, "-nonce             add OCSP nonce to request\n");
475
8
		BIO_printf(bio_err, "-no_nonce          don't add OCSP nonce to request\n");
476
8
		BIO_printf(bio_err, "-url URL           OCSP responder URL\n");
477
8
		BIO_printf(bio_err, "-host host:n       send OCSP request to host on port n\n");
478
8
		BIO_printf(bio_err, "-path              path to use in OCSP request\n");
479
8
		BIO_printf(bio_err, "-CApath dir        trusted certificates directory\n");
480
8
		BIO_printf(bio_err, "-CAfile file       trusted certificates file\n");
481
8
		BIO_printf(bio_err, "-VAfile file       validator certificates file\n");
482
8
		BIO_printf(bio_err, "-validity_period n maximum validity discrepancy in seconds\n");
483
8
		BIO_printf(bio_err, "-status_age n      maximum status age in seconds\n");
484
8
		BIO_printf(bio_err, "-noverify          don't verify response at all\n");
485
8
		BIO_printf(bio_err, "-verify_other file additional certificates to search for signer\n");
486
8
		BIO_printf(bio_err, "-trust_other       don't verify additional certificates\n");
487
8
		BIO_printf(bio_err, "-no_intern         don't search certificates contained in response for signer\n");
488
8
		BIO_printf(bio_err, "-no_signature_verify don't check signature on response\n");
489
8
		BIO_printf(bio_err, "-no_cert_verify    don't check signing certificate\n");
490
8
		BIO_printf(bio_err, "-no_chain          don't chain verify response\n");
491
8
		BIO_printf(bio_err, "-no_cert_checks    don't do additional checks on signing certificate\n");
492
8
		BIO_printf(bio_err, "-port num		 port to run responder on\n");
493
8
		BIO_printf(bio_err, "-index file	 certificate status index file\n");
494
8
		BIO_printf(bio_err, "-CA file		 CA certificate\n");
495
8
		BIO_printf(bio_err, "-rsigner file	 responder certificate to sign responses with\n");
496
8
		BIO_printf(bio_err, "-rkey file	 responder key to sign responses with\n");
497
8
		BIO_printf(bio_err, "-rother file	 other certificates to include in response\n");
498
8
		BIO_printf(bio_err, "-resp_no_certs     don't include any certificates in response\n");
499
8
		BIO_printf(bio_err, "-nmin n		 number of minutes before next update\n");
500
8
		BIO_printf(bio_err, "-ndays n		 number of days before next update\n");
501
8
		BIO_printf(bio_err, "-resp_key_id       identify reponse by signing certificate key ID\n");
502
8
		BIO_printf(bio_err, "-nrequest n        number of requests to accept (default unlimited)\n");
503
8
		BIO_printf(bio_err, "-<dgst alg>     use specified digest in the request\n");
504
8
		goto end;
505
	}
506
32
	if (outfile)
507
		out = BIO_new_file(outfile, "w");
508
	else
509
32
		out = BIO_new_fp(stdout, BIO_NOCLOSE);
510
511
32
	if (!out) {
512
		BIO_printf(bio_err, "Error opening output file\n");
513
		goto end;
514
	}
515
32
	if (!req && (add_nonce != 2))
516
16
		add_nonce = 0;
517
518
32
	if (!req && reqin) {
519
8
		derbio = BIO_new_file(reqin, "rb");
520
8
		if (!derbio) {
521
			BIO_printf(bio_err, "Error Opening OCSP request file\n");
522
			goto end;
523
		}
524
8
		req = d2i_OCSP_REQUEST_bio(derbio, NULL);
525
8
		BIO_free(derbio);
526
8
		if (!req) {
527
			BIO_printf(bio_err, "Error reading OCSP request\n");
528
			goto end;
529
		}
530
	}
531
32
	if (!req && port) {
532
8
		acbio = init_responder(port);
533
8
		if (!acbio)
534
			goto end;
535
	}
536
32
	if (rsignfile && !rdb) {
537
16
		if (!rkeyfile)
538
			rkeyfile = rsignfile;
539
16
		rsigner = load_cert(bio_err, rsignfile, FORMAT_PEM,
540
		    NULL, "responder certificate");
541
16
		if (!rsigner) {
542
			BIO_printf(bio_err, "Error loading responder certificate\n");
543
			goto end;
544
		}
545
16
		rca_cert = load_cert(bio_err, rca_filename, FORMAT_PEM,
546
		    NULL, "CA certificate");
547
16
		if (rcertfile) {
548
			rother = load_certs(bio_err, rcertfile, FORMAT_PEM,
549
			    NULL, "responder other certificates");
550
			if (!rother)
551
				goto end;
552
		}
553
16
		rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL,
554
		    "responder private key");
555
16
		if (!rkey)
556
			goto end;
557
	}
558
32
	if (acbio)
559
8
		BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
560
561
redo_accept:
562
563
32
	if (acbio) {
564
8
		if (!do_responder(&req, &cbio, acbio, port))
565
			goto end;
566
8
		if (!req) {
567
			resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
568
			send_ocsp_response(cbio, resp);
569
			goto done_resp;
570
		}
571
	}
572

32
	if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
573
		BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
574
		goto end;
575
	}
576
32
	if (req && add_nonce)
577
16
		OCSP_request_add1_nonce(req, NULL, -1);
578
579
32
	if (signfile) {
580
		if (!keyfile)
581
			keyfile = signfile;
582
		signer = load_cert(bio_err, signfile, FORMAT_PEM,
583
		    NULL, "signer certificate");
584
		if (!signer) {
585
			BIO_printf(bio_err, "Error loading signer certificate\n");
586
			goto end;
587
		}
588
		if (sign_certfile) {
589
			sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM,
590
			    NULL, "signer certificates");
591
			if (!sign_other)
592
				goto end;
593
		}
594
		key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL,
595
		    "signer private key");
596
		if (!key)
597
			goto end;
598
599
		if (!OCSP_request_sign(req, signer, key, NULL, sign_other, sign_flags)) {
600
			BIO_printf(bio_err, "Error signing OCSP request\n");
601
			goto end;
602
		}
603
	}
604
32
	if (req_text && req)
605
8
		OCSP_REQUEST_print(out, req, 0);
606
607
32
	if (reqout) {
608
8
		derbio = BIO_new_file(reqout, "wb");
609
8
		if (!derbio) {
610
			BIO_printf(bio_err, "Error opening file %s\n", reqout);
611
			goto end;
612
		}
613
8
		i2d_OCSP_REQUEST_bio(derbio, req);
614
8
		BIO_free(derbio);
615
8
	}
616

48
	if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
617
		BIO_printf(bio_err, "Need a responder certificate, key and CA for this operation!\n");
618
		goto end;
619
	}
620
32
	if (ridx_filename && !rdb) {
621
16
		rdb = load_index(ridx_filename, NULL);
622
16
		if (!rdb)
623
			goto end;
624
16
		if (!index_index(rdb))
625
			goto end;
626
	}
627
32
	if (rdb) {
628
16
		i = make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, rother, rflags, nmin, ndays);
629
16
		if (cbio)
630
8
			send_ocsp_response(cbio, resp);
631
16
	} else if (host) {
632
16
		resp = process_responder(bio_err, req, host, path,
633
8
		    port, use_ssl, headers, req_timeout);
634
8
		if (!resp)
635
			goto end;
636
8
	} else if (respin) {
637
		derbio = BIO_new_file(respin, "rb");
638
		if (!derbio) {
639
			BIO_printf(bio_err, "Error Opening OCSP response file\n");
640
			goto end;
641
		}
642
		resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
643
		BIO_free(derbio);
644
		if (!resp) {
645
			BIO_printf(bio_err, "Error reading OCSP response\n");
646
			goto end;
647
		}
648
	} else {
649
		ret = 0;
650
8
		goto end;
651
	}
652
653
done_resp:
654
655
24
	if (respout) {
656
16
		derbio = BIO_new_file(respout, "wb");
657
16
		if (!derbio) {
658
			BIO_printf(bio_err, "Error opening file %s\n", respout);
659
			goto end;
660
		}
661
16
		i2d_OCSP_RESPONSE_bio(derbio, resp);
662
16
		BIO_free(derbio);
663
16
	}
664
24
	i = OCSP_response_status(resp);
665
666
24
	if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
667
		BIO_printf(bio_err, "Responder Error: %s (%d)\n",
668
		    OCSP_response_status_str(i), i);
669
		if (ignore_err)
670
			goto redo_accept;
671
		ret = 1;
672
		goto end;
673
	}
674
24
	if (resp_text)
675
16
		OCSP_RESPONSE_print(out, resp, 0);
676
677
	/* If running as responder don't verify our own response */
678
24
	if (cbio) {
679
8
		if (accept_count > 0)
680
8
			accept_count--;
681
		/* Redo if more connections needed */
682
8
		if (accept_count) {
683
			BIO_free_all(cbio);
684
			cbio = NULL;
685
			OCSP_REQUEST_free(req);
686
			req = NULL;
687
			OCSP_RESPONSE_free(resp);
688
			resp = NULL;
689
			goto redo_accept;
690
		}
691
		goto end;
692
	}
693
16
	if (!store)
694
16
		store = setup_verify(bio_err, CAfile, CApath);
695
16
	if (!store)
696
		goto end;
697
16
	if (verify_certfile) {
698
		verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM,
699
		    NULL, "validator certificate");
700
		if (!verify_other)
701
			goto end;
702
	}
703
16
	bs = OCSP_response_get1_basic(resp);
704
705
16
	if (!bs) {
706
		BIO_printf(bio_err, "Error parsing response\n");
707
		goto end;
708
	}
709
16
	if (!noverify) {
710

32
		if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
711
			if (i == -1)
712
				BIO_printf(bio_err, "WARNING: no nonce in response\n");
713
			else {
714
				BIO_printf(bio_err, "Nonce Verify error\n");
715
				goto end;
716
			}
717
		}
718
16
		i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
719
16
		if (i < 0)
720
			i = OCSP_basic_verify(bs, NULL, store, 0);
721
722
16
		if (i <= 0) {
723
			BIO_printf(bio_err, "Response Verify Failure\n");
724
			ERR_print_errors(bio_err);
725
		} else
726
16
			BIO_printf(bio_err, "Response verify OK\n");
727
728
	}
729
16
	if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
730
		goto end;
731
732
16
	ret = 0;
733
734
end:
735
40
	ERR_print_errors(bio_err);
736
40
	X509_free(signer);
737
40
	X509_STORE_free(store);
738
40
	EVP_PKEY_free(key);
739
40
	EVP_PKEY_free(rkey);
740
40
	X509_free(issuer);
741
40
	X509_free(cert);
742
40
	X509_free(rsigner);
743
40
	X509_free(rca_cert);
744
40
	free_index(rdb);
745
40
	BIO_free_all(cbio);
746
40
	BIO_free_all(acbio);
747
40
	BIO_free(out);
748
40
	OCSP_REQUEST_free(req);
749
40
	OCSP_RESPONSE_free(resp);
750
40
	OCSP_BASICRESP_free(bs);
751
40
	sk_OPENSSL_STRING_free(reqnames);
752
40
	sk_OCSP_CERTID_free(ids);
753
40
	sk_X509_pop_free(sign_other, X509_free);
754
40
	sk_X509_pop_free(verify_other, X509_free);
755
40
	sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
756
757
40
	if (use_ssl != -1) {
758
8
		free(host);
759
8
		free(port);
760
8
		free(path);
761
8
	}
762
40
	return (ret);
763
40
}
764
765
static int
766
add_ocsp_cert(OCSP_REQUEST ** req, X509 * cert, const EVP_MD * cert_id_md, X509 * issuer,
767
    STACK_OF(OCSP_CERTID) * ids)
768
{
769
	OCSP_CERTID *id;
770
64
	if (!issuer) {
771
		BIO_printf(bio_err, "No issuer certificate specified\n");
772
		return 0;
773
	}
774
32
	if (!*req)
775
16
		*req = OCSP_REQUEST_new();
776
32
	if (!*req)
777
		goto err;
778
32
	id = OCSP_cert_to_id(cert_id_md, cert, issuer);
779

64
	if (!id || !sk_OCSP_CERTID_push(ids, id))
780
		goto err;
781
32
	if (!OCSP_request_add0_id(*req, id))
782
		goto err;
783
32
	return 1;
784
785
err:
786
	BIO_printf(bio_err, "Error Creating OCSP request\n");
787
	return 0;
788
32
}
789
790
static int
791
add_ocsp_serial(OCSP_REQUEST ** req, char *serial, const EVP_MD * cert_id_md, X509 * issuer,
792
    STACK_OF(OCSP_CERTID) * ids)
793
{
794
	OCSP_CERTID *id;
795
	X509_NAME *iname;
796
	ASN1_BIT_STRING *ikey;
797
	ASN1_INTEGER *sno;
798
	if (!issuer) {
799
		BIO_printf(bio_err, "No issuer certificate specified\n");
800
		return 0;
801
	}
802
	if (!*req)
803
		*req = OCSP_REQUEST_new();
804
	if (!*req)
805
		goto err;
806
	iname = X509_get_subject_name(issuer);
807
	ikey = X509_get0_pubkey_bitstr(issuer);
808
	sno = s2i_ASN1_INTEGER(NULL, serial);
809
	if (!sno) {
810
		BIO_printf(bio_err, "Error converting serial number %s\n", serial);
811
		return 0;
812
	}
813
	id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
814
	ASN1_INTEGER_free(sno);
815
	if (!id || !sk_OCSP_CERTID_push(ids, id))
816
		goto err;
817
	if (!OCSP_request_add0_id(*req, id))
818
		goto err;
819
	return 1;
820
821
err:
822
	BIO_printf(bio_err, "Error Creating OCSP request\n");
823
	return 0;
824
}
825
826
static int
827
print_ocsp_summary(BIO * out, OCSP_BASICRESP * bs, OCSP_REQUEST * req,
828
    STACK_OF(OPENSSL_STRING) * names,
829
    STACK_OF(OCSP_CERTID) * ids, long nsec,
830
    long maxage)
831
{
832
	OCSP_CERTID *id;
833
	char *name;
834
	int i;
835
836
32
	int status, reason;
837
838
16
	ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
839
840

40
	if (!bs || !req || !sk_OPENSSL_STRING_num(names) || !sk_OCSP_CERTID_num(ids))
841
8
		return 1;
842
843
48
	for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
844
16
		id = sk_OCSP_CERTID_value(ids, i);
845
16
		name = sk_OPENSSL_STRING_value(names, i);
846
16
		BIO_printf(out, "%s: ", name);
847
848
16
		if (!OCSP_resp_find_status(bs, id, &status, &reason,
849
			&rev, &thisupd, &nextupd)) {
850
			BIO_puts(out, "ERROR: No Status found.\n");
851
			continue;
852
		}
853
		/*
854
		 * Check validity: if invalid write to output BIO so we know
855
		 * which response this refers to.
856
		 */
857
16
		if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
858
			BIO_puts(out, "WARNING: Status times invalid.\n");
859
			ERR_print_errors(out);
860
		}
861
16
		BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
862
863
16
		BIO_puts(out, "\tThis Update: ");
864
16
		ASN1_GENERALIZEDTIME_print(out, thisupd);
865
16
		BIO_puts(out, "\n");
866
867
16
		if (nextupd) {
868
			BIO_puts(out, "\tNext Update: ");
869
			ASN1_GENERALIZEDTIME_print(out, nextupd);
870
			BIO_puts(out, "\n");
871
		}
872
16
		if (status != V_OCSP_CERTSTATUS_REVOKED)
873
			continue;
874
875
8
		if (reason != -1)
876
			BIO_printf(out, "\tReason: %s\n",
877
			    OCSP_crl_reason_str(reason));
878
879
8
		BIO_puts(out, "\tRevocation Time: ");
880
8
		ASN1_GENERALIZEDTIME_print(out, rev);
881
8
		BIO_puts(out, "\n");
882
8
	}
883
884
8
	return 1;
885
16
}
886
887
888
static int
889
make_ocsp_response(OCSP_RESPONSE ** resp, OCSP_REQUEST * req, CA_DB * db,
890
    X509 * ca, X509 * rcert, EVP_PKEY * rkey,
891
    STACK_OF(X509) * rother, unsigned long flags,
892
    int nmin, int ndays)
893
{
894
	ASN1_TIME *thisupd = NULL, *nextupd = NULL;
895
	OCSP_CERTID *cid, *ca_id = NULL;
896
	OCSP_BASICRESP *bs = NULL;
897
	int i, id_count, ret = 1;
898
899
32
	id_count = OCSP_request_onereq_count(req);
900
901
16
	if (id_count <= 0) {
902
		*resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
903
		goto end;
904
	}
905
16
	bs = OCSP_BASICRESP_new();
906
16
	thisupd = X509_gmtime_adj(NULL, 0);
907
16
	if (ndays != -1)
908
		nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24);
909
910
	/* Examine each certificate id in the request */
911
96
	for (i = 0; i < id_count; i++) {
912
		OCSP_ONEREQ *one;
913
32
		ASN1_INTEGER *serial;
914
		char **inf;
915
32
		ASN1_OBJECT *cert_id_md_oid;
916
		const EVP_MD *cert_id_md;
917
32
		one = OCSP_request_onereq_get0(req, i);
918
32
		cid = OCSP_onereq_get0_id(one);
919
920
32
		OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
921
922
32
		cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
923
32
		if (!cert_id_md) {
924
			*resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
925
			    NULL);
926
			goto end;
927
		}
928
32
		if (ca_id)
929
16
			OCSP_CERTID_free(ca_id);
930
32
		ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
931
932
		/* Is this request about our CA? */
933
32
		if (OCSP_id_issuer_cmp(ca_id, cid)) {
934
			OCSP_basic_add1_status(bs, cid,
935
			    V_OCSP_CERTSTATUS_UNKNOWN,
936
			    0, NULL,
937
			    thisupd, nextupd);
938
			continue;
939
		}
940
32
		OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
941
32
		inf = lookup_serial(db, serial);
942
32
		if (!inf)
943
			OCSP_basic_add1_status(bs, cid,
944
			    V_OCSP_CERTSTATUS_UNKNOWN,
945
			    0, NULL,
946
			    thisupd, nextupd);
947
32
		else if (inf[DB_type][0] == DB_TYPE_VAL)
948
16
			OCSP_basic_add1_status(bs, cid,
949
			    V_OCSP_CERTSTATUS_GOOD,
950
			    0, NULL,
951
			    thisupd, nextupd);
952
16
		else if (inf[DB_type][0] == DB_TYPE_REV) {
953
16
			ASN1_OBJECT *inst = NULL;
954
16
			ASN1_TIME *revtm = NULL;
955
16
			ASN1_GENERALIZEDTIME *invtm = NULL;
956
			OCSP_SINGLERESP *single;
957
16
			int reason = -1;
958
16
			unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
959
16
			single = OCSP_basic_add1_status(bs, cid,
960
			    V_OCSP_CERTSTATUS_REVOKED,
961
16
			    reason, revtm,
962
			    thisupd, nextupd);
963
16
			if (invtm)
964
				OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, invtm, 0, 0);
965
16
			else if (inst)
966
				OCSP_SINGLERESP_add1_ext_i2d(single, NID_hold_instruction_code, inst, 0, 0);
967
16
			ASN1_OBJECT_free(inst);
968
16
			ASN1_TIME_free(revtm);
969
16
			ASN1_GENERALIZEDTIME_free(invtm);
970
16
		}
971

96
	}
972
973
16
	OCSP_copy_nonce(bs, req);
974
975
16
	OCSP_basic_sign(bs, rcert, rkey, NULL, rother, flags);
976
977
16
	*resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
978
979
end:
980
16
	ASN1_TIME_free(thisupd);
981
16
	ASN1_TIME_free(nextupd);
982
16
	OCSP_CERTID_free(ca_id);
983
16
	OCSP_BASICRESP_free(bs);
984
16
	return ret;
985
986
16
}
987
988
static char **
989
lookup_serial(CA_DB * db, ASN1_INTEGER * ser)
990
{
991
	int i;
992
	BIGNUM *bn = NULL;
993
64
	char *itmp, *row[DB_NUMBER], **rrow;
994
448
	for (i = 0; i < DB_NUMBER; i++)
995
192
		row[i] = NULL;
996
32
	bn = ASN1_INTEGER_to_BN(ser, NULL);
997
32
	OPENSSL_assert(bn);	/* FIXME: should report an error at this
998
				 * point and abort */
999
32
	if (BN_is_zero(bn))
1000
		itmp = strdup("00");
1001
	else
1002
32
		itmp = BN_bn2hex(bn);
1003
32
	row[DB_serial] = itmp;
1004
32
	BN_free(bn);
1005
32
	rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1006
32
	free(itmp);
1007
32
	return rrow;
1008
32
}
1009
1010
/* Quick and dirty OCSP server: read in and parse input request */
1011
1012
static BIO *
1013
init_responder(char *port)
1014
{
1015
	BIO *acbio = NULL, *bufbio = NULL;
1016
16
	bufbio = BIO_new(BIO_f_buffer());
1017
8
	if (!bufbio)
1018
		goto err;
1019
8
	acbio = BIO_new_accept(port);
1020
8
	if (!acbio)
1021
		goto err;
1022
8
	BIO_set_accept_bios(acbio, bufbio);
1023
	bufbio = NULL;
1024
1025
8
	if (BIO_do_accept(acbio) <= 0) {
1026
		BIO_printf(bio_err, "Error setting up accept BIO\n");
1027
		ERR_print_errors(bio_err);
1028
		goto err;
1029
	}
1030
8
	return acbio;
1031
1032
err:
1033
	BIO_free_all(acbio);
1034
	BIO_free(bufbio);
1035
	return NULL;
1036
8
}
1037
1038
static int
1039
do_responder(OCSP_REQUEST ** preq, BIO ** pcbio, BIO * acbio, char *port)
1040
{
1041
	int have_post = 0, len;
1042
	OCSP_REQUEST *req = NULL;
1043
16
	char inbuf[1024];
1044
	BIO *cbio = NULL;
1045
1046
8
	if (BIO_do_accept(acbio) <= 0) {
1047
		BIO_printf(bio_err, "Error accepting connection\n");
1048
		ERR_print_errors(bio_err);
1049
		return 0;
1050
	}
1051
8
	cbio = BIO_pop(acbio);
1052
8
	*pcbio = cbio;
1053
1054
8
	for (;;) {
1055
32
		len = BIO_gets(cbio, inbuf, sizeof inbuf);
1056
32
		if (len <= 0)
1057
			return 1;
1058
		/* Look for "POST" signalling start of query */
1059
32
		if (!have_post) {
1060
8
			if (strncmp(inbuf, "POST", 4)) {
1061
				BIO_printf(bio_err, "Invalid request\n");
1062
				return 1;
1063
			}
1064
			have_post = 1;
1065
8
		}
1066
		/* Look for end of headers */
1067

56
		if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1068
			break;
1069
	}
1070
1071
	/* Try to read OCSP request */
1072
1073
8
	req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1074
1075
8
	if (!req) {
1076
		BIO_printf(bio_err, "Error parsing OCSP request\n");
1077
		ERR_print_errors(bio_err);
1078
	}
1079
8
	*preq = req;
1080
1081
8
	return 1;
1082
1083
8
}
1084
1085
static int
1086
send_ocsp_response(BIO * cbio, OCSP_RESPONSE * resp)
1087
{
1088
	static const char http_resp[] =
1089
	"HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1090
	"Content-Length: %d\r\n\r\n";
1091
16
	if (!cbio)
1092
		return 0;
1093
8
	BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1094
8
	i2d_OCSP_RESPONSE_bio(cbio, resp);
1095
8
	(void) BIO_flush(cbio);
1096
8
	return 1;
1097
8
}
1098
1099
static OCSP_RESPONSE *
1100
query_responder(BIO * err, BIO * cbio, char *path,
1101
    STACK_OF(CONF_VALUE) * headers,
1102
    OCSP_REQUEST * req, int req_timeout)
1103
{
1104
16
	int fd;
1105
	int rv;
1106
	int i;
1107
	OCSP_REQ_CTX *ctx = NULL;
1108
8
	OCSP_RESPONSE *rsp = NULL;
1109
8
	struct pollfd pfd[1];
1110
1111
8
	if (req_timeout != -1)
1112
		BIO_set_nbio(cbio, 1);
1113
1114
8
	rv = BIO_do_connect(cbio);
1115
1116

8
	if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1117
		BIO_puts(err, "Error connecting BIO\n");
1118
		return NULL;
1119
	}
1120
8
	if (BIO_get_fd(cbio, &fd) < 0) {
1121
		BIO_puts(err, "Can't get connection fd\n");
1122
		goto err;
1123
	}
1124
8
	if (req_timeout != -1 && rv <= 0) {
1125
		pfd[0].fd = fd;
1126
		pfd[0].events = POLLOUT;
1127
		rv = poll(pfd, 1, req_timeout * 1000);
1128
		if (rv == 0) {
1129
			BIO_puts(err, "Timeout on connect\n");
1130
			return NULL;
1131
		}
1132
		if (rv == -1) {
1133
			BIO_puts(err, "Poll error\n");
1134
			return NULL;
1135
		}
1136
	}
1137
8
	ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1138
8
	if (!ctx)
1139
		return NULL;
1140
1141
16
	for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1142
		CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1143
		if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1144
			goto err;
1145
	}
1146
1147
8
	if (!OCSP_REQ_CTX_set1_req(ctx, req))
1148
		goto err;
1149
1150
	for (;;) {
1151
8
		rv = OCSP_sendreq_nbio(&rsp, ctx);
1152
8
		if (rv != -1)
1153
			break;
1154
		if (req_timeout == -1)
1155
			continue;
1156
		pfd[0].fd = fd;
1157
		if (BIO_should_read(cbio))
1158
			pfd[0].events = POLLIN;
1159
		else if (BIO_should_write(cbio))
1160
			pfd[0].events = POLLOUT;
1161
		else {
1162
			BIO_puts(err, "Unexpected retry condition\n");
1163
			goto err;
1164
		}
1165
		rv = poll(pfd, 1, req_timeout * 1000);
1166
		if (rv == 0) {
1167
			BIO_puts(err, "Timeout on request\n");
1168
			break;
1169
		}
1170
		if (rv == -1 || (pfd[0].revents & (POLLERR|POLLNVAL))) {
1171
			BIO_puts(err, "Poll error\n");
1172
			break;
1173
		}
1174
	}
1175
err:
1176
8
	if (ctx)
1177
8
		OCSP_REQ_CTX_free(ctx);
1178
1179
8
	return rsp;
1180
8
}
1181
1182
OCSP_RESPONSE *
1183
process_responder(BIO * err, OCSP_REQUEST * req,
1184
    char *host, char *path, char *port, int use_ssl,
1185
    STACK_OF(CONF_VALUE) * headers,
1186
    int req_timeout)
1187
{
1188
	BIO *cbio = NULL;
1189
	SSL_CTX *ctx = NULL;
1190
	OCSP_RESPONSE *resp = NULL;
1191
16
	cbio = BIO_new_connect(host);
1192
8
	if (!cbio) {
1193
		BIO_printf(err, "Error creating connect BIO\n");
1194
		goto end;
1195
	}
1196
8
	if (port)
1197
8
		BIO_set_conn_port(cbio, port);
1198
8
	if (use_ssl == 1) {
1199
		BIO *sbio;
1200
		ctx = SSL_CTX_new(SSLv23_client_method());
1201
		if (ctx == NULL) {
1202
			BIO_printf(err, "Error creating SSL context.\n");
1203
			goto end;
1204
		}
1205
		SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1206
		sbio = BIO_new_ssl(ctx, 1);
1207
		cbio = BIO_push(sbio, cbio);
1208
	}
1209
8
	resp = query_responder(err, cbio, path, headers, req, req_timeout);
1210
8
	if (!resp)
1211
		BIO_printf(bio_err, "Error querying OCSP responder\n");
1212
end:
1213
8
	if (cbio)
1214
8
		BIO_free_all(cbio);
1215
8
	if (ctx)
1216
		SSL_CTX_free(ctx);
1217
8
	return resp;
1218
8
}
1219
1220
#endif