1 /* 2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4 * Copyright 2005 Nokia. All rights reserved. 5 * 6 * Licensed under the Apache License 2.0 (the "License"). You may not use 7 * this file except in compliance with the License. You can obtain a copy 8 * in the file LICENSE in the source distribution or at 9 * https://www.openssl.org/source/license.html 10 */ 11 12 #include "internal/e_os.h" 13 14 #include <ctype.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #if defined(_WIN32) 19 /* Included before async.h to avoid some warnings */ 20 #include <windows.h> 21 #endif 22 23 #include <openssl/e_os2.h> 24 #include <openssl/async.h> 25 #include <openssl/ssl.h> 26 #include <openssl/decoder.h> 27 #include "internal/sockets.h" /* for openssl_fdset() */ 28 29 #ifndef OPENSSL_NO_SOCK 30 31 /* 32 * With IPv6, it looks like Digital has mixed up the proper order of 33 * recursive header file inclusion, resulting in the compiler complaining 34 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is 35 * needed to have fileno() declared correctly... So let's define u_int 36 */ 37 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) 38 #define __U_INT 39 typedef unsigned int u_int; 40 #endif 41 42 #include <openssl/bn.h> 43 #include "apps.h" 44 #include "progs.h" 45 #include <openssl/err.h> 46 #include <openssl/pem.h> 47 #include <openssl/x509.h> 48 #include <openssl/rand.h> 49 #include <openssl/ocsp.h> 50 #ifndef OPENSSL_NO_DH 51 #include <openssl/dh.h> 52 #endif 53 #include <openssl/rsa.h> 54 #include "s_apps.h" 55 #include "timeouts.h" 56 #ifdef CHARSET_EBCDIC 57 #include <openssl/ebcdic.h> 58 #endif 59 #include "internal/sockets.h" 60 61 static int not_resumable_sess_cb(SSL *s, int is_forward_secure); 62 static int sv_body(int s, int stype, int prot, unsigned char *context); 63 static int www_body(int s, int stype, int prot, unsigned char *context); 64 static int rev_body(int s, int stype, int prot, unsigned char *context); 65 static void close_accept_socket(void); 66 static int init_ssl_connection(SSL *s); 67 static void print_stats(BIO *bp, SSL_CTX *ctx); 68 static int generate_session_id(SSL *ssl, unsigned char *id, 69 unsigned int *id_len); 70 static void init_session_cache_ctx(SSL_CTX *sctx); 71 static void free_sessions(void); 72 static void print_connection_info(SSL *con); 73 74 static const int bufsize = 16 * 1024; 75 static int accept_socket = -1; 76 77 #define TEST_CERT "server.pem" 78 #define TEST_CERT2 "server2.pem" 79 80 static int s_nbio = 0; 81 static int s_nbio_test = 0; 82 static int s_crlf = 0; 83 static SSL_CTX *ctx = NULL; 84 static SSL_CTX *ctx2 = NULL; 85 static int www = 0; 86 87 static BIO *bio_s_out = NULL; 88 static BIO *bio_s_msg = NULL; 89 static int s_debug = 0; 90 static int s_tlsextdebug = 0; 91 static int s_msg = 0; 92 static int s_quiet = 0; 93 static int s_ign_eof = 0; 94 static int s_brief = 0; 95 96 static char *keymatexportlabel = NULL; 97 static int keymatexportlen = 20; 98 99 static int async = 0; 100 101 static int use_sendfile = 0; 102 static int use_zc_sendfile = 0; 103 104 static const char *session_id_prefix = NULL; 105 106 static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 }; 107 static int enable_client_rpk = 0; 108 109 #ifndef OPENSSL_NO_DTLS 110 static int enable_timeouts = 0; 111 static long socket_mtu; 112 #endif 113 114 /* 115 * We define this but make it always be 0 in no-dtls builds to simplify the 116 * code. 117 */ 118 static int dtlslisten = 0; 119 static int stateless = 0; 120 121 static int early_data = 0; 122 static SSL_SESSION *psksess = NULL; 123 124 static char *psk_identity = "Client_identity"; 125 char *psk_key = NULL; /* by default PSK is not used */ 126 127 static char http_server_binmode = 0; /* for now: 0/1 = default/binary */ 128 129 #ifndef OPENSSL_NO_PSK 130 static unsigned int psk_server_cb(SSL *ssl, const char *identity, 131 unsigned char *psk, 132 unsigned int max_psk_len) 133 { 134 long key_len = 0; 135 unsigned char *key; 136 137 if (s_debug) 138 BIO_printf(bio_s_out, "psk_server_cb\n"); 139 140 if (!SSL_is_dtls(ssl) && SSL_version(ssl) >= TLS1_3_VERSION) { 141 /* 142 * This callback is designed for use in (D)TLSv1.2 (or below). It is 143 * possible to use a single callback for all protocol versions - but it 144 * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we 145 * have psk_find_session_cb. 146 */ 147 return 0; 148 } 149 150 if (identity == NULL) { 151 BIO_printf(bio_err, "Error: client did not send PSK identity\n"); 152 goto out_err; 153 } 154 if (s_debug) 155 BIO_printf(bio_s_out, "identity_len=%d identity=%s\n", 156 (int)strlen(identity), identity); 157 158 /* here we could lookup the given identity e.g. from a database */ 159 if (strcmp(identity, psk_identity) != 0) { 160 BIO_printf(bio_s_out, "PSK warning: client identity not what we expected" 161 " (got '%s' expected '%s')\n", 162 identity, psk_identity); 163 } else { 164 if (s_debug) 165 BIO_printf(bio_s_out, "PSK client identity found\n"); 166 } 167 168 /* convert the PSK key to binary */ 169 key = OPENSSL_hexstr2buf(psk_key, &key_len); 170 if (key == NULL) { 171 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n", 172 psk_key); 173 return 0; 174 } 175 if (key_len > (int)max_psk_len) { 176 BIO_printf(bio_err, 177 "psk buffer of callback is too small (%d) for key (%ld)\n", 178 max_psk_len, key_len); 179 OPENSSL_free(key); 180 return 0; 181 } 182 183 memcpy(psk, key, key_len); 184 OPENSSL_free(key); 185 186 if (s_debug) 187 BIO_printf(bio_s_out, "fetched PSK len=%ld\n", key_len); 188 return key_len; 189 out_err: 190 if (s_debug) 191 BIO_printf(bio_err, "Error in PSK server callback\n"); 192 (void)BIO_flush(bio_err); 193 (void)BIO_flush(bio_s_out); 194 return 0; 195 } 196 #endif 197 198 static int psk_find_session_cb(SSL *ssl, const unsigned char *identity, 199 size_t identity_len, SSL_SESSION **sess) 200 { 201 SSL_SESSION *tmpsess = NULL; 202 unsigned char *key; 203 long key_len; 204 const SSL_CIPHER *cipher = NULL; 205 206 if (strlen(psk_identity) != identity_len 207 || memcmp(psk_identity, identity, identity_len) != 0) { 208 *sess = NULL; 209 return 1; 210 } 211 212 if (psksess != NULL) { 213 if (!SSL_SESSION_up_ref(psksess)) 214 return 0; 215 216 *sess = psksess; 217 return 1; 218 } 219 220 key = OPENSSL_hexstr2buf(psk_key, &key_len); 221 if (key == NULL) { 222 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n", 223 psk_key); 224 return 0; 225 } 226 227 /* We default to SHA256 */ 228 cipher = SSL_CIPHER_find(ssl, tls13_aes128gcmsha256_id); 229 if (cipher == NULL) { 230 BIO_printf(bio_err, "Error finding suitable ciphersuite\n"); 231 OPENSSL_free(key); 232 return 0; 233 } 234 235 tmpsess = SSL_SESSION_new(); 236 if (tmpsess == NULL 237 || !SSL_SESSION_set1_master_key(tmpsess, key, key_len) 238 || !SSL_SESSION_set_cipher(tmpsess, cipher) 239 || !SSL_SESSION_set_protocol_version(tmpsess, SSL_version(ssl))) { 240 OPENSSL_free(key); 241 SSL_SESSION_free(tmpsess); 242 return 0; 243 } 244 OPENSSL_free(key); 245 *sess = tmpsess; 246 247 return 1; 248 } 249 250 #ifndef OPENSSL_NO_SRP 251 static srpsrvparm srp_callback_parm; 252 #endif 253 254 static int local_argc = 0; 255 static char **local_argv; 256 257 #ifdef CHARSET_EBCDIC 258 static int ebcdic_new(BIO *bi); 259 static int ebcdic_free(BIO *a); 260 static int ebcdic_read(BIO *b, char *out, int outl); 261 static int ebcdic_write(BIO *b, const char *in, int inl); 262 static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr); 263 static int ebcdic_gets(BIO *bp, char *buf, int size); 264 static int ebcdic_puts(BIO *bp, const char *str); 265 266 #define BIO_TYPE_EBCDIC_FILTER (18 | 0x0200) 267 static BIO_METHOD *methods_ebcdic = NULL; 268 269 /* This struct is "unwarranted chumminess with the compiler." */ 270 typedef struct { 271 size_t alloced; 272 char buff[1]; 273 } EBCDIC_OUTBUFF; 274 275 static const BIO_METHOD *BIO_f_ebcdic_filter(void) 276 { 277 if (methods_ebcdic == NULL) { 278 methods_ebcdic = BIO_meth_new(BIO_TYPE_EBCDIC_FILTER, 279 "EBCDIC/ASCII filter"); 280 if (methods_ebcdic == NULL 281 || !BIO_meth_set_write(methods_ebcdic, ebcdic_write) 282 || !BIO_meth_set_read(methods_ebcdic, ebcdic_read) 283 || !BIO_meth_set_puts(methods_ebcdic, ebcdic_puts) 284 || !BIO_meth_set_gets(methods_ebcdic, ebcdic_gets) 285 || !BIO_meth_set_ctrl(methods_ebcdic, ebcdic_ctrl) 286 || !BIO_meth_set_create(methods_ebcdic, ebcdic_new) 287 || !BIO_meth_set_destroy(methods_ebcdic, ebcdic_free)) 288 return NULL; 289 } 290 return methods_ebcdic; 291 } 292 293 static int ebcdic_new(BIO *bi) 294 { 295 EBCDIC_OUTBUFF *wbuf; 296 297 wbuf = app_malloc(sizeof(*wbuf) + 1024, "ebcdic wbuf"); 298 wbuf->alloced = 1024; 299 wbuf->buff[0] = '\0'; 300 301 BIO_set_data(bi, wbuf); 302 BIO_set_init(bi, 1); 303 return 1; 304 } 305 306 static int ebcdic_free(BIO *a) 307 { 308 EBCDIC_OUTBUFF *wbuf; 309 310 if (a == NULL) 311 return 0; 312 wbuf = BIO_get_data(a); 313 OPENSSL_free(wbuf); 314 BIO_set_data(a, NULL); 315 BIO_set_init(a, 0); 316 317 return 1; 318 } 319 320 static int ebcdic_read(BIO *b, char *out, int outl) 321 { 322 int ret = 0; 323 BIO *next = BIO_next(b); 324 325 if (out == NULL || outl == 0) 326 return 0; 327 if (next == NULL) 328 return 0; 329 330 ret = BIO_read(next, out, outl); 331 if (ret > 0) 332 ascii2ebcdic(out, out, ret); 333 return ret; 334 } 335 336 static int ebcdic_write(BIO *b, const char *in, int inl) 337 { 338 EBCDIC_OUTBUFF *wbuf; 339 BIO *next = BIO_next(b); 340 int ret = 0; 341 int num; 342 343 if ((in == NULL) || (inl <= 0)) 344 return 0; 345 if (next == NULL) 346 return 0; 347 348 wbuf = (EBCDIC_OUTBUFF *)BIO_get_data(b); 349 350 if (inl > (num = wbuf->alloced)) { 351 num = num + num; /* double the size */ 352 if (num < inl) 353 num = inl; 354 OPENSSL_free(wbuf); 355 wbuf = app_malloc(sizeof(*wbuf) + num, "grow ebcdic wbuf"); 356 357 wbuf->alloced = num; 358 wbuf->buff[0] = '\0'; 359 360 BIO_set_data(b, wbuf); 361 } 362 363 ebcdic2ascii(wbuf->buff, in, inl); 364 365 ret = BIO_write(next, wbuf->buff, inl); 366 367 return ret; 368 } 369 370 static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr) 371 { 372 long ret; 373 BIO *next = BIO_next(b); 374 375 if (next == NULL) 376 return 0; 377 switch (cmd) { 378 case BIO_CTRL_DUP: 379 ret = 0L; 380 break; 381 default: 382 ret = BIO_ctrl(next, cmd, num, ptr); 383 break; 384 } 385 return ret; 386 } 387 388 static int ebcdic_gets(BIO *bp, char *buf, int size) 389 { 390 int i, ret = 0; 391 BIO *next = BIO_next(bp); 392 393 if (next == NULL) 394 return 0; 395 /* return(BIO_gets(bp->next_bio,buf,size));*/ 396 for (i = 0; i < size - 1; ++i) { 397 ret = ebcdic_read(bp, &buf[i], 1); 398 if (ret <= 0) 399 break; 400 else if (buf[i] == '\n') { 401 ++i; 402 break; 403 } 404 } 405 if (i < size) 406 buf[i] = '\0'; 407 return (ret < 0 && i == 0) ? ret : i; 408 } 409 410 static int ebcdic_puts(BIO *bp, const char *str) 411 { 412 if (BIO_next(bp) == NULL) 413 return 0; 414 return ebcdic_write(bp, str, strlen(str)); 415 } 416 #endif 417 418 /* This is a context that we pass to callbacks */ 419 typedef struct tlsextctx_st { 420 char *servername; 421 BIO *biodebug; 422 int extension_error; 423 } tlsextctx; 424 425 static int ssl_servername_cb(SSL *s, int *ad, void *arg) 426 { 427 tlsextctx *p = (tlsextctx *)arg; 428 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); 429 430 if (servername != NULL && p->biodebug != NULL) { 431 const char *cp = servername; 432 unsigned char uc; 433 434 BIO_printf(p->biodebug, "Hostname in TLS extension: \""); 435 while ((uc = *cp++) != 0) 436 BIO_printf(p->biodebug, 437 (((uc) & ~127) == 0) && isprint(uc) ? "%c" : "\\x%02x", uc); 438 BIO_printf(p->biodebug, "\"\n"); 439 } 440 441 if (p->servername == NULL) 442 return SSL_TLSEXT_ERR_NOACK; 443 444 if (servername != NULL) { 445 if (OPENSSL_strcasecmp(servername, p->servername)) 446 return p->extension_error; 447 if (ctx2 != NULL) { 448 BIO_printf(p->biodebug, "Switching server context.\n"); 449 SSL_set_SSL_CTX(s, ctx2); 450 } 451 } 452 return SSL_TLSEXT_ERR_OK; 453 } 454 455 /* Structure passed to cert status callback */ 456 typedef struct tlsextstatusctx_st { 457 int timeout; 458 /* File to load OCSP Response from (or NULL if no file) */ 459 char *respin; 460 /* Default responder to use */ 461 char *host, *path, *port; 462 char *proxy, *no_proxy; 463 int use_ssl; 464 int verbose; 465 } tlsextstatusctx; 466 467 static tlsextstatusctx tlscstatp = { -1 }; 468 469 #ifndef OPENSSL_NO_OCSP 470 471 /* 472 * Helper function to get an OCSP_RESPONSE from a responder. This is a 473 * simplified version. It examines certificates each time and makes one OCSP 474 * responder query for each request. A full version would store details such as 475 * the OCSP certificate IDs and minimise the number of OCSP responses by caching 476 * them until they were considered "expired". 477 */ 478 static int get_ocsp_resp_from_responder(SSL *s, tlsextstatusctx *srctx, 479 OCSP_RESPONSE **resp) 480 { 481 char *host = NULL, *port = NULL, *path = NULL; 482 char *proxy = NULL, *no_proxy = NULL; 483 int use_ssl; 484 STACK_OF(OPENSSL_STRING) *aia = NULL; 485 X509 *x = NULL, *cert; 486 X509_NAME *iname; 487 STACK_OF(X509) *chain = NULL; 488 SSL_CTX *ssl_ctx; 489 X509_STORE_CTX *inctx = NULL; 490 X509_OBJECT *obj; 491 OCSP_REQUEST *req = NULL; 492 OCSP_CERTID *id = NULL; 493 STACK_OF(X509_EXTENSION) *exts; 494 int ret = SSL_TLSEXT_ERR_NOACK; 495 int i; 496 497 /* Build up OCSP query from server certificate */ 498 x = SSL_get_certificate(s); 499 iname = X509_get_issuer_name(x); 500 aia = X509_get1_ocsp(x); 501 if (aia != NULL) { 502 if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia, 0), &use_ssl, 503 NULL, &host, &port, NULL, &path, NULL, NULL)) { 504 BIO_puts(bio_err, "cert_status: can't parse AIA URL\n"); 505 goto err; 506 } 507 if (srctx->verbose) 508 BIO_printf(bio_err, "cert_status: AIA URL: %s\n", 509 sk_OPENSSL_STRING_value(aia, 0)); 510 } else { 511 if (srctx->host == NULL) { 512 BIO_puts(bio_err, 513 "cert_status: no AIA and no default responder URL\n"); 514 goto done; 515 } 516 host = srctx->host; 517 path = srctx->path; 518 port = srctx->port; 519 use_ssl = srctx->use_ssl; 520 } 521 proxy = srctx->proxy; 522 no_proxy = srctx->no_proxy; 523 524 ssl_ctx = SSL_get_SSL_CTX(s); 525 if (!SSL_CTX_get0_chain_certs(ssl_ctx, &chain)) 526 goto err; 527 for (i = 0; i < sk_X509_num(chain); i++) { 528 /* check the untrusted certificate chain (-cert_chain option) */ 529 cert = sk_X509_value(chain, i); 530 if (X509_name_cmp(iname, X509_get_subject_name(cert)) == 0) { 531 /* the issuer certificate is found */ 532 id = OCSP_cert_to_id(NULL, x, cert); 533 break; 534 } 535 } 536 if (id == NULL) { 537 inctx = X509_STORE_CTX_new(); 538 if (inctx == NULL) 539 goto err; 540 if (!X509_STORE_CTX_init(inctx, SSL_CTX_get_cert_store(ssl_ctx), 541 NULL, NULL)) 542 goto err; 543 obj = X509_STORE_CTX_get_obj_by_subject(inctx, X509_LU_X509, iname); 544 if (obj == NULL) { 545 BIO_puts(bio_err, "cert_status: Can't retrieve issuer certificate.\n"); 546 goto done; 547 } 548 id = OCSP_cert_to_id(NULL, x, X509_OBJECT_get0_X509(obj)); 549 X509_OBJECT_free(obj); 550 } 551 if (id == NULL) 552 goto err; 553 req = OCSP_REQUEST_new(); 554 if (req == NULL) 555 goto err; 556 if (!OCSP_request_add0_id(req, id)) 557 goto err; 558 id = NULL; 559 /* Add any extensions to the request */ 560 SSL_get_tlsext_status_exts(s, &exts); 561 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { 562 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); 563 if (!OCSP_REQUEST_add_ext(req, ext, -1)) 564 goto err; 565 } 566 *resp = process_responder(req, host, port, path, proxy, no_proxy, 567 use_ssl, NULL /* headers */, srctx->timeout); 568 if (*resp == NULL) { 569 BIO_puts(bio_err, "cert_status: error querying responder\n"); 570 goto done; 571 } 572 573 ret = SSL_TLSEXT_ERR_OK; 574 goto done; 575 576 err: 577 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 578 done: 579 /* 580 * If we parsed aia we need to free; otherwise they were copied and we 581 * don't 582 */ 583 if (aia != NULL) { 584 OPENSSL_free(host); 585 OPENSSL_free(path); 586 OPENSSL_free(port); 587 X509_email_free(aia); 588 } 589 OCSP_CERTID_free(id); 590 OCSP_REQUEST_free(req); 591 X509_STORE_CTX_free(inctx); 592 return ret; 593 } 594 595 /* 596 * Certificate Status callback. This is called when a client includes a 597 * certificate status request extension. The response is either obtained from a 598 * file, or from an OCSP responder. 599 */ 600 static int cert_status_cb(SSL *s, void *arg) 601 { 602 tlsextstatusctx *srctx = arg; 603 OCSP_RESPONSE *resp = NULL; 604 unsigned char *rspder = NULL; 605 int rspderlen; 606 int ret = SSL_TLSEXT_ERR_ALERT_FATAL; 607 608 if (srctx->verbose) 609 BIO_puts(bio_err, "cert_status: callback called\n"); 610 611 if (srctx->respin != NULL) { 612 BIO *derbio = bio_open_default(srctx->respin, 'r', FORMAT_ASN1); 613 if (derbio == NULL) { 614 BIO_puts(bio_err, "cert_status: Cannot open OCSP response file\n"); 615 goto err; 616 } 617 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL); 618 BIO_free(derbio); 619 if (resp == NULL) { 620 BIO_puts(bio_err, "cert_status: Error reading OCSP response\n"); 621 goto err; 622 } 623 } else { 624 ret = get_ocsp_resp_from_responder(s, srctx, &resp); 625 if (ret != SSL_TLSEXT_ERR_OK) 626 goto err; 627 } 628 629 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder); 630 if (rspderlen <= 0) 631 goto err; 632 633 SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen); 634 if (srctx->verbose) { 635 BIO_puts(bio_err, "cert_status: ocsp response sent:\n"); 636 OCSP_RESPONSE_print(bio_err, resp, 2); 637 } 638 639 ret = SSL_TLSEXT_ERR_OK; 640 641 err: 642 if (ret != SSL_TLSEXT_ERR_OK) 643 ERR_print_errors(bio_err); 644 645 OCSP_RESPONSE_free(resp); 646 647 return ret; 648 } 649 #endif 650 651 #ifndef OPENSSL_NO_NEXTPROTONEG 652 /* This is the context that we pass to next_proto_cb */ 653 typedef struct tlsextnextprotoctx_st { 654 unsigned char *data; 655 size_t len; 656 } tlsextnextprotoctx; 657 658 static int next_proto_cb(SSL *s, const unsigned char **data, 659 unsigned int *len, void *arg) 660 { 661 tlsextnextprotoctx *next_proto = arg; 662 663 *data = next_proto->data; 664 *len = next_proto->len; 665 666 return SSL_TLSEXT_ERR_OK; 667 } 668 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */ 669 670 /* This the context that we pass to alpn_cb */ 671 typedef struct tlsextalpnctx_st { 672 unsigned char *data; 673 size_t len; 674 } tlsextalpnctx; 675 676 static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen, 677 const unsigned char *in, unsigned int inlen, void *arg) 678 { 679 tlsextalpnctx *alpn_ctx = arg; 680 681 if (!s_quiet) { 682 /* We can assume that |in| is syntactically valid. */ 683 unsigned int i; 684 BIO_printf(bio_s_out, "ALPN protocols advertised by the client: "); 685 for (i = 0; i < inlen;) { 686 if (i) 687 BIO_write(bio_s_out, ", ", 2); 688 BIO_write(bio_s_out, &in[i + 1], in[i]); 689 i += in[i] + 1; 690 } 691 BIO_write(bio_s_out, "\n", 1); 692 } 693 694 if (SSL_select_next_proto((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in, 695 inlen) 696 != OPENSSL_NPN_NEGOTIATED) { 697 return SSL_TLSEXT_ERR_ALERT_FATAL; 698 } 699 700 if (!s_quiet) { 701 BIO_printf(bio_s_out, "ALPN protocols selected: "); 702 BIO_write(bio_s_out, *out, *outlen); 703 BIO_write(bio_s_out, "\n", 1); 704 } 705 706 return SSL_TLSEXT_ERR_OK; 707 } 708 709 static int not_resumable_sess_cb(SSL *s, int is_forward_secure) 710 { 711 /* disable resumption for sessions with forward secure ciphers */ 712 return is_forward_secure; 713 } 714 715 typedef enum OPTION_choice { 716 OPT_COMMON, 717 OPT_ENGINE, 718 OPT_4, 719 OPT_6, 720 OPT_ACCEPT, 721 OPT_PORT, 722 OPT_UNIX, 723 OPT_UNLINK, 724 OPT_NACCEPT, 725 OPT_VERIFY, 726 OPT_NAMEOPT, 727 OPT_UPPER_V_VERIFY, 728 OPT_CONTEXT, 729 OPT_CERT, 730 OPT_CRL, 731 OPT_CRL_DOWNLOAD, 732 OPT_SERVERINFO, 733 OPT_CERTFORM, 734 OPT_KEY, 735 OPT_KEYFORM, 736 OPT_PASS, 737 OPT_CERT_CHAIN, 738 OPT_DHPARAM, 739 OPT_DCERTFORM, 740 OPT_DCERT, 741 OPT_DKEYFORM, 742 OPT_DPASS, 743 OPT_DKEY, 744 OPT_DCERT_CHAIN, 745 OPT_NOCERT, 746 OPT_CAPATH, 747 OPT_NOCAPATH, 748 OPT_CHAINCAPATH, 749 OPT_VERIFYCAPATH, 750 OPT_NO_CACHE, 751 OPT_EXT_CACHE, 752 OPT_CRLFORM, 753 OPT_VERIFY_RET_ERROR, 754 OPT_VERIFY_QUIET, 755 OPT_BUILD_CHAIN, 756 OPT_CAFILE, 757 OPT_NOCAFILE, 758 OPT_CHAINCAFILE, 759 OPT_VERIFYCAFILE, 760 OPT_CASTORE, 761 OPT_NOCASTORE, 762 OPT_CHAINCASTORE, 763 OPT_VERIFYCASTORE, 764 OPT_NBIO, 765 OPT_NBIO_TEST, 766 OPT_IGN_EOF, 767 OPT_NO_IGN_EOF, 768 OPT_DEBUG, 769 OPT_TLSEXTDEBUG, 770 OPT_STATUS, 771 OPT_STATUS_VERBOSE, 772 OPT_STATUS_TIMEOUT, 773 OPT_PROXY, 774 OPT_NO_PROXY, 775 OPT_STATUS_URL, 776 OPT_STATUS_FILE, 777 OPT_MSG, 778 OPT_MSGFILE, 779 OPT_TRACE, 780 OPT_SECURITY_DEBUG, 781 OPT_SECURITY_DEBUG_VERBOSE, 782 OPT_STATE, 783 OPT_CRLF, 784 OPT_QUIET, 785 OPT_BRIEF, 786 OPT_NO_DHE, 787 OPT_NO_RESUME_EPHEMERAL, 788 OPT_PSK_IDENTITY, 789 OPT_PSK_HINT, 790 OPT_PSK, 791 OPT_PSK_SESS, 792 OPT_SRPVFILE, 793 OPT_SRPUSERSEED, 794 OPT_REV, 795 OPT_WWW, 796 OPT_UPPER_WWW, 797 OPT_HTTP, 798 OPT_ASYNC, 799 OPT_SSL_CONFIG, 800 OPT_MAX_SEND_FRAG, 801 OPT_SPLIT_SEND_FRAG, 802 OPT_MAX_PIPELINES, 803 OPT_READ_BUF, 804 OPT_SSL3, 805 OPT_TLS1_3, 806 OPT_TLS1_2, 807 OPT_TLS1_1, 808 OPT_TLS1, 809 OPT_DTLS, 810 OPT_DTLS1, 811 OPT_DTLS1_2, 812 OPT_SCTP, 813 OPT_TIMEOUT, 814 OPT_MTU, 815 OPT_LISTEN, 816 OPT_STATELESS, 817 OPT_ID_PREFIX, 818 OPT_SERVERNAME, 819 OPT_SERVERNAME_FATAL, 820 OPT_CERT2, 821 OPT_KEY2, 822 OPT_NEXTPROTONEG, 823 OPT_ALPN, 824 OPT_SENDFILE, 825 OPT_SRTP_PROFILES, 826 OPT_KEYMATEXPORT, 827 OPT_KEYMATEXPORTLEN, 828 OPT_KEYLOG_FILE, 829 OPT_MAX_EARLY, 830 OPT_RECV_MAX_EARLY, 831 OPT_EARLY_DATA, 832 OPT_S_NUM_TICKETS, 833 OPT_ANTI_REPLAY, 834 OPT_NO_ANTI_REPLAY, 835 OPT_SCTP_LABEL_BUG, 836 OPT_HTTP_SERVER_BINMODE, 837 OPT_NOCANAMES, 838 OPT_IGNORE_UNEXPECTED_EOF, 839 OPT_KTLS, 840 OPT_USE_ZC_SENDFILE, 841 OPT_TFO, 842 OPT_CERT_COMP, 843 OPT_ENABLE_SERVER_RPK, 844 OPT_ENABLE_CLIENT_RPK, 845 OPT_R_ENUM, 846 OPT_S_ENUM, 847 OPT_V_ENUM, 848 OPT_X_ENUM, 849 OPT_PROV_ENUM 850 } OPTION_CHOICE; 851 852 const OPTIONS s_server_options[] = { 853 OPT_SECTION("General"), 854 { "help", OPT_HELP, '-', "Display this summary" }, 855 { "ssl_config", OPT_SSL_CONFIG, 's', 856 "Configure SSL_CTX using the given configuration value" }, 857 #ifndef OPENSSL_NO_SSL_TRACE 858 { "trace", OPT_TRACE, '-', "trace protocol messages" }, 859 #endif 860 #ifndef OPENSSL_NO_ENGINE 861 { "engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device" }, 862 #endif 863 864 OPT_SECTION("Network"), 865 { "port", OPT_PORT, 'p', 866 "TCP/IP port to listen on for connections (default is " PORT ")" }, 867 { "accept", OPT_ACCEPT, 's', 868 "TCP/IP optional host and port to listen on for connections (default is *:" PORT ")" }, 869 #ifdef AF_UNIX 870 { "unix", OPT_UNIX, 's', "Unix domain socket to accept on" }, 871 { "unlink", OPT_UNLINK, '-', "For -unix, unlink existing socket first" }, 872 #endif 873 { "4", OPT_4, '-', "Use IPv4 only" }, 874 { "6", OPT_6, '-', "Use IPv6 only" }, 875 #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO) 876 { "tfo", OPT_TFO, '-', "Listen for TCP Fast Open connections" }, 877 #endif 878 879 OPT_SECTION("Identity"), 880 { "context", OPT_CONTEXT, 's', "Set session ID context" }, 881 { "CAfile", OPT_CAFILE, '<', "PEM format file of CA's" }, 882 { "CApath", OPT_CAPATH, '/', "PEM format directory of CA's" }, 883 { "CAstore", OPT_CASTORE, ':', "URI to store of CA's" }, 884 { "no-CAfile", OPT_NOCAFILE, '-', 885 "Do not load the default certificates file" }, 886 { "no-CApath", OPT_NOCAPATH, '-', 887 "Do not load certificates from the default certificates directory" }, 888 { "no-CAstore", OPT_NOCASTORE, '-', 889 "Do not load certificates from the default certificates store URI" }, 890 { "nocert", OPT_NOCERT, '-', "Don't use any certificates (Anon-DH)" }, 891 { "verify", OPT_VERIFY, 'n', "Turn on peer certificate verification" }, 892 { "Verify", OPT_UPPER_V_VERIFY, 'n', 893 "Turn on peer certificate verification, must have a cert" }, 894 { "nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options" }, 895 { "cert", OPT_CERT, '<', "Server certificate file to use; default " TEST_CERT }, 896 { "cert2", OPT_CERT2, '<', 897 "Certificate file to use for servername; default " TEST_CERT2 }, 898 { "certform", OPT_CERTFORM, 'F', 899 "Server certificate file format (PEM/DER/P12); has no effect" }, 900 { "cert_chain", OPT_CERT_CHAIN, '<', 901 "Server certificate chain file in PEM format" }, 902 { "build_chain", OPT_BUILD_CHAIN, '-', "Build server certificate chain" }, 903 { "serverinfo", OPT_SERVERINFO, 's', 904 "PEM serverinfo file for certificate" }, 905 { "key", OPT_KEY, 's', 906 "Private key file to use; default is -cert file or else" TEST_CERT }, 907 { "key2", OPT_KEY2, '<', 908 "-Private Key file to use for servername if not in -cert2" }, 909 { "keyform", OPT_KEYFORM, 'f', "Key format (ENGINE, other values ignored)" }, 910 { "pass", OPT_PASS, 's', "Private key and cert file pass phrase source" }, 911 { "dcert", OPT_DCERT, '<', 912 "Second server certificate file to use (usually for DSA)" }, 913 { "dcertform", OPT_DCERTFORM, 'F', 914 "Second server certificate file format (PEM/DER/P12); has no effect" }, 915 { "dcert_chain", OPT_DCERT_CHAIN, '<', 916 "second server certificate chain file in PEM format" }, 917 { "dkey", OPT_DKEY, '<', 918 "Second private key file to use (usually for DSA)" }, 919 { "dkeyform", OPT_DKEYFORM, 'f', 920 "Second key file format (ENGINE, other values ignored)" }, 921 { "dpass", OPT_DPASS, 's', 922 "Second private key and cert file pass phrase source" }, 923 { "dhparam", OPT_DHPARAM, '<', "DH parameters file to use" }, 924 { "servername", OPT_SERVERNAME, 's', 925 "Servername for HostName TLS extension" }, 926 { "servername_fatal", OPT_SERVERNAME_FATAL, '-', 927 "On servername mismatch send fatal alert (default warning alert)" }, 928 { "nbio_test", OPT_NBIO_TEST, '-', "Test with the non-blocking test bio" }, 929 { "crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF" }, 930 { "quiet", OPT_QUIET, '-', "No server output" }, 931 { "no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL, '-', 932 "Disable caching and tickets if ephemeral (EC)DH is used" }, 933 { "www", OPT_WWW, '-', "Respond to a 'GET /' with a status page" }, 934 { "WWW", OPT_UPPER_WWW, '-', "Respond to a 'GET with the file ./path" }, 935 { "ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF, '-', 936 "Do not treat lack of close_notify from a peer as an error" }, 937 { "tlsextdebug", OPT_TLSEXTDEBUG, '-', 938 "Hex dump of all TLS extensions received" }, 939 { "HTTP", OPT_HTTP, '-', "Like -WWW but ./path includes HTTP headers" }, 940 { "id_prefix", OPT_ID_PREFIX, 's', 941 "Generate SSL/TLS session IDs prefixed by arg" }, 942 { "keymatexport", OPT_KEYMATEXPORT, 's', 943 "Export keying material using label" }, 944 { "keymatexportlen", OPT_KEYMATEXPORTLEN, 'p', 945 "Export len bytes of keying material; default 20" }, 946 { "CRL", OPT_CRL, '<', "CRL file to use" }, 947 { "CRLform", OPT_CRLFORM, 'F', "CRL file format (PEM or DER); default PEM" }, 948 { "crl_download", OPT_CRL_DOWNLOAD, '-', 949 "Download CRLs from distribution points in certificate CDP entries" }, 950 { "chainCAfile", OPT_CHAINCAFILE, '<', 951 "CA file for certificate chain (PEM format)" }, 952 { "chainCApath", OPT_CHAINCAPATH, '/', 953 "use dir as certificate store path to build CA certificate chain" }, 954 { "chainCAstore", OPT_CHAINCASTORE, ':', 955 "use URI as certificate store to build CA certificate chain" }, 956 { "verifyCAfile", OPT_VERIFYCAFILE, '<', 957 "CA file for certificate verification (PEM format)" }, 958 { "verifyCApath", OPT_VERIFYCAPATH, '/', 959 "use dir as certificate store path to verify CA certificate" }, 960 { "verifyCAstore", OPT_VERIFYCASTORE, ':', 961 "use URI as certificate store to verify CA certificate" }, 962 { "no_cache", OPT_NO_CACHE, '-', "Disable session cache" }, 963 { "ext_cache", OPT_EXT_CACHE, '-', 964 "Disable internal cache, set up and use external cache" }, 965 { "verify_return_error", OPT_VERIFY_RET_ERROR, '-', 966 "Close connection on verification error" }, 967 { "verify_quiet", OPT_VERIFY_QUIET, '-', 968 "No verify output except verify errors" }, 969 { "ign_eof", OPT_IGN_EOF, '-', "Ignore input EOF (default when -quiet)" }, 970 { "no_ign_eof", OPT_NO_IGN_EOF, '-', "Do not ignore input EOF" }, 971 #ifndef OPENSSL_NO_COMP_ALG 972 { "cert_comp", OPT_CERT_COMP, '-', "Pre-compress server certificates" }, 973 #endif 974 975 #ifndef OPENSSL_NO_OCSP 976 OPT_SECTION("OCSP"), 977 { "status", OPT_STATUS, '-', "Request certificate status from server" }, 978 { "status_verbose", OPT_STATUS_VERBOSE, '-', 979 "Print more output in certificate status callback" }, 980 { "status_timeout", OPT_STATUS_TIMEOUT, 'n', 981 "Status request responder timeout" }, 982 { "status_url", OPT_STATUS_URL, 's', "Status request fallback URL" }, 983 { "proxy", OPT_PROXY, 's', 984 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored" }, 985 { "no_proxy", OPT_NO_PROXY, 's', 986 "List of addresses of servers not to use HTTP(S) proxy for" }, 987 { OPT_MORE_STR, 0, 0, 988 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none" }, 989 { "status_file", OPT_STATUS_FILE, '<', 990 "File containing DER encoded OCSP Response" }, 991 #endif 992 993 OPT_SECTION("Debug"), 994 { "security_debug", OPT_SECURITY_DEBUG, '-', 995 "Print output from SSL/TLS security framework" }, 996 { "security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-', 997 "Print more output from SSL/TLS security framework" }, 998 { "brief", OPT_BRIEF, '-', 999 "Restrict output to brief summary of connection parameters" }, 1000 { "rev", OPT_REV, '-', 1001 "act as an echo server that sends back received text reversed" }, 1002 { "debug", OPT_DEBUG, '-', "Print more output" }, 1003 { "msg", OPT_MSG, '-', "Show protocol messages" }, 1004 { "msgfile", OPT_MSGFILE, '>', 1005 "File to send output of -msg or -trace, instead of stdout" }, 1006 { "state", OPT_STATE, '-', "Print the SSL states" }, 1007 { "async", OPT_ASYNC, '-', "Operate in asynchronous mode" }, 1008 { "max_pipelines", OPT_MAX_PIPELINES, 'p', 1009 "Maximum number of encrypt/decrypt pipelines to be used" }, 1010 { "naccept", OPT_NACCEPT, 'p', "Terminate after #num connections" }, 1011 { "keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file" }, 1012 1013 OPT_SECTION("Network"), 1014 { "nbio", OPT_NBIO, '-', "Use non-blocking IO" }, 1015 { "timeout", OPT_TIMEOUT, '-', "Enable timeouts" }, 1016 { "mtu", OPT_MTU, 'p', "Set link-layer MTU" }, 1017 { "read_buf", OPT_READ_BUF, 'p', 1018 "Default read buffer size to be used for connections" }, 1019 { "split_send_frag", OPT_SPLIT_SEND_FRAG, 'p', 1020 "Size used to split data for encrypt pipelines" }, 1021 { "max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames " }, 1022 1023 OPT_SECTION("Server identity"), 1024 { "psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity to expect" }, 1025 #ifndef OPENSSL_NO_PSK 1026 { "psk_hint", OPT_PSK_HINT, 's', "PSK identity hint to use" }, 1027 #endif 1028 { "psk", OPT_PSK, 's', "PSK in hex (without 0x)" }, 1029 { "psk_session", OPT_PSK_SESS, '<', "File to read PSK SSL session from" }, 1030 #ifndef OPENSSL_NO_SRP 1031 { "srpvfile", OPT_SRPVFILE, '<', "(deprecated) The verifier file for SRP" }, 1032 { "srpuserseed", OPT_SRPUSERSEED, 's', 1033 "(deprecated) A seed string for a default user salt" }, 1034 #endif 1035 1036 OPT_SECTION("Protocol and version"), 1037 { "max_early_data", OPT_MAX_EARLY, 'n', 1038 "The maximum number of bytes of early data as advertised in tickets" }, 1039 { "recv_max_early_data", OPT_RECV_MAX_EARLY, 'n', 1040 "The maximum number of bytes of early data (hard limit)" }, 1041 { "early_data", OPT_EARLY_DATA, '-', "Attempt to read early data" }, 1042 { "num_tickets", OPT_S_NUM_TICKETS, 'n', 1043 "The number of TLSv1.3 session tickets that a server will automatically issue" }, 1044 { "anti_replay", OPT_ANTI_REPLAY, '-', "Switch on anti-replay protection (default)" }, 1045 { "no_anti_replay", OPT_NO_ANTI_REPLAY, '-', "Switch off anti-replay protection" }, 1046 { "http_server_binmode", OPT_HTTP_SERVER_BINMODE, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)" }, 1047 { "no_ca_names", OPT_NOCANAMES, '-', 1048 "Disable TLS Extension CA Names" }, 1049 { "stateless", OPT_STATELESS, '-', "Require TLSv1.3 cookies" }, 1050 #ifndef OPENSSL_NO_SSL3 1051 { "ssl3", OPT_SSL3, '-', "Just talk SSLv3" }, 1052 #endif 1053 #ifndef OPENSSL_NO_TLS1 1054 { "tls1", OPT_TLS1, '-', "Just talk TLSv1" }, 1055 #endif 1056 #ifndef OPENSSL_NO_TLS1_1 1057 { "tls1_1", OPT_TLS1_1, '-', "Just talk TLSv1.1" }, 1058 #endif 1059 #ifndef OPENSSL_NO_TLS1_2 1060 { "tls1_2", OPT_TLS1_2, '-', "just talk TLSv1.2" }, 1061 #endif 1062 #ifndef OPENSSL_NO_TLS1_3 1063 { "tls1_3", OPT_TLS1_3, '-', "just talk TLSv1.3" }, 1064 #endif 1065 #ifndef OPENSSL_NO_DTLS 1066 { "dtls", OPT_DTLS, '-', "Use any DTLS version" }, 1067 { "listen", OPT_LISTEN, '-', 1068 "Listen for a DTLS ClientHello with a cookie and then connect" }, 1069 #endif 1070 #ifndef OPENSSL_NO_DTLS1 1071 { "dtls1", OPT_DTLS1, '-', "Just talk DTLSv1" }, 1072 #endif 1073 #ifndef OPENSSL_NO_DTLS1_2 1074 { "dtls1_2", OPT_DTLS1_2, '-', "Just talk DTLSv1.2" }, 1075 #endif 1076 #ifndef OPENSSL_NO_SCTP 1077 { "sctp", OPT_SCTP, '-', "Use SCTP" }, 1078 { "sctp_label_bug", OPT_SCTP_LABEL_BUG, '-', "Enable SCTP label length bug" }, 1079 #endif 1080 #ifndef OPENSSL_NO_SRTP 1081 { "use_srtp", OPT_SRTP_PROFILES, 's', 1082 "Offer SRTP key management with a colon-separated profile list" }, 1083 #endif 1084 { "no_dhe", OPT_NO_DHE, '-', "Disable ephemeral DH" }, 1085 #ifndef OPENSSL_NO_NEXTPROTONEG 1086 { "nextprotoneg", OPT_NEXTPROTONEG, 's', 1087 "Set the advertised protocols for the NPN extension (comma-separated list)" }, 1088 #endif 1089 { "alpn", OPT_ALPN, 's', 1090 "Set the advertised protocols for the ALPN extension (comma-separated list)" }, 1091 #ifndef OPENSSL_NO_KTLS 1092 { "ktls", OPT_KTLS, '-', "Enable Kernel TLS for sending and receiving" }, 1093 { "sendfile", OPT_SENDFILE, '-', "Use sendfile to response file with -WWW" }, 1094 { "zerocopy_sendfile", OPT_USE_ZC_SENDFILE, '-', "Use zerocopy mode of KTLS sendfile" }, 1095 #endif 1096 { "enable_server_rpk", OPT_ENABLE_SERVER_RPK, '-', "Enable raw public keys (RFC7250) from the server" }, 1097 { "enable_client_rpk", OPT_ENABLE_CLIENT_RPK, '-', "Enable raw public keys (RFC7250) from the client" }, 1098 OPT_R_OPTIONS, 1099 OPT_S_OPTIONS, 1100 OPT_V_OPTIONS, 1101 OPT_X_OPTIONS, 1102 OPT_PROV_OPTIONS, 1103 { NULL } 1104 }; 1105 1106 #define IS_PROT_FLAG(o) \ 1107 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \ 1108 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2) 1109 1110 int s_server_main(int argc, char *argv[]) 1111 { 1112 ENGINE *engine = NULL; 1113 EVP_PKEY *s_key = NULL, *s_dkey = NULL; 1114 SSL_CONF_CTX *cctx = NULL; 1115 const SSL_METHOD *meth = TLS_server_method(); 1116 SSL_EXCERT *exc = NULL; 1117 STACK_OF(OPENSSL_STRING) *ssl_args = NULL; 1118 STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL; 1119 STACK_OF(X509_CRL) *crls = NULL; 1120 X509 *s_cert = NULL, *s_dcert = NULL; 1121 X509_VERIFY_PARAM *vpm = NULL; 1122 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL; 1123 const char *chCApath = NULL, *chCAfile = NULL, *chCAstore = NULL; 1124 char *dpassarg = NULL, *dpass = NULL; 1125 char *passarg = NULL, *pass = NULL; 1126 char *vfyCApath = NULL, *vfyCAfile = NULL, *vfyCAstore = NULL; 1127 char *crl_file = NULL, *prog; 1128 #ifdef AF_UNIX 1129 int unlink_unix_path = 0; 1130 #endif 1131 do_server_cb server_cb; 1132 int vpmtouched = 0, build_chain = 0, no_cache = 0, ext_cache = 0; 1133 char *dhfile = NULL; 1134 int no_dhe = 0; 1135 int nocert = 0, ret = 1; 1136 int noCApath = 0, noCAfile = 0, noCAstore = 0; 1137 int s_cert_format = FORMAT_UNDEF, s_key_format = FORMAT_UNDEF; 1138 int s_dcert_format = FORMAT_UNDEF, s_dkey_format = FORMAT_UNDEF; 1139 int rev = 0, naccept = -1, sdebug = 0; 1140 int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0; 1141 int state = 0, crl_format = FORMAT_UNDEF, crl_download = 0; 1142 char *host = NULL; 1143 char *port = NULL; 1144 unsigned char *context = NULL; 1145 OPTION_CHOICE o; 1146 EVP_PKEY *s_key2 = NULL; 1147 X509 *s_cert2 = NULL; 1148 tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING }; 1149 const char *ssl_config = NULL; 1150 int read_buf_len = 0; 1151 #ifndef OPENSSL_NO_NEXTPROTONEG 1152 const char *next_proto_neg_in = NULL; 1153 tlsextnextprotoctx next_proto = { NULL, 0 }; 1154 #endif 1155 const char *alpn_in = NULL; 1156 tlsextalpnctx alpn_ctx = { NULL, 0 }; 1157 #ifndef OPENSSL_NO_PSK 1158 /* by default do not send a PSK identity hint */ 1159 char *psk_identity_hint = NULL; 1160 #endif 1161 char *p; 1162 #ifndef OPENSSL_NO_SRP 1163 char *srpuserseed = NULL; 1164 char *srp_verifier_file = NULL; 1165 #endif 1166 #ifndef OPENSSL_NO_SRTP 1167 char *srtp_profiles = NULL; 1168 #endif 1169 int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0; 1170 int s_server_verify = SSL_VERIFY_NONE; 1171 int s_server_session_id_context = 1; /* anything will do */ 1172 const char *s_cert_file = TEST_CERT, *s_key_file = NULL, *s_chain_file = NULL; 1173 const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL; 1174 char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL; 1175 #ifndef OPENSSL_NO_OCSP 1176 int s_tlsextstatus = 0; 1177 #endif 1178 int no_resume_ephemeral = 0; 1179 unsigned int max_send_fragment = 0; 1180 unsigned int split_send_fragment = 0, max_pipelines = 0; 1181 const char *s_serverinfo_file = NULL; 1182 const char *keylog_file = NULL; 1183 int max_early_data = -1, recv_max_early_data = -1; 1184 char *psksessf = NULL; 1185 int no_ca_names = 0; 1186 #ifndef OPENSSL_NO_SCTP 1187 int sctp_label_bug = 0; 1188 #endif 1189 int ignore_unexpected_eof = 0; 1190 #ifndef OPENSSL_NO_KTLS 1191 int enable_ktls = 0; 1192 #endif 1193 int tfo = 0; 1194 int cert_comp = 0; 1195 int enable_server_rpk = 0; 1196 1197 /* Init of few remaining global variables */ 1198 local_argc = argc; 1199 local_argv = argv; 1200 1201 ctx = ctx2 = NULL; 1202 s_nbio = s_nbio_test = 0; 1203 www = 0; 1204 bio_s_out = NULL; 1205 s_debug = 0; 1206 s_msg = 0; 1207 s_quiet = 0; 1208 s_brief = 0; 1209 async = 0; 1210 use_sendfile = 0; 1211 use_zc_sendfile = 0; 1212 1213 port = OPENSSL_strdup(PORT); 1214 cctx = SSL_CONF_CTX_new(); 1215 vpm = X509_VERIFY_PARAM_new(); 1216 if (port == NULL || cctx == NULL || vpm == NULL) 1217 goto end; 1218 SSL_CONF_CTX_set_flags(cctx, 1219 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CMDLINE); 1220 1221 prog = opt_init(argc, argv, s_server_options); 1222 while ((o = opt_next()) != OPT_EOF) { 1223 if (IS_PROT_FLAG(o) && ++prot_opt > 1) { 1224 BIO_printf(bio_err, "Cannot supply multiple protocol flags\n"); 1225 goto end; 1226 } 1227 if (IS_NO_PROT_FLAG(o)) 1228 no_prot_opt++; 1229 if (prot_opt == 1 && no_prot_opt) { 1230 BIO_printf(bio_err, 1231 "Cannot supply both a protocol flag and '-no_<prot>'\n"); 1232 goto end; 1233 } 1234 switch (o) { 1235 case OPT_EOF: 1236 case OPT_ERR: 1237 opthelp: 1238 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 1239 goto end; 1240 case OPT_HELP: 1241 opt_help(s_server_options); 1242 ret = 0; 1243 goto end; 1244 1245 case OPT_4: 1246 #ifdef AF_UNIX 1247 if (socket_family == AF_UNIX) { 1248 OPENSSL_free(host); 1249 host = NULL; 1250 OPENSSL_free(port); 1251 port = NULL; 1252 } 1253 #endif 1254 socket_family = AF_INET; 1255 break; 1256 case OPT_6: 1257 if (1) { 1258 #ifdef AF_INET6 1259 #ifdef AF_UNIX 1260 if (socket_family == AF_UNIX) { 1261 OPENSSL_free(host); 1262 host = NULL; 1263 OPENSSL_free(port); 1264 port = NULL; 1265 } 1266 #endif 1267 socket_family = AF_INET6; 1268 } else { 1269 #endif 1270 BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\n", prog); 1271 goto end; 1272 } 1273 break; 1274 case OPT_PORT: 1275 #ifdef AF_UNIX 1276 if (socket_family == AF_UNIX) { 1277 socket_family = AF_UNSPEC; 1278 } 1279 #endif 1280 OPENSSL_free(port); 1281 port = NULL; 1282 OPENSSL_free(host); 1283 host = NULL; 1284 if (BIO_parse_hostserv(opt_arg(), NULL, &port, BIO_PARSE_PRIO_SERV) < 1) { 1285 BIO_printf(bio_err, 1286 "%s: -port argument malformed or ambiguous\n", 1287 prog); 1288 goto end; 1289 } 1290 break; 1291 case OPT_ACCEPT: 1292 #ifdef AF_UNIX 1293 if (socket_family == AF_UNIX) { 1294 socket_family = AF_UNSPEC; 1295 } 1296 #endif 1297 OPENSSL_free(port); 1298 port = NULL; 1299 OPENSSL_free(host); 1300 host = NULL; 1301 if (BIO_parse_hostserv(opt_arg(), &host, &port, BIO_PARSE_PRIO_SERV) < 1) { 1302 BIO_printf(bio_err, 1303 "%s: -accept argument malformed or ambiguous\n", 1304 prog); 1305 goto end; 1306 } 1307 break; 1308 #ifdef AF_UNIX 1309 case OPT_UNIX: 1310 socket_family = AF_UNIX; 1311 OPENSSL_free(host); 1312 host = OPENSSL_strdup(opt_arg()); 1313 if (host == NULL) 1314 goto end; 1315 OPENSSL_free(port); 1316 port = NULL; 1317 break; 1318 case OPT_UNLINK: 1319 unlink_unix_path = 1; 1320 break; 1321 #endif 1322 case OPT_NACCEPT: 1323 naccept = atol(opt_arg()); 1324 break; 1325 case OPT_VERIFY: 1326 s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE; 1327 verify_args.depth = atoi(opt_arg()); 1328 if (!s_quiet) 1329 BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth); 1330 break; 1331 case OPT_UPPER_V_VERIFY: 1332 s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE; 1333 verify_args.depth = atoi(opt_arg()); 1334 if (!s_quiet) 1335 BIO_printf(bio_err, 1336 "verify depth is %d, must return a certificate\n", 1337 verify_args.depth); 1338 break; 1339 case OPT_CONTEXT: 1340 context = (unsigned char *)opt_arg(); 1341 break; 1342 case OPT_CERT: 1343 s_cert_file = opt_arg(); 1344 break; 1345 case OPT_NAMEOPT: 1346 if (!set_nameopt(opt_arg())) 1347 goto end; 1348 break; 1349 case OPT_CRL: 1350 crl_file = opt_arg(); 1351 break; 1352 case OPT_CRL_DOWNLOAD: 1353 crl_download = 1; 1354 break; 1355 case OPT_SERVERINFO: 1356 s_serverinfo_file = opt_arg(); 1357 break; 1358 case OPT_CERTFORM: 1359 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_cert_format)) 1360 goto opthelp; 1361 break; 1362 case OPT_KEY: 1363 s_key_file = opt_arg(); 1364 break; 1365 case OPT_KEYFORM: 1366 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_key_format)) 1367 goto opthelp; 1368 break; 1369 case OPT_PASS: 1370 passarg = opt_arg(); 1371 break; 1372 case OPT_CERT_CHAIN: 1373 s_chain_file = opt_arg(); 1374 break; 1375 case OPT_DHPARAM: 1376 dhfile = opt_arg(); 1377 break; 1378 case OPT_DCERTFORM: 1379 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_dcert_format)) 1380 goto opthelp; 1381 break; 1382 case OPT_DCERT: 1383 s_dcert_file = opt_arg(); 1384 break; 1385 case OPT_DKEYFORM: 1386 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_dkey_format)) 1387 goto opthelp; 1388 break; 1389 case OPT_DPASS: 1390 dpassarg = opt_arg(); 1391 break; 1392 case OPT_DKEY: 1393 s_dkey_file = opt_arg(); 1394 break; 1395 case OPT_DCERT_CHAIN: 1396 s_dchain_file = opt_arg(); 1397 break; 1398 case OPT_NOCERT: 1399 nocert = 1; 1400 break; 1401 case OPT_CAPATH: 1402 CApath = opt_arg(); 1403 break; 1404 case OPT_NOCAPATH: 1405 noCApath = 1; 1406 break; 1407 case OPT_CHAINCAPATH: 1408 chCApath = opt_arg(); 1409 break; 1410 case OPT_VERIFYCAPATH: 1411 vfyCApath = opt_arg(); 1412 break; 1413 case OPT_CASTORE: 1414 CAstore = opt_arg(); 1415 break; 1416 case OPT_NOCASTORE: 1417 noCAstore = 1; 1418 break; 1419 case OPT_CHAINCASTORE: 1420 chCAstore = opt_arg(); 1421 break; 1422 case OPT_VERIFYCASTORE: 1423 vfyCAstore = opt_arg(); 1424 break; 1425 case OPT_NO_CACHE: 1426 no_cache = 1; 1427 break; 1428 case OPT_EXT_CACHE: 1429 ext_cache = 1; 1430 break; 1431 case OPT_CRLFORM: 1432 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format)) 1433 goto opthelp; 1434 break; 1435 case OPT_S_CASES: 1436 case OPT_S_NUM_TICKETS: 1437 case OPT_ANTI_REPLAY: 1438 case OPT_NO_ANTI_REPLAY: 1439 if (ssl_args == NULL) 1440 ssl_args = sk_OPENSSL_STRING_new_null(); 1441 if (ssl_args == NULL 1442 || !sk_OPENSSL_STRING_push(ssl_args, opt_flag()) 1443 || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) { 1444 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog); 1445 goto end; 1446 } 1447 break; 1448 case OPT_V_CASES: 1449 if (!opt_verify(o, vpm)) 1450 goto end; 1451 vpmtouched++; 1452 break; 1453 case OPT_X_CASES: 1454 if (!args_excert(o, &exc)) 1455 goto end; 1456 break; 1457 case OPT_VERIFY_RET_ERROR: 1458 verify_args.return_error = 1; 1459 break; 1460 case OPT_VERIFY_QUIET: 1461 verify_args.quiet = 1; 1462 break; 1463 case OPT_BUILD_CHAIN: 1464 build_chain = 1; 1465 break; 1466 case OPT_CAFILE: 1467 CAfile = opt_arg(); 1468 break; 1469 case OPT_NOCAFILE: 1470 noCAfile = 1; 1471 break; 1472 case OPT_CHAINCAFILE: 1473 chCAfile = opt_arg(); 1474 break; 1475 case OPT_VERIFYCAFILE: 1476 vfyCAfile = opt_arg(); 1477 break; 1478 case OPT_NBIO: 1479 s_nbio = 1; 1480 break; 1481 case OPT_NBIO_TEST: 1482 s_nbio = s_nbio_test = 1; 1483 break; 1484 case OPT_IGN_EOF: 1485 s_ign_eof = 1; 1486 break; 1487 case OPT_NO_IGN_EOF: 1488 s_ign_eof = 0; 1489 break; 1490 case OPT_DEBUG: 1491 s_debug = 1; 1492 break; 1493 case OPT_TLSEXTDEBUG: 1494 s_tlsextdebug = 1; 1495 break; 1496 case OPT_STATUS: 1497 #ifndef OPENSSL_NO_OCSP 1498 s_tlsextstatus = 1; 1499 #endif 1500 break; 1501 case OPT_STATUS_VERBOSE: 1502 #ifndef OPENSSL_NO_OCSP 1503 s_tlsextstatus = tlscstatp.verbose = 1; 1504 #endif 1505 break; 1506 case OPT_STATUS_TIMEOUT: 1507 #ifndef OPENSSL_NO_OCSP 1508 s_tlsextstatus = 1; 1509 tlscstatp.timeout = atoi(opt_arg()); 1510 #endif 1511 break; 1512 case OPT_PROXY: 1513 #ifndef OPENSSL_NO_OCSP 1514 tlscstatp.proxy = opt_arg(); 1515 #endif 1516 break; 1517 case OPT_NO_PROXY: 1518 #ifndef OPENSSL_NO_OCSP 1519 tlscstatp.no_proxy = opt_arg(); 1520 #endif 1521 break; 1522 case OPT_STATUS_URL: 1523 #ifndef OPENSSL_NO_OCSP 1524 s_tlsextstatus = 1; 1525 if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp.use_ssl, NULL, 1526 &tlscstatp.host, &tlscstatp.port, NULL, 1527 &tlscstatp.path, NULL, NULL)) { 1528 BIO_printf(bio_err, "Error parsing -status_url argument\n"); 1529 goto end; 1530 } 1531 #endif 1532 break; 1533 case OPT_STATUS_FILE: 1534 #ifndef OPENSSL_NO_OCSP 1535 s_tlsextstatus = 1; 1536 tlscstatp.respin = opt_arg(); 1537 #endif 1538 break; 1539 case OPT_MSG: 1540 s_msg = 1; 1541 break; 1542 case OPT_MSGFILE: 1543 bio_s_msg = BIO_new_file(opt_arg(), "w"); 1544 if (bio_s_msg == NULL) { 1545 BIO_printf(bio_err, "Error writing file %s\n", opt_arg()); 1546 goto end; 1547 } 1548 break; 1549 case OPT_TRACE: 1550 #ifndef OPENSSL_NO_SSL_TRACE 1551 s_msg = 2; 1552 #endif 1553 break; 1554 case OPT_SECURITY_DEBUG: 1555 sdebug = 1; 1556 break; 1557 case OPT_SECURITY_DEBUG_VERBOSE: 1558 sdebug = 2; 1559 break; 1560 case OPT_STATE: 1561 state = 1; 1562 break; 1563 case OPT_CRLF: 1564 s_crlf = 1; 1565 break; 1566 case OPT_QUIET: 1567 s_quiet = 1; 1568 break; 1569 case OPT_BRIEF: 1570 s_quiet = s_brief = verify_args.quiet = 1; 1571 break; 1572 case OPT_NO_DHE: 1573 no_dhe = 1; 1574 break; 1575 case OPT_NO_RESUME_EPHEMERAL: 1576 no_resume_ephemeral = 1; 1577 break; 1578 case OPT_PSK_IDENTITY: 1579 psk_identity = opt_arg(); 1580 break; 1581 case OPT_PSK_HINT: 1582 #ifndef OPENSSL_NO_PSK 1583 psk_identity_hint = opt_arg(); 1584 #endif 1585 break; 1586 case OPT_PSK: 1587 for (p = psk_key = opt_arg(); *p; p++) { 1588 if (isxdigit(_UC(*p))) 1589 continue; 1590 BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key); 1591 goto end; 1592 } 1593 break; 1594 case OPT_PSK_SESS: 1595 psksessf = opt_arg(); 1596 break; 1597 case OPT_SRPVFILE: 1598 #ifndef OPENSSL_NO_SRP 1599 srp_verifier_file = opt_arg(); 1600 if (min_version < TLS1_VERSION) 1601 min_version = TLS1_VERSION; 1602 #endif 1603 break; 1604 case OPT_SRPUSERSEED: 1605 #ifndef OPENSSL_NO_SRP 1606 srpuserseed = opt_arg(); 1607 if (min_version < TLS1_VERSION) 1608 min_version = TLS1_VERSION; 1609 #endif 1610 break; 1611 case OPT_REV: 1612 rev = 1; 1613 break; 1614 case OPT_WWW: 1615 www = 1; 1616 break; 1617 case OPT_UPPER_WWW: 1618 www = 2; 1619 break; 1620 case OPT_HTTP: 1621 www = 3; 1622 break; 1623 case OPT_SSL_CONFIG: 1624 ssl_config = opt_arg(); 1625 break; 1626 case OPT_SSL3: 1627 min_version = SSL3_VERSION; 1628 max_version = SSL3_VERSION; 1629 break; 1630 case OPT_TLS1_3: 1631 min_version = TLS1_3_VERSION; 1632 max_version = TLS1_3_VERSION; 1633 break; 1634 case OPT_TLS1_2: 1635 min_version = TLS1_2_VERSION; 1636 max_version = TLS1_2_VERSION; 1637 break; 1638 case OPT_TLS1_1: 1639 min_version = TLS1_1_VERSION; 1640 max_version = TLS1_1_VERSION; 1641 break; 1642 case OPT_TLS1: 1643 min_version = TLS1_VERSION; 1644 max_version = TLS1_VERSION; 1645 break; 1646 case OPT_DTLS: 1647 #ifndef OPENSSL_NO_DTLS 1648 meth = DTLS_server_method(); 1649 socket_type = SOCK_DGRAM; 1650 #endif 1651 break; 1652 case OPT_DTLS1: 1653 #ifndef OPENSSL_NO_DTLS 1654 meth = DTLS_server_method(); 1655 min_version = DTLS1_VERSION; 1656 max_version = DTLS1_VERSION; 1657 socket_type = SOCK_DGRAM; 1658 #endif 1659 break; 1660 case OPT_DTLS1_2: 1661 #ifndef OPENSSL_NO_DTLS 1662 meth = DTLS_server_method(); 1663 min_version = DTLS1_2_VERSION; 1664 max_version = DTLS1_2_VERSION; 1665 socket_type = SOCK_DGRAM; 1666 #endif 1667 break; 1668 case OPT_SCTP: 1669 #ifndef OPENSSL_NO_SCTP 1670 protocol = IPPROTO_SCTP; 1671 #endif 1672 break; 1673 case OPT_SCTP_LABEL_BUG: 1674 #ifndef OPENSSL_NO_SCTP 1675 sctp_label_bug = 1; 1676 #endif 1677 break; 1678 case OPT_TIMEOUT: 1679 #ifndef OPENSSL_NO_DTLS 1680 enable_timeouts = 1; 1681 #endif 1682 break; 1683 case OPT_MTU: 1684 #ifndef OPENSSL_NO_DTLS 1685 socket_mtu = atol(opt_arg()); 1686 #endif 1687 break; 1688 case OPT_LISTEN: 1689 #ifndef OPENSSL_NO_DTLS 1690 dtlslisten = 1; 1691 #endif 1692 break; 1693 case OPT_STATELESS: 1694 stateless = 1; 1695 break; 1696 case OPT_ID_PREFIX: 1697 session_id_prefix = opt_arg(); 1698 break; 1699 case OPT_ENGINE: 1700 #ifndef OPENSSL_NO_ENGINE 1701 engine = setup_engine(opt_arg(), s_debug); 1702 #endif 1703 break; 1704 case OPT_R_CASES: 1705 if (!opt_rand(o)) 1706 goto end; 1707 break; 1708 case OPT_PROV_CASES: 1709 if (!opt_provider(o)) 1710 goto end; 1711 break; 1712 case OPT_SERVERNAME: 1713 tlsextcbp.servername = opt_arg(); 1714 break; 1715 case OPT_SERVERNAME_FATAL: 1716 tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; 1717 break; 1718 case OPT_CERT2: 1719 s_cert_file2 = opt_arg(); 1720 break; 1721 case OPT_KEY2: 1722 s_key_file2 = opt_arg(); 1723 break; 1724 case OPT_NEXTPROTONEG: 1725 #ifndef OPENSSL_NO_NEXTPROTONEG 1726 next_proto_neg_in = opt_arg(); 1727 #endif 1728 break; 1729 case OPT_ALPN: 1730 alpn_in = opt_arg(); 1731 break; 1732 case OPT_SRTP_PROFILES: 1733 #ifndef OPENSSL_NO_SRTP 1734 srtp_profiles = opt_arg(); 1735 #endif 1736 break; 1737 case OPT_KEYMATEXPORT: 1738 keymatexportlabel = opt_arg(); 1739 break; 1740 case OPT_KEYMATEXPORTLEN: 1741 keymatexportlen = atoi(opt_arg()); 1742 break; 1743 case OPT_ASYNC: 1744 async = 1; 1745 break; 1746 case OPT_MAX_SEND_FRAG: 1747 max_send_fragment = atoi(opt_arg()); 1748 break; 1749 case OPT_SPLIT_SEND_FRAG: 1750 split_send_fragment = atoi(opt_arg()); 1751 break; 1752 case OPT_MAX_PIPELINES: 1753 max_pipelines = atoi(opt_arg()); 1754 break; 1755 case OPT_READ_BUF: 1756 read_buf_len = atoi(opt_arg()); 1757 break; 1758 case OPT_KEYLOG_FILE: 1759 keylog_file = opt_arg(); 1760 break; 1761 case OPT_MAX_EARLY: 1762 max_early_data = atoi(opt_arg()); 1763 if (max_early_data < 0) { 1764 BIO_printf(bio_err, "Invalid value for max_early_data\n"); 1765 goto end; 1766 } 1767 break; 1768 case OPT_RECV_MAX_EARLY: 1769 recv_max_early_data = atoi(opt_arg()); 1770 if (recv_max_early_data < 0) { 1771 BIO_printf(bio_err, "Invalid value for recv_max_early_data\n"); 1772 goto end; 1773 } 1774 break; 1775 case OPT_EARLY_DATA: 1776 early_data = 1; 1777 if (max_early_data == -1) 1778 max_early_data = SSL3_RT_MAX_PLAIN_LENGTH; 1779 break; 1780 case OPT_HTTP_SERVER_BINMODE: 1781 http_server_binmode = 1; 1782 break; 1783 case OPT_NOCANAMES: 1784 no_ca_names = 1; 1785 break; 1786 case OPT_KTLS: 1787 #ifndef OPENSSL_NO_KTLS 1788 enable_ktls = 1; 1789 #endif 1790 break; 1791 case OPT_SENDFILE: 1792 #ifndef OPENSSL_NO_KTLS 1793 use_sendfile = 1; 1794 #endif 1795 break; 1796 case OPT_USE_ZC_SENDFILE: 1797 #ifndef OPENSSL_NO_KTLS 1798 use_zc_sendfile = 1; 1799 #endif 1800 break; 1801 case OPT_IGNORE_UNEXPECTED_EOF: 1802 ignore_unexpected_eof = 1; 1803 break; 1804 case OPT_TFO: 1805 tfo = 1; 1806 break; 1807 case OPT_CERT_COMP: 1808 cert_comp = 1; 1809 break; 1810 case OPT_ENABLE_SERVER_RPK: 1811 enable_server_rpk = 1; 1812 break; 1813 case OPT_ENABLE_CLIENT_RPK: 1814 enable_client_rpk = 1; 1815 break; 1816 } 1817 } 1818 1819 /* No extra arguments. */ 1820 if (!opt_check_rest_arg(NULL)) 1821 goto opthelp; 1822 1823 if (!app_RAND_load()) 1824 goto end; 1825 1826 #ifndef OPENSSL_NO_NEXTPROTONEG 1827 if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) { 1828 BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n"); 1829 goto opthelp; 1830 } 1831 #endif 1832 #ifndef OPENSSL_NO_DTLS 1833 if (www && socket_type == SOCK_DGRAM) { 1834 BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n"); 1835 goto end; 1836 } 1837 1838 if (dtlslisten && socket_type != SOCK_DGRAM) { 1839 BIO_printf(bio_err, "Can only use -listen with DTLS\n"); 1840 goto end; 1841 } 1842 1843 if (rev && socket_type == SOCK_DGRAM) { 1844 BIO_printf(bio_err, "Can't use -rev with DTLS\n"); 1845 goto end; 1846 } 1847 #endif 1848 1849 if (tfo && socket_type != SOCK_STREAM) { 1850 BIO_printf(bio_err, "Can only use -tfo with TLS\n"); 1851 goto end; 1852 } 1853 1854 if (stateless && socket_type != SOCK_STREAM) { 1855 BIO_printf(bio_err, "Can only use --stateless with TLS\n"); 1856 goto end; 1857 } 1858 1859 #ifdef AF_UNIX 1860 if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) { 1861 BIO_printf(bio_err, 1862 "Can't use unix sockets and datagrams together\n"); 1863 goto end; 1864 } 1865 #endif 1866 if (early_data && rev) { 1867 BIO_printf(bio_err, 1868 "Can't use -early_data in combination with -rev\n"); 1869 goto end; 1870 } 1871 1872 #ifndef OPENSSL_NO_SCTP 1873 if (protocol == IPPROTO_SCTP) { 1874 if (socket_type != SOCK_DGRAM) { 1875 BIO_printf(bio_err, "Can't use -sctp without DTLS\n"); 1876 goto end; 1877 } 1878 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */ 1879 socket_type = SOCK_STREAM; 1880 } 1881 #endif 1882 1883 #ifndef OPENSSL_NO_KTLS 1884 if (use_zc_sendfile && !use_sendfile) { 1885 BIO_printf(bio_out, "Warning: -zerocopy_sendfile depends on -sendfile, enabling -sendfile now.\n"); 1886 use_sendfile = 1; 1887 } 1888 1889 if (use_sendfile && enable_ktls == 0) { 1890 BIO_printf(bio_out, "Warning: -sendfile depends on -ktls, enabling -ktls now.\n"); 1891 enable_ktls = 1; 1892 } 1893 1894 if (use_sendfile && www <= 1) { 1895 BIO_printf(bio_err, "Can't use -sendfile without -WWW or -HTTP\n"); 1896 goto end; 1897 } 1898 #endif 1899 1900 if (!app_passwd(passarg, dpassarg, &pass, &dpass)) { 1901 BIO_printf(bio_err, "Error getting password\n"); 1902 goto end; 1903 } 1904 1905 if (s_key_file == NULL) 1906 s_key_file = s_cert_file; 1907 1908 if (s_key_file2 == NULL) 1909 s_key_file2 = s_cert_file2; 1910 1911 if (!load_excert(&exc)) 1912 goto end; 1913 1914 if (nocert == 0) { 1915 s_key = load_key(s_key_file, s_key_format, 0, pass, engine, 1916 "server certificate private key"); 1917 if (s_key == NULL) 1918 goto end; 1919 1920 s_cert = load_cert_pass(s_cert_file, s_cert_format, 1, pass, 1921 "server certificate"); 1922 1923 if (s_cert == NULL) 1924 goto end; 1925 if (s_chain_file != NULL) { 1926 if (!load_certs(s_chain_file, 0, &s_chain, NULL, 1927 "server certificate chain")) 1928 goto end; 1929 } 1930 1931 if (tlsextcbp.servername != NULL) { 1932 s_key2 = load_key(s_key_file2, s_key_format, 0, pass, engine, 1933 "second server certificate private key"); 1934 if (s_key2 == NULL) 1935 goto end; 1936 1937 s_cert2 = load_cert_pass(s_cert_file2, s_cert_format, 1, pass, 1938 "second server certificate"); 1939 1940 if (s_cert2 == NULL) 1941 goto end; 1942 } 1943 } 1944 #if !defined(OPENSSL_NO_NEXTPROTONEG) 1945 if (next_proto_neg_in) { 1946 next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in); 1947 if (next_proto.data == NULL) 1948 goto end; 1949 } 1950 #endif 1951 alpn_ctx.data = NULL; 1952 if (alpn_in) { 1953 alpn_ctx.data = next_protos_parse(&alpn_ctx.len, alpn_in); 1954 if (alpn_ctx.data == NULL) 1955 goto end; 1956 } 1957 1958 if (crl_file != NULL) { 1959 X509_CRL *crl; 1960 crl = load_crl(crl_file, crl_format, 0, "CRL"); 1961 if (crl == NULL) 1962 goto end; 1963 crls = sk_X509_CRL_new_null(); 1964 if (crls == NULL || !sk_X509_CRL_push(crls, crl)) { 1965 BIO_puts(bio_err, "Error adding CRL\n"); 1966 ERR_print_errors(bio_err); 1967 X509_CRL_free(crl); 1968 goto end; 1969 } 1970 } 1971 1972 if (s_dcert_file != NULL) { 1973 1974 if (s_dkey_file == NULL) 1975 s_dkey_file = s_dcert_file; 1976 1977 s_dkey = load_key(s_dkey_file, s_dkey_format, 1978 0, dpass, engine, "second certificate private key"); 1979 if (s_dkey == NULL) 1980 goto end; 1981 1982 s_dcert = load_cert_pass(s_dcert_file, s_dcert_format, 1, dpass, 1983 "second server certificate"); 1984 1985 if (s_dcert == NULL) { 1986 ERR_print_errors(bio_err); 1987 goto end; 1988 } 1989 if (s_dchain_file != NULL) { 1990 if (!load_certs(s_dchain_file, 0, &s_dchain, NULL, 1991 "second server certificate chain")) 1992 goto end; 1993 } 1994 } 1995 1996 if (bio_s_out == NULL) { 1997 if (s_quiet && !s_debug) { 1998 bio_s_out = BIO_new(BIO_s_null()); 1999 if (s_msg && bio_s_msg == NULL) { 2000 bio_s_msg = dup_bio_out(FORMAT_TEXT); 2001 if (bio_s_msg == NULL) { 2002 BIO_printf(bio_err, "Out of memory\n"); 2003 goto end; 2004 } 2005 } 2006 } else { 2007 bio_s_out = dup_bio_out(FORMAT_TEXT); 2008 } 2009 } 2010 2011 if (bio_s_out == NULL) 2012 goto end; 2013 2014 if (nocert) { 2015 s_cert_file = NULL; 2016 s_key_file = NULL; 2017 s_dcert_file = NULL; 2018 s_dkey_file = NULL; 2019 s_cert_file2 = NULL; 2020 s_key_file2 = NULL; 2021 } 2022 2023 ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth); 2024 if (ctx == NULL) { 2025 ERR_print_errors(bio_err); 2026 goto end; 2027 } 2028 2029 SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY); 2030 2031 if (sdebug) 2032 ssl_ctx_security_debug(ctx, sdebug); 2033 2034 if (!config_ctx(cctx, ssl_args, ctx)) 2035 goto end; 2036 2037 if (ssl_config) { 2038 if (SSL_CTX_config(ctx, ssl_config) == 0) { 2039 BIO_printf(bio_err, "Error using configuration \"%s\"\n", 2040 ssl_config); 2041 ERR_print_errors(bio_err); 2042 goto end; 2043 } 2044 } 2045 #ifndef OPENSSL_NO_SCTP 2046 if (protocol == IPPROTO_SCTP && sctp_label_bug == 1) 2047 SSL_CTX_set_mode(ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG); 2048 #endif 2049 2050 if (min_version != 0 2051 && SSL_CTX_set_min_proto_version(ctx, min_version) == 0) 2052 goto end; 2053 if (max_version != 0 2054 && SSL_CTX_set_max_proto_version(ctx, max_version) == 0) 2055 goto end; 2056 2057 if (session_id_prefix) { 2058 if (strlen(session_id_prefix) >= 32) 2059 BIO_printf(bio_err, 2060 "warning: id_prefix is too long, only one new session will be possible\n"); 2061 if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) { 2062 BIO_printf(bio_err, "error setting 'id_prefix'\n"); 2063 ERR_print_errors(bio_err); 2064 goto end; 2065 } 2066 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix); 2067 } 2068 if (exc != NULL) 2069 ssl_ctx_set_excert(ctx, exc); 2070 2071 if (state) 2072 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback); 2073 if (no_cache) 2074 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); 2075 else if (ext_cache) 2076 init_session_cache_ctx(ctx); 2077 else 2078 SSL_CTX_sess_set_cache_size(ctx, 128); 2079 2080 if (async) { 2081 SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC); 2082 } 2083 2084 if (no_ca_names) { 2085 SSL_CTX_set_options(ctx, SSL_OP_DISABLE_TLSEXT_CA_NAMES); 2086 } 2087 2088 if (ignore_unexpected_eof) 2089 SSL_CTX_set_options(ctx, SSL_OP_IGNORE_UNEXPECTED_EOF); 2090 #ifndef OPENSSL_NO_KTLS 2091 if (enable_ktls) 2092 SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS); 2093 if (use_zc_sendfile) 2094 SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE); 2095 #endif 2096 2097 if (max_send_fragment > 0 2098 && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) { 2099 BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n", 2100 prog, max_send_fragment); 2101 goto end; 2102 } 2103 2104 if (split_send_fragment > 0 2105 && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) { 2106 BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n", 2107 prog, split_send_fragment); 2108 goto end; 2109 } 2110 if (max_pipelines > 0 2111 && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) { 2112 BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n", 2113 prog, max_pipelines); 2114 goto end; 2115 } 2116 2117 if (read_buf_len > 0) { 2118 SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len); 2119 } 2120 #ifndef OPENSSL_NO_SRTP 2121 if (srtp_profiles != NULL) { 2122 /* Returns 0 on success! */ 2123 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) { 2124 BIO_printf(bio_err, "Error setting SRTP profile\n"); 2125 ERR_print_errors(bio_err); 2126 goto end; 2127 } 2128 } 2129 #endif 2130 2131 if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath, 2132 CAstore, noCAstore)) { 2133 ERR_print_errors(bio_err); 2134 goto end; 2135 } 2136 if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) { 2137 BIO_printf(bio_err, "Error setting verify params\n"); 2138 ERR_print_errors(bio_err); 2139 goto end; 2140 } 2141 2142 ssl_ctx_add_crls(ctx, crls, 0); 2143 2144 if (!ssl_load_stores(ctx, 2145 vfyCApath, vfyCAfile, vfyCAstore, 2146 chCApath, chCAfile, chCAstore, 2147 crls, crl_download)) { 2148 BIO_printf(bio_err, "Error loading store locations\n"); 2149 ERR_print_errors(bio_err); 2150 goto end; 2151 } 2152 2153 if (s_cert2) { 2154 ctx2 = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth); 2155 if (ctx2 == NULL) { 2156 ERR_print_errors(bio_err); 2157 goto end; 2158 } 2159 } 2160 2161 if (ctx2 != NULL) { 2162 BIO_printf(bio_s_out, "Setting secondary ctx parameters\n"); 2163 2164 if (sdebug) 2165 ssl_ctx_security_debug(ctx2, sdebug); 2166 2167 if (session_id_prefix) { 2168 if (strlen(session_id_prefix) >= 32) 2169 BIO_printf(bio_err, 2170 "warning: id_prefix is too long, only one new session will be possible\n"); 2171 if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) { 2172 BIO_printf(bio_err, "error setting 'id_prefix'\n"); 2173 ERR_print_errors(bio_err); 2174 goto end; 2175 } 2176 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix); 2177 } 2178 if (exc != NULL) 2179 ssl_ctx_set_excert(ctx2, exc); 2180 2181 if (state) 2182 SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback); 2183 2184 if (no_cache) 2185 SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF); 2186 else if (ext_cache) 2187 init_session_cache_ctx(ctx2); 2188 else 2189 SSL_CTX_sess_set_cache_size(ctx2, 128); 2190 2191 if (async) 2192 SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC); 2193 2194 if (!ctx_set_verify_locations(ctx2, CAfile, noCAfile, CApath, 2195 noCApath, CAstore, noCAstore)) { 2196 ERR_print_errors(bio_err); 2197 goto end; 2198 } 2199 if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) { 2200 BIO_printf(bio_err, "Error setting verify params\n"); 2201 ERR_print_errors(bio_err); 2202 goto end; 2203 } 2204 2205 ssl_ctx_add_crls(ctx2, crls, 0); 2206 if (!config_ctx(cctx, ssl_args, ctx2)) 2207 goto end; 2208 } 2209 #ifndef OPENSSL_NO_NEXTPROTONEG 2210 if (next_proto.data) 2211 SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, 2212 &next_proto); 2213 #endif 2214 if (alpn_ctx.data) 2215 SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx); 2216 2217 if (!no_dhe) { 2218 EVP_PKEY *dhpkey = NULL; 2219 2220 if (dhfile != NULL) 2221 dhpkey = load_keyparams(dhfile, FORMAT_UNDEF, 0, "DH", "DH parameters"); 2222 else if (s_cert_file != NULL) 2223 dhpkey = load_keyparams_suppress(s_cert_file, FORMAT_UNDEF, 0, "DH", 2224 "DH parameters", 1); 2225 2226 if (dhpkey != NULL) { 2227 BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 2228 } else { 2229 BIO_printf(bio_s_out, "Using default temp DH parameters\n"); 2230 } 2231 (void)BIO_flush(bio_s_out); 2232 2233 if (dhpkey == NULL) { 2234 SSL_CTX_set_dh_auto(ctx, 1); 2235 } else { 2236 /* 2237 * We need 2 references: one for use by ctx and one for use by 2238 * ctx2 2239 */ 2240 if (!EVP_PKEY_up_ref(dhpkey)) { 2241 EVP_PKEY_free(dhpkey); 2242 goto end; 2243 } 2244 if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey)) { 2245 BIO_puts(bio_err, "Error setting temp DH parameters\n"); 2246 ERR_print_errors(bio_err); 2247 /* Free 2 references */ 2248 EVP_PKEY_free(dhpkey); 2249 EVP_PKEY_free(dhpkey); 2250 goto end; 2251 } 2252 } 2253 2254 if (ctx2 != NULL) { 2255 if (dhfile != NULL) { 2256 EVP_PKEY *dhpkey2 = load_keyparams_suppress(s_cert_file2, 2257 FORMAT_UNDEF, 2258 0, "DH", 2259 "DH parameters", 1); 2260 2261 if (dhpkey2 != NULL) { 2262 BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 2263 (void)BIO_flush(bio_s_out); 2264 2265 EVP_PKEY_free(dhpkey); 2266 dhpkey = dhpkey2; 2267 } 2268 } 2269 if (dhpkey == NULL) { 2270 SSL_CTX_set_dh_auto(ctx2, 1); 2271 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2, dhpkey)) { 2272 BIO_puts(bio_err, "Error setting temp DH parameters\n"); 2273 ERR_print_errors(bio_err); 2274 EVP_PKEY_free(dhpkey); 2275 goto end; 2276 } 2277 dhpkey = NULL; 2278 } 2279 EVP_PKEY_free(dhpkey); 2280 } 2281 2282 if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain)) 2283 goto end; 2284 2285 if (s_serverinfo_file != NULL 2286 && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) { 2287 ERR_print_errors(bio_err); 2288 goto end; 2289 } 2290 2291 if (ctx2 != NULL 2292 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain)) 2293 goto end; 2294 2295 if (s_dcert != NULL) { 2296 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain)) 2297 goto end; 2298 } 2299 2300 if (no_resume_ephemeral) { 2301 SSL_CTX_set_not_resumable_session_callback(ctx, 2302 not_resumable_sess_cb); 2303 2304 if (ctx2 != NULL) 2305 SSL_CTX_set_not_resumable_session_callback(ctx2, 2306 not_resumable_sess_cb); 2307 } 2308 #ifndef OPENSSL_NO_PSK 2309 if (psk_key != NULL) { 2310 if (s_debug) 2311 BIO_printf(bio_s_out, "PSK key given, setting server callback\n"); 2312 SSL_CTX_set_psk_server_callback(ctx, psk_server_cb); 2313 } 2314 2315 if (psk_identity_hint != NULL) { 2316 if (min_version == TLS1_3_VERSION) { 2317 BIO_printf(bio_s_out, "PSK warning: there is NO identity hint in TLSv1.3\n"); 2318 } else { 2319 if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) { 2320 BIO_printf(bio_err, "error setting PSK identity hint to context\n"); 2321 ERR_print_errors(bio_err); 2322 goto end; 2323 } 2324 } 2325 } 2326 #endif 2327 if (psksessf != NULL) { 2328 BIO *stmp = BIO_new_file(psksessf, "r"); 2329 2330 if (stmp == NULL) { 2331 BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf); 2332 ERR_print_errors(bio_err); 2333 goto end; 2334 } 2335 psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL); 2336 BIO_free(stmp); 2337 if (psksess == NULL) { 2338 BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf); 2339 ERR_print_errors(bio_err); 2340 goto end; 2341 } 2342 } 2343 2344 if (psk_key != NULL || psksess != NULL) 2345 SSL_CTX_set_psk_find_session_callback(ctx, psk_find_session_cb); 2346 2347 SSL_CTX_set_verify(ctx, s_server_verify, verify_callback); 2348 if (!SSL_CTX_set_session_id_context(ctx, 2349 (void *)&s_server_session_id_context, 2350 sizeof(s_server_session_id_context))) { 2351 BIO_printf(bio_err, "error setting session id context\n"); 2352 ERR_print_errors(bio_err); 2353 goto end; 2354 } 2355 2356 /* Set DTLS cookie generation and verification callbacks */ 2357 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback); 2358 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback); 2359 2360 /* Set TLS1.3 cookie generation and verification callbacks */ 2361 SSL_CTX_set_stateless_cookie_generate_cb(ctx, generate_stateless_cookie_callback); 2362 SSL_CTX_set_stateless_cookie_verify_cb(ctx, verify_stateless_cookie_callback); 2363 2364 if (ctx2 != NULL) { 2365 SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback); 2366 if (!SSL_CTX_set_session_id_context(ctx2, 2367 (void *)&s_server_session_id_context, 2368 sizeof(s_server_session_id_context))) { 2369 BIO_printf(bio_err, "error setting session id context\n"); 2370 ERR_print_errors(bio_err); 2371 goto end; 2372 } 2373 tlsextcbp.biodebug = bio_s_out; 2374 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb); 2375 SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp); 2376 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); 2377 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp); 2378 } 2379 2380 #ifndef OPENSSL_NO_SRP 2381 if (srp_verifier_file != NULL) { 2382 if (!set_up_srp_verifier_file(ctx, &srp_callback_parm, srpuserseed, 2383 srp_verifier_file)) 2384 goto end; 2385 } else 2386 #endif 2387 if (CAfile != NULL) { 2388 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile)); 2389 2390 if (ctx2) 2391 SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile)); 2392 } 2393 #ifndef OPENSSL_NO_OCSP 2394 if (s_tlsextstatus) { 2395 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb); 2396 SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp); 2397 if (ctx2) { 2398 SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb); 2399 SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp); 2400 } 2401 } 2402 #endif 2403 if (set_keylog_file(ctx, keylog_file)) 2404 goto end; 2405 2406 if (max_early_data >= 0) 2407 SSL_CTX_set_max_early_data(ctx, max_early_data); 2408 if (recv_max_early_data >= 0) 2409 SSL_CTX_set_recv_max_early_data(ctx, recv_max_early_data); 2410 2411 if (cert_comp) { 2412 BIO_printf(bio_s_out, "Compressing certificates\n"); 2413 if (!SSL_CTX_compress_certs(ctx, 0)) 2414 BIO_printf(bio_s_out, "Error compressing certs on ctx\n"); 2415 if (ctx2 != NULL && !SSL_CTX_compress_certs(ctx2, 0)) 2416 BIO_printf(bio_s_out, "Error compressing certs on ctx2\n"); 2417 } 2418 if (enable_server_rpk) 2419 if (!SSL_CTX_set1_server_cert_type(ctx, cert_type_rpk, sizeof(cert_type_rpk))) { 2420 BIO_printf(bio_s_out, "Error setting server certificate types\n"); 2421 goto end; 2422 } 2423 if (enable_client_rpk) 2424 if (!SSL_CTX_set1_client_cert_type(ctx, cert_type_rpk, sizeof(cert_type_rpk))) { 2425 BIO_printf(bio_s_out, "Error setting server certificate types\n"); 2426 goto end; 2427 } 2428 2429 if (rev) 2430 server_cb = rev_body; 2431 else if (www) 2432 server_cb = www_body; 2433 else 2434 server_cb = sv_body; 2435 #ifdef AF_UNIX 2436 if (socket_family == AF_UNIX 2437 && unlink_unix_path) 2438 unlink(host); 2439 #endif 2440 if (tfo) 2441 BIO_printf(bio_s_out, "Listening for TFO\n"); 2442 do_server(&accept_socket, host, port, socket_family, socket_type, protocol, 2443 server_cb, context, naccept, bio_s_out, tfo); 2444 print_stats(bio_s_out, ctx); 2445 ret = 0; 2446 end: 2447 SSL_CTX_free(ctx); 2448 SSL_SESSION_free(psksess); 2449 set_keylog_file(NULL, NULL); 2450 X509_free(s_cert); 2451 sk_X509_CRL_pop_free(crls, X509_CRL_free); 2452 X509_free(s_dcert); 2453 EVP_PKEY_free(s_key); 2454 EVP_PKEY_free(s_dkey); 2455 OSSL_STACK_OF_X509_free(s_chain); 2456 OSSL_STACK_OF_X509_free(s_dchain); 2457 OPENSSL_free(pass); 2458 OPENSSL_free(dpass); 2459 OPENSSL_free(host); 2460 OPENSSL_free(port); 2461 X509_VERIFY_PARAM_free(vpm); 2462 free_sessions(); 2463 OPENSSL_free(tlscstatp.host); 2464 OPENSSL_free(tlscstatp.port); 2465 OPENSSL_free(tlscstatp.path); 2466 SSL_CTX_free(ctx2); 2467 X509_free(s_cert2); 2468 EVP_PKEY_free(s_key2); 2469 #ifndef OPENSSL_NO_NEXTPROTONEG 2470 OPENSSL_free(next_proto.data); 2471 #endif 2472 OPENSSL_free(alpn_ctx.data); 2473 ssl_excert_free(exc); 2474 sk_OPENSSL_STRING_free(ssl_args); 2475 SSL_CONF_CTX_free(cctx); 2476 release_engine(engine); 2477 BIO_free(bio_s_out); 2478 bio_s_out = NULL; 2479 BIO_free(bio_s_msg); 2480 bio_s_msg = NULL; 2481 #ifdef CHARSET_EBCDIC 2482 BIO_meth_free(methods_ebcdic); 2483 #endif 2484 return ret; 2485 } 2486 2487 static void print_stats(BIO *bio, SSL_CTX *ssl_ctx) 2488 { 2489 BIO_printf(bio, "%4ld items in the session cache\n", 2490 SSL_CTX_sess_number(ssl_ctx)); 2491 BIO_printf(bio, "%4ld client connects (SSL_connect())\n", 2492 SSL_CTX_sess_connect(ssl_ctx)); 2493 BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n", 2494 SSL_CTX_sess_connect_renegotiate(ssl_ctx)); 2495 BIO_printf(bio, "%4ld client connects that finished\n", 2496 SSL_CTX_sess_connect_good(ssl_ctx)); 2497 BIO_printf(bio, "%4ld server accepts (SSL_accept())\n", 2498 SSL_CTX_sess_accept(ssl_ctx)); 2499 BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n", 2500 SSL_CTX_sess_accept_renegotiate(ssl_ctx)); 2501 BIO_printf(bio, "%4ld server accepts that finished\n", 2502 SSL_CTX_sess_accept_good(ssl_ctx)); 2503 BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx)); 2504 BIO_printf(bio, "%4ld session cache misses\n", 2505 SSL_CTX_sess_misses(ssl_ctx)); 2506 BIO_printf(bio, "%4ld session cache timeouts\n", 2507 SSL_CTX_sess_timeouts(ssl_ctx)); 2508 BIO_printf(bio, "%4ld callback cache hits\n", 2509 SSL_CTX_sess_cb_hits(ssl_ctx)); 2510 BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n", 2511 SSL_CTX_sess_cache_full(ssl_ctx), 2512 SSL_CTX_sess_get_cache_size(ssl_ctx)); 2513 } 2514 2515 static long int count_reads_callback(BIO *bio, int cmd, const char *argp, size_t len, 2516 int argi, long argl, int ret, size_t *processed) 2517 { 2518 unsigned int *p_counter = (unsigned int *)BIO_get_callback_arg(bio); 2519 2520 switch (cmd) { 2521 case BIO_CB_READ: /* No break here */ 2522 case BIO_CB_GETS: 2523 if (p_counter != NULL) 2524 ++*p_counter; 2525 break; 2526 default: 2527 break; 2528 } 2529 2530 if (s_debug) { 2531 BIO_set_callback_arg(bio, (char *)bio_s_out); 2532 ret = (int)bio_dump_callback(bio, cmd, argp, len, argi, argl, ret, processed); 2533 BIO_set_callback_arg(bio, (char *)p_counter); 2534 } 2535 2536 return ret; 2537 } 2538 2539 static int sv_body(int s, int stype, int prot, unsigned char *context) 2540 { 2541 char *buf = NULL; 2542 fd_set readfds; 2543 int ret = 1, width; 2544 int k; 2545 unsigned long l; 2546 SSL *con = NULL; 2547 BIO *sbio; 2548 struct timeval timeout; 2549 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)) 2550 struct timeval *timeoutp; 2551 #endif 2552 #ifndef OPENSSL_NO_DTLS 2553 #ifndef OPENSSL_NO_SCTP 2554 int isdtls = (stype == SOCK_DGRAM || prot == IPPROTO_SCTP); 2555 #else 2556 int isdtls = (stype == SOCK_DGRAM); 2557 #endif 2558 #endif 2559 2560 buf = app_malloc(bufsize, "server buffer"); 2561 if (s_nbio) { 2562 if (!BIO_socket_nbio(s, 1)) 2563 ERR_print_errors(bio_err); 2564 else if (!s_quiet) 2565 BIO_printf(bio_err, "Turned on non blocking io\n"); 2566 } 2567 2568 con = SSL_new(ctx); 2569 if (con == NULL) { 2570 ret = -1; 2571 goto err; 2572 } 2573 2574 if (s_tlsextdebug) { 2575 SSL_set_tlsext_debug_callback(con, tlsext_cb); 2576 SSL_set_tlsext_debug_arg(con, bio_s_out); 2577 } 2578 2579 if (context != NULL 2580 && !SSL_set_session_id_context(con, context, 2581 strlen((char *)context))) { 2582 BIO_printf(bio_err, "Error setting session id context\n"); 2583 ret = -1; 2584 goto err; 2585 } 2586 2587 if (!SSL_clear(con)) { 2588 BIO_printf(bio_err, "Error clearing SSL connection\n"); 2589 ret = -1; 2590 goto err; 2591 } 2592 #ifndef OPENSSL_NO_DTLS 2593 if (isdtls) { 2594 #ifndef OPENSSL_NO_SCTP 2595 if (prot == IPPROTO_SCTP) 2596 sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE); 2597 else 2598 #endif 2599 sbio = BIO_new_dgram(s, BIO_NOCLOSE); 2600 if (sbio == NULL) { 2601 BIO_printf(bio_err, "Unable to create BIO\n"); 2602 ERR_print_errors(bio_err); 2603 goto err; 2604 } 2605 2606 if (enable_timeouts) { 2607 timeout.tv_sec = 0; 2608 timeout.tv_usec = DGRAM_RCV_TIMEOUT; 2609 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); 2610 2611 timeout.tv_sec = 0; 2612 timeout.tv_usec = DGRAM_SND_TIMEOUT; 2613 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); 2614 } 2615 2616 if (socket_mtu) { 2617 if (socket_mtu < DTLS_get_link_min_mtu(con)) { 2618 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n", 2619 DTLS_get_link_min_mtu(con)); 2620 ret = -1; 2621 BIO_free(sbio); 2622 goto err; 2623 } 2624 SSL_set_options(con, SSL_OP_NO_QUERY_MTU); 2625 if (!DTLS_set_link_mtu(con, socket_mtu)) { 2626 BIO_printf(bio_err, "Failed to set MTU\n"); 2627 ret = -1; 2628 BIO_free(sbio); 2629 goto err; 2630 } 2631 } else 2632 /* want to do MTU discovery */ 2633 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); 2634 2635 #ifndef OPENSSL_NO_SCTP 2636 if (prot != IPPROTO_SCTP) 2637 #endif 2638 /* Turn on cookie exchange. Not necessary for SCTP */ 2639 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE); 2640 } else 2641 #endif 2642 sbio = BIO_new_socket(s, BIO_NOCLOSE); 2643 2644 if (sbio == NULL) { 2645 BIO_printf(bio_err, "Unable to create BIO\n"); 2646 ERR_print_errors(bio_err); 2647 goto err; 2648 } 2649 2650 if (s_nbio_test) { 2651 BIO *test; 2652 2653 test = BIO_new(BIO_f_nbio_test()); 2654 if (test == NULL) { 2655 BIO_printf(bio_err, "Unable to create BIO\n"); 2656 ret = -1; 2657 BIO_free(sbio); 2658 goto err; 2659 } 2660 sbio = BIO_push(test, sbio); 2661 } 2662 2663 SSL_set_bio(con, sbio, sbio); 2664 SSL_set_accept_state(con); 2665 /* SSL_set_fd(con,s); */ 2666 2667 BIO_set_callback_ex(SSL_get_rbio(con), count_reads_callback); 2668 if (s_msg) { 2669 #ifndef OPENSSL_NO_SSL_TRACE 2670 if (s_msg == 2) 2671 SSL_set_msg_callback(con, SSL_trace); 2672 else 2673 #endif 2674 SSL_set_msg_callback(con, msg_cb); 2675 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); 2676 } 2677 2678 if (s_tlsextdebug) { 2679 SSL_set_tlsext_debug_callback(con, tlsext_cb); 2680 SSL_set_tlsext_debug_arg(con, bio_s_out); 2681 } 2682 2683 if (early_data) { 2684 int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR; 2685 size_t readbytes; 2686 2687 while (edret != SSL_READ_EARLY_DATA_FINISH) { 2688 for (;;) { 2689 edret = SSL_read_early_data(con, buf, bufsize, &readbytes); 2690 if (edret != SSL_READ_EARLY_DATA_ERROR) 2691 break; 2692 2693 switch (SSL_get_error(con, 0)) { 2694 case SSL_ERROR_WANT_WRITE: 2695 case SSL_ERROR_WANT_ASYNC: 2696 case SSL_ERROR_WANT_READ: 2697 /* Just keep trying - busy waiting */ 2698 continue; 2699 default: 2700 BIO_printf(bio_err, "Error reading early data\n"); 2701 ERR_print_errors(bio_err); 2702 goto err; 2703 } 2704 } 2705 if (readbytes > 0) { 2706 if (write_header) { 2707 BIO_printf(bio_s_out, "Early data received:\n"); 2708 write_header = 0; 2709 } 2710 raw_write_stdout(buf, (unsigned int)readbytes); 2711 (void)BIO_flush(bio_s_out); 2712 } 2713 } 2714 if (write_header) { 2715 if (SSL_get_early_data_status(con) == SSL_EARLY_DATA_NOT_SENT) 2716 BIO_printf(bio_s_out, "No early data received\n"); 2717 else 2718 BIO_printf(bio_s_out, "Early data was rejected\n"); 2719 } else { 2720 BIO_printf(bio_s_out, "\nEnd of early data\n"); 2721 } 2722 if (SSL_is_init_finished(con)) 2723 print_connection_info(con); 2724 } 2725 2726 if (fileno_stdin() > s) 2727 width = fileno_stdin() + 1; 2728 else 2729 width = s + 1; 2730 for (;;) { 2731 int i; 2732 int read_from_terminal; 2733 int read_from_sslcon; 2734 2735 read_from_terminal = 0; 2736 read_from_sslcon = SSL_has_pending(con) 2737 || (async && SSL_waiting_for_async(con)); 2738 2739 if (!read_from_sslcon) { 2740 FD_ZERO(&readfds); 2741 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) 2742 openssl_fdset(fileno_stdin(), &readfds); 2743 #endif 2744 openssl_fdset(s, &readfds); 2745 /* 2746 * Note: under VMS with SOCKETSHR the second parameter is 2747 * currently of type (int *) whereas under other systems it is 2748 * (void *) if you don't have a cast it will choke the compiler: 2749 * if you do have a cast then you can either go for (int *) or 2750 * (void *). 2751 */ 2752 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) 2753 /* 2754 * Under DOS (non-djgpp) and Windows we can't select on stdin: 2755 * only on sockets. As a workaround we timeout the select every 2756 * second and check for any keypress. In a proper Windows 2757 * application we wouldn't do this because it is inefficient. 2758 */ 2759 timeout.tv_sec = 1; 2760 timeout.tv_usec = 0; 2761 i = select(width, (void *)&readfds, NULL, NULL, &timeout); 2762 if (has_stdin_waiting()) 2763 read_from_terminal = 1; 2764 if ((i < 0) || (!i && !read_from_terminal)) 2765 continue; 2766 #else 2767 if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout)) 2768 timeoutp = &timeout; 2769 else 2770 timeoutp = NULL; 2771 2772 i = select(width, (void *)&readfds, NULL, NULL, timeoutp); 2773 2774 if ((SSL_is_dtls(con)) && DTLSv1_handle_timeout(con) > 0) 2775 BIO_printf(bio_err, "TIMEOUT occurred\n"); 2776 2777 if (i <= 0) 2778 continue; 2779 if (FD_ISSET(fileno_stdin(), &readfds)) 2780 read_from_terminal = 1; 2781 #endif 2782 if (FD_ISSET(s, &readfds)) 2783 read_from_sslcon = 1; 2784 } 2785 if (read_from_terminal) { 2786 if (s_crlf) { 2787 int j, lf_num; 2788 2789 i = raw_read_stdin(buf, bufsize / 2); 2790 lf_num = 0; 2791 /* both loops are skipped when i <= 0 */ 2792 for (j = 0; j < i; j++) 2793 if (buf[j] == '\n') 2794 lf_num++; 2795 for (j = i - 1; j >= 0; j--) { 2796 buf[j + lf_num] = buf[j]; 2797 if (buf[j] == '\n') { 2798 lf_num--; 2799 i++; 2800 buf[j + lf_num] = '\r'; 2801 } 2802 } 2803 assert(lf_num == 0); 2804 } else { 2805 i = raw_read_stdin(buf, bufsize); 2806 } 2807 2808 if (!s_quiet && !s_brief) { 2809 if ((i <= 0) || (buf[0] == 'Q')) { 2810 BIO_printf(bio_s_out, "DONE\n"); 2811 (void)BIO_flush(bio_s_out); 2812 BIO_closesocket(s); 2813 close_accept_socket(); 2814 ret = -11; 2815 goto err; 2816 } 2817 if ((i <= 0) || (buf[0] == 'q')) { 2818 BIO_printf(bio_s_out, "DONE\n"); 2819 (void)BIO_flush(bio_s_out); 2820 if (SSL_version(con) != DTLS1_VERSION) 2821 BIO_closesocket(s); 2822 /* 2823 * close_accept_socket(); ret= -11; 2824 */ 2825 goto err; 2826 } 2827 if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) { 2828 SSL_renegotiate(con); 2829 i = SSL_do_handshake(con); 2830 printf("SSL_do_handshake -> %d\n", i); 2831 continue; 2832 } 2833 if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) { 2834 SSL_set_verify(con, 2835 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, 2836 NULL); 2837 SSL_renegotiate(con); 2838 i = SSL_do_handshake(con); 2839 printf("SSL_do_handshake -> %d\n", i); 2840 continue; 2841 } 2842 if ((buf[0] == 'K' || buf[0] == 'k') 2843 && ((buf[1] == '\n') || (buf[1] == '\r'))) { 2844 SSL_key_update(con, buf[0] == 'K' ? SSL_KEY_UPDATE_REQUESTED : SSL_KEY_UPDATE_NOT_REQUESTED); 2845 i = SSL_do_handshake(con); 2846 printf("SSL_do_handshake -> %d\n", i); 2847 continue; 2848 } 2849 if (buf[0] == 'c' && ((buf[1] == '\n') || (buf[1] == '\r'))) { 2850 SSL_set_verify(con, SSL_VERIFY_PEER, NULL); 2851 i = SSL_verify_client_post_handshake(con); 2852 if (i == 0) { 2853 printf("Failed to initiate request\n"); 2854 ERR_print_errors(bio_err); 2855 } else { 2856 i = SSL_do_handshake(con); 2857 printf("SSL_do_handshake -> %d\n", i); 2858 } 2859 continue; 2860 } 2861 if (buf[0] == 'P') { 2862 static const char str[] = "Lets print some clear text\n"; 2863 BIO_write(SSL_get_wbio(con), str, sizeof(str) - 1); 2864 } 2865 if (buf[0] == 'S') { 2866 print_stats(bio_s_out, SSL_get_SSL_CTX(con)); 2867 } 2868 } 2869 #ifdef CHARSET_EBCDIC 2870 ebcdic2ascii(buf, buf, i); 2871 #endif 2872 l = k = 0; 2873 for (;;) { 2874 /* should do a select for the write */ 2875 #ifdef RENEG 2876 static count = 0; 2877 if (++count == 100) { 2878 count = 0; 2879 SSL_renegotiate(con); 2880 } 2881 #endif 2882 k = SSL_write(con, &(buf[l]), (unsigned int)i); 2883 #ifndef OPENSSL_NO_SRP 2884 while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) { 2885 BIO_printf(bio_s_out, "LOOKUP renego during write\n"); 2886 2887 lookup_srp_user(&srp_callback_parm, bio_s_out); 2888 2889 k = SSL_write(con, &(buf[l]), (unsigned int)i); 2890 } 2891 #endif 2892 switch (SSL_get_error(con, k)) { 2893 case SSL_ERROR_NONE: 2894 break; 2895 case SSL_ERROR_WANT_ASYNC: 2896 BIO_printf(bio_s_out, "Write BLOCK (Async)\n"); 2897 (void)BIO_flush(bio_s_out); 2898 wait_for_async(con); 2899 break; 2900 case SSL_ERROR_WANT_WRITE: 2901 case SSL_ERROR_WANT_READ: 2902 case SSL_ERROR_WANT_X509_LOOKUP: 2903 BIO_printf(bio_s_out, "Write BLOCK\n"); 2904 (void)BIO_flush(bio_s_out); 2905 break; 2906 case SSL_ERROR_WANT_ASYNC_JOB: 2907 /* 2908 * This shouldn't ever happen in s_server. Treat as an error 2909 */ 2910 case SSL_ERROR_SYSCALL: 2911 case SSL_ERROR_SSL: 2912 BIO_printf(bio_s_out, "ERROR\n"); 2913 (void)BIO_flush(bio_s_out); 2914 ERR_print_errors(bio_err); 2915 ret = 1; 2916 goto err; 2917 /* break; */ 2918 case SSL_ERROR_ZERO_RETURN: 2919 BIO_printf(bio_s_out, "DONE\n"); 2920 (void)BIO_flush(bio_s_out); 2921 ret = 1; 2922 goto err; 2923 } 2924 if (k > 0) { 2925 l += k; 2926 i -= k; 2927 } 2928 if (i <= 0) 2929 break; 2930 } 2931 } 2932 if (read_from_sslcon) { 2933 /* 2934 * init_ssl_connection handles all async events itself so if we're 2935 * waiting for async then we shouldn't go back into 2936 * init_ssl_connection 2937 */ 2938 if ((!async || !SSL_waiting_for_async(con)) 2939 && !SSL_is_init_finished(con)) { 2940 /* 2941 * Count number of reads during init_ssl_connection. 2942 * It helps us to distinguish configuration errors from errors 2943 * caused by a client. 2944 */ 2945 unsigned int read_counter = 0; 2946 2947 BIO_set_callback_arg(SSL_get_rbio(con), (char *)&read_counter); 2948 i = init_ssl_connection(con); 2949 BIO_set_callback_arg(SSL_get_rbio(con), NULL); 2950 2951 /* 2952 * If initialization fails without reads, then 2953 * there was a fatal error in configuration. 2954 */ 2955 if (i <= 0 && read_counter == 0) { 2956 ret = -1; 2957 goto err; 2958 } 2959 if (i < 0) { 2960 ret = 0; 2961 goto err; 2962 } else if (i == 0) { 2963 ret = 1; 2964 goto err; 2965 } 2966 } else { 2967 again: 2968 i = SSL_read(con, (char *)buf, bufsize); 2969 #ifndef OPENSSL_NO_SRP 2970 while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { 2971 BIO_printf(bio_s_out, "LOOKUP renego during read\n"); 2972 2973 lookup_srp_user(&srp_callback_parm, bio_s_out); 2974 2975 i = SSL_read(con, (char *)buf, bufsize); 2976 } 2977 #endif 2978 switch (SSL_get_error(con, i)) { 2979 case SSL_ERROR_NONE: 2980 #ifdef CHARSET_EBCDIC 2981 ascii2ebcdic(buf, buf, i); 2982 #endif 2983 raw_write_stdout(buf, (unsigned int)i); 2984 (void)BIO_flush(bio_s_out); 2985 if (SSL_has_pending(con)) 2986 goto again; 2987 break; 2988 case SSL_ERROR_WANT_ASYNC: 2989 BIO_printf(bio_s_out, "Read BLOCK (Async)\n"); 2990 (void)BIO_flush(bio_s_out); 2991 wait_for_async(con); 2992 break; 2993 case SSL_ERROR_WANT_WRITE: 2994 case SSL_ERROR_WANT_READ: 2995 BIO_printf(bio_s_out, "Read BLOCK\n"); 2996 (void)BIO_flush(bio_s_out); 2997 break; 2998 case SSL_ERROR_WANT_ASYNC_JOB: 2999 /* 3000 * This shouldn't ever happen in s_server. Treat as an error 3001 */ 3002 case SSL_ERROR_SYSCALL: 3003 case SSL_ERROR_SSL: 3004 BIO_printf(bio_s_out, "ERROR\n"); 3005 (void)BIO_flush(bio_s_out); 3006 ERR_print_errors(bio_err); 3007 ret = 1; 3008 goto err; 3009 case SSL_ERROR_ZERO_RETURN: 3010 BIO_printf(bio_s_out, "DONE\n"); 3011 (void)BIO_flush(bio_s_out); 3012 ret = 1; 3013 goto err; 3014 } 3015 } 3016 } 3017 } 3018 err: 3019 if (con != NULL) { 3020 BIO_printf(bio_s_out, "shutting down SSL\n"); 3021 do_ssl_shutdown(con); 3022 SSL_free(con); 3023 } 3024 BIO_printf(bio_s_out, "CONNECTION CLOSED\n"); 3025 OPENSSL_clear_free(buf, bufsize); 3026 return ret; 3027 } 3028 3029 static void close_accept_socket(void) 3030 { 3031 BIO_printf(bio_err, "shutdown accept socket\n"); 3032 if (accept_socket >= 0) { 3033 BIO_closesocket(accept_socket); 3034 } 3035 } 3036 3037 static int is_retryable(SSL *con, int i) 3038 { 3039 int err = SSL_get_error(con, i); 3040 3041 /* If it's not a fatal error, it must be retryable */ 3042 return (err != SSL_ERROR_SSL) 3043 && (err != SSL_ERROR_SYSCALL) 3044 && (err != SSL_ERROR_ZERO_RETURN); 3045 } 3046 3047 static int init_ssl_connection(SSL *con) 3048 { 3049 int i; 3050 long verify_err; 3051 int retry = 0; 3052 3053 if (dtlslisten || stateless) { 3054 BIO_ADDR *client = NULL; 3055 3056 if (dtlslisten) { 3057 if ((client = BIO_ADDR_new()) == NULL) { 3058 BIO_printf(bio_err, "ERROR - memory\n"); 3059 return 0; 3060 } 3061 i = DTLSv1_listen(con, client); 3062 } else { 3063 i = SSL_stateless(con); 3064 } 3065 if (i > 0) { 3066 BIO *wbio; 3067 int fd = -1; 3068 3069 if (dtlslisten) { 3070 wbio = SSL_get_wbio(con); 3071 if (wbio) { 3072 BIO_get_fd(wbio, &fd); 3073 } 3074 3075 if (!wbio || BIO_connect(fd, client, 0) == 0) { 3076 BIO_printf(bio_err, "ERROR - unable to connect\n"); 3077 BIO_ADDR_free(client); 3078 return 0; 3079 } 3080 3081 (void)BIO_ctrl_set_connected(wbio, client); 3082 BIO_ADDR_free(client); 3083 dtlslisten = 0; 3084 } else { 3085 stateless = 0; 3086 } 3087 i = SSL_accept(con); 3088 } else { 3089 BIO_ADDR_free(client); 3090 } 3091 } else { 3092 do { 3093 i = SSL_accept(con); 3094 3095 if (i <= 0) 3096 retry = is_retryable(con, i); 3097 #ifdef CERT_CB_TEST_RETRY 3098 { 3099 while (i <= 0 3100 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP 3101 && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) { 3102 BIO_printf(bio_err, 3103 "LOOKUP from certificate callback during accept\n"); 3104 i = SSL_accept(con); 3105 if (i <= 0) 3106 retry = is_retryable(con, i); 3107 } 3108 } 3109 #endif 3110 3111 #ifndef OPENSSL_NO_SRP 3112 while (i <= 0 3113 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { 3114 BIO_printf(bio_s_out, "LOOKUP during accept %s\n", 3115 srp_callback_parm.login); 3116 3117 lookup_srp_user(&srp_callback_parm, bio_s_out); 3118 3119 i = SSL_accept(con); 3120 if (i <= 0) 3121 retry = is_retryable(con, i); 3122 } 3123 #endif 3124 } while (i < 0 && SSL_waiting_for_async(con)); 3125 } 3126 3127 if (i <= 0) { 3128 if (((dtlslisten || stateless) && i == 0) 3129 || (!dtlslisten && !stateless && retry)) { 3130 BIO_printf(bio_s_out, "DELAY\n"); 3131 return 1; 3132 } 3133 3134 BIO_printf(bio_err, "ERROR\n"); 3135 3136 verify_err = SSL_get_verify_result(con); 3137 if (verify_err != X509_V_OK) { 3138 BIO_printf(bio_err, "verify error:%s\n", 3139 X509_verify_cert_error_string(verify_err)); 3140 } 3141 /* Always print any error messages */ 3142 ERR_print_errors(bio_err); 3143 return 0; 3144 } 3145 3146 print_connection_info(con); 3147 return 1; 3148 } 3149 3150 static void print_connection_info(SSL *con) 3151 { 3152 const char *str; 3153 X509 *peer; 3154 char buf[BUFSIZ]; 3155 #if !defined(OPENSSL_NO_NEXTPROTONEG) 3156 const unsigned char *next_proto_neg; 3157 unsigned next_proto_neg_len; 3158 #endif 3159 unsigned char *exportedkeymat; 3160 int i; 3161 3162 if (s_brief) 3163 print_ssl_summary(con); 3164 3165 PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con)); 3166 3167 peer = SSL_get0_peer_certificate(con); 3168 if (peer != NULL) { 3169 BIO_printf(bio_s_out, "Client certificate\n"); 3170 PEM_write_bio_X509(bio_s_out, peer); 3171 dump_cert_text(bio_s_out, peer); 3172 peer = NULL; 3173 } 3174 /* Only display RPK information if configured */ 3175 if (SSL_get_negotiated_server_cert_type(con) == TLSEXT_cert_type_rpk) 3176 BIO_printf(bio_s_out, "Server-to-client raw public key negotiated\n"); 3177 if (SSL_get_negotiated_client_cert_type(con) == TLSEXT_cert_type_rpk) 3178 BIO_printf(bio_s_out, "Client-to-server raw public key negotiated\n"); 3179 if (enable_client_rpk) { 3180 EVP_PKEY *client_rpk = SSL_get0_peer_rpk(con); 3181 3182 if (client_rpk != NULL) { 3183 BIO_printf(bio_s_out, "Client raw public key\n"); 3184 EVP_PKEY_print_public(bio_s_out, client_rpk, 2, NULL); 3185 } 3186 } 3187 3188 if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL) 3189 BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf); 3190 str = SSL_CIPHER_get_name(SSL_get_current_cipher(con)); 3191 ssl_print_sigalgs(bio_s_out, con); 3192 #ifndef OPENSSL_NO_EC 3193 ssl_print_point_formats(bio_s_out, con); 3194 ssl_print_groups(bio_s_out, con, 0); 3195 #endif 3196 print_ca_names(bio_s_out, con); 3197 BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)"); 3198 3199 #if !defined(OPENSSL_NO_NEXTPROTONEG) 3200 SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len); 3201 if (next_proto_neg) { 3202 BIO_printf(bio_s_out, "NEXTPROTO is "); 3203 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len); 3204 BIO_printf(bio_s_out, "\n"); 3205 } 3206 #endif 3207 #ifndef OPENSSL_NO_SRTP 3208 { 3209 SRTP_PROTECTION_PROFILE *srtp_profile 3210 = SSL_get_selected_srtp_profile(con); 3211 3212 if (srtp_profile) 3213 BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n", 3214 srtp_profile->name); 3215 } 3216 #endif 3217 if (SSL_session_reused(con)) 3218 BIO_printf(bio_s_out, "Reused session-id\n"); 3219 3220 ssl_print_secure_renegotiation_notes(bio_s_out, con); 3221 3222 if ((SSL_get_options(con) & SSL_OP_NO_RENEGOTIATION)) 3223 BIO_printf(bio_s_out, "Renegotiation is DISABLED\n"); 3224 3225 if (keymatexportlabel != NULL) { 3226 BIO_printf(bio_s_out, "Keying material exporter:\n"); 3227 BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel); 3228 BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen); 3229 exportedkeymat = app_malloc(keymatexportlen, "export key"); 3230 if (SSL_export_keying_material(con, exportedkeymat, 3231 keymatexportlen, 3232 keymatexportlabel, 3233 strlen(keymatexportlabel), 3234 NULL, 0, 0) 3235 <= 0) { 3236 BIO_printf(bio_s_out, " Error\n"); 3237 } else { 3238 BIO_printf(bio_s_out, " Keying material: "); 3239 for (i = 0; i < keymatexportlen; i++) 3240 BIO_printf(bio_s_out, "%02X", exportedkeymat[i]); 3241 BIO_printf(bio_s_out, "\n"); 3242 } 3243 OPENSSL_free(exportedkeymat); 3244 } 3245 #ifndef OPENSSL_NO_KTLS 3246 if (BIO_get_ktls_send(SSL_get_wbio(con))) 3247 BIO_printf(bio_err, "Using Kernel TLS for sending\n"); 3248 if (BIO_get_ktls_recv(SSL_get_rbio(con))) 3249 BIO_printf(bio_err, "Using Kernel TLS for receiving\n"); 3250 #endif 3251 3252 (void)BIO_flush(bio_s_out); 3253 } 3254 3255 static int www_body(int s, int stype, int prot, unsigned char *context) 3256 { 3257 char *buf = NULL, *p; 3258 int ret = 1; 3259 int i, j, k, dot; 3260 SSL *con; 3261 const SSL_CIPHER *c; 3262 BIO *io, *ssl_bio, *sbio, *edio; 3263 #ifdef RENEG 3264 int total_bytes = 0; 3265 #endif 3266 int width; 3267 #ifndef OPENSSL_NO_KTLS 3268 int use_sendfile_for_req = use_sendfile; 3269 #endif 3270 fd_set readfds; 3271 const char *opmode; 3272 #ifdef CHARSET_EBCDIC 3273 BIO *filter; 3274 #endif 3275 3276 /* Set width for a select call if needed */ 3277 width = s + 1; 3278 3279 /* as we use BIO_gets(), and it always null terminates data, we need 3280 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */ 3281 p = buf = app_malloc(bufsize + 1, "server www buffer"); 3282 io = BIO_new(BIO_f_buffer()); 3283 ssl_bio = BIO_new(BIO_f_ssl()); 3284 edio = BIO_new(BIO_s_mem()); 3285 if ((io == NULL) || (ssl_bio == NULL) || (edio == NULL)) 3286 goto err; 3287 3288 if (s_nbio) { 3289 if (!BIO_socket_nbio(s, 1)) 3290 ERR_print_errors(bio_err); 3291 else if (!s_quiet) 3292 BIO_printf(bio_err, "Turned on non blocking io\n"); 3293 } 3294 3295 /* lets make the output buffer a reasonable size */ 3296 if (BIO_set_write_buffer_size(io, bufsize) <= 0) 3297 goto err; 3298 3299 if ((con = SSL_new(ctx)) == NULL) 3300 goto err; 3301 3302 if (s_tlsextdebug) { 3303 SSL_set_tlsext_debug_callback(con, tlsext_cb); 3304 SSL_set_tlsext_debug_arg(con, bio_s_out); 3305 } 3306 3307 if (context != NULL 3308 && !SSL_set_session_id_context(con, context, 3309 strlen((char *)context))) { 3310 SSL_free(con); 3311 goto err; 3312 } 3313 3314 sbio = BIO_new_socket(s, BIO_NOCLOSE); 3315 if (sbio == NULL) { 3316 SSL_free(con); 3317 goto err; 3318 } 3319 3320 if (s_nbio_test) { 3321 BIO *test; 3322 3323 test = BIO_new(BIO_f_nbio_test()); 3324 if (test == NULL) { 3325 SSL_free(con); 3326 BIO_free(sbio); 3327 goto err; 3328 } 3329 3330 sbio = BIO_push(test, sbio); 3331 } 3332 SSL_set_bio(con, sbio, sbio); 3333 SSL_set_accept_state(con); 3334 3335 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */ 3336 BIO_set_ssl(ssl_bio, con, BIO_CLOSE); 3337 BIO_push(io, ssl_bio); 3338 ssl_bio = NULL; 3339 #ifdef CHARSET_EBCDIC 3340 filter = BIO_new(BIO_f_ebcdic_filter()); 3341 if (filter == NULL) 3342 goto err; 3343 3344 io = BIO_push(filter, io); 3345 3346 filter = BIO_new(BIO_f_ebcdic_filter()); 3347 if (filter == NULL) 3348 goto err; 3349 3350 edio = BIO_push(filter, edio); 3351 #endif 3352 3353 if (s_debug) { 3354 BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback); 3355 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); 3356 } 3357 if (s_msg) { 3358 #ifndef OPENSSL_NO_SSL_TRACE 3359 if (s_msg == 2) 3360 SSL_set_msg_callback(con, SSL_trace); 3361 else 3362 #endif 3363 SSL_set_msg_callback(con, msg_cb); 3364 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); 3365 } 3366 3367 if (early_data) { 3368 int edret = SSL_READ_EARLY_DATA_ERROR; 3369 size_t readbytes; 3370 3371 while (edret != SSL_READ_EARLY_DATA_FINISH) { 3372 for (;;) { 3373 edret = SSL_read_early_data(con, buf, bufsize, &readbytes); 3374 if (edret != SSL_READ_EARLY_DATA_ERROR) 3375 break; 3376 3377 switch (SSL_get_error(con, 0)) { 3378 case SSL_ERROR_WANT_WRITE: 3379 case SSL_ERROR_WANT_ASYNC: 3380 case SSL_ERROR_WANT_READ: 3381 /* Just keep trying - busy waiting */ 3382 continue; 3383 default: 3384 BIO_printf(bio_err, "Error reading early data\n"); 3385 ERR_print_errors(bio_err); 3386 goto err; 3387 } 3388 } 3389 if (readbytes > 0) 3390 BIO_write(edio, buf, (int)readbytes); 3391 } 3392 } 3393 3394 for (;;) { 3395 i = BIO_gets(!BIO_eof(edio) ? edio : io, buf, bufsize + 1); 3396 if (i < 0) { /* error */ 3397 if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) { 3398 if (!s_quiet) 3399 ERR_print_errors(bio_err); 3400 goto err; 3401 } else { 3402 BIO_printf(bio_s_out, "read R BLOCK\n"); 3403 #ifndef OPENSSL_NO_SRP 3404 if (BIO_should_io_special(io) 3405 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { 3406 BIO_printf(bio_s_out, "LOOKUP renego during read\n"); 3407 3408 lookup_srp_user(&srp_callback_parm, bio_s_out); 3409 3410 continue; 3411 } 3412 #endif 3413 OSSL_sleep(1000); 3414 continue; 3415 } 3416 } else if (i == 0) { /* end of input */ 3417 ret = 1; 3418 goto end; 3419 } 3420 3421 /* else we have data */ 3422 if ((www == 1 && HAS_PREFIX(buf, "GET ")) 3423 || (www == 2 && HAS_PREFIX(buf, "GET /stats "))) { 3424 X509 *peer = NULL; 3425 STACK_OF(SSL_CIPHER) *sk; 3426 static const char *space = " "; 3427 3428 if (www == 1 && HAS_PREFIX(buf, "GET /reneg")) { 3429 if (HAS_PREFIX(buf, "GET /renegcert")) 3430 SSL_set_verify(con, 3431 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, 3432 NULL); 3433 i = SSL_renegotiate(con); 3434 BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i); 3435 /* Send the HelloRequest */ 3436 i = SSL_do_handshake(con); 3437 if (i <= 0) { 3438 BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n", 3439 SSL_get_error(con, i)); 3440 ERR_print_errors(bio_err); 3441 goto err; 3442 } 3443 /* Wait for a ClientHello to come back */ 3444 FD_ZERO(&readfds); 3445 openssl_fdset(s, &readfds); 3446 i = select(width, (void *)&readfds, NULL, NULL, NULL); 3447 if (i <= 0 || !FD_ISSET(s, &readfds)) { 3448 BIO_printf(bio_s_out, 3449 "Error waiting for client response\n"); 3450 ERR_print_errors(bio_err); 3451 goto err; 3452 } 3453 /* 3454 * We're not actually expecting any data here and we ignore 3455 * any that is sent. This is just to force the handshake that 3456 * we're expecting to come from the client. If they haven't 3457 * sent one there's not much we can do. 3458 */ 3459 BIO_gets(io, buf, bufsize + 1); 3460 } 3461 3462 BIO_puts(io, 3463 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 3464 BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n"); 3465 BIO_puts(io, "<pre>\n"); 3466 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */ 3467 BIO_puts(io, "\n"); 3468 for (i = 0; i < local_argc; i++) { 3469 const char *myp; 3470 3471 for (myp = local_argv[i]; *myp; myp++) 3472 switch (*myp) { 3473 case '<': 3474 BIO_puts(io, "<"); 3475 break; 3476 case '>': 3477 BIO_puts(io, ">"); 3478 break; 3479 case '&': 3480 BIO_puts(io, "&"); 3481 break; 3482 default: 3483 BIO_write(io, myp, 1); 3484 break; 3485 } 3486 BIO_write(io, " ", 1); 3487 } 3488 BIO_puts(io, "\n"); 3489 3490 ssl_print_secure_renegotiation_notes(io, con); 3491 3492 /* 3493 * The following is evil and should not really be done 3494 */ 3495 BIO_printf(io, "Ciphers supported in s_server binary\n"); 3496 sk = SSL_get_ciphers(con); 3497 j = sk_SSL_CIPHER_num(sk); 3498 for (i = 0; i < j; i++) { 3499 c = sk_SSL_CIPHER_value(sk, i); 3500 BIO_printf(io, "%-11s:%-25s ", 3501 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); 3502 if ((((i + 1) % 2) == 0) && (i + 1 != j)) 3503 BIO_puts(io, "\n"); 3504 } 3505 BIO_puts(io, "\n"); 3506 p = SSL_get_shared_ciphers(con, buf, bufsize); 3507 if (p != NULL) { 3508 BIO_printf(io, 3509 "---\nCiphers common between both SSL end points:\n"); 3510 j = i = 0; 3511 while (*p) { 3512 if (*p == ':') { 3513 BIO_write(io, space, 26 - j); 3514 i++; 3515 j = 0; 3516 BIO_write(io, ((i % 3) ? " " : "\n"), 1); 3517 } else { 3518 BIO_write(io, p, 1); 3519 j++; 3520 } 3521 p++; 3522 } 3523 BIO_puts(io, "\n"); 3524 } 3525 ssl_print_sigalgs(io, con); 3526 #ifndef OPENSSL_NO_EC 3527 ssl_print_groups(io, con, 0); 3528 #endif 3529 print_ca_names(io, con); 3530 BIO_printf(io, (SSL_session_reused(con) ? "---\nReused, " : "---\nNew, ")); 3531 c = SSL_get_current_cipher(con); 3532 BIO_printf(io, "%s, Cipher is %s\n", 3533 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); 3534 SSL_SESSION_print(io, SSL_get_session(con)); 3535 BIO_printf(io, "---\n"); 3536 print_stats(io, SSL_get_SSL_CTX(con)); 3537 BIO_printf(io, "---\n"); 3538 peer = SSL_get0_peer_certificate(con); 3539 if (peer != NULL) { 3540 BIO_printf(io, "Client certificate\n"); 3541 X509_print(io, peer); 3542 PEM_write_bio_X509(io, peer); 3543 peer = NULL; 3544 } else { 3545 BIO_puts(io, "no client certificate available\n"); 3546 } 3547 BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n"); 3548 break; 3549 } else if ((www == 2 || www == 3) && CHECK_AND_SKIP_PREFIX(p, "GET /")) { 3550 BIO *file; 3551 char *e; 3552 static const char *text = "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"; 3553 3554 dot = 1; 3555 for (e = p; *e != '\0'; e++) { 3556 if (e[0] == ' ') 3557 break; 3558 3559 if (e[0] == ':') { 3560 /* Windows drive. We treat this the same way as ".." */ 3561 dot = -1; 3562 break; 3563 } 3564 3565 switch (dot) { 3566 case 1: 3567 dot = (e[0] == '.') ? 2 : 0; 3568 break; 3569 case 2: 3570 dot = (e[0] == '.') ? 3 : 0; 3571 break; 3572 case 3: 3573 dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0; 3574 break; 3575 } 3576 if (dot == 0) 3577 dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0; 3578 } 3579 dot = (dot == 3) || (dot == -1); /* filename contains ".." 3580 * component */ 3581 3582 if (*e == '\0') { 3583 BIO_puts(io, text); 3584 BIO_printf(io, "'%s' is an invalid file name\r\n", p); 3585 break; 3586 } 3587 *e = '\0'; 3588 3589 if (dot) { 3590 BIO_puts(io, text); 3591 BIO_printf(io, "'%s' contains '..' or ':'\r\n", p); 3592 break; 3593 } 3594 3595 if (*p == '/' || *p == '\\') { 3596 BIO_puts(io, text); 3597 BIO_printf(io, "'%s' is an invalid path\r\n", p); 3598 break; 3599 } 3600 3601 /* if a directory, do the index thang */ 3602 if (app_isdir(p) > 0) { 3603 BIO_puts(io, text); 3604 BIO_printf(io, "'%s' is a directory\r\n", p); 3605 break; 3606 } 3607 3608 opmode = (http_server_binmode == 1) ? "rb" : "r"; 3609 if ((file = BIO_new_file(p, opmode)) == NULL) { 3610 BIO_puts(io, text); 3611 BIO_printf(io, "Error opening '%s' mode='%s'\r\n", p, opmode); 3612 ERR_print_errors(io); 3613 break; 3614 } 3615 3616 if (!s_quiet) 3617 BIO_printf(bio_err, "FILE:%s\n", p); 3618 3619 if (www == 2) { 3620 i = strlen(p); 3621 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) || ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) || ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0))) 3622 BIO_puts(io, 3623 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 3624 else 3625 BIO_puts(io, 3626 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"); 3627 } 3628 /* send the file */ 3629 #ifndef OPENSSL_NO_KTLS 3630 if (use_sendfile_for_req && !BIO_get_ktls_send(SSL_get_wbio(con))) { 3631 BIO_printf(bio_err, "Warning: sendfile requested but KTLS is not available\n"); 3632 use_sendfile_for_req = 0; 3633 } 3634 if (use_sendfile_for_req) { 3635 FILE *fp = NULL; 3636 int fd; 3637 struct stat st; 3638 off_t offset = 0; 3639 size_t filesize; 3640 3641 BIO_get_fp(file, &fp); 3642 fd = fileno(fp); 3643 if (fstat(fd, &st) < 0) { 3644 BIO_printf(io, "Error fstat '%s'\r\n", p); 3645 ERR_print_errors(io); 3646 goto write_error; 3647 } 3648 3649 filesize = st.st_size; 3650 if (((int)BIO_flush(io)) < 0) 3651 goto write_error; 3652 3653 for (;;) { 3654 i = SSL_sendfile(con, fd, offset, filesize, 0); 3655 if (i < 0) { 3656 BIO_printf(io, "Error SSL_sendfile '%s'\r\n", p); 3657 ERR_print_errors(io); 3658 break; 3659 } else { 3660 offset += i; 3661 filesize -= i; 3662 } 3663 3664 if (filesize <= 0) { 3665 if (!s_quiet) 3666 BIO_printf(bio_err, "KTLS SENDFILE '%s' OK\n", p); 3667 3668 break; 3669 } 3670 } 3671 } else 3672 #endif 3673 { 3674 for (;;) { 3675 i = BIO_read(file, buf, bufsize); 3676 if (i <= 0) 3677 break; 3678 3679 #ifdef RENEG 3680 total_bytes += i; 3681 BIO_printf(bio_err, "%d\n", i); 3682 if (total_bytes > 3 * 1024) { 3683 total_bytes = 0; 3684 BIO_printf(bio_err, "RENEGOTIATE\n"); 3685 SSL_renegotiate(con); 3686 } 3687 #endif 3688 3689 for (j = 0; j < i;) { 3690 #ifdef RENEG 3691 static count = 0; 3692 if (++count == 13) 3693 SSL_renegotiate(con); 3694 #endif 3695 k = BIO_write(io, &(buf[j]), i - j); 3696 if (k <= 0) { 3697 if (!BIO_should_retry(io) 3698 && !SSL_waiting_for_async(con)) { 3699 goto write_error; 3700 } else { 3701 BIO_printf(bio_s_out, "rwrite W BLOCK\n"); 3702 } 3703 } else { 3704 j += k; 3705 } 3706 } 3707 } 3708 } 3709 write_error: 3710 BIO_free(file); 3711 break; 3712 } 3713 } 3714 3715 for (;;) { 3716 i = (int)BIO_flush(io); 3717 if (i <= 0) { 3718 if (!BIO_should_retry(io)) 3719 break; 3720 } else 3721 break; 3722 } 3723 end: 3724 /* make sure we reuse sessions */ 3725 do_ssl_shutdown(con); 3726 3727 err: 3728 OPENSSL_free(buf); 3729 BIO_free(ssl_bio); 3730 BIO_free_all(io); 3731 BIO_free_all(edio); 3732 return ret; 3733 } 3734 3735 static int rev_body(int s, int stype, int prot, unsigned char *context) 3736 { 3737 char *buf = NULL; 3738 int i; 3739 int ret = 1; 3740 SSL *con; 3741 BIO *io, *ssl_bio, *sbio; 3742 #ifdef CHARSET_EBCDIC 3743 BIO *filter; 3744 #endif 3745 3746 /* as we use BIO_gets(), and it always null terminates data, we need 3747 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */ 3748 buf = app_malloc(bufsize + 1, "server rev buffer"); 3749 io = BIO_new(BIO_f_buffer()); 3750 ssl_bio = BIO_new(BIO_f_ssl()); 3751 if ((io == NULL) || (ssl_bio == NULL)) 3752 goto err; 3753 3754 /* lets make the output buffer a reasonable size */ 3755 if (BIO_set_write_buffer_size(io, bufsize) <= 0) 3756 goto err; 3757 3758 if ((con = SSL_new(ctx)) == NULL) 3759 goto err; 3760 3761 if (s_tlsextdebug) { 3762 SSL_set_tlsext_debug_callback(con, tlsext_cb); 3763 SSL_set_tlsext_debug_arg(con, bio_s_out); 3764 } 3765 if (context != NULL 3766 && !SSL_set_session_id_context(con, context, 3767 strlen((char *)context))) { 3768 SSL_free(con); 3769 ERR_print_errors(bio_err); 3770 goto err; 3771 } 3772 3773 sbio = BIO_new_socket(s, BIO_NOCLOSE); 3774 if (sbio == NULL) { 3775 SSL_free(con); 3776 ERR_print_errors(bio_err); 3777 goto err; 3778 } 3779 3780 SSL_set_bio(con, sbio, sbio); 3781 SSL_set_accept_state(con); 3782 3783 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */ 3784 BIO_set_ssl(ssl_bio, con, BIO_CLOSE); 3785 BIO_push(io, ssl_bio); 3786 ssl_bio = NULL; 3787 #ifdef CHARSET_EBCDIC 3788 filter = BIO_new(BIO_f_ebcdic_filter()); 3789 if (filter == NULL) 3790 goto err; 3791 3792 io = BIO_push(filter, io); 3793 #endif 3794 3795 if (s_debug) { 3796 BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback); 3797 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); 3798 } 3799 if (s_msg) { 3800 #ifndef OPENSSL_NO_SSL_TRACE 3801 if (s_msg == 2) 3802 SSL_set_msg_callback(con, SSL_trace); 3803 else 3804 #endif 3805 SSL_set_msg_callback(con, msg_cb); 3806 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); 3807 } 3808 3809 for (;;) { 3810 i = BIO_do_handshake(io); 3811 if (i > 0) 3812 break; 3813 if (!BIO_should_retry(io)) { 3814 BIO_puts(bio_err, "CONNECTION FAILURE\n"); 3815 ERR_print_errors(bio_err); 3816 goto end; 3817 } 3818 #ifndef OPENSSL_NO_SRP 3819 if (BIO_should_io_special(io) 3820 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { 3821 BIO_printf(bio_s_out, "LOOKUP renego during accept\n"); 3822 3823 lookup_srp_user(&srp_callback_parm, bio_s_out); 3824 3825 continue; 3826 } 3827 #endif 3828 } 3829 BIO_printf(bio_err, "CONNECTION ESTABLISHED\n"); 3830 print_ssl_summary(con); 3831 3832 for (;;) { 3833 i = BIO_gets(io, buf, bufsize + 1); 3834 if (i < 0) { /* error */ 3835 if (!BIO_should_retry(io)) { 3836 if (!s_quiet) 3837 ERR_print_errors(bio_err); 3838 goto err; 3839 } else { 3840 BIO_printf(bio_s_out, "read R BLOCK\n"); 3841 #ifndef OPENSSL_NO_SRP 3842 if (BIO_should_io_special(io) 3843 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { 3844 BIO_printf(bio_s_out, "LOOKUP renego during read\n"); 3845 3846 lookup_srp_user(&srp_callback_parm, bio_s_out); 3847 3848 continue; 3849 } 3850 #endif 3851 OSSL_sleep(1000); 3852 continue; 3853 } 3854 } else if (i == 0) { /* end of input */ 3855 ret = 1; 3856 BIO_printf(bio_err, "CONNECTION CLOSED\n"); 3857 goto end; 3858 } else { 3859 char *p = buf + i - 1; 3860 while (i && (*p == '\n' || *p == '\r')) { 3861 p--; 3862 i--; 3863 } 3864 if (!s_ign_eof && i == 5 && HAS_PREFIX(buf, "CLOSE")) { 3865 ret = 1; 3866 BIO_printf(bio_err, "CONNECTION CLOSED\n"); 3867 goto end; 3868 } 3869 BUF_reverse((unsigned char *)buf, NULL, i); 3870 buf[i] = '\n'; 3871 BIO_write(io, buf, i + 1); 3872 for (;;) { 3873 i = BIO_flush(io); 3874 if (i > 0) 3875 break; 3876 if (!BIO_should_retry(io)) 3877 goto end; 3878 } 3879 } 3880 } 3881 end: 3882 /* make sure we reuse sessions */ 3883 do_ssl_shutdown(con); 3884 3885 err: 3886 3887 OPENSSL_free(buf); 3888 BIO_free(ssl_bio); 3889 BIO_free_all(io); 3890 return ret; 3891 } 3892 3893 #define MAX_SESSION_ID_ATTEMPTS 10 3894 static int generate_session_id(SSL *ssl, unsigned char *id, 3895 unsigned int *id_len) 3896 { 3897 unsigned int count = 0; 3898 unsigned int session_id_prefix_len = strlen(session_id_prefix); 3899 3900 do { 3901 if (RAND_bytes(id, *id_len) <= 0) 3902 return 0; 3903 /* 3904 * Prefix the session_id with the required prefix. NB: If our prefix 3905 * is too long, clip it - but there will be worse effects anyway, eg. 3906 * the server could only possibly create 1 session ID (ie. the 3907 * prefix!) so all future session negotiations will fail due to 3908 * conflicts. 3909 */ 3910 memcpy(id, session_id_prefix, 3911 (session_id_prefix_len < *id_len) ? session_id_prefix_len : *id_len); 3912 } while (SSL_has_matching_session_id(ssl, id, *id_len) && (++count < MAX_SESSION_ID_ATTEMPTS)); 3913 if (count >= MAX_SESSION_ID_ATTEMPTS) 3914 return 0; 3915 return 1; 3916 } 3917 3918 /* 3919 * By default s_server uses an in-memory cache which caches SSL_SESSION 3920 * structures without any serialization. This hides some bugs which only 3921 * become apparent in deployed servers. By implementing a basic external 3922 * session cache some issues can be debugged using s_server. 3923 */ 3924 3925 typedef struct simple_ssl_session_st { 3926 unsigned char *id; 3927 unsigned int idlen; 3928 unsigned char *der; 3929 int derlen; 3930 struct simple_ssl_session_st *next; 3931 } simple_ssl_session; 3932 3933 static simple_ssl_session *first = NULL; 3934 3935 static int add_session(SSL *ssl, SSL_SESSION *session) 3936 { 3937 simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session"); 3938 unsigned char *p; 3939 3940 SSL_SESSION_get_id(session, &sess->idlen); 3941 sess->derlen = i2d_SSL_SESSION(session, NULL); 3942 if (sess->derlen < 0) { 3943 BIO_printf(bio_err, "Error encoding session\n"); 3944 OPENSSL_free(sess); 3945 return 0; 3946 } 3947 3948 sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen); 3949 sess->der = app_malloc(sess->derlen, "get session buffer"); 3950 if (!sess->id) { 3951 BIO_printf(bio_err, "Out of memory adding to external cache\n"); 3952 OPENSSL_free(sess->id); 3953 OPENSSL_free(sess->der); 3954 OPENSSL_free(sess); 3955 return 0; 3956 } 3957 p = sess->der; 3958 3959 /* Assume it still works. */ 3960 if (i2d_SSL_SESSION(session, &p) != sess->derlen) { 3961 BIO_printf(bio_err, "Unexpected session encoding length\n"); 3962 OPENSSL_free(sess->id); 3963 OPENSSL_free(sess->der); 3964 OPENSSL_free(sess); 3965 return 0; 3966 } 3967 3968 sess->next = first; 3969 first = sess; 3970 BIO_printf(bio_err, "New session added to external cache\n"); 3971 return 0; 3972 } 3973 3974 static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen, 3975 int *do_copy) 3976 { 3977 simple_ssl_session *sess; 3978 *do_copy = 0; 3979 for (sess = first; sess; sess = sess->next) { 3980 if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) { 3981 const unsigned char *p = sess->der; 3982 BIO_printf(bio_err, "Lookup session: cache hit\n"); 3983 return d2i_SSL_SESSION_ex(NULL, &p, sess->derlen, app_get0_libctx(), 3984 app_get0_propq()); 3985 } 3986 } 3987 BIO_printf(bio_err, "Lookup session: cache miss\n"); 3988 return NULL; 3989 } 3990 3991 static void del_session(SSL_CTX *sctx, SSL_SESSION *session) 3992 { 3993 simple_ssl_session *sess, *prev = NULL; 3994 const unsigned char *id; 3995 unsigned int idlen; 3996 id = SSL_SESSION_get_id(session, &idlen); 3997 for (sess = first; sess; sess = sess->next) { 3998 if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) { 3999 if (prev) 4000 prev->next = sess->next; 4001 else 4002 first = sess->next; 4003 OPENSSL_free(sess->id); 4004 OPENSSL_free(sess->der); 4005 OPENSSL_free(sess); 4006 return; 4007 } 4008 prev = sess; 4009 } 4010 } 4011 4012 static void init_session_cache_ctx(SSL_CTX *sctx) 4013 { 4014 SSL_CTX_set_session_cache_mode(sctx, 4015 SSL_SESS_CACHE_NO_INTERNAL | SSL_SESS_CACHE_SERVER); 4016 SSL_CTX_sess_set_new_cb(sctx, add_session); 4017 SSL_CTX_sess_set_get_cb(sctx, get_session); 4018 SSL_CTX_sess_set_remove_cb(sctx, del_session); 4019 } 4020 4021 static void free_sessions(void) 4022 { 4023 simple_ssl_session *sess, *tsess; 4024 for (sess = first; sess;) { 4025 OPENSSL_free(sess->id); 4026 OPENSSL_free(sess->der); 4027 tsess = sess; 4028 sess = sess->next; 4029 OPENSSL_free(tsess); 4030 } 4031 first = NULL; 4032 } 4033 4034 #endif /* OPENSSL_NO_SOCK */ 4035