]>
Commit | Line | Data |
---|---|---|
5782ceb2 | 1 | /* ocsp.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 2000. | |
5782ceb2 DSH |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
5782ceb2 DSH |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
85d686e7 | 59 | #ifndef OPENSSL_NO_OCSP |
5782ceb2 | 60 | |
0f113f3e MC |
61 | # ifdef OPENSSL_SYS_VMS |
62 | # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined | |
63 | * on OpenVMS */ | |
64 | # endif | |
65 | ||
66 | # define USE_SOCKETS | |
67 | ||
68 | # include <stdio.h> | |
69 | # include <stdlib.h> | |
70 | # include <string.h> | |
71 | # include <time.h> | |
72 | # include "apps.h" /* needs to be included before the openssl | |
73 | * headers! */ | |
74 | # include <openssl/e_os2.h> | |
75 | # include <openssl/crypto.h> | |
76 | # include <openssl/err.h> | |
77 | # include <openssl/ssl.h> | |
78 | # include <openssl/evp.h> | |
79 | # include <openssl/bn.h> | |
80 | # include <openssl/x509v3.h> | |
81 | ||
82 | # if defined(NETWARE_CLIB) | |
eef0c1f3 | 83 | # ifdef NETWARE_BSDSOCK |
0f113f3e MC |
84 | # include <sys/socket.h> |
85 | # include <sys/bsdskt.h> | |
eef0c1f3 | 86 | # else |
0f113f3e | 87 | # include <novsock2.h> |
eef0c1f3 | 88 | # endif |
0f113f3e | 89 | # elif defined(NETWARE_LIBC) |
eef0c1f3 | 90 | # ifdef NETWARE_BSDSOCK |
0f113f3e | 91 | # include <sys/select.h> |
eef0c1f3 | 92 | # else |
0f113f3e | 93 | # include <novsock2.h> |
eef0c1f3 | 94 | # endif |
0f113f3e | 95 | # endif |
f1965221 | 96 | |
0f113f3e MC |
97 | /* Maximum leeway in validity period: default 5 minutes */ |
98 | # define MAX_VALIDITY_PERIOD (5 * 60) | |
99 | ||
100 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, | |
101 | const EVP_MD *cert_id_md, X509 *issuer, | |
102 | STACK_OF(OCSP_CERTID) *ids); | |
103 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
104 | const EVP_MD *cert_id_md, X509 *issuer, | |
105 | STACK_OF(OCSP_CERTID) *ids); | |
73758d43 | 106 | static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, |
0f113f3e MC |
107 | STACK_OF(OPENSSL_STRING) *names, |
108 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
109 | long maxage); | |
5782ceb2 | 110 | |
0f113f3e MC |
111 | static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, |
112 | CA_DB *db, X509 *ca, X509 *rcert, | |
113 | EVP_PKEY *rkey, const EVP_MD *md, | |
114 | STACK_OF(X509) *rother, unsigned long flags, | |
115 | int nmin, int ndays, int badsig); | |
ee306a13 | 116 | |
f85b68cd | 117 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser); |
c45a48c1 | 118 | static BIO *init_responder(const char *port); |
0f113f3e MC |
119 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, |
120 | const char *port); | |
ee306a13 | 121 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp); |
c45a48c1 | 122 | static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, const char *path, |
0f113f3e MC |
123 | const STACK_OF(CONF_VALUE) *headers, |
124 | OCSP_REQUEST *req, int req_timeout); | |
ee306a13 | 125 | |
0f113f3e MC |
126 | # undef PROG |
127 | # define PROG ocsp_main | |
5782ceb2 DSH |
128 | |
129 | int MAIN(int, char **); | |
130 | ||
131 | int MAIN(int argc, char **argv) | |
0f113f3e MC |
132 | { |
133 | ENGINE *e = NULL; | |
134 | char **args; | |
135 | char *host = NULL, *port = NULL, *path = "/"; | |
136 | char *thost = NULL, *tport = NULL, *tpath = NULL; | |
137 | char *reqin = NULL, *respin = NULL; | |
138 | char *reqout = NULL, *respout = NULL; | |
139 | char *signfile = NULL, *keyfile = NULL; | |
140 | char *rsignfile = NULL, *rkeyfile = NULL; | |
141 | char *outfile = NULL; | |
142 | int add_nonce = 1, noverify = 0, use_ssl = -1; | |
143 | STACK_OF(CONF_VALUE) *headers = NULL; | |
144 | OCSP_REQUEST *req = NULL; | |
145 | OCSP_RESPONSE *resp = NULL; | |
146 | OCSP_BASICRESP *bs = NULL; | |
147 | X509 *issuer = NULL, *cert = NULL; | |
148 | X509 *signer = NULL, *rsigner = NULL; | |
149 | EVP_PKEY *key = NULL, *rkey = NULL; | |
150 | BIO *acbio = NULL, *cbio = NULL; | |
151 | BIO *derbio = NULL; | |
152 | BIO *out = NULL; | |
153 | int req_timeout = -1; | |
154 | int req_text = 0, resp_text = 0; | |
155 | long nsec = MAX_VALIDITY_PERIOD, maxage = -1; | |
156 | char *CAfile = NULL, *CApath = NULL; | |
157 | X509_STORE *store = NULL; | |
158 | X509_VERIFY_PARAM *vpm = NULL; | |
159 | STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL; | |
160 | char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL; | |
161 | unsigned long sign_flags = 0, verify_flags = 0, rflags = 0; | |
162 | int ret = 1; | |
163 | int accept_count = -1; | |
164 | int badarg = 0; | |
165 | int badsig = 0; | |
166 | int i; | |
167 | int ignore_err = 0; | |
168 | STACK_OF(OPENSSL_STRING) *reqnames = NULL; | |
169 | STACK_OF(OCSP_CERTID) *ids = NULL; | |
170 | ||
171 | X509 *rca_cert = NULL; | |
172 | char *ridx_filename = NULL; | |
173 | char *rca_filename = NULL; | |
174 | CA_DB *rdb = NULL; | |
175 | int nmin = 0, ndays = -1; | |
176 | const EVP_MD *cert_id_md = NULL, *rsign_md = NULL; | |
177 | ||
178 | if (bio_err == NULL) | |
179 | bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); | |
180 | ||
181 | if (!load_config(bio_err, NULL)) | |
182 | goto end; | |
183 | SSL_load_error_strings(); | |
184 | OpenSSL_add_ssl_algorithms(); | |
185 | args = argv + 1; | |
186 | reqnames = sk_OPENSSL_STRING_new_null(); | |
187 | ids = sk_OCSP_CERTID_new_null(); | |
188 | while (!badarg && *args && *args[0] == '-') { | |
189 | if (!strcmp(*args, "-out")) { | |
190 | if (args[1]) { | |
191 | args++; | |
192 | outfile = *args; | |
193 | } else | |
194 | badarg = 1; | |
195 | } else if (!strcmp(*args, "-timeout")) { | |
196 | if (args[1]) { | |
197 | args++; | |
198 | req_timeout = atol(*args); | |
199 | if (req_timeout < 0) { | |
200 | BIO_printf(bio_err, "Illegal timeout value %s\n", *args); | |
201 | badarg = 1; | |
202 | } | |
203 | } else | |
204 | badarg = 1; | |
205 | } else if (!strcmp(*args, "-url")) { | |
206 | if (thost) | |
207 | OPENSSL_free(thost); | |
208 | if (tport) | |
209 | OPENSSL_free(tport); | |
210 | if (tpath) | |
211 | OPENSSL_free(tpath); | |
212 | if (args[1]) { | |
213 | args++; | |
214 | if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl)) { | |
215 | BIO_printf(bio_err, "Error parsing URL\n"); | |
216 | badarg = 1; | |
217 | } | |
218 | thost = host; | |
219 | tport = port; | |
220 | tpath = path; | |
221 | } else | |
222 | badarg = 1; | |
223 | } else if (!strcmp(*args, "-host")) { | |
224 | if (args[1]) { | |
225 | args++; | |
226 | host = *args; | |
227 | } else | |
228 | badarg = 1; | |
229 | } else if (!strcmp(*args, "-port")) { | |
230 | if (args[1]) { | |
231 | args++; | |
232 | port = *args; | |
233 | } else | |
234 | badarg = 1; | |
235 | } else if (!strcmp(*args, "-header")) { | |
236 | if (args[1] && args[2]) { | |
237 | if (!X509V3_add_value(args[1], args[2], &headers)) | |
238 | goto end; | |
239 | args += 2; | |
240 | } else | |
241 | badarg = 1; | |
242 | } else if (!strcmp(*args, "-ignore_err")) | |
243 | ignore_err = 1; | |
244 | else if (!strcmp(*args, "-noverify")) | |
245 | noverify = 1; | |
246 | else if (!strcmp(*args, "-nonce")) | |
247 | add_nonce = 2; | |
248 | else if (!strcmp(*args, "-no_nonce")) | |
249 | add_nonce = 0; | |
250 | else if (!strcmp(*args, "-resp_no_certs")) | |
251 | rflags |= OCSP_NOCERTS; | |
252 | else if (!strcmp(*args, "-resp_key_id")) | |
253 | rflags |= OCSP_RESPID_KEY; | |
254 | else if (!strcmp(*args, "-no_certs")) | |
255 | sign_flags |= OCSP_NOCERTS; | |
256 | else if (!strcmp(*args, "-no_signature_verify")) | |
257 | verify_flags |= OCSP_NOSIGS; | |
258 | else if (!strcmp(*args, "-no_cert_verify")) | |
259 | verify_flags |= OCSP_NOVERIFY; | |
260 | else if (!strcmp(*args, "-no_chain")) | |
261 | verify_flags |= OCSP_NOCHAIN; | |
262 | else if (!strcmp(*args, "-no_cert_checks")) | |
263 | verify_flags |= OCSP_NOCHECKS; | |
264 | else if (!strcmp(*args, "-no_explicit")) | |
265 | verify_flags |= OCSP_NOEXPLICIT; | |
266 | else if (!strcmp(*args, "-trust_other")) | |
267 | verify_flags |= OCSP_TRUSTOTHER; | |
268 | else if (!strcmp(*args, "-no_intern")) | |
269 | verify_flags |= OCSP_NOINTERN; | |
270 | else if (!strcmp(*args, "-badsig")) | |
271 | badsig = 1; | |
272 | else if (!strcmp(*args, "-text")) { | |
273 | req_text = 1; | |
274 | resp_text = 1; | |
275 | } else if (!strcmp(*args, "-req_text")) | |
276 | req_text = 1; | |
277 | else if (!strcmp(*args, "-resp_text")) | |
278 | resp_text = 1; | |
279 | else if (!strcmp(*args, "-reqin")) { | |
280 | if (args[1]) { | |
281 | args++; | |
282 | reqin = *args; | |
283 | } else | |
284 | badarg = 1; | |
285 | } else if (!strcmp(*args, "-respin")) { | |
286 | if (args[1]) { | |
287 | args++; | |
288 | respin = *args; | |
289 | } else | |
290 | badarg = 1; | |
291 | } else if (!strcmp(*args, "-signer")) { | |
292 | if (args[1]) { | |
293 | args++; | |
294 | signfile = *args; | |
295 | } else | |
296 | badarg = 1; | |
297 | } else if (!strcmp(*args, "-VAfile")) { | |
298 | if (args[1]) { | |
299 | args++; | |
300 | verify_certfile = *args; | |
301 | verify_flags |= OCSP_TRUSTOTHER; | |
302 | } else | |
303 | badarg = 1; | |
304 | } else if (!strcmp(*args, "-sign_other")) { | |
305 | if (args[1]) { | |
306 | args++; | |
307 | sign_certfile = *args; | |
308 | } else | |
309 | badarg = 1; | |
310 | } else if (!strcmp(*args, "-verify_other")) { | |
311 | if (args[1]) { | |
312 | args++; | |
313 | verify_certfile = *args; | |
314 | } else | |
315 | badarg = 1; | |
316 | } else if (!strcmp(*args, "-CAfile")) { | |
317 | if (args[1]) { | |
318 | args++; | |
319 | CAfile = *args; | |
320 | } else | |
321 | badarg = 1; | |
322 | } else if (!strcmp(*args, "-CApath")) { | |
323 | if (args[1]) { | |
324 | args++; | |
325 | CApath = *args; | |
326 | } else | |
327 | badarg = 1; | |
328 | } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm)) { | |
329 | if (badarg) | |
330 | goto end; | |
331 | continue; | |
332 | } else if (!strcmp(*args, "-validity_period")) { | |
333 | if (args[1]) { | |
334 | args++; | |
335 | nsec = atol(*args); | |
336 | if (nsec < 0) { | |
337 | BIO_printf(bio_err, | |
338 | "Illegal validity period %s\n", *args); | |
339 | badarg = 1; | |
340 | } | |
341 | } else | |
342 | badarg = 1; | |
343 | } else if (!strcmp(*args, "-status_age")) { | |
344 | if (args[1]) { | |
345 | args++; | |
346 | maxage = atol(*args); | |
347 | if (maxage < 0) { | |
348 | BIO_printf(bio_err, "Illegal validity age %s\n", *args); | |
349 | badarg = 1; | |
350 | } | |
351 | } else | |
352 | badarg = 1; | |
353 | } else if (!strcmp(*args, "-signkey")) { | |
354 | if (args[1]) { | |
355 | args++; | |
356 | keyfile = *args; | |
357 | } else | |
358 | badarg = 1; | |
359 | } else if (!strcmp(*args, "-reqout")) { | |
360 | if (args[1]) { | |
361 | args++; | |
362 | reqout = *args; | |
363 | } else | |
364 | badarg = 1; | |
365 | } else if (!strcmp(*args, "-respout")) { | |
366 | if (args[1]) { | |
367 | args++; | |
368 | respout = *args; | |
369 | } else | |
370 | badarg = 1; | |
371 | } else if (!strcmp(*args, "-path")) { | |
372 | if (args[1]) { | |
373 | args++; | |
374 | path = *args; | |
375 | } else | |
376 | badarg = 1; | |
377 | } else if (!strcmp(*args, "-issuer")) { | |
378 | if (args[1]) { | |
379 | args++; | |
380 | X509_free(issuer); | |
381 | issuer = load_cert(bio_err, *args, FORMAT_PEM, | |
382 | NULL, e, "issuer certificate"); | |
383 | if (!issuer) | |
384 | goto end; | |
385 | } else | |
386 | badarg = 1; | |
387 | } else if (!strcmp(*args, "-cert")) { | |
388 | if (args[1]) { | |
389 | args++; | |
390 | X509_free(cert); | |
391 | cert = load_cert(bio_err, *args, FORMAT_PEM, | |
392 | NULL, e, "certificate"); | |
393 | if (!cert) | |
394 | goto end; | |
395 | if (!cert_id_md) | |
396 | cert_id_md = EVP_sha1(); | |
397 | if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids)) | |
398 | goto end; | |
399 | if (!sk_OPENSSL_STRING_push(reqnames, *args)) | |
400 | goto end; | |
401 | } else | |
402 | badarg = 1; | |
403 | } else if (!strcmp(*args, "-serial")) { | |
404 | if (args[1]) { | |
405 | args++; | |
406 | if (!cert_id_md) | |
407 | cert_id_md = EVP_sha1(); | |
408 | if (!add_ocsp_serial(&req, *args, cert_id_md, issuer, ids)) | |
409 | goto end; | |
410 | if (!sk_OPENSSL_STRING_push(reqnames, *args)) | |
411 | goto end; | |
412 | } else | |
413 | badarg = 1; | |
414 | } else if (!strcmp(*args, "-index")) { | |
415 | if (args[1]) { | |
416 | args++; | |
417 | ridx_filename = *args; | |
418 | } else | |
419 | badarg = 1; | |
420 | } else if (!strcmp(*args, "-CA")) { | |
421 | if (args[1]) { | |
422 | args++; | |
423 | rca_filename = *args; | |
424 | } else | |
425 | badarg = 1; | |
426 | } else if (!strcmp(*args, "-nmin")) { | |
427 | if (args[1]) { | |
428 | args++; | |
429 | nmin = atol(*args); | |
430 | if (nmin < 0) { | |
431 | BIO_printf(bio_err, "Illegal update period %s\n", *args); | |
432 | badarg = 1; | |
433 | } | |
434 | } | |
435 | if (ndays == -1) | |
436 | ndays = 0; | |
437 | else | |
438 | badarg = 1; | |
439 | } else if (!strcmp(*args, "-nrequest")) { | |
440 | if (args[1]) { | |
441 | args++; | |
442 | accept_count = atol(*args); | |
443 | if (accept_count < 0) { | |
444 | BIO_printf(bio_err, "Illegal accept count %s\n", *args); | |
445 | badarg = 1; | |
446 | } | |
447 | } else | |
448 | badarg = 1; | |
449 | } else if (!strcmp(*args, "-ndays")) { | |
450 | if (args[1]) { | |
451 | args++; | |
452 | ndays = atol(*args); | |
453 | if (ndays < 0) { | |
454 | BIO_printf(bio_err, "Illegal update period %s\n", *args); | |
455 | badarg = 1; | |
456 | } | |
457 | } else | |
458 | badarg = 1; | |
459 | } else if (!strcmp(*args, "-rsigner")) { | |
460 | if (args[1]) { | |
461 | args++; | |
462 | rsignfile = *args; | |
463 | } else | |
464 | badarg = 1; | |
465 | } else if (!strcmp(*args, "-rkey")) { | |
466 | if (args[1]) { | |
467 | args++; | |
468 | rkeyfile = *args; | |
469 | } else | |
470 | badarg = 1; | |
471 | } else if (!strcmp(*args, "-rother")) { | |
472 | if (args[1]) { | |
473 | args++; | |
474 | rcertfile = *args; | |
475 | } else | |
476 | badarg = 1; | |
477 | } else if (!strcmp(*args, "-rmd")) { | |
478 | if (args[1]) { | |
479 | args++; | |
480 | rsign_md = EVP_get_digestbyname(*args); | |
481 | if (!rsign_md) | |
482 | badarg = 1; | |
483 | } else | |
484 | badarg = 1; | |
485 | } else if ((cert_id_md = EVP_get_digestbyname((*args) + 1)) == NULL) { | |
486 | badarg = 1; | |
487 | } | |
488 | args++; | |
489 | } | |
490 | ||
491 | /* Have we anything to do? */ | |
492 | if (!req && !reqin && !respin && !(port && ridx_filename)) | |
493 | badarg = 1; | |
494 | ||
495 | if (badarg) { | |
496 | BIO_printf(bio_err, "OCSP utility\n"); | |
497 | BIO_printf(bio_err, "Usage ocsp [options]\n"); | |
498 | BIO_printf(bio_err, "where options are\n"); | |
499 | BIO_printf(bio_err, "-out file output filename\n"); | |
500 | BIO_printf(bio_err, "-issuer file issuer certificate\n"); | |
501 | BIO_printf(bio_err, "-cert file certificate to check\n"); | |
502 | BIO_printf(bio_err, "-serial n serial number to check\n"); | |
503 | BIO_printf(bio_err, | |
504 | "-signer file certificate to sign OCSP request with\n"); | |
505 | BIO_printf(bio_err, | |
506 | "-signkey file private key to sign OCSP request with\n"); | |
507 | BIO_printf(bio_err, | |
508 | "-sign_other file additional certificates to include in signed request\n"); | |
509 | BIO_printf(bio_err, | |
510 | "-no_certs don't include any certificates in signed request\n"); | |
511 | BIO_printf(bio_err, | |
512 | "-req_text print text form of request\n"); | |
513 | BIO_printf(bio_err, | |
514 | "-resp_text print text form of response\n"); | |
515 | BIO_printf(bio_err, | |
516 | "-text print text form of request and response\n"); | |
517 | BIO_printf(bio_err, | |
518 | "-reqout file write DER encoded OCSP request to \"file\"\n"); | |
519 | BIO_printf(bio_err, | |
520 | "-respout file write DER encoded OCSP reponse to \"file\"\n"); | |
521 | BIO_printf(bio_err, | |
522 | "-reqin file read DER encoded OCSP request from \"file\"\n"); | |
523 | BIO_printf(bio_err, | |
524 | "-respin file read DER encoded OCSP reponse from \"file\"\n"); | |
525 | BIO_printf(bio_err, | |
526 | "-nonce add OCSP nonce to request\n"); | |
527 | BIO_printf(bio_err, | |
528 | "-no_nonce don't add OCSP nonce to request\n"); | |
529 | BIO_printf(bio_err, "-url URL OCSP responder URL\n"); | |
530 | BIO_printf(bio_err, | |
531 | "-host host:n send OCSP request to host on port n\n"); | |
532 | BIO_printf(bio_err, | |
533 | "-path path to use in OCSP request\n"); | |
534 | BIO_printf(bio_err, | |
535 | "-CApath dir trusted certificates directory\n"); | |
536 | BIO_printf(bio_err, | |
537 | "-CAfile file trusted certificates file\n"); | |
538 | BIO_printf(bio_err, | |
539 | "-trusted_first use locally trusted CA's first when building trust chain\n"); | |
25690b7f MC |
540 | BIO_printf(bio_err, |
541 | "-no_alt_chains only ever use the first certificate chain found\n"); | |
0f113f3e MC |
542 | BIO_printf(bio_err, |
543 | "-VAfile file validator certificates file\n"); | |
544 | BIO_printf(bio_err, | |
545 | "-validity_period n maximum validity discrepancy in seconds\n"); | |
546 | BIO_printf(bio_err, | |
547 | "-status_age n maximum status age in seconds\n"); | |
548 | BIO_printf(bio_err, | |
549 | "-noverify don't verify response at all\n"); | |
550 | BIO_printf(bio_err, | |
551 | "-verify_other file additional certificates to search for signer\n"); | |
552 | BIO_printf(bio_err, | |
553 | "-trust_other don't verify additional certificates\n"); | |
554 | BIO_printf(bio_err, | |
555 | "-no_intern don't search certificates contained in response for signer\n"); | |
556 | BIO_printf(bio_err, | |
557 | "-no_signature_verify don't check signature on response\n"); | |
558 | BIO_printf(bio_err, | |
559 | "-no_cert_verify don't check signing certificate\n"); | |
560 | BIO_printf(bio_err, | |
561 | "-no_chain don't chain verify response\n"); | |
562 | BIO_printf(bio_err, | |
563 | "-no_cert_checks don't do additional checks on signing certificate\n"); | |
564 | BIO_printf(bio_err, | |
565 | "-port num port to run responder on\n"); | |
566 | BIO_printf(bio_err, | |
567 | "-index file certificate status index file\n"); | |
568 | BIO_printf(bio_err, "-CA file CA certificate\n"); | |
569 | BIO_printf(bio_err, | |
570 | "-rsigner file responder certificate to sign responses with\n"); | |
571 | BIO_printf(bio_err, | |
572 | "-rkey file responder key to sign responses with\n"); | |
573 | BIO_printf(bio_err, | |
574 | "-rother file other certificates to include in response\n"); | |
575 | BIO_printf(bio_err, | |
576 | "-resp_no_certs don't include any certificates in response\n"); | |
577 | BIO_printf(bio_err, | |
578 | "-nmin n number of minutes before next update\n"); | |
579 | BIO_printf(bio_err, | |
580 | "-ndays n number of days before next update\n"); | |
581 | BIO_printf(bio_err, | |
582 | "-resp_key_id identify reponse by signing certificate key ID\n"); | |
583 | BIO_printf(bio_err, | |
584 | "-nrequest n number of requests to accept (default unlimited)\n"); | |
585 | BIO_printf(bio_err, | |
586 | "-<dgst alg> use specified digest in the request\n"); | |
587 | BIO_printf(bio_err, | |
588 | "-timeout n timeout connection to OCSP responder after n seconds\n"); | |
589 | goto end; | |
590 | } | |
591 | ||
592 | if (outfile) | |
593 | out = BIO_new_file(outfile, "w"); | |
594 | else | |
595 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | |
596 | ||
597 | if (!out) { | |
598 | BIO_printf(bio_err, "Error opening output file\n"); | |
599 | goto end; | |
600 | } | |
601 | ||
602 | if (!req && (add_nonce != 2)) | |
603 | add_nonce = 0; | |
604 | ||
605 | if (!req && reqin) { | |
606 | if (!strcmp(reqin, "-")) | |
607 | derbio = BIO_new_fp(stdin, BIO_NOCLOSE); | |
608 | else | |
609 | derbio = BIO_new_file(reqin, "rb"); | |
610 | if (!derbio) { | |
611 | BIO_printf(bio_err, "Error Opening OCSP request file\n"); | |
612 | goto end; | |
613 | } | |
614 | req = d2i_OCSP_REQUEST_bio(derbio, NULL); | |
615 | BIO_free(derbio); | |
616 | if (!req) { | |
617 | BIO_printf(bio_err, "Error reading OCSP request\n"); | |
618 | goto end; | |
619 | } | |
620 | } | |
621 | ||
622 | if (!req && port) { | |
623 | acbio = init_responder(port); | |
624 | if (!acbio) | |
625 | goto end; | |
626 | } | |
627 | ||
628 | if (rsignfile && !rdb) { | |
629 | if (!rkeyfile) | |
630 | rkeyfile = rsignfile; | |
631 | rsigner = load_cert(bio_err, rsignfile, FORMAT_PEM, | |
632 | NULL, e, "responder certificate"); | |
633 | if (!rsigner) { | |
634 | BIO_printf(bio_err, "Error loading responder certificate\n"); | |
635 | goto end; | |
636 | } | |
637 | rca_cert = load_cert(bio_err, rca_filename, FORMAT_PEM, | |
638 | NULL, e, "CA certificate"); | |
639 | if (rcertfile) { | |
640 | rother = load_certs(bio_err, rcertfile, FORMAT_PEM, | |
641 | NULL, e, "responder other certificates"); | |
642 | if (!rother) | |
643 | goto end; | |
644 | } | |
645 | rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL, NULL, | |
646 | "responder private key"); | |
647 | if (!rkey) | |
648 | goto end; | |
649 | } | |
650 | if (acbio) | |
651 | BIO_printf(bio_err, "Waiting for OCSP client connections...\n"); | |
652 | ||
653 | redo_accept: | |
654 | ||
655 | if (acbio) { | |
656 | if (!do_responder(&req, &cbio, acbio, port)) | |
657 | goto end; | |
658 | if (!req) { | |
659 | resp = | |
660 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, | |
661 | NULL); | |
662 | send_ocsp_response(cbio, resp); | |
663 | goto done_resp; | |
664 | } | |
665 | } | |
666 | ||
667 | if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) { | |
668 | BIO_printf(bio_err, "Need an OCSP request for this operation!\n"); | |
669 | goto end; | |
670 | } | |
671 | ||
672 | if (req && add_nonce) | |
673 | OCSP_request_add1_nonce(req, NULL, -1); | |
674 | ||
675 | if (signfile) { | |
676 | if (!keyfile) | |
677 | keyfile = signfile; | |
678 | signer = load_cert(bio_err, signfile, FORMAT_PEM, | |
679 | NULL, e, "signer certificate"); | |
680 | if (!signer) { | |
681 | BIO_printf(bio_err, "Error loading signer certificate\n"); | |
682 | goto end; | |
683 | } | |
684 | if (sign_certfile) { | |
685 | sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM, | |
686 | NULL, e, "signer certificates"); | |
687 | if (!sign_other) | |
688 | goto end; | |
689 | } | |
690 | key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL, NULL, | |
691 | "signer private key"); | |
692 | if (!key) | |
693 | goto end; | |
694 | ||
695 | if (!OCSP_request_sign | |
696 | (req, signer, key, NULL, sign_other, sign_flags)) { | |
697 | BIO_printf(bio_err, "Error signing OCSP request\n"); | |
698 | goto end; | |
699 | } | |
700 | } | |
701 | ||
702 | if (req_text && req) | |
703 | OCSP_REQUEST_print(out, req, 0); | |
704 | ||
705 | if (reqout) { | |
706 | if (!strcmp(reqout, "-")) | |
707 | derbio = BIO_new_fp(stdout, BIO_NOCLOSE); | |
708 | else | |
709 | derbio = BIO_new_file(reqout, "wb"); | |
710 | if (!derbio) { | |
711 | BIO_printf(bio_err, "Error opening file %s\n", reqout); | |
712 | goto end; | |
713 | } | |
714 | i2d_OCSP_REQUEST_bio(derbio, req); | |
715 | BIO_free(derbio); | |
716 | } | |
717 | ||
718 | if (ridx_filename && (!rkey || !rsigner || !rca_cert)) { | |
719 | BIO_printf(bio_err, | |
720 | "Need a responder certificate, key and CA for this operation!\n"); | |
721 | goto end; | |
722 | } | |
723 | ||
724 | if (ridx_filename && !rdb) { | |
725 | rdb = load_index(ridx_filename, NULL); | |
726 | if (!rdb) | |
727 | goto end; | |
728 | if (!index_index(rdb)) | |
729 | goto end; | |
730 | } | |
731 | ||
732 | if (rdb) { | |
733 | i = make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, | |
734 | rsign_md, rother, rflags, nmin, ndays, badsig); | |
735 | if (cbio) | |
736 | send_ocsp_response(cbio, resp); | |
737 | } else if (host) { | |
738 | # ifndef OPENSSL_NO_SOCK | |
739 | resp = process_responder(bio_err, req, host, path, | |
740 | port, use_ssl, headers, req_timeout); | |
741 | if (!resp) | |
742 | goto end; | |
743 | # else | |
744 | BIO_printf(bio_err, | |
745 | "Error creating connect BIO - sockets not supported.\n"); | |
746 | goto end; | |
747 | # endif | |
748 | } else if (respin) { | |
749 | if (!strcmp(respin, "-")) | |
750 | derbio = BIO_new_fp(stdin, BIO_NOCLOSE); | |
751 | else | |
752 | derbio = BIO_new_file(respin, "rb"); | |
753 | if (!derbio) { | |
754 | BIO_printf(bio_err, "Error Opening OCSP response file\n"); | |
755 | goto end; | |
756 | } | |
757 | resp = d2i_OCSP_RESPONSE_bio(derbio, NULL); | |
758 | BIO_free(derbio); | |
759 | if (!resp) { | |
760 | BIO_printf(bio_err, "Error reading OCSP response\n"); | |
761 | goto end; | |
762 | } | |
763 | ||
764 | } else { | |
765 | ret = 0; | |
766 | goto end; | |
767 | } | |
768 | ||
769 | done_resp: | |
770 | ||
771 | if (respout) { | |
772 | if (!strcmp(respout, "-")) | |
773 | derbio = BIO_new_fp(stdout, BIO_NOCLOSE); | |
774 | else | |
775 | derbio = BIO_new_file(respout, "wb"); | |
776 | if (!derbio) { | |
777 | BIO_printf(bio_err, "Error opening file %s\n", respout); | |
778 | goto end; | |
779 | } | |
780 | i2d_OCSP_RESPONSE_bio(derbio, resp); | |
781 | BIO_free(derbio); | |
782 | } | |
783 | ||
784 | i = OCSP_response_status(resp); | |
785 | ||
786 | if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) { | |
787 | BIO_printf(out, "Responder Error: %s (%d)\n", | |
788 | OCSP_response_status_str(i), i); | |
789 | if (ignore_err) | |
790 | goto redo_accept; | |
791 | ret = 0; | |
792 | goto end; | |
793 | } | |
794 | ||
795 | if (resp_text) | |
796 | OCSP_RESPONSE_print(out, resp, 0); | |
797 | ||
798 | /* If running as responder don't verify our own response */ | |
799 | if (cbio) { | |
800 | if (accept_count > 0) | |
801 | accept_count--; | |
802 | /* Redo if more connections needed */ | |
803 | if (accept_count) { | |
804 | BIO_free_all(cbio); | |
805 | cbio = NULL; | |
806 | OCSP_REQUEST_free(req); | |
807 | req = NULL; | |
808 | OCSP_RESPONSE_free(resp); | |
809 | resp = NULL; | |
810 | goto redo_accept; | |
811 | } | |
812 | ret = 0; | |
813 | goto end; | |
814 | } else if (ridx_filename) { | |
815 | ret = 0; | |
816 | goto end; | |
817 | } | |
818 | ||
819 | if (!store) | |
820 | store = setup_verify(bio_err, CAfile, CApath); | |
821 | if (!store) | |
822 | goto end; | |
823 | if (vpm) | |
824 | X509_STORE_set1_param(store, vpm); | |
825 | if (verify_certfile) { | |
826 | verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM, | |
827 | NULL, e, "validator certificate"); | |
828 | if (!verify_other) | |
829 | goto end; | |
830 | } | |
831 | ||
832 | bs = OCSP_response_get1_basic(resp); | |
833 | ||
834 | if (!bs) { | |
835 | BIO_printf(bio_err, "Error parsing response\n"); | |
836 | goto end; | |
837 | } | |
838 | ||
839 | ret = 0; | |
840 | ||
841 | if (!noverify) { | |
842 | if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) { | |
843 | if (i == -1) | |
844 | BIO_printf(bio_err, "WARNING: no nonce in response\n"); | |
845 | else { | |
846 | BIO_printf(bio_err, "Nonce Verify error\n"); | |
847 | ret = 1; | |
848 | goto end; | |
849 | } | |
850 | } | |
851 | ||
852 | i = OCSP_basic_verify(bs, verify_other, store, verify_flags); | |
853 | if (i <= 0) { | |
854 | BIO_printf(bio_err, "Response Verify Failure\n"); | |
855 | ERR_print_errors(bio_err); | |
856 | ret = 1; | |
857 | } else | |
858 | BIO_printf(bio_err, "Response verify OK\n"); | |
859 | ||
860 | } | |
861 | ||
862 | if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage)) | |
863 | ret = 1; | |
864 | ||
865 | end: | |
866 | ERR_print_errors(bio_err); | |
867 | X509_free(signer); | |
868 | X509_STORE_free(store); | |
869 | if (vpm) | |
870 | X509_VERIFY_PARAM_free(vpm); | |
871 | EVP_PKEY_free(key); | |
872 | EVP_PKEY_free(rkey); | |
873 | X509_free(issuer); | |
874 | X509_free(cert); | |
875 | X509_free(rsigner); | |
876 | X509_free(rca_cert); | |
877 | free_index(rdb); | |
878 | BIO_free_all(cbio); | |
879 | BIO_free_all(acbio); | |
880 | BIO_free(out); | |
881 | OCSP_REQUEST_free(req); | |
882 | OCSP_RESPONSE_free(resp); | |
883 | OCSP_BASICRESP_free(bs); | |
884 | sk_OPENSSL_STRING_free(reqnames); | |
885 | sk_OCSP_CERTID_free(ids); | |
886 | sk_X509_pop_free(sign_other, X509_free); | |
887 | sk_X509_pop_free(verify_other, X509_free); | |
888 | sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); | |
889 | ||
890 | if (thost) | |
891 | OPENSSL_free(thost); | |
892 | if (tport) | |
893 | OPENSSL_free(tport); | |
894 | if (tpath) | |
895 | OPENSSL_free(tpath); | |
896 | ||
897 | OPENSSL_EXIT(ret); | |
5782ceb2 DSH |
898 | } |
899 | ||
0f113f3e MC |
900 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, |
901 | const EVP_MD *cert_id_md, X509 *issuer, | |
902 | STACK_OF(OCSP_CERTID) *ids) | |
903 | { | |
904 | OCSP_CERTID *id; | |
905 | if (!issuer) { | |
906 | BIO_printf(bio_err, "No issuer certificate specified\n"); | |
907 | return 0; | |
908 | } | |
909 | if (!*req) | |
910 | *req = OCSP_REQUEST_new(); | |
911 | if (!*req) | |
912 | goto err; | |
913 | id = OCSP_cert_to_id(cert_id_md, cert, issuer); | |
914 | if (!id || !sk_OCSP_CERTID_push(ids, id)) | |
915 | goto err; | |
916 | if (!OCSP_request_add0_id(*req, id)) | |
917 | goto err; | |
918 | return 1; | |
919 | ||
920 | err: | |
921 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
922 | return 0; | |
923 | } | |
924 | ||
925 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
926 | const EVP_MD *cert_id_md, X509 *issuer, | |
927 | STACK_OF(OCSP_CERTID) *ids) | |
928 | { | |
929 | OCSP_CERTID *id; | |
930 | X509_NAME *iname; | |
931 | ASN1_BIT_STRING *ikey; | |
932 | ASN1_INTEGER *sno; | |
933 | if (!issuer) { | |
934 | BIO_printf(bio_err, "No issuer certificate specified\n"); | |
935 | return 0; | |
936 | } | |
937 | if (!*req) | |
938 | *req = OCSP_REQUEST_new(); | |
939 | if (!*req) | |
940 | goto err; | |
941 | iname = X509_get_subject_name(issuer); | |
942 | ikey = X509_get0_pubkey_bitstr(issuer); | |
943 | sno = s2i_ASN1_INTEGER(NULL, serial); | |
944 | if (!sno) { | |
945 | BIO_printf(bio_err, "Error converting serial number %s\n", serial); | |
946 | return 0; | |
947 | } | |
948 | id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno); | |
949 | ASN1_INTEGER_free(sno); | |
950 | if (!id || !sk_OCSP_CERTID_push(ids, id)) | |
951 | goto err; | |
952 | if (!OCSP_request_add0_id(*req, id)) | |
953 | goto err; | |
954 | return 1; | |
955 | ||
956 | err: | |
957 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
958 | return 0; | |
959 | } | |
73758d43 DSH |
960 | |
961 | static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, | |
0f113f3e MC |
962 | STACK_OF(OPENSSL_STRING) *names, |
963 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
964 | long maxage) | |
965 | { | |
966 | OCSP_CERTID *id; | |
967 | char *name; | |
968 | int i; | |
969 | ||
970 | int status, reason; | |
971 | ||
972 | ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd; | |
973 | ||
974 | if (!bs || !req || !sk_OPENSSL_STRING_num(names) | |
975 | || !sk_OCSP_CERTID_num(ids)) | |
976 | return 1; | |
977 | ||
978 | for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) { | |
979 | id = sk_OCSP_CERTID_value(ids, i); | |
980 | name = sk_OPENSSL_STRING_value(names, i); | |
981 | BIO_printf(out, "%s: ", name); | |
982 | ||
983 | if (!OCSP_resp_find_status(bs, id, &status, &reason, | |
984 | &rev, &thisupd, &nextupd)) { | |
985 | BIO_puts(out, "ERROR: No Status found.\n"); | |
986 | continue; | |
987 | } | |
988 | ||
989 | /* | |
990 | * Check validity: if invalid write to output BIO so we know which | |
991 | * response this refers to. | |
992 | */ | |
993 | if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) { | |
994 | BIO_puts(out, "WARNING: Status times invalid.\n"); | |
995 | ERR_print_errors(out); | |
996 | } | |
997 | BIO_printf(out, "%s\n", OCSP_cert_status_str(status)); | |
998 | ||
999 | BIO_puts(out, "\tThis Update: "); | |
1000 | ASN1_GENERALIZEDTIME_print(out, thisupd); | |
1001 | BIO_puts(out, "\n"); | |
1002 | ||
1003 | if (nextupd) { | |
1004 | BIO_puts(out, "\tNext Update: "); | |
1005 | ASN1_GENERALIZEDTIME_print(out, nextupd); | |
1006 | BIO_puts(out, "\n"); | |
1007 | } | |
1008 | ||
1009 | if (status != V_OCSP_CERTSTATUS_REVOKED) | |
1010 | continue; | |
1011 | ||
1012 | if (reason != -1) | |
1013 | BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason)); | |
1014 | ||
1015 | BIO_puts(out, "\tRevocation Time: "); | |
1016 | ASN1_GENERALIZEDTIME_print(out, rev); | |
1017 | BIO_puts(out, "\n"); | |
1018 | } | |
1019 | ||
1020 | return 1; | |
1021 | } | |
1022 | ||
1023 | static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, | |
1024 | CA_DB *db, X509 *ca, X509 *rcert, | |
1025 | EVP_PKEY *rkey, const EVP_MD *rmd, | |
1026 | STACK_OF(X509) *rother, unsigned long flags, | |
1027 | int nmin, int ndays, int badsig) | |
1028 | { | |
1029 | ASN1_TIME *thisupd = NULL, *nextupd = NULL; | |
1030 | OCSP_CERTID *cid, *ca_id = NULL; | |
1031 | OCSP_BASICRESP *bs = NULL; | |
1032 | int i, id_count, ret = 1; | |
1033 | ||
1034 | id_count = OCSP_request_onereq_count(req); | |
1035 | ||
1036 | if (id_count <= 0) { | |
1037 | *resp = | |
1038 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL); | |
1039 | goto end; | |
1040 | } | |
1041 | ||
1042 | bs = OCSP_BASICRESP_new(); | |
1043 | thisupd = X509_gmtime_adj(NULL, 0); | |
1044 | if (ndays != -1) | |
1045 | nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24); | |
1046 | ||
1047 | /* Examine each certificate id in the request */ | |
1048 | for (i = 0; i < id_count; i++) { | |
1049 | OCSP_ONEREQ *one; | |
1050 | ASN1_INTEGER *serial; | |
1051 | char **inf; | |
1052 | ASN1_OBJECT *cert_id_md_oid; | |
1053 | const EVP_MD *cert_id_md; | |
1054 | one = OCSP_request_onereq_get0(req, i); | |
1055 | cid = OCSP_onereq_get0_id(one); | |
1056 | ||
1057 | OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid); | |
1058 | ||
1059 | cert_id_md = EVP_get_digestbyobj(cert_id_md_oid); | |
1060 | if (!cert_id_md) { | |
1061 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, | |
1062 | NULL); | |
1063 | goto end; | |
1064 | } | |
1065 | if (ca_id) | |
1066 | OCSP_CERTID_free(ca_id); | |
1067 | ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca); | |
1068 | ||
1069 | /* Is this request about our CA? */ | |
1070 | if (OCSP_id_issuer_cmp(ca_id, cid)) { | |
1071 | OCSP_basic_add1_status(bs, cid, | |
1072 | V_OCSP_CERTSTATUS_UNKNOWN, | |
1073 | 0, NULL, thisupd, nextupd); | |
1074 | continue; | |
1075 | } | |
1076 | OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid); | |
1077 | inf = lookup_serial(db, serial); | |
1078 | if (!inf) | |
1079 | OCSP_basic_add1_status(bs, cid, | |
1080 | V_OCSP_CERTSTATUS_UNKNOWN, | |
1081 | 0, NULL, thisupd, nextupd); | |
1082 | else if (inf[DB_type][0] == DB_TYPE_VAL) | |
1083 | OCSP_basic_add1_status(bs, cid, | |
1084 | V_OCSP_CERTSTATUS_GOOD, | |
1085 | 0, NULL, thisupd, nextupd); | |
1086 | else if (inf[DB_type][0] == DB_TYPE_REV) { | |
1087 | ASN1_OBJECT *inst = NULL; | |
1088 | ASN1_TIME *revtm = NULL; | |
1089 | ASN1_GENERALIZEDTIME *invtm = NULL; | |
1090 | OCSP_SINGLERESP *single; | |
1091 | int reason = -1; | |
1092 | unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]); | |
1093 | single = OCSP_basic_add1_status(bs, cid, | |
1094 | V_OCSP_CERTSTATUS_REVOKED, | |
1095 | reason, revtm, thisupd, nextupd); | |
1096 | if (invtm) | |
1097 | OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, | |
1098 | invtm, 0, 0); | |
1099 | else if (inst) | |
1100 | OCSP_SINGLERESP_add1_ext_i2d(single, | |
1101 | NID_hold_instruction_code, inst, | |
1102 | 0, 0); | |
1103 | ASN1_OBJECT_free(inst); | |
1104 | ASN1_TIME_free(revtm); | |
1105 | ASN1_GENERALIZEDTIME_free(invtm); | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | OCSP_copy_nonce(bs, req); | |
1110 | ||
1111 | OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags); | |
1112 | ||
6ef869d7 DSH |
1113 | if (badsig) { |
1114 | ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs); | |
1115 | unsigned char *sigptr; | |
1116 | sigptr = ASN1_STRING_data(sig); | |
1117 | sigptr[ASN1_STRING_length(sig) - 1] ^= 0x1; | |
1118 | } | |
0f113f3e MC |
1119 | |
1120 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs); | |
1121 | ||
1122 | end: | |
1123 | ASN1_TIME_free(thisupd); | |
1124 | ASN1_TIME_free(nextupd); | |
1125 | OCSP_CERTID_free(ca_id); | |
1126 | OCSP_BASICRESP_free(bs); | |
1127 | return ret; | |
1128 | ||
1129 | } | |
ee306a13 | 1130 | |
f85b68cd | 1131 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser) |
0f113f3e MC |
1132 | { |
1133 | int i; | |
1134 | BIGNUM *bn = NULL; | |
1135 | char *itmp, *row[DB_NUMBER], **rrow; | |
1136 | for (i = 0; i < DB_NUMBER; i++) | |
1137 | row[i] = NULL; | |
1138 | bn = ASN1_INTEGER_to_BN(ser, NULL); | |
1139 | OPENSSL_assert(bn); /* FIXME: should report an error at this | |
1140 | * point and abort */ | |
1141 | if (BN_is_zero(bn)) | |
1142 | itmp = BUF_strdup("00"); | |
1143 | else | |
1144 | itmp = BN_bn2hex(bn); | |
1145 | row[DB_serial] = itmp; | |
1146 | BN_free(bn); | |
1147 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
1148 | OPENSSL_free(itmp); | |
1149 | return rrow; | |
1150 | } | |
ee306a13 DSH |
1151 | |
1152 | /* Quick and dirty OCSP server: read in and parse input request */ | |
1153 | ||
c45a48c1 | 1154 | static BIO *init_responder(const char *port) |
0f113f3e MC |
1155 | { |
1156 | BIO *acbio = NULL, *bufbio = NULL; | |
1157 | bufbio = BIO_new(BIO_f_buffer()); | |
1158 | if (!bufbio) | |
1159 | goto err; | |
1160 | # ifndef OPENSSL_NO_SOCK | |
1161 | acbio = BIO_new_accept(port); | |
1162 | # else | |
1163 | BIO_printf(bio_err, | |
1164 | "Error setting up accept BIO - sockets not supported.\n"); | |
1165 | # endif | |
1166 | if (!acbio) | |
1167 | goto err; | |
1168 | BIO_set_accept_bios(acbio, bufbio); | |
1169 | bufbio = NULL; | |
1170 | ||
1171 | if (BIO_do_accept(acbio) <= 0) { | |
1172 | BIO_printf(bio_err, "Error setting up accept BIO\n"); | |
1173 | ERR_print_errors(bio_err); | |
1174 | goto err; | |
1175 | } | |
1176 | ||
1177 | return acbio; | |
1178 | ||
1179 | err: | |
1180 | BIO_free_all(acbio); | |
1181 | BIO_free(bufbio); | |
1182 | return NULL; | |
1183 | } | |
ee306a13 | 1184 | |
c45a48c1 | 1185 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, |
0f113f3e MC |
1186 | const char *port) |
1187 | { | |
1188 | int have_post = 0, len; | |
1189 | OCSP_REQUEST *req = NULL; | |
1190 | char inbuf[1024]; | |
1191 | BIO *cbio = NULL; | |
1192 | ||
1193 | if (BIO_do_accept(acbio) <= 0) { | |
1194 | BIO_printf(bio_err, "Error accepting connection\n"); | |
1195 | ERR_print_errors(bio_err); | |
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | cbio = BIO_pop(acbio); | |
1200 | *pcbio = cbio; | |
1201 | ||
1202 | for (;;) { | |
1203 | len = BIO_gets(cbio, inbuf, sizeof inbuf); | |
1204 | if (len <= 0) | |
1205 | return 1; | |
1206 | /* Look for "POST" signalling start of query */ | |
1207 | if (!have_post) { | |
1208 | if (strncmp(inbuf, "POST", 4)) { | |
1209 | BIO_printf(bio_err, "Invalid request\n"); | |
1210 | return 1; | |
1211 | } | |
1212 | have_post = 1; | |
1213 | } | |
1214 | /* Look for end of headers */ | |
1215 | if ((inbuf[0] == '\r') || (inbuf[0] == '\n')) | |
1216 | break; | |
1217 | } | |
1218 | ||
1219 | /* Try to read OCSP request */ | |
1220 | ||
1221 | req = d2i_OCSP_REQUEST_bio(cbio, NULL); | |
1222 | ||
1223 | if (!req) { | |
1224 | BIO_printf(bio_err, "Error parsing OCSP request\n"); | |
1225 | ERR_print_errors(bio_err); | |
1226 | } | |
1227 | ||
1228 | *preq = req; | |
1229 | ||
1230 | return 1; | |
1231 | ||
1232 | } | |
ee306a13 DSH |
1233 | |
1234 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp) | |
0f113f3e MC |
1235 | { |
1236 | char http_resp[] = | |
1237 | "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n" | |
1238 | "Content-Length: %d\r\n\r\n"; | |
1239 | if (!cbio) | |
1240 | return 0; | |
1241 | BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL)); | |
1242 | i2d_OCSP_RESPONSE_bio(cbio, resp); | |
1243 | (void)BIO_flush(cbio); | |
1244 | return 1; | |
1245 | } | |
ee306a13 | 1246 | |
c45a48c1 | 1247 | static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, const char *path, |
0f113f3e MC |
1248 | const STACK_OF(CONF_VALUE) *headers, |
1249 | OCSP_REQUEST *req, int req_timeout) | |
1250 | { | |
1251 | int fd; | |
1252 | int rv; | |
1253 | int i; | |
1254 | OCSP_REQ_CTX *ctx = NULL; | |
1255 | OCSP_RESPONSE *rsp = NULL; | |
1256 | fd_set confds; | |
1257 | struct timeval tv; | |
1258 | ||
1259 | if (req_timeout != -1) | |
1260 | BIO_set_nbio(cbio, 1); | |
1261 | ||
1262 | rv = BIO_do_connect(cbio); | |
1263 | ||
1264 | if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) { | |
1265 | BIO_puts(err, "Error connecting BIO\n"); | |
1266 | return NULL; | |
1267 | } | |
1268 | ||
1269 | if (BIO_get_fd(cbio, &fd) <= 0) { | |
1270 | BIO_puts(err, "Can't get connection fd\n"); | |
1271 | goto err; | |
1272 | } | |
1273 | ||
1274 | if (req_timeout != -1 && rv <= 0) { | |
1275 | FD_ZERO(&confds); | |
1276 | openssl_fdset(fd, &confds); | |
1277 | tv.tv_usec = 0; | |
1278 | tv.tv_sec = req_timeout; | |
1279 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); | |
1280 | if (rv == 0) { | |
1281 | BIO_puts(err, "Timeout on connect\n"); | |
1282 | return NULL; | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | ctx = OCSP_sendreq_new(cbio, path, NULL, -1); | |
1287 | if (!ctx) | |
1288 | return NULL; | |
1289 | ||
1290 | for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { | |
1291 | CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i); | |
1292 | if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value)) | |
1293 | goto err; | |
1294 | } | |
1295 | ||
1296 | if (!OCSP_REQ_CTX_set1_req(ctx, req)) | |
1297 | goto err; | |
1298 | ||
1299 | for (;;) { | |
1300 | rv = OCSP_sendreq_nbio(&rsp, ctx); | |
1301 | if (rv != -1) | |
1302 | break; | |
1303 | if (req_timeout == -1) | |
1304 | continue; | |
1305 | FD_ZERO(&confds); | |
1306 | openssl_fdset(fd, &confds); | |
1307 | tv.tv_usec = 0; | |
1308 | tv.tv_sec = req_timeout; | |
1309 | if (BIO_should_read(cbio)) | |
1310 | rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv); | |
1311 | else if (BIO_should_write(cbio)) | |
1312 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); | |
1313 | else { | |
1314 | BIO_puts(err, "Unexpected retry condition\n"); | |
1315 | goto err; | |
1316 | } | |
1317 | if (rv == 0) { | |
1318 | BIO_puts(err, "Timeout on request\n"); | |
1319 | break; | |
1320 | } | |
1321 | if (rv == -1) { | |
1322 | BIO_puts(err, "Select error\n"); | |
1323 | break; | |
1324 | } | |
1325 | ||
1326 | } | |
1327 | err: | |
1328 | if (ctx) | |
1329 | OCSP_REQ_CTX_free(ctx); | |
1330 | ||
1331 | return rsp; | |
1332 | } | |
454dbbc5 | 1333 | |
67c8e7f4 | 1334 | OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req, |
0f113f3e MC |
1335 | const char *host, const char *path, |
1336 | const char *port, int use_ssl, | |
1337 | const STACK_OF(CONF_VALUE) *headers, | |
1338 | int req_timeout) | |
1339 | { | |
1340 | BIO *cbio = NULL; | |
1341 | SSL_CTX *ctx = NULL; | |
1342 | OCSP_RESPONSE *resp = NULL; | |
1343 | cbio = BIO_new_connect(host); | |
1344 | if (!cbio) { | |
1345 | BIO_printf(err, "Error creating connect BIO\n"); | |
1346 | goto end; | |
1347 | } | |
1348 | if (port) | |
1349 | BIO_set_conn_port(cbio, port); | |
1350 | if (use_ssl == 1) { | |
1351 | BIO *sbio; | |
1352 | ctx = SSL_CTX_new(SSLv23_client_method()); | |
1353 | if (ctx == NULL) { | |
1354 | BIO_printf(err, "Error creating SSL context.\n"); | |
1355 | goto end; | |
1356 | } | |
1357 | SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); | |
1358 | sbio = BIO_new_ssl(ctx, 1); | |
1359 | cbio = BIO_push(sbio, cbio); | |
1360 | } | |
1361 | resp = query_responder(err, cbio, path, headers, req, req_timeout); | |
1362 | if (!resp) | |
1363 | BIO_printf(bio_err, "Error querying OCSP responder\n"); | |
1364 | end: | |
1365 | if (cbio) | |
1366 | BIO_free_all(cbio); | |
1367 | if (ctx) | |
1368 | SSL_CTX_free(ctx); | |
1369 | return resp; | |
1370 | } | |
67c8e7f4 | 1371 | |
85d686e7 | 1372 | #endif |