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