2 * Copyright 1995-2021 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.
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
17 /* Included before async.h to avoid some warnings */
21 #include <openssl/e_os2.h>
22 #include <openssl/async.h>
23 #include <openssl/ssl.h>
24 #include <openssl/decoder.h>
26 #ifndef OPENSSL_NO_SOCK
29 * With IPv6, it looks like Digital has mixed up the proper order of
30 * recursive header file inclusion, resulting in the compiler complaining
31 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
32 * needed to have fileno() declared correctly... So let's define u_int
34 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
36 typedef unsigned int u_int
;
39 #include <openssl/bn.h>
42 #include <openssl/err.h>
43 #include <openssl/pem.h>
44 #include <openssl/x509.h>
45 #include <openssl/ssl.h>
46 #include <openssl/rand.h>
47 #include <openssl/ocsp.h>
49 # include <openssl/dh.h>
51 #include <openssl/rsa.h>
55 #include <openssl/ebcdic.h>
57 #include "internal/sockets.h"
59 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
);
60 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
);
61 static int www_body(int s
, int stype
, int prot
, unsigned char *context
);
62 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
);
63 static void close_accept_socket(void);
64 static int init_ssl_connection(SSL
*s
);
65 static void print_stats(BIO
*bp
, SSL_CTX
*ctx
);
66 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
67 unsigned int *id_len
);
68 static void init_session_cache_ctx(SSL_CTX
*sctx
);
69 static void free_sessions(void);
70 static void print_connection_info(SSL
*con
);
72 static const int bufsize
= 16 * 1024;
73 static int accept_socket
= -1;
75 #define TEST_CERT "server.pem"
76 #define TEST_CERT2 "server2.pem"
78 static int s_nbio
= 0;
79 static int s_nbio_test
= 0;
80 static int s_crlf
= 0;
81 static int immediate_reneg
= 0;
82 static SSL_CTX
*ctx
= NULL
;
83 static SSL_CTX
*ctx2
= NULL
;
86 static BIO
*bio_s_out
= NULL
;
87 static BIO
*bio_s_msg
= NULL
;
88 static int s_debug
= 0;
89 static int s_tlsextdebug
= 0;
91 static int s_quiet
= 0;
92 static int s_ign_eof
= 0;
93 static int s_brief
= 0;
95 static char *keymatexportlabel
= NULL
;
96 static int keymatexportlen
= 20;
100 static int use_sendfile
= 0;
102 static const char *session_id_prefix
= NULL
;
104 #ifndef OPENSSL_NO_DTLS
105 static int enable_timeouts
= 0;
106 static long socket_mtu
;
110 * We define this but make it always be 0 in no-dtls builds to simplify the
113 static int dtlslisten
= 0;
114 static int stateless
= 0;
116 static int early_data
= 0;
117 static SSL_SESSION
*psksess
= NULL
;
119 static char *psk_identity
= "Client_identity";
120 char *psk_key
= NULL
; /* by default PSK is not used */
122 static char http_server_binmode
= 0; /* for now: 0/1 = default/binary */
124 #ifndef OPENSSL_NO_PSK
125 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
127 unsigned int max_psk_len
)
133 BIO_printf(bio_s_out
, "psk_server_cb\n");
134 if (identity
== NULL
) {
135 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
139 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
140 (int)strlen(identity
), identity
);
142 /* here we could lookup the given identity e.g. from a database */
143 if (strcmp(identity
, psk_identity
) != 0) {
144 BIO_printf(bio_s_out
, "PSK warning: client identity not what we expected"
145 " (got '%s' expected '%s')\n", identity
, psk_identity
);
148 BIO_printf(bio_s_out
, "PSK client identity found\n");
151 /* convert the PSK key to binary */
152 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
154 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
158 if (key_len
> (int)max_psk_len
) {
160 "psk buffer of callback is too small (%d) for key (%ld)\n",
161 max_psk_len
, key_len
);
166 memcpy(psk
, key
, key_len
);
170 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
174 BIO_printf(bio_err
, "Error in PSK server callback\n");
175 (void)BIO_flush(bio_err
);
176 (void)BIO_flush(bio_s_out
);
181 static int psk_find_session_cb(SSL
*ssl
, const unsigned char *identity
,
182 size_t identity_len
, SSL_SESSION
**sess
)
184 SSL_SESSION
*tmpsess
= NULL
;
187 const SSL_CIPHER
*cipher
= NULL
;
189 if (strlen(psk_identity
) != identity_len
190 || memcmp(psk_identity
, identity
, identity_len
) != 0) {
195 if (psksess
!= NULL
) {
196 SSL_SESSION_up_ref(psksess
);
201 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
203 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
208 /* We default to SHA256 */
209 cipher
= SSL_CIPHER_find(ssl
, tls13_aes128gcmsha256_id
);
210 if (cipher
== NULL
) {
211 BIO_printf(bio_err
, "Error finding suitable ciphersuite\n");
216 tmpsess
= SSL_SESSION_new();
218 || !SSL_SESSION_set1_master_key(tmpsess
, key
, key_len
)
219 || !SSL_SESSION_set_cipher(tmpsess
, cipher
)
220 || !SSL_SESSION_set_protocol_version(tmpsess
, SSL_version(ssl
))) {
230 #ifndef OPENSSL_NO_SRP
231 static srpsrvparm srp_callback_parm
;
234 static int local_argc
= 0;
235 static char **local_argv
;
237 #ifdef CHARSET_EBCDIC
238 static int ebcdic_new(BIO
*bi
);
239 static int ebcdic_free(BIO
*a
);
240 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
241 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
242 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
243 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
244 static int ebcdic_puts(BIO
*bp
, const char *str
);
246 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
247 static BIO_METHOD
*methods_ebcdic
= NULL
;
249 /* This struct is "unwarranted chumminess with the compiler." */
255 static const BIO_METHOD
*BIO_f_ebcdic_filter()
257 if (methods_ebcdic
== NULL
) {
258 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
259 "EBCDIC/ASCII filter");
260 if (methods_ebcdic
== NULL
261 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
262 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
263 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
264 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
265 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
266 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
267 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
270 return methods_ebcdic
;
273 static int ebcdic_new(BIO
*bi
)
275 EBCDIC_OUTBUFF
*wbuf
;
277 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
278 wbuf
->alloced
= 1024;
279 wbuf
->buff
[0] = '\0';
281 BIO_set_data(bi
, wbuf
);
286 static int ebcdic_free(BIO
*a
)
288 EBCDIC_OUTBUFF
*wbuf
;
292 wbuf
= BIO_get_data(a
);
294 BIO_set_data(a
, NULL
);
300 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
303 BIO
*next
= BIO_next(b
);
305 if (out
== NULL
|| outl
== 0)
310 ret
= BIO_read(next
, out
, outl
);
312 ascii2ebcdic(out
, out
, ret
);
316 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
318 EBCDIC_OUTBUFF
*wbuf
;
319 BIO
*next
= BIO_next(b
);
323 if ((in
== NULL
) || (inl
<= 0))
328 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
330 if (inl
> (num
= wbuf
->alloced
)) {
331 num
= num
+ num
; /* double the size */
335 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
338 wbuf
->buff
[0] = '\0';
340 BIO_set_data(b
, wbuf
);
343 ebcdic2ascii(wbuf
->buff
, in
, inl
);
345 ret
= BIO_write(next
, wbuf
->buff
, inl
);
350 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
353 BIO
*next
= BIO_next(b
);
362 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
368 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
371 BIO
*next
= BIO_next(bp
);
375 /* return(BIO_gets(bp->next_bio,buf,size));*/
376 for (i
= 0; i
< size
- 1; ++i
) {
377 ret
= ebcdic_read(bp
, &buf
[i
], 1);
380 else if (buf
[i
] == '\n') {
387 return (ret
< 0 && i
== 0) ? ret
: i
;
390 static int ebcdic_puts(BIO
*bp
, const char *str
)
392 if (BIO_next(bp
) == NULL
)
394 return ebcdic_write(bp
, str
, strlen(str
));
398 /* This is a context that we pass to callbacks */
399 typedef struct tlsextctx_st
{
405 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
407 tlsextctx
*p
= (tlsextctx
*) arg
;
408 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
410 if (servername
!= NULL
&& p
->biodebug
!= NULL
) {
411 const char *cp
= servername
;
414 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"");
415 while ((uc
= *cp
++) != 0)
416 BIO_printf(p
->biodebug
,
417 (((uc
) & ~127) == 0) && isprint(uc
) ? "%c" : "\\x%02x", uc
);
418 BIO_printf(p
->biodebug
, "\"\n");
421 if (p
->servername
== NULL
)
422 return SSL_TLSEXT_ERR_NOACK
;
424 if (servername
!= NULL
) {
425 if (strcasecmp(servername
, p
->servername
))
426 return p
->extension_error
;
428 BIO_printf(p
->biodebug
, "Switching server context.\n");
429 SSL_set_SSL_CTX(s
, ctx2
);
432 return SSL_TLSEXT_ERR_OK
;
435 /* Structure passed to cert status callback */
436 typedef struct tlsextstatusctx_st
{
438 /* File to load OCSP Response from (or NULL if no file) */
440 /* Default responder to use */
441 char *host
, *path
, *port
;
442 char *proxy
, *no_proxy
;
447 static tlsextstatusctx tlscstatp
= { -1 };
449 #ifndef OPENSSL_NO_OCSP
452 * Helper function to get an OCSP_RESPONSE from a responder. This is a
453 * simplified version. It examines certificates each time and makes one OCSP
454 * responder query for each request. A full version would store details such as
455 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
456 * them until they were considered "expired".
458 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
459 OCSP_RESPONSE
**resp
)
461 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
462 char *proxy
= NULL
, *no_proxy
= NULL
;
464 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
466 X509_STORE_CTX
*inctx
= NULL
;
468 OCSP_REQUEST
*req
= NULL
;
469 OCSP_CERTID
*id
= NULL
;
470 STACK_OF(X509_EXTENSION
) *exts
;
471 int ret
= SSL_TLSEXT_ERR_NOACK
;
474 /* Build up OCSP query from server certificate */
475 x
= SSL_get_certificate(s
);
476 aia
= X509_get1_ocsp(x
);
478 if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia
, 0), &use_ssl
,
479 NULL
, &host
, &port
, NULL
, &path
, NULL
, NULL
)) {
480 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
484 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
485 sk_OPENSSL_STRING_value(aia
, 0));
487 if (srctx
->host
== NULL
) {
489 "cert_status: no AIA and no default responder URL\n");
495 use_ssl
= srctx
->use_ssl
;
497 proxy
= srctx
->proxy
;
498 no_proxy
= srctx
->no_proxy
;
500 inctx
= X509_STORE_CTX_new();
503 if (!X509_STORE_CTX_init(inctx
,
504 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s
)),
507 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
,
508 X509_get_issuer_name(x
));
510 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
513 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
514 X509_OBJECT_free(obj
);
517 req
= OCSP_REQUEST_new();
520 if (!OCSP_request_add0_id(req
, id
))
523 /* Add any extensions to the request */
524 SSL_get_tlsext_status_exts(s
, &exts
);
525 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
526 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
527 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
530 *resp
= process_responder(req
, host
, port
, path
, proxy
, no_proxy
,
531 use_ssl
, NULL
/* headers */, srctx
->timeout
);
533 BIO_puts(bio_err
, "cert_status: error querying responder\n");
537 ret
= SSL_TLSEXT_ERR_OK
;
541 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
544 * If we parsed aia we need to free; otherwise they were copied and we
551 X509_email_free(aia
);
553 OCSP_CERTID_free(id
);
554 OCSP_REQUEST_free(req
);
555 X509_STORE_CTX_free(inctx
);
560 * Certificate Status callback. This is called when a client includes a
561 * certificate status request extension. The response is either obtained from a
562 * file, or from an OCSP responder.
564 static int cert_status_cb(SSL
*s
, void *arg
)
566 tlsextstatusctx
*srctx
= arg
;
567 OCSP_RESPONSE
*resp
= NULL
;
568 unsigned char *rspder
= NULL
;
570 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
573 BIO_puts(bio_err
, "cert_status: callback called\n");
575 if (srctx
->respin
!= NULL
) {
576 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
577 if (derbio
== NULL
) {
578 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
581 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
584 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
588 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
589 if (ret
!= SSL_TLSEXT_ERR_OK
)
593 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
597 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
598 if (srctx
->verbose
) {
599 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
600 OCSP_RESPONSE_print(bio_err
, resp
, 2);
603 ret
= SSL_TLSEXT_ERR_OK
;
606 if (ret
!= SSL_TLSEXT_ERR_OK
)
607 ERR_print_errors(bio_err
);
609 OCSP_RESPONSE_free(resp
);
615 #ifndef OPENSSL_NO_NEXTPROTONEG
616 /* This is the context that we pass to next_proto_cb */
617 typedef struct tlsextnextprotoctx_st
{
620 } tlsextnextprotoctx
;
622 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
623 unsigned int *len
, void *arg
)
625 tlsextnextprotoctx
*next_proto
= arg
;
627 *data
= next_proto
->data
;
628 *len
= next_proto
->len
;
630 return SSL_TLSEXT_ERR_OK
;
632 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
634 /* This the context that we pass to alpn_cb */
635 typedef struct tlsextalpnctx_st
{
640 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
641 const unsigned char *in
, unsigned int inlen
, void *arg
)
643 tlsextalpnctx
*alpn_ctx
= arg
;
646 /* We can assume that |in| is syntactically valid. */
648 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
649 for (i
= 0; i
< inlen
;) {
651 BIO_write(bio_s_out
, ", ", 2);
652 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
655 BIO_write(bio_s_out
, "\n", 1);
658 if (SSL_select_next_proto
659 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
660 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
661 return SSL_TLSEXT_ERR_ALERT_FATAL
;
665 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
666 BIO_write(bio_s_out
, *out
, *outlen
);
667 BIO_write(bio_s_out
, "\n", 1);
670 return SSL_TLSEXT_ERR_OK
;
673 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
675 /* disable resumption for sessions with forward secure ciphers */
676 return is_forward_secure
;
679 typedef enum OPTION_choice
{
682 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
683 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
684 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
685 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
686 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
687 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
688 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
689 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
691 OPT_CASTORE
, OPT_NOCASTORE
, OPT_CHAINCASTORE
, OPT_VERIFYCASTORE
,
692 OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
693 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
694 OPT_STATUS_TIMEOUT
, OPT_PROXY
, OPT_NO_PROXY
, OPT_STATUS_URL
,
695 OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
696 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
697 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
698 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_IDENTITY
, OPT_PSK_HINT
, OPT_PSK
,
699 OPT_PSK_SESS
, OPT_SRPVFILE
, OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
,
700 OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
, OPT_SSL_CONFIG
,
701 OPT_MAX_SEND_FRAG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
702 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
703 OPT_DTLS1_2
, OPT_SCTP
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
, OPT_STATELESS
,
704 OPT_ID_PREFIX
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
705 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
, OPT_SENDFILE
,
706 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
707 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_RECV_MAX_EARLY
, OPT_EARLY_DATA
,
708 OPT_S_NUM_TICKETS
, OPT_ANTI_REPLAY
, OPT_NO_ANTI_REPLAY
, OPT_SCTP_LABEL_BUG
,
709 OPT_HTTP_SERVER_BINMODE
, OPT_NOCANAMES
, OPT_IGNORE_UNEXPECTED_EOF
,
717 const OPTIONS s_server_options
[] = {
718 OPT_SECTION("General"),
719 {"help", OPT_HELP
, '-', "Display this summary"},
720 {"ssl_config", OPT_SSL_CONFIG
, 's',
721 "Configure SSL_CTX using the given configuration value"},
722 #ifndef OPENSSL_NO_SSL_TRACE
723 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
725 #ifndef OPENSSL_NO_ENGINE
726 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
729 OPT_SECTION("Network"),
730 {"port", OPT_PORT
, 'p',
731 "TCP/IP port to listen on for connections (default is " PORT
")"},
732 {"accept", OPT_ACCEPT
, 's',
733 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
735 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
736 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
738 {"4", OPT_4
, '-', "Use IPv4 only"},
739 {"6", OPT_6
, '-', "Use IPv6 only"},
741 OPT_SECTION("Identity"),
742 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
743 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
744 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
745 {"CAstore", OPT_CASTORE
, ':', "URI to store of CA's"},
746 {"no-CAfile", OPT_NOCAFILE
, '-',
747 "Do not load the default certificates file"},
748 {"no-CApath", OPT_NOCAPATH
, '-',
749 "Do not load certificates from the default certificates directory"},
750 {"no-CAstore", OPT_NOCASTORE
, '-',
751 "Do not load certificates from the default certificates store URI"},
752 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
753 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
754 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
755 "Turn on peer certificate verification, must have a cert"},
756 {"nameopt", OPT_NAMEOPT
, 's', "Certificate subject/issuer name printing options"},
757 {"cert", OPT_CERT
, '<', "Server certificate file to use; default " TEST_CERT
},
758 {"cert2", OPT_CERT2
, '<',
759 "Certificate file to use for servername; default " TEST_CERT2
},
760 {"certform", OPT_CERTFORM
, 'F',
761 "Server certificate file format (PEM/DER/P12); has no effect"},
762 {"cert_chain", OPT_CERT_CHAIN
, '<',
763 "Server certificate chain file in PEM format"},
764 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build server certificate chain"},
765 {"serverinfo", OPT_SERVERINFO
, 's',
766 "PEM serverinfo file for certificate"},
767 {"key", OPT_KEY
, 's',
768 "Private key file to use; default is -cert file or else" TEST_CERT
},
769 {"key2", OPT_KEY2
, '<',
770 "-Private Key file to use for servername if not in -cert2"},
771 {"keyform", OPT_KEYFORM
, 'f', "Key format (ENGINE, other values ignored)"},
772 {"pass", OPT_PASS
, 's', "Private key and cert file pass phrase source"},
773 {"dcert", OPT_DCERT
, '<',
774 "Second server certificate file to use (usually for DSA)"},
775 {"dcertform", OPT_DCERTFORM
, 'F',
776 "Second server certificate file format (PEM/DER/P12); has no effect"},
777 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
778 "second server certificate chain file in PEM format"},
779 {"dkey", OPT_DKEY
, '<',
780 "Second private key file to use (usually for DSA)"},
781 {"dkeyform", OPT_DKEYFORM
, 'F',
782 "Second key file format (ENGINE, other values ignored)"},
783 {"dpass", OPT_DPASS
, 's',
784 "Second private key and cert file pass phrase source"},
785 {"dhparam", OPT_DHPARAM
, '<', "DH parameters file to use"},
786 {"servername", OPT_SERVERNAME
, 's',
787 "Servername for HostName TLS extension"},
788 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
789 "On servername mismatch send fatal alert (default warning alert)"},
790 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
791 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
792 {"quiet", OPT_QUIET
, '-', "No server output"},
793 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
794 "Disable caching and tickets if ephemeral (EC)DH is used"},
795 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
796 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
797 {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF
, '-',
798 "Do not treat lack of close_notify from a peer as an error"},
799 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
800 "Hex dump of all TLS extensions received"},
801 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
802 {"id_prefix", OPT_ID_PREFIX
, 's',
803 "Generate SSL/TLS session IDs prefixed by arg"},
804 {"keymatexport", OPT_KEYMATEXPORT
, 's',
805 "Export keying material using label"},
806 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
807 "Export len bytes of keying material; default 20"},
808 {"CRL", OPT_CRL
, '<', "CRL file to use"},
809 {"CRLform", OPT_CRLFORM
, 'F', "CRL file format (PEM or DER); default PEM"},
810 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
811 "Download CRLs from distribution points in certificate CDP entries"},
812 {"chainCAfile", OPT_CHAINCAFILE
, '<',
813 "CA file for certificate chain (PEM format)"},
814 {"chainCApath", OPT_CHAINCAPATH
, '/',
815 "use dir as certificate store path to build CA certificate chain"},
816 {"chainCAstore", OPT_CHAINCASTORE
, ':',
817 "use URI as certificate store to build CA certificate chain"},
818 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
819 "CA file for certificate verification (PEM format)"},
820 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
821 "use dir as certificate store path to verify CA certificate"},
822 {"verifyCAstore", OPT_VERIFYCASTORE
, ':',
823 "use URI as certificate store to verify CA certificate"},
824 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
825 {"ext_cache", OPT_EXT_CACHE
, '-',
826 "Disable internal cache, set up and use external cache"},
827 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
828 "Close connection on verification error"},
829 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
830 "No verify output except verify errors"},
831 {"ign_eof", OPT_IGN_EOF
, '-', "Ignore input EOF (default when -quiet)"},
832 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input EOF"},
834 #ifndef OPENSSL_NO_OCSP
836 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
837 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
838 "Print more output in certificate status callback"},
839 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
840 "Status request responder timeout"},
841 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
842 {"proxy", OPT_PROXY
, 's',
843 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
844 {"no_proxy", OPT_NO_PROXY
, 's',
845 "List of addresses of servers not to use HTTP(S) proxy for"},
847 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
848 {"status_file", OPT_STATUS_FILE
, '<',
849 "File containing DER encoded OCSP Response"},
852 OPT_SECTION("Debug"),
853 {"security_debug", OPT_SECURITY_DEBUG
, '-',
854 "Print output from SSL/TLS security framework"},
855 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
856 "Print more output from SSL/TLS security framework"},
857 {"brief", OPT_BRIEF
, '-',
858 "Restrict output to brief summary of connection parameters"},
859 {"rev", OPT_REV
, '-',
860 "act as a simple test server which just sends back with the received text reversed"},
861 {"debug", OPT_DEBUG
, '-', "Print more output"},
862 {"msg", OPT_MSG
, '-', "Show protocol messages"},
863 {"msgfile", OPT_MSGFILE
, '>',
864 "File to send output of -msg or -trace, instead of stdout"},
865 {"state", OPT_STATE
, '-', "Print the SSL states"},
866 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
867 {"max_pipelines", OPT_MAX_PIPELINES
, 'p',
868 "Maximum number of encrypt/decrypt pipelines to be used"},
869 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
870 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
872 OPT_SECTION("Network"),
873 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
874 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
875 {"mtu", OPT_MTU
, 'p', "Set link-layer MTU"},
876 {"read_buf", OPT_READ_BUF
, 'p',
877 "Default read buffer size to be used for connections"},
878 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'p',
879 "Size used to split data for encrypt pipelines"},
880 {"max_send_frag", OPT_MAX_SEND_FRAG
, 'p', "Maximum Size of send frames "},
882 OPT_SECTION("Server identity"),
883 {"psk_identity", OPT_PSK_IDENTITY
, 's', "PSK identity to expect"},
884 #ifndef OPENSSL_NO_PSK
885 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
887 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
888 {"psk_session", OPT_PSK_SESS
, '<', "File to read PSK SSL session from"},
889 #ifndef OPENSSL_NO_SRP
890 {"srpvfile", OPT_SRPVFILE
, '<', "(deprecated) The verifier file for SRP"},
891 {"srpuserseed", OPT_SRPUSERSEED
, 's',
892 "(deprecated) A seed string for a default user salt"},
895 OPT_SECTION("Protocol and version"),
896 {"max_early_data", OPT_MAX_EARLY
, 'n',
897 "The maximum number of bytes of early data as advertised in tickets"},
898 {"recv_max_early_data", OPT_RECV_MAX_EARLY
, 'n',
899 "The maximum number of bytes of early data (hard limit)"},
900 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
901 {"num_tickets", OPT_S_NUM_TICKETS
, 'n',
902 "The number of TLSv1.3 session tickets that a server will automatically issue" },
903 {"anti_replay", OPT_ANTI_REPLAY
, '-', "Switch on anti-replay protection (default)"},
904 {"no_anti_replay", OPT_NO_ANTI_REPLAY
, '-', "Switch off anti-replay protection"},
905 {"http_server_binmode", OPT_HTTP_SERVER_BINMODE
, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)"},
906 {"no_ca_names", OPT_NOCANAMES
, '-',
907 "Disable TLS Extension CA Names"},
908 {"stateless", OPT_STATELESS
, '-', "Require TLSv1.3 cookies"},
909 #ifndef OPENSSL_NO_SSL3
910 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
912 #ifndef OPENSSL_NO_TLS1
913 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
915 #ifndef OPENSSL_NO_TLS1_1
916 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
918 #ifndef OPENSSL_NO_TLS1_2
919 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
921 #ifndef OPENSSL_NO_TLS1_3
922 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
924 #ifndef OPENSSL_NO_DTLS
925 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
926 {"listen", OPT_LISTEN
, '-',
927 "Listen for a DTLS ClientHello with a cookie and then connect"},
929 #ifndef OPENSSL_NO_DTLS1
930 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
932 #ifndef OPENSSL_NO_DTLS1_2
933 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
935 #ifndef OPENSSL_NO_SCTP
936 {"sctp", OPT_SCTP
, '-', "Use SCTP"},
937 {"sctp_label_bug", OPT_SCTP_LABEL_BUG
, '-', "Enable SCTP label length bug"},
939 #ifndef OPENSSL_NO_SRTP
940 {"use_srtp", OPT_SRTP_PROFILES
, 's',
941 "Offer SRTP key management with a colon-separated profile list"},
943 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
944 #ifndef OPENSSL_NO_NEXTPROTONEG
945 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
946 "Set the advertised protocols for the NPN extension (comma-separated list)"},
948 {"alpn", OPT_ALPN
, 's',
949 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
950 #ifndef OPENSSL_NO_KTLS
951 {"sendfile", OPT_SENDFILE
, '-', "Use sendfile to response file with -WWW"},
962 #define IS_PROT_FLAG(o) \
963 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
964 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
966 int s_server_main(int argc
, char *argv
[])
968 ENGINE
*engine
= NULL
;
969 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
970 SSL_CONF_CTX
*cctx
= NULL
;
971 const SSL_METHOD
*meth
= TLS_server_method();
972 SSL_EXCERT
*exc
= NULL
;
973 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
974 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
975 STACK_OF(X509_CRL
) *crls
= NULL
;
976 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
977 X509_VERIFY_PARAM
*vpm
= NULL
;
978 const char *CApath
= NULL
, *CAfile
= NULL
, *CAstore
= NULL
;
979 const char *chCApath
= NULL
, *chCAfile
= NULL
, *chCAstore
= NULL
;
980 char *dpassarg
= NULL
, *dpass
= NULL
;
981 char *passarg
= NULL
, *pass
= NULL
;
982 char *vfyCApath
= NULL
, *vfyCAfile
= NULL
, *vfyCAstore
= NULL
;
983 char *crl_file
= NULL
, *prog
;
985 int unlink_unix_path
= 0;
987 do_server_cb server_cb
;
988 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
991 int nocert
= 0, ret
= 1;
992 int noCApath
= 0, noCAfile
= 0, noCAstore
= 0;
993 int s_cert_format
= FORMAT_UNDEF
, s_key_format
= FORMAT_UNDEF
;
994 int s_dcert_format
= FORMAT_UNDEF
, s_dkey_format
= FORMAT_UNDEF
;
995 int rev
= 0, naccept
= -1, sdebug
= 0;
996 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
, protocol
= 0;
997 int state
= 0, crl_format
= FORMAT_UNDEF
, crl_download
= 0;
999 char *port
= OPENSSL_strdup(PORT
);
1000 unsigned char *context
= NULL
;
1002 EVP_PKEY
*s_key2
= NULL
;
1003 X509
*s_cert2
= NULL
;
1004 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
1005 const char *ssl_config
= NULL
;
1006 int read_buf_len
= 0;
1007 #ifndef OPENSSL_NO_NEXTPROTONEG
1008 const char *next_proto_neg_in
= NULL
;
1009 tlsextnextprotoctx next_proto
= { NULL
, 0 };
1011 const char *alpn_in
= NULL
;
1012 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
1013 #ifndef OPENSSL_NO_PSK
1014 /* by default do not send a PSK identity hint */
1015 char *psk_identity_hint
= NULL
;
1018 #ifndef OPENSSL_NO_SRP
1019 char *srpuserseed
= NULL
;
1020 char *srp_verifier_file
= NULL
;
1022 #ifndef OPENSSL_NO_SRTP
1023 char *srtp_profiles
= NULL
;
1025 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
1026 int s_server_verify
= SSL_VERIFY_NONE
;
1027 int s_server_session_id_context
= 1; /* anything will do */
1028 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
1029 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
1030 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
1031 #ifndef OPENSSL_NO_OCSP
1032 int s_tlsextstatus
= 0;
1034 int no_resume_ephemeral
= 0;
1035 unsigned int max_send_fragment
= 0;
1036 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
1037 const char *s_serverinfo_file
= NULL
;
1038 const char *keylog_file
= NULL
;
1039 int max_early_data
= -1, recv_max_early_data
= -1;
1040 char *psksessf
= NULL
;
1041 int no_ca_names
= 0;
1042 #ifndef OPENSSL_NO_SCTP
1043 int sctp_label_bug
= 0;
1045 int ignore_unexpected_eof
= 0;
1047 /* Init of few remaining global variables */
1052 s_nbio
= s_nbio_test
= 0;
1062 cctx
= SSL_CONF_CTX_new();
1063 vpm
= X509_VERIFY_PARAM_new();
1064 if (cctx
== NULL
|| vpm
== NULL
)
1066 SSL_CONF_CTX_set_flags(cctx
,
1067 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1069 prog
= opt_init(argc
, argv
, s_server_options
);
1070 while ((o
= opt_next()) != OPT_EOF
) {
1071 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1072 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1075 if (IS_NO_PROT_FLAG(o
))
1077 if (prot_opt
== 1 && no_prot_opt
) {
1079 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1086 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1089 opt_help(s_server_options
);
1095 if (socket_family
== AF_UNIX
) {
1096 OPENSSL_free(host
); host
= NULL
;
1097 OPENSSL_free(port
); port
= NULL
;
1100 socket_family
= AF_INET
;
1106 if (socket_family
== AF_UNIX
) {
1107 OPENSSL_free(host
); host
= NULL
;
1108 OPENSSL_free(port
); port
= NULL
;
1111 socket_family
= AF_INET6
;
1114 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1120 if (socket_family
== AF_UNIX
) {
1121 socket_family
= AF_UNSPEC
;
1124 OPENSSL_free(port
); port
= NULL
;
1125 OPENSSL_free(host
); host
= NULL
;
1126 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1128 "%s: -port argument malformed or ambiguous\n",
1135 if (socket_family
== AF_UNIX
) {
1136 socket_family
= AF_UNSPEC
;
1139 OPENSSL_free(port
); port
= NULL
;
1140 OPENSSL_free(host
); host
= NULL
;
1141 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1143 "%s: -accept argument malformed or ambiguous\n",
1150 socket_family
= AF_UNIX
;
1151 OPENSSL_free(host
); host
= OPENSSL_strdup(opt_arg());
1152 OPENSSL_free(port
); port
= NULL
;
1155 unlink_unix_path
= 1;
1159 naccept
= atol(opt_arg());
1162 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1163 verify_args
.depth
= atoi(opt_arg());
1165 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1167 case OPT_UPPER_V_VERIFY
:
1169 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1170 SSL_VERIFY_CLIENT_ONCE
;
1171 verify_args
.depth
= atoi(opt_arg());
1174 "verify depth is %d, must return a certificate\n",
1178 context
= (unsigned char *)opt_arg();
1181 s_cert_file
= opt_arg();
1184 if (!set_nameopt(opt_arg()))
1188 crl_file
= opt_arg();
1190 case OPT_CRL_DOWNLOAD
:
1193 case OPT_SERVERINFO
:
1194 s_serverinfo_file
= opt_arg();
1197 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_cert_format
))
1201 s_key_file
= opt_arg();
1204 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1208 passarg
= opt_arg();
1210 case OPT_CERT_CHAIN
:
1211 s_chain_file
= opt_arg();
1217 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dcert_format
))
1221 s_dcert_file
= opt_arg();
1224 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dkey_format
))
1228 dpassarg
= opt_arg();
1231 s_dkey_file
= opt_arg();
1233 case OPT_DCERT_CHAIN
:
1234 s_dchain_file
= opt_arg();
1245 case OPT_CHAINCAPATH
:
1246 chCApath
= opt_arg();
1248 case OPT_VERIFYCAPATH
:
1249 vfyCApath
= opt_arg();
1252 CAstore
= opt_arg();
1257 case OPT_CHAINCASTORE
:
1258 chCAstore
= opt_arg();
1260 case OPT_VERIFYCASTORE
:
1261 vfyCAstore
= opt_arg();
1270 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1273 case OPT_S_IMMEDIATE_RENEG
:
1274 immediate_reneg
= 1;
1277 case OPT_S_NUM_TICKETS
:
1278 case OPT_ANTI_REPLAY
:
1279 case OPT_NO_ANTI_REPLAY
:
1280 if (ssl_args
== NULL
)
1281 ssl_args
= sk_OPENSSL_STRING_new_null();
1282 if (ssl_args
== NULL
1283 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1284 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1285 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1290 if (!opt_verify(o
, vpm
))
1295 if (!args_excert(o
, &exc
))
1298 case OPT_VERIFY_RET_ERROR
:
1299 verify_args
.return_error
= 1;
1301 case OPT_VERIFY_QUIET
:
1302 verify_args
.quiet
= 1;
1304 case OPT_BUILD_CHAIN
:
1313 case OPT_CHAINCAFILE
:
1314 chCAfile
= opt_arg();
1316 case OPT_VERIFYCAFILE
:
1317 vfyCAfile
= opt_arg();
1323 s_nbio
= s_nbio_test
= 1;
1328 case OPT_NO_IGN_EOF
:
1334 case OPT_TLSEXTDEBUG
:
1338 #ifndef OPENSSL_NO_OCSP
1342 case OPT_STATUS_VERBOSE
:
1343 #ifndef OPENSSL_NO_OCSP
1344 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1347 case OPT_STATUS_TIMEOUT
:
1348 #ifndef OPENSSL_NO_OCSP
1350 tlscstatp
.timeout
= atoi(opt_arg());
1354 #ifndef OPENSSL_NO_OCSP
1355 tlscstatp
.proxy
= opt_arg();
1359 #ifndef OPENSSL_NO_OCSP
1360 tlscstatp
.no_proxy
= opt_arg();
1363 case OPT_STATUS_URL
:
1364 #ifndef OPENSSL_NO_OCSP
1366 if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp
.use_ssl
, NULL
,
1367 &tlscstatp
.host
, &tlscstatp
.port
, NULL
,
1368 &tlscstatp
.path
, NULL
, NULL
)) {
1369 BIO_printf(bio_err
, "Error parsing -status_url argument\n");
1374 case OPT_STATUS_FILE
:
1375 #ifndef OPENSSL_NO_OCSP
1377 tlscstatp
.respin
= opt_arg();
1384 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1387 #ifndef OPENSSL_NO_SSL_TRACE
1391 case OPT_SECURITY_DEBUG
:
1394 case OPT_SECURITY_DEBUG_VERBOSE
:
1407 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1412 case OPT_NO_RESUME_EPHEMERAL
:
1413 no_resume_ephemeral
= 1;
1415 case OPT_PSK_IDENTITY
:
1416 psk_identity
= opt_arg();
1419 #ifndef OPENSSL_NO_PSK
1420 psk_identity_hint
= opt_arg();
1424 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1425 if (isxdigit(_UC(*p
)))
1427 BIO_printf(bio_err
, "Not a hex number '%s'\n", psk_key
);
1432 psksessf
= opt_arg();
1435 #ifndef OPENSSL_NO_SRP
1436 srp_verifier_file
= opt_arg();
1437 if (min_version
< TLS1_VERSION
)
1438 min_version
= TLS1_VERSION
;
1441 case OPT_SRPUSERSEED
:
1442 #ifndef OPENSSL_NO_SRP
1443 srpuserseed
= opt_arg();
1444 if (min_version
< TLS1_VERSION
)
1445 min_version
= TLS1_VERSION
;
1460 case OPT_SSL_CONFIG
:
1461 ssl_config
= opt_arg();
1464 min_version
= SSL3_VERSION
;
1465 max_version
= SSL3_VERSION
;
1468 min_version
= TLS1_3_VERSION
;
1469 max_version
= TLS1_3_VERSION
;
1472 min_version
= TLS1_2_VERSION
;
1473 max_version
= TLS1_2_VERSION
;
1476 min_version
= TLS1_1_VERSION
;
1477 max_version
= TLS1_1_VERSION
;
1480 min_version
= TLS1_VERSION
;
1481 max_version
= TLS1_VERSION
;
1484 #ifndef OPENSSL_NO_DTLS
1485 meth
= DTLS_server_method();
1486 socket_type
= SOCK_DGRAM
;
1490 #ifndef OPENSSL_NO_DTLS
1491 meth
= DTLS_server_method();
1492 min_version
= DTLS1_VERSION
;
1493 max_version
= DTLS1_VERSION
;
1494 socket_type
= SOCK_DGRAM
;
1498 #ifndef OPENSSL_NO_DTLS
1499 meth
= DTLS_server_method();
1500 min_version
= DTLS1_2_VERSION
;
1501 max_version
= DTLS1_2_VERSION
;
1502 socket_type
= SOCK_DGRAM
;
1506 #ifndef OPENSSL_NO_SCTP
1507 protocol
= IPPROTO_SCTP
;
1510 case OPT_SCTP_LABEL_BUG
:
1511 #ifndef OPENSSL_NO_SCTP
1516 #ifndef OPENSSL_NO_DTLS
1517 enable_timeouts
= 1;
1521 #ifndef OPENSSL_NO_DTLS
1522 socket_mtu
= atol(opt_arg());
1526 #ifndef OPENSSL_NO_DTLS
1534 session_id_prefix
= opt_arg();
1537 #ifndef OPENSSL_NO_ENGINE
1538 engine
= setup_engine(opt_arg(), s_debug
);
1545 case OPT_PROV_CASES
:
1546 if (!opt_provider(o
))
1549 case OPT_SERVERNAME
:
1550 tlsextcbp
.servername
= opt_arg();
1552 case OPT_SERVERNAME_FATAL
:
1553 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1556 s_cert_file2
= opt_arg();
1559 s_key_file2
= opt_arg();
1561 case OPT_NEXTPROTONEG
:
1562 # ifndef OPENSSL_NO_NEXTPROTONEG
1563 next_proto_neg_in
= opt_arg();
1567 alpn_in
= opt_arg();
1569 case OPT_SRTP_PROFILES
:
1570 #ifndef OPENSSL_NO_SRTP
1571 srtp_profiles
= opt_arg();
1574 case OPT_KEYMATEXPORT
:
1575 keymatexportlabel
= opt_arg();
1577 case OPT_KEYMATEXPORTLEN
:
1578 keymatexportlen
= atoi(opt_arg());
1583 case OPT_MAX_SEND_FRAG
:
1584 max_send_fragment
= atoi(opt_arg());
1586 case OPT_SPLIT_SEND_FRAG
:
1587 split_send_fragment
= atoi(opt_arg());
1589 case OPT_MAX_PIPELINES
:
1590 max_pipelines
= atoi(opt_arg());
1593 read_buf_len
= atoi(opt_arg());
1595 case OPT_KEYLOG_FILE
:
1596 keylog_file
= opt_arg();
1599 max_early_data
= atoi(opt_arg());
1600 if (max_early_data
< 0) {
1601 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1605 case OPT_RECV_MAX_EARLY
:
1606 recv_max_early_data
= atoi(opt_arg());
1607 if (recv_max_early_data
< 0) {
1608 BIO_printf(bio_err
, "Invalid value for recv_max_early_data\n");
1612 case OPT_EARLY_DATA
:
1614 if (max_early_data
== -1)
1615 max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
1617 case OPT_HTTP_SERVER_BINMODE
:
1618 http_server_binmode
= 1;
1624 #ifndef OPENSSL_NO_KTLS
1628 case OPT_IGNORE_UNEXPECTED_EOF
:
1629 ignore_unexpected_eof
= 1;
1634 /* No extra arguments. */
1635 argc
= opt_num_rest();
1639 if (!app_RAND_load())
1642 #ifndef OPENSSL_NO_NEXTPROTONEG
1643 if (min_version
== TLS1_3_VERSION
&& next_proto_neg_in
!= NULL
) {
1644 BIO_printf(bio_err
, "Cannot supply -nextprotoneg with TLSv1.3\n");
1648 #ifndef OPENSSL_NO_DTLS
1649 if (www
&& socket_type
== SOCK_DGRAM
) {
1650 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1654 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1655 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1660 if (stateless
&& socket_type
!= SOCK_STREAM
) {
1661 BIO_printf(bio_err
, "Can only use --stateless with TLS\n");
1666 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1668 "Can't use unix sockets and datagrams together\n");
1672 if (early_data
&& (www
> 0 || rev
)) {
1674 "Can't use -early_data in combination with -www, -WWW, -HTTP, or -rev\n");
1678 #ifndef OPENSSL_NO_SCTP
1679 if (protocol
== IPPROTO_SCTP
) {
1680 if (socket_type
!= SOCK_DGRAM
) {
1681 BIO_printf(bio_err
, "Can't use -sctp without DTLS\n");
1684 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1685 socket_type
= SOCK_STREAM
;
1689 #ifndef OPENSSL_NO_KTLS
1690 if (use_sendfile
&& www
<= 1) {
1691 BIO_printf(bio_err
, "Can't use -sendfile without -WWW or -HTTP\n");
1696 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1697 BIO_printf(bio_err
, "Error getting password\n");
1701 if (s_key_file
== NULL
)
1702 s_key_file
= s_cert_file
;
1704 if (s_key_file2
== NULL
)
1705 s_key_file2
= s_cert_file2
;
1707 if (!load_excert(&exc
))
1711 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1712 "server certificate private key");
1716 s_cert
= load_cert_pass(s_cert_file
, s_cert_format
, 1, pass
,
1717 "server certificate");
1721 if (s_chain_file
!= NULL
) {
1722 if (!load_certs(s_chain_file
, 0, &s_chain
, NULL
,
1723 "server certificate chain"))
1727 if (tlsextcbp
.servername
!= NULL
) {
1728 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1729 "second server certificate private key");
1733 s_cert2
= load_cert_pass(s_cert_file2
, s_cert_format
, 1, pass
,
1734 "second server certificate");
1736 if (s_cert2
== NULL
)
1740 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1741 if (next_proto_neg_in
) {
1742 next_proto
.data
= next_protos_parse(&next_proto
.len
, next_proto_neg_in
);
1743 if (next_proto
.data
== NULL
)
1747 alpn_ctx
.data
= NULL
;
1749 alpn_ctx
.data
= next_protos_parse(&alpn_ctx
.len
, alpn_in
);
1750 if (alpn_ctx
.data
== NULL
)
1754 if (crl_file
!= NULL
) {
1756 crl
= load_crl(crl_file
, crl_format
, 0, "CRL");
1759 crls
= sk_X509_CRL_new_null();
1760 if (crls
== NULL
|| !sk_X509_CRL_push(crls
, crl
)) {
1761 BIO_puts(bio_err
, "Error adding CRL\n");
1762 ERR_print_errors(bio_err
);
1768 if (s_dcert_file
!= NULL
) {
1770 if (s_dkey_file
== NULL
)
1771 s_dkey_file
= s_dcert_file
;
1773 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1774 0, dpass
, engine
, "second certificate private key");
1778 s_dcert
= load_cert_pass(s_dcert_file
, s_dcert_format
, 1, dpass
,
1779 "second server certificate");
1781 if (s_dcert
== NULL
) {
1782 ERR_print_errors(bio_err
);
1785 if (s_dchain_file
!= NULL
) {
1786 if (!load_certs(s_dchain_file
, 0, &s_dchain
, NULL
,
1787 "second server certificate chain"))
1793 if (bio_s_out
== NULL
) {
1794 if (s_quiet
&& !s_debug
) {
1795 bio_s_out
= BIO_new(BIO_s_null());
1796 if (s_msg
&& bio_s_msg
== NULL
)
1797 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1799 if (bio_s_out
== NULL
)
1800 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1806 s_dcert_file
= NULL
;
1808 s_cert_file2
= NULL
;
1812 ctx
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
1814 ERR_print_errors(bio_err
);
1818 SSL_CTX_clear_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1821 ssl_ctx_security_debug(ctx
, sdebug
);
1823 if (!config_ctx(cctx
, ssl_args
, ctx
))
1827 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1828 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1830 ERR_print_errors(bio_err
);
1834 #ifndef OPENSSL_NO_SCTP
1835 if (protocol
== IPPROTO_SCTP
&& sctp_label_bug
== 1)
1836 SSL_CTX_set_mode(ctx
, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
);
1839 if (min_version
!= 0
1840 && SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1842 if (max_version
!= 0
1843 && SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1846 if (session_id_prefix
) {
1847 if (strlen(session_id_prefix
) >= 32)
1849 "warning: id_prefix is too long, only one new session will be possible\n");
1850 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1851 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1852 ERR_print_errors(bio_err
);
1855 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1858 ssl_ctx_set_excert(ctx
, exc
);
1861 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1863 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1865 init_session_cache_ctx(ctx
);
1867 SSL_CTX_sess_set_cache_size(ctx
, 128);
1870 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1874 SSL_CTX_set_options(ctx
, SSL_OP_DISABLE_TLSEXT_CA_NAMES
);
1877 if (ignore_unexpected_eof
)
1878 SSL_CTX_set_options(ctx
, SSL_OP_IGNORE_UNEXPECTED_EOF
);
1880 if (max_send_fragment
> 0
1881 && !SSL_CTX_set_max_send_fragment(ctx
, max_send_fragment
)) {
1882 BIO_printf(bio_err
, "%s: Max send fragment size %u is out of permitted range\n",
1883 prog
, max_send_fragment
);
1887 if (split_send_fragment
> 0
1888 && !SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
)) {
1889 BIO_printf(bio_err
, "%s: Split send fragment size %u is out of permitted range\n",
1890 prog
, split_send_fragment
);
1893 if (max_pipelines
> 0
1894 && !SSL_CTX_set_max_pipelines(ctx
, max_pipelines
)) {
1895 BIO_printf(bio_err
, "%s: Max pipelines %u is out of permitted range\n",
1896 prog
, max_pipelines
);
1900 if (read_buf_len
> 0) {
1901 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1903 #ifndef OPENSSL_NO_SRTP
1904 if (srtp_profiles
!= NULL
) {
1905 /* Returns 0 on success! */
1906 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1907 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1908 ERR_print_errors(bio_err
);
1914 if (!ctx_set_verify_locations(ctx
, CAfile
, noCAfile
, CApath
, noCApath
,
1915 CAstore
, noCAstore
)) {
1916 ERR_print_errors(bio_err
);
1919 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1920 BIO_printf(bio_err
, "Error setting verify params\n");
1921 ERR_print_errors(bio_err
);
1925 ssl_ctx_add_crls(ctx
, crls
, 0);
1927 if (!ssl_load_stores(ctx
,
1928 vfyCApath
, vfyCAfile
, vfyCAstore
,
1929 chCApath
, chCAfile
, chCAstore
,
1930 crls
, crl_download
)) {
1931 BIO_printf(bio_err
, "Error loading store locations\n");
1932 ERR_print_errors(bio_err
);
1937 ctx2
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
1939 ERR_print_errors(bio_err
);
1945 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
1948 ssl_ctx_security_debug(ctx2
, sdebug
);
1950 if (session_id_prefix
) {
1951 if (strlen(session_id_prefix
) >= 32)
1953 "warning: id_prefix is too long, only one new session will be possible\n");
1954 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
1955 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1956 ERR_print_errors(bio_err
);
1959 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1962 ssl_ctx_set_excert(ctx2
, exc
);
1965 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
1968 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
1970 init_session_cache_ctx(ctx2
);
1972 SSL_CTX_sess_set_cache_size(ctx2
, 128);
1975 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
1977 if (!ctx_set_verify_locations(ctx2
, CAfile
, noCAfile
, CApath
,
1978 noCApath
, CAstore
, noCAstore
)) {
1979 ERR_print_errors(bio_err
);
1982 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
1983 BIO_printf(bio_err
, "Error setting verify params\n");
1984 ERR_print_errors(bio_err
);
1988 ssl_ctx_add_crls(ctx2
, crls
, 0);
1989 if (!config_ctx(cctx
, ssl_args
, ctx2
))
1992 #ifndef OPENSSL_NO_NEXTPROTONEG
1993 if (next_proto
.data
)
1994 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
1998 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
2001 EVP_PKEY
*dhpkey
= NULL
;
2004 dhpkey
= load_keyparams(dhfile
, FORMAT_UNDEF
, 0, "DH", "DH parameters");
2005 else if (s_cert_file
!= NULL
)
2006 dhpkey
= load_keyparams(s_cert_file
, FORMAT_UNDEF
, 0, "DH", "DH parameters");
2008 if (dhpkey
!= NULL
) {
2009 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2011 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
2013 (void)BIO_flush(bio_s_out
);
2015 if (dhpkey
== NULL
) {
2016 SSL_CTX_set_dh_auto(ctx
, 1);
2019 * We need 2 references: one for use by ctx and one for use by
2022 if (!EVP_PKEY_up_ref(dhpkey
)) {
2023 EVP_PKEY_free(dhpkey
);
2026 if (!SSL_CTX_set0_tmp_dh_pkey(ctx
, dhpkey
)) {
2027 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2028 ERR_print_errors(bio_err
);
2029 /* Free 2 references */
2030 EVP_PKEY_free(dhpkey
);
2031 EVP_PKEY_free(dhpkey
);
2037 if (dhfile
!= NULL
) {
2038 EVP_PKEY
*dhpkey2
= load_keyparams(s_cert_file2
, FORMAT_UNDEF
,
2042 if (dhpkey2
!= NULL
) {
2043 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2044 (void)BIO_flush(bio_s_out
);
2046 EVP_PKEY_free(dhpkey
);
2050 if (dhpkey
== NULL
) {
2051 SSL_CTX_set_dh_auto(ctx2
, 1);
2052 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2
, dhpkey
)) {
2053 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2054 ERR_print_errors(bio_err
);
2055 EVP_PKEY_free(dhpkey
);
2060 EVP_PKEY_free(dhpkey
);
2063 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
2066 if (s_serverinfo_file
!= NULL
2067 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
2068 ERR_print_errors(bio_err
);
2073 && !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
2076 if (s_dcert
!= NULL
) {
2077 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
2081 if (no_resume_ephemeral
) {
2082 SSL_CTX_set_not_resumable_session_callback(ctx
,
2083 not_resumable_sess_cb
);
2086 SSL_CTX_set_not_resumable_session_callback(ctx2
,
2087 not_resumable_sess_cb
);
2089 #ifndef OPENSSL_NO_PSK
2090 if (psk_key
!= NULL
) {
2092 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
2093 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
2096 if (psk_identity_hint
!= NULL
) {
2097 if (min_version
== TLS1_3_VERSION
) {
2098 BIO_printf(bio_s_out
, "PSK warning: there is NO identity hint in TLSv1.3\n");
2100 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
2101 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
2102 ERR_print_errors(bio_err
);
2108 if (psksessf
!= NULL
) {
2109 BIO
*stmp
= BIO_new_file(psksessf
, "r");
2112 BIO_printf(bio_err
, "Can't open PSK session file %s\n", psksessf
);
2113 ERR_print_errors(bio_err
);
2116 psksess
= PEM_read_bio_SSL_SESSION(stmp
, NULL
, 0, NULL
);
2118 if (psksess
== NULL
) {
2119 BIO_printf(bio_err
, "Can't read PSK session file %s\n", psksessf
);
2120 ERR_print_errors(bio_err
);
2126 if (psk_key
!= NULL
|| psksess
!= NULL
)
2127 SSL_CTX_set_psk_find_session_callback(ctx
, psk_find_session_cb
);
2129 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
2130 if (!SSL_CTX_set_session_id_context(ctx
,
2131 (void *)&s_server_session_id_context
,
2132 sizeof(s_server_session_id_context
))) {
2133 BIO_printf(bio_err
, "error setting session id context\n");
2134 ERR_print_errors(bio_err
);
2138 /* Set DTLS cookie generation and verification callbacks */
2139 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
2140 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
2142 /* Set TLS1.3 cookie generation and verification callbacks */
2143 SSL_CTX_set_stateless_cookie_generate_cb(ctx
, generate_stateless_cookie_callback
);
2144 SSL_CTX_set_stateless_cookie_verify_cb(ctx
, verify_stateless_cookie_callback
);
2147 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
2148 if (!SSL_CTX_set_session_id_context(ctx2
,
2149 (void *)&s_server_session_id_context
,
2150 sizeof(s_server_session_id_context
))) {
2151 BIO_printf(bio_err
, "error setting session id context\n");
2152 ERR_print_errors(bio_err
);
2155 tlsextcbp
.biodebug
= bio_s_out
;
2156 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
2157 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
2158 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
2159 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
2162 #ifndef OPENSSL_NO_SRP
2163 if (srp_verifier_file
!= NULL
) {
2164 if (!set_up_srp_verifier_file(ctx
, &srp_callback_parm
, srpuserseed
,
2169 if (CAfile
!= NULL
) {
2170 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
2173 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
2175 #ifndef OPENSSL_NO_OCSP
2176 if (s_tlsextstatus
) {
2177 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
2178 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2180 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2181 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2185 if (set_keylog_file(ctx
, keylog_file
))
2188 if (max_early_data
>= 0)
2189 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2190 if (recv_max_early_data
>= 0)
2191 SSL_CTX_set_recv_max_early_data(ctx
, recv_max_early_data
);
2194 server_cb
= rev_body
;
2196 server_cb
= www_body
;
2198 server_cb
= sv_body
;
2200 if (socket_family
== AF_UNIX
2201 && unlink_unix_path
)
2204 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
, protocol
,
2205 server_cb
, context
, naccept
, bio_s_out
);
2206 print_stats(bio_s_out
, ctx
);
2210 SSL_SESSION_free(psksess
);
2211 set_keylog_file(NULL
, NULL
);
2213 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2215 EVP_PKEY_free(s_key
);
2216 EVP_PKEY_free(s_dkey
);
2217 sk_X509_pop_free(s_chain
, X509_free
);
2218 sk_X509_pop_free(s_dchain
, X509_free
);
2220 OPENSSL_free(dpass
);
2223 X509_VERIFY_PARAM_free(vpm
);
2225 OPENSSL_free(tlscstatp
.host
);
2226 OPENSSL_free(tlscstatp
.port
);
2227 OPENSSL_free(tlscstatp
.path
);
2230 EVP_PKEY_free(s_key2
);
2231 #ifndef OPENSSL_NO_NEXTPROTONEG
2232 OPENSSL_free(next_proto
.data
);
2234 OPENSSL_free(alpn_ctx
.data
);
2235 ssl_excert_free(exc
);
2236 sk_OPENSSL_STRING_free(ssl_args
);
2237 SSL_CONF_CTX_free(cctx
);
2238 release_engine(engine
);
2239 BIO_free(bio_s_out
);
2241 BIO_free(bio_s_msg
);
2243 #ifdef CHARSET_EBCDIC
2244 BIO_meth_free(methods_ebcdic
);
2249 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2251 BIO_printf(bio
, "%4ld items in the session cache\n",
2252 SSL_CTX_sess_number(ssl_ctx
));
2253 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2254 SSL_CTX_sess_connect(ssl_ctx
));
2255 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2256 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2257 BIO_printf(bio
, "%4ld client connects that finished\n",
2258 SSL_CTX_sess_connect_good(ssl_ctx
));
2259 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2260 SSL_CTX_sess_accept(ssl_ctx
));
2261 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2262 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2263 BIO_printf(bio
, "%4ld server accepts that finished\n",
2264 SSL_CTX_sess_accept_good(ssl_ctx
));
2265 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2266 BIO_printf(bio
, "%4ld session cache misses\n",
2267 SSL_CTX_sess_misses(ssl_ctx
));
2268 BIO_printf(bio
, "%4ld session cache timeouts\n",
2269 SSL_CTX_sess_timeouts(ssl_ctx
));
2270 BIO_printf(bio
, "%4ld callback cache hits\n",
2271 SSL_CTX_sess_cb_hits(ssl_ctx
));
2272 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2273 SSL_CTX_sess_cache_full(ssl_ctx
),
2274 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2277 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
)
2286 struct timeval timeout
;
2287 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
2288 struct timeval
*timeoutp
;
2290 #ifndef OPENSSL_NO_DTLS
2291 # ifndef OPENSSL_NO_SCTP
2292 int isdtls
= (stype
== SOCK_DGRAM
|| prot
== IPPROTO_SCTP
);
2294 int isdtls
= (stype
== SOCK_DGRAM
);
2298 buf
= app_malloc(bufsize
, "server buffer");
2300 if (!BIO_socket_nbio(s
, 1))
2301 ERR_print_errors(bio_err
);
2303 BIO_printf(bio_err
, "Turned on non blocking io\n");
2312 if (s_tlsextdebug
) {
2313 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2314 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2318 && !SSL_set_session_id_context(con
, context
,
2319 strlen((char *)context
))) {
2320 BIO_printf(bio_err
, "Error setting session id context\n");
2325 if (!SSL_clear(con
)) {
2326 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2330 #ifndef OPENSSL_NO_DTLS
2332 # ifndef OPENSSL_NO_SCTP
2333 if (prot
== IPPROTO_SCTP
)
2334 sbio
= BIO_new_dgram_sctp(s
, BIO_NOCLOSE
);
2337 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2339 if (enable_timeouts
) {
2341 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2342 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2345 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2346 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2350 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2351 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2352 DTLS_get_link_min_mtu(con
));
2357 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2358 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2359 BIO_printf(bio_err
, "Failed to set MTU\n");
2365 /* want to do MTU discovery */
2366 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2368 # ifndef OPENSSL_NO_SCTP
2369 if (prot
!= IPPROTO_SCTP
)
2371 /* Turn on cookie exchange. Not necessary for SCTP */
2372 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2375 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2378 BIO_printf(bio_err
, "Unable to create BIO\n");
2379 ERR_print_errors(bio_err
);
2386 test
= BIO_new(BIO_f_nbio_test());
2387 sbio
= BIO_push(test
, sbio
);
2390 SSL_set_bio(con
, sbio
, sbio
);
2391 SSL_set_accept_state(con
);
2392 /* SSL_set_fd(con,s); */
2395 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2396 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2399 #ifndef OPENSSL_NO_SSL_TRACE
2401 SSL_set_msg_callback(con
, SSL_trace
);
2404 SSL_set_msg_callback(con
, msg_cb
);
2405 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2408 if (s_tlsextdebug
) {
2409 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2410 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2414 int write_header
= 1, edret
= SSL_READ_EARLY_DATA_ERROR
;
2417 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
2419 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
2420 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
2423 switch (SSL_get_error(con
, 0)) {
2424 case SSL_ERROR_WANT_WRITE
:
2425 case SSL_ERROR_WANT_ASYNC
:
2426 case SSL_ERROR_WANT_READ
:
2427 /* Just keep trying - busy waiting */
2430 BIO_printf(bio_err
, "Error reading early data\n");
2431 ERR_print_errors(bio_err
);
2435 if (readbytes
> 0) {
2437 BIO_printf(bio_s_out
, "Early data received:\n");
2440 raw_write_stdout(buf
, (unsigned int)readbytes
);
2441 (void)BIO_flush(bio_s_out
);
2445 if (SSL_get_early_data_status(con
) == SSL_EARLY_DATA_NOT_SENT
)
2446 BIO_printf(bio_s_out
, "No early data received\n");
2448 BIO_printf(bio_s_out
, "Early data was rejected\n");
2450 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2452 if (SSL_is_init_finished(con
))
2453 print_connection_info(con
);
2456 if (fileno_stdin() > s
)
2457 width
= fileno_stdin() + 1;
2461 int read_from_terminal
;
2462 int read_from_sslcon
;
2464 read_from_terminal
= 0;
2465 read_from_sslcon
= SSL_has_pending(con
)
2466 || (async
&& SSL_waiting_for_async(con
));
2468 if (!read_from_sslcon
) {
2470 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2471 openssl_fdset(fileno_stdin(), &readfds
);
2473 openssl_fdset(s
, &readfds
);
2475 * Note: under VMS with SOCKETSHR the second parameter is
2476 * currently of type (int *) whereas under other systems it is
2477 * (void *) if you don't have a cast it will choke the compiler:
2478 * if you do have a cast then you can either go for (int *) or
2481 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2483 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2484 * only on sockets. As a workaround we timeout the select every
2485 * second and check for any keypress. In a proper Windows
2486 * application we wouldn't do this because it is inefficient.
2489 timeout
.tv_usec
= 0;
2490 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &timeout
);
2491 if (has_stdin_waiting())
2492 read_from_terminal
= 1;
2493 if ((i
< 0) || (!i
&& !read_from_terminal
))
2496 if (SSL_is_dtls(con
) && DTLSv1_get_timeout(con
, &timeout
))
2497 timeoutp
= &timeout
;
2501 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2503 if ((SSL_is_dtls(con
)) && DTLSv1_handle_timeout(con
) > 0)
2504 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2508 if (FD_ISSET(fileno_stdin(), &readfds
))
2509 read_from_terminal
= 1;
2511 if (FD_ISSET(s
, &readfds
))
2512 read_from_sslcon
= 1;
2514 if (read_from_terminal
) {
2518 i
= raw_read_stdin(buf
, bufsize
/ 2);
2520 /* both loops are skipped when i <= 0 */
2521 for (j
= 0; j
< i
; j
++)
2524 for (j
= i
- 1; j
>= 0; j
--) {
2525 buf
[j
+ lf_num
] = buf
[j
];
2526 if (buf
[j
] == '\n') {
2529 buf
[j
+ lf_num
] = '\r';
2532 assert(lf_num
== 0);
2534 i
= raw_read_stdin(buf
, bufsize
);
2537 if (!s_quiet
&& !s_brief
) {
2538 if ((i
<= 0) || (buf
[0] == 'Q')) {
2539 BIO_printf(bio_s_out
, "DONE\n");
2540 (void)BIO_flush(bio_s_out
);
2542 close_accept_socket();
2546 if ((i
<= 0) || (buf
[0] == 'q')) {
2547 BIO_printf(bio_s_out
, "DONE\n");
2548 (void)BIO_flush(bio_s_out
);
2549 if (SSL_version(con
) != DTLS1_VERSION
)
2552 * close_accept_socket(); ret= -11;
2556 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2557 SSL_renegotiate(con
);
2558 i
= SSL_do_handshake(con
);
2559 printf("SSL_do_handshake -> %d\n", i
);
2563 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2565 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2567 SSL_renegotiate(con
);
2568 i
= SSL_do_handshake(con
);
2569 printf("SSL_do_handshake -> %d\n", i
);
2573 if ((buf
[0] == 'K' || buf
[0] == 'k')
2574 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2575 SSL_key_update(con
, buf
[0] == 'K' ?
2576 SSL_KEY_UPDATE_REQUESTED
2577 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2578 i
= SSL_do_handshake(con
);
2579 printf("SSL_do_handshake -> %d\n", i
);
2583 if (buf
[0] == 'c' && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2584 SSL_set_verify(con
, SSL_VERIFY_PEER
, NULL
);
2585 i
= SSL_verify_client_post_handshake(con
);
2587 printf("Failed to initiate request\n");
2588 ERR_print_errors(bio_err
);
2590 i
= SSL_do_handshake(con
);
2591 printf("SSL_do_handshake -> %d\n", i
);
2596 if (buf
[0] == 'P') {
2597 static const char str
[] = "Lets print some clear text\n";
2598 BIO_write(SSL_get_wbio(con
), str
, sizeof(str
) -1);
2600 if (buf
[0] == 'S') {
2601 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2604 #ifdef CHARSET_EBCDIC
2605 ebcdic2ascii(buf
, buf
, i
);
2609 /* should do a select for the write */
2612 if (++count
== 100) {
2614 SSL_renegotiate(con
);
2617 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2618 #ifndef OPENSSL_NO_SRP
2619 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2620 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2622 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2624 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2627 switch (SSL_get_error(con
, k
)) {
2628 case SSL_ERROR_NONE
:
2630 case SSL_ERROR_WANT_ASYNC
:
2631 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2632 (void)BIO_flush(bio_s_out
);
2633 wait_for_async(con
);
2635 case SSL_ERROR_WANT_WRITE
:
2636 case SSL_ERROR_WANT_READ
:
2637 case SSL_ERROR_WANT_X509_LOOKUP
:
2638 BIO_printf(bio_s_out
, "Write BLOCK\n");
2639 (void)BIO_flush(bio_s_out
);
2641 case SSL_ERROR_WANT_ASYNC_JOB
:
2643 * This shouldn't ever happen in s_server. Treat as an error
2645 case SSL_ERROR_SYSCALL
:
2647 BIO_printf(bio_s_out
, "ERROR\n");
2648 (void)BIO_flush(bio_s_out
);
2649 ERR_print_errors(bio_err
);
2653 case SSL_ERROR_ZERO_RETURN
:
2654 BIO_printf(bio_s_out
, "DONE\n");
2655 (void)BIO_flush(bio_s_out
);
2667 if (read_from_sslcon
) {
2669 * init_ssl_connection handles all async events itself so if we're
2670 * waiting for async then we shouldn't go back into
2671 * init_ssl_connection
2673 if ((!async
|| !SSL_waiting_for_async(con
))
2674 && !SSL_is_init_finished(con
)) {
2675 i
= init_ssl_connection(con
);
2680 } else if (i
== 0) {
2686 i
= SSL_read(con
, (char *)buf
, bufsize
);
2687 #ifndef OPENSSL_NO_SRP
2688 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2689 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2691 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2693 i
= SSL_read(con
, (char *)buf
, bufsize
);
2696 switch (SSL_get_error(con
, i
)) {
2697 case SSL_ERROR_NONE
:
2698 #ifdef CHARSET_EBCDIC
2699 ascii2ebcdic(buf
, buf
, i
);
2701 raw_write_stdout(buf
, (unsigned int)i
);
2702 (void)BIO_flush(bio_s_out
);
2703 if (SSL_has_pending(con
))
2706 case SSL_ERROR_WANT_ASYNC
:
2707 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2708 (void)BIO_flush(bio_s_out
);
2709 wait_for_async(con
);
2711 case SSL_ERROR_WANT_WRITE
:
2712 case SSL_ERROR_WANT_READ
:
2713 BIO_printf(bio_s_out
, "Read BLOCK\n");
2714 (void)BIO_flush(bio_s_out
);
2716 case SSL_ERROR_WANT_ASYNC_JOB
:
2718 * This shouldn't ever happen in s_server. Treat as an error
2720 case SSL_ERROR_SYSCALL
:
2722 BIO_printf(bio_s_out
, "ERROR\n");
2723 (void)BIO_flush(bio_s_out
);
2724 ERR_print_errors(bio_err
);
2727 case SSL_ERROR_ZERO_RETURN
:
2728 BIO_printf(bio_s_out
, "DONE\n");
2729 (void)BIO_flush(bio_s_out
);
2738 BIO_printf(bio_s_out
, "shutting down SSL\n");
2739 do_ssl_shutdown(con
);
2742 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2743 OPENSSL_clear_free(buf
, bufsize
);
2747 static void close_accept_socket(void)
2749 BIO_printf(bio_err
, "shutdown accept socket\n");
2750 if (accept_socket
>= 0) {
2751 BIO_closesocket(accept_socket
);
2755 static int is_retryable(SSL
*con
, int i
)
2757 int err
= SSL_get_error(con
, i
);
2759 /* If it's not a fatal error, it must be retryable */
2760 return (err
!= SSL_ERROR_SSL
)
2761 && (err
!= SSL_ERROR_SYSCALL
)
2762 && (err
!= SSL_ERROR_ZERO_RETURN
);
2765 static int init_ssl_connection(SSL
*con
)
2771 if (dtlslisten
|| stateless
) {
2772 BIO_ADDR
*client
= NULL
;
2775 if ((client
= BIO_ADDR_new()) == NULL
) {
2776 BIO_printf(bio_err
, "ERROR - memory\n");
2779 i
= DTLSv1_listen(con
, client
);
2781 i
= SSL_stateless(con
);
2788 wbio
= SSL_get_wbio(con
);
2790 BIO_get_fd(wbio
, &fd
);
2793 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2794 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2795 BIO_ADDR_free(client
);
2799 (void)BIO_ctrl_set_connected(wbio
, client
);
2800 BIO_ADDR_free(client
);
2805 i
= SSL_accept(con
);
2807 BIO_ADDR_free(client
);
2811 i
= SSL_accept(con
);
2812 if (immediate_reneg
)
2813 SSL_renegotiate(con
);
2816 retry
= is_retryable(con
, i
);
2817 #ifdef CERT_CB_TEST_RETRY
2820 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2821 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2823 "LOOKUP from certificate callback during accept\n");
2824 i
= SSL_accept(con
);
2826 retry
= is_retryable(con
, i
);
2831 #ifndef OPENSSL_NO_SRP
2833 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2834 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
2835 srp_callback_parm
.login
);
2837 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2839 i
= SSL_accept(con
);
2841 retry
= is_retryable(con
, i
);
2844 } while (i
< 0 && SSL_waiting_for_async(con
));
2848 if (((dtlslisten
|| stateless
) && i
== 0)
2849 || (!dtlslisten
&& !stateless
&& retry
)) {
2850 BIO_printf(bio_s_out
, "DELAY\n");
2854 BIO_printf(bio_err
, "ERROR\n");
2856 verify_err
= SSL_get_verify_result(con
);
2857 if (verify_err
!= X509_V_OK
) {
2858 BIO_printf(bio_err
, "verify error:%s\n",
2859 X509_verify_cert_error_string(verify_err
));
2861 /* Always print any error messages */
2862 ERR_print_errors(bio_err
);
2866 print_connection_info(con
);
2870 static void print_connection_info(SSL
*con
)
2875 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2876 const unsigned char *next_proto_neg
;
2877 unsigned next_proto_neg_len
;
2879 unsigned char *exportedkeymat
;
2883 print_ssl_summary(con
);
2885 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
2887 peer
= SSL_get0_peer_certificate(con
);
2889 BIO_printf(bio_s_out
, "Client certificate\n");
2890 PEM_write_bio_X509(bio_s_out
, peer
);
2891 dump_cert_text(bio_s_out
, peer
);
2895 if (SSL_get_shared_ciphers(con
, buf
, sizeof(buf
)) != NULL
)
2896 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
2897 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
2898 ssl_print_sigalgs(bio_s_out
, con
);
2899 #ifndef OPENSSL_NO_EC
2900 ssl_print_point_formats(bio_s_out
, con
);
2901 ssl_print_groups(bio_s_out
, con
, 0);
2903 print_ca_names(bio_s_out
, con
);
2904 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
2906 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2907 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
2908 if (next_proto_neg
) {
2909 BIO_printf(bio_s_out
, "NEXTPROTO is ");
2910 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
2911 BIO_printf(bio_s_out
, "\n");
2914 #ifndef OPENSSL_NO_SRTP
2916 SRTP_PROTECTION_PROFILE
*srtp_profile
2917 = SSL_get_selected_srtp_profile(con
);
2920 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
2921 srtp_profile
->name
);
2924 if (SSL_session_reused(con
))
2925 BIO_printf(bio_s_out
, "Reused session-id\n");
2926 BIO_printf(bio_s_out
, "Secure Renegotiation IS%s supported\n",
2927 SSL_get_secure_renegotiation_support(con
) ? "" : " NOT");
2928 if ((SSL_get_options(con
) & SSL_OP_NO_RENEGOTIATION
))
2929 BIO_printf(bio_s_out
, "Renegotiation is DISABLED\n");
2931 if (keymatexportlabel
!= NULL
) {
2932 BIO_printf(bio_s_out
, "Keying material exporter:\n");
2933 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
2934 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
2935 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
2936 if (!SSL_export_keying_material(con
, exportedkeymat
,
2939 strlen(keymatexportlabel
),
2941 BIO_printf(bio_s_out
, " Error\n");
2943 BIO_printf(bio_s_out
, " Keying material: ");
2944 for (i
= 0; i
< keymatexportlen
; i
++)
2945 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
2946 BIO_printf(bio_s_out
, "\n");
2948 OPENSSL_free(exportedkeymat
);
2950 #ifndef OPENSSL_NO_KTLS
2951 if (BIO_get_ktls_send(SSL_get_wbio(con
)))
2952 BIO_printf(bio_err
, "Using Kernel TLS for sending\n");
2953 if (BIO_get_ktls_recv(SSL_get_rbio(con
)))
2954 BIO_printf(bio_err
, "Using Kernel TLS for receiving\n");
2957 (void)BIO_flush(bio_s_out
);
2960 static int www_body(int s
, int stype
, int prot
, unsigned char *context
)
2966 const SSL_CIPHER
*c
;
2967 BIO
*io
, *ssl_bio
, *sbio
;
2969 int total_bytes
= 0;
2975 /* Set width for a select call if needed */
2978 buf
= app_malloc(bufsize
, "server www buffer");
2979 io
= BIO_new(BIO_f_buffer());
2980 ssl_bio
= BIO_new(BIO_f_ssl());
2981 if ((io
== NULL
) || (ssl_bio
== NULL
))
2985 if (!BIO_socket_nbio(s
, 1))
2986 ERR_print_errors(bio_err
);
2988 BIO_printf(bio_err
, "Turned on non blocking io\n");
2991 /* lets make the output buffer a reasonable size */
2992 if (!BIO_set_write_buffer_size(io
, bufsize
))
2995 if ((con
= SSL_new(ctx
)) == NULL
)
2998 if (s_tlsextdebug
) {
2999 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3000 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3004 && !SSL_set_session_id_context(con
, context
,
3005 strlen((char *)context
))) {
3010 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3014 test
= BIO_new(BIO_f_nbio_test());
3015 sbio
= BIO_push(test
, sbio
);
3017 SSL_set_bio(con
, sbio
, sbio
);
3018 SSL_set_accept_state(con
);
3020 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3021 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3022 BIO_push(io
, ssl_bio
);
3023 #ifdef CHARSET_EBCDIC
3024 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
3028 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
3029 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3032 #ifndef OPENSSL_NO_SSL_TRACE
3034 SSL_set_msg_callback(con
, SSL_trace
);
3037 SSL_set_msg_callback(con
, msg_cb
);
3038 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3042 i
= BIO_gets(io
, buf
, bufsize
- 1);
3043 if (i
< 0) { /* error */
3044 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
3046 ERR_print_errors(bio_err
);
3049 BIO_printf(bio_s_out
, "read R BLOCK\n");
3050 #ifndef OPENSSL_NO_SRP
3051 if (BIO_should_io_special(io
)
3052 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3053 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3055 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3063 } else if (i
== 0) { /* end of input */
3068 /* else we have data */
3069 if (((www
== 1) && (strncmp("GET ", buf
, 4) == 0)) ||
3070 ((www
== 2) && (strncmp("GET /stats ", buf
, 11) == 0))) {
3073 STACK_OF(SSL_CIPHER
) *sk
;
3074 static const char *space
= " ";
3076 if (www
== 1 && strncmp("GET /reneg", buf
, 10) == 0) {
3077 if (strncmp("GET /renegcert", buf
, 14) == 0)
3079 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
3081 i
= SSL_renegotiate(con
);
3082 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
3083 /* Send the HelloRequest */
3084 i
= SSL_do_handshake(con
);
3086 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
3087 SSL_get_error(con
, i
));
3088 ERR_print_errors(bio_err
);
3091 /* Wait for a ClientHello to come back */
3093 openssl_fdset(s
, &readfds
);
3094 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
3095 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
3096 BIO_printf(bio_s_out
,
3097 "Error waiting for client response\n");
3098 ERR_print_errors(bio_err
);
3102 * We're not actually expecting any data here and we ignore
3103 * any that is sent. This is just to force the handshake that
3104 * we're expecting to come from the client. If they haven't
3105 * sent one there's not much we can do.
3107 BIO_gets(io
, buf
, bufsize
- 1);
3111 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3112 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
3113 BIO_puts(io
, "<pre>\n");
3114 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
3116 for (i
= 0; i
< local_argc
; i
++) {
3118 for (myp
= local_argv
[i
]; *myp
; myp
++)
3121 BIO_puts(io
, "<");
3124 BIO_puts(io
, ">");
3127 BIO_puts(io
, "&");
3130 BIO_write(io
, myp
, 1);
3133 BIO_write(io
, " ", 1);
3138 "Secure Renegotiation IS%s supported\n",
3139 SSL_get_secure_renegotiation_support(con
) ?
3143 * The following is evil and should not really be done
3145 BIO_printf(io
, "Ciphers supported in s_server binary\n");
3146 sk
= SSL_get_ciphers(con
);
3147 j
= sk_SSL_CIPHER_num(sk
);
3148 for (i
= 0; i
< j
; i
++) {
3149 c
= sk_SSL_CIPHER_value(sk
, i
);
3150 BIO_printf(io
, "%-11s:%-25s ",
3151 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3152 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
3156 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
3159 "---\nCiphers common between both SSL end points:\n");
3163 BIO_write(io
, space
, 26 - j
);
3166 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
3168 BIO_write(io
, p
, 1);
3175 ssl_print_sigalgs(io
, con
);
3176 #ifndef OPENSSL_NO_EC
3177 ssl_print_groups(io
, con
, 0);
3179 print_ca_names(io
, con
);
3180 BIO_printf(io
, (SSL_session_reused(con
)
3181 ? "---\nReused, " : "---\nNew, "));
3182 c
= SSL_get_current_cipher(con
);
3183 BIO_printf(io
, "%s, Cipher is %s\n",
3184 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3185 SSL_SESSION_print(io
, SSL_get_session(con
));
3186 BIO_printf(io
, "---\n");
3187 print_stats(io
, SSL_get_SSL_CTX(con
));
3188 BIO_printf(io
, "---\n");
3189 peer
= SSL_get0_peer_certificate(con
);
3191 BIO_printf(io
, "Client certificate\n");
3192 X509_print(io
, peer
);
3193 PEM_write_bio_X509(io
, peer
);
3196 BIO_puts(io
, "no client certificate available\n");
3198 BIO_puts(io
, "</pre></BODY></HTML>\r\n\r\n");
3200 } else if ((www
== 2 || www
== 3)
3201 && (strncmp("GET /", buf
, 5) == 0)) {
3204 static const char *text
=
3205 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3211 for (e
= p
; *e
!= '\0'; e
++) {
3216 /* Windows drive. We treat this the same way as ".." */
3223 dot
= (e
[0] == '.') ? 2 : 0;
3226 dot
= (e
[0] == '.') ? 3 : 0;
3229 dot
= (e
[0] == '/' || e
[0] == '\\') ? -1 : 0;
3233 dot
= (e
[0] == '/' || e
[0] == '\\') ? 1 : 0;
3235 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3240 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3247 BIO_printf(io
, "'%s' contains '..' or ':'\r\n", p
);
3251 if (*p
== '/' || *p
== '\\') {
3253 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3257 /* if a directory, do the index thang */
3258 if (app_isdir(p
) > 0) {
3260 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3264 opmode
= (http_server_binmode
== 1) ? "rb" : "r";
3265 if ((file
= BIO_new_file(p
, opmode
)) == NULL
) {
3267 BIO_printf(io
, "Error opening '%s' mode='%s'\r\n", p
, opmode
);
3268 ERR_print_errors(io
);
3273 BIO_printf(bio_err
, "FILE:%s\n", p
);
3277 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3278 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3279 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3281 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3284 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3287 #ifndef OPENSSL_NO_KTLS
3295 BIO_get_fp(file
, &fp
);
3297 if (fstat(fd
, &st
) < 0) {
3298 BIO_printf(io
, "Error fstat '%s'\r\n", p
);
3299 ERR_print_errors(io
);
3303 filesize
= st
.st_size
;
3304 if (((int)BIO_flush(io
)) < 0)
3308 i
= SSL_sendfile(con
, fd
, offset
, filesize
, 0);
3310 BIO_printf(io
, "Error SSL_sendfile '%s'\r\n", p
);
3311 ERR_print_errors(io
);
3318 if (filesize
<= 0) {
3320 BIO_printf(bio_err
, "KTLS SENDFILE '%s' OK\n", p
);
3329 i
= BIO_read(file
, buf
, bufsize
);
3335 BIO_printf(bio_err
, "%d\n", i
);
3336 if (total_bytes
> 3 * 1024) {
3338 BIO_printf(bio_err
, "RENEGOTIATE\n");
3339 SSL_renegotiate(con
);
3343 for (j
= 0; j
< i
;) {
3347 SSL_renegotiate(con
);
3349 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3351 if (!BIO_should_retry(io
)
3352 && !SSL_waiting_for_async(con
)) {
3355 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3370 i
= (int)BIO_flush(io
);
3372 if (!BIO_should_retry(io
))
3378 /* make sure we re-use sessions */
3379 do_ssl_shutdown(con
);
3387 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
)
3393 BIO
*io
, *ssl_bio
, *sbio
;
3395 buf
= app_malloc(bufsize
, "server rev buffer");
3396 io
= BIO_new(BIO_f_buffer());
3397 ssl_bio
= BIO_new(BIO_f_ssl());
3398 if ((io
== NULL
) || (ssl_bio
== NULL
))
3401 /* lets make the output buffer a reasonable size */
3402 if (!BIO_set_write_buffer_size(io
, bufsize
))
3405 if ((con
= SSL_new(ctx
)) == NULL
)
3408 if (s_tlsextdebug
) {
3409 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3410 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3413 && !SSL_set_session_id_context(con
, context
,
3414 strlen((char *)context
))) {
3416 ERR_print_errors(bio_err
);
3420 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3421 SSL_set_bio(con
, sbio
, sbio
);
3422 SSL_set_accept_state(con
);
3424 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3425 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3426 BIO_push(io
, ssl_bio
);
3427 #ifdef CHARSET_EBCDIC
3428 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
3432 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
3433 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3436 #ifndef OPENSSL_NO_SSL_TRACE
3438 SSL_set_msg_callback(con
, SSL_trace
);
3441 SSL_set_msg_callback(con
, msg_cb
);
3442 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3446 i
= BIO_do_handshake(io
);
3449 if (!BIO_should_retry(io
)) {
3450 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3451 ERR_print_errors(bio_err
);
3454 #ifndef OPENSSL_NO_SRP
3455 if (BIO_should_io_special(io
)
3456 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3457 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3459 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3465 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3466 print_ssl_summary(con
);
3469 i
= BIO_gets(io
, buf
, bufsize
- 1);
3470 if (i
< 0) { /* error */
3471 if (!BIO_should_retry(io
)) {
3473 ERR_print_errors(bio_err
);
3476 BIO_printf(bio_s_out
, "read R BLOCK\n");
3477 #ifndef OPENSSL_NO_SRP
3478 if (BIO_should_io_special(io
)
3479 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3480 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3482 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3490 } else if (i
== 0) { /* end of input */
3492 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3495 char *p
= buf
+ i
- 1;
3496 while (i
&& (*p
== '\n' || *p
== '\r')) {
3500 if (!s_ign_eof
&& (i
== 5) && (strncmp(buf
, "CLOSE", 5) == 0)) {
3502 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3505 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3507 BIO_write(io
, buf
, i
+ 1);
3512 if (!BIO_should_retry(io
))
3518 /* make sure we re-use sessions */
3519 do_ssl_shutdown(con
);
3528 #define MAX_SESSION_ID_ATTEMPTS 10
3529 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
3530 unsigned int *id_len
)
3532 unsigned int count
= 0;
3533 unsigned int session_id_prefix_len
= strlen(session_id_prefix
);
3536 if (RAND_bytes(id
, *id_len
) <= 0)
3539 * Prefix the session_id with the required prefix. NB: If our prefix
3540 * is too long, clip it - but there will be worse effects anyway, eg.
3541 * the server could only possibly create 1 session ID (ie. the
3542 * prefix!) so all future session negotiations will fail due to
3545 memcpy(id
, session_id_prefix
,
3546 (session_id_prefix_len
< *id_len
) ?
3547 session_id_prefix_len
: *id_len
);
3549 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3550 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3551 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3557 * By default s_server uses an in-memory cache which caches SSL_SESSION
3558 * structures without any serialization. This hides some bugs which only
3559 * become apparent in deployed servers. By implementing a basic external
3560 * session cache some issues can be debugged using s_server.
3563 typedef struct simple_ssl_session_st
{
3568 struct simple_ssl_session_st
*next
;
3569 } simple_ssl_session
;
3571 static simple_ssl_session
*first
= NULL
;
3573 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3575 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3578 SSL_SESSION_get_id(session
, &sess
->idlen
);
3579 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3580 if (sess
->derlen
< 0) {
3581 BIO_printf(bio_err
, "Error encoding session\n");
3586 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3587 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3589 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3590 OPENSSL_free(sess
->id
);
3591 OPENSSL_free(sess
->der
);
3597 /* Assume it still works. */
3598 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3599 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3600 OPENSSL_free(sess
->id
);
3601 OPENSSL_free(sess
->der
);
3608 BIO_printf(bio_err
, "New session added to external cache\n");
3612 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3615 simple_ssl_session
*sess
;
3617 for (sess
= first
; sess
; sess
= sess
->next
) {
3618 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3619 const unsigned char *p
= sess
->der
;
3620 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3621 return d2i_SSL_SESSION(NULL
, &p
, sess
->derlen
);
3624 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3628 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3630 simple_ssl_session
*sess
, *prev
= NULL
;
3631 const unsigned char *id
;
3633 id
= SSL_SESSION_get_id(session
, &idlen
);
3634 for (sess
= first
; sess
; sess
= sess
->next
) {
3635 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3637 prev
->next
= sess
->next
;
3640 OPENSSL_free(sess
->id
);
3641 OPENSSL_free(sess
->der
);
3649 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3651 SSL_CTX_set_session_cache_mode(sctx
,
3652 SSL_SESS_CACHE_NO_INTERNAL
|
3653 SSL_SESS_CACHE_SERVER
);
3654 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3655 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3656 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3659 static void free_sessions(void)
3661 simple_ssl_session
*sess
, *tsess
;
3662 for (sess
= first
; sess
;) {
3663 OPENSSL_free(sess
->id
);
3664 OPENSSL_free(sess
->der
);
3667 OPENSSL_free(tsess
);
3672 #endif /* OPENSSL_NO_SOCK */