2 * Copyright 2001-2018 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_NO_OCSP
13 NON_EMPTY_TRANSLATION_UNIT
15 # ifdef OPENSSL_SYS_VMS
16 # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
26 /* Needs to be included before the openssl headers */
29 # include "internal/sockets.h"
30 # include <openssl/e_os2.h>
31 # include <openssl/crypto.h>
32 # include <openssl/err.h>
33 # include <openssl/ssl.h>
34 # include <openssl/evp.h>
35 # include <openssl/bn.h>
36 # include <openssl/x509v3.h>
37 # include <openssl/rand.h>
40 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
53 # if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \
54 && !defined(OPENSSL_NO_POSIX_IO)
56 # include <sys/types.h>
57 # include <sys/wait.h>
60 # define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */
66 # define LOG_WARNING 1
70 # if defined(OPENSSL_SYS_VXWORKS)
72 int setpgid(pid_t pid
, pid_t pgid
)
84 /* Maximum leeway in validity period: default 5 minutes */
85 # define MAX_VALIDITY_PERIOD (5 * 60)
87 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
88 const EVP_MD
*cert_id_md
, X509
*issuer
,
89 STACK_OF(OCSP_CERTID
) *ids
);
90 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
91 const EVP_MD
*cert_id_md
, X509
*issuer
,
92 STACK_OF(OCSP_CERTID
) *ids
);
93 static void print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
94 STACK_OF(OPENSSL_STRING
) *names
,
95 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
97 static void make_ocsp_response(BIO
*err
, OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
98 CA_DB
*db
, STACK_OF(X509
) *ca
, X509
*rcert
,
99 EVP_PKEY
*rkey
, const EVP_MD
*md
,
100 STACK_OF(OPENSSL_STRING
) *sigopts
,
101 STACK_OF(X509
) *rother
, unsigned long flags
,
102 int nmin
, int ndays
, int badsig
,
103 const EVP_MD
*resp_md
);
105 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
);
106 static BIO
*init_responder(const char *port
);
107 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
, int timeout
);
108 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
);
109 static void log_message(int level
, const char *fmt
, ...);
111 static int multi
= 0;
114 static int acfd
= (int) INVALID_SOCKET
;
115 static int index_changed(CA_DB
*);
116 static void spawn_loop(void);
117 static int print_syslog(const char *str
, size_t len
, void *levPtr
);
118 static void socket_timeout(int signum
);
121 typedef enum OPTION_choice
{
122 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
123 OPT_OUTFILE
, OPT_TIMEOUT
, OPT_URL
, OPT_HOST
, OPT_PORT
,
124 OPT_IGNORE_ERR
, OPT_NOVERIFY
, OPT_NONCE
, OPT_NO_NONCE
,
125 OPT_RESP_NO_CERTS
, OPT_RESP_KEY_ID
, OPT_NO_CERTS
,
126 OPT_NO_SIGNATURE_VERIFY
, OPT_NO_CERT_VERIFY
, OPT_NO_CHAIN
,
127 OPT_NO_CERT_CHECKS
, OPT_NO_EXPLICIT
, OPT_TRUST_OTHER
,
128 OPT_NO_INTERN
, OPT_BADSIG
, OPT_TEXT
, OPT_REQ_TEXT
, OPT_RESP_TEXT
,
129 OPT_REQIN
, OPT_RESPIN
, OPT_SIGNER
, OPT_VAFILE
, OPT_SIGN_OTHER
,
130 OPT_VERIFY_OTHER
, OPT_CAFILE
, OPT_CAPATH
, OPT_CASTORE
, OPT_NOCAFILE
,
131 OPT_NOCAPATH
, OPT_NOCASTORE
,
132 OPT_VALIDITY_PERIOD
, OPT_STATUS_AGE
, OPT_SIGNKEY
, OPT_REQOUT
,
133 OPT_RESPOUT
, OPT_PATH
, OPT_ISSUER
, OPT_CERT
, OPT_SERIAL
,
134 OPT_INDEX
, OPT_CA
, OPT_NMIN
, OPT_REQUEST
, OPT_NDAYS
, OPT_RSIGNER
,
135 OPT_RKEY
, OPT_ROTHER
, OPT_RMD
, OPT_RSIGOPT
, OPT_HEADER
,
143 const OPTIONS ocsp_options
[] = {
144 OPT_SECTION("General"),
145 {"help", OPT_HELP
, '-', "Display this summary"},
146 {"ignore_err", OPT_IGNORE_ERR
, '-',
147 "Ignore error on OCSP request or response and continue running"},
148 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
149 {"CApath", OPT_CAPATH
, '<', "Trusted certificates directory"},
150 {"CAstore", OPT_CASTORE
, ':', "Trusted certificates store URI"},
151 {"no-CAfile", OPT_NOCAFILE
, '-',
152 "Do not load the default certificates file"},
153 {"no-CApath", OPT_NOCAPATH
, '-',
154 "Do not load certificates from the default certificates directory"},
155 {"no-CAstore", OPT_NOCAPATH
, '-',
156 "Do not load certificates from the default certificates store"},
158 OPT_SECTION("Responder"),
159 {"timeout", OPT_TIMEOUT
, 'p',
160 "Connection timeout (in seconds) to the OCSP responder"},
161 {"resp_no_certs", OPT_RESP_NO_CERTS
, '-',
162 "Don't include any certificates in response"},
164 {"multi", OPT_MULTI
, 'p', "run multiple responder processes"},
166 {"no_certs", OPT_NO_CERTS
, '-',
167 "Don't include any certificates in signed request"},
168 {"badsig", OPT_BADSIG
, '-',
169 "Corrupt last byte of loaded OSCP response signature (for test)"},
170 {"CA", OPT_CA
, '<', "CA certificate"},
171 {"nmin", OPT_NMIN
, 'p', "Number of minutes before next update"},
172 {"nrequest", OPT_REQUEST
, 'p',
173 "Number of requests to accept (default unlimited)"},
174 {"reqin", OPT_REQIN
, 's', "File with the DER-encoded request"},
175 {"signer", OPT_SIGNER
, '<', "Certificate to sign OCSP request with"},
176 {"sign_other", OPT_SIGN_OTHER
, '<',
177 "Additional certificates to include in signed request"},
178 {"index", OPT_INDEX
, '<', "Certificate status index file"},
179 {"ndays", OPT_NDAYS
, 'p', "Number of days before next update"},
180 {"rsigner", OPT_RSIGNER
, '<',
181 "Responder certificate to sign responses with"},
182 {"rkey", OPT_RKEY
, '<', "Responder key to sign responses with"},
183 {"passin", OPT_PASSIN
, 's', "Responder key pass phrase source"},
184 {"rother", OPT_ROTHER
, '<', "Other certificates to include in response"},
185 {"rmd", OPT_RMD
, 's', "Digest Algorithm to use in signature of OCSP response"},
186 {"rsigopt", OPT_RSIGOPT
, 's', "OCSP response signature parameter in n:v form"},
187 {"header", OPT_HEADER
, 's', "key=value header to add"},
188 {"rcid", OPT_RCID
, 's', "Use specified algorithm for cert id in response"},
189 {"", OPT_MD
, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
191 OPT_SECTION("Client"),
192 {"url", OPT_URL
, 's', "Responder URL"},
193 {"host", OPT_HOST
, 's', "TCP/IP hostname:port to connect to"},
194 {"port", OPT_PORT
, 'p', "Port to run responder on"},
195 {"out", OPT_OUTFILE
, '>', "Output filename"},
196 {"noverify", OPT_NOVERIFY
, '-', "Don't verify response at all"},
197 {"nonce", OPT_NONCE
, '-', "Add OCSP nonce to request"},
198 {"no_nonce", OPT_NO_NONCE
, '-', "Don't add OCSP nonce to request"},
199 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY
, '-',
200 "Don't check signature on response"},
201 {"resp_key_id", OPT_RESP_KEY_ID
, '-',
202 "Identify response by signing certificate key ID"},
203 {"no_cert_verify", OPT_NO_CERT_VERIFY
, '-',
204 "Don't check signing certificate"},
205 {"text", OPT_TEXT
, '-', "Print text form of request and response"},
206 {"req_text", OPT_REQ_TEXT
, '-', "Print text form of request"},
207 {"resp_text", OPT_RESP_TEXT
, '-', "Print text form of response"},
208 {"no_chain", OPT_NO_CHAIN
, '-', "Don't chain verify response"},
209 {"no_cert_checks", OPT_NO_CERT_CHECKS
, '-',
210 "Don't do additional checks on signing certificate"},
211 {"no_explicit", OPT_NO_EXPLICIT
, '-',
212 "Do not explicitly check the chain, just verify the root"},
213 {"trust_other", OPT_TRUST_OTHER
, '-',
214 "Don't verify additional certificates"},
215 {"no_intern", OPT_NO_INTERN
, '-',
216 "Don't search certificates contained in response for signer"},
217 {"respin", OPT_RESPIN
, 's', "File with the DER-encoded response"},
218 {"VAfile", OPT_VAFILE
, '<', "Validator certificates file"},
219 {"verify_other", OPT_VERIFY_OTHER
, '<',
220 "Additional certificates to search for signer"},
221 {"path", OPT_PATH
, 's', "Path to use in OCSP request"},
222 {"cert", OPT_CERT
, '<', "Certificate to check"},
223 {"serial", OPT_SERIAL
, 's', "Serial number to check"},
224 {"validity_period", OPT_VALIDITY_PERIOD
, 'u',
225 "Maximum validity discrepancy in seconds"},
226 {"signkey", OPT_SIGNKEY
, 's', "Private key to sign OCSP request with"},
227 {"reqout", OPT_REQOUT
, 's', "Output file for the DER-encoded request"},
228 {"respout", OPT_RESPOUT
, 's', "Output file for the DER-encoded response"},
229 {"issuer", OPT_ISSUER
, '<', "Issuer certificate"},
230 {"status_age", OPT_STATUS_AGE
, 'p', "Maximum status age in seconds"},
236 int ocsp_main(int argc
, char **argv
)
238 BIO
*acbio
= NULL
, *cbio
= NULL
, *derbio
= NULL
, *out
= NULL
;
239 const EVP_MD
*cert_id_md
= NULL
, *rsign_md
= NULL
;
240 STACK_OF(OPENSSL_STRING
) *rsign_sigopts
= NULL
;
243 EVP_PKEY
*key
= NULL
, *rkey
= NULL
;
244 OCSP_BASICRESP
*bs
= NULL
;
245 OCSP_REQUEST
*req
= NULL
;
246 OCSP_RESPONSE
*resp
= NULL
;
247 STACK_OF(CONF_VALUE
) *headers
= NULL
;
248 STACK_OF(OCSP_CERTID
) *ids
= NULL
;
249 STACK_OF(OPENSSL_STRING
) *reqnames
= NULL
;
250 STACK_OF(X509
) *sign_other
= NULL
, *verify_other
= NULL
, *rother
= NULL
;
251 STACK_OF(X509
) *issuers
= NULL
;
252 X509
*issuer
= NULL
, *cert
= NULL
;
253 STACK_OF(X509
) *rca_cert
= NULL
;
254 const EVP_MD
*resp_certid_md
= NULL
;
255 X509
*signer
= NULL
, *rsigner
= NULL
;
256 X509_STORE
*store
= NULL
;
257 X509_VERIFY_PARAM
*vpm
= NULL
;
258 const char *CAfile
= NULL
, *CApath
= NULL
, *CAstore
= NULL
;
259 char *header
, *value
;
260 char *host
= NULL
, *port
= NULL
, *path
= "/", *outfile
= NULL
;
261 char *rca_filename
= NULL
, *reqin
= NULL
, *respin
= NULL
;
262 char *reqout
= NULL
, *respout
= NULL
, *ridx_filename
= NULL
;
263 char *rsignfile
= NULL
, *rkeyfile
= NULL
;
264 char *passinarg
= NULL
, *passin
= NULL
;
265 char *sign_certfile
= NULL
, *verify_certfile
= NULL
, *rcertfile
= NULL
;
266 char *signfile
= NULL
, *keyfile
= NULL
;
267 char *thost
= NULL
, *tport
= NULL
, *tpath
= NULL
;
268 int noCAfile
= 0, noCApath
= 0, noCAstore
= 0;
269 int accept_count
= -1, add_nonce
= 1, noverify
= 0, use_ssl
= -1;
270 int vpmtouched
= 0, badsig
= 0, i
, ignore_err
= 0, nmin
= 0, ndays
= -1;
271 int req_text
= 0, resp_text
= 0, ret
= 1;
272 int req_timeout
= -1;
273 long nsec
= MAX_VALIDITY_PERIOD
, maxage
= -1;
274 unsigned long sign_flags
= 0, verify_flags
= 0, rflags
= 0;
277 reqnames
= sk_OPENSSL_STRING_new_null();
278 if (reqnames
== NULL
)
280 ids
= sk_OCSP_CERTID_new_null();
283 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
286 prog
= opt_init(argc
, argv
, ocsp_options
);
287 while ((o
= opt_next()) != OPT_EOF
) {
292 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
296 opt_help(ocsp_options
);
302 #ifndef OPENSSL_NO_SOCK
303 req_timeout
= atoi(opt_arg());
310 thost
= tport
= tpath
= NULL
;
311 if (!OSSL_HTTP_parse_url(opt_arg(),
312 &host
, &port
, &path
, &use_ssl
)) {
313 BIO_printf(bio_err
, "%s Error parsing URL\n", prog
);
338 case OPT_RESP_NO_CERTS
:
339 rflags
|= OCSP_NOCERTS
;
341 case OPT_RESP_KEY_ID
:
342 rflags
|= OCSP_RESPID_KEY
;
345 sign_flags
|= OCSP_NOCERTS
;
347 case OPT_NO_SIGNATURE_VERIFY
:
348 verify_flags
|= OCSP_NOSIGS
;
350 case OPT_NO_CERT_VERIFY
:
351 verify_flags
|= OCSP_NOVERIFY
;
354 verify_flags
|= OCSP_NOCHAIN
;
356 case OPT_NO_CERT_CHECKS
:
357 verify_flags
|= OCSP_NOCHECKS
;
359 case OPT_NO_EXPLICIT
:
360 verify_flags
|= OCSP_NOEXPLICIT
;
362 case OPT_TRUST_OTHER
:
363 verify_flags
|= OCSP_TRUSTOTHER
;
366 verify_flags
|= OCSP_NOINTERN
;
372 req_text
= resp_text
= 1;
387 signfile
= opt_arg();
390 verify_certfile
= opt_arg();
391 verify_flags
|= OCSP_TRUSTOTHER
;
394 sign_certfile
= opt_arg();
396 case OPT_VERIFY_OTHER
:
397 verify_certfile
= opt_arg();
418 if (!opt_verify(o
, vpm
))
422 case OPT_VALIDITY_PERIOD
:
423 opt_long(opt_arg(), &nsec
);
426 opt_long(opt_arg(), &maxage
);
441 issuer
= load_cert(opt_arg(), FORMAT_PEM
, "issuer certificate");
444 if (issuers
== NULL
) {
445 if ((issuers
= sk_X509_new_null()) == NULL
)
448 if (!sk_X509_push(issuers
, issuer
))
453 cert
= load_cert(opt_arg(), FORMAT_PEM
, "certificate");
456 if (cert_id_md
== NULL
)
457 cert_id_md
= EVP_sha1();
458 if (!add_ocsp_cert(&req
, cert
, cert_id_md
, issuer
, ids
))
460 if (!sk_OPENSSL_STRING_push(reqnames
, opt_arg()))
465 if (cert_id_md
== NULL
)
466 cert_id_md
= EVP_sha1();
467 if (!add_ocsp_serial(&req
, opt_arg(), cert_id_md
, issuer
, ids
))
469 if (!sk_OPENSSL_STRING_push(reqnames
, opt_arg()))
474 ridx_filename
= opt_arg();
477 rca_filename
= opt_arg();
480 opt_int(opt_arg(), &nmin
);
485 opt_int(opt_arg(), &accept_count
);
488 ndays
= atoi(opt_arg());
491 rsignfile
= opt_arg();
494 rkeyfile
= opt_arg();
497 passinarg
= opt_arg();
500 rcertfile
= opt_arg();
502 case OPT_RMD
: /* Response MessageDigest */
503 if (!opt_md(opt_arg(), &rsign_md
))
507 if (rsign_sigopts
== NULL
)
508 rsign_sigopts
= sk_OPENSSL_STRING_new_null();
509 if (rsign_sigopts
== NULL
|| !sk_OPENSSL_STRING_push(rsign_sigopts
, opt_arg()))
514 value
= strchr(header
, '=');
516 BIO_printf(bio_err
, "Missing = in header key=value\n");
520 if (!X509V3_add_value(header
, value
, &headers
))
524 resp_certid_md
= EVP_get_digestbyname(opt_arg());
525 if (resp_certid_md
== NULL
)
531 "%s: Digest must be before -cert or -serial\n",
535 if (!opt_md(opt_unknown(), &cert_id_md
))
541 multi
= atoi(opt_arg());
547 BIO_printf(bio_err
, "%s: Digest must be before -cert or -serial\n",
551 argc
= opt_num_rest();
555 /* Have we anything to do? */
556 if (req
== NULL
&& reqin
== NULL
557 && respin
== NULL
&& !(port
!= NULL
&& ridx_filename
!= NULL
))
560 out
= bio_open_default(outfile
, 'w', FORMAT_TEXT
);
564 if (req
== NULL
&& (add_nonce
!= 2))
567 if (req
== NULL
&& reqin
!= NULL
) {
568 derbio
= bio_open_default(reqin
, 'r', FORMAT_ASN1
);
571 req
= d2i_OCSP_REQUEST_bio(derbio
, NULL
);
574 BIO_printf(bio_err
, "Error reading OCSP request\n");
579 if (req
== NULL
&& port
!= NULL
) {
580 acbio
= init_responder(port
);
585 if (rsignfile
!= NULL
) {
586 if (rkeyfile
== NULL
)
587 rkeyfile
= rsignfile
;
588 rsigner
= load_cert(rsignfile
, FORMAT_PEM
, "responder certificate");
589 if (rsigner
== NULL
) {
590 BIO_printf(bio_err
, "Error loading responder certificate\n");
593 if (!load_certs(rca_filename
, &rca_cert
, FORMAT_PEM
,
594 NULL
, "CA certificate"))
596 if (rcertfile
!= NULL
) {
597 if (!load_certs(rcertfile
, &rother
, FORMAT_PEM
, 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_PEM
, 0, passin
, NULL
,
606 "responder private key");
611 if (ridx_filename
!= NULL
612 && (rkey
== NULL
|| rsigner
== NULL
|| rca_cert
== 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) {
627 if (multi
&& acbio
!= NULL
)
629 if (acbio
!= NULL
&& req_timeout
> 0)
630 signal(SIGALRM
, socket_timeout
);
634 log_message(LOG_INFO
, "waiting for OCSP client connections...");
640 if (index_changed(rdb
)) {
641 CA_DB
*newrdb
= load_index(ridx_filename
, NULL
);
643 if (newrdb
!= NULL
&& index_index(newrdb
) > 0) {
648 log_message(LOG_ERR
, "error reloading updated index: %s",
655 if (!do_responder(&req
, &cbio
, acbio
, req_timeout
))
660 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
,
662 send_ocsp_response(cbio
, resp
);
668 && (signfile
!= NULL
|| reqout
!= NULL
669 || host
!= NULL
|| add_nonce
|| ridx_filename
!= NULL
)) {
670 BIO_printf(bio_err
, "Need an OCSP request for this operation!\n");
674 if (req
!= NULL
&& add_nonce
) {
675 if (!OCSP_request_add1_nonce(req
, NULL
, -1))
679 if (signfile
!= NULL
) {
682 signer
= load_cert(signfile
, FORMAT_PEM
, "signer certificate");
683 if (signer
== NULL
) {
684 BIO_printf(bio_err
, "Error loading signer certificate\n");
687 if (sign_certfile
!= NULL
) {
688 if (!load_certs(sign_certfile
, &sign_other
, FORMAT_PEM
, NULL
,
689 "signer certificates"))
692 key
= load_key(keyfile
, FORMAT_PEM
, 0, NULL
, NULL
,
693 "signer private key");
697 if (!OCSP_request_sign
698 (req
, signer
, key
, NULL
, sign_other
, sign_flags
)) {
699 BIO_printf(bio_err
, "Error signing OCSP request\n");
704 if (req_text
&& req
!= NULL
)
705 OCSP_REQUEST_print(out
, req
, 0);
707 if (reqout
!= NULL
) {
708 derbio
= bio_open_default(reqout
, 'w', FORMAT_ASN1
);
711 i2d_OCSP_REQUEST_bio(derbio
, req
);
716 make_ocsp_response(bio_err
, &resp
, req
, rdb
, rca_cert
, rsigner
, rkey
,
717 rsign_md
, rsign_sigopts
, rother
, rflags
, nmin
, ndays
, badsig
,
720 send_ocsp_response(cbio
, resp
);
721 } else if (host
!= NULL
) {
722 # ifndef OPENSSL_NO_SOCK
723 resp
= process_responder(req
, host
, path
,
724 port
, use_ssl
, headers
, req_timeout
);
729 "Error creating connect BIO - sockets not supported.\n");
732 } else if (respin
!= NULL
) {
733 derbio
= bio_open_default(respin
, 'r', FORMAT_ASN1
);
736 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
739 BIO_printf(bio_err
, "Error reading OCSP response\n");
749 if (respout
!= NULL
) {
750 derbio
= bio_open_default(respout
, 'w', FORMAT_ASN1
);
753 i2d_OCSP_RESPONSE_bio(derbio
, resp
);
757 i
= OCSP_response_status(resp
);
758 if (i
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
759 BIO_printf(out
, "Responder Error: %s (%d)\n",
760 OCSP_response_status_str(i
), i
);
766 OCSP_RESPONSE_print(out
, resp
, 0);
768 /* If running as responder don't verify our own response */
770 /* If not unlimited, see if we took all we should. */
771 if (accept_count
!= -1 && --accept_count
<= 0) {
777 OCSP_REQUEST_free(req
);
779 OCSP_RESPONSE_free(resp
);
783 if (ridx_filename
!= NULL
) {
789 store
= setup_verify(CAfile
, noCAfile
, CApath
, noCApath
,
795 X509_STORE_set1_param(store
, vpm
);
796 if (verify_certfile
!= NULL
) {
797 if (!load_certs(verify_certfile
, &verify_other
, FORMAT_PEM
, NULL
,
798 "validator certificate"))
802 bs
= OCSP_response_get1_basic(resp
);
804 BIO_printf(bio_err
, "Error parsing response\n");
811 if (req
!= NULL
&& ((i
= OCSP_check_nonce(req
, bs
)) <= 0)) {
813 BIO_printf(bio_err
, "WARNING: no nonce in response\n");
815 BIO_printf(bio_err
, "Nonce Verify error\n");
821 i
= OCSP_basic_verify(bs
, verify_other
, store
, verify_flags
);
822 if (i
<= 0 && issuers
) {
823 i
= OCSP_basic_verify(bs
, issuers
, store
, OCSP_TRUSTOTHER
);
828 BIO_printf(bio_err
, "Response Verify Failure\n");
829 ERR_print_errors(bio_err
);
832 BIO_printf(bio_err
, "Response verify OK\n");
836 print_ocsp_summary(out
, bs
, req
, reqnames
, ids
, nsec
, maxage
);
839 ERR_print_errors(bio_err
);
841 X509_STORE_free(store
);
842 X509_VERIFY_PARAM_free(vpm
);
843 sk_OPENSSL_STRING_free(rsign_sigopts
);
847 sk_X509_pop_free(issuers
, X509_free
);
849 sk_X509_pop_free(rca_cert
, X509_free
);
854 OCSP_REQUEST_free(req
);
855 OCSP_RESPONSE_free(resp
);
856 OCSP_BASICRESP_free(bs
);
857 sk_OPENSSL_STRING_free(reqnames
);
858 sk_OCSP_CERTID_free(ids
);
859 sk_X509_pop_free(sign_other
, X509_free
);
860 sk_X509_pop_free(verify_other
, X509_free
);
861 sk_CONF_VALUE_pop_free(headers
, X509V3_conf_free
);
870 log_message(int level
, const char *fmt
, ...)
878 if (vsnprintf(buf
, sizeof(buf
), fmt
, ap
) > 0) {
879 syslog(level
, "%s", buf
);
881 if (level
>= LOG_ERR
)
882 ERR_print_errors_cb(print_syslog
, &level
);
886 BIO_printf(bio_err
, "%s: ", prog
);
887 BIO_vprintf(bio_err
, fmt
, ap
);
888 BIO_printf(bio_err
, "\n");
895 static int print_syslog(const char *str
, size_t len
, void *levPtr
)
897 int level
= *(int *)levPtr
;
898 int ilen
= (len
> MAXERRLEN
) ? MAXERRLEN
: len
;
900 syslog(level
, "%.*s", ilen
, str
);
905 static int index_changed(CA_DB
*rdb
)
909 if (rdb
!= NULL
&& stat(rdb
->dbfname
, &sb
) != -1) {
910 if (rdb
->dbst
.st_mtime
!= sb
.st_mtime
911 || rdb
->dbst
.st_ctime
!= sb
.st_ctime
912 || rdb
->dbst
.st_ino
!= sb
.st_ino
913 || rdb
->dbst
.st_dev
!= sb
.st_dev
) {
914 syslog(LOG_INFO
, "index file changed, reloading");
921 static void killall(int ret
, pid_t
*kidpids
)
925 for (i
= 0; i
< multi
; ++i
)
927 (void)kill(kidpids
[i
], SIGTERM
);
928 OPENSSL_free(kidpids
);
933 static int termsig
= 0;
935 static void noteterm (int sig
)
941 * Loop spawning up to `multi` child processes, only child processes return
942 * from this function. The parent process loops until receiving a termination
943 * signal, kills extant children and exits without returning.
945 static void spawn_loop(void)
947 pid_t
*kidpids
= NULL
;
952 openlog(prog
, LOG_PID
, LOG_DAEMON
);
955 syslog(LOG_ERR
, "fatal: error detaching from parent process group: %s",
959 kidpids
= app_malloc(multi
* sizeof(*kidpids
), "child PID array");
960 for (i
= 0; i
< multi
; ++i
)
963 signal(SIGINT
, noteterm
);
964 signal(SIGTERM
, noteterm
);
966 while (termsig
== 0) {
970 * Wait for a child to replace when we're at the limit.
971 * Slow down if a child exited abnormally or waitpid() < 0
973 while (termsig
== 0 && procs
>= multi
) {
974 if ((fpid
= waitpid(-1, &status
, 0)) > 0) {
975 for (i
= 0; i
< procs
; ++i
) {
976 if (kidpids
[i
] == fpid
) {
983 syslog(LOG_ERR
, "fatal: internal error: "
984 "no matching child slot for pid: %ld",
989 if (WIFEXITED(status
))
990 syslog(LOG_WARNING
, "child process: %ld, exit status: %d",
991 (long)fpid
, WEXITSTATUS(status
));
992 else if (WIFSIGNALED(status
))
993 syslog(LOG_WARNING
, "child process: %ld, term signal %d%s",
994 (long)fpid
, WTERMSIG(status
),
996 WCOREDUMP(status
) ? " (core dumped)" :
1002 } else if (errno
!= EINTR
) {
1003 syslog(LOG_ERR
, "fatal: waitpid(): %s", strerror(errno
));
1004 killall(1, kidpids
);
1010 switch(fpid
= fork()) {
1011 case -1: /* error */
1012 /* System critically low on memory, pause and try again later */
1016 OPENSSL_free(kidpids
);
1017 signal(SIGINT
, SIG_DFL
);
1018 signal(SIGTERM
, SIG_DFL
);
1021 if (RAND_poll() <= 0) {
1022 syslog(LOG_ERR
, "fatal: RAND_poll() failed");
1026 default: /* parent */
1027 for (i
= 0; i
< multi
; ++i
) {
1028 if (kidpids
[i
] == 0) {
1035 syslog(LOG_ERR
, "fatal: internal error: no free child slots");
1036 killall(1, kidpids
);
1042 /* The loop above can only break on termsig */
1043 syslog(LOG_INFO
, "terminating on signal: %d", termsig
);
1044 killall(0, kidpids
);
1048 static int add_ocsp_cert(OCSP_REQUEST
**req
, X509
*cert
,
1049 const EVP_MD
*cert_id_md
, X509
*issuer
,
1050 STACK_OF(OCSP_CERTID
) *ids
)
1054 if (issuer
== NULL
) {
1055 BIO_printf(bio_err
, "No issuer certificate specified\n");
1059 *req
= OCSP_REQUEST_new();
1062 id
= OCSP_cert_to_id(cert_id_md
, cert
, issuer
);
1063 if (id
== NULL
|| !sk_OCSP_CERTID_push(ids
, id
))
1065 if (!OCSP_request_add0_id(*req
, id
))
1070 BIO_printf(bio_err
, "Error Creating OCSP request\n");
1074 static int add_ocsp_serial(OCSP_REQUEST
**req
, char *serial
,
1075 const EVP_MD
*cert_id_md
, X509
*issuer
,
1076 STACK_OF(OCSP_CERTID
) *ids
)
1080 ASN1_BIT_STRING
*ikey
;
1083 if (issuer
== NULL
) {
1084 BIO_printf(bio_err
, "No issuer certificate specified\n");
1088 *req
= OCSP_REQUEST_new();
1091 iname
= X509_get_subject_name(issuer
);
1092 ikey
= X509_get0_pubkey_bitstr(issuer
);
1093 sno
= s2i_ASN1_INTEGER(NULL
, serial
);
1095 BIO_printf(bio_err
, "Error converting serial number %s\n", serial
);
1098 id
= OCSP_cert_id_new(cert_id_md
, iname
, ikey
, sno
);
1099 ASN1_INTEGER_free(sno
);
1100 if (id
== NULL
|| !sk_OCSP_CERTID_push(ids
, id
))
1102 if (!OCSP_request_add0_id(*req
, id
))
1107 BIO_printf(bio_err
, "Error Creating OCSP request\n");
1111 static void print_ocsp_summary(BIO
*out
, OCSP_BASICRESP
*bs
, OCSP_REQUEST
*req
,
1112 STACK_OF(OPENSSL_STRING
) *names
,
1113 STACK_OF(OCSP_CERTID
) *ids
, long nsec
,
1118 int i
, status
, reason
;
1119 ASN1_GENERALIZEDTIME
*rev
, *thisupd
, *nextupd
;
1121 if (bs
== NULL
|| req
== NULL
|| !sk_OPENSSL_STRING_num(names
)
1122 || !sk_OCSP_CERTID_num(ids
))
1125 for (i
= 0; i
< sk_OCSP_CERTID_num(ids
); i
++) {
1126 id
= sk_OCSP_CERTID_value(ids
, i
);
1127 name
= sk_OPENSSL_STRING_value(names
, i
);
1128 BIO_printf(out
, "%s: ", name
);
1130 if (!OCSP_resp_find_status(bs
, id
, &status
, &reason
,
1131 &rev
, &thisupd
, &nextupd
)) {
1132 BIO_puts(out
, "ERROR: No Status found.\n");
1137 * Check validity: if invalid write to output BIO so we know which
1138 * response this refers to.
1140 if (!OCSP_check_validity(thisupd
, nextupd
, nsec
, maxage
)) {
1141 BIO_puts(out
, "WARNING: Status times invalid.\n");
1142 ERR_print_errors(out
);
1144 BIO_printf(out
, "%s\n", OCSP_cert_status_str(status
));
1146 BIO_puts(out
, "\tThis Update: ");
1147 ASN1_GENERALIZEDTIME_print(out
, thisupd
);
1148 BIO_puts(out
, "\n");
1151 BIO_puts(out
, "\tNext Update: ");
1152 ASN1_GENERALIZEDTIME_print(out
, nextupd
);
1153 BIO_puts(out
, "\n");
1156 if (status
!= V_OCSP_CERTSTATUS_REVOKED
)
1160 BIO_printf(out
, "\tReason: %s\n", OCSP_crl_reason_str(reason
));
1162 BIO_puts(out
, "\tRevocation Time: ");
1163 ASN1_GENERALIZEDTIME_print(out
, rev
);
1164 BIO_puts(out
, "\n");
1168 static void make_ocsp_response(BIO
*err
, OCSP_RESPONSE
**resp
, OCSP_REQUEST
*req
,
1169 CA_DB
*db
, STACK_OF(X509
) *ca
, X509
*rcert
,
1170 EVP_PKEY
*rkey
, const EVP_MD
*rmd
,
1171 STACK_OF(OPENSSL_STRING
) *sigopts
,
1172 STACK_OF(X509
) *rother
, unsigned long flags
,
1173 int nmin
, int ndays
, int badsig
,
1174 const EVP_MD
*resp_md
)
1176 ASN1_TIME
*thisupd
= NULL
, *nextupd
= NULL
;
1178 OCSP_BASICRESP
*bs
= NULL
;
1180 EVP_MD_CTX
*mctx
= NULL
;
1181 EVP_PKEY_CTX
*pkctx
= NULL
;
1183 id_count
= OCSP_request_onereq_count(req
);
1185 if (id_count
<= 0) {
1187 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST
, NULL
);
1191 bs
= OCSP_BASICRESP_new();
1192 thisupd
= X509_gmtime_adj(NULL
, 0);
1194 nextupd
= X509_time_adj_ex(NULL
, ndays
, nmin
* 60, NULL
);
1196 /* Examine each certificate id in the request */
1197 for (i
= 0; i
< id_count
; i
++) {
1199 ASN1_INTEGER
*serial
;
1203 ASN1_OBJECT
*cert_id_md_oid
;
1204 const EVP_MD
*cert_id_md
;
1205 OCSP_CERTID
*cid_resp_md
= NULL
;
1207 one
= OCSP_request_onereq_get0(req
, i
);
1208 cid
= OCSP_onereq_get0_id(one
);
1210 OCSP_id_get0_info(NULL
, &cert_id_md_oid
, NULL
, NULL
, cid
);
1212 cert_id_md
= EVP_get_digestbyobj(cert_id_md_oid
);
1213 if (cert_id_md
== NULL
) {
1214 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1218 for (jj
= 0; jj
< sk_X509_num(ca
) && !found
; jj
++) {
1219 X509
*ca_cert
= sk_X509_value(ca
, jj
);
1220 OCSP_CERTID
*ca_id
= OCSP_cert_to_id(cert_id_md
, NULL
, ca_cert
);
1222 if (OCSP_id_issuer_cmp(ca_id
, cid
) == 0) {
1224 if (resp_md
!= NULL
)
1225 cid_resp_md
= OCSP_cert_to_id(resp_md
, NULL
, ca_cert
);
1227 OCSP_CERTID_free(ca_id
);
1229 OCSP_id_get0_info(NULL
, NULL
, NULL
, &serial
, cid
);
1230 inf
= lookup_serial(db
, serial
);
1232 /* at this point, we can have cid be an alias of cid_resp_md */
1233 cid
= (cid_resp_md
!= NULL
) ? cid_resp_md
: cid
;
1236 OCSP_basic_add1_status(bs
, cid
,
1237 V_OCSP_CERTSTATUS_UNKNOWN
,
1238 0, NULL
, thisupd
, nextupd
);
1242 OCSP_basic_add1_status(bs
, cid
,
1243 V_OCSP_CERTSTATUS_UNKNOWN
,
1244 0, NULL
, thisupd
, nextupd
);
1245 } else if (inf
[DB_type
][0] == DB_TYPE_VAL
) {
1246 OCSP_basic_add1_status(bs
, cid
,
1247 V_OCSP_CERTSTATUS_GOOD
,
1248 0, NULL
, thisupd
, nextupd
);
1249 } else if (inf
[DB_type
][0] == DB_TYPE_REV
) {
1250 ASN1_OBJECT
*inst
= NULL
;
1251 ASN1_TIME
*revtm
= NULL
;
1252 ASN1_GENERALIZEDTIME
*invtm
= NULL
;
1253 OCSP_SINGLERESP
*single
;
1256 unpack_revinfo(&revtm
, &reason
, &inst
, &invtm
, inf
[DB_rev_date
]);
1257 single
= OCSP_basic_add1_status(bs
, cid
,
1258 V_OCSP_CERTSTATUS_REVOKED
,
1259 reason
, revtm
, thisupd
, nextupd
);
1261 OCSP_SINGLERESP_add1_ext_i2d(single
, NID_invalidity_date
,
1263 else if (inst
!= NULL
)
1264 OCSP_SINGLERESP_add1_ext_i2d(single
,
1265 NID_hold_instruction_code
, inst
,
1267 ASN1_OBJECT_free(inst
);
1268 ASN1_TIME_free(revtm
);
1269 ASN1_GENERALIZEDTIME_free(invtm
);
1271 OCSP_CERTID_free(cid_resp_md
);
1274 OCSP_copy_nonce(bs
, req
);
1276 mctx
= EVP_MD_CTX_new();
1277 if ( mctx
== NULL
|| !EVP_DigestSignInit(mctx
, &pkctx
, rmd
, NULL
, rkey
)) {
1278 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
, NULL
);
1281 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1282 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1284 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1285 BIO_printf(err
, "parameter error \"%s\"\n", sigopt
);
1286 ERR_print_errors(bio_err
);
1287 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
,
1292 if (!OCSP_basic_sign_ctx(bs
, rcert
, mctx
, rother
, flags
)) {
1293 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR
, bs
);
1298 const ASN1_OCTET_STRING
*sig
= OCSP_resp_get0_signature(bs
);
1299 corrupt_signature(sig
);
1302 *resp
= OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL
, bs
);
1305 EVP_MD_CTX_free(mctx
);
1306 ASN1_TIME_free(thisupd
);
1307 ASN1_TIME_free(nextupd
);
1308 OCSP_BASICRESP_free(bs
);
1311 static char **lookup_serial(CA_DB
*db
, ASN1_INTEGER
*ser
)
1315 char *itmp
, *row
[DB_NUMBER
], **rrow
;
1316 for (i
= 0; i
< DB_NUMBER
; i
++)
1318 bn
= ASN1_INTEGER_to_BN(ser
, NULL
);
1319 OPENSSL_assert(bn
); /* FIXME: should report an error at this
1320 * point and abort */
1322 itmp
= OPENSSL_strdup("00");
1324 itmp
= BN_bn2hex(bn
);
1325 row
[DB_serial
] = itmp
;
1327 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1332 /* Quick and dirty OCSP server: read in and parse input request */
1334 static BIO
*init_responder(const char *port
)
1336 # ifdef OPENSSL_NO_SOCK
1338 "Error setting up accept BIO - sockets not supported.\n");
1341 BIO
*acbio
= NULL
, *bufbio
= NULL
;
1343 bufbio
= BIO_new(BIO_f_buffer());
1346 acbio
= BIO_new(BIO_s_accept());
1348 || BIO_set_bind_mode(acbio
, BIO_BIND_REUSEADDR
) < 0
1349 || BIO_set_accept_port(acbio
, port
) < 0) {
1350 log_message(LOG_ERR
, "Error setting up accept BIO");
1354 BIO_set_accept_bios(acbio
, bufbio
);
1356 if (BIO_do_accept(acbio
) <= 0) {
1357 log_message(LOG_ERR
, "Error starting accept");
1364 BIO_free_all(acbio
);
1370 # ifndef OPENSSL_NO_SOCK
1372 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1374 static int urldecode(char *p
)
1376 unsigned char *out
= (unsigned char *)p
;
1377 unsigned char *save
= out
;
1382 else if (isxdigit(_UC(p
[1])) && isxdigit(_UC(p
[2]))) {
1383 /* Don't check, can't fail because of ixdigit() call. */
1384 *out
++ = (OPENSSL_hexchar2int(p
[1]) << 4)
1385 | OPENSSL_hexchar2int(p
[2]);
1392 return (int)(out
- save
);
1397 static void socket_timeout(int signum
)
1399 if (acfd
!= (int)INVALID_SOCKET
)
1400 (void)shutdown(acfd
, SHUT_RD
);
1404 static int do_responder(OCSP_REQUEST
**preq
, BIO
**pcbio
, BIO
*acbio
,
1407 # ifdef OPENSSL_NO_SOCK
1411 OCSP_REQUEST
*req
= NULL
;
1412 char inbuf
[2048], reqbuf
[2048];
1414 BIO
*cbio
= NULL
, *getbio
= NULL
, *b64
= NULL
;
1419 /* Connection loss before accept() is routine, ignore silently */
1420 if (BIO_do_accept(acbio
) <= 0)
1423 cbio
= BIO_pop(acbio
);
1425 client
= BIO_get_peer_name(cbio
);
1429 (void) BIO_get_fd(cbio
, &acfd
);
1434 /* Read the request line. */
1435 len
= BIO_gets(cbio
, reqbuf
, sizeof(reqbuf
));
1439 if (strncmp(reqbuf
, "GET ", 4) == 0) {
1440 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
1441 for (p
= reqbuf
+ 4; *p
== ' '; ++p
)
1444 log_message(LOG_INFO
, "Invalid request -- bad URL: %s", client
);
1449 /* Splice off the HTTP version identifier. */
1450 for (q
= p
; *q
; q
++)
1453 if (strncmp(q
, " HTTP/1.", 8) != 0) {
1454 log_message(LOG_INFO
,
1455 "Invalid request -- bad HTTP version: %s", client
);
1461 * Skip "GET / HTTP..." requests often used by load-balancers. Note:
1462 * 'p' was incremented above to point to the first byte *after* the
1463 * leading slash, so with 'GET / ' it is now an empty string.
1470 log_message(LOG_INFO
,
1471 "Invalid request -- bad URL encoding: %s", client
);
1474 if ((getbio
= BIO_new_mem_buf(p
, len
)) == NULL
1475 || (b64
= BIO_new(BIO_f_base64())) == NULL
) {
1476 log_message(LOG_ERR
, "Could not allocate base64 bio: %s", client
);
1479 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
1480 getbio
= BIO_push(b64
, getbio
);
1481 } else if (strncmp(reqbuf
, "POST ", 5) != 0) {
1482 log_message(LOG_INFO
, "Invalid request -- bad HTTP verb: %s", client
);
1486 /* Read and skip past the headers. */
1488 len
= BIO_gets(cbio
, inbuf
, sizeof(inbuf
));
1491 if ((inbuf
[0] == '\r') || (inbuf
[0] == '\n'))
1496 /* Clear alarm before we close the client socket */
1501 /* Try to read OCSP request */
1502 if (getbio
!= NULL
) {
1503 req
= d2i_OCSP_REQUEST_bio(getbio
, NULL
);
1504 BIO_free_all(getbio
);
1506 req
= d2i_OCSP_REQUEST_bio(cbio
, NULL
);
1510 log_message(LOG_ERR
, "Error parsing OCSP request");
1518 acfd
= (int)INVALID_SOCKET
;
1524 static int send_ocsp_response(BIO
*cbio
, OCSP_RESPONSE
*resp
)
1527 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1528 "Content-Length: %d\r\n\r\n";
1531 BIO_printf(cbio
, http_resp
, i2d_OCSP_RESPONSE(resp
, NULL
));
1532 i2d_OCSP_RESPONSE_bio(cbio
, resp
);
1533 (void)BIO_flush(cbio
);
1537 # ifndef OPENSSL_NO_SOCK
1538 OCSP_RESPONSE
*process_responder(OCSP_REQUEST
*req
,
1539 const char *host
, const char *path
,
1540 const char *port
, int use_ssl
,
1541 STACK_OF(CONF_VALUE
) *headers
,
1544 SSL_CTX
*ctx
= NULL
;
1545 OCSP_RESPONSE
*resp
= NULL
;
1548 ctx
= SSL_CTX_new(TLS_client_method());
1550 BIO_printf(bio_err
, "Error creating SSL context.\n");
1553 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1556 resp
= (OCSP_RESPONSE
*)
1557 app_http_post_asn1(host
, port
, path
, NULL
, NULL
/* no proxy used */,
1558 ctx
, headers
, "application/ocsp-request",
1559 (ASN1_VALUE
*)req
, ASN1_ITEM_rptr(OCSP_REQUEST
),
1560 req_timeout
, ASN1_ITEM_rptr(OCSP_RESPONSE
));
1563 BIO_printf(bio_err
, "Error querying OCSP responder\n");