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