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