1 /* 2 * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved. 3 * 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 8 */ 9 10 #include <openssl/opensslconf.h> 11 12 #ifdef OPENSSL_NO_OCSP 13 NON_EMPTY_TRANSLATION_UNIT 14 #else 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 # include <stdio.h> 21 # include <stdlib.h> 22 # include <string.h> 23 # include <time.h> 24 # include <ctype.h> 25 26 /* Needs to be included before the openssl headers */ 27 # include "apps.h" 28 # include "progs.h" 29 # include "internal/sockets.h" 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 # include <openssl/rand.h> 38 39 #ifndef HAVE_FORK 40 # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) 41 # define HAVE_FORK 0 42 # else 43 # define HAVE_FORK 1 44 # endif 45 #endif 46 47 #if HAVE_FORK 48 # undef NO_FORK 49 #else 50 # define NO_FORK 51 #endif 52 53 # if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \ 54 && !defined(OPENSSL_NO_POSIX_IO) 55 # define OCSP_DAEMON 56 # include <sys/types.h> 57 # include <sys/wait.h> 58 # include <syslog.h> 59 # include <signal.h> 60 # define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */ 61 # else 62 # undef LOG_INFO 63 # undef LOG_WARNING 64 # undef LOG_ERR 65 # define LOG_INFO 0 66 # define LOG_WARNING 1 67 # define LOG_ERR 2 68 # endif 69 70 # if defined(OPENSSL_SYS_VXWORKS) 71 /* not supported */ 72 int setpgid(pid_t pid, pid_t pgid) 73 { 74 errno = ENOSYS; 75 return 0; 76 } 77 /* not supported */ 78 pid_t fork(void) 79 { 80 errno = ENOSYS; 81 return (pid_t) -1; 82 } 83 # endif 84 /* Maximum leeway in validity period: default 5 minutes */ 85 # define MAX_VALIDITY_PERIOD (5 * 60) 86 87 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, 88 const EVP_MD *cert_id_md, X509 *issuer, 89 STACK_OF(OCSP_CERTID) *ids); 90 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, 91 const EVP_MD *cert_id_md, X509 *issuer, 92 STACK_OF(OCSP_CERTID) *ids); 93 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, 94 STACK_OF(OPENSSL_STRING) *names, 95 STACK_OF(OCSP_CERTID) *ids, long nsec, 96 long maxage); 97 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req, 98 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, 99 EVP_PKEY *rkey, const EVP_MD *md, 100 STACK_OF(OPENSSL_STRING) *sigopts, 101 STACK_OF(X509) *rother, unsigned long flags, 102 int nmin, int ndays, int badsig); 103 104 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser); 105 static BIO *init_responder(const char *port); 106 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, int timeout); 107 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp); 108 static void log_message(int level, const char *fmt, ...); 109 static char *prog; 110 static int multi = 0; 111 112 # ifdef OCSP_DAEMON 113 static int acfd = (int) INVALID_SOCKET; 114 static int index_changed(CA_DB *); 115 static void spawn_loop(void); 116 static int print_syslog(const char *str, size_t len, void *levPtr); 117 static void sock_timeout(int signum); 118 # endif 119 120 # ifndef OPENSSL_NO_SOCK 121 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, 122 const char *path, 123 const STACK_OF(CONF_VALUE) *headers, 124 OCSP_REQUEST *req, int req_timeout); 125 # endif 126 127 typedef enum OPTION_choice { 128 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, 129 OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT, 130 OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE, 131 OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS, 132 OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN, 133 OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER, 134 OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT, 135 OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER, 136 OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, 137 OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT, 138 OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL, 139 OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER, 140 OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER, 141 OPT_V_ENUM, 142 OPT_MD, 143 OPT_MULTI 144 } OPTION_CHOICE; 145 146 const OPTIONS ocsp_options[] = { 147 {"help", OPT_HELP, '-', "Display this summary"}, 148 {"out", OPT_OUTFILE, '>', "Output filename"}, 149 {"timeout", OPT_TIMEOUT, 'p', 150 "Connection timeout (in seconds) to the OCSP responder"}, 151 {"url", OPT_URL, 's', "Responder URL"}, 152 {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"}, 153 {"port", OPT_PORT, 'p', "Port to run responder on"}, 154 {"ignore_err", OPT_IGNORE_ERR, '-', 155 "Ignore error on OCSP request or response and continue running"}, 156 {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"}, 157 {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"}, 158 {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"}, 159 {"resp_no_certs", OPT_RESP_NO_CERTS, '-', 160 "Don't include any certificates in response"}, 161 {"resp_key_id", OPT_RESP_KEY_ID, '-', 162 "Identify response by signing certificate key ID"}, 163 # ifdef OCSP_DAEMON 164 {"multi", OPT_MULTI, 'p', "run multiple responder processes"}, 165 # endif 166 {"no_certs", OPT_NO_CERTS, '-', 167 "Don't include any certificates in signed request"}, 168 {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-', 169 "Don't check signature on response"}, 170 {"no_cert_verify", OPT_NO_CERT_VERIFY, '-', 171 "Don't check signing certificate"}, 172 {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"}, 173 {"no_cert_checks", OPT_NO_CERT_CHECKS, '-', 174 "Don't do additional checks on signing certificate"}, 175 {"no_explicit", OPT_NO_EXPLICIT, '-', 176 "Do not explicitly check the chain, just verify the root"}, 177 {"trust_other", OPT_TRUST_OTHER, '-', 178 "Don't verify additional certificates"}, 179 {"no_intern", OPT_NO_INTERN, '-', 180 "Don't search certificates contained in response for signer"}, 181 {"badsig", OPT_BADSIG, '-', 182 "Corrupt last byte of loaded OSCP response signature (for test)"}, 183 {"text", OPT_TEXT, '-', "Print text form of request and response"}, 184 {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"}, 185 {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"}, 186 {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"}, 187 {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"}, 188 {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"}, 189 {"VAfile", OPT_VAFILE, '<', "Validator certificates file"}, 190 {"sign_other", OPT_SIGN_OTHER, '<', 191 "Additional certificates to include in signed request"}, 192 {"verify_other", OPT_VERIFY_OTHER, '<', 193 "Additional certificates to search for signer"}, 194 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, 195 {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"}, 196 {"no-CAfile", OPT_NOCAFILE, '-', 197 "Do not load the default certificates file"}, 198 {"no-CApath", OPT_NOCAPATH, '-', 199 "Do not load certificates from the default certificates directory"}, 200 {"validity_period", OPT_VALIDITY_PERIOD, 'u', 201 "Maximum validity discrepancy in seconds"}, 202 {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"}, 203 {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"}, 204 {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"}, 205 {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"}, 206 {"path", OPT_PATH, 's', "Path to use in OCSP request"}, 207 {"issuer", OPT_ISSUER, '<', "Issuer certificate"}, 208 {"cert", OPT_CERT, '<', "Certificate to check"}, 209 {"serial", OPT_SERIAL, 's', "Serial number to check"}, 210 {"index", OPT_INDEX, '<', "Certificate status index file"}, 211 {"CA", OPT_CA, '<', "CA certificate"}, 212 {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"}, 213 {"nrequest", OPT_REQUEST, 'p', 214 "Number of requests to accept (default unlimited)"}, 215 {"ndays", OPT_NDAYS, 'p', "Number of days before next update"}, 216 {"rsigner", OPT_RSIGNER, '<', 217 "Responder certificate to sign responses with"}, 218 {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"}, 219 {"rother", OPT_ROTHER, '<', "Other certificates to include in response"}, 220 {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"}, 221 {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"}, 222 {"header", OPT_HEADER, 's', "key=value header to add"}, 223 {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"}, 224 OPT_V_OPTIONS, 225 {NULL} 226 }; 227 228 int ocsp_main(int argc, char **argv) 229 { 230 BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL; 231 const EVP_MD *cert_id_md = NULL, *rsign_md = NULL; 232 STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL; 233 int trailing_md = 0; 234 CA_DB *rdb = NULL; 235 EVP_PKEY *key = NULL, *rkey = NULL; 236 OCSP_BASICRESP *bs = NULL; 237 OCSP_REQUEST *req = NULL; 238 OCSP_RESPONSE *resp = NULL; 239 STACK_OF(CONF_VALUE) *headers = NULL; 240 STACK_OF(OCSP_CERTID) *ids = NULL; 241 STACK_OF(OPENSSL_STRING) *reqnames = NULL; 242 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL; 243 STACK_OF(X509) *issuers = NULL; 244 X509 *issuer = NULL, *cert = NULL; 245 STACK_OF(X509) *rca_cert = NULL; 246 X509 *signer = NULL, *rsigner = NULL; 247 X509_STORE *store = NULL; 248 X509_VERIFY_PARAM *vpm = NULL; 249 const char *CAfile = NULL, *CApath = NULL; 250 char *header, *value; 251 char *host = NULL, *port = NULL, *path = "/", *outfile = NULL; 252 char *rca_filename = NULL, *reqin = NULL, *respin = NULL; 253 char *reqout = NULL, *respout = NULL, *ridx_filename = NULL; 254 char *rsignfile = NULL, *rkeyfile = NULL; 255 char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL; 256 char *signfile = NULL, *keyfile = NULL; 257 char *thost = NULL, *tport = NULL, *tpath = NULL; 258 int noCAfile = 0, noCApath = 0; 259 int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1; 260 int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1; 261 int req_text = 0, resp_text = 0, ret = 1; 262 int req_timeout = -1; 263 long nsec = MAX_VALIDITY_PERIOD, maxage = -1; 264 unsigned long sign_flags = 0, verify_flags = 0, rflags = 0; 265 OPTION_CHOICE o; 266 267 reqnames = sk_OPENSSL_STRING_new_null(); 268 if (reqnames == NULL) 269 goto end; 270 ids = sk_OCSP_CERTID_new_null(); 271 if (ids == NULL) 272 goto end; 273 if ((vpm = X509_VERIFY_PARAM_new()) == NULL) 274 return 1; 275 276 prog = opt_init(argc, argv, ocsp_options); 277 while ((o = opt_next()) != OPT_EOF) { 278 switch (o) { 279 case OPT_EOF: 280 case OPT_ERR: 281 opthelp: 282 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 283 goto end; 284 case OPT_HELP: 285 ret = 0; 286 opt_help(ocsp_options); 287 goto end; 288 case OPT_OUTFILE: 289 outfile = opt_arg(); 290 break; 291 case OPT_TIMEOUT: 292 #ifndef OPENSSL_NO_SOCK 293 req_timeout = atoi(opt_arg()); 294 #endif 295 break; 296 case OPT_URL: 297 OPENSSL_free(thost); 298 OPENSSL_free(tport); 299 OPENSSL_free(tpath); 300 thost = tport = tpath = NULL; 301 if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) { 302 BIO_printf(bio_err, "%s Error parsing URL\n", prog); 303 goto end; 304 } 305 thost = host; 306 tport = port; 307 tpath = path; 308 break; 309 case OPT_HOST: 310 host = opt_arg(); 311 break; 312 case OPT_PORT: 313 port = opt_arg(); 314 break; 315 case OPT_IGNORE_ERR: 316 ignore_err = 1; 317 break; 318 case OPT_NOVERIFY: 319 noverify = 1; 320 break; 321 case OPT_NONCE: 322 add_nonce = 2; 323 break; 324 case OPT_NO_NONCE: 325 add_nonce = 0; 326 break; 327 case OPT_RESP_NO_CERTS: 328 rflags |= OCSP_NOCERTS; 329 break; 330 case OPT_RESP_KEY_ID: 331 rflags |= OCSP_RESPID_KEY; 332 break; 333 case OPT_NO_CERTS: 334 sign_flags |= OCSP_NOCERTS; 335 break; 336 case OPT_NO_SIGNATURE_VERIFY: 337 verify_flags |= OCSP_NOSIGS; 338 break; 339 case OPT_NO_CERT_VERIFY: 340 verify_flags |= OCSP_NOVERIFY; 341 break; 342 case OPT_NO_CHAIN: 343 verify_flags |= OCSP_NOCHAIN; 344 break; 345 case OPT_NO_CERT_CHECKS: 346 verify_flags |= OCSP_NOCHECKS; 347 break; 348 case OPT_NO_EXPLICIT: 349 verify_flags |= OCSP_NOEXPLICIT; 350 break; 351 case OPT_TRUST_OTHER: 352 verify_flags |= OCSP_TRUSTOTHER; 353 break; 354 case OPT_NO_INTERN: 355 verify_flags |= OCSP_NOINTERN; 356 break; 357 case OPT_BADSIG: 358 badsig = 1; 359 break; 360 case OPT_TEXT: 361 req_text = resp_text = 1; 362 break; 363 case OPT_REQ_TEXT: 364 req_text = 1; 365 break; 366 case OPT_RESP_TEXT: 367 resp_text = 1; 368 break; 369 case OPT_REQIN: 370 reqin = opt_arg(); 371 break; 372 case OPT_RESPIN: 373 respin = opt_arg(); 374 break; 375 case OPT_SIGNER: 376 signfile = opt_arg(); 377 break; 378 case OPT_VAFILE: 379 verify_certfile = opt_arg(); 380 verify_flags |= OCSP_TRUSTOTHER; 381 break; 382 case OPT_SIGN_OTHER: 383 sign_certfile = opt_arg(); 384 break; 385 case OPT_VERIFY_OTHER: 386 verify_certfile = opt_arg(); 387 break; 388 case OPT_CAFILE: 389 CAfile = opt_arg(); 390 break; 391 case OPT_CAPATH: 392 CApath = opt_arg(); 393 break; 394 case OPT_NOCAFILE: 395 noCAfile = 1; 396 break; 397 case OPT_NOCAPATH: 398 noCApath = 1; 399 break; 400 case OPT_V_CASES: 401 if (!opt_verify(o, vpm)) 402 goto end; 403 vpmtouched++; 404 break; 405 case OPT_VALIDITY_PERIOD: 406 opt_long(opt_arg(), &nsec); 407 break; 408 case OPT_STATUS_AGE: 409 opt_long(opt_arg(), &maxage); 410 break; 411 case OPT_SIGNKEY: 412 keyfile = opt_arg(); 413 break; 414 case OPT_REQOUT: 415 reqout = opt_arg(); 416 break; 417 case OPT_RESPOUT: 418 respout = opt_arg(); 419 break; 420 case OPT_PATH: 421 path = opt_arg(); 422 break; 423 case OPT_ISSUER: 424 issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate"); 425 if (issuer == NULL) 426 goto end; 427 if (issuers == NULL) { 428 if ((issuers = sk_X509_new_null()) == NULL) 429 goto end; 430 } 431 sk_X509_push(issuers, issuer); 432 break; 433 case OPT_CERT: 434 X509_free(cert); 435 cert = load_cert(opt_arg(), FORMAT_PEM, "certificate"); 436 if (cert == NULL) 437 goto end; 438 if (cert_id_md == NULL) 439 cert_id_md = EVP_sha1(); 440 if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids)) 441 goto end; 442 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) 443 goto end; 444 trailing_md = 0; 445 break; 446 case OPT_SERIAL: 447 if (cert_id_md == NULL) 448 cert_id_md = EVP_sha1(); 449 if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids)) 450 goto end; 451 if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) 452 goto end; 453 trailing_md = 0; 454 break; 455 case OPT_INDEX: 456 ridx_filename = opt_arg(); 457 break; 458 case OPT_CA: 459 rca_filename = opt_arg(); 460 break; 461 case OPT_NMIN: 462 opt_int(opt_arg(), &nmin); 463 if (ndays == -1) 464 ndays = 0; 465 break; 466 case OPT_REQUEST: 467 opt_int(opt_arg(), &accept_count); 468 break; 469 case OPT_NDAYS: 470 ndays = atoi(opt_arg()); 471 break; 472 case OPT_RSIGNER: 473 rsignfile = opt_arg(); 474 break; 475 case OPT_RKEY: 476 rkeyfile = opt_arg(); 477 break; 478 case OPT_ROTHER: 479 rcertfile = opt_arg(); 480 break; 481 case OPT_RMD: /* Response MessageDigest */ 482 if (!opt_md(opt_arg(), &rsign_md)) 483 goto end; 484 break; 485 case OPT_RSIGOPT: 486 if (rsign_sigopts == NULL) 487 rsign_sigopts = sk_OPENSSL_STRING_new_null(); 488 if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg())) 489 goto end; 490 break; 491 case OPT_HEADER: 492 header = opt_arg(); 493 value = strchr(header, '='); 494 if (value == NULL) { 495 BIO_printf(bio_err, "Missing = in header key=value\n"); 496 goto opthelp; 497 } 498 *value++ = '\0'; 499 if (!X509V3_add_value(header, value, &headers)) 500 goto end; 501 break; 502 case OPT_MD: 503 if (trailing_md) { 504 BIO_printf(bio_err, 505 "%s: Digest must be before -cert or -serial\n", 506 prog); 507 goto opthelp; 508 } 509 if (!opt_md(opt_unknown(), &cert_id_md)) 510 goto opthelp; 511 trailing_md = 1; 512 break; 513 case OPT_MULTI: 514 # ifdef OCSP_DAEMON 515 multi = atoi(opt_arg()); 516 # endif 517 break; 518 } 519 } 520 if (trailing_md) { 521 BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n", 522 prog); 523 goto opthelp; 524 } 525 argc = opt_num_rest(); 526 if (argc != 0) 527 goto opthelp; 528 529 /* Have we anything to do? */ 530 if (req == NULL && reqin == NULL 531 && respin == NULL && !(port != NULL && ridx_filename != NULL)) 532 goto opthelp; 533 534 out = bio_open_default(outfile, 'w', FORMAT_TEXT); 535 if (out == NULL) 536 goto end; 537 538 if (req == NULL && (add_nonce != 2)) 539 add_nonce = 0; 540 541 if (req == NULL && reqin != NULL) { 542 derbio = bio_open_default(reqin, 'r', FORMAT_ASN1); 543 if (derbio == NULL) 544 goto end; 545 req = d2i_OCSP_REQUEST_bio(derbio, NULL); 546 BIO_free(derbio); 547 if (req == NULL) { 548 BIO_printf(bio_err, "Error reading OCSP request\n"); 549 goto end; 550 } 551 } 552 553 if (req == NULL && port != NULL) { 554 acbio = init_responder(port); 555 if (acbio == NULL) 556 goto end; 557 } 558 559 if (rsignfile != NULL) { 560 if (rkeyfile == NULL) 561 rkeyfile = rsignfile; 562 rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate"); 563 if (rsigner == NULL) { 564 BIO_printf(bio_err, "Error loading responder certificate\n"); 565 goto end; 566 } 567 if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM, 568 NULL, "CA certificate")) 569 goto end; 570 if (rcertfile != NULL) { 571 if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL, 572 "responder other certificates")) 573 goto end; 574 } 575 rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL, 576 "responder private key"); 577 if (rkey == NULL) 578 goto end; 579 } 580 581 if (ridx_filename != NULL 582 && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) { 583 BIO_printf(bio_err, 584 "Responder mode requires certificate, key, and CA.\n"); 585 goto end; 586 } 587 588 if (ridx_filename != NULL) { 589 rdb = load_index(ridx_filename, NULL); 590 if (rdb == NULL || index_index(rdb) <= 0) { 591 ret = 1; 592 goto end; 593 } 594 } 595 596 # ifdef OCSP_DAEMON 597 if (multi && acbio != NULL) 598 spawn_loop(); 599 if (acbio != NULL && req_timeout > 0) 600 signal(SIGALRM, sock_timeout); 601 #endif 602 603 if (acbio != NULL) 604 log_message(LOG_INFO, "waiting for OCSP client connections..."); 605 606 redo_accept: 607 608 if (acbio != NULL) { 609 # ifdef OCSP_DAEMON 610 if (index_changed(rdb)) { 611 CA_DB *newrdb = load_index(ridx_filename, NULL); 612 613 if (newrdb != NULL && index_index(newrdb) > 0) { 614 free_index(rdb); 615 rdb = newrdb; 616 } else { 617 free_index(newrdb); 618 log_message(LOG_ERR, "error reloading updated index: %s", 619 ridx_filename); 620 } 621 } 622 # endif 623 624 req = NULL; 625 if (!do_responder(&req, &cbio, acbio, req_timeout)) 626 goto redo_accept; 627 628 if (req == NULL) { 629 resp = 630 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, 631 NULL); 632 send_ocsp_response(cbio, resp); 633 goto done_resp; 634 } 635 } 636 637 if (req == NULL 638 && (signfile != NULL || reqout != NULL 639 || host != NULL || add_nonce || ridx_filename != NULL)) { 640 BIO_printf(bio_err, "Need an OCSP request for this operation!\n"); 641 goto end; 642 } 643 644 if (req != NULL && add_nonce) { 645 if (!OCSP_request_add1_nonce(req, NULL, -1)) 646 goto end; 647 } 648 649 if (signfile != NULL) { 650 if (keyfile == NULL) 651 keyfile = signfile; 652 signer = load_cert(signfile, FORMAT_PEM, "signer certificate"); 653 if (signer == NULL) { 654 BIO_printf(bio_err, "Error loading signer certificate\n"); 655 goto end; 656 } 657 if (sign_certfile != NULL) { 658 if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL, 659 "signer certificates")) 660 goto end; 661 } 662 key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL, 663 "signer private key"); 664 if (key == NULL) 665 goto end; 666 667 if (!OCSP_request_sign 668 (req, signer, key, NULL, sign_other, sign_flags)) { 669 BIO_printf(bio_err, "Error signing OCSP request\n"); 670 goto end; 671 } 672 } 673 674 if (req_text && req != NULL) 675 OCSP_REQUEST_print(out, req, 0); 676 677 if (reqout != NULL) { 678 derbio = bio_open_default(reqout, 'w', FORMAT_ASN1); 679 if (derbio == NULL) 680 goto end; 681 i2d_OCSP_REQUEST_bio(derbio, req); 682 BIO_free(derbio); 683 } 684 685 if (rdb != NULL) { 686 make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey, 687 rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig); 688 if (cbio != NULL) 689 send_ocsp_response(cbio, resp); 690 } else if (host != NULL) { 691 # ifndef OPENSSL_NO_SOCK 692 resp = process_responder(req, host, path, 693 port, use_ssl, headers, req_timeout); 694 if (resp == NULL) 695 goto end; 696 # else 697 BIO_printf(bio_err, 698 "Error creating connect BIO - sockets not supported.\n"); 699 goto end; 700 # endif 701 } else if (respin != NULL) { 702 derbio = bio_open_default(respin, 'r', FORMAT_ASN1); 703 if (derbio == NULL) 704 goto end; 705 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL); 706 BIO_free(derbio); 707 if (resp == NULL) { 708 BIO_printf(bio_err, "Error reading OCSP response\n"); 709 goto end; 710 } 711 } else { 712 ret = 0; 713 goto end; 714 } 715 716 done_resp: 717 718 if (respout != NULL) { 719 derbio = bio_open_default(respout, 'w', FORMAT_ASN1); 720 if (derbio == NULL) 721 goto end; 722 i2d_OCSP_RESPONSE_bio(derbio, resp); 723 BIO_free(derbio); 724 } 725 726 i = OCSP_response_status(resp); 727 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) { 728 BIO_printf(out, "Responder Error: %s (%d)\n", 729 OCSP_response_status_str(i), i); 730 if (!ignore_err) 731 goto end; 732 } 733 734 if (resp_text) 735 OCSP_RESPONSE_print(out, resp, 0); 736 737 /* If running as responder don't verify our own response */ 738 if (cbio != NULL) { 739 /* If not unlimited, see if we took all we should. */ 740 if (accept_count != -1 && --accept_count <= 0) { 741 ret = 0; 742 goto end; 743 } 744 BIO_free_all(cbio); 745 cbio = NULL; 746 OCSP_REQUEST_free(req); 747 req = NULL; 748 OCSP_RESPONSE_free(resp); 749 resp = NULL; 750 goto redo_accept; 751 } 752 if (ridx_filename != NULL) { 753 ret = 0; 754 goto end; 755 } 756 757 if (store == NULL) { 758 store = setup_verify(CAfile, CApath, noCAfile, noCApath); 759 if (!store) 760 goto end; 761 } 762 if (vpmtouched) 763 X509_STORE_set1_param(store, vpm); 764 if (verify_certfile != NULL) { 765 if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL, 766 "validator certificate")) 767 goto end; 768 } 769 770 bs = OCSP_response_get1_basic(resp); 771 if (bs == NULL) { 772 BIO_printf(bio_err, "Error parsing response\n"); 773 goto end; 774 } 775 776 ret = 0; 777 778 if (!noverify) { 779 if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) { 780 if (i == -1) 781 BIO_printf(bio_err, "WARNING: no nonce in response\n"); 782 else { 783 BIO_printf(bio_err, "Nonce Verify error\n"); 784 ret = 1; 785 goto end; 786 } 787 } 788 789 i = OCSP_basic_verify(bs, verify_other, store, verify_flags); 790 if (i <= 0 && issuers) { 791 i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER); 792 if (i > 0) 793 ERR_clear_error(); 794 } 795 if (i <= 0) { 796 BIO_printf(bio_err, "Response Verify Failure\n"); 797 ERR_print_errors(bio_err); 798 ret = 1; 799 } else { 800 BIO_printf(bio_err, "Response verify OK\n"); 801 } 802 } 803 804 print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage); 805 806 end: 807 ERR_print_errors(bio_err); 808 X509_free(signer); 809 X509_STORE_free(store); 810 X509_VERIFY_PARAM_free(vpm); 811 sk_OPENSSL_STRING_free(rsign_sigopts); 812 EVP_PKEY_free(key); 813 EVP_PKEY_free(rkey); 814 X509_free(cert); 815 sk_X509_pop_free(issuers, X509_free); 816 X509_free(rsigner); 817 sk_X509_pop_free(rca_cert, X509_free); 818 free_index(rdb); 819 BIO_free_all(cbio); 820 BIO_free_all(acbio); 821 BIO_free_all(out); 822 OCSP_REQUEST_free(req); 823 OCSP_RESPONSE_free(resp); 824 OCSP_BASICRESP_free(bs); 825 sk_OPENSSL_STRING_free(reqnames); 826 sk_OCSP_CERTID_free(ids); 827 sk_X509_pop_free(sign_other, X509_free); 828 sk_X509_pop_free(verify_other, X509_free); 829 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); 830 OPENSSL_free(thost); 831 OPENSSL_free(tport); 832 OPENSSL_free(tpath); 833 834 return ret; 835 } 836 837 static void 838 log_message(int level, const char *fmt, ...) 839 { 840 va_list ap; 841 842 va_start(ap, fmt); 843 # ifdef OCSP_DAEMON 844 if (multi) { 845 char buf[1024]; 846 if (vsnprintf(buf, sizeof(buf), fmt, ap) > 0) { 847 syslog(level, "%s", buf); 848 } 849 if (level >= LOG_ERR) 850 ERR_print_errors_cb(print_syslog, &level); 851 } 852 # endif 853 if (!multi) { 854 BIO_printf(bio_err, "%s: ", prog); 855 BIO_vprintf(bio_err, fmt, ap); 856 BIO_printf(bio_err, "\n"); 857 } 858 va_end(ap); 859 } 860 861 # ifdef OCSP_DAEMON 862 863 static int print_syslog(const char *str, size_t len, void *levPtr) 864 { 865 int level = *(int *)levPtr; 866 int ilen = (len > MAXERRLEN) ? MAXERRLEN : len; 867 868 syslog(level, "%.*s", ilen, str); 869 870 return ilen; 871 } 872 873 static int index_changed(CA_DB *rdb) 874 { 875 struct stat sb; 876 877 if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) { 878 if (rdb->dbst.st_mtime != sb.st_mtime 879 || rdb->dbst.st_ctime != sb.st_ctime 880 || rdb->dbst.st_ino != sb.st_ino 881 || rdb->dbst.st_dev != sb.st_dev) { 882 syslog(LOG_INFO, "index file changed, reloading"); 883 return 1; 884 } 885 } 886 return 0; 887 } 888 889 static void killall(int ret, pid_t *kidpids) 890 { 891 int i; 892 893 for (i = 0; i < multi; ++i) 894 if (kidpids[i] != 0) 895 (void)kill(kidpids[i], SIGTERM); 896 OPENSSL_free(kidpids); 897 sleep(1); 898 exit(ret); 899 } 900 901 static int termsig = 0; 902 903 static void noteterm (int sig) 904 { 905 termsig = sig; 906 } 907 908 /* 909 * Loop spawning up to `multi` child processes, only child processes return 910 * from this function. The parent process loops until receiving a termination 911 * signal, kills extant children and exits without returning. 912 */ 913 static void spawn_loop(void) 914 { 915 pid_t *kidpids = NULL; 916 int status; 917 int procs = 0; 918 int i; 919 920 openlog(prog, LOG_PID, LOG_DAEMON); 921 922 if (setpgid(0, 0)) { 923 syslog(LOG_ERR, "fatal: error detaching from parent process group: %s", 924 strerror(errno)); 925 exit(1); 926 } 927 kidpids = app_malloc(multi * sizeof(*kidpids), "child PID array"); 928 for (i = 0; i < multi; ++i) 929 kidpids[i] = 0; 930 931 signal(SIGINT, noteterm); 932 signal(SIGTERM, noteterm); 933 934 while (termsig == 0) { 935 pid_t fpid; 936 937 /* 938 * Wait for a child to replace when we're at the limit. 939 * Slow down if a child exited abnormally or waitpid() < 0 940 */ 941 while (termsig == 0 && procs >= multi) { 942 if ((fpid = waitpid(-1, &status, 0)) > 0) { 943 for (i = 0; i < procs; ++i) { 944 if (kidpids[i] == fpid) { 945 kidpids[i] = 0; 946 --procs; 947 break; 948 } 949 } 950 if (i >= multi) { 951 syslog(LOG_ERR, "fatal: internal error: " 952 "no matching child slot for pid: %ld", 953 (long) fpid); 954 killall(1, kidpids); 955 } 956 if (status != 0) { 957 if (WIFEXITED(status)) 958 syslog(LOG_WARNING, "child process: %ld, exit status: %d", 959 (long)fpid, WEXITSTATUS(status)); 960 else if (WIFSIGNALED(status)) 961 syslog(LOG_WARNING, "child process: %ld, term signal %d%s", 962 (long)fpid, WTERMSIG(status), 963 #ifdef WCOREDUMP 964 WCOREDUMP(status) ? " (core dumped)" : 965 #endif 966 ""); 967 sleep(1); 968 } 969 break; 970 } else if (errno != EINTR) { 971 syslog(LOG_ERR, "fatal: waitpid(): %s", strerror(errno)); 972 killall(1, kidpids); 973 } 974 } 975 if (termsig) 976 break; 977 978 switch(fpid = fork()) { 979 case -1: /* error */ 980 /* System critically low on memory, pause and try again later */ 981 sleep(30); 982 break; 983 case 0: /* child */ 984 OPENSSL_free(kidpids); 985 signal(SIGINT, SIG_DFL); 986 signal(SIGTERM, SIG_DFL); 987 if (termsig) 988 _exit(0); 989 if (RAND_poll() <= 0) { 990 syslog(LOG_ERR, "fatal: RAND_poll() failed"); 991 _exit(1); 992 } 993 return; 994 default: /* parent */ 995 for (i = 0; i < multi; ++i) { 996 if (kidpids[i] == 0) { 997 kidpids[i] = fpid; 998 procs++; 999 break; 1000 } 1001 } 1002 if (i >= multi) { 1003 syslog(LOG_ERR, "fatal: internal error: no free child slots"); 1004 killall(1, kidpids); 1005 } 1006 break; 1007 } 1008 } 1009 1010 /* The loop above can only break on termsig */ 1011 syslog(LOG_INFO, "terminating on signal: %d", termsig); 1012 killall(0, kidpids); 1013 } 1014 # endif 1015 1016 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, 1017 const EVP_MD *cert_id_md, X509 *issuer, 1018 STACK_OF(OCSP_CERTID) *ids) 1019 { 1020 OCSP_CERTID *id; 1021 1022 if (issuer == NULL) { 1023 BIO_printf(bio_err, "No issuer certificate specified\n"); 1024 return 0; 1025 } 1026 if (*req == NULL) 1027 *req = OCSP_REQUEST_new(); 1028 if (*req == NULL) 1029 goto err; 1030 id = OCSP_cert_to_id(cert_id_md, cert, issuer); 1031 if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) 1032 goto err; 1033 if (!OCSP_request_add0_id(*req, id)) 1034 goto err; 1035 return 1; 1036 1037 err: 1038 BIO_printf(bio_err, "Error Creating OCSP request\n"); 1039 return 0; 1040 } 1041 1042 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, 1043 const EVP_MD *cert_id_md, X509 *issuer, 1044 STACK_OF(OCSP_CERTID) *ids) 1045 { 1046 OCSP_CERTID *id; 1047 X509_NAME *iname; 1048 ASN1_BIT_STRING *ikey; 1049 ASN1_INTEGER *sno; 1050 1051 if (issuer == NULL) { 1052 BIO_printf(bio_err, "No issuer certificate specified\n"); 1053 return 0; 1054 } 1055 if (*req == NULL) 1056 *req = OCSP_REQUEST_new(); 1057 if (*req == NULL) 1058 goto err; 1059 iname = X509_get_subject_name(issuer); 1060 ikey = X509_get0_pubkey_bitstr(issuer); 1061 sno = s2i_ASN1_INTEGER(NULL, serial); 1062 if (sno == NULL) { 1063 BIO_printf(bio_err, "Error converting serial number %s\n", serial); 1064 return 0; 1065 } 1066 id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno); 1067 ASN1_INTEGER_free(sno); 1068 if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) 1069 goto err; 1070 if (!OCSP_request_add0_id(*req, id)) 1071 goto err; 1072 return 1; 1073 1074 err: 1075 BIO_printf(bio_err, "Error Creating OCSP request\n"); 1076 return 0; 1077 } 1078 1079 static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, 1080 STACK_OF(OPENSSL_STRING) *names, 1081 STACK_OF(OCSP_CERTID) *ids, long nsec, 1082 long maxage) 1083 { 1084 OCSP_CERTID *id; 1085 const char *name; 1086 int i, status, reason; 1087 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd; 1088 1089 if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names) 1090 || !sk_OCSP_CERTID_num(ids)) 1091 return; 1092 1093 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) { 1094 id = sk_OCSP_CERTID_value(ids, i); 1095 name = sk_OPENSSL_STRING_value(names, i); 1096 BIO_printf(out, "%s: ", name); 1097 1098 if (!OCSP_resp_find_status(bs, id, &status, &reason, 1099 &rev, &thisupd, &nextupd)) { 1100 BIO_puts(out, "ERROR: No Status found.\n"); 1101 continue; 1102 } 1103 1104 /* 1105 * Check validity: if invalid write to output BIO so we know which 1106 * response this refers to. 1107 */ 1108 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) { 1109 BIO_puts(out, "WARNING: Status times invalid.\n"); 1110 ERR_print_errors(out); 1111 } 1112 BIO_printf(out, "%s\n", OCSP_cert_status_str(status)); 1113 1114 BIO_puts(out, "\tThis Update: "); 1115 ASN1_GENERALIZEDTIME_print(out, thisupd); 1116 BIO_puts(out, "\n"); 1117 1118 if (nextupd) { 1119 BIO_puts(out, "\tNext Update: "); 1120 ASN1_GENERALIZEDTIME_print(out, nextupd); 1121 BIO_puts(out, "\n"); 1122 } 1123 1124 if (status != V_OCSP_CERTSTATUS_REVOKED) 1125 continue; 1126 1127 if (reason != -1) 1128 BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason)); 1129 1130 BIO_puts(out, "\tRevocation Time: "); 1131 ASN1_GENERALIZEDTIME_print(out, rev); 1132 BIO_puts(out, "\n"); 1133 } 1134 } 1135 1136 static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req, 1137 CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, 1138 EVP_PKEY *rkey, const EVP_MD *rmd, 1139 STACK_OF(OPENSSL_STRING) *sigopts, 1140 STACK_OF(X509) *rother, unsigned long flags, 1141 int nmin, int ndays, int badsig) 1142 { 1143 ASN1_TIME *thisupd = NULL, *nextupd = NULL; 1144 OCSP_CERTID *cid; 1145 OCSP_BASICRESP *bs = NULL; 1146 int i, id_count; 1147 EVP_MD_CTX *mctx = NULL; 1148 EVP_PKEY_CTX *pkctx = NULL; 1149 1150 id_count = OCSP_request_onereq_count(req); 1151 1152 if (id_count <= 0) { 1153 *resp = 1154 OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL); 1155 goto end; 1156 } 1157 1158 bs = OCSP_BASICRESP_new(); 1159 thisupd = X509_gmtime_adj(NULL, 0); 1160 if (ndays != -1) 1161 nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL); 1162 1163 /* Examine each certificate id in the request */ 1164 for (i = 0; i < id_count; i++) { 1165 OCSP_ONEREQ *one; 1166 ASN1_INTEGER *serial; 1167 char **inf; 1168 int jj; 1169 int found = 0; 1170 ASN1_OBJECT *cert_id_md_oid; 1171 const EVP_MD *cert_id_md; 1172 one = OCSP_request_onereq_get0(req, i); 1173 cid = OCSP_onereq_get0_id(one); 1174 1175 OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid); 1176 1177 cert_id_md = EVP_get_digestbyobj(cert_id_md_oid); 1178 if (cert_id_md == NULL) { 1179 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, 1180 NULL); 1181 goto end; 1182 } 1183 for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) { 1184 X509 *ca_cert = sk_X509_value(ca, jj); 1185 OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert); 1186 1187 if (OCSP_id_issuer_cmp(ca_id, cid) == 0) 1188 found = 1; 1189 1190 OCSP_CERTID_free(ca_id); 1191 } 1192 1193 if (!found) { 1194 OCSP_basic_add1_status(bs, cid, 1195 V_OCSP_CERTSTATUS_UNKNOWN, 1196 0, NULL, thisupd, nextupd); 1197 continue; 1198 } 1199 OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid); 1200 inf = lookup_serial(db, serial); 1201 if (inf == NULL) { 1202 OCSP_basic_add1_status(bs, cid, 1203 V_OCSP_CERTSTATUS_UNKNOWN, 1204 0, NULL, thisupd, nextupd); 1205 } else if (inf[DB_type][0] == DB_TYPE_VAL) { 1206 OCSP_basic_add1_status(bs, cid, 1207 V_OCSP_CERTSTATUS_GOOD, 1208 0, NULL, thisupd, nextupd); 1209 } else if (inf[DB_type][0] == DB_TYPE_REV) { 1210 ASN1_OBJECT *inst = NULL; 1211 ASN1_TIME *revtm = NULL; 1212 ASN1_GENERALIZEDTIME *invtm = NULL; 1213 OCSP_SINGLERESP *single; 1214 int reason = -1; 1215 unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]); 1216 single = OCSP_basic_add1_status(bs, cid, 1217 V_OCSP_CERTSTATUS_REVOKED, 1218 reason, revtm, thisupd, nextupd); 1219 if (invtm != NULL) 1220 OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, 1221 invtm, 0, 0); 1222 else if (inst != NULL) 1223 OCSP_SINGLERESP_add1_ext_i2d(single, 1224 NID_hold_instruction_code, inst, 1225 0, 0); 1226 ASN1_OBJECT_free(inst); 1227 ASN1_TIME_free(revtm); 1228 ASN1_GENERALIZEDTIME_free(invtm); 1229 } 1230 } 1231 1232 OCSP_copy_nonce(bs, req); 1233 1234 mctx = EVP_MD_CTX_new(); 1235 if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) { 1236 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL); 1237 goto end; 1238 } 1239 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) { 1240 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i); 1241 1242 if (pkey_ctrl_string(pkctx, sigopt) <= 0) { 1243 BIO_printf(err, "parameter error \"%s\"\n", sigopt); 1244 ERR_print_errors(bio_err); 1245 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, 1246 NULL); 1247 goto end; 1248 } 1249 } 1250 if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) { 1251 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs); 1252 goto end; 1253 } 1254 1255 if (badsig) { 1256 const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs); 1257 corrupt_signature(sig); 1258 } 1259 1260 *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs); 1261 1262 end: 1263 EVP_MD_CTX_free(mctx); 1264 ASN1_TIME_free(thisupd); 1265 ASN1_TIME_free(nextupd); 1266 OCSP_BASICRESP_free(bs); 1267 } 1268 1269 static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser) 1270 { 1271 int i; 1272 BIGNUM *bn = NULL; 1273 char *itmp, *row[DB_NUMBER], **rrow; 1274 for (i = 0; i < DB_NUMBER; i++) 1275 row[i] = NULL; 1276 bn = ASN1_INTEGER_to_BN(ser, NULL); 1277 OPENSSL_assert(bn); /* FIXME: should report an error at this 1278 * point and abort */ 1279 if (BN_is_zero(bn)) 1280 itmp = OPENSSL_strdup("00"); 1281 else 1282 itmp = BN_bn2hex(bn); 1283 row[DB_serial] = itmp; 1284 BN_free(bn); 1285 rrow = TXT_DB_get_by_index(db->db, DB_serial, row); 1286 OPENSSL_free(itmp); 1287 return rrow; 1288 } 1289 1290 /* Quick and dirty OCSP server: read in and parse input request */ 1291 1292 static BIO *init_responder(const char *port) 1293 { 1294 # ifdef OPENSSL_NO_SOCK 1295 BIO_printf(bio_err, 1296 "Error setting up accept BIO - sockets not supported.\n"); 1297 return NULL; 1298 # else 1299 BIO *acbio = NULL, *bufbio = NULL; 1300 1301 bufbio = BIO_new(BIO_f_buffer()); 1302 if (bufbio == NULL) 1303 goto err; 1304 acbio = BIO_new(BIO_s_accept()); 1305 if (acbio == NULL 1306 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0 1307 || BIO_set_accept_port(acbio, port) < 0) { 1308 log_message(LOG_ERR, "Error setting up accept BIO"); 1309 goto err; 1310 } 1311 1312 BIO_set_accept_bios(acbio, bufbio); 1313 bufbio = NULL; 1314 if (BIO_do_accept(acbio) <= 0) { 1315 log_message(LOG_ERR, "Error starting accept"); 1316 goto err; 1317 } 1318 1319 return acbio; 1320 1321 err: 1322 BIO_free_all(acbio); 1323 BIO_free(bufbio); 1324 return NULL; 1325 # endif 1326 } 1327 1328 # ifndef OPENSSL_NO_SOCK 1329 /* 1330 * Decode %xx URL-decoding in-place. Ignores mal-formed sequences. 1331 */ 1332 static int urldecode(char *p) 1333 { 1334 unsigned char *out = (unsigned char *)p; 1335 unsigned char *save = out; 1336 1337 for (; *p; p++) { 1338 if (*p != '%') 1339 *out++ = *p; 1340 else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) { 1341 /* Don't check, can't fail because of ixdigit() call. */ 1342 *out++ = (OPENSSL_hexchar2int(p[1]) << 4) 1343 | OPENSSL_hexchar2int(p[2]); 1344 p += 2; 1345 } 1346 else 1347 return -1; 1348 } 1349 *out = '\0'; 1350 return (int)(out - save); 1351 } 1352 # endif 1353 1354 # ifdef OCSP_DAEMON 1355 static void sock_timeout(int signum) 1356 { 1357 if (acfd != (int)INVALID_SOCKET) 1358 (void)shutdown(acfd, SHUT_RD); 1359 } 1360 # endif 1361 1362 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, 1363 int timeout) 1364 { 1365 # ifdef OPENSSL_NO_SOCK 1366 return 0; 1367 # else 1368 int len; 1369 OCSP_REQUEST *req = NULL; 1370 char inbuf[2048], reqbuf[2048]; 1371 char *p, *q; 1372 BIO *cbio = NULL, *getbio = NULL, *b64 = NULL; 1373 const char *client; 1374 1375 *preq = NULL; 1376 1377 /* Connection loss before accept() is routine, ignore silently */ 1378 if (BIO_do_accept(acbio) <= 0) 1379 return 0; 1380 1381 cbio = BIO_pop(acbio); 1382 *pcbio = cbio; 1383 client = BIO_get_peer_name(cbio); 1384 1385 # ifdef OCSP_DAEMON 1386 if (timeout > 0) { 1387 (void) BIO_get_fd(cbio, &acfd); 1388 alarm(timeout); 1389 } 1390 # endif 1391 1392 /* Read the request line. */ 1393 len = BIO_gets(cbio, reqbuf, sizeof(reqbuf)); 1394 if (len <= 0) 1395 goto out; 1396 1397 if (strncmp(reqbuf, "GET ", 4) == 0) { 1398 /* Expecting GET {sp} /URL {sp} HTTP/1.x */ 1399 for (p = reqbuf + 4; *p == ' '; ++p) 1400 continue; 1401 if (*p != '/') { 1402 log_message(LOG_INFO, "Invalid request -- bad URL: %s", client); 1403 goto out; 1404 } 1405 p++; 1406 1407 /* Splice off the HTTP version identifier. */ 1408 for (q = p; *q; q++) 1409 if (*q == ' ') 1410 break; 1411 if (strncmp(q, " HTTP/1.", 8) != 0) { 1412 log_message(LOG_INFO, 1413 "Invalid request -- bad HTTP version: %s", client); 1414 goto out; 1415 } 1416 *q = '\0'; 1417 1418 /* 1419 * Skip "GET / HTTP..." requests often used by load-balancers 1420 */ 1421 if (p[1] == '\0') 1422 goto out; 1423 1424 len = urldecode(p); 1425 if (len <= 0) { 1426 log_message(LOG_INFO, 1427 "Invalid request -- bad URL encoding: %s", client); 1428 goto out; 1429 } 1430 if ((getbio = BIO_new_mem_buf(p, len)) == NULL 1431 || (b64 = BIO_new(BIO_f_base64())) == NULL) { 1432 log_message(LOG_ERR, "Could not allocate base64 bio: %s", client); 1433 goto out; 1434 } 1435 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); 1436 getbio = BIO_push(b64, getbio); 1437 } else if (strncmp(reqbuf, "POST ", 5) != 0) { 1438 log_message(LOG_INFO, "Invalid request -- bad HTTP verb: %s", client); 1439 goto out; 1440 } 1441 1442 /* Read and skip past the headers. */ 1443 for (;;) { 1444 len = BIO_gets(cbio, inbuf, sizeof(inbuf)); 1445 if (len <= 0) 1446 goto out; 1447 if ((inbuf[0] == '\r') || (inbuf[0] == '\n')) 1448 break; 1449 } 1450 1451 # ifdef OCSP_DAEMON 1452 /* Clear alarm before we close the client socket */ 1453 alarm(0); 1454 timeout = 0; 1455 # endif 1456 1457 /* Try to read OCSP request */ 1458 if (getbio != NULL) { 1459 req = d2i_OCSP_REQUEST_bio(getbio, NULL); 1460 BIO_free_all(getbio); 1461 } else { 1462 req = d2i_OCSP_REQUEST_bio(cbio, NULL); 1463 } 1464 1465 if (req == NULL) 1466 log_message(LOG_ERR, "Error parsing OCSP request"); 1467 1468 *preq = req; 1469 1470 out: 1471 # ifdef OCSP_DAEMON 1472 if (timeout > 0) 1473 alarm(0); 1474 acfd = (int)INVALID_SOCKET; 1475 # endif 1476 return 1; 1477 # endif 1478 } 1479 1480 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp) 1481 { 1482 char http_resp[] = 1483 "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n" 1484 "Content-Length: %d\r\n\r\n"; 1485 if (cbio == NULL) 1486 return 0; 1487 BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL)); 1488 i2d_OCSP_RESPONSE_bio(cbio, resp); 1489 (void)BIO_flush(cbio); 1490 return 1; 1491 } 1492 1493 # ifndef OPENSSL_NO_SOCK 1494 static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, 1495 const char *path, 1496 const STACK_OF(CONF_VALUE) *headers, 1497 OCSP_REQUEST *req, int req_timeout) 1498 { 1499 int fd; 1500 int rv; 1501 int i; 1502 int add_host = 1; 1503 OCSP_REQ_CTX *ctx = NULL; 1504 OCSP_RESPONSE *rsp = NULL; 1505 fd_set confds; 1506 struct timeval tv; 1507 1508 if (req_timeout != -1) 1509 BIO_set_nbio(cbio, 1); 1510 1511 rv = BIO_do_connect(cbio); 1512 1513 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) { 1514 BIO_puts(bio_err, "Error connecting BIO\n"); 1515 return NULL; 1516 } 1517 1518 if (BIO_get_fd(cbio, &fd) < 0) { 1519 BIO_puts(bio_err, "Can't get connection fd\n"); 1520 goto err; 1521 } 1522 1523 if (req_timeout != -1 && rv <= 0) { 1524 FD_ZERO(&confds); 1525 openssl_fdset(fd, &confds); 1526 tv.tv_usec = 0; 1527 tv.tv_sec = req_timeout; 1528 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); 1529 if (rv == 0) { 1530 BIO_puts(bio_err, "Timeout on connect\n"); 1531 return NULL; 1532 } 1533 } 1534 1535 ctx = OCSP_sendreq_new(cbio, path, NULL, -1); 1536 if (ctx == NULL) 1537 return NULL; 1538 1539 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { 1540 CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i); 1541 if (add_host == 1 && strcasecmp("host", hdr->name) == 0) 1542 add_host = 0; 1543 if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value)) 1544 goto err; 1545 } 1546 1547 if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0) 1548 goto err; 1549 1550 if (!OCSP_REQ_CTX_set1_req(ctx, req)) 1551 goto err; 1552 1553 for (;;) { 1554 rv = OCSP_sendreq_nbio(&rsp, ctx); 1555 if (rv != -1) 1556 break; 1557 if (req_timeout == -1) 1558 continue; 1559 FD_ZERO(&confds); 1560 openssl_fdset(fd, &confds); 1561 tv.tv_usec = 0; 1562 tv.tv_sec = req_timeout; 1563 if (BIO_should_read(cbio)) { 1564 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv); 1565 } else if (BIO_should_write(cbio)) { 1566 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); 1567 } else { 1568 BIO_puts(bio_err, "Unexpected retry condition\n"); 1569 goto err; 1570 } 1571 if (rv == 0) { 1572 BIO_puts(bio_err, "Timeout on request\n"); 1573 break; 1574 } 1575 if (rv == -1) { 1576 BIO_puts(bio_err, "Select error\n"); 1577 break; 1578 } 1579 1580 } 1581 err: 1582 OCSP_REQ_CTX_free(ctx); 1583 1584 return rsp; 1585 } 1586 1587 OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, 1588 const char *host, const char *path, 1589 const char *port, int use_ssl, 1590 STACK_OF(CONF_VALUE) *headers, 1591 int req_timeout) 1592 { 1593 BIO *cbio = NULL; 1594 SSL_CTX *ctx = NULL; 1595 OCSP_RESPONSE *resp = NULL; 1596 1597 cbio = BIO_new_connect(host); 1598 if (cbio == NULL) { 1599 BIO_printf(bio_err, "Error creating connect BIO\n"); 1600 goto end; 1601 } 1602 if (port != NULL) 1603 BIO_set_conn_port(cbio, port); 1604 if (use_ssl == 1) { 1605 BIO *sbio; 1606 ctx = SSL_CTX_new(TLS_client_method()); 1607 if (ctx == NULL) { 1608 BIO_printf(bio_err, "Error creating SSL context.\n"); 1609 goto end; 1610 } 1611 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); 1612 sbio = BIO_new_ssl(ctx, 1); 1613 cbio = BIO_push(sbio, cbio); 1614 } 1615 1616 resp = query_responder(cbio, host, path, headers, req, req_timeout); 1617 if (resp == NULL) 1618 BIO_printf(bio_err, "Error querying OCSP responder\n"); 1619 end: 1620 BIO_free_all(cbio); 1621 SSL_CTX_free(ctx); 1622 return resp; 1623 } 1624 # endif 1625 1626 #endif 1627