]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ocsp.c
Fix coverity issues CID 1457745...1457752, 1457853, 1457854
[thirdparty/openssl.git] / apps / ocsp.c
CommitLineData
0f113f3e 1/*
3c7d0945 2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
5782ceb2 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
5782ceb2
DSH
8 */
9
3e41ac35
MC
10#include <openssl/opensslconf.h>
11
12#ifdef OPENSSL_NO_OCSP
13NON_EMPTY_TRANSLATION_UNIT
14#else
0f113f3e
MC
15# ifdef OPENSSL_SYS_VMS
16# define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
17 * on OpenVMS */
18# endif
19
0f113f3e
MC
20# include <stdio.h>
21# include <stdlib.h>
22# include <string.h>
23# include <time.h>
98cd49db 24# include <ctype.h>
3e41ac35
MC
25
26/* Needs to be included before the openssl headers */
27# include "apps.h"
dab2cd68 28# include "progs.h"
3e3c7c36 29# include "internal/sockets.h"
0f113f3e
MC
30# include <openssl/e_os2.h>
31# include <openssl/crypto.h>
32# include <openssl/err.h>
33# include <openssl/ssl.h>
34# include <openssl/evp.h>
35# include <openssl/bn.h>
36# include <openssl/x509v3.h>
3e3c7c36
VD
37# include <openssl/rand.h>
38
ca811248
RL
39#ifndef HAVE_FORK
40# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
41# define HAVE_FORK 0
42# else
43# define HAVE_FORK 1
44# endif
45#endif
46
47#if HAVE_FORK
48# undef NO_FORK
49#else
50# define NO_FORK
51#endif
52
53# if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \
1a54618b 54 && !defined(OPENSSL_NO_POSIX_IO)
3e3c7c36
VD
55# define OCSP_DAEMON
56# include <sys/types.h>
57# include <sys/wait.h>
58# include <syslog.h>
59# include <signal.h>
60# define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */
61# else
62# undef LOG_INFO
63# undef LOG_WARNING
64# undef LOG_ERR
65# define LOG_INFO 0
66# define LOG_WARNING 1
67# define LOG_ERR 2
68# endif
0f113f3e 69
5c8b7b4c
KT
70# if defined(OPENSSL_SYS_VXWORKS)
71/* not supported */
72int setpgid(pid_t pid, pid_t pgid)
73{
74 errno = ENOSYS;
75 return 0;
76}
77/* not supported */
78pid_t fork(void)
79{
80 errno = ENOSYS;
81 return (pid_t) -1;
82}
83# endif
0f113f3e 84/* Maximum leeway in validity period: default 5 minutes */
7e1b7485 85# define MAX_VALIDITY_PERIOD (5 * 60)
0f113f3e
MC
86
87static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
88 const EVP_MD *cert_id_md, X509 *issuer,
89 STACK_OF(OCSP_CERTID) *ids);
90static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
91 const EVP_MD *cert_id_md, X509 *issuer,
92 STACK_OF(OCSP_CERTID) *ids);
7e1b7485 93static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
0f113f3e
MC
94 STACK_OF(OPENSSL_STRING) *names,
95 STACK_OF(OCSP_CERTID) *ids, long nsec,
96 long maxage);
b4dd21a7 97static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
3bb0f989 98 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
0f113f3e 99 EVP_PKEY *rkey, const EVP_MD *md,
b4dd21a7 100 STACK_OF(OPENSSL_STRING) *sigopts,
0f113f3e 101 STACK_OF(X509) *rother, unsigned long flags,
0770c882
TS
102 int nmin, int ndays, int badsig,
103 const EVP_MD *resp_md);
ee306a13 104
f85b68cd 105static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
c45a48c1 106static BIO *init_responder(const char *port);
3e3c7c36 107static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, int timeout);
ee306a13 108static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
3e3c7c36
VD
109static void log_message(int level, const char *fmt, ...);
110static char *prog;
111static int multi = 0;
112
113# ifdef OCSP_DAEMON
114static int acfd = (int) INVALID_SOCKET;
115static int index_changed(CA_DB *);
116static void spawn_loop(void);
117static int print_syslog(const char *str, size_t len, void *levPtr);
e9b95e42 118static void socket_timeout(int signum);
3e3c7c36 119# endif
f9e55034
MC
120
121# ifndef OPENSSL_NO_SOCK
76e0cd12
DSH
122static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
123 const char *path,
0f113f3e
MC
124 const STACK_OF(CONF_VALUE) *headers,
125 OCSP_REQUEST *req, int req_timeout);
f9e55034 126# endif
ee306a13 127
7e1b7485
RS
128typedef enum OPTION_choice {
129 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
130 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
131 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
132 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
133 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
134 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
135 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
136 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
fd3397fc
RL
137 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
138 OPT_NOCAPATH, OPT_NOCASTORE,
7e1b7485 139 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
c6724060 140 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
7e1b7485 141 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
04940147 142 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
1cf20ca3 143 OPT_PASSIN,
0770c882 144 OPT_RCID,
7e1b7485 145 OPT_V_ENUM,
3e3c7c36
VD
146 OPT_MD,
147 OPT_MULTI
7e1b7485
RS
148} OPTION_CHOICE;
149
44c83ebd 150const OPTIONS ocsp_options[] = {
5388f986 151 OPT_SECTION("General"),
7e1b7485 152 {"help", OPT_HELP, '-', "Display this summary"},
12d56b29 153 {"ignore_err", OPT_IGNORE_ERR, '-',
bbe9c3d5 154 "Ignore error on OCSP request or response and continue running"},
5388f986
RS
155 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
156 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
157 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
158 {"no-CAfile", OPT_NOCAFILE, '-',
159 "Do not load the default certificates file"},
160 {"no-CApath", OPT_NOCAPATH, '-',
161 "Do not load certificates from the default certificates directory"},
162 {"no-CAstore", OPT_NOCAPATH, '-',
163 "Do not load certificates from the default certificates store"},
164
165 OPT_SECTION("Responder"),
166 {"timeout", OPT_TIMEOUT, 'p',
167 "Connection timeout (in seconds) to the OCSP responder"},
7e1b7485
RS
168 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
169 "Don't include any certificates in response"},
3e3c7c36
VD
170# ifdef OCSP_DAEMON
171 {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
172# endif
7e1b7485
RS
173 {"no_certs", OPT_NO_CERTS, '-',
174 "Don't include any certificates in signed request"},
5388f986
RS
175 {"badsig", OPT_BADSIG, '-',
176 "Corrupt last byte of loaded OSCP response signature (for test)"},
177 {"CA", OPT_CA, '<', "CA certificate"},
178 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
179 {"nrequest", OPT_REQUEST, 'p',
180 "Number of requests to accept (default unlimited)"},
181 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
182 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
183 {"sign_other", OPT_SIGN_OTHER, '<',
184 "Additional certificates to include in signed request"},
185 {"index", OPT_INDEX, '<', "Certificate status index file"},
186 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
187 {"rsigner", OPT_RSIGNER, '<',
188 "Responder certificate to sign responses with"},
189 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
1cf20ca3 190 {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
5388f986
RS
191 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
192 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
193 {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
194 {"header", OPT_HEADER, 's', "key=value header to add"},
195 {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
196 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
197
198 OPT_SECTION("Client"),
199 {"url", OPT_URL, 's', "Responder URL"},
200 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
201 {"port", OPT_PORT, 'p', "Port to run responder on"},
202 {"out", OPT_OUTFILE, '>', "Output filename"},
203 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
204 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
205 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
7e1b7485
RS
206 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
207 "Don't check signature on response"},
5388f986
RS
208 {"resp_key_id", OPT_RESP_KEY_ID, '-',
209 "Identify response by signing certificate key ID"},
7e1b7485
RS
210 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
211 "Don't check signing certificate"},
5388f986
RS
212 {"text", OPT_TEXT, '-', "Print text form of request and response"},
213 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
214 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
7e1b7485
RS
215 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
216 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
217 "Don't do additional checks on signing certificate"},
12d56b29
F
218 {"no_explicit", OPT_NO_EXPLICIT, '-',
219 "Do not explicitly check the chain, just verify the root"},
7e1b7485
RS
220 {"trust_other", OPT_TRUST_OTHER, '-',
221 "Don't verify additional certificates"},
222 {"no_intern", OPT_NO_INTERN, '-',
223 "Don't search certificates contained in response for signer"},
7e1b7485 224 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
7e1b7485 225 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
7e1b7485
RS
226 {"verify_other", OPT_VERIFY_OTHER, '<',
227 "Additional certificates to search for signer"},
5388f986
RS
228 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
229 {"cert", OPT_CERT, '<', "Certificate to check"},
230 {"serial", OPT_SERIAL, 's', "Serial number to check"},
7e1b7485
RS
231 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
232 "Maximum validity discrepancy in seconds"},
7e1b7485
RS
233 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
234 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
235 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
c6724060 236 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
5388f986
RS
237 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
238
7e1b7485
RS
239 OPT_V_OPTIONS,
240 {NULL}
241};
242
243int ocsp_main(int argc, char **argv)
0f113f3e 244{
7e1b7485
RS
245 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
246 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
b4dd21a7 247 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
6302bbd2 248 int trailing_md = 0;
7e1b7485
RS
249 CA_DB *rdb = NULL;
250 EVP_PKEY *key = NULL, *rkey = NULL;
251 OCSP_BASICRESP *bs = NULL;
0f113f3e
MC
252 OCSP_REQUEST *req = NULL;
253 OCSP_RESPONSE *resp = NULL;
7e1b7485
RS
254 STACK_OF(CONF_VALUE) *headers = NULL;
255 STACK_OF(OCSP_CERTID) *ids = NULL;
256 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
257 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
88806cfc 258 STACK_OF(X509) *issuers = NULL;
3bb0f989
TS
259 X509 *issuer = NULL, *cert = NULL;
260 STACK_OF(X509) *rca_cert = NULL;
0770c882 261 const EVP_MD *resp_certid_md = NULL;
0f113f3e 262 X509 *signer = NULL, *rsigner = NULL;
0f113f3e
MC
263 X509_STORE *store = NULL;
264 X509_VERIFY_PARAM *vpm = NULL;
fd3397fc 265 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
cc696296 266 char *header, *value;
7e1b7485
RS
267 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
268 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
269 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
270 char *rsignfile = NULL, *rkeyfile = NULL;
1cf20ca3 271 char *passinarg = NULL, *passin = NULL;
0f113f3e 272 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
7e1b7485
RS
273 char *signfile = NULL, *keyfile = NULL;
274 char *thost = NULL, *tport = NULL, *tpath = NULL;
fd3397fc 275 int noCAfile = 0, noCApath = 0, noCAstore = 0;
7e1b7485
RS
276 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
277 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
f9e55034 278 int req_text = 0, resp_text = 0, ret = 1;
f9e55034 279 int req_timeout = -1;
7e1b7485 280 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
0f113f3e 281 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
7e1b7485 282 OPTION_CHOICE o;
0f113f3e 283
0f113f3e 284 reqnames = sk_OPENSSL_STRING_new_null();
2234212c 285 if (reqnames == NULL)
7e1b7485 286 goto end;
0f113f3e 287 ids = sk_OCSP_CERTID_new_null();
2234212c 288 if (ids == NULL)
7e1b7485
RS
289 goto end;
290 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
291 return 1;
292
293 prog = opt_init(argc, argv, ocsp_options);
294 while ((o = opt_next()) != OPT_EOF) {
295 switch (o) {
296 case OPT_EOF:
297 case OPT_ERR:
298 opthelp:
299 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
300 goto end;
301 case OPT_HELP:
302 ret = 0;
303 opt_help(ocsp_options);
304 goto end;
305 case OPT_OUTFILE:
306 outfile = opt_arg();
307 break;
308 case OPT_TIMEOUT:
f9e55034 309#ifndef OPENSSL_NO_SOCK
7e1b7485 310 req_timeout = atoi(opt_arg());
f9e55034 311#endif
7e1b7485
RS
312 break;
313 case OPT_URL:
b548a1f1
RS
314 OPENSSL_free(thost);
315 OPENSSL_free(tport);
316 OPENSSL_free(tpath);
4b8d8e2a 317 thost = tport = tpath = NULL;
7e1b7485
RS
318 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
319 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
320 goto end;
321 }
322 thost = host;
323 tport = port;
324 tpath = path;
325 break;
326 case OPT_HOST:
327 host = opt_arg();
328 break;
329 case OPT_PORT:
330 port = opt_arg();
331 break;
332 case OPT_IGNORE_ERR:
0f113f3e 333 ignore_err = 1;
7e1b7485
RS
334 break;
335 case OPT_NOVERIFY:
0f113f3e 336 noverify = 1;
7e1b7485
RS
337 break;
338 case OPT_NONCE:
0f113f3e 339 add_nonce = 2;
7e1b7485
RS
340 break;
341 case OPT_NO_NONCE:
0f113f3e 342 add_nonce = 0;
7e1b7485
RS
343 break;
344 case OPT_RESP_NO_CERTS:
0f113f3e 345 rflags |= OCSP_NOCERTS;
7e1b7485
RS
346 break;
347 case OPT_RESP_KEY_ID:
0f113f3e 348 rflags |= OCSP_RESPID_KEY;
7e1b7485
RS
349 break;
350 case OPT_NO_CERTS:
0f113f3e 351 sign_flags |= OCSP_NOCERTS;
7e1b7485
RS
352 break;
353 case OPT_NO_SIGNATURE_VERIFY:
0f113f3e 354 verify_flags |= OCSP_NOSIGS;
7e1b7485
RS
355 break;
356 case OPT_NO_CERT_VERIFY:
0f113f3e 357 verify_flags |= OCSP_NOVERIFY;
7e1b7485
RS
358 break;
359 case OPT_NO_CHAIN:
0f113f3e 360 verify_flags |= OCSP_NOCHAIN;
7e1b7485
RS
361 break;
362 case OPT_NO_CERT_CHECKS:
0f113f3e 363 verify_flags |= OCSP_NOCHECKS;
7e1b7485
RS
364 break;
365 case OPT_NO_EXPLICIT:
0f113f3e 366 verify_flags |= OCSP_NOEXPLICIT;
7e1b7485
RS
367 break;
368 case OPT_TRUST_OTHER:
0f113f3e 369 verify_flags |= OCSP_TRUSTOTHER;
7e1b7485
RS
370 break;
371 case OPT_NO_INTERN:
0f113f3e 372 verify_flags |= OCSP_NOINTERN;
7e1b7485
RS
373 break;
374 case OPT_BADSIG:
0f113f3e 375 badsig = 1;
7e1b7485
RS
376 break;
377 case OPT_TEXT:
378 req_text = resp_text = 1;
379 break;
380 case OPT_REQ_TEXT:
0f113f3e 381 req_text = 1;
7e1b7485
RS
382 break;
383 case OPT_RESP_TEXT:
0f113f3e 384 resp_text = 1;
7e1b7485
RS
385 break;
386 case OPT_REQIN:
387 reqin = opt_arg();
388 break;
389 case OPT_RESPIN:
390 respin = opt_arg();
391 break;
392 case OPT_SIGNER:
393 signfile = opt_arg();
394 break;
395 case OPT_VAFILE:
396 verify_certfile = opt_arg();
397 verify_flags |= OCSP_TRUSTOTHER;
398 break;
399 case OPT_SIGN_OTHER:
400 sign_certfile = opt_arg();
401 break;
402 case OPT_VERIFY_OTHER:
403 verify_certfile = opt_arg();
404 break;
405 case OPT_CAFILE:
406 CAfile = opt_arg();
407 break;
408 case OPT_CAPATH:
409 CApath = opt_arg();
410 break;
fd3397fc
RL
411 case OPT_CASTORE:
412 CAstore = opt_arg();
413 break;
2b6bcb70
MC
414 case OPT_NOCAFILE:
415 noCAfile = 1;
416 break;
417 case OPT_NOCAPATH:
418 noCApath = 1;
419 break;
fd3397fc
RL
420 case OPT_NOCASTORE:
421 noCAstore = 1;
422 break;
7e1b7485
RS
423 case OPT_V_CASES:
424 if (!opt_verify(o, vpm))
0f113f3e 425 goto end;
7e1b7485
RS
426 vpmtouched++;
427 break;
428 case OPT_VALIDITY_PERIOD:
429 opt_long(opt_arg(), &nsec);
430 break;
431 case OPT_STATUS_AGE:
432 opt_long(opt_arg(), &maxage);
433 break;
434 case OPT_SIGNKEY:
435 keyfile = opt_arg();
436 break;
437 case OPT_REQOUT:
438 reqout = opt_arg();
439 break;
440 case OPT_RESPOUT:
441 respout = opt_arg();
442 break;
443 case OPT_PATH:
444 path = opt_arg();
445 break;
c6724060 446 case OPT_ISSUER:
a773b52a 447 issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate");
c6724060
RS
448 if (issuer == NULL)
449 goto end;
bb7fc98c
DSH
450 if (issuers == NULL) {
451 if ((issuers = sk_X509_new_null()) == NULL)
452 goto end;
453 }
d5e66eab
SL
454 if (!sk_X509_push(issuers, issuer))
455 goto end;
c6724060 456 break;
7e1b7485
RS
457 case OPT_CERT:
458 X509_free(cert);
a773b52a 459 cert = load_cert(opt_arg(), FORMAT_PEM, "certificate");
7e1b7485
RS
460 if (cert == NULL)
461 goto end;
462 if (cert_id_md == NULL)
463 cert_id_md = EVP_sha1();
464 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
465 goto end;
466 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
467 goto end;
6302bbd2 468 trailing_md = 0;
7e1b7485
RS
469 break;
470 case OPT_SERIAL:
471 if (cert_id_md == NULL)
472 cert_id_md = EVP_sha1();
473 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
474 goto end;
475 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
476 goto end;
6302bbd2 477 trailing_md = 0;
7e1b7485
RS
478 break;
479 case OPT_INDEX:
480 ridx_filename = opt_arg();
481 break;
482 case OPT_CA:
483 rca_filename = opt_arg();
484 break;
485 case OPT_NMIN:
486 opt_int(opt_arg(), &nmin);
0f113f3e
MC
487 if (ndays == -1)
488 ndays = 0;
7e1b7485
RS
489 break;
490 case OPT_REQUEST:
491 opt_int(opt_arg(), &accept_count);
492 break;
493 case OPT_NDAYS:
494 ndays = atoi(opt_arg());
495 break;
496 case OPT_RSIGNER:
497 rsignfile = opt_arg();
498 break;
499 case OPT_RKEY:
500 rkeyfile = opt_arg();
501 break;
1cf20ca3 502 case OPT_PASSIN:
503 passinarg = opt_arg();
504 break;
7e1b7485
RS
505 case OPT_ROTHER:
506 rcertfile = opt_arg();
507 break;
16e1b281 508 case OPT_RMD: /* Response MessageDigest */
7e1b7485
RS
509 if (!opt_md(opt_arg(), &rsign_md))
510 goto end;
511 break;
04940147 512 case OPT_RSIGOPT:
b4dd21a7
DC
513 if (rsign_sigopts == NULL)
514 rsign_sigopts = sk_OPENSSL_STRING_new_null();
515 if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
516 goto end;
517 break;
7e1b7485
RS
518 case OPT_HEADER:
519 header = opt_arg();
520 value = strchr(header, '=');
521 if (value == NULL) {
522 BIO_printf(bio_err, "Missing = in header key=value\n");
523 goto opthelp;
524 }
525 *value++ = '\0';
526 if (!X509V3_add_value(header, value, &headers))
527 goto end;
528 break;
0770c882
TS
529 case OPT_RCID:
530 resp_certid_md = EVP_get_digestbyname(opt_arg());
531 if (resp_certid_md == NULL)
532 goto opthelp;
533 break;
7e1b7485 534 case OPT_MD:
6302bbd2 535 if (trailing_md) {
7e1b7485
RS
536 BIO_printf(bio_err,
537 "%s: Digest must be before -cert or -serial\n",
538 prog);
539 goto opthelp;
540 }
541 if (!opt_md(opt_unknown(), &cert_id_md))
542 goto opthelp;
6302bbd2 543 trailing_md = 1;
7e1b7485 544 break;
3e3c7c36 545 case OPT_MULTI:
a7fb4fa1 546# ifdef OCSP_DAEMON
3e3c7c36 547 multi = atoi(opt_arg());
3e3c7c36 548# endif
a7fb4fa1 549 break;
0f113f3e 550 }
0f113f3e 551 }
6302bbd2
DSH
552 if (trailing_md) {
553 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
554 prog);
555 goto opthelp;
556 }
7e1b7485 557 argc = opt_num_rest();
03358517
KR
558 if (argc != 0)
559 goto opthelp;
0f113f3e
MC
560
561 /* Have we anything to do? */
3e3c7c36 562 if (req == NULL && reqin == NULL
2234212c 563 && respin == NULL && !(port != NULL && ridx_filename != NULL))
7e1b7485 564 goto opthelp;
0f113f3e 565
bdd58d98 566 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
7e1b7485 567 if (out == NULL)
0f113f3e 568 goto end;
0f113f3e 569
2234212c 570 if (req == NULL && (add_nonce != 2))
0f113f3e
MC
571 add_nonce = 0;
572
2234212c 573 if (req == NULL && reqin != NULL) {
bdd58d98 574 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
7e1b7485 575 if (derbio == NULL)
0f113f3e 576 goto end;
0f113f3e
MC
577 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
578 BIO_free(derbio);
2234212c 579 if (req == NULL) {
0f113f3e
MC
580 BIO_printf(bio_err, "Error reading OCSP request\n");
581 goto end;
582 }
583 }
584
2234212c 585 if (req == NULL && port != NULL) {
0f113f3e 586 acbio = init_responder(port);
2234212c 587 if (acbio == NULL)
0f113f3e
MC
588 goto end;
589 }
590
2234212c
PY
591 if (rsignfile != NULL) {
592 if (rkeyfile == NULL)
0f113f3e 593 rkeyfile = rsignfile;
a773b52a 594 rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate");
2234212c 595 if (rsigner == NULL) {
0f113f3e
MC
596 BIO_printf(bio_err, "Error loading responder certificate\n");
597 goto end;
598 }
3bb0f989
TS
599 if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM,
600 NULL, "CA certificate"))
601 goto end;
2234212c 602 if (rcertfile != NULL) {
a773b52a 603 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
0996dc54 604 "responder other certificates"))
0f113f3e
MC
605 goto end;
606 }
1cf20ca3 607 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
608 BIO_printf(bio_err, "Error getting password\n");
609 goto end;
610 }
611 rkey = load_key(rkeyfile, FORMAT_PEM, 0, passin, NULL,
0f113f3e 612 "responder private key");
2234212c 613 if (rkey == NULL)
0f113f3e
MC
614 goto end;
615 }
c7d5ea26 616
3e3c7c36 617 if (ridx_filename != NULL
e4693110 618 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
c7d5ea26
VD
619 BIO_printf(bio_err,
620 "Responder mode requires certificate, key, and CA.\n");
621 goto end;
622 }
623
3e3c7c36 624 if (ridx_filename != NULL) {
c7d5ea26 625 rdb = load_index(ridx_filename, NULL);
a4107d73 626 if (rdb == NULL || index_index(rdb) <= 0) {
c7d5ea26
VD
627 ret = 1;
628 goto end;
629 }
630 }
631
3e3c7c36
VD
632# ifdef OCSP_DAEMON
633 if (multi && acbio != NULL)
634 spawn_loop();
635 if (acbio != NULL && req_timeout > 0)
e9b95e42 636 signal(SIGALRM, socket_timeout);
3e3c7c36
VD
637#endif
638
2234212c 639 if (acbio != NULL)
3e3c7c36 640 log_message(LOG_INFO, "waiting for OCSP client connections...");
0f113f3e 641
2234212c 642redo_accept:
0f113f3e 643
2234212c 644 if (acbio != NULL) {
3e3c7c36
VD
645# ifdef OCSP_DAEMON
646 if (index_changed(rdb)) {
647 CA_DB *newrdb = load_index(ridx_filename, NULL);
648
a4107d73 649 if (newrdb != NULL && index_index(newrdb) > 0) {
3e3c7c36
VD
650 free_index(rdb);
651 rdb = newrdb;
652 } else {
a4107d73 653 free_index(newrdb);
3e3c7c36
VD
654 log_message(LOG_ERR, "error reloading updated index: %s",
655 ridx_filename);
656 }
657 }
658# endif
659
660 req = NULL;
661 if (!do_responder(&req, &cbio, acbio, req_timeout))
662 goto redo_accept;
663
2234212c 664 if (req == NULL) {
0f113f3e
MC
665 resp =
666 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
667 NULL);
668 send_ocsp_response(cbio, resp);
669 goto done_resp;
670 }
671 }
672
2234212c
PY
673 if (req == NULL
674 && (signfile != NULL || reqout != NULL
675 || host != NULL || add_nonce || ridx_filename != NULL)) {
0f113f3e
MC
676 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
677 goto end;
678 }
679
72b89b8e
DB
680 if (req != NULL && add_nonce) {
681 if (!OCSP_request_add1_nonce(req, NULL, -1))
682 goto end;
683 }
0f113f3e 684
2234212c
PY
685 if (signfile != NULL) {
686 if (keyfile == NULL)
0f113f3e 687 keyfile = signfile;
a773b52a 688 signer = load_cert(signfile, FORMAT_PEM, "signer certificate");
2234212c 689 if (signer == NULL) {
0f113f3e
MC
690 BIO_printf(bio_err, "Error loading signer certificate\n");
691 goto end;
692 }
2234212c 693 if (sign_certfile != NULL) {
a773b52a 694 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL,
0996dc54 695 "signer certificates"))
0f113f3e
MC
696 goto end;
697 }
7e1b7485 698 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
0f113f3e 699 "signer private key");
2234212c 700 if (key == NULL)
0f113f3e
MC
701 goto end;
702
703 if (!OCSP_request_sign
704 (req, signer, key, NULL, sign_other, sign_flags)) {
705 BIO_printf(bio_err, "Error signing OCSP request\n");
706 goto end;
707 }
708 }
709
2234212c 710 if (req_text && req != NULL)
0f113f3e
MC
711 OCSP_REQUEST_print(out, req, 0);
712
2234212c 713 if (reqout != NULL) {
bdd58d98 714 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
7e1b7485 715 if (derbio == NULL)
0f113f3e 716 goto end;
0f113f3e
MC
717 i2d_OCSP_REQUEST_bio(derbio, req);
718 BIO_free(derbio);
719 }
720
2234212c 721 if (rdb != NULL) {
b4dd21a7 722 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
0770c882
TS
723 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig,
724 resp_certid_md);
2234212c 725 if (cbio != NULL)
0f113f3e 726 send_ocsp_response(cbio, resp);
2234212c 727 } else if (host != NULL) {
0f113f3e 728# ifndef OPENSSL_NO_SOCK
7e1b7485 729 resp = process_responder(req, host, path,
0f113f3e 730 port, use_ssl, headers, req_timeout);
2234212c 731 if (resp == NULL)
0f113f3e
MC
732 goto end;
733# else
734 BIO_printf(bio_err,
735 "Error creating connect BIO - sockets not supported.\n");
736 goto end;
737# endif
2234212c 738 } else if (respin != NULL) {
bdd58d98 739 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
7e1b7485 740 if (derbio == NULL)
0f113f3e 741 goto end;
0f113f3e
MC
742 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
743 BIO_free(derbio);
2234212c 744 if (resp == NULL) {
0f113f3e
MC
745 BIO_printf(bio_err, "Error reading OCSP response\n");
746 goto end;
747 }
0f113f3e
MC
748 } else {
749 ret = 0;
750 goto end;
751 }
752
753 done_resp:
754
2234212c 755 if (respout != NULL) {
bdd58d98 756 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
7e1b7485 757 if (derbio == NULL)
0f113f3e 758 goto end;
0f113f3e
MC
759 i2d_OCSP_RESPONSE_bio(derbio, resp);
760 BIO_free(derbio);
761 }
762
763 i = OCSP_response_status(resp);
0f113f3e
MC
764 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
765 BIO_printf(out, "Responder Error: %s (%d)\n",
766 OCSP_response_status_str(i), i);
c324ecfb 767 if (!ignore_err)
3e3c7c36 768 goto end;
0f113f3e
MC
769 }
770
771 if (resp_text)
772 OCSP_RESPONSE_print(out, resp, 0);
773
774 /* If running as responder don't verify our own response */
2234212c 775 if (cbio != NULL) {
e46bcca2
AE
776 /* If not unlimited, see if we took all we should. */
777 if (accept_count != -1 && --accept_count <= 0) {
7e1b7485
RS
778 ret = 0;
779 goto end;
0f113f3e 780 }
7e1b7485
RS
781 BIO_free_all(cbio);
782 cbio = NULL;
783 OCSP_REQUEST_free(req);
784 req = NULL;
785 OCSP_RESPONSE_free(resp);
786 resp = NULL;
787 goto redo_accept;
788 }
2234212c 789 if (ridx_filename != NULL) {
0f113f3e
MC
790 ret = 0;
791 goto end;
792 }
793
2234212c 794 if (store == NULL) {
fd3397fc
RL
795 store = setup_verify(CAfile, noCAfile, CApath, noCApath,
796 CAstore, noCAstore);
7e1b7485
RS
797 if (!store)
798 goto end;
799 }
800 if (vpmtouched)
0f113f3e 801 X509_STORE_set1_param(store, vpm);
2234212c 802 if (verify_certfile != NULL) {
a773b52a 803 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL,
0996dc54 804 "validator certificate"))
0f113f3e
MC
805 goto end;
806 }
807
808 bs = OCSP_response_get1_basic(resp);
2234212c 809 if (bs == NULL) {
0f113f3e
MC
810 BIO_printf(bio_err, "Error parsing response\n");
811 goto end;
812 }
813
814 ret = 0;
815
816 if (!noverify) {
2234212c 817 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
0f113f3e
MC
818 if (i == -1)
819 BIO_printf(bio_err, "WARNING: no nonce in response\n");
820 else {
821 BIO_printf(bio_err, "Nonce Verify error\n");
822 ret = 1;
823 goto end;
824 }
825 }
826
827 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
c6724060
RS
828 if (i <= 0 && issuers) {
829 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
830 if (i > 0)
831 ERR_clear_error();
832 }
0f113f3e
MC
833 if (i <= 0) {
834 BIO_printf(bio_err, "Response Verify Failure\n");
835 ERR_print_errors(bio_err);
836 ret = 1;
2234212c 837 } else {
0f113f3e 838 BIO_printf(bio_err, "Response verify OK\n");
2234212c 839 }
0f113f3e
MC
840 }
841
7e1b7485 842 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
0f113f3e
MC
843
844 end:
845 ERR_print_errors(bio_err);
846 X509_free(signer);
847 X509_STORE_free(store);
222561fe 848 X509_VERIFY_PARAM_free(vpm);
89623f84 849 sk_OPENSSL_STRING_free(rsign_sigopts);
0f113f3e
MC
850 EVP_PKEY_free(key);
851 EVP_PKEY_free(rkey);
0f113f3e 852 X509_free(cert);
bb7fc98c 853 sk_X509_pop_free(issuers, X509_free);
0f113f3e 854 X509_free(rsigner);
3bb0f989 855 sk_X509_pop_free(rca_cert, X509_free);
0f113f3e
MC
856 free_index(rdb);
857 BIO_free_all(cbio);
858 BIO_free_all(acbio);
4e525a0b 859 BIO_free_all(out);
0f113f3e
MC
860 OCSP_REQUEST_free(req);
861 OCSP_RESPONSE_free(resp);
862 OCSP_BASICRESP_free(bs);
863 sk_OPENSSL_STRING_free(reqnames);
864 sk_OCSP_CERTID_free(ids);
865 sk_X509_pop_free(sign_other, X509_free);
866 sk_X509_pop_free(verify_other, X509_free);
867 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
b548a1f1
RS
868 OPENSSL_free(thost);
869 OPENSSL_free(tport);
870 OPENSSL_free(tpath);
0f113f3e 871
26a7d938 872 return ret;
5782ceb2
DSH
873}
874
3e3c7c36
VD
875static void
876log_message(int level, const char *fmt, ...)
877{
878 va_list ap;
879
880 va_start(ap, fmt);
881# ifdef OCSP_DAEMON
882 if (multi) {
a9dd51a8
EC
883 char buf[1024];
884 if (vsnprintf(buf, sizeof(buf), fmt, ap) > 0) {
885 syslog(level, "%s", buf);
886 }
3e3c7c36
VD
887 if (level >= LOG_ERR)
888 ERR_print_errors_cb(print_syslog, &level);
889 }
890# endif
891 if (!multi) {
892 BIO_printf(bio_err, "%s: ", prog);
893 BIO_vprintf(bio_err, fmt, ap);
894 BIO_printf(bio_err, "\n");
895 }
896 va_end(ap);
897}
898
899# ifdef OCSP_DAEMON
900
901static int print_syslog(const char *str, size_t len, void *levPtr)
902{
903 int level = *(int *)levPtr;
904 int ilen = (len > MAXERRLEN) ? MAXERRLEN : len;
905
906 syslog(level, "%.*s", ilen, str);
907
908 return ilen;
909}
910
911static int index_changed(CA_DB *rdb)
912{
913 struct stat sb;
914
915 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
916 if (rdb->dbst.st_mtime != sb.st_mtime
917 || rdb->dbst.st_ctime != sb.st_ctime
918 || rdb->dbst.st_ino != sb.st_ino
919 || rdb->dbst.st_dev != sb.st_dev) {
920 syslog(LOG_INFO, "index file changed, reloading");
921 return 1;
922 }
923 }
924 return 0;
925}
926
927static void killall(int ret, pid_t *kidpids)
928{
929 int i;
930
931 for (i = 0; i < multi; ++i)
932 if (kidpids[i] != 0)
933 (void)kill(kidpids[i], SIGTERM);
39fc4c17 934 OPENSSL_free(kidpids);
3e3c7c36
VD
935 sleep(1);
936 exit(ret);
937}
938
939static int termsig = 0;
940
941static void noteterm (int sig)
942{
943 termsig = sig;
944}
945
946/*
947 * Loop spawning up to `multi` child processes, only child processes return
948 * from this function. The parent process loops until receiving a termination
949 * signal, kills extant children and exits without returning.
950 */
951static void spawn_loop(void)
952{
3e3c7c36
VD
953 pid_t *kidpids = NULL;
954 int status;
955 int procs = 0;
956 int i;
957
958 openlog(prog, LOG_PID, LOG_DAEMON);
959
960 if (setpgid(0, 0)) {
961 syslog(LOG_ERR, "fatal: error detaching from parent process group: %s",
962 strerror(errno));
963 exit(1);
964 }
965 kidpids = app_malloc(multi * sizeof(*kidpids), "child PID array");
966 for (i = 0; i < multi; ++i)
967 kidpids[i] = 0;
968
969 signal(SIGINT, noteterm);
970 signal(SIGTERM, noteterm);
971
972 while (termsig == 0) {
973 pid_t fpid;
974
975 /*
976 * Wait for a child to replace when we're at the limit.
977 * Slow down if a child exited abnormally or waitpid() < 0
978 */
979 while (termsig == 0 && procs >= multi) {
980 if ((fpid = waitpid(-1, &status, 0)) > 0) {
981 for (i = 0; i < procs; ++i) {
982 if (kidpids[i] == fpid) {
983 kidpids[i] = 0;
984 --procs;
985 break;
986 }
987 }
988 if (i >= multi) {
989 syslog(LOG_ERR, "fatal: internal error: "
990 "no matching child slot for pid: %ld",
991 (long) fpid);
992 killall(1, kidpids);
993 }
994 if (status != 0) {
995 if (WIFEXITED(status))
996 syslog(LOG_WARNING, "child process: %ld, exit status: %d",
997 (long)fpid, WEXITSTATUS(status));
998 else if (WIFSIGNALED(status))
999 syslog(LOG_WARNING, "child process: %ld, term signal %d%s",
1000 (long)fpid, WTERMSIG(status),
a9dd51a8
EC
1001#ifdef WCOREDUMP
1002 WCOREDUMP(status) ? " (core dumped)" :
1003#endif
1004 "");
3e3c7c36
VD
1005 sleep(1);
1006 }
1007 break;
1008 } else if (errno != EINTR) {
1009 syslog(LOG_ERR, "fatal: waitpid(): %s", strerror(errno));
1010 killall(1, kidpids);
1011 }
1012 }
1013 if (termsig)
1014 break;
1015
1016 switch(fpid = fork()) {
1017 case -1: /* error */
1018 /* System critically low on memory, pause and try again later */
1019 sleep(30);
1020 break;
1021 case 0: /* child */
c20a76f6 1022 OPENSSL_free(kidpids);
3e3c7c36
VD
1023 signal(SIGINT, SIG_DFL);
1024 signal(SIGTERM, SIG_DFL);
1025 if (termsig)
1026 _exit(0);
1027 if (RAND_poll() <= 0) {
1028 syslog(LOG_ERR, "fatal: RAND_poll() failed");
1029 _exit(1);
1030 }
1031 return;
1032 default: /* parent */
1033 for (i = 0; i < multi; ++i) {
1034 if (kidpids[i] == 0) {
1035 kidpids[i] = fpid;
1036 procs++;
1037 break;
1038 }
1039 }
1040 if (i >= multi) {
1041 syslog(LOG_ERR, "fatal: internal error: no free child slots");
1042 killall(1, kidpids);
1043 }
1044 break;
1045 }
1046 }
1047
1048 /* The loop above can only break on termsig */
aed3df20 1049 syslog(LOG_INFO, "terminating on signal: %d", termsig);
3e3c7c36
VD
1050 killall(0, kidpids);
1051}
1052# endif
1053
0f113f3e
MC
1054static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
1055 const EVP_MD *cert_id_md, X509 *issuer,
1056 STACK_OF(OCSP_CERTID) *ids)
1057{
1058 OCSP_CERTID *id;
2234212c
PY
1059
1060 if (issuer == NULL) {
0f113f3e
MC
1061 BIO_printf(bio_err, "No issuer certificate specified\n");
1062 return 0;
1063 }
96487cdd 1064 if (*req == NULL)
0f113f3e 1065 *req = OCSP_REQUEST_new();
96487cdd 1066 if (*req == NULL)
0f113f3e
MC
1067 goto err;
1068 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
2234212c 1069 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
0f113f3e
MC
1070 goto err;
1071 if (!OCSP_request_add0_id(*req, id))
1072 goto err;
1073 return 1;
1074
1075 err:
1076 BIO_printf(bio_err, "Error Creating OCSP request\n");
1077 return 0;
1078}
1079
1080static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
1081 const EVP_MD *cert_id_md, X509 *issuer,
1082 STACK_OF(OCSP_CERTID) *ids)
1083{
1084 OCSP_CERTID *id;
1085 X509_NAME *iname;
1086 ASN1_BIT_STRING *ikey;
1087 ASN1_INTEGER *sno;
2234212c
PY
1088
1089 if (issuer == NULL) {
0f113f3e
MC
1090 BIO_printf(bio_err, "No issuer certificate specified\n");
1091 return 0;
1092 }
96487cdd 1093 if (*req == NULL)
0f113f3e 1094 *req = OCSP_REQUEST_new();
96487cdd 1095 if (*req == NULL)
0f113f3e
MC
1096 goto err;
1097 iname = X509_get_subject_name(issuer);
1098 ikey = X509_get0_pubkey_bitstr(issuer);
1099 sno = s2i_ASN1_INTEGER(NULL, serial);
2234212c 1100 if (sno == NULL) {
0f113f3e
MC
1101 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
1102 return 0;
1103 }
1104 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
1105 ASN1_INTEGER_free(sno);
96487cdd 1106 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
0f113f3e
MC
1107 goto err;
1108 if (!OCSP_request_add0_id(*req, id))
1109 goto err;
1110 return 1;
1111
1112 err:
1113 BIO_printf(bio_err, "Error Creating OCSP request\n");
1114 return 0;
1115}
73758d43 1116
7e1b7485 1117static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
0f113f3e
MC
1118 STACK_OF(OPENSSL_STRING) *names,
1119 STACK_OF(OCSP_CERTID) *ids, long nsec,
1120 long maxage)
1121{
1122 OCSP_CERTID *id;
cc696296 1123 const char *name;
7e1b7485 1124 int i, status, reason;
0f113f3e
MC
1125 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
1126
2234212c 1127 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
0f113f3e 1128 || !sk_OCSP_CERTID_num(ids))
7e1b7485 1129 return;
0f113f3e
MC
1130
1131 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
1132 id = sk_OCSP_CERTID_value(ids, i);
1133 name = sk_OPENSSL_STRING_value(names, i);
1134 BIO_printf(out, "%s: ", name);
1135
1136 if (!OCSP_resp_find_status(bs, id, &status, &reason,
1137 &rev, &thisupd, &nextupd)) {
1138 BIO_puts(out, "ERROR: No Status found.\n");
1139 continue;
1140 }
1141
1142 /*
1143 * Check validity: if invalid write to output BIO so we know which
1144 * response this refers to.
1145 */
1146 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
1147 BIO_puts(out, "WARNING: Status times invalid.\n");
1148 ERR_print_errors(out);
1149 }
1150 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
1151
1152 BIO_puts(out, "\tThis Update: ");
1153 ASN1_GENERALIZEDTIME_print(out, thisupd);
1154 BIO_puts(out, "\n");
1155
1156 if (nextupd) {
1157 BIO_puts(out, "\tNext Update: ");
1158 ASN1_GENERALIZEDTIME_print(out, nextupd);
1159 BIO_puts(out, "\n");
1160 }
1161
1162 if (status != V_OCSP_CERTSTATUS_REVOKED)
1163 continue;
1164
1165 if (reason != -1)
1166 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
1167
1168 BIO_puts(out, "\tRevocation Time: ");
1169 ASN1_GENERALIZEDTIME_print(out, rev);
1170 BIO_puts(out, "\n");
1171 }
0f113f3e
MC
1172}
1173
b4dd21a7 1174static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
3bb0f989 1175 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
0f113f3e 1176 EVP_PKEY *rkey, const EVP_MD *rmd,
b4dd21a7 1177 STACK_OF(OPENSSL_STRING) *sigopts,
0f113f3e 1178 STACK_OF(X509) *rother, unsigned long flags,
0770c882
TS
1179 int nmin, int ndays, int badsig,
1180 const EVP_MD *resp_md)
0f113f3e
MC
1181{
1182 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
3bb0f989 1183 OCSP_CERTID *cid;
0f113f3e 1184 OCSP_BASICRESP *bs = NULL;
7e1b7485 1185 int i, id_count;
b4dd21a7
DC
1186 EVP_MD_CTX *mctx = NULL;
1187 EVP_PKEY_CTX *pkctx = NULL;
0f113f3e
MC
1188
1189 id_count = OCSP_request_onereq_count(req);
1190
1191 if (id_count <= 0) {
1192 *resp =
1193 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1194 goto end;
1195 }
1196
1197 bs = OCSP_BASICRESP_new();
1198 thisupd = X509_gmtime_adj(NULL, 0);
1199 if (ndays != -1)
9aa00b18 1200 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
0f113f3e
MC
1201
1202 /* Examine each certificate id in the request */
1203 for (i = 0; i < id_count; i++) {
1204 OCSP_ONEREQ *one;
1205 ASN1_INTEGER *serial;
1206 char **inf;
3bb0f989
TS
1207 int jj;
1208 int found = 0;
0f113f3e
MC
1209 ASN1_OBJECT *cert_id_md_oid;
1210 const EVP_MD *cert_id_md;
0770c882
TS
1211 OCSP_CERTID *cid_resp_md = NULL;
1212
0f113f3e
MC
1213 one = OCSP_request_onereq_get0(req, i);
1214 cid = OCSP_onereq_get0_id(one);
1215
1216 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1217
1218 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
2234212c 1219 if (cert_id_md == NULL) {
0f113f3e
MC
1220 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1221 NULL);
1222 goto end;
1223 }
3bb0f989
TS
1224 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
1225 X509 *ca_cert = sk_X509_value(ca, jj);
1226 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
1227
0770c882 1228 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
3bb0f989 1229 found = 1;
0770c882
TS
1230 if (resp_md != NULL)
1231 cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
1232 }
3bb0f989
TS
1233 OCSP_CERTID_free(ca_id);
1234 }
0770c882
TS
1235 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1236 inf = lookup_serial(db, serial);
1237
1238 /* at this point, we can have cid be an alias of cid_resp_md */
1239 cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
0f113f3e 1240
3bb0f989 1241 if (!found) {
0f113f3e
MC
1242 OCSP_basic_add1_status(bs, cid,
1243 V_OCSP_CERTSTATUS_UNKNOWN,
1244 0, NULL, thisupd, nextupd);
1245 continue;
1246 }
2234212c 1247 if (inf == NULL) {
0f113f3e
MC
1248 OCSP_basic_add1_status(bs, cid,
1249 V_OCSP_CERTSTATUS_UNKNOWN,
1250 0, NULL, thisupd, nextupd);
2234212c 1251 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
0f113f3e
MC
1252 OCSP_basic_add1_status(bs, cid,
1253 V_OCSP_CERTSTATUS_GOOD,
1254 0, NULL, thisupd, nextupd);
2234212c 1255 } else if (inf[DB_type][0] == DB_TYPE_REV) {
0f113f3e
MC
1256 ASN1_OBJECT *inst = NULL;
1257 ASN1_TIME *revtm = NULL;
1258 ASN1_GENERALIZEDTIME *invtm = NULL;
1259 OCSP_SINGLERESP *single;
1260 int reason = -1;
0770c882 1261
0f113f3e
MC
1262 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1263 single = OCSP_basic_add1_status(bs, cid,
1264 V_OCSP_CERTSTATUS_REVOKED,
1265 reason, revtm, thisupd, nextupd);
2234212c 1266 if (invtm != NULL)
0f113f3e
MC
1267 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1268 invtm, 0, 0);
2234212c 1269 else if (inst != NULL)
0f113f3e
MC
1270 OCSP_SINGLERESP_add1_ext_i2d(single,
1271 NID_hold_instruction_code, inst,
1272 0, 0);
1273 ASN1_OBJECT_free(inst);
1274 ASN1_TIME_free(revtm);
1275 ASN1_GENERALIZEDTIME_free(invtm);
1276 }
0770c882 1277 OCSP_CERTID_free(cid_resp_md);
0f113f3e
MC
1278 }
1279
1280 OCSP_copy_nonce(bs, req);
1281
b4dd21a7
DC
1282 mctx = EVP_MD_CTX_new();
1283 if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
1284 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
1285 goto end;
1286 }
1287 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1288 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
89623f84 1289
b4dd21a7
DC
1290 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1291 BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1292 ERR_print_errors(bio_err);
1293 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1294 NULL);
1295 goto end;
1296 }
1297 }
72b89b8e
DB
1298 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
1299 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
1300 goto end;
1301 }
0f113f3e 1302
6ef869d7 1303 if (badsig) {
a0754084
DSH
1304 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
1305 corrupt_signature(sig);
6ef869d7 1306 }
0f113f3e
MC
1307
1308 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1309
1310 end:
89623f84 1311 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1312 ASN1_TIME_free(thisupd);
1313 ASN1_TIME_free(nextupd);
0f113f3e 1314 OCSP_BASICRESP_free(bs);
0f113f3e 1315}
ee306a13 1316
f85b68cd 1317static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
0f113f3e
MC
1318{
1319 int i;
1320 BIGNUM *bn = NULL;
1321 char *itmp, *row[DB_NUMBER], **rrow;
1322 for (i = 0; i < DB_NUMBER; i++)
1323 row[i] = NULL;
1324 bn = ASN1_INTEGER_to_BN(ser, NULL);
1325 OPENSSL_assert(bn); /* FIXME: should report an error at this
1326 * point and abort */
1327 if (BN_is_zero(bn))
7644a9ae 1328 itmp = OPENSSL_strdup("00");
0f113f3e
MC
1329 else
1330 itmp = BN_bn2hex(bn);
1331 row[DB_serial] = itmp;
1332 BN_free(bn);
1333 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1334 OPENSSL_free(itmp);
1335 return rrow;
1336}
ee306a13
DSH
1337
1338/* Quick and dirty OCSP server: read in and parse input request */
1339
c45a48c1 1340static BIO *init_responder(const char *port)
0f113f3e 1341{
366e2a60 1342# ifdef OPENSSL_NO_SOCK
0f113f3e
MC
1343 BIO_printf(bio_err,
1344 "Error setting up accept BIO - sockets not supported.\n");
366e2a60 1345 return NULL;
f863ad0c
MC
1346# else
1347 BIO *acbio = NULL, *bufbio = NULL;
1348
366e2a60 1349 bufbio = BIO_new(BIO_f_buffer());
96487cdd 1350 if (bufbio == NULL)
0f113f3e 1351 goto err;
366e2a60
RS
1352 acbio = BIO_new(BIO_s_accept());
1353 if (acbio == NULL
1354 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1355 || BIO_set_accept_port(acbio, port) < 0) {
3e3c7c36 1356 log_message(LOG_ERR, "Error setting up accept BIO");
366e2a60
RS
1357 goto err;
1358 }
1359
0f113f3e
MC
1360 BIO_set_accept_bios(acbio, bufbio);
1361 bufbio = NULL;
0f113f3e 1362 if (BIO_do_accept(acbio) <= 0) {
3e3c7c36 1363 log_message(LOG_ERR, "Error starting accept");
0f113f3e
MC
1364 goto err;
1365 }
1366
1367 return acbio;
1368
1369 err:
1370 BIO_free_all(acbio);
1371 BIO_free(bufbio);
1372 return NULL;
f863ad0c 1373# endif
0f113f3e 1374}
ee306a13 1375
f863ad0c 1376# ifndef OPENSSL_NO_SOCK
fc3cec53
RS
1377/*
1378 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1379 */
1380static int urldecode(char *p)
995101d6
RS
1381{
1382 unsigned char *out = (unsigned char *)p;
fc3cec53 1383 unsigned char *save = out;
995101d6
RS
1384
1385 for (; *p; p++) {
1386 if (*p != '%')
1387 *out++ = *p;
18295f0c 1388 else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
14f051a0
RS
1389 /* Don't check, can't fail because of ixdigit() call. */
1390 *out++ = (OPENSSL_hexchar2int(p[1]) << 4)
1391 | OPENSSL_hexchar2int(p[2]);
995101d6
RS
1392 p += 2;
1393 }
fc3cec53
RS
1394 else
1395 return -1;
995101d6 1396 }
fc3cec53
RS
1397 *out = '\0';
1398 return (int)(out - save);
995101d6 1399}
f863ad0c 1400# endif
995101d6 1401
3e3c7c36 1402# ifdef OCSP_DAEMON
e9b95e42 1403static void socket_timeout(int signum)
3e3c7c36
VD
1404{
1405 if (acfd != (int)INVALID_SOCKET)
1406 (void)shutdown(acfd, SHUT_RD);
1407}
1408# endif
1409
1410static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1411 int timeout)
0f113f3e 1412{
f863ad0c
MC
1413# ifdef OPENSSL_NO_SOCK
1414 return 0;
1415# else
7e1b7485 1416 int len;
0f113f3e 1417 OCSP_REQUEST *req = NULL;
fc3cec53 1418 char inbuf[2048], reqbuf[2048];
995101d6
RS
1419 char *p, *q;
1420 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
3e3c7c36 1421 const char *client;
0f113f3e 1422
3e3c7c36
VD
1423 *preq = NULL;
1424
1425 /* Connection loss before accept() is routine, ignore silently */
1426 if (BIO_do_accept(acbio) <= 0)
0f113f3e 1427 return 0;
0f113f3e
MC
1428
1429 cbio = BIO_pop(acbio);
1430 *pcbio = cbio;
3e3c7c36
VD
1431 client = BIO_get_peer_name(cbio);
1432
1433# ifdef OCSP_DAEMON
1434 if (timeout > 0) {
1435 (void) BIO_get_fd(cbio, &acfd);
1436 alarm(timeout);
1437 }
1438# endif
0f113f3e 1439
7e1b7485 1440 /* Read the request line. */
cbe29648 1441 len = BIO_gets(cbio, reqbuf, sizeof(reqbuf));
7e1b7485 1442 if (len <= 0)
3e3c7c36
VD
1443 goto out;
1444
fc3cec53 1445 if (strncmp(reqbuf, "GET ", 4) == 0) {
995101d6 1446 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
fc3cec53 1447 for (p = reqbuf + 4; *p == ' '; ++p)
995101d6 1448 continue;
fc3cec53 1449 if (*p != '/') {
3e3c7c36
VD
1450 log_message(LOG_INFO, "Invalid request -- bad URL: %s", client);
1451 goto out;
995101d6 1452 }
fc3cec53
RS
1453 p++;
1454
995101d6
RS
1455 /* Splice off the HTTP version identifier. */
1456 for (q = p; *q; q++)
fc3cec53 1457 if (*q == ' ')
995101d6 1458 break;
fc3cec53 1459 if (strncmp(q, " HTTP/1.", 8) != 0) {
3e3c7c36
VD
1460 log_message(LOG_INFO,
1461 "Invalid request -- bad HTTP version: %s", client);
1462 goto out;
995101d6
RS
1463 }
1464 *q = '\0';
3e3c7c36
VD
1465
1466 /*
5fe499cb
VD
1467 * Skip "GET / HTTP..." requests often used by load-balancers. Note:
1468 * 'p' was incremented above to point to the first byte *after* the
1469 * leading slash, so with 'GET / ' it is now an empty string.
3e3c7c36 1470 */
5fe499cb 1471 if (p[0] == '\0')
3e3c7c36
VD
1472 goto out;
1473
fc3cec53
RS
1474 len = urldecode(p);
1475 if (len <= 0) {
3e3c7c36
VD
1476 log_message(LOG_INFO,
1477 "Invalid request -- bad URL encoding: %s", client);
1478 goto out;
fc3cec53
RS
1479 }
1480 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1481 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
3e3c7c36
VD
1482 log_message(LOG_ERR, "Could not allocate base64 bio: %s", client);
1483 goto out;
fc3cec53 1484 }
995101d6
RS
1485 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1486 getbio = BIO_push(b64, getbio);
fc3cec53 1487 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
3e3c7c36
VD
1488 log_message(LOG_INFO, "Invalid request -- bad HTTP verb: %s", client);
1489 goto out;
7e1b7485 1490 }
fc3cec53
RS
1491
1492 /* Read and skip past the headers. */
0f113f3e 1493 for (;;) {
cbe29648 1494 len = BIO_gets(cbio, inbuf, sizeof(inbuf));
0f113f3e 1495 if (len <= 0)
3e3c7c36 1496 goto out;
0f113f3e
MC
1497 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1498 break;
1499 }
1500
3e3c7c36
VD
1501# ifdef OCSP_DAEMON
1502 /* Clear alarm before we close the client socket */
1503 alarm(0);
1504 timeout = 0;
1505# endif
1506
0f113f3e 1507 /* Try to read OCSP request */
2234212c 1508 if (getbio != NULL) {
995101d6
RS
1509 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1510 BIO_free_all(getbio);
2234212c 1511 } else {
995101d6 1512 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
2234212c 1513 }
0f113f3e 1514
3e3c7c36
VD
1515 if (req == NULL)
1516 log_message(LOG_ERR, "Error parsing OCSP request");
0f113f3e
MC
1517
1518 *preq = req;
1519
3e3c7c36
VD
1520out:
1521# ifdef OCSP_DAEMON
1522 if (timeout > 0)
1523 alarm(0);
1524 acfd = (int)INVALID_SOCKET;
1525# endif
0f113f3e 1526 return 1;
f863ad0c 1527# endif
0f113f3e 1528}
ee306a13
DSH
1529
1530static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
0f113f3e
MC
1531{
1532 char http_resp[] =
1533 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1534 "Content-Length: %d\r\n\r\n";
2234212c 1535 if (cbio == NULL)
0f113f3e
MC
1536 return 0;
1537 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1538 i2d_OCSP_RESPONSE_bio(cbio, resp);
1539 (void)BIO_flush(cbio);
1540 return 1;
1541}
ee306a13 1542
f9e55034 1543# ifndef OPENSSL_NO_SOCK
76e0cd12
DSH
1544static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
1545 const char *path,
0f113f3e
MC
1546 const STACK_OF(CONF_VALUE) *headers,
1547 OCSP_REQUEST *req, int req_timeout)
1548{
1549 int fd;
1550 int rv;
1551 int i;
76e0cd12 1552 int add_host = 1;
0f113f3e
MC
1553 OCSP_REQ_CTX *ctx = NULL;
1554 OCSP_RESPONSE *rsp = NULL;
1555 fd_set confds;
1556 struct timeval tv;
1557
1558 if (req_timeout != -1)
1559 BIO_set_nbio(cbio, 1);
1560
1561 rv = BIO_do_connect(cbio);
1562
1563 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
7e1b7485 1564 BIO_puts(bio_err, "Error connecting BIO\n");
0f113f3e
MC
1565 return NULL;
1566 }
1567
4428c7db 1568 if (BIO_get_fd(cbio, &fd) < 0) {
7e1b7485 1569 BIO_puts(bio_err, "Can't get connection fd\n");
0f113f3e
MC
1570 goto err;
1571 }
1572
1573 if (req_timeout != -1 && rv <= 0) {
1574 FD_ZERO(&confds);
1575 openssl_fdset(fd, &confds);
1576 tv.tv_usec = 0;
1577 tv.tv_sec = req_timeout;
1578 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1579 if (rv == 0) {
7e1b7485 1580 BIO_puts(bio_err, "Timeout on connect\n");
0f113f3e
MC
1581 return NULL;
1582 }
1583 }
1584
1585 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
96487cdd 1586 if (ctx == NULL)
0f113f3e
MC
1587 return NULL;
1588
1589 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1590 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
76e0cd12
DSH
1591 if (add_host == 1 && strcasecmp("host", hdr->name) == 0)
1592 add_host = 0;
0f113f3e
MC
1593 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1594 goto err;
1595 }
1596
76e0cd12
DSH
1597 if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0)
1598 goto err;
1599
0f113f3e
MC
1600 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1601 goto err;
1602
1603 for (;;) {
1604 rv = OCSP_sendreq_nbio(&rsp, ctx);
1605 if (rv != -1)
1606 break;
1607 if (req_timeout == -1)
1608 continue;
1609 FD_ZERO(&confds);
1610 openssl_fdset(fd, &confds);
1611 tv.tv_usec = 0;
1612 tv.tv_sec = req_timeout;
2234212c 1613 if (BIO_should_read(cbio)) {
0f113f3e 1614 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
2234212c 1615 } else if (BIO_should_write(cbio)) {
0f113f3e 1616 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
2234212c 1617 } else {
7e1b7485 1618 BIO_puts(bio_err, "Unexpected retry condition\n");
0f113f3e
MC
1619 goto err;
1620 }
1621 if (rv == 0) {
7e1b7485 1622 BIO_puts(bio_err, "Timeout on request\n");
0f113f3e
MC
1623 break;
1624 }
1625 if (rv == -1) {
7e1b7485 1626 BIO_puts(bio_err, "Select error\n");
0f113f3e
MC
1627 break;
1628 }
1629
1630 }
1631 err:
895cba19 1632 OCSP_REQ_CTX_free(ctx);
0f113f3e
MC
1633
1634 return rsp;
1635}
454dbbc5 1636
7e1b7485 1637OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
0f113f3e
MC
1638 const char *host, const char *path,
1639 const char *port, int use_ssl,
82c49427 1640 STACK_OF(CONF_VALUE) *headers,
0f113f3e
MC
1641 int req_timeout)
1642{
1643 BIO *cbio = NULL;
1644 SSL_CTX *ctx = NULL;
1645 OCSP_RESPONSE *resp = NULL;
ff4a9394 1646
0f113f3e 1647 cbio = BIO_new_connect(host);
2234212c 1648 if (cbio == NULL) {
7e1b7485 1649 BIO_printf(bio_err, "Error creating connect BIO\n");
0f113f3e
MC
1650 goto end;
1651 }
2234212c 1652 if (port != NULL)
0f113f3e
MC
1653 BIO_set_conn_port(cbio, port);
1654 if (use_ssl == 1) {
1655 BIO *sbio;
13c9bb3e 1656 ctx = SSL_CTX_new(TLS_client_method());
0f113f3e 1657 if (ctx == NULL) {
7e1b7485 1658 BIO_printf(bio_err, "Error creating SSL context.\n");
0f113f3e
MC
1659 goto end;
1660 }
1661 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1662 sbio = BIO_new_ssl(ctx, 1);
1663 cbio = BIO_push(sbio, cbio);
1664 }
ff4a9394 1665
76e0cd12 1666 resp = query_responder(cbio, host, path, headers, req, req_timeout);
2234212c 1667 if (resp == NULL)
0f113f3e
MC
1668 BIO_printf(bio_err, "Error querying OCSP responder\n");
1669 end:
ca3a82c3 1670 BIO_free_all(cbio);
62adbcee 1671 SSL_CTX_free(ctx);
0f113f3e
MC
1672 return resp;
1673}
f9e55034 1674# endif
67c8e7f4 1675
85d686e7 1676#endif