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