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>
37 # if defined(OPENSSL_TANDEM_FLOSS)
38 # include <floss.h(floss_fork)>
42 #if defined(OPENSSL_SYS_VXWORKS)
44 int setpgid(pid_t pid, pid_t pgid)
56 /* Maximum leeway in validity period: default 5 minutes */
57 #define MAX_VALIDITY_PERIOD (5 * 60)
59 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
60 const EVP_MD *cert_id_md, X509 *issuer,
61 STACK_OF(OCSP_CERTID) *ids);
62 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
63 const EVP_MD *cert_id_md, X509 *issuer,
64 STACK_OF(OCSP_CERTID) *ids);
65 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
66 STACK_OF(OPENSSL_STRING) *names,
67 STACK_OF(OCSP_CERTID) *ids, long nsec,
69 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
70 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
71 EVP_PKEY *rkey, const EVP_MD *md,
72 STACK_OF(OPENSSL_STRING) *sigopts,
73 STACK_OF(X509) *rother, unsigned long flags,
74 int nmin, int ndays, int badsig,
75 const EVP_MD *resp_md);
77 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
78 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
80 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp);
84 static int index_changed(CA_DB *);
87 typedef 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,
96 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
97 OPT_NOCAPATH, OPT_NOCASTORE,
98 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
99 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
100 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
101 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
106 OPT_MULTI, OPT_PROV_ENUM
109 const OPTIONS ocsp_options[] = {
110 OPT_SECTION("General"),
111 {"help", OPT_HELP, '-', "Display this summary"},
112 {"ignore_err", OPT_IGNORE_ERR, '-',
113 "Ignore error on OCSP request or response and continue running"},
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"},
121 {"no-CAstore", OPT_NOCASTORE, '-',
122 "Do not load certificates from the default certificates store"},
124 OPT_SECTION("Responder"),
125 {"timeout", OPT_TIMEOUT, 'p',
126 "Connection timeout (in seconds) to the OCSP responder"},
127 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
128 "Don't include any certificates in response"},
130 {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
132 {"no_certs", OPT_NO_CERTS, '-',
133 "Don't include any certificates in signed request"},
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"},
149 {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
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, ... )"},
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"},
165 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
166 "Don't check signature on response"},
167 {"resp_key_id", OPT_RESP_KEY_ID, '-',
168 "Identify response by signing certificate key ID"},
169 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
170 "Don't check signing certificate"},
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"},
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"},
177 {"no_explicit", OPT_NO_EXPLICIT, '-',
178 "Do not explicitly check the chain, just verify the root"},
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"},
183 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
184 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
185 {"verify_other", OPT_VERIFY_OTHER, '<',
186 "Additional certificates to search for signer"},
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"},
190 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
191 "Maximum validity discrepancy in seconds"},
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"},
195 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
196 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
203 int ocsp_main(int argc, char **argv)
205 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
206 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
207 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
210 EVP_PKEY *key = NULL, *rkey = NULL;
211 OCSP_BASICRESP *bs = NULL;
212 OCSP_REQUEST *req = NULL;
213 OCSP_RESPONSE *resp = NULL;
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;
218 STACK_OF(X509) *issuers = NULL;
219 X509 *issuer = NULL, *cert = NULL;
220 STACK_OF(X509) *rca_cert = NULL;
221 const EVP_MD *resp_certid_md = NULL;
222 X509 *signer = NULL, *rsigner = NULL;
223 X509_STORE *store = NULL;
224 X509_VERIFY_PARAM *vpm = NULL;
225 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
226 char *header, *value;
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;
231 char *passinarg = NULL, *passin = NULL;
232 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
233 char *signfile = NULL, *keyfile = NULL;
234 char *thost = NULL, *tport = NULL, *tpath = NULL;
235 int noCAfile = 0, noCApath = 0, noCAstore = 0;
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;
238 int req_text = 0, resp_text = 0, res, ret = 1;
239 int req_timeout = -1;
240 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
241 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
244 reqnames = sk_OPENSSL_STRING_new_null();
245 if (reqnames == NULL)
247 ids = sk_OCSP_CERTID_new_null();
250 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
253 prog = opt_init(argc, argv, ocsp_options);
254 while ((o = opt_next()) != OPT_EOF) {
259 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
263 opt_help(ocsp_options);
269 #ifndef OPENSSL_NO_SOCK
270 req_timeout = atoi(opt_arg());
277 thost = tport = tpath = NULL;
278 if (!OSSL_HTTP_parse_url(opt_arg(),
279 &host, &port, NULL, &path, &use_ssl)) {
280 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
305 case OPT_RESP_NO_CERTS:
306 rflags |= OCSP_NOCERTS;
308 case OPT_RESP_KEY_ID:
309 rflags |= OCSP_RESPID_KEY;
312 sign_flags |= OCSP_NOCERTS;
314 case OPT_NO_SIGNATURE_VERIFY:
315 verify_flags |= OCSP_NOSIGS;
317 case OPT_NO_CERT_VERIFY:
318 verify_flags |= OCSP_NOVERIFY;
321 verify_flags |= OCSP_NOCHAIN;
323 case OPT_NO_CERT_CHECKS:
324 verify_flags |= OCSP_NOCHECKS;
326 case OPT_NO_EXPLICIT:
327 verify_flags |= OCSP_NOEXPLICIT;
329 case OPT_TRUST_OTHER:
330 verify_flags |= OCSP_TRUSTOTHER;
333 verify_flags |= OCSP_NOINTERN;
339 req_text = resp_text = 1;
354 signfile = opt_arg();
357 verify_certfile = opt_arg();
358 verify_flags |= OCSP_TRUSTOTHER;
361 sign_certfile = opt_arg();
363 case OPT_VERIFY_OTHER:
364 verify_certfile = opt_arg();
385 if (!opt_verify(o, vpm))
389 case OPT_VALIDITY_PERIOD:
390 opt_long(opt_arg(), &nsec);
393 opt_long(opt_arg(), &maxage);
408 issuer = load_cert(opt_arg(), FORMAT_UNDEF,
409 "issuer certificate");
412 if (issuers == NULL) {
413 if ((issuers = sk_X509_new_null()) == NULL)
416 if (!sk_X509_push(issuers, issuer))
421 cert = load_cert(opt_arg(), FORMAT_UNDEF, "certificate");
424 if (cert_id_md == NULL)
425 cert_id_md = EVP_sha1();
426 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
428 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
433 if (cert_id_md == NULL)
434 cert_id_md = EVP_sha1();
435 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
437 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
442 ridx_filename = opt_arg();
445 rca_filename = opt_arg();
448 opt_int(opt_arg(), &nmin);
453 opt_int(opt_arg(), &accept_count);
456 ndays = atoi(opt_arg());
459 rsignfile = opt_arg();
462 rkeyfile = opt_arg();
465 passinarg = opt_arg();
468 rcertfile = opt_arg();
470 case OPT_RMD: /* Response MessageDigest */
471 if (!opt_md(opt_arg(), &rsign_md))
475 if (rsign_sigopts == NULL)
476 rsign_sigopts = sk_OPENSSL_STRING_new_null();
477 if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
482 value = strchr(header, '=');
484 BIO_printf(bio_err, "Missing = in header key=value\n");
488 if (!X509V3_add_value(header, value, &headers))
492 resp_certid_md = EVP_get_digestbyname(opt_arg());
493 if (resp_certid_md == NULL)
499 "%s: Digest must be before -cert or -serial\n",
503 if (!opt_md(opt_unknown(), &cert_id_md))
509 multi = atoi(opt_arg());
513 if (!opt_provider(o))
519 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
523 argc = opt_num_rest();
527 /* Have we anything to do? */
528 if (req == NULL && reqin == NULL
529 && respin == NULL && !(port != NULL && ridx_filename != NULL))
532 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
536 if (req == NULL && (add_nonce != 2))
539 if (req == NULL && reqin != NULL) {
540 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
543 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
546 BIO_printf(bio_err, "Error reading OCSP request\n");
551 if (req == NULL && port != NULL) {
552 #ifndef OPENSSL_NO_SOCK
553 acbio = http_server_init_bio(prog, port);
557 BIO_printf(bio_err, "Cannot act as server - sockets not supported\n");
562 if (rsignfile != NULL) {
563 if (rkeyfile == NULL)
564 rkeyfile = rsignfile;
565 rsigner = load_cert(rsignfile, FORMAT_UNDEF,
566 "responder certificate");
567 if (rsigner == NULL) {
568 BIO_printf(bio_err, "Error loading responder certificate\n");
571 if (!load_certs(rca_filename, &rca_cert, NULL, "CA certificates"))
573 if (rcertfile != NULL) {
574 if (!load_certs(rcertfile, &rother, NULL,
575 "responder other certificates"))
578 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
579 BIO_printf(bio_err, "Error getting password\n");
582 rkey = load_key(rkeyfile, FORMAT_PEM, 0, passin, NULL,
583 "responder private key");
588 if (ridx_filename != NULL
589 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
591 "Responder mode requires certificate, key, and CA.\n");
595 if (ridx_filename != NULL) {
596 rdb = load_index(ridx_filename, NULL);
597 if (rdb == NULL || index_index(rdb) <= 0) {
604 if (multi && acbio != NULL)
606 if (acbio != NULL && req_timeout > 0)
607 signal(SIGALRM, socket_timeout);
611 log_message(prog, LOG_INFO, "waiting for OCSP client connections...");
617 if (index_changed(rdb)) {
618 CA_DB *newrdb = load_index(ridx_filename, NULL);
620 if (newrdb != NULL && index_index(newrdb) > 0) {
625 log_message(prog, LOG_ERR, "error reloading updated index: %s",
632 res = do_responder(&req, &cbio, acbio, req_timeout);
639 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
641 send_ocsp_response(cbio, resp);
648 && (signfile != NULL || reqout != NULL
649 || host != NULL || add_nonce || ridx_filename != NULL)) {
650 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
654 if (req != NULL && add_nonce) {
655 if (!OCSP_request_add1_nonce(req, NULL, -1))
659 if (signfile != NULL) {
662 signer = load_cert(signfile, FORMAT_UNDEF, "signer certificate");
663 if (signer == NULL) {
664 BIO_printf(bio_err, "Error loading signer certificate\n");
667 if (sign_certfile != NULL) {
668 if (!load_certs(sign_certfile, &sign_other, NULL,
669 "signer certificates"))
672 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
673 "signer private key");
677 if (!OCSP_request_sign
678 (req, signer, key, NULL, sign_other, sign_flags)) {
679 BIO_printf(bio_err, "Error signing OCSP request\n");
684 if (req_text && req != NULL)
685 OCSP_REQUEST_print(out, req, 0);
687 if (reqout != NULL) {
688 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
691 i2d_OCSP_REQUEST_bio(derbio, req);
696 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
697 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig,
700 send_ocsp_response(cbio, resp);
701 } else if (host != NULL) {
702 #ifndef OPENSSL_NO_SOCK
703 resp = process_responder(req, host, path,
704 port, use_ssl, headers, req_timeout);
709 "Error creating connect BIO - sockets not supported\n");
712 } else if (respin != NULL) {
713 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
716 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
719 BIO_printf(bio_err, "Error reading OCSP response\n");
729 if (respout != NULL) {
730 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
733 i2d_OCSP_RESPONSE_bio(derbio, resp);
737 i = OCSP_response_status(resp);
738 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
739 BIO_printf(out, "Responder Error: %s (%d)\n",
740 OCSP_response_status_str(i), i);
746 OCSP_RESPONSE_print(out, resp, 0);
748 /* If running as responder don't verify our own response */
750 /* If not unlimited, see if we took all we should. */
751 if (accept_count != -1 && --accept_count <= 0) {
757 OCSP_REQUEST_free(req);
759 OCSP_RESPONSE_free(resp);
763 if (ridx_filename != NULL) {
769 store = setup_verify(CAfile, noCAfile, CApath, noCApath,
775 X509_STORE_set1_param(store, vpm);
776 if (verify_certfile != NULL) {
777 if (!load_certs(verify_certfile, &verify_other, NULL,
778 "validator certificates"))
782 bs = OCSP_response_get1_basic(resp);
784 BIO_printf(bio_err, "Error parsing response\n");
791 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
793 BIO_printf(bio_err, "WARNING: no nonce in response\n");
795 BIO_printf(bio_err, "Nonce Verify error\n");
801 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
802 if (i <= 0 && issuers) {
803 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
808 BIO_printf(bio_err, "Response Verify Failure\n");
809 ERR_print_errors(bio_err);
812 BIO_printf(bio_err, "Response verify OK\n");
816 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
819 ERR_print_errors(bio_err);
821 X509_STORE_free(store);
822 X509_VERIFY_PARAM_free(vpm);
823 sk_OPENSSL_STRING_free(rsign_sigopts);
827 sk_X509_pop_free(issuers, X509_free);
829 sk_X509_pop_free(rca_cert, X509_free);
834 OCSP_REQUEST_free(req);
835 OCSP_RESPONSE_free(resp);
836 OCSP_BASICRESP_free(bs);
837 sk_OPENSSL_STRING_free(reqnames);
838 sk_OCSP_CERTID_free(ids);
839 sk_X509_pop_free(sign_other, X509_free);
840 sk_X509_pop_free(verify_other, X509_free);
841 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
851 static int index_changed(CA_DB *rdb)
855 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
856 if (rdb->dbst.st_mtime != sb.st_mtime
857 || rdb->dbst.st_ctime != sb.st_ctime
858 || rdb->dbst.st_ino != sb.st_ino
859 || rdb->dbst.st_dev != sb.st_dev) {
860 syslog(LOG_INFO, "index file changed, reloading");
869 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
870 const EVP_MD *cert_id_md, X509 *issuer,
871 STACK_OF(OCSP_CERTID) *ids)
875 if (issuer == NULL) {
876 BIO_printf(bio_err, "No issuer certificate specified\n");
880 *req = OCSP_REQUEST_new();
883 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
884 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
886 if (!OCSP_request_add0_id(*req, id))
891 BIO_printf(bio_err, "Error Creating OCSP request\n");
895 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
896 const EVP_MD *cert_id_md, X509 *issuer,
897 STACK_OF(OCSP_CERTID) *ids)
900 const X509_NAME *iname;
901 ASN1_BIT_STRING *ikey;
904 if (issuer == NULL) {
905 BIO_printf(bio_err, "No issuer certificate specified\n");
909 *req = OCSP_REQUEST_new();
912 iname = X509_get_subject_name(issuer);
913 ikey = X509_get0_pubkey_bitstr(issuer);
914 sno = s2i_ASN1_INTEGER(NULL, serial);
916 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
919 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
920 ASN1_INTEGER_free(sno);
921 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
923 if (!OCSP_request_add0_id(*req, id))
928 BIO_printf(bio_err, "Error Creating OCSP request\n");
932 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
933 STACK_OF(OPENSSL_STRING) *names,
934 STACK_OF(OCSP_CERTID) *ids, long nsec,
939 int i, status, reason;
940 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
942 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
943 || !sk_OCSP_CERTID_num(ids))
946 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
947 id = sk_OCSP_CERTID_value(ids, i);
948 name = sk_OPENSSL_STRING_value(names, i);
949 BIO_printf(out, "%s: ", name);
951 if (!OCSP_resp_find_status(bs, id, &status, &reason,
952 &rev, &thisupd, &nextupd)) {
953 BIO_puts(out, "ERROR: No Status found.\n");
958 * Check validity: if invalid write to output BIO so we know which
959 * response this refers to.
961 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
962 BIO_puts(out, "WARNING: Status times invalid.\n");
963 ERR_print_errors(out);
965 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
967 BIO_puts(out, "\tThis Update: ");
968 ASN1_GENERALIZEDTIME_print(out, thisupd);
972 BIO_puts(out, "\tNext Update: ");
973 ASN1_GENERALIZEDTIME_print(out, nextupd);
977 if (status != V_OCSP_CERTSTATUS_REVOKED)
981 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
983 BIO_puts(out, "\tRevocation Time: ");
984 ASN1_GENERALIZEDTIME_print(out, rev);
989 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
990 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
991 EVP_PKEY *rkey, const EVP_MD *rmd,
992 STACK_OF(OPENSSL_STRING) *sigopts,
993 STACK_OF(X509) *rother, unsigned long flags,
994 int nmin, int ndays, int badsig,
995 const EVP_MD *resp_md)
997 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
999 OCSP_BASICRESP *bs = NULL;
1001 EVP_MD_CTX *mctx = NULL;
1002 EVP_PKEY_CTX *pkctx = NULL;
1004 id_count = OCSP_request_onereq_count(req);
1006 if (id_count <= 0) {
1008 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1012 bs = OCSP_BASICRESP_new();
1013 thisupd = X509_gmtime_adj(NULL, 0);
1015 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
1017 /* Examine each certificate id in the request */
1018 for (i = 0; i < id_count; i++) {
1020 ASN1_INTEGER *serial;
1024 ASN1_OBJECT *cert_id_md_oid;
1025 const EVP_MD *cert_id_md;
1026 OCSP_CERTID *cid_resp_md = NULL;
1028 one = OCSP_request_onereq_get0(req, i);
1029 cid = OCSP_onereq_get0_id(one);
1031 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1033 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
1034 if (cert_id_md == NULL) {
1035 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1039 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
1040 X509 *ca_cert = sk_X509_value(ca, jj);
1041 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
1043 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
1045 if (resp_md != NULL)
1046 cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
1048 OCSP_CERTID_free(ca_id);
1050 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1051 inf = lookup_serial(db, serial);
1053 /* at this point, we can have cid be an alias of cid_resp_md */
1054 cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
1057 OCSP_basic_add1_status(bs, cid,
1058 V_OCSP_CERTSTATUS_UNKNOWN,
1059 0, NULL, thisupd, nextupd);
1063 OCSP_basic_add1_status(bs, cid,
1064 V_OCSP_CERTSTATUS_UNKNOWN,
1065 0, NULL, thisupd, nextupd);
1066 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
1067 OCSP_basic_add1_status(bs, cid,
1068 V_OCSP_CERTSTATUS_GOOD,
1069 0, NULL, thisupd, nextupd);
1070 } else if (inf[DB_type][0] == DB_TYPE_REV) {
1071 ASN1_OBJECT *inst = NULL;
1072 ASN1_TIME *revtm = NULL;
1073 ASN1_GENERALIZEDTIME *invtm = NULL;
1074 OCSP_SINGLERESP *single;
1077 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1078 single = OCSP_basic_add1_status(bs, cid,
1079 V_OCSP_CERTSTATUS_REVOKED,
1080 reason, revtm, thisupd, nextupd);
1082 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1084 else if (inst != NULL)
1085 OCSP_SINGLERESP_add1_ext_i2d(single,
1086 NID_hold_instruction_code, inst,
1088 ASN1_OBJECT_free(inst);
1089 ASN1_TIME_free(revtm);
1090 ASN1_GENERALIZEDTIME_free(invtm);
1092 OCSP_CERTID_free(cid_resp_md);
1095 OCSP_copy_nonce(bs, req);
1097 mctx = EVP_MD_CTX_new();
1098 if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
1099 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
1102 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1103 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1105 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1106 BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1107 ERR_print_errors(bio_err);
1108 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1113 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
1114 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
1119 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
1120 corrupt_signature(sig);
1123 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1126 EVP_MD_CTX_free(mctx);
1127 ASN1_TIME_free(thisupd);
1128 ASN1_TIME_free(nextupd);
1129 OCSP_BASICRESP_free(bs);
1132 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
1136 char *itmp, *row[DB_NUMBER], **rrow;
1137 for (i = 0; i < DB_NUMBER; i++)
1139 bn = ASN1_INTEGER_to_BN(ser, NULL);
1140 OPENSSL_assert(bn); /* FIXME: should report an error at this
1141 * point and abort */
1143 itmp = OPENSSL_strdup("00");
1145 itmp = BN_bn2hex(bn);
1146 row[DB_serial] = itmp;
1148 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1153 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1156 #ifndef OPENSSL_NO_SOCK
1157 return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_RESPONSE),
1158 (ASN1_VALUE **)preq, NULL, pcbio, acbio,
1159 prog, 1 /* accept_get */, timeout);
1162 "Error getting OCSP request - sockets not supported\n");
1168 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
1170 #ifndef OPENSSL_NO_SOCK
1171 return http_server_send_asn1_resp(cbio, "application/ocsp-response",
1172 ASN1_ITEM_rptr(OCSP_RESPONSE),
1173 (const ASN1_VALUE *)resp);
1176 "Error sending OCSP response - sockets not supported\n");
1181 #ifndef OPENSSL_NO_SOCK
1182 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1183 const char *host, const char *path,
1184 const char *port, int use_ssl,
1185 STACK_OF(CONF_VALUE) *headers,
1188 SSL_CTX *ctx = NULL;
1189 OCSP_RESPONSE *resp = NULL;
1192 ctx = SSL_CTX_new(TLS_client_method());
1194 BIO_printf(bio_err, "Error creating SSL context.\n");
1197 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1200 resp = (OCSP_RESPONSE *)
1201 app_http_post_asn1(host, port, path, NULL, NULL /* no proxy used */,
1202 ctx, headers, "application/ocsp-request",
1203 (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
1204 req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
1207 BIO_printf(bio_err, "Error querying OCSP responder\n");