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