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