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