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