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