2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * ECC cipher suite support in OpenSSL originally developed by
13 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
15 /* ====================================================================
16 * Copyright 2005 Nokia. All rights reserved.
18 * The portions of the attached software ("Contribution") is developed by
19 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
22 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
23 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
24 * support (see RFC 4279) to OpenSSL.
26 * No patent licenses or other rights except those expressly stated in
27 * the OpenSSL open source license shall be deemed granted or received
28 * expressly, by implication, estoppel, or otherwise.
30 * No assurances are provided by Nokia that the Contribution does not
31 * infringe the patent or other intellectual property rights of any third
32 * party or that the license provides you with all the necessary rights
33 * to make use of the Contribution.
35 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
36 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
37 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
38 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
47 /* Included before async.h to avoid some warnings */
51 #include <openssl/e_os2.h>
52 #include <openssl/async.h>
53 #include <openssl/ssl.h>
55 #ifndef OPENSSL_NO_SOCK
58 * With IPv6, it looks like Digital has mixed up the proper order of
59 * recursive header file inclusion, resulting in the compiler complaining
60 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
61 * needed to have fileno() declared correctly... So let's define u_int
63 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
65 typedef unsigned int u_int
;
68 #include <openssl/lhash.h>
69 #include <openssl/bn.h>
72 #include <openssl/err.h>
73 #include <openssl/pem.h>
74 #include <openssl/x509.h>
75 #include <openssl/ssl.h>
76 #include <openssl/rand.h>
77 #include <openssl/ocsp.h>
79 # include <openssl/dh.h>
81 #ifndef OPENSSL_NO_RSA
82 # include <openssl/rsa.h>
84 #ifndef OPENSSL_NO_SRP
85 # include <openssl/srp.h>
90 #include <openssl/ebcdic.h>
93 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
);
94 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
);
95 static int www_body(int s
, int stype
, int prot
, unsigned char *context
);
96 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
);
97 static void close_accept_socket(void);
98 static int init_ssl_connection(SSL
*s
);
99 static void print_stats(BIO
*bp
, SSL_CTX
*ctx
);
100 static int generate_session_id(const SSL
*ssl
, unsigned char *id
,
101 unsigned int *id_len
);
102 static void init_session_cache_ctx(SSL_CTX
*sctx
);
103 static void free_sessions(void);
104 #ifndef OPENSSL_NO_DH
105 static DH
*load_dh_param(const char *dhfile
);
107 static void print_connection_info(SSL
*con
);
109 static const int bufsize
= 16 * 1024;
110 static int accept_socket
= -1;
112 #define TEST_CERT "server.pem"
113 #define TEST_CERT2 "server2.pem"
115 static int s_nbio
= 0;
116 static int s_nbio_test
= 0;
117 static int s_crlf
= 0;
118 static SSL_CTX
*ctx
= NULL
;
119 static SSL_CTX
*ctx2
= NULL
;
122 static BIO
*bio_s_out
= NULL
;
123 static BIO
*bio_s_msg
= NULL
;
124 static int s_debug
= 0;
125 static int s_tlsextdebug
= 0;
126 static int s_msg
= 0;
127 static int s_quiet
= 0;
128 static int s_ign_eof
= 0;
129 static int s_brief
= 0;
131 static char *keymatexportlabel
= NULL
;
132 static int keymatexportlen
= 20;
134 static int async
= 0;
136 static const char *session_id_prefix
= NULL
;
138 #ifndef OPENSSL_NO_DTLS
139 static int enable_timeouts
= 0;
140 static long socket_mtu
;
144 * We define this but make it always be 0 in no-dtls builds to simplify the
147 static int dtlslisten
= 0;
149 static int early_data
= 0;
151 #ifndef OPENSSL_NO_PSK
152 static const char psk_identity
[] = "Client_identity";
153 char *psk_key
= NULL
; /* by default PSK is not used */
155 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
157 unsigned int max_psk_len
)
163 BIO_printf(bio_s_out
, "psk_server_cb\n");
165 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
169 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
170 (int)strlen(identity
), identity
);
172 /* here we could lookup the given identity e.g. from a database */
173 if (strcmp(identity
, psk_identity
) != 0) {
174 BIO_printf(bio_s_out
, "PSK error: client identity not found"
175 " (got '%s' expected '%s')\n", identity
, psk_identity
);
179 BIO_printf(bio_s_out
, "PSK client identity found\n");
181 /* convert the PSK key to binary */
182 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
184 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
188 if (key_len
> (int)max_psk_len
) {
190 "psk buffer of callback is too small (%d) for key (%ld)\n",
191 max_psk_len
, key_len
);
196 memcpy(psk
, key
, key_len
);
200 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
204 BIO_printf(bio_err
, "Error in PSK server callback\n");
205 (void)BIO_flush(bio_err
);
206 (void)BIO_flush(bio_s_out
);
211 #ifndef OPENSSL_NO_SRP
212 /* This is a context that we pass to callbacks */
213 typedef struct srpsrvparm_st
{
220 * This callback pretends to require some asynchronous logic in order to
221 * obtain a verifier. When the callback is called for a new connection we
222 * return with a negative value. This will provoke the accept etc to return
223 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
224 * (which would normally occur after a worker has finished) and we set the
227 static int ssl_srp_server_param_cb(SSL
*s
, int *ad
, void *arg
)
229 srpsrvparm
*p
= (srpsrvparm
*) arg
;
230 int ret
= SSL3_AL_FATAL
;
232 if (p
->login
== NULL
&& p
->user
== NULL
) {
233 p
->login
= SSL_get_srp_username(s
);
234 BIO_printf(bio_err
, "SRP username = \"%s\"\n", p
->login
);
238 if (p
->user
== NULL
) {
239 BIO_printf(bio_err
, "User %s doesn't exist\n", p
->login
);
243 if (SSL_set_srp_server_param
244 (s
, p
->user
->N
, p
->user
->g
, p
->user
->s
, p
->user
->v
,
245 p
->user
->info
) < 0) {
246 *ad
= SSL_AD_INTERNAL_ERROR
;
250 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
251 p
->login
, p
->user
->info
);
252 ret
= SSL_ERROR_NONE
;
255 SRP_user_pwd_free(p
->user
);
263 static int local_argc
= 0;
264 static char **local_argv
;
266 #ifdef CHARSET_EBCDIC
267 static int ebcdic_new(BIO
*bi
);
268 static int ebcdic_free(BIO
*a
);
269 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
270 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
271 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
272 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
273 static int ebcdic_puts(BIO
*bp
, const char *str
);
275 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
276 static BIO_METHOD
*methods_ebcdic
= NULL
;
278 /* This struct is "unwarranted chumminess with the compiler." */
284 static const BIO_METHOD
*BIO_f_ebcdic_filter()
286 if (methods_ebcdic
== NULL
) {
287 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
288 "EBCDIC/ASCII filter");
289 if (methods_ebcdic
== NULL
290 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
291 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
292 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
293 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
294 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
295 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
296 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
299 return methods_ebcdic
;
302 static int ebcdic_new(BIO
*bi
)
304 EBCDIC_OUTBUFF
*wbuf
;
306 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
307 wbuf
->alloced
= 1024;
308 wbuf
->buff
[0] = '\0';
310 BIO_set_data(bi
, wbuf
);
315 static int ebcdic_free(BIO
*a
)
317 EBCDIC_OUTBUFF
*wbuf
;
321 wbuf
= BIO_get_data(a
);
323 BIO_set_data(a
, NULL
);
329 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
332 BIO
*next
= BIO_next(b
);
334 if (out
== NULL
|| outl
== 0)
339 ret
= BIO_read(next
, out
, outl
);
341 ascii2ebcdic(out
, out
, ret
);
345 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
347 EBCDIC_OUTBUFF
*wbuf
;
348 BIO
*next
= BIO_next(b
);
352 if ((in
== NULL
) || (inl
<= 0))
357 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
359 if (inl
> (num
= wbuf
->alloced
)) {
360 num
= num
+ num
; /* double the size */
364 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
367 wbuf
->buff
[0] = '\0';
369 BIO_set_data(b
, wbuf
);
372 ebcdic2ascii(wbuf
->buff
, in
, inl
);
374 ret
= BIO_write(next
, wbuf
->buff
, inl
);
379 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
382 BIO
*next
= BIO_next(b
);
391 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
397 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
400 BIO
*next
= BIO_next(bp
);
404 /* return(BIO_gets(bp->next_bio,buf,size));*/
405 for (i
= 0; i
< size
- 1; ++i
) {
406 ret
= ebcdic_read(bp
, &buf
[i
], 1);
409 else if (buf
[i
] == '\n') {
416 return (ret
< 0 && i
== 0) ? ret
: i
;
419 static int ebcdic_puts(BIO
*bp
, const char *str
)
421 if (BIO_next(bp
) == NULL
)
423 return ebcdic_write(bp
, str
, strlen(str
));
427 /* This is a context that we pass to callbacks */
428 typedef struct tlsextctx_st
{
434 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
436 tlsextctx
*p
= (tlsextctx
*) arg
;
437 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
438 if (servername
&& p
->biodebug
)
439 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"%s\"\n",
443 return SSL_TLSEXT_ERR_NOACK
;
446 if (strcasecmp(servername
, p
->servername
))
447 return p
->extension_error
;
449 BIO_printf(p
->biodebug
, "Switching server context.\n");
450 SSL_set_SSL_CTX(s
, ctx2
);
453 return SSL_TLSEXT_ERR_OK
;
456 /* Structure passed to cert status callback */
457 typedef struct tlsextstatusctx_st
{
459 /* File to load OCSP Response from (or NULL if no file) */
461 /* Default responder to use */
462 char *host
, *path
, *port
;
467 static tlsextstatusctx tlscstatp
= { -1 };
469 #ifndef OPENSSL_NO_OCSP
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".
478 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
479 OCSP_RESPONSE
**resp
)
481 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
483 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
485 X509_STORE_CTX
*inctx
= NULL
;
487 OCSP_REQUEST
*req
= NULL
;
488 OCSP_CERTID
*id
= NULL
;
489 STACK_OF(X509_EXTENSION
) *exts
;
490 int ret
= SSL_TLSEXT_ERR_NOACK
;
493 /* Build up OCSP query from server certificate */
494 x
= SSL_get_certificate(s
);
495 aia
= X509_get1_ocsp(x
);
497 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia
, 0),
498 &host
, &port
, &path
, &use_ssl
)) {
499 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
503 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
504 sk_OPENSSL_STRING_value(aia
, 0));
508 "cert_status: no AIA and no default responder URL\n");
514 use_ssl
= srctx
->use_ssl
;
517 inctx
= X509_STORE_CTX_new();
520 if (!X509_STORE_CTX_init(inctx
,
521 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s
)),
524 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
,
525 X509_get_issuer_name(x
));
527 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
530 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
531 X509_OBJECT_free(obj
);
534 req
= OCSP_REQUEST_new();
537 if (!OCSP_request_add0_id(req
, id
))
540 /* Add any extensions to the request */
541 SSL_get_tlsext_status_exts(s
, &exts
);
542 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
543 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
544 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
547 *resp
= process_responder(req
, host
, path
, port
, use_ssl
, NULL
,
550 BIO_puts(bio_err
, "cert_status: error querying responder\n");
554 ret
= SSL_TLSEXT_ERR_OK
;
558 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
561 * If we parsed aia we need to free; otherwise they were copied and we
568 X509_email_free(aia
);
570 OCSP_CERTID_free(id
);
571 OCSP_REQUEST_free(req
);
572 X509_STORE_CTX_free(inctx
);
577 * Certificate Status callback. This is called when a client includes a
578 * certificate status request extension. The response is either obtained from a
579 * file, or from an OCSP responder.
581 static int cert_status_cb(SSL
*s
, void *arg
)
583 tlsextstatusctx
*srctx
= arg
;
584 OCSP_RESPONSE
*resp
= NULL
;
585 unsigned char *rspder
= NULL
;
587 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
590 BIO_puts(bio_err
, "cert_status: callback called\n");
592 if (srctx
->respin
!= NULL
) {
593 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
594 if (derbio
== NULL
) {
595 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
598 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
601 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
605 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
606 if (ret
!= SSL_TLSEXT_ERR_OK
)
610 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
614 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
615 if (srctx
->verbose
) {
616 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
617 OCSP_RESPONSE_print(bio_err
, resp
, 2);
620 ret
= SSL_TLSEXT_ERR_OK
;
623 if (ret
!= SSL_TLSEXT_ERR_OK
)
624 ERR_print_errors(bio_err
);
626 OCSP_RESPONSE_free(resp
);
632 #ifndef OPENSSL_NO_NEXTPROTONEG
633 /* This is the context that we pass to next_proto_cb */
634 typedef struct tlsextnextprotoctx_st
{
637 } tlsextnextprotoctx
;
639 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
640 unsigned int *len
, void *arg
)
642 tlsextnextprotoctx
*next_proto
= arg
;
644 *data
= next_proto
->data
;
645 *len
= next_proto
->len
;
647 return SSL_TLSEXT_ERR_OK
;
649 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
651 /* This the context that we pass to alpn_cb */
652 typedef struct tlsextalpnctx_st
{
657 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
658 const unsigned char *in
, unsigned int inlen
, void *arg
)
660 tlsextalpnctx
*alpn_ctx
= arg
;
663 /* We can assume that |in| is syntactically valid. */
665 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
666 for (i
= 0; i
< inlen
;) {
668 BIO_write(bio_s_out
, ", ", 2);
669 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
672 BIO_write(bio_s_out
, "\n", 1);
675 if (SSL_select_next_proto
676 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
677 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
678 return SSL_TLSEXT_ERR_NOACK
;
682 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
683 BIO_write(bio_s_out
, *out
, *outlen
);
684 BIO_write(bio_s_out
, "\n", 1);
687 return SSL_TLSEXT_ERR_OK
;
690 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
692 /* disable resumption for sessions with forward secure ciphers */
693 return is_forward_secure
;
696 #ifndef OPENSSL_NO_SRP
697 static srpsrvparm srp_callback_parm
;
699 #ifndef OPENSSL_NO_SRTP
700 static char *srtp_profiles
= NULL
;
703 typedef enum OPTION_choice
{
704 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ENGINE
,
705 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
706 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
707 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
708 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
709 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
710 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
711 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
712 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
713 OPT_VERIFYCAFILE
, OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
714 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
715 OPT_STATUS_TIMEOUT
, OPT_STATUS_URL
, OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
716 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
717 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
718 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_HINT
, OPT_PSK
, OPT_SRPVFILE
,
719 OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
, OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
,
721 OPT_MAX_SEND_FRAG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
722 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
723 OPT_DTLS1_2
, OPT_SCTP
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
,
724 OPT_ID_PREFIX
, OPT_RAND
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
725 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
,
726 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
727 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_EARLY_DATA
,
733 const OPTIONS s_server_options
[] = {
734 {"help", OPT_HELP
, '-', "Display this summary"},
735 {"port", OPT_PORT
, 'p',
736 "TCP/IP port to listen on for connections (default is " PORT
")"},
737 {"accept", OPT_ACCEPT
, 's',
738 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
740 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
742 {"4", OPT_4
, '-', "Use IPv4 only"},
743 {"6", OPT_6
, '-', "Use IPv6 only"},
745 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
747 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
748 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
749 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
750 "Turn on peer certificate verification, must have a cert"},
751 {"cert", OPT_CERT
, '<', "Certificate file to use; default is " TEST_CERT
},
752 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
753 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
754 {"serverinfo", OPT_SERVERINFO
, 's',
755 "PEM serverinfo file for certificate"},
756 {"certform", OPT_CERTFORM
, 'F',
757 "Certificate format (PEM or DER) PEM default"},
758 {"key", OPT_KEY
, '<',
759 "Private Key if not in -cert; default is " TEST_CERT
},
760 {"keyform", OPT_KEYFORM
, 'f',
761 "Key format (PEM, DER or ENGINE) PEM default"},
762 {"pass", OPT_PASS
, 's', "Private key file pass phrase source"},
763 {"dcert", OPT_DCERT
, '<',
764 "Second certificate file to use (usually for DSA)"},
765 {"dcertform", OPT_DCERTFORM
, 'F',
766 "Second certificate format (PEM or DER) PEM default"},
767 {"dkey", OPT_DKEY
, '<',
768 "Second private key file to use (usually for DSA)"},
769 {"dkeyform", OPT_DKEYFORM
, 'F',
770 "Second key format (PEM, DER or ENGINE) PEM default"},
771 {"dpass", OPT_DPASS
, 's', "Second private key file pass phrase source"},
772 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
773 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
774 {"debug", OPT_DEBUG
, '-', "Print more output"},
775 {"msg", OPT_MSG
, '-', "Show protocol messages"},
776 {"msgfile", OPT_MSGFILE
, '>',
777 "File to send output of -msg or -trace, instead of stdout"},
778 {"state", OPT_STATE
, '-', "Print the SSL states"},
779 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
780 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
781 {"no-CAfile", OPT_NOCAFILE
, '-',
782 "Do not load the default certificates file"},
783 {"no-CApath", OPT_NOCAPATH
, '-',
784 "Do not load certificates from the default certificates directory"},
785 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
786 {"quiet", OPT_QUIET
, '-', "No server output"},
787 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
788 "Disable caching and tickets if ephemeral (EC)DH is used"},
789 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
790 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
791 {"servername", OPT_SERVERNAME
, 's',
792 "Servername for HostName TLS extension"},
793 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
794 "mismatch send fatal alert (default warning alert)"},
795 {"cert2", OPT_CERT2
, '<',
796 "Certificate file to use for servername; default is" TEST_CERT2
},
797 {"key2", OPT_KEY2
, '<',
798 "-Private Key file to use for servername if not in -cert2"},
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 {"rand", OPT_RAND
, 's',
805 "Load the file(s) into the random number generator"},
806 {"keymatexport", OPT_KEYMATEXPORT
, 's',
807 "Export keying material using label"},
808 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
809 "Export len bytes of keying material (default 20)"},
810 {"CRL", OPT_CRL
, '<', "CRL file to use"},
811 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
812 "Download CRL from distribution points"},
813 {"cert_chain", OPT_CERT_CHAIN
, '<',
814 "certificate chain file in PEM format"},
815 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
816 "second certificate chain file in PEM format"},
817 {"chainCApath", OPT_CHAINCAPATH
, '/',
818 "use dir as certificate store path to build CA certificate chain"},
819 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
820 "use dir as certificate store path to verify CA certificate"},
821 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
822 {"ext_cache", OPT_EXT_CACHE
, '-',
823 "Disable internal cache, setup and use external cache"},
824 {"CRLform", OPT_CRLFORM
, 'F', "CRL format (PEM or DER) PEM is default"},
825 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
826 "Close connection on verification error"},
827 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
828 "No verify output except verify errors"},
829 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build certificate chain"},
830 {"chainCAfile", OPT_CHAINCAFILE
, '<',
831 "CA file for certificate chain (PEM format)"},
832 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
833 "CA file for certificate verification (PEM format)"},
834 {"ign_eof", OPT_IGN_EOF
, '-', "ignore input eof (default when -quiet)"},
835 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input eof"},
836 #ifndef OPENSSL_NO_OCSP
837 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
838 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
839 "Print more output in certificate status callback"},
840 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
841 "Status request responder timeout"},
842 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
843 {"status_file", OPT_STATUS_FILE
, '<',
844 "File containing DER encoded OCSP Response"},
846 #ifndef OPENSSL_NO_SSL_TRACE
847 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
849 {"security_debug", OPT_SECURITY_DEBUG
, '-',
850 "Print output from SSL/TLS security framework"},
851 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
852 "Print more output from SSL/TLS security framework"},
853 {"brief", OPT_BRIEF
, '-',
854 "Restrict output to brief summary of connection parameters"},
855 {"rev", OPT_REV
, '-',
856 "act as a simple test server which just sends back with the received text reversed"},
857 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
858 {"ssl_config", OPT_SSL_CONFIG
, 's',
859 "Configure SSL_CTX using the configuration 'val'"},
860 {"max_send_frag", OPT_MAX_SEND_FRAG
, 'p', "Maximum Size of send frames "},
861 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'n',
862 "Size used to split data for encrypt pipelines"},
863 {"max_pipelines", OPT_MAX_PIPELINES
, 'n',
864 "Maximum number of encrypt/decrypt pipelines to be used"},
865 {"read_buf", OPT_READ_BUF
, 'n',
866 "Default read buffer size to be used for connections"},
870 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
871 #ifndef OPENSSL_NO_PSK
872 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
873 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
875 #ifndef OPENSSL_NO_SRP
876 {"srpvfile", OPT_SRPVFILE
, '<', "The verifier file for SRP"},
877 {"srpuserseed", OPT_SRPUSERSEED
, 's',
878 "A seed string for a default user salt"},
880 #ifndef OPENSSL_NO_SSL3
881 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
883 #ifndef OPENSSL_NO_TLS1
884 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
886 #ifndef OPENSSL_NO_TLS1_1
887 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
889 #ifndef OPENSSL_NO_TLS1_2
890 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
892 #ifndef OPENSSL_NO_TLS1_3
893 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
895 #ifndef OPENSSL_NO_DTLS
896 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
897 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
898 {"mtu", OPT_MTU
, 'p', "Set link layer MTU"},
899 {"listen", OPT_LISTEN
, '-',
900 "Listen for a DTLS ClientHello with a cookie and then connect"},
902 #ifndef OPENSSL_NO_DTLS1
903 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
905 #ifndef OPENSSL_NO_DTLS1_2
906 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
908 #ifndef OPENSSL_NO_SCTP
909 {"sctp", OPT_SCTP
, '-', "Use SCTP"},
911 #ifndef OPENSSL_NO_DH
912 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
914 #ifndef OPENSSL_NO_NEXTPROTONEG
915 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
916 "Set the advertised protocols for the NPN extension (comma-separated list)"},
918 #ifndef OPENSSL_NO_SRTP
919 {"use_srtp", OPT_SRTP_PROFILES
, 's',
920 "Offer SRTP key management with a colon-separated profile list"},
922 {"alpn", OPT_ALPN
, 's',
923 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
924 #ifndef OPENSSL_NO_ENGINE
925 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
927 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
928 {"max_early_data", OPT_MAX_EARLY
, 'n',
929 "The maximum number of bytes of early data"},
930 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
931 {NULL
, OPT_EOF
, 0, NULL
}
934 #define IS_PROT_FLAG(o) \
935 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
936 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
938 int s_server_main(int argc
, char *argv
[])
940 ENGINE
*engine
= NULL
;
941 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
942 SSL_CONF_CTX
*cctx
= NULL
;
943 const SSL_METHOD
*meth
= TLS_server_method();
944 SSL_EXCERT
*exc
= NULL
;
945 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
946 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
947 STACK_OF(X509_CRL
) *crls
= NULL
;
948 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
949 X509_VERIFY_PARAM
*vpm
= NULL
;
950 const char *CApath
= NULL
, *CAfile
= NULL
, *chCApath
= NULL
, *chCAfile
= NULL
;
951 char *dpassarg
= NULL
, *dpass
= NULL
, *inrand
= NULL
;
952 char *passarg
= NULL
, *pass
= NULL
, *vfyCApath
= NULL
, *vfyCAfile
= NULL
;
953 char *crl_file
= NULL
, *prog
;
955 int unlink_unix_path
= 0;
957 do_server_cb server_cb
;
958 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
959 #ifndef OPENSSL_NO_DH
963 int nocert
= 0, ret
= 1;
964 int noCApath
= 0, noCAfile
= 0;
965 int s_cert_format
= FORMAT_PEM
, s_key_format
= FORMAT_PEM
;
966 int s_dcert_format
= FORMAT_PEM
, s_dkey_format
= FORMAT_PEM
;
967 int rev
= 0, naccept
= -1, sdebug
= 0;
968 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
, protocol
= 0;
969 int state
= 0, crl_format
= FORMAT_PEM
, crl_download
= 0;
971 char *port
= BUF_strdup(PORT
);
972 unsigned char *context
= NULL
;
974 EVP_PKEY
*s_key2
= NULL
;
975 X509
*s_cert2
= NULL
;
976 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
977 const char *ssl_config
= NULL
;
978 int read_buf_len
= 0;
979 #ifndef OPENSSL_NO_NEXTPROTONEG
980 const char *next_proto_neg_in
= NULL
;
981 tlsextnextprotoctx next_proto
= { NULL
, 0 };
983 const char *alpn_in
= NULL
;
984 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
985 #ifndef OPENSSL_NO_PSK
986 /* by default do not send a PSK identity hint */
987 char *psk_identity_hint
= NULL
;
990 #ifndef OPENSSL_NO_SRP
991 char *srpuserseed
= NULL
;
992 char *srp_verifier_file
= NULL
;
994 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
995 int s_server_verify
= SSL_VERIFY_NONE
;
996 int s_server_session_id_context
= 1; /* anything will do */
997 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
998 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
999 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
1000 #ifndef OPENSSL_NO_OCSP
1001 int s_tlsextstatus
= 0;
1003 int no_resume_ephemeral
= 0;
1004 unsigned int max_send_fragment
= 0;
1005 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
1006 const char *s_serverinfo_file
= NULL
;
1007 const char *keylog_file
= NULL
;
1008 int max_early_data
= -1;
1010 /* Init of few remaining global variables */
1015 s_nbio
= s_nbio_test
= 0;
1024 cctx
= SSL_CONF_CTX_new();
1025 vpm
= X509_VERIFY_PARAM_new();
1026 if (cctx
== NULL
|| vpm
== NULL
)
1028 SSL_CONF_CTX_set_flags(cctx
,
1029 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1031 prog
= opt_init(argc
, argv
, s_server_options
);
1032 while ((o
= opt_next()) != OPT_EOF
) {
1033 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1034 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1037 if (IS_NO_PROT_FLAG(o
))
1039 if (prot_opt
== 1 && no_prot_opt
) {
1041 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1048 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1051 opt_help(s_server_options
);
1057 if (socket_family
== AF_UNIX
) {
1058 OPENSSL_free(host
); host
= NULL
;
1059 OPENSSL_free(port
); port
= NULL
;
1062 socket_family
= AF_INET
;
1068 if (socket_family
== AF_UNIX
) {
1069 OPENSSL_free(host
); host
= NULL
;
1070 OPENSSL_free(port
); port
= NULL
;
1073 socket_family
= AF_INET6
;
1076 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1082 if (socket_family
== AF_UNIX
) {
1083 socket_family
= AF_UNSPEC
;
1086 OPENSSL_free(port
); port
= NULL
;
1087 OPENSSL_free(host
); host
= NULL
;
1088 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1090 "%s: -port argument malformed or ambiguous\n",
1097 if (socket_family
== AF_UNIX
) {
1098 socket_family
= AF_UNSPEC
;
1101 OPENSSL_free(port
); port
= NULL
;
1102 OPENSSL_free(host
); host
= NULL
;
1103 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1105 "%s: -accept argument malformed or ambiguous\n",
1112 socket_family
= AF_UNIX
;
1113 OPENSSL_free(host
); host
= BUF_strdup(opt_arg());
1114 OPENSSL_free(port
); port
= NULL
;
1117 unlink_unix_path
= 1;
1121 naccept
= atol(opt_arg());
1124 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1125 verify_args
.depth
= atoi(opt_arg());
1127 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1129 case OPT_UPPER_V_VERIFY
:
1131 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1132 SSL_VERIFY_CLIENT_ONCE
;
1133 verify_args
.depth
= atoi(opt_arg());
1136 "verify depth is %d, must return a certificate\n",
1140 context
= (unsigned char *)opt_arg();
1143 s_cert_file
= opt_arg();
1146 if (!set_nameopt(opt_arg()))
1150 crl_file
= opt_arg();
1152 case OPT_CRL_DOWNLOAD
:
1155 case OPT_SERVERINFO
:
1156 s_serverinfo_file
= opt_arg();
1159 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_cert_format
))
1163 s_key_file
= opt_arg();
1166 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1170 passarg
= opt_arg();
1172 case OPT_CERT_CHAIN
:
1173 s_chain_file
= opt_arg();
1176 #ifndef OPENSSL_NO_DH
1181 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dcert_format
))
1185 s_dcert_file
= opt_arg();
1188 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dkey_format
))
1192 dpassarg
= opt_arg();
1195 s_dkey_file
= opt_arg();
1197 case OPT_DCERT_CHAIN
:
1198 s_dchain_file
= opt_arg();
1209 case OPT_CHAINCAPATH
:
1210 chCApath
= opt_arg();
1212 case OPT_VERIFYCAPATH
:
1213 vfyCApath
= opt_arg();
1222 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1226 if (ssl_args
== NULL
)
1227 ssl_args
= sk_OPENSSL_STRING_new_null();
1228 if (ssl_args
== NULL
1229 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1230 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1231 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1236 if (!opt_verify(o
, vpm
))
1241 if (!args_excert(o
, &exc
))
1244 case OPT_VERIFY_RET_ERROR
:
1245 verify_args
.return_error
= 1;
1247 case OPT_VERIFY_QUIET
:
1248 verify_args
.quiet
= 1;
1250 case OPT_BUILD_CHAIN
:
1259 case OPT_CHAINCAFILE
:
1260 chCAfile
= opt_arg();
1262 case OPT_VERIFYCAFILE
:
1263 vfyCAfile
= opt_arg();
1269 s_nbio
= s_nbio_test
= 1;
1274 case OPT_NO_IGN_EOF
:
1280 case OPT_TLSEXTDEBUG
:
1284 #ifndef OPENSSL_NO_OCSP
1288 case OPT_STATUS_VERBOSE
:
1289 #ifndef OPENSSL_NO_OCSP
1290 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1293 case OPT_STATUS_TIMEOUT
:
1294 #ifndef OPENSSL_NO_OCSP
1296 tlscstatp
.timeout
= atoi(opt_arg());
1299 case OPT_STATUS_URL
:
1300 #ifndef OPENSSL_NO_OCSP
1302 if (!OCSP_parse_url(opt_arg(),
1305 &tlscstatp
.path
, &tlscstatp
.use_ssl
)) {
1306 BIO_printf(bio_err
, "Error parsing URL\n");
1311 case OPT_STATUS_FILE
:
1312 #ifndef OPENSSL_NO_OCSP
1314 tlscstatp
.respin
= opt_arg();
1321 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1324 #ifndef OPENSSL_NO_SSL_TRACE
1328 case OPT_SECURITY_DEBUG
:
1331 case OPT_SECURITY_DEBUG_VERBOSE
:
1344 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1347 #ifndef OPENSSL_NO_DH
1351 case OPT_NO_RESUME_EPHEMERAL
:
1352 no_resume_ephemeral
= 1;
1355 #ifndef OPENSSL_NO_PSK
1356 psk_identity_hint
= opt_arg();
1360 #ifndef OPENSSL_NO_PSK
1361 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1362 if (isxdigit(_UC(*p
)))
1364 BIO_printf(bio_err
, "Not a hex number '%s'\n", *argv
);
1370 #ifndef OPENSSL_NO_SRP
1371 srp_verifier_file
= opt_arg();
1372 if (min_version
< TLS1_VERSION
)
1373 min_version
= TLS1_VERSION
;
1376 case OPT_SRPUSERSEED
:
1377 #ifndef OPENSSL_NO_SRP
1378 srpuserseed
= opt_arg();
1379 if (min_version
< TLS1_VERSION
)
1380 min_version
= TLS1_VERSION
;
1395 case OPT_SSL_CONFIG
:
1396 ssl_config
= opt_arg();
1399 min_version
= SSL3_VERSION
;
1400 max_version
= SSL3_VERSION
;
1403 min_version
= TLS1_3_VERSION
;
1404 max_version
= TLS1_3_VERSION
;
1407 min_version
= TLS1_2_VERSION
;
1408 max_version
= TLS1_2_VERSION
;
1411 min_version
= TLS1_1_VERSION
;
1412 max_version
= TLS1_1_VERSION
;
1415 min_version
= TLS1_VERSION
;
1416 max_version
= TLS1_VERSION
;
1419 #ifndef OPENSSL_NO_DTLS
1420 meth
= DTLS_server_method();
1421 socket_type
= SOCK_DGRAM
;
1425 #ifndef OPENSSL_NO_DTLS
1426 meth
= DTLS_server_method();
1427 min_version
= DTLS1_VERSION
;
1428 max_version
= DTLS1_VERSION
;
1429 socket_type
= SOCK_DGRAM
;
1433 #ifndef OPENSSL_NO_DTLS
1434 meth
= DTLS_server_method();
1435 min_version
= DTLS1_2_VERSION
;
1436 max_version
= DTLS1_2_VERSION
;
1437 socket_type
= SOCK_DGRAM
;
1441 #ifndef OPENSSL_NO_SCTP
1442 protocol
= IPPROTO_SCTP
;
1446 #ifndef OPENSSL_NO_DTLS
1447 enable_timeouts
= 1;
1451 #ifndef OPENSSL_NO_DTLS
1452 socket_mtu
= atol(opt_arg());
1456 #ifndef OPENSSL_NO_DTLS
1461 session_id_prefix
= opt_arg();
1464 engine
= setup_engine(opt_arg(), 1);
1469 case OPT_SERVERNAME
:
1470 tlsextcbp
.servername
= opt_arg();
1472 case OPT_SERVERNAME_FATAL
:
1473 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1476 s_cert_file2
= opt_arg();
1479 s_key_file2
= opt_arg();
1481 case OPT_NEXTPROTONEG
:
1482 # ifndef OPENSSL_NO_NEXTPROTONEG
1483 next_proto_neg_in
= opt_arg();
1487 alpn_in
= opt_arg();
1489 case OPT_SRTP_PROFILES
:
1490 #ifndef OPENSSL_NO_SRTP
1491 srtp_profiles
= opt_arg();
1494 case OPT_KEYMATEXPORT
:
1495 keymatexportlabel
= opt_arg();
1497 case OPT_KEYMATEXPORTLEN
:
1498 keymatexportlen
= atoi(opt_arg());
1503 case OPT_MAX_SEND_FRAG
:
1504 max_send_fragment
= atoi(opt_arg());
1505 if (max_send_fragment
== 0) {
1507 * Not allowed - set to a deliberately bad value so we get an
1508 * error message below
1510 max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
+ 1;
1513 case OPT_SPLIT_SEND_FRAG
:
1514 split_send_fragment
= atoi(opt_arg());
1515 if (split_send_fragment
== 0) {
1517 * Not allowed - set to a deliberately bad value so we get an
1518 * error message below
1520 split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
+ 1;
1523 case OPT_MAX_PIPELINES
:
1524 max_pipelines
= atoi(opt_arg());
1527 read_buf_len
= atoi(opt_arg());
1529 case OPT_KEYLOG_FILE
:
1530 keylog_file
= opt_arg();
1533 max_early_data
= atoi(opt_arg());
1534 if (max_early_data
< 0) {
1535 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1539 case OPT_EARLY_DATA
:
1544 argc
= opt_num_rest();
1547 #ifndef OPENSSL_NO_DTLS
1548 if (www
&& socket_type
== SOCK_DGRAM
) {
1549 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1553 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1554 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1560 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1562 "Can't use unix sockets and datagrams together\n");
1567 #ifndef OPENSSL_NO_SCTP
1568 if (protocol
== IPPROTO_SCTP
) {
1569 if (socket_type
!= SOCK_DGRAM
) {
1570 BIO_printf(bio_err
, "Can't use -sctp without DTLS\n");
1573 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1574 socket_type
= SOCK_STREAM
;
1577 if (max_send_fragment
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1578 BIO_printf(bio_err
, "%s: Bad max send fragment size\n", prog
);
1582 if (split_send_fragment
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1583 BIO_printf(bio_err
, "%s:Bad split send fragment size\n", prog
);
1587 if (max_pipelines
> SSL_MAX_PIPELINES
) {
1588 BIO_printf(bio_err
, "%s:too large max-pipelines value\n", prog
);
1592 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1593 BIO_printf(bio_err
, "Error getting password\n");
1597 if (s_key_file
== NULL
)
1598 s_key_file
= s_cert_file
;
1600 if (s_key_file2
== NULL
)
1601 s_key_file2
= s_cert_file2
;
1603 if (!load_excert(&exc
))
1607 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1608 "server certificate private key file");
1610 ERR_print_errors(bio_err
);
1614 s_cert
= load_cert(s_cert_file
, s_cert_format
,
1615 "server certificate file");
1618 ERR_print_errors(bio_err
);
1622 if (!load_certs(s_chain_file
, &s_chain
, FORMAT_PEM
, NULL
,
1623 "server certificate chain"))
1627 if (tlsextcbp
.servername
) {
1628 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1629 "second server certificate private key file");
1631 ERR_print_errors(bio_err
);
1635 s_cert2
= load_cert(s_cert_file2
, s_cert_format
,
1636 "second server certificate file");
1639 ERR_print_errors(bio_err
);
1644 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1645 if (next_proto_neg_in
) {
1646 next_proto
.data
= next_protos_parse(&next_proto
.len
, next_proto_neg_in
);
1647 if (next_proto
.data
== NULL
)
1651 alpn_ctx
.data
= NULL
;
1653 alpn_ctx
.data
= next_protos_parse(&alpn_ctx
.len
, alpn_in
);
1654 if (alpn_ctx
.data
== NULL
)
1660 crl
= load_crl(crl_file
, crl_format
);
1662 BIO_puts(bio_err
, "Error loading CRL\n");
1663 ERR_print_errors(bio_err
);
1666 crls
= sk_X509_CRL_new_null();
1667 if (!crls
|| !sk_X509_CRL_push(crls
, crl
)) {
1668 BIO_puts(bio_err
, "Error adding CRL\n");
1669 ERR_print_errors(bio_err
);
1677 if (s_dkey_file
== NULL
)
1678 s_dkey_file
= s_dcert_file
;
1680 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1681 0, dpass
, engine
, "second certificate private key file");
1683 ERR_print_errors(bio_err
);
1687 s_dcert
= load_cert(s_dcert_file
, s_dcert_format
,
1688 "second server certificate file");
1691 ERR_print_errors(bio_err
);
1694 if (s_dchain_file
) {
1695 if (!load_certs(s_dchain_file
, &s_dchain
, FORMAT_PEM
, NULL
,
1696 "second server certificate chain"))
1702 if (!app_RAND_load_file(NULL
, 1) && inrand
== NULL
1703 && !RAND_status()) {
1705 "warning, not much extra random data, consider using the -rand option\n");
1708 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
1709 app_RAND_load_files(inrand
));
1711 if (bio_s_out
== NULL
) {
1712 if (s_quiet
&& !s_debug
) {
1713 bio_s_out
= BIO_new(BIO_s_null());
1714 if (s_msg
&& !bio_s_msg
)
1715 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1717 if (bio_s_out
== NULL
)
1718 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1721 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
1727 s_dcert_file
= NULL
;
1729 s_cert_file2
= NULL
;
1733 ctx
= SSL_CTX_new(meth
);
1735 ERR_print_errors(bio_err
);
1739 ssl_ctx_security_debug(ctx
, sdebug
);
1741 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1742 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1744 ERR_print_errors(bio_err
);
1748 if (SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1750 if (SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1753 if (session_id_prefix
) {
1754 if (strlen(session_id_prefix
) >= 32)
1756 "warning: id_prefix is too long, only one new session will be possible\n");
1757 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1758 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1759 ERR_print_errors(bio_err
);
1762 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1764 SSL_CTX_set_quiet_shutdown(ctx
, 1);
1766 ssl_ctx_set_excert(ctx
, exc
);
1769 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1771 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1773 init_session_cache_ctx(ctx
);
1775 SSL_CTX_sess_set_cache_size(ctx
, 128);
1778 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1781 if (max_send_fragment
> 0)
1782 SSL_CTX_set_max_send_fragment(ctx
, max_send_fragment
);
1784 if (split_send_fragment
> 0) {
1785 SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
);
1787 if (max_pipelines
> 0) {
1788 SSL_CTX_set_max_pipelines(ctx
, max_pipelines
);
1791 if (read_buf_len
> 0) {
1792 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1794 #ifndef OPENSSL_NO_SRTP
1795 if (srtp_profiles
!= NULL
) {
1796 /* Returns 0 on success! */
1797 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1798 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1799 ERR_print_errors(bio_err
);
1805 if (!ctx_set_verify_locations(ctx
, CAfile
, CApath
, noCAfile
, noCApath
)) {
1806 ERR_print_errors(bio_err
);
1809 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1810 BIO_printf(bio_err
, "Error setting verify params\n");
1811 ERR_print_errors(bio_err
);
1815 ssl_ctx_add_crls(ctx
, crls
, 0);
1816 if (!config_ctx(cctx
, ssl_args
, ctx
))
1819 if (!ssl_load_stores(ctx
, vfyCApath
, vfyCAfile
, chCApath
, chCAfile
,
1820 crls
, crl_download
)) {
1821 BIO_printf(bio_err
, "Error loading store locations\n");
1822 ERR_print_errors(bio_err
);
1827 ctx2
= SSL_CTX_new(meth
);
1829 ERR_print_errors(bio_err
);
1835 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
1838 ssl_ctx_security_debug(ctx
, sdebug
);
1840 if (session_id_prefix
) {
1841 if (strlen(session_id_prefix
) >= 32)
1843 "warning: id_prefix is too long, only one new session will be possible\n");
1844 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
1845 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1846 ERR_print_errors(bio_err
);
1849 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1851 SSL_CTX_set_quiet_shutdown(ctx2
, 1);
1853 ssl_ctx_set_excert(ctx2
, exc
);
1856 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
1859 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
1861 init_session_cache_ctx(ctx2
);
1863 SSL_CTX_sess_set_cache_size(ctx2
, 128);
1866 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
1868 if (!ctx_set_verify_locations(ctx2
, CAfile
, CApath
, noCAfile
,
1870 ERR_print_errors(bio_err
);
1873 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
1874 BIO_printf(bio_err
, "Error setting verify params\n");
1875 ERR_print_errors(bio_err
);
1879 ssl_ctx_add_crls(ctx2
, crls
, 0);
1880 if (!config_ctx(cctx
, ssl_args
, ctx2
))
1883 #ifndef OPENSSL_NO_NEXTPROTONEG
1884 if (next_proto
.data
)
1885 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
1889 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
1891 #ifndef OPENSSL_NO_DH
1896 dh
= load_dh_param(dhfile
);
1897 else if (s_cert_file
)
1898 dh
= load_dh_param(s_cert_file
);
1901 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1903 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
1905 (void)BIO_flush(bio_s_out
);
1908 SSL_CTX_set_dh_auto(ctx
, 1);
1909 else if (!SSL_CTX_set_tmp_dh(ctx
, dh
)) {
1910 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1911 ERR_print_errors(bio_err
);
1918 DH
*dh2
= load_dh_param(s_cert_file2
);
1920 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1921 (void)BIO_flush(bio_s_out
);
1928 SSL_CTX_set_dh_auto(ctx2
, 1);
1929 else if (!SSL_CTX_set_tmp_dh(ctx2
, dh
)) {
1930 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1931 ERR_print_errors(bio_err
);
1940 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
1943 if (s_serverinfo_file
!= NULL
1944 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
1945 ERR_print_errors(bio_err
);
1949 if (ctx2
&& !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
1952 if (s_dcert
!= NULL
) {
1953 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
1957 if (no_resume_ephemeral
) {
1958 SSL_CTX_set_not_resumable_session_callback(ctx
,
1959 not_resumable_sess_cb
);
1962 SSL_CTX_set_not_resumable_session_callback(ctx2
,
1963 not_resumable_sess_cb
);
1965 #ifndef OPENSSL_NO_PSK
1966 if (psk_key
!= NULL
) {
1968 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
1969 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
1972 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
1973 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
1974 ERR_print_errors(bio_err
);
1979 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
1980 if (!SSL_CTX_set_session_id_context(ctx
,
1981 (void *)&s_server_session_id_context
,
1982 sizeof s_server_session_id_context
)) {
1983 BIO_printf(bio_err
, "error setting session id context\n");
1984 ERR_print_errors(bio_err
);
1988 /* Set DTLS cookie generation and verification callbacks */
1989 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
1990 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
1993 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
1994 if (!SSL_CTX_set_session_id_context(ctx2
,
1995 (void *)&s_server_session_id_context
,
1996 sizeof s_server_session_id_context
)) {
1997 BIO_printf(bio_err
, "error setting session id context\n");
1998 ERR_print_errors(bio_err
);
2001 tlsextcbp
.biodebug
= bio_s_out
;
2002 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
2003 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
2004 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
2005 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
2008 #ifndef OPENSSL_NO_SRP
2009 if (srp_verifier_file
!= NULL
) {
2010 srp_callback_parm
.vb
= SRP_VBASE_new(srpuserseed
);
2011 srp_callback_parm
.user
= NULL
;
2012 srp_callback_parm
.login
= NULL
;
2014 SRP_VBASE_init(srp_callback_parm
.vb
,
2015 srp_verifier_file
)) != SRP_NO_ERROR
) {
2017 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
2018 srp_verifier_file
, ret
);
2021 SSL_CTX_set_verify(ctx
, SSL_VERIFY_NONE
, verify_callback
);
2022 SSL_CTX_set_srp_cb_arg(ctx
, &srp_callback_parm
);
2023 SSL_CTX_set_srp_username_callback(ctx
, ssl_srp_server_param_cb
);
2026 if (CAfile
!= NULL
) {
2027 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
2030 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
2032 #ifndef OPENSSL_NO_OCSP
2033 if (s_tlsextstatus
) {
2034 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
2035 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2037 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2038 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2042 if (set_keylog_file(ctx
, keylog_file
))
2045 if (max_early_data
>= 0)
2046 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2048 BIO_printf(bio_s_out
, "ACCEPT\n");
2049 (void)BIO_flush(bio_s_out
);
2051 server_cb
= rev_body
;
2053 server_cb
= www_body
;
2055 server_cb
= sv_body
;
2057 if (socket_family
== AF_UNIX
2058 && unlink_unix_path
)
2061 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
, protocol
,
2062 server_cb
, context
, naccept
);
2063 print_stats(bio_s_out
, ctx
);
2067 set_keylog_file(NULL
, NULL
);
2069 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2071 EVP_PKEY_free(s_key
);
2072 EVP_PKEY_free(s_dkey
);
2073 sk_X509_pop_free(s_chain
, X509_free
);
2074 sk_X509_pop_free(s_dchain
, X509_free
);
2076 OPENSSL_free(dpass
);
2079 X509_VERIFY_PARAM_free(vpm
);
2081 OPENSSL_free(tlscstatp
.host
);
2082 OPENSSL_free(tlscstatp
.port
);
2083 OPENSSL_free(tlscstatp
.path
);
2086 EVP_PKEY_free(s_key2
);
2087 #ifndef OPENSSL_NO_NEXTPROTONEG
2088 OPENSSL_free(next_proto
.data
);
2090 OPENSSL_free(alpn_ctx
.data
);
2091 ssl_excert_free(exc
);
2092 sk_OPENSSL_STRING_free(ssl_args
);
2093 SSL_CONF_CTX_free(cctx
);
2094 release_engine(engine
);
2095 BIO_free(bio_s_out
);
2097 BIO_free(bio_s_msg
);
2099 #ifdef CHARSET_EBCDIC
2100 BIO_meth_free(methods_ebcdic
);
2105 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2107 BIO_printf(bio
, "%4ld items in the session cache\n",
2108 SSL_CTX_sess_number(ssl_ctx
));
2109 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2110 SSL_CTX_sess_connect(ssl_ctx
));
2111 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2112 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2113 BIO_printf(bio
, "%4ld client connects that finished\n",
2114 SSL_CTX_sess_connect_good(ssl_ctx
));
2115 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2116 SSL_CTX_sess_accept(ssl_ctx
));
2117 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2118 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2119 BIO_printf(bio
, "%4ld server accepts that finished\n",
2120 SSL_CTX_sess_accept_good(ssl_ctx
));
2121 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2122 BIO_printf(bio
, "%4ld session cache misses\n",
2123 SSL_CTX_sess_misses(ssl_ctx
));
2124 BIO_printf(bio
, "%4ld session cache timeouts\n",
2125 SSL_CTX_sess_timeouts(ssl_ctx
));
2126 BIO_printf(bio
, "%4ld callback cache hits\n",
2127 SSL_CTX_sess_cb_hits(ssl_ctx
));
2128 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2129 SSL_CTX_sess_cache_full(ssl_ctx
),
2130 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2133 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
)
2142 struct timeval timeout
;
2143 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2146 struct timeval
*timeoutp
;
2148 #ifndef OPENSSL_NO_DTLS
2149 # ifndef OPENSSL_NO_SCTP
2150 int isdtls
= (stype
== SOCK_DGRAM
|| prot
== IPPROTO_SCTP
);
2152 int isdtls
= (stype
== SOCK_DGRAM
);
2156 buf
= app_malloc(bufsize
, "server buffer");
2158 if (!BIO_socket_nbio(s
, 1))
2159 ERR_print_errors(bio_err
);
2161 BIO_printf(bio_err
, "Turned on non blocking io\n");
2167 if (s_tlsextdebug
) {
2168 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2169 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2173 && !SSL_set_session_id_context(con
,
2174 context
, strlen((char *)context
))) {
2175 BIO_printf(bio_err
, "Error setting session id context\n");
2180 if (!SSL_clear(con
)) {
2181 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2185 #ifndef OPENSSL_NO_DTLS
2187 # ifndef OPENSSL_NO_SCTP
2188 if (prot
== IPPROTO_SCTP
)
2189 sbio
= BIO_new_dgram_sctp(s
, BIO_NOCLOSE
);
2192 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2194 if (enable_timeouts
) {
2196 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2197 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2200 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2201 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2205 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2206 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2207 DTLS_get_link_min_mtu(con
));
2212 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2213 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2214 BIO_printf(bio_err
, "Failed to set MTU\n");
2220 /* want to do MTU discovery */
2221 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2223 # ifndef OPENSSL_NO_SCTP
2224 if (prot
!= IPPROTO_SCTP
) {
2225 /* Turn on cookie exchange. Not necessary for SCTP */
2226 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2231 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2234 BIO_printf(bio_err
, "Unable to create BIO\n");
2235 ERR_print_errors(bio_err
);
2242 test
= BIO_new(BIO_f_nbio_test());
2243 sbio
= BIO_push(test
, sbio
);
2246 SSL_set_bio(con
, sbio
, sbio
);
2247 SSL_set_accept_state(con
);
2248 /* SSL_set_fd(con,s); */
2251 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2252 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2255 #ifndef OPENSSL_NO_SSL_TRACE
2257 SSL_set_msg_callback(con
, SSL_trace
);
2260 SSL_set_msg_callback(con
, msg_cb
);
2261 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2264 if (s_tlsextdebug
) {
2265 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2266 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2270 int write_header
= 1, edret
= SSL_READ_EARLY_DATA_ERROR
;
2273 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
2275 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
2276 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
2279 switch (SSL_get_error(con
, 0)) {
2280 case SSL_ERROR_WANT_WRITE
:
2281 case SSL_ERROR_WANT_ASYNC
:
2282 case SSL_ERROR_WANT_READ
:
2283 /* Just keep trying - busy waiting */
2286 BIO_printf(bio_err
, "Error reading early data\n");
2287 ERR_print_errors(bio_err
);
2291 if (readbytes
> 0) {
2293 BIO_printf(bio_s_out
, "Early data received:\n");
2296 raw_write_stdout(buf
, (unsigned int)readbytes
);
2297 (void)BIO_flush(bio_s_out
);
2301 BIO_printf(bio_s_out
, "No early data received\n");
2303 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2304 if (SSL_is_init_finished(con
))
2305 print_connection_info(con
);
2308 if (fileno_stdin() > s
)
2309 width
= fileno_stdin() + 1;
2313 int read_from_terminal
;
2314 int read_from_sslcon
;
2316 read_from_terminal
= 0;
2317 read_from_sslcon
= SSL_has_pending(con
)
2318 || (async
&& SSL_waiting_for_async(con
));
2320 if (!read_from_sslcon
) {
2322 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2323 openssl_fdset(fileno_stdin(), &readfds
);
2325 openssl_fdset(s
, &readfds
);
2327 * Note: under VMS with SOCKETSHR the second parameter is
2328 * currently of type (int *) whereas under other systems it is
2329 * (void *) if you don't have a cast it will choke the compiler:
2330 * if you do have a cast then you can either go for (int *) or
2333 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2335 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2336 * only on sockets. As a workaround we timeout the select every
2337 * second and check for any keypress. In a proper Windows
2338 * application we wouldn't do this because it is inefficient.
2342 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &tv
);
2343 if (has_stdin_waiting())
2344 read_from_terminal
= 1;
2345 if ((i
< 0) || (!i
&& !read_from_terminal
))
2348 if ((SSL_version(con
) == DTLS1_VERSION
) &&
2349 DTLSv1_get_timeout(con
, &timeout
))
2350 timeoutp
= &timeout
;
2354 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2356 if ((SSL_version(con
) == DTLS1_VERSION
)
2357 && DTLSv1_handle_timeout(con
) > 0) {
2358 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2363 if (FD_ISSET(fileno_stdin(), &readfds
))
2364 read_from_terminal
= 1;
2366 if (FD_ISSET(s
, &readfds
))
2367 read_from_sslcon
= 1;
2369 if (read_from_terminal
) {
2373 i
= raw_read_stdin(buf
, bufsize
/ 2);
2375 /* both loops are skipped when i <= 0 */
2376 for (j
= 0; j
< i
; j
++)
2379 for (j
= i
- 1; j
>= 0; j
--) {
2380 buf
[j
+ lf_num
] = buf
[j
];
2381 if (buf
[j
] == '\n') {
2384 buf
[j
+ lf_num
] = '\r';
2387 assert(lf_num
== 0);
2389 i
= raw_read_stdin(buf
, bufsize
);
2391 if (!s_quiet
&& !s_brief
) {
2392 if ((i
<= 0) || (buf
[0] == 'Q')) {
2393 BIO_printf(bio_s_out
, "DONE\n");
2394 (void)BIO_flush(bio_s_out
);
2396 close_accept_socket();
2400 if ((i
<= 0) || (buf
[0] == 'q')) {
2401 BIO_printf(bio_s_out
, "DONE\n");
2402 (void)BIO_flush(bio_s_out
);
2403 if (SSL_version(con
) != DTLS1_VERSION
)
2406 * close_accept_socket(); ret= -11;
2410 #ifndef OPENSSL_NO_HEARTBEATS
2411 if ((buf
[0] == 'B') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2412 BIO_printf(bio_err
, "HEARTBEATING\n");
2418 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2419 SSL_renegotiate(con
);
2420 i
= SSL_do_handshake(con
);
2421 printf("SSL_do_handshake -> %d\n", i
);
2425 * strcpy(buf,"server side RE-NEGOTIATE\n");
2428 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2430 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2432 SSL_renegotiate(con
);
2433 i
= SSL_do_handshake(con
);
2434 printf("SSL_do_handshake -> %d\n", i
);
2438 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2442 if ((buf
[0] == 'K' || buf
[0] == 'k')
2443 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2444 SSL_key_update(con
, buf
[0] == 'K' ?
2445 SSL_KEY_UPDATE_REQUESTED
2446 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2447 i
= SSL_do_handshake(con
);
2448 printf("SSL_do_handshake -> %d\n", i
);
2452 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2456 if (buf
[0] == 'P') {
2457 static const char *str
= "Lets print some clear text\n";
2458 BIO_write(SSL_get_wbio(con
), str
, strlen(str
));
2460 if (buf
[0] == 'S') {
2461 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2464 #ifdef CHARSET_EBCDIC
2465 ebcdic2ascii(buf
, buf
, i
);
2469 /* should do a select for the write */
2472 if (++count
== 100) {
2474 SSL_renegotiate(con
);
2477 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2478 #ifndef OPENSSL_NO_SRP
2479 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2480 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2481 SRP_user_pwd_free(srp_callback_parm
.user
);
2482 srp_callback_parm
.user
=
2483 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2484 srp_callback_parm
.login
);
2485 if (srp_callback_parm
.user
)
2486 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2487 srp_callback_parm
.user
->info
);
2489 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2490 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2493 switch (SSL_get_error(con
, k
)) {
2494 case SSL_ERROR_NONE
:
2496 case SSL_ERROR_WANT_ASYNC
:
2497 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2498 (void)BIO_flush(bio_s_out
);
2499 wait_for_async(con
);
2501 case SSL_ERROR_WANT_WRITE
:
2502 case SSL_ERROR_WANT_READ
:
2503 case SSL_ERROR_WANT_X509_LOOKUP
:
2504 BIO_printf(bio_s_out
, "Write BLOCK\n");
2505 (void)BIO_flush(bio_s_out
);
2507 case SSL_ERROR_WANT_ASYNC_JOB
:
2509 * This shouldn't ever happen in s_server. Treat as an error
2511 case SSL_ERROR_SYSCALL
:
2513 BIO_printf(bio_s_out
, "ERROR\n");
2514 (void)BIO_flush(bio_s_out
);
2515 ERR_print_errors(bio_err
);
2519 case SSL_ERROR_ZERO_RETURN
:
2520 BIO_printf(bio_s_out
, "DONE\n");
2521 (void)BIO_flush(bio_s_out
);
2533 if (read_from_sslcon
) {
2535 * init_ssl_connection handles all async events itself so if we're
2536 * waiting for async then we shouldn't go back into
2537 * init_ssl_connection
2539 if ((!async
|| !SSL_waiting_for_async(con
))
2540 && !SSL_is_init_finished(con
)) {
2541 i
= init_ssl_connection(con
);
2546 } else if (i
== 0) {
2552 i
= SSL_read(con
, (char *)buf
, bufsize
);
2553 #ifndef OPENSSL_NO_SRP
2554 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2555 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2556 SRP_user_pwd_free(srp_callback_parm
.user
);
2557 srp_callback_parm
.user
=
2558 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2559 srp_callback_parm
.login
);
2560 if (srp_callback_parm
.user
)
2561 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2562 srp_callback_parm
.user
->info
);
2564 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2565 i
= SSL_read(con
, (char *)buf
, bufsize
);
2568 switch (SSL_get_error(con
, i
)) {
2569 case SSL_ERROR_NONE
:
2570 #ifdef CHARSET_EBCDIC
2571 ascii2ebcdic(buf
, buf
, i
);
2573 raw_write_stdout(buf
, (unsigned int)i
);
2574 (void)BIO_flush(bio_s_out
);
2575 if (SSL_has_pending(con
))
2578 case SSL_ERROR_WANT_ASYNC
:
2579 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2580 (void)BIO_flush(bio_s_out
);
2581 wait_for_async(con
);
2583 case SSL_ERROR_WANT_WRITE
:
2584 case SSL_ERROR_WANT_READ
:
2585 BIO_printf(bio_s_out
, "Read BLOCK\n");
2586 (void)BIO_flush(bio_s_out
);
2588 case SSL_ERROR_WANT_ASYNC_JOB
:
2590 * This shouldn't ever happen in s_server. Treat as an error
2592 case SSL_ERROR_SYSCALL
:
2594 BIO_printf(bio_s_out
, "ERROR\n");
2595 (void)BIO_flush(bio_s_out
);
2596 ERR_print_errors(bio_err
);
2599 case SSL_ERROR_ZERO_RETURN
:
2600 BIO_printf(bio_s_out
, "DONE\n");
2601 (void)BIO_flush(bio_s_out
);
2610 BIO_printf(bio_s_out
, "shutting down SSL\n");
2611 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
2614 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2615 OPENSSL_clear_free(buf
, bufsize
);
2617 BIO_printf(bio_s_out
, "ACCEPT\n");
2618 (void)BIO_flush(bio_s_out
);
2622 static void close_accept_socket(void)
2624 BIO_printf(bio_err
, "shutdown accept socket\n");
2625 if (accept_socket
>= 0) {
2626 BIO_closesocket(accept_socket
);
2630 static int init_ssl_connection(SSL
*con
)
2636 #ifndef OPENSSL_NO_DTLS
2638 BIO_ADDR
*client
= NULL
;
2640 if ((client
= BIO_ADDR_new()) == NULL
) {
2641 BIO_printf(bio_err
, "ERROR - memory\n");
2644 i
= DTLSv1_listen(con
, client
);
2649 wbio
= SSL_get_wbio(con
);
2651 BIO_get_fd(wbio
, &fd
);
2654 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2655 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2656 BIO_ADDR_free(client
);
2659 BIO_ADDR_free(client
);
2661 i
= SSL_accept(con
);
2663 BIO_ADDR_free(client
);
2669 i
= SSL_accept(con
);
2672 retry
= !SSL_want_nothing(con
);
2673 #ifdef CERT_CB_TEST_RETRY
2676 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2677 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2679 "LOOKUP from certificate callback during accept\n");
2680 i
= SSL_accept(con
);
2682 retry
= !SSL_want_nothing(con
);
2687 #ifndef OPENSSL_NO_SRP
2689 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2690 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
2691 srp_callback_parm
.login
);
2692 SRP_user_pwd_free(srp_callback_parm
.user
);
2693 srp_callback_parm
.user
=
2694 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2695 srp_callback_parm
.login
);
2696 if (srp_callback_parm
.user
)
2697 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2698 srp_callback_parm
.user
->info
);
2700 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2701 i
= SSL_accept(con
);
2703 retry
= !SSL_want_nothing(con
);
2706 } while (i
< 0 && SSL_waiting_for_async(con
));
2709 if ((dtlslisten
&& i
== 0)
2710 || (!dtlslisten
&& retry
)) {
2711 BIO_printf(bio_s_out
, "DELAY\n");
2715 BIO_printf(bio_err
, "ERROR\n");
2717 verify_err
= SSL_get_verify_result(con
);
2718 if (verify_err
!= X509_V_OK
) {
2719 BIO_printf(bio_err
, "verify error:%s\n",
2720 X509_verify_cert_error_string(verify_err
));
2722 /* Always print any error messages */
2723 ERR_print_errors(bio_err
);
2727 print_connection_info(con
);
2731 static void print_connection_info(SSL
*con
)
2736 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2737 const unsigned char *next_proto_neg
;
2738 unsigned next_proto_neg_len
;
2740 unsigned char *exportedkeymat
;
2744 print_ssl_summary(con
);
2746 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
2748 peer
= SSL_get_peer_certificate(con
);
2750 BIO_printf(bio_s_out
, "Client certificate\n");
2751 PEM_write_bio_X509(bio_s_out
, peer
);
2752 dump_cert_text(bio_s_out
, peer
);
2757 if (SSL_get_shared_ciphers(con
, buf
, sizeof buf
) != NULL
)
2758 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
2759 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
2760 ssl_print_sigalgs(bio_s_out
, con
);
2761 #ifndef OPENSSL_NO_EC
2762 ssl_print_point_formats(bio_s_out
, con
);
2763 ssl_print_groups(bio_s_out
, con
, 0);
2765 print_ca_names(bio_s_out
, con
);
2766 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
2768 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2769 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
2770 if (next_proto_neg
) {
2771 BIO_printf(bio_s_out
, "NEXTPROTO is ");
2772 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
2773 BIO_printf(bio_s_out
, "\n");
2776 #ifndef OPENSSL_NO_SRTP
2778 SRTP_PROTECTION_PROFILE
*srtp_profile
2779 = SSL_get_selected_srtp_profile(con
);
2782 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
2783 srtp_profile
->name
);
2786 if (SSL_session_reused(con
))
2787 BIO_printf(bio_s_out
, "Reused session-id\n");
2788 BIO_printf(bio_s_out
, "Secure Renegotiation IS%s supported\n",
2789 SSL_get_secure_renegotiation_support(con
) ? "" : " NOT");
2790 if (keymatexportlabel
!= NULL
) {
2791 BIO_printf(bio_s_out
, "Keying material exporter:\n");
2792 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
2793 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
2794 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
2795 if (!SSL_export_keying_material(con
, exportedkeymat
,
2798 strlen(keymatexportlabel
),
2800 BIO_printf(bio_s_out
, " Error\n");
2802 BIO_printf(bio_s_out
, " Keying material: ");
2803 for (i
= 0; i
< keymatexportlen
; i
++)
2804 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
2805 BIO_printf(bio_s_out
, "\n");
2807 OPENSSL_free(exportedkeymat
);
2810 (void)BIO_flush(bio_s_out
);
2813 #ifndef OPENSSL_NO_DH
2814 static DH
*load_dh_param(const char *dhfile
)
2819 if ((bio
= BIO_new_file(dhfile
, "r")) == NULL
)
2821 ret
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2828 static int www_body(int s
, int stype
, int prot
, unsigned char *context
)
2834 const SSL_CIPHER
*c
;
2835 BIO
*io
, *ssl_bio
, *sbio
;
2837 int total_bytes
= 0;
2842 /* Set width for a select call if needed */
2845 buf
= app_malloc(bufsize
, "server www buffer");
2846 io
= BIO_new(BIO_f_buffer());
2847 ssl_bio
= BIO_new(BIO_f_ssl());
2848 if ((io
== NULL
) || (ssl_bio
== NULL
))
2852 if (!BIO_socket_nbio(s
, 1))
2853 ERR_print_errors(bio_err
);
2855 BIO_printf(bio_err
, "Turned on non blocking io\n");
2858 /* lets make the output buffer a reasonable size */
2859 if (!BIO_set_write_buffer_size(io
, bufsize
))
2862 if ((con
= SSL_new(ctx
)) == NULL
)
2865 if (s_tlsextdebug
) {
2866 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2867 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2871 && !SSL_set_session_id_context(con
, context
,
2872 strlen((char *)context
)))
2875 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2879 test
= BIO_new(BIO_f_nbio_test());
2880 sbio
= BIO_push(test
, sbio
);
2882 SSL_set_bio(con
, sbio
, sbio
);
2883 SSL_set_accept_state(con
);
2885 /* SSL_set_fd(con,s); */
2886 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
2887 BIO_push(io
, ssl_bio
);
2888 #ifdef CHARSET_EBCDIC
2889 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
2893 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2894 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2897 #ifndef OPENSSL_NO_SSL_TRACE
2899 SSL_set_msg_callback(con
, SSL_trace
);
2902 SSL_set_msg_callback(con
, msg_cb
);
2903 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2907 i
= BIO_gets(io
, buf
, bufsize
- 1);
2908 if (i
< 0) { /* error */
2909 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
2911 ERR_print_errors(bio_err
);
2914 BIO_printf(bio_s_out
, "read R BLOCK\n");
2915 #ifndef OPENSSL_NO_SRP
2916 if (BIO_should_io_special(io
)
2917 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
2918 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2919 SRP_user_pwd_free(srp_callback_parm
.user
);
2920 srp_callback_parm
.user
=
2921 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2922 srp_callback_parm
.login
);
2923 if (srp_callback_parm
.user
)
2924 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2925 srp_callback_parm
.user
->info
);
2927 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2931 #if !defined(OPENSSL_SYS_MSDOS)
2936 } else if (i
== 0) { /* end of input */
2941 /* else we have data */
2942 if (((www
== 1) && (strncmp("GET ", buf
, 4) == 0)) ||
2943 ((www
== 2) && (strncmp("GET /stats ", buf
, 11) == 0))) {
2946 STACK_OF(SSL_CIPHER
) *sk
;
2947 static const char *space
= " ";
2949 if (www
== 1 && strncmp("GET /reneg", buf
, 10) == 0) {
2950 if (strncmp("GET /renegcert", buf
, 14) == 0)
2952 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2954 i
= SSL_renegotiate(con
);
2955 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
2956 /* Send the HelloRequest */
2957 i
= SSL_do_handshake(con
);
2959 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
2960 SSL_get_error(con
, i
));
2961 ERR_print_errors(bio_err
);
2964 /* Wait for a ClientHello to come back */
2966 openssl_fdset(s
, &readfds
);
2967 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
2968 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
2969 BIO_printf(bio_s_out
,
2970 "Error waiting for client response\n");
2971 ERR_print_errors(bio_err
);
2975 * We're not actually expecting any data here and we ignore
2976 * any that is sent. This is just to force the handshake that
2977 * we're expecting to come from the client. If they haven't
2978 * sent one there's not much we can do.
2980 BIO_gets(io
, buf
, bufsize
- 1);
2984 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2985 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2986 BIO_puts(io
, "<pre>\n");
2987 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
2989 for (i
= 0; i
< local_argc
; i
++) {
2991 for (myp
= local_argv
[i
]; *myp
; myp
++)
2994 BIO_puts(io
, "<");
2997 BIO_puts(io
, ">");
3000 BIO_puts(io
, "&");
3003 BIO_write(io
, myp
, 1);
3006 BIO_write(io
, " ", 1);
3011 "Secure Renegotiation IS%s supported\n",
3012 SSL_get_secure_renegotiation_support(con
) ?
3016 * The following is evil and should not really be done
3018 BIO_printf(io
, "Ciphers supported in s_server binary\n");
3019 sk
= SSL_get_ciphers(con
);
3020 j
= sk_SSL_CIPHER_num(sk
);
3021 for (i
= 0; i
< j
; i
++) {
3022 c
= sk_SSL_CIPHER_value(sk
, i
);
3023 BIO_printf(io
, "%-11s:%-25s ",
3024 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3025 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
3029 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
3032 "---\nCiphers common between both SSL end points:\n");
3036 BIO_write(io
, space
, 26 - j
);
3039 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
3041 BIO_write(io
, p
, 1);
3048 ssl_print_sigalgs(io
, con
);
3049 #ifndef OPENSSL_NO_EC
3050 ssl_print_groups(io
, con
, 0);
3052 print_ca_names(io
, con
);
3053 BIO_printf(io
, (SSL_session_reused(con
)
3054 ? "---\nReused, " : "---\nNew, "));
3055 c
= SSL_get_current_cipher(con
);
3056 BIO_printf(io
, "%s, Cipher is %s\n",
3057 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3058 SSL_SESSION_print(io
, SSL_get_session(con
));
3059 BIO_printf(io
, "---\n");
3060 print_stats(io
, SSL_get_SSL_CTX(con
));
3061 BIO_printf(io
, "---\n");
3062 peer
= SSL_get_peer_certificate(con
);
3064 BIO_printf(io
, "Client certificate\n");
3065 X509_print(io
, peer
);
3066 PEM_write_bio_X509(io
, peer
);
3070 BIO_puts(io
, "no client certificate available\n");
3071 BIO_puts(io
, "</BODY></HTML>\r\n\r\n");
3073 } else if ((www
== 2 || www
== 3)
3074 && (strncmp("GET /", buf
, 5) == 0)) {
3077 static const char *text
=
3078 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3084 for (e
= p
; *e
!= '\0'; e
++) {
3090 dot
= (e
[0] == '.') ? 2 : 0;
3093 dot
= (e
[0] == '.') ? 3 : 0;
3096 dot
= (e
[0] == '/') ? -1 : 0;
3100 dot
= (e
[0] == '/') ? 1 : 0;
3102 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3107 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3114 BIO_printf(io
, "'%s' contains '..' reference\r\n", p
);
3120 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3124 /* if a directory, do the index thang */
3125 if (app_isdir(p
) > 0) {
3127 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3131 if ((file
= BIO_new_file(p
, "r")) == NULL
) {
3133 BIO_printf(io
, "Error opening '%s'\r\n", p
);
3134 ERR_print_errors(io
);
3139 BIO_printf(bio_err
, "FILE:%s\n", p
);
3143 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3144 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3145 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3147 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3150 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3154 i
= BIO_read(file
, buf
, bufsize
);
3160 BIO_printf(bio_err
, "%d\n", i
);
3161 if (total_bytes
> 3 * 1024) {
3163 BIO_printf(bio_err
, "RENEGOTIATE\n");
3164 SSL_renegotiate(con
);
3168 for (j
= 0; j
< i
;) {
3171 if (++count
== 13) {
3172 SSL_renegotiate(con
);
3175 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3177 if (!BIO_should_retry(io
)
3178 && !SSL_waiting_for_async(con
))
3181 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3195 i
= (int)BIO_flush(io
);
3197 if (!BIO_should_retry(io
))
3203 /* make sure we re-use sessions */
3204 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3208 BIO_printf(bio_s_out
, "ACCEPT\n");
3214 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
)
3220 BIO
*io
, *ssl_bio
, *sbio
;
3222 buf
= app_malloc(bufsize
, "server rev buffer");
3223 io
= BIO_new(BIO_f_buffer());
3224 ssl_bio
= BIO_new(BIO_f_ssl());
3225 if ((io
== NULL
) || (ssl_bio
== NULL
))
3228 /* lets make the output buffer a reasonable size */
3229 if (!BIO_set_write_buffer_size(io
, bufsize
))
3232 if ((con
= SSL_new(ctx
)) == NULL
)
3235 if (s_tlsextdebug
) {
3236 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3237 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3240 && !SSL_set_session_id_context(con
, context
,
3241 strlen((char *)context
))) {
3242 ERR_print_errors(bio_err
);
3246 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3247 SSL_set_bio(con
, sbio
, sbio
);
3248 SSL_set_accept_state(con
);
3250 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3251 BIO_push(io
, ssl_bio
);
3252 #ifdef CHARSET_EBCDIC
3253 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
3257 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
3258 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3261 #ifndef OPENSSL_NO_SSL_TRACE
3263 SSL_set_msg_callback(con
, SSL_trace
);
3266 SSL_set_msg_callback(con
, msg_cb
);
3267 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3271 i
= BIO_do_handshake(io
);
3274 if (!BIO_should_retry(io
)) {
3275 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3276 ERR_print_errors(bio_err
);
3279 #ifndef OPENSSL_NO_SRP
3280 if (BIO_should_io_special(io
)
3281 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3282 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3283 SRP_user_pwd_free(srp_callback_parm
.user
);
3284 srp_callback_parm
.user
=
3285 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3286 srp_callback_parm
.login
);
3287 if (srp_callback_parm
.user
)
3288 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3289 srp_callback_parm
.user
->info
);
3291 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3296 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3297 print_ssl_summary(con
);
3300 i
= BIO_gets(io
, buf
, bufsize
- 1);
3301 if (i
< 0) { /* error */
3302 if (!BIO_should_retry(io
)) {
3304 ERR_print_errors(bio_err
);
3307 BIO_printf(bio_s_out
, "read R BLOCK\n");
3308 #ifndef OPENSSL_NO_SRP
3309 if (BIO_should_io_special(io
)
3310 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3311 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3312 SRP_user_pwd_free(srp_callback_parm
.user
);
3313 srp_callback_parm
.user
=
3314 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3315 srp_callback_parm
.login
);
3316 if (srp_callback_parm
.user
)
3317 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3318 srp_callback_parm
.user
->info
);
3320 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3324 #if !defined(OPENSSL_SYS_MSDOS)
3329 } else if (i
== 0) { /* end of input */
3331 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3334 char *p
= buf
+ i
- 1;
3335 while (i
&& (*p
== '\n' || *p
== '\r')) {
3339 if (!s_ign_eof
&& (i
== 5) && (strncmp(buf
, "CLOSE", 5) == 0)) {
3341 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3344 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3346 BIO_write(io
, buf
, i
+ 1);
3351 if (!BIO_should_retry(io
))
3357 /* make sure we re-use sessions */
3358 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3367 #define MAX_SESSION_ID_ATTEMPTS 10
3368 static int generate_session_id(const SSL
*ssl
, unsigned char *id
,
3369 unsigned int *id_len
)
3371 unsigned int count
= 0;
3373 if (RAND_bytes(id
, *id_len
) <= 0)
3376 * Prefix the session_id with the required prefix. NB: If our prefix
3377 * is too long, clip it - but there will be worse effects anyway, eg.
3378 * the server could only possibly create 1 session ID (ie. the
3379 * prefix!) so all future session negotiations will fail due to
3382 memcpy(id
, session_id_prefix
,
3383 (strlen(session_id_prefix
) < *id_len
) ?
3384 strlen(session_id_prefix
) : *id_len
);
3386 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3387 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3388 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3394 * By default s_server uses an in-memory cache which caches SSL_SESSION
3395 * structures without any serialisation. This hides some bugs which only
3396 * become apparent in deployed servers. By implementing a basic external
3397 * session cache some issues can be debugged using s_server.
3400 typedef struct simple_ssl_session_st
{
3405 struct simple_ssl_session_st
*next
;
3406 } simple_ssl_session
;
3408 static simple_ssl_session
*first
= NULL
;
3410 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3412 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3415 SSL_SESSION_get_id(session
, &sess
->idlen
);
3416 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3417 if (sess
->derlen
< 0) {
3418 BIO_printf(bio_err
, "Error encoding session\n");
3423 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3424 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3426 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3427 OPENSSL_free(sess
->id
);
3428 OPENSSL_free(sess
->der
);
3434 /* Assume it still works. */
3435 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3436 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3437 OPENSSL_free(sess
->id
);
3438 OPENSSL_free(sess
->der
);
3445 BIO_printf(bio_err
, "New session added to external cache\n");
3449 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3452 simple_ssl_session
*sess
;
3454 for (sess
= first
; sess
; sess
= sess
->next
) {
3455 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3456 const unsigned char *p
= sess
->der
;
3457 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3458 return d2i_SSL_SESSION(NULL
, &p
, sess
->derlen
);
3461 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3465 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3467 simple_ssl_session
*sess
, *prev
= NULL
;
3468 const unsigned char *id
;
3470 id
= SSL_SESSION_get_id(session
, &idlen
);
3471 for (sess
= first
; sess
; sess
= sess
->next
) {
3472 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3474 prev
->next
= sess
->next
;
3477 OPENSSL_free(sess
->id
);
3478 OPENSSL_free(sess
->der
);
3486 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3488 SSL_CTX_set_session_cache_mode(sctx
,
3489 SSL_SESS_CACHE_NO_INTERNAL
|
3490 SSL_SESS_CACHE_SERVER
);
3491 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3492 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3493 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3496 static void free_sessions(void)
3498 simple_ssl_session
*sess
, *tsess
;
3499 for (sess
= first
; sess
;) {
3500 OPENSSL_free(sess
->id
);
3501 OPENSSL_free(sess
->der
);
3504 OPENSSL_free(tsess
);
3509 #endif /* OPENSSL_NO_SOCK */