]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ocsp.c
add OSSL_STACK_OF_X509_free() for commonly used pattern
[thirdparty/openssl.git] / apps / ocsp.c
CommitLineData
0f113f3e 1/*
a28d06f3 2 * Copyright 2001-2021 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
1ae56f2f
RS
12#ifdef OPENSSL_SYS_VMS
13 /* So fd_set and friends get properly defined on OpenVMS */
14# define _XOPEN_SOURCE_EXTENDED
15#endif
0f113f3e 16
1ae56f2f
RS
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21#include <ctype.h>
3e41ac35
MC
22
23/* Needs to be included before the openssl headers */
1ae56f2f 24#include "apps.h"
582311d7 25#include "http_server.h"
1ae56f2f
RS
26#include "progs.h"
27#include "internal/sockets.h"
28#include <openssl/e_os2.h>
29#include <openssl/crypto.h>
30#include <openssl/err.h>
31#include <openssl/ssl.h>
32#include <openssl/evp.h>
33#include <openssl/bn.h>
34#include <openssl/x509v3.h>
3e3c7c36 35
08073700
RB
36#if defined(__TANDEM)
37# if defined(OPENSSL_TANDEM_FLOSS)
38# include <floss.h(floss_fork)>
39# endif
40#endif
41
1ae56f2f 42#if defined(OPENSSL_SYS_VXWORKS)
5c8b7b4c
KT
43/* not supported */
44int setpgid(pid_t pid, pid_t pgid)
45{
46 errno = ENOSYS;
47 return 0;
48}
49/* not supported */
50pid_t fork(void)
51{
52 errno = ENOSYS;
53 return (pid_t) -1;
54}
1ae56f2f 55#endif
0f113f3e 56/* Maximum leeway in validity period: default 5 minutes */
1ae56f2f 57#define MAX_VALIDITY_PERIOD (5 * 60)
0f113f3e
MC
58
59static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
60 const EVP_MD *cert_id_md, X509 *issuer,
61 STACK_OF(OCSP_CERTID) *ids);
62static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
63 const EVP_MD *cert_id_md, X509 *issuer,
64 STACK_OF(OCSP_CERTID) *ids);
639bb581 65static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
0f113f3e
MC
66 STACK_OF(OPENSSL_STRING) *names,
67 STACK_OF(OCSP_CERTID) *ids, long nsec,
68 long maxage);
b4dd21a7 69static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
3bb0f989 70 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
0f113f3e 71 EVP_PKEY *rkey, const EVP_MD *md,
b4dd21a7 72 STACK_OF(OPENSSL_STRING) *sigopts,
0f113f3e 73 STACK_OF(X509) *rother, unsigned long flags,
0770c882
TS
74 int nmin, int ndays, int badsig,
75 const EVP_MD *resp_md);
ee306a13 76
f85b68cd 77static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
582311d7 78static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
4599ea9f 79 int timeout);
582311d7 80static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp);
3e3c7c36 81static char *prog;
3e3c7c36 82
582311d7 83#ifdef HTTP_DAEMON
3e3c7c36 84static int index_changed(CA_DB *);
1ae56f2f 85#endif
f9e55034 86
7e1b7485 87typedef enum OPTION_choice {
b0f96018 88 OPT_COMMON,
7e1b7485 89 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
4edb29b7 90#ifndef OPENSSL_NO_SOCK
88d96983 91 OPT_PROXY, OPT_NO_PROXY,
4edb29b7 92#endif
7e1b7485
RS
93 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
94 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
95 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
96 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
97 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
98 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
fd3397fc
RL
99 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
100 OPT_NOCAPATH, OPT_NOCASTORE,
7e1b7485 101 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
c6724060 102 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
7e1b7485 103 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
04940147 104 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
1cf20ca3 105 OPT_PASSIN,
0770c882 106 OPT_RCID,
7e1b7485 107 OPT_V_ENUM,
3e3c7c36 108 OPT_MD,
6bd4e3f2 109 OPT_MULTI, OPT_PROV_ENUM
7e1b7485
RS
110} OPTION_CHOICE;
111
44c83ebd 112const OPTIONS ocsp_options[] = {
5388f986 113 OPT_SECTION("General"),
7e1b7485 114 {"help", OPT_HELP, '-', "Display this summary"},
12d56b29 115 {"ignore_err", OPT_IGNORE_ERR, '-',
bbe9c3d5 116 "Ignore error on OCSP request or response and continue running"},
5388f986
RS
117 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
118 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
119 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
120 {"no-CAfile", OPT_NOCAFILE, '-',
121 "Do not load the default certificates file"},
122 {"no-CApath", OPT_NOCAPATH, '-',
123 "Do not load certificates from the default certificates directory"},
00493490 124 {"no-CAstore", OPT_NOCASTORE, '-',
5388f986
RS
125 "Do not load certificates from the default certificates store"},
126
127 OPT_SECTION("Responder"),
128 {"timeout", OPT_TIMEOUT, 'p',
129 "Connection timeout (in seconds) to the OCSP responder"},
7e1b7485
RS
130 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
131 "Don't include any certificates in response"},
582311d7 132#ifdef HTTP_DAEMON
3e3c7c36 133 {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
1ae56f2f 134#endif
7e1b7485
RS
135 {"no_certs", OPT_NO_CERTS, '-',
136 "Don't include any certificates in signed request"},
5388f986
RS
137 {"badsig", OPT_BADSIG, '-',
138 "Corrupt last byte of loaded OSCP response signature (for test)"},
139 {"CA", OPT_CA, '<', "CA certificate"},
140 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
141 {"nrequest", OPT_REQUEST, 'p',
142 "Number of requests to accept (default unlimited)"},
143 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
144 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
145 {"sign_other", OPT_SIGN_OTHER, '<',
146 "Additional certificates to include in signed request"},
147 {"index", OPT_INDEX, '<', "Certificate status index file"},
148 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
149 {"rsigner", OPT_RSIGNER, '<',
150 "Responder certificate to sign responses with"},
151 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
1cf20ca3 152 {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
5388f986
RS
153 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
154 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
155 {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
156 {"header", OPT_HEADER, 's', "key=value header to add"},
157 {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
158 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
159
160 OPT_SECTION("Client"),
161 {"url", OPT_URL, 's', "Responder URL"},
162 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
f4706b16 163 {"port", OPT_PORT, 'N', "Port to run responder on"},
88d96983 164 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
4edb29b7 165#ifndef OPENSSL_NO_SOCK
88d96983
DDO
166 {"proxy", OPT_PROXY, 's',
167 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
168 {"no_proxy", OPT_NO_PROXY, 's',
169 "List of addresses of servers not to use HTTP(S) proxy for"},
170 {OPT_MORE_STR, 0, 0,
171 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
4edb29b7 172#endif
5388f986
RS
173 {"out", OPT_OUTFILE, '>', "Output filename"},
174 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
175 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
176 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
7e1b7485
RS
177 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
178 "Don't check signature on response"},
5388f986
RS
179 {"resp_key_id", OPT_RESP_KEY_ID, '-',
180 "Identify response by signing certificate key ID"},
7e1b7485
RS
181 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
182 "Don't check signing certificate"},
5388f986
RS
183 {"text", OPT_TEXT, '-', "Print text form of request and response"},
184 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
185 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
7e1b7485
RS
186 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
187 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
188 "Don't do additional checks on signing certificate"},
12d56b29
F
189 {"no_explicit", OPT_NO_EXPLICIT, '-',
190 "Do not explicitly check the chain, just verify the root"},
7e1b7485
RS
191 {"trust_other", OPT_TRUST_OTHER, '-',
192 "Don't verify additional certificates"},
193 {"no_intern", OPT_NO_INTERN, '-',
194 "Don't search certificates contained in response for signer"},
7e1b7485 195 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
7e1b7485 196 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
7e1b7485
RS
197 {"verify_other", OPT_VERIFY_OTHER, '<',
198 "Additional certificates to search for signer"},
5388f986
RS
199 {"cert", OPT_CERT, '<', "Certificate to check"},
200 {"serial", OPT_SERIAL, 's', "Serial number to check"},
7e1b7485
RS
201 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
202 "Maximum validity discrepancy in seconds"},
7e1b7485
RS
203 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
204 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
205 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
c6724060 206 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
5388f986
RS
207 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
208
7e1b7485 209 OPT_V_OPTIONS,
6bd4e3f2 210 OPT_PROV_OPTIONS,
7e1b7485
RS
211 {NULL}
212};
213
214int ocsp_main(int argc, char **argv)
0f113f3e 215{
7e1b7485 216 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
606a417f 217 EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
b4dd21a7 218 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
6302bbd2 219 int trailing_md = 0;
7e1b7485
RS
220 CA_DB *rdb = NULL;
221 EVP_PKEY *key = NULL, *rkey = NULL;
222 OCSP_BASICRESP *bs = NULL;
0f113f3e
MC
223 OCSP_REQUEST *req = NULL;
224 OCSP_RESPONSE *resp = NULL;
7e1b7485
RS
225 STACK_OF(CONF_VALUE) *headers = NULL;
226 STACK_OF(OCSP_CERTID) *ids = NULL;
227 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
228 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
88806cfc 229 STACK_OF(X509) *issuers = NULL;
3bb0f989
TS
230 X509 *issuer = NULL, *cert = NULL;
231 STACK_OF(X509) *rca_cert = NULL;
606a417f 232 EVP_MD *resp_certid_md = NULL;
0f113f3e 233 X509 *signer = NULL, *rsigner = NULL;
0f113f3e
MC
234 X509_STORE *store = NULL;
235 X509_VERIFY_PARAM *vpm = NULL;
fd3397fc 236 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
d0190e11 237 char *header, *value, *respdigname = NULL;
7e1b7485 238 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
4edb29b7 239#ifndef OPENSSL_NO_SOCK
88d96983
DDO
240 char *opt_proxy = NULL;
241 char *opt_no_proxy = NULL;
4edb29b7 242#endif
7e1b7485
RS
243 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
244 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
245 char *rsignfile = NULL, *rkeyfile = NULL;
1cf20ca3 246 char *passinarg = NULL, *passin = NULL;
0f113f3e 247 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
7e1b7485
RS
248 char *signfile = NULL, *keyfile = NULL;
249 char *thost = NULL, *tport = NULL, *tpath = NULL;
fd3397fc 250 int noCAfile = 0, noCApath = 0, noCAstore = 0;
7e1b7485
RS
251 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
252 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
5a2ba207 253 int req_text = 0, resp_text = 0, res, ret = 1;
f9e55034 254 int req_timeout = -1;
7e1b7485 255 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
0f113f3e 256 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
7e1b7485 257 OPTION_CHOICE o;
0f113f3e 258
77b8b49b
P
259 if ((reqnames = sk_OPENSSL_STRING_new_null()) == NULL
260 || (ids = sk_OCSP_CERTID_new_null()) == NULL
261 || (vpm = X509_VERIFY_PARAM_new()) == NULL)
7e1b7485 262 goto end;
7e1b7485
RS
263
264 prog = opt_init(argc, argv, ocsp_options);
265 while ((o = opt_next()) != OPT_EOF) {
266 switch (o) {
267 case OPT_EOF:
268 case OPT_ERR:
269 opthelp:
270 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
271 goto end;
272 case OPT_HELP:
273 ret = 0;
274 opt_help(ocsp_options);
275 goto end;
276 case OPT_OUTFILE:
277 outfile = opt_arg();
278 break;
279 case OPT_TIMEOUT:
f9e55034 280#ifndef OPENSSL_NO_SOCK
7e1b7485 281 req_timeout = atoi(opt_arg());
f9e55034 282#endif
7e1b7485
RS
283 break;
284 case OPT_URL:
b548a1f1
RS
285 OPENSSL_free(thost);
286 OPENSSL_free(tport);
287 OPENSSL_free(tpath);
4b8d8e2a 288 thost = tport = tpath = NULL;
7932982b
DDO
289 if (!OSSL_HTTP_parse_url(opt_arg(), &use_ssl, NULL /* userinfo */,
290 &host, &port, NULL /* port_num */,
291 &path, NULL /* qry */, NULL /* frag */)) {
292 BIO_printf(bio_err, "%s Error parsing -url argument\n", prog);
7e1b7485
RS
293 goto end;
294 }
295 thost = host;
296 tport = port;
297 tpath = path;
298 break;
299 case OPT_HOST:
300 host = opt_arg();
301 break;
302 case OPT_PORT:
303 port = opt_arg();
304 break;
88d96983
DDO
305 case OPT_PATH:
306 path = opt_arg();
307 break;
4edb29b7 308#ifndef OPENSSL_NO_SOCK
88d96983
DDO
309 case OPT_PROXY:
310 opt_proxy = opt_arg();
311 break;
312 case OPT_NO_PROXY:
313 opt_no_proxy = opt_arg();
314 break;
4edb29b7 315#endif
7e1b7485 316 case OPT_IGNORE_ERR:
0f113f3e 317 ignore_err = 1;
7e1b7485
RS
318 break;
319 case OPT_NOVERIFY:
0f113f3e 320 noverify = 1;
7e1b7485
RS
321 break;
322 case OPT_NONCE:
0f113f3e 323 add_nonce = 2;
7e1b7485
RS
324 break;
325 case OPT_NO_NONCE:
0f113f3e 326 add_nonce = 0;
7e1b7485
RS
327 break;
328 case OPT_RESP_NO_CERTS:
0f113f3e 329 rflags |= OCSP_NOCERTS;
7e1b7485
RS
330 break;
331 case OPT_RESP_KEY_ID:
0f113f3e 332 rflags |= OCSP_RESPID_KEY;
7e1b7485
RS
333 break;
334 case OPT_NO_CERTS:
0f113f3e 335 sign_flags |= OCSP_NOCERTS;
7e1b7485
RS
336 break;
337 case OPT_NO_SIGNATURE_VERIFY:
0f113f3e 338 verify_flags |= OCSP_NOSIGS;
7e1b7485
RS
339 break;
340 case OPT_NO_CERT_VERIFY:
0f113f3e 341 verify_flags |= OCSP_NOVERIFY;
7e1b7485
RS
342 break;
343 case OPT_NO_CHAIN:
0f113f3e 344 verify_flags |= OCSP_NOCHAIN;
7e1b7485
RS
345 break;
346 case OPT_NO_CERT_CHECKS:
0f113f3e 347 verify_flags |= OCSP_NOCHECKS;
7e1b7485
RS
348 break;
349 case OPT_NO_EXPLICIT:
0f113f3e 350 verify_flags |= OCSP_NOEXPLICIT;
7e1b7485
RS
351 break;
352 case OPT_TRUST_OTHER:
0f113f3e 353 verify_flags |= OCSP_TRUSTOTHER;
7e1b7485
RS
354 break;
355 case OPT_NO_INTERN:
0f113f3e 356 verify_flags |= OCSP_NOINTERN;
7e1b7485
RS
357 break;
358 case OPT_BADSIG:
0f113f3e 359 badsig = 1;
7e1b7485
RS
360 break;
361 case OPT_TEXT:
362 req_text = resp_text = 1;
363 break;
364 case OPT_REQ_TEXT:
0f113f3e 365 req_text = 1;
7e1b7485
RS
366 break;
367 case OPT_RESP_TEXT:
0f113f3e 368 resp_text = 1;
7e1b7485
RS
369 break;
370 case OPT_REQIN:
371 reqin = opt_arg();
372 break;
373 case OPT_RESPIN:
374 respin = opt_arg();
375 break;
376 case OPT_SIGNER:
377 signfile = opt_arg();
378 break;
379 case OPT_VAFILE:
380 verify_certfile = opt_arg();
381 verify_flags |= OCSP_TRUSTOTHER;
382 break;
383 case OPT_SIGN_OTHER:
384 sign_certfile = opt_arg();
385 break;
386 case OPT_VERIFY_OTHER:
387 verify_certfile = opt_arg();
388 break;
389 case OPT_CAFILE:
390 CAfile = opt_arg();
391 break;
392 case OPT_CAPATH:
393 CApath = opt_arg();
394 break;
fd3397fc
RL
395 case OPT_CASTORE:
396 CAstore = opt_arg();
397 break;
2b6bcb70
MC
398 case OPT_NOCAFILE:
399 noCAfile = 1;
400 break;
401 case OPT_NOCAPATH:
402 noCApath = 1;
403 break;
fd3397fc
RL
404 case OPT_NOCASTORE:
405 noCAstore = 1;
406 break;
7e1b7485
RS
407 case OPT_V_CASES:
408 if (!opt_verify(o, vpm))
0f113f3e 409 goto end;
7e1b7485
RS
410 vpmtouched++;
411 break;
412 case OPT_VALIDITY_PERIOD:
413 opt_long(opt_arg(), &nsec);
414 break;
415 case OPT_STATUS_AGE:
416 opt_long(opt_arg(), &maxage);
417 break;
418 case OPT_SIGNKEY:
419 keyfile = opt_arg();
420 break;
421 case OPT_REQOUT:
422 reqout = opt_arg();
423 break;
424 case OPT_RESPOUT:
425 respout = opt_arg();
426 break;
c6724060 427 case OPT_ISSUER:
d382e796 428 issuer = load_cert(opt_arg(), FORMAT_UNDEF, "issuer certificate");
c6724060
RS
429 if (issuer == NULL)
430 goto end;
bb7fc98c
DSH
431 if (issuers == NULL) {
432 if ((issuers = sk_X509_new_null()) == NULL)
433 goto end;
434 }
d5e66eab
SL
435 if (!sk_X509_push(issuers, issuer))
436 goto end;
c6724060 437 break;
7e1b7485
RS
438 case OPT_CERT:
439 X509_free(cert);
d382e796 440 cert = load_cert(opt_arg(), FORMAT_UNDEF, "certificate");
7e1b7485
RS
441 if (cert == NULL)
442 goto end;
443 if (cert_id_md == NULL)
606a417f 444 cert_id_md = (EVP_MD *)EVP_sha1();
7e1b7485
RS
445 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
446 goto end;
447 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
448 goto end;
6302bbd2 449 trailing_md = 0;
7e1b7485
RS
450 break;
451 case OPT_SERIAL:
452 if (cert_id_md == NULL)
606a417f 453 cert_id_md = (EVP_MD *)EVP_sha1();
7e1b7485
RS
454 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
455 goto end;
456 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
457 goto end;
6302bbd2 458 trailing_md = 0;
7e1b7485
RS
459 break;
460 case OPT_INDEX:
461 ridx_filename = opt_arg();
462 break;
463 case OPT_CA:
464 rca_filename = opt_arg();
465 break;
466 case OPT_NMIN:
d830526c 467 nmin = opt_int_arg();
0f113f3e
MC
468 if (ndays == -1)
469 ndays = 0;
7e1b7485
RS
470 break;
471 case OPT_REQUEST:
d830526c 472 accept_count = opt_int_arg();
7e1b7485
RS
473 break;
474 case OPT_NDAYS:
475 ndays = atoi(opt_arg());
476 break;
477 case OPT_RSIGNER:
478 rsignfile = opt_arg();
479 break;
480 case OPT_RKEY:
481 rkeyfile = opt_arg();
482 break;
1cf20ca3 483 case OPT_PASSIN:
484 passinarg = opt_arg();
485 break;
7e1b7485
RS
486 case OPT_ROTHER:
487 rcertfile = opt_arg();
488 break;
16e1b281 489 case OPT_RMD: /* Response MessageDigest */
d0190e11 490 respdigname = opt_arg();
7e1b7485 491 break;
04940147 492 case OPT_RSIGOPT:
b4dd21a7
DC
493 if (rsign_sigopts == NULL)
494 rsign_sigopts = sk_OPENSSL_STRING_new_null();
e6c2f964
SL
495 if (rsign_sigopts == NULL
496 || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
b4dd21a7
DC
497 goto end;
498 break;
7e1b7485
RS
499 case OPT_HEADER:
500 header = opt_arg();
501 value = strchr(header, '=');
502 if (value == NULL) {
503 BIO_printf(bio_err, "Missing = in header key=value\n");
504 goto opthelp;
505 }
506 *value++ = '\0';
507 if (!X509V3_add_value(header, value, &headers))
508 goto end;
509 break;
0770c882 510 case OPT_RCID:
606a417f 511 if (!opt_md(opt_arg(), &resp_certid_md))
0770c882
TS
512 goto opthelp;
513 break;
7e1b7485 514 case OPT_MD:
6302bbd2 515 if (trailing_md) {
7e1b7485
RS
516 BIO_printf(bio_err,
517 "%s: Digest must be before -cert or -serial\n",
518 prog);
519 goto opthelp;
520 }
521 if (!opt_md(opt_unknown(), &cert_id_md))
522 goto opthelp;
6302bbd2 523 trailing_md = 1;
7e1b7485 524 break;
3e3c7c36 525 case OPT_MULTI:
582311d7 526#ifdef HTTP_DAEMON
3e3c7c36 527 multi = atoi(opt_arg());
1ae56f2f 528#endif
a7fb4fa1 529 break;
6bd4e3f2
P
530 case OPT_PROV_CASES:
531 if (!opt_provider(o))
532 goto end;
533 break;
0f113f3e 534 }
0f113f3e 535 }
021410ea
RS
536
537 /* No extra arguments. */
d9f07357 538 if (!opt_check_rest_arg(NULL))
021410ea
RS
539 goto opthelp;
540
6302bbd2
DSH
541 if (trailing_md) {
542 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
543 prog);
544 goto opthelp;
545 }
0f113f3e 546
d0190e11
RS
547 if (respdigname != NULL) {
548 if (!opt_md(respdigname, &rsign_md))
549 goto end;
550 }
551
0f113f3e 552 /* Have we anything to do? */
3e3c7c36 553 if (req == NULL && reqin == NULL
2234212c 554 && respin == NULL && !(port != NULL && ridx_filename != NULL))
7e1b7485 555 goto opthelp;
0f113f3e 556
bdd58d98 557 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
7e1b7485 558 if (out == NULL)
0f113f3e 559 goto end;
0f113f3e 560
2234212c 561 if (req == NULL && (add_nonce != 2))
0f113f3e
MC
562 add_nonce = 0;
563
2234212c 564 if (req == NULL && reqin != NULL) {
bdd58d98 565 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
7e1b7485 566 if (derbio == NULL)
0f113f3e 567 goto end;
0f113f3e
MC
568 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
569 BIO_free(derbio);
2234212c 570 if (req == NULL) {
0f113f3e
MC
571 BIO_printf(bio_err, "Error reading OCSP request\n");
572 goto end;
573 }
574 }
575
2234212c 576 if (req == NULL && port != NULL) {
582311d7 577#ifndef OPENSSL_NO_SOCK
4599ea9f 578 acbio = http_server_init(prog, port, -1);
2234212c 579 if (acbio == NULL)
0f113f3e 580 goto end;
582311d7
DDO
581#else
582 BIO_printf(bio_err, "Cannot act as server - sockets not supported\n");
583 goto end;
584#endif
0f113f3e
MC
585 }
586
2234212c
PY
587 if (rsignfile != NULL) {
588 if (rkeyfile == NULL)
0f113f3e 589 rkeyfile = rsignfile;
d382e796 590 rsigner = load_cert(rsignfile, FORMAT_UNDEF, "responder certificate");
2234212c 591 if (rsigner == NULL) {
0f113f3e
MC
592 BIO_printf(bio_err, "Error loading responder certificate\n");
593 goto end;
594 }
ea51096e 595 if (!load_certs(rca_filename, 0, &rca_cert, NULL, "CA certificates"))
3bb0f989 596 goto end;
2234212c 597 if (rcertfile != NULL) {
ea51096e 598 if (!load_certs(rcertfile, 0, &rother, NULL,
0996dc54 599 "responder other certificates"))
0f113f3e
MC
600 goto end;
601 }
1cf20ca3 602 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
603 BIO_printf(bio_err, "Error getting password\n");
604 goto end;
605 }
d382e796 606 rkey = load_key(rkeyfile, FORMAT_UNDEF, 0, passin, NULL,
0f113f3e 607 "responder private key");
2234212c 608 if (rkey == NULL)
0f113f3e
MC
609 goto end;
610 }
c7d5ea26 611
3e3c7c36 612 if (ridx_filename != NULL
e4693110 613 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
c7d5ea26
VD
614 BIO_printf(bio_err,
615 "Responder mode requires certificate, key, and CA.\n");
616 goto end;
617 }
618
3e3c7c36 619 if (ridx_filename != NULL) {
c7d5ea26 620 rdb = load_index(ridx_filename, NULL);
a4107d73 621 if (rdb == NULL || index_index(rdb) <= 0) {
2c6e33d8
FM
622 BIO_printf(bio_err,
623 "Problem with index file: %s (could not load/parse file)\n",
624 ridx_filename);
c7d5ea26
VD
625 ret = 1;
626 goto end;
627 }
628 }
629
582311d7 630#ifdef HTTP_DAEMON
3e3c7c36 631 if (multi && acbio != NULL)
582311d7 632 spawn_loop(prog);
3e3c7c36 633 if (acbio != NULL && req_timeout > 0)
e9b95e42 634 signal(SIGALRM, socket_timeout);
3e3c7c36
VD
635#endif
636
2234212c 637 if (acbio != NULL)
582311d7 638 log_message(prog, LOG_INFO, "waiting for OCSP client connections...");
0f113f3e 639
2234212c 640redo_accept:
0f113f3e 641
2234212c 642 if (acbio != NULL) {
582311d7 643#ifdef HTTP_DAEMON
3e3c7c36
VD
644 if (index_changed(rdb)) {
645 CA_DB *newrdb = load_index(ridx_filename, NULL);
646
a4107d73 647 if (newrdb != NULL && index_index(newrdb) > 0) {
3e3c7c36
VD
648 free_index(rdb);
649 rdb = newrdb;
650 } else {
a4107d73 651 free_index(newrdb);
582311d7 652 log_message(prog, LOG_ERR, "error reloading updated index: %s",
3e3c7c36
VD
653 ridx_filename);
654 }
655 }
1ae56f2f 656#endif
3e3c7c36
VD
657
658 req = NULL;
4599ea9f 659 res = do_responder(&req, &cbio, acbio, req_timeout);
5a2ba207 660 if (res == 0)
3e3c7c36
VD
661 goto redo_accept;
662
2234212c 663 if (req == NULL) {
5a2ba207
DDO
664 if (res == 1) {
665 resp =
666 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
667 NULL);
668 send_ocsp_response(cbio, resp);
669 }
0f113f3e
MC
670 goto done_resp;
671 }
672 }
673
2234212c
PY
674 if (req == NULL
675 && (signfile != NULL || reqout != NULL
676 || host != NULL || add_nonce || ridx_filename != NULL)) {
0f113f3e
MC
677 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
678 goto end;
679 }
680
72b89b8e
DB
681 if (req != NULL && add_nonce) {
682 if (!OCSP_request_add1_nonce(req, NULL, -1))
683 goto end;
684 }
0f113f3e 685
2234212c
PY
686 if (signfile != NULL) {
687 if (keyfile == NULL)
0f113f3e 688 keyfile = signfile;
d382e796 689 signer = load_cert(signfile, FORMAT_UNDEF, "signer certificate");
2234212c 690 if (signer == NULL) {
0f113f3e
MC
691 BIO_printf(bio_err, "Error loading signer certificate\n");
692 goto end;
693 }
2234212c 694 if (sign_certfile != NULL) {
ea51096e 695 if (!load_certs(sign_certfile, 0, &sign_other, NULL,
0996dc54 696 "signer certificates"))
0f113f3e
MC
697 goto end;
698 }
d382e796 699 key = load_key(keyfile, FORMAT_UNDEF, 0, NULL, NULL,
0f113f3e 700 "signer private key");
2234212c 701 if (key == NULL)
0f113f3e
MC
702 goto end;
703
e6c2f964
SL
704 if (!OCSP_request_sign(req, signer, key, NULL,
705 sign_other, sign_flags)) {
0f113f3e
MC
706 BIO_printf(bio_err, "Error signing OCSP request\n");
707 goto end;
708 }
709 }
710
2234212c 711 if (req_text && req != NULL)
0f113f3e
MC
712 OCSP_REQUEST_print(out, req, 0);
713
2234212c 714 if (reqout != NULL) {
bdd58d98 715 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
7e1b7485 716 if (derbio == NULL)
0f113f3e 717 goto end;
0f113f3e
MC
718 i2d_OCSP_REQUEST_bio(derbio, req);
719 BIO_free(derbio);
720 }
721
2234212c 722 if (rdb != NULL) {
b4dd21a7 723 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
e6c2f964
SL
724 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays,
725 badsig, resp_certid_md);
2234212c 726 if (cbio != NULL)
0f113f3e 727 send_ocsp_response(cbio, resp);
2234212c 728 } else if (host != NULL) {
1ae56f2f 729#ifndef OPENSSL_NO_SOCK
88d96983
DDO
730 resp = process_responder(req, host, port, path, opt_proxy, opt_no_proxy,
731 use_ssl, headers, req_timeout);
2234212c 732 if (resp == NULL)
0f113f3e 733 goto end;
1ae56f2f 734#else
0f113f3e 735 BIO_printf(bio_err,
582311d7 736 "Error creating connect BIO - sockets not supported\n");
0f113f3e 737 goto end;
1ae56f2f 738#endif
2234212c 739 } else if (respin != NULL) {
bdd58d98 740 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
7e1b7485 741 if (derbio == NULL)
0f113f3e 742 goto end;
0f113f3e
MC
743 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
744 BIO_free(derbio);
2234212c 745 if (resp == NULL) {
0f113f3e
MC
746 BIO_printf(bio_err, "Error reading OCSP response\n");
747 goto end;
748 }
0f113f3e
MC
749 } else {
750 ret = 0;
751 goto end;
752 }
753
754 done_resp:
755
2234212c 756 if (respout != NULL) {
bdd58d98 757 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
7e1b7485 758 if (derbio == NULL)
0f113f3e 759 goto end;
0f113f3e
MC
760 i2d_OCSP_RESPONSE_bio(derbio, resp);
761 BIO_free(derbio);
762 }
763
764 i = OCSP_response_status(resp);
0f113f3e
MC
765 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
766 BIO_printf(out, "Responder Error: %s (%d)\n",
767 OCSP_response_status_str(i), i);
c324ecfb 768 if (!ignore_err)
3e3c7c36 769 goto end;
0f113f3e
MC
770 }
771
772 if (resp_text)
773 OCSP_RESPONSE_print(out, resp, 0);
774
775 /* If running as responder don't verify our own response */
2234212c 776 if (cbio != NULL) {
e46bcca2
AE
777 /* If not unlimited, see if we took all we should. */
778 if (accept_count != -1 && --accept_count <= 0) {
7e1b7485
RS
779 ret = 0;
780 goto end;
0f113f3e 781 }
7e1b7485
RS
782 BIO_free_all(cbio);
783 cbio = NULL;
784 OCSP_REQUEST_free(req);
785 req = NULL;
786 OCSP_RESPONSE_free(resp);
787 resp = NULL;
788 goto redo_accept;
789 }
2234212c 790 if (ridx_filename != NULL) {
0f113f3e
MC
791 ret = 0;
792 goto end;
793 }
794
2234212c 795 if (store == NULL) {
fd3397fc
RL
796 store = setup_verify(CAfile, noCAfile, CApath, noCApath,
797 CAstore, noCAstore);
7e1b7485
RS
798 if (!store)
799 goto end;
800 }
801 if (vpmtouched)
0f113f3e 802 X509_STORE_set1_param(store, vpm);
2234212c 803 if (verify_certfile != NULL) {
ea51096e 804 if (!load_certs(verify_certfile, 0, &verify_other, NULL,
b3c5aadf 805 "validator certificates"))
0f113f3e
MC
806 goto end;
807 }
808
809 bs = OCSP_response_get1_basic(resp);
2234212c 810 if (bs == NULL) {
0f113f3e
MC
811 BIO_printf(bio_err, "Error parsing response\n");
812 goto end;
813 }
814
815 ret = 0;
816
817 if (!noverify) {
2234212c 818 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
0f113f3e
MC
819 if (i == -1)
820 BIO_printf(bio_err, "WARNING: no nonce in response\n");
821 else {
822 BIO_printf(bio_err, "Nonce Verify error\n");
823 ret = 1;
824 goto end;
825 }
826 }
827
828 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
c6724060
RS
829 if (i <= 0 && issuers) {
830 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
831 if (i > 0)
832 ERR_clear_error();
833 }
0f113f3e
MC
834 if (i <= 0) {
835 BIO_printf(bio_err, "Response Verify Failure\n");
836 ERR_print_errors(bio_err);
837 ret = 1;
2234212c 838 } else {
0f113f3e 839 BIO_printf(bio_err, "Response verify OK\n");
2234212c 840 }
0f113f3e
MC
841 }
842
639bb581
TM
843 if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
844 ret = 1;
0f113f3e
MC
845
846 end:
847 ERR_print_errors(bio_err);
848 X509_free(signer);
849 X509_STORE_free(store);
222561fe 850 X509_VERIFY_PARAM_free(vpm);
89623f84 851 sk_OPENSSL_STRING_free(rsign_sigopts);
0f113f3e
MC
852 EVP_PKEY_free(key);
853 EVP_PKEY_free(rkey);
606a417f
RS
854 EVP_MD_free(cert_id_md);
855 EVP_MD_free(rsign_md);
856 EVP_MD_free(resp_certid_md);
0f113f3e 857 X509_free(cert);
79b2a2f2 858 OSSL_STACK_OF_X509_free(issuers);
0f113f3e 859 X509_free(rsigner);
79b2a2f2 860 OSSL_STACK_OF_X509_free(rca_cert);
0f113f3e
MC
861 free_index(rdb);
862 BIO_free_all(cbio);
863 BIO_free_all(acbio);
4e525a0b 864 BIO_free_all(out);
0f113f3e
MC
865 OCSP_REQUEST_free(req);
866 OCSP_RESPONSE_free(resp);
867 OCSP_BASICRESP_free(bs);
868 sk_OPENSSL_STRING_free(reqnames);
869 sk_OCSP_CERTID_free(ids);
79b2a2f2
DDO
870 OSSL_STACK_OF_X509_free(sign_other);
871 OSSL_STACK_OF_X509_free(verify_other);
0f113f3e 872 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
b548a1f1
RS
873 OPENSSL_free(thost);
874 OPENSSL_free(tport);
875 OPENSSL_free(tpath);
0f113f3e 876
26a7d938 877 return ret;
5782ceb2
DSH
878}
879
582311d7 880#ifdef HTTP_DAEMON
3e3c7c36
VD
881
882static int index_changed(CA_DB *rdb)
883{
884 struct stat sb;
885
886 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
887 if (rdb->dbst.st_mtime != sb.st_mtime
888 || rdb->dbst.st_ctime != sb.st_ctime
889 || rdb->dbst.st_ino != sb.st_ino
890 || rdb->dbst.st_dev != sb.st_dev) {
891 syslog(LOG_INFO, "index file changed, reloading");
892 return 1;
893 }
894 }
895 return 0;
896}
897
1ae56f2f 898#endif
3e3c7c36 899
0f113f3e
MC
900static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
901 const EVP_MD *cert_id_md, X509 *issuer,
902 STACK_OF(OCSP_CERTID) *ids)
903{
904 OCSP_CERTID *id;
2234212c
PY
905
906 if (issuer == NULL) {
0f113f3e
MC
907 BIO_printf(bio_err, "No issuer certificate specified\n");
908 return 0;
909 }
96487cdd 910 if (*req == NULL)
0f113f3e 911 *req = OCSP_REQUEST_new();
96487cdd 912 if (*req == NULL)
0f113f3e
MC
913 goto err;
914 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
2234212c 915 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
0f113f3e
MC
916 goto err;
917 if (!OCSP_request_add0_id(*req, id))
918 goto err;
919 return 1;
920
921 err:
922 BIO_printf(bio_err, "Error Creating OCSP request\n");
923 return 0;
924}
925
926static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
927 const EVP_MD *cert_id_md, X509 *issuer,
928 STACK_OF(OCSP_CERTID) *ids)
929{
930 OCSP_CERTID *id;
8cc86b81 931 const X509_NAME *iname;
0f113f3e
MC
932 ASN1_BIT_STRING *ikey;
933 ASN1_INTEGER *sno;
2234212c
PY
934
935 if (issuer == NULL) {
0f113f3e
MC
936 BIO_printf(bio_err, "No issuer certificate specified\n");
937 return 0;
938 }
96487cdd 939 if (*req == NULL)
0f113f3e 940 *req = OCSP_REQUEST_new();
96487cdd 941 if (*req == NULL)
0f113f3e
MC
942 goto err;
943 iname = X509_get_subject_name(issuer);
944 ikey = X509_get0_pubkey_bitstr(issuer);
945 sno = s2i_ASN1_INTEGER(NULL, serial);
2234212c 946 if (sno == NULL) {
0f113f3e
MC
947 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
948 return 0;
949 }
950 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
951 ASN1_INTEGER_free(sno);
96487cdd 952 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
0f113f3e
MC
953 goto err;
954 if (!OCSP_request_add0_id(*req, id))
955 goto err;
956 return 1;
957
958 err:
959 BIO_printf(bio_err, "Error Creating OCSP request\n");
960 return 0;
961}
73758d43 962
639bb581 963static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
0f113f3e
MC
964 STACK_OF(OPENSSL_STRING) *names,
965 STACK_OF(OCSP_CERTID) *ids, long nsec,
966 long maxage)
967{
968 OCSP_CERTID *id;
cc696296 969 const char *name;
7e1b7485 970 int i, status, reason;
0f113f3e 971 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
639bb581 972 int ret = 1;
0f113f3e 973
639bb581
TM
974 if (req == NULL || !sk_OPENSSL_STRING_num(names))
975 return 1;
976
977 if (bs == NULL || !sk_OCSP_CERTID_num(ids))
978 return 0;
0f113f3e
MC
979
980 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
981 id = sk_OCSP_CERTID_value(ids, i);
982 name = sk_OPENSSL_STRING_value(names, i);
983 BIO_printf(out, "%s: ", name);
984
985 if (!OCSP_resp_find_status(bs, id, &status, &reason,
986 &rev, &thisupd, &nextupd)) {
987 BIO_puts(out, "ERROR: No Status found.\n");
639bb581 988 ret = 0;
0f113f3e
MC
989 continue;
990 }
991
992 /*
993 * Check validity: if invalid write to output BIO so we know which
994 * response this refers to.
995 */
996 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
997 BIO_puts(out, "WARNING: Status times invalid.\n");
998 ERR_print_errors(out);
999 }
1000 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
1001
1002 BIO_puts(out, "\tThis Update: ");
1003 ASN1_GENERALIZEDTIME_print(out, thisupd);
1004 BIO_puts(out, "\n");
1005
1006 if (nextupd) {
1007 BIO_puts(out, "\tNext Update: ");
1008 ASN1_GENERALIZEDTIME_print(out, nextupd);
1009 BIO_puts(out, "\n");
1010 }
1011
1012 if (status != V_OCSP_CERTSTATUS_REVOKED)
1013 continue;
1014
1015 if (reason != -1)
1016 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
1017
1018 BIO_puts(out, "\tRevocation Time: ");
1019 ASN1_GENERALIZEDTIME_print(out, rev);
1020 BIO_puts(out, "\n");
1021 }
639bb581 1022 return ret;
0f113f3e
MC
1023}
1024
b4dd21a7 1025static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
3bb0f989 1026 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
0f113f3e 1027 EVP_PKEY *rkey, const EVP_MD *rmd,
b4dd21a7 1028 STACK_OF(OPENSSL_STRING) *sigopts,
0f113f3e 1029 STACK_OF(X509) *rother, unsigned long flags,
0770c882
TS
1030 int nmin, int ndays, int badsig,
1031 const EVP_MD *resp_md)
0f113f3e
MC
1032{
1033 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
3bb0f989 1034 OCSP_CERTID *cid;
0f113f3e 1035 OCSP_BASICRESP *bs = NULL;
7e1b7485 1036 int i, id_count;
b4dd21a7
DC
1037 EVP_MD_CTX *mctx = NULL;
1038 EVP_PKEY_CTX *pkctx = NULL;
0f113f3e
MC
1039
1040 id_count = OCSP_request_onereq_count(req);
1041
1042 if (id_count <= 0) {
1043 *resp =
1044 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1045 goto end;
1046 }
1047
1048 bs = OCSP_BASICRESP_new();
1049 thisupd = X509_gmtime_adj(NULL, 0);
1050 if (ndays != -1)
9aa00b18 1051 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
0f113f3e
MC
1052
1053 /* Examine each certificate id in the request */
1054 for (i = 0; i < id_count; i++) {
1055 OCSP_ONEREQ *one;
1056 ASN1_INTEGER *serial;
1057 char **inf;
3bb0f989
TS
1058 int jj;
1059 int found = 0;
0f113f3e
MC
1060 ASN1_OBJECT *cert_id_md_oid;
1061 const EVP_MD *cert_id_md;
0770c882
TS
1062 OCSP_CERTID *cid_resp_md = NULL;
1063
0f113f3e
MC
1064 one = OCSP_request_onereq_get0(req, i);
1065 cid = OCSP_onereq_get0_id(one);
1066
1067 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1068
1069 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
2234212c 1070 if (cert_id_md == NULL) {
0f113f3e
MC
1071 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1072 NULL);
1073 goto end;
1074 }
3bb0f989
TS
1075 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
1076 X509 *ca_cert = sk_X509_value(ca, jj);
1077 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
1078
0770c882 1079 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
3bb0f989 1080 found = 1;
0770c882
TS
1081 if (resp_md != NULL)
1082 cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
1083 }
3bb0f989
TS
1084 OCSP_CERTID_free(ca_id);
1085 }
0770c882
TS
1086 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1087 inf = lookup_serial(db, serial);
1088
1089 /* at this point, we can have cid be an alias of cid_resp_md */
1090 cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
0f113f3e 1091
3bb0f989 1092 if (!found) {
0f113f3e
MC
1093 OCSP_basic_add1_status(bs, cid,
1094 V_OCSP_CERTSTATUS_UNKNOWN,
1095 0, NULL, thisupd, nextupd);
1096 continue;
1097 }
2234212c 1098 if (inf == NULL) {
0f113f3e
MC
1099 OCSP_basic_add1_status(bs, cid,
1100 V_OCSP_CERTSTATUS_UNKNOWN,
1101 0, NULL, thisupd, nextupd);
2234212c 1102 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
0f113f3e
MC
1103 OCSP_basic_add1_status(bs, cid,
1104 V_OCSP_CERTSTATUS_GOOD,
1105 0, NULL, thisupd, nextupd);
2234212c 1106 } else if (inf[DB_type][0] == DB_TYPE_REV) {
0f113f3e
MC
1107 ASN1_OBJECT *inst = NULL;
1108 ASN1_TIME *revtm = NULL;
1109 ASN1_GENERALIZEDTIME *invtm = NULL;
1110 OCSP_SINGLERESP *single;
1111 int reason = -1;
0770c882 1112
0f113f3e
MC
1113 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1114 single = OCSP_basic_add1_status(bs, cid,
1115 V_OCSP_CERTSTATUS_REVOKED,
1116 reason, revtm, thisupd, nextupd);
2234212c 1117 if (invtm != NULL)
0f113f3e
MC
1118 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1119 invtm, 0, 0);
2234212c 1120 else if (inst != NULL)
0f113f3e
MC
1121 OCSP_SINGLERESP_add1_ext_i2d(single,
1122 NID_hold_instruction_code, inst,
1123 0, 0);
1124 ASN1_OBJECT_free(inst);
1125 ASN1_TIME_free(revtm);
1126 ASN1_GENERALIZEDTIME_free(invtm);
1127 }
0770c882 1128 OCSP_CERTID_free(cid_resp_md);
0f113f3e
MC
1129 }
1130
1131 OCSP_copy_nonce(bs, req);
1132
b4dd21a7 1133 mctx = EVP_MD_CTX_new();
1287dabd 1134 if (mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
b4dd21a7
DC
1135 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
1136 goto end;
1137 }
1138 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1139 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
89623f84 1140
b4dd21a7
DC
1141 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1142 BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1143 ERR_print_errors(bio_err);
1144 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1145 NULL);
1146 goto end;
1147 }
1148 }
72b89b8e
DB
1149 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
1150 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
1151 goto end;
1152 }
0f113f3e 1153
6ef869d7 1154 if (badsig) {
a0754084
DSH
1155 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
1156 corrupt_signature(sig);
6ef869d7 1157 }
0f113f3e
MC
1158
1159 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1160
1161 end:
89623f84 1162 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1163 ASN1_TIME_free(thisupd);
1164 ASN1_TIME_free(nextupd);
0f113f3e 1165 OCSP_BASICRESP_free(bs);
0f113f3e 1166}
ee306a13 1167
f85b68cd 1168static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
0f113f3e
MC
1169{
1170 int i;
1171 BIGNUM *bn = NULL;
1172 char *itmp, *row[DB_NUMBER], **rrow;
1173 for (i = 0; i < DB_NUMBER; i++)
1174 row[i] = NULL;
1175 bn = ASN1_INTEGER_to_BN(ser, NULL);
1176 OPENSSL_assert(bn); /* FIXME: should report an error at this
1177 * point and abort */
1178 if (BN_is_zero(bn))
7644a9ae 1179 itmp = OPENSSL_strdup("00");
0f113f3e
MC
1180 else
1181 itmp = BN_bn2hex(bn);
1182 row[DB_serial] = itmp;
1183 BN_free(bn);
1184 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1185 OPENSSL_free(itmp);
1186 return rrow;
1187}
ee306a13 1188
3e3c7c36 1189static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
4599ea9f 1190 int timeout)
0f113f3e 1191{
582311d7 1192#ifndef OPENSSL_NO_SOCK
ef7ae359 1193 return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_REQUEST),
5a2ba207 1194 (ASN1_VALUE **)preq, NULL, pcbio, acbio,
19f97fe6 1195 NULL /* found_keep_alive */,
4599ea9f 1196 prog, 1 /* accept_get */, timeout);
1ae56f2f 1197#else
582311d7
DDO
1198 BIO_printf(bio_err,
1199 "Error getting OCSP request - sockets not supported\n");
3e3c7c36 1200 *preq = NULL;
582311d7 1201 return 0;
1ae56f2f 1202#endif
0f113f3e 1203}
ee306a13 1204
582311d7 1205static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
0f113f3e 1206{
582311d7 1207#ifndef OPENSSL_NO_SOCK
19f97fe6
DDO
1208 return http_server_send_asn1_resp(cbio,
1209 0 /* no keep-alive */,
1210 "application/ocsp-response",
582311d7
DDO
1211 ASN1_ITEM_rptr(OCSP_RESPONSE),
1212 (const ASN1_VALUE *)resp);
1213#else
1214 BIO_printf(bio_err,
1215 "Error sending OCSP response - sockets not supported\n");
1216 return 0;
1217#endif
0f113f3e 1218}
ee306a13 1219
1ae56f2f 1220#ifndef OPENSSL_NO_SOCK
88d96983
DDO
1221OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, const char *host,
1222 const char *port, const char *path,
1223 const char *proxy, const char *no_proxy,
1224 int use_ssl, STACK_OF(CONF_VALUE) *headers,
0f113f3e
MC
1225 int req_timeout)
1226{
0f113f3e
MC
1227 SSL_CTX *ctx = NULL;
1228 OCSP_RESPONSE *resp = NULL;
ff4a9394 1229
0f113f3e 1230 if (use_ssl == 1) {
13c9bb3e 1231 ctx = SSL_CTX_new(TLS_client_method());
0f113f3e 1232 if (ctx == NULL) {
7e1b7485 1233 BIO_printf(bio_err, "Error creating SSL context.\n");
0f113f3e
MC
1234 goto end;
1235 }
0f113f3e 1236 }
ff4a9394 1237
29f178bd 1238 resp = (OCSP_RESPONSE *)
88d96983 1239 app_http_post_asn1(host, port, path, proxy, no_proxy,
29f178bd
DDO
1240 ctx, headers, "application/ocsp-request",
1241 (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
82990287 1242 "application/ocsp-response",
29f178bd
DDO
1243 req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
1244
2234212c 1245 if (resp == NULL)
0f113f3e 1246 BIO_printf(bio_err, "Error querying OCSP responder\n");
29f178bd 1247
0f113f3e 1248 end:
62adbcee 1249 SSL_CTX_free(ctx);
0f113f3e
MC
1250 return resp;
1251}
85d686e7 1252#endif