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(OCSP_CERTID)
37 DEFINE_STACK_OF(CONF_VALUE)
39 DEFINE_STACK_OF_STRING()
41 #if defined(OPENSSL_SYS_VXWORKS)
43 int setpgid(pid_t pid, pid_t pgid)
55 /* Maximum leeway in validity period: default 5 minutes */
56 #define MAX_VALIDITY_PERIOD (5 * 60)
58 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
59 const EVP_MD *cert_id_md, X509 *issuer,
60 STACK_OF(OCSP_CERTID) *ids);
61 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
62 const EVP_MD *cert_id_md, X509 *issuer,
63 STACK_OF(OCSP_CERTID) *ids);
64 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
65 STACK_OF(OPENSSL_STRING) *names,
66 STACK_OF(OCSP_CERTID) *ids, long nsec,
68 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
69 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
70 EVP_PKEY *rkey, const EVP_MD *md,
71 STACK_OF(OPENSSL_STRING) *sigopts,
72 STACK_OF(X509) *rother, unsigned long flags,
73 int nmin, int ndays, int badsig,
74 const EVP_MD *resp_md);
76 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
77 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
79 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp);
83 static int index_changed(CA_DB *);
86 typedef enum OPTION_choice {
87 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
88 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
89 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
90 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
91 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
92 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
93 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
94 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
95 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
96 OPT_NOCAPATH, OPT_NOCASTORE,
97 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
98 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
99 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
100 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
105 OPT_MULTI, OPT_PROV_ENUM
108 const OPTIONS ocsp_options[] = {
109 OPT_SECTION("General"),
110 {"help", OPT_HELP, '-', "Display this summary"},
111 {"ignore_err", OPT_IGNORE_ERR, '-',
112 "Ignore error on OCSP request or response and continue running"},
113 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
114 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
115 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
116 {"no-CAfile", OPT_NOCAFILE, '-',
117 "Do not load the default certificates file"},
118 {"no-CApath", OPT_NOCAPATH, '-',
119 "Do not load certificates from the default certificates directory"},
120 {"no-CAstore", OPT_NOCAPATH, '-',
121 "Do not load certificates from the default certificates store"},
123 OPT_SECTION("Responder"),
124 {"timeout", OPT_TIMEOUT, 'p',
125 "Connection timeout (in seconds) to the OCSP responder"},
126 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
127 "Don't include any certificates in response"},
129 {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
131 {"no_certs", OPT_NO_CERTS, '-',
132 "Don't include any certificates in signed request"},
133 {"badsig", OPT_BADSIG, '-',
134 "Corrupt last byte of loaded OSCP response signature (for test)"},
135 {"CA", OPT_CA, '<', "CA certificate"},
136 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
137 {"nrequest", OPT_REQUEST, 'p',
138 "Number of requests to accept (default unlimited)"},
139 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
140 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
141 {"sign_other", OPT_SIGN_OTHER, '<',
142 "Additional certificates to include in signed request"},
143 {"index", OPT_INDEX, '<', "Certificate status index file"},
144 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
145 {"rsigner", OPT_RSIGNER, '<',
146 "Responder certificate to sign responses with"},
147 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
148 {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
149 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
150 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
151 {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
152 {"header", OPT_HEADER, 's', "key=value header to add"},
153 {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
154 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
156 OPT_SECTION("Client"),
157 {"url", OPT_URL, 's', "Responder URL"},
158 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
159 {"port", OPT_PORT, 'p', "Port to run responder on"},
160 {"out", OPT_OUTFILE, '>', "Output filename"},
161 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
162 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
163 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
164 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
165 "Don't check signature on response"},
166 {"resp_key_id", OPT_RESP_KEY_ID, '-',
167 "Identify response by signing certificate key ID"},
168 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
169 "Don't check signing certificate"},
170 {"text", OPT_TEXT, '-', "Print text form of request and response"},
171 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
172 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
173 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
174 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
175 "Don't do additional checks on signing certificate"},
176 {"no_explicit", OPT_NO_EXPLICIT, '-',
177 "Do not explicitly check the chain, just verify the root"},
178 {"trust_other", OPT_TRUST_OTHER, '-',
179 "Don't verify additional certificates"},
180 {"no_intern", OPT_NO_INTERN, '-',
181 "Don't search certificates contained in response for signer"},
182 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
183 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
184 {"verify_other", OPT_VERIFY_OTHER, '<',
185 "Additional certificates to search for signer"},
186 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
187 {"cert", OPT_CERT, '<', "Certificate to check"},
188 {"serial", OPT_SERIAL, 's', "Serial number to check"},
189 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
190 "Maximum validity discrepancy in seconds"},
191 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
192 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
193 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
194 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
195 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
202 int ocsp_main(int argc, char **argv)
204 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
205 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
206 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
209 EVP_PKEY *key = NULL, *rkey = NULL;
210 OCSP_BASICRESP *bs = NULL;
211 OCSP_REQUEST *req = NULL;
212 OCSP_RESPONSE *resp = NULL;
213 STACK_OF(CONF_VALUE) *headers = NULL;
214 STACK_OF(OCSP_CERTID) *ids = NULL;
215 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
216 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
217 STACK_OF(X509) *issuers = NULL;
218 X509 *issuer = NULL, *cert = NULL;
219 STACK_OF(X509) *rca_cert = NULL;
220 const EVP_MD *resp_certid_md = NULL;
221 X509 *signer = NULL, *rsigner = NULL;
222 X509_STORE *store = NULL;
223 X509_VERIFY_PARAM *vpm = NULL;
224 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
225 char *header, *value;
226 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
227 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
228 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
229 char *rsignfile = NULL, *rkeyfile = NULL;
230 char *passinarg = NULL, *passin = NULL;
231 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
232 char *signfile = NULL, *keyfile = NULL;
233 char *thost = NULL, *tport = NULL, *tpath = NULL;
234 int noCAfile = 0, noCApath = 0, noCAstore = 0;
235 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
236 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
237 int req_text = 0, resp_text = 0, ret = 1;
238 int req_timeout = -1;
239 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
240 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
243 reqnames = sk_OPENSSL_STRING_new_null();
244 if (reqnames == NULL)
246 ids = sk_OCSP_CERTID_new_null();
249 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
252 prog = opt_init(argc, argv, ocsp_options);
253 while ((o = opt_next()) != OPT_EOF) {
258 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
262 opt_help(ocsp_options);
268 #ifndef OPENSSL_NO_SOCK
269 req_timeout = atoi(opt_arg());
276 thost = tport = tpath = NULL;
277 if (!OSSL_HTTP_parse_url(opt_arg(),
278 &host, &port, &path, &use_ssl)) {
279 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
304 case OPT_RESP_NO_CERTS:
305 rflags |= OCSP_NOCERTS;
307 case OPT_RESP_KEY_ID:
308 rflags |= OCSP_RESPID_KEY;
311 sign_flags |= OCSP_NOCERTS;
313 case OPT_NO_SIGNATURE_VERIFY:
314 verify_flags |= OCSP_NOSIGS;
316 case OPT_NO_CERT_VERIFY:
317 verify_flags |= OCSP_NOVERIFY;
320 verify_flags |= OCSP_NOCHAIN;
322 case OPT_NO_CERT_CHECKS:
323 verify_flags |= OCSP_NOCHECKS;
325 case OPT_NO_EXPLICIT:
326 verify_flags |= OCSP_NOEXPLICIT;
328 case OPT_TRUST_OTHER:
329 verify_flags |= OCSP_TRUSTOTHER;
332 verify_flags |= OCSP_NOINTERN;
338 req_text = resp_text = 1;
353 signfile = opt_arg();
356 verify_certfile = opt_arg();
357 verify_flags |= OCSP_TRUSTOTHER;
360 sign_certfile = opt_arg();
362 case OPT_VERIFY_OTHER:
363 verify_certfile = opt_arg();
384 if (!opt_verify(o, vpm))
388 case OPT_VALIDITY_PERIOD:
389 opt_long(opt_arg(), &nsec);
392 opt_long(opt_arg(), &maxage);
407 issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate");
410 if (issuers == NULL) {
411 if ((issuers = sk_X509_new_null()) == NULL)
414 if (!sk_X509_push(issuers, issuer))
419 cert = load_cert(opt_arg(), FORMAT_PEM, "certificate");
422 if (cert_id_md == NULL)
423 cert_id_md = EVP_sha1();
424 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
426 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
431 if (cert_id_md == NULL)
432 cert_id_md = EVP_sha1();
433 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
435 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
440 ridx_filename = opt_arg();
443 rca_filename = opt_arg();
446 opt_int(opt_arg(), &nmin);
451 opt_int(opt_arg(), &accept_count);
454 ndays = atoi(opt_arg());
457 rsignfile = opt_arg();
460 rkeyfile = opt_arg();
463 passinarg = opt_arg();
466 rcertfile = opt_arg();
468 case OPT_RMD: /* Response MessageDigest */
469 if (!opt_md(opt_arg(), &rsign_md))
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()))
480 value = strchr(header, '=');
482 BIO_printf(bio_err, "Missing = in header key=value\n");
486 if (!X509V3_add_value(header, value, &headers))
490 resp_certid_md = EVP_get_digestbyname(opt_arg());
491 if (resp_certid_md == NULL)
497 "%s: Digest must be before -cert or -serial\n",
501 if (!opt_md(opt_unknown(), &cert_id_md))
507 multi = atoi(opt_arg());
511 if (!opt_provider(o))
517 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
521 argc = opt_num_rest();
525 /* Have we anything to do? */
526 if (req == NULL && reqin == NULL
527 && respin == NULL && !(port != NULL && ridx_filename != NULL))
530 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
534 if (req == NULL && (add_nonce != 2))
537 if (req == NULL && reqin != NULL) {
538 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
541 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
544 BIO_printf(bio_err, "Error reading OCSP request\n");
549 if (req == NULL && port != NULL) {
550 #ifndef OPENSSL_NO_SOCK
551 acbio = http_server_init_bio(prog, port);
555 BIO_printf(bio_err, "Cannot act as server - sockets not supported\n");
560 if (rsignfile != NULL) {
561 if (rkeyfile == NULL)
562 rkeyfile = rsignfile;
563 rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate");
564 if (rsigner == NULL) {
565 BIO_printf(bio_err, "Error loading responder certificate\n");
568 if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM,
569 NULL, "CA certificate"))
571 if (rcertfile != NULL) {
572 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
573 "responder other certificates"))
576 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
577 BIO_printf(bio_err, "Error getting password\n");
580 rkey = load_key(rkeyfile, FORMAT_PEM, 0, passin, NULL,
581 "responder private key");
586 if (ridx_filename != NULL
587 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
589 "Responder mode requires certificate, key, and CA.\n");
593 if (ridx_filename != NULL) {
594 rdb = load_index(ridx_filename, NULL);
595 if (rdb == NULL || index_index(rdb) <= 0) {
602 if (multi && acbio != NULL)
604 if (acbio != NULL && req_timeout > 0)
605 signal(SIGALRM, socket_timeout);
609 log_message(prog, LOG_INFO, "waiting for OCSP client connections...");
615 if (index_changed(rdb)) {
616 CA_DB *newrdb = load_index(ridx_filename, NULL);
618 if (newrdb != NULL && index_index(newrdb) > 0) {
623 log_message(prog, LOG_ERR, "error reloading updated index: %s",
630 if (!do_responder(&req, &cbio, acbio, req_timeout))
634 resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
636 send_ocsp_response(cbio, resp);
642 && (signfile != NULL || reqout != NULL
643 || host != NULL || add_nonce || ridx_filename != NULL)) {
644 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
648 if (req != NULL && add_nonce) {
649 if (!OCSP_request_add1_nonce(req, NULL, -1))
653 if (signfile != NULL) {
656 signer = load_cert(signfile, FORMAT_PEM, "signer certificate");
657 if (signer == NULL) {
658 BIO_printf(bio_err, "Error loading signer certificate\n");
661 if (sign_certfile != NULL) {
662 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL,
663 "signer certificates"))
666 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
667 "signer private key");
671 if (!OCSP_request_sign
672 (req, signer, key, NULL, sign_other, sign_flags)) {
673 BIO_printf(bio_err, "Error signing OCSP request\n");
678 if (req_text && req != NULL)
679 OCSP_REQUEST_print(out, req, 0);
681 if (reqout != NULL) {
682 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
685 i2d_OCSP_REQUEST_bio(derbio, req);
690 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
691 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig,
694 send_ocsp_response(cbio, resp);
695 } else if (host != NULL) {
696 #ifndef OPENSSL_NO_SOCK
697 resp = process_responder(req, host, path,
698 port, use_ssl, headers, req_timeout);
703 "Error creating connect BIO - sockets not supported\n");
706 } else if (respin != NULL) {
707 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
710 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
713 BIO_printf(bio_err, "Error reading OCSP response\n");
723 if (respout != NULL) {
724 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
727 i2d_OCSP_RESPONSE_bio(derbio, resp);
731 i = OCSP_response_status(resp);
732 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
733 BIO_printf(out, "Responder Error: %s (%d)\n",
734 OCSP_response_status_str(i), i);
740 OCSP_RESPONSE_print(out, resp, 0);
742 /* If running as responder don't verify our own response */
744 /* If not unlimited, see if we took all we should. */
745 if (accept_count != -1 && --accept_count <= 0) {
751 OCSP_REQUEST_free(req);
753 OCSP_RESPONSE_free(resp);
757 if (ridx_filename != NULL) {
763 store = setup_verify(CAfile, noCAfile, CApath, noCApath,
769 X509_STORE_set1_param(store, vpm);
770 if (verify_certfile != NULL) {
771 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL,
772 "validator certificate"))
776 bs = OCSP_response_get1_basic(resp);
778 BIO_printf(bio_err, "Error parsing response\n");
785 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
787 BIO_printf(bio_err, "WARNING: no nonce in response\n");
789 BIO_printf(bio_err, "Nonce Verify error\n");
795 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
796 if (i <= 0 && issuers) {
797 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
802 BIO_printf(bio_err, "Response Verify Failure\n");
803 ERR_print_errors(bio_err);
806 BIO_printf(bio_err, "Response verify OK\n");
810 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
813 ERR_print_errors(bio_err);
815 X509_STORE_free(store);
816 X509_VERIFY_PARAM_free(vpm);
817 sk_OPENSSL_STRING_free(rsign_sigopts);
821 sk_X509_pop_free(issuers, X509_free);
823 sk_X509_pop_free(rca_cert, X509_free);
828 OCSP_REQUEST_free(req);
829 OCSP_RESPONSE_free(resp);
830 OCSP_BASICRESP_free(bs);
831 sk_OPENSSL_STRING_free(reqnames);
832 sk_OCSP_CERTID_free(ids);
833 sk_X509_pop_free(sign_other, X509_free);
834 sk_X509_pop_free(verify_other, X509_free);
835 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
845 static int index_changed(CA_DB *rdb)
849 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
850 if (rdb->dbst.st_mtime != sb.st_mtime
851 || rdb->dbst.st_ctime != sb.st_ctime
852 || rdb->dbst.st_ino != sb.st_ino
853 || rdb->dbst.st_dev != sb.st_dev) {
854 syslog(LOG_INFO, "index file changed, reloading");
863 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
864 const EVP_MD *cert_id_md, X509 *issuer,
865 STACK_OF(OCSP_CERTID) *ids)
869 if (issuer == NULL) {
870 BIO_printf(bio_err, "No issuer certificate specified\n");
874 *req = OCSP_REQUEST_new();
877 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
878 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
880 if (!OCSP_request_add0_id(*req, id))
885 BIO_printf(bio_err, "Error Creating OCSP request\n");
889 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
890 const EVP_MD *cert_id_md, X509 *issuer,
891 STACK_OF(OCSP_CERTID) *ids)
894 const X509_NAME *iname;
895 ASN1_BIT_STRING *ikey;
898 if (issuer == NULL) {
899 BIO_printf(bio_err, "No issuer certificate specified\n");
903 *req = OCSP_REQUEST_new();
906 iname = X509_get_subject_name(issuer);
907 ikey = X509_get0_pubkey_bitstr(issuer);
908 sno = s2i_ASN1_INTEGER(NULL, serial);
910 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
913 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
914 ASN1_INTEGER_free(sno);
915 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
917 if (!OCSP_request_add0_id(*req, id))
922 BIO_printf(bio_err, "Error Creating OCSP request\n");
926 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
927 STACK_OF(OPENSSL_STRING) *names,
928 STACK_OF(OCSP_CERTID) *ids, long nsec,
933 int i, status, reason;
934 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
936 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
937 || !sk_OCSP_CERTID_num(ids))
940 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
941 id = sk_OCSP_CERTID_value(ids, i);
942 name = sk_OPENSSL_STRING_value(names, i);
943 BIO_printf(out, "%s: ", name);
945 if (!OCSP_resp_find_status(bs, id, &status, &reason,
946 &rev, &thisupd, &nextupd)) {
947 BIO_puts(out, "ERROR: No Status found.\n");
952 * Check validity: if invalid write to output BIO so we know which
953 * response this refers to.
955 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
956 BIO_puts(out, "WARNING: Status times invalid.\n");
957 ERR_print_errors(out);
959 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
961 BIO_puts(out, "\tThis Update: ");
962 ASN1_GENERALIZEDTIME_print(out, thisupd);
966 BIO_puts(out, "\tNext Update: ");
967 ASN1_GENERALIZEDTIME_print(out, nextupd);
971 if (status != V_OCSP_CERTSTATUS_REVOKED)
975 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
977 BIO_puts(out, "\tRevocation Time: ");
978 ASN1_GENERALIZEDTIME_print(out, rev);
983 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
984 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
985 EVP_PKEY *rkey, const EVP_MD *rmd,
986 STACK_OF(OPENSSL_STRING) *sigopts,
987 STACK_OF(X509) *rother, unsigned long flags,
988 int nmin, int ndays, int badsig,
989 const EVP_MD *resp_md)
991 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
993 OCSP_BASICRESP *bs = NULL;
995 EVP_MD_CTX *mctx = NULL;
996 EVP_PKEY_CTX *pkctx = NULL;
998 id_count = OCSP_request_onereq_count(req);
1000 if (id_count <= 0) {
1002 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1006 bs = OCSP_BASICRESP_new();
1007 thisupd = X509_gmtime_adj(NULL, 0);
1009 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
1011 /* Examine each certificate id in the request */
1012 for (i = 0; i < id_count; i++) {
1014 ASN1_INTEGER *serial;
1018 ASN1_OBJECT *cert_id_md_oid;
1019 const EVP_MD *cert_id_md;
1020 OCSP_CERTID *cid_resp_md = NULL;
1022 one = OCSP_request_onereq_get0(req, i);
1023 cid = OCSP_onereq_get0_id(one);
1025 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1027 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
1028 if (cert_id_md == NULL) {
1029 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1033 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
1034 X509 *ca_cert = sk_X509_value(ca, jj);
1035 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
1037 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
1039 if (resp_md != NULL)
1040 cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
1042 OCSP_CERTID_free(ca_id);
1044 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1045 inf = lookup_serial(db, serial);
1047 /* at this point, we can have cid be an alias of cid_resp_md */
1048 cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
1051 OCSP_basic_add1_status(bs, cid,
1052 V_OCSP_CERTSTATUS_UNKNOWN,
1053 0, NULL, thisupd, nextupd);
1057 OCSP_basic_add1_status(bs, cid,
1058 V_OCSP_CERTSTATUS_UNKNOWN,
1059 0, NULL, thisupd, nextupd);
1060 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
1061 OCSP_basic_add1_status(bs, cid,
1062 V_OCSP_CERTSTATUS_GOOD,
1063 0, NULL, thisupd, nextupd);
1064 } else if (inf[DB_type][0] == DB_TYPE_REV) {
1065 ASN1_OBJECT *inst = NULL;
1066 ASN1_TIME *revtm = NULL;
1067 ASN1_GENERALIZEDTIME *invtm = NULL;
1068 OCSP_SINGLERESP *single;
1071 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1072 single = OCSP_basic_add1_status(bs, cid,
1073 V_OCSP_CERTSTATUS_REVOKED,
1074 reason, revtm, thisupd, nextupd);
1076 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1078 else if (inst != NULL)
1079 OCSP_SINGLERESP_add1_ext_i2d(single,
1080 NID_hold_instruction_code, inst,
1082 ASN1_OBJECT_free(inst);
1083 ASN1_TIME_free(revtm);
1084 ASN1_GENERALIZEDTIME_free(invtm);
1086 OCSP_CERTID_free(cid_resp_md);
1089 OCSP_copy_nonce(bs, req);
1091 mctx = EVP_MD_CTX_new();
1092 if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
1093 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
1096 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1097 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1099 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1100 BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1101 ERR_print_errors(bio_err);
1102 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1107 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
1108 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
1113 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
1114 corrupt_signature(sig);
1117 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1120 EVP_MD_CTX_free(mctx);
1121 ASN1_TIME_free(thisupd);
1122 ASN1_TIME_free(nextupd);
1123 OCSP_BASICRESP_free(bs);
1126 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
1130 char *itmp, *row[DB_NUMBER], **rrow;
1131 for (i = 0; i < DB_NUMBER; i++)
1133 bn = ASN1_INTEGER_to_BN(ser, NULL);
1134 OPENSSL_assert(bn); /* FIXME: should report an error at this
1135 * point and abort */
1137 itmp = OPENSSL_strdup("00");
1139 itmp = BN_bn2hex(bn);
1140 row[DB_serial] = itmp;
1142 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1147 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1150 #ifndef OPENSSL_NO_SOCK
1151 return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_RESPONSE),
1152 (ASN1_VALUE **)preq, pcbio, acbio,
1153 prog, 1 /* accept_get */, timeout);
1156 "Error getting OCSP request - sockets not supported\n");
1162 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
1164 #ifndef OPENSSL_NO_SOCK
1165 return http_server_send_asn1_resp(cbio, "application/ocsp-response",
1166 ASN1_ITEM_rptr(OCSP_RESPONSE),
1167 (const ASN1_VALUE *)resp);
1170 "Error sending OCSP response - sockets not supported\n");
1175 #ifndef OPENSSL_NO_SOCK
1176 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1177 const char *host, const char *path,
1178 const char *port, int use_ssl,
1179 STACK_OF(CONF_VALUE) *headers,
1182 SSL_CTX *ctx = NULL;
1183 OCSP_RESPONSE *resp = NULL;
1186 ctx = SSL_CTX_new(TLS_client_method());
1188 BIO_printf(bio_err, "Error creating SSL context.\n");
1191 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1194 resp = (OCSP_RESPONSE *)
1195 app_http_post_asn1(host, port, path, NULL, NULL /* no proxy used */,
1196 ctx, headers, "application/ocsp-request",
1197 (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
1198 req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
1201 BIO_printf(bio_err, "Error querying OCSP responder\n");