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
72 # include "apps.h" /* needs to be included before the openssl
74 # include <openssl/e_os2.h>
75 # include <openssl/crypto.h>
76 # include <openssl/err.h>
77 # include <openssl/ssl.h>
78 # include <openssl/evp.h>
79 # include <openssl/bn.h>
80 # include <openssl/x509v3.h>
82 # if defined(NETWARE_CLIB)
83 # ifdef NETWARE_BSDSOCK
84 # include <sys/socket.h>
85 # include <sys/bsdskt.h>
87 # include <novsock2.h>
89 # elif defined(NETWARE_LIBC)
90 # ifdef NETWARE_BSDSOCK
91 # include <sys/select.h>
93 # include <novsock2.h>
97 /* Maximum leeway in validity period: default 5 minutes */
98 # define MAX_VALIDITY_PERIOD (5 * 60)
100 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
101 const EVP_MD *cert_id_md, X509 *issuer,
102 STACK_OF(OCSP_CERTID) *ids);
103 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
104 const EVP_MD *cert_id_md, X509 *issuer,
105 STACK_OF(OCSP_CERTID) *ids);
106 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
107 STACK_OF(OPENSSL_STRING) *names,
108 STACK_OF(OCSP_CERTID) *ids, long nsec,
110 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
111 CA_DB *db, X509 *ca, X509 *rcert,
112 EVP_PKEY *rkey, const EVP_MD *md,
113 STACK_OF(X509) *rother, unsigned long flags,
114 int nmin, int ndays, int badsig);
116 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
117 static BIO *init_responder(const char *port);
118 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
120 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
121 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *path,
122 const STACK_OF(CONF_VALUE) *headers,
123 OCSP_REQUEST *req, int req_timeout);
125 typedef enum OPTION_choice {
126 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
127 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
128 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
129 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
130 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
131 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
132 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
133 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
134 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH,
135 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
136 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
137 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
138 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
143 OPTIONS ocsp_options[] = {
144 {"help", OPT_HELP, '-', "Display this summary"},
145 {"out", OPT_OUTFILE, '>', "Output filename"},
146 {"timeout", OPT_TIMEOUT, 'p'},
147 {"url", OPT_URL, 's', "Responder URL"},
148 {"host", OPT_HOST, 's', "host:prot top to connect to"},
149 {"port", OPT_PORT, 'p', "Port to run responder on"},
150 {"ignore_err", OPT_IGNORE_ERR, '-'},
151 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
152 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
153 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
154 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
155 "Don't include any certificates in response"},
156 {"resp_key_id", OPT_RESP_KEY_ID, '-',
157 "Identify reponse by signing certificate key ID"},
158 {"no_certs", OPT_NO_CERTS, '-',
159 "Don't include any certificates in signed request"},
160 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
161 "Don't check signature on response"},
162 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
163 "Don't check signing certificate"},
164 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
165 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
166 "Don't do additional checks on signing certificate"},
167 {"no_explicit", OPT_NO_EXPLICIT, '-'},
168 {"trust_other", OPT_TRUST_OTHER, '-',
169 "Don't verify additional certificates"},
170 {"no_intern", OPT_NO_INTERN, '-',
171 "Don't search certificates contained in response for signer"},
172 {"badsig", OPT_BADSIG, '-'},
173 {"text", OPT_TEXT, '-', "Print text form of request and response"},
174 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
175 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
176 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
177 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
178 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
179 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
180 {"sign_other", OPT_SIGN_OTHER, '<',
181 "Additional certificates to include in signed request"},
182 {"verify_other", OPT_VERIFY_OTHER, '<',
183 "Additional certificates to search for signer"},
184 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
185 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
186 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
187 "Maximum validity discrepancy in seconds"},
188 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
189 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
190 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
191 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
192 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
193 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
194 {"cert", OPT_CERT, '<', "Certificate to check"},
195 {"serial", OPT_SERIAL, 's', "Nerial number to check"},
196 {"index", OPT_INDEX, '<', "Certificate status index file"},
197 {"CA", OPT_CA, '<', "CA certificate"},
198 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
199 {"nrequest", OPT_REQUEST, 'p',
200 "Number of requests to accept (default unlimited)"},
201 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
202 {"rsigner", OPT_RSIGNER, '<',
203 "Sesponder certificate to sign responses with"},
204 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
205 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
206 {"rmd", OPT_RMD, 's'},
207 {"header", OPT_HEADER, 's', "key=value header to add"},
208 {"", OPT_MD, '-', "Any supported digest"},
213 int ocsp_main(int argc, char **argv)
215 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
216 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
218 EVP_PKEY *key = NULL, *rkey = NULL;
219 OCSP_BASICRESP *bs = NULL;
220 OCSP_REQUEST *req = NULL;
221 OCSP_RESPONSE *resp = NULL;
222 STACK_OF(CONF_VALUE) *headers = NULL;
223 STACK_OF(OCSP_CERTID) *ids = NULL;
224 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
225 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
226 STACK_OF(X509) *issuers = NULL;
227 X509 *issuer = NULL, *cert = NULL, *rca_cert = NULL;
228 X509 *signer = NULL, *rsigner = NULL;
229 X509_STORE *store = NULL;
230 X509_VERIFY_PARAM *vpm = NULL;
231 char *CAfile = NULL, *CApath = NULL, *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 *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
237 char *signfile = NULL, *keyfile = NULL;
238 char *thost = NULL, *tport = NULL, *tpath = NULL;
239 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
240 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
241 int req_text = 0, resp_text = 0, req_timeout = -1, ret = 1;
242 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
243 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
247 reqnames = sk_OPENSSL_STRING_new_null();
250 ids = sk_OCSP_CERTID_new_null();
253 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
256 prog = opt_init(argc, argv, ocsp_options);
257 while ((o = opt_next()) != OPT_EOF) {
262 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
266 opt_help(ocsp_options);
272 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 issuer = load_cert(opt_arg(), FORMAT_PEM,
397 NULL, NULL, "issuer certificate");
400 if ((issuers = sk_X509_new_null()) == NULL)
402 sk_X509_push(issuers, issuer);
406 cert = load_cert(opt_arg(), FORMAT_PEM,
407 NULL, NULL, "certificate");
410 if (cert_id_md == NULL)
411 cert_id_md = EVP_sha1();
412 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
414 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
418 if (cert_id_md == NULL)
419 cert_id_md = EVP_sha1();
420 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
422 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
426 ridx_filename = opt_arg();
429 rca_filename = opt_arg();
432 opt_int(opt_arg(), &nmin);
437 opt_int(opt_arg(), &accept_count);
440 ndays = atoi(opt_arg());
443 rsignfile = opt_arg();
446 rkeyfile = opt_arg();
449 rcertfile = opt_arg();
452 if (!opt_md(opt_arg(), &rsign_md))
457 value = strchr(header, '=');
459 BIO_printf(bio_err, "Missing = in header key=value\n");
463 if (!X509V3_add_value(header, value, &headers))
467 if (cert_id_md != NULL) {
469 "%s: Digest must be before -cert or -serial\n",
473 if (!opt_md(opt_unknown(), &cert_id_md))
478 argc = opt_num_rest();
481 /* Have we anything to do? */
482 if (!req && !reqin && !respin && !(port && ridx_filename))
485 if (!app_load_modules(NULL))
488 out = bio_open_default(outfile, "w");
492 if (!req && (add_nonce != 2))
496 derbio = bio_open_default(reqin, "rb");
499 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
502 BIO_printf(bio_err, "Error reading OCSP request\n");
508 acbio = init_responder(port);
513 if (rsignfile && !rdb) {
515 rkeyfile = rsignfile;
516 rsigner = load_cert(rsignfile, FORMAT_PEM,
517 NULL, NULL, "responder certificate");
519 BIO_printf(bio_err, "Error loading responder certificate\n");
522 rca_cert = load_cert(rca_filename, FORMAT_PEM,
523 NULL, NULL, "CA certificate");
525 rother = load_certs(rcertfile, FORMAT_PEM,
526 NULL, NULL, "responder other certificates");
530 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
531 "responder private key");
536 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
541 if (!do_responder(&req, &cbio, acbio, port))
545 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
547 send_ocsp_response(cbio, resp);
552 if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
553 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
557 if (req && add_nonce)
558 OCSP_request_add1_nonce(req, NULL, -1);
563 signer = load_cert(signfile, FORMAT_PEM,
564 NULL, NULL, "signer certificate");
566 BIO_printf(bio_err, "Error loading signer certificate\n");
570 sign_other = load_certs(sign_certfile, FORMAT_PEM,
571 NULL, NULL, "signer certificates");
575 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
576 "signer private key");
580 if (!OCSP_request_sign
581 (req, signer, key, NULL, sign_other, sign_flags)) {
582 BIO_printf(bio_err, "Error signing OCSP request\n");
588 OCSP_REQUEST_print(out, req, 0);
591 derbio = bio_open_default(reqout, "wb");
594 i2d_OCSP_REQUEST_bio(derbio, req);
598 if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
600 "Need a responder certificate, key and CA for this operation!\n");
604 if (ridx_filename && !rdb) {
605 rdb = load_index(ridx_filename, NULL);
608 if (!index_index(rdb))
613 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
614 rsign_md, rother, rflags, nmin, ndays, badsig);
616 send_ocsp_response(cbio, resp);
618 # ifndef OPENSSL_NO_SOCK
619 resp = process_responder(req, host, path,
620 port, use_ssl, headers, req_timeout);
625 "Error creating connect BIO - sockets not supported.\n");
629 derbio = bio_open_default(respin, "rb");
632 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
635 BIO_printf(bio_err, "Error reading OCSP response\n");
646 derbio = bio_open_default(respout, "wb");
649 i2d_OCSP_RESPONSE_bio(derbio, resp);
653 i = OCSP_response_status(resp);
654 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
655 BIO_printf(out, "Responder Error: %s (%d)\n",
656 OCSP_response_status_str(i), i);
664 OCSP_RESPONSE_print(out, resp, 0);
666 /* If running as responder don't verify our own response */
668 if (--accept_count <= 0) {
674 OCSP_REQUEST_free(req);
676 OCSP_RESPONSE_free(resp);
686 store = setup_verify(CAfile, CApath);
691 X509_STORE_set1_param(store, vpm);
692 if (verify_certfile) {
693 verify_other = load_certs(verify_certfile, FORMAT_PEM,
694 NULL, NULL, "validator certificate");
699 bs = OCSP_response_get1_basic(resp);
701 BIO_printf(bio_err, "Error parsing response\n");
708 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
710 BIO_printf(bio_err, "WARNING: no nonce in response\n");
712 BIO_printf(bio_err, "Nonce Verify error\n");
718 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
719 if (i <= 0 && issuers) {
720 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
725 BIO_printf(bio_err, "Response Verify Failure\n");
726 ERR_print_errors(bio_err);
729 BIO_printf(bio_err, "Response verify OK\n");
733 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
736 ERR_print_errors(bio_err);
738 X509_STORE_free(store);
739 X509_VERIFY_PARAM_free(vpm);
749 OCSP_REQUEST_free(req);
750 OCSP_RESPONSE_free(resp);
751 OCSP_BASICRESP_free(bs);
752 sk_OPENSSL_STRING_free(reqnames);
753 sk_OCSP_CERTID_free(ids);
754 sk_X509_pop_free(sign_other, X509_free);
755 sk_X509_pop_free(verify_other, X509_free);
756 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
764 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
765 const EVP_MD *cert_id_md, X509 *issuer,
766 STACK_OF(OCSP_CERTID) *ids)
770 BIO_printf(bio_err, "No issuer certificate specified\n");
774 *req = OCSP_REQUEST_new();
777 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
778 if (!id || !sk_OCSP_CERTID_push(ids, id))
780 if (!OCSP_request_add0_id(*req, id))
785 BIO_printf(bio_err, "Error Creating OCSP request\n");
789 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
790 const EVP_MD *cert_id_md, X509 *issuer,
791 STACK_OF(OCSP_CERTID) *ids)
795 ASN1_BIT_STRING *ikey;
798 BIO_printf(bio_err, "No issuer certificate specified\n");
802 *req = OCSP_REQUEST_new();
805 iname = X509_get_subject_name(issuer);
806 ikey = X509_get0_pubkey_bitstr(issuer);
807 sno = s2i_ASN1_INTEGER(NULL, serial);
809 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
812 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
813 ASN1_INTEGER_free(sno);
814 if (!id || !sk_OCSP_CERTID_push(ids, id))
816 if (!OCSP_request_add0_id(*req, id))
821 BIO_printf(bio_err, "Error Creating OCSP request\n");
825 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
826 STACK_OF(OPENSSL_STRING) *names,
827 STACK_OF(OCSP_CERTID) *ids, long nsec,
832 int i, status, reason;
833 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
835 if (!bs || !req || !sk_OPENSSL_STRING_num(names)
836 || !sk_OCSP_CERTID_num(ids))
839 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
840 id = sk_OCSP_CERTID_value(ids, i);
841 name = sk_OPENSSL_STRING_value(names, i);
842 BIO_printf(out, "%s: ", name);
844 if (!OCSP_resp_find_status(bs, id, &status, &reason,
845 &rev, &thisupd, &nextupd)) {
846 BIO_puts(out, "ERROR: No Status found.\n");
851 * Check validity: if invalid write to output BIO so we know which
852 * response this refers to.
854 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
855 BIO_puts(out, "WARNING: Status times invalid.\n");
856 ERR_print_errors(out);
858 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
860 BIO_puts(out, "\tThis Update: ");
861 ASN1_GENERALIZEDTIME_print(out, thisupd);
865 BIO_puts(out, "\tNext Update: ");
866 ASN1_GENERALIZEDTIME_print(out, nextupd);
870 if (status != V_OCSP_CERTSTATUS_REVOKED)
874 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
876 BIO_puts(out, "\tRevocation Time: ");
877 ASN1_GENERALIZEDTIME_print(out, rev);
882 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
883 CA_DB *db, X509 *ca, X509 *rcert,
884 EVP_PKEY *rkey, const EVP_MD *rmd,
885 STACK_OF(X509) *rother, unsigned long flags,
886 int nmin, int ndays, int badsig)
888 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
889 OCSP_CERTID *cid, *ca_id = NULL;
890 OCSP_BASICRESP *bs = NULL;
893 id_count = OCSP_request_onereq_count(req);
897 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
901 bs = OCSP_BASICRESP_new();
902 thisupd = X509_gmtime_adj(NULL, 0);
904 nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24);
906 /* Examine each certificate id in the request */
907 for (i = 0; i < id_count; i++) {
909 ASN1_INTEGER *serial;
911 ASN1_OBJECT *cert_id_md_oid;
912 const EVP_MD *cert_id_md;
913 one = OCSP_request_onereq_get0(req, i);
914 cid = OCSP_onereq_get0_id(one);
916 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
918 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
920 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
924 OCSP_CERTID_free(ca_id);
925 ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
927 /* Is this request about our CA? */
928 if (OCSP_id_issuer_cmp(ca_id, cid)) {
929 OCSP_basic_add1_status(bs, cid,
930 V_OCSP_CERTSTATUS_UNKNOWN,
931 0, NULL, thisupd, nextupd);
934 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
935 inf = lookup_serial(db, serial);
937 OCSP_basic_add1_status(bs, cid,
938 V_OCSP_CERTSTATUS_UNKNOWN,
939 0, NULL, thisupd, nextupd);
940 else if (inf[DB_type][0] == DB_TYPE_VAL)
941 OCSP_basic_add1_status(bs, cid,
942 V_OCSP_CERTSTATUS_GOOD,
943 0, NULL, thisupd, nextupd);
944 else if (inf[DB_type][0] == DB_TYPE_REV) {
945 ASN1_OBJECT *inst = NULL;
946 ASN1_TIME *revtm = NULL;
947 ASN1_GENERALIZEDTIME *invtm = NULL;
948 OCSP_SINGLERESP *single;
950 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
951 single = OCSP_basic_add1_status(bs, cid,
952 V_OCSP_CERTSTATUS_REVOKED,
953 reason, revtm, thisupd, nextupd);
955 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
958 OCSP_SINGLERESP_add1_ext_i2d(single,
959 NID_hold_instruction_code, inst,
961 ASN1_OBJECT_free(inst);
962 ASN1_TIME_free(revtm);
963 ASN1_GENERALIZEDTIME_free(invtm);
967 OCSP_copy_nonce(bs, req);
969 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
972 ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
973 unsigned char *sigptr = ASN1_STRING_data(sig);
974 sigptr[ASN1_STRING_length(sig) - 1] ^= 0x1;
977 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
980 ASN1_TIME_free(thisupd);
981 ASN1_TIME_free(nextupd);
982 OCSP_CERTID_free(ca_id);
983 OCSP_BASICRESP_free(bs);
986 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
990 char *itmp, *row[DB_NUMBER], **rrow;
991 for (i = 0; i < DB_NUMBER; i++)
993 bn = ASN1_INTEGER_to_BN(ser, NULL);
994 OPENSSL_assert(bn); /* FIXME: should report an error at this
997 itmp = BUF_strdup("00");
999 itmp = BN_bn2hex(bn);
1000 row[DB_serial] = itmp;
1002 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1007 /* Quick and dirty OCSP server: read in and parse input request */
1009 static BIO *init_responder(const char *port)
1011 BIO *acbio = NULL, *bufbio = NULL;
1013 # ifdef OPENSSL_NO_SOCK
1015 "Error setting up accept BIO - sockets not supported.\n");
1018 bufbio = BIO_new(BIO_f_buffer());
1021 acbio = BIO_new(BIO_s_accept());
1023 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1024 || BIO_set_accept_port(acbio, port) < 0) {
1025 BIO_printf(bio_err, "Error setting up accept BIO\n");
1026 ERR_print_errors(bio_err);
1030 BIO_set_accept_bios(acbio, bufbio);
1032 if (BIO_do_accept(acbio) <= 0) {
1033 BIO_printf(bio_err, "Error starting accept\n");
1034 ERR_print_errors(bio_err);
1041 BIO_free_all(acbio);
1048 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1050 static int urldecode(char *p)
1052 unsigned char *out = (unsigned char *)p;
1053 unsigned char *save = out;
1058 else if (isxdigit(p[1]) && isxdigit(p[2])) {
1059 *out++ = (app_hex(p[1]) << 4) | app_hex(p[2]);
1066 return (int)(out - save);
1069 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1073 OCSP_REQUEST *req = NULL;
1074 char inbuf[2048], reqbuf[2048];
1076 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
1078 if (BIO_do_accept(acbio) <= 0) {
1079 BIO_printf(bio_err, "Error accepting connection\n");
1080 ERR_print_errors(bio_err);
1084 cbio = BIO_pop(acbio);
1087 /* Read the request line. */
1088 len = BIO_gets(cbio, reqbuf, sizeof reqbuf);
1091 if (strncmp(reqbuf, "GET ", 4) == 0) {
1092 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
1093 for (p = reqbuf + 4; *p == ' '; ++p)
1096 BIO_printf(bio_err, "Invalid request -- bad URL\n");
1101 /* Splice off the HTTP version identifier. */
1102 for (q = p; *q; q++)
1105 if (strncmp(q, " HTTP/1.", 8) != 0) {
1106 BIO_printf(bio_err, "Invalid request -- bad HTTP vesion\n");
1112 BIO_printf(bio_err, "Invalid request -- bad URL encoding\n");
1115 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1116 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
1117 BIO_printf(bio_err, "Could not allocate memory\n");
1118 ERR_print_errors(bio_err);
1121 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1122 getbio = BIO_push(b64, getbio);
1123 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
1124 BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n");
1128 /* Read and skip past the headers. */
1130 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1133 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1137 /* Try to read OCSP request */
1139 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1140 BIO_free_all(getbio);
1142 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1145 BIO_printf(bio_err, "Error parsing OCSP request\n");
1146 ERR_print_errors(bio_err);
1155 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1158 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1159 "Content-Length: %d\r\n\r\n";
1162 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1163 i2d_OCSP_RESPONSE_bio(cbio, resp);
1164 (void)BIO_flush(cbio);
1168 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *path,
1169 const STACK_OF(CONF_VALUE) *headers,
1170 OCSP_REQUEST *req, int req_timeout)
1175 OCSP_REQ_CTX *ctx = NULL;
1176 OCSP_RESPONSE *rsp = NULL;
1180 if (req_timeout != -1)
1181 BIO_set_nbio(cbio, 1);
1183 rv = BIO_do_connect(cbio);
1185 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1186 BIO_puts(bio_err, "Error connecting BIO\n");
1190 if (BIO_get_fd(cbio, &fd) <= 0) {
1191 BIO_puts(bio_err, "Can't get connection fd\n");
1195 if (req_timeout != -1 && rv <= 0) {
1197 openssl_fdset(fd, &confds);
1199 tv.tv_sec = req_timeout;
1200 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1202 BIO_puts(bio_err, "Timeout on connect\n");
1207 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1211 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1212 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1213 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1217 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1221 rv = OCSP_sendreq_nbio(&rsp, ctx);
1224 if (req_timeout == -1)
1227 openssl_fdset(fd, &confds);
1229 tv.tv_sec = req_timeout;
1230 if (BIO_should_read(cbio))
1231 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1232 else if (BIO_should_write(cbio))
1233 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1235 BIO_puts(bio_err, "Unexpected retry condition\n");
1239 BIO_puts(bio_err, "Timeout on request\n");
1243 BIO_puts(bio_err, "Select error\n");
1249 OCSP_REQ_CTX_free(ctx);
1254 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1255 const char *host, const char *path,
1256 const char *port, int use_ssl,
1257 const STACK_OF(CONF_VALUE) *headers,
1261 SSL_CTX *ctx = NULL;
1262 OCSP_RESPONSE *resp = NULL;
1263 cbio = BIO_new_connect(host);
1265 BIO_printf(bio_err, "Error creating connect BIO\n");
1269 BIO_set_conn_port(cbio, port);
1272 ctx = SSL_CTX_new(TLS_client_method());
1274 BIO_printf(bio_err, "Error creating SSL context.\n");
1277 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1278 sbio = BIO_new_ssl(ctx, 1);
1279 cbio = BIO_push(sbio, cbio);
1281 resp = query_responder(cbio, path, headers, req, req_timeout);
1283 BIO_printf(bio_err, "Error querying OCSP responder\n");