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()
42 # if defined(OPENSSL_TANDEM_FLOSS)
43 # include <floss.h(floss_fork)>
47 #if defined(OPENSSL_SYS_VXWORKS)
49 int setpgid(pid_t pid, pid_t pgid)
61 /* Maximum leeway in validity period: default 5 minutes */
62 #define MAX_VALIDITY_PERIOD (5 * 60)
64 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
65 const EVP_MD *cert_id_md, X509 *issuer,
66 STACK_OF(OCSP_CERTID) *ids);
67 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
68 const EVP_MD *cert_id_md, X509 *issuer,
69 STACK_OF(OCSP_CERTID) *ids);
70 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
71 STACK_OF(OPENSSL_STRING) *names,
72 STACK_OF(OCSP_CERTID) *ids, long nsec,
74 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
75 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
76 EVP_PKEY *rkey, const EVP_MD *md,
77 STACK_OF(OPENSSL_STRING) *sigopts,
78 STACK_OF(X509) *rother, unsigned long flags,
79 int nmin, int ndays, int badsig,
80 const EVP_MD *resp_md);
82 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
83 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
85 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp);
89 static int index_changed(CA_DB *);
92 typedef enum OPTION_choice {
93 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
94 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
95 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
96 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
97 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
98 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
99 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
100 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
101 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
102 OPT_NOCAPATH, OPT_NOCASTORE,
103 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
104 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
105 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
106 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
111 OPT_MULTI, OPT_PROV_ENUM
114 const OPTIONS ocsp_options[] = {
115 OPT_SECTION("General"),
116 {"help", OPT_HELP, '-', "Display this summary"},
117 {"ignore_err", OPT_IGNORE_ERR, '-',
118 "Ignore error on OCSP request or response and continue running"},
119 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
120 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
121 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
122 {"no-CAfile", OPT_NOCAFILE, '-',
123 "Do not load the default certificates file"},
124 {"no-CApath", OPT_NOCAPATH, '-',
125 "Do not load certificates from the default certificates directory"},
126 {"no-CAstore", OPT_NOCASTORE, '-',
127 "Do not load certificates from the default certificates store"},
129 OPT_SECTION("Responder"),
130 {"timeout", OPT_TIMEOUT, 'p',
131 "Connection timeout (in seconds) to the OCSP responder"},
132 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
133 "Don't include any certificates in response"},
135 {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
137 {"no_certs", OPT_NO_CERTS, '-',
138 "Don't include any certificates in signed request"},
139 {"badsig", OPT_BADSIG, '-',
140 "Corrupt last byte of loaded OSCP response signature (for test)"},
141 {"CA", OPT_CA, '<', "CA certificate"},
142 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
143 {"nrequest", OPT_REQUEST, 'p',
144 "Number of requests to accept (default unlimited)"},
145 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
146 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
147 {"sign_other", OPT_SIGN_OTHER, '<',
148 "Additional certificates to include in signed request"},
149 {"index", OPT_INDEX, '<', "Certificate status index file"},
150 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
151 {"rsigner", OPT_RSIGNER, '<',
152 "Responder certificate to sign responses with"},
153 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
154 {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
155 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
156 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
157 {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
158 {"header", OPT_HEADER, 's', "key=value header to add"},
159 {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
160 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
162 OPT_SECTION("Client"),
163 {"url", OPT_URL, 's', "Responder URL"},
164 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
165 {"port", OPT_PORT, 'p', "Port to run responder on"},
166 {"out", OPT_OUTFILE, '>', "Output filename"},
167 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
168 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
169 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
170 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
171 "Don't check signature on response"},
172 {"resp_key_id", OPT_RESP_KEY_ID, '-',
173 "Identify response by signing certificate key ID"},
174 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
175 "Don't check signing certificate"},
176 {"text", OPT_TEXT, '-', "Print text form of request and response"},
177 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
178 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
179 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
180 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
181 "Don't do additional checks on signing certificate"},
182 {"no_explicit", OPT_NO_EXPLICIT, '-',
183 "Do not explicitly check the chain, just verify the root"},
184 {"trust_other", OPT_TRUST_OTHER, '-',
185 "Don't verify additional certificates"},
186 {"no_intern", OPT_NO_INTERN, '-',
187 "Don't search certificates contained in response for signer"},
188 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
189 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
190 {"verify_other", OPT_VERIFY_OTHER, '<',
191 "Additional certificates to search for signer"},
192 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
193 {"cert", OPT_CERT, '<', "Certificate to check"},
194 {"serial", OPT_SERIAL, 's', "Serial number to check"},
195 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
196 "Maximum validity discrepancy in seconds"},
197 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
198 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
199 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
200 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
201 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
208 int ocsp_main(int argc, char **argv)
210 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
211 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
212 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
215 EVP_PKEY *key = NULL, *rkey = NULL;
216 OCSP_BASICRESP *bs = NULL;
217 OCSP_REQUEST *req = NULL;
218 OCSP_RESPONSE *resp = NULL;
219 STACK_OF(CONF_VALUE) *headers = NULL;
220 STACK_OF(OCSP_CERTID) *ids = NULL;
221 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
222 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
223 STACK_OF(X509) *issuers = NULL;
224 X509 *issuer = NULL, *cert = NULL;
225 STACK_OF(X509) *rca_cert = NULL;
226 const EVP_MD *resp_certid_md = NULL;
227 X509 *signer = NULL, *rsigner = NULL;
228 X509_STORE *store = NULL;
229 X509_VERIFY_PARAM *vpm = NULL;
230 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
231 char *header, *value;
232 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
233 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
234 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
235 char *rsignfile = NULL, *rkeyfile = NULL;
236 char *passinarg = NULL, *passin = NULL;
237 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
238 char *signfile = NULL, *keyfile = NULL;
239 char *thost = NULL, *tport = NULL, *tpath = NULL;
240 int noCAfile = 0, noCApath = 0, noCAstore = 0;
241 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
242 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
243 int req_text = 0, resp_text = 0, res, ret = 1;
244 int req_timeout = -1;
245 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
246 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
249 reqnames = sk_OPENSSL_STRING_new_null();
250 if (reqnames == NULL)
252 ids = sk_OCSP_CERTID_new_null();
255 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
258 prog = opt_init(argc, argv, ocsp_options);
259 while ((o = opt_next()) != OPT_EOF) {
264 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
268 opt_help(ocsp_options);
274 #ifndef OPENSSL_NO_SOCK
275 req_timeout = atoi(opt_arg());
282 thost = tport = tpath = NULL;
283 if (!OSSL_HTTP_parse_url(opt_arg(),
284 &host, &port, NULL, &path, &use_ssl)) {
285 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
310 case OPT_RESP_NO_CERTS:
311 rflags |= OCSP_NOCERTS;
313 case OPT_RESP_KEY_ID:
314 rflags |= OCSP_RESPID_KEY;
317 sign_flags |= OCSP_NOCERTS;
319 case OPT_NO_SIGNATURE_VERIFY:
320 verify_flags |= OCSP_NOSIGS;
322 case OPT_NO_CERT_VERIFY:
323 verify_flags |= OCSP_NOVERIFY;
326 verify_flags |= OCSP_NOCHAIN;
328 case OPT_NO_CERT_CHECKS:
329 verify_flags |= OCSP_NOCHECKS;
331 case OPT_NO_EXPLICIT:
332 verify_flags |= OCSP_NOEXPLICIT;
334 case OPT_TRUST_OTHER:
335 verify_flags |= OCSP_TRUSTOTHER;
338 verify_flags |= OCSP_NOINTERN;
344 req_text = resp_text = 1;
359 signfile = opt_arg();
362 verify_certfile = opt_arg();
363 verify_flags |= OCSP_TRUSTOTHER;
366 sign_certfile = opt_arg();
368 case OPT_VERIFY_OTHER:
369 verify_certfile = opt_arg();
390 if (!opt_verify(o, vpm))
394 case OPT_VALIDITY_PERIOD:
395 opt_long(opt_arg(), &nsec);
398 opt_long(opt_arg(), &maxage);
413 issuer = load_cert(opt_arg(), FORMAT_UNDEF,
414 "issuer certificate");
417 if (issuers == NULL) {
418 if ((issuers = sk_X509_new_null()) == NULL)
421 if (!sk_X509_push(issuers, issuer))
426 cert = load_cert(opt_arg(), FORMAT_UNDEF, "certificate");
429 if (cert_id_md == NULL)
430 cert_id_md = EVP_sha1();
431 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
433 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
438 if (cert_id_md == NULL)
439 cert_id_md = EVP_sha1();
440 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
442 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
447 ridx_filename = opt_arg();
450 rca_filename = opt_arg();
453 opt_int(opt_arg(), &nmin);
458 opt_int(opt_arg(), &accept_count);
461 ndays = atoi(opt_arg());
464 rsignfile = opt_arg();
467 rkeyfile = opt_arg();
470 passinarg = opt_arg();
473 rcertfile = opt_arg();
475 case OPT_RMD: /* Response MessageDigest */
476 if (!opt_md(opt_arg(), &rsign_md))
480 if (rsign_sigopts == NULL)
481 rsign_sigopts = sk_OPENSSL_STRING_new_null();
482 if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
487 value = strchr(header, '=');
489 BIO_printf(bio_err, "Missing = in header key=value\n");
493 if (!X509V3_add_value(header, value, &headers))
497 resp_certid_md = EVP_get_digestbyname(opt_arg());
498 if (resp_certid_md == NULL)
504 "%s: Digest must be before -cert or -serial\n",
508 if (!opt_md(opt_unknown(), &cert_id_md))
514 multi = atoi(opt_arg());
518 if (!opt_provider(o))
524 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
528 argc = opt_num_rest();
532 /* Have we anything to do? */
533 if (req == NULL && reqin == NULL
534 && respin == NULL && !(port != NULL && ridx_filename != NULL))
537 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
541 if (req == NULL && (add_nonce != 2))
544 if (req == NULL && reqin != NULL) {
545 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
548 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
551 BIO_printf(bio_err, "Error reading OCSP request\n");
556 if (req == NULL && port != NULL) {
557 #ifndef OPENSSL_NO_SOCK
558 acbio = http_server_init_bio(prog, port);
562 BIO_printf(bio_err, "Cannot act as server - sockets not supported\n");
567 if (rsignfile != NULL) {
568 if (rkeyfile == NULL)
569 rkeyfile = rsignfile;
570 rsigner = load_cert(rsignfile, FORMAT_UNDEF,
571 "responder certificate");
572 if (rsigner == NULL) {
573 BIO_printf(bio_err, "Error loading responder certificate\n");
576 if (!load_certs(rca_filename, &rca_cert, NULL, "CA certificates"))
578 if (rcertfile != NULL) {
579 if (!load_certs(rcertfile, &rother, NULL,
580 "responder other certificates"))
583 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
584 BIO_printf(bio_err, "Error getting password\n");
587 rkey = load_key(rkeyfile, FORMAT_PEM, 0, passin, NULL,
588 "responder private key");
593 if (ridx_filename != NULL
594 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
596 "Responder mode requires certificate, key, and CA.\n");
600 if (ridx_filename != NULL) {
601 rdb = load_index(ridx_filename, NULL);
602 if (rdb == NULL || index_index(rdb) <= 0) {
609 if (multi && acbio != NULL)
611 if (acbio != NULL && req_timeout > 0)
612 signal(SIGALRM, socket_timeout);
616 log_message(prog, LOG_INFO, "waiting for OCSP client connections...");
622 if (index_changed(rdb)) {
623 CA_DB *newrdb = load_index(ridx_filename, NULL);
625 if (newrdb != NULL && index_index(newrdb) > 0) {
630 log_message(prog, LOG_ERR, "error reloading updated index: %s",
637 res = do_responder(&req, &cbio, acbio, req_timeout);
644 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
646 send_ocsp_response(cbio, resp);
653 && (signfile != NULL || reqout != NULL
654 || host != NULL || add_nonce || ridx_filename != NULL)) {
655 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
659 if (req != NULL && add_nonce) {
660 if (!OCSP_request_add1_nonce(req, NULL, -1))
664 if (signfile != NULL) {
667 signer = load_cert(signfile, FORMAT_UNDEF, "signer certificate");
668 if (signer == NULL) {
669 BIO_printf(bio_err, "Error loading signer certificate\n");
672 if (sign_certfile != NULL) {
673 if (!load_certs(sign_certfile, &sign_other, NULL,
674 "signer certificates"))
677 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
678 "signer private key");
682 if (!OCSP_request_sign
683 (req, signer, key, NULL, sign_other, sign_flags)) {
684 BIO_printf(bio_err, "Error signing OCSP request\n");
689 if (req_text && req != NULL)
690 OCSP_REQUEST_print(out, req, 0);
692 if (reqout != NULL) {
693 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
696 i2d_OCSP_REQUEST_bio(derbio, req);
701 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
702 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig,
705 send_ocsp_response(cbio, resp);
706 } else if (host != NULL) {
707 #ifndef OPENSSL_NO_SOCK
708 resp = process_responder(req, host, path,
709 port, use_ssl, headers, req_timeout);
714 "Error creating connect BIO - sockets not supported\n");
717 } else if (respin != NULL) {
718 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
721 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
724 BIO_printf(bio_err, "Error reading OCSP response\n");
734 if (respout != NULL) {
735 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
738 i2d_OCSP_RESPONSE_bio(derbio, resp);
742 i = OCSP_response_status(resp);
743 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
744 BIO_printf(out, "Responder Error: %s (%d)\n",
745 OCSP_response_status_str(i), i);
751 OCSP_RESPONSE_print(out, resp, 0);
753 /* If running as responder don't verify our own response */
755 /* If not unlimited, see if we took all we should. */
756 if (accept_count != -1 && --accept_count <= 0) {
762 OCSP_REQUEST_free(req);
764 OCSP_RESPONSE_free(resp);
768 if (ridx_filename != NULL) {
774 store = setup_verify(CAfile, noCAfile, CApath, noCApath,
780 X509_STORE_set1_param(store, vpm);
781 if (verify_certfile != NULL) {
782 if (!load_certs(verify_certfile, &verify_other, NULL,
783 "validator certificates"))
787 bs = OCSP_response_get1_basic(resp);
789 BIO_printf(bio_err, "Error parsing response\n");
796 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
798 BIO_printf(bio_err, "WARNING: no nonce in response\n");
800 BIO_printf(bio_err, "Nonce Verify error\n");
806 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
807 if (i <= 0 && issuers) {
808 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
813 BIO_printf(bio_err, "Response Verify Failure\n");
814 ERR_print_errors(bio_err);
817 BIO_printf(bio_err, "Response verify OK\n");
821 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
824 ERR_print_errors(bio_err);
826 X509_STORE_free(store);
827 X509_VERIFY_PARAM_free(vpm);
828 sk_OPENSSL_STRING_free(rsign_sigopts);
832 sk_X509_pop_free(issuers, X509_free);
834 sk_X509_pop_free(rca_cert, X509_free);
839 OCSP_REQUEST_free(req);
840 OCSP_RESPONSE_free(resp);
841 OCSP_BASICRESP_free(bs);
842 sk_OPENSSL_STRING_free(reqnames);
843 sk_OCSP_CERTID_free(ids);
844 sk_X509_pop_free(sign_other, X509_free);
845 sk_X509_pop_free(verify_other, X509_free);
846 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
856 static int index_changed(CA_DB *rdb)
860 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
861 if (rdb->dbst.st_mtime != sb.st_mtime
862 || rdb->dbst.st_ctime != sb.st_ctime
863 || rdb->dbst.st_ino != sb.st_ino
864 || rdb->dbst.st_dev != sb.st_dev) {
865 syslog(LOG_INFO, "index file changed, reloading");
874 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
875 const EVP_MD *cert_id_md, X509 *issuer,
876 STACK_OF(OCSP_CERTID) *ids)
880 if (issuer == NULL) {
881 BIO_printf(bio_err, "No issuer certificate specified\n");
885 *req = OCSP_REQUEST_new();
888 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
889 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
891 if (!OCSP_request_add0_id(*req, id))
896 BIO_printf(bio_err, "Error Creating OCSP request\n");
900 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
901 const EVP_MD *cert_id_md, X509 *issuer,
902 STACK_OF(OCSP_CERTID) *ids)
905 const X509_NAME *iname;
906 ASN1_BIT_STRING *ikey;
909 if (issuer == NULL) {
910 BIO_printf(bio_err, "No issuer certificate specified\n");
914 *req = OCSP_REQUEST_new();
917 iname = X509_get_subject_name(issuer);
918 ikey = X509_get0_pubkey_bitstr(issuer);
919 sno = s2i_ASN1_INTEGER(NULL, serial);
921 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
924 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
925 ASN1_INTEGER_free(sno);
926 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
928 if (!OCSP_request_add0_id(*req, id))
933 BIO_printf(bio_err, "Error Creating OCSP request\n");
937 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
938 STACK_OF(OPENSSL_STRING) *names,
939 STACK_OF(OCSP_CERTID) *ids, long nsec,
944 int i, status, reason;
945 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
947 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
948 || !sk_OCSP_CERTID_num(ids))
951 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
952 id = sk_OCSP_CERTID_value(ids, i);
953 name = sk_OPENSSL_STRING_value(names, i);
954 BIO_printf(out, "%s: ", name);
956 if (!OCSP_resp_find_status(bs, id, &status, &reason,
957 &rev, &thisupd, &nextupd)) {
958 BIO_puts(out, "ERROR: No Status found.\n");
963 * Check validity: if invalid write to output BIO so we know which
964 * response this refers to.
966 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
967 BIO_puts(out, "WARNING: Status times invalid.\n");
968 ERR_print_errors(out);
970 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
972 BIO_puts(out, "\tThis Update: ");
973 ASN1_GENERALIZEDTIME_print(out, thisupd);
977 BIO_puts(out, "\tNext Update: ");
978 ASN1_GENERALIZEDTIME_print(out, nextupd);
982 if (status != V_OCSP_CERTSTATUS_REVOKED)
986 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
988 BIO_puts(out, "\tRevocation Time: ");
989 ASN1_GENERALIZEDTIME_print(out, rev);
994 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
995 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
996 EVP_PKEY *rkey, const EVP_MD *rmd,
997 STACK_OF(OPENSSL_STRING) *sigopts,
998 STACK_OF(X509) *rother, unsigned long flags,
999 int nmin, int ndays, int badsig,
1000 const EVP_MD *resp_md)
1002 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
1004 OCSP_BASICRESP *bs = NULL;
1006 EVP_MD_CTX *mctx = NULL;
1007 EVP_PKEY_CTX *pkctx = NULL;
1009 id_count = OCSP_request_onereq_count(req);
1011 if (id_count <= 0) {
1013 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1017 bs = OCSP_BASICRESP_new();
1018 thisupd = X509_gmtime_adj(NULL, 0);
1020 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
1022 /* Examine each certificate id in the request */
1023 for (i = 0; i < id_count; i++) {
1025 ASN1_INTEGER *serial;
1029 ASN1_OBJECT *cert_id_md_oid;
1030 const EVP_MD *cert_id_md;
1031 OCSP_CERTID *cid_resp_md = NULL;
1033 one = OCSP_request_onereq_get0(req, i);
1034 cid = OCSP_onereq_get0_id(one);
1036 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1038 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
1039 if (cert_id_md == NULL) {
1040 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1044 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
1045 X509 *ca_cert = sk_X509_value(ca, jj);
1046 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
1048 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
1050 if (resp_md != NULL)
1051 cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
1053 OCSP_CERTID_free(ca_id);
1055 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1056 inf = lookup_serial(db, serial);
1058 /* at this point, we can have cid be an alias of cid_resp_md */
1059 cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
1062 OCSP_basic_add1_status(bs, cid,
1063 V_OCSP_CERTSTATUS_UNKNOWN,
1064 0, NULL, thisupd, nextupd);
1068 OCSP_basic_add1_status(bs, cid,
1069 V_OCSP_CERTSTATUS_UNKNOWN,
1070 0, NULL, thisupd, nextupd);
1071 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
1072 OCSP_basic_add1_status(bs, cid,
1073 V_OCSP_CERTSTATUS_GOOD,
1074 0, NULL, thisupd, nextupd);
1075 } else if (inf[DB_type][0] == DB_TYPE_REV) {
1076 ASN1_OBJECT *inst = NULL;
1077 ASN1_TIME *revtm = NULL;
1078 ASN1_GENERALIZEDTIME *invtm = NULL;
1079 OCSP_SINGLERESP *single;
1082 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1083 single = OCSP_basic_add1_status(bs, cid,
1084 V_OCSP_CERTSTATUS_REVOKED,
1085 reason, revtm, thisupd, nextupd);
1087 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1089 else if (inst != NULL)
1090 OCSP_SINGLERESP_add1_ext_i2d(single,
1091 NID_hold_instruction_code, inst,
1093 ASN1_OBJECT_free(inst);
1094 ASN1_TIME_free(revtm);
1095 ASN1_GENERALIZEDTIME_free(invtm);
1097 OCSP_CERTID_free(cid_resp_md);
1100 OCSP_copy_nonce(bs, req);
1102 mctx = EVP_MD_CTX_new();
1103 if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
1104 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
1107 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1108 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1110 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1111 BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1112 ERR_print_errors(bio_err);
1113 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1118 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
1119 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
1124 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
1125 corrupt_signature(sig);
1128 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1131 EVP_MD_CTX_free(mctx);
1132 ASN1_TIME_free(thisupd);
1133 ASN1_TIME_free(nextupd);
1134 OCSP_BASICRESP_free(bs);
1137 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
1141 char *itmp, *row[DB_NUMBER], **rrow;
1142 for (i = 0; i < DB_NUMBER; i++)
1144 bn = ASN1_INTEGER_to_BN(ser, NULL);
1145 OPENSSL_assert(bn); /* FIXME: should report an error at this
1146 * point and abort */
1148 itmp = OPENSSL_strdup("00");
1150 itmp = BN_bn2hex(bn);
1151 row[DB_serial] = itmp;
1153 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1158 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1161 #ifndef OPENSSL_NO_SOCK
1162 return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_RESPONSE),
1163 (ASN1_VALUE **)preq, NULL, pcbio, acbio,
1164 prog, 1 /* accept_get */, timeout);
1167 "Error getting OCSP request - sockets not supported\n");
1173 static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
1175 #ifndef OPENSSL_NO_SOCK
1176 return http_server_send_asn1_resp(cbio, "application/ocsp-response",
1177 ASN1_ITEM_rptr(OCSP_RESPONSE),
1178 (const ASN1_VALUE *)resp);
1181 "Error sending OCSP response - sockets not supported\n");
1186 #ifndef OPENSSL_NO_SOCK
1187 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1188 const char *host, const char *path,
1189 const char *port, int use_ssl,
1190 STACK_OF(CONF_VALUE) *headers,
1193 SSL_CTX *ctx = NULL;
1194 OCSP_RESPONSE *resp = NULL;
1197 ctx = SSL_CTX_new(TLS_client_method());
1199 BIO_printf(bio_err, "Error creating SSL context.\n");
1202 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1205 resp = (OCSP_RESPONSE *)
1206 app_http_post_asn1(host, port, path, NULL, NULL /* no proxy used */,
1207 ctx, headers, "application/ocsp-request",
1208 (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
1209 req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
1212 BIO_printf(bio_err, "Error querying OCSP responder\n");