2 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
12 #ifdef OPENSSL_SYS_VMS
13 /* So fd_set and friends get properly defined on OpenVMS */
14 # define _XOPEN_SOURCE_EXTENDED
23 /* Needs to be included before the openssl headers */
25 #include "http_server.h"
27 #include "internal/sockets.h"
28 #include <openssl/e_os2.h>
29 #include <openssl/crypto.h>
30 #include <openssl/err.h>
31 #include <openssl/ssl.h>
32 #include <openssl/evp.h>
33 #include <openssl/bn.h>
34 #include <openssl/x509v3.h>
36 #if defined(OPENSSL_SYS_VXWORKS)
38 int setpgid(pid_t pid
, pid_t pgid
)
50 /* Maximum leeway in validity period: default 5 minutes */
51 #define MAX_VALIDITY_PERIOD (5 * 60)
53 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
54 const EVP_MD
*cert_id_md
, X509
*issuer
,
55 STACK_OF(OCSP_CERTID
) *ids
);
56 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
57 const EVP_MD
*cert_id_md
, X509
*issuer
,
58 STACK_OF(OCSP_CERTID
) *ids
);
59 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
60 STACK_OF(OPENSSL_STRING
) *names
,
61 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
63 static void make_ocsp_response(BIO
*err
, OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
64 CA_DB
*db
, STACK_OF(X509
) *ca
, X509
*rcert
,
65 EVP_PKEY
*rkey
, const EVP_MD
*md
,
66 STACK_OF(OPENSSL_STRING
) *sigopts
,
67 STACK_OF(X509
) *rother
, unsigned long flags
,
68 int nmin
, int ndays
, int badsig
,
69 const EVP_MD
*resp_md
);
71 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
);
72 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
74 static int send_ocsp_response(BIO
*cbio
, const OCSP_RESPONSE
*resp
);
78 static int index_changed(CA_DB
*);
81 typedef enum OPTION_choice
{
83 OPT_OUTFILE
, OPT_TIMEOUT
, OPT_URL
, OPT_HOST
, OPT_PORT
,
84 #ifndef OPENSSL_NO_SOCK
85 OPT_PROXY
, OPT_NO_PROXY
,
87 OPT_IGNORE_ERR
, OPT_NOVERIFY
, OPT_NONCE
, OPT_NO_NONCE
,
88 OPT_RESP_NO_CERTS
, OPT_RESP_KEY_ID
, OPT_NO_CERTS
,
89 OPT_NO_SIGNATURE_VERIFY
, OPT_NO_CERT_VERIFY
, OPT_NO_CHAIN
,
90 OPT_NO_CERT_CHECKS
, OPT_NO_EXPLICIT
, OPT_TRUST_OTHER
,
91 OPT_NO_INTERN
, OPT_BADSIG
, OPT_TEXT
, OPT_REQ_TEXT
, OPT_RESP_TEXT
,
92 OPT_REQIN
, OPT_RESPIN
, OPT_SIGNER
, OPT_VAFILE
, OPT_SIGN_OTHER
,
93 OPT_VERIFY_OTHER
, OPT_CAFILE
, OPT_CAPATH
, OPT_CASTORE
, OPT_NOCAFILE
,
94 OPT_NOCAPATH
, OPT_NOCASTORE
,
95 OPT_VALIDITY_PERIOD
, OPT_STATUS_AGE
, OPT_SIGNKEY
, OPT_REQOUT
,
96 OPT_RESPOUT
, OPT_PATH
, OPT_ISSUER
, OPT_CERT
, OPT_SERIAL
,
97 OPT_INDEX
, OPT_CA
, OPT_NMIN
, OPT_REQUEST
, OPT_NDAYS
, OPT_RSIGNER
,
98 OPT_RKEY
, OPT_ROTHER
, OPT_RMD
, OPT_RSIGOPT
, OPT_HEADER
,
103 OPT_MULTI
, OPT_PROV_ENUM
106 const OPTIONS ocsp_options
[] = {
107 OPT_SECTION("General"),
108 {"help", OPT_HELP
, '-', "Display this summary"},
109 {"ignore_err", OPT_IGNORE_ERR
, '-',
110 "Ignore error on OCSP request or response and continue running"},
111 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
112 {"CApath", OPT_CAPATH
, '<', "Trusted certificates directory"},
113 {"CAstore", OPT_CASTORE
, ':', "Trusted certificates store URI"},
114 {"no-CAfile", OPT_NOCAFILE
, '-',
115 "Do not load the default certificates file"},
116 {"no-CApath", OPT_NOCAPATH
, '-',
117 "Do not load certificates from the default certificates directory"},
118 {"no-CAstore", OPT_NOCASTORE
, '-',
119 "Do not load certificates from the default certificates store"},
121 OPT_SECTION("Responder"),
122 {"timeout", OPT_TIMEOUT
, 'p',
123 "Connection timeout (in seconds) to the OCSP responder"},
124 {"resp_no_certs", OPT_RESP_NO_CERTS
, '-',
125 "Don't include any certificates in response"},
127 {"multi", OPT_MULTI
, 'p', "run multiple responder processes"},
129 {"no_certs", OPT_NO_CERTS
, '-',
130 "Don't include any certificates in signed request"},
131 {"badsig", OPT_BADSIG
, '-',
132 "Corrupt last byte of loaded OCSP response signature (for test)"},
133 {"CA", OPT_CA
, '<', "CA certificates"},
134 {"nmin", OPT_NMIN
, 'p', "Number of minutes before next update"},
135 {"nrequest", OPT_REQUEST
, 'p',
136 "Number of requests to accept (default unlimited)"},
137 {"reqin", OPT_REQIN
, 's', "File with the DER-encoded request"},
138 {"signer", OPT_SIGNER
, '<', "Certificate to sign OCSP request with"},
139 {"sign_other", OPT_SIGN_OTHER
, '<',
140 "Additional certificates to include in signed request"},
141 {"index", OPT_INDEX
, '<', "Certificate status index file"},
142 {"ndays", OPT_NDAYS
, 'p', "Number of days before next update"},
143 {"rsigner", OPT_RSIGNER
, '<',
144 "Responder certificate to sign responses with"},
145 {"rkey", OPT_RKEY
, '<', "Responder key to sign responses with"},
146 {"passin", OPT_PASSIN
, 's', "Responder key pass phrase source"},
147 {"rother", OPT_ROTHER
, '<', "Other certificates to include in response"},
148 {"rmd", OPT_RMD
, 's', "Digest Algorithm to use in signature of OCSP response"},
149 {"rsigopt", OPT_RSIGOPT
, 's', "OCSP response signature parameter in n:v form"},
150 {"header", OPT_HEADER
, 's', "key=value header to add"},
151 {"rcid", OPT_RCID
, 's', "Use specified algorithm for cert id in response"},
152 {"", OPT_MD
, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
154 OPT_SECTION("Client"),
155 {"url", OPT_URL
, 's', "Responder URL"},
156 {"host", OPT_HOST
, 's', "TCP/IP hostname:port to connect to"},
157 {"port", OPT_PORT
, 'N', "Port to run responder on"},
158 {"path", OPT_PATH
, 's', "Path to use in OCSP request"},
159 #ifndef OPENSSL_NO_SOCK
160 {"proxy", OPT_PROXY
, 's',
161 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
162 {"no_proxy", OPT_NO_PROXY
, 's',
163 "List of addresses of servers not to use HTTP(S) proxy for"},
165 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
167 {"out", OPT_OUTFILE
, '>', "Output filename"},
168 {"noverify", OPT_NOVERIFY
, '-', "Don't verify response at all"},
169 {"nonce", OPT_NONCE
, '-', "Add OCSP nonce to request"},
170 {"no_nonce", OPT_NO_NONCE
, '-', "Don't add OCSP nonce to request"},
171 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY
, '-',
172 "Don't check signature on response"},
173 {"resp_key_id", OPT_RESP_KEY_ID
, '-',
174 "Identify response by signing certificate key ID"},
175 {"no_cert_verify", OPT_NO_CERT_VERIFY
, '-',
176 "Don't check signing certificate"},
177 {"text", OPT_TEXT
, '-', "Print text form of request and response"},
178 {"req_text", OPT_REQ_TEXT
, '-', "Print text form of request"},
179 {"resp_text", OPT_RESP_TEXT
, '-', "Print text form of response"},
180 {"no_chain", OPT_NO_CHAIN
, '-', "Don't chain verify response"},
181 {"no_cert_checks", OPT_NO_CERT_CHECKS
, '-',
182 "Don't do additional checks on signing certificate"},
183 {"no_explicit", OPT_NO_EXPLICIT
, '-',
184 "Do not explicitly check the chain, just verify the root"},
185 {"trust_other", OPT_TRUST_OTHER
, '-',
186 "Don't verify additional certificates"},
187 {"no_intern", OPT_NO_INTERN
, '-',
188 "Don't search certificates contained in response for signer"},
189 {"respin", OPT_RESPIN
, 's', "File with the DER-encoded response"},
190 {"VAfile", OPT_VAFILE
, '<', "Validator certificates file"},
191 {"verify_other", OPT_VERIFY_OTHER
, '<',
192 "Additional certificates to search for signer"},
193 {"cert", OPT_CERT
, '<',
194 "Certificate to check; may be given multiple times"},
195 {"serial", OPT_SERIAL
, 's',
196 "Serial number to check; may be given multiple times"},
197 {"validity_period", OPT_VALIDITY_PERIOD
, 'u',
198 "Maximum validity discrepancy in seconds"},
199 {"signkey", OPT_SIGNKEY
, 's', "Private key to sign OCSP request with"},
200 {"reqout", OPT_REQOUT
, 's', "Output file for the DER-encoded request"},
201 {"respout", OPT_RESPOUT
, 's', "Output file for the DER-encoded response"},
202 {"issuer", OPT_ISSUER
, '<', "Issuer certificate"},
203 {"status_age", OPT_STATUS_AGE
, 'p', "Maximum status age in seconds"},
210 int ocsp_main(int argc
, char **argv
)
212 BIO
*acbio
= NULL
, *cbio
= NULL
, *derbio
= NULL
, *out
= NULL
;
213 EVP_MD
*cert_id_md
= NULL
, *rsign_md
= NULL
;
214 STACK_OF(OPENSSL_STRING
) *rsign_sigopts
= NULL
;
217 EVP_PKEY
*key
= NULL
, *rkey
= NULL
;
218 OCSP_BASICRESP
*bs
= NULL
;
219 OCSP_REQUEST
*req
= NULL
;
220 OCSP_RESPONSE
*resp
= NULL
;
221 STACK_OF(CONF_VALUE
) *headers
= NULL
;
222 STACK_OF(OCSP_CERTID
) *ids
= NULL
;
223 STACK_OF(OPENSSL_STRING
) *reqnames
= NULL
;
224 STACK_OF(X509
) *sign_other
= NULL
, *verify_other
= NULL
, *rother
= NULL
;
225 STACK_OF(X509
) *issuers
= NULL
;
226 X509
*issuer
= NULL
, *cert
= NULL
;
227 STACK_OF(X509
) *rca_certs
= NULL
;
228 EVP_MD
*resp_certid_md
= NULL
;
229 X509
*signer
= NULL
, *rsigner
= NULL
;
230 X509_STORE
*store
= NULL
;
231 X509_VERIFY_PARAM
*vpm
= NULL
;
232 const char *CAfile
= NULL
, *CApath
= NULL
, *CAstore
= NULL
;
233 char *header
, *value
, *respdigname
= NULL
;
234 char *host
= NULL
, *port
= NULL
, *path
= "/", *outfile
= NULL
;
235 #ifndef OPENSSL_NO_SOCK
236 char *opt_proxy
= NULL
;
237 char *opt_no_proxy
= NULL
;
239 char *rca_filename
= NULL
, *reqin
= NULL
, *respin
= NULL
;
240 char *reqout
= NULL
, *respout
= NULL
, *ridx_filename
= NULL
;
241 char *rsignfile
= NULL
, *rkeyfile
= NULL
;
242 char *passinarg
= NULL
, *passin
= NULL
;
243 char *sign_certfile
= NULL
, *verify_certfile
= NULL
, *rcertfile
= NULL
;
244 char *signfile
= NULL
, *keyfile
= NULL
;
245 char *thost
= NULL
, *tport
= NULL
, *tpath
= NULL
;
246 int noCAfile
= 0, noCApath
= 0, noCAstore
= 0;
247 int accept_count
= -1, add_nonce
= 1, noverify
= 0, use_ssl
= -1;
248 int vpmtouched
= 0, badsig
= 0, i
, ignore_err
= 0, nmin
= 0, ndays
= -1;
249 int req_text
= 0, resp_text
= 0, res
, ret
= 1;
250 int req_timeout
= -1;
251 long nsec
= MAX_VALIDITY_PERIOD
, maxage
= -1;
252 unsigned long sign_flags
= 0, verify_flags
= 0, rflags
= 0;
255 if ((reqnames
= sk_OPENSSL_STRING_new_null()) == NULL
256 || (ids
= sk_OCSP_CERTID_new_null()) == NULL
257 || (vpm
= X509_VERIFY_PARAM_new()) == NULL
)
260 opt_set_unknown_name("digest");
261 prog
= opt_init(argc
, argv
, ocsp_options
);
262 while ((o
= opt_next()) != OPT_EOF
) {
267 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
271 opt_help(ocsp_options
);
277 #ifndef OPENSSL_NO_SOCK
278 req_timeout
= atoi(opt_arg());
285 thost
= tport
= tpath
= NULL
;
286 if (!OSSL_HTTP_parse_url(opt_arg(), &use_ssl
, NULL
/* userinfo */,
287 &host
, &port
, NULL
/* port_num */,
288 &path
, NULL
/* qry */, NULL
/* frag */)) {
289 BIO_printf(bio_err
, "%s Error parsing -url argument\n", prog
);
305 #ifndef OPENSSL_NO_SOCK
307 opt_proxy
= opt_arg();
310 opt_no_proxy
= opt_arg();
325 case OPT_RESP_NO_CERTS
:
326 rflags
|= OCSP_NOCERTS
;
328 case OPT_RESP_KEY_ID
:
329 rflags
|= OCSP_RESPID_KEY
;
332 sign_flags
|= OCSP_NOCERTS
;
334 case OPT_NO_SIGNATURE_VERIFY
:
335 verify_flags
|= OCSP_NOSIGS
;
337 case OPT_NO_CERT_VERIFY
:
338 verify_flags
|= OCSP_NOVERIFY
;
341 verify_flags
|= OCSP_NOCHAIN
;
343 case OPT_NO_CERT_CHECKS
:
344 verify_flags
|= OCSP_NOCHECKS
;
346 case OPT_NO_EXPLICIT
:
347 verify_flags
|= OCSP_NOEXPLICIT
;
349 case OPT_TRUST_OTHER
:
350 verify_flags
|= OCSP_TRUSTOTHER
;
353 verify_flags
|= OCSP_NOINTERN
;
359 req_text
= resp_text
= 1;
374 signfile
= opt_arg();
377 verify_certfile
= opt_arg();
378 verify_flags
|= OCSP_TRUSTOTHER
;
381 sign_certfile
= opt_arg();
383 case OPT_VERIFY_OTHER
:
384 verify_certfile
= opt_arg();
405 if (!opt_verify(o
, vpm
))
409 case OPT_VALIDITY_PERIOD
:
410 opt_long(opt_arg(), &nsec
);
413 opt_long(opt_arg(), &maxage
);
425 issuer
= load_cert(opt_arg(), FORMAT_UNDEF
, "issuer certificate");
428 if (issuers
== NULL
) {
429 if ((issuers
= sk_X509_new_null()) == NULL
)
432 if (!sk_X509_push(issuers
, issuer
))
438 cert
= load_cert(opt_arg(), FORMAT_UNDEF
, "certificate");
441 if (cert_id_md
== NULL
)
442 cert_id_md
= (EVP_MD
*)EVP_sha1();
443 if (!add_ocsp_cert(&req
, cert
, cert_id_md
, issuer
, ids
))
445 if (!sk_OPENSSL_STRING_push(reqnames
, opt_arg()))
451 if (cert_id_md
== NULL
)
452 cert_id_md
= (EVP_MD
*)EVP_sha1();
453 if (!add_ocsp_serial(&req
, opt_arg(), cert_id_md
, issuer
, ids
))
455 if (!sk_OPENSSL_STRING_push(reqnames
, opt_arg()))
460 ridx_filename
= opt_arg();
463 rca_filename
= opt_arg();
466 nmin
= opt_int_arg();
471 accept_count
= opt_int_arg();
474 ndays
= atoi(opt_arg());
477 rsignfile
= opt_arg();
480 rkeyfile
= opt_arg();
483 passinarg
= opt_arg();
486 rcertfile
= opt_arg();
488 case OPT_RMD
: /* Response MessageDigest */
489 respdigname
= opt_arg();
492 if (rsign_sigopts
== NULL
)
493 rsign_sigopts
= sk_OPENSSL_STRING_new_null();
494 if (rsign_sigopts
== NULL
495 || !sk_OPENSSL_STRING_push(rsign_sigopts
, opt_arg()))
500 value
= strchr(header
, '=');
502 BIO_printf(bio_err
, "Missing = in header key=value\n");
506 if (!X509V3_add_value(header
, value
, &headers
))
510 if (!opt_md(opt_arg(), &resp_certid_md
))
516 "%s: Digest must be before -cert or -serial\n",
520 if (!opt_md(opt_unknown(), &cert_id_md
))
526 n_responders
= atoi(opt_arg());
530 if (!opt_provider(o
))
536 /* No extra arguments. */
537 if (!opt_check_rest_arg(NULL
))
541 BIO_printf(bio_err
, "%s: Digest must be before -cert or -serial\n",
546 if (respdigname
!= NULL
) {
547 if (!opt_md(respdigname
, &rsign_md
))
551 /* Have we anything to do? */
552 if (req
== NULL
&& reqin
== NULL
553 && respin
== NULL
&& !(port
!= NULL
&& ridx_filename
!= NULL
))
556 out
= bio_open_default(outfile
, 'w', FORMAT_TEXT
);
560 if (req
== NULL
&& (add_nonce
!= 2))
563 if (req
== NULL
&& reqin
!= NULL
) {
564 derbio
= bio_open_default(reqin
, 'r', FORMAT_ASN1
);
567 req
= d2i_OCSP_REQUEST_bio(derbio
, NULL
);
570 BIO_printf(bio_err
, "Error reading OCSP request\n");
575 if (req
== NULL
&& port
!= NULL
) {
576 #ifndef OPENSSL_NO_SOCK
577 acbio
= http_server_init(prog
, port
, -1);
581 BIO_printf(bio_err
, "Cannot act as server - sockets not supported\n");
586 if (rsignfile
!= NULL
) {
587 if (rkeyfile
== NULL
)
588 rkeyfile
= rsignfile
;
589 rsigner
= load_cert(rsignfile
, FORMAT_UNDEF
, "responder certificate");
590 if (rsigner
== NULL
) {
591 BIO_printf(bio_err
, "Error loading responder certificate\n");
594 if (!load_certs(rca_filename
, 0, &rca_certs
, NULL
, "CA certificates"))
596 if (rcertfile
!= NULL
) {
597 if (!load_certs(rcertfile
, 0, &rother
, NULL
,
598 "responder other certificates"))
601 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
602 BIO_printf(bio_err
, "Error getting password\n");
605 rkey
= load_key(rkeyfile
, FORMAT_UNDEF
, 0, passin
, NULL
,
606 "responder private key");
611 if (ridx_filename
!= NULL
612 && (rkey
== NULL
|| rsigner
== NULL
|| rca_certs
== NULL
)) {
614 "Responder mode requires certificate, key, and CA.\n");
618 if (ridx_filename
!= NULL
) {
619 rdb
= load_index(ridx_filename
, NULL
);
620 if (rdb
== NULL
|| index_index(rdb
) <= 0) {
622 "Problem with index file: %s (could not load/parse file)\n",
630 if (n_responders
!= 0 && acbio
!= NULL
)
632 if (acbio
!= NULL
&& req_timeout
> 0)
633 signal(SIGALRM
, socket_timeout
);
637 trace_log_message(-1, prog
,
638 LOG_INFO
, "waiting for OCSP client connections...");
644 if (index_changed(rdb
)) {
645 CA_DB
*newrdb
= load_index(ridx_filename
, NULL
);
647 if (newrdb
!= NULL
&& index_index(newrdb
) > 0) {
652 trace_log_message(-1, prog
,
653 LOG_ERR
, "error reloading updated index: %s",
660 res
= do_responder(&req
, &cbio
, acbio
, req_timeout
);
667 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
,
669 send_ocsp_response(cbio
, resp
);
676 && (signfile
!= NULL
|| reqout
!= NULL
677 || host
!= NULL
|| add_nonce
|| ridx_filename
!= NULL
)) {
678 BIO_printf(bio_err
, "Need an OCSP request for this operation!\n");
682 if (req
!= NULL
&& add_nonce
) {
683 if (!OCSP_request_add1_nonce(req
, NULL
, -1))
687 if (signfile
!= NULL
) {
690 signer
= load_cert(signfile
, FORMAT_UNDEF
, "signer certificate");
691 if (signer
== NULL
) {
692 BIO_printf(bio_err
, "Error loading signer certificate\n");
695 if (sign_certfile
!= NULL
) {
696 if (!load_certs(sign_certfile
, 0, &sign_other
, NULL
,
697 "signer certificates"))
700 key
= load_key(keyfile
, FORMAT_UNDEF
, 0, NULL
, NULL
,
701 "signer private key");
705 if (!OCSP_request_sign(req
, signer
, key
, NULL
,
706 sign_other
, sign_flags
)) {
707 BIO_printf(bio_err
, "Error signing OCSP request\n");
712 if (req_text
&& req
!= NULL
)
713 OCSP_REQUEST_print(out
, req
, 0);
715 if (reqout
!= NULL
) {
716 derbio
= bio_open_default(reqout
, 'w', FORMAT_ASN1
);
719 i2d_OCSP_REQUEST_bio(derbio
, req
);
724 make_ocsp_response(bio_err
, &resp
, req
, rdb
, rca_certs
, rsigner
, rkey
,
725 rsign_md
, rsign_sigopts
, rother
, rflags
, nmin
, ndays
,
726 badsig
, resp_certid_md
);
730 send_ocsp_response(cbio
, resp
);
731 } else if (host
!= NULL
) {
732 #ifndef OPENSSL_NO_SOCK
733 resp
= process_responder(req
, host
, port
, path
, opt_proxy
, opt_no_proxy
,
734 use_ssl
, headers
, req_timeout
);
739 "Error creating connect BIO - sockets not supported\n");
742 } else if (respin
!= NULL
) {
743 derbio
= bio_open_default(respin
, 'r', FORMAT_ASN1
);
746 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
749 BIO_printf(bio_err
, "Error reading OCSP response\n");
759 if (respout
!= NULL
) {
760 derbio
= bio_open_default(respout
, 'w', FORMAT_ASN1
);
763 i2d_OCSP_RESPONSE_bio(derbio
, resp
);
767 i
= OCSP_response_status(resp
);
768 if (i
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
769 BIO_printf(out
, "Responder Error: %s (%d)\n",
770 OCSP_response_status_str(i
), i
);
776 OCSP_RESPONSE_print(out
, resp
, 0);
778 /* If running as responder don't verify our own response */
780 /* If not unlimited, see if we took all we should. */
781 if (accept_count
!= -1 && --accept_count
<= 0) {
787 OCSP_REQUEST_free(req
);
789 OCSP_RESPONSE_free(resp
);
793 if (ridx_filename
!= NULL
) {
799 store
= setup_verify(CAfile
, noCAfile
, CApath
, noCApath
,
805 X509_STORE_set1_param(store
, vpm
);
806 if (verify_certfile
!= NULL
) {
807 if (!load_certs(verify_certfile
, 0, &verify_other
, NULL
,
808 "validator certificates"))
812 bs
= OCSP_response_get1_basic(resp
);
814 BIO_printf(bio_err
, "Error parsing response\n");
821 if (req
!= NULL
&& ((i
= OCSP_check_nonce(req
, bs
)) <= 0)) {
823 BIO_printf(bio_err
, "WARNING: no nonce in response\n");
825 BIO_printf(bio_err
, "Nonce Verify error\n");
831 i
= OCSP_basic_verify(bs
, verify_other
, store
, verify_flags
);
832 if (i
<= 0 && issuers
) {
833 i
= OCSP_basic_verify(bs
, issuers
, store
, OCSP_TRUSTOTHER
);
838 BIO_printf(bio_err
, "Response Verify Failure\n");
839 ERR_print_errors(bio_err
);
842 BIO_printf(bio_err
, "Response verify OK\n");
846 if (!print_ocsp_summary(out
, bs
, req
, reqnames
, ids
, nsec
, maxage
))
850 ERR_print_errors(bio_err
);
852 X509_STORE_free(store
);
853 X509_VERIFY_PARAM_free(vpm
);
854 sk_OPENSSL_STRING_free(rsign_sigopts
);
857 EVP_MD_free(cert_id_md
);
858 EVP_MD_free(rsign_md
);
859 EVP_MD_free(resp_certid_md
);
861 OSSL_STACK_OF_X509_free(issuers
);
863 OSSL_STACK_OF_X509_free(rca_certs
);
868 OCSP_REQUEST_free(req
);
869 OCSP_RESPONSE_free(resp
);
870 OCSP_BASICRESP_free(bs
);
871 sk_OPENSSL_STRING_free(reqnames
);
872 sk_OCSP_CERTID_free(ids
);
873 OSSL_STACK_OF_X509_free(sign_other
);
874 OSSL_STACK_OF_X509_free(verify_other
);
875 sk_CONF_VALUE_pop_free(headers
, X509V3_conf_free
);
885 static int index_changed(CA_DB
*rdb
)
889 if (rdb
!= NULL
&& stat(rdb
->dbfname
, &sb
) != -1) {
890 if (rdb
->dbst
.st_mtime
!= sb
.st_mtime
891 || rdb
->dbst
.st_ctime
!= sb
.st_ctime
892 || rdb
->dbst
.st_ino
!= sb
.st_ino
893 || rdb
->dbst
.st_dev
!= sb
.st_dev
) {
894 syslog(LOG_INFO
, "index file changed, reloading");
903 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
904 const EVP_MD
*cert_id_md
, X509
*issuer
,
905 STACK_OF(OCSP_CERTID
) *ids
)
909 if (issuer
== NULL
) {
910 BIO_printf(bio_err
, "No issuer certificate specified\n");
914 *req
= OCSP_REQUEST_new();
917 id
= OCSP_cert_to_id(cert_id_md
, cert
, issuer
);
918 if (id
== NULL
|| !sk_OCSP_CERTID_push(ids
, id
))
920 if (!OCSP_request_add0_id(*req
, id
))
925 BIO_printf(bio_err
, "Error Creating OCSP request\n");
929 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
930 const EVP_MD
*cert_id_md
, X509
*issuer
,
931 STACK_OF(OCSP_CERTID
) *ids
)
934 const X509_NAME
*iname
;
935 ASN1_BIT_STRING
*ikey
;
938 if (issuer
== NULL
) {
939 BIO_printf(bio_err
, "No issuer certificate specified\n");
943 *req
= OCSP_REQUEST_new();
946 iname
= X509_get_subject_name(issuer
);
947 ikey
= X509_get0_pubkey_bitstr(issuer
);
948 sno
= s2i_ASN1_INTEGER(NULL
, serial
);
950 BIO_printf(bio_err
, "Error converting serial number %s\n", serial
);
953 id
= OCSP_cert_id_new(cert_id_md
, iname
, ikey
, sno
);
954 ASN1_INTEGER_free(sno
);
955 if (id
== NULL
|| !sk_OCSP_CERTID_push(ids
, id
))
957 if (!OCSP_request_add0_id(*req
, id
))
962 BIO_printf(bio_err
, "Error Creating OCSP request\n");
966 static int print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
967 STACK_OF(OPENSSL_STRING
) *names
,
968 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
973 int i
, status
, reason
;
974 ASN1_GENERALIZEDTIME
*rev
, *thisupd
, *nextupd
;
977 if (req
== NULL
|| !sk_OPENSSL_STRING_num(names
))
980 if (bs
== NULL
|| !sk_OCSP_CERTID_num(ids
))
983 for (i
= 0; i
< sk_OCSP_CERTID_num(ids
); i
++) {
984 id
= sk_OCSP_CERTID_value(ids
, i
);
985 name
= sk_OPENSSL_STRING_value(names
, i
);
986 BIO_printf(out
, "%s: ", name
);
988 if (!OCSP_resp_find_status(bs
, id
, &status
, &reason
,
989 &rev
, &thisupd
, &nextupd
)) {
990 BIO_puts(out
, "ERROR: No Status found.\n");
996 * Check validity: if invalid write to output BIO so we know which
997 * response this refers to.
999 if (!OCSP_check_validity(thisupd
, nextupd
, nsec
, maxage
)) {
1000 BIO_puts(out
, "WARNING: Status times invalid.\n");
1001 ERR_print_errors(out
);
1003 BIO_printf(out
, "%s\n", OCSP_cert_status_str(status
));
1005 BIO_puts(out
, "\tThis Update: ");
1006 ASN1_GENERALIZEDTIME_print(out
, thisupd
);
1007 BIO_puts(out
, "\n");
1010 BIO_puts(out
, "\tNext Update: ");
1011 ASN1_GENERALIZEDTIME_print(out
, nextupd
);
1012 BIO_puts(out
, "\n");
1015 if (status
!= V_OCSP_CERTSTATUS_REVOKED
)
1019 BIO_printf(out
, "\tReason: %s\n", OCSP_crl_reason_str(reason
));
1021 BIO_puts(out
, "\tRevocation Time: ");
1022 ASN1_GENERALIZEDTIME_print(out
, rev
);
1023 BIO_puts(out
, "\n");
1028 static void make_ocsp_response(BIO
*err
, OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
1029 CA_DB
*db
, STACK_OF(X509
) *ca
, X509
*rcert
,
1030 EVP_PKEY
*rkey
, const EVP_MD
*rmd
,
1031 STACK_OF(OPENSSL_STRING
) *sigopts
,
1032 STACK_OF(X509
) *rother
, unsigned long flags
,
1033 int nmin
, int ndays
, int badsig
,
1034 const EVP_MD
*resp_md
)
1036 ASN1_TIME
*thisupd
= NULL
, *nextupd
= NULL
;
1038 OCSP_BASICRESP
*bs
= NULL
;
1040 EVP_MD_CTX
*mctx
= NULL
;
1041 EVP_PKEY_CTX
*pkctx
= NULL
;
1043 id_count
= OCSP_request_onereq_count(req
);
1045 if (id_count
<= 0) {
1047 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
1051 bs
= OCSP_BASICRESP_new();
1052 thisupd
= X509_gmtime_adj(NULL
, 0);
1054 nextupd
= X509_time_adj_ex(NULL
, ndays
, nmin
* 60, NULL
);
1056 /* Examine each certificate id in the request */
1057 for (i
= 0; i
< id_count
; i
++) {
1059 ASN1_INTEGER
*serial
;
1063 ASN1_OBJECT
*cert_id_md_oid
;
1064 const EVP_MD
*cert_id_md
;
1065 OCSP_CERTID
*cid_resp_md
= NULL
;
1067 one
= OCSP_request_onereq_get0(req
, i
);
1068 cid
= OCSP_onereq_get0_id(one
);
1070 OCSP_id_get0_info(NULL
, &cert_id_md_oid
, NULL
, NULL
, cid
);
1072 cert_id_md
= EVP_get_digestbyobj(cert_id_md_oid
);
1073 if (cert_id_md
== NULL
) {
1074 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1078 for (jj
= 0; jj
< sk_X509_num(ca
) && !found
; jj
++) {
1079 X509
*ca_cert
= sk_X509_value(ca
, jj
);
1080 OCSP_CERTID
*ca_id
= OCSP_cert_to_id(cert_id_md
, NULL
, ca_cert
);
1082 if (OCSP_id_issuer_cmp(ca_id
, cid
) == 0) {
1084 if (resp_md
!= NULL
)
1085 cid_resp_md
= OCSP_cert_to_id(resp_md
, NULL
, ca_cert
);
1087 OCSP_CERTID_free(ca_id
);
1089 OCSP_id_get0_info(NULL
, NULL
, NULL
, &serial
, cid
);
1090 inf
= lookup_serial(db
, serial
);
1092 /* at this point, we can have cid be an alias of cid_resp_md */
1093 cid
= (cid_resp_md
!= NULL
) ? cid_resp_md
: cid
;
1096 OCSP_basic_add1_status(bs
, cid
,
1097 V_OCSP_CERTSTATUS_UNKNOWN
,
1098 0, NULL
, thisupd
, nextupd
);
1102 OCSP_basic_add1_status(bs
, cid
,
1103 V_OCSP_CERTSTATUS_UNKNOWN
,
1104 0, NULL
, thisupd
, nextupd
);
1105 } else if (inf
[DB_type
][0] == DB_TYPE_VAL
) {
1106 OCSP_basic_add1_status(bs
, cid
,
1107 V_OCSP_CERTSTATUS_GOOD
,
1108 0, NULL
, thisupd
, nextupd
);
1109 } else if (inf
[DB_type
][0] == DB_TYPE_REV
) {
1110 ASN1_OBJECT
*inst
= NULL
;
1111 ASN1_TIME
*revtm
= NULL
;
1112 ASN1_GENERALIZEDTIME
*invtm
= NULL
;
1113 OCSP_SINGLERESP
*single
;
1116 unpack_revinfo(&revtm
, &reason
, &inst
, &invtm
, inf
[DB_rev_date
]);
1117 single
= OCSP_basic_add1_status(bs
, cid
,
1118 V_OCSP_CERTSTATUS_REVOKED
,
1119 reason
, revtm
, thisupd
, nextupd
);
1120 if (single
== NULL
) {
1121 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1126 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_invalidity_date
,
1128 else if (inst
!= NULL
)
1129 OCSP_SINGLERESP_add1_ext_i2d(single
,
1130 NID_hold_instruction_code
, inst
,
1132 ASN1_OBJECT_free(inst
);
1133 ASN1_TIME_free(revtm
);
1134 ASN1_GENERALIZEDTIME_free(invtm
);
1136 OCSP_CERTID_free(cid_resp_md
);
1139 OCSP_copy_nonce(bs
, req
);
1141 mctx
= EVP_MD_CTX_new();
1142 if (mctx
== NULL
|| !EVP_DigestSignInit(mctx
, &pkctx
, rmd
, NULL
, rkey
)) {
1143 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
, NULL
);
1146 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1147 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1149 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1150 BIO_printf(err
, "parameter error \"%s\"\n", sigopt
);
1151 ERR_print_errors(bio_err
);
1152 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1157 if (!OCSP_basic_sign_ctx(bs
, rcert
, mctx
, rother
, flags
)) {
1158 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
, bs
);
1163 const ASN1_OCTET_STRING
*sig
= OCSP_resp_get0_signature(bs
);
1164 corrupt_signature(sig
);
1167 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL
, bs
);
1170 EVP_MD_CTX_free(mctx
);
1171 ASN1_TIME_free(thisupd
);
1172 ASN1_TIME_free(nextupd
);
1173 OCSP_BASICRESP_free(bs
);
1176 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
)
1180 char *itmp
, *row
[DB_NUMBER
], **rrow
;
1181 for (i
= 0; i
< DB_NUMBER
; i
++)
1183 bn
= ASN1_INTEGER_to_BN(ser
, NULL
);
1184 OPENSSL_assert(bn
); /* FIXME: should report an error at this
1185 * point and abort */
1186 if (BN_is_zero(bn
)) {
1187 itmp
= OPENSSL_strdup("00");
1188 OPENSSL_assert(itmp
);
1190 itmp
= BN_bn2hex(bn
);
1192 row
[DB_serial
] = itmp
;
1194 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1199 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
1202 #ifndef OPENSSL_NO_SOCK
1203 return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_REQUEST
),
1204 (ASN1_VALUE
**)preq
, NULL
, pcbio
, acbio
,
1205 NULL
/* found_keep_alive */,
1206 prog
, 1 /* accept_get */, timeout
);
1209 "Error getting OCSP request - sockets not supported\n");
1215 static int send_ocsp_response(BIO
*cbio
, const OCSP_RESPONSE
*resp
)
1217 #ifndef OPENSSL_NO_SOCK
1218 return http_server_send_asn1_resp(prog
, cbio
,
1219 0 /* no keep-alive */,
1220 "application/ocsp-response",
1221 ASN1_ITEM_rptr(OCSP_RESPONSE
),
1222 (const ASN1_VALUE
*)resp
);
1225 "Error sending OCSP response - sockets not supported\n");
1230 #ifndef OPENSSL_NO_SOCK
1231 OCSP_RESPONSE
*process_responder(OCSP_REQUEST
*req
, const char *host
,
1232 const char *port
, const char *path
,
1233 const char *proxy
, const char *no_proxy
,
1234 int use_ssl
, STACK_OF(CONF_VALUE
) *headers
,
1237 SSL_CTX
*ctx
= NULL
;
1238 OCSP_RESPONSE
*resp
= NULL
;
1241 ctx
= SSL_CTX_new(TLS_client_method());
1243 BIO_printf(bio_err
, "Error creating SSL context.\n");
1248 resp
= (OCSP_RESPONSE
*)
1249 app_http_post_asn1(host
, port
, path
, proxy
, no_proxy
,
1250 ctx
, headers
, "application/ocsp-request",
1251 (ASN1_VALUE
*)req
, ASN1_ITEM_rptr(OCSP_REQUEST
),
1252 "application/ocsp-response",
1253 req_timeout
, ASN1_ITEM_rptr(OCSP_RESPONSE
));
1256 BIO_printf(bio_err
, "Error querying OCSP responder\n");