]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/ocsp.c
Centralise loading default apps config file
[thirdparty/openssl.git] / apps / ocsp.c
1 /*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 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 # ifdef OPENSSL_SYS_VMS
61 # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
62 * on OpenVMS */
63 # endif
64
65 # define USE_SOCKETS
66
67 # include <stdio.h>
68 # include <stdlib.h>
69 # include <string.h>
70 # include <time.h>
71 # include <ctype.h>
72 # include "apps.h" /* needs to be included before the openssl
73 * headers! */
74 # include <openssl/e_os2.h>
75 # include <openssl/crypto.h>
76 # include <openssl/err.h>
77 # include <openssl/ssl.h>
78 # include <openssl/evp.h>
79 # include <openssl/bn.h>
80 # include <openssl/x509v3.h>
81
82 # if defined(NETWARE_CLIB)
83 # ifdef NETWARE_BSDSOCK
84 # include <sys/socket.h>
85 # include <sys/bsdskt.h>
86 # else
87 # include <novsock2.h>
88 # endif
89 # elif defined(NETWARE_LIBC)
90 # ifdef NETWARE_BSDSOCK
91 # include <sys/select.h>
92 # else
93 # include <novsock2.h>
94 # endif
95 # endif
96
97 /* Maximum leeway in validity period: default 5 minutes */
98 # define MAX_VALIDITY_PERIOD (5 * 60)
99
100 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
101 const EVP_MD *cert_id_md, X509 *issuer,
102 STACK_OF(OCSP_CERTID) *ids);
103 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
104 const EVP_MD *cert_id_md, X509 *issuer,
105 STACK_OF(OCSP_CERTID) *ids);
106 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
107 STACK_OF(OPENSSL_STRING) *names,
108 STACK_OF(OCSP_CERTID) *ids, long nsec,
109 long maxage);
110 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
111 CA_DB *db, X509 *ca, X509 *rcert,
112 EVP_PKEY *rkey, const EVP_MD *md,
113 STACK_OF(X509) *rother, unsigned long flags,
114 int nmin, int ndays, int badsig);
115
116 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
117 static BIO *init_responder(const char *port);
118 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
119 const char *port);
120 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
121 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *path,
122 const STACK_OF(CONF_VALUE) *headers,
123 OCSP_REQUEST *req, int req_timeout);
124
125 typedef enum OPTION_choice {
126 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
127 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
128 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
129 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
130 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
131 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
132 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
133 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
134 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH,
135 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
136 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
137 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
138 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
139 OPT_V_ENUM,
140 OPT_MD
141 } OPTION_CHOICE;
142
143 OPTIONS ocsp_options[] = {
144 {"help", OPT_HELP, '-', "Display this summary"},
145 {"out", OPT_OUTFILE, '>', "Output filename"},
146 {"timeout", OPT_TIMEOUT, 'p'},
147 {"url", OPT_URL, 's', "Responder URL"},
148 {"host", OPT_HOST, 's', "host:prot top to connect to"},
149 {"port", OPT_PORT, 'p', "Port to run responder on"},
150 {"ignore_err", OPT_IGNORE_ERR, '-'},
151 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
152 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
153 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
154 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
155 "Don't include any certificates in response"},
156 {"resp_key_id", OPT_RESP_KEY_ID, '-',
157 "Identify reponse by signing certificate key ID"},
158 {"no_certs", OPT_NO_CERTS, '-',
159 "Don't include any certificates in signed request"},
160 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
161 "Don't check signature on response"},
162 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
163 "Don't check signing certificate"},
164 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
165 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
166 "Don't do additional checks on signing certificate"},
167 {"no_explicit", OPT_NO_EXPLICIT, '-'},
168 {"trust_other", OPT_TRUST_OTHER, '-',
169 "Don't verify additional certificates"},
170 {"no_intern", OPT_NO_INTERN, '-',
171 "Don't search certificates contained in response for signer"},
172 {"badsig", OPT_BADSIG, '-'},
173 {"text", OPT_TEXT, '-', "Print text form of request and response"},
174 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
175 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
176 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
177 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
178 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
179 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
180 {"sign_other", OPT_SIGN_OTHER, '<',
181 "Additional certificates to include in signed request"},
182 {"verify_other", OPT_VERIFY_OTHER, '<',
183 "Additional certificates to search for signer"},
184 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
185 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
186 {"no-CAfile", OPT_NOCAFILE, '-',
187 "Do not load the default certificates file"},
188 {"no-CApath", OPT_NOCAPATH, '-',
189 "Do not load certificates from the default certificates directory"},
190 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
191 "Maximum validity discrepancy in seconds"},
192 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
193 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
194 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
195 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
196 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
197 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
198 {"cert", OPT_CERT, '<', "Certificate to check"},
199 {"serial", OPT_SERIAL, 's', "Nerial number to check"},
200 {"index", OPT_INDEX, '<', "Certificate status index file"},
201 {"CA", OPT_CA, '<', "CA certificate"},
202 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
203 {"nrequest", OPT_REQUEST, 'p',
204 "Number of requests to accept (default unlimited)"},
205 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
206 {"rsigner", OPT_RSIGNER, '<',
207 "Sesponder certificate to sign responses with"},
208 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
209 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
210 {"rmd", OPT_RMD, 's'},
211 {"header", OPT_HEADER, 's', "key=value header to add"},
212 {"", OPT_MD, '-', "Any supported digest"},
213 OPT_V_OPTIONS,
214 {NULL}
215 };
216
217 int ocsp_main(int argc, char **argv)
218 {
219 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
220 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
221 CA_DB *rdb = NULL;
222 EVP_PKEY *key = NULL, *rkey = NULL;
223 OCSP_BASICRESP *bs = NULL;
224 OCSP_REQUEST *req = NULL;
225 OCSP_RESPONSE *resp = NULL;
226 STACK_OF(CONF_VALUE) *headers = NULL;
227 STACK_OF(OCSP_CERTID) *ids = NULL;
228 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
229 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
230 STACK_OF(X509) *issuers = NULL;
231 X509 *issuer = NULL, *cert = NULL, *rca_cert = NULL;
232 X509 *signer = NULL, *rsigner = NULL;
233 X509_STORE *store = NULL;
234 X509_VERIFY_PARAM *vpm = NULL;
235 char *CAfile = NULL, *CApath = NULL, *header, *value;
236 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
237 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
238 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
239 char *rsignfile = NULL, *rkeyfile = NULL;
240 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
241 char *signfile = NULL, *keyfile = NULL;
242 char *thost = NULL, *tport = NULL, *tpath = NULL;
243 int noCAfile = 0, noCApath = 0;
244 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
245 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
246 int req_text = 0, resp_text = 0, req_timeout = -1, ret = 1;
247 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
248 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
249 OPTION_CHOICE o;
250 char *prog;
251
252 reqnames = sk_OPENSSL_STRING_new_null();
253 if (!reqnames)
254 goto end;
255 ids = sk_OCSP_CERTID_new_null();
256 if (!ids)
257 goto end;
258 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
259 return 1;
260
261 prog = opt_init(argc, argv, ocsp_options);
262 while ((o = opt_next()) != OPT_EOF) {
263 switch (o) {
264 case OPT_EOF:
265 case OPT_ERR:
266 opthelp:
267 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
268 goto end;
269 case OPT_HELP:
270 ret = 0;
271 opt_help(ocsp_options);
272 goto end;
273 case OPT_OUTFILE:
274 outfile = opt_arg();
275 break;
276 case OPT_TIMEOUT:
277 req_timeout = atoi(opt_arg());
278 break;
279 case OPT_URL:
280 OPENSSL_free(thost);
281 OPENSSL_free(tport);
282 OPENSSL_free(tpath);
283 thost = tport = tpath = NULL;
284 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
285 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
286 goto end;
287 }
288 thost = host;
289 tport = port;
290 tpath = path;
291 break;
292 case OPT_HOST:
293 host = opt_arg();
294 break;
295 case OPT_PORT:
296 port = opt_arg();
297 break;
298 case OPT_IGNORE_ERR:
299 ignore_err = 1;
300 break;
301 case OPT_NOVERIFY:
302 noverify = 1;
303 break;
304 case OPT_NONCE:
305 add_nonce = 2;
306 break;
307 case OPT_NO_NONCE:
308 add_nonce = 0;
309 break;
310 case OPT_RESP_NO_CERTS:
311 rflags |= OCSP_NOCERTS;
312 break;
313 case OPT_RESP_KEY_ID:
314 rflags |= OCSP_RESPID_KEY;
315 break;
316 case OPT_NO_CERTS:
317 sign_flags |= OCSP_NOCERTS;
318 break;
319 case OPT_NO_SIGNATURE_VERIFY:
320 verify_flags |= OCSP_NOSIGS;
321 break;
322 case OPT_NO_CERT_VERIFY:
323 verify_flags |= OCSP_NOVERIFY;
324 break;
325 case OPT_NO_CHAIN:
326 verify_flags |= OCSP_NOCHAIN;
327 break;
328 case OPT_NO_CERT_CHECKS:
329 verify_flags |= OCSP_NOCHECKS;
330 break;
331 case OPT_NO_EXPLICIT:
332 verify_flags |= OCSP_NOEXPLICIT;
333 break;
334 case OPT_TRUST_OTHER:
335 verify_flags |= OCSP_TRUSTOTHER;
336 break;
337 case OPT_NO_INTERN:
338 verify_flags |= OCSP_NOINTERN;
339 break;
340 case OPT_BADSIG:
341 badsig = 1;
342 break;
343 case OPT_TEXT:
344 req_text = resp_text = 1;
345 break;
346 case OPT_REQ_TEXT:
347 req_text = 1;
348 break;
349 case OPT_RESP_TEXT:
350 resp_text = 1;
351 break;
352 case OPT_REQIN:
353 reqin = opt_arg();
354 break;
355 case OPT_RESPIN:
356 respin = opt_arg();
357 break;
358 case OPT_SIGNER:
359 signfile = opt_arg();
360 break;
361 case OPT_VAFILE:
362 verify_certfile = opt_arg();
363 verify_flags |= OCSP_TRUSTOTHER;
364 break;
365 case OPT_SIGN_OTHER:
366 sign_certfile = opt_arg();
367 break;
368 case OPT_VERIFY_OTHER:
369 verify_certfile = opt_arg();
370 break;
371 case OPT_CAFILE:
372 CAfile = opt_arg();
373 break;
374 case OPT_CAPATH:
375 CApath = opt_arg();
376 break;
377 case OPT_NOCAFILE:
378 noCAfile = 1;
379 break;
380 case OPT_NOCAPATH:
381 noCApath = 1;
382 break;
383 case OPT_V_CASES:
384 if (!opt_verify(o, vpm))
385 goto end;
386 vpmtouched++;
387 break;
388 case OPT_VALIDITY_PERIOD:
389 opt_long(opt_arg(), &nsec);
390 break;
391 case OPT_STATUS_AGE:
392 opt_long(opt_arg(), &maxage);
393 break;
394 case OPT_SIGNKEY:
395 keyfile = opt_arg();
396 break;
397 case OPT_REQOUT:
398 reqout = opt_arg();
399 break;
400 case OPT_RESPOUT:
401 respout = opt_arg();
402 break;
403 case OPT_PATH:
404 path = opt_arg();
405 break;
406 case OPT_ISSUER:
407 X509_free(issuer);
408 issuer = load_cert(opt_arg(), FORMAT_PEM,
409 NULL, NULL, "issuer certificate");
410 if (issuer == NULL)
411 goto end;
412 if ((issuers = sk_X509_new_null()) == NULL)
413 goto end;
414 sk_X509_push(issuers, issuer);
415 break;
416 case OPT_CERT:
417 X509_free(cert);
418 cert = load_cert(opt_arg(), FORMAT_PEM,
419 NULL, NULL, "certificate");
420 if (cert == NULL)
421 goto end;
422 if (cert_id_md == NULL)
423 cert_id_md = EVP_sha1();
424 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
425 goto end;
426 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
427 goto end;
428 break;
429 case OPT_SERIAL:
430 if (cert_id_md == NULL)
431 cert_id_md = EVP_sha1();
432 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
433 goto end;
434 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
435 goto end;
436 break;
437 case OPT_INDEX:
438 ridx_filename = opt_arg();
439 break;
440 case OPT_CA:
441 rca_filename = opt_arg();
442 break;
443 case OPT_NMIN:
444 opt_int(opt_arg(), &nmin);
445 if (ndays == -1)
446 ndays = 0;
447 break;
448 case OPT_REQUEST:
449 opt_int(opt_arg(), &accept_count);
450 break;
451 case OPT_NDAYS:
452 ndays = atoi(opt_arg());
453 break;
454 case OPT_RSIGNER:
455 rsignfile = opt_arg();
456 break;
457 case OPT_RKEY:
458 rkeyfile = opt_arg();
459 break;
460 case OPT_ROTHER:
461 rcertfile = opt_arg();
462 break;
463 case OPT_RMD:
464 if (!opt_md(opt_arg(), &rsign_md))
465 goto end;
466 break;
467 case OPT_HEADER:
468 header = opt_arg();
469 value = strchr(header, '=');
470 if (value == NULL) {
471 BIO_printf(bio_err, "Missing = in header key=value\n");
472 goto opthelp;
473 }
474 *value++ = '\0';
475 if (!X509V3_add_value(header, value, &headers))
476 goto end;
477 break;
478 case OPT_MD:
479 if (cert_id_md != NULL) {
480 BIO_printf(bio_err,
481 "%s: Digest must be before -cert or -serial\n",
482 prog);
483 goto opthelp;
484 }
485 if (!opt_md(opt_unknown(), &cert_id_md))
486 goto opthelp;
487 break;
488 }
489 }
490 argc = opt_num_rest();
491 argv = opt_rest();
492
493 /* Have we anything to do? */
494 if (!req && !reqin && !respin && !(port && ridx_filename))
495 goto opthelp;
496
497 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
498 if (out == NULL)
499 goto end;
500
501 if (!req && (add_nonce != 2))
502 add_nonce = 0;
503
504 if (!req && reqin) {
505 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
506 if (derbio == NULL)
507 goto end;
508 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
509 BIO_free(derbio);
510 if (!req) {
511 BIO_printf(bio_err, "Error reading OCSP request\n");
512 goto end;
513 }
514 }
515
516 if (!req && port) {
517 acbio = init_responder(port);
518 if (!acbio)
519 goto end;
520 }
521
522 if (rsignfile && !rdb) {
523 if (!rkeyfile)
524 rkeyfile = rsignfile;
525 rsigner = load_cert(rsignfile, FORMAT_PEM,
526 NULL, NULL, "responder certificate");
527 if (!rsigner) {
528 BIO_printf(bio_err, "Error loading responder certificate\n");
529 goto end;
530 }
531 rca_cert = load_cert(rca_filename, FORMAT_PEM,
532 NULL, NULL, "CA certificate");
533 if (rcertfile) {
534 rother = load_certs(rcertfile, FORMAT_PEM,
535 NULL, NULL, "responder other certificates");
536 if (!rother)
537 goto end;
538 }
539 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
540 "responder private key");
541 if (!rkey)
542 goto end;
543 }
544 if (acbio)
545 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
546
547 redo_accept:
548
549 if (acbio) {
550 if (!do_responder(&req, &cbio, acbio, port))
551 goto end;
552 if (!req) {
553 resp =
554 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
555 NULL);
556 send_ocsp_response(cbio, resp);
557 goto done_resp;
558 }
559 }
560
561 if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
562 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
563 goto end;
564 }
565
566 if (req && add_nonce)
567 OCSP_request_add1_nonce(req, NULL, -1);
568
569 if (signfile) {
570 if (!keyfile)
571 keyfile = signfile;
572 signer = load_cert(signfile, FORMAT_PEM,
573 NULL, NULL, "signer certificate");
574 if (!signer) {
575 BIO_printf(bio_err, "Error loading signer certificate\n");
576 goto end;
577 }
578 if (sign_certfile) {
579 sign_other = load_certs(sign_certfile, FORMAT_PEM,
580 NULL, NULL, "signer certificates");
581 if (!sign_other)
582 goto end;
583 }
584 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
585 "signer private key");
586 if (!key)
587 goto end;
588
589 if (!OCSP_request_sign
590 (req, signer, key, NULL, sign_other, sign_flags)) {
591 BIO_printf(bio_err, "Error signing OCSP request\n");
592 goto end;
593 }
594 }
595
596 if (req_text && req)
597 OCSP_REQUEST_print(out, req, 0);
598
599 if (reqout) {
600 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
601 if (derbio == NULL)
602 goto end;
603 i2d_OCSP_REQUEST_bio(derbio, req);
604 BIO_free(derbio);
605 }
606
607 if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
608 BIO_printf(bio_err,
609 "Need a responder certificate, key and CA for this operation!\n");
610 goto end;
611 }
612
613 if (ridx_filename && !rdb) {
614 rdb = load_index(ridx_filename, NULL);
615 if (!rdb)
616 goto end;
617 if (!index_index(rdb))
618 goto end;
619 }
620
621 if (rdb) {
622 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
623 rsign_md, rother, rflags, nmin, ndays, badsig);
624 if (cbio)
625 send_ocsp_response(cbio, resp);
626 } else if (host) {
627 # ifndef OPENSSL_NO_SOCK
628 resp = process_responder(req, host, path,
629 port, use_ssl, headers, req_timeout);
630 if (!resp)
631 goto end;
632 # else
633 BIO_printf(bio_err,
634 "Error creating connect BIO - sockets not supported.\n");
635 goto end;
636 # endif
637 } else if (respin) {
638 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
639 if (derbio == NULL)
640 goto end;
641 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
642 BIO_free(derbio);
643 if (!resp) {
644 BIO_printf(bio_err, "Error reading OCSP response\n");
645 goto end;
646 }
647 } else {
648 ret = 0;
649 goto end;
650 }
651
652 done_resp:
653
654 if (respout) {
655 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
656 if (derbio == NULL)
657 goto end;
658 i2d_OCSP_RESPONSE_bio(derbio, resp);
659 BIO_free(derbio);
660 }
661
662 i = OCSP_response_status(resp);
663 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
664 BIO_printf(out, "Responder Error: %s (%d)\n",
665 OCSP_response_status_str(i), i);
666 if (ignore_err)
667 goto redo_accept;
668 ret = 0;
669 goto end;
670 }
671
672 if (resp_text)
673 OCSP_RESPONSE_print(out, resp, 0);
674
675 /* If running as responder don't verify our own response */
676 if (cbio) {
677 /* If not unlimited, see if we took all we should. */
678 if (accept_count != -1 && --accept_count <= 0) {
679 ret = 0;
680 goto end;
681 }
682 BIO_free_all(cbio);
683 cbio = NULL;
684 OCSP_REQUEST_free(req);
685 req = NULL;
686 OCSP_RESPONSE_free(resp);
687 resp = NULL;
688 goto redo_accept;
689 }
690 if (ridx_filename) {
691 ret = 0;
692 goto end;
693 }
694
695 if (!store) {
696 store = setup_verify(CAfile, CApath, noCAfile, noCApath);
697 if (!store)
698 goto end;
699 }
700 if (vpmtouched)
701 X509_STORE_set1_param(store, vpm);
702 if (verify_certfile) {
703 verify_other = load_certs(verify_certfile, FORMAT_PEM,
704 NULL, NULL, "validator certificate");
705 if (!verify_other)
706 goto end;
707 }
708
709 bs = OCSP_response_get1_basic(resp);
710 if (!bs) {
711 BIO_printf(bio_err, "Error parsing response\n");
712 goto end;
713 }
714
715 ret = 0;
716
717 if (!noverify) {
718 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
719 if (i == -1)
720 BIO_printf(bio_err, "WARNING: no nonce in response\n");
721 else {
722 BIO_printf(bio_err, "Nonce Verify error\n");
723 ret = 1;
724 goto end;
725 }
726 }
727
728 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
729 if (i <= 0 && issuers) {
730 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
731 if (i > 0)
732 ERR_clear_error();
733 }
734 if (i <= 0) {
735 BIO_printf(bio_err, "Response Verify Failure\n");
736 ERR_print_errors(bio_err);
737 ret = 1;
738 } else
739 BIO_printf(bio_err, "Response verify OK\n");
740
741 }
742
743 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
744
745 end:
746 ERR_print_errors(bio_err);
747 X509_free(signer);
748 X509_STORE_free(store);
749 X509_VERIFY_PARAM_free(vpm);
750 EVP_PKEY_free(key);
751 EVP_PKEY_free(rkey);
752 X509_free(cert);
753 X509_free(rsigner);
754 X509_free(rca_cert);
755 free_index(rdb);
756 BIO_free_all(cbio);
757 BIO_free_all(acbio);
758 BIO_free(out);
759 OCSP_REQUEST_free(req);
760 OCSP_RESPONSE_free(resp);
761 OCSP_BASICRESP_free(bs);
762 sk_OPENSSL_STRING_free(reqnames);
763 sk_OCSP_CERTID_free(ids);
764 sk_X509_pop_free(sign_other, X509_free);
765 sk_X509_pop_free(verify_other, X509_free);
766 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
767 OPENSSL_free(thost);
768 OPENSSL_free(tport);
769 OPENSSL_free(tpath);
770
771 return (ret);
772 }
773
774 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
775 const EVP_MD *cert_id_md, X509 *issuer,
776 STACK_OF(OCSP_CERTID) *ids)
777 {
778 OCSP_CERTID *id;
779 if (!issuer) {
780 BIO_printf(bio_err, "No issuer certificate specified\n");
781 return 0;
782 }
783 if (!*req)
784 *req = OCSP_REQUEST_new();
785 if (!*req)
786 goto err;
787 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
788 if (!id || !sk_OCSP_CERTID_push(ids, id))
789 goto err;
790 if (!OCSP_request_add0_id(*req, id))
791 goto err;
792 return 1;
793
794 err:
795 BIO_printf(bio_err, "Error Creating OCSP request\n");
796 return 0;
797 }
798
799 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
800 const EVP_MD *cert_id_md, X509 *issuer,
801 STACK_OF(OCSP_CERTID) *ids)
802 {
803 OCSP_CERTID *id;
804 X509_NAME *iname;
805 ASN1_BIT_STRING *ikey;
806 ASN1_INTEGER *sno;
807 if (!issuer) {
808 BIO_printf(bio_err, "No issuer certificate specified\n");
809 return 0;
810 }
811 if (!*req)
812 *req = OCSP_REQUEST_new();
813 if (!*req)
814 goto err;
815 iname = X509_get_subject_name(issuer);
816 ikey = X509_get0_pubkey_bitstr(issuer);
817 sno = s2i_ASN1_INTEGER(NULL, serial);
818 if (!sno) {
819 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
820 return 0;
821 }
822 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
823 ASN1_INTEGER_free(sno);
824 if (!id || !sk_OCSP_CERTID_push(ids, id))
825 goto err;
826 if (!OCSP_request_add0_id(*req, id))
827 goto err;
828 return 1;
829
830 err:
831 BIO_printf(bio_err, "Error Creating OCSP request\n");
832 return 0;
833 }
834
835 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
836 STACK_OF(OPENSSL_STRING) *names,
837 STACK_OF(OCSP_CERTID) *ids, long nsec,
838 long maxage)
839 {
840 OCSP_CERTID *id;
841 char *name;
842 int i, status, reason;
843 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
844
845 if (!bs || !req || !sk_OPENSSL_STRING_num(names)
846 || !sk_OCSP_CERTID_num(ids))
847 return;
848
849 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
850 id = sk_OCSP_CERTID_value(ids, i);
851 name = sk_OPENSSL_STRING_value(names, i);
852 BIO_printf(out, "%s: ", name);
853
854 if (!OCSP_resp_find_status(bs, id, &status, &reason,
855 &rev, &thisupd, &nextupd)) {
856 BIO_puts(out, "ERROR: No Status found.\n");
857 continue;
858 }
859
860 /*
861 * Check validity: if invalid write to output BIO so we know which
862 * response this refers to.
863 */
864 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
865 BIO_puts(out, "WARNING: Status times invalid.\n");
866 ERR_print_errors(out);
867 }
868 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
869
870 BIO_puts(out, "\tThis Update: ");
871 ASN1_GENERALIZEDTIME_print(out, thisupd);
872 BIO_puts(out, "\n");
873
874 if (nextupd) {
875 BIO_puts(out, "\tNext Update: ");
876 ASN1_GENERALIZEDTIME_print(out, nextupd);
877 BIO_puts(out, "\n");
878 }
879
880 if (status != V_OCSP_CERTSTATUS_REVOKED)
881 continue;
882
883 if (reason != -1)
884 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
885
886 BIO_puts(out, "\tRevocation Time: ");
887 ASN1_GENERALIZEDTIME_print(out, rev);
888 BIO_puts(out, "\n");
889 }
890 }
891
892 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
893 CA_DB *db, X509 *ca, X509 *rcert,
894 EVP_PKEY *rkey, const EVP_MD *rmd,
895 STACK_OF(X509) *rother, unsigned long flags,
896 int nmin, int ndays, int badsig)
897 {
898 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
899 OCSP_CERTID *cid, *ca_id = NULL;
900 OCSP_BASICRESP *bs = NULL;
901 int i, id_count;
902
903 id_count = OCSP_request_onereq_count(req);
904
905 if (id_count <= 0) {
906 *resp =
907 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
908 goto end;
909 }
910
911 bs = OCSP_BASICRESP_new();
912 thisupd = X509_gmtime_adj(NULL, 0);
913 if (ndays != -1)
914 nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24);
915
916 /* Examine each certificate id in the request */
917 for (i = 0; i < id_count; i++) {
918 OCSP_ONEREQ *one;
919 ASN1_INTEGER *serial;
920 char **inf;
921 ASN1_OBJECT *cert_id_md_oid;
922 const EVP_MD *cert_id_md;
923 one = OCSP_request_onereq_get0(req, i);
924 cid = OCSP_onereq_get0_id(one);
925
926 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
927
928 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
929 if (!cert_id_md) {
930 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
931 NULL);
932 goto end;
933 }
934 OCSP_CERTID_free(ca_id);
935 ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
936
937 /* Is this request about our CA? */
938 if (OCSP_id_issuer_cmp(ca_id, cid)) {
939 OCSP_basic_add1_status(bs, cid,
940 V_OCSP_CERTSTATUS_UNKNOWN,
941 0, NULL, thisupd, nextupd);
942 continue;
943 }
944 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
945 inf = lookup_serial(db, serial);
946 if (!inf)
947 OCSP_basic_add1_status(bs, cid,
948 V_OCSP_CERTSTATUS_UNKNOWN,
949 0, NULL, thisupd, nextupd);
950 else if (inf[DB_type][0] == DB_TYPE_VAL)
951 OCSP_basic_add1_status(bs, cid,
952 V_OCSP_CERTSTATUS_GOOD,
953 0, NULL, thisupd, nextupd);
954 else if (inf[DB_type][0] == DB_TYPE_REV) {
955 ASN1_OBJECT *inst = NULL;
956 ASN1_TIME *revtm = NULL;
957 ASN1_GENERALIZEDTIME *invtm = NULL;
958 OCSP_SINGLERESP *single;
959 int reason = -1;
960 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
961 single = OCSP_basic_add1_status(bs, cid,
962 V_OCSP_CERTSTATUS_REVOKED,
963 reason, revtm, thisupd, nextupd);
964 if (invtm)
965 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
966 invtm, 0, 0);
967 else if (inst)
968 OCSP_SINGLERESP_add1_ext_i2d(single,
969 NID_hold_instruction_code, inst,
970 0, 0);
971 ASN1_OBJECT_free(inst);
972 ASN1_TIME_free(revtm);
973 ASN1_GENERALIZEDTIME_free(invtm);
974 }
975 }
976
977 OCSP_copy_nonce(bs, req);
978
979 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
980
981 if (badsig) {
982 ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
983 unsigned char *sigptr = ASN1_STRING_data(sig);
984 sigptr[ASN1_STRING_length(sig) - 1] ^= 0x1;
985 }
986
987 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
988
989 end:
990 ASN1_TIME_free(thisupd);
991 ASN1_TIME_free(nextupd);
992 OCSP_CERTID_free(ca_id);
993 OCSP_BASICRESP_free(bs);
994 }
995
996 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
997 {
998 int i;
999 BIGNUM *bn = NULL;
1000 char *itmp, *row[DB_NUMBER], **rrow;
1001 for (i = 0; i < DB_NUMBER; i++)
1002 row[i] = NULL;
1003 bn = ASN1_INTEGER_to_BN(ser, NULL);
1004 OPENSSL_assert(bn); /* FIXME: should report an error at this
1005 * point and abort */
1006 if (BN_is_zero(bn))
1007 itmp = BUF_strdup("00");
1008 else
1009 itmp = BN_bn2hex(bn);
1010 row[DB_serial] = itmp;
1011 BN_free(bn);
1012 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1013 OPENSSL_free(itmp);
1014 return rrow;
1015 }
1016
1017 /* Quick and dirty OCSP server: read in and parse input request */
1018
1019 static BIO *init_responder(const char *port)
1020 {
1021 BIO *acbio = NULL, *bufbio = NULL;
1022
1023 # ifdef OPENSSL_NO_SOCK
1024 BIO_printf(bio_err,
1025 "Error setting up accept BIO - sockets not supported.\n");
1026 return NULL;
1027 # endif
1028 bufbio = BIO_new(BIO_f_buffer());
1029 if (!bufbio)
1030 goto err;
1031 acbio = BIO_new(BIO_s_accept());
1032 if (acbio == NULL
1033 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1034 || BIO_set_accept_port(acbio, port) < 0) {
1035 BIO_printf(bio_err, "Error setting up accept BIO\n");
1036 ERR_print_errors(bio_err);
1037 goto err;
1038 }
1039
1040 BIO_set_accept_bios(acbio, bufbio);
1041 bufbio = NULL;
1042 if (BIO_do_accept(acbio) <= 0) {
1043 BIO_printf(bio_err, "Error starting accept\n");
1044 ERR_print_errors(bio_err);
1045 goto err;
1046 }
1047
1048 return acbio;
1049
1050 err:
1051 BIO_free_all(acbio);
1052 BIO_free(bufbio);
1053 return NULL;
1054 }
1055
1056
1057 /*
1058 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1059 */
1060 static int urldecode(char *p)
1061 {
1062 unsigned char *out = (unsigned char *)p;
1063 unsigned char *save = out;
1064
1065 for (; *p; p++) {
1066 if (*p != '%')
1067 *out++ = *p;
1068 else if (isxdigit(p[1]) && isxdigit(p[2])) {
1069 *out++ = (app_hex(p[1]) << 4) | app_hex(p[2]);
1070 p += 2;
1071 }
1072 else
1073 return -1;
1074 }
1075 *out = '\0';
1076 return (int)(out - save);
1077 }
1078
1079 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1080 const char *port)
1081 {
1082 int len;
1083 OCSP_REQUEST *req = NULL;
1084 char inbuf[2048], reqbuf[2048];
1085 char *p, *q;
1086 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
1087
1088 if (BIO_do_accept(acbio) <= 0) {
1089 BIO_printf(bio_err, "Error accepting connection\n");
1090 ERR_print_errors(bio_err);
1091 return 0;
1092 }
1093
1094 cbio = BIO_pop(acbio);
1095 *pcbio = cbio;
1096
1097 /* Read the request line. */
1098 len = BIO_gets(cbio, reqbuf, sizeof reqbuf);
1099 if (len <= 0)
1100 return 1;
1101 if (strncmp(reqbuf, "GET ", 4) == 0) {
1102 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
1103 for (p = reqbuf + 4; *p == ' '; ++p)
1104 continue;
1105 if (*p != '/') {
1106 BIO_printf(bio_err, "Invalid request -- bad URL\n");
1107 return 1;
1108 }
1109 p++;
1110
1111 /* Splice off the HTTP version identifier. */
1112 for (q = p; *q; q++)
1113 if (*q == ' ')
1114 break;
1115 if (strncmp(q, " HTTP/1.", 8) != 0) {
1116 BIO_printf(bio_err, "Invalid request -- bad HTTP vesion\n");
1117 return 1;
1118 }
1119 *q = '\0';
1120 len = urldecode(p);
1121 if (len <= 0) {
1122 BIO_printf(bio_err, "Invalid request -- bad URL encoding\n");
1123 return 1;
1124 }
1125 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1126 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
1127 BIO_printf(bio_err, "Could not allocate memory\n");
1128 ERR_print_errors(bio_err);
1129 return 1;
1130 }
1131 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1132 getbio = BIO_push(b64, getbio);
1133 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
1134 BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n");
1135 return 1;
1136 }
1137
1138 /* Read and skip past the headers. */
1139 for (;;) {
1140 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1141 if (len <= 0)
1142 return 1;
1143 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1144 break;
1145 }
1146
1147 /* Try to read OCSP request */
1148 if (getbio) {
1149 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1150 BIO_free_all(getbio);
1151 } else
1152 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1153
1154 if (!req) {
1155 BIO_printf(bio_err, "Error parsing OCSP request\n");
1156 ERR_print_errors(bio_err);
1157 }
1158
1159 *preq = req;
1160
1161 return 1;
1162
1163 }
1164
1165 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1166 {
1167 char http_resp[] =
1168 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1169 "Content-Length: %d\r\n\r\n";
1170 if (!cbio)
1171 return 0;
1172 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1173 i2d_OCSP_RESPONSE_bio(cbio, resp);
1174 (void)BIO_flush(cbio);
1175 return 1;
1176 }
1177
1178 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *path,
1179 const STACK_OF(CONF_VALUE) *headers,
1180 OCSP_REQUEST *req, int req_timeout)
1181 {
1182 int fd;
1183 int rv;
1184 int i;
1185 OCSP_REQ_CTX *ctx = NULL;
1186 OCSP_RESPONSE *rsp = NULL;
1187 fd_set confds;
1188 struct timeval tv;
1189
1190 if (req_timeout != -1)
1191 BIO_set_nbio(cbio, 1);
1192
1193 rv = BIO_do_connect(cbio);
1194
1195 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1196 BIO_puts(bio_err, "Error connecting BIO\n");
1197 return NULL;
1198 }
1199
1200 if (BIO_get_fd(cbio, &fd) <= 0) {
1201 BIO_puts(bio_err, "Can't get connection fd\n");
1202 goto err;
1203 }
1204
1205 if (req_timeout != -1 && rv <= 0) {
1206 FD_ZERO(&confds);
1207 openssl_fdset(fd, &confds);
1208 tv.tv_usec = 0;
1209 tv.tv_sec = req_timeout;
1210 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1211 if (rv == 0) {
1212 BIO_puts(bio_err, "Timeout on connect\n");
1213 return NULL;
1214 }
1215 }
1216
1217 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1218 if (!ctx)
1219 return NULL;
1220
1221 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1222 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1223 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1224 goto err;
1225 }
1226
1227 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1228 goto err;
1229
1230 for (;;) {
1231 rv = OCSP_sendreq_nbio(&rsp, ctx);
1232 if (rv != -1)
1233 break;
1234 if (req_timeout == -1)
1235 continue;
1236 FD_ZERO(&confds);
1237 openssl_fdset(fd, &confds);
1238 tv.tv_usec = 0;
1239 tv.tv_sec = req_timeout;
1240 if (BIO_should_read(cbio))
1241 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1242 else if (BIO_should_write(cbio))
1243 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1244 else {
1245 BIO_puts(bio_err, "Unexpected retry condition\n");
1246 goto err;
1247 }
1248 if (rv == 0) {
1249 BIO_puts(bio_err, "Timeout on request\n");
1250 break;
1251 }
1252 if (rv == -1) {
1253 BIO_puts(bio_err, "Select error\n");
1254 break;
1255 }
1256
1257 }
1258 err:
1259 OCSP_REQ_CTX_free(ctx);
1260
1261 return rsp;
1262 }
1263
1264 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1265 const char *host, const char *path,
1266 const char *port, int use_ssl,
1267 STACK_OF(CONF_VALUE) *headers,
1268 int req_timeout)
1269 {
1270 BIO *cbio = NULL;
1271 SSL_CTX *ctx = NULL;
1272 OCSP_RESPONSE *resp = NULL;
1273 int found, i;
1274
1275 cbio = BIO_new_connect(host);
1276 if (!cbio) {
1277 BIO_printf(bio_err, "Error creating connect BIO\n");
1278 goto end;
1279 }
1280 if (port)
1281 BIO_set_conn_port(cbio, port);
1282 if (use_ssl == 1) {
1283 BIO *sbio;
1284 ctx = SSL_CTX_new(TLS_client_method());
1285 if (ctx == NULL) {
1286 BIO_printf(bio_err, "Error creating SSL context.\n");
1287 goto end;
1288 }
1289 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1290 sbio = BIO_new_ssl(ctx, 1);
1291 cbio = BIO_push(sbio, cbio);
1292 }
1293 for (found = i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1294 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1295 if (strcasecmp("host", hdr->name) == 0) {
1296 found = 1;
1297 break;
1298 }
1299 }
1300
1301 if (!found && !X509V3_add_value("Host", host, &headers))
1302 BIO_printf(bio_err, "Error setting HTTP Host header\n");
1303
1304 resp = query_responder(cbio, path, headers, req, req_timeout);
1305 if (!resp)
1306 BIO_printf(bio_err, "Error querying OCSP responder\n");
1307 end:
1308 BIO_free_all(cbio);
1309 SSL_CTX_free(ctx);
1310 return resp;
1311 }
1312
1313 #endif