]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
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 | |
8 | */ | |
9 | ||
10 | #include <openssl/opensslconf.h> | |
11 | ||
12 | #ifdef OPENSSL_NO_OCSP | |
13 | NON_EMPTY_TRANSLATION_UNIT | |
14 | #else | |
15 | # ifdef OPENSSL_SYS_VMS | |
16 | # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined | |
17 | * on OpenVMS */ | |
18 | # endif | |
19 | ||
20 | # include <stdio.h> | |
21 | # include <stdlib.h> | |
22 | # include <string.h> | |
23 | # include <time.h> | |
24 | # include <ctype.h> | |
25 | ||
26 | /* Needs to be included before the openssl headers */ | |
27 | # include "apps.h" | |
28 | # include <openssl/e_os2.h> | |
29 | # include <openssl/crypto.h> | |
30 | # include <openssl/err.h> | |
31 | # include <openssl/ssl.h> | |
32 | # include <openssl/evp.h> | |
33 | # include <openssl/bn.h> | |
34 | # include <openssl/x509v3.h> | |
35 | ||
36 | /* Maximum leeway in validity period: default 5 minutes */ | |
37 | # define MAX_VALIDITY_PERIOD (5 * 60) | |
38 | ||
39 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, | |
40 | const EVP_MD *cert_id_md, X509 *issuer, | |
41 | STACK_OF(OCSP_CERTID) *ids); | |
42 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
43 | const EVP_MD *cert_id_md, X509 *issuer, | |
44 | STACK_OF(OCSP_CERTID) *ids); | |
45 | static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, | |
46 | STACK_OF(OPENSSL_STRING) *names, | |
47 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
48 | long maxage); | |
49 | static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, | |
50 | CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, | |
51 | EVP_PKEY *rkey, const EVP_MD *md, | |
52 | STACK_OF(X509) *rother, unsigned long flags, | |
53 | int nmin, int ndays, int badsig); | |
54 | ||
55 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser); | |
56 | static BIO *init_responder(const char *port); | |
57 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio); | |
58 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp); | |
59 | ||
60 | # ifndef OPENSSL_NO_SOCK | |
61 | static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, | |
62 | const char *path, | |
63 | const STACK_OF(CONF_VALUE) *headers, | |
64 | OCSP_REQUEST *req, int req_timeout); | |
65 | # endif | |
66 | ||
67 | typedef enum OPTION_choice { | |
68 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
69 | OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT, | |
70 | OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE, | |
71 | OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS, | |
72 | OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN, | |
73 | OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER, | |
74 | OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT, | |
75 | OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER, | |
76 | OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, | |
77 | OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT, | |
78 | OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL, | |
79 | OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER, | |
80 | OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER, | |
81 | OPT_V_ENUM, | |
82 | OPT_MD | |
83 | } OPTION_CHOICE; | |
84 | ||
85 | const OPTIONS ocsp_options[] = { | |
86 | {"help", OPT_HELP, '-', "Display this summary"}, | |
87 | {"out", OPT_OUTFILE, '>', "Output filename"}, | |
88 | {"timeout", OPT_TIMEOUT, 'p', | |
89 | "Connection timeout (in seconds) to the OCSP responder"}, | |
90 | {"url", OPT_URL, 's', "Responder URL"}, | |
91 | {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"}, | |
92 | {"port", OPT_PORT, 'p', "Port to run responder on"}, | |
93 | {"ignore_err", OPT_IGNORE_ERR, '-', | |
94 | "Ignore error on OCSP request or response and continue running"}, | |
95 | {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"}, | |
96 | {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"}, | |
97 | {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"}, | |
98 | {"resp_no_certs", OPT_RESP_NO_CERTS, '-', | |
99 | "Don't include any certificates in response"}, | |
100 | {"resp_key_id", OPT_RESP_KEY_ID, '-', | |
101 | "Identify response by signing certificate key ID"}, | |
102 | {"no_certs", OPT_NO_CERTS, '-', | |
103 | "Don't include any certificates in signed request"}, | |
104 | {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-', | |
105 | "Don't check signature on response"}, | |
106 | {"no_cert_verify", OPT_NO_CERT_VERIFY, '-', | |
107 | "Don't check signing certificate"}, | |
108 | {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"}, | |
109 | {"no_cert_checks", OPT_NO_CERT_CHECKS, '-', | |
110 | "Don't do additional checks on signing certificate"}, | |
111 | {"no_explicit", OPT_NO_EXPLICIT, '-', | |
112 | "Do not explicitly check the chain, just verify the root"}, | |
113 | {"trust_other", OPT_TRUST_OTHER, '-', | |
114 | "Don't verify additional certificates"}, | |
115 | {"no_intern", OPT_NO_INTERN, '-', | |
116 | "Don't search certificates contained in response for signer"}, | |
117 | {"badsig", OPT_BADSIG, '-', | |
118 | "Corrupt last byte of loaded OSCP response signature (for test)"}, | |
119 | {"text", OPT_TEXT, '-', "Print text form of request and response"}, | |
120 | {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"}, | |
121 | {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"}, | |
122 | {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"}, | |
123 | {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"}, | |
124 | {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"}, | |
125 | {"VAfile", OPT_VAFILE, '<', "Validator certificates file"}, | |
126 | {"sign_other", OPT_SIGN_OTHER, '<', | |
127 | "Additional certificates to include in signed request"}, | |
128 | {"verify_other", OPT_VERIFY_OTHER, '<', | |
129 | "Additional certificates to search for signer"}, | |
130 | {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, | |
131 | {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"}, | |
132 | {"no-CAfile", OPT_NOCAFILE, '-', | |
133 | "Do not load the default certificates file"}, | |
134 | {"no-CApath", OPT_NOCAPATH, '-', | |
135 | "Do not load certificates from the default certificates directory"}, | |
136 | {"validity_period", OPT_VALIDITY_PERIOD, 'u', | |
137 | "Maximum validity discrepancy in seconds"}, | |
138 | {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"}, | |
139 | {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"}, | |
140 | {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"}, | |
141 | {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"}, | |
142 | {"path", OPT_PATH, 's', "Path to use in OCSP request"}, | |
143 | {"issuer", OPT_ISSUER, '<', "Issuer certificate"}, | |
144 | {"cert", OPT_CERT, '<', "Certificate to check"}, | |
145 | {"serial", OPT_SERIAL, 's', "Serial number to check"}, | |
146 | {"index", OPT_INDEX, '<', "Certificate status index file"}, | |
147 | {"CA", OPT_CA, '<', "CA certificate"}, | |
148 | {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"}, | |
149 | {"nrequest", OPT_REQUEST, 'p', | |
150 | "Number of requests to accept (default unlimited)"}, | |
151 | {"ndays", OPT_NDAYS, 'p', "Number of days before next update"}, | |
152 | {"rsigner", OPT_RSIGNER, '<', | |
153 | "Responder certificate to sign responses with"}, | |
154 | {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"}, | |
155 | {"rother", OPT_ROTHER, '<', "Other certificates to include in response"}, | |
156 | {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"}, | |
157 | {"header", OPT_HEADER, 's', "key=value header to add"}, | |
158 | {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"}, | |
159 | OPT_V_OPTIONS, | |
160 | {NULL} | |
161 | }; | |
162 | ||
163 | int ocsp_main(int argc, char **argv) | |
164 | { | |
165 | BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL; | |
166 | const EVP_MD *cert_id_md = NULL, *rsign_md = NULL; | |
167 | int trailing_md = 0; | |
168 | CA_DB *rdb = NULL; | |
169 | EVP_PKEY *key = NULL, *rkey = NULL; | |
170 | OCSP_BASICRESP *bs = NULL; | |
171 | OCSP_REQUEST *req = NULL; | |
172 | OCSP_RESPONSE *resp = NULL; | |
173 | STACK_OF(CONF_VALUE) *headers = NULL; | |
174 | STACK_OF(OCSP_CERTID) *ids = NULL; | |
175 | STACK_OF(OPENSSL_STRING) *reqnames = NULL; | |
176 | STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL; | |
177 | STACK_OF(X509) *issuers = NULL; | |
178 | X509 *issuer = NULL, *cert = NULL; | |
179 | STACK_OF(X509) *rca_cert = NULL; | |
180 | X509 *signer = NULL, *rsigner = NULL; | |
181 | X509_STORE *store = NULL; | |
182 | X509_VERIFY_PARAM *vpm = NULL; | |
183 | const char *CAfile = NULL, *CApath = NULL; | |
184 | char *header, *value; | |
185 | char *host = NULL, *port = NULL, *path = "/", *outfile = NULL; | |
186 | char *rca_filename = NULL, *reqin = NULL, *respin = NULL; | |
187 | char *reqout = NULL, *respout = NULL, *ridx_filename = NULL; | |
188 | char *rsignfile = NULL, *rkeyfile = NULL; | |
189 | char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL; | |
190 | char *signfile = NULL, *keyfile = NULL; | |
191 | char *thost = NULL, *tport = NULL, *tpath = NULL; | |
192 | int noCAfile = 0, noCApath = 0; | |
193 | int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1; | |
194 | int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1; | |
195 | int req_text = 0, resp_text = 0, ret = 1; | |
196 | #ifndef OPENSSL_NO_SOCK | |
197 | int req_timeout = -1; | |
198 | #endif | |
199 | long nsec = MAX_VALIDITY_PERIOD, maxage = -1; | |
200 | unsigned long sign_flags = 0, verify_flags = 0, rflags = 0; | |
201 | OPTION_CHOICE o; | |
202 | char *prog; | |
203 | ||
204 | reqnames = sk_OPENSSL_STRING_new_null(); | |
205 | if (reqnames == NULL) | |
206 | goto end; | |
207 | ids = sk_OCSP_CERTID_new_null(); | |
208 | if (ids == NULL) | |
209 | goto end; | |
210 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) | |
211 | return 1; | |
212 | ||
213 | prog = opt_init(argc, argv, ocsp_options); | |
214 | while ((o = opt_next()) != OPT_EOF) { | |
215 | switch (o) { | |
216 | case OPT_EOF: | |
217 | case OPT_ERR: | |
218 | opthelp: | |
219 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
220 | goto end; | |
221 | case OPT_HELP: | |
222 | ret = 0; | |
223 | opt_help(ocsp_options); | |
224 | goto end; | |
225 | case OPT_OUTFILE: | |
226 | outfile = opt_arg(); | |
227 | break; | |
228 | case OPT_TIMEOUT: | |
229 | #ifndef OPENSSL_NO_SOCK | |
230 | req_timeout = atoi(opt_arg()); | |
231 | #endif | |
232 | break; | |
233 | case OPT_URL: | |
234 | OPENSSL_free(thost); | |
235 | OPENSSL_free(tport); | |
236 | OPENSSL_free(tpath); | |
237 | thost = tport = tpath = NULL; | |
238 | if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) { | |
239 | BIO_printf(bio_err, "%s Error parsing URL\n", prog); | |
240 | goto end; | |
241 | } | |
242 | thost = host; | |
243 | tport = port; | |
244 | tpath = path; | |
245 | break; | |
246 | case OPT_HOST: | |
247 | host = opt_arg(); | |
248 | break; | |
249 | case OPT_PORT: | |
250 | port = opt_arg(); | |
251 | break; | |
252 | case OPT_IGNORE_ERR: | |
253 | ignore_err = 1; | |
254 | break; | |
255 | case OPT_NOVERIFY: | |
256 | noverify = 1; | |
257 | break; | |
258 | case OPT_NONCE: | |
259 | add_nonce = 2; | |
260 | break; | |
261 | case OPT_NO_NONCE: | |
262 | add_nonce = 0; | |
263 | break; | |
264 | case OPT_RESP_NO_CERTS: | |
265 | rflags |= OCSP_NOCERTS; | |
266 | break; | |
267 | case OPT_RESP_KEY_ID: | |
268 | rflags |= OCSP_RESPID_KEY; | |
269 | break; | |
270 | case OPT_NO_CERTS: | |
271 | sign_flags |= OCSP_NOCERTS; | |
272 | break; | |
273 | case OPT_NO_SIGNATURE_VERIFY: | |
274 | verify_flags |= OCSP_NOSIGS; | |
275 | break; | |
276 | case OPT_NO_CERT_VERIFY: | |
277 | verify_flags |= OCSP_NOVERIFY; | |
278 | break; | |
279 | case OPT_NO_CHAIN: | |
280 | verify_flags |= OCSP_NOCHAIN; | |
281 | break; | |
282 | case OPT_NO_CERT_CHECKS: | |
283 | verify_flags |= OCSP_NOCHECKS; | |
284 | break; | |
285 | case OPT_NO_EXPLICIT: | |
286 | verify_flags |= OCSP_NOEXPLICIT; | |
287 | break; | |
288 | case OPT_TRUST_OTHER: | |
289 | verify_flags |= OCSP_TRUSTOTHER; | |
290 | break; | |
291 | case OPT_NO_INTERN: | |
292 | verify_flags |= OCSP_NOINTERN; | |
293 | break; | |
294 | case OPT_BADSIG: | |
295 | badsig = 1; | |
296 | break; | |
297 | case OPT_TEXT: | |
298 | req_text = resp_text = 1; | |
299 | break; | |
300 | case OPT_REQ_TEXT: | |
301 | req_text = 1; | |
302 | break; | |
303 | case OPT_RESP_TEXT: | |
304 | resp_text = 1; | |
305 | break; | |
306 | case OPT_REQIN: | |
307 | reqin = opt_arg(); | |
308 | break; | |
309 | case OPT_RESPIN: | |
310 | respin = opt_arg(); | |
311 | break; | |
312 | case OPT_SIGNER: | |
313 | signfile = opt_arg(); | |
314 | break; | |
315 | case OPT_VAFILE: | |
316 | verify_certfile = opt_arg(); | |
317 | verify_flags |= OCSP_TRUSTOTHER; | |
318 | break; | |
319 | case OPT_SIGN_OTHER: | |
320 | sign_certfile = opt_arg(); | |
321 | break; | |
322 | case OPT_VERIFY_OTHER: | |
323 | verify_certfile = opt_arg(); | |
324 | break; | |
325 | case OPT_CAFILE: | |
326 | CAfile = opt_arg(); | |
327 | break; | |
328 | case OPT_CAPATH: | |
329 | CApath = opt_arg(); | |
330 | break; | |
331 | case OPT_NOCAFILE: | |
332 | noCAfile = 1; | |
333 | break; | |
334 | case OPT_NOCAPATH: | |
335 | noCApath = 1; | |
336 | break; | |
337 | case OPT_V_CASES: | |
338 | if (!opt_verify(o, vpm)) | |
339 | goto end; | |
340 | vpmtouched++; | |
341 | break; | |
342 | case OPT_VALIDITY_PERIOD: | |
343 | opt_long(opt_arg(), &nsec); | |
344 | break; | |
345 | case OPT_STATUS_AGE: | |
346 | opt_long(opt_arg(), &maxage); | |
347 | break; | |
348 | case OPT_SIGNKEY: | |
349 | keyfile = opt_arg(); | |
350 | break; | |
351 | case OPT_REQOUT: | |
352 | reqout = opt_arg(); | |
353 | break; | |
354 | case OPT_RESPOUT: | |
355 | respout = opt_arg(); | |
356 | break; | |
357 | case OPT_PATH: | |
358 | path = opt_arg(); | |
359 | break; | |
360 | case OPT_ISSUER: | |
361 | issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate"); | |
362 | if (issuer == NULL) | |
363 | goto end; | |
364 | if (issuers == NULL) { | |
365 | if ((issuers = sk_X509_new_null()) == NULL) | |
366 | goto end; | |
367 | } | |
368 | sk_X509_push(issuers, issuer); | |
369 | break; | |
370 | case OPT_CERT: | |
371 | X509_free(cert); | |
372 | cert = load_cert(opt_arg(), FORMAT_PEM, "certificate"); | |
373 | if (cert == NULL) | |
374 | goto end; | |
375 | if (cert_id_md == NULL) | |
376 | cert_id_md = EVP_sha1(); | |
377 | if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids)) | |
378 | goto end; | |
379 | if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) | |
380 | goto end; | |
381 | trailing_md = 0; | |
382 | break; | |
383 | case OPT_SERIAL: | |
384 | if (cert_id_md == NULL) | |
385 | cert_id_md = EVP_sha1(); | |
386 | if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids)) | |
387 | goto end; | |
388 | if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) | |
389 | goto end; | |
390 | trailing_md = 0; | |
391 | break; | |
392 | case OPT_INDEX: | |
393 | ridx_filename = opt_arg(); | |
394 | break; | |
395 | case OPT_CA: | |
396 | rca_filename = opt_arg(); | |
397 | break; | |
398 | case OPT_NMIN: | |
399 | opt_int(opt_arg(), &nmin); | |
400 | if (ndays == -1) | |
401 | ndays = 0; | |
402 | break; | |
403 | case OPT_REQUEST: | |
404 | opt_int(opt_arg(), &accept_count); | |
405 | break; | |
406 | case OPT_NDAYS: | |
407 | ndays = atoi(opt_arg()); | |
408 | break; | |
409 | case OPT_RSIGNER: | |
410 | rsignfile = opt_arg(); | |
411 | break; | |
412 | case OPT_RKEY: | |
413 | rkeyfile = opt_arg(); | |
414 | break; | |
415 | case OPT_ROTHER: | |
416 | rcertfile = opt_arg(); | |
417 | break; | |
418 | case OPT_RMD: /* Response MessageDigest */ | |
419 | if (!opt_md(opt_arg(), &rsign_md)) | |
420 | goto end; | |
421 | break; | |
422 | case OPT_HEADER: | |
423 | header = opt_arg(); | |
424 | value = strchr(header, '='); | |
425 | if (value == NULL) { | |
426 | BIO_printf(bio_err, "Missing = in header key=value\n"); | |
427 | goto opthelp; | |
428 | } | |
429 | *value++ = '\0'; | |
430 | if (!X509V3_add_value(header, value, &headers)) | |
431 | goto end; | |
432 | break; | |
433 | case OPT_MD: | |
434 | if (trailing_md) { | |
435 | BIO_printf(bio_err, | |
436 | "%s: Digest must be before -cert or -serial\n", | |
437 | prog); | |
438 | goto opthelp; | |
439 | } | |
440 | if (!opt_md(opt_unknown(), &cert_id_md)) | |
441 | goto opthelp; | |
442 | trailing_md = 1; | |
443 | break; | |
444 | } | |
445 | } | |
446 | ||
447 | if (trailing_md) { | |
448 | BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n", | |
449 | prog); | |
450 | goto opthelp; | |
451 | } | |
452 | argc = opt_num_rest(); | |
453 | if (argc != 0) | |
454 | goto opthelp; | |
455 | ||
456 | /* Have we anything to do? */ | |
457 | if (req == NULL&& reqin == NULL | |
458 | && respin == NULL && !(port != NULL && ridx_filename != NULL)) | |
459 | goto opthelp; | |
460 | ||
461 | out = bio_open_default(outfile, 'w', FORMAT_TEXT); | |
462 | if (out == NULL) | |
463 | goto end; | |
464 | ||
465 | if (req == NULL && (add_nonce != 2)) | |
466 | add_nonce = 0; | |
467 | ||
468 | if (req == NULL && reqin != NULL) { | |
469 | derbio = bio_open_default(reqin, 'r', FORMAT_ASN1); | |
470 | if (derbio == NULL) | |
471 | goto end; | |
472 | req = d2i_OCSP_REQUEST_bio(derbio, NULL); | |
473 | BIO_free(derbio); | |
474 | if (req == NULL) { | |
475 | BIO_printf(bio_err, "Error reading OCSP request\n"); | |
476 | goto end; | |
477 | } | |
478 | } | |
479 | ||
480 | if (req == NULL && port != NULL) { | |
481 | acbio = init_responder(port); | |
482 | if (acbio == NULL) | |
483 | goto end; | |
484 | } | |
485 | ||
486 | if (rsignfile != NULL) { | |
487 | if (rkeyfile == NULL) | |
488 | rkeyfile = rsignfile; | |
489 | rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate"); | |
490 | if (rsigner == NULL) { | |
491 | BIO_printf(bio_err, "Error loading responder certificate\n"); | |
492 | goto end; | |
493 | } | |
494 | if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM, | |
495 | NULL, "CA certificate")) | |
496 | goto end; | |
497 | if (rcertfile != NULL) { | |
498 | if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL, | |
499 | "responder other certificates")) | |
500 | goto end; | |
501 | } | |
502 | rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL, | |
503 | "responder private key"); | |
504 | if (rkey == NULL) | |
505 | goto end; | |
506 | } | |
507 | if (acbio != NULL) | |
508 | BIO_printf(bio_err, "Waiting for OCSP client connections...\n"); | |
509 | ||
510 | redo_accept: | |
511 | ||
512 | if (acbio != NULL) { | |
513 | if (!do_responder(&req, &cbio, acbio)) | |
514 | goto end; | |
515 | if (req == NULL) { | |
516 | resp = | |
517 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, | |
518 | NULL); | |
519 | send_ocsp_response(cbio, resp); | |
520 | goto done_resp; | |
521 | } | |
522 | } | |
523 | ||
524 | if (req == NULL | |
525 | && (signfile != NULL || reqout != NULL | |
526 | || host != NULL || add_nonce || ridx_filename != NULL)) { | |
527 | BIO_printf(bio_err, "Need an OCSP request for this operation!\n"); | |
528 | goto end; | |
529 | } | |
530 | ||
531 | if (req != NULL && add_nonce) | |
532 | OCSP_request_add1_nonce(req, NULL, -1); | |
533 | ||
534 | if (signfile != NULL) { | |
535 | if (keyfile == NULL) | |
536 | keyfile = signfile; | |
537 | signer = load_cert(signfile, FORMAT_PEM, "signer certificate"); | |
538 | if (signer == NULL) { | |
539 | BIO_printf(bio_err, "Error loading signer certificate\n"); | |
540 | goto end; | |
541 | } | |
542 | if (sign_certfile != NULL) { | |
543 | if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL, | |
544 | "signer certificates")) | |
545 | goto end; | |
546 | } | |
547 | key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL, | |
548 | "signer private key"); | |
549 | if (key == NULL) | |
550 | goto end; | |
551 | ||
552 | if (!OCSP_request_sign | |
553 | (req, signer, key, NULL, sign_other, sign_flags)) { | |
554 | BIO_printf(bio_err, "Error signing OCSP request\n"); | |
555 | goto end; | |
556 | } | |
557 | } | |
558 | ||
559 | if (req_text && req != NULL) | |
560 | OCSP_REQUEST_print(out, req, 0); | |
561 | ||
562 | if (reqout != NULL) { | |
563 | derbio = bio_open_default(reqout, 'w', FORMAT_ASN1); | |
564 | if (derbio == NULL) | |
565 | goto end; | |
566 | i2d_OCSP_REQUEST_bio(derbio, req); | |
567 | BIO_free(derbio); | |
568 | } | |
569 | ||
570 | if (ridx_filename != NULL | |
571 | && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) { | |
572 | BIO_printf(bio_err, | |
573 | "Need a responder certificate, key and CA for this operation!\n"); | |
574 | goto end; | |
575 | } | |
576 | ||
577 | if (ridx_filename != NULL && rdb == NULL) { | |
578 | rdb = load_index(ridx_filename, NULL); | |
579 | if (rdb == NULL) | |
580 | goto end; | |
581 | if (!index_index(rdb)) | |
582 | goto end; | |
583 | } | |
584 | ||
585 | if (rdb != NULL) { | |
586 | make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, | |
587 | rsign_md, rother, rflags, nmin, ndays, badsig); | |
588 | if (cbio != NULL) | |
589 | send_ocsp_response(cbio, resp); | |
590 | } else if (host != NULL) { | |
591 | # ifndef OPENSSL_NO_SOCK | |
592 | resp = process_responder(req, host, path, | |
593 | port, use_ssl, headers, req_timeout); | |
594 | if (resp == NULL) | |
595 | goto end; | |
596 | # else | |
597 | BIO_printf(bio_err, | |
598 | "Error creating connect BIO - sockets not supported.\n"); | |
599 | goto end; | |
600 | # endif | |
601 | } else if (respin != NULL) { | |
602 | derbio = bio_open_default(respin, 'r', FORMAT_ASN1); | |
603 | if (derbio == NULL) | |
604 | goto end; | |
605 | resp = d2i_OCSP_RESPONSE_bio(derbio, NULL); | |
606 | BIO_free(derbio); | |
607 | if (resp == NULL) { | |
608 | BIO_printf(bio_err, "Error reading OCSP response\n"); | |
609 | goto end; | |
610 | } | |
611 | } else { | |
612 | ret = 0; | |
613 | goto end; | |
614 | } | |
615 | ||
616 | done_resp: | |
617 | ||
618 | if (respout != NULL) { | |
619 | derbio = bio_open_default(respout, 'w', FORMAT_ASN1); | |
620 | if (derbio == NULL) | |
621 | goto end; | |
622 | i2d_OCSP_RESPONSE_bio(derbio, resp); | |
623 | BIO_free(derbio); | |
624 | } | |
625 | ||
626 | i = OCSP_response_status(resp); | |
627 | if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) { | |
628 | BIO_printf(out, "Responder Error: %s (%d)\n", | |
629 | OCSP_response_status_str(i), i); | |
630 | if (ignore_err) | |
631 | goto redo_accept; | |
632 | ret = 0; | |
633 | goto end; | |
634 | } | |
635 | ||
636 | if (resp_text) | |
637 | OCSP_RESPONSE_print(out, resp, 0); | |
638 | ||
639 | /* If running as responder don't verify our own response */ | |
640 | if (cbio != NULL) { | |
641 | /* If not unlimited, see if we took all we should. */ | |
642 | if (accept_count != -1 && --accept_count <= 0) { | |
643 | ret = 0; | |
644 | goto end; | |
645 | } | |
646 | BIO_free_all(cbio); | |
647 | cbio = NULL; | |
648 | OCSP_REQUEST_free(req); | |
649 | req = NULL; | |
650 | OCSP_RESPONSE_free(resp); | |
651 | resp = NULL; | |
652 | goto redo_accept; | |
653 | } | |
654 | if (ridx_filename != NULL) { | |
655 | ret = 0; | |
656 | goto end; | |
657 | } | |
658 | ||
659 | if (store == NULL) { | |
660 | store = setup_verify(CAfile, CApath, noCAfile, noCApath); | |
661 | if (!store) | |
662 | goto end; | |
663 | } | |
664 | if (vpmtouched) | |
665 | X509_STORE_set1_param(store, vpm); | |
666 | if (verify_certfile != NULL) { | |
667 | if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL, | |
668 | "validator certificate")) | |
669 | goto end; | |
670 | } | |
671 | ||
672 | bs = OCSP_response_get1_basic(resp); | |
673 | if (bs == NULL) { | |
674 | BIO_printf(bio_err, "Error parsing response\n"); | |
675 | goto end; | |
676 | } | |
677 | ||
678 | ret = 0; | |
679 | ||
680 | if (!noverify) { | |
681 | if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) { | |
682 | if (i == -1) | |
683 | BIO_printf(bio_err, "WARNING: no nonce in response\n"); | |
684 | else { | |
685 | BIO_printf(bio_err, "Nonce Verify error\n"); | |
686 | ret = 1; | |
687 | goto end; | |
688 | } | |
689 | } | |
690 | ||
691 | i = OCSP_basic_verify(bs, verify_other, store, verify_flags); | |
692 | if (i <= 0 && issuers) { | |
693 | i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER); | |
694 | if (i > 0) | |
695 | ERR_clear_error(); | |
696 | } | |
697 | if (i <= 0) { | |
698 | BIO_printf(bio_err, "Response Verify Failure\n"); | |
699 | ERR_print_errors(bio_err); | |
700 | ret = 1; | |
701 | } else { | |
702 | BIO_printf(bio_err, "Response verify OK\n"); | |
703 | } | |
704 | } | |
705 | ||
706 | print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage); | |
707 | ||
708 | end: | |
709 | ERR_print_errors(bio_err); | |
710 | X509_free(signer); | |
711 | X509_STORE_free(store); | |
712 | X509_VERIFY_PARAM_free(vpm); | |
713 | EVP_PKEY_free(key); | |
714 | EVP_PKEY_free(rkey); | |
715 | X509_free(cert); | |
716 | sk_X509_pop_free(issuers, X509_free); | |
717 | X509_free(rsigner); | |
718 | sk_X509_pop_free(rca_cert, X509_free); | |
719 | free_index(rdb); | |
720 | BIO_free_all(cbio); | |
721 | BIO_free_all(acbio); | |
722 | BIO_free(out); | |
723 | OCSP_REQUEST_free(req); | |
724 | OCSP_RESPONSE_free(resp); | |
725 | OCSP_BASICRESP_free(bs); | |
726 | sk_OPENSSL_STRING_free(reqnames); | |
727 | sk_OCSP_CERTID_free(ids); | |
728 | sk_X509_pop_free(sign_other, X509_free); | |
729 | sk_X509_pop_free(verify_other, X509_free); | |
730 | sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); | |
731 | OPENSSL_free(thost); | |
732 | OPENSSL_free(tport); | |
733 | OPENSSL_free(tpath); | |
734 | ||
735 | return ret; | |
736 | } | |
737 | ||
738 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, | |
739 | const EVP_MD *cert_id_md, X509 *issuer, | |
740 | STACK_OF(OCSP_CERTID) *ids) | |
741 | { | |
742 | OCSP_CERTID *id; | |
743 | ||
744 | if (issuer == NULL) { | |
745 | BIO_printf(bio_err, "No issuer certificate specified\n"); | |
746 | return 0; | |
747 | } | |
748 | if (*req == NULL) | |
749 | *req = OCSP_REQUEST_new(); | |
750 | if (*req == NULL) | |
751 | goto err; | |
752 | id = OCSP_cert_to_id(cert_id_md, cert, issuer); | |
753 | if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) | |
754 | goto err; | |
755 | if (!OCSP_request_add0_id(*req, id)) | |
756 | goto err; | |
757 | return 1; | |
758 | ||
759 | err: | |
760 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
761 | return 0; | |
762 | } | |
763 | ||
764 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
765 | const EVP_MD *cert_id_md, X509 *issuer, | |
766 | STACK_OF(OCSP_CERTID) *ids) | |
767 | { | |
768 | OCSP_CERTID *id; | |
769 | X509_NAME *iname; | |
770 | ASN1_BIT_STRING *ikey; | |
771 | ASN1_INTEGER *sno; | |
772 | ||
773 | if (issuer == NULL) { | |
774 | BIO_printf(bio_err, "No issuer certificate specified\n"); | |
775 | return 0; | |
776 | } | |
777 | if (*req == NULL) | |
778 | *req = OCSP_REQUEST_new(); | |
779 | if (*req == NULL) | |
780 | goto err; | |
781 | iname = X509_get_subject_name(issuer); | |
782 | ikey = X509_get0_pubkey_bitstr(issuer); | |
783 | sno = s2i_ASN1_INTEGER(NULL, serial); | |
784 | if (sno == NULL) { | |
785 | BIO_printf(bio_err, "Error converting serial number %s\n", serial); | |
786 | return 0; | |
787 | } | |
788 | id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno); | |
789 | ASN1_INTEGER_free(sno); | |
790 | if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) | |
791 | goto err; | |
792 | if (!OCSP_request_add0_id(*req, id)) | |
793 | goto err; | |
794 | return 1; | |
795 | ||
796 | err: | |
797 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
798 | return 0; | |
799 | } | |
800 | ||
801 | static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, | |
802 | STACK_OF(OPENSSL_STRING) *names, | |
803 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
804 | long maxage) | |
805 | { | |
806 | OCSP_CERTID *id; | |
807 | const char *name; | |
808 | int i, status, reason; | |
809 | ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd; | |
810 | ||
811 | if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names) | |
812 | || !sk_OCSP_CERTID_num(ids)) | |
813 | return; | |
814 | ||
815 | for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) { | |
816 | id = sk_OCSP_CERTID_value(ids, i); | |
817 | name = sk_OPENSSL_STRING_value(names, i); | |
818 | BIO_printf(out, "%s: ", name); | |
819 | ||
820 | if (!OCSP_resp_find_status(bs, id, &status, &reason, | |
821 | &rev, &thisupd, &nextupd)) { | |
822 | BIO_puts(out, "ERROR: No Status found.\n"); | |
823 | continue; | |
824 | } | |
825 | ||
826 | /* | |
827 | * Check validity: if invalid write to output BIO so we know which | |
828 | * response this refers to. | |
829 | */ | |
830 | if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) { | |
831 | BIO_puts(out, "WARNING: Status times invalid.\n"); | |
832 | ERR_print_errors(out); | |
833 | } | |
834 | BIO_printf(out, "%s\n", OCSP_cert_status_str(status)); | |
835 | ||
836 | BIO_puts(out, "\tThis Update: "); | |
837 | ASN1_GENERALIZEDTIME_print(out, thisupd); | |
838 | BIO_puts(out, "\n"); | |
839 | ||
840 | if (nextupd) { | |
841 | BIO_puts(out, "\tNext Update: "); | |
842 | ASN1_GENERALIZEDTIME_print(out, nextupd); | |
843 | BIO_puts(out, "\n"); | |
844 | } | |
845 | ||
846 | if (status != V_OCSP_CERTSTATUS_REVOKED) | |
847 | continue; | |
848 | ||
849 | if (reason != -1) | |
850 | BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason)); | |
851 | ||
852 | BIO_puts(out, "\tRevocation Time: "); | |
853 | ASN1_GENERALIZEDTIME_print(out, rev); | |
854 | BIO_puts(out, "\n"); | |
855 | } | |
856 | } | |
857 | ||
858 | static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, | |
859 | CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, | |
860 | EVP_PKEY *rkey, const EVP_MD *rmd, | |
861 | STACK_OF(X509) *rother, unsigned long flags, | |
862 | int nmin, int ndays, int badsig) | |
863 | { | |
864 | ASN1_TIME *thisupd = NULL, *nextupd = NULL; | |
865 | OCSP_CERTID *cid; | |
866 | OCSP_BASICRESP *bs = NULL; | |
867 | int i, id_count; | |
868 | ||
869 | id_count = OCSP_request_onereq_count(req); | |
870 | ||
871 | if (id_count <= 0) { | |
872 | *resp = | |
873 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL); | |
874 | goto end; | |
875 | } | |
876 | ||
877 | bs = OCSP_BASICRESP_new(); | |
878 | thisupd = X509_gmtime_adj(NULL, 0); | |
879 | if (ndays != -1) | |
880 | nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL); | |
881 | ||
882 | /* Examine each certificate id in the request */ | |
883 | for (i = 0; i < id_count; i++) { | |
884 | OCSP_ONEREQ *one; | |
885 | ASN1_INTEGER *serial; | |
886 | char **inf; | |
887 | int jj; | |
888 | int found = 0; | |
889 | ASN1_OBJECT *cert_id_md_oid; | |
890 | const EVP_MD *cert_id_md; | |
891 | one = OCSP_request_onereq_get0(req, i); | |
892 | cid = OCSP_onereq_get0_id(one); | |
893 | ||
894 | OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid); | |
895 | ||
896 | cert_id_md = EVP_get_digestbyobj(cert_id_md_oid); | |
897 | if (cert_id_md == NULL) { | |
898 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, | |
899 | NULL); | |
900 | goto end; | |
901 | } | |
902 | for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) { | |
903 | X509 *ca_cert = sk_X509_value(ca, jj); | |
904 | OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert); | |
905 | ||
906 | if (OCSP_id_issuer_cmp(ca_id, cid) == 0) | |
907 | found = 1; | |
908 | ||
909 | OCSP_CERTID_free(ca_id); | |
910 | } | |
911 | ||
912 | if (!found) { | |
913 | OCSP_basic_add1_status(bs, cid, | |
914 | V_OCSP_CERTSTATUS_UNKNOWN, | |
915 | 0, NULL, thisupd, nextupd); | |
916 | continue; | |
917 | } | |
918 | OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid); | |
919 | inf = lookup_serial(db, serial); | |
920 | if (inf == NULL) { | |
921 | OCSP_basic_add1_status(bs, cid, | |
922 | V_OCSP_CERTSTATUS_UNKNOWN, | |
923 | 0, NULL, thisupd, nextupd); | |
924 | } else if (inf[DB_type][0] == DB_TYPE_VAL) { | |
925 | OCSP_basic_add1_status(bs, cid, | |
926 | V_OCSP_CERTSTATUS_GOOD, | |
927 | 0, NULL, thisupd, nextupd); | |
928 | } else if (inf[DB_type][0] == DB_TYPE_REV) { | |
929 | ASN1_OBJECT *inst = NULL; | |
930 | ASN1_TIME *revtm = NULL; | |
931 | ASN1_GENERALIZEDTIME *invtm = NULL; | |
932 | OCSP_SINGLERESP *single; | |
933 | int reason = -1; | |
934 | unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]); | |
935 | single = OCSP_basic_add1_status(bs, cid, | |
936 | V_OCSP_CERTSTATUS_REVOKED, | |
937 | reason, revtm, thisupd, nextupd); | |
938 | if (invtm != NULL) | |
939 | OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, | |
940 | invtm, 0, 0); | |
941 | else if (inst != NULL) | |
942 | OCSP_SINGLERESP_add1_ext_i2d(single, | |
943 | NID_hold_instruction_code, inst, | |
944 | 0, 0); | |
945 | ASN1_OBJECT_free(inst); | |
946 | ASN1_TIME_free(revtm); | |
947 | ASN1_GENERALIZEDTIME_free(invtm); | |
948 | } | |
949 | } | |
950 | ||
951 | OCSP_copy_nonce(bs, req); | |
952 | ||
953 | OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags); | |
954 | ||
955 | if (badsig) { | |
956 | const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs); | |
957 | corrupt_signature(sig); | |
958 | } | |
959 | ||
960 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs); | |
961 | ||
962 | end: | |
963 | ASN1_TIME_free(thisupd); | |
964 | ASN1_TIME_free(nextupd); | |
965 | OCSP_BASICRESP_free(bs); | |
966 | } | |
967 | ||
968 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser) | |
969 | { | |
970 | int i; | |
971 | BIGNUM *bn = NULL; | |
972 | char *itmp, *row[DB_NUMBER], **rrow; | |
973 | for (i = 0; i < DB_NUMBER; i++) | |
974 | row[i] = NULL; | |
975 | bn = ASN1_INTEGER_to_BN(ser, NULL); | |
976 | OPENSSL_assert(bn); /* FIXME: should report an error at this | |
977 | * point and abort */ | |
978 | if (BN_is_zero(bn)) | |
979 | itmp = OPENSSL_strdup("00"); | |
980 | else | |
981 | itmp = BN_bn2hex(bn); | |
982 | row[DB_serial] = itmp; | |
983 | BN_free(bn); | |
984 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
985 | OPENSSL_free(itmp); | |
986 | return rrow; | |
987 | } | |
988 | ||
989 | /* Quick and dirty OCSP server: read in and parse input request */ | |
990 | ||
991 | static BIO *init_responder(const char *port) | |
992 | { | |
993 | # ifdef OPENSSL_NO_SOCK | |
994 | BIO_printf(bio_err, | |
995 | "Error setting up accept BIO - sockets not supported.\n"); | |
996 | return NULL; | |
997 | # else | |
998 | BIO *acbio = NULL, *bufbio = NULL; | |
999 | ||
1000 | bufbio = BIO_new(BIO_f_buffer()); | |
1001 | if (bufbio == NULL) | |
1002 | goto err; | |
1003 | acbio = BIO_new(BIO_s_accept()); | |
1004 | if (acbio == NULL | |
1005 | || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0 | |
1006 | || BIO_set_accept_port(acbio, port) < 0) { | |
1007 | BIO_printf(bio_err, "Error setting up accept BIO\n"); | |
1008 | ERR_print_errors(bio_err); | |
1009 | goto err; | |
1010 | } | |
1011 | ||
1012 | BIO_set_accept_bios(acbio, bufbio); | |
1013 | bufbio = NULL; | |
1014 | if (BIO_do_accept(acbio) <= 0) { | |
1015 | BIO_printf(bio_err, "Error starting accept\n"); | |
1016 | ERR_print_errors(bio_err); | |
1017 | goto err; | |
1018 | } | |
1019 | ||
1020 | return acbio; | |
1021 | ||
1022 | err: | |
1023 | BIO_free_all(acbio); | |
1024 | BIO_free(bufbio); | |
1025 | return NULL; | |
1026 | # endif | |
1027 | } | |
1028 | ||
1029 | # ifndef OPENSSL_NO_SOCK | |
1030 | /* | |
1031 | * Decode %xx URL-decoding in-place. Ignores mal-formed sequences. | |
1032 | */ | |
1033 | static int urldecode(char *p) | |
1034 | { | |
1035 | unsigned char *out = (unsigned char *)p; | |
1036 | unsigned char *save = out; | |
1037 | ||
1038 | for (; *p; p++) { | |
1039 | if (*p != '%') | |
1040 | *out++ = *p; | |
1041 | else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) { | |
1042 | /* Don't check, can't fail because of ixdigit() call. */ | |
1043 | *out++ = (OPENSSL_hexchar2int(p[1]) << 4) | |
1044 | | OPENSSL_hexchar2int(p[2]); | |
1045 | p += 2; | |
1046 | } | |
1047 | else | |
1048 | return -1; | |
1049 | } | |
1050 | *out = '\0'; | |
1051 | return (int)(out - save); | |
1052 | } | |
1053 | # endif | |
1054 | ||
1055 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio) | |
1056 | { | |
1057 | # ifdef OPENSSL_NO_SOCK | |
1058 | return 0; | |
1059 | # else | |
1060 | int len; | |
1061 | OCSP_REQUEST *req = NULL; | |
1062 | char inbuf[2048], reqbuf[2048]; | |
1063 | char *p, *q; | |
1064 | BIO *cbio = NULL, *getbio = NULL, *b64 = NULL; | |
1065 | ||
1066 | if (BIO_do_accept(acbio) <= 0) { | |
1067 | BIO_printf(bio_err, "Error accepting connection\n"); | |
1068 | ERR_print_errors(bio_err); | |
1069 | return 0; | |
1070 | } | |
1071 | ||
1072 | cbio = BIO_pop(acbio); | |
1073 | *pcbio = cbio; | |
1074 | ||
1075 | /* Read the request line. */ | |
1076 | len = BIO_gets(cbio, reqbuf, sizeof(reqbuf)); | |
1077 | if (len <= 0) | |
1078 | return 1; | |
1079 | if (strncmp(reqbuf, "GET ", 4) == 0) { | |
1080 | /* Expecting GET {sp} /URL {sp} HTTP/1.x */ | |
1081 | for (p = reqbuf + 4; *p == ' '; ++p) | |
1082 | continue; | |
1083 | if (*p != '/') { | |
1084 | BIO_printf(bio_err, "Invalid request -- bad URL\n"); | |
1085 | return 1; | |
1086 | } | |
1087 | p++; | |
1088 | ||
1089 | /* Splice off the HTTP version identifier. */ | |
1090 | for (q = p; *q; q++) | |
1091 | if (*q == ' ') | |
1092 | break; | |
1093 | if (strncmp(q, " HTTP/1.", 8) != 0) { | |
1094 | BIO_printf(bio_err, "Invalid request -- bad HTTP version\n"); | |
1095 | return 1; | |
1096 | } | |
1097 | *q = '\0'; | |
1098 | len = urldecode(p); | |
1099 | if (len <= 0) { | |
1100 | BIO_printf(bio_err, "Invalid request -- bad URL encoding\n"); | |
1101 | return 1; | |
1102 | } | |
1103 | if ((getbio = BIO_new_mem_buf(p, len)) == NULL | |
1104 | || (b64 = BIO_new(BIO_f_base64())) == NULL) { | |
1105 | BIO_printf(bio_err, "Could not allocate memory\n"); | |
1106 | ERR_print_errors(bio_err); | |
1107 | return 1; | |
1108 | } | |
1109 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); | |
1110 | getbio = BIO_push(b64, getbio); | |
1111 | } else if (strncmp(reqbuf, "POST ", 5) != 0) { | |
1112 | BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n"); | |
1113 | return 1; | |
1114 | } | |
1115 | ||
1116 | /* Read and skip past the headers. */ | |
1117 | for (;;) { | |
1118 | len = BIO_gets(cbio, inbuf, sizeof(inbuf)); | |
1119 | if (len <= 0) | |
1120 | return 1; | |
1121 | if ((inbuf[0] == '\r') || (inbuf[0] == '\n')) | |
1122 | break; | |
1123 | } | |
1124 | ||
1125 | /* Try to read OCSP request */ | |
1126 | if (getbio != NULL) { | |
1127 | req = d2i_OCSP_REQUEST_bio(getbio, NULL); | |
1128 | BIO_free_all(getbio); | |
1129 | } else { | |
1130 | req = d2i_OCSP_REQUEST_bio(cbio, NULL); | |
1131 | } | |
1132 | ||
1133 | if (req == NULL) { | |
1134 | BIO_printf(bio_err, "Error parsing OCSP request\n"); | |
1135 | ERR_print_errors(bio_err); | |
1136 | } | |
1137 | ||
1138 | *preq = req; | |
1139 | ||
1140 | return 1; | |
1141 | # endif | |
1142 | } | |
1143 | ||
1144 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp) | |
1145 | { | |
1146 | char http_resp[] = | |
1147 | "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n" | |
1148 | "Content-Length: %d\r\n\r\n"; | |
1149 | if (cbio == NULL) | |
1150 | return 0; | |
1151 | BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL)); | |
1152 | i2d_OCSP_RESPONSE_bio(cbio, resp); | |
1153 | (void)BIO_flush(cbio); | |
1154 | return 1; | |
1155 | } | |
1156 | ||
1157 | # ifndef OPENSSL_NO_SOCK | |
1158 | static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, | |
1159 | const char *path, | |
1160 | const STACK_OF(CONF_VALUE) *headers, | |
1161 | OCSP_REQUEST *req, int req_timeout) | |
1162 | { | |
1163 | int fd; | |
1164 | int rv; | |
1165 | int i; | |
1166 | int add_host = 1; | |
1167 | OCSP_REQ_CTX *ctx = NULL; | |
1168 | OCSP_RESPONSE *rsp = NULL; | |
1169 | fd_set confds; | |
1170 | struct timeval tv; | |
1171 | ||
1172 | if (req_timeout != -1) | |
1173 | BIO_set_nbio(cbio, 1); | |
1174 | ||
1175 | rv = BIO_do_connect(cbio); | |
1176 | ||
1177 | if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) { | |
1178 | BIO_puts(bio_err, "Error connecting BIO\n"); | |
1179 | return NULL; | |
1180 | } | |
1181 | ||
1182 | if (BIO_get_fd(cbio, &fd) < 0) { | |
1183 | BIO_puts(bio_err, "Can't get connection fd\n"); | |
1184 | goto err; | |
1185 | } | |
1186 | ||
1187 | if (req_timeout != -1 && rv <= 0) { | |
1188 | FD_ZERO(&confds); | |
1189 | openssl_fdset(fd, &confds); | |
1190 | tv.tv_usec = 0; | |
1191 | tv.tv_sec = req_timeout; | |
1192 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); | |
1193 | if (rv == 0) { | |
1194 | BIO_puts(bio_err, "Timeout on connect\n"); | |
1195 | return NULL; | |
1196 | } | |
1197 | } | |
1198 | ||
1199 | ctx = OCSP_sendreq_new(cbio, path, NULL, -1); | |
1200 | if (ctx == NULL) | |
1201 | return NULL; | |
1202 | ||
1203 | for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { | |
1204 | CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i); | |
1205 | if (add_host == 1 && strcasecmp("host", hdr->name) == 0) | |
1206 | add_host = 0; | |
1207 | if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value)) | |
1208 | goto err; | |
1209 | } | |
1210 | ||
1211 | if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0) | |
1212 | goto err; | |
1213 | ||
1214 | if (!OCSP_REQ_CTX_set1_req(ctx, req)) | |
1215 | goto err; | |
1216 | ||
1217 | for (;;) { | |
1218 | rv = OCSP_sendreq_nbio(&rsp, ctx); | |
1219 | if (rv != -1) | |
1220 | break; | |
1221 | if (req_timeout == -1) | |
1222 | continue; | |
1223 | FD_ZERO(&confds); | |
1224 | openssl_fdset(fd, &confds); | |
1225 | tv.tv_usec = 0; | |
1226 | tv.tv_sec = req_timeout; | |
1227 | if (BIO_should_read(cbio)) { | |
1228 | rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv); | |
1229 | } else if (BIO_should_write(cbio)) { | |
1230 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); | |
1231 | } else { | |
1232 | BIO_puts(bio_err, "Unexpected retry condition\n"); | |
1233 | goto err; | |
1234 | } | |
1235 | if (rv == 0) { | |
1236 | BIO_puts(bio_err, "Timeout on request\n"); | |
1237 | break; | |
1238 | } | |
1239 | if (rv == -1) { | |
1240 | BIO_puts(bio_err, "Select error\n"); | |
1241 | break; | |
1242 | } | |
1243 | ||
1244 | } | |
1245 | err: | |
1246 | OCSP_REQ_CTX_free(ctx); | |
1247 | ||
1248 | return rsp; | |
1249 | } | |
1250 | ||
1251 | OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, | |
1252 | const char *host, const char *path, | |
1253 | const char *port, int use_ssl, | |
1254 | STACK_OF(CONF_VALUE) *headers, | |
1255 | int req_timeout) | |
1256 | { | |
1257 | BIO *cbio = NULL; | |
1258 | SSL_CTX *ctx = NULL; | |
1259 | OCSP_RESPONSE *resp = NULL; | |
1260 | ||
1261 | cbio = BIO_new_connect(host); | |
1262 | if (cbio == NULL) { | |
1263 | BIO_printf(bio_err, "Error creating connect BIO\n"); | |
1264 | goto end; | |
1265 | } | |
1266 | if (port != NULL) | |
1267 | BIO_set_conn_port(cbio, port); | |
1268 | if (use_ssl == 1) { | |
1269 | BIO *sbio; | |
1270 | ctx = SSL_CTX_new(TLS_client_method()); | |
1271 | if (ctx == NULL) { | |
1272 | BIO_printf(bio_err, "Error creating SSL context.\n"); | |
1273 | goto end; | |
1274 | } | |
1275 | SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); | |
1276 | sbio = BIO_new_ssl(ctx, 1); | |
1277 | cbio = BIO_push(sbio, cbio); | |
1278 | } | |
1279 | ||
1280 | resp = query_responder(cbio, host, path, headers, req, req_timeout); | |
1281 | if (resp == NULL) | |
1282 | BIO_printf(bio_err, "Error querying OCSP responder\n"); | |
1283 | end: | |
1284 | BIO_free_all(cbio); | |
1285 | SSL_CTX_free(ctx); | |
1286 | return resp; | |
1287 | } | |
1288 | # endif | |
1289 | ||
1290 | #endif |