blob: 8f6084214dac4620eb336c80d56c43b565f7c139 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Rich Salz846e33c2016-05-17 14:18:30 -04002 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +00008 */
9
Matt Caswell3e41ac32016-03-21 16:54:53 +000010#include <openssl/opensslconf.h>
11
12#ifdef OPENSSL_NO_OCSP
13NON_EMPTY_TRANSLATION_UNIT
14#else
Matt Caswell0f113f32015-01-22 03:40:55 +000015# ifdef OPENSSL_SYS_VMS
16# define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
17 * on OpenVMS */
18# endif
Richard Levitte5871ddb2009-01-28 07:38:14 +000019
Matt Caswell0f113f32015-01-22 03:40:55 +000020# define USE_SOCKETS
Dr. Stephen Henson14023fe2009-04-03 11:45:19 +000021
Matt Caswell0f113f32015-01-22 03:40:55 +000022# include <stdio.h>
23# include <stdlib.h>
24# include <string.h>
25# include <time.h>
Rich Salz98cd49d2015-05-13 14:41:53 -040026# include <ctype.h>
Matt Caswell3e41ac32016-03-21 16:54:53 +000027
28/* Needs to be included before the openssl headers */
29# include "apps.h"
Matt Caswell0f113f32015-01-22 03:40:55 +000030# include <openssl/e_os2.h>
31# include <openssl/crypto.h>
32# include <openssl/err.h>
33# include <openssl/ssl.h>
34# include <openssl/evp.h>
35# include <openssl/bn.h>
36# include <openssl/x509v3.h>
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +000037
Matt Caswell0f113f32015-01-22 03:40:55 +000038# if defined(NETWARE_CLIB)
Dr. Stephen Hensoneef0c1f2008-01-03 22:43:04 +000039# ifdef NETWARE_BSDSOCK
Matt Caswell0f113f32015-01-22 03:40:55 +000040# include <sys/socket.h>
41# include <sys/bsdskt.h>
Dr. Stephen Hensoneef0c1f2008-01-03 22:43:04 +000042# else
Matt Caswell0f113f32015-01-22 03:40:55 +000043# include <novsock2.h>
Dr. Stephen Hensoneef0c1f2008-01-03 22:43:04 +000044# endif
Matt Caswell0f113f32015-01-22 03:40:55 +000045# elif defined(NETWARE_LIBC)
Dr. Stephen Hensoneef0c1f2008-01-03 22:43:04 +000046# ifdef NETWARE_BSDSOCK
Matt Caswell0f113f32015-01-22 03:40:55 +000047# include <sys/select.h>
Dr. Stephen Hensoneef0c1f2008-01-03 22:43:04 +000048# else
Matt Caswell0f113f32015-01-22 03:40:55 +000049# include <novsock2.h>
Dr. Stephen Hensoneef0c1f2008-01-03 22:43:04 +000050# endif
Matt Caswell0f113f32015-01-22 03:40:55 +000051# endif
52
Dr. Stephen Hensonf1965222001-02-24 13:50:06 +000053/* Maximum leeway in validity period: default 5 minutes */
Rich Salz7e1b7482015-04-24 15:26:15 -040054# define MAX_VALIDITY_PERIOD (5 * 60)
Dr. Stephen Hensonf1965222001-02-24 13:50:06 +000055
Matt Caswell0f113f32015-01-22 03:40:55 +000056static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
57 const EVP_MD *cert_id_md, X509 *issuer,
58 STACK_OF(OCSP_CERTID) *ids);
59static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
60 const EVP_MD *cert_id_md, X509 *issuer,
61 STACK_OF(OCSP_CERTID) *ids);
Rich Salz7e1b7482015-04-24 15:26:15 -040062static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
Matt Caswell0f113f32015-01-22 03:40:55 +000063 STACK_OF(OPENSSL_STRING) *names,
64 STACK_OF(OCSP_CERTID) *ids, long nsec,
65 long maxage);
Rich Salz7e1b7482015-04-24 15:26:15 -040066static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
Matt Caswell0f113f32015-01-22 03:40:55 +000067 CA_DB *db, X509 *ca, X509 *rcert,
68 EVP_PKEY *rkey, const EVP_MD *md,
69 STACK_OF(X509) *rother, unsigned long flags,
70 int nmin, int ndays, int badsig);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +000071
Richard Levittef85b68c2003-04-03 16:33:03 +000072static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
Ben Lauriec45a48c2013-10-07 12:41:43 +010073static BIO *init_responder(const char *port);
Rich Salza773b522016-02-13 22:33:56 -050074static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +000075static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
Matt Caswellf9e55032016-03-21 15:32:40 +000076
77# ifndef OPENSSL_NO_SOCK
Dr. Stephen Henson76e0cd12015-10-18 00:16:23 +010078static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
79 const char *path,
Matt Caswell0f113f32015-01-22 03:40:55 +000080 const STACK_OF(CONF_VALUE) *headers,
81 OCSP_REQUEST *req, int req_timeout);
Matt Caswellf9e55032016-03-21 15:32:40 +000082# endif
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +000083
Rich Salz7e1b7482015-04-24 15:26:15 -040084typedef enum OPTION_choice {
85 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
86 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
87 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
88 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
89 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
90 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
91 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
92 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
Matt Caswell2b6bcb72015-09-22 16:00:52 +010093 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH,
Rich Salz7e1b7482015-04-24 15:26:15 -040094 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
Rich Salzc6724062015-04-25 16:04:42 -040095 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
Rich Salz7e1b7482015-04-24 15:26:15 -040096 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
97 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER,
98 OPT_V_ENUM,
99 OPT_MD
100} OPTION_CHOICE;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000101
FdaSilvaYY44c83eb2016-03-13 14:07:50 +0100102const OPTIONS ocsp_options[] = {
Rich Salz7e1b7482015-04-24 15:26:15 -0400103 {"help", OPT_HELP, '-', "Display this summary"},
104 {"out", OPT_OUTFILE, '>', "Output filename"},
Rich Salz9a13bb32016-02-18 12:23:27 -0500105 {"timeout", OPT_TIMEOUT, 'p',
106 "Connection timeout (in seconds) to the OCSP responder"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400107 {"url", OPT_URL, 's', "Responder URL"},
FdaSilvaYYceab33e2016-07-05 21:22:18 +0200108 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400109 {"port", OPT_PORT, 'p', "Port to run responder on"},
FdaSilvaYY12d56b22016-07-31 19:02:50 +0200110 {"ignore_err", OPT_IGNORE_ERR, '-',
111 "Ignore Error response from OCSP responder, and retry "},
Rich Salz7e1b7482015-04-24 15:26:15 -0400112 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
113 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
114 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
115 {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
116 "Don't include any certificates in response"},
117 {"resp_key_id", OPT_RESP_KEY_ID, '-',
FdaSilvaYYceab33e2016-07-05 21:22:18 +0200118 "Identify response by signing certificate key ID"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400119 {"no_certs", OPT_NO_CERTS, '-',
120 "Don't include any certificates in signed request"},
121 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
122 "Don't check signature on response"},
123 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
124 "Don't check signing certificate"},
125 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
126 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
127 "Don't do additional checks on signing certificate"},
FdaSilvaYY12d56b22016-07-31 19:02:50 +0200128 {"no_explicit", OPT_NO_EXPLICIT, '-',
129 "Do not explicitly check the chain, just verify the root"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400130 {"trust_other", OPT_TRUST_OTHER, '-',
131 "Don't verify additional certificates"},
132 {"no_intern", OPT_NO_INTERN, '-',
133 "Don't search certificates contained in response for signer"},
FdaSilvaYY16e1b282016-03-20 21:14:10 +0100134 {"badsig", OPT_BADSIG, '-',
135 "Corrupt last byte of loaded OSCP response signature (for test)"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400136 {"text", OPT_TEXT, '-', "Print text form of request and response"},
137 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
138 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
139 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
140 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
141 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
142 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
143 {"sign_other", OPT_SIGN_OTHER, '<',
144 "Additional certificates to include in signed request"},
145 {"verify_other", OPT_VERIFY_OTHER, '<',
146 "Additional certificates to search for signer"},
147 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
148 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100149 {"no-CAfile", OPT_NOCAFILE, '-',
150 "Do not load the default certificates file"},
151 {"no-CApath", OPT_NOCAPATH, '-',
152 "Do not load certificates from the default certificates directory"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400153 {"validity_period", OPT_VALIDITY_PERIOD, 'u',
154 "Maximum validity discrepancy in seconds"},
155 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
156 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
157 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
158 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
159 {"path", OPT_PATH, 's', "Path to use in OCSP request"},
Rich Salzc6724062015-04-25 16:04:42 -0400160 {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400161 {"cert", OPT_CERT, '<', "Certificate to check"},
FdaSilvaYY16e1b282016-03-20 21:14:10 +0100162 {"serial", OPT_SERIAL, 's', "Serial number to check"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400163 {"index", OPT_INDEX, '<', "Certificate status index file"},
164 {"CA", OPT_CA, '<', "CA certificate"},
165 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
166 {"nrequest", OPT_REQUEST, 'p',
167 "Number of requests to accept (default unlimited)"},
168 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
169 {"rsigner", OPT_RSIGNER, '<',
FdaSilvaYY0ad69cd2016-06-14 23:02:16 +0200170 "Responder certificate to sign responses with"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400171 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
172 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
FdaSilvaYY16e1b282016-03-20 21:14:10 +0100173 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400174 {"header", OPT_HEADER, 's', "key=value header to add"},
FdaSilvaYY16e1b282016-03-20 21:14:10 +0100175 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400176 OPT_V_OPTIONS,
177 {NULL}
178};
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000179
Rich Salz7e1b7482015-04-24 15:26:15 -0400180int ocsp_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +0000181{
Rich Salz7e1b7482015-04-24 15:26:15 -0400182 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
183 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
Dr. Stephen Henson6302bbd2016-05-12 15:24:06 +0100184 int trailing_md = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400185 CA_DB *rdb = NULL;
186 EVP_PKEY *key = NULL, *rkey = NULL;
187 OCSP_BASICRESP *bs = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000188 OCSP_REQUEST *req = NULL;
189 OCSP_RESPONSE *resp = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400190 STACK_OF(CONF_VALUE) *headers = NULL;
191 STACK_OF(OCSP_CERTID) *ids = NULL;
192 STACK_OF(OPENSSL_STRING) *reqnames = NULL;
193 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
Rich Salz88806cf2015-04-26 13:12:04 -0400194 STACK_OF(X509) *issuers = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400195 X509 *issuer = NULL, *cert = NULL, *rca_cert = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000196 X509 *signer = NULL, *rsigner = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000197 X509_STORE *store = NULL;
198 X509_VERIFY_PARAM *vpm = NULL;
FdaSilvaYYcc696292016-08-04 23:52:22 +0200199 const char *CAfile = NULL, *CApath = NULL;
200 char *header, *value;
Rich Salz7e1b7482015-04-24 15:26:15 -0400201 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
202 char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
203 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
204 char *rsignfile = NULL, *rkeyfile = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400206 char *signfile = NULL, *keyfile = NULL;
207 char *thost = NULL, *tport = NULL, *tpath = NULL;
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100208 int noCAfile = 0, noCApath = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400209 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
210 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
Matt Caswellf9e55032016-03-21 15:32:40 +0000211 int req_text = 0, resp_text = 0, ret = 1;
212#ifndef OPENSSL_NO_SOCK
213 int req_timeout = -1;
214#endif
Rich Salz7e1b7482015-04-24 15:26:15 -0400215 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000216 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400217 OPTION_CHOICE o;
218 char *prog;
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000219
Matt Caswell0f113f32015-01-22 03:40:55 +0000220 reqnames = sk_OPENSSL_STRING_new_null();
Rich Salz7e1b7482015-04-24 15:26:15 -0400221 if (!reqnames)
222 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000223 ids = sk_OCSP_CERTID_new_null();
Rich Salz7e1b7482015-04-24 15:26:15 -0400224 if (!ids)
225 goto end;
226 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
227 return 1;
228
229 prog = opt_init(argc, argv, ocsp_options);
230 while ((o = opt_next()) != OPT_EOF) {
231 switch (o) {
232 case OPT_EOF:
233 case OPT_ERR:
234 opthelp:
235 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
236 goto end;
237 case OPT_HELP:
238 ret = 0;
239 opt_help(ocsp_options);
240 goto end;
241 case OPT_OUTFILE:
242 outfile = opt_arg();
243 break;
244 case OPT_TIMEOUT:
Matt Caswellf9e55032016-03-21 15:32:40 +0000245#ifndef OPENSSL_NO_SOCK
Rich Salz7e1b7482015-04-24 15:26:15 -0400246 req_timeout = atoi(opt_arg());
Matt Caswellf9e55032016-03-21 15:32:40 +0000247#endif
Rich Salz7e1b7482015-04-24 15:26:15 -0400248 break;
249 case OPT_URL:
Rich Salzb548a1f2015-05-01 10:02:07 -0400250 OPENSSL_free(thost);
251 OPENSSL_free(tport);
252 OPENSSL_free(tpath);
Rich Salz4b8d8e22015-06-13 09:29:10 -0400253 thost = tport = tpath = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400254 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
255 BIO_printf(bio_err, "%s Error parsing URL\n", prog);
Matt Caswell0f113f32015-01-22 03:40:55 +0000256 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000257 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400258 thost = host;
259 tport = port;
260 tpath = path;
261 break;
262 case OPT_HOST:
263 host = opt_arg();
264 break;
265 case OPT_PORT:
266 port = opt_arg();
267 break;
268 case OPT_IGNORE_ERR:
269 ignore_err = 1;
270 break;
271 case OPT_NOVERIFY:
272 noverify = 1;
273 break;
274 case OPT_NONCE:
275 add_nonce = 2;
276 break;
277 case OPT_NO_NONCE:
278 add_nonce = 0;
279 break;
280 case OPT_RESP_NO_CERTS:
281 rflags |= OCSP_NOCERTS;
282 break;
283 case OPT_RESP_KEY_ID:
284 rflags |= OCSP_RESPID_KEY;
285 break;
286 case OPT_NO_CERTS:
287 sign_flags |= OCSP_NOCERTS;
288 break;
289 case OPT_NO_SIGNATURE_VERIFY:
290 verify_flags |= OCSP_NOSIGS;
291 break;
292 case OPT_NO_CERT_VERIFY:
293 verify_flags |= OCSP_NOVERIFY;
294 break;
295 case OPT_NO_CHAIN:
296 verify_flags |= OCSP_NOCHAIN;
297 break;
298 case OPT_NO_CERT_CHECKS:
299 verify_flags |= OCSP_NOCHECKS;
300 break;
301 case OPT_NO_EXPLICIT:
302 verify_flags |= OCSP_NOEXPLICIT;
303 break;
304 case OPT_TRUST_OTHER:
305 verify_flags |= OCSP_TRUSTOTHER;
306 break;
307 case OPT_NO_INTERN:
308 verify_flags |= OCSP_NOINTERN;
309 break;
310 case OPT_BADSIG:
311 badsig = 1;
312 break;
313 case OPT_TEXT:
314 req_text = resp_text = 1;
315 break;
316 case OPT_REQ_TEXT:
317 req_text = 1;
318 break;
319 case OPT_RESP_TEXT:
320 resp_text = 1;
321 break;
322 case OPT_REQIN:
323 reqin = opt_arg();
324 break;
325 case OPT_RESPIN:
326 respin = opt_arg();
327 break;
328 case OPT_SIGNER:
329 signfile = opt_arg();
330 break;
331 case OPT_VAFILE:
332 verify_certfile = opt_arg();
333 verify_flags |= OCSP_TRUSTOTHER;
334 break;
335 case OPT_SIGN_OTHER:
336 sign_certfile = opt_arg();
337 break;
338 case OPT_VERIFY_OTHER:
339 verify_certfile = opt_arg();
340 break;
341 case OPT_CAFILE:
342 CAfile = opt_arg();
343 break;
344 case OPT_CAPATH:
345 CApath = opt_arg();
346 break;
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100347 case OPT_NOCAFILE:
348 noCAfile = 1;
349 break;
350 case OPT_NOCAPATH:
351 noCApath = 1;
352 break;
Rich Salz7e1b7482015-04-24 15:26:15 -0400353 case OPT_V_CASES:
354 if (!opt_verify(o, vpm))
355 goto end;
356 vpmtouched++;
357 break;
358 case OPT_VALIDITY_PERIOD:
359 opt_long(opt_arg(), &nsec);
360 break;
361 case OPT_STATUS_AGE:
362 opt_long(opt_arg(), &maxage);
363 break;
364 case OPT_SIGNKEY:
365 keyfile = opt_arg();
366 break;
367 case OPT_REQOUT:
368 reqout = opt_arg();
369 break;
370 case OPT_RESPOUT:
371 respout = opt_arg();
372 break;
373 case OPT_PATH:
374 path = opt_arg();
375 break;
Rich Salzc6724062015-04-25 16:04:42 -0400376 case OPT_ISSUER:
Rich Salza773b522016-02-13 22:33:56 -0500377 issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate");
Rich Salzc6724062015-04-25 16:04:42 -0400378 if (issuer == NULL)
379 goto end;
Dr. Stephen Hensonbb7fc982015-10-18 00:04:10 +0100380 if (issuers == NULL) {
381 if ((issuers = sk_X509_new_null()) == NULL)
382 goto end;
383 }
Rich Salzc6724062015-04-25 16:04:42 -0400384 sk_X509_push(issuers, issuer);
385 break;
Rich Salz7e1b7482015-04-24 15:26:15 -0400386 case OPT_CERT:
387 X509_free(cert);
Rich Salza773b522016-02-13 22:33:56 -0500388 cert = load_cert(opt_arg(), FORMAT_PEM, "certificate");
Rich Salz7e1b7482015-04-24 15:26:15 -0400389 if (cert == NULL)
390 goto end;
391 if (cert_id_md == NULL)
392 cert_id_md = EVP_sha1();
393 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
394 goto end;
395 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
396 goto end;
Dr. Stephen Henson6302bbd2016-05-12 15:24:06 +0100397 trailing_md = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400398 break;
399 case OPT_SERIAL:
400 if (cert_id_md == NULL)
401 cert_id_md = EVP_sha1();
402 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
403 goto end;
404 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
405 goto end;
Dr. Stephen Henson6302bbd2016-05-12 15:24:06 +0100406 trailing_md = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400407 break;
408 case OPT_INDEX:
409 ridx_filename = opt_arg();
410 break;
411 case OPT_CA:
412 rca_filename = opt_arg();
413 break;
414 case OPT_NMIN:
415 opt_int(opt_arg(), &nmin);
Matt Caswell0f113f32015-01-22 03:40:55 +0000416 if (ndays == -1)
417 ndays = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400418 break;
419 case OPT_REQUEST:
420 opt_int(opt_arg(), &accept_count);
421 break;
422 case OPT_NDAYS:
423 ndays = atoi(opt_arg());
424 break;
425 case OPT_RSIGNER:
426 rsignfile = opt_arg();
427 break;
428 case OPT_RKEY:
429 rkeyfile = opt_arg();
430 break;
431 case OPT_ROTHER:
432 rcertfile = opt_arg();
433 break;
FdaSilvaYY16e1b282016-03-20 21:14:10 +0100434 case OPT_RMD: /* Response MessageDigest */
Rich Salz7e1b7482015-04-24 15:26:15 -0400435 if (!opt_md(opt_arg(), &rsign_md))
436 goto end;
437 break;
438 case OPT_HEADER:
439 header = opt_arg();
440 value = strchr(header, '=');
441 if (value == NULL) {
442 BIO_printf(bio_err, "Missing = in header key=value\n");
443 goto opthelp;
444 }
445 *value++ = '\0';
446 if (!X509V3_add_value(header, value, &headers))
447 goto end;
448 break;
449 case OPT_MD:
Dr. Stephen Henson6302bbd2016-05-12 15:24:06 +0100450 if (trailing_md) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400451 BIO_printf(bio_err,
452 "%s: Digest must be before -cert or -serial\n",
453 prog);
454 goto opthelp;
455 }
456 if (!opt_md(opt_unknown(), &cert_id_md))
457 goto opthelp;
Dr. Stephen Henson6302bbd2016-05-12 15:24:06 +0100458 trailing_md = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400459 break;
Matt Caswell0f113f32015-01-22 03:40:55 +0000460 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000461 }
Dr. Stephen Henson6302bbd2016-05-12 15:24:06 +0100462
463 if (trailing_md) {
464 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
465 prog);
466 goto opthelp;
467 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400468 argc = opt_num_rest();
Kurt Roeckx03358512016-02-14 20:45:02 +0100469 if (argc != 0)
470 goto opthelp;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000471
Matt Caswell0f113f32015-01-22 03:40:55 +0000472 /* Have we anything to do? */
473 if (!req && !reqin && !respin && !(port && ridx_filename))
Rich Salz7e1b7482015-04-24 15:26:15 -0400474 goto opthelp;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000475
Richard Levittebdd58d92015-09-04 12:49:06 +0200476 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
Rich Salz7e1b7482015-04-24 15:26:15 -0400477 if (out == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000478 goto end;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000479
Matt Caswell0f113f32015-01-22 03:40:55 +0000480 if (!req && (add_nonce != 2))
481 add_nonce = 0;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000482
Matt Caswell0f113f32015-01-22 03:40:55 +0000483 if (!req && reqin) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200484 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
Rich Salz7e1b7482015-04-24 15:26:15 -0400485 if (derbio == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000486 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000487 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
488 BIO_free(derbio);
489 if (!req) {
490 BIO_printf(bio_err, "Error reading OCSP request\n");
491 goto end;
492 }
493 }
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000494
Matt Caswell0f113f32015-01-22 03:40:55 +0000495 if (!req && port) {
496 acbio = init_responder(port);
497 if (!acbio)
498 goto end;
499 }
Dr. Stephen Henson534a1ed2001-07-13 13:13:44 +0000500
Benjamin Kaduk21c6c502016-02-09 20:29:21 -0600501 if (rsignfile) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000502 if (!rkeyfile)
503 rkeyfile = rsignfile;
Rich Salza773b522016-02-13 22:33:56 -0500504 rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate");
Matt Caswell0f113f32015-01-22 03:40:55 +0000505 if (!rsigner) {
506 BIO_printf(bio_err, "Error loading responder certificate\n");
507 goto end;
508 }
Rich Salza773b522016-02-13 22:33:56 -0500509 rca_cert = load_cert(rca_filename, FORMAT_PEM, "CA certificate");
Matt Caswell0f113f32015-01-22 03:40:55 +0000510 if (rcertfile) {
Rich Salza773b522016-02-13 22:33:56 -0500511 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
Viktor Dukhovni0996dc52016-01-16 00:08:38 -0500512 "responder other certificates"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000513 goto end;
514 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400515 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
Matt Caswell0f113f32015-01-22 03:40:55 +0000516 "responder private key");
517 if (!rkey)
518 goto end;
519 }
520 if (acbio)
521 BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
Dr. Stephen Hensonb439a742001-08-23 23:54:11 +0000522
Matt Caswell0f113f32015-01-22 03:40:55 +0000523 redo_accept:
Dr. Stephen Henson534a1ed2001-07-13 13:13:44 +0000524
Matt Caswell0f113f32015-01-22 03:40:55 +0000525 if (acbio) {
Rich Salza773b522016-02-13 22:33:56 -0500526 if (!do_responder(&req, &cbio, acbio))
Matt Caswell0f113f32015-01-22 03:40:55 +0000527 goto end;
528 if (!req) {
529 resp =
530 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
531 NULL);
532 send_ocsp_response(cbio, resp);
533 goto done_resp;
534 }
535 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000536
Matt Caswell0f113f32015-01-22 03:40:55 +0000537 if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
538 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
539 goto end;
540 }
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000541
Matt Caswell0f113f32015-01-22 03:40:55 +0000542 if (req && add_nonce)
543 OCSP_request_add1_nonce(req, NULL, -1);
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000544
Matt Caswell0f113f32015-01-22 03:40:55 +0000545 if (signfile) {
546 if (!keyfile)
547 keyfile = signfile;
Rich Salza773b522016-02-13 22:33:56 -0500548 signer = load_cert(signfile, FORMAT_PEM, "signer certificate");
Matt Caswell0f113f32015-01-22 03:40:55 +0000549 if (!signer) {
550 BIO_printf(bio_err, "Error loading signer certificate\n");
551 goto end;
552 }
553 if (sign_certfile) {
Rich Salza773b522016-02-13 22:33:56 -0500554 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL,
Viktor Dukhovni0996dc52016-01-16 00:08:38 -0500555 "signer certificates"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000556 goto end;
557 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400558 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
Matt Caswell0f113f32015-01-22 03:40:55 +0000559 "signer private key");
560 if (!key)
561 goto end;
Dr. Stephen Hensoncec25382007-12-04 12:41:28 +0000562
Matt Caswell0f113f32015-01-22 03:40:55 +0000563 if (!OCSP_request_sign
564 (req, signer, key, NULL, sign_other, sign_flags)) {
565 BIO_printf(bio_err, "Error signing OCSP request\n");
566 goto end;
567 }
568 }
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000569
Matt Caswell0f113f32015-01-22 03:40:55 +0000570 if (req_text && req)
571 OCSP_REQUEST_print(out, req, 0);
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000572
Matt Caswell0f113f32015-01-22 03:40:55 +0000573 if (reqout) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200574 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
Rich Salz7e1b7482015-04-24 15:26:15 -0400575 if (derbio == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000576 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000577 i2d_OCSP_REQUEST_bio(derbio, req);
578 BIO_free(derbio);
579 }
Dr. Stephen Henson99889b42002-06-13 12:56:27 +0000580
Matt Caswell0f113f32015-01-22 03:40:55 +0000581 if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
582 BIO_printf(bio_err,
583 "Need a responder certificate, key and CA for this operation!\n");
584 goto end;
585 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000586
Matt Caswell0f113f32015-01-22 03:40:55 +0000587 if (ridx_filename && !rdb) {
588 rdb = load_index(ridx_filename, NULL);
589 if (!rdb)
590 goto end;
591 if (!index_index(rdb))
592 goto end;
593 }
Dr. Stephen Henson534a1ed2001-07-13 13:13:44 +0000594
Matt Caswell0f113f32015-01-22 03:40:55 +0000595 if (rdb) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400596 make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000597 rsign_md, rother, rflags, nmin, ndays, badsig);
598 if (cbio)
599 send_ocsp_response(cbio, resp);
600 } else if (host) {
601# ifndef OPENSSL_NO_SOCK
Rich Salz7e1b7482015-04-24 15:26:15 -0400602 resp = process_responder(req, host, path,
Matt Caswell0f113f32015-01-22 03:40:55 +0000603 port, use_ssl, headers, req_timeout);
604 if (!resp)
605 goto end;
606# else
607 BIO_printf(bio_err,
608 "Error creating connect BIO - sockets not supported.\n");
609 goto end;
610# endif
611 } else if (respin) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200612 derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
Rich Salz7e1b7482015-04-24 15:26:15 -0400613 if (derbio == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000614 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000615 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
616 BIO_free(derbio);
617 if (!resp) {
618 BIO_printf(bio_err, "Error reading OCSP response\n");
619 goto end;
620 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000621 } else {
622 ret = 0;
623 goto end;
624 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000625
Matt Caswell0f113f32015-01-22 03:40:55 +0000626 done_resp:
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000627
Matt Caswell0f113f32015-01-22 03:40:55 +0000628 if (respout) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200629 derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
Rich Salz7e1b7482015-04-24 15:26:15 -0400630 if (derbio == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000631 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000632 i2d_OCSP_RESPONSE_bio(derbio, resp);
633 BIO_free(derbio);
634 }
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000635
Matt Caswell0f113f32015-01-22 03:40:55 +0000636 i = OCSP_response_status(resp);
Matt Caswell0f113f32015-01-22 03:40:55 +0000637 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
638 BIO_printf(out, "Responder Error: %s (%d)\n",
639 OCSP_response_status_str(i), i);
640 if (ignore_err)
641 goto redo_accept;
642 ret = 0;
643 goto end;
644 }
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000645
Matt Caswell0f113f32015-01-22 03:40:55 +0000646 if (resp_text)
647 OCSP_RESPONSE_print(out, resp, 0);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000648
Matt Caswell0f113f32015-01-22 03:40:55 +0000649 /* If running as responder don't verify our own response */
650 if (cbio) {
Adam Eijdenberge46bcca2015-07-29 21:38:22 -0400651 /* If not unlimited, see if we took all we should. */
652 if (accept_count != -1 && --accept_count <= 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400653 ret = 0;
654 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000655 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400656 BIO_free_all(cbio);
657 cbio = NULL;
658 OCSP_REQUEST_free(req);
659 req = NULL;
660 OCSP_RESPONSE_free(resp);
661 resp = NULL;
662 goto redo_accept;
663 }
664 if (ridx_filename) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000665 ret = 0;
666 goto end;
667 }
Richard Levitte9235adb2001-02-08 17:59:29 +0000668
Rich Salz7e1b7482015-04-24 15:26:15 -0400669 if (!store) {
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100670 store = setup_verify(CAfile, CApath, noCAfile, noCApath);
Rich Salz7e1b7482015-04-24 15:26:15 -0400671 if (!store)
672 goto end;
673 }
674 if (vpmtouched)
Matt Caswell0f113f32015-01-22 03:40:55 +0000675 X509_STORE_set1_param(store, vpm);
676 if (verify_certfile) {
Rich Salza773b522016-02-13 22:33:56 -0500677 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL,
Viktor Dukhovni0996dc52016-01-16 00:08:38 -0500678 "validator certificate"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000679 goto end;
680 }
Dr. Stephen Henson81f169e2001-01-17 01:31:34 +0000681
Matt Caswell0f113f32015-01-22 03:40:55 +0000682 bs = OCSP_response_get1_basic(resp);
Matt Caswell0f113f32015-01-22 03:40:55 +0000683 if (!bs) {
684 BIO_printf(bio_err, "Error parsing response\n");
685 goto end;
686 }
Ben Laurie30c278a2012-12-07 18:47:47 +0000687
Matt Caswell0f113f32015-01-22 03:40:55 +0000688 ret = 0;
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000689
Matt Caswell0f113f32015-01-22 03:40:55 +0000690 if (!noverify) {
691 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
692 if (i == -1)
693 BIO_printf(bio_err, "WARNING: no nonce in response\n");
694 else {
695 BIO_printf(bio_err, "Nonce Verify error\n");
696 ret = 1;
697 goto end;
698 }
699 }
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000700
Matt Caswell0f113f32015-01-22 03:40:55 +0000701 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
Rich Salzc6724062015-04-25 16:04:42 -0400702 if (i <= 0 && issuers) {
703 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
704 if (i > 0)
705 ERR_clear_error();
706 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000707 if (i <= 0) {
708 BIO_printf(bio_err, "Response Verify Failure\n");
709 ERR_print_errors(bio_err);
710 ret = 1;
711 } else
712 BIO_printf(bio_err, "Response verify OK\n");
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000713
Matt Caswell0f113f32015-01-22 03:40:55 +0000714 }
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000715
Rich Salz7e1b7482015-04-24 15:26:15 -0400716 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000717
Matt Caswell0f113f32015-01-22 03:40:55 +0000718 end:
719 ERR_print_errors(bio_err);
720 X509_free(signer);
721 X509_STORE_free(store);
Rich Salz222561f2015-04-30 17:33:59 -0400722 X509_VERIFY_PARAM_free(vpm);
Matt Caswell0f113f32015-01-22 03:40:55 +0000723 EVP_PKEY_free(key);
724 EVP_PKEY_free(rkey);
Matt Caswell0f113f32015-01-22 03:40:55 +0000725 X509_free(cert);
Dr. Stephen Hensonbb7fc982015-10-18 00:04:10 +0100726 sk_X509_pop_free(issuers, X509_free);
Matt Caswell0f113f32015-01-22 03:40:55 +0000727 X509_free(rsigner);
728 X509_free(rca_cert);
729 free_index(rdb);
730 BIO_free_all(cbio);
731 BIO_free_all(acbio);
732 BIO_free(out);
733 OCSP_REQUEST_free(req);
734 OCSP_RESPONSE_free(resp);
735 OCSP_BASICRESP_free(bs);
736 sk_OPENSSL_STRING_free(reqnames);
737 sk_OCSP_CERTID_free(ids);
738 sk_X509_pop_free(sign_other, X509_free);
739 sk_X509_pop_free(verify_other, X509_free);
740 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
Rich Salzb548a1f2015-05-01 10:02:07 -0400741 OPENSSL_free(thost);
742 OPENSSL_free(tport);
743 OPENSSL_free(tpath);
Matt Caswell0f113f32015-01-22 03:40:55 +0000744
Rich Salz7e1b7482015-04-24 15:26:15 -0400745 return (ret);
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000746}
747
Matt Caswell0f113f32015-01-22 03:40:55 +0000748static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
749 const EVP_MD *cert_id_md, X509 *issuer,
750 STACK_OF(OCSP_CERTID) *ids)
751{
752 OCSP_CERTID *id;
753 if (!issuer) {
754 BIO_printf(bio_err, "No issuer certificate specified\n");
755 return 0;
756 }
Matt Caswell96487cd2015-10-30 11:18:04 +0000757 if (*req == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000758 *req = OCSP_REQUEST_new();
Matt Caswell96487cd2015-10-30 11:18:04 +0000759 if (*req == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000760 goto err;
761 id = OCSP_cert_to_id(cert_id_md, cert, issuer);
762 if (!id || !sk_OCSP_CERTID_push(ids, id))
763 goto err;
764 if (!OCSP_request_add0_id(*req, id))
765 goto err;
766 return 1;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000767
Matt Caswell0f113f32015-01-22 03:40:55 +0000768 err:
769 BIO_printf(bio_err, "Error Creating OCSP request\n");
770 return 0;
771}
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000772
Matt Caswell0f113f32015-01-22 03:40:55 +0000773static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
774 const EVP_MD *cert_id_md, X509 *issuer,
775 STACK_OF(OCSP_CERTID) *ids)
776{
777 OCSP_CERTID *id;
778 X509_NAME *iname;
779 ASN1_BIT_STRING *ikey;
780 ASN1_INTEGER *sno;
781 if (!issuer) {
782 BIO_printf(bio_err, "No issuer certificate specified\n");
783 return 0;
784 }
Matt Caswell96487cd2015-10-30 11:18:04 +0000785 if (*req == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000786 *req = OCSP_REQUEST_new();
Matt Caswell96487cd2015-10-30 11:18:04 +0000787 if (*req == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000788 goto err;
789 iname = X509_get_subject_name(issuer);
790 ikey = X509_get0_pubkey_bitstr(issuer);
791 sno = s2i_ASN1_INTEGER(NULL, serial);
792 if (!sno) {
793 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
794 return 0;
795 }
796 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
797 ASN1_INTEGER_free(sno);
Matt Caswell96487cd2015-10-30 11:18:04 +0000798 if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
Matt Caswell0f113f32015-01-22 03:40:55 +0000799 goto err;
800 if (!OCSP_request_add0_id(*req, id))
801 goto err;
802 return 1;
Dr. Stephen Henson5782ceb2001-01-13 01:48:38 +0000803
Matt Caswell0f113f32015-01-22 03:40:55 +0000804 err:
805 BIO_printf(bio_err, "Error Creating OCSP request\n");
806 return 0;
807}
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000808
Rich Salz7e1b7482015-04-24 15:26:15 -0400809static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
Matt Caswell0f113f32015-01-22 03:40:55 +0000810 STACK_OF(OPENSSL_STRING) *names,
811 STACK_OF(OCSP_CERTID) *ids, long nsec,
812 long maxage)
813{
814 OCSP_CERTID *id;
FdaSilvaYYcc696292016-08-04 23:52:22 +0200815 const char *name;
Rich Salz7e1b7482015-04-24 15:26:15 -0400816 int i, status, reason;
Matt Caswell0f113f32015-01-22 03:40:55 +0000817 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000818
Matt Caswell0f113f32015-01-22 03:40:55 +0000819 if (!bs || !req || !sk_OPENSSL_STRING_num(names)
820 || !sk_OCSP_CERTID_num(ids))
Rich Salz7e1b7482015-04-24 15:26:15 -0400821 return;
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000822
Matt Caswell0f113f32015-01-22 03:40:55 +0000823 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
824 id = sk_OCSP_CERTID_value(ids, i);
825 name = sk_OPENSSL_STRING_value(names, i);
826 BIO_printf(out, "%s: ", name);
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000827
Matt Caswell0f113f32015-01-22 03:40:55 +0000828 if (!OCSP_resp_find_status(bs, id, &status, &reason,
829 &rev, &thisupd, &nextupd)) {
830 BIO_puts(out, "ERROR: No Status found.\n");
831 continue;
832 }
Dr. Stephen Hensonf1965222001-02-24 13:50:06 +0000833
Matt Caswell0f113f32015-01-22 03:40:55 +0000834 /*
835 * Check validity: if invalid write to output BIO so we know which
836 * response this refers to.
837 */
838 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
839 BIO_puts(out, "WARNING: Status times invalid.\n");
840 ERR_print_errors(out);
841 }
842 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000843
Matt Caswell0f113f32015-01-22 03:40:55 +0000844 BIO_puts(out, "\tThis Update: ");
845 ASN1_GENERALIZEDTIME_print(out, thisupd);
846 BIO_puts(out, "\n");
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000847
Matt Caswell0f113f32015-01-22 03:40:55 +0000848 if (nextupd) {
849 BIO_puts(out, "\tNext Update: ");
850 ASN1_GENERALIZEDTIME_print(out, nextupd);
851 BIO_puts(out, "\n");
852 }
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000853
Matt Caswell0f113f32015-01-22 03:40:55 +0000854 if (status != V_OCSP_CERTSTATUS_REVOKED)
855 continue;
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000856
Matt Caswell0f113f32015-01-22 03:40:55 +0000857 if (reason != -1)
858 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000859
Matt Caswell0f113f32015-01-22 03:40:55 +0000860 BIO_puts(out, "\tRevocation Time: ");
861 ASN1_GENERALIZEDTIME_print(out, rev);
862 BIO_puts(out, "\n");
863 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000864}
Dr. Stephen Henson73758d42001-01-19 01:32:23 +0000865
Rich Salz7e1b7482015-04-24 15:26:15 -0400866static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req,
Matt Caswell0f113f32015-01-22 03:40:55 +0000867 CA_DB *db, X509 *ca, X509 *rcert,
868 EVP_PKEY *rkey, const EVP_MD *rmd,
869 STACK_OF(X509) *rother, unsigned long flags,
870 int nmin, int ndays, int badsig)
871{
872 ASN1_TIME *thisupd = NULL, *nextupd = NULL;
873 OCSP_CERTID *cid, *ca_id = NULL;
874 OCSP_BASICRESP *bs = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400875 int i, id_count;
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000876
Matt Caswell0f113f32015-01-22 03:40:55 +0000877 id_count = OCSP_request_onereq_count(req);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000878
Matt Caswell0f113f32015-01-22 03:40:55 +0000879 if (id_count <= 0) {
880 *resp =
881 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
882 goto end;
883 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000884
Matt Caswell0f113f32015-01-22 03:40:55 +0000885 bs = OCSP_BASICRESP_new();
886 thisupd = X509_gmtime_adj(NULL, 0);
887 if (ndays != -1)
Dr. Stephen Henson9aa00b12016-01-14 00:25:25 +0000888 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000889
Matt Caswell0f113f32015-01-22 03:40:55 +0000890 /* Examine each certificate id in the request */
891 for (i = 0; i < id_count; i++) {
892 OCSP_ONEREQ *one;
893 ASN1_INTEGER *serial;
894 char **inf;
895 ASN1_OBJECT *cert_id_md_oid;
896 const EVP_MD *cert_id_md;
897 one = OCSP_request_onereq_get0(req, i);
898 cid = OCSP_onereq_get0_id(one);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000899
Matt Caswell0f113f32015-01-22 03:40:55 +0000900 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000901
Matt Caswell0f113f32015-01-22 03:40:55 +0000902 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
903 if (!cert_id_md) {
904 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
905 NULL);
906 goto end;
907 }
Rich Salz25aaa982015-05-01 14:37:16 -0400908 OCSP_CERTID_free(ca_id);
Matt Caswell0f113f32015-01-22 03:40:55 +0000909 ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
Dr. Stephen Henson341e18b2007-12-14 12:43:50 +0000910
Matt Caswell0f113f32015-01-22 03:40:55 +0000911 /* Is this request about our CA? */
912 if (OCSP_id_issuer_cmp(ca_id, cid)) {
913 OCSP_basic_add1_status(bs, cid,
914 V_OCSP_CERTSTATUS_UNKNOWN,
915 0, NULL, thisupd, nextupd);
916 continue;
917 }
918 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
919 inf = lookup_serial(db, serial);
920 if (!inf)
921 OCSP_basic_add1_status(bs, cid,
922 V_OCSP_CERTSTATUS_UNKNOWN,
923 0, NULL, thisupd, nextupd);
924 else if (inf[DB_type][0] == DB_TYPE_VAL)
925 OCSP_basic_add1_status(bs, cid,
926 V_OCSP_CERTSTATUS_GOOD,
927 0, NULL, thisupd, nextupd);
928 else if (inf[DB_type][0] == DB_TYPE_REV) {
929 ASN1_OBJECT *inst = NULL;
930 ASN1_TIME *revtm = NULL;
931 ASN1_GENERALIZEDTIME *invtm = NULL;
932 OCSP_SINGLERESP *single;
933 int reason = -1;
934 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
935 single = OCSP_basic_add1_status(bs, cid,
936 V_OCSP_CERTSTATUS_REVOKED,
937 reason, revtm, thisupd, nextupd);
938 if (invtm)
939 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
940 invtm, 0, 0);
941 else if (inst)
942 OCSP_SINGLERESP_add1_ext_i2d(single,
943 NID_hold_instruction_code, inst,
944 0, 0);
945 ASN1_OBJECT_free(inst);
946 ASN1_TIME_free(revtm);
947 ASN1_GENERALIZEDTIME_free(invtm);
948 }
949 }
Dr. Stephen Henson341e18b2007-12-14 12:43:50 +0000950
Matt Caswell0f113f32015-01-22 03:40:55 +0000951 OCSP_copy_nonce(bs, req);
Dr. Stephen Henson341e18b2007-12-14 12:43:50 +0000952
Matt Caswell0f113f32015-01-22 03:40:55 +0000953 OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000954
Dr. Stephen Henson6ef869d2015-03-05 13:41:11 +0000955 if (badsig) {
Dr. Stephen Hensona0754082016-08-17 12:34:22 +0100956 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
957 corrupt_signature(sig);
Dr. Stephen Henson6ef869d2015-03-05 13:41:11 +0000958 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000959
Matt Caswell0f113f32015-01-22 03:40:55 +0000960 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
Dr. Stephen Henson1e8b9e72012-12-09 16:21:46 +0000961
Matt Caswell0f113f32015-01-22 03:40:55 +0000962 end:
963 ASN1_TIME_free(thisupd);
964 ASN1_TIME_free(nextupd);
965 OCSP_CERTID_free(ca_id);
966 OCSP_BASICRESP_free(bs);
Matt Caswell0f113f32015-01-22 03:40:55 +0000967}
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000968
Richard Levittef85b68c2003-04-03 16:33:03 +0000969static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
Matt Caswell0f113f32015-01-22 03:40:55 +0000970{
971 int i;
972 BIGNUM *bn = NULL;
973 char *itmp, *row[DB_NUMBER], **rrow;
974 for (i = 0; i < DB_NUMBER; i++)
975 row[i] = NULL;
976 bn = ASN1_INTEGER_to_BN(ser, NULL);
977 OPENSSL_assert(bn); /* FIXME: should report an error at this
978 * point and abort */
979 if (BN_is_zero(bn))
Rich Salz7644a9a2015-12-16 16:12:24 -0500980 itmp = OPENSSL_strdup("00");
Matt Caswell0f113f32015-01-22 03:40:55 +0000981 else
982 itmp = BN_bn2hex(bn);
983 row[DB_serial] = itmp;
984 BN_free(bn);
985 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
986 OPENSSL_free(itmp);
987 return rrow;
988}
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +0000989
990/* Quick and dirty OCSP server: read in and parse input request */
991
Ben Lauriec45a48c2013-10-07 12:41:43 +0100992static BIO *init_responder(const char *port)
Matt Caswell0f113f32015-01-22 03:40:55 +0000993{
Rich Salz366e2a62015-05-02 10:44:31 -0400994# ifdef OPENSSL_NO_SOCK
995 BIO_printf(bio_err,
996 "Error setting up accept BIO - sockets not supported.\n");
997 return NULL;
Matt Caswellf863ad02016-04-20 12:56:37 +0100998# else
999 BIO *acbio = NULL, *bufbio = NULL;
1000
Matt Caswell0f113f32015-01-22 03:40:55 +00001001 bufbio = BIO_new(BIO_f_buffer());
Matt Caswell96487cd2015-10-30 11:18:04 +00001002 if (bufbio == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00001003 goto err;
Rich Salz366e2a62015-05-02 10:44:31 -04001004 acbio = BIO_new(BIO_s_accept());
1005 if (acbio == NULL
1006 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
1007 || BIO_set_accept_port(acbio, port) < 0) {
1008 BIO_printf(bio_err, "Error setting up accept BIO\n");
1009 ERR_print_errors(bio_err);
Matt Caswell0f113f32015-01-22 03:40:55 +00001010 goto err;
Rich Salz366e2a62015-05-02 10:44:31 -04001011 }
1012
Matt Caswell0f113f32015-01-22 03:40:55 +00001013 BIO_set_accept_bios(acbio, bufbio);
1014 bufbio = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001015 if (BIO_do_accept(acbio) <= 0) {
Rich Salz366e2a62015-05-02 10:44:31 -04001016 BIO_printf(bio_err, "Error starting accept\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001017 ERR_print_errors(bio_err);
1018 goto err;
1019 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001020
Matt Caswell0f113f32015-01-22 03:40:55 +00001021 return acbio;
Dr. Stephen Henson534a1ed2001-07-13 13:13:44 +00001022
Matt Caswell0f113f32015-01-22 03:40:55 +00001023 err:
1024 BIO_free_all(acbio);
1025 BIO_free(bufbio);
1026 return NULL;
Matt Caswellf863ad02016-04-20 12:56:37 +01001027# endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001028}
Dr. Stephen Henson534a1ed2001-07-13 13:13:44 +00001029
Matt Caswellf863ad02016-04-20 12:56:37 +01001030# ifndef OPENSSL_NO_SOCK
Rich Salzfc3cec52015-05-01 23:36:11 -04001031/*
1032 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
1033 */
1034static int urldecode(char *p)
Rich Salz995101d2015-04-29 17:37:04 -04001035{
1036 unsigned char *out = (unsigned char *)p;
Rich Salzfc3cec52015-05-01 23:36:11 -04001037 unsigned char *save = out;
Rich Salz995101d2015-04-29 17:37:04 -04001038
1039 for (; *p; p++) {
1040 if (*p != '%')
1041 *out++ = *p;
Richard Levitte18295f02016-02-14 13:02:15 +01001042 else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
Rich Salz14f051a2016-04-13 15:58:28 -04001043 /* Don't check, can't fail because of ixdigit() call. */
1044 *out++ = (OPENSSL_hexchar2int(p[1]) << 4)
1045 | OPENSSL_hexchar2int(p[2]);
Rich Salz995101d2015-04-29 17:37:04 -04001046 p += 2;
1047 }
Rich Salzfc3cec52015-05-01 23:36:11 -04001048 else
1049 return -1;
Rich Salz995101d2015-04-29 17:37:04 -04001050 }
Rich Salzfc3cec52015-05-01 23:36:11 -04001051 *out = '\0';
1052 return (int)(out - save);
Rich Salz995101d2015-04-29 17:37:04 -04001053}
Matt Caswellf863ad02016-04-20 12:56:37 +01001054# endif
Rich Salz995101d2015-04-29 17:37:04 -04001055
Rich Salza773b522016-02-13 22:33:56 -05001056static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio)
Matt Caswell0f113f32015-01-22 03:40:55 +00001057{
Matt Caswellf863ad02016-04-20 12:56:37 +01001058# ifdef OPENSSL_NO_SOCK
1059 return 0;
1060# else
Rich Salz7e1b7482015-04-24 15:26:15 -04001061 int len;
Matt Caswell0f113f32015-01-22 03:40:55 +00001062 OCSP_REQUEST *req = NULL;
Rich Salzfc3cec52015-05-01 23:36:11 -04001063 char inbuf[2048], reqbuf[2048];
Rich Salz995101d2015-04-29 17:37:04 -04001064 char *p, *q;
1065 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001066
Matt Caswell0f113f32015-01-22 03:40:55 +00001067 if (BIO_do_accept(acbio) <= 0) {
1068 BIO_printf(bio_err, "Error accepting connection\n");
1069 ERR_print_errors(bio_err);
1070 return 0;
1071 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001072
Matt Caswell0f113f32015-01-22 03:40:55 +00001073 cbio = BIO_pop(acbio);
1074 *pcbio = cbio;
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001075
Rich Salz7e1b7482015-04-24 15:26:15 -04001076 /* Read the request line. */
Rich Salzfc3cec52015-05-01 23:36:11 -04001077 len = BIO_gets(cbio, reqbuf, sizeof reqbuf);
Rich Salz7e1b7482015-04-24 15:26:15 -04001078 if (len <= 0)
1079 return 1;
Rich Salzfc3cec52015-05-01 23:36:11 -04001080 if (strncmp(reqbuf, "GET ", 4) == 0) {
Rich Salz995101d2015-04-29 17:37:04 -04001081 /* Expecting GET {sp} /URL {sp} HTTP/1.x */
Rich Salzfc3cec52015-05-01 23:36:11 -04001082 for (p = reqbuf + 4; *p == ' '; ++p)
Rich Salz995101d2015-04-29 17:37:04 -04001083 continue;
Rich Salzfc3cec52015-05-01 23:36:11 -04001084 if (*p != '/') {
1085 BIO_printf(bio_err, "Invalid request -- bad URL\n");
1086 return 1;
Rich Salz995101d2015-04-29 17:37:04 -04001087 }
Rich Salzfc3cec52015-05-01 23:36:11 -04001088 p++;
1089
Rich Salz995101d2015-04-29 17:37:04 -04001090 /* Splice off the HTTP version identifier. */
1091 for (q = p; *q; q++)
Rich Salzfc3cec52015-05-01 23:36:11 -04001092 if (*q == ' ')
Rich Salz995101d2015-04-29 17:37:04 -04001093 break;
Rich Salzfc3cec52015-05-01 23:36:11 -04001094 if (strncmp(q, " HTTP/1.", 8) != 0) {
1095 BIO_printf(bio_err, "Invalid request -- bad HTTP vesion\n");
Rich Salz995101d2015-04-29 17:37:04 -04001096 return 1;
1097 }
1098 *q = '\0';
Rich Salzfc3cec52015-05-01 23:36:11 -04001099 len = urldecode(p);
1100 if (len <= 0) {
1101 BIO_printf(bio_err, "Invalid request -- bad URL encoding\n");
1102 return 1;
1103 }
1104 if ((getbio = BIO_new_mem_buf(p, len)) == NULL
1105 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
1106 BIO_printf(bio_err, "Could not allocate memory\n");
1107 ERR_print_errors(bio_err);
1108 return 1;
1109 }
Rich Salz995101d2015-04-29 17:37:04 -04001110 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1111 getbio = BIO_push(b64, getbio);
Rich Salzfc3cec52015-05-01 23:36:11 -04001112 } else if (strncmp(reqbuf, "POST ", 5) != 0) {
1113 BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04001114 return 1;
1115 }
Rich Salzfc3cec52015-05-01 23:36:11 -04001116
1117 /* Read and skip past the headers. */
Matt Caswell0f113f32015-01-22 03:40:55 +00001118 for (;;) {
1119 len = BIO_gets(cbio, inbuf, sizeof inbuf);
1120 if (len <= 0)
1121 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00001122 if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1123 break;
1124 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001125
Matt Caswell0f113f32015-01-22 03:40:55 +00001126 /* Try to read OCSP request */
Rich Salz995101d2015-04-29 17:37:04 -04001127 if (getbio) {
1128 req = d2i_OCSP_REQUEST_bio(getbio, NULL);
1129 BIO_free_all(getbio);
1130 } else
1131 req = d2i_OCSP_REQUEST_bio(cbio, NULL);
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001132
Matt Caswell0f113f32015-01-22 03:40:55 +00001133 if (!req) {
1134 BIO_printf(bio_err, "Error parsing OCSP request\n");
1135 ERR_print_errors(bio_err);
1136 }
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001137
Matt Caswell0f113f32015-01-22 03:40:55 +00001138 *preq = req;
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001139
Matt Caswell0f113f32015-01-22 03:40:55 +00001140 return 1;
Matt Caswellf863ad02016-04-20 12:56:37 +01001141# endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001142}
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001143
1144static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
Matt Caswell0f113f32015-01-22 03:40:55 +00001145{
1146 char http_resp[] =
1147 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1148 "Content-Length: %d\r\n\r\n";
1149 if (!cbio)
1150 return 0;
1151 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1152 i2d_OCSP_RESPONSE_bio(cbio, resp);
1153 (void)BIO_flush(cbio);
1154 return 1;
1155}
Dr. Stephen Hensonee306a12001-07-12 20:41:51 +00001156
Matt Caswellf9e55032016-03-21 15:32:40 +00001157# ifndef OPENSSL_NO_SOCK
Dr. Stephen Henson76e0cd12015-10-18 00:16:23 +01001158static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
1159 const char *path,
Matt Caswell0f113f32015-01-22 03:40:55 +00001160 const STACK_OF(CONF_VALUE) *headers,
1161 OCSP_REQUEST *req, int req_timeout)
1162{
1163 int fd;
1164 int rv;
1165 int i;
Dr. Stephen Henson76e0cd12015-10-18 00:16:23 +01001166 int add_host = 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00001167 OCSP_REQ_CTX *ctx = NULL;
1168 OCSP_RESPONSE *rsp = NULL;
1169 fd_set confds;
1170 struct timeval tv;
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001171
Matt Caswell0f113f32015-01-22 03:40:55 +00001172 if (req_timeout != -1)
1173 BIO_set_nbio(cbio, 1);
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001174
Matt Caswell0f113f32015-01-22 03:40:55 +00001175 rv = BIO_do_connect(cbio);
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001176
Matt Caswell0f113f32015-01-22 03:40:55 +00001177 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001178 BIO_puts(bio_err, "Error connecting BIO\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001179 return NULL;
1180 }
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001181
Alessandro Ghedini4428c7d2015-10-02 15:16:08 +02001182 if (BIO_get_fd(cbio, &fd) < 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001183 BIO_puts(bio_err, "Can't get connection fd\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001184 goto err;
1185 }
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001186
Matt Caswell0f113f32015-01-22 03:40:55 +00001187 if (req_timeout != -1 && rv <= 0) {
1188 FD_ZERO(&confds);
1189 openssl_fdset(fd, &confds);
1190 tv.tv_usec = 0;
1191 tv.tv_sec = req_timeout;
1192 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1193 if (rv == 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001194 BIO_puts(bio_err, "Timeout on connect\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001195 return NULL;
1196 }
1197 }
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001198
Matt Caswell0f113f32015-01-22 03:40:55 +00001199 ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
Matt Caswell96487cd2015-10-30 11:18:04 +00001200 if (ctx == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00001201 return NULL;
Dr. Stephen Hensonb5894272006-07-17 18:52:51 +00001202
Matt Caswell0f113f32015-01-22 03:40:55 +00001203 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1204 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
Dr. Stephen Henson76e0cd12015-10-18 00:16:23 +01001205 if (add_host == 1 && strcasecmp("host", hdr->name) == 0)
1206 add_host = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001207 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1208 goto err;
1209 }
Dr. Stephen Henson18e503f2009-09-30 21:40:55 +00001210
Dr. Stephen Henson76e0cd12015-10-18 00:16:23 +01001211 if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0)
1212 goto err;
1213
Matt Caswell0f113f32015-01-22 03:40:55 +00001214 if (!OCSP_REQ_CTX_set1_req(ctx, req))
1215 goto err;
Dr. Stephen Henson18e503f2009-09-30 21:40:55 +00001216
Matt Caswell0f113f32015-01-22 03:40:55 +00001217 for (;;) {
1218 rv = OCSP_sendreq_nbio(&rsp, ctx);
1219 if (rv != -1)
1220 break;
1221 if (req_timeout == -1)
1222 continue;
1223 FD_ZERO(&confds);
1224 openssl_fdset(fd, &confds);
1225 tv.tv_usec = 0;
1226 tv.tv_sec = req_timeout;
1227 if (BIO_should_read(cbio))
1228 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
1229 else if (BIO_should_write(cbio))
1230 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
1231 else {
Rich Salz7e1b7482015-04-24 15:26:15 -04001232 BIO_puts(bio_err, "Unexpected retry condition\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001233 goto err;
1234 }
1235 if (rv == 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001236 BIO_puts(bio_err, "Timeout on request\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001237 break;
1238 }
1239 if (rv == -1) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001240 BIO_puts(bio_err, "Select error\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001241 break;
1242 }
Dr. Stephen Henson18e503f2009-09-30 21:40:55 +00001243
Matt Caswell0f113f32015-01-22 03:40:55 +00001244 }
1245 err:
Rich Salz895cba12015-04-30 18:10:52 -04001246 OCSP_REQ_CTX_free(ctx);
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001247
Matt Caswell0f113f32015-01-22 03:40:55 +00001248 return rsp;
1249}
Dr. Stephen Henson454dbbc2006-07-17 13:26:54 +00001250
Rich Salz7e1b7482015-04-24 15:26:15 -04001251OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
Matt Caswell0f113f32015-01-22 03:40:55 +00001252 const char *host, const char *path,
1253 const char *port, int use_ssl,
Rich Salz82c49422015-08-10 11:37:48 -04001254 STACK_OF(CONF_VALUE) *headers,
Matt Caswell0f113f32015-01-22 03:40:55 +00001255 int req_timeout)
1256{
1257 BIO *cbio = NULL;
1258 SSL_CTX *ctx = NULL;
1259 OCSP_RESPONSE *resp = NULL;
bluelineXYff4a9392015-08-04 13:23:00 +02001260
Matt Caswell0f113f32015-01-22 03:40:55 +00001261 cbio = BIO_new_connect(host);
1262 if (!cbio) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001263 BIO_printf(bio_err, "Error creating connect BIO\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001264 goto end;
1265 }
1266 if (port)
1267 BIO_set_conn_port(cbio, port);
1268 if (use_ssl == 1) {
1269 BIO *sbio;
Matt Caswell13c9bb32015-03-31 00:18:31 +01001270 ctx = SSL_CTX_new(TLS_client_method());
Matt Caswell0f113f32015-01-22 03:40:55 +00001271 if (ctx == NULL) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001272 BIO_printf(bio_err, "Error creating SSL context.\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001273 goto end;
1274 }
1275 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1276 sbio = BIO_new_ssl(ctx, 1);
1277 cbio = BIO_push(sbio, cbio);
1278 }
bluelineXYff4a9392015-08-04 13:23:00 +02001279
Dr. Stephen Henson76e0cd12015-10-18 00:16:23 +01001280 resp = query_responder(cbio, host, path, headers, req, req_timeout);
Matt Caswell0f113f32015-01-22 03:40:55 +00001281 if (!resp)
1282 BIO_printf(bio_err, "Error querying OCSP responder\n");
1283 end:
Rich Salzca3a82c2015-03-25 11:31:18 -04001284 BIO_free_all(cbio);
Rich Salz62adbce2015-04-11 10:22:36 -04001285 SSL_CTX_free(ctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001286 return resp;
1287}
Matt Caswellf9e55032016-03-21 15:32:40 +00001288# endif
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +00001289
Richard Levitte85d686e2003-02-14 01:02:58 +00001290#endif