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