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