2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
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
63 #include <openssl/pem.h>
64 #include <openssl/ocsp.h>
65 #include <openssl/err.h>
66 #include <openssl/ssl.h>
68 /* Maximum leeway in validity period: default 5 minutes */
69 #define MAX_VALIDITY_PERIOD (5 * 60)
71 /* CA index.txt definitions */
75 #define DB_serial 3 /* index - unique */
77 #define DB_name 5 /* index - unique for active */
80 #define DB_TYPE_REV 'R'
81 #define DB_TYPE_EXP 'E'
82 #define DB_TYPE_VAL 'V'
84 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
85 STACK_OF(OCSP_CERTID) *ids);
86 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
87 STACK_OF(OCSP_CERTID) *ids);
88 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
89 STACK *names, STACK_OF(OCSP_CERTID) *ids,
90 long nsec, long maxage);
92 static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, TXT_DB *db,
93 X509 *ca, X509 *rcert, EVP_PKEY *rkey,
94 STACK_OF(X509) *rother, unsigned long flags,
97 static char **lookup_serial(TXT_DB *db, ASN1_INTEGER *ser);
98 static BIO *init_responder(char *port);
99 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, char *port);
100 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
103 #define PROG ocsp_main
105 int MAIN(int, char **);
107 int MAIN(int argc, char **argv)
111 char *host = NULL, *port = NULL, *path = "/";
112 char *reqin = NULL, *respin = NULL;
113 char *reqout = NULL, *respout = NULL;
114 char *signfile = NULL, *keyfile = NULL;
115 char *rsignfile = NULL, *rkeyfile = NULL;
116 char *outfile = NULL;
117 int add_nonce = 1, noverify = 0, use_ssl = -1;
118 OCSP_REQUEST *req = NULL;
119 OCSP_RESPONSE *resp = NULL;
120 OCSP_BASICRESP *bs = NULL;
121 X509 *issuer = NULL, *cert = NULL;
122 X509 *signer = NULL, *rsigner = NULL;
123 EVP_PKEY *key = NULL, *rkey = NULL;
124 BIO *acbio = NULL, *cbio = NULL;
127 int req_text = 0, resp_text = 0;
128 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
129 char *CAfile = NULL, *CApath = NULL;
130 X509_STORE *store = NULL;
132 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
133 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
134 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
136 int accept_count = -1;
139 STACK *reqnames = NULL;
140 STACK_OF(OCSP_CERTID) *ids = NULL;
142 X509 *rca_cert = NULL;
143 char *ridx_filename = NULL;
144 char *rca_filename = NULL;
146 int nmin = 0, ndays = -1;
148 if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
150 if (!load_config(bio_err, NULL))
152 SSL_load_error_strings();
154 reqnames = sk_new_null();
155 ids = sk_OCSP_CERTID_new_null();
156 while (!badarg && *args && *args[0] == '-')
158 if (!strcmp(*args, "-out"))
167 else if (!strcmp(*args, "-url"))
172 if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl))
174 BIO_printf(bio_err, "Error parsing URL\n");
180 else if (!strcmp(*args, "-host"))
189 else if (!strcmp(*args, "-port"))
198 else if (!strcmp(*args, "-noverify"))
200 else if (!strcmp(*args, "-nonce"))
202 else if (!strcmp(*args, "-no_nonce"))
204 else if (!strcmp(*args, "-resp_no_certs"))
205 rflags |= OCSP_NOCERTS;
206 else if (!strcmp(*args, "-resp_key_id"))
207 rflags |= OCSP_RESPID_KEY;
208 else if (!strcmp(*args, "-no_certs"))
209 sign_flags |= OCSP_NOCERTS;
210 else if (!strcmp(*args, "-no_signature_verify"))
211 verify_flags |= OCSP_NOSIGS;
212 else if (!strcmp(*args, "-no_cert_verify"))
213 verify_flags |= OCSP_NOVERIFY;
214 else if (!strcmp(*args, "-no_chain"))
215 verify_flags |= OCSP_NOCHAIN;
216 else if (!strcmp(*args, "-no_cert_checks"))
217 verify_flags |= OCSP_NOCHECKS;
218 else if (!strcmp(*args, "-no_explicit"))
219 verify_flags |= OCSP_NOEXPLICIT;
220 else if (!strcmp(*args, "-trust_other"))
221 verify_flags |= OCSP_TRUSTOTHER;
222 else if (!strcmp(*args, "-no_intern"))
223 verify_flags |= OCSP_NOINTERN;
224 else if (!strcmp(*args, "-text"))
229 else if (!strcmp(*args, "-req_text"))
231 else if (!strcmp(*args, "-resp_text"))
233 else if (!strcmp(*args, "-reqin"))
242 else if (!strcmp(*args, "-respin"))
251 else if (!strcmp(*args, "-signer"))
260 else if (!strcmp (*args, "-VAfile"))
265 verify_certfile = *args;
266 verify_flags |= OCSP_TRUSTOTHER;
270 else if (!strcmp(*args, "-sign_other"))
275 sign_certfile = *args;
279 else if (!strcmp(*args, "-verify_other"))
284 verify_certfile = *args;
288 else if (!strcmp (*args, "-CAfile"))
297 else if (!strcmp (*args, "-CApath"))
306 else if (!strcmp (*args, "-validity_period"))
315 "Illegal validity period %s\n",
322 else if (!strcmp (*args, "-status_age"))
327 maxage = atol(*args);
331 "Illegal validity age %s\n",
338 else if (!strcmp(*args, "-signkey"))
347 else if (!strcmp(*args, "-reqout"))
356 else if (!strcmp(*args, "-respout"))
365 else if (!strcmp(*args, "-path"))
374 else if (!strcmp(*args, "-issuer"))
380 issuer = load_cert(bio_err, *args, FORMAT_PEM,
381 NULL, e, "issuer certificate");
382 if(!issuer) goto end;
386 else if (!strcmp (*args, "-cert"))
392 cert = load_cert(bio_err, *args, FORMAT_PEM,
393 NULL, e, "certificate");
395 if(!add_ocsp_cert(&req, cert, issuer, ids))
397 if(!sk_push(reqnames, *args))
402 else if (!strcmp(*args, "-serial"))
407 if(!add_ocsp_serial(&req, *args, issuer, ids))
409 if(!sk_push(reqnames, *args))
414 else if (!strcmp(*args, "-index"))
419 ridx_filename = *args;
423 else if (!strcmp(*args, "-CA"))
428 rca_filename = *args;
432 else if (!strcmp (*args, "-nmin"))
441 "Illegal update period %s\n",
450 else if (!strcmp (*args, "-nrequest"))
455 accept_count = atol(*args);
456 if (accept_count < 0)
459 "Illegal accept count %s\n",
466 else if (!strcmp (*args, "-ndays"))
475 "Illegal update period %s\n",
482 else if (!strcmp(*args, "-rsigner"))
491 else if (!strcmp(*args, "-rkey"))
500 else if (!strcmp(*args, "-rother"))
513 /* Have we anything to do? */
514 if (!req && !reqin && !respin && !(port && ridx_filename)) badarg = 1;
518 BIO_printf (bio_err, "OCSP utility\n");
519 BIO_printf (bio_err, "Usage ocsp [options]\n");
520 BIO_printf (bio_err, "where options are\n");
521 BIO_printf (bio_err, "-out file output filename\n");
522 BIO_printf (bio_err, "-issuer file issuer certificate\n");
523 BIO_printf (bio_err, "-cert file certificate to check\n");
524 BIO_printf (bio_err, "-serial n serial number to check\n");
525 BIO_printf (bio_err, "-signer file certificate to sign OCSP request with\n");
526 BIO_printf (bio_err, "-signkey file private key to sign OCSP request with\n");
527 BIO_printf (bio_err, "-sign_certs file additional certificates to include in signed request\n");
528 BIO_printf (bio_err, "-no_certs don't include any certificates in signed request\n");
529 BIO_printf (bio_err, "-req_text print text form of request\n");
530 BIO_printf (bio_err, "-resp_text print text form of response\n");
531 BIO_printf (bio_err, "-text print text form of request and response\n");
532 BIO_printf (bio_err, "-reqout file write DER encoded OCSP request to \"file\"\n");
533 BIO_printf (bio_err, "-respout file write DER encoded OCSP reponse to \"file\"\n");
534 BIO_printf (bio_err, "-reqin file read DER encoded OCSP request from \"file\"\n");
535 BIO_printf (bio_err, "-respin file read DER encoded OCSP reponse from \"file\"\n");
536 BIO_printf (bio_err, "-nonce add OCSP nonce to request\n");
537 BIO_printf (bio_err, "-no_nonce don't add OCSP nonce to request\n");
538 BIO_printf (bio_err, "-url URL OCSP responder URL\n");
539 BIO_printf (bio_err, "-host host:n send OCSP request to host on port n\n");
540 BIO_printf (bio_err, "-path path to use in OCSP request\n");
541 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
542 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
543 BIO_printf (bio_err, "-VAfile file validator certificates file\n");
544 BIO_printf (bio_err, "-validity_period n maximum validity discrepancy in seconds\n");
545 BIO_printf (bio_err, "-status_age n maximum status age in seconds\n");
546 BIO_printf (bio_err, "-noverify don't verify response at all\n");
547 BIO_printf (bio_err, "-verify_certs file additional certificates to search for signer\n");
548 BIO_printf (bio_err, "-trust_other don't verify additional certificates\n");
549 BIO_printf (bio_err, "-no_intern don't search certificates contained in response for signer\n");
550 BIO_printf (bio_err, "-no_sig_verify don't check signature on response\n");
551 BIO_printf (bio_err, "-no_cert_verify don't check signing certificate\n");
552 BIO_printf (bio_err, "-no_chain don't chain verify response\n");
553 BIO_printf (bio_err, "-no_cert_checks don't do additional checks on signing certificate\n");
554 BIO_printf (bio_err, "-port num port to run responder on\n");
555 BIO_printf (bio_err, "-index file certificate status index file\n");
556 BIO_printf (bio_err, "-CA file CA certificate\n");
557 BIO_printf (bio_err, "-rsigner file responder certificate to sign responses with\n");
558 BIO_printf (bio_err, "-rkey file responder key to sign responses with\n");
559 BIO_printf (bio_err, "-rother file other certificates to include in response\n");
560 BIO_printf (bio_err, "-resp_no_certs don't include any certificates in response\n");
561 BIO_printf (bio_err, "-nmin n number of minutes before next update\n");
562 BIO_printf (bio_err, "-ndays n number of days before next update\n");
563 BIO_printf (bio_err, "-resp_key_id identify reponse by signing certificate key ID\n");
564 BIO_printf (bio_err, "-nrequest n number of requests to accept (default unlimited)\n");
568 if(outfile) out = BIO_new_file(outfile, "w");
569 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
573 BIO_printf(bio_err, "Error opening output file\n");
577 if (!req && (add_nonce != 2)) add_nonce = 0;
581 derbio = BIO_new_file(reqin, "rb");
584 BIO_printf(bio_err, "Error Opening OCSP request file\n");
587 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
591 BIO_printf(bio_err, "Error reading OCSP request\n");
598 acbio = init_responder(port);
603 if (rsignfile && !rdb)
605 if (!rkeyfile) rkeyfile = rsignfile;
606 rsigner = load_cert(bio_err, rsignfile, FORMAT_PEM,
607 NULL, e, "responder certificate");
610 BIO_printf(bio_err, "Error loading responder certificate\n");
613 rca_cert = load_cert(bio_err, rca_filename, FORMAT_PEM,
614 NULL, e, "CA certificate");
617 rother = load_certs(bio_err, rcertfile, FORMAT_PEM,
618 NULL, e, "responder other certificates");
619 if (!rother) goto end;
621 rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL, NULL,
622 "responder private key");
627 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
633 if (!do_responder(&req, &cbio, acbio, port))
637 resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
638 send_ocsp_response(cbio, resp);
643 if (!req && (signfile || reqout || host || add_nonce || ridx_filename))
645 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
649 if (req && add_nonce) OCSP_request_add1_nonce(req, NULL, -1);
653 if (!keyfile) keyfile = signfile;
654 signer = load_cert(bio_err, signfile, FORMAT_PEM,
655 NULL, e, "signer certificate");
658 BIO_printf(bio_err, "Error loading signer certificate\n");
663 sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM,
664 NULL, e, "signer certificates");
665 if (!sign_other) goto end;
667 key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL, NULL,
668 "signer private key");
671 if (!OCSP_request_sign(req, signer, key, EVP_sha1(), sign_other, sign_flags))
673 BIO_printf(bio_err, "Error signing OCSP request\n");
678 if (req_text && req) OCSP_REQUEST_print(out, req, 0);
682 derbio = BIO_new_file(reqout, "wb");
685 BIO_printf(bio_err, "Error opening file %s\n", reqout);
688 i2d_OCSP_REQUEST_bio(derbio, req);
692 if (ridx_filename && (!rkey || !rsigner || !rca_cert))
694 BIO_printf(bio_err, "Need a responder certificate, key and CA for this operation!\n");
698 if (ridx_filename && !rdb)
701 db_bio = BIO_new_file(ridx_filename, "r");
704 BIO_printf(bio_err, "Error opening index file %s\n", ridx_filename);
707 rdb = TXT_DB_read(db_bio, DB_NUMBER);
711 BIO_printf(bio_err, "Error reading index file %s\n", ridx_filename);
714 if (!make_serial_index(rdb))
720 i = make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, rother, rflags, nmin, ndays);
722 send_ocsp_response(cbio, resp);
726 #ifndef OPENSSL_NO_SOCK
727 cbio = BIO_new_connect(host);
729 BIO_printf(bio_err, "Error creating connect BIO - sockets not supported.\n");
734 BIO_printf(bio_err, "Error creating connect BIO\n");
737 if (port) BIO_set_conn_port(cbio, port);
741 #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
742 ctx = SSL_CTX_new(SSLv23_client_method());
743 #elif !defined(OPENSSL_NO_SSL3)
744 ctx = SSL_CTX_new(SSLv3_client_method());
745 #elif !defined(OPENSSL_NO_SSL2)
746 ctx = SSL_CTX_new(SSLv2_client_method());
748 BIO_printf(bio_err, "SSL is disabled\n");
751 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
752 sbio = BIO_new_ssl(ctx, 1);
753 cbio = BIO_push(sbio, cbio);
755 if (BIO_do_connect(cbio) <= 0)
757 BIO_printf(bio_err, "Error connecting BIO\n");
760 resp = OCSP_sendreq_bio(cbio, path, req);
765 BIO_printf(bio_err, "Error querying OCSP responsder\n");
771 derbio = BIO_new_file(respin, "rb");
774 BIO_printf(bio_err, "Error Opening OCSP response file\n");
777 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
781 BIO_printf(bio_err, "Error reading OCSP response\n");
796 derbio = BIO_new_file(respout, "wb");
799 BIO_printf(bio_err, "Error opening file %s\n", respout);
802 i2d_OCSP_RESPONSE_bio(derbio, resp);
806 i = OCSP_response_status(resp);
808 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL)
810 BIO_printf(out, "Responder Error: %s (%ld)\n",
811 OCSP_response_status_str(i), i);
816 if (resp_text) OCSP_RESPONSE_print(out, resp, 0);
818 /* If running as responder don't verify our own response */
821 if (accept_count > 0)
823 /* Redo if more connections needed */
828 OCSP_REQUEST_free(req);
830 OCSP_RESPONSE_free(resp);
838 store = setup_verify(bio_err, CAfile, CApath);
843 verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM,
844 NULL, e, "validator certificate");
845 if (!verify_other) goto end;
848 bs = OCSP_response_get1_basic(resp);
852 BIO_printf(bio_err, "Error parsing response\n");
858 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0))
861 BIO_printf(bio_err, "WARNING: no nonce in response\n");
864 BIO_printf(bio_err, "Nonce Verify error\n");
869 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
870 if (i < 0) i = OCSP_basic_verify(bs, NULL, store, 0);
874 BIO_printf(bio_err, "Response Verify Failure\n", i);
875 ERR_print_errors(bio_err);
878 BIO_printf(bio_err, "Response verify OK\n");
882 if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
888 ERR_print_errors(bio_err);
890 X509_STORE_free(store);
901 OCSP_REQUEST_free(req);
902 OCSP_RESPONSE_free(resp);
903 OCSP_BASICRESP_free(bs);
905 sk_OCSP_CERTID_free(ids);
906 sk_X509_pop_free(sign_other, X509_free);
907 sk_X509_pop_free(verify_other, X509_free);
920 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
921 STACK_OF(OCSP_CERTID) *ids)
926 BIO_printf(bio_err, "No issuer certificate specified\n");
929 if(!*req) *req = OCSP_REQUEST_new();
931 id = OCSP_cert_to_id(NULL, cert, issuer);
932 if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
933 if(!OCSP_request_add0_id(*req, id)) goto err;
937 BIO_printf(bio_err, "Error Creating OCSP request\n");
941 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
942 STACK_OF(OCSP_CERTID) *ids)
946 ASN1_BIT_STRING *ikey;
950 BIO_printf(bio_err, "No issuer certificate specified\n");
953 if(!*req) *req = OCSP_REQUEST_new();
955 iname = X509_get_subject_name(issuer);
956 ikey = X509_get0_pubkey_bitstr(issuer);
957 sno = s2i_ASN1_INTEGER(NULL, serial);
960 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
963 id = OCSP_cert_id_new(EVP_sha1(), iname, ikey, sno);
964 ASN1_INTEGER_free(sno);
965 if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
966 if(!OCSP_request_add0_id(*req, id)) goto err;
970 BIO_printf(bio_err, "Error Creating OCSP request\n");
974 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
975 STACK *names, STACK_OF(OCSP_CERTID) *ids,
976 long nsec, long maxage)
984 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
986 if (!bs || !req || !sk_num(names) || !sk_OCSP_CERTID_num(ids))
989 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++)
991 id = sk_OCSP_CERTID_value(ids, i);
992 name = sk_value(names, i);
993 BIO_printf(out, "%s: ", name);
995 if(!OCSP_resp_find_status(bs, id, &status, &reason,
996 &rev, &thisupd, &nextupd))
998 BIO_puts(out, "ERROR: No Status found.\n");
1002 /* Check validity: if invalid write to output BIO so we
1003 * know which response this refers to.
1005 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage))
1007 BIO_puts(out, "WARNING: Status times invalid.\n");
1008 ERR_print_errors(out);
1010 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
1012 BIO_puts(out, "\tThis Update: ");
1013 ASN1_GENERALIZEDTIME_print(out, thisupd);
1014 BIO_puts(out, "\n");
1018 BIO_puts(out, "\tNext Update: ");
1019 ASN1_GENERALIZEDTIME_print(out, nextupd);
1020 BIO_puts(out, "\n");
1023 if (status != V_OCSP_CERTSTATUS_REVOKED)
1027 BIO_printf(out, "\tReason: %s\n",
1028 OCSP_crl_reason_str(reason));
1030 BIO_puts(out, "\tRevocation Time: ");
1031 ASN1_GENERALIZEDTIME_print(out, rev);
1032 BIO_puts(out, "\n");
1039 static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, TXT_DB *db,
1040 X509 *ca, X509 *rcert, EVP_PKEY *rkey,
1041 STACK_OF(X509) *rother, unsigned long flags,
1042 int nmin, int ndays)
1044 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
1045 OCSP_CERTID *cid, *ca_id = NULL;
1046 OCSP_BASICRESP *bs = NULL;
1047 int i, id_count, ret = 1;
1050 id_count = OCSP_request_onereq_count(req);
1054 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1058 ca_id = OCSP_cert_to_id(EVP_sha1(), NULL, ca);
1060 bs = OCSP_BASICRESP_new();
1061 thisupd = X509_gmtime_adj(NULL, 0);
1063 nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24 );
1065 /* Examine each certificate id in the request */
1066 for (i = 0; i < id_count; i++)
1069 ASN1_INTEGER *serial;
1071 one = OCSP_request_onereq_get0(req, i);
1072 cid = OCSP_onereq_get0_id(one);
1073 /* Is this request about our CA? */
1074 if (OCSP_id_issuer_cmp(ca_id, cid))
1076 OCSP_basic_add1_status(bs, cid,
1077 V_OCSP_CERTSTATUS_UNKNOWN,
1082 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1083 inf = lookup_serial(db, serial);
1085 OCSP_basic_add1_status(bs, cid,
1086 V_OCSP_CERTSTATUS_UNKNOWN,
1089 else if (inf[DB_type][0] == DB_TYPE_VAL)
1090 OCSP_basic_add1_status(bs, cid,
1091 V_OCSP_CERTSTATUS_GOOD,
1094 else if (inf[DB_type][0] == DB_TYPE_REV)
1096 ASN1_OBJECT *inst = NULL;
1097 ASN1_TIME *revtm = NULL;
1098 ASN1_GENERALIZEDTIME *invtm = NULL;
1099 OCSP_SINGLERESP *single;
1101 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1102 single = OCSP_basic_add1_status(bs, cid,
1103 V_OCSP_CERTSTATUS_REVOKED,
1107 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, invtm, 0, 0);
1109 OCSP_SINGLERESP_add1_ext_i2d(single, NID_hold_instruction_code, inst, 0, 0);
1110 ASN1_OBJECT_free(inst);
1111 ASN1_TIME_free(revtm);
1112 ASN1_GENERALIZEDTIME_free(invtm);
1116 OCSP_copy_nonce(bs, req);
1118 OCSP_basic_sign(bs, rcert, rkey, EVP_sha1(), rother, flags);
1120 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1123 ASN1_TIME_free(thisupd);
1124 ASN1_TIME_free(nextupd);
1125 OCSP_CERTID_free(ca_id);
1126 OCSP_BASICRESP_free(bs);
1131 static char **lookup_serial(TXT_DB *db, ASN1_INTEGER *ser)
1135 char *itmp, *row[DB_NUMBER],**rrow;
1136 for (i = 0; i < DB_NUMBER; i++) row[i] = NULL;
1137 bn = ASN1_INTEGER_to_BN(ser,NULL);
1139 itmp = BUF_strdup("00");
1141 itmp = BN_bn2hex(bn);
1142 row[DB_serial] = itmp;
1144 rrow=TXT_DB_get_by_index(db,DB_serial,row);
1149 /* Quick and dirty OCSP server: read in and parse input request */
1151 static BIO *init_responder(char *port)
1153 BIO *acbio = NULL, *bufbio = NULL;
1154 bufbio = BIO_new(BIO_f_buffer());
1157 #ifndef OPENSSL_NO_SOCK
1158 acbio = BIO_new_accept(port);
1160 BIO_printf(bio_err, "Error setting up accept BIO - sockets not supported.\n");
1164 BIO_set_accept_bios(acbio, bufbio);
1167 if (BIO_do_accept(acbio) <= 0)
1169 BIO_printf(bio_err, "Error setting up accept BIO\n");
1170 ERR_print_errors(bio_err);
1177 BIO_free_all(acbio);
1182 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, char *port)
1184 int have_post = 0, len;
1185 OCSP_REQUEST *req = NULL;
1189 if (BIO_do_accept(acbio) <= 0)
1191 BIO_printf(bio_err, "Error accepting connection\n");
1192 ERR_print_errors(bio_err);
1196 cbio = BIO_pop(acbio);
1201 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1204 /* Look for "POST" signalling start of query */
1207 if(strncmp(inbuf, "POST", 4))
1209 BIO_printf(bio_err, "Invalid request\n");
1214 /* Look for end of headers */
1215 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1219 /* Try to read OCSP request */
1221 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1225 BIO_printf(bio_err, "Error parsing OCSP request\n");
1226 ERR_print_errors(bio_err);
1235 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1238 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1239 "Content-Length: %d\r\n\r\n";
1242 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1243 i2d_OCSP_RESPONSE_bio(cbio, resp);