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 *host,
123 const STACK_OF(CONF_VALUE) *headers,
124 OCSP_REQUEST *req, int req_timeout);
126 typedef enum OPTION_choice {
127 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
128 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
129 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
130 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
131 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
132 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
133 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
134 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
135 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH,
136 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
137 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
138 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
139 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
144 OPTIONS ocsp_options[] = {
145 {"help", OPT_HELP, '-', "Display this summary"},
146 {"out", OPT_OUTFILE, '>', "Output filename"},
147 {"timeout", OPT_TIMEOUT, 'p',
148 "Connection timeout (in seconds) to the OCSP responder"},
149 {"url", OPT_URL, 's', "Responder URL"},
150 {"host", OPT_HOST, 's', "host:prot top to connect to"},
151 {"port", OPT_PORT, 'p', "Port to run responder on"},
152 {"ignore_err", OPT_IGNORE_ERR, '-'},
153 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
154 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
155 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
156 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
157 "Don't include any certificates in response"},
158 {"resp_key_id", OPT_RESP_KEY_ID, '-',
159 "Identify reponse by signing certificate key ID"},
160 {"no_certs", OPT_NO_CERTS, '-',
161 "Don't include any certificates in signed request"},
162 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
163 "Don't check signature on response"},
164 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
165 "Don't check signing certificate"},
166 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
167 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
168 "Don't do additional checks on signing certificate"},
169 {"no_explicit", OPT_NO_EXPLICIT, '-'},
170 {"trust_other", OPT_TRUST_OTHER, '-',
171 "Don't verify additional certificates"},
172 {"no_intern", OPT_NO_INTERN, '-',
173 "Don't search certificates contained in response for signer"},
174 {"badsig", OPT_BADSIG, '-'},
175 {"text", OPT_TEXT, '-', "Print text form of request and response"},
176 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
177 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
178 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
179 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
180 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
181 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
182 {"sign_other", OPT_SIGN_OTHER, '<',
183 "Additional certificates to include in signed request"},
184 {"verify_other", OPT_VERIFY_OTHER, '<',
185 "Additional certificates to search for signer"},
186 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
187 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
188 {"no-CAfile", OPT_NOCAFILE, '-',
189 "Do not load the default certificates file"},
190 {"no-CApath", OPT_NOCAPATH, '-',
191 "Do not load certificates from the default certificates directory"},
192 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
193 "Maximum validity discrepancy in seconds"},
194 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
195 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
196 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
197 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
198 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
199 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
200 {"cert", OPT_CERT, '<', "Certificate to check"},
201 {"serial", OPT_SERIAL, 's', "Nerial number to check"},
202 {"index", OPT_INDEX, '<', "Certificate status index file"},
203 {"CA", OPT_CA, '<', "CA certificate"},
204 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
205 {"nrequest", OPT_REQUEST, 'p',
206 "Number of requests to accept (default unlimited)"},
207 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
208 {"rsigner", OPT_RSIGNER, '<',
209 "Sesponder certificate to sign responses with"},
210 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
211 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
212 {"rmd", OPT_RMD, 's'},
213 {"header", OPT_HEADER, 's', "key=value header to add"},
214 {"", OPT_MD, '-', "Any supported digest"},
219 int ocsp_main(int argc, char **argv)
221 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
222 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
224 EVP_PKEY *key = NULL, *rkey = NULL;
225 OCSP_BASICRESP *bs = NULL;
226 OCSP_REQUEST *req = NULL;
227 OCSP_RESPONSE *resp = NULL;
228 STACK_OF(CONF_VALUE) *headers = NULL;
229 STACK_OF(OCSP_CERTID) *ids = NULL;
230 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
231 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
232 STACK_OF(X509) *issuers = NULL;
233 X509 *issuer = NULL, *cert = NULL, *rca_cert = NULL;
234 X509 *signer = NULL, *rsigner = NULL;
235 X509_STORE *store = NULL;
236 X509_VERIFY_PARAM *vpm = NULL;
237 char *CAfile = NULL, *CApath = NULL, *header, *value;
238 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
239 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
240 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
241 char *rsignfile = NULL, *rkeyfile = NULL;
242 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
243 char *signfile = NULL, *keyfile = NULL;
244 char *thost = NULL, *tport = NULL, *tpath = NULL;
245 int noCAfile = 0, noCApath = 0;
246 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
247 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
248 int req_text = 0, resp_text = 0, req_timeout = -1, ret = 1;
249 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
250 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
254 reqnames = sk_OPENSSL_STRING_new_null();
257 ids = sk_OCSP_CERTID_new_null();
260 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
263 prog = opt_init(argc, argv, ocsp_options);
264 while ((o = opt_next()) != OPT_EOF) {
269 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
273 opt_help(ocsp_options);
279 req_timeout = atoi(opt_arg());
285 thost = tport = tpath = NULL;
286 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
287 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
312 case OPT_RESP_NO_CERTS:
313 rflags |= OCSP_NOCERTS;
315 case OPT_RESP_KEY_ID:
316 rflags |= OCSP_RESPID_KEY;
319 sign_flags |= OCSP_NOCERTS;
321 case OPT_NO_SIGNATURE_VERIFY:
322 verify_flags |= OCSP_NOSIGS;
324 case OPT_NO_CERT_VERIFY:
325 verify_flags |= OCSP_NOVERIFY;
328 verify_flags |= OCSP_NOCHAIN;
330 case OPT_NO_CERT_CHECKS:
331 verify_flags |= OCSP_NOCHECKS;
333 case OPT_NO_EXPLICIT:
334 verify_flags |= OCSP_NOEXPLICIT;
336 case OPT_TRUST_OTHER:
337 verify_flags |= OCSP_TRUSTOTHER;
340 verify_flags |= OCSP_NOINTERN;
346 req_text = resp_text = 1;
361 signfile = opt_arg();
364 verify_certfile = opt_arg();
365 verify_flags |= OCSP_TRUSTOTHER;
368 sign_certfile = opt_arg();
370 case OPT_VERIFY_OTHER:
371 verify_certfile = opt_arg();
386 if (!opt_verify(o, vpm))
390 case OPT_VALIDITY_PERIOD:
391 opt_long(opt_arg(), &nsec);
394 opt_long(opt_arg(), &maxage);
409 issuer = load_cert(opt_arg(), FORMAT_PEM,
410 NULL, NULL, "issuer certificate");
413 if (issuers == NULL) {
414 if ((issuers = sk_X509_new_null()) == NULL)
417 sk_X509_push(issuers, issuer);
421 cert = load_cert(opt_arg(), FORMAT_PEM,
422 NULL, NULL, "certificate");
425 if (cert_id_md == NULL)
426 cert_id_md = EVP_sha1();
427 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
429 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()))
441 ridx_filename = opt_arg();
444 rca_filename = opt_arg();
447 opt_int(opt_arg(), &nmin);
452 opt_int(opt_arg(), &accept_count);
455 ndays = atoi(opt_arg());
458 rsignfile = opt_arg();
461 rkeyfile = opt_arg();
464 rcertfile = opt_arg();
467 if (!opt_md(opt_arg(), &rsign_md))
472 value = strchr(header, '=');
474 BIO_printf(bio_err, "Missing = in header key=value\n");
478 if (!X509V3_add_value(header, value, &headers))
482 if (cert_id_md != NULL) {
484 "%s: Digest must be before -cert or -serial\n",
488 if (!opt_md(opt_unknown(), &cert_id_md))
493 argc = opt_num_rest();
497 /* Have we anything to do? */
498 if (!req && !reqin && !respin && !(port && ridx_filename))
501 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
505 if (!req && (add_nonce != 2))
509 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
512 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
515 BIO_printf(bio_err, "Error reading OCSP request\n");
521 acbio = init_responder(port);
528 rkeyfile = rsignfile;
529 rsigner = load_cert(rsignfile, FORMAT_PEM,
530 NULL, NULL, "responder certificate");
532 BIO_printf(bio_err, "Error loading responder certificate\n");
535 rca_cert = load_cert(rca_filename, FORMAT_PEM,
536 NULL, NULL, "CA certificate");
538 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL, NULL,
539 "responder other certificates"))
542 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
543 "responder private key");
548 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
553 if (!do_responder(&req, &cbio, acbio, port))
557 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
559 send_ocsp_response(cbio, resp);
564 if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
565 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
569 if (req && add_nonce)
570 OCSP_request_add1_nonce(req, NULL, -1);
575 signer = load_cert(signfile, FORMAT_PEM,
576 NULL, NULL, "signer certificate");
578 BIO_printf(bio_err, "Error loading signer certificate\n");
582 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL, NULL,
583 "signer certificates"))
586 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
587 "signer private key");
591 if (!OCSP_request_sign
592 (req, signer, key, NULL, sign_other, sign_flags)) {
593 BIO_printf(bio_err, "Error signing OCSP request\n");
599 OCSP_REQUEST_print(out, req, 0);
602 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
605 i2d_OCSP_REQUEST_bio(derbio, req);
609 if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
611 "Need a responder certificate, key and CA for this operation!\n");
615 if (ridx_filename && !rdb) {
616 rdb = load_index(ridx_filename, NULL);
619 if (!index_index(rdb))
624 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
625 rsign_md, rother, rflags, nmin, ndays, badsig);
627 send_ocsp_response(cbio, resp);
629 # ifndef OPENSSL_NO_SOCK
630 resp = process_responder(req, host, path,
631 port, use_ssl, headers, req_timeout);
636 "Error creating connect BIO - sockets not supported.\n");
640 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
643 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
646 BIO_printf(bio_err, "Error reading OCSP response\n");
657 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
660 i2d_OCSP_RESPONSE_bio(derbio, resp);
664 i = OCSP_response_status(resp);
665 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
666 BIO_printf(out, "Responder Error: %s (%d)\n",
667 OCSP_response_status_str(i), i);
675 OCSP_RESPONSE_print(out, resp, 0);
677 /* If running as responder don't verify our own response */
679 /* If not unlimited, see if we took all we should. */
680 if (accept_count != -1 && --accept_count <= 0) {
686 OCSP_REQUEST_free(req);
688 OCSP_RESPONSE_free(resp);
698 store = setup_verify(CAfile, CApath, noCAfile, noCApath);
703 X509_STORE_set1_param(store, vpm);
704 if (verify_certfile) {
705 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL, NULL,
706 "validator certificate"))
710 bs = OCSP_response_get1_basic(resp);
712 BIO_printf(bio_err, "Error parsing response\n");
719 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
721 BIO_printf(bio_err, "WARNING: no nonce in response\n");
723 BIO_printf(bio_err, "Nonce Verify error\n");
729 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
730 if (i <= 0 && issuers) {
731 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
736 BIO_printf(bio_err, "Response Verify Failure\n");
737 ERR_print_errors(bio_err);
740 BIO_printf(bio_err, "Response verify OK\n");
744 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
747 ERR_print_errors(bio_err);
749 X509_STORE_free(store);
750 X509_VERIFY_PARAM_free(vpm);
754 sk_X509_pop_free(issuers, X509_free);
761 OCSP_REQUEST_free(req);
762 OCSP_RESPONSE_free(resp);
763 OCSP_BASICRESP_free(bs);
764 sk_OPENSSL_STRING_free(reqnames);
765 sk_OCSP_CERTID_free(ids);
766 sk_X509_pop_free(sign_other, X509_free);
767 sk_X509_pop_free(verify_other, X509_free);
768 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
776 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
777 const EVP_MD *cert_id_md, X509 *issuer,
778 STACK_OF(OCSP_CERTID) *ids)
782 BIO_printf(bio_err, "No issuer certificate specified\n");
786 *req = OCSP_REQUEST_new();
789 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
790 if (!id || !sk_OCSP_CERTID_push(ids, id))
792 if (!OCSP_request_add0_id(*req, id))
797 BIO_printf(bio_err, "Error Creating OCSP request\n");
801 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
802 const EVP_MD *cert_id_md, X509 *issuer,
803 STACK_OF(OCSP_CERTID) *ids)
807 ASN1_BIT_STRING *ikey;
810 BIO_printf(bio_err, "No issuer certificate specified\n");
814 *req = OCSP_REQUEST_new();
817 iname = X509_get_subject_name(issuer);
818 ikey = X509_get0_pubkey_bitstr(issuer);
819 sno = s2i_ASN1_INTEGER(NULL, serial);
821 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
824 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
825 ASN1_INTEGER_free(sno);
826 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
828 if (!OCSP_request_add0_id(*req, id))
833 BIO_printf(bio_err, "Error Creating OCSP request\n");
837 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
838 STACK_OF(OPENSSL_STRING) *names,
839 STACK_OF(OCSP_CERTID) *ids, long nsec,
844 int i, status, reason;
845 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
847 if (!bs || !req || !sk_OPENSSL_STRING_num(names)
848 || !sk_OCSP_CERTID_num(ids))
851 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
852 id = sk_OCSP_CERTID_value(ids, i);
853 name = sk_OPENSSL_STRING_value(names, i);
854 BIO_printf(out, "%s: ", name);
856 if (!OCSP_resp_find_status(bs, id, &status, &reason,
857 &rev, &thisupd, &nextupd)) {
858 BIO_puts(out, "ERROR: No Status found.\n");
863 * Check validity: if invalid write to output BIO so we know which
864 * response this refers to.
866 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
867 BIO_puts(out, "WARNING: Status times invalid.\n");
868 ERR_print_errors(out);
870 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
872 BIO_puts(out, "\tThis Update: ");
873 ASN1_GENERALIZEDTIME_print(out, thisupd);
877 BIO_puts(out, "\tNext Update: ");
878 ASN1_GENERALIZEDTIME_print(out, nextupd);
882 if (status != V_OCSP_CERTSTATUS_REVOKED)
886 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
888 BIO_puts(out, "\tRevocation Time: ");
889 ASN1_GENERALIZEDTIME_print(out, rev);
894 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
895 CA_DB *db, X509 *ca, X509 *rcert,
896 EVP_PKEY *rkey, const EVP_MD *rmd,
897 STACK_OF(X509) *rother, unsigned long flags,
898 int nmin, int ndays, int badsig)
900 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
901 OCSP_CERTID *cid, *ca_id = NULL;
902 OCSP_BASICRESP *bs = NULL;
905 id_count = OCSP_request_onereq_count(req);
909 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
913 bs = OCSP_BASICRESP_new();
914 thisupd = X509_gmtime_adj(NULL, 0);
916 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
918 /* Examine each certificate id in the request */
919 for (i = 0; i < id_count; i++) {
921 ASN1_INTEGER *serial;
923 ASN1_OBJECT *cert_id_md_oid;
924 const EVP_MD *cert_id_md;
925 one = OCSP_request_onereq_get0(req, i);
926 cid = OCSP_onereq_get0_id(one);
928 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
930 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
932 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
936 OCSP_CERTID_free(ca_id);
937 ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
939 /* Is this request about our CA? */
940 if (OCSP_id_issuer_cmp(ca_id, cid)) {
941 OCSP_basic_add1_status(bs, cid,
942 V_OCSP_CERTSTATUS_UNKNOWN,
943 0, NULL, thisupd, nextupd);
946 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
947 inf = lookup_serial(db, serial);
949 OCSP_basic_add1_status(bs, cid,
950 V_OCSP_CERTSTATUS_UNKNOWN,
951 0, NULL, thisupd, nextupd);
952 else if (inf[DB_type][0] == DB_TYPE_VAL)
953 OCSP_basic_add1_status(bs, cid,
954 V_OCSP_CERTSTATUS_GOOD,
955 0, NULL, thisupd, nextupd);
956 else if (inf[DB_type][0] == DB_TYPE_REV) {
957 ASN1_OBJECT *inst = NULL;
958 ASN1_TIME *revtm = NULL;
959 ASN1_GENERALIZEDTIME *invtm = NULL;
960 OCSP_SINGLERESP *single;
962 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
963 single = OCSP_basic_add1_status(bs, cid,
964 V_OCSP_CERTSTATUS_REVOKED,
965 reason, revtm, thisupd, nextupd);
967 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
970 OCSP_SINGLERESP_add1_ext_i2d(single,
971 NID_hold_instruction_code, inst,
973 ASN1_OBJECT_free(inst);
974 ASN1_TIME_free(revtm);
975 ASN1_GENERALIZEDTIME_free(invtm);
979 OCSP_copy_nonce(bs, req);
981 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
984 ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
985 unsigned char *sigptr = ASN1_STRING_data(sig);
986 sigptr[ASN1_STRING_length(sig) - 1] ^= 0x1;
989 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
992 ASN1_TIME_free(thisupd);
993 ASN1_TIME_free(nextupd);
994 OCSP_CERTID_free(ca_id);
995 OCSP_BASICRESP_free(bs);
998 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
1002 char *itmp, *row[DB_NUMBER], **rrow;
1003 for (i = 0; i < DB_NUMBER; i++)
1005 bn = ASN1_INTEGER_to_BN(ser, NULL);
1006 OPENSSL_assert(bn); /* FIXME: should report an error at this
1007 * point and abort */
1009 itmp = OPENSSL_strdup("00");
1011 itmp = BN_bn2hex(bn);
1012 row[DB_serial] = itmp;
1014 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1019 /* Quick and dirty OCSP server: read in and parse input request */
1021 static BIO *init_responder(const char *port)
1023 BIO *acbio = NULL, *bufbio = NULL;
1025 # ifdef OPENSSL_NO_SOCK
1027 "Error setting up accept BIO - sockets not supported.\n");
1030 bufbio = BIO_new(BIO_f_buffer());
1033 acbio = BIO_new(BIO_s_accept());
1035 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1036 || BIO_set_accept_port(acbio, port) < 0) {
1037 BIO_printf(bio_err, "Error setting up accept BIO\n");
1038 ERR_print_errors(bio_err);
1042 BIO_set_accept_bios(acbio, bufbio);
1044 if (BIO_do_accept(acbio) <= 0) {
1045 BIO_printf(bio_err, "Error starting accept\n");
1046 ERR_print_errors(bio_err);
1053 BIO_free_all(acbio);
1060 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1062 static int urldecode(char *p)
1064 unsigned char *out = (unsigned char *)p;
1065 unsigned char *save = out;
1070 else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
1071 *out++ = (app_hex(p[1]) << 4) | app_hex(p[2]);
1078 return (int)(out - save);
1081 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1085 OCSP_REQUEST *req = NULL;
1086 char inbuf[2048], reqbuf[2048];
1088 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
1090 if (BIO_do_accept(acbio) <= 0) {
1091 BIO_printf(bio_err, "Error accepting connection\n");
1092 ERR_print_errors(bio_err);
1096 cbio = BIO_pop(acbio);
1099 /* Read the request line. */
1100 len = BIO_gets(cbio, reqbuf, sizeof reqbuf);
1103 if (strncmp(reqbuf, "GET ", 4) == 0) {
1104 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
1105 for (p = reqbuf + 4; *p == ' '; ++p)
1108 BIO_printf(bio_err, "Invalid request -- bad URL\n");
1113 /* Splice off the HTTP version identifier. */
1114 for (q = p; *q; q++)
1117 if (strncmp(q, " HTTP/1.", 8) != 0) {
1118 BIO_printf(bio_err, "Invalid request -- bad HTTP vesion\n");
1124 BIO_printf(bio_err, "Invalid request -- bad URL encoding\n");
1127 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1128 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
1129 BIO_printf(bio_err, "Could not allocate memory\n");
1130 ERR_print_errors(bio_err);
1133 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1134 getbio = BIO_push(b64, getbio);
1135 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
1136 BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n");
1140 /* Read and skip past the headers. */
1142 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1145 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1149 /* Try to read OCSP request */
1151 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1152 BIO_free_all(getbio);
1154 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1157 BIO_printf(bio_err, "Error parsing OCSP request\n");
1158 ERR_print_errors(bio_err);
1167 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1170 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1171 "Content-Length: %d\r\n\r\n";
1174 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1175 i2d_OCSP_RESPONSE_bio(cbio, resp);
1176 (void)BIO_flush(cbio);
1180 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
1182 const STACK_OF(CONF_VALUE) *headers,
1183 OCSP_REQUEST *req, int req_timeout)
1189 OCSP_REQ_CTX *ctx = NULL;
1190 OCSP_RESPONSE *rsp = NULL;
1194 if (req_timeout != -1)
1195 BIO_set_nbio(cbio, 1);
1197 rv = BIO_do_connect(cbio);
1199 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1200 BIO_puts(bio_err, "Error connecting BIO\n");
1204 if (BIO_get_fd(cbio, &fd) < 0) {
1205 BIO_puts(bio_err, "Can't get connection fd\n");
1209 if (req_timeout != -1 && rv <= 0) {
1211 openssl_fdset(fd, &confds);
1213 tv.tv_sec = req_timeout;
1214 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1216 BIO_puts(bio_err, "Timeout on connect\n");
1221 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1225 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1226 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1227 if (add_host == 1 && strcasecmp("host", hdr->name) == 0)
1229 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1233 if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0)
1236 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1240 rv = OCSP_sendreq_nbio(&rsp, ctx);
1243 if (req_timeout == -1)
1246 openssl_fdset(fd, &confds);
1248 tv.tv_sec = req_timeout;
1249 if (BIO_should_read(cbio))
1250 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1251 else if (BIO_should_write(cbio))
1252 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1254 BIO_puts(bio_err, "Unexpected retry condition\n");
1258 BIO_puts(bio_err, "Timeout on request\n");
1262 BIO_puts(bio_err, "Select error\n");
1268 OCSP_REQ_CTX_free(ctx);
1273 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1274 const char *host, const char *path,
1275 const char *port, int use_ssl,
1276 STACK_OF(CONF_VALUE) *headers,
1280 SSL_CTX *ctx = NULL;
1281 OCSP_RESPONSE *resp = NULL;
1283 cbio = BIO_new_connect(host);
1285 BIO_printf(bio_err, "Error creating connect BIO\n");
1289 BIO_set_conn_port(cbio, port);
1292 ctx = SSL_CTX_new(TLS_client_method());
1294 BIO_printf(bio_err, "Error creating SSL context.\n");
1297 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1298 sbio = BIO_new_ssl(ctx, 1);
1299 cbio = BIO_push(sbio, cbio);
1302 resp = query_responder(cbio, host, path, headers, req, req_timeout);
1304 BIO_printf(bio_err, "Error querying OCSP responder\n");