1 /* $OpenBSD: s_server.c,v 1.49 2021/08/29 13:16:17 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 /* ==================================================================== 59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111 /* ==================================================================== 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 113 * ECC cipher suite support in OpenSSL originally developed by 114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 115 */ 116 /* ==================================================================== 117 * Copyright 2005 Nokia. All rights reserved. 118 * 119 * The portions of the attached software ("Contribution") is developed by 120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 121 * license. 122 * 123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 125 * support (see RFC 4279) to OpenSSL. 126 * 127 * No patent licenses or other rights except those expressly stated in 128 * the OpenSSL open source license shall be deemed granted or received 129 * expressly, by implication, estoppel, or otherwise. 130 * 131 * No assurances are provided by Nokia that the Contribution does not 132 * infringe the patent or other intellectual property rights of any third 133 * party or that the license provides you with all the necessary rights 134 * to make use of the Contribution. 135 * 136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 140 * OTHERWISE. 141 */ 142 143 /* Until the key-gen callbacks are modified to use newer prototypes, we allow 144 * deprecated functions for openssl-internal code */ 145 #ifdef OPENSSL_NO_DEPRECATED 146 #undef OPENSSL_NO_DEPRECATED 147 #endif 148 149 #include <sys/types.h> 150 #include <sys/socket.h> 151 152 #include <assert.h> 153 #include <ctype.h> 154 #include <stdio.h> 155 #include <stdlib.h> 156 #include <limits.h> 157 #include <string.h> 158 #include <unistd.h> 159 #include <poll.h> 160 161 #include "apps.h" 162 163 #include <openssl/bn.h> 164 #include <openssl/err.h> 165 #include <openssl/lhash.h> 166 #include <openssl/ocsp.h> 167 #include <openssl/pem.h> 168 #include <openssl/ssl.h> 169 #include <openssl/x509.h> 170 171 #ifndef OPENSSL_NO_DH 172 #include <openssl/dh.h> 173 #endif 174 175 #include <openssl/rsa.h> 176 177 #include "s_apps.h" 178 #include "timeouts.h" 179 180 static void s_server_init(void); 181 static void sv_usage(void); 182 static void print_stats(BIO *bp, SSL_CTX *ctx); 183 static int sv_body(char *hostname, int s, unsigned char *context); 184 static void close_accept_socket(void); 185 static int init_ssl_connection(SSL *s); 186 #ifndef OPENSSL_NO_DH 187 static DH *load_dh_param(const char *dhfile); 188 #endif 189 static int www_body(char *hostname, int s, unsigned char *context); 190 static int generate_session_id(const SSL *ssl, unsigned char *id, 191 unsigned int *id_len); 192 static int ssl_servername_cb(SSL *s, int *ad, void *arg); 193 static int cert_status_cb(SSL * s, void *arg); 194 static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen, 195 const unsigned char *in, unsigned int inlen, void *arg); 196 /* static int load_CA(SSL_CTX *ctx, char *file);*/ 197 198 #define BUFSIZZ 16*1024 199 static int bufsize = BUFSIZZ; 200 static int accept_socket = -1; 201 202 #define TEST_CERT "server.pem" 203 #define TEST_CERT2 "server2.pem" 204 205 static int s_server_session_id_context = 1; /* anything will do */ 206 static SSL_CTX *ctx = NULL; 207 static SSL_CTX *ctx2 = NULL; 208 static BIO *bio_s_out = NULL; 209 210 static int local_argc = 0; 211 static char **local_argv; 212 213 /* This is a context that we pass to callbacks */ 214 typedef struct tlsextctx_st { 215 char *servername; 216 BIO *biodebug; 217 int extension_error; 218 } tlsextctx; 219 220 /* Structure passed to cert status callback */ 221 typedef struct tlsextstatusctx_st { 222 /* Default responder to use */ 223 char *host, *path, *port; 224 int use_ssl; 225 int timeout; 226 BIO *err; 227 int verbose; 228 } tlsextstatusctx; 229 230 /* This the context that we pass to alpn_cb */ 231 typedef struct tlsextalpnctx_st { 232 unsigned char *data; 233 unsigned short len; 234 } tlsextalpnctx; 235 236 static struct { 237 char *alpn_in; 238 char *npn_in; /* Ignored. */ 239 int bugs; 240 char *CAfile; 241 char *CApath; 242 #ifndef OPENSSL_NO_DTLS 243 int cert_chain; 244 #endif 245 char *cert_file; 246 char *cert_file2; 247 int cert_format; 248 char *cipher; 249 unsigned char *context; 250 int crlf; 251 char *dcert_file; 252 int dcert_format; 253 int debug; 254 char *dhfile; 255 char *dkey_file; 256 int dkey_format; 257 char *dpassarg; 258 int enable_timeouts; 259 const char *errstr; 260 char *groups_in; 261 char *key_file; 262 char *key_file2; 263 int key_format; 264 char *keymatexportlabel; 265 int keymatexportlen; 266 uint16_t max_version; 267 uint16_t min_version; 268 const SSL_METHOD *meth; 269 int msg; 270 int naccept; 271 char *named_curve; 272 int nbio; 273 int nbio_test; 274 int no_cache; 275 int nocert; 276 int no_dhe; 277 int no_ecdhe; 278 int no_tmp_rsa; /* No-op. */ 279 int off; 280 char *passarg; 281 short port; 282 int quiet; 283 int server_verify; 284 char *session_id_prefix; 285 long socket_mtu; 286 int socket_type; 287 #ifndef OPENSSL_NO_SRTP 288 char *srtp_profiles; 289 #endif 290 int state; 291 tlsextstatusctx tlscstatp; 292 tlsextctx tlsextcbp; 293 int tlsextdebug; 294 int tlsextstatus; 295 X509_VERIFY_PARAM *vpm; 296 int www; 297 } s_server_config; 298 299 static int 300 s_server_opt_context(char *arg) 301 { 302 s_server_config.context = (unsigned char *) arg; 303 return (0); 304 } 305 306 static int 307 s_server_opt_keymatexportlen(char *arg) 308 { 309 s_server_config.keymatexportlen = strtonum(arg, 1, INT_MAX, 310 &s_server_config.errstr); 311 if (s_server_config.errstr != NULL) { 312 BIO_printf(bio_err, "invalid argument %s: %s\n", 313 arg, s_server_config.errstr); 314 return (1); 315 } 316 return (0); 317 } 318 319 #ifndef OPENSSL_NO_DTLS 320 static int 321 s_server_opt_mtu(char *arg) 322 { 323 s_server_config.socket_mtu = strtonum(arg, 0, LONG_MAX, 324 &s_server_config.errstr); 325 if (s_server_config.errstr != NULL) { 326 BIO_printf(bio_err, "invalid argument %s: %s\n", 327 arg, s_server_config.errstr); 328 return (1); 329 } 330 return (0); 331 } 332 #endif 333 334 #ifndef OPENSSL_NO_DTLS 335 static int 336 s_server_opt_protocol_version_dtls(void) 337 { 338 s_server_config.meth = DTLS_server_method(); 339 s_server_config.socket_type = SOCK_DGRAM; 340 return (0); 341 } 342 #endif 343 344 #ifndef OPENSSL_NO_DTLS1 345 static int 346 s_server_opt_protocol_version_dtls1(void) 347 { 348 s_server_config.meth = DTLS_server_method(); 349 s_server_config.min_version = DTLS1_VERSION; 350 s_server_config.max_version = DTLS1_VERSION; 351 s_server_config.socket_type = SOCK_DGRAM; 352 return (0); 353 } 354 #endif 355 356 #ifndef OPENSSL_NO_DTLS1_2 357 static int 358 s_server_opt_protocol_version_dtls1_2(void) 359 { 360 s_server_config.meth = DTLS_server_method(); 361 s_server_config.min_version = DTLS1_2_VERSION; 362 s_server_config.max_version = DTLS1_2_VERSION; 363 s_server_config.socket_type = SOCK_DGRAM; 364 return (0); 365 } 366 #endif 367 368 static int 369 s_server_opt_protocol_version_tls1(void) 370 { 371 s_server_config.min_version = TLS1_VERSION; 372 s_server_config.max_version = TLS1_VERSION; 373 return (0); 374 } 375 376 static int 377 s_server_opt_protocol_version_tls1_1(void) 378 { 379 s_server_config.min_version = TLS1_1_VERSION; 380 s_server_config.max_version = TLS1_1_VERSION; 381 return (0); 382 } 383 384 static int 385 s_server_opt_protocol_version_tls1_2(void) 386 { 387 s_server_config.min_version = TLS1_2_VERSION; 388 s_server_config.max_version = TLS1_2_VERSION; 389 return (0); 390 } 391 392 static int 393 s_server_opt_protocol_version_tls1_3(void) 394 { 395 s_server_config.min_version = TLS1_3_VERSION; 396 s_server_config.max_version = TLS1_3_VERSION; 397 return (0); 398 } 399 400 static int 401 s_server_opt_nbio_test(void) 402 { 403 s_server_config.nbio = 1; 404 s_server_config.nbio_test = 1; 405 return (0); 406 } 407 408 static int 409 s_server_opt_port(char *arg) 410 { 411 if (!extract_port(arg, &s_server_config.port)) 412 return (1); 413 return (0); 414 } 415 416 static int 417 s_server_opt_status_timeout(char *arg) 418 { 419 s_server_config.tlsextstatus = 1; 420 s_server_config.tlscstatp.timeout = strtonum(arg, 0, INT_MAX, 421 &s_server_config.errstr); 422 if (s_server_config.errstr != NULL) { 423 BIO_printf(bio_err, "invalid argument %s: %s\n", 424 arg, s_server_config.errstr); 425 return (1); 426 } 427 return (0); 428 } 429 430 static int 431 s_server_opt_status_url(char *arg) 432 { 433 s_server_config.tlsextstatus = 1; 434 if (!OCSP_parse_url(arg, &s_server_config.tlscstatp.host, 435 &s_server_config.tlscstatp.port, &s_server_config.tlscstatp.path, 436 &s_server_config.tlscstatp.use_ssl)) { 437 BIO_printf(bio_err, "Error parsing URL\n"); 438 return (1); 439 } 440 return (0); 441 } 442 443 static int 444 s_server_opt_status_verbose(void) 445 { 446 s_server_config.tlsextstatus = 1; 447 s_server_config.tlscstatp.verbose = 1; 448 return (0); 449 } 450 451 static int 452 s_server_opt_verify(char *arg) 453 { 454 s_server_config.server_verify = SSL_VERIFY_PEER | 455 SSL_VERIFY_CLIENT_ONCE; 456 verify_depth = strtonum(arg, 0, INT_MAX, &s_server_config.errstr); 457 if (s_server_config.errstr != NULL) { 458 BIO_printf(bio_err, "invalid argument %s: %s\n", 459 arg, s_server_config.errstr); 460 return (1); 461 } 462 BIO_printf(bio_err, "verify depth is %d\n", verify_depth); 463 return (0); 464 } 465 466 static int 467 s_server_opt_verify_fail(char *arg) 468 { 469 s_server_config.server_verify = SSL_VERIFY_PEER | 470 SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE; 471 verify_depth = strtonum(arg, 0, INT_MAX, &s_server_config.errstr); 472 if (s_server_config.errstr != NULL) { 473 BIO_printf(bio_err, "invalid argument %s: %s\n", 474 arg, s_server_config.errstr); 475 return (1); 476 } 477 BIO_printf(bio_err, "verify depth is %d, must return a certificate\n", 478 verify_depth); 479 return (0); 480 } 481 482 static int 483 s_server_opt_verify_param(int argc, char **argv, int *argsused) 484 { 485 char **pargs = argv; 486 int pargc = argc; 487 int badarg = 0; 488 489 if (!args_verify(&pargs, &pargc, &badarg, bio_err, 490 &s_server_config.vpm)) { 491 BIO_printf(bio_err, "unknown option %s\n", *argv); 492 return (1); 493 } 494 if (badarg) 495 return (1); 496 497 *argsused = argc - pargc; 498 return (0); 499 } 500 501 static const struct option s_server_options[] = { 502 { 503 .name = "4", 504 .type = OPTION_DISCARD, 505 }, 506 { 507 .name = "6", 508 .type = OPTION_DISCARD, 509 }, 510 { 511 .name = "accept", 512 .argname = "port", 513 .desc = "Port to accept on (default is 4433)", 514 .type = OPTION_ARG_FUNC, 515 .opt.argfunc = s_server_opt_port, 516 }, 517 { 518 .name = "alpn", 519 .argname = "protocols", 520 .desc = "Set the advertised protocols for the ALPN extension" 521 " (comma-separated list)", 522 .type = OPTION_ARG, 523 .opt.arg = &s_server_config.alpn_in, 524 }, 525 { 526 .name = "bugs", 527 .desc = "Turn on SSL bug compatibility", 528 .type = OPTION_FLAG, 529 .opt.flag = &s_server_config.bugs, 530 }, 531 { 532 .name = "CAfile", 533 .argname = "file", 534 .desc = "PEM format file of CA certificates", 535 .type = OPTION_ARG, 536 .opt.arg = &s_server_config.CAfile, 537 }, 538 { 539 .name = "CApath", 540 .argname = "directory", 541 .desc = "PEM format directory of CA certificates", 542 .type = OPTION_ARG, 543 .opt.arg = &s_server_config.CApath, 544 }, 545 { 546 .name = "cert", 547 .argname = "file", 548 .desc = "Certificate file to use\n" 549 "(default is " TEST_CERT ")", 550 .type = OPTION_ARG, 551 .opt.arg = &s_server_config.cert_file, 552 }, 553 { 554 .name = "cert2", 555 .argname = "file", 556 .desc = "Certificate file to use for servername\n" 557 "(default is " TEST_CERT2 ")", 558 .type = OPTION_ARG, 559 .opt.arg = &s_server_config.cert_file2, 560 }, 561 { 562 .name = "certform", 563 .argname = "fmt", 564 .desc = "Certificate format (PEM or DER) PEM default", 565 .type = OPTION_ARG_FORMAT, 566 .opt.value = &s_server_config.cert_format, 567 }, 568 #ifndef OPENSSL_NO_DTLS 569 { 570 .name = "chain", 571 .type = OPTION_FLAG, 572 .opt.flag = &s_server_config.cert_chain, 573 }, 574 #endif 575 { 576 .name = "cipher", 577 .argname = "list", 578 .desc = "List of ciphers to enable (see `openssl ciphers`)", 579 .type = OPTION_ARG, 580 .opt.arg = &s_server_config.cipher, 581 }, 582 { 583 .name = "context", 584 .argname = "id", 585 .desc = "Set session ID context", 586 .type = OPTION_ARG_FUNC, 587 .opt.argfunc = s_server_opt_context, 588 }, 589 { 590 .name = "crlf", 591 .desc = "Convert LF from terminal into CRLF", 592 .type = OPTION_FLAG, 593 .opt.flag = &s_server_config.crlf, 594 }, 595 { 596 .name = "dcert", 597 .argname = "file", 598 .desc = "Second certificate file to use (usually for DSA)", 599 .type = OPTION_ARG, 600 .opt.arg = &s_server_config.dcert_file, 601 }, 602 { 603 .name = "dcertform", 604 .argname = "fmt", 605 .desc = "Second certificate format (PEM or DER) PEM default", 606 .type = OPTION_ARG_FORMAT, 607 .opt.value = &s_server_config.dcert_format, 608 }, 609 { 610 .name = "debug", 611 .desc = "Print more output", 612 .type = OPTION_FLAG, 613 .opt.flag = &s_server_config.debug, 614 }, 615 { 616 .name = "dhparam", 617 .argname = "file", 618 .desc = "DH parameter file to use, in cert file if not specified", 619 .type = OPTION_ARG, 620 .opt.arg = &s_server_config.dhfile, 621 }, 622 { 623 .name = "dkey", 624 .argname = "file", 625 .desc = "Second private key file to use (usually for DSA)", 626 .type = OPTION_ARG, 627 .opt.arg = &s_server_config.dkey_file, 628 }, 629 { 630 .name = "dkeyform", 631 .argname = "fmt", 632 .desc = "Second key format (PEM or DER) PEM default", 633 .type = OPTION_ARG_FORMAT, 634 .opt.value = &s_server_config.dkey_format, 635 }, 636 { 637 .name = "dpass", 638 .argname = "arg", 639 .desc = "Second private key file pass phrase source", 640 .type = OPTION_ARG, 641 .opt.arg = &s_server_config.dpassarg, 642 }, 643 #ifndef OPENSSL_NO_DTLS 644 { 645 .name = "dtls", 646 .desc = "Use any version of DTLS", 647 .type = OPTION_FUNC, 648 .opt.func = s_server_opt_protocol_version_dtls, 649 }, 650 #endif 651 #ifndef OPENSSL_NO_DTLS1 652 { 653 .name = "dtls1", 654 .desc = "Just use DTLSv1", 655 .type = OPTION_FUNC, 656 .opt.func = s_server_opt_protocol_version_dtls1, 657 }, 658 #endif 659 #ifndef OPENSSL_NO_DTLS1_2 660 { 661 .name = "dtls1_2", 662 .desc = "Just use DTLSv1.2", 663 .type = OPTION_FUNC, 664 .opt.func = s_server_opt_protocol_version_dtls1_2, 665 }, 666 #endif 667 { 668 .name = "groups", 669 .argname = "list", 670 .desc = "Specify EC groups (colon-separated list)", 671 .type = OPTION_ARG, 672 .opt.arg = &s_server_config.groups_in, 673 }, 674 { 675 .name = "HTTP", 676 .desc = "Respond to a 'GET /<path> HTTP/1.0' with file ./<path>", 677 .type = OPTION_VALUE, 678 .opt.value = &s_server_config.www, 679 .value = 3, 680 }, 681 { 682 .name = "id_prefix", 683 .argname = "arg", 684 .desc = "Generate SSL/TLS session IDs prefixed by 'arg'", 685 .type = OPTION_ARG, 686 .opt.arg = &s_server_config.session_id_prefix, 687 }, 688 { 689 .name = "key", 690 .argname = "file", 691 .desc = "Private Key file to use, in cert file if\n" 692 "not specified (default is " TEST_CERT ")", 693 .type = OPTION_ARG, 694 .opt.arg = &s_server_config.key_file, 695 }, 696 { 697 .name = "key2", 698 .argname = "file", 699 .desc = "Private Key file to use for servername, in cert file if\n" 700 "not specified (default is " TEST_CERT2 ")", 701 .type = OPTION_ARG, 702 .opt.arg = &s_server_config.key_file2, 703 }, 704 { 705 .name = "keyform", 706 .argname = "fmt", 707 .desc = "Key format (PEM or DER) PEM default", 708 .type = OPTION_ARG_FORMAT, 709 .opt.value = &s_server_config.key_format, 710 }, 711 { 712 .name = "keymatexport", 713 .argname = "label", 714 .desc = "Export keying material using label", 715 .type = OPTION_ARG, 716 .opt.arg = &s_server_config.keymatexportlabel, 717 }, 718 { 719 .name = "keymatexportlen", 720 .argname = "len", 721 .desc = "Export len bytes of keying material (default 20)", 722 .type = OPTION_ARG_FUNC, 723 .opt.argfunc = s_server_opt_keymatexportlen, 724 }, 725 { 726 .name = "legacy_renegotiation", 727 .type = OPTION_DISCARD, 728 }, 729 { 730 .name = "msg", 731 .desc = "Show protocol messages", 732 .type = OPTION_FLAG, 733 .opt.flag = &s_server_config.msg, 734 }, 735 #ifndef OPENSSL_NO_DTLS 736 { 737 .name = "mtu", 738 .argname = "mtu", 739 .desc = "Set link layer MTU", 740 .type = OPTION_ARG_FUNC, 741 .opt.argfunc = s_server_opt_mtu, 742 }, 743 #endif 744 { 745 .name = "naccept", 746 .argname = "num", 747 .desc = "Terminate after num connections", 748 .type = OPTION_ARG_INT, 749 .opt.value = &s_server_config.naccept 750 }, 751 { 752 .name = "named_curve", 753 .argname = "arg", 754 .type = OPTION_ARG, 755 .opt.arg = &s_server_config.named_curve, 756 }, 757 { 758 .name = "nbio", 759 .desc = "Run with non-blocking I/O", 760 .type = OPTION_FLAG, 761 .opt.flag = &s_server_config.nbio, 762 }, 763 { 764 .name = "nbio_test", 765 .desc = "Test with the non-blocking test bio", 766 .type = OPTION_FUNC, 767 .opt.func = s_server_opt_nbio_test, 768 }, 769 { 770 .name = "nextprotoneg", 771 .argname = "arg", 772 .type = OPTION_ARG, 773 .opt.arg = &s_server_config.npn_in, /* Ignored. */ 774 }, 775 { 776 .name = "no_cache", 777 .desc = "Disable session cache", 778 .type = OPTION_FLAG, 779 .opt.flag = &s_server_config.no_cache, 780 }, 781 { 782 .name = "no_comp", 783 .desc = "Disable SSL/TLS compression", 784 .type = OPTION_VALUE_OR, 785 .opt.value = &s_server_config.off, 786 .value = SSL_OP_NO_COMPRESSION, 787 }, 788 { 789 .name = "no_dhe", 790 .desc = "Disable ephemeral DH", 791 .type = OPTION_FLAG, 792 .opt.flag = &s_server_config.no_dhe, 793 }, 794 { 795 .name = "no_ecdhe", 796 .desc = "Disable ephemeral ECDH", 797 .type = OPTION_FLAG, 798 .opt.flag = &s_server_config.no_ecdhe, 799 }, 800 { 801 .name = "no_ticket", 802 .desc = "Disable use of RFC4507bis session tickets", 803 .type = OPTION_VALUE_OR, 804 .opt.value = &s_server_config.off, 805 .value = SSL_OP_NO_TICKET, 806 }, 807 { 808 .name = "no_ssl2", 809 .type = OPTION_VALUE_OR, 810 .opt.value = &s_server_config.off, 811 .value = SSL_OP_NO_SSLv2, 812 }, 813 { 814 .name = "no_ssl3", 815 .type = OPTION_VALUE_OR, 816 .opt.value = &s_server_config.off, 817 .value = SSL_OP_NO_SSLv3, 818 }, 819 { 820 .name = "no_tls1", 821 .desc = "Just disable TLSv1", 822 .type = OPTION_VALUE_OR, 823 .opt.value = &s_server_config.off, 824 .value = SSL_OP_NO_TLSv1, 825 }, 826 { 827 .name = "no_tls1_1", 828 .desc = "Just disable TLSv1.1", 829 .type = OPTION_VALUE_OR, 830 .opt.value = &s_server_config.off, 831 .value = SSL_OP_NO_TLSv1_1, 832 }, 833 { 834 .name = "no_tls1_2", 835 .desc = "Just disable TLSv1.2", 836 .type = OPTION_VALUE_OR, 837 .opt.value = &s_server_config.off, 838 .value = SSL_OP_NO_TLSv1_2, 839 }, 840 { 841 .name = "no_tls1_3", 842 .desc = "Just disable TLSv1.3", 843 .type = OPTION_VALUE_OR, 844 .opt.value = &s_server_config.off, 845 .value = SSL_OP_NO_TLSv1_3, 846 }, 847 { 848 .name = "no_tmp_rsa", 849 .type = OPTION_DISCARD, 850 }, 851 { 852 .name = "nocert", 853 .desc = "Don't use any certificates (Anon-DH)", 854 .type = OPTION_FLAG, 855 .opt.flag = &s_server_config.nocert, 856 }, 857 { 858 .name = "pass", 859 .argname = "arg", 860 .desc = "Private key file pass phrase source", 861 .type = OPTION_ARG, 862 .opt.arg = &s_server_config.passarg, 863 }, 864 { 865 .name = "port", 866 .argname = "port", 867 .type = OPTION_ARG_FUNC, 868 .opt.argfunc = s_server_opt_port, 869 }, 870 { 871 .name = "quiet", 872 .desc = "Inhibit printing of session and certificate information", 873 .type = OPTION_FLAG, 874 .opt.flag = &s_server_config.quiet, 875 }, 876 { 877 .name = "servername", 878 .argname = "name", 879 .desc = "Servername for HostName TLS extension", 880 .type = OPTION_ARG, 881 .opt.arg = &s_server_config.tlsextcbp.servername, 882 }, 883 { 884 .name = "servername_fatal", 885 .desc = "On mismatch send fatal alert (default warning alert)", 886 .type = OPTION_VALUE, 887 .opt.value = &s_server_config.tlsextcbp.extension_error, 888 .value = SSL_TLSEXT_ERR_ALERT_FATAL, 889 }, 890 { 891 .name = "serverpref", 892 .desc = "Use server's cipher preferences", 893 .type = OPTION_VALUE_OR, 894 .opt.value = &s_server_config.off, 895 .value = SSL_OP_CIPHER_SERVER_PREFERENCE, 896 }, 897 { 898 .name = "state", 899 .desc = "Print the SSL states", 900 .type = OPTION_FLAG, 901 .opt.flag = &s_server_config.state, 902 }, 903 { 904 .name = "status", 905 .desc = "Respond to certificate status requests", 906 .type = OPTION_FLAG, 907 .opt.flag = &s_server_config.tlsextstatus, 908 }, 909 { 910 .name = "status_timeout", 911 .argname = "nsec", 912 .desc = "Status request responder timeout", 913 .type = OPTION_ARG_FUNC, 914 .opt.argfunc = s_server_opt_status_timeout, 915 }, 916 { 917 .name = "status_url", 918 .argname = "url", 919 .desc = "Status request fallback URL", 920 .type = OPTION_ARG_FUNC, 921 .opt.argfunc = s_server_opt_status_url, 922 }, 923 { 924 .name = "status_verbose", 925 .desc = "Enable status request verbose printout", 926 .type = OPTION_FUNC, 927 .opt.func = s_server_opt_status_verbose, 928 }, 929 #ifndef OPENSSL_NO_DTLS 930 { 931 .name = "timeout", 932 .desc = "Enable timeouts", 933 .type = OPTION_FLAG, 934 .opt.flag = &s_server_config.enable_timeouts, 935 }, 936 #endif 937 { 938 .name = "tls1", 939 .desc = "Just talk TLSv1", 940 .type = OPTION_FUNC, 941 .opt.func = s_server_opt_protocol_version_tls1, 942 }, 943 { 944 .name = "tls1_1", 945 .desc = "Just talk TLSv1.1", 946 .type = OPTION_FUNC, 947 .opt.func = s_server_opt_protocol_version_tls1_1, 948 }, 949 { 950 .name = "tls1_2", 951 .desc = "Just talk TLSv1.2", 952 .type = OPTION_FUNC, 953 .opt.func = s_server_opt_protocol_version_tls1_2, 954 }, 955 { 956 .name = "tls1_3", 957 .desc = "Just talk TLSv1.3", 958 .type = OPTION_FUNC, 959 .opt.func = s_server_opt_protocol_version_tls1_3, 960 }, 961 { 962 .name = "tlsextdebug", 963 .desc = "Hex dump of all TLS extensions received", 964 .type = OPTION_FLAG, 965 .opt.flag = &s_server_config.tlsextdebug, 966 }, 967 #ifndef OPENSSL_NO_SRTP 968 { 969 .name = "use_srtp", 970 .argname = "profiles", 971 .desc = "Offer SRTP key management with a colon-separated profile list", 972 .type = OPTION_ARG, 973 .opt.arg = &s_server_config.srtp_profiles, 974 }, 975 #endif 976 { 977 .name = "Verify", 978 .argname = "depth", 979 .desc = "Turn on peer certificate verification, must have a cert", 980 .type = OPTION_ARG_FUNC, 981 .opt.argfunc = s_server_opt_verify_fail, 982 }, 983 { 984 .name = "verify", 985 .argname = "depth", 986 .desc = "Turn on peer certificate verification", 987 .type = OPTION_ARG_FUNC, 988 .opt.argfunc = s_server_opt_verify, 989 }, 990 { 991 .name = "verify_return_error", 992 .desc = "Return verification error", 993 .type = OPTION_FLAG, 994 .opt.flag = &verify_return_error, 995 }, 996 { 997 .name = "WWW", 998 .desc = "Respond to a 'GET /<path> HTTP/1.0' with file ./<path>", 999 .type = OPTION_VALUE, 1000 .opt.value = &s_server_config.www, 1001 .value = 2, 1002 }, 1003 { 1004 .name = "www", 1005 .desc = "Respond to a 'GET /' with a status page", 1006 .type = OPTION_VALUE, 1007 .opt.value = &s_server_config.www, 1008 .value = 1, 1009 }, 1010 { 1011 .name = NULL, 1012 .desc = "", 1013 .type = OPTION_ARGV_FUNC, 1014 .opt.argvfunc = s_server_opt_verify_param, 1015 }, 1016 { NULL }, 1017 }; 1018 1019 static void 1020 s_server_init(void) 1021 { 1022 accept_socket = -1; 1023 s_server_config.cipher = NULL; 1024 s_server_config.server_verify = SSL_VERIFY_NONE; 1025 s_server_config.dcert_file = NULL; 1026 s_server_config.dkey_file = NULL; 1027 s_server_config.cert_file = TEST_CERT; 1028 s_server_config.key_file = NULL; 1029 s_server_config.cert_file2 = TEST_CERT2; 1030 s_server_config.key_file2 = NULL; 1031 ctx2 = NULL; 1032 s_server_config.nbio = 0; 1033 s_server_config.nbio_test = 0; 1034 ctx = NULL; 1035 s_server_config.www = 0; 1036 1037 bio_s_out = NULL; 1038 s_server_config.debug = 0; 1039 s_server_config.msg = 0; 1040 s_server_config.quiet = 0; 1041 } 1042 1043 static void 1044 sv_usage(void) 1045 { 1046 fprintf(stderr, "usage: s_server " 1047 "[-accept port] [-alpn protocols] [-bugs] [-CAfile file]\n" 1048 " [-CApath directory] [-cert file] [-cert2 file]\n" 1049 " [-certform der | pem] [-cipher cipherlist]\n" 1050 " [-context id] [-crl_check] [-crl_check_all] [-crlf]\n" 1051 " [-dcert file] [-dcertform der | pem] [-debug]\n" 1052 " [-dhparam file] [-dkey file] [-dkeyform der | pem]\n" 1053 " [-dpass arg] [-dtls] [-dtls1] [-dtls1_2] [-groups list] [-HTTP]\n" 1054 " [-id_prefix arg] [-key keyfile] [-key2 keyfile]\n" 1055 " [-keyform der | pem] [-keymatexport label]\n" 1056 " [-keymatexportlen len] [-msg] [-mtu mtu] [-naccept num]\n" 1057 " [-named_curve arg] [-nbio] [-nbio_test] [-no_cache]\n" 1058 " [-no_dhe] [-no_ecdhe] [-no_ticket] [-no_tls1]\n" 1059 " [-no_tls1_1] [-no_tls1_2] [-no_tls1_3] [-no_tmp_rsa]\n" 1060 " [-nocert] [-pass arg] [-quiet] [-servername name]\n" 1061 " [-servername_fatal] [-serverpref] [-state] [-status]\n" 1062 " [-status_timeout nsec] [-status_url url]\n" 1063 " [-status_verbose] [-timeout] [-tls1] [-tls1_1]\n" 1064 " [-tls1_2] [-tls1_3] [-tlsextdebug] [-use_srtp profiles]\n" 1065 " [-Verify depth] [-verify depth] [-verify_return_error]\n" 1066 " [-WWW] [-www]\n"); 1067 fprintf(stderr, "\n"); 1068 options_usage(s_server_options); 1069 fprintf(stderr, "\n"); 1070 } 1071 1072 int 1073 s_server_main(int argc, char *argv[]) 1074 { 1075 int badop = 0; 1076 int ret = 1; 1077 char *pass = NULL; 1078 char *dpass = NULL; 1079 X509 *s_cert = NULL, *s_dcert = NULL; 1080 EVP_PKEY *s_key = NULL, *s_dkey = NULL; 1081 EVP_PKEY *s_key2 = NULL; 1082 X509 *s_cert2 = NULL; 1083 tlsextalpnctx alpn_ctx = { NULL, 0 }; 1084 1085 if (single_execution) { 1086 if (pledge("stdio rpath inet dns tty", NULL) == -1) { 1087 perror("pledge"); 1088 exit(1); 1089 } 1090 } 1091 1092 memset(&s_server_config, 0, sizeof(s_server_config)); 1093 s_server_config.keymatexportlen = 20; 1094 s_server_config.meth = TLS_server_method(); 1095 s_server_config.naccept = -1; 1096 s_server_config.port = PORT; 1097 s_server_config.cert_file = TEST_CERT; 1098 s_server_config.cert_file2 = TEST_CERT2; 1099 s_server_config.cert_format = FORMAT_PEM; 1100 s_server_config.dcert_format = FORMAT_PEM; 1101 s_server_config.dkey_format = FORMAT_PEM; 1102 s_server_config.key_format = FORMAT_PEM; 1103 s_server_config.server_verify = SSL_VERIFY_NONE; 1104 s_server_config.socket_type = SOCK_STREAM; 1105 s_server_config.tlscstatp.timeout = -1; 1106 s_server_config.tlsextcbp.extension_error = 1107 SSL_TLSEXT_ERR_ALERT_WARNING; 1108 1109 local_argc = argc; 1110 local_argv = argv; 1111 1112 s_server_init(); 1113 1114 verify_depth = 0; 1115 1116 if (options_parse(argc, argv, s_server_options, NULL, NULL) != 0) { 1117 badop = 1; 1118 goto bad; 1119 } 1120 if (badop) { 1121 bad: 1122 if (s_server_config.errstr == NULL) 1123 sv_usage(); 1124 goto end; 1125 } 1126 1127 if (!app_passwd(bio_err, s_server_config.passarg, 1128 s_server_config.dpassarg, &pass, &dpass)) { 1129 BIO_printf(bio_err, "Error getting password\n"); 1130 goto end; 1131 } 1132 if (s_server_config.key_file == NULL) 1133 s_server_config.key_file = s_server_config.cert_file; 1134 if (s_server_config.key_file2 == NULL) 1135 s_server_config.key_file2 = s_server_config.cert_file2; 1136 1137 if (s_server_config.nocert == 0) { 1138 s_key = load_key(bio_err, s_server_config.key_file, 1139 s_server_config.key_format, 0, pass, 1140 "server certificate private key file"); 1141 if (!s_key) { 1142 ERR_print_errors(bio_err); 1143 goto end; 1144 } 1145 s_cert = load_cert(bio_err, s_server_config.cert_file, 1146 s_server_config.cert_format, 1147 NULL, "server certificate file"); 1148 1149 if (!s_cert) { 1150 ERR_print_errors(bio_err); 1151 goto end; 1152 } 1153 if (s_server_config.tlsextcbp.servername) { 1154 s_key2 = load_key(bio_err, s_server_config.key_file2, 1155 s_server_config.key_format, 0, pass, 1156 "second server certificate private key file"); 1157 if (!s_key2) { 1158 ERR_print_errors(bio_err); 1159 goto end; 1160 } 1161 s_cert2 = load_cert(bio_err, s_server_config.cert_file2, 1162 s_server_config.cert_format, 1163 NULL, "second server certificate file"); 1164 1165 if (!s_cert2) { 1166 ERR_print_errors(bio_err); 1167 goto end; 1168 } 1169 } 1170 } 1171 alpn_ctx.data = NULL; 1172 if (s_server_config.alpn_in) { 1173 unsigned short len; 1174 alpn_ctx.data = next_protos_parse(&len, 1175 s_server_config.alpn_in); 1176 if (alpn_ctx.data == NULL) 1177 goto end; 1178 alpn_ctx.len = len; 1179 } 1180 1181 if (s_server_config.dcert_file) { 1182 1183 if (s_server_config.dkey_file == NULL) 1184 s_server_config.dkey_file = s_server_config.dcert_file; 1185 1186 s_dkey = load_key(bio_err, s_server_config.dkey_file, 1187 s_server_config.dkey_format, 1188 0, dpass, "second certificate private key file"); 1189 if (!s_dkey) { 1190 ERR_print_errors(bio_err); 1191 goto end; 1192 } 1193 s_dcert = load_cert(bio_err, s_server_config.dcert_file, 1194 s_server_config.dcert_format, 1195 NULL, "second server certificate file"); 1196 1197 if (!s_dcert) { 1198 ERR_print_errors(bio_err); 1199 goto end; 1200 } 1201 } 1202 if (bio_s_out == NULL) { 1203 if (s_server_config.quiet && !s_server_config.debug && 1204 !s_server_config.msg) { 1205 bio_s_out = BIO_new(BIO_s_null()); 1206 } else { 1207 if (bio_s_out == NULL) 1208 bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE); 1209 } 1210 } 1211 if (s_server_config.nocert) { 1212 s_server_config.cert_file = NULL; 1213 s_server_config.key_file = NULL; 1214 s_server_config.dcert_file = NULL; 1215 s_server_config.dkey_file = NULL; 1216 s_server_config.cert_file2 = NULL; 1217 s_server_config.key_file2 = NULL; 1218 } 1219 ctx = SSL_CTX_new(s_server_config.meth); 1220 if (ctx == NULL) { 1221 ERR_print_errors(bio_err); 1222 goto end; 1223 } 1224 1225 SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY); 1226 1227 if (!SSL_CTX_set_min_proto_version(ctx, s_server_config.min_version)) 1228 goto end; 1229 if (!SSL_CTX_set_max_proto_version(ctx, s_server_config.max_version)) 1230 goto end; 1231 1232 if (s_server_config.session_id_prefix) { 1233 if (strlen(s_server_config.session_id_prefix) >= 32) 1234 BIO_printf(bio_err, 1235 "warning: id_prefix is too long, only one new session will be possible\n"); 1236 else if (strlen(s_server_config.session_id_prefix) >= 16) 1237 BIO_printf(bio_err, 1238 "warning: id_prefix is too long if you use SSLv2\n"); 1239 if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) { 1240 BIO_printf(bio_err, "error setting 'id_prefix'\n"); 1241 ERR_print_errors(bio_err); 1242 goto end; 1243 } 1244 BIO_printf(bio_err, "id_prefix '%s' set.\n", 1245 s_server_config.session_id_prefix); 1246 } 1247 SSL_CTX_set_quiet_shutdown(ctx, 1); 1248 if (s_server_config.bugs) 1249 SSL_CTX_set_options(ctx, SSL_OP_ALL); 1250 SSL_CTX_set_options(ctx, s_server_config.off); 1251 1252 if (s_server_config.state) 1253 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback); 1254 if (s_server_config.no_cache) 1255 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); 1256 else 1257 SSL_CTX_sess_set_cache_size(ctx, 128); 1258 1259 #ifndef OPENSSL_NO_SRTP 1260 if (s_server_config.srtp_profiles != NULL) 1261 SSL_CTX_set_tlsext_use_srtp(ctx, s_server_config.srtp_profiles); 1262 #endif 1263 1264 if ((!SSL_CTX_load_verify_locations(ctx, s_server_config.CAfile, 1265 s_server_config.CApath)) || 1266 (!SSL_CTX_set_default_verify_paths(ctx))) { 1267 /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */ 1268 ERR_print_errors(bio_err); 1269 /* goto end; */ 1270 } 1271 if (s_server_config.vpm) 1272 SSL_CTX_set1_param(ctx, s_server_config.vpm); 1273 1274 if (s_cert2) { 1275 ctx2 = SSL_CTX_new(s_server_config.meth); 1276 if (ctx2 == NULL) { 1277 ERR_print_errors(bio_err); 1278 goto end; 1279 } 1280 1281 if (!SSL_CTX_set_min_proto_version(ctx2, 1282 s_server_config.min_version)) 1283 goto end; 1284 if (!SSL_CTX_set_max_proto_version(ctx2, 1285 s_server_config.max_version)) 1286 goto end; 1287 SSL_CTX_clear_mode(ctx2, SSL_MODE_AUTO_RETRY); 1288 } 1289 if (ctx2) { 1290 BIO_printf(bio_s_out, "Setting secondary ctx parameters\n"); 1291 1292 if (s_server_config.session_id_prefix) { 1293 if (strlen(s_server_config.session_id_prefix) >= 32) 1294 BIO_printf(bio_err, 1295 "warning: id_prefix is too long, only one new session will be possible\n"); 1296 else if (strlen(s_server_config.session_id_prefix) >= 16) 1297 BIO_printf(bio_err, 1298 "warning: id_prefix is too long if you use SSLv2\n"); 1299 if (!SSL_CTX_set_generate_session_id(ctx2, 1300 generate_session_id)) { 1301 BIO_printf(bio_err, 1302 "error setting 'id_prefix'\n"); 1303 ERR_print_errors(bio_err); 1304 goto end; 1305 } 1306 BIO_printf(bio_err, "id_prefix '%s' set.\n", 1307 s_server_config.session_id_prefix); 1308 } 1309 SSL_CTX_set_quiet_shutdown(ctx2, 1); 1310 if (s_server_config.bugs) 1311 SSL_CTX_set_options(ctx2, SSL_OP_ALL); 1312 SSL_CTX_set_options(ctx2, s_server_config.off); 1313 1314 if (s_server_config.state) 1315 SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback); 1316 1317 if (s_server_config.no_cache) 1318 SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF); 1319 else 1320 SSL_CTX_sess_set_cache_size(ctx2, 128); 1321 1322 if ((!SSL_CTX_load_verify_locations(ctx2, 1323 s_server_config.CAfile, s_server_config.CApath)) || 1324 (!SSL_CTX_set_default_verify_paths(ctx2))) { 1325 ERR_print_errors(bio_err); 1326 } 1327 if (s_server_config.vpm) 1328 SSL_CTX_set1_param(ctx2, s_server_config.vpm); 1329 } 1330 if (alpn_ctx.data) 1331 SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx); 1332 1333 if (s_server_config.groups_in != NULL) { 1334 if (SSL_CTX_set1_groups_list(ctx, s_server_config.groups_in) != 1) { 1335 BIO_printf(bio_err, "Failed to set groups '%s'\n", 1336 s_server_config.groups_in); 1337 goto end; 1338 } 1339 } 1340 1341 #ifndef OPENSSL_NO_DH 1342 if (!s_server_config.no_dhe) { 1343 DH *dh = NULL; 1344 1345 if (s_server_config.dhfile) 1346 dh = load_dh_param(s_server_config.dhfile); 1347 else if (s_server_config.cert_file) 1348 dh = load_dh_param(s_server_config.cert_file); 1349 1350 if (dh != NULL) 1351 BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 1352 else 1353 BIO_printf(bio_s_out, "Using auto DH parameters\n"); 1354 (void) BIO_flush(bio_s_out); 1355 1356 if (dh == NULL) 1357 SSL_CTX_set_dh_auto(ctx, 1); 1358 else if (!SSL_CTX_set_tmp_dh(ctx, dh)) { 1359 BIO_printf(bio_err, 1360 "Error setting temp DH parameters\n"); 1361 ERR_print_errors(bio_err); 1362 DH_free(dh); 1363 goto end; 1364 } 1365 1366 if (ctx2) { 1367 if (!s_server_config.dhfile) { 1368 DH *dh2 = NULL; 1369 1370 if (s_server_config.cert_file2 != NULL) 1371 dh2 = load_dh_param( 1372 s_server_config.cert_file2); 1373 if (dh2 != NULL) { 1374 BIO_printf(bio_s_out, 1375 "Setting temp DH parameters\n"); 1376 (void) BIO_flush(bio_s_out); 1377 1378 DH_free(dh); 1379 dh = dh2; 1380 } 1381 } 1382 if (dh == NULL) 1383 SSL_CTX_set_dh_auto(ctx2, 1); 1384 else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) { 1385 BIO_printf(bio_err, 1386 "Error setting temp DH parameters\n"); 1387 ERR_print_errors(bio_err); 1388 DH_free(dh); 1389 goto end; 1390 } 1391 } 1392 DH_free(dh); 1393 } 1394 #endif 1395 1396 if (!s_server_config.no_ecdhe && s_server_config.named_curve != NULL) { 1397 EC_KEY *ecdh = NULL; 1398 int nid; 1399 1400 if ((nid = OBJ_sn2nid(s_server_config.named_curve)) == 0) { 1401 BIO_printf(bio_err, "unknown curve name (%s)\n", 1402 s_server_config.named_curve); 1403 goto end; 1404 } 1405 if ((ecdh = EC_KEY_new_by_curve_name(nid)) == NULL) { 1406 BIO_printf(bio_err, "unable to create curve (%s)\n", 1407 s_server_config.named_curve); 1408 goto end; 1409 } 1410 BIO_printf(bio_s_out, "Setting temp ECDH parameters\n"); 1411 (void) BIO_flush(bio_s_out); 1412 1413 SSL_CTX_set_tmp_ecdh(ctx, ecdh); 1414 if (ctx2) 1415 SSL_CTX_set_tmp_ecdh(ctx2, ecdh); 1416 EC_KEY_free(ecdh); 1417 } 1418 1419 if (!set_cert_key_stuff(ctx, s_cert, s_key)) 1420 goto end; 1421 if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2)) 1422 goto end; 1423 if (s_dcert != NULL) { 1424 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey)) 1425 goto end; 1426 } 1427 1428 if (s_server_config.cipher != NULL) { 1429 if (!SSL_CTX_set_cipher_list(ctx, s_server_config.cipher)) { 1430 BIO_printf(bio_err, "error setting cipher list\n"); 1431 ERR_print_errors(bio_err); 1432 goto end; 1433 } 1434 if (ctx2 && !SSL_CTX_set_cipher_list(ctx2, 1435 s_server_config.cipher)) { 1436 BIO_printf(bio_err, "error setting cipher list\n"); 1437 ERR_print_errors(bio_err); 1438 goto end; 1439 } 1440 } 1441 SSL_CTX_set_verify(ctx, s_server_config.server_verify, verify_callback); 1442 SSL_CTX_set_session_id_context(ctx, 1443 (void *) &s_server_session_id_context, 1444 sizeof s_server_session_id_context); 1445 1446 /* Set DTLS cookie generation and verification callbacks */ 1447 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback); 1448 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback); 1449 1450 if (ctx2) { 1451 SSL_CTX_set_verify(ctx2, s_server_config.server_verify, 1452 verify_callback); 1453 SSL_CTX_set_session_id_context(ctx2, 1454 (void *) &s_server_session_id_context, 1455 sizeof s_server_session_id_context); 1456 1457 s_server_config.tlsextcbp.biodebug = bio_s_out; 1458 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb); 1459 SSL_CTX_set_tlsext_servername_arg(ctx2, 1460 &s_server_config.tlsextcbp); 1461 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); 1462 SSL_CTX_set_tlsext_servername_arg(ctx, 1463 &s_server_config.tlsextcbp); 1464 } 1465 1466 if (s_server_config.CAfile != NULL) { 1467 SSL_CTX_set_client_CA_list(ctx, 1468 SSL_load_client_CA_file(s_server_config.CAfile)); 1469 if (ctx2) 1470 SSL_CTX_set_client_CA_list(ctx2, 1471 SSL_load_client_CA_file(s_server_config.CAfile)); 1472 } 1473 BIO_printf(bio_s_out, "ACCEPT\n"); 1474 (void) BIO_flush(bio_s_out); 1475 if (s_server_config.www) 1476 do_server(s_server_config.port, s_server_config.socket_type, 1477 &accept_socket, www_body, s_server_config.context, 1478 s_server_config.naccept); 1479 else 1480 do_server(s_server_config.port, s_server_config.socket_type, 1481 &accept_socket, sv_body, s_server_config.context, 1482 s_server_config.naccept); 1483 print_stats(bio_s_out, ctx); 1484 ret = 0; 1485 end: 1486 SSL_CTX_free(ctx); 1487 X509_free(s_cert); 1488 X509_free(s_dcert); 1489 EVP_PKEY_free(s_key); 1490 EVP_PKEY_free(s_dkey); 1491 free(pass); 1492 free(dpass); 1493 X509_VERIFY_PARAM_free(s_server_config.vpm); 1494 free(s_server_config.tlscstatp.host); 1495 free(s_server_config.tlscstatp.port); 1496 free(s_server_config.tlscstatp.path); 1497 SSL_CTX_free(ctx2); 1498 X509_free(s_cert2); 1499 EVP_PKEY_free(s_key2); 1500 free(alpn_ctx.data); 1501 if (bio_s_out != NULL) { 1502 BIO_free(bio_s_out); 1503 bio_s_out = NULL; 1504 } 1505 1506 return (ret); 1507 } 1508 1509 static void 1510 print_stats(BIO *bio, SSL_CTX *ssl_ctx) 1511 { 1512 BIO_printf(bio, "%4ld items in the session cache\n", 1513 SSL_CTX_sess_number(ssl_ctx)); 1514 BIO_printf(bio, "%4ld client connects (SSL_connect())\n", 1515 SSL_CTX_sess_connect(ssl_ctx)); 1516 BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n", 1517 SSL_CTX_sess_connect_renegotiate(ssl_ctx)); 1518 BIO_printf(bio, "%4ld client connects that finished\n", 1519 SSL_CTX_sess_connect_good(ssl_ctx)); 1520 BIO_printf(bio, "%4ld server accepts (SSL_accept())\n", 1521 SSL_CTX_sess_accept(ssl_ctx)); 1522 BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n", 1523 SSL_CTX_sess_accept_renegotiate(ssl_ctx)); 1524 BIO_printf(bio, "%4ld server accepts that finished\n", 1525 SSL_CTX_sess_accept_good(ssl_ctx)); 1526 BIO_printf(bio, "%4ld session cache hits\n", 1527 SSL_CTX_sess_hits(ssl_ctx)); 1528 BIO_printf(bio, "%4ld session cache misses\n", 1529 SSL_CTX_sess_misses(ssl_ctx)); 1530 BIO_printf(bio, "%4ld session cache timeouts\n", 1531 SSL_CTX_sess_timeouts(ssl_ctx)); 1532 BIO_printf(bio, "%4ld callback cache hits\n", 1533 SSL_CTX_sess_cb_hits(ssl_ctx)); 1534 BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n", 1535 SSL_CTX_sess_cache_full(ssl_ctx), 1536 SSL_CTX_sess_get_cache_size(ssl_ctx)); 1537 } 1538 1539 static int 1540 sv_body(char *hostname, int s, unsigned char *context) 1541 { 1542 char *buf = NULL; 1543 int ret = 1; 1544 int k, i; 1545 unsigned long l; 1546 SSL *con = NULL; 1547 BIO *sbio; 1548 struct timeval timeout; 1549 1550 if ((buf = malloc(bufsize)) == NULL) { 1551 BIO_printf(bio_err, "out of memory\n"); 1552 goto err; 1553 } 1554 if (s_server_config.nbio) { 1555 if (!s_server_config.quiet) 1556 BIO_printf(bio_err, "turning on non blocking io\n"); 1557 if (!BIO_socket_nbio(s, 1)) 1558 ERR_print_errors(bio_err); 1559 } 1560 1561 if (con == NULL) { 1562 con = SSL_new(ctx); 1563 if (s_server_config.tlsextdebug) { 1564 SSL_set_tlsext_debug_callback(con, tlsext_cb); 1565 SSL_set_tlsext_debug_arg(con, bio_s_out); 1566 } 1567 if (s_server_config.tlsextstatus) { 1568 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb); 1569 s_server_config.tlscstatp.err = bio_err; 1570 SSL_CTX_set_tlsext_status_arg(ctx, 1571 &s_server_config.tlscstatp); 1572 } 1573 if (context) 1574 SSL_set_session_id_context(con, context, 1575 strlen((char *) context)); 1576 } 1577 SSL_clear(con); 1578 1579 if (SSL_is_dtls(con)) { 1580 sbio = BIO_new_dgram(s, BIO_NOCLOSE); 1581 1582 if (s_server_config.enable_timeouts) { 1583 timeout.tv_sec = 0; 1584 timeout.tv_usec = DGRAM_RCV_TIMEOUT; 1585 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, 1586 &timeout); 1587 1588 timeout.tv_sec = 0; 1589 timeout.tv_usec = DGRAM_SND_TIMEOUT; 1590 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, 1591 &timeout); 1592 } 1593 if (s_server_config.socket_mtu > 28) { 1594 SSL_set_options(con, SSL_OP_NO_QUERY_MTU); 1595 SSL_set_mtu(con, s_server_config.socket_mtu - 28); 1596 } else 1597 /* want to do MTU discovery */ 1598 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); 1599 1600 /* turn on cookie exchange */ 1601 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE); 1602 } else 1603 sbio = BIO_new_socket(s, BIO_NOCLOSE); 1604 1605 if (s_server_config.nbio_test) { 1606 BIO *test; 1607 1608 test = BIO_new(BIO_f_nbio_test()); 1609 sbio = BIO_push(test, sbio); 1610 } 1611 1612 SSL_set_bio(con, sbio, sbio); 1613 SSL_set_accept_state(con); 1614 /* SSL_set_fd(con,s); */ 1615 1616 if (s_server_config.debug) { 1617 SSL_set_debug(con, 1); 1618 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback); 1619 BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out); 1620 } 1621 if (s_server_config.msg) { 1622 SSL_set_msg_callback(con, msg_cb); 1623 SSL_set_msg_callback_arg(con, bio_s_out); 1624 } 1625 if (s_server_config.tlsextdebug) { 1626 SSL_set_tlsext_debug_callback(con, tlsext_cb); 1627 SSL_set_tlsext_debug_arg(con, bio_s_out); 1628 } 1629 1630 for (;;) { 1631 int read_from_terminal; 1632 int read_from_sslcon; 1633 struct pollfd pfd[2]; 1634 int ptimeout; 1635 1636 read_from_terminal = 0; 1637 read_from_sslcon = SSL_pending(con); 1638 1639 if (!read_from_sslcon) { 1640 pfd[0].fd = fileno(stdin); 1641 pfd[0].events = POLLIN; 1642 pfd[1].fd = s; 1643 pfd[1].events = POLLIN; 1644 1645 if (SSL_is_dtls(con) && 1646 DTLSv1_get_timeout(con, &timeout)) 1647 ptimeout = timeout.tv_sec * 1000 + 1648 timeout.tv_usec / 1000; 1649 else 1650 ptimeout = -1; 1651 1652 i = poll(pfd, 2, ptimeout); 1653 1654 if (SSL_is_dtls(con) && 1655 DTLSv1_handle_timeout(con) > 0) 1656 BIO_printf(bio_err, "TIMEOUT occured\n"); 1657 if (i <= 0) 1658 continue; 1659 if (pfd[0].revents) { 1660 if ((pfd[0].revents & (POLLERR|POLLNVAL))) 1661 continue; 1662 read_from_terminal = 1; 1663 } 1664 if (pfd[1].revents) { 1665 if ((pfd[1].revents & (POLLERR|POLLNVAL))) 1666 continue; 1667 read_from_sslcon = 1; 1668 } 1669 } 1670 if (read_from_terminal) { 1671 if (s_server_config.crlf) { 1672 int j, lf_num; 1673 1674 i = read(fileno(stdin), buf, bufsize / 2); 1675 lf_num = 0; 1676 /* both loops are skipped when i <= 0 */ 1677 for (j = 0; j < i; j++) 1678 if (buf[j] == '\n') 1679 lf_num++; 1680 for (j = i - 1; j >= 0; j--) { 1681 buf[j + lf_num] = buf[j]; 1682 if (buf[j] == '\n') { 1683 lf_num--; 1684 i++; 1685 buf[j + lf_num] = '\r'; 1686 } 1687 } 1688 assert(lf_num == 0); 1689 } else 1690 i = read(fileno(stdin), buf, bufsize); 1691 if (!s_server_config.quiet) { 1692 if ((i <= 0) || (buf[0] == 'Q')) { 1693 BIO_printf(bio_s_out, "DONE\n"); 1694 shutdown(s, SHUT_RD); 1695 close(s); 1696 close_accept_socket(); 1697 ret = -11; 1698 goto err; 1699 } 1700 if ((i <= 0) || (buf[0] == 'q')) { 1701 BIO_printf(bio_s_out, "DONE\n"); 1702 if (!SSL_is_dtls(con)) { 1703 shutdown(s, SHUT_RD); 1704 close(s); 1705 } 1706 /* 1707 * close_accept_socket(); ret= -11; 1708 */ 1709 goto err; 1710 } 1711 if ((buf[0] == 'r') && 1712 ((buf[1] == '\n') || (buf[1] == '\r'))) { 1713 SSL_renegotiate(con); 1714 i = SSL_do_handshake(con); 1715 printf("SSL_do_handshake -> %d\n", i); 1716 i = 0; /* 13; */ 1717 continue; 1718 /* 1719 * RE-NEGOTIATE\n"); 1720 */ 1721 } 1722 if ((buf[0] == 'R') && 1723 ((buf[1] == '\n') || (buf[1] == '\r'))) { 1724 SSL_set_verify(con, 1725 SSL_VERIFY_PEER | 1726 SSL_VERIFY_CLIENT_ONCE, 1727 NULL); 1728 SSL_renegotiate(con); 1729 i = SSL_do_handshake(con); 1730 printf("SSL_do_handshake -> %d\n", i); 1731 i = 0; /* 13; */ 1732 continue; 1733 /* 1734 * RE-NEGOTIATE asking for client 1735 * cert\n"); 1736 */ 1737 } 1738 if (buf[0] == 'P') { 1739 static const char *str = 1740 "Lets print some clear text\n"; 1741 BIO_write(SSL_get_wbio(con), str, 1742 strlen(str)); 1743 } 1744 if (buf[0] == 'S') { 1745 print_stats(bio_s_out, 1746 SSL_get_SSL_CTX(con)); 1747 } 1748 } 1749 l = k = 0; 1750 for (;;) { 1751 /* should do a select for the write */ 1752 #ifdef RENEG 1753 { 1754 static count = 0; 1755 if (++count == 100) { 1756 count = 0; 1757 SSL_renegotiate(con); 1758 } 1759 } 1760 #endif 1761 k = SSL_write(con, &(buf[l]), (unsigned int) i); 1762 switch (SSL_get_error(con, k)) { 1763 case SSL_ERROR_NONE: 1764 break; 1765 case SSL_ERROR_WANT_WRITE: 1766 case SSL_ERROR_WANT_READ: 1767 case SSL_ERROR_WANT_X509_LOOKUP: 1768 BIO_printf(bio_s_out, "Write BLOCK\n"); 1769 break; 1770 case SSL_ERROR_SYSCALL: 1771 case SSL_ERROR_SSL: 1772 BIO_printf(bio_s_out, "ERROR\n"); 1773 ERR_print_errors(bio_err); 1774 ret = 1; 1775 goto err; 1776 /* break; */ 1777 case SSL_ERROR_ZERO_RETURN: 1778 BIO_printf(bio_s_out, "DONE\n"); 1779 ret = 1; 1780 goto err; 1781 } 1782 if (k <= 0) 1783 continue; 1784 l += k; 1785 i -= k; 1786 if (i <= 0) 1787 break; 1788 } 1789 } 1790 if (read_from_sslcon) { 1791 if (!SSL_is_init_finished(con)) { 1792 i = init_ssl_connection(con); 1793 1794 if (i < 0) { 1795 ret = 0; 1796 goto err; 1797 } else if (i == 0) { 1798 ret = 1; 1799 goto err; 1800 } 1801 } else { 1802 again: 1803 i = SSL_read(con, (char *) buf, bufsize); 1804 switch (SSL_get_error(con, i)) { 1805 case SSL_ERROR_NONE: { 1806 int len, n; 1807 for (len = 0; len < i;) { 1808 do { 1809 n = write(fileno(stdout), buf + len, i - len); 1810 } while (n == -1 && errno == EINTR); 1811 1812 if (n == -1) { 1813 BIO_printf(bio_s_out, "ERROR\n"); 1814 goto err; 1815 } 1816 len += n; 1817 } 1818 } 1819 if (SSL_pending(con)) 1820 goto again; 1821 break; 1822 case SSL_ERROR_WANT_WRITE: 1823 case SSL_ERROR_WANT_READ: 1824 BIO_printf(bio_s_out, "Read BLOCK\n"); 1825 break; 1826 case SSL_ERROR_SYSCALL: 1827 case SSL_ERROR_SSL: 1828 BIO_printf(bio_s_out, "ERROR\n"); 1829 ERR_print_errors(bio_err); 1830 ret = 1; 1831 goto err; 1832 case SSL_ERROR_ZERO_RETURN: 1833 BIO_printf(bio_s_out, "DONE\n"); 1834 ret = 1; 1835 goto err; 1836 } 1837 } 1838 } 1839 } 1840 err: 1841 if (con != NULL) { 1842 BIO_printf(bio_s_out, "shutting down SSL\n"); 1843 SSL_set_shutdown(con, 1844 SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); 1845 SSL_free(con); 1846 } 1847 BIO_printf(bio_s_out, "CONNECTION CLOSED\n"); 1848 freezero(buf, bufsize); 1849 if (ret >= 0) 1850 BIO_printf(bio_s_out, "ACCEPT\n"); 1851 return (ret); 1852 } 1853 1854 static void 1855 close_accept_socket(void) 1856 { 1857 BIO_printf(bio_err, "shutdown accept socket\n"); 1858 if (accept_socket >= 0) { 1859 shutdown(accept_socket, SHUT_RDWR); 1860 close(accept_socket); 1861 } 1862 } 1863 1864 static int 1865 init_ssl_connection(SSL *con) 1866 { 1867 int i; 1868 const char *str; 1869 X509 *peer; 1870 long verify_error; 1871 char buf[BUFSIZ]; 1872 unsigned char *exportedkeymat; 1873 1874 i = SSL_accept(con); 1875 if (i <= 0) { 1876 if (BIO_sock_should_retry(i)) { 1877 BIO_printf(bio_s_out, "DELAY\n"); 1878 return (1); 1879 } 1880 BIO_printf(bio_err, "ERROR\n"); 1881 verify_error = SSL_get_verify_result(con); 1882 if (verify_error != X509_V_OK) { 1883 BIO_printf(bio_err, "verify error:%s\n", 1884 X509_verify_cert_error_string(verify_error)); 1885 } else 1886 ERR_print_errors(bio_err); 1887 return (0); 1888 } 1889 PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con)); 1890 1891 peer = SSL_get_peer_certificate(con); 1892 if (peer != NULL) { 1893 BIO_printf(bio_s_out, "Client certificate\n"); 1894 PEM_write_bio_X509(bio_s_out, peer); 1895 X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf); 1896 BIO_printf(bio_s_out, "subject=%s\n", buf); 1897 X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf); 1898 BIO_printf(bio_s_out, "issuer=%s\n", buf); 1899 X509_free(peer); 1900 } 1901 if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL) 1902 BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf); 1903 str = SSL_CIPHER_get_name(SSL_get_current_cipher(con)); 1904 BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)"); 1905 1906 #ifndef OPENSSL_NO_SRTP 1907 { 1908 SRTP_PROTECTION_PROFILE *srtp_profile 1909 = SSL_get_selected_srtp_profile(con); 1910 1911 if (srtp_profile) 1912 BIO_printf(bio_s_out, 1913 "SRTP Extension negotiated, profile=%s\n", 1914 srtp_profile->name); 1915 } 1916 #endif 1917 if (SSL_cache_hit(con)) 1918 BIO_printf(bio_s_out, "Reused session-id\n"); 1919 BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n", 1920 SSL_get_secure_renegotiation_support(con) ? "" : " NOT"); 1921 if (s_server_config.keymatexportlabel != NULL) { 1922 BIO_printf(bio_s_out, "Keying material exporter:\n"); 1923 BIO_printf(bio_s_out, " Label: '%s'\n", 1924 s_server_config.keymatexportlabel); 1925 BIO_printf(bio_s_out, " Length: %i bytes\n", 1926 s_server_config.keymatexportlen); 1927 exportedkeymat = malloc(s_server_config.keymatexportlen); 1928 if (exportedkeymat != NULL) { 1929 if (!SSL_export_keying_material(con, exportedkeymat, 1930 s_server_config.keymatexportlen, 1931 s_server_config.keymatexportlabel, 1932 strlen(s_server_config.keymatexportlabel), 1933 NULL, 0, 0)) { 1934 BIO_printf(bio_s_out, " Error\n"); 1935 } else { 1936 BIO_printf(bio_s_out, " Keying material: "); 1937 for (i = 0; i < s_server_config.keymatexportlen; i++) 1938 BIO_printf(bio_s_out, "%02X", 1939 exportedkeymat[i]); 1940 BIO_printf(bio_s_out, "\n"); 1941 } 1942 free(exportedkeymat); 1943 } 1944 } 1945 return (1); 1946 } 1947 1948 #ifndef OPENSSL_NO_DH 1949 static DH * 1950 load_dh_param(const char *dhfile) 1951 { 1952 DH *ret = NULL; 1953 BIO *bio; 1954 1955 if ((bio = BIO_new_file(dhfile, "r")) == NULL) 1956 goto err; 1957 ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); 1958 err: 1959 BIO_free(bio); 1960 return (ret); 1961 } 1962 #endif 1963 1964 static int 1965 www_body(char *hostname, int s, unsigned char *context) 1966 { 1967 char *buf = NULL; 1968 int ret = 1; 1969 int i, j, k, dot; 1970 SSL *con; 1971 const SSL_CIPHER *c; 1972 BIO *io, *ssl_bio, *sbio; 1973 1974 buf = malloc(bufsize); 1975 if (buf == NULL) 1976 return (0); 1977 io = BIO_new(BIO_f_buffer()); 1978 ssl_bio = BIO_new(BIO_f_ssl()); 1979 if ((io == NULL) || (ssl_bio == NULL)) 1980 goto err; 1981 1982 if (s_server_config.nbio) { 1983 if (!s_server_config.quiet) 1984 BIO_printf(bio_err, "turning on non blocking io\n"); 1985 if (!BIO_socket_nbio(s, 1)) 1986 ERR_print_errors(bio_err); 1987 } 1988 1989 /* lets make the output buffer a reasonable size */ 1990 if (!BIO_set_write_buffer_size(io, bufsize)) 1991 goto err; 1992 1993 if ((con = SSL_new(ctx)) == NULL) 1994 goto err; 1995 if (s_server_config.tlsextdebug) { 1996 SSL_set_tlsext_debug_callback(con, tlsext_cb); 1997 SSL_set_tlsext_debug_arg(con, bio_s_out); 1998 } 1999 if (context) 2000 SSL_set_session_id_context(con, context, 2001 strlen((char *) context)); 2002 2003 sbio = BIO_new_socket(s, BIO_NOCLOSE); 2004 if (s_server_config.nbio_test) { 2005 BIO *test; 2006 2007 test = BIO_new(BIO_f_nbio_test()); 2008 sbio = BIO_push(test, sbio); 2009 } 2010 SSL_set_bio(con, sbio, sbio); 2011 SSL_set_accept_state(con); 2012 2013 /* SSL_set_fd(con,s); */ 2014 BIO_set_ssl(ssl_bio, con, BIO_CLOSE); 2015 BIO_push(io, ssl_bio); 2016 2017 if (s_server_config.debug) { 2018 SSL_set_debug(con, 1); 2019 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback); 2020 BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out); 2021 } 2022 if (s_server_config.msg) { 2023 SSL_set_msg_callback(con, msg_cb); 2024 SSL_set_msg_callback_arg(con, bio_s_out); 2025 } 2026 for (;;) { 2027 i = BIO_gets(io, buf, bufsize - 1); 2028 if (i < 0) { /* error */ 2029 if (!BIO_should_retry(io)) { 2030 if (!s_server_config.quiet) 2031 ERR_print_errors(bio_err); 2032 goto err; 2033 } else { 2034 if (s_server_config.debug) { 2035 BIO_printf(bio_s_out, "read R BLOCK\n"); 2036 sleep(1); 2037 } 2038 continue; 2039 } 2040 } else if (i == 0) { /* end of input */ 2041 ret = 1; 2042 goto end; 2043 } 2044 /* else we have data */ 2045 if (((s_server_config.www == 1) && 2046 (strncmp("GET ", buf, 4) == 0)) || 2047 ((s_server_config.www == 2) && 2048 (strncmp("GET /stats ", buf, 11) == 0))) { 2049 char *p; 2050 X509 *peer; 2051 STACK_OF(SSL_CIPHER) *sk; 2052 static const char *space = " "; 2053 2054 BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 2055 BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n"); 2056 BIO_puts(io, "<pre>\n"); 2057 /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/ 2058 BIO_puts(io, "\n"); 2059 for (i = 0; i < local_argc; i++) { 2060 BIO_puts(io, local_argv[i]); 2061 BIO_write(io, " ", 1); 2062 } 2063 BIO_puts(io, "\n"); 2064 2065 BIO_printf(io, 2066 "Secure Renegotiation IS%s supported\n", 2067 SSL_get_secure_renegotiation_support(con) ? 2068 "" : " NOT"); 2069 2070 /* 2071 * The following is evil and should not really be 2072 * done 2073 */ 2074 BIO_printf(io, 2075 "Ciphers supported in s_server binary\n"); 2076 sk = SSL_get_ciphers(con); 2077 j = sk_SSL_CIPHER_num(sk); 2078 for (i = 0; i < j; i++) { 2079 c = sk_SSL_CIPHER_value(sk, i); 2080 BIO_printf(io, "%-11s:%-25s", 2081 SSL_CIPHER_get_version(c), 2082 SSL_CIPHER_get_name(c)); 2083 if ((((i + 1) % 2) == 0) && (i + 1 != j)) 2084 BIO_puts(io, "\n"); 2085 } 2086 BIO_puts(io, "\n"); 2087 p = SSL_get_shared_ciphers(con, buf, bufsize); 2088 if (p != NULL) { 2089 BIO_printf(io, 2090 "---\nCiphers common between both SSL end points:\n"); 2091 j = i = 0; 2092 while (*p) { 2093 if (*p == ':') { 2094 BIO_write(io, space, 26 - j); 2095 i++; 2096 j = 0; 2097 BIO_write(io, 2098 ((i % 3) ? " " : "\n"), 1); 2099 } else { 2100 BIO_write(io, p, 1); 2101 j++; 2102 } 2103 p++; 2104 } 2105 BIO_puts(io, "\n"); 2106 } 2107 BIO_printf(io, (SSL_cache_hit(con) 2108 ? "---\nReused, " 2109 : "---\nNew, ")); 2110 c = SSL_get_current_cipher(con); 2111 BIO_printf(io, "%s, Cipher is %s\n", 2112 SSL_CIPHER_get_version(c), 2113 SSL_CIPHER_get_name(c)); 2114 SSL_SESSION_print(io, SSL_get_session(con)); 2115 BIO_printf(io, "---\n"); 2116 print_stats(io, SSL_get_SSL_CTX(con)); 2117 BIO_printf(io, "---\n"); 2118 peer = SSL_get_peer_certificate(con); 2119 if (peer != NULL) { 2120 BIO_printf(io, "Client certificate\n"); 2121 X509_print(io, peer); 2122 PEM_write_bio_X509(io, peer); 2123 } else 2124 BIO_puts(io, 2125 "no client certificate available\n"); 2126 BIO_puts(io, "</BODY></HTML>\r\n\r\n"); 2127 break; 2128 } else if ((s_server_config.www == 2 || 2129 s_server_config.www == 3) && 2130 (strncmp("GET /", buf, 5) == 0)) { 2131 BIO *file; 2132 char *p, *e; 2133 static const char *text = "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"; 2134 2135 /* skip the '/' */ 2136 p = &(buf[5]); 2137 2138 dot = 1; 2139 for (e = p; *e != '\0'; e++) { 2140 if (e[0] == ' ') 2141 break; 2142 2143 switch (dot) { 2144 case 1: 2145 dot = (e[0] == '.') ? 2 : 0; 2146 break; 2147 case 2: 2148 dot = (e[0] == '.') ? 3 : 0; 2149 break; 2150 case 3: 2151 dot = (e[0] == '/' || e[0] == '\\') ? 2152 -1 : 0; 2153 break; 2154 } 2155 if (dot == 0) 2156 dot = (e[0] == '/' || e[0] == '\\') ? 2157 1 : 0; 2158 } 2159 dot = (dot == 3) || (dot == -1); /* filename contains 2160 * ".." component */ 2161 2162 if (*e == '\0') { 2163 BIO_puts(io, text); 2164 BIO_printf(io, 2165 "'%s' is an invalid file name\r\n", p); 2166 break; 2167 } 2168 *e = '\0'; 2169 2170 if (dot) { 2171 BIO_puts(io, text); 2172 BIO_printf(io, 2173 "'%s' contains '..' reference\r\n", p); 2174 break; 2175 } 2176 if (*p == '/') { 2177 BIO_puts(io, text); 2178 BIO_printf(io, 2179 "'%s' is an invalid path\r\n", p); 2180 break; 2181 } 2182 /* if a directory, do the index thang */ 2183 if (app_isdir(p) > 0) { 2184 BIO_puts(io, text); 2185 BIO_printf(io, "'%s' is a directory\r\n", p); 2186 break; 2187 } 2188 if ((file = BIO_new_file(p, "r")) == NULL) { 2189 BIO_puts(io, text); 2190 BIO_printf(io, "Error opening '%s'\r\n", p); 2191 ERR_print_errors(io); 2192 break; 2193 } 2194 if (!s_server_config.quiet) 2195 BIO_printf(bio_err, "FILE:%s\n", p); 2196 2197 if (s_server_config.www == 2) { 2198 i = strlen(p); 2199 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) || 2200 ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) || 2201 ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0))) 2202 BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 2203 else 2204 BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"); 2205 } 2206 /* send the file */ 2207 for (;;) { 2208 i = BIO_read(file, buf, bufsize); 2209 if (i <= 0) 2210 break; 2211 2212 #ifdef RENEG 2213 total_bytes += i; 2214 fprintf(stderr, "%d\n", i); 2215 if (total_bytes > 3 * 1024) { 2216 total_bytes = 0; 2217 fprintf(stderr, "RENEGOTIATE\n"); 2218 SSL_renegotiate(con); 2219 } 2220 #endif 2221 2222 for (j = 0; j < i;) { 2223 #ifdef RENEG 2224 { 2225 static count = 0; 2226 if (++count == 13) { 2227 SSL_renegotiate(con); 2228 } 2229 } 2230 #endif 2231 k = BIO_write(io, &(buf[j]), i - j); 2232 if (k <= 0) { 2233 if (!BIO_should_retry(io)) 2234 goto write_error; 2235 else { 2236 BIO_printf(bio_s_out, 2237 "rwrite W BLOCK\n"); 2238 } 2239 } else { 2240 j += k; 2241 } 2242 } 2243 } 2244 write_error: 2245 BIO_free(file); 2246 break; 2247 } 2248 } 2249 2250 for (;;) { 2251 i = (int) BIO_flush(io); 2252 if (i <= 0) { 2253 if (!BIO_should_retry(io)) 2254 break; 2255 } else 2256 break; 2257 } 2258 end: 2259 /* make sure we re-use sessions */ 2260 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); 2261 2262 err: 2263 2264 if (ret >= 0) 2265 BIO_printf(bio_s_out, "ACCEPT\n"); 2266 2267 free(buf); 2268 BIO_free_all(io); 2269 /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/ 2270 return (ret); 2271 } 2272 2273 #define MAX_SESSION_ID_ATTEMPTS 10 2274 static int 2275 generate_session_id(const SSL *ssl, unsigned char *id, unsigned int *id_len) 2276 { 2277 unsigned int count = 0; 2278 do { 2279 arc4random_buf(id, *id_len); 2280 /* 2281 * Prefix the session_id with the required prefix. NB: If our 2282 * prefix is too long, clip it - but there will be worse 2283 * effects anyway, eg. the server could only possibly create 2284 * 1 session ID (ie. the prefix!) so all future session 2285 * negotiations will fail due to conflicts. 2286 */ 2287 memcpy(id, s_server_config.session_id_prefix, 2288 (strlen(s_server_config.session_id_prefix) < *id_len) ? 2289 strlen(s_server_config.session_id_prefix) : *id_len); 2290 } 2291 while (SSL_has_matching_session_id(ssl, id, *id_len) && 2292 (++count < MAX_SESSION_ID_ATTEMPTS)); 2293 if (count >= MAX_SESSION_ID_ATTEMPTS) 2294 return 0; 2295 return 1; 2296 } 2297 2298 static int 2299 ssl_servername_cb(SSL *s, int *ad, void *arg) 2300 { 2301 tlsextctx *p = (tlsextctx *) arg; 2302 const char *servername = SSL_get_servername(s, 2303 TLSEXT_NAMETYPE_host_name); 2304 2305 if (servername && p->biodebug) 2306 BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n", 2307 servername); 2308 2309 if (!p->servername) 2310 return SSL_TLSEXT_ERR_NOACK; 2311 2312 if (servername) { 2313 if (strcmp(servername, p->servername)) 2314 return p->extension_error; 2315 if (ctx2) { 2316 BIO_printf(p->biodebug, "Switching server context.\n"); 2317 SSL_set_SSL_CTX(s, ctx2); 2318 } 2319 } 2320 return SSL_TLSEXT_ERR_OK; 2321 } 2322 2323 /* Certificate Status callback. This is called when a client includes a 2324 * certificate status request extension. 2325 * 2326 * This is a simplified version. It examines certificates each time and 2327 * makes one OCSP responder query for each request. 2328 * 2329 * A full version would store details such as the OCSP certificate IDs and 2330 * minimise the number of OCSP responses by caching them until they were 2331 * considered "expired". 2332 */ 2333 2334 static int 2335 cert_status_cb(SSL *s, void *arg) 2336 { 2337 tlsextstatusctx *srctx = arg; 2338 BIO *err = srctx->err; 2339 char *host = NULL, *port = NULL, *path = NULL; 2340 int use_ssl; 2341 unsigned char *rspder = NULL; 2342 int rspderlen; 2343 STACK_OF(OPENSSL_STRING) *aia = NULL; 2344 X509 *x = NULL; 2345 X509_STORE_CTX inctx; 2346 X509_OBJECT obj; 2347 OCSP_REQUEST *req = NULL; 2348 OCSP_RESPONSE *resp = NULL; 2349 OCSP_CERTID *id = NULL; 2350 STACK_OF(X509_EXTENSION) *exts; 2351 int ret = SSL_TLSEXT_ERR_NOACK; 2352 int i; 2353 2354 if (srctx->verbose) 2355 BIO_puts(err, "cert_status: callback called\n"); 2356 /* Build up OCSP query from server certificate */ 2357 x = SSL_get_certificate(s); 2358 aia = X509_get1_ocsp(x); 2359 if (aia) { 2360 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0), 2361 &host, &port, &path, &use_ssl)) { 2362 BIO_puts(err, "cert_status: can't parse AIA URL\n"); 2363 goto err; 2364 } 2365 if (srctx->verbose) 2366 BIO_printf(err, "cert_status: AIA URL: %s\n", 2367 sk_OPENSSL_STRING_value(aia, 0)); 2368 } else { 2369 if (!srctx->host) { 2370 BIO_puts(srctx->err, 2371 "cert_status: no AIA and no default responder URL\n"); 2372 goto done; 2373 } 2374 host = srctx->host; 2375 path = srctx->path; 2376 port = srctx->port; 2377 use_ssl = srctx->use_ssl; 2378 } 2379 2380 if (!X509_STORE_CTX_init(&inctx, 2381 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)), 2382 NULL, NULL)) 2383 goto err; 2384 if (X509_STORE_get_by_subject(&inctx, X509_LU_X509, 2385 X509_get_issuer_name(x), &obj) <= 0) { 2386 BIO_puts(err, 2387 "cert_status: Can't retrieve issuer certificate.\n"); 2388 X509_STORE_CTX_cleanup(&inctx); 2389 goto done; 2390 } 2391 req = OCSP_REQUEST_new(); 2392 if (!req) 2393 goto err; 2394 id = OCSP_cert_to_id(NULL, x, obj.data.x509); 2395 X509_free(obj.data.x509); 2396 X509_STORE_CTX_cleanup(&inctx); 2397 if (!id) 2398 goto err; 2399 if (!OCSP_request_add0_id(req, id)) 2400 goto err; 2401 id = NULL; 2402 /* Add any extensions to the request */ 2403 SSL_get_tlsext_status_exts(s, &exts); 2404 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { 2405 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); 2406 if (!OCSP_REQUEST_add_ext(req, ext, -1)) 2407 goto err; 2408 } 2409 resp = process_responder(err, req, host, path, port, use_ssl, NULL, 2410 srctx->timeout); 2411 if (!resp) { 2412 BIO_puts(err, "cert_status: error querying responder\n"); 2413 goto done; 2414 } 2415 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder); 2416 if (rspderlen <= 0) 2417 goto err; 2418 SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen); 2419 if (srctx->verbose) { 2420 BIO_puts(err, "cert_status: ocsp response sent:\n"); 2421 OCSP_RESPONSE_print(err, resp, 2); 2422 } 2423 ret = SSL_TLSEXT_ERR_OK; 2424 done: 2425 if (ret != SSL_TLSEXT_ERR_OK) 2426 ERR_print_errors(err); 2427 if (aia) { 2428 free(host); 2429 free(path); 2430 free(port); 2431 X509_email_free(aia); 2432 } 2433 if (id) 2434 OCSP_CERTID_free(id); 2435 if (req) 2436 OCSP_REQUEST_free(req); 2437 if (resp) 2438 OCSP_RESPONSE_free(resp); 2439 return ret; 2440 err: 2441 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 2442 goto done; 2443 } 2444 2445 static int 2446 alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen, 2447 const unsigned char *in, unsigned int inlen, void *arg) 2448 { 2449 tlsextalpnctx *alpn_ctx = arg; 2450 2451 if (!s_server_config.quiet) { 2452 /* We can assume that in is syntactically valid. */ 2453 unsigned i; 2454 2455 BIO_printf(bio_s_out, 2456 "ALPN protocols advertised by the client: "); 2457 for (i = 0; i < inlen; ) { 2458 if (i) 2459 BIO_write(bio_s_out, ", ", 2); 2460 BIO_write(bio_s_out, &in[i + 1], in[i]); 2461 i += in[i] + 1; 2462 } 2463 BIO_write(bio_s_out, "\n", 1); 2464 } 2465 2466 if (SSL_select_next_proto((unsigned char**)out, outlen, alpn_ctx->data, 2467 alpn_ctx->len, in, inlen) != OPENSSL_NPN_NEGOTIATED) 2468 return (SSL_TLSEXT_ERR_NOACK); 2469 2470 if (!s_server_config.quiet) { 2471 BIO_printf(bio_s_out, "ALPN protocols selected: "); 2472 BIO_write(bio_s_out, *out, *outlen); 2473 BIO_write(bio_s_out, "\n", 1); 2474 } 2475 2476 return (SSL_TLSEXT_ERR_OK); 2477 } 2478