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