2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
12 #ifdef OPENSSL_SYS_VMS
13 /* So fd_set and friends get properly defined on OpenVMS */
14 # define _XOPEN_SOURCE_EXTENDED
23 /* Needs to be included before the openssl headers */
25 #include "http_server.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>
36 DEFINE_STACK_OF(CONF_VALUE)
39 # if defined(OPENSSL_TANDEM_FLOSS)
40 # include <floss.h(floss_fork)>
44 #if defined(OPENSSL_SYS_VXWORKS)
46 int setpgid(pid_t pid, pid_t pgid)
58 /* Maximum leeway in validity period: default 5 minutes */
59 #define MAX_VALIDITY_PERIOD (5 * 60)
61 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
62 const EVP_MD *cert_id_md, X509 *issuer,
63 STACK_OF(OCSP_CERTID) *ids);
64 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
65 const EVP_MD *cert_id_md, X509 *issuer,
66 STACK_OF(OCSP_CERTID) *ids);
67 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
68 STACK_OF(OPENSSL_STRING) *names,
69 STACK_OF(OCSP_CERTID) *ids, long nsec,
71 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
72 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
73 EVP_PKEY *rkey, const EVP_MD *md,
74 STACK_OF(OPENSSL_STRING) *sigopts,
75 STACK_OF(X509) *rother, unsigned long flags,
76 int nmin, int ndays, int badsig,
77 const EVP_MD *resp_md);
79 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
80 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
82 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp);
86 static int index_changed(CA_DB *);
89 typedef enum OPTION_choice {
90 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
91 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
92 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
93 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
94 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
95 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
96 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
97 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
98 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
99 OPT_NOCAPATH, OPT_NOCASTORE,
100 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
101 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
102 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
103 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
108 OPT_MULTI, OPT_PROV_ENUM
111 const OPTIONS ocsp_options[] = {
112 OPT_SECTION("General"),
113 {"help", OPT_HELP, '-', "Display this summary"},
114 {"ignore_err", OPT_IGNORE_ERR, '-',
115 "Ignore error on OCSP request or response and continue running"},
116 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
117 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
118 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
119 {"no-CAfile", OPT_NOCAFILE, '-',
120 "Do not load the default certificates file"},
121 {"no-CApath", OPT_NOCAPATH, '-',
122 "Do not load certificates from the default certificates directory"},
123 {"no-CAstore", OPT_NOCASTORE, '-',
124 "Do not load certificates from the default certificates store"},
126 OPT_SECTION("Responder"),
127 {"timeout", OPT_TIMEOUT, 'p',
128 "Connection timeout (in seconds) to the OCSP responder"},
129 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
130 "Don't include any certificates in response"},
132 {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
134 {"no_certs", OPT_NO_CERTS, '-',
135 "Don't include any certificates in signed request"},
136 {"badsig", OPT_BADSIG, '-',
137 "Corrupt last byte of loaded OSCP response signature (for test)"},
138 {"CA", OPT_CA, '<', "CA certificate"},
139 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
140 {"nrequest", OPT_REQUEST, 'p',
141 "Number of requests to accept (default unlimited)"},
142 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
143 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
144 {"sign_other", OPT_SIGN_OTHER, '<',
145 "Additional certificates to include in signed request"},
146 {"index", OPT_INDEX, '<', "Certificate status index file"},
147 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
148 {"rsigner", OPT_RSIGNER, '<',
149 "Responder certificate to sign responses with"},
150 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
151 {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
152 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
153 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
154 {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
155 {"header", OPT_HEADER, 's', "key=value header to add"},
156 {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
157 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
159 OPT_SECTION("Client"),
160 {"url", OPT_URL, 's', "Responder URL"},
161 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
162 {"port", OPT_PORT, 'p', "Port to run responder on"},
163 {"out", OPT_OUTFILE, '>', "Output filename"},
164 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
165 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
166 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
167 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
168 "Don't check signature on response"},
169 {"resp_key_id", OPT_RESP_KEY_ID, '-',
170 "Identify response by signing certificate key ID"},
171 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
172 "Don't check signing certificate"},
173 {"text", OPT_TEXT, '-', "Print text form of request and response"},
174 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
175 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
176 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
177 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
178 "Don't do additional checks on signing certificate"},
179 {"no_explicit", OPT_NO_EXPLICIT, '-',
180 "Do not explicitly check the chain, just verify the root"},
181 {"trust_other", OPT_TRUST_OTHER, '-',
182 "Don't verify additional certificates"},
183 {"no_intern", OPT_NO_INTERN, '-',
184 "Don't search certificates contained in response for signer"},
185 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
186 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
187 {"verify_other", OPT_VERIFY_OTHER, '<',
188 "Additional certificates to search for signer"},
189 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
190 {"cert", OPT_CERT, '<', "Certificate to check"},
191 {"serial", OPT_SERIAL, 's', "Serial number to check"},
192 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
193 "Maximum validity discrepancy in seconds"},
194 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
195 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
196 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
197 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
198 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
205 int ocsp_main(int argc, char **argv)
207 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
208 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
209 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
212 EVP_PKEY *key = NULL, *rkey = NULL;
213 OCSP_BASICRESP *bs = NULL;
214 OCSP_REQUEST *req = NULL;
215 OCSP_RESPONSE *resp = NULL;
216 STACK_OF(CONF_VALUE) *headers = NULL;
217 STACK_OF(OCSP_CERTID) *ids = NULL;
218 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
219 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
220 STACK_OF(X509) *issuers = NULL;
221 X509 *issuer = NULL, *cert = NULL;
222 STACK_OF(X509) *rca_cert = NULL;
223 const EVP_MD *resp_certid_md = NULL;
224 X509 *signer = NULL, *rsigner = NULL;
225 X509_STORE *store = NULL;
226 X509_VERIFY_PARAM *vpm = NULL;
227 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
228 char *header, *value;
229 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
230 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
231 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
232 char *rsignfile = NULL, *rkeyfile = NULL;
233 char *passinarg = NULL, *passin = NULL;
234 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
235 char *signfile = NULL, *keyfile = NULL;
236 char *thost = NULL, *tport = NULL, *tpath = NULL;
237 int noCAfile = 0, noCApath = 0, noCAstore = 0;
238 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
239 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
240 int req_text = 0, resp_text = 0, res, ret = 1;
241 int req_timeout = -1;
242 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
243 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
246 reqnames = sk_OPENSSL_STRING_new_null();
247 if (reqnames == NULL)
249 ids = sk_OCSP_CERTID_new_null();
252 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
255 prog = opt_init(argc, argv, ocsp_options);
256 while ((o = opt_next()) != OPT_EOF) {
261 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
265 opt_help(ocsp_options);
271 #ifndef OPENSSL_NO_SOCK
272 req_timeout = atoi(opt_arg());
279 thost = tport = tpath = NULL;
280 if (!OSSL_HTTP_parse_url(opt_arg(),
281 &host, &port, NULL, &path, &use_ssl)) {
282 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
307 case OPT_RESP_NO_CERTS:
308 rflags |= OCSP_NOCERTS;
310 case OPT_RESP_KEY_ID:
311 rflags |= OCSP_RESPID_KEY;
314 sign_flags |= OCSP_NOCERTS;
316 case OPT_NO_SIGNATURE_VERIFY:
317 verify_flags |= OCSP_NOSIGS;
319 case OPT_NO_CERT_VERIFY:
320 verify_flags |= OCSP_NOVERIFY;
323 verify_flags |= OCSP_NOCHAIN;
325 case OPT_NO_CERT_CHECKS:
326 verify_flags |= OCSP_NOCHECKS;
328 case OPT_NO_EXPLICIT:
329 verify_flags |= OCSP_NOEXPLICIT;
331 case OPT_TRUST_OTHER:
332 verify_flags |= OCSP_TRUSTOTHER;
335 verify_flags |= OCSP_NOINTERN;
341 req_text = resp_text = 1;
356 signfile = opt_arg();
359 verify_certfile = opt_arg();
360 verify_flags |= OCSP_TRUSTOTHER;
363 sign_certfile = opt_arg();
365 case OPT_VERIFY_OTHER:
366 verify_certfile = opt_arg();
387 if (!opt_verify(o, vpm))
391 case OPT_VALIDITY_PERIOD:
392 opt_long(opt_arg(), &nsec);
395 opt_long(opt_arg(), &maxage);
410 issuer = load_cert(opt_arg(), FORMAT_UNDEF,
411 "issuer certificate");
414 if (issuers == NULL) {
415 if ((issuers = sk_X509_new_null()) == NULL)
418 if (!sk_X509_push(issuers, issuer))
423 cert = load_cert(opt_arg(), FORMAT_UNDEF, "certificate");
426 if (cert_id_md == NULL)
427 cert_id_md = EVP_sha1();
428 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
430 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
435 if (cert_id_md == NULL)
436 cert_id_md = EVP_sha1();
437 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
439 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
444 ridx_filename = opt_arg();
447 rca_filename = opt_arg();
450 opt_int(opt_arg(), &nmin);
455 opt_int(opt_arg(), &accept_count);
458 ndays = atoi(opt_arg());
461 rsignfile = opt_arg();
464 rkeyfile = opt_arg();
467 passinarg = opt_arg();
470 rcertfile = opt_arg();
472 case OPT_RMD: /* Response MessageDigest */
473 if (!opt_md(opt_arg(), &rsign_md))
477 if (rsign_sigopts == NULL)
478 rsign_sigopts = sk_OPENSSL_STRING_new_null();
479 if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
484 value = strchr(header, '=');
486 BIO_printf(bio_err, "Missing = in header key=value\n");
490 if (!X509V3_add_value(header, value, &headers))
494 resp_certid_md = EVP_get_digestbyname(opt_arg());
495 if (resp_certid_md == NULL)
501 "%s: Digest must be before -cert or -serial\n",
505 if (!opt_md(opt_unknown(), &cert_id_md))
511 multi = atoi(opt_arg());
515 if (!opt_provider(o))
521 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
525 argc = opt_num_rest();
529 /* Have we anything to do? */
530 if (req == NULL && reqin == NULL
531 && respin == NULL && !(port != NULL && ridx_filename != NULL))
534 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
538 if (req == NULL && (add_nonce != 2))
541 if (req == NULL && reqin != NULL) {
542 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
545 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
548 BIO_printf(bio_err, "Error reading OCSP request\n");
553 if (req == NULL && port != NULL) {
554 #ifndef OPENSSL_NO_SOCK
555 acbio = http_server_init_bio(prog, port);
559 BIO_printf(bio_err, "Cannot act as server - sockets not supported\n");
564 if (rsignfile != NULL) {
565 if (rkeyfile == NULL)
566 rkeyfile = rsignfile;
567 rsigner = load_cert(rsignfile, FORMAT_UNDEF,
568 "responder certificate");
569 if (rsigner == NULL) {
570 BIO_printf(bio_err, "Error loading responder certificate\n");
573 if (!load_certs(rca_filename, &rca_cert, NULL, "CA certificates"))
575 if (rcertfile != NULL) {
576 if (!load_certs(rcertfile, &rother, NULL,
577 "responder other certificates"))
580 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
581 BIO_printf(bio_err, "Error getting password\n");
584 rkey = load_key(rkeyfile, FORMAT_PEM, 0, passin, NULL,
585 "responder private key");
590 if (ridx_filename != NULL
591 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
593 "Responder mode requires certificate, key, and CA.\n");
597 if (ridx_filename != NULL) {
598 rdb = load_index(ridx_filename, NULL);
599 if (rdb == NULL || index_index(rdb) <= 0) {
606 if (multi && acbio != NULL)
608 if (acbio != NULL && req_timeout > 0)
609 signal(SIGALRM, socket_timeout);
613 log_message(prog, LOG_INFO, "waiting for OCSP client connections...");
619 if (index_changed(rdb)) {
620 CA_DB *newrdb = load_index(ridx_filename, NULL);
622 if (newrdb != NULL && index_index(newrdb) > 0) {
627 log_message(prog, LOG_ERR, "error reloading updated index: %s",
634 res = do_responder(&req, &cbio, acbio, req_timeout);
641 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
643 send_ocsp_response(cbio, resp);
650 && (signfile != NULL || reqout != NULL
651 || host != NULL || add_nonce || ridx_filename != NULL)) {
652 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
656 if (req != NULL && add_nonce) {
657 if (!OCSP_request_add1_nonce(req, NULL, -1))
661 if (signfile != NULL) {
664 signer = load_cert(signfile, FORMAT_UNDEF, "signer certificate");
665 if (signer == NULL) {
666 BIO_printf(bio_err, "Error loading signer certificate\n");
669 if (sign_certfile != NULL) {
670 if (!load_certs(sign_certfile, &sign_other, NULL,
671 "signer certificates"))
674 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
675 "signer private key");
679 if (!OCSP_request_sign
680 (req, signer, key, NULL, sign_other, sign_flags)) {
681 BIO_printf(bio_err, "Error signing OCSP request\n");
686 if (req_text && req != NULL)
687 OCSP_REQUEST_print(out, req, 0);
689 if (reqout != NULL) {
690 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
693 i2d_OCSP_REQUEST_bio(derbio, req);
698 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
699 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig,
702 send_ocsp_response(cbio, resp);
703 } else if (host != NULL) {
704 #ifndef OPENSSL_NO_SOCK
705 resp = process_responder(req, host, path,
706 port, use_ssl, headers, req_timeout);
711 "Error creating connect BIO - sockets not supported\n");
714 } else if (respin != NULL) {
715 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
718 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
721 BIO_printf(bio_err, "Error reading OCSP response\n");
731 if (respout != NULL) {
732 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
735 i2d_OCSP_RESPONSE_bio(derbio, resp);
739 i = OCSP_response_status(resp);
740 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
741 BIO_printf(out, "Responder Error: %s (%d)\n",
742 OCSP_response_status_str(i), i);
748 OCSP_RESPONSE_print(out, resp, 0);
750 /* If running as responder don't verify our own response */
752 /* If not unlimited, see if we took all we should. */
753 if (accept_count != -1 && --accept_count <= 0) {
759 OCSP_REQUEST_free(req);
761 OCSP_RESPONSE_free(resp);
765 if (ridx_filename != NULL) {
771 store = setup_verify(CAfile, noCAfile, CApath, noCApath,
777 X509_STORE_set1_param(store, vpm);
778 if (verify_certfile != NULL) {
779 if (!load_certs(verify_certfile, &verify_other, NULL,
780 "validator certificates"))
784 bs = OCSP_response_get1_basic(resp);
786 BIO_printf(bio_err, "Error parsing response\n");
793 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
795 BIO_printf(bio_err, "WARNING: no nonce in response\n");
797 BIO_printf(bio_err, "Nonce Verify error\n");
803 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
804 if (i <= 0 && issuers) {
805 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
810 BIO_printf(bio_err, "Response Verify Failure\n");
811 ERR_print_errors(bio_err);
814 BIO_printf(bio_err, "Response verify OK\n");
818 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
821 ERR_print_errors(bio_err);
823 X509_STORE_free(store);
824 X509_VERIFY_PARAM_free(vpm);
825 sk_OPENSSL_STRING_free(rsign_sigopts);
829 sk_X509_pop_free(issuers, X509_free);
831 sk_X509_pop_free(rca_cert, X509_free);
836 OCSP_REQUEST_free(req);
837 OCSP_RESPONSE_free(resp);
838 OCSP_BASICRESP_free(bs);
839 sk_OPENSSL_STRING_free(reqnames);
840 sk_OCSP_CERTID_free(ids);
841 sk_X509_pop_free(sign_other, X509_free);
842 sk_X509_pop_free(verify_other, X509_free);
843 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
853 static int index_changed(CA_DB *rdb)
857 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
858 if (rdb->dbst.st_mtime != sb.st_mtime
859 || rdb->dbst.st_ctime != sb.st_ctime
860 || rdb->dbst.st_ino != sb.st_ino
861 || rdb->dbst.st_dev != sb.st_dev) {
862 syslog(LOG_INFO, "index file changed, reloading");
871 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
872 const EVP_MD *cert_id_md, X509 *issuer,
873 STACK_OF(OCSP_CERTID) *ids)
877 if (issuer == NULL) {
878 BIO_printf(bio_err, "No issuer certificate specified\n");
882 *req = OCSP_REQUEST_new();
885 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
886 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
888 if (!OCSP_request_add0_id(*req, id))
893 BIO_printf(bio_err, "Error Creating OCSP request\n");
897 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
898 const EVP_MD *cert_id_md, X509 *issuer,
899 STACK_OF(OCSP_CERTID) *ids)
902 const X509_NAME *iname;
903 ASN1_BIT_STRING *ikey;
906 if (issuer == NULL) {
907 BIO_printf(bio_err, "No issuer certificate specified\n");
911 *req = OCSP_REQUEST_new();
914 iname = X509_get_subject_name(issuer);
915 ikey = X509_get0_pubkey_bitstr(issuer);
916 sno = s2i_ASN1_INTEGER(NULL, serial);
918 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
921 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
922 ASN1_INTEGER_free(sno);
923 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
925 if (!OCSP_request_add0_id(*req, id))
930 BIO_printf(bio_err, "Error Creating OCSP request\n");
934 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
935 STACK_OF(OPENSSL_STRING) *names,
936 STACK_OF(OCSP_CERTID) *ids, long nsec,
941 int i, status, reason;
942 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
944 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
945 || !sk_OCSP_CERTID_num(ids))
948 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
949 id = sk_OCSP_CERTID_value(ids, i);
950 name = sk_OPENSSL_STRING_value(names, i);
951 BIO_printf(out, "%s: ", name);
953 if (!OCSP_resp_find_status(bs, id, &status, &reason,
954 &rev, &thisupd, &nextupd)) {
955 BIO_puts(out, "ERROR: No Status found.\n");
960 * Check validity: if invalid write to output BIO so we know which
961 * response this refers to.
963 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
964 BIO_puts(out, "WARNING: Status times invalid.\n");
965 ERR_print_errors(out);
967 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
969 BIO_puts(out, "\tThis Update: ");
970 ASN1_GENERALIZEDTIME_print(out, thisupd);
974 BIO_puts(out, "\tNext Update: ");
975 ASN1_GENERALIZEDTIME_print(out, nextupd);
979 if (status != V_OCSP_CERTSTATUS_REVOKED)
983 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
985 BIO_puts(out, "\tRevocation Time: ");
986 ASN1_GENERALIZEDTIME_print(out, rev);
991 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
992 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
993 EVP_PKEY *rkey, const EVP_MD *rmd,
994 STACK_OF(OPENSSL_STRING) *sigopts,
995 STACK_OF(X509) *rother, unsigned long flags,
996 int nmin, int ndays, int badsig,
997 const EVP_MD *resp_md)
999 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
1001 OCSP_BASICRESP *bs = NULL;
1003 EVP_MD_CTX *mctx = NULL;
1004 EVP_PKEY_CTX *pkctx = NULL;
1006 id_count = OCSP_request_onereq_count(req);
1008 if (id_count <= 0) {
1010 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1014 bs = OCSP_BASICRESP_new();
1015 thisupd = X509_gmtime_adj(NULL, 0);
1017 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
1019 /* Examine each certificate id in the request */
1020 for (i = 0; i < id_count; i++) {
1022 ASN1_INTEGER *serial;
1026 ASN1_OBJECT *cert_id_md_oid;
1027 const EVP_MD *cert_id_md;
1028 OCSP_CERTID *cid_resp_md = NULL;
1030 one = OCSP_request_onereq_get0(req, i);
1031 cid = OCSP_onereq_get0_id(one);
1033 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1035 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
1036 if (cert_id_md == NULL) {
1037 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1041 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
1042 X509 *ca_cert = sk_X509_value(ca, jj);
1043 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
1045 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
1047 if (resp_md != NULL)
1048 cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
1050 OCSP_CERTID_free(ca_id);
1052 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1053 inf = lookup_serial(db, serial);
1055 /* at this point, we can have cid be an alias of cid_resp_md */
1056 cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
1059 OCSP_basic_add1_status(bs, cid,
1060 V_OCSP_CERTSTATUS_UNKNOWN,
1061 0, NULL, thisupd, nextupd);
1065 OCSP_basic_add1_status(bs, cid,
1066 V_OCSP_CERTSTATUS_UNKNOWN,
1067 0, NULL, thisupd, nextupd);
1068 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
1069 OCSP_basic_add1_status(bs, cid,
1070 V_OCSP_CERTSTATUS_GOOD,
1071 0, NULL, thisupd, nextupd);
1072 } else if (inf[DB_type][0] == DB_TYPE_REV) {
1073 ASN1_OBJECT *inst = NULL;
1074 ASN1_TIME *revtm = NULL;
1075 ASN1_GENERALIZEDTIME *invtm = NULL;
1076 OCSP_SINGLERESP *single;
1079 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1080 single = OCSP_basic_add1_status(bs, cid,
1081 V_OCSP_CERTSTATUS_REVOKED,
1082 reason, revtm, thisupd, nextupd);
1084 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1086 else if (inst != NULL)
1087 OCSP_SINGLERESP_add1_ext_i2d(single,
1088 NID_hold_instruction_code, inst,
1090 ASN1_OBJECT_free(inst);
1091 ASN1_TIME_free(revtm);
1092 ASN1_GENERALIZEDTIME_free(invtm);
1094 OCSP_CERTID_free(cid_resp_md);
1097 OCSP_copy_nonce(bs, req);
1099 mctx = EVP_MD_CTX_new();
1100 if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
1101 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
1104 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1105 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1107 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1108 BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1109 ERR_print_errors(bio_err);
1110 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1115 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
1116 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
1121 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
1122 corrupt_signature(sig);
1125 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1128 EVP_MD_CTX_free(mctx);
1129 ASN1_TIME_free(thisupd);
1130 ASN1_TIME_free(nextupd);
1131 OCSP_BASICRESP_free(bs);
1134 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
1138 char *itmp, *row[DB_NUMBER], **rrow;
1139 for (i = 0; i < DB_NUMBER; i++)
1141 bn = ASN1_INTEGER_to_BN(ser, NULL);
1142 OPENSSL_assert(bn); /* FIXME: should report an error at this
1143 * point and abort */
1145 itmp = OPENSSL_strdup("00");
1147 itmp = BN_bn2hex(bn);
1148 row[DB_serial] = itmp;
1150 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1155 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1158 #ifndef OPENSSL_NO_SOCK
1159 return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_RESPONSE),
1160 (ASN1_VALUE **)preq, NULL, pcbio, acbio,
1161 prog, 1 /* accept_get */, timeout);
1164 "Error getting OCSP request - sockets not supported\n");
1170 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
1172 #ifndef OPENSSL_NO_SOCK
1173 return http_server_send_asn1_resp(cbio, "application/ocsp-response",
1174 ASN1_ITEM_rptr(OCSP_RESPONSE),
1175 (const ASN1_VALUE *)resp);
1178 "Error sending OCSP response - sockets not supported\n");
1183 #ifndef OPENSSL_NO_SOCK
1184 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1185 const char *host, const char *path,
1186 const char *port, int use_ssl,
1187 STACK_OF(CONF_VALUE) *headers,
1190 SSL_CTX *ctx = NULL;
1191 OCSP_RESPONSE *resp = NULL;
1194 ctx = SSL_CTX_new(TLS_client_method());
1196 BIO_printf(bio_err, "Error creating SSL context.\n");
1199 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1202 resp = (OCSP_RESPONSE *)
1203 app_http_post_asn1(host, port, path, NULL, NULL /* no proxy used */,
1204 ctx, headers, "application/ocsp-request",
1205 (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
1206 req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
1209 BIO_printf(bio_err, "Error querying OCSP responder\n");