1ebfedea0SLionel Sambuc /* ocsp.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc * 2000.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_OCSP
60ebfedea0SLionel Sambuc
61ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
62ebfedea0SLionel Sambuc # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
63*0a6a1f1dSLionel Sambuc * on OpenVMS */
64ebfedea0SLionel Sambuc # endif
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc # define USE_SOCKETS
67ebfedea0SLionel Sambuc
68ebfedea0SLionel Sambuc # include <stdio.h>
69ebfedea0SLionel Sambuc # include <stdlib.h>
70ebfedea0SLionel Sambuc # include <string.h>
71ebfedea0SLionel Sambuc # include <time.h>
72*0a6a1f1dSLionel Sambuc # include "apps.h" /* needs to be included before the openssl
73*0a6a1f1dSLionel Sambuc * headers! */
74ebfedea0SLionel Sambuc # include <openssl/e_os2.h>
75ebfedea0SLionel Sambuc # include <openssl/crypto.h>
76ebfedea0SLionel Sambuc # include <openssl/err.h>
77ebfedea0SLionel Sambuc # include <openssl/ssl.h>
78ebfedea0SLionel Sambuc # include <openssl/evp.h>
79ebfedea0SLionel Sambuc # include <openssl/bn.h>
80ebfedea0SLionel Sambuc # include <openssl/x509v3.h>
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc # if defined(NETWARE_CLIB)
83ebfedea0SLionel Sambuc # ifdef NETWARE_BSDSOCK
84ebfedea0SLionel Sambuc # include <sys/socket.h>
85ebfedea0SLionel Sambuc # include <sys/bsdskt.h>
86ebfedea0SLionel Sambuc # else
87ebfedea0SLionel Sambuc # include <novsock2.h>
88ebfedea0SLionel Sambuc # endif
89ebfedea0SLionel Sambuc # elif defined(NETWARE_LIBC)
90ebfedea0SLionel Sambuc # ifdef NETWARE_BSDSOCK
91ebfedea0SLionel Sambuc # include <sys/select.h>
92ebfedea0SLionel Sambuc # else
93ebfedea0SLionel Sambuc # include <novsock2.h>
94ebfedea0SLionel Sambuc # endif
95ebfedea0SLionel Sambuc # endif
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc /* Maximum leeway in validity period: default 5 minutes */
98ebfedea0SLionel Sambuc # define MAX_VALIDITY_PERIOD (5 * 60)
99ebfedea0SLionel Sambuc
100*0a6a1f1dSLionel Sambuc static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
101*0a6a1f1dSLionel Sambuc const EVP_MD *cert_id_md, X509 *issuer,
102ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids);
103*0a6a1f1dSLionel Sambuc static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
104*0a6a1f1dSLionel Sambuc const EVP_MD *cert_id_md, X509 *issuer,
105ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids);
106ebfedea0SLionel Sambuc static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
107ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *names,
108ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids, long nsec,
109ebfedea0SLionel Sambuc long maxage);
110ebfedea0SLionel Sambuc
111*0a6a1f1dSLionel Sambuc static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
112*0a6a1f1dSLionel Sambuc CA_DB *db, X509 *ca, X509 *rcert,
113*0a6a1f1dSLionel Sambuc EVP_PKEY *rkey, STACK_OF(X509) *rother,
114*0a6a1f1dSLionel Sambuc unsigned long flags, int nmin, int ndays);
115ebfedea0SLionel Sambuc
116ebfedea0SLionel Sambuc static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
117ebfedea0SLionel Sambuc static BIO *init_responder(char *port);
118*0a6a1f1dSLionel Sambuc static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
119*0a6a1f1dSLionel Sambuc char *port);
120ebfedea0SLionel Sambuc static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
121ebfedea0SLionel Sambuc static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
122ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *headers,
123ebfedea0SLionel Sambuc OCSP_REQUEST *req, int req_timeout);
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc # undef PROG
126ebfedea0SLionel Sambuc # define PROG ocsp_main
127ebfedea0SLionel Sambuc
128ebfedea0SLionel Sambuc int MAIN(int, char **);
129ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)130ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
131ebfedea0SLionel Sambuc {
132ebfedea0SLionel Sambuc ENGINE *e = NULL;
133ebfedea0SLionel Sambuc char **args;
134ebfedea0SLionel Sambuc char *host = NULL, *port = NULL, *path = "/";
135*0a6a1f1dSLionel Sambuc char *thost = NULL, *tport = NULL, *tpath = NULL;
136ebfedea0SLionel Sambuc char *reqin = NULL, *respin = NULL;
137ebfedea0SLionel Sambuc char *reqout = NULL, *respout = NULL;
138ebfedea0SLionel Sambuc char *signfile = NULL, *keyfile = NULL;
139ebfedea0SLionel Sambuc char *rsignfile = NULL, *rkeyfile = NULL;
140ebfedea0SLionel Sambuc char *outfile = NULL;
141ebfedea0SLionel Sambuc int add_nonce = 1, noverify = 0, use_ssl = -1;
142ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *headers = NULL;
143ebfedea0SLionel Sambuc OCSP_REQUEST *req = NULL;
144ebfedea0SLionel Sambuc OCSP_RESPONSE *resp = NULL;
145ebfedea0SLionel Sambuc OCSP_BASICRESP *bs = NULL;
146ebfedea0SLionel Sambuc X509 *issuer = NULL, *cert = NULL;
147ebfedea0SLionel Sambuc X509 *signer = NULL, *rsigner = NULL;
148ebfedea0SLionel Sambuc EVP_PKEY *key = NULL, *rkey = NULL;
149ebfedea0SLionel Sambuc BIO *acbio = NULL, *cbio = NULL;
150ebfedea0SLionel Sambuc BIO *derbio = NULL;
151ebfedea0SLionel Sambuc BIO *out = NULL;
152ebfedea0SLionel Sambuc int req_timeout = -1;
153ebfedea0SLionel Sambuc int req_text = 0, resp_text = 0;
154ebfedea0SLionel Sambuc long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
155ebfedea0SLionel Sambuc char *CAfile = NULL, *CApath = NULL;
156ebfedea0SLionel Sambuc X509_STORE *store = NULL;
157ebfedea0SLionel Sambuc STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
158ebfedea0SLionel Sambuc char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
159ebfedea0SLionel Sambuc unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
160ebfedea0SLionel Sambuc int ret = 1;
161ebfedea0SLionel Sambuc int accept_count = -1;
162ebfedea0SLionel Sambuc int badarg = 0;
163ebfedea0SLionel Sambuc int i;
164ebfedea0SLionel Sambuc int ignore_err = 0;
165ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *reqnames = NULL;
166ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids = NULL;
167ebfedea0SLionel Sambuc
168ebfedea0SLionel Sambuc X509 *rca_cert = NULL;
169ebfedea0SLionel Sambuc char *ridx_filename = NULL;
170ebfedea0SLionel Sambuc char *rca_filename = NULL;
171ebfedea0SLionel Sambuc CA_DB *rdb = NULL;
172ebfedea0SLionel Sambuc int nmin = 0, ndays = -1;
173ebfedea0SLionel Sambuc const EVP_MD *cert_id_md = NULL;
174ebfedea0SLionel Sambuc
175*0a6a1f1dSLionel Sambuc if (bio_err == NULL)
176*0a6a1f1dSLionel Sambuc bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc if (!load_config(bio_err, NULL))
179ebfedea0SLionel Sambuc goto end;
180ebfedea0SLionel Sambuc SSL_load_error_strings();
181ebfedea0SLionel Sambuc OpenSSL_add_ssl_algorithms();
182ebfedea0SLionel Sambuc args = argv + 1;
183ebfedea0SLionel Sambuc reqnames = sk_OPENSSL_STRING_new_null();
184ebfedea0SLionel Sambuc ids = sk_OCSP_CERTID_new_null();
185*0a6a1f1dSLionel Sambuc while (!badarg && *args && *args[0] == '-') {
186*0a6a1f1dSLionel Sambuc if (!strcmp(*args, "-out")) {
187*0a6a1f1dSLionel Sambuc if (args[1]) {
188ebfedea0SLionel Sambuc args++;
189ebfedea0SLionel Sambuc outfile = *args;
190*0a6a1f1dSLionel Sambuc } else
191*0a6a1f1dSLionel Sambuc badarg = 1;
192*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-timeout")) {
193*0a6a1f1dSLionel Sambuc if (args[1]) {
194ebfedea0SLionel Sambuc args++;
195ebfedea0SLionel Sambuc req_timeout = atol(*args);
196*0a6a1f1dSLionel Sambuc if (req_timeout < 0) {
197*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Illegal timeout value %s\n", *args);
198ebfedea0SLionel Sambuc badarg = 1;
199ebfedea0SLionel Sambuc }
200*0a6a1f1dSLionel Sambuc } else
201*0a6a1f1dSLionel Sambuc badarg = 1;
202*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-url")) {
203*0a6a1f1dSLionel Sambuc if (thost)
204*0a6a1f1dSLionel Sambuc OPENSSL_free(thost);
205*0a6a1f1dSLionel Sambuc if (tport)
206*0a6a1f1dSLionel Sambuc OPENSSL_free(tport);
207*0a6a1f1dSLionel Sambuc if (tpath)
208*0a6a1f1dSLionel Sambuc OPENSSL_free(tpath);
209*0a6a1f1dSLionel Sambuc if (args[1]) {
210ebfedea0SLionel Sambuc args++;
211*0a6a1f1dSLionel Sambuc if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl)) {
212ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error parsing URL\n");
213ebfedea0SLionel Sambuc badarg = 1;
214ebfedea0SLionel Sambuc }
215*0a6a1f1dSLionel Sambuc thost = host;
216*0a6a1f1dSLionel Sambuc tport = port;
217*0a6a1f1dSLionel Sambuc tpath = path;
218*0a6a1f1dSLionel Sambuc } else
219*0a6a1f1dSLionel Sambuc badarg = 1;
220*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-host")) {
221*0a6a1f1dSLionel Sambuc if (args[1]) {
222ebfedea0SLionel Sambuc args++;
223ebfedea0SLionel Sambuc host = *args;
224*0a6a1f1dSLionel Sambuc } else
225*0a6a1f1dSLionel Sambuc badarg = 1;
226*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-port")) {
227*0a6a1f1dSLionel Sambuc if (args[1]) {
228ebfedea0SLionel Sambuc args++;
229ebfedea0SLionel Sambuc port = BUF_strdup(*args);
230*0a6a1f1dSLionel Sambuc } else
231*0a6a1f1dSLionel Sambuc badarg = 1;
232*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-header")) {
233*0a6a1f1dSLionel Sambuc if (args[1] && args[2]) {
234ebfedea0SLionel Sambuc if (!X509V3_add_value(args[1], args[2], &headers))
235ebfedea0SLionel Sambuc goto end;
236ebfedea0SLionel Sambuc args += 2;
237*0a6a1f1dSLionel Sambuc } else
238*0a6a1f1dSLionel Sambuc badarg = 1;
239*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-ignore_err"))
240ebfedea0SLionel Sambuc ignore_err = 1;
241ebfedea0SLionel Sambuc else if (!strcmp(*args, "-noverify"))
242ebfedea0SLionel Sambuc noverify = 1;
243ebfedea0SLionel Sambuc else if (!strcmp(*args, "-nonce"))
244ebfedea0SLionel Sambuc add_nonce = 2;
245ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_nonce"))
246ebfedea0SLionel Sambuc add_nonce = 0;
247ebfedea0SLionel Sambuc else if (!strcmp(*args, "-resp_no_certs"))
248ebfedea0SLionel Sambuc rflags |= OCSP_NOCERTS;
249ebfedea0SLionel Sambuc else if (!strcmp(*args, "-resp_key_id"))
250ebfedea0SLionel Sambuc rflags |= OCSP_RESPID_KEY;
251ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_certs"))
252ebfedea0SLionel Sambuc sign_flags |= OCSP_NOCERTS;
253ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_signature_verify"))
254ebfedea0SLionel Sambuc verify_flags |= OCSP_NOSIGS;
255ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_cert_verify"))
256ebfedea0SLionel Sambuc verify_flags |= OCSP_NOVERIFY;
257ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_chain"))
258ebfedea0SLionel Sambuc verify_flags |= OCSP_NOCHAIN;
259ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_cert_checks"))
260ebfedea0SLionel Sambuc verify_flags |= OCSP_NOCHECKS;
261ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_explicit"))
262ebfedea0SLionel Sambuc verify_flags |= OCSP_NOEXPLICIT;
263ebfedea0SLionel Sambuc else if (!strcmp(*args, "-trust_other"))
264ebfedea0SLionel Sambuc verify_flags |= OCSP_TRUSTOTHER;
265ebfedea0SLionel Sambuc else if (!strcmp(*args, "-no_intern"))
266ebfedea0SLionel Sambuc verify_flags |= OCSP_NOINTERN;
267*0a6a1f1dSLionel Sambuc else if (!strcmp(*args, "-text")) {
268ebfedea0SLionel Sambuc req_text = 1;
269ebfedea0SLionel Sambuc resp_text = 1;
270*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-req_text"))
271ebfedea0SLionel Sambuc req_text = 1;
272ebfedea0SLionel Sambuc else if (!strcmp(*args, "-resp_text"))
273ebfedea0SLionel Sambuc resp_text = 1;
274*0a6a1f1dSLionel Sambuc else if (!strcmp(*args, "-reqin")) {
275*0a6a1f1dSLionel Sambuc if (args[1]) {
276ebfedea0SLionel Sambuc args++;
277ebfedea0SLionel Sambuc reqin = *args;
278*0a6a1f1dSLionel Sambuc } else
279*0a6a1f1dSLionel Sambuc badarg = 1;
280*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-respin")) {
281*0a6a1f1dSLionel Sambuc if (args[1]) {
282ebfedea0SLionel Sambuc args++;
283ebfedea0SLionel Sambuc respin = *args;
284*0a6a1f1dSLionel Sambuc } else
285*0a6a1f1dSLionel Sambuc badarg = 1;
286*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-signer")) {
287*0a6a1f1dSLionel Sambuc if (args[1]) {
288ebfedea0SLionel Sambuc args++;
289ebfedea0SLionel Sambuc signfile = *args;
290*0a6a1f1dSLionel Sambuc } else
291*0a6a1f1dSLionel Sambuc badarg = 1;
292*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-VAfile")) {
293*0a6a1f1dSLionel Sambuc if (args[1]) {
294ebfedea0SLionel Sambuc args++;
295ebfedea0SLionel Sambuc verify_certfile = *args;
296ebfedea0SLionel Sambuc verify_flags |= OCSP_TRUSTOTHER;
297*0a6a1f1dSLionel Sambuc } else
298*0a6a1f1dSLionel Sambuc badarg = 1;
299*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-sign_other")) {
300*0a6a1f1dSLionel Sambuc if (args[1]) {
301ebfedea0SLionel Sambuc args++;
302ebfedea0SLionel Sambuc sign_certfile = *args;
303*0a6a1f1dSLionel Sambuc } else
304*0a6a1f1dSLionel Sambuc badarg = 1;
305*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-verify_other")) {
306*0a6a1f1dSLionel Sambuc if (args[1]) {
307ebfedea0SLionel Sambuc args++;
308ebfedea0SLionel Sambuc verify_certfile = *args;
309*0a6a1f1dSLionel Sambuc } else
310*0a6a1f1dSLionel Sambuc badarg = 1;
311*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-CAfile")) {
312*0a6a1f1dSLionel Sambuc if (args[1]) {
313ebfedea0SLionel Sambuc args++;
314ebfedea0SLionel Sambuc CAfile = *args;
315*0a6a1f1dSLionel Sambuc } else
316*0a6a1f1dSLionel Sambuc badarg = 1;
317*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-CApath")) {
318*0a6a1f1dSLionel Sambuc if (args[1]) {
319ebfedea0SLionel Sambuc args++;
320ebfedea0SLionel Sambuc CApath = *args;
321*0a6a1f1dSLionel Sambuc } else
322*0a6a1f1dSLionel Sambuc badarg = 1;
323*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-validity_period")) {
324*0a6a1f1dSLionel Sambuc if (args[1]) {
325ebfedea0SLionel Sambuc args++;
326ebfedea0SLionel Sambuc nsec = atol(*args);
327*0a6a1f1dSLionel Sambuc if (nsec < 0) {
328ebfedea0SLionel Sambuc BIO_printf(bio_err,
329*0a6a1f1dSLionel Sambuc "Illegal validity period %s\n", *args);
330ebfedea0SLionel Sambuc badarg = 1;
331ebfedea0SLionel Sambuc }
332*0a6a1f1dSLionel Sambuc } else
333*0a6a1f1dSLionel Sambuc badarg = 1;
334*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-status_age")) {
335*0a6a1f1dSLionel Sambuc if (args[1]) {
336ebfedea0SLionel Sambuc args++;
337ebfedea0SLionel Sambuc maxage = atol(*args);
338*0a6a1f1dSLionel Sambuc if (maxage < 0) {
339*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Illegal validity age %s\n", *args);
340ebfedea0SLionel Sambuc badarg = 1;
341ebfedea0SLionel Sambuc }
342*0a6a1f1dSLionel Sambuc } else
343*0a6a1f1dSLionel Sambuc badarg = 1;
344*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-signkey")) {
345*0a6a1f1dSLionel Sambuc if (args[1]) {
346ebfedea0SLionel Sambuc args++;
347ebfedea0SLionel Sambuc keyfile = *args;
348*0a6a1f1dSLionel Sambuc } else
349*0a6a1f1dSLionel Sambuc badarg = 1;
350*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-reqout")) {
351*0a6a1f1dSLionel Sambuc if (args[1]) {
352ebfedea0SLionel Sambuc args++;
353ebfedea0SLionel Sambuc reqout = *args;
354*0a6a1f1dSLionel Sambuc } else
355*0a6a1f1dSLionel Sambuc badarg = 1;
356*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-respout")) {
357*0a6a1f1dSLionel Sambuc if (args[1]) {
358ebfedea0SLionel Sambuc args++;
359ebfedea0SLionel Sambuc respout = *args;
360*0a6a1f1dSLionel Sambuc } else
361*0a6a1f1dSLionel Sambuc badarg = 1;
362*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-path")) {
363*0a6a1f1dSLionel Sambuc if (args[1]) {
364ebfedea0SLionel Sambuc args++;
365ebfedea0SLionel Sambuc path = *args;
366*0a6a1f1dSLionel Sambuc } else
367*0a6a1f1dSLionel Sambuc badarg = 1;
368*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-issuer")) {
369*0a6a1f1dSLionel Sambuc if (args[1]) {
370ebfedea0SLionel Sambuc args++;
371ebfedea0SLionel Sambuc X509_free(issuer);
372ebfedea0SLionel Sambuc issuer = load_cert(bio_err, *args, FORMAT_PEM,
373ebfedea0SLionel Sambuc NULL, e, "issuer certificate");
374*0a6a1f1dSLionel Sambuc if (!issuer)
375*0a6a1f1dSLionel Sambuc goto end;
376*0a6a1f1dSLionel Sambuc } else
377*0a6a1f1dSLionel Sambuc badarg = 1;
378*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-cert")) {
379*0a6a1f1dSLionel Sambuc if (args[1]) {
380ebfedea0SLionel Sambuc args++;
381ebfedea0SLionel Sambuc X509_free(cert);
382ebfedea0SLionel Sambuc cert = load_cert(bio_err, *args, FORMAT_PEM,
383ebfedea0SLionel Sambuc NULL, e, "certificate");
384*0a6a1f1dSLionel Sambuc if (!cert)
385*0a6a1f1dSLionel Sambuc goto end;
386*0a6a1f1dSLionel Sambuc if (!cert_id_md)
387*0a6a1f1dSLionel Sambuc cert_id_md = EVP_sha1();
388ebfedea0SLionel Sambuc if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
389ebfedea0SLionel Sambuc goto end;
390ebfedea0SLionel Sambuc if (!sk_OPENSSL_STRING_push(reqnames, *args))
391ebfedea0SLionel Sambuc goto end;
392*0a6a1f1dSLionel Sambuc } else
393*0a6a1f1dSLionel Sambuc badarg = 1;
394*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-serial")) {
395*0a6a1f1dSLionel Sambuc if (args[1]) {
396ebfedea0SLionel Sambuc args++;
397*0a6a1f1dSLionel Sambuc if (!cert_id_md)
398*0a6a1f1dSLionel Sambuc cert_id_md = EVP_sha1();
399ebfedea0SLionel Sambuc if (!add_ocsp_serial(&req, *args, cert_id_md, issuer, ids))
400ebfedea0SLionel Sambuc goto end;
401ebfedea0SLionel Sambuc if (!sk_OPENSSL_STRING_push(reqnames, *args))
402ebfedea0SLionel Sambuc goto end;
403*0a6a1f1dSLionel Sambuc } else
404*0a6a1f1dSLionel Sambuc badarg = 1;
405*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-index")) {
406*0a6a1f1dSLionel Sambuc if (args[1]) {
407ebfedea0SLionel Sambuc args++;
408ebfedea0SLionel Sambuc ridx_filename = *args;
409*0a6a1f1dSLionel Sambuc } else
410*0a6a1f1dSLionel Sambuc badarg = 1;
411*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-CA")) {
412*0a6a1f1dSLionel Sambuc if (args[1]) {
413ebfedea0SLionel Sambuc args++;
414ebfedea0SLionel Sambuc rca_filename = *args;
415*0a6a1f1dSLionel Sambuc } else
416*0a6a1f1dSLionel Sambuc badarg = 1;
417*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-nmin")) {
418*0a6a1f1dSLionel Sambuc if (args[1]) {
419ebfedea0SLionel Sambuc args++;
420ebfedea0SLionel Sambuc nmin = atol(*args);
421*0a6a1f1dSLionel Sambuc if (nmin < 0) {
422*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Illegal update period %s\n", *args);
423ebfedea0SLionel Sambuc badarg = 1;
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc }
426ebfedea0SLionel Sambuc if (ndays == -1)
427ebfedea0SLionel Sambuc ndays = 0;
428*0a6a1f1dSLionel Sambuc else
429*0a6a1f1dSLionel Sambuc badarg = 1;
430*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-nrequest")) {
431*0a6a1f1dSLionel Sambuc if (args[1]) {
432ebfedea0SLionel Sambuc args++;
433ebfedea0SLionel Sambuc accept_count = atol(*args);
434*0a6a1f1dSLionel Sambuc if (accept_count < 0) {
435*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Illegal accept count %s\n", *args);
436ebfedea0SLionel Sambuc badarg = 1;
437ebfedea0SLionel Sambuc }
438*0a6a1f1dSLionel Sambuc } else
439*0a6a1f1dSLionel Sambuc badarg = 1;
440*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-ndays")) {
441*0a6a1f1dSLionel Sambuc if (args[1]) {
442ebfedea0SLionel Sambuc args++;
443ebfedea0SLionel Sambuc ndays = atol(*args);
444*0a6a1f1dSLionel Sambuc if (ndays < 0) {
445*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Illegal update period %s\n", *args);
446ebfedea0SLionel Sambuc badarg = 1;
447ebfedea0SLionel Sambuc }
448*0a6a1f1dSLionel Sambuc } else
449*0a6a1f1dSLionel Sambuc badarg = 1;
450*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-rsigner")) {
451*0a6a1f1dSLionel Sambuc if (args[1]) {
452ebfedea0SLionel Sambuc args++;
453ebfedea0SLionel Sambuc rsignfile = *args;
454*0a6a1f1dSLionel Sambuc } else
455*0a6a1f1dSLionel Sambuc badarg = 1;
456*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-rkey")) {
457*0a6a1f1dSLionel Sambuc if (args[1]) {
458ebfedea0SLionel Sambuc args++;
459ebfedea0SLionel Sambuc rkeyfile = *args;
460*0a6a1f1dSLionel Sambuc } else
461*0a6a1f1dSLionel Sambuc badarg = 1;
462*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-rother")) {
463*0a6a1f1dSLionel Sambuc if (args[1]) {
464ebfedea0SLionel Sambuc args++;
465ebfedea0SLionel Sambuc rcertfile = *args;
466*0a6a1f1dSLionel Sambuc } else
467*0a6a1f1dSLionel Sambuc badarg = 1;
468*0a6a1f1dSLionel Sambuc } else if ((cert_id_md = EVP_get_digestbyname((*args) + 1)) == NULL) {
469ebfedea0SLionel Sambuc badarg = 1;
470ebfedea0SLionel Sambuc }
471ebfedea0SLionel Sambuc args++;
472ebfedea0SLionel Sambuc }
473ebfedea0SLionel Sambuc
474ebfedea0SLionel Sambuc /* Have we anything to do? */
475*0a6a1f1dSLionel Sambuc if (!req && !reqin && !respin && !(port && ridx_filename))
476*0a6a1f1dSLionel Sambuc badarg = 1;
477ebfedea0SLionel Sambuc
478*0a6a1f1dSLionel Sambuc if (badarg) {
479ebfedea0SLionel Sambuc BIO_printf(bio_err, "OCSP utility\n");
480ebfedea0SLionel Sambuc BIO_printf(bio_err, "Usage ocsp [options]\n");
481ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options are\n");
482ebfedea0SLionel Sambuc BIO_printf(bio_err, "-out file output filename\n");
483ebfedea0SLionel Sambuc BIO_printf(bio_err, "-issuer file issuer certificate\n");
484ebfedea0SLionel Sambuc BIO_printf(bio_err, "-cert file certificate to check\n");
485ebfedea0SLionel Sambuc BIO_printf(bio_err, "-serial n serial number to check\n");
486*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
487*0a6a1f1dSLionel Sambuc "-signer file certificate to sign OCSP request with\n");
488*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
489*0a6a1f1dSLionel Sambuc "-signkey file private key to sign OCSP request with\n");
490*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
491*0a6a1f1dSLionel Sambuc "-sign_other file additional certificates to include in signed request\n");
492*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
493*0a6a1f1dSLionel Sambuc "-no_certs don't include any certificates in signed request\n");
494*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
495*0a6a1f1dSLionel Sambuc "-req_text print text form of request\n");
496*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
497*0a6a1f1dSLionel Sambuc "-resp_text print text form of response\n");
498*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
499*0a6a1f1dSLionel Sambuc "-text print text form of request and response\n");
500*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
501*0a6a1f1dSLionel Sambuc "-reqout file write DER encoded OCSP request to \"file\"\n");
502*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
503*0a6a1f1dSLionel Sambuc "-respout file write DER encoded OCSP reponse to \"file\"\n");
504*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
505*0a6a1f1dSLionel Sambuc "-reqin file read DER encoded OCSP request from \"file\"\n");
506*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
507*0a6a1f1dSLionel Sambuc "-respin file read DER encoded OCSP reponse from \"file\"\n");
508*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
509*0a6a1f1dSLionel Sambuc "-nonce add OCSP nonce to request\n");
510*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
511*0a6a1f1dSLionel Sambuc "-no_nonce don't add OCSP nonce to request\n");
512ebfedea0SLionel Sambuc BIO_printf(bio_err, "-url URL OCSP responder URL\n");
513*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
514*0a6a1f1dSLionel Sambuc "-host host:n send OCSP request to host on port n\n");
515*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
516*0a6a1f1dSLionel Sambuc "-path path to use in OCSP request\n");
517*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
518*0a6a1f1dSLionel Sambuc "-CApath dir trusted certificates directory\n");
519*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
520*0a6a1f1dSLionel Sambuc "-CAfile file trusted certificates file\n");
521*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
522*0a6a1f1dSLionel Sambuc "-no_alt_chains only ever use the first certificate chain found\n");
523*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
524*0a6a1f1dSLionel Sambuc "-VAfile file validator certificates file\n");
525*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
526*0a6a1f1dSLionel Sambuc "-validity_period n maximum validity discrepancy in seconds\n");
527*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
528*0a6a1f1dSLionel Sambuc "-status_age n maximum status age in seconds\n");
529*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
530*0a6a1f1dSLionel Sambuc "-noverify don't verify response at all\n");
531*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
532*0a6a1f1dSLionel Sambuc "-verify_other file additional certificates to search for signer\n");
533*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
534*0a6a1f1dSLionel Sambuc "-trust_other don't verify additional certificates\n");
535*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
536*0a6a1f1dSLionel Sambuc "-no_intern don't search certificates contained in response for signer\n");
537*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
538*0a6a1f1dSLionel Sambuc "-no_signature_verify don't check signature on response\n");
539*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
540*0a6a1f1dSLionel Sambuc "-no_cert_verify don't check signing certificate\n");
541*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
542*0a6a1f1dSLionel Sambuc "-no_chain don't chain verify response\n");
543*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
544*0a6a1f1dSLionel Sambuc "-no_cert_checks don't do additional checks on signing certificate\n");
545*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
546*0a6a1f1dSLionel Sambuc "-port num port to run responder on\n");
547*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
548*0a6a1f1dSLionel Sambuc "-index file certificate status index file\n");
549ebfedea0SLionel Sambuc BIO_printf(bio_err, "-CA file CA certificate\n");
550*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
551*0a6a1f1dSLionel Sambuc "-rsigner file responder certificate to sign responses with\n");
552*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
553*0a6a1f1dSLionel Sambuc "-rkey file responder key to sign responses with\n");
554*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
555*0a6a1f1dSLionel Sambuc "-rother file other certificates to include in response\n");
556*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
557*0a6a1f1dSLionel Sambuc "-resp_no_certs don't include any certificates in response\n");
558*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
559*0a6a1f1dSLionel Sambuc "-nmin n number of minutes before next update\n");
560*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
561*0a6a1f1dSLionel Sambuc "-ndays n number of days before next update\n");
562*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
563*0a6a1f1dSLionel Sambuc "-resp_key_id identify reponse by signing certificate key ID\n");
564*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
565*0a6a1f1dSLionel Sambuc "-nrequest n number of requests to accept (default unlimited)\n");
566*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
567*0a6a1f1dSLionel Sambuc "-<dgst alg> use specified digest in the request\n");
568*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
569*0a6a1f1dSLionel Sambuc "-timeout n timeout connection to OCSP responder after n seconds\n");
570ebfedea0SLionel Sambuc goto end;
571ebfedea0SLionel Sambuc }
572ebfedea0SLionel Sambuc
573*0a6a1f1dSLionel Sambuc if (outfile)
574*0a6a1f1dSLionel Sambuc out = BIO_new_file(outfile, "w");
575*0a6a1f1dSLionel Sambuc else
576*0a6a1f1dSLionel Sambuc out = BIO_new_fp(stdout, BIO_NOCLOSE);
577ebfedea0SLionel Sambuc
578*0a6a1f1dSLionel Sambuc if (!out) {
579ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error opening output file\n");
580ebfedea0SLionel Sambuc goto end;
581ebfedea0SLionel Sambuc }
582ebfedea0SLionel Sambuc
583*0a6a1f1dSLionel Sambuc if (!req && (add_nonce != 2))
584*0a6a1f1dSLionel Sambuc add_nonce = 0;
585ebfedea0SLionel Sambuc
586*0a6a1f1dSLionel Sambuc if (!req && reqin) {
587ebfedea0SLionel Sambuc derbio = BIO_new_file(reqin, "rb");
588*0a6a1f1dSLionel Sambuc if (!derbio) {
589ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error Opening OCSP request file\n");
590ebfedea0SLionel Sambuc goto end;
591ebfedea0SLionel Sambuc }
592ebfedea0SLionel Sambuc req = d2i_OCSP_REQUEST_bio(derbio, NULL);
593ebfedea0SLionel Sambuc BIO_free(derbio);
594*0a6a1f1dSLionel Sambuc if (!req) {
595ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading OCSP request\n");
596ebfedea0SLionel Sambuc goto end;
597ebfedea0SLionel Sambuc }
598ebfedea0SLionel Sambuc }
599ebfedea0SLionel Sambuc
600*0a6a1f1dSLionel Sambuc if (!req && port) {
601ebfedea0SLionel Sambuc acbio = init_responder(port);
602ebfedea0SLionel Sambuc if (!acbio)
603ebfedea0SLionel Sambuc goto end;
604ebfedea0SLionel Sambuc }
605ebfedea0SLionel Sambuc
606*0a6a1f1dSLionel Sambuc if (rsignfile && !rdb) {
607*0a6a1f1dSLionel Sambuc if (!rkeyfile)
608*0a6a1f1dSLionel Sambuc rkeyfile = rsignfile;
609ebfedea0SLionel Sambuc rsigner = load_cert(bio_err, rsignfile, FORMAT_PEM,
610ebfedea0SLionel Sambuc NULL, e, "responder certificate");
611*0a6a1f1dSLionel Sambuc if (!rsigner) {
612ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error loading responder certificate\n");
613ebfedea0SLionel Sambuc goto end;
614ebfedea0SLionel Sambuc }
615ebfedea0SLionel Sambuc rca_cert = load_cert(bio_err, rca_filename, FORMAT_PEM,
616ebfedea0SLionel Sambuc NULL, e, "CA certificate");
617*0a6a1f1dSLionel Sambuc if (rcertfile) {
618ebfedea0SLionel Sambuc rother = load_certs(bio_err, rcertfile, FORMAT_PEM,
619ebfedea0SLionel Sambuc NULL, e, "responder other certificates");
620*0a6a1f1dSLionel Sambuc if (!rother)
621*0a6a1f1dSLionel Sambuc goto end;
622ebfedea0SLionel Sambuc }
623ebfedea0SLionel Sambuc rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL, NULL,
624ebfedea0SLionel Sambuc "responder private key");
625ebfedea0SLionel Sambuc if (!rkey)
626ebfedea0SLionel Sambuc goto end;
627ebfedea0SLionel Sambuc }
628ebfedea0SLionel Sambuc if (acbio)
629ebfedea0SLionel Sambuc BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
630ebfedea0SLionel Sambuc
631ebfedea0SLionel Sambuc redo_accept:
632ebfedea0SLionel Sambuc
633*0a6a1f1dSLionel Sambuc if (acbio) {
634ebfedea0SLionel Sambuc if (!do_responder(&req, &cbio, acbio, port))
635ebfedea0SLionel Sambuc goto end;
636*0a6a1f1dSLionel Sambuc if (!req) {
637*0a6a1f1dSLionel Sambuc resp =
638*0a6a1f1dSLionel Sambuc OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
639*0a6a1f1dSLionel Sambuc NULL);
640ebfedea0SLionel Sambuc send_ocsp_response(cbio, resp);
641ebfedea0SLionel Sambuc goto done_resp;
642ebfedea0SLionel Sambuc }
643ebfedea0SLionel Sambuc }
644ebfedea0SLionel Sambuc
645*0a6a1f1dSLionel Sambuc if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
646ebfedea0SLionel Sambuc BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
647ebfedea0SLionel Sambuc goto end;
648ebfedea0SLionel Sambuc }
649ebfedea0SLionel Sambuc
650*0a6a1f1dSLionel Sambuc if (req && add_nonce)
651*0a6a1f1dSLionel Sambuc OCSP_request_add1_nonce(req, NULL, -1);
652ebfedea0SLionel Sambuc
653*0a6a1f1dSLionel Sambuc if (signfile) {
654*0a6a1f1dSLionel Sambuc if (!keyfile)
655*0a6a1f1dSLionel Sambuc keyfile = signfile;
656ebfedea0SLionel Sambuc signer = load_cert(bio_err, signfile, FORMAT_PEM,
657ebfedea0SLionel Sambuc NULL, e, "signer certificate");
658*0a6a1f1dSLionel Sambuc if (!signer) {
659ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error loading signer certificate\n");
660ebfedea0SLionel Sambuc goto end;
661ebfedea0SLionel Sambuc }
662*0a6a1f1dSLionel Sambuc if (sign_certfile) {
663ebfedea0SLionel Sambuc sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM,
664ebfedea0SLionel Sambuc NULL, e, "signer certificates");
665*0a6a1f1dSLionel Sambuc if (!sign_other)
666*0a6a1f1dSLionel Sambuc goto end;
667ebfedea0SLionel Sambuc }
668ebfedea0SLionel Sambuc key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL, NULL,
669ebfedea0SLionel Sambuc "signer private key");
670ebfedea0SLionel Sambuc if (!key)
671ebfedea0SLionel Sambuc goto end;
672ebfedea0SLionel Sambuc
673*0a6a1f1dSLionel Sambuc if (!OCSP_request_sign
674*0a6a1f1dSLionel Sambuc (req, signer, key, NULL, sign_other, sign_flags)) {
675ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error signing OCSP request\n");
676ebfedea0SLionel Sambuc goto end;
677ebfedea0SLionel Sambuc }
678ebfedea0SLionel Sambuc }
679ebfedea0SLionel Sambuc
680*0a6a1f1dSLionel Sambuc if (req_text && req)
681*0a6a1f1dSLionel Sambuc OCSP_REQUEST_print(out, req, 0);
682ebfedea0SLionel Sambuc
683*0a6a1f1dSLionel Sambuc if (reqout) {
684ebfedea0SLionel Sambuc derbio = BIO_new_file(reqout, "wb");
685*0a6a1f1dSLionel Sambuc if (!derbio) {
686ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error opening file %s\n", reqout);
687ebfedea0SLionel Sambuc goto end;
688ebfedea0SLionel Sambuc }
689ebfedea0SLionel Sambuc i2d_OCSP_REQUEST_bio(derbio, req);
690ebfedea0SLionel Sambuc BIO_free(derbio);
691ebfedea0SLionel Sambuc }
692ebfedea0SLionel Sambuc
693*0a6a1f1dSLionel Sambuc if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
694*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
695*0a6a1f1dSLionel Sambuc "Need a responder certificate, key and CA for this operation!\n");
696ebfedea0SLionel Sambuc goto end;
697ebfedea0SLionel Sambuc }
698ebfedea0SLionel Sambuc
699*0a6a1f1dSLionel Sambuc if (ridx_filename && !rdb) {
700ebfedea0SLionel Sambuc rdb = load_index(ridx_filename, NULL);
701*0a6a1f1dSLionel Sambuc if (!rdb)
702*0a6a1f1dSLionel Sambuc goto end;
703*0a6a1f1dSLionel Sambuc if (!index_index(rdb))
704*0a6a1f1dSLionel Sambuc goto end;
705ebfedea0SLionel Sambuc }
706ebfedea0SLionel Sambuc
707*0a6a1f1dSLionel Sambuc if (rdb) {
708*0a6a1f1dSLionel Sambuc i = make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
709*0a6a1f1dSLionel Sambuc rother, rflags, nmin, ndays);
710ebfedea0SLionel Sambuc if (cbio)
711ebfedea0SLionel Sambuc send_ocsp_response(cbio, resp);
712*0a6a1f1dSLionel Sambuc } else if (host) {
713ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SOCK
714ebfedea0SLionel Sambuc resp = process_responder(bio_err, req, host, path,
715ebfedea0SLionel Sambuc port, use_ssl, headers, req_timeout);
716ebfedea0SLionel Sambuc if (!resp)
717ebfedea0SLionel Sambuc goto end;
718ebfedea0SLionel Sambuc # else
719*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
720*0a6a1f1dSLionel Sambuc "Error creating connect BIO - sockets not supported.\n");
721ebfedea0SLionel Sambuc goto end;
722ebfedea0SLionel Sambuc # endif
723*0a6a1f1dSLionel Sambuc } else if (respin) {
724ebfedea0SLionel Sambuc derbio = BIO_new_file(respin, "rb");
725*0a6a1f1dSLionel Sambuc if (!derbio) {
726ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error Opening OCSP response file\n");
727ebfedea0SLionel Sambuc goto end;
728ebfedea0SLionel Sambuc }
729ebfedea0SLionel Sambuc resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
730ebfedea0SLionel Sambuc BIO_free(derbio);
731*0a6a1f1dSLionel Sambuc if (!resp) {
732ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading OCSP response\n");
733ebfedea0SLionel Sambuc goto end;
734ebfedea0SLionel Sambuc }
735ebfedea0SLionel Sambuc
736*0a6a1f1dSLionel Sambuc } else {
737ebfedea0SLionel Sambuc ret = 0;
738ebfedea0SLionel Sambuc goto end;
739ebfedea0SLionel Sambuc }
740ebfedea0SLionel Sambuc
741ebfedea0SLionel Sambuc done_resp:
742ebfedea0SLionel Sambuc
743*0a6a1f1dSLionel Sambuc if (respout) {
744ebfedea0SLionel Sambuc derbio = BIO_new_file(respout, "wb");
745*0a6a1f1dSLionel Sambuc if (!derbio) {
746ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error opening file %s\n", respout);
747ebfedea0SLionel Sambuc goto end;
748ebfedea0SLionel Sambuc }
749ebfedea0SLionel Sambuc i2d_OCSP_RESPONSE_bio(derbio, resp);
750ebfedea0SLionel Sambuc BIO_free(derbio);
751ebfedea0SLionel Sambuc }
752ebfedea0SLionel Sambuc
753ebfedea0SLionel Sambuc i = OCSP_response_status(resp);
754ebfedea0SLionel Sambuc
755*0a6a1f1dSLionel Sambuc if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
756ebfedea0SLionel Sambuc BIO_printf(out, "Responder Error: %s (%d)\n",
757ebfedea0SLionel Sambuc OCSP_response_status_str(i), i);
758ebfedea0SLionel Sambuc if (ignore_err)
759ebfedea0SLionel Sambuc goto redo_accept;
760ebfedea0SLionel Sambuc ret = 0;
761ebfedea0SLionel Sambuc goto end;
762ebfedea0SLionel Sambuc }
763ebfedea0SLionel Sambuc
764*0a6a1f1dSLionel Sambuc if (resp_text)
765*0a6a1f1dSLionel Sambuc OCSP_RESPONSE_print(out, resp, 0);
766ebfedea0SLionel Sambuc
767ebfedea0SLionel Sambuc /* If running as responder don't verify our own response */
768*0a6a1f1dSLionel Sambuc if (cbio) {
769ebfedea0SLionel Sambuc if (accept_count > 0)
770ebfedea0SLionel Sambuc accept_count--;
771ebfedea0SLionel Sambuc /* Redo if more connections needed */
772*0a6a1f1dSLionel Sambuc if (accept_count) {
773ebfedea0SLionel Sambuc BIO_free_all(cbio);
774ebfedea0SLionel Sambuc cbio = NULL;
775ebfedea0SLionel Sambuc OCSP_REQUEST_free(req);
776ebfedea0SLionel Sambuc req = NULL;
777ebfedea0SLionel Sambuc OCSP_RESPONSE_free(resp);
778ebfedea0SLionel Sambuc resp = NULL;
779ebfedea0SLionel Sambuc goto redo_accept;
780ebfedea0SLionel Sambuc }
781ebfedea0SLionel Sambuc goto end;
782ebfedea0SLionel Sambuc }
783ebfedea0SLionel Sambuc
784ebfedea0SLionel Sambuc if (!store)
785ebfedea0SLionel Sambuc store = setup_verify(bio_err, CAfile, CApath);
786ebfedea0SLionel Sambuc if (!store)
787ebfedea0SLionel Sambuc goto end;
788*0a6a1f1dSLionel Sambuc if (verify_certfile) {
789ebfedea0SLionel Sambuc verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM,
790ebfedea0SLionel Sambuc NULL, e, "validator certificate");
791*0a6a1f1dSLionel Sambuc if (!verify_other)
792*0a6a1f1dSLionel Sambuc goto end;
793ebfedea0SLionel Sambuc }
794ebfedea0SLionel Sambuc
795ebfedea0SLionel Sambuc bs = OCSP_response_get1_basic(resp);
796ebfedea0SLionel Sambuc
797*0a6a1f1dSLionel Sambuc if (!bs) {
798ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error parsing response\n");
799ebfedea0SLionel Sambuc goto end;
800ebfedea0SLionel Sambuc }
801ebfedea0SLionel Sambuc
802*0a6a1f1dSLionel Sambuc if (!noverify) {
803*0a6a1f1dSLionel Sambuc if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
804ebfedea0SLionel Sambuc if (i == -1)
805ebfedea0SLionel Sambuc BIO_printf(bio_err, "WARNING: no nonce in response\n");
806*0a6a1f1dSLionel Sambuc else {
807ebfedea0SLionel Sambuc BIO_printf(bio_err, "Nonce Verify error\n");
808ebfedea0SLionel Sambuc goto end;
809ebfedea0SLionel Sambuc }
810ebfedea0SLionel Sambuc }
811ebfedea0SLionel Sambuc
812ebfedea0SLionel Sambuc i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
813*0a6a1f1dSLionel Sambuc if (i < 0)
814*0a6a1f1dSLionel Sambuc i = OCSP_basic_verify(bs, NULL, store, 0);
815ebfedea0SLionel Sambuc
816*0a6a1f1dSLionel Sambuc if (i <= 0) {
817ebfedea0SLionel Sambuc BIO_printf(bio_err, "Response Verify Failure\n");
818ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
819*0a6a1f1dSLionel Sambuc } else
820ebfedea0SLionel Sambuc BIO_printf(bio_err, "Response verify OK\n");
821ebfedea0SLionel Sambuc
822ebfedea0SLionel Sambuc }
823ebfedea0SLionel Sambuc
824ebfedea0SLionel Sambuc if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
825ebfedea0SLionel Sambuc goto end;
826ebfedea0SLionel Sambuc
827ebfedea0SLionel Sambuc ret = 0;
828ebfedea0SLionel Sambuc
829ebfedea0SLionel Sambuc end:
830ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
831ebfedea0SLionel Sambuc X509_free(signer);
832ebfedea0SLionel Sambuc X509_STORE_free(store);
833ebfedea0SLionel Sambuc EVP_PKEY_free(key);
834ebfedea0SLionel Sambuc EVP_PKEY_free(rkey);
835ebfedea0SLionel Sambuc X509_free(issuer);
836ebfedea0SLionel Sambuc X509_free(cert);
837ebfedea0SLionel Sambuc X509_free(rsigner);
838ebfedea0SLionel Sambuc X509_free(rca_cert);
839ebfedea0SLionel Sambuc free_index(rdb);
840ebfedea0SLionel Sambuc BIO_free_all(cbio);
841ebfedea0SLionel Sambuc BIO_free_all(acbio);
842ebfedea0SLionel Sambuc BIO_free(out);
843ebfedea0SLionel Sambuc OCSP_REQUEST_free(req);
844ebfedea0SLionel Sambuc OCSP_RESPONSE_free(resp);
845ebfedea0SLionel Sambuc OCSP_BASICRESP_free(bs);
846ebfedea0SLionel Sambuc sk_OPENSSL_STRING_free(reqnames);
847ebfedea0SLionel Sambuc sk_OCSP_CERTID_free(ids);
848ebfedea0SLionel Sambuc sk_X509_pop_free(sign_other, X509_free);
849ebfedea0SLionel Sambuc sk_X509_pop_free(verify_other, X509_free);
850ebfedea0SLionel Sambuc sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
851ebfedea0SLionel Sambuc
852*0a6a1f1dSLionel Sambuc if (thost)
853*0a6a1f1dSLionel Sambuc OPENSSL_free(thost);
854*0a6a1f1dSLionel Sambuc if (tport)
855*0a6a1f1dSLionel Sambuc OPENSSL_free(tport);
856*0a6a1f1dSLionel Sambuc if (tpath)
857*0a6a1f1dSLionel Sambuc OPENSSL_free(tpath);
858ebfedea0SLionel Sambuc
859ebfedea0SLionel Sambuc OPENSSL_EXIT(ret);
860ebfedea0SLionel Sambuc }
861ebfedea0SLionel Sambuc
add_ocsp_cert(OCSP_REQUEST ** req,X509 * cert,const EVP_MD * cert_id_md,X509 * issuer,STACK_OF (OCSP_CERTID)* ids)862*0a6a1f1dSLionel Sambuc static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
863*0a6a1f1dSLionel Sambuc const EVP_MD *cert_id_md, X509 *issuer,
864ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids)
865ebfedea0SLionel Sambuc {
866ebfedea0SLionel Sambuc OCSP_CERTID *id;
867*0a6a1f1dSLionel Sambuc if (!issuer) {
868ebfedea0SLionel Sambuc BIO_printf(bio_err, "No issuer certificate specified\n");
869ebfedea0SLionel Sambuc return 0;
870ebfedea0SLionel Sambuc }
871*0a6a1f1dSLionel Sambuc if (!*req)
872*0a6a1f1dSLionel Sambuc *req = OCSP_REQUEST_new();
873*0a6a1f1dSLionel Sambuc if (!*req)
874*0a6a1f1dSLionel Sambuc goto err;
875ebfedea0SLionel Sambuc id = OCSP_cert_to_id(cert_id_md, cert, issuer);
876*0a6a1f1dSLionel Sambuc if (!id || !sk_OCSP_CERTID_push(ids, id))
877*0a6a1f1dSLionel Sambuc goto err;
878*0a6a1f1dSLionel Sambuc if (!OCSP_request_add0_id(*req, id))
879*0a6a1f1dSLionel Sambuc goto err;
880ebfedea0SLionel Sambuc return 1;
881ebfedea0SLionel Sambuc
882ebfedea0SLionel Sambuc err:
883ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error Creating OCSP request\n");
884ebfedea0SLionel Sambuc return 0;
885ebfedea0SLionel Sambuc }
886ebfedea0SLionel Sambuc
add_ocsp_serial(OCSP_REQUEST ** req,char * serial,const EVP_MD * cert_id_md,X509 * issuer,STACK_OF (OCSP_CERTID)* ids)887*0a6a1f1dSLionel Sambuc static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
888*0a6a1f1dSLionel Sambuc const EVP_MD *cert_id_md, X509 *issuer,
889ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids)
890ebfedea0SLionel Sambuc {
891ebfedea0SLionel Sambuc OCSP_CERTID *id;
892ebfedea0SLionel Sambuc X509_NAME *iname;
893ebfedea0SLionel Sambuc ASN1_BIT_STRING *ikey;
894ebfedea0SLionel Sambuc ASN1_INTEGER *sno;
895*0a6a1f1dSLionel Sambuc if (!issuer) {
896ebfedea0SLionel Sambuc BIO_printf(bio_err, "No issuer certificate specified\n");
897ebfedea0SLionel Sambuc return 0;
898ebfedea0SLionel Sambuc }
899*0a6a1f1dSLionel Sambuc if (!*req)
900*0a6a1f1dSLionel Sambuc *req = OCSP_REQUEST_new();
901*0a6a1f1dSLionel Sambuc if (!*req)
902*0a6a1f1dSLionel Sambuc goto err;
903ebfedea0SLionel Sambuc iname = X509_get_subject_name(issuer);
904ebfedea0SLionel Sambuc ikey = X509_get0_pubkey_bitstr(issuer);
905ebfedea0SLionel Sambuc sno = s2i_ASN1_INTEGER(NULL, serial);
906*0a6a1f1dSLionel Sambuc if (!sno) {
907ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error converting serial number %s\n", serial);
908ebfedea0SLionel Sambuc return 0;
909ebfedea0SLionel Sambuc }
910ebfedea0SLionel Sambuc id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
911ebfedea0SLionel Sambuc ASN1_INTEGER_free(sno);
912*0a6a1f1dSLionel Sambuc if (!id || !sk_OCSP_CERTID_push(ids, id))
913*0a6a1f1dSLionel Sambuc goto err;
914*0a6a1f1dSLionel Sambuc if (!OCSP_request_add0_id(*req, id))
915*0a6a1f1dSLionel Sambuc goto err;
916ebfedea0SLionel Sambuc return 1;
917ebfedea0SLionel Sambuc
918ebfedea0SLionel Sambuc err:
919ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error Creating OCSP request\n");
920ebfedea0SLionel Sambuc return 0;
921ebfedea0SLionel Sambuc }
922ebfedea0SLionel Sambuc
print_ocsp_summary(BIO * out,OCSP_BASICRESP * bs,OCSP_REQUEST * req,STACK_OF (OPENSSL_STRING)* names,STACK_OF (OCSP_CERTID)* ids,long nsec,long maxage)923ebfedea0SLionel Sambuc static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
924ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *names,
925ebfedea0SLionel Sambuc STACK_OF(OCSP_CERTID) *ids, long nsec,
926ebfedea0SLionel Sambuc long maxage)
927ebfedea0SLionel Sambuc {
928ebfedea0SLionel Sambuc OCSP_CERTID *id;
929ebfedea0SLionel Sambuc char *name;
930ebfedea0SLionel Sambuc int i;
931ebfedea0SLionel Sambuc
932ebfedea0SLionel Sambuc int status, reason;
933ebfedea0SLionel Sambuc
934ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
935ebfedea0SLionel Sambuc
936*0a6a1f1dSLionel Sambuc if (!bs || !req || !sk_OPENSSL_STRING_num(names)
937*0a6a1f1dSLionel Sambuc || !sk_OCSP_CERTID_num(ids))
938ebfedea0SLionel Sambuc return 1;
939ebfedea0SLionel Sambuc
940*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
941ebfedea0SLionel Sambuc id = sk_OCSP_CERTID_value(ids, i);
942ebfedea0SLionel Sambuc name = sk_OPENSSL_STRING_value(names, i);
943ebfedea0SLionel Sambuc BIO_printf(out, "%s: ", name);
944ebfedea0SLionel Sambuc
945ebfedea0SLionel Sambuc if (!OCSP_resp_find_status(bs, id, &status, &reason,
946*0a6a1f1dSLionel Sambuc &rev, &thisupd, &nextupd)) {
947ebfedea0SLionel Sambuc BIO_puts(out, "ERROR: No Status found.\n");
948ebfedea0SLionel Sambuc continue;
949ebfedea0SLionel Sambuc }
950ebfedea0SLionel Sambuc
951*0a6a1f1dSLionel Sambuc /*
952*0a6a1f1dSLionel Sambuc * Check validity: if invalid write to output BIO so we know which
953*0a6a1f1dSLionel Sambuc * response this refers to.
954ebfedea0SLionel Sambuc */
955*0a6a1f1dSLionel Sambuc if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
956ebfedea0SLionel Sambuc BIO_puts(out, "WARNING: Status times invalid.\n");
957ebfedea0SLionel Sambuc ERR_print_errors(out);
958ebfedea0SLionel Sambuc }
959ebfedea0SLionel Sambuc BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
960ebfedea0SLionel Sambuc
961ebfedea0SLionel Sambuc BIO_puts(out, "\tThis Update: ");
962ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME_print(out, thisupd);
963ebfedea0SLionel Sambuc BIO_puts(out, "\n");
964ebfedea0SLionel Sambuc
965*0a6a1f1dSLionel Sambuc if (nextupd) {
966ebfedea0SLionel Sambuc BIO_puts(out, "\tNext Update: ");
967ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME_print(out, nextupd);
968ebfedea0SLionel Sambuc BIO_puts(out, "\n");
969ebfedea0SLionel Sambuc }
970ebfedea0SLionel Sambuc
971ebfedea0SLionel Sambuc if (status != V_OCSP_CERTSTATUS_REVOKED)
972ebfedea0SLionel Sambuc continue;
973ebfedea0SLionel Sambuc
974ebfedea0SLionel Sambuc if (reason != -1)
975*0a6a1f1dSLionel Sambuc BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
976ebfedea0SLionel Sambuc
977ebfedea0SLionel Sambuc BIO_puts(out, "\tRevocation Time: ");
978ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME_print(out, rev);
979ebfedea0SLionel Sambuc BIO_puts(out, "\n");
980ebfedea0SLionel Sambuc }
981ebfedea0SLionel Sambuc
982ebfedea0SLionel Sambuc return 1;
983ebfedea0SLionel Sambuc }
984ebfedea0SLionel Sambuc
make_ocsp_response(OCSP_RESPONSE ** resp,OCSP_REQUEST * req,CA_DB * db,X509 * ca,X509 * rcert,EVP_PKEY * rkey,STACK_OF (X509)* rother,unsigned long flags,int nmin,int ndays)985*0a6a1f1dSLionel Sambuc static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
986*0a6a1f1dSLionel Sambuc CA_DB *db, X509 *ca, X509 *rcert,
987*0a6a1f1dSLionel Sambuc EVP_PKEY *rkey, STACK_OF(X509) *rother,
988*0a6a1f1dSLionel Sambuc unsigned long flags, int nmin, int ndays)
989ebfedea0SLionel Sambuc {
990ebfedea0SLionel Sambuc ASN1_TIME *thisupd = NULL, *nextupd = NULL;
991ebfedea0SLionel Sambuc OCSP_CERTID *cid, *ca_id = NULL;
992ebfedea0SLionel Sambuc OCSP_BASICRESP *bs = NULL;
993ebfedea0SLionel Sambuc int i, id_count, ret = 1;
994ebfedea0SLionel Sambuc
995ebfedea0SLionel Sambuc id_count = OCSP_request_onereq_count(req);
996ebfedea0SLionel Sambuc
997*0a6a1f1dSLionel Sambuc if (id_count <= 0) {
998*0a6a1f1dSLionel Sambuc *resp =
999*0a6a1f1dSLionel Sambuc OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
1000ebfedea0SLionel Sambuc goto end;
1001ebfedea0SLionel Sambuc }
1002ebfedea0SLionel Sambuc
1003ebfedea0SLionel Sambuc bs = OCSP_BASICRESP_new();
1004ebfedea0SLionel Sambuc thisupd = X509_gmtime_adj(NULL, 0);
1005ebfedea0SLionel Sambuc if (ndays != -1)
1006ebfedea0SLionel Sambuc nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24);
1007ebfedea0SLionel Sambuc
1008ebfedea0SLionel Sambuc /* Examine each certificate id in the request */
1009*0a6a1f1dSLionel Sambuc for (i = 0; i < id_count; i++) {
1010ebfedea0SLionel Sambuc OCSP_ONEREQ *one;
1011ebfedea0SLionel Sambuc ASN1_INTEGER *serial;
1012ebfedea0SLionel Sambuc char **inf;
1013ebfedea0SLionel Sambuc ASN1_OBJECT *cert_id_md_oid;
1014ebfedea0SLionel Sambuc const EVP_MD *cert_id_md;
1015ebfedea0SLionel Sambuc one = OCSP_request_onereq_get0(req, i);
1016ebfedea0SLionel Sambuc cid = OCSP_onereq_get0_id(one);
1017ebfedea0SLionel Sambuc
1018ebfedea0SLionel Sambuc OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
1019ebfedea0SLionel Sambuc
1020ebfedea0SLionel Sambuc cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
1021*0a6a1f1dSLionel Sambuc if (!cert_id_md) {
1022ebfedea0SLionel Sambuc *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
1023ebfedea0SLionel Sambuc NULL);
1024ebfedea0SLionel Sambuc goto end;
1025ebfedea0SLionel Sambuc }
1026*0a6a1f1dSLionel Sambuc if (ca_id)
1027*0a6a1f1dSLionel Sambuc OCSP_CERTID_free(ca_id);
1028ebfedea0SLionel Sambuc ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
1029ebfedea0SLionel Sambuc
1030ebfedea0SLionel Sambuc /* Is this request about our CA? */
1031*0a6a1f1dSLionel Sambuc if (OCSP_id_issuer_cmp(ca_id, cid)) {
1032ebfedea0SLionel Sambuc OCSP_basic_add1_status(bs, cid,
1033ebfedea0SLionel Sambuc V_OCSP_CERTSTATUS_UNKNOWN,
1034*0a6a1f1dSLionel Sambuc 0, NULL, thisupd, nextupd);
1035ebfedea0SLionel Sambuc continue;
1036ebfedea0SLionel Sambuc }
1037ebfedea0SLionel Sambuc OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
1038ebfedea0SLionel Sambuc inf = lookup_serial(db, serial);
1039ebfedea0SLionel Sambuc if (!inf)
1040ebfedea0SLionel Sambuc OCSP_basic_add1_status(bs, cid,
1041ebfedea0SLionel Sambuc V_OCSP_CERTSTATUS_UNKNOWN,
1042*0a6a1f1dSLionel Sambuc 0, NULL, thisupd, nextupd);
1043ebfedea0SLionel Sambuc else if (inf[DB_type][0] == DB_TYPE_VAL)
1044ebfedea0SLionel Sambuc OCSP_basic_add1_status(bs, cid,
1045ebfedea0SLionel Sambuc V_OCSP_CERTSTATUS_GOOD,
1046*0a6a1f1dSLionel Sambuc 0, NULL, thisupd, nextupd);
1047*0a6a1f1dSLionel Sambuc else if (inf[DB_type][0] == DB_TYPE_REV) {
1048ebfedea0SLionel Sambuc ASN1_OBJECT *inst = NULL;
1049ebfedea0SLionel Sambuc ASN1_TIME *revtm = NULL;
1050ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME *invtm = NULL;
1051ebfedea0SLionel Sambuc OCSP_SINGLERESP *single;
1052ebfedea0SLionel Sambuc int reason = -1;
1053ebfedea0SLionel Sambuc unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
1054ebfedea0SLionel Sambuc single = OCSP_basic_add1_status(bs, cid,
1055ebfedea0SLionel Sambuc V_OCSP_CERTSTATUS_REVOKED,
1056*0a6a1f1dSLionel Sambuc reason, revtm, thisupd, nextupd);
1057ebfedea0SLionel Sambuc if (invtm)
1058*0a6a1f1dSLionel Sambuc OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
1059*0a6a1f1dSLionel Sambuc invtm, 0, 0);
1060ebfedea0SLionel Sambuc else if (inst)
1061*0a6a1f1dSLionel Sambuc OCSP_SINGLERESP_add1_ext_i2d(single,
1062*0a6a1f1dSLionel Sambuc NID_hold_instruction_code, inst,
1063*0a6a1f1dSLionel Sambuc 0, 0);
1064ebfedea0SLionel Sambuc ASN1_OBJECT_free(inst);
1065ebfedea0SLionel Sambuc ASN1_TIME_free(revtm);
1066ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME_free(invtm);
1067ebfedea0SLionel Sambuc }
1068ebfedea0SLionel Sambuc }
1069ebfedea0SLionel Sambuc
1070ebfedea0SLionel Sambuc OCSP_copy_nonce(bs, req);
1071ebfedea0SLionel Sambuc
1072ebfedea0SLionel Sambuc OCSP_basic_sign(bs, rcert, rkey, NULL, rother, flags);
1073ebfedea0SLionel Sambuc
1074ebfedea0SLionel Sambuc *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
1075ebfedea0SLionel Sambuc
1076ebfedea0SLionel Sambuc end:
1077ebfedea0SLionel Sambuc ASN1_TIME_free(thisupd);
1078ebfedea0SLionel Sambuc ASN1_TIME_free(nextupd);
1079ebfedea0SLionel Sambuc OCSP_CERTID_free(ca_id);
1080ebfedea0SLionel Sambuc OCSP_BASICRESP_free(bs);
1081ebfedea0SLionel Sambuc return ret;
1082ebfedea0SLionel Sambuc
1083ebfedea0SLionel Sambuc }
1084ebfedea0SLionel Sambuc
lookup_serial(CA_DB * db,ASN1_INTEGER * ser)1085ebfedea0SLionel Sambuc static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
1086ebfedea0SLionel Sambuc {
1087ebfedea0SLionel Sambuc int i;
1088ebfedea0SLionel Sambuc BIGNUM *bn = NULL;
1089ebfedea0SLionel Sambuc char *itmp, *row[DB_NUMBER], **rrow;
1090*0a6a1f1dSLionel Sambuc for (i = 0; i < DB_NUMBER; i++)
1091*0a6a1f1dSLionel Sambuc row[i] = NULL;
1092ebfedea0SLionel Sambuc bn = ASN1_INTEGER_to_BN(ser, NULL);
1093*0a6a1f1dSLionel Sambuc OPENSSL_assert(bn); /* FIXME: should report an error at this
1094*0a6a1f1dSLionel Sambuc * point and abort */
1095ebfedea0SLionel Sambuc if (BN_is_zero(bn))
1096ebfedea0SLionel Sambuc itmp = BUF_strdup("00");
1097ebfedea0SLionel Sambuc else
1098ebfedea0SLionel Sambuc itmp = BN_bn2hex(bn);
1099ebfedea0SLionel Sambuc row[DB_serial] = itmp;
1100ebfedea0SLionel Sambuc BN_free(bn);
1101ebfedea0SLionel Sambuc rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1102ebfedea0SLionel Sambuc OPENSSL_free(itmp);
1103ebfedea0SLionel Sambuc return rrow;
1104ebfedea0SLionel Sambuc }
1105ebfedea0SLionel Sambuc
1106ebfedea0SLionel Sambuc /* Quick and dirty OCSP server: read in and parse input request */
1107ebfedea0SLionel Sambuc
init_responder(char * port)1108ebfedea0SLionel Sambuc static BIO *init_responder(char *port)
1109ebfedea0SLionel Sambuc {
1110ebfedea0SLionel Sambuc BIO *acbio = NULL, *bufbio = NULL;
1111ebfedea0SLionel Sambuc bufbio = BIO_new(BIO_f_buffer());
1112ebfedea0SLionel Sambuc if (!bufbio)
1113ebfedea0SLionel Sambuc goto err;
1114ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SOCK
1115ebfedea0SLionel Sambuc acbio = BIO_new_accept(port);
1116ebfedea0SLionel Sambuc # else
1117*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1118*0a6a1f1dSLionel Sambuc "Error setting up accept BIO - sockets not supported.\n");
1119ebfedea0SLionel Sambuc # endif
1120ebfedea0SLionel Sambuc if (!acbio)
1121ebfedea0SLionel Sambuc goto err;
1122ebfedea0SLionel Sambuc BIO_set_accept_bios(acbio, bufbio);
1123ebfedea0SLionel Sambuc bufbio = NULL;
1124ebfedea0SLionel Sambuc
1125*0a6a1f1dSLionel Sambuc if (BIO_do_accept(acbio) <= 0) {
1126ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error setting up accept BIO\n");
1127ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1128ebfedea0SLionel Sambuc goto err;
1129ebfedea0SLionel Sambuc }
1130ebfedea0SLionel Sambuc
1131ebfedea0SLionel Sambuc return acbio;
1132ebfedea0SLionel Sambuc
1133ebfedea0SLionel Sambuc err:
1134ebfedea0SLionel Sambuc BIO_free_all(acbio);
1135ebfedea0SLionel Sambuc BIO_free(bufbio);
1136ebfedea0SLionel Sambuc return NULL;
1137ebfedea0SLionel Sambuc }
1138ebfedea0SLionel Sambuc
do_responder(OCSP_REQUEST ** preq,BIO ** pcbio,BIO * acbio,char * port)1139*0a6a1f1dSLionel Sambuc static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
1140*0a6a1f1dSLionel Sambuc char *port)
1141ebfedea0SLionel Sambuc {
1142ebfedea0SLionel Sambuc int have_post = 0, len;
1143ebfedea0SLionel Sambuc OCSP_REQUEST *req = NULL;
1144ebfedea0SLionel Sambuc char inbuf[1024];
1145ebfedea0SLionel Sambuc BIO *cbio = NULL;
1146ebfedea0SLionel Sambuc
1147*0a6a1f1dSLionel Sambuc if (BIO_do_accept(acbio) <= 0) {
1148ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error accepting connection\n");
1149ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1150ebfedea0SLionel Sambuc return 0;
1151ebfedea0SLionel Sambuc }
1152ebfedea0SLionel Sambuc
1153ebfedea0SLionel Sambuc cbio = BIO_pop(acbio);
1154ebfedea0SLionel Sambuc *pcbio = cbio;
1155ebfedea0SLionel Sambuc
1156*0a6a1f1dSLionel Sambuc for (;;) {
1157ebfedea0SLionel Sambuc len = BIO_gets(cbio, inbuf, sizeof inbuf);
1158ebfedea0SLionel Sambuc if (len <= 0)
1159ebfedea0SLionel Sambuc return 1;
1160ebfedea0SLionel Sambuc /* Look for "POST" signalling start of query */
1161*0a6a1f1dSLionel Sambuc if (!have_post) {
1162*0a6a1f1dSLionel Sambuc if (strncmp(inbuf, "POST", 4)) {
1163ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid request\n");
1164ebfedea0SLionel Sambuc return 1;
1165ebfedea0SLionel Sambuc }
1166ebfedea0SLionel Sambuc have_post = 1;
1167ebfedea0SLionel Sambuc }
1168ebfedea0SLionel Sambuc /* Look for end of headers */
1169ebfedea0SLionel Sambuc if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1170ebfedea0SLionel Sambuc break;
1171ebfedea0SLionel Sambuc }
1172ebfedea0SLionel Sambuc
1173ebfedea0SLionel Sambuc /* Try to read OCSP request */
1174ebfedea0SLionel Sambuc
1175ebfedea0SLionel Sambuc req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1176ebfedea0SLionel Sambuc
1177*0a6a1f1dSLionel Sambuc if (!req) {
1178ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error parsing OCSP request\n");
1179ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1180ebfedea0SLionel Sambuc }
1181ebfedea0SLionel Sambuc
1182ebfedea0SLionel Sambuc *preq = req;
1183ebfedea0SLionel Sambuc
1184ebfedea0SLionel Sambuc return 1;
1185ebfedea0SLionel Sambuc
1186ebfedea0SLionel Sambuc }
1187ebfedea0SLionel Sambuc
send_ocsp_response(BIO * cbio,OCSP_RESPONSE * resp)1188ebfedea0SLionel Sambuc static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
1189ebfedea0SLionel Sambuc {
1190ebfedea0SLionel Sambuc char http_resp[] =
1191ebfedea0SLionel Sambuc "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1192ebfedea0SLionel Sambuc "Content-Length: %d\r\n\r\n";
1193ebfedea0SLionel Sambuc if (!cbio)
1194ebfedea0SLionel Sambuc return 0;
1195ebfedea0SLionel Sambuc BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1196ebfedea0SLionel Sambuc i2d_OCSP_RESPONSE_bio(cbio, resp);
1197ebfedea0SLionel Sambuc (void)BIO_flush(cbio);
1198ebfedea0SLionel Sambuc return 1;
1199ebfedea0SLionel Sambuc }
1200ebfedea0SLionel Sambuc
query_responder(BIO * err,BIO * cbio,char * path,STACK_OF (CONF_VALUE)* headers,OCSP_REQUEST * req,int req_timeout)1201ebfedea0SLionel Sambuc static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
1202ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *headers,
1203ebfedea0SLionel Sambuc OCSP_REQUEST *req, int req_timeout)
1204ebfedea0SLionel Sambuc {
1205ebfedea0SLionel Sambuc int fd;
1206ebfedea0SLionel Sambuc int rv;
1207ebfedea0SLionel Sambuc int i;
1208ebfedea0SLionel Sambuc OCSP_REQ_CTX *ctx = NULL;
1209ebfedea0SLionel Sambuc OCSP_RESPONSE *rsp = NULL;
1210ebfedea0SLionel Sambuc fd_set confds;
1211ebfedea0SLionel Sambuc struct timeval tv;
1212ebfedea0SLionel Sambuc
1213ebfedea0SLionel Sambuc if (req_timeout != -1)
1214ebfedea0SLionel Sambuc BIO_set_nbio(cbio, 1);
1215ebfedea0SLionel Sambuc
1216ebfedea0SLionel Sambuc rv = BIO_do_connect(cbio);
1217ebfedea0SLionel Sambuc
1218*0a6a1f1dSLionel Sambuc if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1219ebfedea0SLionel Sambuc BIO_puts(err, "Error connecting BIO\n");
1220ebfedea0SLionel Sambuc return NULL;
1221ebfedea0SLionel Sambuc }
1222ebfedea0SLionel Sambuc
1223*0a6a1f1dSLionel Sambuc if (BIO_get_fd(cbio, &fd) <= 0) {
1224ebfedea0SLionel Sambuc BIO_puts(err, "Can't get connection fd\n");
1225ebfedea0SLionel Sambuc goto err;
1226ebfedea0SLionel Sambuc }
1227ebfedea0SLionel Sambuc
1228*0a6a1f1dSLionel Sambuc if (req_timeout != -1 && rv <= 0) {
1229ebfedea0SLionel Sambuc FD_ZERO(&confds);
1230ebfedea0SLionel Sambuc openssl_fdset(fd, &confds);
1231ebfedea0SLionel Sambuc tv.tv_usec = 0;
1232ebfedea0SLionel Sambuc tv.tv_sec = req_timeout;
1233ebfedea0SLionel Sambuc rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1234*0a6a1f1dSLionel Sambuc if (rv == 0) {
1235ebfedea0SLionel Sambuc BIO_puts(err, "Timeout on connect\n");
1236ebfedea0SLionel Sambuc return NULL;
1237ebfedea0SLionel Sambuc }
1238ebfedea0SLionel Sambuc }
1239ebfedea0SLionel Sambuc
1240ebfedea0SLionel Sambuc ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1241ebfedea0SLionel Sambuc if (!ctx)
1242ebfedea0SLionel Sambuc return NULL;
1243ebfedea0SLionel Sambuc
1244*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1245ebfedea0SLionel Sambuc CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1246ebfedea0SLionel Sambuc if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1247ebfedea0SLionel Sambuc goto err;
1248ebfedea0SLionel Sambuc }
1249ebfedea0SLionel Sambuc
1250ebfedea0SLionel Sambuc if (!OCSP_REQ_CTX_set1_req(ctx, req))
1251ebfedea0SLionel Sambuc goto err;
1252ebfedea0SLionel Sambuc
1253*0a6a1f1dSLionel Sambuc for (;;) {
1254ebfedea0SLionel Sambuc rv = OCSP_sendreq_nbio(&rsp, ctx);
1255ebfedea0SLionel Sambuc if (rv != -1)
1256ebfedea0SLionel Sambuc break;
1257ebfedea0SLionel Sambuc if (req_timeout == -1)
1258ebfedea0SLionel Sambuc continue;
1259ebfedea0SLionel Sambuc FD_ZERO(&confds);
1260ebfedea0SLionel Sambuc openssl_fdset(fd, &confds);
1261ebfedea0SLionel Sambuc tv.tv_usec = 0;
1262ebfedea0SLionel Sambuc tv.tv_sec = req_timeout;
1263ebfedea0SLionel Sambuc if (BIO_should_read(cbio))
1264ebfedea0SLionel Sambuc rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1265ebfedea0SLionel Sambuc else if (BIO_should_write(cbio))
1266ebfedea0SLionel Sambuc rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1267*0a6a1f1dSLionel Sambuc else {
1268ebfedea0SLionel Sambuc BIO_puts(err, "Unexpected retry condition\n");
1269ebfedea0SLionel Sambuc goto err;
1270ebfedea0SLionel Sambuc }
1271*0a6a1f1dSLionel Sambuc if (rv == 0) {
1272ebfedea0SLionel Sambuc BIO_puts(err, "Timeout on request\n");
1273ebfedea0SLionel Sambuc break;
1274ebfedea0SLionel Sambuc }
1275*0a6a1f1dSLionel Sambuc if (rv == -1) {
1276ebfedea0SLionel Sambuc BIO_puts(err, "Select error\n");
1277ebfedea0SLionel Sambuc break;
1278ebfedea0SLionel Sambuc }
1279ebfedea0SLionel Sambuc
1280ebfedea0SLionel Sambuc }
1281ebfedea0SLionel Sambuc err:
1282ebfedea0SLionel Sambuc if (ctx)
1283ebfedea0SLionel Sambuc OCSP_REQ_CTX_free(ctx);
1284ebfedea0SLionel Sambuc
1285ebfedea0SLionel Sambuc return rsp;
1286ebfedea0SLionel Sambuc }
1287ebfedea0SLionel Sambuc
process_responder(BIO * err,OCSP_REQUEST * req,char * host,char * path,char * port,int use_ssl,STACK_OF (CONF_VALUE)* headers,int req_timeout)1288ebfedea0SLionel Sambuc OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req,
1289*0a6a1f1dSLionel Sambuc char *host, char *path, char *port,
1290*0a6a1f1dSLionel Sambuc int use_ssl, STACK_OF(CONF_VALUE) *headers,
1291ebfedea0SLionel Sambuc int req_timeout)
1292ebfedea0SLionel Sambuc {
1293ebfedea0SLionel Sambuc BIO *cbio = NULL;
1294ebfedea0SLionel Sambuc SSL_CTX *ctx = NULL;
1295ebfedea0SLionel Sambuc OCSP_RESPONSE *resp = NULL;
1296ebfedea0SLionel Sambuc cbio = BIO_new_connect(host);
1297*0a6a1f1dSLionel Sambuc if (!cbio) {
1298ebfedea0SLionel Sambuc BIO_printf(err, "Error creating connect BIO\n");
1299ebfedea0SLionel Sambuc goto end;
1300ebfedea0SLionel Sambuc }
1301*0a6a1f1dSLionel Sambuc if (port)
1302*0a6a1f1dSLionel Sambuc BIO_set_conn_port(cbio, port);
1303*0a6a1f1dSLionel Sambuc if (use_ssl == 1) {
1304ebfedea0SLionel Sambuc BIO *sbio;
1305ebfedea0SLionel Sambuc ctx = SSL_CTX_new(SSLv23_client_method());
1306*0a6a1f1dSLionel Sambuc if (ctx == NULL) {
1307ebfedea0SLionel Sambuc BIO_printf(err, "Error creating SSL context.\n");
1308ebfedea0SLionel Sambuc goto end;
1309ebfedea0SLionel Sambuc }
1310ebfedea0SLionel Sambuc SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1311ebfedea0SLionel Sambuc sbio = BIO_new_ssl(ctx, 1);
1312ebfedea0SLionel Sambuc cbio = BIO_push(sbio, cbio);
1313ebfedea0SLionel Sambuc }
1314ebfedea0SLionel Sambuc resp = query_responder(err, cbio, path, headers, req, req_timeout);
1315ebfedea0SLionel Sambuc if (!resp)
1316*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Error querying OCSP responder\n");
1317ebfedea0SLionel Sambuc end:
1318ebfedea0SLionel Sambuc if (cbio)
1319ebfedea0SLionel Sambuc BIO_free_all(cbio);
1320ebfedea0SLionel Sambuc if (ctx)
1321ebfedea0SLionel Sambuc SSL_CTX_free(ctx);
1322ebfedea0SLionel Sambuc return resp;
1323ebfedea0SLionel Sambuc }
1324ebfedea0SLionel Sambuc
1325ebfedea0SLionel Sambuc #endif
1326