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