]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
3c7d0945 | 2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. |
5782ceb2 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
5782ceb2 DSH |
8 | */ |
9 | ||
3e41ac35 MC |
10 | #include <openssl/opensslconf.h> |
11 | ||
12 | #ifdef OPENSSL_NO_OCSP | |
13 | NON_EMPTY_TRANSLATION_UNIT | |
14 | #else | |
0f113f3e MC |
15 | # ifdef OPENSSL_SYS_VMS |
16 | # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined | |
17 | * on OpenVMS */ | |
18 | # endif | |
19 | ||
0f113f3e MC |
20 | # include <stdio.h> |
21 | # include <stdlib.h> | |
22 | # include <string.h> | |
23 | # include <time.h> | |
98cd49db | 24 | # include <ctype.h> |
3e41ac35 MC |
25 | |
26 | /* Needs to be included before the openssl headers */ | |
27 | # include "apps.h" | |
dab2cd68 | 28 | # include "progs.h" |
3e3c7c36 | 29 | # include "internal/sockets.h" |
0f113f3e MC |
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> | |
3e3c7c36 VD |
37 | # include <openssl/rand.h> |
38 | ||
1a54618b MC |
39 | # if defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_NO_SOCK) \ |
40 | && !defined(OPENSSL_NO_POSIX_IO) | |
3e3c7c36 VD |
41 | # define OCSP_DAEMON |
42 | # include <sys/types.h> | |
43 | # include <sys/wait.h> | |
44 | # include <syslog.h> | |
45 | # include <signal.h> | |
46 | # define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */ | |
47 | # else | |
48 | # undef LOG_INFO | |
49 | # undef LOG_WARNING | |
50 | # undef LOG_ERR | |
51 | # define LOG_INFO 0 | |
52 | # define LOG_WARNING 1 | |
53 | # define LOG_ERR 2 | |
54 | # endif | |
0f113f3e | 55 | |
0f113f3e | 56 | /* Maximum leeway in validity period: default 5 minutes */ |
7e1b7485 | 57 | # define MAX_VALIDITY_PERIOD (5 * 60) |
0f113f3e MC |
58 | |
59 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, | |
60 | const EVP_MD *cert_id_md, X509 *issuer, | |
61 | STACK_OF(OCSP_CERTID) *ids); | |
62 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
63 | const EVP_MD *cert_id_md, X509 *issuer, | |
64 | STACK_OF(OCSP_CERTID) *ids); | |
7e1b7485 | 65 | static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, |
0f113f3e MC |
66 | STACK_OF(OPENSSL_STRING) *names, |
67 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
68 | long maxage); | |
b4dd21a7 | 69 | static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req, |
3bb0f989 | 70 | CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, |
0f113f3e | 71 | EVP_PKEY *rkey, const EVP_MD *md, |
b4dd21a7 | 72 | STACK_OF(OPENSSL_STRING) *sigopts, |
0f113f3e MC |
73 | STACK_OF(X509) *rother, unsigned long flags, |
74 | int nmin, int ndays, int badsig); | |
ee306a13 | 75 | |
f85b68cd | 76 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser); |
c45a48c1 | 77 | static BIO *init_responder(const char *port); |
3e3c7c36 | 78 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, int timeout); |
ee306a13 | 79 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp); |
3e3c7c36 VD |
80 | static void log_message(int level, const char *fmt, ...); |
81 | static char *prog; | |
82 | static int multi = 0; | |
83 | ||
84 | # ifdef OCSP_DAEMON | |
85 | static int acfd = (int) INVALID_SOCKET; | |
86 | static int index_changed(CA_DB *); | |
87 | static void spawn_loop(void); | |
88 | static int print_syslog(const char *str, size_t len, void *levPtr); | |
89 | static void sock_timeout(int signum); | |
90 | # endif | |
f9e55034 MC |
91 | |
92 | # ifndef OPENSSL_NO_SOCK | |
76e0cd12 DSH |
93 | static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, |
94 | const char *path, | |
0f113f3e MC |
95 | const STACK_OF(CONF_VALUE) *headers, |
96 | OCSP_REQUEST *req, int req_timeout); | |
f9e55034 | 97 | # endif |
ee306a13 | 98 | |
7e1b7485 RS |
99 | typedef enum OPTION_choice { |
100 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
101 | OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT, | |
102 | OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE, | |
103 | OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS, | |
104 | OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN, | |
105 | OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER, | |
106 | OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT, | |
107 | OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER, | |
2b6bcb70 | 108 | OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, |
7e1b7485 | 109 | OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT, |
c6724060 | 110 | OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL, |
7e1b7485 | 111 | OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER, |
04940147 | 112 | OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER, |
7e1b7485 | 113 | OPT_V_ENUM, |
3e3c7c36 VD |
114 | OPT_MD, |
115 | OPT_MULTI | |
7e1b7485 RS |
116 | } OPTION_CHOICE; |
117 | ||
44c83ebd | 118 | const OPTIONS ocsp_options[] = { |
7e1b7485 RS |
119 | {"help", OPT_HELP, '-', "Display this summary"}, |
120 | {"out", OPT_OUTFILE, '>', "Output filename"}, | |
9a13bb38 RS |
121 | {"timeout", OPT_TIMEOUT, 'p', |
122 | "Connection timeout (in seconds) to the OCSP responder"}, | |
7e1b7485 | 123 | {"url", OPT_URL, 's', "Responder URL"}, |
ceab33e2 | 124 | {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"}, |
7e1b7485 | 125 | {"port", OPT_PORT, 'p', "Port to run responder on"}, |
12d56b29 | 126 | {"ignore_err", OPT_IGNORE_ERR, '-', |
bbe9c3d5 | 127 | "Ignore error on OCSP request or response and continue running"}, |
7e1b7485 RS |
128 | {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"}, |
129 | {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"}, | |
130 | {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"}, | |
131 | {"resp_no_certs", OPT_RESP_NO_CERTS, '-', | |
132 | "Don't include any certificates in response"}, | |
133 | {"resp_key_id", OPT_RESP_KEY_ID, '-', | |
ceab33e2 | 134 | "Identify response by signing certificate key ID"}, |
3e3c7c36 VD |
135 | # ifdef OCSP_DAEMON |
136 | {"multi", OPT_MULTI, 'p', "run multiple responder processes"}, | |
137 | # endif | |
7e1b7485 RS |
138 | {"no_certs", OPT_NO_CERTS, '-', |
139 | "Don't include any certificates in signed request"}, | |
140 | {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-', | |
141 | "Don't check signature on response"}, | |
142 | {"no_cert_verify", OPT_NO_CERT_VERIFY, '-', | |
143 | "Don't check signing certificate"}, | |
144 | {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"}, | |
145 | {"no_cert_checks", OPT_NO_CERT_CHECKS, '-', | |
146 | "Don't do additional checks on signing certificate"}, | |
12d56b29 F |
147 | {"no_explicit", OPT_NO_EXPLICIT, '-', |
148 | "Do not explicitly check the chain, just verify the root"}, | |
7e1b7485 RS |
149 | {"trust_other", OPT_TRUST_OTHER, '-', |
150 | "Don't verify additional certificates"}, | |
151 | {"no_intern", OPT_NO_INTERN, '-', | |
152 | "Don't search certificates contained in response for signer"}, | |
16e1b281 F |
153 | {"badsig", OPT_BADSIG, '-', |
154 | "Corrupt last byte of loaded OSCP response signature (for test)"}, | |
7e1b7485 RS |
155 | {"text", OPT_TEXT, '-', "Print text form of request and response"}, |
156 | {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"}, | |
157 | {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"}, | |
158 | {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"}, | |
159 | {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"}, | |
160 | {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"}, | |
161 | {"VAfile", OPT_VAFILE, '<', "Validator certificates file"}, | |
162 | {"sign_other", OPT_SIGN_OTHER, '<', | |
163 | "Additional certificates to include in signed request"}, | |
164 | {"verify_other", OPT_VERIFY_OTHER, '<', | |
165 | "Additional certificates to search for signer"}, | |
166 | {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, | |
167 | {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"}, | |
2b6bcb70 MC |
168 | {"no-CAfile", OPT_NOCAFILE, '-', |
169 | "Do not load the default certificates file"}, | |
170 | {"no-CApath", OPT_NOCAPATH, '-', | |
171 | "Do not load certificates from the default certificates directory"}, | |
7e1b7485 RS |
172 | {"validity_period", OPT_VALIDITY_PERIOD, 'u', |
173 | "Maximum validity discrepancy in seconds"}, | |
174 | {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"}, | |
175 | {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"}, | |
176 | {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"}, | |
177 | {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"}, | |
178 | {"path", OPT_PATH, 's', "Path to use in OCSP request"}, | |
c6724060 | 179 | {"issuer", OPT_ISSUER, '<', "Issuer certificate"}, |
7e1b7485 | 180 | {"cert", OPT_CERT, '<', "Certificate to check"}, |
16e1b281 | 181 | {"serial", OPT_SERIAL, 's', "Serial number to check"}, |
7e1b7485 RS |
182 | {"index", OPT_INDEX, '<', "Certificate status index file"}, |
183 | {"CA", OPT_CA, '<', "CA certificate"}, | |
184 | {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"}, | |
185 | {"nrequest", OPT_REQUEST, 'p', | |
186 | "Number of requests to accept (default unlimited)"}, | |
187 | {"ndays", OPT_NDAYS, 'p', "Number of days before next update"}, | |
188 | {"rsigner", OPT_RSIGNER, '<', | |
0ad69cd6 | 189 | "Responder certificate to sign responses with"}, |
7e1b7485 RS |
190 | {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"}, |
191 | {"rother", OPT_ROTHER, '<', "Other certificates to include in response"}, | |
16e1b281 | 192 | {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"}, |
04940147 | 193 | {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"}, |
7e1b7485 | 194 | {"header", OPT_HEADER, 's', "key=value header to add"}, |
16e1b281 | 195 | {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"}, |
7e1b7485 RS |
196 | OPT_V_OPTIONS, |
197 | {NULL} | |
198 | }; | |
199 | ||
200 | int ocsp_main(int argc, char **argv) | |
0f113f3e | 201 | { |
7e1b7485 RS |
202 | BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL; |
203 | const EVP_MD *cert_id_md = NULL, *rsign_md = NULL; | |
b4dd21a7 | 204 | STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL; |
6302bbd2 | 205 | int trailing_md = 0; |
7e1b7485 RS |
206 | CA_DB *rdb = NULL; |
207 | EVP_PKEY *key = NULL, *rkey = NULL; | |
208 | OCSP_BASICRESP *bs = NULL; | |
0f113f3e MC |
209 | OCSP_REQUEST *req = NULL; |
210 | OCSP_RESPONSE *resp = NULL; | |
7e1b7485 RS |
211 | STACK_OF(CONF_VALUE) *headers = NULL; |
212 | STACK_OF(OCSP_CERTID) *ids = NULL; | |
213 | STACK_OF(OPENSSL_STRING) *reqnames = NULL; | |
214 | STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL; | |
88806cfc | 215 | STACK_OF(X509) *issuers = NULL; |
3bb0f989 TS |
216 | X509 *issuer = NULL, *cert = NULL; |
217 | STACK_OF(X509) *rca_cert = NULL; | |
0f113f3e | 218 | X509 *signer = NULL, *rsigner = NULL; |
0f113f3e MC |
219 | X509_STORE *store = NULL; |
220 | X509_VERIFY_PARAM *vpm = NULL; | |
cc696296 F |
221 | const char *CAfile = NULL, *CApath = NULL; |
222 | char *header, *value; | |
7e1b7485 RS |
223 | char *host = NULL, *port = NULL, *path = "/", *outfile = NULL; |
224 | char *rca_filename = NULL, *reqin = NULL, *respin = NULL; | |
225 | char *reqout = NULL, *respout = NULL, *ridx_filename = NULL; | |
226 | char *rsignfile = NULL, *rkeyfile = NULL; | |
0f113f3e | 227 | char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL; |
7e1b7485 RS |
228 | char *signfile = NULL, *keyfile = NULL; |
229 | char *thost = NULL, *tport = NULL, *tpath = NULL; | |
2b6bcb70 | 230 | int noCAfile = 0, noCApath = 0; |
7e1b7485 RS |
231 | int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1; |
232 | int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1; | |
f9e55034 | 233 | int req_text = 0, resp_text = 0, ret = 1; |
f9e55034 | 234 | int req_timeout = -1; |
7e1b7485 | 235 | long nsec = MAX_VALIDITY_PERIOD, maxage = -1; |
0f113f3e | 236 | unsigned long sign_flags = 0, verify_flags = 0, rflags = 0; |
7e1b7485 | 237 | OPTION_CHOICE o; |
0f113f3e | 238 | |
0f113f3e | 239 | reqnames = sk_OPENSSL_STRING_new_null(); |
2234212c | 240 | if (reqnames == NULL) |
7e1b7485 | 241 | goto end; |
0f113f3e | 242 | ids = sk_OCSP_CERTID_new_null(); |
2234212c | 243 | if (ids == NULL) |
7e1b7485 RS |
244 | goto end; |
245 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) | |
246 | return 1; | |
247 | ||
248 | prog = opt_init(argc, argv, ocsp_options); | |
249 | while ((o = opt_next()) != OPT_EOF) { | |
250 | switch (o) { | |
251 | case OPT_EOF: | |
252 | case OPT_ERR: | |
253 | opthelp: | |
254 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
255 | goto end; | |
256 | case OPT_HELP: | |
257 | ret = 0; | |
258 | opt_help(ocsp_options); | |
259 | goto end; | |
260 | case OPT_OUTFILE: | |
261 | outfile = opt_arg(); | |
262 | break; | |
263 | case OPT_TIMEOUT: | |
f9e55034 | 264 | #ifndef OPENSSL_NO_SOCK |
7e1b7485 | 265 | req_timeout = atoi(opt_arg()); |
f9e55034 | 266 | #endif |
7e1b7485 RS |
267 | break; |
268 | case OPT_URL: | |
b548a1f1 RS |
269 | OPENSSL_free(thost); |
270 | OPENSSL_free(tport); | |
271 | OPENSSL_free(tpath); | |
4b8d8e2a | 272 | thost = tport = tpath = NULL; |
7e1b7485 RS |
273 | if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) { |
274 | BIO_printf(bio_err, "%s Error parsing URL\n", prog); | |
275 | goto end; | |
276 | } | |
277 | thost = host; | |
278 | tport = port; | |
279 | tpath = path; | |
280 | break; | |
281 | case OPT_HOST: | |
282 | host = opt_arg(); | |
283 | break; | |
284 | case OPT_PORT: | |
285 | port = opt_arg(); | |
286 | break; | |
287 | case OPT_IGNORE_ERR: | |
0f113f3e | 288 | ignore_err = 1; |
7e1b7485 RS |
289 | break; |
290 | case OPT_NOVERIFY: | |
0f113f3e | 291 | noverify = 1; |
7e1b7485 RS |
292 | break; |
293 | case OPT_NONCE: | |
0f113f3e | 294 | add_nonce = 2; |
7e1b7485 RS |
295 | break; |
296 | case OPT_NO_NONCE: | |
0f113f3e | 297 | add_nonce = 0; |
7e1b7485 RS |
298 | break; |
299 | case OPT_RESP_NO_CERTS: | |
0f113f3e | 300 | rflags |= OCSP_NOCERTS; |
7e1b7485 RS |
301 | break; |
302 | case OPT_RESP_KEY_ID: | |
0f113f3e | 303 | rflags |= OCSP_RESPID_KEY; |
7e1b7485 RS |
304 | break; |
305 | case OPT_NO_CERTS: | |
0f113f3e | 306 | sign_flags |= OCSP_NOCERTS; |
7e1b7485 RS |
307 | break; |
308 | case OPT_NO_SIGNATURE_VERIFY: | |
0f113f3e | 309 | verify_flags |= OCSP_NOSIGS; |
7e1b7485 RS |
310 | break; |
311 | case OPT_NO_CERT_VERIFY: | |
0f113f3e | 312 | verify_flags |= OCSP_NOVERIFY; |
7e1b7485 RS |
313 | break; |
314 | case OPT_NO_CHAIN: | |
0f113f3e | 315 | verify_flags |= OCSP_NOCHAIN; |
7e1b7485 RS |
316 | break; |
317 | case OPT_NO_CERT_CHECKS: | |
0f113f3e | 318 | verify_flags |= OCSP_NOCHECKS; |
7e1b7485 RS |
319 | break; |
320 | case OPT_NO_EXPLICIT: | |
0f113f3e | 321 | verify_flags |= OCSP_NOEXPLICIT; |
7e1b7485 RS |
322 | break; |
323 | case OPT_TRUST_OTHER: | |
0f113f3e | 324 | verify_flags |= OCSP_TRUSTOTHER; |
7e1b7485 RS |
325 | break; |
326 | case OPT_NO_INTERN: | |
0f113f3e | 327 | verify_flags |= OCSP_NOINTERN; |
7e1b7485 RS |
328 | break; |
329 | case OPT_BADSIG: | |
0f113f3e | 330 | badsig = 1; |
7e1b7485 RS |
331 | break; |
332 | case OPT_TEXT: | |
333 | req_text = resp_text = 1; | |
334 | break; | |
335 | case OPT_REQ_TEXT: | |
0f113f3e | 336 | req_text = 1; |
7e1b7485 RS |
337 | break; |
338 | case OPT_RESP_TEXT: | |
0f113f3e | 339 | resp_text = 1; |
7e1b7485 RS |
340 | break; |
341 | case OPT_REQIN: | |
342 | reqin = opt_arg(); | |
343 | break; | |
344 | case OPT_RESPIN: | |
345 | respin = opt_arg(); | |
346 | break; | |
347 | case OPT_SIGNER: | |
348 | signfile = opt_arg(); | |
349 | break; | |
350 | case OPT_VAFILE: | |
351 | verify_certfile = opt_arg(); | |
352 | verify_flags |= OCSP_TRUSTOTHER; | |
353 | break; | |
354 | case OPT_SIGN_OTHER: | |
355 | sign_certfile = opt_arg(); | |
356 | break; | |
357 | case OPT_VERIFY_OTHER: | |
358 | verify_certfile = opt_arg(); | |
359 | break; | |
360 | case OPT_CAFILE: | |
361 | CAfile = opt_arg(); | |
362 | break; | |
363 | case OPT_CAPATH: | |
364 | CApath = opt_arg(); | |
365 | break; | |
2b6bcb70 MC |
366 | case OPT_NOCAFILE: |
367 | noCAfile = 1; | |
368 | break; | |
369 | case OPT_NOCAPATH: | |
370 | noCApath = 1; | |
371 | break; | |
7e1b7485 RS |
372 | case OPT_V_CASES: |
373 | if (!opt_verify(o, vpm)) | |
0f113f3e | 374 | goto end; |
7e1b7485 RS |
375 | vpmtouched++; |
376 | break; | |
377 | case OPT_VALIDITY_PERIOD: | |
378 | opt_long(opt_arg(), &nsec); | |
379 | break; | |
380 | case OPT_STATUS_AGE: | |
381 | opt_long(opt_arg(), &maxage); | |
382 | break; | |
383 | case OPT_SIGNKEY: | |
384 | keyfile = opt_arg(); | |
385 | break; | |
386 | case OPT_REQOUT: | |
387 | reqout = opt_arg(); | |
388 | break; | |
389 | case OPT_RESPOUT: | |
390 | respout = opt_arg(); | |
391 | break; | |
392 | case OPT_PATH: | |
393 | path = opt_arg(); | |
394 | break; | |
c6724060 | 395 | case OPT_ISSUER: |
a773b52a | 396 | issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate"); |
c6724060 RS |
397 | if (issuer == NULL) |
398 | goto end; | |
bb7fc98c DSH |
399 | if (issuers == NULL) { |
400 | if ((issuers = sk_X509_new_null()) == NULL) | |
401 | goto end; | |
402 | } | |
c6724060 RS |
403 | sk_X509_push(issuers, issuer); |
404 | break; | |
7e1b7485 RS |
405 | case OPT_CERT: |
406 | X509_free(cert); | |
a773b52a | 407 | cert = load_cert(opt_arg(), FORMAT_PEM, "certificate"); |
7e1b7485 RS |
408 | if (cert == NULL) |
409 | goto end; | |
410 | if (cert_id_md == NULL) | |
411 | cert_id_md = EVP_sha1(); | |
412 | if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids)) | |
413 | goto end; | |
414 | if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) | |
415 | goto end; | |
6302bbd2 | 416 | trailing_md = 0; |
7e1b7485 RS |
417 | break; |
418 | case OPT_SERIAL: | |
419 | if (cert_id_md == NULL) | |
420 | cert_id_md = EVP_sha1(); | |
421 | if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids)) | |
422 | goto end; | |
423 | if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) | |
424 | goto end; | |
6302bbd2 | 425 | trailing_md = 0; |
7e1b7485 RS |
426 | break; |
427 | case OPT_INDEX: | |
428 | ridx_filename = opt_arg(); | |
429 | break; | |
430 | case OPT_CA: | |
431 | rca_filename = opt_arg(); | |
432 | break; | |
433 | case OPT_NMIN: | |
434 | opt_int(opt_arg(), &nmin); | |
0f113f3e MC |
435 | if (ndays == -1) |
436 | ndays = 0; | |
7e1b7485 RS |
437 | break; |
438 | case OPT_REQUEST: | |
439 | opt_int(opt_arg(), &accept_count); | |
440 | break; | |
441 | case OPT_NDAYS: | |
442 | ndays = atoi(opt_arg()); | |
443 | break; | |
444 | case OPT_RSIGNER: | |
445 | rsignfile = opt_arg(); | |
446 | break; | |
447 | case OPT_RKEY: | |
448 | rkeyfile = opt_arg(); | |
449 | break; | |
450 | case OPT_ROTHER: | |
451 | rcertfile = opt_arg(); | |
452 | break; | |
16e1b281 | 453 | case OPT_RMD: /* Response MessageDigest */ |
7e1b7485 RS |
454 | if (!opt_md(opt_arg(), &rsign_md)) |
455 | goto end; | |
456 | break; | |
04940147 | 457 | case OPT_RSIGOPT: |
b4dd21a7 DC |
458 | if (rsign_sigopts == NULL) |
459 | rsign_sigopts = sk_OPENSSL_STRING_new_null(); | |
460 | if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg())) | |
461 | goto end; | |
462 | break; | |
7e1b7485 RS |
463 | case OPT_HEADER: |
464 | header = opt_arg(); | |
465 | value = strchr(header, '='); | |
466 | if (value == NULL) { | |
467 | BIO_printf(bio_err, "Missing = in header key=value\n"); | |
468 | goto opthelp; | |
469 | } | |
470 | *value++ = '\0'; | |
471 | if (!X509V3_add_value(header, value, &headers)) | |
472 | goto end; | |
473 | break; | |
474 | case OPT_MD: | |
6302bbd2 | 475 | if (trailing_md) { |
7e1b7485 RS |
476 | BIO_printf(bio_err, |
477 | "%s: Digest must be before -cert or -serial\n", | |
478 | prog); | |
479 | goto opthelp; | |
480 | } | |
481 | if (!opt_md(opt_unknown(), &cert_id_md)) | |
482 | goto opthelp; | |
6302bbd2 | 483 | trailing_md = 1; |
7e1b7485 | 484 | break; |
3e3c7c36 | 485 | case OPT_MULTI: |
a7fb4fa1 | 486 | # ifdef OCSP_DAEMON |
3e3c7c36 | 487 | multi = atoi(opt_arg()); |
3e3c7c36 | 488 | # endif |
a7fb4fa1 | 489 | break; |
0f113f3e | 490 | } |
0f113f3e | 491 | } |
6302bbd2 DSH |
492 | if (trailing_md) { |
493 | BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n", | |
494 | prog); | |
495 | goto opthelp; | |
496 | } | |
7e1b7485 | 497 | argc = opt_num_rest(); |
03358517 KR |
498 | if (argc != 0) |
499 | goto opthelp; | |
0f113f3e MC |
500 | |
501 | /* Have we anything to do? */ | |
3e3c7c36 | 502 | if (req == NULL && reqin == NULL |
2234212c | 503 | && respin == NULL && !(port != NULL && ridx_filename != NULL)) |
7e1b7485 | 504 | goto opthelp; |
0f113f3e | 505 | |
bdd58d98 | 506 | out = bio_open_default(outfile, 'w', FORMAT_TEXT); |
7e1b7485 | 507 | if (out == NULL) |
0f113f3e | 508 | goto end; |
0f113f3e | 509 | |
2234212c | 510 | if (req == NULL && (add_nonce != 2)) |
0f113f3e MC |
511 | add_nonce = 0; |
512 | ||
2234212c | 513 | if (req == NULL && reqin != NULL) { |
bdd58d98 | 514 | derbio = bio_open_default(reqin, 'r', FORMAT_ASN1); |
7e1b7485 | 515 | if (derbio == NULL) |
0f113f3e | 516 | goto end; |
0f113f3e MC |
517 | req = d2i_OCSP_REQUEST_bio(derbio, NULL); |
518 | BIO_free(derbio); | |
2234212c | 519 | if (req == NULL) { |
0f113f3e MC |
520 | BIO_printf(bio_err, "Error reading OCSP request\n"); |
521 | goto end; | |
522 | } | |
523 | } | |
524 | ||
2234212c | 525 | if (req == NULL && port != NULL) { |
0f113f3e | 526 | acbio = init_responder(port); |
2234212c | 527 | if (acbio == NULL) |
0f113f3e MC |
528 | goto end; |
529 | } | |
530 | ||
2234212c PY |
531 | if (rsignfile != NULL) { |
532 | if (rkeyfile == NULL) | |
0f113f3e | 533 | rkeyfile = rsignfile; |
a773b52a | 534 | rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate"); |
2234212c | 535 | if (rsigner == NULL) { |
0f113f3e MC |
536 | BIO_printf(bio_err, "Error loading responder certificate\n"); |
537 | goto end; | |
538 | } | |
3bb0f989 TS |
539 | if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM, |
540 | NULL, "CA certificate")) | |
541 | goto end; | |
2234212c | 542 | if (rcertfile != NULL) { |
a773b52a | 543 | if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL, |
0996dc54 | 544 | "responder other certificates")) |
0f113f3e MC |
545 | goto end; |
546 | } | |
7e1b7485 | 547 | rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL, |
0f113f3e | 548 | "responder private key"); |
2234212c | 549 | if (rkey == NULL) |
0f113f3e MC |
550 | goto end; |
551 | } | |
c7d5ea26 | 552 | |
3e3c7c36 | 553 | if (ridx_filename != NULL |
e4693110 | 554 | && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) { |
c7d5ea26 VD |
555 | BIO_printf(bio_err, |
556 | "Responder mode requires certificate, key, and CA.\n"); | |
557 | goto end; | |
558 | } | |
559 | ||
3e3c7c36 | 560 | if (ridx_filename != NULL) { |
c7d5ea26 | 561 | rdb = load_index(ridx_filename, NULL); |
3e3c7c36 | 562 | if (rdb == NULL || !index_index(rdb)) { |
c7d5ea26 VD |
563 | ret = 1; |
564 | goto end; | |
565 | } | |
566 | } | |
567 | ||
3e3c7c36 VD |
568 | # ifdef OCSP_DAEMON |
569 | if (multi && acbio != NULL) | |
570 | spawn_loop(); | |
571 | if (acbio != NULL && req_timeout > 0) | |
572 | signal(SIGALRM, sock_timeout); | |
573 | #endif | |
574 | ||
2234212c | 575 | if (acbio != NULL) |
3e3c7c36 | 576 | log_message(LOG_INFO, "waiting for OCSP client connections..."); |
0f113f3e | 577 | |
2234212c | 578 | redo_accept: |
0f113f3e | 579 | |
2234212c | 580 | if (acbio != NULL) { |
3e3c7c36 VD |
581 | # ifdef OCSP_DAEMON |
582 | if (index_changed(rdb)) { | |
583 | CA_DB *newrdb = load_index(ridx_filename, NULL); | |
584 | ||
585 | if (newrdb != NULL) { | |
586 | free_index(rdb); | |
587 | rdb = newrdb; | |
588 | } else { | |
589 | log_message(LOG_ERR, "error reloading updated index: %s", | |
590 | ridx_filename); | |
591 | } | |
592 | } | |
593 | # endif | |
594 | ||
595 | req = NULL; | |
596 | if (!do_responder(&req, &cbio, acbio, req_timeout)) | |
597 | goto redo_accept; | |
598 | ||
2234212c | 599 | if (req == NULL) { |
0f113f3e MC |
600 | resp = |
601 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, | |
602 | NULL); | |
603 | send_ocsp_response(cbio, resp); | |
604 | goto done_resp; | |
605 | } | |
606 | } | |
607 | ||
2234212c PY |
608 | if (req == NULL |
609 | && (signfile != NULL || reqout != NULL | |
610 | || host != NULL || add_nonce || ridx_filename != NULL)) { | |
0f113f3e MC |
611 | BIO_printf(bio_err, "Need an OCSP request for this operation!\n"); |
612 | goto end; | |
613 | } | |
614 | ||
2234212c | 615 | if (req != NULL && add_nonce) |
0f113f3e MC |
616 | OCSP_request_add1_nonce(req, NULL, -1); |
617 | ||
2234212c PY |
618 | if (signfile != NULL) { |
619 | if (keyfile == NULL) | |
0f113f3e | 620 | keyfile = signfile; |
a773b52a | 621 | signer = load_cert(signfile, FORMAT_PEM, "signer certificate"); |
2234212c | 622 | if (signer == NULL) { |
0f113f3e MC |
623 | BIO_printf(bio_err, "Error loading signer certificate\n"); |
624 | goto end; | |
625 | } | |
2234212c | 626 | if (sign_certfile != NULL) { |
a773b52a | 627 | if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL, |
0996dc54 | 628 | "signer certificates")) |
0f113f3e MC |
629 | goto end; |
630 | } | |
7e1b7485 | 631 | key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL, |
0f113f3e | 632 | "signer private key"); |
2234212c | 633 | if (key == NULL) |
0f113f3e MC |
634 | goto end; |
635 | ||
636 | if (!OCSP_request_sign | |
637 | (req, signer, key, NULL, sign_other, sign_flags)) { | |
638 | BIO_printf(bio_err, "Error signing OCSP request\n"); | |
639 | goto end; | |
640 | } | |
641 | } | |
642 | ||
2234212c | 643 | if (req_text && req != NULL) |
0f113f3e MC |
644 | OCSP_REQUEST_print(out, req, 0); |
645 | ||
2234212c | 646 | if (reqout != NULL) { |
bdd58d98 | 647 | derbio = bio_open_default(reqout, 'w', FORMAT_ASN1); |
7e1b7485 | 648 | if (derbio == NULL) |
0f113f3e | 649 | goto end; |
0f113f3e MC |
650 | i2d_OCSP_REQUEST_bio(derbio, req); |
651 | BIO_free(derbio); | |
652 | } | |
653 | ||
2234212c | 654 | if (rdb != NULL) { |
b4dd21a7 DC |
655 | make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey, |
656 | rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig); | |
2234212c | 657 | if (cbio != NULL) |
0f113f3e | 658 | send_ocsp_response(cbio, resp); |
2234212c | 659 | } else if (host != NULL) { |
0f113f3e | 660 | # ifndef OPENSSL_NO_SOCK |
7e1b7485 | 661 | resp = process_responder(req, host, path, |
0f113f3e | 662 | port, use_ssl, headers, req_timeout); |
2234212c | 663 | if (resp == NULL) |
0f113f3e MC |
664 | goto end; |
665 | # else | |
666 | BIO_printf(bio_err, | |
667 | "Error creating connect BIO - sockets not supported.\n"); | |
668 | goto end; | |
669 | # endif | |
2234212c | 670 | } else if (respin != NULL) { |
bdd58d98 | 671 | derbio = bio_open_default(respin, 'r', FORMAT_ASN1); |
7e1b7485 | 672 | if (derbio == NULL) |
0f113f3e | 673 | goto end; |
0f113f3e MC |
674 | resp = d2i_OCSP_RESPONSE_bio(derbio, NULL); |
675 | BIO_free(derbio); | |
2234212c | 676 | if (resp == NULL) { |
0f113f3e MC |
677 | BIO_printf(bio_err, "Error reading OCSP response\n"); |
678 | goto end; | |
679 | } | |
0f113f3e MC |
680 | } else { |
681 | ret = 0; | |
682 | goto end; | |
683 | } | |
684 | ||
685 | done_resp: | |
686 | ||
2234212c | 687 | if (respout != NULL) { |
bdd58d98 | 688 | derbio = bio_open_default(respout, 'w', FORMAT_ASN1); |
7e1b7485 | 689 | if (derbio == NULL) |
0f113f3e | 690 | goto end; |
0f113f3e MC |
691 | i2d_OCSP_RESPONSE_bio(derbio, resp); |
692 | BIO_free(derbio); | |
693 | } | |
694 | ||
695 | i = OCSP_response_status(resp); | |
0f113f3e MC |
696 | if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
697 | BIO_printf(out, "Responder Error: %s (%d)\n", | |
698 | OCSP_response_status_str(i), i); | |
3e3c7c36 VD |
699 | if (!ignore_err) { |
700 | ret = 0; | |
701 | goto end; | |
702 | } | |
0f113f3e MC |
703 | } |
704 | ||
705 | if (resp_text) | |
706 | OCSP_RESPONSE_print(out, resp, 0); | |
707 | ||
708 | /* If running as responder don't verify our own response */ | |
2234212c | 709 | if (cbio != NULL) { |
e46bcca2 AE |
710 | /* If not unlimited, see if we took all we should. */ |
711 | if (accept_count != -1 && --accept_count <= 0) { | |
7e1b7485 RS |
712 | ret = 0; |
713 | goto end; | |
0f113f3e | 714 | } |
7e1b7485 RS |
715 | BIO_free_all(cbio); |
716 | cbio = NULL; | |
717 | OCSP_REQUEST_free(req); | |
718 | req = NULL; | |
719 | OCSP_RESPONSE_free(resp); | |
720 | resp = NULL; | |
721 | goto redo_accept; | |
722 | } | |
2234212c | 723 | if (ridx_filename != NULL) { |
0f113f3e MC |
724 | ret = 0; |
725 | goto end; | |
726 | } | |
727 | ||
2234212c | 728 | if (store == NULL) { |
2b6bcb70 | 729 | store = setup_verify(CAfile, CApath, noCAfile, noCApath); |
7e1b7485 RS |
730 | if (!store) |
731 | goto end; | |
732 | } | |
733 | if (vpmtouched) | |
0f113f3e | 734 | X509_STORE_set1_param(store, vpm); |
2234212c | 735 | if (verify_certfile != NULL) { |
a773b52a | 736 | if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL, |
0996dc54 | 737 | "validator certificate")) |
0f113f3e MC |
738 | goto end; |
739 | } | |
740 | ||
741 | bs = OCSP_response_get1_basic(resp); | |
2234212c | 742 | if (bs == NULL) { |
0f113f3e MC |
743 | BIO_printf(bio_err, "Error parsing response\n"); |
744 | goto end; | |
745 | } | |
746 | ||
747 | ret = 0; | |
748 | ||
749 | if (!noverify) { | |
2234212c | 750 | if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) { |
0f113f3e MC |
751 | if (i == -1) |
752 | BIO_printf(bio_err, "WARNING: no nonce in response\n"); | |
753 | else { | |
754 | BIO_printf(bio_err, "Nonce Verify error\n"); | |
755 | ret = 1; | |
756 | goto end; | |
757 | } | |
758 | } | |
759 | ||
760 | i = OCSP_basic_verify(bs, verify_other, store, verify_flags); | |
c6724060 RS |
761 | if (i <= 0 && issuers) { |
762 | i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER); | |
763 | if (i > 0) | |
764 | ERR_clear_error(); | |
765 | } | |
0f113f3e MC |
766 | if (i <= 0) { |
767 | BIO_printf(bio_err, "Response Verify Failure\n"); | |
768 | ERR_print_errors(bio_err); | |
769 | ret = 1; | |
2234212c | 770 | } else { |
0f113f3e | 771 | BIO_printf(bio_err, "Response verify OK\n"); |
2234212c | 772 | } |
0f113f3e MC |
773 | } |
774 | ||
7e1b7485 | 775 | print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage); |
0f113f3e MC |
776 | |
777 | end: | |
778 | ERR_print_errors(bio_err); | |
779 | X509_free(signer); | |
780 | X509_STORE_free(store); | |
222561fe | 781 | X509_VERIFY_PARAM_free(vpm); |
89623f84 | 782 | sk_OPENSSL_STRING_free(rsign_sigopts); |
0f113f3e MC |
783 | EVP_PKEY_free(key); |
784 | EVP_PKEY_free(rkey); | |
0f113f3e | 785 | X509_free(cert); |
bb7fc98c | 786 | sk_X509_pop_free(issuers, X509_free); |
0f113f3e | 787 | X509_free(rsigner); |
3bb0f989 | 788 | sk_X509_pop_free(rca_cert, X509_free); |
0f113f3e MC |
789 | free_index(rdb); |
790 | BIO_free_all(cbio); | |
791 | BIO_free_all(acbio); | |
4e525a0b | 792 | BIO_free_all(out); |
0f113f3e MC |
793 | OCSP_REQUEST_free(req); |
794 | OCSP_RESPONSE_free(resp); | |
795 | OCSP_BASICRESP_free(bs); | |
796 | sk_OPENSSL_STRING_free(reqnames); | |
797 | sk_OCSP_CERTID_free(ids); | |
798 | sk_X509_pop_free(sign_other, X509_free); | |
799 | sk_X509_pop_free(verify_other, X509_free); | |
800 | sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); | |
b548a1f1 RS |
801 | OPENSSL_free(thost); |
802 | OPENSSL_free(tport); | |
803 | OPENSSL_free(tpath); | |
0f113f3e | 804 | |
26a7d938 | 805 | return ret; |
5782ceb2 DSH |
806 | } |
807 | ||
3e3c7c36 VD |
808 | static void |
809 | log_message(int level, const char *fmt, ...) | |
810 | { | |
811 | va_list ap; | |
812 | ||
813 | va_start(ap, fmt); | |
814 | # ifdef OCSP_DAEMON | |
815 | if (multi) { | |
a9dd51a8 EC |
816 | char buf[1024]; |
817 | if (vsnprintf(buf, sizeof(buf), fmt, ap) > 0) { | |
818 | syslog(level, "%s", buf); | |
819 | } | |
3e3c7c36 VD |
820 | if (level >= LOG_ERR) |
821 | ERR_print_errors_cb(print_syslog, &level); | |
822 | } | |
823 | # endif | |
824 | if (!multi) { | |
825 | BIO_printf(bio_err, "%s: ", prog); | |
826 | BIO_vprintf(bio_err, fmt, ap); | |
827 | BIO_printf(bio_err, "\n"); | |
828 | } | |
829 | va_end(ap); | |
830 | } | |
831 | ||
832 | # ifdef OCSP_DAEMON | |
833 | ||
834 | static int print_syslog(const char *str, size_t len, void *levPtr) | |
835 | { | |
836 | int level = *(int *)levPtr; | |
837 | int ilen = (len > MAXERRLEN) ? MAXERRLEN : len; | |
838 | ||
839 | syslog(level, "%.*s", ilen, str); | |
840 | ||
841 | return ilen; | |
842 | } | |
843 | ||
844 | static int index_changed(CA_DB *rdb) | |
845 | { | |
846 | struct stat sb; | |
847 | ||
848 | if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) { | |
849 | if (rdb->dbst.st_mtime != sb.st_mtime | |
850 | || rdb->dbst.st_ctime != sb.st_ctime | |
851 | || rdb->dbst.st_ino != sb.st_ino | |
852 | || rdb->dbst.st_dev != sb.st_dev) { | |
853 | syslog(LOG_INFO, "index file changed, reloading"); | |
854 | return 1; | |
855 | } | |
856 | } | |
857 | return 0; | |
858 | } | |
859 | ||
860 | static void killall(int ret, pid_t *kidpids) | |
861 | { | |
862 | int i; | |
863 | ||
864 | for (i = 0; i < multi; ++i) | |
865 | if (kidpids[i] != 0) | |
866 | (void)kill(kidpids[i], SIGTERM); | |
867 | sleep(1); | |
868 | exit(ret); | |
869 | } | |
870 | ||
871 | static int termsig = 0; | |
872 | ||
873 | static void noteterm (int sig) | |
874 | { | |
875 | termsig = sig; | |
876 | } | |
877 | ||
878 | /* | |
879 | * Loop spawning up to `multi` child processes, only child processes return | |
880 | * from this function. The parent process loops until receiving a termination | |
881 | * signal, kills extant children and exits without returning. | |
882 | */ | |
883 | static void spawn_loop(void) | |
884 | { | |
3e3c7c36 VD |
885 | pid_t *kidpids = NULL; |
886 | int status; | |
887 | int procs = 0; | |
888 | int i; | |
889 | ||
890 | openlog(prog, LOG_PID, LOG_DAEMON); | |
891 | ||
892 | if (setpgid(0, 0)) { | |
893 | syslog(LOG_ERR, "fatal: error detaching from parent process group: %s", | |
894 | strerror(errno)); | |
895 | exit(1); | |
896 | } | |
897 | kidpids = app_malloc(multi * sizeof(*kidpids), "child PID array"); | |
898 | for (i = 0; i < multi; ++i) | |
899 | kidpids[i] = 0; | |
900 | ||
901 | signal(SIGINT, noteterm); | |
902 | signal(SIGTERM, noteterm); | |
903 | ||
904 | while (termsig == 0) { | |
905 | pid_t fpid; | |
906 | ||
907 | /* | |
908 | * Wait for a child to replace when we're at the limit. | |
909 | * Slow down if a child exited abnormally or waitpid() < 0 | |
910 | */ | |
911 | while (termsig == 0 && procs >= multi) { | |
912 | if ((fpid = waitpid(-1, &status, 0)) > 0) { | |
913 | for (i = 0; i < procs; ++i) { | |
914 | if (kidpids[i] == fpid) { | |
915 | kidpids[i] = 0; | |
916 | --procs; | |
917 | break; | |
918 | } | |
919 | } | |
920 | if (i >= multi) { | |
921 | syslog(LOG_ERR, "fatal: internal error: " | |
922 | "no matching child slot for pid: %ld", | |
923 | (long) fpid); | |
924 | killall(1, kidpids); | |
925 | } | |
926 | if (status != 0) { | |
927 | if (WIFEXITED(status)) | |
928 | syslog(LOG_WARNING, "child process: %ld, exit status: %d", | |
929 | (long)fpid, WEXITSTATUS(status)); | |
930 | else if (WIFSIGNALED(status)) | |
931 | syslog(LOG_WARNING, "child process: %ld, term signal %d%s", | |
932 | (long)fpid, WTERMSIG(status), | |
a9dd51a8 EC |
933 | #ifdef WCOREDUMP |
934 | WCOREDUMP(status) ? " (core dumped)" : | |
935 | #endif | |
936 | ""); | |
3e3c7c36 VD |
937 | sleep(1); |
938 | } | |
939 | break; | |
940 | } else if (errno != EINTR) { | |
941 | syslog(LOG_ERR, "fatal: waitpid(): %s", strerror(errno)); | |
942 | killall(1, kidpids); | |
943 | } | |
944 | } | |
945 | if (termsig) | |
946 | break; | |
947 | ||
948 | switch(fpid = fork()) { | |
949 | case -1: /* error */ | |
950 | /* System critically low on memory, pause and try again later */ | |
951 | sleep(30); | |
952 | break; | |
953 | case 0: /* child */ | |
954 | signal(SIGINT, SIG_DFL); | |
955 | signal(SIGTERM, SIG_DFL); | |
956 | if (termsig) | |
957 | _exit(0); | |
958 | if (RAND_poll() <= 0) { | |
959 | syslog(LOG_ERR, "fatal: RAND_poll() failed"); | |
960 | _exit(1); | |
961 | } | |
962 | return; | |
963 | default: /* parent */ | |
964 | for (i = 0; i < multi; ++i) { | |
965 | if (kidpids[i] == 0) { | |
966 | kidpids[i] = fpid; | |
967 | procs++; | |
968 | break; | |
969 | } | |
970 | } | |
971 | if (i >= multi) { | |
972 | syslog(LOG_ERR, "fatal: internal error: no free child slots"); | |
973 | killall(1, kidpids); | |
974 | } | |
975 | break; | |
976 | } | |
977 | } | |
978 | ||
979 | /* The loop above can only break on termsig */ | |
aed3df20 | 980 | syslog(LOG_INFO, "terminating on signal: %d", termsig); |
3e3c7c36 VD |
981 | killall(0, kidpids); |
982 | } | |
983 | # endif | |
984 | ||
0f113f3e MC |
985 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, |
986 | const EVP_MD *cert_id_md, X509 *issuer, | |
987 | STACK_OF(OCSP_CERTID) *ids) | |
988 | { | |
989 | OCSP_CERTID *id; | |
2234212c PY |
990 | |
991 | if (issuer == NULL) { | |
0f113f3e MC |
992 | BIO_printf(bio_err, "No issuer certificate specified\n"); |
993 | return 0; | |
994 | } | |
96487cdd | 995 | if (*req == NULL) |
0f113f3e | 996 | *req = OCSP_REQUEST_new(); |
96487cdd | 997 | if (*req == NULL) |
0f113f3e MC |
998 | goto err; |
999 | id = OCSP_cert_to_id(cert_id_md, cert, issuer); | |
2234212c | 1000 | if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) |
0f113f3e MC |
1001 | goto err; |
1002 | if (!OCSP_request_add0_id(*req, id)) | |
1003 | goto err; | |
1004 | return 1; | |
1005 | ||
1006 | err: | |
1007 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
1012 | const EVP_MD *cert_id_md, X509 *issuer, | |
1013 | STACK_OF(OCSP_CERTID) *ids) | |
1014 | { | |
1015 | OCSP_CERTID *id; | |
1016 | X509_NAME *iname; | |
1017 | ASN1_BIT_STRING *ikey; | |
1018 | ASN1_INTEGER *sno; | |
2234212c PY |
1019 | |
1020 | if (issuer == NULL) { | |
0f113f3e MC |
1021 | BIO_printf(bio_err, "No issuer certificate specified\n"); |
1022 | return 0; | |
1023 | } | |
96487cdd | 1024 | if (*req == NULL) |
0f113f3e | 1025 | *req = OCSP_REQUEST_new(); |
96487cdd | 1026 | if (*req == NULL) |
0f113f3e MC |
1027 | goto err; |
1028 | iname = X509_get_subject_name(issuer); | |
1029 | ikey = X509_get0_pubkey_bitstr(issuer); | |
1030 | sno = s2i_ASN1_INTEGER(NULL, serial); | |
2234212c | 1031 | if (sno == NULL) { |
0f113f3e MC |
1032 | BIO_printf(bio_err, "Error converting serial number %s\n", serial); |
1033 | return 0; | |
1034 | } | |
1035 | id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno); | |
1036 | ASN1_INTEGER_free(sno); | |
96487cdd | 1037 | if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) |
0f113f3e MC |
1038 | goto err; |
1039 | if (!OCSP_request_add0_id(*req, id)) | |
1040 | goto err; | |
1041 | return 1; | |
1042 | ||
1043 | err: | |
1044 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
1045 | return 0; | |
1046 | } | |
73758d43 | 1047 | |
7e1b7485 | 1048 | static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, |
0f113f3e MC |
1049 | STACK_OF(OPENSSL_STRING) *names, |
1050 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
1051 | long maxage) | |
1052 | { | |
1053 | OCSP_CERTID *id; | |
cc696296 | 1054 | const char *name; |
7e1b7485 | 1055 | int i, status, reason; |
0f113f3e MC |
1056 | ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd; |
1057 | ||
2234212c | 1058 | if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names) |
0f113f3e | 1059 | || !sk_OCSP_CERTID_num(ids)) |
7e1b7485 | 1060 | return; |
0f113f3e MC |
1061 | |
1062 | for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) { | |
1063 | id = sk_OCSP_CERTID_value(ids, i); | |
1064 | name = sk_OPENSSL_STRING_value(names, i); | |
1065 | BIO_printf(out, "%s: ", name); | |
1066 | ||
1067 | if (!OCSP_resp_find_status(bs, id, &status, &reason, | |
1068 | &rev, &thisupd, &nextupd)) { | |
1069 | BIO_puts(out, "ERROR: No Status found.\n"); | |
1070 | continue; | |
1071 | } | |
1072 | ||
1073 | /* | |
1074 | * Check validity: if invalid write to output BIO so we know which | |
1075 | * response this refers to. | |
1076 | */ | |
1077 | if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) { | |
1078 | BIO_puts(out, "WARNING: Status times invalid.\n"); | |
1079 | ERR_print_errors(out); | |
1080 | } | |
1081 | BIO_printf(out, "%s\n", OCSP_cert_status_str(status)); | |
1082 | ||
1083 | BIO_puts(out, "\tThis Update: "); | |
1084 | ASN1_GENERALIZEDTIME_print(out, thisupd); | |
1085 | BIO_puts(out, "\n"); | |
1086 | ||
1087 | if (nextupd) { | |
1088 | BIO_puts(out, "\tNext Update: "); | |
1089 | ASN1_GENERALIZEDTIME_print(out, nextupd); | |
1090 | BIO_puts(out, "\n"); | |
1091 | } | |
1092 | ||
1093 | if (status != V_OCSP_CERTSTATUS_REVOKED) | |
1094 | continue; | |
1095 | ||
1096 | if (reason != -1) | |
1097 | BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason)); | |
1098 | ||
1099 | BIO_puts(out, "\tRevocation Time: "); | |
1100 | ASN1_GENERALIZEDTIME_print(out, rev); | |
1101 | BIO_puts(out, "\n"); | |
1102 | } | |
0f113f3e MC |
1103 | } |
1104 | ||
b4dd21a7 | 1105 | static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req, |
3bb0f989 | 1106 | CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, |
0f113f3e | 1107 | EVP_PKEY *rkey, const EVP_MD *rmd, |
b4dd21a7 | 1108 | STACK_OF(OPENSSL_STRING) *sigopts, |
0f113f3e MC |
1109 | STACK_OF(X509) *rother, unsigned long flags, |
1110 | int nmin, int ndays, int badsig) | |
1111 | { | |
1112 | ASN1_TIME *thisupd = NULL, *nextupd = NULL; | |
3bb0f989 | 1113 | OCSP_CERTID *cid; |
0f113f3e | 1114 | OCSP_BASICRESP *bs = NULL; |
7e1b7485 | 1115 | int i, id_count; |
b4dd21a7 DC |
1116 | EVP_MD_CTX *mctx = NULL; |
1117 | EVP_PKEY_CTX *pkctx = NULL; | |
0f113f3e MC |
1118 | |
1119 | id_count = OCSP_request_onereq_count(req); | |
1120 | ||
1121 | if (id_count <= 0) { | |
1122 | *resp = | |
1123 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL); | |
1124 | goto end; | |
1125 | } | |
1126 | ||
1127 | bs = OCSP_BASICRESP_new(); | |
1128 | thisupd = X509_gmtime_adj(NULL, 0); | |
1129 | if (ndays != -1) | |
9aa00b18 | 1130 | nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL); |
0f113f3e MC |
1131 | |
1132 | /* Examine each certificate id in the request */ | |
1133 | for (i = 0; i < id_count; i++) { | |
1134 | OCSP_ONEREQ *one; | |
1135 | ASN1_INTEGER *serial; | |
1136 | char **inf; | |
3bb0f989 TS |
1137 | int jj; |
1138 | int found = 0; | |
0f113f3e MC |
1139 | ASN1_OBJECT *cert_id_md_oid; |
1140 | const EVP_MD *cert_id_md; | |
1141 | one = OCSP_request_onereq_get0(req, i); | |
1142 | cid = OCSP_onereq_get0_id(one); | |
1143 | ||
1144 | OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid); | |
1145 | ||
1146 | cert_id_md = EVP_get_digestbyobj(cert_id_md_oid); | |
2234212c | 1147 | if (cert_id_md == NULL) { |
0f113f3e MC |
1148 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, |
1149 | NULL); | |
1150 | goto end; | |
1151 | } | |
3bb0f989 TS |
1152 | for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) { |
1153 | X509 *ca_cert = sk_X509_value(ca, jj); | |
1154 | OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert); | |
1155 | ||
1156 | if (OCSP_id_issuer_cmp(ca_id, cid) == 0) | |
1157 | found = 1; | |
1158 | ||
1159 | OCSP_CERTID_free(ca_id); | |
1160 | } | |
0f113f3e | 1161 | |
3bb0f989 | 1162 | if (!found) { |
0f113f3e MC |
1163 | OCSP_basic_add1_status(bs, cid, |
1164 | V_OCSP_CERTSTATUS_UNKNOWN, | |
1165 | 0, NULL, thisupd, nextupd); | |
1166 | continue; | |
1167 | } | |
1168 | OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid); | |
1169 | inf = lookup_serial(db, serial); | |
2234212c | 1170 | if (inf == NULL) { |
0f113f3e MC |
1171 | OCSP_basic_add1_status(bs, cid, |
1172 | V_OCSP_CERTSTATUS_UNKNOWN, | |
1173 | 0, NULL, thisupd, nextupd); | |
2234212c | 1174 | } else if (inf[DB_type][0] == DB_TYPE_VAL) { |
0f113f3e MC |
1175 | OCSP_basic_add1_status(bs, cid, |
1176 | V_OCSP_CERTSTATUS_GOOD, | |
1177 | 0, NULL, thisupd, nextupd); | |
2234212c | 1178 | } else if (inf[DB_type][0] == DB_TYPE_REV) { |
0f113f3e MC |
1179 | ASN1_OBJECT *inst = NULL; |
1180 | ASN1_TIME *revtm = NULL; | |
1181 | ASN1_GENERALIZEDTIME *invtm = NULL; | |
1182 | OCSP_SINGLERESP *single; | |
1183 | int reason = -1; | |
1184 | unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]); | |
1185 | single = OCSP_basic_add1_status(bs, cid, | |
1186 | V_OCSP_CERTSTATUS_REVOKED, | |
1187 | reason, revtm, thisupd, nextupd); | |
2234212c | 1188 | if (invtm != NULL) |
0f113f3e MC |
1189 | OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, |
1190 | invtm, 0, 0); | |
2234212c | 1191 | else if (inst != NULL) |
0f113f3e MC |
1192 | OCSP_SINGLERESP_add1_ext_i2d(single, |
1193 | NID_hold_instruction_code, inst, | |
1194 | 0, 0); | |
1195 | ASN1_OBJECT_free(inst); | |
1196 | ASN1_TIME_free(revtm); | |
1197 | ASN1_GENERALIZEDTIME_free(invtm); | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | OCSP_copy_nonce(bs, req); | |
1202 | ||
b4dd21a7 DC |
1203 | mctx = EVP_MD_CTX_new(); |
1204 | if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) { | |
1205 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL); | |
1206 | goto end; | |
1207 | } | |
1208 | for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) { | |
1209 | char *sigopt = sk_OPENSSL_STRING_value(sigopts, i); | |
89623f84 | 1210 | |
b4dd21a7 DC |
1211 | if (pkey_ctrl_string(pkctx, sigopt) <= 0) { |
1212 | BIO_printf(err, "parameter error \"%s\"\n", sigopt); | |
1213 | ERR_print_errors(bio_err); | |
1214 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, | |
1215 | NULL); | |
1216 | goto end; | |
1217 | } | |
1218 | } | |
1219 | OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags); | |
0f113f3e | 1220 | |
6ef869d7 | 1221 | if (badsig) { |
a0754084 DSH |
1222 | const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs); |
1223 | corrupt_signature(sig); | |
6ef869d7 | 1224 | } |
0f113f3e MC |
1225 | |
1226 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs); | |
1227 | ||
1228 | end: | |
89623f84 | 1229 | EVP_MD_CTX_free(mctx); |
0f113f3e MC |
1230 | ASN1_TIME_free(thisupd); |
1231 | ASN1_TIME_free(nextupd); | |
0f113f3e | 1232 | OCSP_BASICRESP_free(bs); |
0f113f3e | 1233 | } |
ee306a13 | 1234 | |
f85b68cd | 1235 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser) |
0f113f3e MC |
1236 | { |
1237 | int i; | |
1238 | BIGNUM *bn = NULL; | |
1239 | char *itmp, *row[DB_NUMBER], **rrow; | |
1240 | for (i = 0; i < DB_NUMBER; i++) | |
1241 | row[i] = NULL; | |
1242 | bn = ASN1_INTEGER_to_BN(ser, NULL); | |
1243 | OPENSSL_assert(bn); /* FIXME: should report an error at this | |
1244 | * point and abort */ | |
1245 | if (BN_is_zero(bn)) | |
7644a9ae | 1246 | itmp = OPENSSL_strdup("00"); |
0f113f3e MC |
1247 | else |
1248 | itmp = BN_bn2hex(bn); | |
1249 | row[DB_serial] = itmp; | |
1250 | BN_free(bn); | |
1251 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
1252 | OPENSSL_free(itmp); | |
1253 | return rrow; | |
1254 | } | |
ee306a13 DSH |
1255 | |
1256 | /* Quick and dirty OCSP server: read in and parse input request */ | |
1257 | ||
c45a48c1 | 1258 | static BIO *init_responder(const char *port) |
0f113f3e | 1259 | { |
366e2a60 | 1260 | # ifdef OPENSSL_NO_SOCK |
0f113f3e MC |
1261 | BIO_printf(bio_err, |
1262 | "Error setting up accept BIO - sockets not supported.\n"); | |
366e2a60 | 1263 | return NULL; |
f863ad0c MC |
1264 | # else |
1265 | BIO *acbio = NULL, *bufbio = NULL; | |
1266 | ||
366e2a60 | 1267 | bufbio = BIO_new(BIO_f_buffer()); |
96487cdd | 1268 | if (bufbio == NULL) |
0f113f3e | 1269 | goto err; |
366e2a60 RS |
1270 | acbio = BIO_new(BIO_s_accept()); |
1271 | if (acbio == NULL | |
1272 | || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0 | |
1273 | || BIO_set_accept_port(acbio, port) < 0) { | |
3e3c7c36 | 1274 | log_message(LOG_ERR, "Error setting up accept BIO"); |
366e2a60 RS |
1275 | goto err; |
1276 | } | |
1277 | ||
0f113f3e MC |
1278 | BIO_set_accept_bios(acbio, bufbio); |
1279 | bufbio = NULL; | |
0f113f3e | 1280 | if (BIO_do_accept(acbio) <= 0) { |
3e3c7c36 | 1281 | log_message(LOG_ERR, "Error starting accept"); |
0f113f3e MC |
1282 | goto err; |
1283 | } | |
1284 | ||
1285 | return acbio; | |
1286 | ||
1287 | err: | |
1288 | BIO_free_all(acbio); | |
1289 | BIO_free(bufbio); | |
1290 | return NULL; | |
f863ad0c | 1291 | # endif |
0f113f3e | 1292 | } |
ee306a13 | 1293 | |
f863ad0c | 1294 | # ifndef OPENSSL_NO_SOCK |
fc3cec53 RS |
1295 | /* |
1296 | * Decode %xx URL-decoding in-place. Ignores mal-formed sequences. | |
1297 | */ | |
1298 | static int urldecode(char *p) | |
995101d6 RS |
1299 | { |
1300 | unsigned char *out = (unsigned char *)p; | |
fc3cec53 | 1301 | unsigned char *save = out; |
995101d6 RS |
1302 | |
1303 | for (; *p; p++) { | |
1304 | if (*p != '%') | |
1305 | *out++ = *p; | |
18295f0c | 1306 | else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) { |
14f051a0 RS |
1307 | /* Don't check, can't fail because of ixdigit() call. */ |
1308 | *out++ = (OPENSSL_hexchar2int(p[1]) << 4) | |
1309 | | OPENSSL_hexchar2int(p[2]); | |
995101d6 RS |
1310 | p += 2; |
1311 | } | |
fc3cec53 RS |
1312 | else |
1313 | return -1; | |
995101d6 | 1314 | } |
fc3cec53 RS |
1315 | *out = '\0'; |
1316 | return (int)(out - save); | |
995101d6 | 1317 | } |
f863ad0c | 1318 | # endif |
995101d6 | 1319 | |
3e3c7c36 VD |
1320 | # ifdef OCSP_DAEMON |
1321 | static void sock_timeout(int signum) | |
1322 | { | |
1323 | if (acfd != (int)INVALID_SOCKET) | |
1324 | (void)shutdown(acfd, SHUT_RD); | |
1325 | } | |
1326 | # endif | |
1327 | ||
1328 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, | |
1329 | int timeout) | |
0f113f3e | 1330 | { |
f863ad0c MC |
1331 | # ifdef OPENSSL_NO_SOCK |
1332 | return 0; | |
1333 | # else | |
7e1b7485 | 1334 | int len; |
0f113f3e | 1335 | OCSP_REQUEST *req = NULL; |
fc3cec53 | 1336 | char inbuf[2048], reqbuf[2048]; |
995101d6 RS |
1337 | char *p, *q; |
1338 | BIO *cbio = NULL, *getbio = NULL, *b64 = NULL; | |
3e3c7c36 | 1339 | const char *client; |
0f113f3e | 1340 | |
3e3c7c36 VD |
1341 | *preq = NULL; |
1342 | ||
1343 | /* Connection loss before accept() is routine, ignore silently */ | |
1344 | if (BIO_do_accept(acbio) <= 0) | |
0f113f3e | 1345 | return 0; |
0f113f3e MC |
1346 | |
1347 | cbio = BIO_pop(acbio); | |
1348 | *pcbio = cbio; | |
3e3c7c36 VD |
1349 | client = BIO_get_peer_name(cbio); |
1350 | ||
1351 | # ifdef OCSP_DAEMON | |
1352 | if (timeout > 0) { | |
1353 | (void) BIO_get_fd(cbio, &acfd); | |
1354 | alarm(timeout); | |
1355 | } | |
1356 | # endif | |
0f113f3e | 1357 | |
7e1b7485 | 1358 | /* Read the request line. */ |
cbe29648 | 1359 | len = BIO_gets(cbio, reqbuf, sizeof(reqbuf)); |
7e1b7485 | 1360 | if (len <= 0) |
3e3c7c36 VD |
1361 | goto out; |
1362 | ||
fc3cec53 | 1363 | if (strncmp(reqbuf, "GET ", 4) == 0) { |
995101d6 | 1364 | /* Expecting GET {sp} /URL {sp} HTTP/1.x */ |
fc3cec53 | 1365 | for (p = reqbuf + 4; *p == ' '; ++p) |
995101d6 | 1366 | continue; |
fc3cec53 | 1367 | if (*p != '/') { |
3e3c7c36 VD |
1368 | log_message(LOG_INFO, "Invalid request -- bad URL: %s", client); |
1369 | goto out; | |
995101d6 | 1370 | } |
fc3cec53 RS |
1371 | p++; |
1372 | ||
995101d6 RS |
1373 | /* Splice off the HTTP version identifier. */ |
1374 | for (q = p; *q; q++) | |
fc3cec53 | 1375 | if (*q == ' ') |
995101d6 | 1376 | break; |
fc3cec53 | 1377 | if (strncmp(q, " HTTP/1.", 8) != 0) { |
3e3c7c36 VD |
1378 | log_message(LOG_INFO, |
1379 | "Invalid request -- bad HTTP version: %s", client); | |
1380 | goto out; | |
995101d6 RS |
1381 | } |
1382 | *q = '\0'; | |
3e3c7c36 VD |
1383 | |
1384 | /* | |
1385 | * Skip "GET / HTTP..." requests often used by load-balancers | |
1386 | */ | |
1387 | if (p[1] == '\0') | |
1388 | goto out; | |
1389 | ||
fc3cec53 RS |
1390 | len = urldecode(p); |
1391 | if (len <= 0) { | |
3e3c7c36 VD |
1392 | log_message(LOG_INFO, |
1393 | "Invalid request -- bad URL encoding: %s", client); | |
1394 | goto out; | |
fc3cec53 RS |
1395 | } |
1396 | if ((getbio = BIO_new_mem_buf(p, len)) == NULL | |
1397 | || (b64 = BIO_new(BIO_f_base64())) == NULL) { | |
3e3c7c36 VD |
1398 | log_message(LOG_ERR, "Could not allocate base64 bio: %s", client); |
1399 | goto out; | |
fc3cec53 | 1400 | } |
995101d6 RS |
1401 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
1402 | getbio = BIO_push(b64, getbio); | |
fc3cec53 | 1403 | } else if (strncmp(reqbuf, "POST ", 5) != 0) { |
3e3c7c36 VD |
1404 | log_message(LOG_INFO, "Invalid request -- bad HTTP verb: %s", client); |
1405 | goto out; | |
7e1b7485 | 1406 | } |
fc3cec53 RS |
1407 | |
1408 | /* Read and skip past the headers. */ | |
0f113f3e | 1409 | for (;;) { |
cbe29648 | 1410 | len = BIO_gets(cbio, inbuf, sizeof(inbuf)); |
0f113f3e | 1411 | if (len <= 0) |
3e3c7c36 | 1412 | goto out; |
0f113f3e MC |
1413 | if ((inbuf[0] == '\r') || (inbuf[0] == '\n')) |
1414 | break; | |
1415 | } | |
1416 | ||
3e3c7c36 VD |
1417 | # ifdef OCSP_DAEMON |
1418 | /* Clear alarm before we close the client socket */ | |
1419 | alarm(0); | |
1420 | timeout = 0; | |
1421 | # endif | |
1422 | ||
0f113f3e | 1423 | /* Try to read OCSP request */ |
2234212c | 1424 | if (getbio != NULL) { |
995101d6 RS |
1425 | req = d2i_OCSP_REQUEST_bio(getbio, NULL); |
1426 | BIO_free_all(getbio); | |
2234212c | 1427 | } else { |
995101d6 | 1428 | req = d2i_OCSP_REQUEST_bio(cbio, NULL); |
2234212c | 1429 | } |
0f113f3e | 1430 | |
3e3c7c36 VD |
1431 | if (req == NULL) |
1432 | log_message(LOG_ERR, "Error parsing OCSP request"); | |
0f113f3e MC |
1433 | |
1434 | *preq = req; | |
1435 | ||
3e3c7c36 VD |
1436 | out: |
1437 | # ifdef OCSP_DAEMON | |
1438 | if (timeout > 0) | |
1439 | alarm(0); | |
1440 | acfd = (int)INVALID_SOCKET; | |
1441 | # endif | |
0f113f3e | 1442 | return 1; |
f863ad0c | 1443 | # endif |
0f113f3e | 1444 | } |
ee306a13 DSH |
1445 | |
1446 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp) | |
0f113f3e MC |
1447 | { |
1448 | char http_resp[] = | |
1449 | "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n" | |
1450 | "Content-Length: %d\r\n\r\n"; | |
2234212c | 1451 | if (cbio == NULL) |
0f113f3e MC |
1452 | return 0; |
1453 | BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL)); | |
1454 | i2d_OCSP_RESPONSE_bio(cbio, resp); | |
1455 | (void)BIO_flush(cbio); | |
1456 | return 1; | |
1457 | } | |
ee306a13 | 1458 | |
f9e55034 | 1459 | # ifndef OPENSSL_NO_SOCK |
76e0cd12 DSH |
1460 | static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, |
1461 | const char *path, | |
0f113f3e MC |
1462 | const STACK_OF(CONF_VALUE) *headers, |
1463 | OCSP_REQUEST *req, int req_timeout) | |
1464 | { | |
1465 | int fd; | |
1466 | int rv; | |
1467 | int i; | |
76e0cd12 | 1468 | int add_host = 1; |
0f113f3e MC |
1469 | OCSP_REQ_CTX *ctx = NULL; |
1470 | OCSP_RESPONSE *rsp = NULL; | |
1471 | fd_set confds; | |
1472 | struct timeval tv; | |
1473 | ||
1474 | if (req_timeout != -1) | |
1475 | BIO_set_nbio(cbio, 1); | |
1476 | ||
1477 | rv = BIO_do_connect(cbio); | |
1478 | ||
1479 | if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) { | |
7e1b7485 | 1480 | BIO_puts(bio_err, "Error connecting BIO\n"); |
0f113f3e MC |
1481 | return NULL; |
1482 | } | |
1483 | ||
4428c7db | 1484 | if (BIO_get_fd(cbio, &fd) < 0) { |
7e1b7485 | 1485 | BIO_puts(bio_err, "Can't get connection fd\n"); |
0f113f3e MC |
1486 | goto err; |
1487 | } | |
1488 | ||
1489 | if (req_timeout != -1 && rv <= 0) { | |
1490 | FD_ZERO(&confds); | |
1491 | openssl_fdset(fd, &confds); | |
1492 | tv.tv_usec = 0; | |
1493 | tv.tv_sec = req_timeout; | |
1494 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); | |
1495 | if (rv == 0) { | |
7e1b7485 | 1496 | BIO_puts(bio_err, "Timeout on connect\n"); |
0f113f3e MC |
1497 | return NULL; |
1498 | } | |
1499 | } | |
1500 | ||
1501 | ctx = OCSP_sendreq_new(cbio, path, NULL, -1); | |
96487cdd | 1502 | if (ctx == NULL) |
0f113f3e MC |
1503 | return NULL; |
1504 | ||
1505 | for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { | |
1506 | CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i); | |
76e0cd12 DSH |
1507 | if (add_host == 1 && strcasecmp("host", hdr->name) == 0) |
1508 | add_host = 0; | |
0f113f3e MC |
1509 | if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value)) |
1510 | goto err; | |
1511 | } | |
1512 | ||
76e0cd12 DSH |
1513 | if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0) |
1514 | goto err; | |
1515 | ||
0f113f3e MC |
1516 | if (!OCSP_REQ_CTX_set1_req(ctx, req)) |
1517 | goto err; | |
1518 | ||
1519 | for (;;) { | |
1520 | rv = OCSP_sendreq_nbio(&rsp, ctx); | |
1521 | if (rv != -1) | |
1522 | break; | |
1523 | if (req_timeout == -1) | |
1524 | continue; | |
1525 | FD_ZERO(&confds); | |
1526 | openssl_fdset(fd, &confds); | |
1527 | tv.tv_usec = 0; | |
1528 | tv.tv_sec = req_timeout; | |
2234212c | 1529 | if (BIO_should_read(cbio)) { |
0f113f3e | 1530 | rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv); |
2234212c | 1531 | } else if (BIO_should_write(cbio)) { |
0f113f3e | 1532 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); |
2234212c | 1533 | } else { |
7e1b7485 | 1534 | BIO_puts(bio_err, "Unexpected retry condition\n"); |
0f113f3e MC |
1535 | goto err; |
1536 | } | |
1537 | if (rv == 0) { | |
7e1b7485 | 1538 | BIO_puts(bio_err, "Timeout on request\n"); |
0f113f3e MC |
1539 | break; |
1540 | } | |
1541 | if (rv == -1) { | |
7e1b7485 | 1542 | BIO_puts(bio_err, "Select error\n"); |
0f113f3e MC |
1543 | break; |
1544 | } | |
1545 | ||
1546 | } | |
1547 | err: | |
895cba19 | 1548 | OCSP_REQ_CTX_free(ctx); |
0f113f3e MC |
1549 | |
1550 | return rsp; | |
1551 | } | |
454dbbc5 | 1552 | |
7e1b7485 | 1553 | OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, |
0f113f3e MC |
1554 | const char *host, const char *path, |
1555 | const char *port, int use_ssl, | |
82c49427 | 1556 | STACK_OF(CONF_VALUE) *headers, |
0f113f3e MC |
1557 | int req_timeout) |
1558 | { | |
1559 | BIO *cbio = NULL; | |
1560 | SSL_CTX *ctx = NULL; | |
1561 | OCSP_RESPONSE *resp = NULL; | |
ff4a9394 | 1562 | |
0f113f3e | 1563 | cbio = BIO_new_connect(host); |
2234212c | 1564 | if (cbio == NULL) { |
7e1b7485 | 1565 | BIO_printf(bio_err, "Error creating connect BIO\n"); |
0f113f3e MC |
1566 | goto end; |
1567 | } | |
2234212c | 1568 | if (port != NULL) |
0f113f3e MC |
1569 | BIO_set_conn_port(cbio, port); |
1570 | if (use_ssl == 1) { | |
1571 | BIO *sbio; | |
13c9bb3e | 1572 | ctx = SSL_CTX_new(TLS_client_method()); |
0f113f3e | 1573 | if (ctx == NULL) { |
7e1b7485 | 1574 | BIO_printf(bio_err, "Error creating SSL context.\n"); |
0f113f3e MC |
1575 | goto end; |
1576 | } | |
1577 | SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); | |
1578 | sbio = BIO_new_ssl(ctx, 1); | |
1579 | cbio = BIO_push(sbio, cbio); | |
1580 | } | |
ff4a9394 | 1581 | |
76e0cd12 | 1582 | resp = query_responder(cbio, host, path, headers, req, req_timeout); |
2234212c | 1583 | if (resp == NULL) |
0f113f3e MC |
1584 | BIO_printf(bio_err, "Error querying OCSP responder\n"); |
1585 | end: | |
ca3a82c3 | 1586 | BIO_free_all(cbio); |
62adbcee | 1587 | SSL_CTX_free(ctx); |
0f113f3e MC |
1588 | return resp; |
1589 | } | |
f9e55034 | 1590 | # endif |
67c8e7f4 | 1591 | |
85d686e7 | 1592 | #endif |