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
, unsigned char *context
);
95 static int www_body(int s
, int stype
, unsigned char *context
);
96 static int rev_body(int s
, int stype
, 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 int load_CA(SSL_CTX *ctx, char *file);*/
111 static const int bufsize
= 16 * 1024;
112 static int accept_socket
= -1;
114 #define TEST_CERT "server.pem"
115 #define TEST_CERT2 "server2.pem"
117 static int s_nbio
= 0;
118 static int s_nbio_test
= 0;
119 static int s_crlf
= 0;
120 static SSL_CTX
*ctx
= NULL
;
121 static SSL_CTX
*ctx2
= NULL
;
124 static BIO
*bio_s_out
= NULL
;
125 static BIO
*bio_s_msg
= NULL
;
126 static int s_debug
= 0;
127 static int s_tlsextdebug
= 0;
128 static int s_msg
= 0;
129 static int s_quiet
= 0;
130 static int s_ign_eof
= 0;
131 static int s_brief
= 0;
133 static char *keymatexportlabel
= NULL
;
134 static int keymatexportlen
= 20;
136 static int async
= 0;
138 static const char *session_id_prefix
= NULL
;
140 #ifndef OPENSSL_NO_DTLS
141 static int enable_timeouts
= 0;
142 static long socket_mtu
;
145 static int dtlslisten
= 0;
147 #ifndef OPENSSL_NO_PSK
148 static char *psk_identity
= "Client_identity";
149 char *psk_key
= NULL
; /* by default PSK is not used */
153 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
155 unsigned int max_psk_len
)
161 BIO_printf(bio_s_out
, "psk_server_cb\n");
163 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
167 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
168 (int)strlen(identity
), identity
);
170 /* here we could lookup the given identity e.g. from a database */
171 if (strcmp(identity
, psk_identity
) != 0) {
172 BIO_printf(bio_s_out
, "PSK error: client identity not found"
173 " (got '%s' expected '%s')\n", identity
, psk_identity
);
177 BIO_printf(bio_s_out
, "PSK client identity found\n");
179 /* convert the PSK key to binary */
180 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
182 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
186 if (key_len
> (int)max_psk_len
) {
188 "psk buffer of callback is too small (%d) for key (%ld)\n",
189 max_psk_len
, key_len
);
194 memcpy(psk
, key
, key_len
);
198 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
202 BIO_printf(bio_err
, "Error in PSK server callback\n");
203 (void)BIO_flush(bio_err
);
204 (void)BIO_flush(bio_s_out
);
209 #ifndef OPENSSL_NO_SRP
210 /* This is a context that we pass to callbacks */
211 typedef struct srpsrvparm_st
{
218 * This callback pretends to require some asynchronous logic in order to
219 * obtain a verifier. When the callback is called for a new connection we
220 * return with a negative value. This will provoke the accept etc to return
221 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
222 * (which would normally occur after a worker has finished) and we set the
225 static int ssl_srp_server_param_cb(SSL
*s
, int *ad
, void *arg
)
227 srpsrvparm
*p
= (srpsrvparm
*) arg
;
228 int ret
= SSL3_AL_FATAL
;
230 if (p
->login
== NULL
&& p
->user
== NULL
) {
231 p
->login
= SSL_get_srp_username(s
);
232 BIO_printf(bio_err
, "SRP username = \"%s\"\n", p
->login
);
236 if (p
->user
== NULL
) {
237 BIO_printf(bio_err
, "User %s doesn't exist\n", p
->login
);
241 if (SSL_set_srp_server_param
242 (s
, p
->user
->N
, p
->user
->g
, p
->user
->s
, p
->user
->v
,
243 p
->user
->info
) < 0) {
244 *ad
= SSL_AD_INTERNAL_ERROR
;
248 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
249 p
->login
, p
->user
->info
);
250 ret
= SSL_ERROR_NONE
;
253 SRP_user_pwd_free(p
->user
);
261 static int local_argc
= 0;
262 static char **local_argv
;
264 #ifdef CHARSET_EBCDIC
265 static int ebcdic_new(BIO
*bi
);
266 static int ebcdic_free(BIO
*a
);
267 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
268 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
269 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
270 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
271 static int ebcdic_puts(BIO
*bp
, const char *str
);
273 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
274 static BIO_METHOD
*methods_ebcdic
= NULL
;
276 /* This struct is "unwarranted chumminess with the compiler." */
282 static const BIO_METHOD
*BIO_f_ebcdic_filter()
284 if (methods_ebcdic
== NULL
) {
285 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
286 "EBCDIC/ASCII filter");
287 if (methods_ebcdic
== NULL
288 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
289 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
290 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
291 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
292 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
293 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
294 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
297 return methods_ebcdic
;
300 static int ebcdic_new(BIO
*bi
)
302 EBCDIC_OUTBUFF
*wbuf
;
304 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
305 wbuf
->alloced
= 1024;
306 wbuf
->buff
[0] = '\0';
308 BIO_set_data(bi
, wbuf
);
313 static int ebcdic_free(BIO
*a
)
315 EBCDIC_OUTBUFF
*wbuf
;
319 wbuf
= BIO_get_data(a
);
321 BIO_set_data(a
, NULL
);
327 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
330 BIO
*next
= BIO_next(b
);
332 if (out
== NULL
|| outl
== 0)
337 ret
= BIO_read(next
, out
, outl
);
339 ascii2ebcdic(out
, out
, ret
);
343 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
345 EBCDIC_OUTBUFF
*wbuf
;
346 BIO
*next
= BIO_next(b
);
350 if ((in
== NULL
) || (inl
<= 0))
355 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
357 if (inl
> (num
= wbuf
->alloced
)) {
358 num
= num
+ num
; /* double the size */
362 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
365 wbuf
->buff
[0] = '\0';
367 BIO_set_data(b
, wbuf
);
370 ebcdic2ascii(wbuf
->buff
, in
, inl
);
372 ret
= BIO_write(next
, wbuf
->buff
, inl
);
377 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
380 BIO
*next
= BIO_next(b
);
389 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
395 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
398 BIO
*next
= BIO_next(bp
);
402 /* return(BIO_gets(bp->next_bio,buf,size));*/
403 for (i
= 0; i
< size
- 1; ++i
) {
404 ret
= ebcdic_read(bp
, &buf
[i
], 1);
407 else if (buf
[i
] == '\n') {
414 return (ret
< 0 && i
== 0) ? ret
: i
;
417 static int ebcdic_puts(BIO
*bp
, const char *str
)
419 if (BIO_next(bp
) == NULL
)
421 return ebcdic_write(bp
, str
, strlen(str
));
425 /* This is a context that we pass to callbacks */
426 typedef struct tlsextctx_st
{
432 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
434 tlsextctx
*p
= (tlsextctx
*) arg
;
435 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
436 if (servername
&& p
->biodebug
)
437 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"%s\"\n",
441 return SSL_TLSEXT_ERR_NOACK
;
444 if (strcasecmp(servername
, p
->servername
))
445 return p
->extension_error
;
447 BIO_printf(p
->biodebug
, "Switching server context.\n");
448 SSL_set_SSL_CTX(s
, ctx2
);
451 return SSL_TLSEXT_ERR_OK
;
454 /* Structure passed to cert status callback */
456 typedef struct tlsextstatusctx_st
{
458 /* File to load OCSP Response from (or NULL if no file) */
460 /* Default responder to use */
461 char *host
, *path
, *port
;
466 static tlsextstatusctx tlscstatp
= { -1 };
468 #ifndef OPENSSL_NO_OCSP
471 * Helper function to get an OCSP_RESPONSE from a responder. This is a
472 * simplified version. It examines certificates each time and makes one OCSP
473 * responder query for each request. A full version would store details such as
474 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
475 * them until they were considered "expired".
477 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
478 OCSP_RESPONSE
**resp
)
480 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
482 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
484 X509_STORE_CTX
*inctx
= NULL
;
486 OCSP_REQUEST
*req
= NULL
;
487 OCSP_CERTID
*id
= NULL
;
488 STACK_OF(X509_EXTENSION
) *exts
;
489 int ret
= SSL_TLSEXT_ERR_NOACK
;
492 /* Build up OCSP query from server certificate */
493 x
= SSL_get_certificate(s
);
494 aia
= X509_get1_ocsp(x
);
496 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia
, 0),
497 &host
, &port
, &path
, &use_ssl
)) {
498 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
502 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
503 sk_OPENSSL_STRING_value(aia
, 0));
507 "cert_status: no AIA and no default responder URL\n");
513 use_ssl
= srctx
->use_ssl
;
516 inctx
= X509_STORE_CTX_new();
519 if (!X509_STORE_CTX_init(inctx
,
520 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s
)),
523 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
,
524 X509_get_issuer_name(x
));
526 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
529 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
530 X509_OBJECT_free(obj
);
533 req
= OCSP_REQUEST_new();
536 if (!OCSP_request_add0_id(req
, id
))
539 /* Add any extensions to the request */
540 SSL_get_tlsext_status_exts(s
, &exts
);
541 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
542 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
543 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
546 *resp
= process_responder(req
, host
, path
, port
, use_ssl
, NULL
,
549 BIO_puts(bio_err
, "cert_status: error querying responder\n");
553 ret
= SSL_TLSEXT_ERR_OK
;
557 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
560 * If we parsed aia we need to free; otherwise they were copied and we
567 X509_email_free(aia
);
569 OCSP_CERTID_free(id
);
570 OCSP_REQUEST_free(req
);
571 X509_STORE_CTX_free(inctx
);
576 * Certificate Status callback. This is called when a client includes a
577 * certificate status request extension. The response is either obtained from a
578 * file, or from an OCSP responder.
580 static int cert_status_cb(SSL
*s
, void *arg
)
582 tlsextstatusctx
*srctx
= arg
;
583 OCSP_RESPONSE
*resp
= NULL
;
584 unsigned char *rspder
= NULL
;
586 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
589 BIO_puts(bio_err
, "cert_status: callback called\n");
591 if (srctx
->respin
!= NULL
) {
592 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
593 if (derbio
== NULL
) {
594 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
597 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
600 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
604 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
605 if (ret
!= SSL_TLSEXT_ERR_OK
)
609 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
613 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
614 if (srctx
->verbose
) {
615 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
616 OCSP_RESPONSE_print(bio_err
, resp
, 2);
619 ret
= SSL_TLSEXT_ERR_OK
;
622 if (ret
!= SSL_TLSEXT_ERR_OK
)
623 ERR_print_errors(bio_err
);
625 OCSP_RESPONSE_free(resp
);
631 #ifndef OPENSSL_NO_NEXTPROTONEG
632 /* This is the context that we pass to next_proto_cb */
633 typedef struct tlsextnextprotoctx_st
{
636 } tlsextnextprotoctx
;
638 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
639 unsigned int *len
, void *arg
)
641 tlsextnextprotoctx
*next_proto
= arg
;
643 *data
= next_proto
->data
;
644 *len
= next_proto
->len
;
646 return SSL_TLSEXT_ERR_OK
;
648 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
650 /* This the context that we pass to alpn_cb */
651 typedef struct tlsextalpnctx_st
{
656 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
657 const unsigned char *in
, unsigned int inlen
, void *arg
)
659 tlsextalpnctx
*alpn_ctx
= arg
;
662 /* We can assume that |in| is syntactically valid. */
664 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
665 for (i
= 0; i
< inlen
;) {
667 BIO_write(bio_s_out
, ", ", 2);
668 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
671 BIO_write(bio_s_out
, "\n", 1);
674 if (SSL_select_next_proto
675 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
676 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
677 return SSL_TLSEXT_ERR_NOACK
;
681 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
682 BIO_write(bio_s_out
, *out
, *outlen
);
683 BIO_write(bio_s_out
, "\n", 1);
686 return SSL_TLSEXT_ERR_OK
;
689 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
691 /* disable resumption for sessions with forward secure ciphers */
692 return is_forward_secure
;
695 #ifndef OPENSSL_NO_SRP
696 static srpsrvparm srp_callback_parm
;
698 #ifndef OPENSSL_NO_SRTP
699 static char *srtp_profiles
= NULL
;
702 typedef enum OPTION_choice
{
703 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ENGINE
,
704 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
705 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
706 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
707 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
708 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
709 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
710 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
711 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
712 OPT_VERIFYCAFILE
, OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
713 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
714 OPT_STATUS_TIMEOUT
, OPT_STATUS_URL
, OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
715 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
716 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
717 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_HINT
, OPT_PSK
, OPT_SRPVFILE
,
718 OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
, OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
,
719 OPT_SSL_CONFIG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
720 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
721 OPT_DTLS1_2
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
,
722 OPT_ID_PREFIX
, OPT_RAND
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
723 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
,
724 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
725 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_EARLY_DATA
,
731 const OPTIONS s_server_options
[] = {
732 {"help", OPT_HELP
, '-', "Display this summary"},
733 {"port", OPT_PORT
, 'p',
734 "TCP/IP port to listen on for connections (default is " PORT
")"},
735 {"accept", OPT_ACCEPT
, 's',
736 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
738 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
740 {"4", OPT_4
, '-', "Use IPv4 only"},
741 {"6", OPT_6
, '-', "Use IPv6 only"},
743 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
745 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
746 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
747 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
748 "Turn on peer certificate verification, must have a cert"},
749 {"cert", OPT_CERT
, '<', "Certificate file to use; default is " TEST_CERT
},
750 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
751 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
752 {"serverinfo", OPT_SERVERINFO
, 's',
753 "PEM serverinfo file for certificate"},
754 {"certform", OPT_CERTFORM
, 'F',
755 "Certificate format (PEM or DER) PEM default"},
756 {"key", OPT_KEY
, '<',
757 "Private Key if not in -cert; default is " TEST_CERT
},
758 {"keyform", OPT_KEYFORM
, 'f',
759 "Key format (PEM, DER or ENGINE) PEM default"},
760 {"pass", OPT_PASS
, 's', "Private key file pass phrase source"},
761 {"dcert", OPT_DCERT
, '<',
762 "Second certificate file to use (usually for DSA)"},
763 {"dcertform", OPT_DCERTFORM
, 'F',
764 "Second certificate format (PEM or DER) PEM default"},
765 {"dkey", OPT_DKEY
, '<',
766 "Second private key file to use (usually for DSA)"},
767 {"dkeyform", OPT_DKEYFORM
, 'F',
768 "Second key format (PEM, DER or ENGINE) PEM default"},
769 {"dpass", OPT_DPASS
, 's', "Second private key file pass phrase source"},
770 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
771 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
772 {"debug", OPT_DEBUG
, '-', "Print more output"},
773 {"msg", OPT_MSG
, '-', "Show protocol messages"},
774 {"msgfile", OPT_MSGFILE
, '>',
775 "File to send output of -msg or -trace, instead of stdout"},
776 {"state", OPT_STATE
, '-', "Print the SSL states"},
777 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
778 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
779 {"no-CAfile", OPT_NOCAFILE
, '-',
780 "Do not load the default certificates file"},
781 {"no-CApath", OPT_NOCAPATH
, '-',
782 "Do not load certificates from the default certificates directory"},
783 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
784 {"quiet", OPT_QUIET
, '-', "No server output"},
785 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
786 "Disable caching and tickets if ephemeral (EC)DH is used"},
787 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
788 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
789 {"servername", OPT_SERVERNAME
, 's',
790 "Servername for HostName TLS extension"},
791 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
792 "mismatch send fatal alert (default warning alert)"},
793 {"cert2", OPT_CERT2
, '<',
794 "Certificate file to use for servername; default is" TEST_CERT2
},
795 {"key2", OPT_KEY2
, '<',
796 "-Private Key file to use for servername if not in -cert2"},
797 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
798 "Hex dump of all TLS extensions received"},
799 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
800 {"id_prefix", OPT_ID_PREFIX
, 's',
801 "Generate SSL/TLS session IDs prefixed by arg"},
802 {"rand", OPT_RAND
, 's',
803 "Load the file(s) into the random number generator"},
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 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
810 "Download CRL from distribution points"},
811 {"cert_chain", OPT_CERT_CHAIN
, '<',
812 "certificate chain file in PEM format"},
813 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
814 "second certificate chain file in PEM format"},
815 {"chainCApath", OPT_CHAINCAPATH
, '/',
816 "use dir as certificate store path to build CA certificate chain"},
817 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
818 "use dir as certificate store path to verify CA certificate"},
819 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
820 {"ext_cache", OPT_EXT_CACHE
, '-',
821 "Disable internal cache, setup and use external cache"},
822 {"CRLform", OPT_CRLFORM
, 'F', "CRL format (PEM or DER) PEM is default"},
823 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
824 "Close connection on verification error"},
825 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
826 "No verify output except verify errors"},
827 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build certificate chain"},
828 {"chainCAfile", OPT_CHAINCAFILE
, '<',
829 "CA file for certificate chain (PEM format)"},
830 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
831 "CA file for certificate verification (PEM format)"},
832 {"ign_eof", OPT_IGN_EOF
, '-', "ignore input eof (default when -quiet)"},
833 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input eof"},
834 #ifndef OPENSSL_NO_OCSP
835 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
836 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
837 "Print more output in certificate status callback"},
838 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
839 "Status request responder timeout"},
840 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
841 {"status_file", OPT_STATUS_FILE
, '<',
842 "File containing DER encoded OCSP Response"},
844 #ifndef OPENSSL_NO_SSL_TRACE
845 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
847 {"security_debug", OPT_SECURITY_DEBUG
, '-',
848 "Print output from SSL/TLS security framework"},
849 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
850 "Print more output from SSL/TLS security framework"},
851 {"brief", OPT_BRIEF
, '-',
852 "Restrict output to brief summary of connection parameters"},
853 {"rev", OPT_REV
, '-',
854 "act as a simple test server which just sends back with the received text reversed"},
855 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
856 {"ssl_config", OPT_SSL_CONFIG
, 's',
857 "Configure SSL_CTX using the configuration 'val'"},
858 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'n',
859 "Size used to split data for encrypt pipelines"},
860 {"max_pipelines", OPT_MAX_PIPELINES
, 'n',
861 "Maximum number of encrypt/decrypt pipelines to be used"},
862 {"read_buf", OPT_READ_BUF
, 'n',
863 "Default read buffer size to be used for connections"},
867 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
868 #ifndef OPENSSL_NO_PSK
869 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
870 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
872 #ifndef OPENSSL_NO_SRP
873 {"srpvfile", OPT_SRPVFILE
, '<', "The verifier file for SRP"},
874 {"srpuserseed", OPT_SRPUSERSEED
, 's',
875 "A seed string for a default user salt"},
877 #ifndef OPENSSL_NO_SSL3
878 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
880 #ifndef OPENSSL_NO_TLS1
881 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
883 #ifndef OPENSSL_NO_TLS1_1
884 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
886 #ifndef OPENSSL_NO_TLS1_2
887 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
889 #ifndef OPENSSL_NO_TLS1_3
890 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
892 #ifndef OPENSSL_NO_DTLS
893 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
894 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
895 {"mtu", OPT_MTU
, 'p', "Set link layer MTU"},
896 {"listen", OPT_LISTEN
, '-',
897 "Listen for a DTLS ClientHello with a cookie and then connect"},
899 #ifndef OPENSSL_NO_DTLS1
900 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
902 #ifndef OPENSSL_NO_DTLS1_2
903 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
905 #ifndef OPENSSL_NO_DH
906 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
908 #ifndef OPENSSL_NO_NEXTPROTONEG
909 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
910 "Set the advertised protocols for the NPN extension (comma-separated list)"},
912 #ifndef OPENSSL_NO_SRTP
913 {"use_srtp", OPT_SRTP_PROFILES
, 's',
914 "Offer SRTP key management with a colon-separated profile list"},
916 {"alpn", OPT_ALPN
, 's',
917 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
918 #ifndef OPENSSL_NO_ENGINE
919 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
921 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
922 {"max_early_data", OPT_MAX_EARLY
, 'n',
923 "The maximum number of bytes of early data"},
924 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
925 {NULL
, OPT_EOF
, 0, NULL
}
928 #define IS_PROT_FLAG(o) \
929 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
930 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
932 int s_server_main(int argc
, char *argv
[])
934 ENGINE
*engine
= NULL
;
935 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
936 SSL_CONF_CTX
*cctx
= NULL
;
937 const SSL_METHOD
*meth
= TLS_server_method();
938 SSL_EXCERT
*exc
= NULL
;
939 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
940 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
941 STACK_OF(X509_CRL
) *crls
= NULL
;
942 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
943 X509_VERIFY_PARAM
*vpm
= NULL
;
944 const char *CApath
= NULL
, *CAfile
= NULL
, *chCApath
= NULL
, *chCAfile
= NULL
;
945 char *dpassarg
= NULL
, *dpass
= NULL
, *inrand
= NULL
;
946 char *passarg
= NULL
, *pass
= NULL
, *vfyCApath
= NULL
, *vfyCAfile
= NULL
;
947 char *crl_file
= NULL
, *prog
;
949 int unlink_unix_path
= 0;
951 do_server_cb server_cb
;
952 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
953 #ifndef OPENSSL_NO_DH
957 int nocert
= 0, ret
= 1;
958 int noCApath
= 0, noCAfile
= 0;
959 int s_cert_format
= FORMAT_PEM
, s_key_format
= FORMAT_PEM
;
960 int s_dcert_format
= FORMAT_PEM
, s_dkey_format
= FORMAT_PEM
;
961 int rev
= 0, naccept
= -1, sdebug
= 0;
962 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
;
963 int state
= 0, crl_format
= FORMAT_PEM
, crl_download
= 0;
965 char *port
= BUF_strdup(PORT
);
966 unsigned char *context
= NULL
;
968 EVP_PKEY
*s_key2
= NULL
;
969 X509
*s_cert2
= NULL
;
970 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
971 const char *ssl_config
= NULL
;
972 int read_buf_len
= 0;
973 #ifndef OPENSSL_NO_NEXTPROTONEG
974 const char *next_proto_neg_in
= NULL
;
975 tlsextnextprotoctx next_proto
= { NULL
, 0 };
977 const char *alpn_in
= NULL
;
978 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
979 #ifndef OPENSSL_NO_PSK
980 /* by default do not send a PSK identity hint */
981 static char *psk_identity_hint
= NULL
;
984 #ifndef OPENSSL_NO_SRP
985 char *srpuserseed
= NULL
;
986 char *srp_verifier_file
= NULL
;
988 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
989 int s_server_verify
= SSL_VERIFY_NONE
;
990 int s_server_session_id_context
= 1; /* anything will do */
991 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
992 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
993 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
994 #ifndef OPENSSL_NO_OCSP
995 int s_tlsextstatus
= 0;
997 int no_resume_ephemeral
= 0;
998 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
999 const char *s_serverinfo_file
= NULL
;
1000 const char *keylog_file
= NULL
;
1001 int max_early_data
= -1;
1003 /* Init of few remaining global variables */
1008 s_nbio
= s_nbio_test
= 0;
1017 cctx
= SSL_CONF_CTX_new();
1018 vpm
= X509_VERIFY_PARAM_new();
1019 if (cctx
== NULL
|| vpm
== NULL
)
1021 SSL_CONF_CTX_set_flags(cctx
,
1022 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1024 prog
= opt_init(argc
, argv
, s_server_options
);
1025 while ((o
= opt_next()) != OPT_EOF
) {
1026 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1027 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1030 if (IS_NO_PROT_FLAG(o
))
1032 if (prot_opt
== 1 && no_prot_opt
) {
1034 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1041 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1044 opt_help(s_server_options
);
1050 if (socket_family
== AF_UNIX
) {
1051 OPENSSL_free(host
); host
= NULL
;
1052 OPENSSL_free(port
); port
= NULL
;
1055 socket_family
= AF_INET
;
1061 if (socket_family
== AF_UNIX
) {
1062 OPENSSL_free(host
); host
= NULL
;
1063 OPENSSL_free(port
); port
= NULL
;
1066 socket_family
= AF_INET6
;
1069 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1075 if (socket_family
== AF_UNIX
) {
1076 socket_family
= AF_UNSPEC
;
1079 OPENSSL_free(port
); port
= NULL
;
1080 OPENSSL_free(host
); host
= NULL
;
1081 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1083 "%s: -port argument malformed or ambiguous\n",
1090 if (socket_family
== AF_UNIX
) {
1091 socket_family
= AF_UNSPEC
;
1094 OPENSSL_free(port
); port
= NULL
;
1095 OPENSSL_free(host
); host
= NULL
;
1096 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1098 "%s: -accept argument malformed or ambiguous\n",
1105 socket_family
= AF_UNIX
;
1106 OPENSSL_free(host
); host
= BUF_strdup(opt_arg());
1107 OPENSSL_free(port
); port
= NULL
;
1110 unlink_unix_path
= 1;
1114 naccept
= atol(opt_arg());
1117 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1118 verify_args
.depth
= atoi(opt_arg());
1120 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1122 case OPT_UPPER_V_VERIFY
:
1124 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1125 SSL_VERIFY_CLIENT_ONCE
;
1126 verify_args
.depth
= atoi(opt_arg());
1129 "verify depth is %d, must return a certificate\n",
1133 context
= (unsigned char *)opt_arg();
1136 s_cert_file
= opt_arg();
1139 if (!set_nameopt(opt_arg()))
1143 crl_file
= opt_arg();
1145 case OPT_CRL_DOWNLOAD
:
1148 case OPT_SERVERINFO
:
1149 s_serverinfo_file
= opt_arg();
1152 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_cert_format
))
1156 s_key_file
= opt_arg();
1159 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1163 passarg
= opt_arg();
1165 case OPT_CERT_CHAIN
:
1166 s_chain_file
= opt_arg();
1169 #ifndef OPENSSL_NO_DH
1174 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dcert_format
))
1178 s_dcert_file
= opt_arg();
1181 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dkey_format
))
1185 dpassarg
= opt_arg();
1188 s_dkey_file
= opt_arg();
1190 case OPT_DCERT_CHAIN
:
1191 s_dchain_file
= opt_arg();
1202 case OPT_CHAINCAPATH
:
1203 chCApath
= opt_arg();
1205 case OPT_VERIFYCAPATH
:
1206 vfyCApath
= opt_arg();
1215 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1219 if (ssl_args
== NULL
)
1220 ssl_args
= sk_OPENSSL_STRING_new_null();
1221 if (ssl_args
== NULL
1222 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1223 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1224 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1229 if (!opt_verify(o
, vpm
))
1234 if (!args_excert(o
, &exc
))
1237 case OPT_VERIFY_RET_ERROR
:
1238 verify_args
.return_error
= 1;
1240 case OPT_VERIFY_QUIET
:
1241 verify_args
.quiet
= 1;
1243 case OPT_BUILD_CHAIN
:
1252 case OPT_CHAINCAFILE
:
1253 chCAfile
= opt_arg();
1255 case OPT_VERIFYCAFILE
:
1256 vfyCAfile
= opt_arg();
1262 s_nbio
= s_nbio_test
= 1;
1267 case OPT_NO_IGN_EOF
:
1273 case OPT_TLSEXTDEBUG
:
1277 #ifndef OPENSSL_NO_OCSP
1281 case OPT_STATUS_VERBOSE
:
1282 #ifndef OPENSSL_NO_OCSP
1283 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1286 case OPT_STATUS_TIMEOUT
:
1287 #ifndef OPENSSL_NO_OCSP
1289 tlscstatp
.timeout
= atoi(opt_arg());
1292 case OPT_STATUS_URL
:
1293 #ifndef OPENSSL_NO_OCSP
1295 if (!OCSP_parse_url(opt_arg(),
1298 &tlscstatp
.path
, &tlscstatp
.use_ssl
)) {
1299 BIO_printf(bio_err
, "Error parsing URL\n");
1304 case OPT_STATUS_FILE
:
1305 #ifndef OPENSSL_NO_OCSP
1307 tlscstatp
.respin
= opt_arg();
1314 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1317 #ifndef OPENSSL_NO_SSL_TRACE
1321 case OPT_SECURITY_DEBUG
:
1324 case OPT_SECURITY_DEBUG_VERBOSE
:
1337 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1340 #ifndef OPENSSL_NO_DH
1344 case OPT_NO_RESUME_EPHEMERAL
:
1345 no_resume_ephemeral
= 1;
1348 #ifndef OPENSSL_NO_PSK
1349 psk_identity_hint
= opt_arg();
1353 #ifndef OPENSSL_NO_PSK
1354 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1355 if (isxdigit(_UC(*p
)))
1357 BIO_printf(bio_err
, "Not a hex number '%s'\n", *argv
);
1363 #ifndef OPENSSL_NO_SRP
1364 srp_verifier_file
= opt_arg();
1365 if (min_version
< TLS1_VERSION
)
1366 min_version
= TLS1_VERSION
;
1369 case OPT_SRPUSERSEED
:
1370 #ifndef OPENSSL_NO_SRP
1371 srpuserseed
= opt_arg();
1372 if (min_version
< TLS1_VERSION
)
1373 min_version
= TLS1_VERSION
;
1388 case OPT_SSL_CONFIG
:
1389 ssl_config
= opt_arg();
1392 min_version
= SSL3_VERSION
;
1393 max_version
= SSL3_VERSION
;
1396 min_version
= TLS1_3_VERSION
;
1397 max_version
= TLS1_3_VERSION
;
1400 min_version
= TLS1_2_VERSION
;
1401 max_version
= TLS1_2_VERSION
;
1404 min_version
= TLS1_1_VERSION
;
1405 max_version
= TLS1_1_VERSION
;
1408 min_version
= TLS1_VERSION
;
1409 max_version
= TLS1_VERSION
;
1412 #ifndef OPENSSL_NO_DTLS
1413 meth
= DTLS_server_method();
1414 socket_type
= SOCK_DGRAM
;
1418 #ifndef OPENSSL_NO_DTLS
1419 meth
= DTLS_server_method();
1420 min_version
= DTLS1_VERSION
;
1421 max_version
= DTLS1_VERSION
;
1422 socket_type
= SOCK_DGRAM
;
1426 #ifndef OPENSSL_NO_DTLS
1427 meth
= DTLS_server_method();
1428 min_version
= DTLS1_2_VERSION
;
1429 max_version
= DTLS1_2_VERSION
;
1430 socket_type
= SOCK_DGRAM
;
1434 #ifndef OPENSSL_NO_DTLS
1435 enable_timeouts
= 1;
1439 #ifndef OPENSSL_NO_DTLS
1440 socket_mtu
= atol(opt_arg());
1444 #ifndef OPENSSL_NO_DTLS
1449 session_id_prefix
= opt_arg();
1452 engine
= setup_engine(opt_arg(), 1);
1457 case OPT_SERVERNAME
:
1458 tlsextcbp
.servername
= opt_arg();
1460 case OPT_SERVERNAME_FATAL
:
1461 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1464 s_cert_file2
= opt_arg();
1467 s_key_file2
= opt_arg();
1469 case OPT_NEXTPROTONEG
:
1470 # ifndef OPENSSL_NO_NEXTPROTONEG
1471 next_proto_neg_in
= opt_arg();
1475 alpn_in
= opt_arg();
1477 case OPT_SRTP_PROFILES
:
1478 #ifndef OPENSSL_NO_SRTP
1479 srtp_profiles
= opt_arg();
1482 case OPT_KEYMATEXPORT
:
1483 keymatexportlabel
= opt_arg();
1485 case OPT_KEYMATEXPORTLEN
:
1486 keymatexportlen
= atoi(opt_arg());
1491 case OPT_SPLIT_SEND_FRAG
:
1492 split_send_fragment
= atoi(opt_arg());
1493 if (split_send_fragment
== 0) {
1495 * Not allowed - set to a deliberately bad value so we get an
1496 * error message below
1498 split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
+ 1;
1501 case OPT_MAX_PIPELINES
:
1502 max_pipelines
= atoi(opt_arg());
1505 read_buf_len
= atoi(opt_arg());
1507 case OPT_KEYLOG_FILE
:
1508 keylog_file
= opt_arg();
1511 max_early_data
= atoi(opt_arg());
1512 if (max_early_data
< 0) {
1513 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1517 case OPT_EARLY_DATA
:
1522 argc
= opt_num_rest();
1525 #ifndef OPENSSL_NO_DTLS
1526 if (www
&& socket_type
== SOCK_DGRAM
) {
1527 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1531 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1532 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1538 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1540 "Can't use unix sockets and datagrams together\n");
1545 if (split_send_fragment
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1546 BIO_printf(bio_err
, "Bad split send fragment size\n");
1550 if (max_pipelines
> SSL_MAX_PIPELINES
) {
1551 BIO_printf(bio_err
, "Bad max pipelines value\n");
1555 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1556 BIO_printf(bio_err
, "Error getting password\n");
1560 if (s_key_file
== NULL
)
1561 s_key_file
= s_cert_file
;
1563 if (s_key_file2
== NULL
)
1564 s_key_file2
= s_cert_file2
;
1566 if (!load_excert(&exc
))
1570 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1571 "server certificate private key file");
1573 ERR_print_errors(bio_err
);
1577 s_cert
= load_cert(s_cert_file
, s_cert_format
,
1578 "server certificate file");
1581 ERR_print_errors(bio_err
);
1585 if (!load_certs(s_chain_file
, &s_chain
, FORMAT_PEM
, NULL
,
1586 "server certificate chain"))
1590 if (tlsextcbp
.servername
) {
1591 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1592 "second server certificate private key file");
1594 ERR_print_errors(bio_err
);
1598 s_cert2
= load_cert(s_cert_file2
, s_cert_format
,
1599 "second server certificate file");
1602 ERR_print_errors(bio_err
);
1607 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1608 if (next_proto_neg_in
) {
1610 next_proto
.data
= next_protos_parse(&len
, next_proto_neg_in
);
1611 if (next_proto
.data
== NULL
)
1613 next_proto
.len
= len
;
1615 next_proto
.data
= NULL
;
1618 alpn_ctx
.data
= NULL
;
1621 alpn_ctx
.data
= next_protos_parse(&len
, alpn_in
);
1622 if (alpn_ctx
.data
== NULL
)
1629 crl
= load_crl(crl_file
, crl_format
);
1631 BIO_puts(bio_err
, "Error loading CRL\n");
1632 ERR_print_errors(bio_err
);
1635 crls
= sk_X509_CRL_new_null();
1636 if (!crls
|| !sk_X509_CRL_push(crls
, crl
)) {
1637 BIO_puts(bio_err
, "Error adding CRL\n");
1638 ERR_print_errors(bio_err
);
1646 if (s_dkey_file
== NULL
)
1647 s_dkey_file
= s_dcert_file
;
1649 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1650 0, dpass
, engine
, "second certificate private key file");
1652 ERR_print_errors(bio_err
);
1656 s_dcert
= load_cert(s_dcert_file
, s_dcert_format
,
1657 "second server certificate file");
1660 ERR_print_errors(bio_err
);
1663 if (s_dchain_file
) {
1664 if (!load_certs(s_dchain_file
, &s_dchain
, FORMAT_PEM
, NULL
,
1665 "second server certificate chain"))
1671 if (!app_RAND_load_file(NULL
, 1) && inrand
== NULL
1672 && !RAND_status()) {
1674 "warning, not much extra random data, consider using the -rand option\n");
1677 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
1678 app_RAND_load_files(inrand
));
1680 if (bio_s_out
== NULL
) {
1681 if (s_quiet
&& !s_debug
) {
1682 bio_s_out
= BIO_new(BIO_s_null());
1683 if (s_msg
&& !bio_s_msg
)
1684 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1686 if (bio_s_out
== NULL
)
1687 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1690 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
1696 s_dcert_file
= NULL
;
1698 s_cert_file2
= NULL
;
1702 ctx
= SSL_CTX_new(meth
);
1704 ERR_print_errors(bio_err
);
1708 ssl_ctx_security_debug(ctx
, sdebug
);
1710 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1711 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1713 ERR_print_errors(bio_err
);
1717 if (SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1719 if (SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1722 if (session_id_prefix
) {
1723 if (strlen(session_id_prefix
) >= 32)
1725 "warning: id_prefix is too long, only one new session will be possible\n");
1726 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1727 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1728 ERR_print_errors(bio_err
);
1731 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1733 SSL_CTX_set_quiet_shutdown(ctx
, 1);
1735 ssl_ctx_set_excert(ctx
, exc
);
1738 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1740 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1742 init_session_cache_ctx(ctx
);
1744 SSL_CTX_sess_set_cache_size(ctx
, 128);
1747 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1749 if (split_send_fragment
> 0) {
1750 SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
);
1752 if (max_pipelines
> 0) {
1753 SSL_CTX_set_max_pipelines(ctx
, max_pipelines
);
1756 if (read_buf_len
> 0) {
1757 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1759 #ifndef OPENSSL_NO_SRTP
1760 if (srtp_profiles
!= NULL
) {
1761 /* Returns 0 on success! */
1762 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1763 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1764 ERR_print_errors(bio_err
);
1770 if (!ctx_set_verify_locations(ctx
, CAfile
, CApath
, noCAfile
, noCApath
)) {
1771 ERR_print_errors(bio_err
);
1774 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1775 BIO_printf(bio_err
, "Error setting verify params\n");
1776 ERR_print_errors(bio_err
);
1780 ssl_ctx_add_crls(ctx
, crls
, 0);
1781 if (!config_ctx(cctx
, ssl_args
, ctx
))
1784 if (!ssl_load_stores(ctx
, vfyCApath
, vfyCAfile
, chCApath
, chCAfile
,
1785 crls
, crl_download
)) {
1786 BIO_printf(bio_err
, "Error loading store locations\n");
1787 ERR_print_errors(bio_err
);
1792 ctx2
= SSL_CTX_new(meth
);
1794 ERR_print_errors(bio_err
);
1800 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
1803 ssl_ctx_security_debug(ctx
, sdebug
);
1805 if (session_id_prefix
) {
1806 if (strlen(session_id_prefix
) >= 32)
1808 "warning: id_prefix is too long, only one new session will be possible\n");
1809 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
1810 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1811 ERR_print_errors(bio_err
);
1814 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1816 SSL_CTX_set_quiet_shutdown(ctx2
, 1);
1818 ssl_ctx_set_excert(ctx2
, exc
);
1821 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
1824 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
1826 init_session_cache_ctx(ctx2
);
1828 SSL_CTX_sess_set_cache_size(ctx2
, 128);
1831 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
1833 if (!ctx_set_verify_locations(ctx2
, CAfile
, CApath
, noCAfile
,
1835 ERR_print_errors(bio_err
);
1838 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
1839 BIO_printf(bio_err
, "Error setting verify params\n");
1840 ERR_print_errors(bio_err
);
1844 ssl_ctx_add_crls(ctx2
, crls
, 0);
1845 if (!config_ctx(cctx
, ssl_args
, ctx2
))
1848 #ifndef OPENSSL_NO_NEXTPROTONEG
1849 if (next_proto
.data
)
1850 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
1854 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
1856 #ifndef OPENSSL_NO_DH
1861 dh
= load_dh_param(dhfile
);
1862 else if (s_cert_file
)
1863 dh
= load_dh_param(s_cert_file
);
1866 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1868 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
1870 (void)BIO_flush(bio_s_out
);
1873 SSL_CTX_set_dh_auto(ctx
, 1);
1874 else if (!SSL_CTX_set_tmp_dh(ctx
, dh
)) {
1875 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1876 ERR_print_errors(bio_err
);
1883 DH
*dh2
= load_dh_param(s_cert_file2
);
1885 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1886 (void)BIO_flush(bio_s_out
);
1893 SSL_CTX_set_dh_auto(ctx2
, 1);
1894 else if (!SSL_CTX_set_tmp_dh(ctx2
, dh
)) {
1895 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1896 ERR_print_errors(bio_err
);
1905 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
1908 if (s_serverinfo_file
!= NULL
1909 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
1910 ERR_print_errors(bio_err
);
1914 if (ctx2
&& !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
1917 if (s_dcert
!= NULL
) {
1918 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
1922 if (no_resume_ephemeral
) {
1923 SSL_CTX_set_not_resumable_session_callback(ctx
,
1924 not_resumable_sess_cb
);
1927 SSL_CTX_set_not_resumable_session_callback(ctx2
,
1928 not_resumable_sess_cb
);
1930 #ifndef OPENSSL_NO_PSK
1931 if (psk_key
!= NULL
) {
1933 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
1934 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
1937 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
1938 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
1939 ERR_print_errors(bio_err
);
1944 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
1945 if (!SSL_CTX_set_session_id_context(ctx
,
1946 (void *)&s_server_session_id_context
,
1947 sizeof s_server_session_id_context
)) {
1948 BIO_printf(bio_err
, "error setting session id context\n");
1949 ERR_print_errors(bio_err
);
1953 /* Set DTLS cookie generation and verification callbacks */
1954 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
1955 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
1958 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
1959 if (!SSL_CTX_set_session_id_context(ctx2
,
1960 (void *)&s_server_session_id_context
,
1961 sizeof s_server_session_id_context
)) {
1962 BIO_printf(bio_err
, "error setting session id context\n");
1963 ERR_print_errors(bio_err
);
1966 tlsextcbp
.biodebug
= bio_s_out
;
1967 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
1968 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
1969 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
1970 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
1973 #ifndef OPENSSL_NO_SRP
1974 if (srp_verifier_file
!= NULL
) {
1975 srp_callback_parm
.vb
= SRP_VBASE_new(srpuserseed
);
1976 srp_callback_parm
.user
= NULL
;
1977 srp_callback_parm
.login
= NULL
;
1979 SRP_VBASE_init(srp_callback_parm
.vb
,
1980 srp_verifier_file
)) != SRP_NO_ERROR
) {
1982 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
1983 srp_verifier_file
, ret
);
1986 SSL_CTX_set_verify(ctx
, SSL_VERIFY_NONE
, verify_callback
);
1987 SSL_CTX_set_srp_cb_arg(ctx
, &srp_callback_parm
);
1988 SSL_CTX_set_srp_username_callback(ctx
, ssl_srp_server_param_cb
);
1991 if (CAfile
!= NULL
) {
1992 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
1995 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
1997 #ifndef OPENSSL_NO_OCSP
1998 if (s_tlsextstatus
) {
1999 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
2000 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2002 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2003 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2007 if (set_keylog_file(ctx
, keylog_file
))
2010 if (max_early_data
>= 0)
2011 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2013 BIO_printf(bio_s_out
, "ACCEPT\n");
2014 (void)BIO_flush(bio_s_out
);
2016 server_cb
= rev_body
;
2018 server_cb
= www_body
;
2020 server_cb
= sv_body
;
2022 if (socket_family
== AF_UNIX
2023 && unlink_unix_path
)
2026 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
,
2027 server_cb
, context
, naccept
);
2028 print_stats(bio_s_out
, ctx
);
2032 set_keylog_file(NULL
, NULL
);
2034 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2036 EVP_PKEY_free(s_key
);
2037 EVP_PKEY_free(s_dkey
);
2038 sk_X509_pop_free(s_chain
, X509_free
);
2039 sk_X509_pop_free(s_dchain
, X509_free
);
2041 OPENSSL_free(dpass
);
2044 X509_VERIFY_PARAM_free(vpm
);
2046 OPENSSL_free(tlscstatp
.host
);
2047 OPENSSL_free(tlscstatp
.port
);
2048 OPENSSL_free(tlscstatp
.path
);
2051 EVP_PKEY_free(s_key2
);
2052 #ifndef OPENSSL_NO_NEXTPROTONEG
2053 OPENSSL_free(next_proto
.data
);
2055 OPENSSL_free(alpn_ctx
.data
);
2056 ssl_excert_free(exc
);
2057 sk_OPENSSL_STRING_free(ssl_args
);
2058 SSL_CONF_CTX_free(cctx
);
2059 release_engine(engine
);
2060 BIO_free(bio_s_out
);
2062 BIO_free(bio_s_msg
);
2064 #ifdef CHARSET_EBCDIC
2065 BIO_meth_free(methods_ebcdic
);
2070 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2072 BIO_printf(bio
, "%4ld items in the session cache\n",
2073 SSL_CTX_sess_number(ssl_ctx
));
2074 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2075 SSL_CTX_sess_connect(ssl_ctx
));
2076 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2077 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2078 BIO_printf(bio
, "%4ld client connects that finished\n",
2079 SSL_CTX_sess_connect_good(ssl_ctx
));
2080 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2081 SSL_CTX_sess_accept(ssl_ctx
));
2082 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2083 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2084 BIO_printf(bio
, "%4ld server accepts that finished\n",
2085 SSL_CTX_sess_accept_good(ssl_ctx
));
2086 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2087 BIO_printf(bio
, "%4ld session cache misses\n",
2088 SSL_CTX_sess_misses(ssl_ctx
));
2089 BIO_printf(bio
, "%4ld session cache timeouts\n",
2090 SSL_CTX_sess_timeouts(ssl_ctx
));
2091 BIO_printf(bio
, "%4ld callback cache hits\n",
2092 SSL_CTX_sess_cb_hits(ssl_ctx
));
2093 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2094 SSL_CTX_sess_cache_full(ssl_ctx
),
2095 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2098 static int sv_body(int s
, int stype
, unsigned char *context
)
2107 struct timeval timeout
;
2108 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2111 struct timeval
*timeoutp
;
2114 buf
= app_malloc(bufsize
, "server buffer");
2116 if (!BIO_socket_nbio(s
, 1))
2117 ERR_print_errors(bio_err
);
2119 BIO_printf(bio_err
, "Turned on non blocking io\n");
2125 if (s_tlsextdebug
) {
2126 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2127 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2131 && !SSL_set_session_id_context(con
,
2132 context
, strlen((char *)context
))) {
2133 BIO_printf(bio_err
, "Error setting session id context\n");
2138 if (!SSL_clear(con
)) {
2139 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2143 #ifndef OPENSSL_NO_DTLS
2144 if (stype
== SOCK_DGRAM
) {
2146 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2148 if (enable_timeouts
) {
2150 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2151 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2154 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2155 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2159 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2160 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2161 DTLS_get_link_min_mtu(con
));
2166 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2167 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2168 BIO_printf(bio_err
, "Failed to set MTU\n");
2174 /* want to do MTU discovery */
2175 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2177 /* turn on cookie exchange */
2178 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2181 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2186 test
= BIO_new(BIO_f_nbio_test());
2187 sbio
= BIO_push(test
, sbio
);
2190 SSL_set_bio(con
, sbio
, sbio
);
2191 SSL_set_accept_state(con
);
2192 /* SSL_set_fd(con,s); */
2195 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2196 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2199 #ifndef OPENSSL_NO_SSL_TRACE
2201 SSL_set_msg_callback(con
, SSL_trace
);
2204 SSL_set_msg_callback(con
, msg_cb
);
2205 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2208 if (s_tlsextdebug
) {
2209 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2210 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2214 int write_header
= 1, edret
= SSL_READ_EARLY_DATA_ERROR
;
2217 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
2219 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
2220 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
2223 switch (SSL_get_error(con
, 0)) {
2224 case SSL_ERROR_WANT_WRITE
:
2225 case SSL_ERROR_WANT_ASYNC
:
2226 case SSL_ERROR_WANT_READ
:
2227 /* Just keep trying - busy waiting */
2230 BIO_printf(bio_err
, "Error reading early data\n");
2231 ERR_print_errors(bio_err
);
2235 if (readbytes
> 0) {
2237 BIO_printf(bio_s_out
, "Early data received:\n");
2240 raw_write_stdout(buf
, (unsigned int)readbytes
);
2241 (void)BIO_flush(bio_s_out
);
2245 BIO_printf(bio_s_out
, "No early data received\n");
2247 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2248 if (SSL_is_init_finished(con
))
2249 print_connection_info(con
);
2252 if (fileno_stdin() > s
)
2253 width
= fileno_stdin() + 1;
2257 int read_from_terminal
;
2258 int read_from_sslcon
;
2260 read_from_terminal
= 0;
2261 read_from_sslcon
= SSL_has_pending(con
)
2262 || (async
&& SSL_waiting_for_async(con
));
2264 if (!read_from_sslcon
) {
2266 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2267 openssl_fdset(fileno_stdin(), &readfds
);
2269 openssl_fdset(s
, &readfds
);
2271 * Note: under VMS with SOCKETSHR the second parameter is
2272 * currently of type (int *) whereas under other systems it is
2273 * (void *) if you don't have a cast it will choke the compiler:
2274 * if you do have a cast then you can either go for (int *) or
2277 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2279 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2280 * only on sockets. As a workaround we timeout the select every
2281 * second and check for any keypress. In a proper Windows
2282 * application we wouldn't do this because it is inefficient.
2286 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &tv
);
2287 if (has_stdin_waiting())
2288 read_from_terminal
= 1;
2289 if ((i
< 0) || (!i
&& !read_from_terminal
))
2292 if ((SSL_version(con
) == DTLS1_VERSION
) &&
2293 DTLSv1_get_timeout(con
, &timeout
))
2294 timeoutp
= &timeout
;
2298 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2300 if ((SSL_version(con
) == DTLS1_VERSION
)
2301 && DTLSv1_handle_timeout(con
) > 0) {
2302 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2307 if (FD_ISSET(fileno_stdin(), &readfds
))
2308 read_from_terminal
= 1;
2310 if (FD_ISSET(s
, &readfds
))
2311 read_from_sslcon
= 1;
2313 if (read_from_terminal
) {
2317 i
= raw_read_stdin(buf
, bufsize
/ 2);
2319 /* both loops are skipped when i <= 0 */
2320 for (j
= 0; j
< i
; j
++)
2323 for (j
= i
- 1; j
>= 0; j
--) {
2324 buf
[j
+ lf_num
] = buf
[j
];
2325 if (buf
[j
] == '\n') {
2328 buf
[j
+ lf_num
] = '\r';
2331 assert(lf_num
== 0);
2333 i
= raw_read_stdin(buf
, bufsize
);
2335 if (!s_quiet
&& !s_brief
) {
2336 if ((i
<= 0) || (buf
[0] == 'Q')) {
2337 BIO_printf(bio_s_out
, "DONE\n");
2338 (void)BIO_flush(bio_s_out
);
2340 close_accept_socket();
2344 if ((i
<= 0) || (buf
[0] == 'q')) {
2345 BIO_printf(bio_s_out
, "DONE\n");
2346 (void)BIO_flush(bio_s_out
);
2347 if (SSL_version(con
) != DTLS1_VERSION
)
2350 * close_accept_socket(); ret= -11;
2354 #ifndef OPENSSL_NO_HEARTBEATS
2355 if ((buf
[0] == 'B') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2356 BIO_printf(bio_err
, "HEARTBEATING\n");
2362 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2363 SSL_renegotiate(con
);
2364 i
= SSL_do_handshake(con
);
2365 printf("SSL_do_handshake -> %d\n", i
);
2369 * strcpy(buf,"server side RE-NEGOTIATE\n");
2372 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2374 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2376 SSL_renegotiate(con
);
2377 i
= SSL_do_handshake(con
);
2378 printf("SSL_do_handshake -> %d\n", i
);
2382 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2386 if ((buf
[0] == 'K' || buf
[0] == 'k')
2387 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2388 SSL_key_update(con
, buf
[0] == 'K' ?
2389 SSL_KEY_UPDATE_REQUESTED
2390 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2391 i
= SSL_do_handshake(con
);
2392 printf("SSL_do_handshake -> %d\n", i
);
2396 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2400 if (buf
[0] == 'P') {
2401 static const char *str
= "Lets print some clear text\n";
2402 BIO_write(SSL_get_wbio(con
), str
, strlen(str
));
2404 if (buf
[0] == 'S') {
2405 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2408 #ifdef CHARSET_EBCDIC
2409 ebcdic2ascii(buf
, buf
, i
);
2413 /* should do a select for the write */
2416 if (++count
== 100) {
2418 SSL_renegotiate(con
);
2421 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2422 #ifndef OPENSSL_NO_SRP
2423 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2424 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2425 SRP_user_pwd_free(srp_callback_parm
.user
);
2426 srp_callback_parm
.user
=
2427 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2428 srp_callback_parm
.login
);
2429 if (srp_callback_parm
.user
)
2430 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2431 srp_callback_parm
.user
->info
);
2433 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2434 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2437 switch (SSL_get_error(con
, k
)) {
2438 case SSL_ERROR_NONE
:
2440 case SSL_ERROR_WANT_ASYNC
:
2441 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2442 (void)BIO_flush(bio_s_out
);
2443 wait_for_async(con
);
2445 case SSL_ERROR_WANT_WRITE
:
2446 case SSL_ERROR_WANT_READ
:
2447 case SSL_ERROR_WANT_X509_LOOKUP
:
2448 BIO_printf(bio_s_out
, "Write BLOCK\n");
2449 (void)BIO_flush(bio_s_out
);
2451 case SSL_ERROR_WANT_ASYNC_JOB
:
2453 * This shouldn't ever happen in s_server. Treat as an error
2455 case SSL_ERROR_SYSCALL
:
2457 BIO_printf(bio_s_out
, "ERROR\n");
2458 (void)BIO_flush(bio_s_out
);
2459 ERR_print_errors(bio_err
);
2463 case SSL_ERROR_ZERO_RETURN
:
2464 BIO_printf(bio_s_out
, "DONE\n");
2465 (void)BIO_flush(bio_s_out
);
2477 if (read_from_sslcon
) {
2479 * init_ssl_connection handles all async events itself so if we're
2480 * waiting for async then we shouldn't go back into
2481 * init_ssl_connection
2483 if ((!async
|| !SSL_waiting_for_async(con
))
2484 && !SSL_is_init_finished(con
)) {
2485 i
= init_ssl_connection(con
);
2490 } else if (i
== 0) {
2496 i
= SSL_read(con
, (char *)buf
, bufsize
);
2497 #ifndef OPENSSL_NO_SRP
2498 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2499 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2500 SRP_user_pwd_free(srp_callback_parm
.user
);
2501 srp_callback_parm
.user
=
2502 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2503 srp_callback_parm
.login
);
2504 if (srp_callback_parm
.user
)
2505 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2506 srp_callback_parm
.user
->info
);
2508 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2509 i
= SSL_read(con
, (char *)buf
, bufsize
);
2512 switch (SSL_get_error(con
, i
)) {
2513 case SSL_ERROR_NONE
:
2514 #ifdef CHARSET_EBCDIC
2515 ascii2ebcdic(buf
, buf
, i
);
2517 raw_write_stdout(buf
, (unsigned int)i
);
2518 (void)BIO_flush(bio_s_out
);
2519 if (SSL_has_pending(con
))
2522 case SSL_ERROR_WANT_ASYNC
:
2523 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2524 (void)BIO_flush(bio_s_out
);
2525 wait_for_async(con
);
2527 case SSL_ERROR_WANT_WRITE
:
2528 case SSL_ERROR_WANT_READ
:
2529 BIO_printf(bio_s_out
, "Read BLOCK\n");
2530 (void)BIO_flush(bio_s_out
);
2532 case SSL_ERROR_WANT_ASYNC_JOB
:
2534 * This shouldn't ever happen in s_server. Treat as an error
2536 case SSL_ERROR_SYSCALL
:
2538 BIO_printf(bio_s_out
, "ERROR\n");
2539 (void)BIO_flush(bio_s_out
);
2540 ERR_print_errors(bio_err
);
2543 case SSL_ERROR_ZERO_RETURN
:
2544 BIO_printf(bio_s_out
, "DONE\n");
2545 (void)BIO_flush(bio_s_out
);
2554 BIO_printf(bio_s_out
, "shutting down SSL\n");
2555 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
2558 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2559 OPENSSL_clear_free(buf
, bufsize
);
2561 BIO_printf(bio_s_out
, "ACCEPT\n");
2562 (void)BIO_flush(bio_s_out
);
2566 static void close_accept_socket(void)
2568 BIO_printf(bio_err
, "shutdown accept socket\n");
2569 if (accept_socket
>= 0) {
2570 BIO_closesocket(accept_socket
);
2574 static int init_ssl_connection(SSL
*con
)
2580 #ifndef OPENSSL_NO_DTLS
2582 BIO_ADDR
*client
= NULL
;
2584 if ((client
= BIO_ADDR_new()) == NULL
) {
2585 BIO_printf(bio_err
, "ERROR - memory\n");
2588 i
= DTLSv1_listen(con
, client
);
2593 wbio
= SSL_get_wbio(con
);
2595 BIO_get_fd(wbio
, &fd
);
2598 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2599 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2600 BIO_ADDR_free(client
);
2603 BIO_ADDR_free(client
);
2605 i
= SSL_accept(con
);
2607 BIO_ADDR_free(client
);
2613 i
= SSL_accept(con
);
2616 retry
= BIO_sock_should_retry(i
);
2617 #ifdef CERT_CB_TEST_RETRY
2620 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2621 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2623 "LOOKUP from certificate callback during accept\n");
2624 i
= SSL_accept(con
);
2626 retry
= BIO_sock_should_retry(i
);
2631 #ifndef OPENSSL_NO_SRP
2633 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2634 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
2635 srp_callback_parm
.login
);
2636 SRP_user_pwd_free(srp_callback_parm
.user
);
2637 srp_callback_parm
.user
=
2638 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2639 srp_callback_parm
.login
);
2640 if (srp_callback_parm
.user
)
2641 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2642 srp_callback_parm
.user
->info
);
2644 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2645 i
= SSL_accept(con
);
2647 retry
= BIO_sock_should_retry(i
);
2650 } while (i
< 0 && SSL_waiting_for_async(con
));
2653 if ((dtlslisten
&& i
== 0)
2654 || (!dtlslisten
&& retry
)) {
2655 BIO_printf(bio_s_out
, "DELAY\n");
2659 BIO_printf(bio_err
, "ERROR\n");
2661 verify_err
= SSL_get_verify_result(con
);
2662 if (verify_err
!= X509_V_OK
) {
2663 BIO_printf(bio_err
, "verify error:%s\n",
2664 X509_verify_cert_error_string(verify_err
));
2666 /* Always print any error messages */
2667 ERR_print_errors(bio_err
);
2671 print_connection_info(con
);
2675 static void print_connection_info(SSL
*con
)
2680 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2681 const unsigned char *next_proto_neg
;
2682 unsigned next_proto_neg_len
;
2684 unsigned char *exportedkeymat
;
2688 print_ssl_summary(con
);
2690 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
2692 peer
= SSL_get_peer_certificate(con
);
2694 BIO_printf(bio_s_out
, "Client certificate\n");
2695 PEM_write_bio_X509(bio_s_out
, peer
);
2696 X509_NAME_oneline(X509_get_subject_name(peer
), buf
, sizeof buf
);
2697 BIO_printf(bio_s_out
, "subject=%s\n", buf
);
2698 X509_NAME_oneline(X509_get_issuer_name(peer
), buf
, sizeof buf
);
2699 BIO_printf(bio_s_out
, "issuer=%s\n", buf
);
2704 if (SSL_get_shared_ciphers(con
, buf
, sizeof buf
) != NULL
)
2705 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
2706 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
2707 ssl_print_sigalgs(bio_s_out
, con
);
2708 #ifndef OPENSSL_NO_EC
2709 ssl_print_point_formats(bio_s_out
, con
);
2710 ssl_print_groups(bio_s_out
, con
, 0);
2712 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
2714 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2715 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
2716 if (next_proto_neg
) {
2717 BIO_printf(bio_s_out
, "NEXTPROTO is ");
2718 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
2719 BIO_printf(bio_s_out
, "\n");
2722 #ifndef OPENSSL_NO_SRTP
2724 SRTP_PROTECTION_PROFILE
*srtp_profile
2725 = SSL_get_selected_srtp_profile(con
);
2728 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
2729 srtp_profile
->name
);
2732 if (SSL_session_reused(con
))
2733 BIO_printf(bio_s_out
, "Reused session-id\n");
2734 BIO_printf(bio_s_out
, "Secure Renegotiation IS%s supported\n",
2735 SSL_get_secure_renegotiation_support(con
) ? "" : " NOT");
2736 if (keymatexportlabel
!= NULL
) {
2737 BIO_printf(bio_s_out
, "Keying material exporter:\n");
2738 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
2739 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
2740 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
2741 if (!SSL_export_keying_material(con
, exportedkeymat
,
2744 strlen(keymatexportlabel
),
2746 BIO_printf(bio_s_out
, " Error\n");
2748 BIO_printf(bio_s_out
, " Keying material: ");
2749 for (i
= 0; i
< keymatexportlen
; i
++)
2750 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
2751 BIO_printf(bio_s_out
, "\n");
2753 OPENSSL_free(exportedkeymat
);
2756 (void)BIO_flush(bio_s_out
);
2759 #ifndef OPENSSL_NO_DH
2760 static DH
*load_dh_param(const char *dhfile
)
2765 if ((bio
= BIO_new_file(dhfile
, "r")) == NULL
)
2767 ret
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2774 static int www_body(int s
, int stype
, unsigned char *context
)
2780 const SSL_CIPHER
*c
;
2781 BIO
*io
, *ssl_bio
, *sbio
;
2783 int total_bytes
= 0;
2788 /* Set width for a select call if needed */
2791 buf
= app_malloc(bufsize
, "server www buffer");
2792 io
= BIO_new(BIO_f_buffer());
2793 ssl_bio
= BIO_new(BIO_f_ssl());
2794 if ((io
== NULL
) || (ssl_bio
== NULL
))
2798 if (!BIO_socket_nbio(s
, 1))
2799 ERR_print_errors(bio_err
);
2801 BIO_printf(bio_err
, "Turned on non blocking io\n");
2804 /* lets make the output buffer a reasonable size */
2805 if (!BIO_set_write_buffer_size(io
, bufsize
))
2808 if ((con
= SSL_new(ctx
)) == NULL
)
2811 if (s_tlsextdebug
) {
2812 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2813 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2817 && !SSL_set_session_id_context(con
, context
,
2818 strlen((char *)context
)))
2821 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2825 test
= BIO_new(BIO_f_nbio_test());
2826 sbio
= BIO_push(test
, sbio
);
2828 SSL_set_bio(con
, sbio
, sbio
);
2829 SSL_set_accept_state(con
);
2831 /* SSL_set_fd(con,s); */
2832 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
2833 BIO_push(io
, ssl_bio
);
2834 #ifdef CHARSET_EBCDIC
2835 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
2839 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2840 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2843 #ifndef OPENSSL_NO_SSL_TRACE
2845 SSL_set_msg_callback(con
, SSL_trace
);
2848 SSL_set_msg_callback(con
, msg_cb
);
2849 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2853 i
= BIO_gets(io
, buf
, bufsize
- 1);
2854 if (i
< 0) { /* error */
2855 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
2857 ERR_print_errors(bio_err
);
2860 BIO_printf(bio_s_out
, "read R BLOCK\n");
2861 #ifndef OPENSSL_NO_SRP
2862 if (BIO_should_io_special(io
)
2863 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
2864 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2865 SRP_user_pwd_free(srp_callback_parm
.user
);
2866 srp_callback_parm
.user
=
2867 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2868 srp_callback_parm
.login
);
2869 if (srp_callback_parm
.user
)
2870 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2871 srp_callback_parm
.user
->info
);
2873 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2877 #if !defined(OPENSSL_SYS_MSDOS)
2882 } else if (i
== 0) { /* end of input */
2887 /* else we have data */
2888 if (((www
== 1) && (strncmp("GET ", buf
, 4) == 0)) ||
2889 ((www
== 2) && (strncmp("GET /stats ", buf
, 11) == 0))) {
2892 STACK_OF(SSL_CIPHER
) *sk
;
2893 static const char *space
= " ";
2895 if (www
== 1 && strncmp("GET /reneg", buf
, 10) == 0) {
2896 if (strncmp("GET /renegcert", buf
, 14) == 0)
2898 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2900 i
= SSL_renegotiate(con
);
2901 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
2902 /* Send the HelloRequest */
2903 i
= SSL_do_handshake(con
);
2905 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
2906 SSL_get_error(con
, i
));
2907 ERR_print_errors(bio_err
);
2910 /* Wait for a ClientHello to come back */
2912 openssl_fdset(s
, &readfds
);
2913 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
2914 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
2915 BIO_printf(bio_s_out
,
2916 "Error waiting for client response\n");
2917 ERR_print_errors(bio_err
);
2921 * We're not actually expecting any data here and we ignore
2922 * any that is sent. This is just to force the handshake that
2923 * we're expecting to come from the client. If they haven't
2924 * sent one there's not much we can do.
2926 BIO_gets(io
, buf
, bufsize
- 1);
2930 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2931 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2932 BIO_puts(io
, "<pre>\n");
2933 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
2935 for (i
= 0; i
< local_argc
; i
++) {
2937 for (myp
= local_argv
[i
]; *myp
; myp
++)
2940 BIO_puts(io
, "<");
2943 BIO_puts(io
, ">");
2946 BIO_puts(io
, "&");
2949 BIO_write(io
, myp
, 1);
2952 BIO_write(io
, " ", 1);
2957 "Secure Renegotiation IS%s supported\n",
2958 SSL_get_secure_renegotiation_support(con
) ?
2962 * The following is evil and should not really be done
2964 BIO_printf(io
, "Ciphers supported in s_server binary\n");
2965 sk
= SSL_get_ciphers(con
);
2966 j
= sk_SSL_CIPHER_num(sk
);
2967 for (i
= 0; i
< j
; i
++) {
2968 c
= sk_SSL_CIPHER_value(sk
, i
);
2969 BIO_printf(io
, "%-11s:%-25s ",
2970 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
2971 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
2975 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
2978 "---\nCiphers common between both SSL end points:\n");
2982 BIO_write(io
, space
, 26 - j
);
2985 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
2987 BIO_write(io
, p
, 1);
2994 ssl_print_sigalgs(io
, con
);
2995 #ifndef OPENSSL_NO_EC
2996 ssl_print_groups(io
, con
, 0);
2998 BIO_printf(io
, (SSL_session_reused(con
)
2999 ? "---\nReused, " : "---\nNew, "));
3000 c
= SSL_get_current_cipher(con
);
3001 BIO_printf(io
, "%s, Cipher is %s\n",
3002 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3003 SSL_SESSION_print(io
, SSL_get_session(con
));
3004 BIO_printf(io
, "---\n");
3005 print_stats(io
, SSL_get_SSL_CTX(con
));
3006 BIO_printf(io
, "---\n");
3007 peer
= SSL_get_peer_certificate(con
);
3009 BIO_printf(io
, "Client certificate\n");
3010 X509_print(io
, peer
);
3011 PEM_write_bio_X509(io
, peer
);
3015 BIO_puts(io
, "no client certificate available\n");
3016 BIO_puts(io
, "</BODY></HTML>\r\n\r\n");
3018 } else if ((www
== 2 || www
== 3)
3019 && (strncmp("GET /", buf
, 5) == 0)) {
3022 static const char *text
=
3023 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3029 for (e
= p
; *e
!= '\0'; e
++) {
3035 dot
= (e
[0] == '.') ? 2 : 0;
3038 dot
= (e
[0] == '.') ? 3 : 0;
3041 dot
= (e
[0] == '/') ? -1 : 0;
3045 dot
= (e
[0] == '/') ? 1 : 0;
3047 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3052 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3059 BIO_printf(io
, "'%s' contains '..' reference\r\n", p
);
3065 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3069 /* if a directory, do the index thang */
3070 if (app_isdir(p
) > 0) {
3072 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3076 if ((file
= BIO_new_file(p
, "r")) == NULL
) {
3078 BIO_printf(io
, "Error opening '%s'\r\n", p
);
3079 ERR_print_errors(io
);
3084 BIO_printf(bio_err
, "FILE:%s\n", p
);
3088 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3089 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3090 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3092 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3095 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3099 i
= BIO_read(file
, buf
, bufsize
);
3105 BIO_printf(bio_err
, "%d\n", i
);
3106 if (total_bytes
> 3 * 1024) {
3108 BIO_printf(bio_err
, "RENEGOTIATE\n");
3109 SSL_renegotiate(con
);
3113 for (j
= 0; j
< i
;) {
3116 if (++count
== 13) {
3117 SSL_renegotiate(con
);
3120 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3122 if (!BIO_should_retry(io
)
3123 && !SSL_waiting_for_async(con
))
3126 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3140 i
= (int)BIO_flush(io
);
3142 if (!BIO_should_retry(io
))
3148 /* make sure we re-use sessions */
3149 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3153 BIO_printf(bio_s_out
, "ACCEPT\n");
3159 static int rev_body(int s
, int stype
, unsigned char *context
)
3165 BIO
*io
, *ssl_bio
, *sbio
;
3167 buf
= app_malloc(bufsize
, "server rev buffer");
3168 io
= BIO_new(BIO_f_buffer());
3169 ssl_bio
= BIO_new(BIO_f_ssl());
3170 if ((io
== NULL
) || (ssl_bio
== NULL
))
3173 /* lets make the output buffer a reasonable size */
3174 if (!BIO_set_write_buffer_size(io
, bufsize
))
3177 if ((con
= SSL_new(ctx
)) == NULL
)
3180 if (s_tlsextdebug
) {
3181 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3182 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3185 && !SSL_set_session_id_context(con
, context
,
3186 strlen((char *)context
))) {
3187 ERR_print_errors(bio_err
);
3191 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3192 SSL_set_bio(con
, sbio
, sbio
);
3193 SSL_set_accept_state(con
);
3195 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3196 BIO_push(io
, ssl_bio
);
3197 #ifdef CHARSET_EBCDIC
3198 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
3202 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
3203 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3206 #ifndef OPENSSL_NO_SSL_TRACE
3208 SSL_set_msg_callback(con
, SSL_trace
);
3211 SSL_set_msg_callback(con
, msg_cb
);
3212 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3216 i
= BIO_do_handshake(io
);
3219 if (!BIO_should_retry(io
)) {
3220 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3221 ERR_print_errors(bio_err
);
3224 #ifndef OPENSSL_NO_SRP
3225 if (BIO_should_io_special(io
)
3226 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3227 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3228 SRP_user_pwd_free(srp_callback_parm
.user
);
3229 srp_callback_parm
.user
=
3230 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3231 srp_callback_parm
.login
);
3232 if (srp_callback_parm
.user
)
3233 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3234 srp_callback_parm
.user
->info
);
3236 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3241 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3242 print_ssl_summary(con
);
3245 i
= BIO_gets(io
, buf
, bufsize
- 1);
3246 if (i
< 0) { /* error */
3247 if (!BIO_should_retry(io
)) {
3249 ERR_print_errors(bio_err
);
3252 BIO_printf(bio_s_out
, "read R BLOCK\n");
3253 #ifndef OPENSSL_NO_SRP
3254 if (BIO_should_io_special(io
)
3255 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3256 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3257 SRP_user_pwd_free(srp_callback_parm
.user
);
3258 srp_callback_parm
.user
=
3259 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3260 srp_callback_parm
.login
);
3261 if (srp_callback_parm
.user
)
3262 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3263 srp_callback_parm
.user
->info
);
3265 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3269 #if !defined(OPENSSL_SYS_MSDOS)
3274 } else if (i
== 0) { /* end of input */
3276 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3279 char *p
= buf
+ i
- 1;
3280 while (i
&& (*p
== '\n' || *p
== '\r')) {
3284 if (!s_ign_eof
&& (i
== 5) && (strncmp(buf
, "CLOSE", 5) == 0)) {
3286 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3289 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3291 BIO_write(io
, buf
, i
+ 1);
3296 if (!BIO_should_retry(io
))
3302 /* make sure we re-use sessions */
3303 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3312 #define MAX_SESSION_ID_ATTEMPTS 10
3313 static int generate_session_id(const SSL
*ssl
, unsigned char *id
,
3314 unsigned int *id_len
)
3316 unsigned int count
= 0;
3318 if (RAND_bytes(id
, *id_len
) <= 0)
3321 * Prefix the session_id with the required prefix. NB: If our prefix
3322 * is too long, clip it - but there will be worse effects anyway, eg.
3323 * the server could only possibly create 1 session ID (ie. the
3324 * prefix!) so all future session negotiations will fail due to
3327 memcpy(id
, session_id_prefix
,
3328 (strlen(session_id_prefix
) < *id_len
) ?
3329 strlen(session_id_prefix
) : *id_len
);
3331 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3332 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3333 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3339 * By default s_server uses an in-memory cache which caches SSL_SESSION
3340 * structures without any serialisation. This hides some bugs which only
3341 * become apparent in deployed servers. By implementing a basic external
3342 * session cache some issues can be debugged using s_server.
3345 typedef struct simple_ssl_session_st
{
3350 struct simple_ssl_session_st
*next
;
3351 } simple_ssl_session
;
3353 static simple_ssl_session
*first
= NULL
;
3355 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3357 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3360 SSL_SESSION_get_id(session
, &sess
->idlen
);
3361 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3362 if (sess
->derlen
< 0) {
3363 BIO_printf(bio_err
, "Error encoding session\n");
3368 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3369 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3371 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3372 OPENSSL_free(sess
->id
);
3373 OPENSSL_free(sess
->der
);
3379 /* Assume it still works. */
3380 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3381 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3382 OPENSSL_free(sess
->id
);
3383 OPENSSL_free(sess
->der
);
3390 BIO_printf(bio_err
, "New session added to external cache\n");
3394 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3397 simple_ssl_session
*sess
;
3399 for (sess
= first
; sess
; sess
= sess
->next
) {
3400 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3401 const unsigned char *p
= sess
->der
;
3402 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3403 return d2i_SSL_SESSION(NULL
, &p
, sess
->derlen
);
3406 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3410 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3412 simple_ssl_session
*sess
, *prev
= NULL
;
3413 const unsigned char *id
;
3415 id
= SSL_SESSION_get_id(session
, &idlen
);
3416 for (sess
= first
; sess
; sess
= sess
->next
) {
3417 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3419 prev
->next
= sess
->next
;
3422 OPENSSL_free(sess
->id
);
3423 OPENSSL_free(sess
->der
);
3431 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3433 SSL_CTX_set_session_cache_mode(sctx
,
3434 SSL_SESS_CACHE_NO_INTERNAL
|
3435 SSL_SESS_CACHE_SERVER
);
3436 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3437 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3438 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3441 static void free_sessions(void)
3443 simple_ssl_session
*sess
, *tsess
;
3444 for (sess
= first
; sess
;) {
3445 OPENSSL_free(sess
->id
);
3446 OPENSSL_free(sess
->der
);
3449 OPENSSL_free(tsess
);
3454 #endif /* OPENSSL_NO_SOCK */