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