1 /* $OpenBSD: ocsp.c,v 1.15 2018/02/07 05:49:36 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) 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
60 #include <sys/types.h>
69 /* Needs to be included before the openssl headers! */
72 #include <openssl/bn.h>
73 #include <openssl/crypto.h>
74 #include <openssl/err.h>
75 #include <openssl/evp.h>
76 #include <openssl/ssl.h>
77 #include <openssl/x509v3.h>
79 /* Maximum leeway in validity period: default 5 minutes */
80 #define MAX_VALIDITY_PERIOD (5 * 60)
83 add_ocsp_cert(OCSP_REQUEST
** req
, X509
* cert
, const EVP_MD
* cert_id_md
, X509
* issuer
,
84 STACK_OF(OCSP_CERTID
) * ids
);
85 static int add_ocsp_serial(OCSP_REQUEST
** req
, char *serial
, const EVP_MD
* cert_id_md
, X509
* issuer
,
86 STACK_OF(OCSP_CERTID
) * ids
);
87 static int print_ocsp_summary(BIO
* out
, OCSP_BASICRESP
* bs
, OCSP_REQUEST
* req
,
88 STACK_OF(OPENSSL_STRING
) * names
,
89 STACK_OF(OCSP_CERTID
) * ids
, long nsec
,
92 static int make_ocsp_response(OCSP_RESPONSE
** resp
, OCSP_REQUEST
* req
, CA_DB
* db
,
93 X509
* ca
, X509
* rcert
, EVP_PKEY
* rkey
,
94 STACK_OF(X509
) * rother
, unsigned long flags
,
97 static char **lookup_serial(CA_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
);
101 static OCSP_RESPONSE
*query_responder(BIO
* err
, BIO
* cbio
, char *path
,
102 STACK_OF(CONF_VALUE
) * headers
,
103 OCSP_REQUEST
* req
, int req_timeout
);
106 ocsp_main(int argc
, char **argv
)
109 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
110 char *reqin
= NULL
, *respin
= NULL
;
111 char *reqout
= NULL
, *respout
= NULL
;
112 char *signfile
= NULL
, *keyfile
= NULL
;
113 char *rsignfile
= NULL
, *rkeyfile
= NULL
;
114 char *outfile
= NULL
;
115 int add_nonce
= 1, noverify
= 0, use_ssl
= -1;
116 STACK_OF(CONF_VALUE
) * headers
= NULL
;
117 OCSP_REQUEST
*req
= NULL
;
118 OCSP_RESPONSE
*resp
= NULL
;
119 OCSP_BASICRESP
*bs
= NULL
;
120 X509
*issuer
= NULL
, *cert
= NULL
;
121 X509
*signer
= NULL
, *rsigner
= NULL
;
122 EVP_PKEY
*key
= NULL
, *rkey
= NULL
;
123 BIO
*acbio
= NULL
, *cbio
= NULL
;
126 int req_timeout
= -1;
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
;
131 STACK_OF(X509
) * sign_other
= NULL
, *verify_other
= NULL
, *rother
= NULL
;
132 char *sign_certfile
= NULL
, *verify_certfile
= NULL
, *rcertfile
= NULL
;
133 unsigned long sign_flags
= 0, verify_flags
= 0, rflags
= 0;
135 int accept_count
= -1;
139 STACK_OF(OPENSSL_STRING
) * reqnames
= NULL
;
140 STACK_OF(OCSP_CERTID
) * ids
= NULL
;
141 X509
*rca_cert
= NULL
;
142 char *ridx_filename
= NULL
;
143 char *rca_filename
= NULL
;
145 int nmin
= 0, ndays
= -1;
146 const EVP_MD
*cert_id_md
= NULL
;
147 const char *errstr
= NULL
;
149 if (single_execution
) {
150 if (pledge("stdio cpath wpath rpath inet dns tty", NULL
) == -1) {
157 reqnames
= sk_OPENSSL_STRING_new_null();
158 ids
= sk_OCSP_CERTID_new_null();
159 while (!badarg
&& *args
&& *args
[0] == '-') {
160 if (!strcmp(*args
, "-out")) {
166 } else if (!strcmp(*args
, "-timeout")) {
169 req_timeout
= strtonum(*args
, 0,
173 "Illegal timeout value %s: %s\n",
179 } else if (!strcmp(*args
, "-url")) {
180 if (args
[1] && host
== NULL
&& port
== NULL
&&
183 if (!OCSP_parse_url(*args
, &host
, &port
, &path
, &use_ssl
)) {
184 BIO_printf(bio_err
, "Error parsing URL\n");
189 } else if (!strcmp(*args
, "-host")) {
190 if (args
[1] && use_ssl
== -1) {
195 } else if (!strcmp(*args
, "-port")) {
196 if (args
[1] && use_ssl
== -1) {
201 } else if (!strcmp(*args
, "-header")) {
202 if (args
[1] && args
[2]) {
203 if (!X509V3_add_value(args
[1], args
[2], &headers
))
208 } else if (!strcmp(*args
, "-ignore_err"))
210 else if (!strcmp(*args
, "-noverify"))
212 else if (!strcmp(*args
, "-nonce"))
214 else if (!strcmp(*args
, "-no_nonce"))
216 else if (!strcmp(*args
, "-resp_no_certs"))
217 rflags
|= OCSP_NOCERTS
;
218 else if (!strcmp(*args
, "-resp_key_id"))
219 rflags
|= OCSP_RESPID_KEY
;
220 else if (!strcmp(*args
, "-no_certs"))
221 sign_flags
|= OCSP_NOCERTS
;
222 else if (!strcmp(*args
, "-no_signature_verify"))
223 verify_flags
|= OCSP_NOSIGS
;
224 else if (!strcmp(*args
, "-no_cert_verify"))
225 verify_flags
|= OCSP_NOVERIFY
;
226 else if (!strcmp(*args
, "-no_chain"))
227 verify_flags
|= OCSP_NOCHAIN
;
228 else if (!strcmp(*args
, "-no_cert_checks"))
229 verify_flags
|= OCSP_NOCHECKS
;
230 else if (!strcmp(*args
, "-no_explicit"))
231 verify_flags
|= OCSP_NOEXPLICIT
;
232 else if (!strcmp(*args
, "-trust_other"))
233 verify_flags
|= OCSP_TRUSTOTHER
;
234 else if (!strcmp(*args
, "-no_intern"))
235 verify_flags
|= OCSP_NOINTERN
;
236 else if (!strcmp(*args
, "-text")) {
239 } else if (!strcmp(*args
, "-req_text"))
241 else if (!strcmp(*args
, "-resp_text"))
243 else if (!strcmp(*args
, "-reqin")) {
249 } else if (!strcmp(*args
, "-respin")) {
255 } else if (!strcmp(*args
, "-signer")) {
261 } else if (!strcmp(*args
, "-VAfile")) {
264 verify_certfile
= *args
;
265 verify_flags
|= OCSP_TRUSTOTHER
;
268 } else if (!strcmp(*args
, "-sign_other")) {
271 sign_certfile
= *args
;
274 } else if (!strcmp(*args
, "-verify_other")) {
277 verify_certfile
= *args
;
280 } else if (!strcmp(*args
, "-CAfile")) {
286 } else if (!strcmp(*args
, "-CApath")) {
292 } else if (!strcmp(*args
, "-validity_period")) {
295 nsec
= strtonum(*args
, 0, LONG_MAX
, &errstr
);
298 "Illegal validity period %s: %s\n",
304 } else if (!strcmp(*args
, "-status_age")) {
307 maxage
= strtonum(*args
, 0, LONG_MAX
, &errstr
);
310 "Illegal validity age %s: %s\n",
316 } else if (!strcmp(*args
, "-signkey")) {
322 } else if (!strcmp(*args
, "-reqout")) {
328 } else if (!strcmp(*args
, "-respout")) {
334 } else if (!strcmp(*args
, "-path")) {
335 if (args
[1] && use_ssl
== -1) {
340 } else if (!strcmp(*args
, "-issuer")) {
344 issuer
= load_cert(bio_err
, *args
, FORMAT_PEM
,
345 NULL
, "issuer certificate");
350 } else if (!strcmp(*args
, "-cert")) {
354 cert
= load_cert(bio_err
, *args
, FORMAT_PEM
,
355 NULL
, "certificate");
359 cert_id_md
= EVP_sha1();
360 if (!add_ocsp_cert(&req
, cert
, cert_id_md
, issuer
, ids
))
362 if (!sk_OPENSSL_STRING_push(reqnames
, *args
))
366 } else if (!strcmp(*args
, "-serial")) {
370 cert_id_md
= EVP_sha1();
371 if (!add_ocsp_serial(&req
, *args
, cert_id_md
, issuer
, ids
))
373 if (!sk_OPENSSL_STRING_push(reqnames
, *args
))
377 } else if (!strcmp(*args
, "-index")) {
380 ridx_filename
= *args
;
383 } else if (!strcmp(*args
, "-CA")) {
386 rca_filename
= *args
;
389 } else if (!strcmp(*args
, "-nmin")) {
392 nmin
= strtonum(*args
, 0, INT_MAX
, &errstr
);
395 "Illegal update period %s: %s\n",
404 } else if (!strcmp(*args
, "-nrequest")) {
407 accept_count
= strtonum(*args
, 0, INT_MAX
, &errstr
);
410 "Illegal accept count %s: %s\n",
416 } else if (!strcmp(*args
, "-ndays")) {
419 ndays
= strtonum(*args
, 0, INT_MAX
, &errstr
);
422 "Illegal update period %s: %s\n",
428 } else if (!strcmp(*args
, "-rsigner")) {
434 } else if (!strcmp(*args
, "-rkey")) {
440 } else if (!strcmp(*args
, "-rother")) {
446 } else if ((cert_id_md
= EVP_get_digestbyname((*args
) + 1)) == NULL
) {
452 /* Have we anything to do? */
453 if (!req
&& !reqin
&& !respin
&& !(port
&& ridx_filename
))
457 BIO_printf(bio_err
, "OCSP utility\n");
458 BIO_printf(bio_err
, "Usage ocsp [options]\n");
459 BIO_printf(bio_err
, "where options are\n");
460 BIO_printf(bio_err
, "-out file output filename\n");
461 BIO_printf(bio_err
, "-issuer file issuer certificate\n");
462 BIO_printf(bio_err
, "-cert file certificate to check\n");
463 BIO_printf(bio_err
, "-serial n serial number to check\n");
464 BIO_printf(bio_err
, "-signer file certificate to sign OCSP request with\n");
465 BIO_printf(bio_err
, "-signkey file private key to sign OCSP request with\n");
466 BIO_printf(bio_err
, "-sign_other file additional certificates to include in signed request\n");
467 BIO_printf(bio_err
, "-no_certs don't include any certificates in signed request\n");
468 BIO_printf(bio_err
, "-req_text print text form of request\n");
469 BIO_printf(bio_err
, "-resp_text print text form of response\n");
470 BIO_printf(bio_err
, "-text print text form of request and response\n");
471 BIO_printf(bio_err
, "-reqout file write DER encoded OCSP request to \"file\"\n");
472 BIO_printf(bio_err
, "-respout file write DER encoded OCSP reponse to \"file\"\n");
473 BIO_printf(bio_err
, "-reqin file read DER encoded OCSP request from \"file\"\n");
474 BIO_printf(bio_err
, "-respin file read DER encoded OCSP reponse from \"file\"\n");
475 BIO_printf(bio_err
, "-nonce add OCSP nonce to request\n");
476 BIO_printf(bio_err
, "-no_nonce don't add OCSP nonce to request\n");
477 BIO_printf(bio_err
, "-url URL OCSP responder URL\n");
478 BIO_printf(bio_err
, "-host host:n send OCSP request to host on port n\n");
479 BIO_printf(bio_err
, "-path path to use in OCSP request\n");
480 BIO_printf(bio_err
, "-CApath dir trusted certificates directory\n");
481 BIO_printf(bio_err
, "-CAfile file trusted certificates file\n");
482 BIO_printf(bio_err
, "-VAfile file validator certificates file\n");
483 BIO_printf(bio_err
, "-validity_period n maximum validity discrepancy in seconds\n");
484 BIO_printf(bio_err
, "-status_age n maximum status age in seconds\n");
485 BIO_printf(bio_err
, "-noverify don't verify response at all\n");
486 BIO_printf(bio_err
, "-verify_other file additional certificates to search for signer\n");
487 BIO_printf(bio_err
, "-trust_other don't verify additional certificates\n");
488 BIO_printf(bio_err
, "-no_intern don't search certificates contained in response for signer\n");
489 BIO_printf(bio_err
, "-no_signature_verify don't check signature on response\n");
490 BIO_printf(bio_err
, "-no_cert_verify don't check signing certificate\n");
491 BIO_printf(bio_err
, "-no_chain don't chain verify response\n");
492 BIO_printf(bio_err
, "-no_cert_checks don't do additional checks on signing certificate\n");
493 BIO_printf(bio_err
, "-port num port to run responder on\n");
494 BIO_printf(bio_err
, "-index file certificate status index file\n");
495 BIO_printf(bio_err
, "-CA file CA certificate\n");
496 BIO_printf(bio_err
, "-rsigner file responder certificate to sign responses with\n");
497 BIO_printf(bio_err
, "-rkey file responder key to sign responses with\n");
498 BIO_printf(bio_err
, "-rother file other certificates to include in response\n");
499 BIO_printf(bio_err
, "-resp_no_certs don't include any certificates in response\n");
500 BIO_printf(bio_err
, "-nmin n number of minutes before next update\n");
501 BIO_printf(bio_err
, "-ndays n number of days before next update\n");
502 BIO_printf(bio_err
, "-resp_key_id identify reponse by signing certificate key ID\n");
503 BIO_printf(bio_err
, "-nrequest n number of requests to accept (default unlimited)\n");
504 BIO_printf(bio_err
, "-<dgst alg> use specified digest in the request\n");
508 out
= BIO_new_file(outfile
, "w");
510 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
513 BIO_printf(bio_err
, "Error opening output file\n");
516 if (!req
&& (add_nonce
!= 2))
520 derbio
= BIO_new_file(reqin
, "rb");
522 BIO_printf(bio_err
, "Error Opening OCSP request file\n");
525 req
= d2i_OCSP_REQUEST_bio(derbio
, NULL
);
528 BIO_printf(bio_err
, "Error reading OCSP request\n");
533 acbio
= init_responder(port
);
537 if (rsignfile
&& !rdb
) {
539 rkeyfile
= rsignfile
;
540 rsigner
= load_cert(bio_err
, rsignfile
, FORMAT_PEM
,
541 NULL
, "responder certificate");
543 BIO_printf(bio_err
, "Error loading responder certificate\n");
546 rca_cert
= load_cert(bio_err
, rca_filename
, FORMAT_PEM
,
547 NULL
, "CA certificate");
549 rother
= load_certs(bio_err
, rcertfile
, FORMAT_PEM
,
550 NULL
, "responder other certificates");
554 rkey
= load_key(bio_err
, rkeyfile
, FORMAT_PEM
, 0, NULL
,
555 "responder private key");
560 BIO_printf(bio_err
, "Waiting for OCSP client connections...\n");
565 if (!do_responder(&req
, &cbio
, acbio
, port
))
568 resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
569 send_ocsp_response(cbio
, resp
);
573 if (!req
&& (signfile
|| reqout
|| host
|| add_nonce
|| ridx_filename
)) {
574 BIO_printf(bio_err
, "Need an OCSP request for this operation!\n");
577 if (req
&& add_nonce
)
578 OCSP_request_add1_nonce(req
, NULL
, -1);
583 signer
= load_cert(bio_err
, signfile
, FORMAT_PEM
,
584 NULL
, "signer certificate");
586 BIO_printf(bio_err
, "Error loading signer certificate\n");
590 sign_other
= load_certs(bio_err
, sign_certfile
, FORMAT_PEM
,
591 NULL
, "signer certificates");
595 key
= load_key(bio_err
, keyfile
, FORMAT_PEM
, 0, NULL
,
596 "signer private key");
600 if (!OCSP_request_sign(req
, signer
, key
, NULL
, sign_other
, sign_flags
)) {
601 BIO_printf(bio_err
, "Error signing OCSP request\n");
606 OCSP_REQUEST_print(out
, req
, 0);
609 derbio
= BIO_new_file(reqout
, "wb");
611 BIO_printf(bio_err
, "Error opening file %s\n", reqout
);
614 i2d_OCSP_REQUEST_bio(derbio
, req
);
617 if (ridx_filename
&& (!rkey
|| !rsigner
|| !rca_cert
)) {
618 BIO_printf(bio_err
, "Need a responder certificate, key and CA for this operation!\n");
621 if (ridx_filename
&& !rdb
) {
622 rdb
= load_index(ridx_filename
, NULL
);
625 if (!index_index(rdb
))
629 i
= make_ocsp_response(&resp
, req
, rdb
, rca_cert
, rsigner
, rkey
, rother
, rflags
, nmin
, ndays
);
631 send_ocsp_response(cbio
, resp
);
633 resp
= process_responder(bio_err
, req
, host
, path
? path
: "/",
634 port
, use_ssl
, headers
, req_timeout
);
638 derbio
= BIO_new_file(respin
, "rb");
640 BIO_printf(bio_err
, "Error Opening OCSP response file\n");
643 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
646 BIO_printf(bio_err
, "Error reading OCSP response\n");
657 derbio
= BIO_new_file(respout
, "wb");
659 BIO_printf(bio_err
, "Error opening file %s\n", respout
);
662 i2d_OCSP_RESPONSE_bio(derbio
, resp
);
665 i
= OCSP_response_status(resp
);
667 if (i
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
668 BIO_printf(bio_err
, "Responder Error: %s (%d)\n",
669 OCSP_response_status_str(i
), i
);
676 OCSP_RESPONSE_print(out
, resp
, 0);
678 /* If running as responder don't verify our own response */
680 if (accept_count
> 0)
682 /* Redo if more connections needed */
686 OCSP_REQUEST_free(req
);
688 OCSP_RESPONSE_free(resp
);
695 store
= setup_verify(bio_err
, CAfile
, CApath
);
698 if (verify_certfile
) {
699 verify_other
= load_certs(bio_err
, verify_certfile
, FORMAT_PEM
,
700 NULL
, "validator certificate");
704 bs
= OCSP_response_get1_basic(resp
);
707 BIO_printf(bio_err
, "Error parsing response\n");
711 if (req
&& ((i
= OCSP_check_nonce(req
, bs
)) <= 0)) {
713 BIO_printf(bio_err
, "WARNING: no nonce in response\n");
715 BIO_printf(bio_err
, "Nonce Verify error\n");
719 i
= OCSP_basic_verify(bs
, verify_other
, store
, verify_flags
);
721 i
= OCSP_basic_verify(bs
, NULL
, store
, 0);
724 BIO_printf(bio_err
, "Response Verify Failure\n");
725 ERR_print_errors(bio_err
);
727 BIO_printf(bio_err
, "Response verify OK\n");
730 if (!print_ocsp_summary(out
, bs
, req
, reqnames
, ids
, nsec
, maxage
))
736 ERR_print_errors(bio_err
);
738 X509_STORE_free(store
);
749 OCSP_REQUEST_free(req
);
750 OCSP_RESPONSE_free(resp
);
751 OCSP_BASICRESP_free(bs
);
752 sk_OPENSSL_STRING_free(reqnames
);
753 sk_OCSP_CERTID_free(ids
);
754 sk_X509_pop_free(sign_other
, X509_free
);
755 sk_X509_pop_free(verify_other
, X509_free
);
756 sk_CONF_VALUE_pop_free(headers
, X509V3_conf_free
);
767 add_ocsp_cert(OCSP_REQUEST
** req
, X509
* cert
, const EVP_MD
* cert_id_md
, X509
* issuer
,
768 STACK_OF(OCSP_CERTID
) * ids
)
772 BIO_printf(bio_err
, "No issuer certificate specified\n");
776 *req
= OCSP_REQUEST_new();
779 id
= OCSP_cert_to_id(cert_id_md
, cert
, issuer
);
780 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
782 if (!OCSP_request_add0_id(*req
, id
))
787 BIO_printf(bio_err
, "Error Creating OCSP request\n");
792 add_ocsp_serial(OCSP_REQUEST
** req
, char *serial
, const EVP_MD
* cert_id_md
, X509
* issuer
,
793 STACK_OF(OCSP_CERTID
) * ids
)
797 ASN1_BIT_STRING
*ikey
;
800 BIO_printf(bio_err
, "No issuer certificate specified\n");
804 *req
= OCSP_REQUEST_new();
807 iname
= X509_get_subject_name(issuer
);
808 ikey
= X509_get0_pubkey_bitstr(issuer
);
809 sno
= s2i_ASN1_INTEGER(NULL
, serial
);
811 BIO_printf(bio_err
, "Error converting serial number %s\n", serial
);
814 id
= OCSP_cert_id_new(cert_id_md
, iname
, ikey
, sno
);
815 ASN1_INTEGER_free(sno
);
816 if (!id
|| !sk_OCSP_CERTID_push(ids
, id
))
818 if (!OCSP_request_add0_id(*req
, id
))
823 BIO_printf(bio_err
, "Error Creating OCSP request\n");
828 print_ocsp_summary(BIO
* out
, OCSP_BASICRESP
* bs
, OCSP_REQUEST
* req
,
829 STACK_OF(OPENSSL_STRING
) * names
,
830 STACK_OF(OCSP_CERTID
) * ids
, long nsec
,
839 ASN1_GENERALIZEDTIME
*rev
, *thisupd
, *nextupd
;
841 if (!bs
|| !req
|| !sk_OPENSSL_STRING_num(names
) || !sk_OCSP_CERTID_num(ids
))
844 for (i
= 0; i
< sk_OCSP_CERTID_num(ids
); i
++) {
845 id
= sk_OCSP_CERTID_value(ids
, i
);
846 name
= sk_OPENSSL_STRING_value(names
, i
);
847 BIO_printf(out
, "%s: ", name
);
849 if (!OCSP_resp_find_status(bs
, id
, &status
, &reason
,
850 &rev
, &thisupd
, &nextupd
)) {
851 BIO_puts(out
, "ERROR: No Status found.\n");
855 * Check validity: if invalid write to output BIO so we know
856 * which response this refers to.
858 if (!OCSP_check_validity(thisupd
, nextupd
, nsec
, maxage
)) {
859 BIO_puts(out
, "WARNING: Status times invalid.\n");
860 ERR_print_errors(out
);
862 BIO_printf(out
, "%s\n", OCSP_cert_status_str(status
));
864 BIO_puts(out
, "\tThis Update: ");
865 ASN1_GENERALIZEDTIME_print(out
, thisupd
);
869 BIO_puts(out
, "\tNext Update: ");
870 ASN1_GENERALIZEDTIME_print(out
, nextupd
);
873 if (status
!= V_OCSP_CERTSTATUS_REVOKED
)
877 BIO_printf(out
, "\tReason: %s\n",
878 OCSP_crl_reason_str(reason
));
880 BIO_puts(out
, "\tRevocation Time: ");
881 ASN1_GENERALIZEDTIME_print(out
, rev
);
890 make_ocsp_response(OCSP_RESPONSE
** resp
, OCSP_REQUEST
* req
, CA_DB
* db
,
891 X509
* ca
, X509
* rcert
, EVP_PKEY
* rkey
,
892 STACK_OF(X509
) * rother
, unsigned long flags
,
895 ASN1_TIME
*thisupd
= NULL
, *nextupd
= NULL
;
896 OCSP_CERTID
*cid
, *ca_id
= NULL
;
897 OCSP_BASICRESP
*bs
= NULL
;
898 int i
, id_count
, ret
= 1;
900 id_count
= OCSP_request_onereq_count(req
);
903 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
906 bs
= OCSP_BASICRESP_new();
907 thisupd
= X509_gmtime_adj(NULL
, 0);
909 nextupd
= X509_gmtime_adj(NULL
, nmin
* 60 + ndays
* 3600 * 24);
911 /* Examine each certificate id in the request */
912 for (i
= 0; i
< id_count
; i
++) {
914 ASN1_INTEGER
*serial
;
916 ASN1_OBJECT
*cert_id_md_oid
;
917 const EVP_MD
*cert_id_md
;
918 one
= OCSP_request_onereq_get0(req
, i
);
919 cid
= OCSP_onereq_get0_id(one
);
921 OCSP_id_get0_info(NULL
, &cert_id_md_oid
, NULL
, NULL
, cid
);
923 cert_id_md
= EVP_get_digestbyobj(cert_id_md_oid
);
925 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
929 OCSP_CERTID_free(ca_id
);
930 ca_id
= OCSP_cert_to_id(cert_id_md
, NULL
, ca
);
932 /* Is this request about our CA? */
933 if (OCSP_id_issuer_cmp(ca_id
, cid
)) {
934 OCSP_basic_add1_status(bs
, cid
,
935 V_OCSP_CERTSTATUS_UNKNOWN
,
940 OCSP_id_get0_info(NULL
, NULL
, NULL
, &serial
, cid
);
941 inf
= lookup_serial(db
, serial
);
943 OCSP_basic_add1_status(bs
, cid
,
944 V_OCSP_CERTSTATUS_UNKNOWN
,
947 else if (inf
[DB_type
][0] == DB_TYPE_VAL
)
948 OCSP_basic_add1_status(bs
, cid
,
949 V_OCSP_CERTSTATUS_GOOD
,
952 else if (inf
[DB_type
][0] == DB_TYPE_REV
) {
953 ASN1_OBJECT
*inst
= NULL
;
954 ASN1_TIME
*revtm
= NULL
;
955 ASN1_GENERALIZEDTIME
*invtm
= NULL
;
956 OCSP_SINGLERESP
*single
;
958 unpack_revinfo(&revtm
, &reason
, &inst
, &invtm
, inf
[DB_rev_date
]);
959 single
= OCSP_basic_add1_status(bs
, cid
,
960 V_OCSP_CERTSTATUS_REVOKED
,
964 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_invalidity_date
, invtm
, 0, 0);
966 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_hold_instruction_code
, inst
, 0, 0);
967 ASN1_OBJECT_free(inst
);
968 ASN1_TIME_free(revtm
);
969 ASN1_GENERALIZEDTIME_free(invtm
);
973 OCSP_copy_nonce(bs
, req
);
975 OCSP_basic_sign(bs
, rcert
, rkey
, NULL
, rother
, flags
);
977 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL
, bs
);
980 ASN1_TIME_free(thisupd
);
981 ASN1_TIME_free(nextupd
);
982 OCSP_CERTID_free(ca_id
);
983 OCSP_BASICRESP_free(bs
);
989 lookup_serial(CA_DB
* db
, ASN1_INTEGER
* ser
)
993 char *itmp
, *row
[DB_NUMBER
], **rrow
;
994 for (i
= 0; i
< DB_NUMBER
; i
++)
996 bn
= ASN1_INTEGER_to_BN(ser
, NULL
);
997 OPENSSL_assert(bn
); /* FIXME: should report an error at this
1000 itmp
= strdup("00");
1002 itmp
= BN_bn2hex(bn
);
1003 row
[DB_serial
] = itmp
;
1005 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1010 /* Quick and dirty OCSP server: read in and parse input request */
1013 init_responder(char *port
)
1015 BIO
*acbio
= NULL
, *bufbio
= NULL
;
1016 bufbio
= BIO_new(BIO_f_buffer());
1019 acbio
= BIO_new_accept(port
);
1022 BIO_set_accept_bios(acbio
, bufbio
);
1025 if (BIO_do_accept(acbio
) <= 0) {
1026 BIO_printf(bio_err
, "Error setting up accept BIO\n");
1027 ERR_print_errors(bio_err
);
1033 BIO_free_all(acbio
);
1039 do_responder(OCSP_REQUEST
** preq
, BIO
** pcbio
, BIO
* acbio
, char *port
)
1041 int have_post
= 0, len
;
1042 OCSP_REQUEST
*req
= NULL
;
1046 if (BIO_do_accept(acbio
) <= 0) {
1047 BIO_printf(bio_err
, "Error accepting connection\n");
1048 ERR_print_errors(bio_err
);
1051 cbio
= BIO_pop(acbio
);
1055 len
= BIO_gets(cbio
, inbuf
, sizeof inbuf
);
1058 /* Look for "POST" signalling start of query */
1060 if (strncmp(inbuf
, "POST", 4)) {
1061 BIO_printf(bio_err
, "Invalid request\n");
1066 /* Look for end of headers */
1067 if ((inbuf
[0] == '\r') || (inbuf
[0] == '\n'))
1071 /* Try to read OCSP request */
1073 req
= d2i_OCSP_REQUEST_bio(cbio
, NULL
);
1076 BIO_printf(bio_err
, "Error parsing OCSP request\n");
1077 ERR_print_errors(bio_err
);
1086 send_ocsp_response(BIO
* cbio
, OCSP_RESPONSE
* resp
)
1088 static const char http_resp
[] =
1089 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1090 "Content-Length: %d\r\n\r\n";
1093 BIO_printf(cbio
, http_resp
, i2d_OCSP_RESPONSE(resp
, NULL
));
1094 i2d_OCSP_RESPONSE_bio(cbio
, resp
);
1095 (void) BIO_flush(cbio
);
1099 static OCSP_RESPONSE
*
1100 query_responder(BIO
* err
, BIO
* cbio
, char *path
,
1101 STACK_OF(CONF_VALUE
) * headers
,
1102 OCSP_REQUEST
* req
, int req_timeout
)
1107 OCSP_REQ_CTX
*ctx
= NULL
;
1108 OCSP_RESPONSE
*rsp
= NULL
;
1109 struct pollfd pfd
[1];
1111 if (req_timeout
!= -1)
1112 BIO_set_nbio(cbio
, 1);
1114 rv
= BIO_do_connect(cbio
);
1116 if ((rv
<= 0) && ((req_timeout
== -1) || !BIO_should_retry(cbio
))) {
1117 BIO_puts(err
, "Error connecting BIO\n");
1120 if (BIO_get_fd(cbio
, &fd
) < 0) {
1121 BIO_puts(err
, "Can't get connection fd\n");
1124 if (req_timeout
!= -1 && rv
<= 0) {
1126 pfd
[0].events
= POLLOUT
;
1127 rv
= poll(pfd
, 1, req_timeout
* 1000);
1129 BIO_puts(err
, "Timeout on connect\n");
1133 BIO_puts(err
, "Poll error\n");
1137 ctx
= OCSP_sendreq_new(cbio
, path
, NULL
, -1);
1141 for (i
= 0; i
< sk_CONF_VALUE_num(headers
); i
++) {
1142 CONF_VALUE
*hdr
= sk_CONF_VALUE_value(headers
, i
);
1143 if (!OCSP_REQ_CTX_add1_header(ctx
, hdr
->name
, hdr
->value
))
1147 if (!OCSP_REQ_CTX_set1_req(ctx
, req
))
1151 rv
= OCSP_sendreq_nbio(&rsp
, ctx
);
1154 if (req_timeout
== -1)
1157 if (BIO_should_read(cbio
))
1158 pfd
[0].events
= POLLIN
;
1159 else if (BIO_should_write(cbio
))
1160 pfd
[0].events
= POLLOUT
;
1162 BIO_puts(err
, "Unexpected retry condition\n");
1165 rv
= poll(pfd
, 1, req_timeout
* 1000);
1167 BIO_puts(err
, "Timeout on request\n");
1170 if (rv
== -1 || (pfd
[0].revents
& (POLLERR
|POLLNVAL
))) {
1171 BIO_puts(err
, "Poll error\n");
1176 OCSP_REQ_CTX_free(ctx
);
1182 process_responder(BIO
* err
, OCSP_REQUEST
* req
,
1183 char *host
, char *path
, char *port
, int use_ssl
,
1184 STACK_OF(CONF_VALUE
) * headers
,
1188 SSL_CTX
*ctx
= NULL
;
1189 OCSP_RESPONSE
*resp
= NULL
;
1190 cbio
= BIO_new_connect(host
);
1192 BIO_printf(err
, "Error creating connect BIO\n");
1196 BIO_set_conn_port(cbio
, port
);
1199 ctx
= SSL_CTX_new(SSLv23_client_method());
1201 BIO_printf(err
, "Error creating SSL context.\n");
1204 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1205 sbio
= BIO_new_ssl(ctx
, 1);
1206 cbio
= BIO_push(sbio
, cbio
);
1208 resp
= query_responder(err
, cbio
, path
, headers
, req
, req_timeout
);
1210 BIO_printf(bio_err
, "Error querying OCSP responder\n");