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