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