2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 #ifndef OPENSSL_NO_OCSP
60 # ifdef OPENSSL_SYS_VMS
61 # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
71 # include "apps.h" /* needs to be included before the openssl
73 # include <openssl/e_os2.h>
74 # include <openssl/crypto.h>
75 # include <openssl/err.h>
76 # include <openssl/ssl.h>
77 # include <openssl/evp.h>
78 # include <openssl/bn.h>
79 # include <openssl/x509v3.h>
81 # if defined(NETWARE_CLIB)
82 # ifdef NETWARE_BSDSOCK
83 # include <sys/socket.h>
84 # include <sys/bsdskt.h>
86 # include <novsock2.h>
88 # elif defined(NETWARE_LIBC)
89 # ifdef NETWARE_BSDSOCK
90 # include <sys/select.h>
92 # include <novsock2.h>
96 /* Maximum leeway in validity period: default 5 minutes */
97 # define MAX_VALIDITY_PERIOD (5 * 60)
99 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
100 const EVP_MD *cert_id_md, X509 *issuer,
101 STACK_OF(OCSP_CERTID) *ids);
102 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
103 const EVP_MD *cert_id_md, X509 *issuer,
104 STACK_OF(OCSP_CERTID) *ids);
105 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
106 STACK_OF(OPENSSL_STRING) *names,
107 STACK_OF(OCSP_CERTID) *ids, long nsec,
109 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
110 CA_DB *db, X509 *ca, X509 *rcert,
111 EVP_PKEY *rkey, const EVP_MD *md,
112 STACK_OF(X509) *rother, unsigned long flags,
113 int nmin, int ndays, int badsig);
115 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
116 static BIO *init_responder(const char *port);
117 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
119 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
120 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *path,
121 const STACK_OF(CONF_VALUE) *headers,
122 OCSP_REQUEST *req, int req_timeout);
124 typedef enum OPTION_choice {
125 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
126 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
127 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
128 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
129 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
130 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
131 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
132 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
133 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH,
134 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
135 OPT_RESPOUT, OPT_PATH, OPT_CERT, OPT_SERIAL,
136 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
137 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
142 OPTIONS ocsp_options[] = {
143 {"help", OPT_HELP, '-', "Display this summary"},
144 {"out", OPT_OUTFILE, '>', "Output filename"},
145 {"timeout", OPT_TIMEOUT, 'p'},
146 {"url", OPT_URL, 's', "Responder URL"},
147 {"host", OPT_HOST, 's', "host:prot top to connect to"},
148 {"port", OPT_PORT, 'p', "Port to run responder on"},
149 {"ignore_err", OPT_IGNORE_ERR, '-'},
150 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
151 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
152 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
153 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
154 "Don't include any certificates in response"},
155 {"resp_key_id", OPT_RESP_KEY_ID, '-',
156 "Identify reponse by signing certificate key ID"},
157 {"no_certs", OPT_NO_CERTS, '-',
158 "Don't include any certificates in signed request"},
159 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
160 "Don't check signature on response"},
161 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
162 "Don't check signing certificate"},
163 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
164 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
165 "Don't do additional checks on signing certificate"},
166 {"no_explicit", OPT_NO_EXPLICIT, '-'},
167 {"trust_other", OPT_TRUST_OTHER, '-',
168 "Don't verify additional certificates"},
169 {"no_intern", OPT_NO_INTERN, '-',
170 "Don't search certificates contained in response for signer"},
171 {"badsig", OPT_BADSIG, '-'},
172 {"text", OPT_TEXT, '-', "Print text form of request and response"},
173 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
174 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
175 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
176 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
177 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
178 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
179 {"sign_other", OPT_SIGN_OTHER, '<',
180 "Additional certificates to include in signed request"},
181 {"verify_other", OPT_VERIFY_OTHER, '<',
182 "Additional certificates to search for signer"},
183 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
184 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
185 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
186 "Maximum validity discrepancy in seconds"},
187 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
188 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
189 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
190 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
191 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
192 {"cert", OPT_CERT, '<', "Certificate to check"},
193 {"serial", OPT_SERIAL, 's', "Nerial number to check"},
194 {"index", OPT_INDEX, '<', "Certificate status index file"},
195 {"CA", OPT_CA, '<', "CA certificate"},
196 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
197 {"nrequest", OPT_REQUEST, 'p',
198 "Number of requests to accept (default unlimited)"},
199 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
200 {"rsigner", OPT_RSIGNER, '<',
201 "Sesponder certificate to sign responses with"},
202 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
203 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
204 {"rmd", OPT_RMD, 's'},
205 {"header", OPT_HEADER, 's', "key=value header to add"},
206 {"", OPT_MD, '-', "Any supported digest"},
211 int ocsp_main(int argc, char **argv)
213 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
214 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
216 EVP_PKEY *key = NULL, *rkey = NULL;
217 OCSP_BASICRESP *bs = NULL;
218 OCSP_REQUEST *req = NULL;
219 OCSP_RESPONSE *resp = NULL;
220 STACK_OF(CONF_VALUE) *headers = NULL;
221 STACK_OF(OCSP_CERTID) *ids = NULL;
222 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
223 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
224 X509 *issuer = NULL, *cert = NULL, *rca_cert = NULL;
225 X509 *signer = NULL, *rsigner = NULL;
226 X509_STORE *store = NULL;
227 X509_VERIFY_PARAM *vpm = NULL;
228 char *CAfile = NULL, *CApath = NULL, *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 *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
234 char *signfile = NULL, *keyfile = NULL;
235 char *thost = NULL, *tport = NULL, *tpath = NULL;
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, req_timeout = -1, ret = 1;
239 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
240 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
244 reqnames = sk_OPENSSL_STRING_new_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 req_timeout = atoi(opt_arg());
278 if (!OCSP_parse_url(opt_arg(), &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();
372 if (!opt_verify(o, vpm))
376 case OPT_VALIDITY_PERIOD:
377 opt_long(opt_arg(), &nsec);
380 opt_long(opt_arg(), &maxage);
396 cert = load_cert(opt_arg(), FORMAT_PEM,
397 NULL, NULL, "certificate");
400 if (cert_id_md == NULL)
401 cert_id_md = EVP_sha1();
402 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
404 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
408 if (cert_id_md == NULL)
409 cert_id_md = EVP_sha1();
410 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
412 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
416 ridx_filename = opt_arg();
419 rca_filename = opt_arg();
422 opt_int(opt_arg(), &nmin);
427 opt_int(opt_arg(), &accept_count);
430 ndays = atoi(opt_arg());
433 rsignfile = opt_arg();
436 rkeyfile = opt_arg();
439 rcertfile = opt_arg();
442 if (!opt_md(opt_arg(), &rsign_md))
447 value = strchr(header, '=');
449 BIO_printf(bio_err, "Missing = in header key=value\n");
453 if (!X509V3_add_value(header, value, &headers))
457 if (cert_id_md != NULL) {
459 "%s: Digest must be before -cert or -serial\n",
463 if (!opt_md(opt_unknown(), &cert_id_md))
468 argc = opt_num_rest();
471 /* Have we anything to do? */
472 if (!req && !reqin && !respin && !(port && ridx_filename))
475 out = bio_open_default(outfile, "w");
479 if (!req && (add_nonce != 2))
483 derbio = bio_open_default(reqin, "rb");
486 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
489 BIO_printf(bio_err, "Error reading OCSP request\n");
495 acbio = init_responder(port);
500 if (rsignfile && !rdb) {
502 rkeyfile = rsignfile;
503 rsigner = load_cert(rsignfile, FORMAT_PEM,
504 NULL, NULL, "responder certificate");
506 BIO_printf(bio_err, "Error loading responder certificate\n");
509 rca_cert = load_cert(rca_filename, FORMAT_PEM,
510 NULL, NULL, "CA certificate");
512 rother = load_certs(rcertfile, FORMAT_PEM,
513 NULL, NULL, "responder other certificates");
517 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
518 "responder private key");
523 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
528 if (!do_responder(&req, &cbio, acbio, port))
532 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
534 send_ocsp_response(cbio, resp);
539 if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
540 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
544 if (req && add_nonce)
545 OCSP_request_add1_nonce(req, NULL, -1);
550 signer = load_cert(signfile, FORMAT_PEM,
551 NULL, NULL, "signer certificate");
553 BIO_printf(bio_err, "Error loading signer certificate\n");
557 sign_other = load_certs(sign_certfile, FORMAT_PEM,
558 NULL, NULL, "signer certificates");
562 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
563 "signer private key");
567 if (!OCSP_request_sign
568 (req, signer, key, NULL, sign_other, sign_flags)) {
569 BIO_printf(bio_err, "Error signing OCSP request\n");
575 OCSP_REQUEST_print(out, req, 0);
578 derbio = bio_open_default(reqout, "wb");
581 i2d_OCSP_REQUEST_bio(derbio, req);
585 if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
587 "Need a responder certificate, key and CA for this operation!\n");
591 if (ridx_filename && !rdb) {
592 rdb = load_index(ridx_filename, NULL);
595 if (!index_index(rdb))
600 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
601 rsign_md, rother, rflags, nmin, ndays, badsig);
603 send_ocsp_response(cbio, resp);
605 # ifndef OPENSSL_NO_SOCK
606 resp = process_responder(req, host, path,
607 port, use_ssl, headers, req_timeout);
612 "Error creating connect BIO - sockets not supported.\n");
616 derbio = bio_open_default(respin, "rb");
619 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
622 BIO_printf(bio_err, "Error reading OCSP response\n");
633 derbio = bio_open_default(respout, "wb");
636 i2d_OCSP_RESPONSE_bio(derbio, resp);
640 i = OCSP_response_status(resp);
641 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
642 BIO_printf(out, "Responder Error: %s (%d)\n",
643 OCSP_response_status_str(i), i);
651 OCSP_RESPONSE_print(out, resp, 0);
653 /* If running as responder don't verify our own response */
655 if (--accept_count <= 0) {
661 OCSP_REQUEST_free(req);
663 OCSP_RESPONSE_free(resp);
673 store = setup_verify(CAfile, CApath);
678 X509_STORE_set1_param(store, vpm);
679 if (verify_certfile) {
680 verify_other = load_certs(verify_certfile, FORMAT_PEM,
681 NULL, NULL, "validator certificate");
686 bs = OCSP_response_get1_basic(resp);
688 BIO_printf(bio_err, "Error parsing response\n");
695 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
697 BIO_printf(bio_err, "WARNING: no nonce in response\n");
699 BIO_printf(bio_err, "Nonce Verify error\n");
705 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
707 BIO_printf(bio_err, "Response Verify Failure\n");
708 ERR_print_errors(bio_err);
711 BIO_printf(bio_err, "Response verify OK\n");
715 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
718 ERR_print_errors(bio_err);
720 X509_STORE_free(store);
722 X509_VERIFY_PARAM_free(vpm);
732 OCSP_REQUEST_free(req);
733 OCSP_RESPONSE_free(resp);
734 OCSP_BASICRESP_free(bs);
735 sk_OPENSSL_STRING_free(reqnames);
736 sk_OCSP_CERTID_free(ids);
737 sk_X509_pop_free(sign_other, X509_free);
738 sk_X509_pop_free(verify_other, X509_free);
739 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
751 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
752 const EVP_MD *cert_id_md, X509 *issuer,
753 STACK_OF(OCSP_CERTID) *ids)
757 BIO_printf(bio_err, "No issuer certificate specified\n");
761 *req = OCSP_REQUEST_new();
764 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
765 if (!id || !sk_OCSP_CERTID_push(ids, id))
767 if (!OCSP_request_add0_id(*req, id))
772 BIO_printf(bio_err, "Error Creating OCSP request\n");
776 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
777 const EVP_MD *cert_id_md, X509 *issuer,
778 STACK_OF(OCSP_CERTID) *ids)
782 ASN1_BIT_STRING *ikey;
785 BIO_printf(bio_err, "No issuer certificate specified\n");
789 *req = OCSP_REQUEST_new();
792 iname = X509_get_subject_name(issuer);
793 ikey = X509_get0_pubkey_bitstr(issuer);
794 sno = s2i_ASN1_INTEGER(NULL, serial);
796 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
799 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
800 ASN1_INTEGER_free(sno);
801 if (!id || !sk_OCSP_CERTID_push(ids, id))
803 if (!OCSP_request_add0_id(*req, id))
808 BIO_printf(bio_err, "Error Creating OCSP request\n");
812 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
813 STACK_OF(OPENSSL_STRING) *names,
814 STACK_OF(OCSP_CERTID) *ids, long nsec,
819 int i, status, reason;
820 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
822 if (!bs || !req || !sk_OPENSSL_STRING_num(names)
823 || !sk_OCSP_CERTID_num(ids))
826 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
827 id = sk_OCSP_CERTID_value(ids, i);
828 name = sk_OPENSSL_STRING_value(names, i);
829 BIO_printf(out, "%s: ", name);
831 if (!OCSP_resp_find_status(bs, id, &status, &reason,
832 &rev, &thisupd, &nextupd)) {
833 BIO_puts(out, "ERROR: No Status found.\n");
838 * Check validity: if invalid write to output BIO so we know which
839 * response this refers to.
841 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
842 BIO_puts(out, "WARNING: Status times invalid.\n");
843 ERR_print_errors(out);
845 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
847 BIO_puts(out, "\tThis Update: ");
848 ASN1_GENERALIZEDTIME_print(out, thisupd);
852 BIO_puts(out, "\tNext Update: ");
853 ASN1_GENERALIZEDTIME_print(out, nextupd);
857 if (status != V_OCSP_CERTSTATUS_REVOKED)
861 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
863 BIO_puts(out, "\tRevocation Time: ");
864 ASN1_GENERALIZEDTIME_print(out, rev);
869 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
870 CA_DB *db, X509 *ca, X509 *rcert,
871 EVP_PKEY *rkey, const EVP_MD *rmd,
872 STACK_OF(X509) *rother, unsigned long flags,
873 int nmin, int ndays, int badsig)
875 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
876 OCSP_CERTID *cid, *ca_id = NULL;
877 OCSP_BASICRESP *bs = NULL;
880 id_count = OCSP_request_onereq_count(req);
884 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
888 bs = OCSP_BASICRESP_new();
889 thisupd = X509_gmtime_adj(NULL, 0);
891 nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24);
893 /* Examine each certificate id in the request */
894 for (i = 0; i < id_count; i++) {
896 ASN1_INTEGER *serial;
898 ASN1_OBJECT *cert_id_md_oid;
899 const EVP_MD *cert_id_md;
900 one = OCSP_request_onereq_get0(req, i);
901 cid = OCSP_onereq_get0_id(one);
903 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
905 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
907 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
912 OCSP_CERTID_free(ca_id);
913 ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
915 /* Is this request about our CA? */
916 if (OCSP_id_issuer_cmp(ca_id, cid)) {
917 OCSP_basic_add1_status(bs, cid,
918 V_OCSP_CERTSTATUS_UNKNOWN,
919 0, NULL, thisupd, nextupd);
922 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
923 inf = lookup_serial(db, serial);
925 OCSP_basic_add1_status(bs, cid,
926 V_OCSP_CERTSTATUS_UNKNOWN,
927 0, NULL, thisupd, nextupd);
928 else if (inf[DB_type][0] == DB_TYPE_VAL)
929 OCSP_basic_add1_status(bs, cid,
930 V_OCSP_CERTSTATUS_GOOD,
931 0, NULL, thisupd, nextupd);
932 else if (inf[DB_type][0] == DB_TYPE_REV) {
933 ASN1_OBJECT *inst = NULL;
934 ASN1_TIME *revtm = NULL;
935 ASN1_GENERALIZEDTIME *invtm = NULL;
936 OCSP_SINGLERESP *single;
938 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
939 single = OCSP_basic_add1_status(bs, cid,
940 V_OCSP_CERTSTATUS_REVOKED,
941 reason, revtm, thisupd, nextupd);
943 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
946 OCSP_SINGLERESP_add1_ext_i2d(single,
947 NID_hold_instruction_code, inst,
949 ASN1_OBJECT_free(inst);
950 ASN1_TIME_free(revtm);
951 ASN1_GENERALIZEDTIME_free(invtm);
955 OCSP_copy_nonce(bs, req);
957 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
960 ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
961 unsigned char *sigptr = ASN1_STRING_data(sig);
962 sigptr[ASN1_STRING_length(sig) - 1] ^= 0x1;
965 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
968 ASN1_TIME_free(thisupd);
969 ASN1_TIME_free(nextupd);
970 OCSP_CERTID_free(ca_id);
971 OCSP_BASICRESP_free(bs);
974 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
978 char *itmp, *row[DB_NUMBER], **rrow;
979 for (i = 0; i < DB_NUMBER; i++)
981 bn = ASN1_INTEGER_to_BN(ser, NULL);
982 OPENSSL_assert(bn); /* FIXME: should report an error at this
985 itmp = BUF_strdup("00");
987 itmp = BN_bn2hex(bn);
988 row[DB_serial] = itmp;
990 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
995 /* Quick and dirty OCSP server: read in and parse input request */
997 static BIO *init_responder(const char *port)
999 BIO *acbio = NULL, *bufbio = NULL;
1001 bufbio = BIO_new(BIO_f_buffer());
1004 # ifndef OPENSSL_NO_SOCK
1005 acbio = BIO_new_accept(port);
1008 "Error setting up accept BIO - sockets not supported.\n");
1012 BIO_set_accept_bios(acbio, bufbio);
1015 if (BIO_do_accept(acbio) <= 0) {
1016 BIO_printf(bio_err, "Error setting up accept BIO\n");
1017 ERR_print_errors(bio_err);
1024 BIO_free_all(acbio);
1029 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1033 OCSP_REQUEST *req = NULL;
1037 if (BIO_do_accept(acbio) <= 0) {
1038 BIO_printf(bio_err, "Error accepting connection\n");
1039 ERR_print_errors(bio_err);
1043 cbio = BIO_pop(acbio);
1046 /* Read the request line. */
1047 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1050 if (strncmp(inbuf, "POST", 4) != 0) {
1051 BIO_printf(bio_err, "Invalid request\n");
1055 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1058 /* Look for end of headers */
1059 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1063 /* Try to read OCSP request */
1064 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1067 BIO_printf(bio_err, "Error parsing OCSP request\n");
1068 ERR_print_errors(bio_err);
1077 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1080 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1081 "Content-Length: %d\r\n\r\n";
1084 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1085 i2d_OCSP_RESPONSE_bio(cbio, resp);
1086 (void)BIO_flush(cbio);
1090 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *path,
1091 const STACK_OF(CONF_VALUE) *headers,
1092 OCSP_REQUEST *req, int req_timeout)
1097 OCSP_REQ_CTX *ctx = NULL;
1098 OCSP_RESPONSE *rsp = NULL;
1102 if (req_timeout != -1)
1103 BIO_set_nbio(cbio, 1);
1105 rv = BIO_do_connect(cbio);
1107 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1108 BIO_puts(bio_err, "Error connecting BIO\n");
1112 if (BIO_get_fd(cbio, &fd) <= 0) {
1113 BIO_puts(bio_err, "Can't get connection fd\n");
1117 if (req_timeout != -1 && rv <= 0) {
1119 openssl_fdset(fd, &confds);
1121 tv.tv_sec = req_timeout;
1122 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1124 BIO_puts(bio_err, "Timeout on connect\n");
1129 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1133 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1134 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1135 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1139 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1143 rv = OCSP_sendreq_nbio(&rsp, ctx);
1146 if (req_timeout == -1)
1149 openssl_fdset(fd, &confds);
1151 tv.tv_sec = req_timeout;
1152 if (BIO_should_read(cbio))
1153 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1154 else if (BIO_should_write(cbio))
1155 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1157 BIO_puts(bio_err, "Unexpected retry condition\n");
1161 BIO_puts(bio_err, "Timeout on request\n");
1165 BIO_puts(bio_err, "Select error\n");
1172 OCSP_REQ_CTX_free(ctx);
1177 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1178 const char *host, const char *path,
1179 const char *port, int use_ssl,
1180 const STACK_OF(CONF_VALUE) *headers,
1184 SSL_CTX *ctx = NULL;
1185 OCSP_RESPONSE *resp = NULL;
1186 cbio = BIO_new_connect(host);
1188 BIO_printf(bio_err, "Error creating connect BIO\n");
1192 BIO_set_conn_port(cbio, port);
1195 ctx = SSL_CTX_new(SSLv23_client_method());
1197 BIO_printf(bio_err, "Error creating SSL context.\n");
1200 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1201 sbio = BIO_new_ssl(ctx, 1);
1202 cbio = BIO_push(sbio, cbio);
1204 resp = query_responder(cbio, path, headers, req, req_timeout);
1206 BIO_printf(bio_err, "Error querying OCSP responder\n");