2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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_NO_OCSP
13 NON_EMPTY_TRANSLATION_UNIT
15 # ifdef OPENSSL_SYS_VMS
16 # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
28 /* Needs to be included before the openssl headers */
30 # include <openssl/e_os2.h>
31 # include <openssl/crypto.h>
32 # include <openssl/err.h>
33 # include <openssl/ssl.h>
34 # include <openssl/evp.h>
35 # include <openssl/bn.h>
36 # include <openssl/x509v3.h>
38 # if defined(NETWARE_CLIB)
39 # ifdef NETWARE_BSDSOCK
40 # include <sys/socket.h>
41 # include <sys/bsdskt.h>
43 # include <novsock2.h>
45 # elif defined(NETWARE_LIBC)
46 # ifdef NETWARE_BSDSOCK
47 # include <sys/select.h>
49 # include <novsock2.h>
53 /* Maximum leeway in validity period: default 5 minutes */
54 # define MAX_VALIDITY_PERIOD (5 * 60)
56 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
57 const EVP_MD *cert_id_md, X509 *issuer,
58 STACK_OF(OCSP_CERTID) *ids);
59 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
60 const EVP_MD *cert_id_md, X509 *issuer,
61 STACK_OF(OCSP_CERTID) *ids);
62 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
63 STACK_OF(OPENSSL_STRING) *names,
64 STACK_OF(OCSP_CERTID) *ids, long nsec,
66 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
67 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
68 EVP_PKEY *rkey, const EVP_MD *md,
69 STACK_OF(X509) *rother, unsigned long flags,
70 int nmin, int ndays, int badsig);
72 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
73 static BIO *init_responder(const char *port);
74 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio);
75 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
77 # ifndef OPENSSL_NO_SOCK
78 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
80 const STACK_OF(CONF_VALUE) *headers,
81 OCSP_REQUEST *req, int req_timeout);
84 typedef enum OPTION_choice {
85 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
86 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
87 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
88 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
89 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
90 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
91 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
92 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
93 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH,
94 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
95 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
96 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
97 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
102 const OPTIONS ocsp_options[] = {
103 {"help", OPT_HELP, '-', "Display this summary"},
104 {"out", OPT_OUTFILE, '>', "Output filename"},
105 {"timeout", OPT_TIMEOUT, 'p',
106 "Connection timeout (in seconds) to the OCSP responder"},
107 {"url", OPT_URL, 's', "Responder URL"},
108 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
109 {"port", OPT_PORT, 'p', "Port to run responder on"},
110 {"ignore_err", OPT_IGNORE_ERR, '-',
111 "Ignore Error response from OCSP responder, and retry "},
112 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
113 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
114 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
115 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
116 "Don't include any certificates in response"},
117 {"resp_key_id", OPT_RESP_KEY_ID, '-',
118 "Identify response by signing certificate key ID"},
119 {"no_certs", OPT_NO_CERTS, '-',
120 "Don't include any certificates in signed request"},
121 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
122 "Don't check signature on response"},
123 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
124 "Don't check signing certificate"},
125 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
126 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
127 "Don't do additional checks on signing certificate"},
128 {"no_explicit", OPT_NO_EXPLICIT, '-',
129 "Do not explicitly check the chain, just verify the root"},
130 {"trust_other", OPT_TRUST_OTHER, '-',
131 "Don't verify additional certificates"},
132 {"no_intern", OPT_NO_INTERN, '-',
133 "Don't search certificates contained in response for signer"},
134 {"badsig", OPT_BADSIG, '-',
135 "Corrupt last byte of loaded OSCP response signature (for test)"},
136 {"text", OPT_TEXT, '-', "Print text form of request and response"},
137 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
138 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
139 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
140 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
141 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
142 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
143 {"sign_other", OPT_SIGN_OTHER, '<',
144 "Additional certificates to include in signed request"},
145 {"verify_other", OPT_VERIFY_OTHER, '<',
146 "Additional certificates to search for signer"},
147 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
148 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
149 {"no-CAfile", OPT_NOCAFILE, '-',
150 "Do not load the default certificates file"},
151 {"no-CApath", OPT_NOCAPATH, '-',
152 "Do not load certificates from the default certificates directory"},
153 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
154 "Maximum validity discrepancy in seconds"},
155 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
156 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
157 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
158 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
159 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
160 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
161 {"cert", OPT_CERT, '<', "Certificate to check"},
162 {"serial", OPT_SERIAL, 's', "Serial number to check"},
163 {"index", OPT_INDEX, '<', "Certificate status index file"},
164 {"CA", OPT_CA, '<', "CA certificate"},
165 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
166 {"nrequest", OPT_REQUEST, 'p',
167 "Number of requests to accept (default unlimited)"},
168 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
169 {"rsigner", OPT_RSIGNER, '<',
170 "Responder certificate to sign responses with"},
171 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
172 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
173 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
174 {"header", OPT_HEADER, 's', "key=value header to add"},
175 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
180 int ocsp_main(int argc, char **argv)
182 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
183 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
186 EVP_PKEY *key = NULL, *rkey = NULL;
187 OCSP_BASICRESP *bs = NULL;
188 OCSP_REQUEST *req = NULL;
189 OCSP_RESPONSE *resp = NULL;
190 STACK_OF(CONF_VALUE) *headers = NULL;
191 STACK_OF(OCSP_CERTID) *ids = NULL;
192 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
193 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
194 STACK_OF(X509) *issuers = NULL;
195 X509 *issuer = NULL, *cert = NULL;
196 STACK_OF(X509) *rca_cert = NULL;
197 X509 *signer = NULL, *rsigner = NULL;
198 X509_STORE *store = NULL;
199 X509_VERIFY_PARAM *vpm = NULL;
200 const char *CAfile = NULL, *CApath = NULL;
201 char *header, *value;
202 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
203 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
204 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
205 char *rsignfile = NULL, *rkeyfile = NULL;
206 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
207 char *signfile = NULL, *keyfile = NULL;
208 char *thost = NULL, *tport = NULL, *tpath = NULL;
209 int noCAfile = 0, noCApath = 0;
210 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
211 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
212 int req_text = 0, resp_text = 0, ret = 1;
213 #ifndef OPENSSL_NO_SOCK
214 int req_timeout = -1;
216 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
217 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
221 reqnames = sk_OPENSSL_STRING_new_null();
224 ids = sk_OCSP_CERTID_new_null();
227 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
230 prog = opt_init(argc, argv, ocsp_options);
231 while ((o = opt_next()) != OPT_EOF) {
236 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
240 opt_help(ocsp_options);
246 #ifndef OPENSSL_NO_SOCK
247 req_timeout = atoi(opt_arg());
254 thost = tport = tpath = NULL;
255 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
256 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
281 case OPT_RESP_NO_CERTS:
282 rflags |= OCSP_NOCERTS;
284 case OPT_RESP_KEY_ID:
285 rflags |= OCSP_RESPID_KEY;
288 sign_flags |= OCSP_NOCERTS;
290 case OPT_NO_SIGNATURE_VERIFY:
291 verify_flags |= OCSP_NOSIGS;
293 case OPT_NO_CERT_VERIFY:
294 verify_flags |= OCSP_NOVERIFY;
297 verify_flags |= OCSP_NOCHAIN;
299 case OPT_NO_CERT_CHECKS:
300 verify_flags |= OCSP_NOCHECKS;
302 case OPT_NO_EXPLICIT:
303 verify_flags |= OCSP_NOEXPLICIT;
305 case OPT_TRUST_OTHER:
306 verify_flags |= OCSP_TRUSTOTHER;
309 verify_flags |= OCSP_NOINTERN;
315 req_text = resp_text = 1;
330 signfile = opt_arg();
333 verify_certfile = opt_arg();
334 verify_flags |= OCSP_TRUSTOTHER;
337 sign_certfile = opt_arg();
339 case OPT_VERIFY_OTHER:
340 verify_certfile = opt_arg();
355 if (!opt_verify(o, vpm))
359 case OPT_VALIDITY_PERIOD:
360 opt_long(opt_arg(), &nsec);
363 opt_long(opt_arg(), &maxage);
378 issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate");
381 if (issuers == NULL) {
382 if ((issuers = sk_X509_new_null()) == NULL)
385 sk_X509_push(issuers, issuer);
389 cert = load_cert(opt_arg(), FORMAT_PEM, "certificate");
392 if (cert_id_md == NULL)
393 cert_id_md = EVP_sha1();
394 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
396 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
401 if (cert_id_md == NULL)
402 cert_id_md = EVP_sha1();
403 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
405 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
410 ridx_filename = opt_arg();
413 rca_filename = opt_arg();
416 opt_int(opt_arg(), &nmin);
421 opt_int(opt_arg(), &accept_count);
424 ndays = atoi(opt_arg());
427 rsignfile = opt_arg();
430 rkeyfile = opt_arg();
433 rcertfile = opt_arg();
435 case OPT_RMD: /* Response MessageDigest */
436 if (!opt_md(opt_arg(), &rsign_md))
441 value = strchr(header, '=');
443 BIO_printf(bio_err, "Missing = in header key=value\n");
447 if (!X509V3_add_value(header, value, &headers))
453 "%s: Digest must be before -cert or -serial\n",
457 if (!opt_md(opt_unknown(), &cert_id_md))
465 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
469 argc = opt_num_rest();
473 /* Have we anything to do? */
474 if (!req && !reqin && !respin && !(port && ridx_filename))
477 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
481 if (!req && (add_nonce != 2))
485 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
488 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
491 BIO_printf(bio_err, "Error reading OCSP request\n");
497 acbio = init_responder(port);
504 rkeyfile = rsignfile;
505 rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate");
507 BIO_printf(bio_err, "Error loading responder certificate\n");
510 if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM,
511 NULL, "CA certificate"))
514 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
515 "responder other certificates"))
518 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
519 "responder private key");
524 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
529 if (!do_responder(&req, &cbio, acbio))
533 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
535 send_ocsp_response(cbio, resp);
540 if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
541 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
545 if (req && add_nonce)
546 OCSP_request_add1_nonce(req, NULL, -1);
551 signer = load_cert(signfile, FORMAT_PEM, "signer certificate");
553 BIO_printf(bio_err, "Error loading signer certificate\n");
557 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL,
558 "signer certificates"))
561 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
562 "signer private key");
566 if (!OCSP_request_sign
567 (req, signer, key, NULL, sign_other, sign_flags)) {
568 BIO_printf(bio_err, "Error signing OCSP request\n");
574 OCSP_REQUEST_print(out, req, 0);
577 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
580 i2d_OCSP_REQUEST_bio(derbio, req);
584 if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
586 "Need a responder certificate, key and CA for this operation!\n");
590 if (ridx_filename && !rdb) {
591 rdb = load_index(ridx_filename, NULL);
594 if (!index_index(rdb))
599 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
600 rsign_md, rother, rflags, nmin, ndays, badsig);
602 send_ocsp_response(cbio, resp);
604 # ifndef OPENSSL_NO_SOCK
605 resp = process_responder(req, host, path,
606 port, use_ssl, headers, req_timeout);
611 "Error creating connect BIO - sockets not supported.\n");
615 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
618 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
621 BIO_printf(bio_err, "Error reading OCSP response\n");
632 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
635 i2d_OCSP_RESPONSE_bio(derbio, resp);
639 i = OCSP_response_status(resp);
640 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
641 BIO_printf(out, "Responder Error: %s (%d)\n",
642 OCSP_response_status_str(i), i);
650 OCSP_RESPONSE_print(out, resp, 0);
652 /* If running as responder don't verify our own response */
654 /* If not unlimited, see if we took all we should. */
655 if (accept_count != -1 && --accept_count <= 0) {
661 OCSP_REQUEST_free(req);
663 OCSP_RESPONSE_free(resp);
673 store = setup_verify(CAfile, CApath, noCAfile, noCApath);
678 X509_STORE_set1_param(store, vpm);
679 if (verify_certfile) {
680 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL,
681 "validator certificate"))
685 bs = OCSP_response_get1_basic(resp);
687 BIO_printf(bio_err, "Error parsing response\n");
694 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
696 BIO_printf(bio_err, "WARNING: no nonce in response\n");
698 BIO_printf(bio_err, "Nonce Verify error\n");
704 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
705 if (i <= 0 && issuers) {
706 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
711 BIO_printf(bio_err, "Response Verify Failure\n");
712 ERR_print_errors(bio_err);
715 BIO_printf(bio_err, "Response verify OK\n");
719 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
722 ERR_print_errors(bio_err);
724 X509_STORE_free(store);
725 X509_VERIFY_PARAM_free(vpm);
729 sk_X509_pop_free(issuers, X509_free);
731 sk_X509_pop_free(rca_cert, X509_free);
736 OCSP_REQUEST_free(req);
737 OCSP_RESPONSE_free(resp);
738 OCSP_BASICRESP_free(bs);
739 sk_OPENSSL_STRING_free(reqnames);
740 sk_OCSP_CERTID_free(ids);
741 sk_X509_pop_free(sign_other, X509_free);
742 sk_X509_pop_free(verify_other, X509_free);
743 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 == NULL || !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, STACK_OF(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;
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_time_adj_ex(NULL, ndays, nmin * 60, NULL);
893 /* Examine each certificate id in the request */
894 for (i = 0; i < id_count; i++) {
896 ASN1_INTEGER *serial;
900 ASN1_OBJECT *cert_id_md_oid;
901 const EVP_MD *cert_id_md;
902 one = OCSP_request_onereq_get0(req, i);
903 cid = OCSP_onereq_get0_id(one);
905 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
907 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
909 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
913 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
914 X509 *ca_cert = sk_X509_value(ca, jj);
915 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
917 if (OCSP_id_issuer_cmp(ca_id, cid) == 0)
920 OCSP_CERTID_free(ca_id);
924 OCSP_basic_add1_status(bs, cid,
925 V_OCSP_CERTSTATUS_UNKNOWN,
926 0, NULL, thisupd, nextupd);
929 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
930 inf = lookup_serial(db, serial);
932 OCSP_basic_add1_status(bs, cid,
933 V_OCSP_CERTSTATUS_UNKNOWN,
934 0, NULL, thisupd, nextupd);
935 else if (inf[DB_type][0] == DB_TYPE_VAL)
936 OCSP_basic_add1_status(bs, cid,
937 V_OCSP_CERTSTATUS_GOOD,
938 0, NULL, thisupd, nextupd);
939 else if (inf[DB_type][0] == DB_TYPE_REV) {
940 ASN1_OBJECT *inst = NULL;
941 ASN1_TIME *revtm = NULL;
942 ASN1_GENERALIZEDTIME *invtm = NULL;
943 OCSP_SINGLERESP *single;
945 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
946 single = OCSP_basic_add1_status(bs, cid,
947 V_OCSP_CERTSTATUS_REVOKED,
948 reason, revtm, thisupd, nextupd);
950 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
953 OCSP_SINGLERESP_add1_ext_i2d(single,
954 NID_hold_instruction_code, inst,
956 ASN1_OBJECT_free(inst);
957 ASN1_TIME_free(revtm);
958 ASN1_GENERALIZEDTIME_free(invtm);
962 OCSP_copy_nonce(bs, req);
964 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
967 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
968 corrupt_signature(sig);
971 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
974 ASN1_TIME_free(thisupd);
975 ASN1_TIME_free(nextupd);
976 OCSP_BASICRESP_free(bs);
979 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
983 char *itmp, *row[DB_NUMBER], **rrow;
984 for (i = 0; i < DB_NUMBER; i++)
986 bn = ASN1_INTEGER_to_BN(ser, NULL);
987 OPENSSL_assert(bn); /* FIXME: should report an error at this
990 itmp = OPENSSL_strdup("00");
992 itmp = BN_bn2hex(bn);
993 row[DB_serial] = itmp;
995 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1000 /* Quick and dirty OCSP server: read in and parse input request */
1002 static BIO *init_responder(const char *port)
1004 # ifdef OPENSSL_NO_SOCK
1006 "Error setting up accept BIO - sockets not supported.\n");
1009 BIO *acbio = NULL, *bufbio = NULL;
1011 bufbio = BIO_new(BIO_f_buffer());
1014 acbio = BIO_new(BIO_s_accept());
1016 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1017 || BIO_set_accept_port(acbio, port) < 0) {
1018 BIO_printf(bio_err, "Error setting up accept BIO\n");
1019 ERR_print_errors(bio_err);
1023 BIO_set_accept_bios(acbio, bufbio);
1025 if (BIO_do_accept(acbio) <= 0) {
1026 BIO_printf(bio_err, "Error starting accept\n");
1027 ERR_print_errors(bio_err);
1034 BIO_free_all(acbio);
1040 # ifndef OPENSSL_NO_SOCK
1042 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1044 static int urldecode(char *p)
1046 unsigned char *out = (unsigned char *)p;
1047 unsigned char *save = out;
1052 else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
1053 /* Don't check, can't fail because of ixdigit() call. */
1054 *out++ = (OPENSSL_hexchar2int(p[1]) << 4)
1055 | OPENSSL_hexchar2int(p[2]);
1062 return (int)(out - save);
1066 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio)
1068 # ifdef OPENSSL_NO_SOCK
1072 OCSP_REQUEST *req = NULL;
1073 char inbuf[2048], reqbuf[2048];
1075 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
1077 if (BIO_do_accept(acbio) <= 0) {
1078 BIO_printf(bio_err, "Error accepting connection\n");
1079 ERR_print_errors(bio_err);
1083 cbio = BIO_pop(acbio);
1086 /* Read the request line. */
1087 len = BIO_gets(cbio, reqbuf, sizeof reqbuf);
1090 if (strncmp(reqbuf, "GET ", 4) == 0) {
1091 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
1092 for (p = reqbuf + 4; *p == ' '; ++p)
1095 BIO_printf(bio_err, "Invalid request -- bad URL\n");
1100 /* Splice off the HTTP version identifier. */
1101 for (q = p; *q; q++)
1104 if (strncmp(q, " HTTP/1.", 8) != 0) {
1105 BIO_printf(bio_err, "Invalid request -- bad HTTP vesion\n");
1111 BIO_printf(bio_err, "Invalid request -- bad URL encoding\n");
1114 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1115 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
1116 BIO_printf(bio_err, "Could not allocate memory\n");
1117 ERR_print_errors(bio_err);
1120 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1121 getbio = BIO_push(b64, getbio);
1122 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
1123 BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n");
1127 /* Read and skip past the headers. */
1129 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1132 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1136 /* Try to read OCSP request */
1138 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1139 BIO_free_all(getbio);
1141 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1144 BIO_printf(bio_err, "Error parsing OCSP request\n");
1145 ERR_print_errors(bio_err);
1154 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1157 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1158 "Content-Length: %d\r\n\r\n";
1161 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1162 i2d_OCSP_RESPONSE_bio(cbio, resp);
1163 (void)BIO_flush(cbio);
1167 # ifndef OPENSSL_NO_SOCK
1168 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
1170 const STACK_OF(CONF_VALUE) *headers,
1171 OCSP_REQUEST *req, int req_timeout)
1177 OCSP_REQ_CTX *ctx = NULL;
1178 OCSP_RESPONSE *rsp = NULL;
1182 if (req_timeout != -1)
1183 BIO_set_nbio(cbio, 1);
1185 rv = BIO_do_connect(cbio);
1187 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1188 BIO_puts(bio_err, "Error connecting BIO\n");
1192 if (BIO_get_fd(cbio, &fd) < 0) {
1193 BIO_puts(bio_err, "Can't get connection fd\n");
1197 if (req_timeout != -1 && rv <= 0) {
1199 openssl_fdset(fd, &confds);
1201 tv.tv_sec = req_timeout;
1202 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1204 BIO_puts(bio_err, "Timeout on connect\n");
1209 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1213 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1214 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1215 if (add_host == 1 && strcasecmp("host", hdr->name) == 0)
1217 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1221 if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0)
1224 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1228 rv = OCSP_sendreq_nbio(&rsp, ctx);
1231 if (req_timeout == -1)
1234 openssl_fdset(fd, &confds);
1236 tv.tv_sec = req_timeout;
1237 if (BIO_should_read(cbio))
1238 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1239 else if (BIO_should_write(cbio))
1240 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1242 BIO_puts(bio_err, "Unexpected retry condition\n");
1246 BIO_puts(bio_err, "Timeout on request\n");
1250 BIO_puts(bio_err, "Select error\n");
1256 OCSP_REQ_CTX_free(ctx);
1261 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1262 const char *host, const char *path,
1263 const char *port, int use_ssl,
1264 STACK_OF(CONF_VALUE) *headers,
1268 SSL_CTX *ctx = NULL;
1269 OCSP_RESPONSE *resp = NULL;
1271 cbio = BIO_new_connect(host);
1273 BIO_printf(bio_err, "Error creating connect BIO\n");
1277 BIO_set_conn_port(cbio, port);
1280 ctx = SSL_CTX_new(TLS_client_method());
1282 BIO_printf(bio_err, "Error creating SSL context.\n");
1285 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1286 sbio = BIO_new_ssl(ctx, 1);
1287 cbio = BIO_push(sbio, cbio);
1290 resp = query_responder(cbio, host, path, headers, req, req_timeout);
1292 BIO_printf(bio_err, "Error querying OCSP responder\n");