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