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
61 # ifdef OPENSSL_SYS_VMS
62 # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
72 # include "apps.h" /* needs to be included before the openssl
74 # include <openssl/e_os2.h>
75 # include <openssl/crypto.h>
76 # include <openssl/err.h>
77 # include <openssl/ssl.h>
78 # include <openssl/evp.h>
79 # include <openssl/bn.h>
80 # include <openssl/x509v3.h>
82 # if defined(NETWARE_CLIB)
83 # ifdef NETWARE_BSDSOCK
84 # include <sys/socket.h>
85 # include <sys/bsdskt.h>
87 # include <novsock2.h>
89 # elif defined(NETWARE_LIBC)
90 # ifdef NETWARE_BSDSOCK
91 # include <sys/select.h>
93 # include <novsock2.h>
97 /* Maximum leeway in validity period: default 5 minutes */
98 # define MAX_VALIDITY_PERIOD (5 * 60)
100 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
101 const EVP_MD
*cert_id_md
, X509
*issuer
,
102 STACK_OF(OCSP_CERTID
) *ids
);
103 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
104 const EVP_MD
*cert_id_md
, X509
*issuer
,
105 STACK_OF(OCSP_CERTID
) *ids
);
106 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
107 STACK_OF(OPENSSL_STRING
) *names
,
108 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
111 static int make_ocsp_response(OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
112 CA_DB
*db
, X509
*ca
, X509
*rcert
,
113 EVP_PKEY
*rkey
, STACK_OF(X509
) *rother
,
114 unsigned long flags
, int nmin
, int ndays
);
116 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
);
117 static BIO
*init_responder(char *port
);
118 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
120 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
);
121 static OCSP_RESPONSE
*query_responder(BIO
*err
, BIO
*cbio
, char *path
,
122 STACK_OF(CONF_VALUE
) *headers
,
123 OCSP_REQUEST
*req
, int req_timeout
);
126 # define PROG ocsp_main
128 int MAIN(int, char **);
130 int MAIN(int argc
, char **argv
)
134 char *host
= NULL
, *port
= NULL
, *path
= "/";
135 char *thost
= NULL
, *tport
= NULL
, *tpath
= NULL
;
136 char *reqin
= NULL
, *respin
= NULL
;
137 char *reqout
= NULL
, *respout
= NULL
;
138 char *signfile
= NULL
, *keyfile
= NULL
;
139 char *rsignfile
= NULL
, *rkeyfile
= NULL
;
140 char *outfile
= NULL
;
141 int add_nonce
= 1, noverify
= 0, use_ssl
= -1;
142 STACK_OF(CONF_VALUE
) *headers
= NULL
;
143 OCSP_REQUEST
*req
= NULL
;
144 OCSP_RESPONSE
*resp
= NULL
;
145 OCSP_BASICRESP
*bs
= NULL
;
146 X509
*issuer
= NULL
, *cert
= NULL
;
147 X509
*signer
= NULL
, *rsigner
= NULL
;
148 EVP_PKEY
*key
= NULL
, *rkey
= NULL
;
149 BIO
*acbio
= NULL
, *cbio
= NULL
;
152 int req_timeout
= -1;
153 int req_text
= 0, resp_text
= 0;
154 long nsec
= MAX_VALIDITY_PERIOD
, maxage
= -1;
155 char *CAfile
= NULL
, *CApath
= NULL
;
156 X509_STORE
*store
= NULL
;
157 STACK_OF(X509
) *sign_other
= NULL
, *verify_other
= NULL
, *rother
= NULL
;
158 char *sign_certfile
= NULL
, *verify_certfile
= NULL
, *rcertfile
= NULL
;
159 unsigned long sign_flags
= 0, verify_flags
= 0, rflags
= 0;
161 int accept_count
= -1;
165 STACK_OF(OPENSSL_STRING
) *reqnames
= NULL
;
166 STACK_OF(OCSP_CERTID
) *ids
= NULL
;
168 X509
*rca_cert
= NULL
;
169 char *ridx_filename
= NULL
;
170 char *rca_filename
= NULL
;
172 int nmin
= 0, ndays
= -1;
173 const EVP_MD
*cert_id_md
= NULL
;
176 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
178 if (!load_config(bio_err
, NULL
))
180 SSL_load_error_strings();
181 OpenSSL_add_ssl_algorithms();
183 reqnames
= sk_OPENSSL_STRING_new_null();
184 ids
= sk_OCSP_CERTID_new_null();
185 while (!badarg
&& *args
&& *args
[0] == '-') {
186 if (!strcmp(*args
, "-out")) {
192 } else if (!strcmp(*args
, "-timeout")) {
195 req_timeout
= atol(*args
);
196 if (req_timeout
< 0) {
197 BIO_printf(bio_err
, "Illegal timeout value %s\n", *args
);
202 } else if (!strcmp(*args
, "-url")) {
211 if (!OCSP_parse_url(*args
, &host
, &port
, &path
, &use_ssl
)) {
212 BIO_printf(bio_err
, "Error parsing URL\n");
220 } else if (!strcmp(*args
, "-host")) {
226 } else if (!strcmp(*args
, "-port")) {
229 port
= BUF_strdup(*args
);
232 } else if (!strcmp(*args
, "-header")) {
233 if (args
[1] && args
[2]) {
234 if (!X509V3_add_value(args
[1], args
[2], &headers
))
239 } else if (!strcmp(*args
, "-ignore_err"))
241 else if (!strcmp(*args
, "-noverify"))
243 else if (!strcmp(*args
, "-nonce"))
245 else if (!strcmp(*args
, "-no_nonce"))
247 else if (!strcmp(*args
, "-resp_no_certs"))
248 rflags
|= OCSP_NOCERTS
;
249 else if (!strcmp(*args
, "-resp_key_id"))
250 rflags
|= OCSP_RESPID_KEY
;
251 else if (!strcmp(*args
, "-no_certs"))
252 sign_flags
|= OCSP_NOCERTS
;
253 else if (!strcmp(*args
, "-no_signature_verify"))
254 verify_flags
|= OCSP_NOSIGS
;
255 else if (!strcmp(*args
, "-no_cert_verify"))
256 verify_flags
|= OCSP_NOVERIFY
;
257 else if (!strcmp(*args
, "-no_chain"))
258 verify_flags
|= OCSP_NOCHAIN
;
259 else if (!strcmp(*args
, "-no_cert_checks"))
260 verify_flags
|= OCSP_NOCHECKS
;
261 else if (!strcmp(*args
, "-no_explicit"))
262 verify_flags
|= OCSP_NOEXPLICIT
;
263 else if (!strcmp(*args
, "-trust_other"))
264 verify_flags
|= OCSP_TRUSTOTHER
;
265 else if (!strcmp(*args
, "-no_intern"))
266 verify_flags
|= OCSP_NOINTERN
;
267 else if (!strcmp(*args
, "-text")) {
270 } else if (!strcmp(*args
, "-req_text"))
272 else if (!strcmp(*args
, "-resp_text"))
274 else if (!strcmp(*args
, "-reqin")) {
280 } else if (!strcmp(*args
, "-respin")) {
286 } else if (!strcmp(*args
, "-signer")) {
292 } else if (!strcmp(*args
, "-VAfile")) {
295 verify_certfile
= *args
;
296 verify_flags
|= OCSP_TRUSTOTHER
;
299 } else if (!strcmp(*args
, "-sign_other")) {
302 sign_certfile
= *args
;
305 } else if (!strcmp(*args
, "-verify_other")) {
308 verify_certfile
= *args
;
311 } else if (!strcmp(*args
, "-CAfile")) {
317 } else if (!strcmp(*args
, "-CApath")) {
323 } else if (!strcmp(*args
, "-validity_period")) {
329 "Illegal validity period %s\n", *args
);
334 } else if (!strcmp(*args
, "-status_age")) {
337 maxage
= atol(*args
);
339 BIO_printf(bio_err
, "Illegal validity age %s\n", *args
);
344 } else if (!strcmp(*args
, "-signkey")) {
350 } else if (!strcmp(*args
, "-reqout")) {
356 } else if (!strcmp(*args
, "-respout")) {
362 } else if (!strcmp(*args
, "-path")) {
368 } else if (!strcmp(*args
, "-issuer")) {
372 issuer
= load_cert(bio_err
, *args
, FORMAT_PEM
,
373 NULL
, e
, "issuer certificate");
378 } else if (!strcmp(*args
, "-cert")) {
382 cert
= load_cert(bio_err
, *args
, FORMAT_PEM
,
383 NULL
, e
, "certificate");
387 cert_id_md
= EVP_sha1();
388 if (!add_ocsp_cert(&req
, cert
, cert_id_md
, issuer
, ids
))
390 if (!sk_OPENSSL_STRING_push(reqnames
, *args
))
394 } else if (!strcmp(*args
, "-serial")) {
398 cert_id_md
= EVP_sha1();
399 if (!add_ocsp_serial(&req
, *args
, cert_id_md
, issuer
, ids
))
401 if (!sk_OPENSSL_STRING_push(reqnames
, *args
))
405 } else if (!strcmp(*args
, "-index")) {
408 ridx_filename
= *args
;
411 } else if (!strcmp(*args
, "-CA")) {
414 rca_filename
= *args
;
417 } else if (!strcmp(*args
, "-nmin")) {
422 BIO_printf(bio_err
, "Illegal update period %s\n", *args
);
430 } else if (!strcmp(*args
, "-nrequest")) {
433 accept_count
= atol(*args
);
434 if (accept_count
< 0) {
435 BIO_printf(bio_err
, "Illegal accept count %s\n", *args
);
440 } else if (!strcmp(*args
, "-ndays")) {
445 BIO_printf(bio_err
, "Illegal update period %s\n", *args
);
450 } else if (!strcmp(*args
, "-rsigner")) {
456 } else if (!strcmp(*args
, "-rkey")) {
462 } else if (!strcmp(*args
, "-rother")) {
468 } else if ((cert_id_md
= EVP_get_digestbyname((*args
) + 1)) == NULL
) {
474 /* Have we anything to do? */
475 if (!req
&& !reqin
&& !respin
&& !(port
&& ridx_filename
))
479 BIO_printf(bio_err
, "OCSP utility\n");
480 BIO_printf(bio_err
, "Usage ocsp [options]\n");
481 BIO_printf(bio_err
, "where options are\n");
482 BIO_printf(bio_err
, "-out file output filename\n");
483 BIO_printf(bio_err
, "-issuer file issuer certificate\n");
484 BIO_printf(bio_err
, "-cert file certificate to check\n");
485 BIO_printf(bio_err
, "-serial n serial number to check\n");
487 "-signer file certificate to sign OCSP request with\n");
489 "-signkey file private key to sign OCSP request with\n");
491 "-sign_other file additional certificates to include in signed request\n");
493 "-no_certs don't include any certificates in signed request\n");
495 "-req_text print text form of request\n");
497 "-resp_text print text form of response\n");
499 "-text print text form of request and response\n");
501 "-reqout file write DER encoded OCSP request to \"file\"\n");
503 "-respout file write DER encoded OCSP reponse to \"file\"\n");
505 "-reqin file read DER encoded OCSP request from \"file\"\n");
507 "-respin file read DER encoded OCSP reponse from \"file\"\n");
509 "-nonce add OCSP nonce to request\n");
511 "-no_nonce don't add OCSP nonce to request\n");
512 BIO_printf(bio_err
, "-url URL OCSP responder URL\n");
514 "-host host:n send OCSP request to host on port n\n");
516 "-path path to use in OCSP request\n");
518 "-CApath dir trusted certificates directory\n");
520 "-CAfile file trusted certificates file\n");
522 "-no_alt_chains only ever use the first certificate chain found\n");
524 "-VAfile file validator certificates file\n");
526 "-validity_period n maximum validity discrepancy in seconds\n");
528 "-status_age n maximum status age in seconds\n");
530 "-noverify don't verify response at all\n");
532 "-verify_other file additional certificates to search for signer\n");
534 "-trust_other don't verify additional certificates\n");
536 "-no_intern don't search certificates contained in response for signer\n");
538 "-no_signature_verify don't check signature on response\n");
540 "-no_cert_verify don't check signing certificate\n");
542 "-no_chain don't chain verify response\n");
544 "-no_cert_checks don't do additional checks on signing certificate\n");
546 "-port num port to run responder on\n");
548 "-index file certificate status index file\n");
549 BIO_printf(bio_err
, "-CA file CA certificate\n");
551 "-rsigner file responder certificate to sign responses with\n");
553 "-rkey file responder key to sign responses with\n");
555 "-rother file other certificates to include in response\n");
557 "-resp_no_certs don't include any certificates in response\n");
559 "-nmin n number of minutes before next update\n");
561 "-ndays n number of days before next update\n");
563 "-resp_key_id identify reponse by signing certificate key ID\n");
565 "-nrequest n number of requests to accept (default unlimited)\n");
567 "-<dgst alg> use specified digest in the request\n");
569 "-timeout n timeout connection to OCSP responder after n seconds\n");
574 out
= BIO_new_file(outfile
, "w");
576 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
579 BIO_printf(bio_err
, "Error opening output file\n");
583 if (!req
&& (add_nonce
!= 2))
587 derbio
= BIO_new_file(reqin
, "rb");
589 BIO_printf(bio_err
, "Error Opening OCSP request file\n");
592 req
= d2i_OCSP_REQUEST_bio(derbio
, NULL
);
595 BIO_printf(bio_err
, "Error reading OCSP request\n");
601 acbio
= init_responder(port
);
606 if (rsignfile
&& !rdb
) {
608 rkeyfile
= rsignfile
;
609 rsigner
= load_cert(bio_err
, rsignfile
, FORMAT_PEM
,
610 NULL
, e
, "responder certificate");
612 BIO_printf(bio_err
, "Error loading responder certificate\n");
615 rca_cert
= load_cert(bio_err
, rca_filename
, FORMAT_PEM
,
616 NULL
, e
, "CA certificate");
618 rother
= load_certs(bio_err
, rcertfile
, FORMAT_PEM
,
619 NULL
, e
, "responder other certificates");
623 rkey
= load_key(bio_err
, rkeyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
624 "responder private key");
629 BIO_printf(bio_err
, "Waiting for OCSP client connections...\n");
634 if (!do_responder(&req
, &cbio
, acbio
, port
))
638 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
,
640 send_ocsp_response(cbio
, resp
);
645 if (!req
&& (signfile
|| reqout
|| host
|| add_nonce
|| ridx_filename
)) {
646 BIO_printf(bio_err
, "Need an OCSP request for this operation!\n");
650 if (req
&& add_nonce
)
651 OCSP_request_add1_nonce(req
, NULL
, -1);
656 signer
= load_cert(bio_err
, signfile
, FORMAT_PEM
,
657 NULL
, e
, "signer certificate");
659 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");
668 key
= load_key(bio_err
, keyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
669 "signer private key");
673 if (!OCSP_request_sign
674 (req
, signer
, key
, NULL
, sign_other
, sign_flags
)) {
675 BIO_printf(bio_err
, "Error signing OCSP request\n");
681 OCSP_REQUEST_print(out
, req
, 0);
684 derbio
= BIO_new_file(reqout
, "wb");
686 BIO_printf(bio_err
, "Error opening file %s\n", reqout
);
689 i2d_OCSP_REQUEST_bio(derbio
, req
);
693 if (ridx_filename
&& (!rkey
|| !rsigner
|| !rca_cert
)) {
695 "Need a responder certificate, key and CA for this operation!\n");
699 if (ridx_filename
&& !rdb
) {
700 rdb
= load_index(ridx_filename
, NULL
);
703 if (!index_index(rdb
))
708 i
= make_ocsp_response(&resp
, req
, rdb
, rca_cert
, rsigner
, rkey
,
709 rother
, rflags
, nmin
, ndays
);
711 send_ocsp_response(cbio
, resp
);
713 # ifndef OPENSSL_NO_SOCK
714 resp
= process_responder(bio_err
, req
, host
, path
,
715 port
, use_ssl
, headers
, req_timeout
);
720 "Error creating connect BIO - sockets not supported.\n");
724 derbio
= BIO_new_file(respin
, "rb");
726 BIO_printf(bio_err
, "Error Opening OCSP response file\n");
729 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
732 BIO_printf(bio_err
, "Error reading OCSP response\n");
744 derbio
= BIO_new_file(respout
, "wb");
746 BIO_printf(bio_err
, "Error opening file %s\n", respout
);
749 i2d_OCSP_RESPONSE_bio(derbio
, resp
);
753 i
= OCSP_response_status(resp
);
755 if (i
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
756 BIO_printf(out
, "Responder Error: %s (%d)\n",
757 OCSP_response_status_str(i
), i
);
765 OCSP_RESPONSE_print(out
, resp
, 0);
767 /* If running as responder don't verify our own response */
769 if (accept_count
> 0)
771 /* Redo if more connections needed */
775 OCSP_REQUEST_free(req
);
777 OCSP_RESPONSE_free(resp
);
785 store
= setup_verify(bio_err
, CAfile
, CApath
);
788 if (verify_certfile
) {
789 verify_other
= load_certs(bio_err
, verify_certfile
, FORMAT_PEM
,
790 NULL
, e
, "validator certificate");
795 bs
= OCSP_response_get1_basic(resp
);
798 BIO_printf(bio_err
, "Error parsing response\n");
803 if (req
&& ((i
= OCSP_check_nonce(req
, bs
)) <= 0)) {
805 BIO_printf(bio_err
, "WARNING: no nonce in response\n");
807 BIO_printf(bio_err
, "Nonce Verify error\n");
812 i
= OCSP_basic_verify(bs
, verify_other
, store
, verify_flags
);
814 i
= OCSP_basic_verify(bs
, NULL
, store
, 0);
817 BIO_printf(bio_err
, "Response Verify Failure\n");
818 ERR_print_errors(bio_err
);
820 BIO_printf(bio_err
, "Response verify OK\n");
824 if (!print_ocsp_summary(out
, bs
, req
, reqnames
, ids
, nsec
, maxage
))
830 ERR_print_errors(bio_err
);
832 X509_STORE_free(store
);
843 OCSP_REQUEST_free(req
);
844 OCSP_RESPONSE_free(resp
);
845 OCSP_BASICRESP_free(bs
);
846 sk_OPENSSL_STRING_free(reqnames
);
847 sk_OCSP_CERTID_free(ids
);
848 sk_X509_pop_free(sign_other
, X509_free
);
849 sk_X509_pop_free(verify_other
, X509_free
);
850 sk_CONF_VALUE_pop_free(headers
, X509V3_conf_free
);
862 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
863 const EVP_MD
*cert_id_md
, X509
*issuer
,
864 STACK_OF(OCSP_CERTID
) *ids
)
868 BIO_printf(bio_err
, "No issuer certificate specified\n");
872 *req
= OCSP_REQUEST_new();
875 id
= OCSP_cert_to_id(cert_id_md
, cert
, issuer
);
876 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
878 if (!OCSP_request_add0_id(*req
, id
))
883 BIO_printf(bio_err
, "Error Creating OCSP request\n");
887 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
888 const EVP_MD
*cert_id_md
, X509
*issuer
,
889 STACK_OF(OCSP_CERTID
) *ids
)
893 ASN1_BIT_STRING
*ikey
;
896 BIO_printf(bio_err
, "No issuer certificate specified\n");
900 *req
= OCSP_REQUEST_new();
903 iname
= X509_get_subject_name(issuer
);
904 ikey
= X509_get0_pubkey_bitstr(issuer
);
905 sno
= s2i_ASN1_INTEGER(NULL
, serial
);
907 BIO_printf(bio_err
, "Error converting serial number %s\n", serial
);
910 id
= OCSP_cert_id_new(cert_id_md
, iname
, ikey
, sno
);
911 ASN1_INTEGER_free(sno
);
912 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
914 if (!OCSP_request_add0_id(*req
, id
))
919 BIO_printf(bio_err
, "Error Creating OCSP request\n");
923 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
924 STACK_OF(OPENSSL_STRING
) *names
,
925 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
934 ASN1_GENERALIZEDTIME
*rev
, *thisupd
, *nextupd
;
936 if (!bs
|| !req
|| !sk_OPENSSL_STRING_num(names
)
937 || !sk_OCSP_CERTID_num(ids
))
940 for (i
= 0; i
< sk_OCSP_CERTID_num(ids
); i
++) {
941 id
= sk_OCSP_CERTID_value(ids
, i
);
942 name
= sk_OPENSSL_STRING_value(names
, i
);
943 BIO_printf(out
, "%s: ", name
);
945 if (!OCSP_resp_find_status(bs
, id
, &status
, &reason
,
946 &rev
, &thisupd
, &nextupd
)) {
947 BIO_puts(out
, "ERROR: No Status found.\n");
952 * Check validity: if invalid write to output BIO so we know which
953 * response this refers to.
955 if (!OCSP_check_validity(thisupd
, nextupd
, nsec
, maxage
)) {
956 BIO_puts(out
, "WARNING: Status times invalid.\n");
957 ERR_print_errors(out
);
959 BIO_printf(out
, "%s\n", OCSP_cert_status_str(status
));
961 BIO_puts(out
, "\tThis Update: ");
962 ASN1_GENERALIZEDTIME_print(out
, thisupd
);
966 BIO_puts(out
, "\tNext Update: ");
967 ASN1_GENERALIZEDTIME_print(out
, nextupd
);
971 if (status
!= V_OCSP_CERTSTATUS_REVOKED
)
975 BIO_printf(out
, "\tReason: %s\n", OCSP_crl_reason_str(reason
));
977 BIO_puts(out
, "\tRevocation Time: ");
978 ASN1_GENERALIZEDTIME_print(out
, rev
);
985 static int make_ocsp_response(OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
986 CA_DB
*db
, X509
*ca
, X509
*rcert
,
987 EVP_PKEY
*rkey
, STACK_OF(X509
) *rother
,
988 unsigned long flags
, int nmin
, int ndays
)
990 ASN1_TIME
*thisupd
= NULL
, *nextupd
= NULL
;
991 OCSP_CERTID
*cid
, *ca_id
= NULL
;
992 OCSP_BASICRESP
*bs
= NULL
;
993 int i
, id_count
, ret
= 1;
995 id_count
= OCSP_request_onereq_count(req
);
999 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
1003 bs
= OCSP_BASICRESP_new();
1004 thisupd
= X509_gmtime_adj(NULL
, 0);
1006 nextupd
= X509_gmtime_adj(NULL
, nmin
* 60 + ndays
* 3600 * 24);
1008 /* Examine each certificate id in the request */
1009 for (i
= 0; i
< id_count
; i
++) {
1011 ASN1_INTEGER
*serial
;
1013 ASN1_OBJECT
*cert_id_md_oid
;
1014 const EVP_MD
*cert_id_md
;
1015 one
= OCSP_request_onereq_get0(req
, i
);
1016 cid
= OCSP_onereq_get0_id(one
);
1018 OCSP_id_get0_info(NULL
, &cert_id_md_oid
, NULL
, NULL
, cid
);
1020 cert_id_md
= EVP_get_digestbyobj(cert_id_md_oid
);
1022 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1027 OCSP_CERTID_free(ca_id
);
1028 ca_id
= OCSP_cert_to_id(cert_id_md
, NULL
, ca
);
1030 /* Is this request about our CA? */
1031 if (OCSP_id_issuer_cmp(ca_id
, cid
)) {
1032 OCSP_basic_add1_status(bs
, cid
,
1033 V_OCSP_CERTSTATUS_UNKNOWN
,
1034 0, NULL
, thisupd
, nextupd
);
1037 OCSP_id_get0_info(NULL
, NULL
, NULL
, &serial
, cid
);
1038 inf
= lookup_serial(db
, serial
);
1040 OCSP_basic_add1_status(bs
, cid
,
1041 V_OCSP_CERTSTATUS_UNKNOWN
,
1042 0, NULL
, thisupd
, nextupd
);
1043 else if (inf
[DB_type
][0] == DB_TYPE_VAL
)
1044 OCSP_basic_add1_status(bs
, cid
,
1045 V_OCSP_CERTSTATUS_GOOD
,
1046 0, NULL
, thisupd
, nextupd
);
1047 else if (inf
[DB_type
][0] == DB_TYPE_REV
) {
1048 ASN1_OBJECT
*inst
= NULL
;
1049 ASN1_TIME
*revtm
= NULL
;
1050 ASN1_GENERALIZEDTIME
*invtm
= NULL
;
1051 OCSP_SINGLERESP
*single
;
1053 unpack_revinfo(&revtm
, &reason
, &inst
, &invtm
, inf
[DB_rev_date
]);
1054 single
= OCSP_basic_add1_status(bs
, cid
,
1055 V_OCSP_CERTSTATUS_REVOKED
,
1056 reason
, revtm
, thisupd
, nextupd
);
1058 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_invalidity_date
,
1061 OCSP_SINGLERESP_add1_ext_i2d(single
,
1062 NID_hold_instruction_code
, inst
,
1064 ASN1_OBJECT_free(inst
);
1065 ASN1_TIME_free(revtm
);
1066 ASN1_GENERALIZEDTIME_free(invtm
);
1070 OCSP_copy_nonce(bs
, req
);
1072 OCSP_basic_sign(bs
, rcert
, rkey
, NULL
, rother
, flags
);
1074 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL
, bs
);
1077 ASN1_TIME_free(thisupd
);
1078 ASN1_TIME_free(nextupd
);
1079 OCSP_CERTID_free(ca_id
);
1080 OCSP_BASICRESP_free(bs
);
1085 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
)
1089 char *itmp
, *row
[DB_NUMBER
], **rrow
;
1090 for (i
= 0; i
< DB_NUMBER
; i
++)
1092 bn
= ASN1_INTEGER_to_BN(ser
, NULL
);
1093 OPENSSL_assert(bn
); /* FIXME: should report an error at this
1094 * point and abort */
1096 itmp
= BUF_strdup("00");
1098 itmp
= BN_bn2hex(bn
);
1099 row
[DB_serial
] = itmp
;
1101 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1106 /* Quick and dirty OCSP server: read in and parse input request */
1108 static BIO
*init_responder(char *port
)
1110 BIO
*acbio
= NULL
, *bufbio
= NULL
;
1111 bufbio
= BIO_new(BIO_f_buffer());
1114 # ifndef OPENSSL_NO_SOCK
1115 acbio
= BIO_new_accept(port
);
1118 "Error setting up accept BIO - sockets not supported.\n");
1122 BIO_set_accept_bios(acbio
, bufbio
);
1125 if (BIO_do_accept(acbio
) <= 0) {
1126 BIO_printf(bio_err
, "Error setting up accept BIO\n");
1127 ERR_print_errors(bio_err
);
1134 BIO_free_all(acbio
);
1139 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
1142 int have_post
= 0, len
;
1143 OCSP_REQUEST
*req
= NULL
;
1147 if (BIO_do_accept(acbio
) <= 0) {
1148 BIO_printf(bio_err
, "Error accepting connection\n");
1149 ERR_print_errors(bio_err
);
1153 cbio
= BIO_pop(acbio
);
1157 len
= BIO_gets(cbio
, inbuf
, sizeof inbuf
);
1160 /* Look for "POST" signalling start of query */
1162 if (strncmp(inbuf
, "POST", 4)) {
1163 BIO_printf(bio_err
, "Invalid request\n");
1168 /* Look for end of headers */
1169 if ((inbuf
[0] == '\r') || (inbuf
[0] == '\n'))
1173 /* Try to read OCSP request */
1175 req
= d2i_OCSP_REQUEST_bio(cbio
, NULL
);
1178 BIO_printf(bio_err
, "Error parsing OCSP request\n");
1179 ERR_print_errors(bio_err
);
1188 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
)
1191 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1192 "Content-Length: %d\r\n\r\n";
1195 BIO_printf(cbio
, http_resp
, i2d_OCSP_RESPONSE(resp
, NULL
));
1196 i2d_OCSP_RESPONSE_bio(cbio
, resp
);
1197 (void)BIO_flush(cbio
);
1201 static OCSP_RESPONSE
*query_responder(BIO
*err
, BIO
*cbio
, char *path
,
1202 STACK_OF(CONF_VALUE
) *headers
,
1203 OCSP_REQUEST
*req
, int req_timeout
)
1208 OCSP_REQ_CTX
*ctx
= NULL
;
1209 OCSP_RESPONSE
*rsp
= NULL
;
1213 if (req_timeout
!= -1)
1214 BIO_set_nbio(cbio
, 1);
1216 rv
= BIO_do_connect(cbio
);
1218 if ((rv
<= 0) && ((req_timeout
== -1) || !BIO_should_retry(cbio
))) {
1219 BIO_puts(err
, "Error connecting BIO\n");
1223 if (BIO_get_fd(cbio
, &fd
) <= 0) {
1224 BIO_puts(err
, "Can't get connection fd\n");
1228 if (req_timeout
!= -1 && rv
<= 0) {
1230 openssl_fdset(fd
, &confds
);
1232 tv
.tv_sec
= req_timeout
;
1233 rv
= select(fd
+ 1, NULL
, (void *)&confds
, NULL
, &tv
);
1235 BIO_puts(err
, "Timeout on connect\n");
1240 ctx
= OCSP_sendreq_new(cbio
, path
, NULL
, -1);
1244 for (i
= 0; i
< sk_CONF_VALUE_num(headers
); i
++) {
1245 CONF_VALUE
*hdr
= sk_CONF_VALUE_value(headers
, i
);
1246 if (!OCSP_REQ_CTX_add1_header(ctx
, hdr
->name
, hdr
->value
))
1250 if (!OCSP_REQ_CTX_set1_req(ctx
, req
))
1254 rv
= OCSP_sendreq_nbio(&rsp
, ctx
);
1257 if (req_timeout
== -1)
1260 openssl_fdset(fd
, &confds
);
1262 tv
.tv_sec
= req_timeout
;
1263 if (BIO_should_read(cbio
))
1264 rv
= select(fd
+ 1, (void *)&confds
, NULL
, NULL
, &tv
);
1265 else if (BIO_should_write(cbio
))
1266 rv
= select(fd
+ 1, NULL
, (void *)&confds
, NULL
, &tv
);
1268 BIO_puts(err
, "Unexpected retry condition\n");
1272 BIO_puts(err
, "Timeout on request\n");
1276 BIO_puts(err
, "Select error\n");
1283 OCSP_REQ_CTX_free(ctx
);
1288 OCSP_RESPONSE
*process_responder(BIO
*err
, OCSP_REQUEST
*req
,
1289 char *host
, char *path
, char *port
,
1290 int use_ssl
, STACK_OF(CONF_VALUE
) *headers
,
1294 SSL_CTX
*ctx
= NULL
;
1295 OCSP_RESPONSE
*resp
= NULL
;
1296 cbio
= BIO_new_connect(host
);
1298 BIO_printf(err
, "Error creating connect BIO\n");
1302 BIO_set_conn_port(cbio
, port
);
1305 ctx
= SSL_CTX_new(SSLv23_client_method());
1307 BIO_printf(err
, "Error creating SSL context.\n");
1310 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1311 sbio
= BIO_new_ssl(ctx
, 1);
1312 cbio
= BIO_push(sbio
, cbio
);
1314 resp
= query_responder(err
, cbio
, path
, headers
, req
, req_timeout
);
1316 BIO_printf(bio_err
, "Error querying OCSP responder\n");