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