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