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
, const EVP_MD
*md
,
114 STACK_OF(X509
) *rother
, unsigned long flags
,
115 int nmin
, int ndays
, int badsig
);
117 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
);
118 static BIO
*init_responder(const char *port
);
119 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
121 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
);
122 static OCSP_RESPONSE
*query_responder(BIO
*err
, BIO
*cbio
, const char *path
,
123 const STACK_OF(CONF_VALUE
) *headers
,
124 OCSP_REQUEST
*req
, int req_timeout
);
127 # define PROG ocsp_main
129 int MAIN(int, char **);
131 int MAIN(int argc
, char **argv
)
135 char *host
= NULL
, *port
= NULL
, *path
= "/";
136 char *thost
= NULL
, *tport
= NULL
, *tpath
= NULL
;
137 char *reqin
= NULL
, *respin
= NULL
;
138 char *reqout
= NULL
, *respout
= NULL
;
139 char *signfile
= NULL
, *keyfile
= NULL
;
140 char *rsignfile
= NULL
, *rkeyfile
= NULL
;
141 char *outfile
= NULL
;
142 int add_nonce
= 1, noverify
= 0, use_ssl
= -1;
143 STACK_OF(CONF_VALUE
) *headers
= NULL
;
144 OCSP_REQUEST
*req
= NULL
;
145 OCSP_RESPONSE
*resp
= NULL
;
146 OCSP_BASICRESP
*bs
= NULL
;
147 X509
*issuer
= NULL
, *cert
= NULL
;
148 X509
*signer
= NULL
, *rsigner
= NULL
;
149 EVP_PKEY
*key
= NULL
, *rkey
= NULL
;
150 BIO
*acbio
= NULL
, *cbio
= NULL
;
153 int req_timeout
= -1;
154 int req_text
= 0, resp_text
= 0;
155 long nsec
= MAX_VALIDITY_PERIOD
, maxage
= -1;
156 char *CAfile
= NULL
, *CApath
= NULL
;
157 X509_STORE
*store
= NULL
;
158 X509_VERIFY_PARAM
*vpm
= NULL
;
159 STACK_OF(X509
) *sign_other
= NULL
, *verify_other
= NULL
, *rother
= NULL
;
160 char *sign_certfile
= NULL
, *verify_certfile
= NULL
, *rcertfile
= NULL
;
161 unsigned long sign_flags
= 0, verify_flags
= 0, rflags
= 0;
163 int accept_count
= -1;
168 STACK_OF(OPENSSL_STRING
) *reqnames
= NULL
;
169 STACK_OF(OCSP_CERTID
) *ids
= NULL
;
171 X509
*rca_cert
= NULL
;
172 char *ridx_filename
= NULL
;
173 char *rca_filename
= NULL
;
175 int nmin
= 0, ndays
= -1;
176 const EVP_MD
*cert_id_md
= NULL
, *rsign_md
= NULL
;
179 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
181 if (!load_config(bio_err
, NULL
))
183 SSL_load_error_strings();
184 OpenSSL_add_ssl_algorithms();
186 reqnames
= sk_OPENSSL_STRING_new_null();
187 ids
= sk_OCSP_CERTID_new_null();
188 while (!badarg
&& *args
&& *args
[0] == '-') {
189 if (!strcmp(*args
, "-out")) {
195 } else if (!strcmp(*args
, "-timeout")) {
198 req_timeout
= atol(*args
);
199 if (req_timeout
< 0) {
200 BIO_printf(bio_err
, "Illegal timeout value %s\n", *args
);
205 } else if (!strcmp(*args
, "-url")) {
214 if (!OCSP_parse_url(*args
, &host
, &port
, &path
, &use_ssl
)) {
215 BIO_printf(bio_err
, "Error parsing URL\n");
223 } else if (!strcmp(*args
, "-host")) {
229 } else if (!strcmp(*args
, "-port")) {
235 } else if (!strcmp(*args
, "-header")) {
236 if (args
[1] && args
[2]) {
237 if (!X509V3_add_value(args
[1], args
[2], &headers
))
242 } else if (!strcmp(*args
, "-ignore_err"))
244 else if (!strcmp(*args
, "-noverify"))
246 else if (!strcmp(*args
, "-nonce"))
248 else if (!strcmp(*args
, "-no_nonce"))
250 else if (!strcmp(*args
, "-resp_no_certs"))
251 rflags
|= OCSP_NOCERTS
;
252 else if (!strcmp(*args
, "-resp_key_id"))
253 rflags
|= OCSP_RESPID_KEY
;
254 else if (!strcmp(*args
, "-no_certs"))
255 sign_flags
|= OCSP_NOCERTS
;
256 else if (!strcmp(*args
, "-no_signature_verify"))
257 verify_flags
|= OCSP_NOSIGS
;
258 else if (!strcmp(*args
, "-no_cert_verify"))
259 verify_flags
|= OCSP_NOVERIFY
;
260 else if (!strcmp(*args
, "-no_chain"))
261 verify_flags
|= OCSP_NOCHAIN
;
262 else if (!strcmp(*args
, "-no_cert_checks"))
263 verify_flags
|= OCSP_NOCHECKS
;
264 else if (!strcmp(*args
, "-no_explicit"))
265 verify_flags
|= OCSP_NOEXPLICIT
;
266 else if (!strcmp(*args
, "-trust_other"))
267 verify_flags
|= OCSP_TRUSTOTHER
;
268 else if (!strcmp(*args
, "-no_intern"))
269 verify_flags
|= OCSP_NOINTERN
;
270 else if (!strcmp(*args
, "-badsig"))
272 else if (!strcmp(*args
, "-text")) {
275 } else if (!strcmp(*args
, "-req_text"))
277 else if (!strcmp(*args
, "-resp_text"))
279 else if (!strcmp(*args
, "-reqin")) {
285 } else if (!strcmp(*args
, "-respin")) {
291 } else if (!strcmp(*args
, "-signer")) {
297 } else if (!strcmp(*args
, "-VAfile")) {
300 verify_certfile
= *args
;
301 verify_flags
|= OCSP_TRUSTOTHER
;
304 } else if (!strcmp(*args
, "-sign_other")) {
307 sign_certfile
= *args
;
310 } else if (!strcmp(*args
, "-verify_other")) {
313 verify_certfile
= *args
;
316 } else if (!strcmp(*args
, "-CAfile")) {
322 } else if (!strcmp(*args
, "-CApath")) {
328 } else if (args_verify(&args
, NULL
, &badarg
, bio_err
, &vpm
)) {
332 } else if (!strcmp(*args
, "-validity_period")) {
338 "Illegal validity period %s\n", *args
);
343 } else if (!strcmp(*args
, "-status_age")) {
346 maxage
= atol(*args
);
348 BIO_printf(bio_err
, "Illegal validity age %s\n", *args
);
353 } else if (!strcmp(*args
, "-signkey")) {
359 } else if (!strcmp(*args
, "-reqout")) {
365 } else if (!strcmp(*args
, "-respout")) {
371 } else if (!strcmp(*args
, "-path")) {
377 } else if (!strcmp(*args
, "-issuer")) {
381 issuer
= load_cert(bio_err
, *args
, FORMAT_PEM
,
382 NULL
, e
, "issuer certificate");
387 } else if (!strcmp(*args
, "-cert")) {
391 cert
= load_cert(bio_err
, *args
, FORMAT_PEM
,
392 NULL
, e
, "certificate");
396 cert_id_md
= EVP_sha1();
397 if (!add_ocsp_cert(&req
, cert
, cert_id_md
, issuer
, ids
))
399 if (!sk_OPENSSL_STRING_push(reqnames
, *args
))
403 } else if (!strcmp(*args
, "-serial")) {
407 cert_id_md
= EVP_sha1();
408 if (!add_ocsp_serial(&req
, *args
, cert_id_md
, issuer
, ids
))
410 if (!sk_OPENSSL_STRING_push(reqnames
, *args
))
414 } else if (!strcmp(*args
, "-index")) {
417 ridx_filename
= *args
;
420 } else if (!strcmp(*args
, "-CA")) {
423 rca_filename
= *args
;
426 } else if (!strcmp(*args
, "-nmin")) {
431 BIO_printf(bio_err
, "Illegal update period %s\n", *args
);
439 } else if (!strcmp(*args
, "-nrequest")) {
442 accept_count
= atol(*args
);
443 if (accept_count
< 0) {
444 BIO_printf(bio_err
, "Illegal accept count %s\n", *args
);
449 } else if (!strcmp(*args
, "-ndays")) {
454 BIO_printf(bio_err
, "Illegal update period %s\n", *args
);
459 } else if (!strcmp(*args
, "-rsigner")) {
465 } else if (!strcmp(*args
, "-rkey")) {
471 } else if (!strcmp(*args
, "-rother")) {
477 } else if (!strcmp(*args
, "-rmd")) {
480 rsign_md
= EVP_get_digestbyname(*args
);
485 } else if ((cert_id_md
= EVP_get_digestbyname((*args
) + 1)) == NULL
) {
491 /* Have we anything to do? */
492 if (!req
&& !reqin
&& !respin
&& !(port
&& ridx_filename
))
496 BIO_printf(bio_err
, "OCSP utility\n");
497 BIO_printf(bio_err
, "Usage ocsp [options]\n");
498 BIO_printf(bio_err
, "where options are\n");
499 BIO_printf(bio_err
, "-out file output filename\n");
500 BIO_printf(bio_err
, "-issuer file issuer certificate\n");
501 BIO_printf(bio_err
, "-cert file certificate to check\n");
502 BIO_printf(bio_err
, "-serial n serial number to check\n");
504 "-signer file certificate to sign OCSP request with\n");
506 "-signkey file private key to sign OCSP request with\n");
508 "-sign_other file additional certificates to include in signed request\n");
510 "-no_certs don't include any certificates in signed request\n");
512 "-req_text print text form of request\n");
514 "-resp_text print text form of response\n");
516 "-text print text form of request and response\n");
518 "-reqout file write DER encoded OCSP request to \"file\"\n");
520 "-respout file write DER encoded OCSP reponse to \"file\"\n");
522 "-reqin file read DER encoded OCSP request from \"file\"\n");
524 "-respin file read DER encoded OCSP reponse from \"file\"\n");
526 "-nonce add OCSP nonce to request\n");
528 "-no_nonce don't add OCSP nonce to request\n");
529 BIO_printf(bio_err
, "-url URL OCSP responder URL\n");
531 "-host host:n send OCSP request to host on port n\n");
533 "-path path to use in OCSP request\n");
535 "-CApath dir trusted certificates directory\n");
537 "-CAfile file trusted certificates file\n");
539 "-no_alt_chains only ever use the first certificate chain found\n");
541 "-VAfile file validator certificates file\n");
543 "-validity_period n maximum validity discrepancy in seconds\n");
545 "-status_age n maximum status age in seconds\n");
547 "-noverify don't verify response at all\n");
549 "-verify_other file additional certificates to search for signer\n");
551 "-trust_other don't verify additional certificates\n");
553 "-no_intern don't search certificates contained in response for signer\n");
555 "-no_signature_verify don't check signature on response\n");
557 "-no_cert_verify don't check signing certificate\n");
559 "-no_chain don't chain verify response\n");
561 "-no_cert_checks don't do additional checks on signing certificate\n");
563 "-port num port to run responder on\n");
565 "-index file certificate status index file\n");
566 BIO_printf(bio_err
, "-CA file CA certificate\n");
568 "-rsigner file responder certificate to sign responses with\n");
570 "-rkey file responder key to sign responses with\n");
572 "-rother file other certificates to include in response\n");
574 "-resp_no_certs don't include any certificates in response\n");
576 "-nmin n number of minutes before next update\n");
578 "-ndays n number of days before next update\n");
580 "-resp_key_id identify reponse by signing certificate key ID\n");
582 "-nrequest n number of requests to accept (default unlimited)\n");
584 "-<dgst alg> use specified digest in the request\n");
586 "-timeout n timeout connection to OCSP responder after n seconds\n");
591 out
= BIO_new_file(outfile
, "w");
593 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
596 BIO_printf(bio_err
, "Error opening output file\n");
600 if (!req
&& (add_nonce
!= 2))
604 if (!strcmp(reqin
, "-"))
605 derbio
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
607 derbio
= BIO_new_file(reqin
, "rb");
609 BIO_printf(bio_err
, "Error Opening OCSP request file\n");
612 req
= d2i_OCSP_REQUEST_bio(derbio
, NULL
);
615 BIO_printf(bio_err
, "Error reading OCSP request\n");
621 acbio
= init_responder(port
);
626 if (rsignfile
&& !rdb
) {
628 rkeyfile
= rsignfile
;
629 rsigner
= load_cert(bio_err
, rsignfile
, FORMAT_PEM
,
630 NULL
, e
, "responder certificate");
632 BIO_printf(bio_err
, "Error loading responder certificate\n");
635 rca_cert
= load_cert(bio_err
, rca_filename
, FORMAT_PEM
,
636 NULL
, e
, "CA certificate");
638 rother
= load_certs(bio_err
, rcertfile
, FORMAT_PEM
,
639 NULL
, e
, "responder other certificates");
643 rkey
= load_key(bio_err
, rkeyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
644 "responder private key");
649 BIO_printf(bio_err
, "Waiting for OCSP client connections...\n");
654 if (!do_responder(&req
, &cbio
, acbio
, port
))
658 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
,
660 send_ocsp_response(cbio
, resp
);
665 if (!req
&& (signfile
|| reqout
|| host
|| add_nonce
|| ridx_filename
)) {
666 BIO_printf(bio_err
, "Need an OCSP request for this operation!\n");
670 if (req
&& add_nonce
)
671 OCSP_request_add1_nonce(req
, NULL
, -1);
676 signer
= load_cert(bio_err
, signfile
, FORMAT_PEM
,
677 NULL
, e
, "signer certificate");
679 BIO_printf(bio_err
, "Error loading signer certificate\n");
683 sign_other
= load_certs(bio_err
, sign_certfile
, FORMAT_PEM
,
684 NULL
, e
, "signer certificates");
688 key
= load_key(bio_err
, keyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
689 "signer private key");
693 if (!OCSP_request_sign
694 (req
, signer
, key
, NULL
, sign_other
, sign_flags
)) {
695 BIO_printf(bio_err
, "Error signing OCSP request\n");
701 OCSP_REQUEST_print(out
, req
, 0);
704 if (!strcmp(reqout
, "-"))
705 derbio
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
707 derbio
= BIO_new_file(reqout
, "wb");
709 BIO_printf(bio_err
, "Error opening file %s\n", reqout
);
712 i2d_OCSP_REQUEST_bio(derbio
, req
);
716 if (ridx_filename
&& (!rkey
|| !rsigner
|| !rca_cert
)) {
718 "Need a responder certificate, key and CA for this operation!\n");
722 if (ridx_filename
&& !rdb
) {
723 rdb
= load_index(ridx_filename
, NULL
);
726 if (!index_index(rdb
))
731 i
= make_ocsp_response(&resp
, req
, rdb
, rca_cert
, rsigner
, rkey
,
732 rsign_md
, rother
, rflags
, nmin
, ndays
, badsig
);
734 send_ocsp_response(cbio
, resp
);
736 # ifndef OPENSSL_NO_SOCK
737 resp
= process_responder(bio_err
, req
, host
, path
,
738 port
, use_ssl
, headers
, req_timeout
);
743 "Error creating connect BIO - sockets not supported.\n");
747 if (!strcmp(respin
, "-"))
748 derbio
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
750 derbio
= BIO_new_file(respin
, "rb");
752 BIO_printf(bio_err
, "Error Opening OCSP response file\n");
755 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
758 BIO_printf(bio_err
, "Error reading OCSP response\n");
770 if (!strcmp(respout
, "-"))
771 derbio
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
773 derbio
= BIO_new_file(respout
, "wb");
775 BIO_printf(bio_err
, "Error opening file %s\n", respout
);
778 i2d_OCSP_RESPONSE_bio(derbio
, resp
);
782 i
= OCSP_response_status(resp
);
784 if (i
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
785 BIO_printf(out
, "Responder Error: %s (%d)\n",
786 OCSP_response_status_str(i
), i
);
794 OCSP_RESPONSE_print(out
, resp
, 0);
796 /* If running as responder don't verify our own response */
798 if (accept_count
> 0)
800 /* Redo if more connections needed */
804 OCSP_REQUEST_free(req
);
806 OCSP_RESPONSE_free(resp
);
812 } else if (ridx_filename
) {
818 store
= setup_verify(bio_err
, CAfile
, CApath
);
822 X509_STORE_set1_param(store
, vpm
);
823 if (verify_certfile
) {
824 verify_other
= load_certs(bio_err
, verify_certfile
, FORMAT_PEM
,
825 NULL
, e
, "validator certificate");
830 bs
= OCSP_response_get1_basic(resp
);
833 BIO_printf(bio_err
, "Error parsing response\n");
840 if (req
&& ((i
= OCSP_check_nonce(req
, bs
)) <= 0)) {
842 BIO_printf(bio_err
, "WARNING: no nonce in response\n");
844 BIO_printf(bio_err
, "Nonce Verify error\n");
850 i
= OCSP_basic_verify(bs
, verify_other
, store
, verify_flags
);
852 BIO_printf(bio_err
, "Response Verify Failure\n");
853 ERR_print_errors(bio_err
);
856 BIO_printf(bio_err
, "Response verify OK\n");
860 if (!print_ocsp_summary(out
, bs
, req
, reqnames
, ids
, nsec
, maxage
))
864 ERR_print_errors(bio_err
);
866 X509_STORE_free(store
);
868 X509_VERIFY_PARAM_free(vpm
);
879 OCSP_REQUEST_free(req
);
880 OCSP_RESPONSE_free(resp
);
881 OCSP_BASICRESP_free(bs
);
882 sk_OPENSSL_STRING_free(reqnames
);
883 sk_OCSP_CERTID_free(ids
);
884 sk_X509_pop_free(sign_other
, X509_free
);
885 sk_X509_pop_free(verify_other
, X509_free
);
886 sk_CONF_VALUE_pop_free(headers
, X509V3_conf_free
);
898 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
899 const EVP_MD
*cert_id_md
, X509
*issuer
,
900 STACK_OF(OCSP_CERTID
) *ids
)
904 BIO_printf(bio_err
, "No issuer certificate specified\n");
908 *req
= OCSP_REQUEST_new();
911 id
= OCSP_cert_to_id(cert_id_md
, cert
, issuer
);
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 add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
924 const EVP_MD
*cert_id_md
, X509
*issuer
,
925 STACK_OF(OCSP_CERTID
) *ids
)
929 ASN1_BIT_STRING
*ikey
;
932 BIO_printf(bio_err
, "No issuer certificate specified\n");
936 *req
= OCSP_REQUEST_new();
939 iname
= X509_get_subject_name(issuer
);
940 ikey
= X509_get0_pubkey_bitstr(issuer
);
941 sno
= s2i_ASN1_INTEGER(NULL
, serial
);
943 BIO_printf(bio_err
, "Error converting serial number %s\n", serial
);
946 id
= OCSP_cert_id_new(cert_id_md
, iname
, ikey
, sno
);
947 ASN1_INTEGER_free(sno
);
948 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
950 if (!OCSP_request_add0_id(*req
, id
))
955 BIO_printf(bio_err
, "Error Creating OCSP request\n");
959 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
960 STACK_OF(OPENSSL_STRING
) *names
,
961 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
970 ASN1_GENERALIZEDTIME
*rev
, *thisupd
, *nextupd
;
972 if (!bs
|| !req
|| !sk_OPENSSL_STRING_num(names
)
973 || !sk_OCSP_CERTID_num(ids
))
976 for (i
= 0; i
< sk_OCSP_CERTID_num(ids
); i
++) {
977 id
= sk_OCSP_CERTID_value(ids
, i
);
978 name
= sk_OPENSSL_STRING_value(names
, i
);
979 BIO_printf(out
, "%s: ", name
);
981 if (!OCSP_resp_find_status(bs
, id
, &status
, &reason
,
982 &rev
, &thisupd
, &nextupd
)) {
983 BIO_puts(out
, "ERROR: No Status found.\n");
988 * Check validity: if invalid write to output BIO so we know which
989 * response this refers to.
991 if (!OCSP_check_validity(thisupd
, nextupd
, nsec
, maxage
)) {
992 BIO_puts(out
, "WARNING: Status times invalid.\n");
993 ERR_print_errors(out
);
995 BIO_printf(out
, "%s\n", OCSP_cert_status_str(status
));
997 BIO_puts(out
, "\tThis Update: ");
998 ASN1_GENERALIZEDTIME_print(out
, thisupd
);
1002 BIO_puts(out
, "\tNext Update: ");
1003 ASN1_GENERALIZEDTIME_print(out
, nextupd
);
1004 BIO_puts(out
, "\n");
1007 if (status
!= V_OCSP_CERTSTATUS_REVOKED
)
1011 BIO_printf(out
, "\tReason: %s\n", OCSP_crl_reason_str(reason
));
1013 BIO_puts(out
, "\tRevocation Time: ");
1014 ASN1_GENERALIZEDTIME_print(out
, rev
);
1015 BIO_puts(out
, "\n");
1021 static int make_ocsp_response(OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
1022 CA_DB
*db
, X509
*ca
, X509
*rcert
,
1023 EVP_PKEY
*rkey
, const EVP_MD
*rmd
,
1024 STACK_OF(X509
) *rother
, unsigned long flags
,
1025 int nmin
, int ndays
, int badsig
)
1027 ASN1_TIME
*thisupd
= NULL
, *nextupd
= NULL
;
1028 OCSP_CERTID
*cid
, *ca_id
= NULL
;
1029 OCSP_BASICRESP
*bs
= NULL
;
1030 int i
, id_count
, ret
= 1;
1032 id_count
= OCSP_request_onereq_count(req
);
1034 if (id_count
<= 0) {
1036 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
1040 bs
= OCSP_BASICRESP_new();
1041 thisupd
= X509_gmtime_adj(NULL
, 0);
1043 nextupd
= X509_gmtime_adj(NULL
, nmin
* 60 + ndays
* 3600 * 24);
1045 /* Examine each certificate id in the request */
1046 for (i
= 0; i
< id_count
; i
++) {
1048 ASN1_INTEGER
*serial
;
1050 ASN1_OBJECT
*cert_id_md_oid
;
1051 const EVP_MD
*cert_id_md
;
1052 one
= OCSP_request_onereq_get0(req
, i
);
1053 cid
= OCSP_onereq_get0_id(one
);
1055 OCSP_id_get0_info(NULL
, &cert_id_md_oid
, NULL
, NULL
, cid
);
1057 cert_id_md
= EVP_get_digestbyobj(cert_id_md_oid
);
1059 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1064 OCSP_CERTID_free(ca_id
);
1065 ca_id
= OCSP_cert_to_id(cert_id_md
, NULL
, ca
);
1067 /* Is this request about our CA? */
1068 if (OCSP_id_issuer_cmp(ca_id
, cid
)) {
1069 OCSP_basic_add1_status(bs
, cid
,
1070 V_OCSP_CERTSTATUS_UNKNOWN
,
1071 0, NULL
, thisupd
, nextupd
);
1074 OCSP_id_get0_info(NULL
, NULL
, NULL
, &serial
, cid
);
1075 inf
= lookup_serial(db
, serial
);
1077 OCSP_basic_add1_status(bs
, cid
,
1078 V_OCSP_CERTSTATUS_UNKNOWN
,
1079 0, NULL
, thisupd
, nextupd
);
1080 else if (inf
[DB_type
][0] == DB_TYPE_VAL
)
1081 OCSP_basic_add1_status(bs
, cid
,
1082 V_OCSP_CERTSTATUS_GOOD
,
1083 0, NULL
, thisupd
, nextupd
);
1084 else if (inf
[DB_type
][0] == DB_TYPE_REV
) {
1085 ASN1_OBJECT
*inst
= NULL
;
1086 ASN1_TIME
*revtm
= NULL
;
1087 ASN1_GENERALIZEDTIME
*invtm
= NULL
;
1088 OCSP_SINGLERESP
*single
;
1090 unpack_revinfo(&revtm
, &reason
, &inst
, &invtm
, inf
[DB_rev_date
]);
1091 single
= OCSP_basic_add1_status(bs
, cid
,
1092 V_OCSP_CERTSTATUS_REVOKED
,
1093 reason
, revtm
, thisupd
, nextupd
);
1095 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_invalidity_date
,
1098 OCSP_SINGLERESP_add1_ext_i2d(single
,
1099 NID_hold_instruction_code
, inst
,
1101 ASN1_OBJECT_free(inst
);
1102 ASN1_TIME_free(revtm
);
1103 ASN1_GENERALIZEDTIME_free(invtm
);
1107 OCSP_copy_nonce(bs
, req
);
1109 OCSP_basic_sign(bs
, rcert
, rkey
, rmd
, rother
, flags
);
1112 bs
->signature
->data
[bs
->signature
->length
- 1] ^= 0x1;
1114 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL
, bs
);
1117 ASN1_TIME_free(thisupd
);
1118 ASN1_TIME_free(nextupd
);
1119 OCSP_CERTID_free(ca_id
);
1120 OCSP_BASICRESP_free(bs
);
1125 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
)
1129 char *itmp
, *row
[DB_NUMBER
], **rrow
;
1130 for (i
= 0; i
< DB_NUMBER
; i
++)
1132 bn
= ASN1_INTEGER_to_BN(ser
, NULL
);
1133 OPENSSL_assert(bn
); /* FIXME: should report an error at this
1134 * point and abort */
1136 itmp
= BUF_strdup("00");
1138 itmp
= BN_bn2hex(bn
);
1139 row
[DB_serial
] = itmp
;
1141 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1146 /* Quick and dirty OCSP server: read in and parse input request */
1148 static BIO
*init_responder(const char *port
)
1150 BIO
*acbio
= NULL
, *bufbio
= NULL
;
1151 bufbio
= BIO_new(BIO_f_buffer());
1154 # ifndef OPENSSL_NO_SOCK
1155 acbio
= BIO_new_accept(port
);
1158 "Error setting up accept BIO - sockets not supported.\n");
1162 BIO_set_accept_bios(acbio
, bufbio
);
1165 if (BIO_do_accept(acbio
) <= 0) {
1166 BIO_printf(bio_err
, "Error setting up accept BIO\n");
1167 ERR_print_errors(bio_err
);
1174 BIO_free_all(acbio
);
1179 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
1182 int have_post
= 0, len
;
1183 OCSP_REQUEST
*req
= NULL
;
1187 if (BIO_do_accept(acbio
) <= 0) {
1188 BIO_printf(bio_err
, "Error accepting connection\n");
1189 ERR_print_errors(bio_err
);
1193 cbio
= BIO_pop(acbio
);
1197 len
= BIO_gets(cbio
, inbuf
, sizeof inbuf
);
1200 /* Look for "POST" signalling start of query */
1202 if (strncmp(inbuf
, "POST", 4)) {
1203 BIO_printf(bio_err
, "Invalid request\n");
1208 /* Look for end of headers */
1209 if ((inbuf
[0] == '\r') || (inbuf
[0] == '\n'))
1213 /* Try to read OCSP request */
1215 req
= d2i_OCSP_REQUEST_bio(cbio
, NULL
);
1218 BIO_printf(bio_err
, "Error parsing OCSP request\n");
1219 ERR_print_errors(bio_err
);
1228 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
)
1231 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1232 "Content-Length: %d\r\n\r\n";
1235 BIO_printf(cbio
, http_resp
, i2d_OCSP_RESPONSE(resp
, NULL
));
1236 i2d_OCSP_RESPONSE_bio(cbio
, resp
);
1237 (void)BIO_flush(cbio
);
1241 static OCSP_RESPONSE
*query_responder(BIO
*err
, BIO
*cbio
, const char *path
,
1242 const STACK_OF(CONF_VALUE
) *headers
,
1243 OCSP_REQUEST
*req
, int req_timeout
)
1248 OCSP_REQ_CTX
*ctx
= NULL
;
1249 OCSP_RESPONSE
*rsp
= NULL
;
1253 if (req_timeout
!= -1)
1254 BIO_set_nbio(cbio
, 1);
1256 rv
= BIO_do_connect(cbio
);
1258 if ((rv
<= 0) && ((req_timeout
== -1) || !BIO_should_retry(cbio
))) {
1259 BIO_puts(err
, "Error connecting BIO\n");
1263 if (BIO_get_fd(cbio
, &fd
) <= 0) {
1264 BIO_puts(err
, "Can't get connection fd\n");
1268 if (req_timeout
!= -1 && rv
<= 0) {
1270 openssl_fdset(fd
, &confds
);
1272 tv
.tv_sec
= req_timeout
;
1273 rv
= select(fd
+ 1, NULL
, (void *)&confds
, NULL
, &tv
);
1275 BIO_puts(err
, "Timeout on connect\n");
1280 ctx
= OCSP_sendreq_new(cbio
, path
, NULL
, -1);
1284 for (i
= 0; i
< sk_CONF_VALUE_num(headers
); i
++) {
1285 CONF_VALUE
*hdr
= sk_CONF_VALUE_value(headers
, i
);
1286 if (!OCSP_REQ_CTX_add1_header(ctx
, hdr
->name
, hdr
->value
))
1290 if (!OCSP_REQ_CTX_set1_req(ctx
, req
))
1294 rv
= OCSP_sendreq_nbio(&rsp
, ctx
);
1297 if (req_timeout
== -1)
1300 openssl_fdset(fd
, &confds
);
1302 tv
.tv_sec
= req_timeout
;
1303 if (BIO_should_read(cbio
))
1304 rv
= select(fd
+ 1, (void *)&confds
, NULL
, NULL
, &tv
);
1305 else if (BIO_should_write(cbio
))
1306 rv
= select(fd
+ 1, NULL
, (void *)&confds
, NULL
, &tv
);
1308 BIO_puts(err
, "Unexpected retry condition\n");
1312 BIO_puts(err
, "Timeout on request\n");
1316 BIO_puts(err
, "Select error\n");
1323 OCSP_REQ_CTX_free(ctx
);
1328 OCSP_RESPONSE
*process_responder(BIO
*err
, OCSP_REQUEST
*req
,
1329 const char *host
, const char *path
,
1330 const char *port
, int use_ssl
,
1331 const STACK_OF(CONF_VALUE
) *headers
,
1335 SSL_CTX
*ctx
= NULL
;
1336 OCSP_RESPONSE
*resp
= NULL
;
1337 cbio
= BIO_new_connect(host
);
1339 BIO_printf(err
, "Error creating connect BIO\n");
1343 BIO_set_conn_port(cbio
, port
);
1346 ctx
= SSL_CTX_new(SSLv23_client_method());
1348 BIO_printf(err
, "Error creating SSL context.\n");
1351 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1352 sbio
= BIO_new_ssl(ctx
, 1);
1353 cbio
= BIO_push(sbio
, cbio
);
1355 resp
= query_responder(err
, cbio
, path
, headers
, req
, req_timeout
);
1357 BIO_printf(bio_err
, "Error querying OCSP responder\n");