3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 #ifndef OPENSSL_NO_OCSP
64 # include "apps.h" /* needs to be included before the openssl
66 # include <openssl/e_os2.h>
67 # include <openssl/ssl.h>
68 # include <openssl/err.h>
70 /* Maximum leeway in validity period: default 5 minutes */
71 # define MAX_VALIDITY_PERIOD (5 * 60)
73 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
, X509
*issuer
,
74 STACK_OF(OCSP_CERTID
) *ids
);
75 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
, X509
*issuer
,
76 STACK_OF(OCSP_CERTID
) *ids
);
77 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
78 STACK
* names
, STACK_OF(OCSP_CERTID
) *ids
,
79 long nsec
, long maxage
);
81 static int make_ocsp_response(OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
82 CA_DB
*db
, X509
*ca
, X509
*rcert
,
83 EVP_PKEY
*rkey
, STACK_OF(X509
) *rother
,
84 unsigned long flags
, int nmin
, int ndays
);
86 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
);
87 static BIO
*init_responder(char *port
);
88 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
90 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
);
91 static OCSP_RESPONSE
*query_responder(BIO
*err
, BIO
*cbio
, char *path
,
92 OCSP_REQUEST
*req
, int req_timeout
);
95 # define PROG ocsp_main
97 int MAIN(int, char **);
99 int MAIN(int argc
, char **argv
)
103 char *host
= NULL
, *port
= NULL
, *path
= "/";
104 char *thost
= NULL
, *tport
= NULL
, *tpath
= NULL
;
105 char *reqin
= NULL
, *respin
= NULL
;
106 char *reqout
= NULL
, *respout
= NULL
;
107 char *signfile
= NULL
, *keyfile
= NULL
;
108 char *rsignfile
= NULL
, *rkeyfile
= NULL
;
109 char *outfile
= NULL
;
110 int add_nonce
= 1, noverify
= 0, use_ssl
= -1;
111 OCSP_REQUEST
*req
= NULL
;
112 OCSP_RESPONSE
*resp
= NULL
;
113 OCSP_BASICRESP
*bs
= NULL
;
114 X509
*issuer
= NULL
, *cert
= NULL
;
115 X509
*signer
= NULL
, *rsigner
= NULL
;
116 EVP_PKEY
*key
= NULL
, *rkey
= NULL
;
117 BIO
*acbio
= NULL
, *cbio
= NULL
;
120 int req_timeout
= -1;
121 int req_text
= 0, resp_text
= 0;
122 long nsec
= MAX_VALIDITY_PERIOD
, maxage
= -1;
123 char *CAfile
= NULL
, *CApath
= NULL
;
124 X509_STORE
*store
= NULL
;
125 STACK_OF(X509
) *sign_other
= NULL
, *verify_other
= NULL
, *rother
= NULL
;
126 char *sign_certfile
= NULL
, *verify_certfile
= NULL
, *rcertfile
= NULL
;
127 unsigned long sign_flags
= 0, verify_flags
= 0, rflags
= 0;
129 int accept_count
= -1;
133 STACK
*reqnames
= NULL
;
134 STACK_OF(OCSP_CERTID
) *ids
= NULL
;
136 X509
*rca_cert
= NULL
;
137 char *ridx_filename
= NULL
;
138 char *rca_filename
= NULL
;
140 int nmin
= 0, ndays
= -1;
143 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
145 if (!load_config(bio_err
, NULL
))
147 SSL_load_error_strings();
148 OpenSSL_add_ssl_algorithms();
150 reqnames
= sk_new_null();
151 ids
= sk_OCSP_CERTID_new_null();
152 while (!badarg
&& *args
&& *args
[0] == '-') {
153 if (!strcmp(*args
, "-out")) {
159 } else if (!strcmp(*args
, "-timeout")) {
162 req_timeout
= atol(*args
);
163 if (req_timeout
< 0) {
164 BIO_printf(bio_err
, "Illegal timeout value %s\n", *args
);
169 } else if (!strcmp(*args
, "-url")) {
178 if (!OCSP_parse_url(*args
, &host
, &port
, &path
, &use_ssl
)) {
179 BIO_printf(bio_err
, "Error parsing URL\n");
187 } else if (!strcmp(*args
, "-host")) {
193 } else if (!strcmp(*args
, "-port")) {
199 } else if (!strcmp(*args
, "-ignore_err"))
201 else if (!strcmp(*args
, "-noverify"))
203 else if (!strcmp(*args
, "-nonce"))
205 else if (!strcmp(*args
, "-no_nonce"))
207 else if (!strcmp(*args
, "-resp_no_certs"))
208 rflags
|= OCSP_NOCERTS
;
209 else if (!strcmp(*args
, "-resp_key_id"))
210 rflags
|= OCSP_RESPID_KEY
;
211 else if (!strcmp(*args
, "-no_certs"))
212 sign_flags
|= OCSP_NOCERTS
;
213 else if (!strcmp(*args
, "-no_signature_verify"))
214 verify_flags
|= OCSP_NOSIGS
;
215 else if (!strcmp(*args
, "-no_cert_verify"))
216 verify_flags
|= OCSP_NOVERIFY
;
217 else if (!strcmp(*args
, "-no_chain"))
218 verify_flags
|= OCSP_NOCHAIN
;
219 else if (!strcmp(*args
, "-no_cert_checks"))
220 verify_flags
|= OCSP_NOCHECKS
;
221 else if (!strcmp(*args
, "-no_explicit"))
222 verify_flags
|= OCSP_NOEXPLICIT
;
223 else if (!strcmp(*args
, "-trust_other"))
224 verify_flags
|= OCSP_TRUSTOTHER
;
225 else if (!strcmp(*args
, "-no_intern"))
226 verify_flags
|= OCSP_NOINTERN
;
227 else if (!strcmp(*args
, "-text")) {
230 } else if (!strcmp(*args
, "-req_text"))
232 else if (!strcmp(*args
, "-resp_text"))
234 else if (!strcmp(*args
, "-reqin")) {
240 } else if (!strcmp(*args
, "-respin")) {
246 } else if (!strcmp(*args
, "-signer")) {
252 } else if (!strcmp(*args
, "-VAfile")) {
255 verify_certfile
= *args
;
256 verify_flags
|= OCSP_TRUSTOTHER
;
259 } else if (!strcmp(*args
, "-sign_other")) {
262 sign_certfile
= *args
;
265 } else if (!strcmp(*args
, "-verify_other")) {
268 verify_certfile
= *args
;
271 } else if (!strcmp(*args
, "-CAfile")) {
277 } else if (!strcmp(*args
, "-CApath")) {
283 } else if (!strcmp(*args
, "-validity_period")) {
289 "Illegal validity period %s\n", *args
);
294 } else if (!strcmp(*args
, "-status_age")) {
297 maxage
= atol(*args
);
299 BIO_printf(bio_err
, "Illegal validity age %s\n", *args
);
304 } else if (!strcmp(*args
, "-signkey")) {
310 } else if (!strcmp(*args
, "-reqout")) {
316 } else if (!strcmp(*args
, "-respout")) {
322 } else if (!strcmp(*args
, "-path")) {
328 } else if (!strcmp(*args
, "-issuer")) {
332 issuer
= load_cert(bio_err
, *args
, FORMAT_PEM
,
333 NULL
, e
, "issuer certificate");
338 } else if (!strcmp(*args
, "-cert")) {
342 cert
= load_cert(bio_err
, *args
, FORMAT_PEM
,
343 NULL
, e
, "certificate");
346 if (!add_ocsp_cert(&req
, cert
, issuer
, ids
))
348 if (!sk_push(reqnames
, *args
))
352 } else if (!strcmp(*args
, "-serial")) {
355 if (!add_ocsp_serial(&req
, *args
, issuer
, ids
))
357 if (!sk_push(reqnames
, *args
))
361 } else if (!strcmp(*args
, "-index")) {
364 ridx_filename
= *args
;
367 } else if (!strcmp(*args
, "-CA")) {
370 rca_filename
= *args
;
373 } else if (!strcmp(*args
, "-nmin")) {
378 BIO_printf(bio_err
, "Illegal update period %s\n", *args
);
386 } else if (!strcmp(*args
, "-nrequest")) {
389 accept_count
= atol(*args
);
390 if (accept_count
< 0) {
391 BIO_printf(bio_err
, "Illegal accept count %s\n", *args
);
396 } else if (!strcmp(*args
, "-ndays")) {
401 BIO_printf(bio_err
, "Illegal update period %s\n", *args
);
406 } else if (!strcmp(*args
, "-rsigner")) {
412 } else if (!strcmp(*args
, "-rkey")) {
418 } else if (!strcmp(*args
, "-rother")) {
429 /* Have we anything to do? */
430 if (!req
&& !reqin
&& !respin
&& !(port
&& ridx_filename
))
434 BIO_printf(bio_err
, "OCSP utility\n");
435 BIO_printf(bio_err
, "Usage ocsp [options]\n");
436 BIO_printf(bio_err
, "where options are\n");
437 BIO_printf(bio_err
, "-out file output filename\n");
438 BIO_printf(bio_err
, "-issuer file issuer certificate\n");
439 BIO_printf(bio_err
, "-cert file certificate to check\n");
440 BIO_printf(bio_err
, "-serial n serial number to check\n");
442 "-signer file certificate to sign OCSP request with\n");
444 "-signkey file private key to sign OCSP request with\n");
446 "-sign_other file additional certificates to include in signed request\n");
448 "-no_certs don't include any certificates in signed request\n");
450 "-req_text print text form of request\n");
452 "-resp_text print text form of response\n");
454 "-text print text form of request and response\n");
456 "-reqout file write DER encoded OCSP request to \"file\"\n");
458 "-respout file write DER encoded OCSP reponse to \"file\"\n");
460 "-reqin file read DER encoded OCSP request from \"file\"\n");
462 "-respin file read DER encoded OCSP reponse from \"file\"\n");
463 BIO_printf(bio_err
, "-nonce add OCSP nonce to request\n");
465 "-no_nonce don't add OCSP nonce to request\n");
466 BIO_printf(bio_err
, "-url URL OCSP responder URL\n");
468 "-host host:n send OCSP request to host on port n\n");
470 "-path path to use in OCSP request\n");
472 "-CApath dir trusted certificates directory\n");
473 BIO_printf(bio_err
, "-CAfile file trusted certificates file\n");
475 "-VAfile file validator certificates file\n");
477 "-validity_period n maximum validity discrepancy in seconds\n");
479 "-status_age n maximum status age in seconds\n");
481 "-noverify don't verify response at all\n");
483 "-verify_other file additional certificates to search for signer\n");
485 "-trust_other don't verify additional certificates\n");
487 "-no_intern don't search certificates contained in response for signer\n");
489 "-no_signature_verify don't check signature on response\n");
491 "-no_cert_verify don't check signing certificate\n");
493 "-no_chain don't chain verify response\n");
495 "-no_cert_checks don't do additional checks on signing certificate\n");
496 BIO_printf(bio_err
, "-port num port to run responder on\n");
498 "-index file certificate status index file\n");
499 BIO_printf(bio_err
, "-CA file CA certificate\n");
501 "-rsigner file responder certificate to sign responses with\n");
503 "-rkey file responder key to sign responses with\n");
505 "-rother file other certificates to include in response\n");
507 "-resp_no_certs don't include any certificates in response\n");
509 "-nmin n number of minutes before next update\n");
511 "-ndays n number of days before next update\n");
513 "-resp_key_id identify reponse by signing certificate key ID\n");
515 "-nrequest n number of requests to accept (default unlimited)\n");
520 out
= BIO_new_file(outfile
, "w");
522 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
525 BIO_printf(bio_err
, "Error opening output file\n");
529 if (!req
&& (add_nonce
!= 2))
533 derbio
= BIO_new_file(reqin
, "rb");
535 BIO_printf(bio_err
, "Error Opening OCSP request file\n");
538 req
= d2i_OCSP_REQUEST_bio(derbio
, NULL
);
541 BIO_printf(bio_err
, "Error reading OCSP request\n");
547 acbio
= init_responder(port
);
552 if (rsignfile
&& !rdb
) {
554 rkeyfile
= rsignfile
;
555 rsigner
= load_cert(bio_err
, rsignfile
, FORMAT_PEM
,
556 NULL
, e
, "responder certificate");
558 BIO_printf(bio_err
, "Error loading responder certificate\n");
561 rca_cert
= load_cert(bio_err
, rca_filename
, FORMAT_PEM
,
562 NULL
, e
, "CA certificate");
564 rother
= load_certs(bio_err
, rcertfile
, FORMAT_PEM
,
565 NULL
, e
, "responder other certificates");
569 rkey
= load_key(bio_err
, rkeyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
570 "responder private key");
575 BIO_printf(bio_err
, "Waiting for OCSP client connections...\n");
580 if (!do_responder(&req
, &cbio
, acbio
, port
))
584 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
,
586 send_ocsp_response(cbio
, resp
);
591 if (!req
&& (signfile
|| reqout
|| host
|| add_nonce
|| ridx_filename
)) {
592 BIO_printf(bio_err
, "Need an OCSP request for this operation!\n");
596 if (req
&& add_nonce
)
597 OCSP_request_add1_nonce(req
, NULL
, -1);
602 signer
= load_cert(bio_err
, signfile
, FORMAT_PEM
,
603 NULL
, e
, "signer certificate");
605 BIO_printf(bio_err
, "Error loading signer certificate\n");
609 sign_other
= load_certs(bio_err
, sign_certfile
, FORMAT_PEM
,
610 NULL
, e
, "signer certificates");
614 key
= load_key(bio_err
, keyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
615 "signer private key");
618 if (!OCSP_request_sign
619 (req
, signer
, key
, EVP_sha1(), sign_other
, sign_flags
)) {
620 BIO_printf(bio_err
, "Error signing OCSP request\n");
626 OCSP_REQUEST_print(out
, req
, 0);
629 derbio
= BIO_new_file(reqout
, "wb");
631 BIO_printf(bio_err
, "Error opening file %s\n", reqout
);
634 i2d_OCSP_REQUEST_bio(derbio
, req
);
638 if (ridx_filename
&& (!rkey
|| !rsigner
|| !rca_cert
)) {
640 "Need a responder certificate, key and CA for this operation!\n");
644 if (ridx_filename
&& !rdb
) {
645 rdb
= load_index(ridx_filename
, NULL
);
648 if (!index_index(rdb
))
653 i
= make_ocsp_response(&resp
, req
, rdb
, rca_cert
, rsigner
, rkey
,
654 rother
, rflags
, nmin
, ndays
);
656 send_ocsp_response(cbio
, resp
);
658 # ifndef OPENSSL_NO_SOCK
659 resp
= process_responder(bio_err
, req
, host
, path
,
660 port
, use_ssl
, req_timeout
);
665 "Error creating connect BIO - sockets not supported.\n");
669 derbio
= BIO_new_file(respin
, "rb");
671 BIO_printf(bio_err
, "Error Opening OCSP response file\n");
674 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
677 BIO_printf(bio_err
, "Error reading OCSP response\n");
689 derbio
= BIO_new_file(respout
, "wb");
691 BIO_printf(bio_err
, "Error opening file %s\n", respout
);
694 i2d_OCSP_RESPONSE_bio(derbio
, resp
);
698 i
= OCSP_response_status(resp
);
700 if (i
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
701 BIO_printf(out
, "Responder Error: %s (%d)\n",
702 OCSP_response_status_str(i
), i
);
710 OCSP_RESPONSE_print(out
, resp
, 0);
712 /* If running as responder don't verify our own response */
714 if (accept_count
> 0)
716 /* Redo if more connections needed */
720 OCSP_REQUEST_free(req
);
722 OCSP_RESPONSE_free(resp
);
730 store
= setup_verify(bio_err
, CAfile
, CApath
);
733 if (verify_certfile
) {
734 verify_other
= load_certs(bio_err
, verify_certfile
, FORMAT_PEM
,
735 NULL
, e
, "validator certificate");
740 bs
= OCSP_response_get1_basic(resp
);
743 BIO_printf(bio_err
, "Error parsing response\n");
748 if (req
&& ((i
= OCSP_check_nonce(req
, bs
)) <= 0)) {
750 BIO_printf(bio_err
, "WARNING: no nonce in response\n");
752 BIO_printf(bio_err
, "Nonce Verify error\n");
757 i
= OCSP_basic_verify(bs
, verify_other
, store
, verify_flags
);
759 i
= OCSP_basic_verify(bs
, NULL
, store
, 0);
762 BIO_printf(bio_err
, "Response Verify Failure\n");
763 ERR_print_errors(bio_err
);
765 BIO_printf(bio_err
, "Response verify OK\n");
769 if (!print_ocsp_summary(out
, bs
, req
, reqnames
, ids
, nsec
, maxage
))
775 ERR_print_errors(bio_err
);
777 X509_STORE_free(store
);
788 OCSP_REQUEST_free(req
);
789 OCSP_RESPONSE_free(resp
);
790 OCSP_BASICRESP_free(bs
);
792 sk_OCSP_CERTID_free(ids
);
793 sk_X509_pop_free(sign_other
, X509_free
);
794 sk_X509_pop_free(verify_other
, X509_free
);
806 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
, X509
*issuer
,
807 STACK_OF(OCSP_CERTID
) *ids
)
811 BIO_printf(bio_err
, "No issuer certificate specified\n");
815 *req
= OCSP_REQUEST_new();
818 id
= OCSP_cert_to_id(NULL
, cert
, issuer
);
819 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
821 if (!OCSP_request_add0_id(*req
, id
))
826 BIO_printf(bio_err
, "Error Creating OCSP request\n");
830 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
, X509
*issuer
,
831 STACK_OF(OCSP_CERTID
) *ids
)
835 ASN1_BIT_STRING
*ikey
;
838 BIO_printf(bio_err
, "No issuer certificate specified\n");
842 *req
= OCSP_REQUEST_new();
845 iname
= X509_get_subject_name(issuer
);
846 ikey
= X509_get0_pubkey_bitstr(issuer
);
847 sno
= s2i_ASN1_INTEGER(NULL
, serial
);
849 BIO_printf(bio_err
, "Error converting serial number %s\n", serial
);
852 id
= OCSP_cert_id_new(EVP_sha1(), iname
, ikey
, sno
);
853 ASN1_INTEGER_free(sno
);
854 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
856 if (!OCSP_request_add0_id(*req
, id
))
861 BIO_printf(bio_err
, "Error Creating OCSP request\n");
865 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
866 STACK
* names
, STACK_OF(OCSP_CERTID
) *ids
,
867 long nsec
, long maxage
)
875 ASN1_GENERALIZEDTIME
*rev
, *thisupd
, *nextupd
;
877 if (!bs
|| !req
|| !sk_num(names
) || !sk_OCSP_CERTID_num(ids
))
880 for (i
= 0; i
< sk_OCSP_CERTID_num(ids
); i
++) {
881 id
= sk_OCSP_CERTID_value(ids
, i
);
882 name
= sk_value(names
, i
);
883 BIO_printf(out
, "%s: ", name
);
885 if (!OCSP_resp_find_status(bs
, id
, &status
, &reason
,
886 &rev
, &thisupd
, &nextupd
)) {
887 BIO_puts(out
, "ERROR: No Status found.\n");
892 * Check validity: if invalid write to output BIO so we know which
893 * response this refers to.
895 if (!OCSP_check_validity(thisupd
, nextupd
, nsec
, maxage
)) {
896 BIO_puts(out
, "WARNING: Status times invalid.\n");
897 ERR_print_errors(out
);
899 BIO_printf(out
, "%s\n", OCSP_cert_status_str(status
));
901 BIO_puts(out
, "\tThis Update: ");
902 ASN1_GENERALIZEDTIME_print(out
, thisupd
);
906 BIO_puts(out
, "\tNext Update: ");
907 ASN1_GENERALIZEDTIME_print(out
, nextupd
);
911 if (status
!= V_OCSP_CERTSTATUS_REVOKED
)
915 BIO_printf(out
, "\tReason: %s\n", OCSP_crl_reason_str(reason
));
917 BIO_puts(out
, "\tRevocation Time: ");
918 ASN1_GENERALIZEDTIME_print(out
, rev
);
925 static int make_ocsp_response(OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
926 CA_DB
*db
, X509
*ca
, X509
*rcert
,
927 EVP_PKEY
*rkey
, STACK_OF(X509
) *rother
,
928 unsigned long flags
, int nmin
, int ndays
)
930 ASN1_TIME
*thisupd
= NULL
, *nextupd
= NULL
;
931 OCSP_CERTID
*cid
, *ca_id
= NULL
;
932 OCSP_BASICRESP
*bs
= NULL
;
933 int i
, id_count
, ret
= 1;
935 id_count
= OCSP_request_onereq_count(req
);
939 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
943 ca_id
= OCSP_cert_to_id(EVP_sha1(), NULL
, ca
);
945 bs
= OCSP_BASICRESP_new();
946 thisupd
= X509_gmtime_adj(NULL
, 0);
948 nextupd
= X509_gmtime_adj(NULL
, nmin
* 60 + ndays
* 3600 * 24);
950 /* Examine each certificate id in the request */
951 for (i
= 0; i
< id_count
; i
++) {
953 ASN1_INTEGER
*serial
;
955 one
= OCSP_request_onereq_get0(req
, i
);
956 cid
= OCSP_onereq_get0_id(one
);
957 /* Is this request about our CA? */
958 if (OCSP_id_issuer_cmp(ca_id
, cid
)) {
959 OCSP_basic_add1_status(bs
, cid
,
960 V_OCSP_CERTSTATUS_UNKNOWN
,
961 0, NULL
, thisupd
, nextupd
);
964 OCSP_id_get0_info(NULL
, NULL
, NULL
, &serial
, cid
);
965 inf
= lookup_serial(db
, serial
);
967 OCSP_basic_add1_status(bs
, cid
,
968 V_OCSP_CERTSTATUS_UNKNOWN
,
969 0, NULL
, thisupd
, nextupd
);
970 else if (inf
[DB_type
][0] == DB_TYPE_VAL
)
971 OCSP_basic_add1_status(bs
, cid
,
972 V_OCSP_CERTSTATUS_GOOD
,
973 0, NULL
, thisupd
, nextupd
);
974 else if (inf
[DB_type
][0] == DB_TYPE_REV
) {
975 ASN1_OBJECT
*inst
= NULL
;
976 ASN1_TIME
*revtm
= NULL
;
977 ASN1_GENERALIZEDTIME
*invtm
= NULL
;
978 OCSP_SINGLERESP
*single
;
980 unpack_revinfo(&revtm
, &reason
, &inst
, &invtm
, inf
[DB_rev_date
]);
981 single
= OCSP_basic_add1_status(bs
, cid
,
982 V_OCSP_CERTSTATUS_REVOKED
,
983 reason
, revtm
, thisupd
, nextupd
);
985 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_invalidity_date
,
988 OCSP_SINGLERESP_add1_ext_i2d(single
,
989 NID_hold_instruction_code
, inst
,
991 ASN1_OBJECT_free(inst
);
992 ASN1_TIME_free(revtm
);
993 ASN1_GENERALIZEDTIME_free(invtm
);
997 OCSP_copy_nonce(bs
, req
);
999 OCSP_basic_sign(bs
, rcert
, rkey
, EVP_sha1(), rother
, flags
);
1001 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL
, bs
);
1004 ASN1_TIME_free(thisupd
);
1005 ASN1_TIME_free(nextupd
);
1006 OCSP_CERTID_free(ca_id
);
1007 OCSP_BASICRESP_free(bs
);
1012 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
)
1016 char *itmp
, *row
[DB_NUMBER
], **rrow
;
1017 for (i
= 0; i
< DB_NUMBER
; i
++)
1019 bn
= ASN1_INTEGER_to_BN(ser
, NULL
);
1020 OPENSSL_assert(bn
); /* FIXME: should report an error at this
1021 * point and abort */
1023 itmp
= BUF_strdup("00");
1025 itmp
= BN_bn2hex(bn
);
1026 row
[DB_serial
] = itmp
;
1028 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1033 /* Quick and dirty OCSP server: read in and parse input request */
1035 static BIO
*init_responder(char *port
)
1037 BIO
*acbio
= NULL
, *bufbio
= NULL
;
1038 bufbio
= BIO_new(BIO_f_buffer());
1041 # ifndef OPENSSL_NO_SOCK
1042 acbio
= BIO_new_accept(port
);
1045 "Error setting up accept BIO - sockets not supported.\n");
1049 BIO_set_accept_bios(acbio
, bufbio
);
1052 if (BIO_do_accept(acbio
) <= 0) {
1053 BIO_printf(bio_err
, "Error setting up accept BIO\n");
1054 ERR_print_errors(bio_err
);
1061 BIO_free_all(acbio
);
1066 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
1069 int have_post
= 0, len
;
1070 OCSP_REQUEST
*req
= NULL
;
1074 if (BIO_do_accept(acbio
) <= 0) {
1075 BIO_printf(bio_err
, "Error accepting connection\n");
1076 ERR_print_errors(bio_err
);
1080 cbio
= BIO_pop(acbio
);
1084 len
= BIO_gets(cbio
, inbuf
, sizeof inbuf
);
1087 /* Look for "POST" signalling start of query */
1089 if (strncmp(inbuf
, "POST", 4)) {
1090 BIO_printf(bio_err
, "Invalid request\n");
1095 /* Look for end of headers */
1096 if ((inbuf
[0] == '\r') || (inbuf
[0] == '\n'))
1100 /* Try to read OCSP request */
1102 req
= d2i_OCSP_REQUEST_bio(cbio
, NULL
);
1105 BIO_printf(bio_err
, "Error parsing OCSP request\n");
1106 ERR_print_errors(bio_err
);
1115 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
)
1118 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1119 "Content-Length: %d\r\n\r\n";
1122 BIO_printf(cbio
, http_resp
, i2d_OCSP_RESPONSE(resp
, NULL
));
1123 i2d_OCSP_RESPONSE_bio(cbio
, resp
);
1124 (void)BIO_flush(cbio
);
1128 static OCSP_RESPONSE
*query_responder(BIO
*err
, BIO
*cbio
, char *path
,
1129 OCSP_REQUEST
*req
, int req_timeout
)
1133 OCSP_REQ_CTX
*ctx
= NULL
;
1134 OCSP_RESPONSE
*rsp
= NULL
;
1138 if (req_timeout
!= -1)
1139 BIO_set_nbio(cbio
, 1);
1141 rv
= BIO_do_connect(cbio
);
1143 if ((rv
<= 0) && ((req_timeout
== -1) || !BIO_should_retry(cbio
))) {
1144 BIO_puts(err
, "Error connecting BIO\n");
1148 if (req_timeout
== -1)
1149 return OCSP_sendreq_bio(cbio
, path
, req
);
1151 if (BIO_get_fd(cbio
, &fd
) <= 0) {
1152 BIO_puts(err
, "Can't get connection fd\n");
1158 openssl_fdset(fd
, &confds
);
1160 tv
.tv_sec
= req_timeout
;
1161 rv
= select(fd
+ 1, NULL
, (void *)&confds
, NULL
, &tv
);
1163 BIO_puts(err
, "Timeout on connect\n");
1168 ctx
= OCSP_sendreq_new(cbio
, path
, req
, -1);
1173 rv
= OCSP_sendreq_nbio(&rsp
, ctx
);
1177 openssl_fdset(fd
, &confds
);
1179 tv
.tv_sec
= req_timeout
;
1180 if (BIO_should_read(cbio
))
1181 rv
= select(fd
+ 1, (void *)&confds
, NULL
, NULL
, &tv
);
1182 else if (BIO_should_write(cbio
))
1183 rv
= select(fd
+ 1, NULL
, (void *)&confds
, NULL
, &tv
);
1185 BIO_puts(err
, "Unexpected retry condition\n");
1189 BIO_puts(err
, "Timeout on request\n");
1193 BIO_puts(err
, "Select error\n");
1200 OCSP_REQ_CTX_free(ctx
);
1205 OCSP_RESPONSE
*process_responder(BIO
*err
, OCSP_REQUEST
*req
,
1206 char *host
, char *path
, char *port
,
1207 int use_ssl
, int req_timeout
)
1210 SSL_CTX
*ctx
= NULL
;
1211 OCSP_RESPONSE
*resp
= NULL
;
1212 cbio
= BIO_new_connect(host
);
1214 BIO_printf(err
, "Error creating connect BIO\n");
1218 BIO_set_conn_port(cbio
, port
);
1221 # if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
1222 ctx
= SSL_CTX_new(SSLv23_client_method());
1223 # elif !defined(OPENSSL_NO_SSL3)
1224 ctx
= SSL_CTX_new(SSLv3_client_method());
1225 # elif !defined(OPENSSL_NO_SSL2)
1226 ctx
= SSL_CTX_new(SSLv2_client_method());
1228 BIO_printf(err
, "SSL is disabled\n");
1232 BIO_printf(err
, "Error creating SSL context.\n");
1235 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1236 sbio
= BIO_new_ssl(ctx
, 1);
1237 cbio
= BIO_push(sbio
, cbio
);
1239 resp
= query_responder(err
, cbio
, path
, req
, req_timeout
);
1241 BIO_printf(bio_err
, "Error querying OCSP responder\n");