2 * Copyright 2001-2017 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
26 /* Needs to be included before the openssl headers */
28 # include <openssl/e_os2.h>
29 # include <openssl/crypto.h>
30 # include <openssl/err.h>
31 # include <openssl/ssl.h>
32 # include <openssl/evp.h>
33 # include <openssl/bn.h>
34 # include <openssl/x509v3.h>
36 # if defined(NETWARE_CLIB)
37 # ifdef NETWARE_BSDSOCK
38 # include <sys/socket.h>
39 # include <sys/bsdskt.h>
41 # include <novsock2.h>
43 # elif defined(NETWARE_LIBC)
44 # ifdef NETWARE_BSDSOCK
45 # include <sys/select.h>
47 # include <novsock2.h>
51 /* Maximum leeway in validity period: default 5 minutes */
52 # define MAX_VALIDITY_PERIOD (5 * 60)
54 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
55 const EVP_MD *cert_id_md, X509 *issuer,
56 STACK_OF(OCSP_CERTID) *ids);
57 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
58 const EVP_MD *cert_id_md, X509 *issuer,
59 STACK_OF(OCSP_CERTID) *ids);
60 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
61 STACK_OF(OPENSSL_STRING) *names,
62 STACK_OF(OCSP_CERTID) *ids, long nsec,
64 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
65 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
66 EVP_PKEY *rkey, const EVP_MD *md,
67 STACK_OF(X509) *rother, unsigned long flags,
68 int nmin, int ndays, int badsig);
70 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
71 static BIO *init_responder(const char *port);
72 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio);
73 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
75 # ifndef OPENSSL_NO_SOCK
76 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
78 const STACK_OF(CONF_VALUE) *headers,
79 OCSP_REQUEST *req, int req_timeout);
82 typedef enum OPTION_choice {
83 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
84 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
85 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
86 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
87 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
88 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
89 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
90 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
91 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH,
92 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
93 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
94 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
95 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
100 const OPTIONS ocsp_options[] = {
101 {"help", OPT_HELP, '-', "Display this summary"},
102 {"out", OPT_OUTFILE, '>', "Output filename"},
103 {"timeout", OPT_TIMEOUT, 'p',
104 "Connection timeout (in seconds) to the OCSP responder"},
105 {"url", OPT_URL, 's', "Responder URL"},
106 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
107 {"port", OPT_PORT, 'p', "Port to run responder on"},
108 {"ignore_err", OPT_IGNORE_ERR, '-',
109 "Ignore error on OCSP request or response and continue running"},
110 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
111 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
112 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
113 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
114 "Don't include any certificates in response"},
115 {"resp_key_id", OPT_RESP_KEY_ID, '-',
116 "Identify response by signing certificate key ID"},
117 {"no_certs", OPT_NO_CERTS, '-',
118 "Don't include any certificates in signed request"},
119 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
120 "Don't check signature on response"},
121 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
122 "Don't check signing certificate"},
123 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
124 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
125 "Don't do additional checks on signing certificate"},
126 {"no_explicit", OPT_NO_EXPLICIT, '-',
127 "Do not explicitly check the chain, just verify the root"},
128 {"trust_other", OPT_TRUST_OTHER, '-',
129 "Don't verify additional certificates"},
130 {"no_intern", OPT_NO_INTERN, '-',
131 "Don't search certificates contained in response for signer"},
132 {"badsig", OPT_BADSIG, '-',
133 "Corrupt last byte of loaded OSCP response signature (for test)"},
134 {"text", OPT_TEXT, '-', "Print text form of request and response"},
135 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
136 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
137 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
138 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
139 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
140 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
141 {"sign_other", OPT_SIGN_OTHER, '<',
142 "Additional certificates to include in signed request"},
143 {"verify_other", OPT_VERIFY_OTHER, '<',
144 "Additional certificates to search for signer"},
145 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
146 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
147 {"no-CAfile", OPT_NOCAFILE, '-',
148 "Do not load the default certificates file"},
149 {"no-CApath", OPT_NOCAPATH, '-',
150 "Do not load certificates from the default certificates directory"},
151 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
152 "Maximum validity discrepancy in seconds"},
153 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
154 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
155 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
156 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
157 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
158 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
159 {"cert", OPT_CERT, '<', "Certificate to check"},
160 {"serial", OPT_SERIAL, 's', "Serial number to check"},
161 {"index", OPT_INDEX, '<', "Certificate status index file"},
162 {"CA", OPT_CA, '<', "CA certificate"},
163 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
164 {"nrequest", OPT_REQUEST, 'p',
165 "Number of requests to accept (default unlimited)"},
166 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
167 {"rsigner", OPT_RSIGNER, '<',
168 "Responder certificate to sign responses with"},
169 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
170 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
171 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
172 {"header", OPT_HEADER, 's', "key=value header to add"},
173 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
178 int ocsp_main(int argc, char **argv)
180 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
181 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
184 EVP_PKEY *key = NULL, *rkey = NULL;
185 OCSP_BASICRESP *bs = NULL;
186 OCSP_REQUEST *req = NULL;
187 OCSP_RESPONSE *resp = NULL;
188 STACK_OF(CONF_VALUE) *headers = NULL;
189 STACK_OF(OCSP_CERTID) *ids = NULL;
190 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
191 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
192 STACK_OF(X509) *issuers = NULL;
193 X509 *issuer = NULL, *cert = NULL;
194 STACK_OF(X509) *rca_cert = NULL;
195 X509 *signer = NULL, *rsigner = NULL;
196 X509_STORE *store = NULL;
197 X509_VERIFY_PARAM *vpm = NULL;
198 const char *CAfile = NULL, *CApath = NULL;
199 char *header, *value;
200 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
201 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
202 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
203 char *rsignfile = NULL, *rkeyfile = NULL;
204 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
205 char *signfile = NULL, *keyfile = NULL;
206 char *thost = NULL, *tport = NULL, *tpath = NULL;
207 int noCAfile = 0, noCApath = 0;
208 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
209 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
210 int req_text = 0, resp_text = 0, ret = 1;
211 #ifndef OPENSSL_NO_SOCK
212 int req_timeout = -1;
214 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
215 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
219 reqnames = sk_OPENSSL_STRING_new_null();
220 if (reqnames == NULL)
222 ids = sk_OCSP_CERTID_new_null();
225 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
228 prog = opt_init(argc, argv, ocsp_options);
229 while ((o = opt_next()) != OPT_EOF) {
234 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
238 opt_help(ocsp_options);
244 #ifndef OPENSSL_NO_SOCK
245 req_timeout = atoi(opt_arg());
252 thost = tport = tpath = NULL;
253 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
254 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
279 case OPT_RESP_NO_CERTS:
280 rflags |= OCSP_NOCERTS;
282 case OPT_RESP_KEY_ID:
283 rflags |= OCSP_RESPID_KEY;
286 sign_flags |= OCSP_NOCERTS;
288 case OPT_NO_SIGNATURE_VERIFY:
289 verify_flags |= OCSP_NOSIGS;
291 case OPT_NO_CERT_VERIFY:
292 verify_flags |= OCSP_NOVERIFY;
295 verify_flags |= OCSP_NOCHAIN;
297 case OPT_NO_CERT_CHECKS:
298 verify_flags |= OCSP_NOCHECKS;
300 case OPT_NO_EXPLICIT:
301 verify_flags |= OCSP_NOEXPLICIT;
303 case OPT_TRUST_OTHER:
304 verify_flags |= OCSP_TRUSTOTHER;
307 verify_flags |= OCSP_NOINTERN;
313 req_text = resp_text = 1;
328 signfile = opt_arg();
331 verify_certfile = opt_arg();
332 verify_flags |= OCSP_TRUSTOTHER;
335 sign_certfile = opt_arg();
337 case OPT_VERIFY_OTHER:
338 verify_certfile = opt_arg();
353 if (!opt_verify(o, vpm))
357 case OPT_VALIDITY_PERIOD:
358 opt_long(opt_arg(), &nsec);
361 opt_long(opt_arg(), &maxage);
376 issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate");
379 if (issuers == NULL) {
380 if ((issuers = sk_X509_new_null()) == NULL)
383 sk_X509_push(issuers, issuer);
387 cert = load_cert(opt_arg(), FORMAT_PEM, "certificate");
390 if (cert_id_md == NULL)
391 cert_id_md = EVP_sha1();
392 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
394 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
399 if (cert_id_md == NULL)
400 cert_id_md = EVP_sha1();
401 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
403 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
408 ridx_filename = opt_arg();
411 rca_filename = opt_arg();
414 opt_int(opt_arg(), &nmin);
419 opt_int(opt_arg(), &accept_count);
422 ndays = atoi(opt_arg());
425 rsignfile = opt_arg();
428 rkeyfile = opt_arg();
431 rcertfile = opt_arg();
433 case OPT_RMD: /* Response MessageDigest */
434 if (!opt_md(opt_arg(), &rsign_md))
439 value = strchr(header, '=');
441 BIO_printf(bio_err, "Missing = in header key=value\n");
445 if (!X509V3_add_value(header, value, &headers))
451 "%s: Digest must be before -cert or -serial\n",
455 if (!opt_md(opt_unknown(), &cert_id_md))
463 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
467 argc = opt_num_rest();
471 /* Have we anything to do? */
472 if (req == NULL&& reqin == NULL
473 && respin == NULL && !(port != NULL && ridx_filename != NULL))
476 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
480 if (req == NULL && (add_nonce != 2))
483 if (req == NULL && reqin != NULL) {
484 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
487 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
490 BIO_printf(bio_err, "Error reading OCSP request\n");
495 if (req == NULL && port != NULL) {
496 acbio = init_responder(port);
501 if (rsignfile != NULL) {
502 if (rkeyfile == NULL)
503 rkeyfile = rsignfile;
504 rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate");
505 if (rsigner == NULL) {
506 BIO_printf(bio_err, "Error loading responder certificate\n");
509 if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM,
510 NULL, "CA certificate"))
512 if (rcertfile != NULL) {
513 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
514 "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))
532 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
534 send_ocsp_response(cbio, resp);
540 && (signfile != NULL || reqout != NULL
541 || host != NULL || add_nonce || ridx_filename != NULL)) {
542 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
546 if (req != NULL && add_nonce)
547 OCSP_request_add1_nonce(req, NULL, -1);
549 if (signfile != NULL) {
552 signer = load_cert(signfile, FORMAT_PEM, "signer certificate");
553 if (signer == NULL) {
554 BIO_printf(bio_err, "Error loading signer certificate\n");
557 if (sign_certfile != NULL) {
558 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL,
559 "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");
574 if (req_text && req != NULL)
575 OCSP_REQUEST_print(out, req, 0);
577 if (reqout != NULL) {
578 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
581 i2d_OCSP_REQUEST_bio(derbio, req);
585 if (ridx_filename != NULL
586 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
588 "Need a responder certificate, key and CA for this operation!\n");
592 if (ridx_filename != NULL && rdb == NULL) {
593 rdb = load_index(ridx_filename, NULL);
596 if (!index_index(rdb))
601 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
602 rsign_md, rother, rflags, nmin, ndays, badsig);
604 send_ocsp_response(cbio, resp);
605 } else if (host != NULL) {
606 # ifndef OPENSSL_NO_SOCK
607 resp = process_responder(req, host, path,
608 port, use_ssl, headers, req_timeout);
613 "Error creating connect BIO - sockets not supported.\n");
616 } else if (respin != NULL) {
617 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
620 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
623 BIO_printf(bio_err, "Error reading OCSP response\n");
633 if (respout != NULL) {
634 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
637 i2d_OCSP_RESPONSE_bio(derbio, resp);
641 i = OCSP_response_status(resp);
642 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
643 BIO_printf(out, "Responder Error: %s (%d)\n",
644 OCSP_response_status_str(i), i);
652 OCSP_RESPONSE_print(out, resp, 0);
654 /* If running as responder don't verify our own response */
656 /* If not unlimited, see if we took all we should. */
657 if (accept_count != -1 && --accept_count <= 0) {
663 OCSP_REQUEST_free(req);
665 OCSP_RESPONSE_free(resp);
669 if (ridx_filename != NULL) {
675 store = setup_verify(CAfile, CApath, noCAfile, noCApath);
680 X509_STORE_set1_param(store, vpm);
681 if (verify_certfile != NULL) {
682 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL,
683 "validator certificate"))
687 bs = OCSP_response_get1_basic(resp);
689 BIO_printf(bio_err, "Error parsing response\n");
696 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
698 BIO_printf(bio_err, "WARNING: no nonce in response\n");
700 BIO_printf(bio_err, "Nonce Verify error\n");
706 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
707 if (i <= 0 && issuers) {
708 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
713 BIO_printf(bio_err, "Response Verify Failure\n");
714 ERR_print_errors(bio_err);
717 BIO_printf(bio_err, "Response verify OK\n");
721 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
724 ERR_print_errors(bio_err);
726 X509_STORE_free(store);
727 X509_VERIFY_PARAM_free(vpm);
731 sk_X509_pop_free(issuers, X509_free);
733 sk_X509_pop_free(rca_cert, X509_free);
738 OCSP_REQUEST_free(req);
739 OCSP_RESPONSE_free(resp);
740 OCSP_BASICRESP_free(bs);
741 sk_OPENSSL_STRING_free(reqnames);
742 sk_OCSP_CERTID_free(ids);
743 sk_X509_pop_free(sign_other, X509_free);
744 sk_X509_pop_free(verify_other, X509_free);
745 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
753 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
754 const EVP_MD *cert_id_md, X509 *issuer,
755 STACK_OF(OCSP_CERTID) *ids)
759 if (issuer == NULL) {
760 BIO_printf(bio_err, "No issuer certificate specified\n");
764 *req = OCSP_REQUEST_new();
767 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
768 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
770 if (!OCSP_request_add0_id(*req, id))
775 BIO_printf(bio_err, "Error Creating OCSP request\n");
779 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
780 const EVP_MD *cert_id_md, X509 *issuer,
781 STACK_OF(OCSP_CERTID) *ids)
785 ASN1_BIT_STRING *ikey;
788 if (issuer == NULL) {
789 BIO_printf(bio_err, "No issuer certificate specified\n");
793 *req = OCSP_REQUEST_new();
796 iname = X509_get_subject_name(issuer);
797 ikey = X509_get0_pubkey_bitstr(issuer);
798 sno = s2i_ASN1_INTEGER(NULL, serial);
800 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
803 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
804 ASN1_INTEGER_free(sno);
805 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
807 if (!OCSP_request_add0_id(*req, id))
812 BIO_printf(bio_err, "Error Creating OCSP request\n");
816 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
817 STACK_OF(OPENSSL_STRING) *names,
818 STACK_OF(OCSP_CERTID) *ids, long nsec,
823 int i, status, reason;
824 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
826 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
827 || !sk_OCSP_CERTID_num(ids))
830 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
831 id = sk_OCSP_CERTID_value(ids, i);
832 name = sk_OPENSSL_STRING_value(names, i);
833 BIO_printf(out, "%s: ", name);
835 if (!OCSP_resp_find_status(bs, id, &status, &reason,
836 &rev, &thisupd, &nextupd)) {
837 BIO_puts(out, "ERROR: No Status found.\n");
842 * Check validity: if invalid write to output BIO so we know which
843 * response this refers to.
845 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
846 BIO_puts(out, "WARNING: Status times invalid.\n");
847 ERR_print_errors(out);
849 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
851 BIO_puts(out, "\tThis Update: ");
852 ASN1_GENERALIZEDTIME_print(out, thisupd);
856 BIO_puts(out, "\tNext Update: ");
857 ASN1_GENERALIZEDTIME_print(out, nextupd);
861 if (status != V_OCSP_CERTSTATUS_REVOKED)
865 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
867 BIO_puts(out, "\tRevocation Time: ");
868 ASN1_GENERALIZEDTIME_print(out, rev);
873 static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
874 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
875 EVP_PKEY *rkey, const EVP_MD *rmd,
876 STACK_OF(X509) *rother, unsigned long flags,
877 int nmin, int ndays, int badsig)
879 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
881 OCSP_BASICRESP *bs = NULL;
884 id_count = OCSP_request_onereq_count(req);
888 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
892 bs = OCSP_BASICRESP_new();
893 thisupd = X509_gmtime_adj(NULL, 0);
895 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
897 /* Examine each certificate id in the request */
898 for (i = 0; i < id_count; i++) {
900 ASN1_INTEGER *serial;
904 ASN1_OBJECT *cert_id_md_oid;
905 const EVP_MD *cert_id_md;
906 one = OCSP_request_onereq_get0(req, i);
907 cid = OCSP_onereq_get0_id(one);
909 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
911 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
912 if (cert_id_md == NULL) {
913 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
917 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
918 X509 *ca_cert = sk_X509_value(ca, jj);
919 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
921 if (OCSP_id_issuer_cmp(ca_id, cid) == 0)
924 OCSP_CERTID_free(ca_id);
928 OCSP_basic_add1_status(bs, cid,
929 V_OCSP_CERTSTATUS_UNKNOWN,
930 0, NULL, thisupd, nextupd);
933 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
934 inf = lookup_serial(db, serial);
936 OCSP_basic_add1_status(bs, cid,
937 V_OCSP_CERTSTATUS_UNKNOWN,
938 0, NULL, thisupd, nextupd);
939 } else if (inf[DB_type][0] == DB_TYPE_VAL) {
940 OCSP_basic_add1_status(bs, cid,
941 V_OCSP_CERTSTATUS_GOOD,
942 0, NULL, thisupd, nextupd);
943 } else if (inf[DB_type][0] == DB_TYPE_REV) {
944 ASN1_OBJECT *inst = NULL;
945 ASN1_TIME *revtm = NULL;
946 ASN1_GENERALIZEDTIME *invtm = NULL;
947 OCSP_SINGLERESP *single;
949 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
950 single = OCSP_basic_add1_status(bs, cid,
951 V_OCSP_CERTSTATUS_REVOKED,
952 reason, revtm, thisupd, nextupd);
954 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
956 else if (inst != NULL)
957 OCSP_SINGLERESP_add1_ext_i2d(single,
958 NID_hold_instruction_code, inst,
960 ASN1_OBJECT_free(inst);
961 ASN1_TIME_free(revtm);
962 ASN1_GENERALIZEDTIME_free(invtm);
966 OCSP_copy_nonce(bs, req);
968 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
971 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
972 corrupt_signature(sig);
975 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
978 ASN1_TIME_free(thisupd);
979 ASN1_TIME_free(nextupd);
980 OCSP_BASICRESP_free(bs);
983 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
987 char *itmp, *row[DB_NUMBER], **rrow;
988 for (i = 0; i < DB_NUMBER; i++)
990 bn = ASN1_INTEGER_to_BN(ser, NULL);
991 OPENSSL_assert(bn); /* FIXME: should report an error at this
994 itmp = OPENSSL_strdup("00");
996 itmp = BN_bn2hex(bn);
997 row[DB_serial] = itmp;
999 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1004 /* Quick and dirty OCSP server: read in and parse input request */
1006 static BIO *init_responder(const char *port)
1008 # ifdef OPENSSL_NO_SOCK
1010 "Error setting up accept BIO - sockets not supported.\n");
1013 BIO *acbio = NULL, *bufbio = NULL;
1015 bufbio = BIO_new(BIO_f_buffer());
1018 acbio = BIO_new(BIO_s_accept());
1020 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1021 || BIO_set_accept_port(acbio, port) < 0) {
1022 BIO_printf(bio_err, "Error setting up accept BIO\n");
1023 ERR_print_errors(bio_err);
1027 BIO_set_accept_bios(acbio, bufbio);
1029 if (BIO_do_accept(acbio) <= 0) {
1030 BIO_printf(bio_err, "Error starting accept\n");
1031 ERR_print_errors(bio_err);
1038 BIO_free_all(acbio);
1044 # ifndef OPENSSL_NO_SOCK
1046 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1048 static int urldecode(char *p)
1050 unsigned char *out = (unsigned char *)p;
1051 unsigned char *save = out;
1056 else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
1057 /* Don't check, can't fail because of ixdigit() call. */
1058 *out++ = (OPENSSL_hexchar2int(p[1]) << 4)
1059 | OPENSSL_hexchar2int(p[2]);
1066 return (int)(out - save);
1070 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio)
1072 # ifdef OPENSSL_NO_SOCK
1076 OCSP_REQUEST *req = NULL;
1077 char inbuf[2048], reqbuf[2048];
1079 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
1081 if (BIO_do_accept(acbio) <= 0) {
1082 BIO_printf(bio_err, "Error accepting connection\n");
1083 ERR_print_errors(bio_err);
1087 cbio = BIO_pop(acbio);
1090 /* Read the request line. */
1091 len = BIO_gets(cbio, reqbuf, sizeof(reqbuf));
1094 if (strncmp(reqbuf, "GET ", 4) == 0) {
1095 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
1096 for (p = reqbuf + 4; *p == ' '; ++p)
1099 BIO_printf(bio_err, "Invalid request -- bad URL\n");
1104 /* Splice off the HTTP version identifier. */
1105 for (q = p; *q; q++)
1108 if (strncmp(q, " HTTP/1.", 8) != 0) {
1109 BIO_printf(bio_err, "Invalid request -- bad HTTP version\n");
1115 BIO_printf(bio_err, "Invalid request -- bad URL encoding\n");
1118 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1119 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
1120 BIO_printf(bio_err, "Could not allocate memory\n");
1121 ERR_print_errors(bio_err);
1124 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1125 getbio = BIO_push(b64, getbio);
1126 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
1127 BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n");
1131 /* Read and skip past the headers. */
1133 len = BIO_gets(cbio, inbuf, sizeof(inbuf));
1136 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1140 /* Try to read OCSP request */
1141 if (getbio != NULL) {
1142 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1143 BIO_free_all(getbio);
1145 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1149 BIO_printf(bio_err, "Error parsing OCSP request\n");
1150 ERR_print_errors(bio_err);
1159 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1162 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1163 "Content-Length: %d\r\n\r\n";
1166 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1167 i2d_OCSP_RESPONSE_bio(cbio, resp);
1168 (void)BIO_flush(cbio);
1172 # ifndef OPENSSL_NO_SOCK
1173 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
1175 const STACK_OF(CONF_VALUE) *headers,
1176 OCSP_REQUEST *req, int req_timeout)
1182 OCSP_REQ_CTX *ctx = NULL;
1183 OCSP_RESPONSE *rsp = NULL;
1187 if (req_timeout != -1)
1188 BIO_set_nbio(cbio, 1);
1190 rv = BIO_do_connect(cbio);
1192 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1193 BIO_puts(bio_err, "Error connecting BIO\n");
1197 if (BIO_get_fd(cbio, &fd) < 0) {
1198 BIO_puts(bio_err, "Can't get connection fd\n");
1202 if (req_timeout != -1 && rv <= 0) {
1204 openssl_fdset(fd, &confds);
1206 tv.tv_sec = req_timeout;
1207 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1209 BIO_puts(bio_err, "Timeout on connect\n");
1214 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1218 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1219 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1220 if (add_host == 1 && strcasecmp("host", hdr->name) == 0)
1222 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1226 if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0)
1229 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1233 rv = OCSP_sendreq_nbio(&rsp, ctx);
1236 if (req_timeout == -1)
1239 openssl_fdset(fd, &confds);
1241 tv.tv_sec = req_timeout;
1242 if (BIO_should_read(cbio)) {
1243 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1244 } else if (BIO_should_write(cbio)) {
1245 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1247 BIO_puts(bio_err, "Unexpected retry condition\n");
1251 BIO_puts(bio_err, "Timeout on request\n");
1255 BIO_puts(bio_err, "Select error\n");
1261 OCSP_REQ_CTX_free(ctx);
1266 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
1267 const char *host, const char *path,
1268 const char *port, int use_ssl,
1269 STACK_OF(CONF_VALUE) *headers,
1273 SSL_CTX *ctx = NULL;
1274 OCSP_RESPONSE *resp = NULL;
1276 cbio = BIO_new_connect(host);
1278 BIO_printf(bio_err, "Error creating connect BIO\n");
1282 BIO_set_conn_port(cbio, port);
1285 ctx = SSL_CTX_new(TLS_client_method());
1287 BIO_printf(bio_err, "Error creating SSL context.\n");
1290 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1291 sbio = BIO_new_ssl(ctx, 1);
1292 cbio = BIO_push(sbio, cbio);
1295 resp = query_responder(cbio, host, path, headers, req, req_timeout);
1297 BIO_printf(bio_err, "Error querying OCSP responder\n");