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
);
108 /* static int load_CA(SSL_CTX *ctx, char *file);*/
110 static const int bufsize
= 16 * 1024;
111 static int accept_socket
= -1;
113 #define TEST_CERT "server.pem"
114 #define TEST_CERT2 "server2.pem"
116 static int s_nbio
= 0;
117 static int s_nbio_test
= 0;
118 static int s_crlf
= 0;
119 static SSL_CTX
*ctx
= NULL
;
120 static SSL_CTX
*ctx2
= NULL
;
123 static BIO
*bio_s_out
= NULL
;
124 static BIO
*bio_s_msg
= NULL
;
125 static int s_debug
= 0;
126 static int s_tlsextdebug
= 0;
127 static int s_msg
= 0;
128 static int s_quiet
= 0;
129 static int s_ign_eof
= 0;
130 static int s_brief
= 0;
132 static char *keymatexportlabel
= NULL
;
133 static int keymatexportlen
= 20;
135 static int async
= 0;
137 static const char *session_id_prefix
= NULL
;
139 #ifndef OPENSSL_NO_DTLS
140 static int enable_timeouts
= 0;
141 static long socket_mtu
;
144 static int dtlslisten
= 0;
146 #ifndef OPENSSL_NO_PSK
147 static char *psk_identity
= "Client_identity";
148 char *psk_key
= NULL
; /* by default PSK is not used */
152 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
154 unsigned int max_psk_len
)
160 BIO_printf(bio_s_out
, "psk_server_cb\n");
162 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
166 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
167 (int)strlen(identity
), identity
);
169 /* here we could lookup the given identity e.g. from a database */
170 if (strcmp(identity
, psk_identity
) != 0) {
171 BIO_printf(bio_s_out
, "PSK error: client identity not found"
172 " (got '%s' expected '%s')\n", identity
, psk_identity
);
176 BIO_printf(bio_s_out
, "PSK client identity found\n");
178 /* convert the PSK key to binary */
179 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
181 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
185 if (key_len
> (int)max_psk_len
) {
187 "psk buffer of callback is too small (%d) for key (%ld)\n",
188 max_psk_len
, key_len
);
193 memcpy(psk
, key
, key_len
);
197 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
201 BIO_printf(bio_err
, "Error in PSK server callback\n");
202 (void)BIO_flush(bio_err
);
203 (void)BIO_flush(bio_s_out
);
208 #ifndef OPENSSL_NO_SRP
209 /* This is a context that we pass to callbacks */
210 typedef struct srpsrvparm_st
{
217 * This callback pretends to require some asynchronous logic in order to
218 * obtain a verifier. When the callback is called for a new connection we
219 * return with a negative value. This will provoke the accept etc to return
220 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
221 * (which would normally occur after a worker has finished) and we set the
224 static int ssl_srp_server_param_cb(SSL
*s
, int *ad
, void *arg
)
226 srpsrvparm
*p
= (srpsrvparm
*) arg
;
227 int ret
= SSL3_AL_FATAL
;
229 if (p
->login
== NULL
&& p
->user
== NULL
) {
230 p
->login
= SSL_get_srp_username(s
);
231 BIO_printf(bio_err
, "SRP username = \"%s\"\n", p
->login
);
235 if (p
->user
== NULL
) {
236 BIO_printf(bio_err
, "User %s doesn't exist\n", p
->login
);
240 if (SSL_set_srp_server_param
241 (s
, p
->user
->N
, p
->user
->g
, p
->user
->s
, p
->user
->v
,
242 p
->user
->info
) < 0) {
243 *ad
= SSL_AD_INTERNAL_ERROR
;
247 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
248 p
->login
, p
->user
->info
);
249 ret
= SSL_ERROR_NONE
;
252 SRP_user_pwd_free(p
->user
);
260 static int local_argc
= 0;
261 static char **local_argv
;
263 #ifdef CHARSET_EBCDIC
264 static int ebcdic_new(BIO
*bi
);
265 static int ebcdic_free(BIO
*a
);
266 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
267 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
268 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
269 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
270 static int ebcdic_puts(BIO
*bp
, const char *str
);
272 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
273 static BIO_METHOD
*methods_ebcdic
= NULL
;
275 /* This struct is "unwarranted chumminess with the compiler." */
281 static const BIO_METHOD
*BIO_f_ebcdic_filter()
283 if (methods_ebcdic
== NULL
) {
284 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
285 "EBCDIC/ASCII filter");
286 if (methods_ebcdic
== NULL
287 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
288 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
289 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
290 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
291 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
292 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
293 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
296 return methods_ebcdic
;
299 static int ebcdic_new(BIO
*bi
)
301 EBCDIC_OUTBUFF
*wbuf
;
303 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
304 wbuf
->alloced
= 1024;
305 wbuf
->buff
[0] = '\0';
307 BIO_set_data(bi
, wbuf
);
312 static int ebcdic_free(BIO
*a
)
314 EBCDIC_OUTBUFF
*wbuf
;
318 wbuf
= BIO_get_data(a
);
320 BIO_set_data(a
, NULL
);
326 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
329 BIO
*next
= BIO_next(b
);
331 if (out
== NULL
|| outl
== 0)
336 ret
= BIO_read(next
, out
, outl
);
338 ascii2ebcdic(out
, out
, ret
);
342 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
344 EBCDIC_OUTBUFF
*wbuf
;
345 BIO
*next
= BIO_next(b
);
349 if ((in
== NULL
) || (inl
<= 0))
354 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
356 if (inl
> (num
= wbuf
->alloced
)) {
357 num
= num
+ num
; /* double the size */
361 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
364 wbuf
->buff
[0] = '\0';
366 BIO_set_data(b
, wbuf
);
369 ebcdic2ascii(wbuf
->buff
, in
, inl
);
371 ret
= BIO_write(next
, wbuf
->buff
, inl
);
376 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
379 BIO
*next
= BIO_next(b
);
388 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
394 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
397 BIO
*next
= BIO_next(bp
);
401 /* return(BIO_gets(bp->next_bio,buf,size));*/
402 for (i
= 0; i
< size
- 1; ++i
) {
403 ret
= ebcdic_read(bp
, &buf
[i
], 1);
406 else if (buf
[i
] == '\n') {
413 return (ret
< 0 && i
== 0) ? ret
: i
;
416 static int ebcdic_puts(BIO
*bp
, const char *str
)
418 if (BIO_next(bp
) == NULL
)
420 return ebcdic_write(bp
, str
, strlen(str
));
424 /* This is a context that we pass to callbacks */
425 typedef struct tlsextctx_st
{
431 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
433 tlsextctx
*p
= (tlsextctx
*) arg
;
434 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
435 if (servername
&& p
->biodebug
)
436 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"%s\"\n",
440 return SSL_TLSEXT_ERR_NOACK
;
443 if (strcasecmp(servername
, p
->servername
))
444 return p
->extension_error
;
446 BIO_printf(p
->biodebug
, "Switching server context.\n");
447 SSL_set_SSL_CTX(s
, ctx2
);
450 return SSL_TLSEXT_ERR_OK
;
453 /* Structure passed to cert status callback */
455 typedef struct tlsextstatusctx_st
{
457 /* File to load OCSP Response from (or NULL if no file) */
459 /* Default responder to use */
460 char *host
, *path
, *port
;
465 static tlsextstatusctx tlscstatp
= { -1 };
467 #ifndef OPENSSL_NO_OCSP
470 * Helper function to get an OCSP_RESPONSE from a responder. This is a
471 * simplified version. It examines certificates each time and makes one OCSP
472 * responder query for each request. A full version would store details such as
473 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
474 * them until they were considered "expired".
476 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
477 OCSP_RESPONSE
**resp
)
479 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
481 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
483 X509_STORE_CTX
*inctx
= NULL
;
485 OCSP_REQUEST
*req
= NULL
;
486 OCSP_CERTID
*id
= NULL
;
487 STACK_OF(X509_EXTENSION
) *exts
;
488 int ret
= SSL_TLSEXT_ERR_NOACK
;
491 /* Build up OCSP query from server certificate */
492 x
= SSL_get_certificate(s
);
493 aia
= X509_get1_ocsp(x
);
495 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia
, 0),
496 &host
, &port
, &path
, &use_ssl
)) {
497 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
501 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
502 sk_OPENSSL_STRING_value(aia
, 0));
506 "cert_status: no AIA and no default responder URL\n");
512 use_ssl
= srctx
->use_ssl
;
515 inctx
= X509_STORE_CTX_new();
518 if (!X509_STORE_CTX_init(inctx
,
519 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s
)),
522 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
,
523 X509_get_issuer_name(x
));
525 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
528 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
529 X509_OBJECT_free(obj
);
532 req
= OCSP_REQUEST_new();
535 if (!OCSP_request_add0_id(req
, id
))
538 /* Add any extensions to the request */
539 SSL_get_tlsext_status_exts(s
, &exts
);
540 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
541 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
542 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
545 *resp
= process_responder(req
, host
, path
, port
, use_ssl
, NULL
,
548 BIO_puts(bio_err
, "cert_status: error querying responder\n");
552 ret
= SSL_TLSEXT_ERR_OK
;
556 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
559 * If we parsed aia we need to free; otherwise they were copied and we
566 X509_email_free(aia
);
568 OCSP_CERTID_free(id
);
569 OCSP_REQUEST_free(req
);
570 X509_STORE_CTX_free(inctx
);
575 * Certificate Status callback. This is called when a client includes a
576 * certificate status request extension. The response is either obtained from a
577 * file, or from an OCSP responder.
579 static int cert_status_cb(SSL
*s
, void *arg
)
581 tlsextstatusctx
*srctx
= arg
;
582 OCSP_RESPONSE
*resp
= NULL
;
583 unsigned char *rspder
= NULL
;
585 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
588 BIO_puts(bio_err
, "cert_status: callback called\n");
590 if (srctx
->respin
!= NULL
) {
591 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
592 if (derbio
== NULL
) {
593 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
596 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
599 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
603 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
604 if (ret
!= SSL_TLSEXT_ERR_OK
)
608 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
612 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
613 if (srctx
->verbose
) {
614 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
615 OCSP_RESPONSE_print(bio_err
, resp
, 2);
618 ret
= SSL_TLSEXT_ERR_OK
;
621 if (ret
!= SSL_TLSEXT_ERR_OK
)
622 ERR_print_errors(bio_err
);
624 OCSP_RESPONSE_free(resp
);
630 #ifndef OPENSSL_NO_NEXTPROTONEG
631 /* This is the context that we pass to next_proto_cb */
632 typedef struct tlsextnextprotoctx_st
{
635 } tlsextnextprotoctx
;
637 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
638 unsigned int *len
, void *arg
)
640 tlsextnextprotoctx
*next_proto
= arg
;
642 *data
= next_proto
->data
;
643 *len
= next_proto
->len
;
645 return SSL_TLSEXT_ERR_OK
;
647 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
649 /* This the context that we pass to alpn_cb */
650 typedef struct tlsextalpnctx_st
{
655 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
656 const unsigned char *in
, unsigned int inlen
, void *arg
)
658 tlsextalpnctx
*alpn_ctx
= arg
;
661 /* We can assume that |in| is syntactically valid. */
663 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
664 for (i
= 0; i
< inlen
;) {
666 BIO_write(bio_s_out
, ", ", 2);
667 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
670 BIO_write(bio_s_out
, "\n", 1);
673 if (SSL_select_next_proto
674 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
675 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
676 return SSL_TLSEXT_ERR_NOACK
;
680 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
681 BIO_write(bio_s_out
, *out
, *outlen
);
682 BIO_write(bio_s_out
, "\n", 1);
685 return SSL_TLSEXT_ERR_OK
;
688 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
690 /* disable resumption for sessions with forward secure ciphers */
691 return is_forward_secure
;
694 #ifndef OPENSSL_NO_SRP
695 static srpsrvparm srp_callback_parm
;
697 #ifndef OPENSSL_NO_SRTP
698 static char *srtp_profiles
= NULL
;
701 typedef enum OPTION_choice
{
702 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ENGINE
,
703 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
704 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
705 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
706 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
707 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
708 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
709 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
710 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
711 OPT_VERIFYCAFILE
, OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
712 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
713 OPT_STATUS_TIMEOUT
, OPT_STATUS_URL
, OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
714 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
715 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
716 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_HINT
, OPT_PSK
, OPT_SRPVFILE
,
717 OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
, OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
,
718 OPT_SSL_CONFIG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
719 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
720 OPT_DTLS1_2
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
,
721 OPT_ID_PREFIX
, OPT_RAND
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
722 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
,
723 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
724 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_EARLY_DATA
,
730 const OPTIONS s_server_options
[] = {
731 {"help", OPT_HELP
, '-', "Display this summary"},
732 {"port", OPT_PORT
, 'p',
733 "TCP/IP port to listen on for connections (default is " PORT
")"},
734 {"accept", OPT_ACCEPT
, 's',
735 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
737 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
739 {"4", OPT_4
, '-', "Use IPv4 only"},
740 {"6", OPT_6
, '-', "Use IPv6 only"},
742 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
744 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
745 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
746 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
747 "Turn on peer certificate verification, must have a cert"},
748 {"cert", OPT_CERT
, '<', "Certificate file to use; default is " TEST_CERT
},
749 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
750 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
751 {"serverinfo", OPT_SERVERINFO
, 's',
752 "PEM serverinfo file for certificate"},
753 {"certform", OPT_CERTFORM
, 'F',
754 "Certificate format (PEM or DER) PEM default"},
755 {"key", OPT_KEY
, '<',
756 "Private Key if not in -cert; default is " TEST_CERT
},
757 {"keyform", OPT_KEYFORM
, 'f',
758 "Key format (PEM, DER or ENGINE) PEM default"},
759 {"pass", OPT_PASS
, 's', "Private key file pass phrase source"},
760 {"dcert", OPT_DCERT
, '<',
761 "Second certificate file to use (usually for DSA)"},
762 {"dcertform", OPT_DCERTFORM
, 'F',
763 "Second certificate format (PEM or DER) PEM default"},
764 {"dkey", OPT_DKEY
, '<',
765 "Second private key file to use (usually for DSA)"},
766 {"dkeyform", OPT_DKEYFORM
, 'F',
767 "Second key format (PEM, DER or ENGINE) PEM default"},
768 {"dpass", OPT_DPASS
, 's', "Second private key file pass phrase source"},
769 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
770 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
771 {"debug", OPT_DEBUG
, '-', "Print more output"},
772 {"msg", OPT_MSG
, '-', "Show protocol messages"},
773 {"msgfile", OPT_MSGFILE
, '>',
774 "File to send output of -msg or -trace, instead of stdout"},
775 {"state", OPT_STATE
, '-', "Print the SSL states"},
776 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
777 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
778 {"no-CAfile", OPT_NOCAFILE
, '-',
779 "Do not load the default certificates file"},
780 {"no-CApath", OPT_NOCAPATH
, '-',
781 "Do not load certificates from the default certificates directory"},
782 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
783 {"quiet", OPT_QUIET
, '-', "No server output"},
784 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
785 "Disable caching and tickets if ephemeral (EC)DH is used"},
786 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
787 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
788 {"servername", OPT_SERVERNAME
, 's',
789 "Servername for HostName TLS extension"},
790 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
791 "mismatch send fatal alert (default warning alert)"},
792 {"cert2", OPT_CERT2
, '<',
793 "Certificate file to use for servername; default is" TEST_CERT2
},
794 {"key2", OPT_KEY2
, '<',
795 "-Private Key file to use for servername if not in -cert2"},
796 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
797 "Hex dump of all TLS extensions received"},
798 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
799 {"id_prefix", OPT_ID_PREFIX
, 's',
800 "Generate SSL/TLS session IDs prefixed by arg"},
801 {"rand", OPT_RAND
, 's',
802 "Load the file(s) into the random number generator"},
803 {"keymatexport", OPT_KEYMATEXPORT
, 's',
804 "Export keying material using label"},
805 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
806 "Export len bytes of keying material (default 20)"},
807 {"CRL", OPT_CRL
, '<', "CRL file to use"},
808 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
809 "Download CRL from distribution points"},
810 {"cert_chain", OPT_CERT_CHAIN
, '<',
811 "certificate chain file in PEM format"},
812 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
813 "second certificate chain file in PEM format"},
814 {"chainCApath", OPT_CHAINCAPATH
, '/',
815 "use dir as certificate store path to build CA certificate chain"},
816 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
817 "use dir as certificate store path to verify CA certificate"},
818 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
819 {"ext_cache", OPT_EXT_CACHE
, '-',
820 "Disable internal cache, setup and use external cache"},
821 {"CRLform", OPT_CRLFORM
, 'F', "CRL format (PEM or DER) PEM is default"},
822 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
823 "Close connection on verification error"},
824 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
825 "No verify output except verify errors"},
826 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build certificate chain"},
827 {"chainCAfile", OPT_CHAINCAFILE
, '<',
828 "CA file for certificate chain (PEM format)"},
829 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
830 "CA file for certificate verification (PEM format)"},
831 {"ign_eof", OPT_IGN_EOF
, '-', "ignore input eof (default when -quiet)"},
832 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input eof"},
833 #ifndef OPENSSL_NO_OCSP
834 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
835 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
836 "Print more output in certificate status callback"},
837 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
838 "Status request responder timeout"},
839 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
840 {"status_file", OPT_STATUS_FILE
, '<',
841 "File containing DER encoded OCSP Response"},
843 #ifndef OPENSSL_NO_SSL_TRACE
844 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
846 {"security_debug", OPT_SECURITY_DEBUG
, '-',
847 "Print output from SSL/TLS security framework"},
848 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
849 "Print more output from SSL/TLS security framework"},
850 {"brief", OPT_BRIEF
, '-',
851 "Restrict output to brief summary of connection parameters"},
852 {"rev", OPT_REV
, '-',
853 "act as a simple test server which just sends back with the received text reversed"},
854 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
855 {"ssl_config", OPT_SSL_CONFIG
, 's',
856 "Configure SSL_CTX using the configuration 'val'"},
857 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'n',
858 "Size used to split data for encrypt pipelines"},
859 {"max_pipelines", OPT_MAX_PIPELINES
, 'n',
860 "Maximum number of encrypt/decrypt pipelines to be used"},
861 {"read_buf", OPT_READ_BUF
, 'n',
862 "Default read buffer size to be used for connections"},
866 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
867 #ifndef OPENSSL_NO_PSK
868 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
869 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
871 #ifndef OPENSSL_NO_SRP
872 {"srpvfile", OPT_SRPVFILE
, '<', "The verifier file for SRP"},
873 {"srpuserseed", OPT_SRPUSERSEED
, 's',
874 "A seed string for a default user salt"},
876 #ifndef OPENSSL_NO_SSL3
877 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
879 #ifndef OPENSSL_NO_TLS1
880 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
882 #ifndef OPENSSL_NO_TLS1_1
883 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
885 #ifndef OPENSSL_NO_TLS1_2
886 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
888 #ifndef OPENSSL_NO_TLS1_3
889 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
891 #ifndef OPENSSL_NO_DTLS
892 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
893 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
894 {"mtu", OPT_MTU
, 'p', "Set link layer MTU"},
895 {"listen", OPT_LISTEN
, '-',
896 "Listen for a DTLS ClientHello with a cookie and then connect"},
898 #ifndef OPENSSL_NO_DTLS1
899 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
901 #ifndef OPENSSL_NO_DTLS1_2
902 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
904 #ifndef OPENSSL_NO_DH
905 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
907 #ifndef OPENSSL_NO_NEXTPROTONEG
908 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
909 "Set the advertised protocols for the NPN extension (comma-separated list)"},
911 #ifndef OPENSSL_NO_SRTP
912 {"use_srtp", OPT_SRTP_PROFILES
, 's',
913 "Offer SRTP key management with a colon-separated profile list"},
915 {"alpn", OPT_ALPN
, 's',
916 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
917 #ifndef OPENSSL_NO_ENGINE
918 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
920 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
921 {"max_early_data", OPT_MAX_EARLY
, 'n',
922 "The maximum number of bytes of early data"},
923 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
924 {NULL
, OPT_EOF
, 0, NULL
}
927 #define IS_PROT_FLAG(o) \
928 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
929 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
931 int s_server_main(int argc
, char *argv
[])
933 ENGINE
*engine
= NULL
;
934 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
935 SSL_CONF_CTX
*cctx
= NULL
;
936 const SSL_METHOD
*meth
= TLS_server_method();
937 SSL_EXCERT
*exc
= NULL
;
938 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
939 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
940 STACK_OF(X509_CRL
) *crls
= NULL
;
941 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
942 X509_VERIFY_PARAM
*vpm
= NULL
;
943 const char *CApath
= NULL
, *CAfile
= NULL
, *chCApath
= NULL
, *chCAfile
= NULL
;
944 char *dpassarg
= NULL
, *dpass
= NULL
, *inrand
= NULL
;
945 char *passarg
= NULL
, *pass
= NULL
, *vfyCApath
= NULL
, *vfyCAfile
= NULL
;
946 char *crl_file
= NULL
, *prog
;
948 int unlink_unix_path
= 0;
950 do_server_cb server_cb
;
951 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
952 #ifndef OPENSSL_NO_DH
956 int nocert
= 0, ret
= 1;
957 int noCApath
= 0, noCAfile
= 0;
958 int s_cert_format
= FORMAT_PEM
, s_key_format
= FORMAT_PEM
;
959 int s_dcert_format
= FORMAT_PEM
, s_dkey_format
= FORMAT_PEM
;
960 int rev
= 0, naccept
= -1, sdebug
= 0;
961 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
;
962 int state
= 0, crl_format
= FORMAT_PEM
, crl_download
= 0;
964 char *port
= BUF_strdup(PORT
);
965 unsigned char *context
= NULL
;
967 EVP_PKEY
*s_key2
= NULL
;
968 X509
*s_cert2
= NULL
;
969 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
970 const char *ssl_config
= NULL
;
971 int read_buf_len
= 0;
972 #ifndef OPENSSL_NO_NEXTPROTONEG
973 const char *next_proto_neg_in
= NULL
;
974 tlsextnextprotoctx next_proto
= { NULL
, 0 };
976 const char *alpn_in
= NULL
;
977 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
978 #ifndef OPENSSL_NO_PSK
979 /* by default do not send a PSK identity hint */
980 static char *psk_identity_hint
= NULL
;
983 #ifndef OPENSSL_NO_SRP
984 char *srpuserseed
= NULL
;
985 char *srp_verifier_file
= NULL
;
987 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
988 int s_server_verify
= SSL_VERIFY_NONE
;
989 int s_server_session_id_context
= 1; /* anything will do */
990 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
991 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
992 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
993 #ifndef OPENSSL_NO_OCSP
994 int s_tlsextstatus
= 0;
996 int no_resume_ephemeral
= 0;
997 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
998 const char *s_serverinfo_file
= NULL
;
999 const char *keylog_file
= NULL
;
1000 int max_early_data
= -1;
1002 /* Init of few remaining global variables */
1007 s_nbio
= s_nbio_test
= 0;
1016 cctx
= SSL_CONF_CTX_new();
1017 vpm
= X509_VERIFY_PARAM_new();
1018 if (cctx
== NULL
|| vpm
== NULL
)
1020 SSL_CONF_CTX_set_flags(cctx
,
1021 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1023 prog
= opt_init(argc
, argv
, s_server_options
);
1024 while ((o
= opt_next()) != OPT_EOF
) {
1025 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1026 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1029 if (IS_NO_PROT_FLAG(o
))
1031 if (prot_opt
== 1 && no_prot_opt
) {
1033 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1040 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1043 opt_help(s_server_options
);
1049 if (socket_family
== AF_UNIX
) {
1050 OPENSSL_free(host
); host
= NULL
;
1051 OPENSSL_free(port
); port
= NULL
;
1054 socket_family
= AF_INET
;
1060 if (socket_family
== AF_UNIX
) {
1061 OPENSSL_free(host
); host
= NULL
;
1062 OPENSSL_free(port
); port
= NULL
;
1065 socket_family
= AF_INET6
;
1068 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1074 if (socket_family
== AF_UNIX
) {
1075 socket_family
= AF_UNSPEC
;
1078 OPENSSL_free(port
); port
= NULL
;
1079 OPENSSL_free(host
); host
= NULL
;
1080 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1082 "%s: -port argument malformed or ambiguous\n",
1089 if (socket_family
== AF_UNIX
) {
1090 socket_family
= AF_UNSPEC
;
1093 OPENSSL_free(port
); port
= NULL
;
1094 OPENSSL_free(host
); host
= NULL
;
1095 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1097 "%s: -accept argument malformed or ambiguous\n",
1104 socket_family
= AF_UNIX
;
1105 OPENSSL_free(host
); host
= BUF_strdup(opt_arg());
1106 OPENSSL_free(port
); port
= NULL
;
1109 unlink_unix_path
= 1;
1113 naccept
= atol(opt_arg());
1116 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1117 verify_args
.depth
= atoi(opt_arg());
1119 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1121 case OPT_UPPER_V_VERIFY
:
1123 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1124 SSL_VERIFY_CLIENT_ONCE
;
1125 verify_args
.depth
= atoi(opt_arg());
1128 "verify depth is %d, must return a certificate\n",
1132 context
= (unsigned char *)opt_arg();
1135 s_cert_file
= opt_arg();
1138 if (!set_nameopt(opt_arg()))
1142 crl_file
= opt_arg();
1144 case OPT_CRL_DOWNLOAD
:
1147 case OPT_SERVERINFO
:
1148 s_serverinfo_file
= opt_arg();
1151 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_cert_format
))
1155 s_key_file
= opt_arg();
1158 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1162 passarg
= opt_arg();
1164 case OPT_CERT_CHAIN
:
1165 s_chain_file
= opt_arg();
1168 #ifndef OPENSSL_NO_DH
1173 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dcert_format
))
1177 s_dcert_file
= opt_arg();
1180 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dkey_format
))
1184 dpassarg
= opt_arg();
1187 s_dkey_file
= opt_arg();
1189 case OPT_DCERT_CHAIN
:
1190 s_dchain_file
= opt_arg();
1201 case OPT_CHAINCAPATH
:
1202 chCApath
= opt_arg();
1204 case OPT_VERIFYCAPATH
:
1205 vfyCApath
= opt_arg();
1214 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1218 if (ssl_args
== NULL
)
1219 ssl_args
= sk_OPENSSL_STRING_new_null();
1220 if (ssl_args
== NULL
1221 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1222 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1223 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1228 if (!opt_verify(o
, vpm
))
1233 if (!args_excert(o
, &exc
))
1236 case OPT_VERIFY_RET_ERROR
:
1237 verify_args
.return_error
= 1;
1239 case OPT_VERIFY_QUIET
:
1240 verify_args
.quiet
= 1;
1242 case OPT_BUILD_CHAIN
:
1251 case OPT_CHAINCAFILE
:
1252 chCAfile
= opt_arg();
1254 case OPT_VERIFYCAFILE
:
1255 vfyCAfile
= opt_arg();
1261 s_nbio
= s_nbio_test
= 1;
1266 case OPT_NO_IGN_EOF
:
1272 case OPT_TLSEXTDEBUG
:
1276 #ifndef OPENSSL_NO_OCSP
1280 case OPT_STATUS_VERBOSE
:
1281 #ifndef OPENSSL_NO_OCSP
1282 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1285 case OPT_STATUS_TIMEOUT
:
1286 #ifndef OPENSSL_NO_OCSP
1288 tlscstatp
.timeout
= atoi(opt_arg());
1291 case OPT_STATUS_URL
:
1292 #ifndef OPENSSL_NO_OCSP
1294 if (!OCSP_parse_url(opt_arg(),
1297 &tlscstatp
.path
, &tlscstatp
.use_ssl
)) {
1298 BIO_printf(bio_err
, "Error parsing URL\n");
1303 case OPT_STATUS_FILE
:
1304 #ifndef OPENSSL_NO_OCSP
1306 tlscstatp
.respin
= opt_arg();
1313 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1316 #ifndef OPENSSL_NO_SSL_TRACE
1320 case OPT_SECURITY_DEBUG
:
1323 case OPT_SECURITY_DEBUG_VERBOSE
:
1336 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1339 #ifndef OPENSSL_NO_DH
1343 case OPT_NO_RESUME_EPHEMERAL
:
1344 no_resume_ephemeral
= 1;
1347 #ifndef OPENSSL_NO_PSK
1348 psk_identity_hint
= opt_arg();
1352 #ifndef OPENSSL_NO_PSK
1353 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1354 if (isxdigit(_UC(*p
)))
1356 BIO_printf(bio_err
, "Not a hex number '%s'\n", *argv
);
1362 #ifndef OPENSSL_NO_SRP
1363 srp_verifier_file
= opt_arg();
1364 if (min_version
< TLS1_VERSION
)
1365 min_version
= TLS1_VERSION
;
1368 case OPT_SRPUSERSEED
:
1369 #ifndef OPENSSL_NO_SRP
1370 srpuserseed
= opt_arg();
1371 if (min_version
< TLS1_VERSION
)
1372 min_version
= TLS1_VERSION
;
1387 case OPT_SSL_CONFIG
:
1388 ssl_config
= opt_arg();
1391 min_version
= SSL3_VERSION
;
1392 max_version
= SSL3_VERSION
;
1395 min_version
= TLS1_3_VERSION
;
1396 max_version
= TLS1_3_VERSION
;
1399 min_version
= TLS1_2_VERSION
;
1400 max_version
= TLS1_2_VERSION
;
1403 min_version
= TLS1_1_VERSION
;
1404 max_version
= TLS1_1_VERSION
;
1407 min_version
= TLS1_VERSION
;
1408 max_version
= TLS1_VERSION
;
1411 #ifndef OPENSSL_NO_DTLS
1412 meth
= DTLS_server_method();
1413 socket_type
= SOCK_DGRAM
;
1417 #ifndef OPENSSL_NO_DTLS
1418 meth
= DTLS_server_method();
1419 min_version
= DTLS1_VERSION
;
1420 max_version
= DTLS1_VERSION
;
1421 socket_type
= SOCK_DGRAM
;
1425 #ifndef OPENSSL_NO_DTLS
1426 meth
= DTLS_server_method();
1427 min_version
= DTLS1_2_VERSION
;
1428 max_version
= DTLS1_2_VERSION
;
1429 socket_type
= SOCK_DGRAM
;
1433 #ifndef OPENSSL_NO_DTLS
1434 enable_timeouts
= 1;
1438 #ifndef OPENSSL_NO_DTLS
1439 socket_mtu
= atol(opt_arg());
1443 #ifndef OPENSSL_NO_DTLS
1448 session_id_prefix
= opt_arg();
1451 engine
= setup_engine(opt_arg(), 1);
1456 case OPT_SERVERNAME
:
1457 tlsextcbp
.servername
= opt_arg();
1459 case OPT_SERVERNAME_FATAL
:
1460 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1463 s_cert_file2
= opt_arg();
1466 s_key_file2
= opt_arg();
1468 case OPT_NEXTPROTONEG
:
1469 # ifndef OPENSSL_NO_NEXTPROTONEG
1470 next_proto_neg_in
= opt_arg();
1474 alpn_in
= opt_arg();
1476 case OPT_SRTP_PROFILES
:
1477 #ifndef OPENSSL_NO_SRTP
1478 srtp_profiles
= opt_arg();
1481 case OPT_KEYMATEXPORT
:
1482 keymatexportlabel
= opt_arg();
1484 case OPT_KEYMATEXPORTLEN
:
1485 keymatexportlen
= atoi(opt_arg());
1490 case OPT_SPLIT_SEND_FRAG
:
1491 split_send_fragment
= atoi(opt_arg());
1492 if (split_send_fragment
== 0) {
1494 * Not allowed - set to a deliberately bad value so we get an
1495 * error message below
1497 split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
+ 1;
1500 case OPT_MAX_PIPELINES
:
1501 max_pipelines
= atoi(opt_arg());
1504 read_buf_len
= atoi(opt_arg());
1506 case OPT_KEYLOG_FILE
:
1507 keylog_file
= opt_arg();
1510 max_early_data
= atoi(opt_arg());
1511 if (max_early_data
< 0) {
1512 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1516 case OPT_EARLY_DATA
:
1521 argc
= opt_num_rest();
1524 #ifndef OPENSSL_NO_DTLS
1525 if (www
&& socket_type
== SOCK_DGRAM
) {
1526 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1530 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1531 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1537 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1539 "Can't use unix sockets and datagrams together\n");
1544 if (split_send_fragment
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1545 BIO_printf(bio_err
, "Bad split send fragment size\n");
1549 if (max_pipelines
> SSL_MAX_PIPELINES
) {
1550 BIO_printf(bio_err
, "Bad max pipelines value\n");
1554 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1555 BIO_printf(bio_err
, "Error getting password\n");
1559 if (s_key_file
== NULL
)
1560 s_key_file
= s_cert_file
;
1562 if (s_key_file2
== NULL
)
1563 s_key_file2
= s_cert_file2
;
1565 if (!load_excert(&exc
))
1569 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1570 "server certificate private key file");
1572 ERR_print_errors(bio_err
);
1576 s_cert
= load_cert(s_cert_file
, s_cert_format
,
1577 "server certificate file");
1580 ERR_print_errors(bio_err
);
1584 if (!load_certs(s_chain_file
, &s_chain
, FORMAT_PEM
, NULL
,
1585 "server certificate chain"))
1589 if (tlsextcbp
.servername
) {
1590 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1591 "second server certificate private key file");
1593 ERR_print_errors(bio_err
);
1597 s_cert2
= load_cert(s_cert_file2
, s_cert_format
,
1598 "second server certificate file");
1601 ERR_print_errors(bio_err
);
1606 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1607 if (next_proto_neg_in
) {
1609 next_proto
.data
= next_protos_parse(&len
, next_proto_neg_in
);
1610 if (next_proto
.data
== NULL
)
1612 next_proto
.len
= len
;
1614 next_proto
.data
= NULL
;
1617 alpn_ctx
.data
= NULL
;
1620 alpn_ctx
.data
= next_protos_parse(&len
, alpn_in
);
1621 if (alpn_ctx
.data
== NULL
)
1628 crl
= load_crl(crl_file
, crl_format
);
1630 BIO_puts(bio_err
, "Error loading CRL\n");
1631 ERR_print_errors(bio_err
);
1634 crls
= sk_X509_CRL_new_null();
1635 if (!crls
|| !sk_X509_CRL_push(crls
, crl
)) {
1636 BIO_puts(bio_err
, "Error adding CRL\n");
1637 ERR_print_errors(bio_err
);
1645 if (s_dkey_file
== NULL
)
1646 s_dkey_file
= s_dcert_file
;
1648 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1649 0, dpass
, engine
, "second certificate private key file");
1651 ERR_print_errors(bio_err
);
1655 s_dcert
= load_cert(s_dcert_file
, s_dcert_format
,
1656 "second server certificate file");
1659 ERR_print_errors(bio_err
);
1662 if (s_dchain_file
) {
1663 if (!load_certs(s_dchain_file
, &s_dchain
, FORMAT_PEM
, NULL
,
1664 "second server certificate chain"))
1670 if (!app_RAND_load_file(NULL
, 1) && inrand
== NULL
1671 && !RAND_status()) {
1673 "warning, not much extra random data, consider using the -rand option\n");
1676 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
1677 app_RAND_load_files(inrand
));
1679 if (bio_s_out
== NULL
) {
1680 if (s_quiet
&& !s_debug
) {
1681 bio_s_out
= BIO_new(BIO_s_null());
1682 if (s_msg
&& !bio_s_msg
)
1683 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1685 if (bio_s_out
== NULL
)
1686 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1689 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
1695 s_dcert_file
= NULL
;
1697 s_cert_file2
= NULL
;
1701 ctx
= SSL_CTX_new(meth
);
1703 ERR_print_errors(bio_err
);
1707 ssl_ctx_security_debug(ctx
, sdebug
);
1709 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1710 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1712 ERR_print_errors(bio_err
);
1716 if (SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1718 if (SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1721 if (session_id_prefix
) {
1722 if (strlen(session_id_prefix
) >= 32)
1724 "warning: id_prefix is too long, only one new session will be possible\n");
1725 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1726 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1727 ERR_print_errors(bio_err
);
1730 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1732 SSL_CTX_set_quiet_shutdown(ctx
, 1);
1734 ssl_ctx_set_excert(ctx
, exc
);
1737 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1739 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1741 init_session_cache_ctx(ctx
);
1743 SSL_CTX_sess_set_cache_size(ctx
, 128);
1746 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1748 if (split_send_fragment
> 0) {
1749 SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
);
1751 if (max_pipelines
> 0) {
1752 SSL_CTX_set_max_pipelines(ctx
, max_pipelines
);
1755 if (read_buf_len
> 0) {
1756 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1758 #ifndef OPENSSL_NO_SRTP
1759 if (srtp_profiles
!= NULL
) {
1760 /* Returns 0 on success! */
1761 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1762 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1763 ERR_print_errors(bio_err
);
1769 if (!ctx_set_verify_locations(ctx
, CAfile
, CApath
, noCAfile
, noCApath
)) {
1770 ERR_print_errors(bio_err
);
1773 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1774 BIO_printf(bio_err
, "Error setting verify params\n");
1775 ERR_print_errors(bio_err
);
1779 ssl_ctx_add_crls(ctx
, crls
, 0);
1780 if (!config_ctx(cctx
, ssl_args
, ctx
))
1783 if (!ssl_load_stores(ctx
, vfyCApath
, vfyCAfile
, chCApath
, chCAfile
,
1784 crls
, crl_download
)) {
1785 BIO_printf(bio_err
, "Error loading store locations\n");
1786 ERR_print_errors(bio_err
);
1791 ctx2
= SSL_CTX_new(meth
);
1793 ERR_print_errors(bio_err
);
1799 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
1802 ssl_ctx_security_debug(ctx
, sdebug
);
1804 if (session_id_prefix
) {
1805 if (strlen(session_id_prefix
) >= 32)
1807 "warning: id_prefix is too long, only one new session will be possible\n");
1808 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
1809 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1810 ERR_print_errors(bio_err
);
1813 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1815 SSL_CTX_set_quiet_shutdown(ctx2
, 1);
1817 ssl_ctx_set_excert(ctx2
, exc
);
1820 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
1823 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
1825 init_session_cache_ctx(ctx2
);
1827 SSL_CTX_sess_set_cache_size(ctx2
, 128);
1830 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
1832 if (!ctx_set_verify_locations(ctx2
, CAfile
, CApath
, noCAfile
,
1834 ERR_print_errors(bio_err
);
1837 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
1838 BIO_printf(bio_err
, "Error setting verify params\n");
1839 ERR_print_errors(bio_err
);
1843 ssl_ctx_add_crls(ctx2
, crls
, 0);
1844 if (!config_ctx(cctx
, ssl_args
, ctx2
))
1847 #ifndef OPENSSL_NO_NEXTPROTONEG
1848 if (next_proto
.data
)
1849 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
1853 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
1855 #ifndef OPENSSL_NO_DH
1860 dh
= load_dh_param(dhfile
);
1861 else if (s_cert_file
)
1862 dh
= load_dh_param(s_cert_file
);
1865 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1867 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
1869 (void)BIO_flush(bio_s_out
);
1872 SSL_CTX_set_dh_auto(ctx
, 1);
1873 else if (!SSL_CTX_set_tmp_dh(ctx
, dh
)) {
1874 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1875 ERR_print_errors(bio_err
);
1882 DH
*dh2
= load_dh_param(s_cert_file2
);
1884 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1885 (void)BIO_flush(bio_s_out
);
1892 SSL_CTX_set_dh_auto(ctx2
, 1);
1893 else if (!SSL_CTX_set_tmp_dh(ctx2
, dh
)) {
1894 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1895 ERR_print_errors(bio_err
);
1904 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
1907 if (s_serverinfo_file
!= NULL
1908 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
1909 ERR_print_errors(bio_err
);
1913 if (ctx2
&& !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
1916 if (s_dcert
!= NULL
) {
1917 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
1921 if (no_resume_ephemeral
) {
1922 SSL_CTX_set_not_resumable_session_callback(ctx
,
1923 not_resumable_sess_cb
);
1926 SSL_CTX_set_not_resumable_session_callback(ctx2
,
1927 not_resumable_sess_cb
);
1929 #ifndef OPENSSL_NO_PSK
1930 if (psk_key
!= NULL
) {
1932 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
1933 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
1936 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
1937 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
1938 ERR_print_errors(bio_err
);
1943 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
1944 if (!SSL_CTX_set_session_id_context(ctx
,
1945 (void *)&s_server_session_id_context
,
1946 sizeof s_server_session_id_context
)) {
1947 BIO_printf(bio_err
, "error setting session id context\n");
1948 ERR_print_errors(bio_err
);
1952 /* Set DTLS cookie generation and verification callbacks */
1953 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
1954 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
1957 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
1958 if (!SSL_CTX_set_session_id_context(ctx2
,
1959 (void *)&s_server_session_id_context
,
1960 sizeof s_server_session_id_context
)) {
1961 BIO_printf(bio_err
, "error setting session id context\n");
1962 ERR_print_errors(bio_err
);
1965 tlsextcbp
.biodebug
= bio_s_out
;
1966 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
1967 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
1968 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
1969 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
1972 #ifndef OPENSSL_NO_SRP
1973 if (srp_verifier_file
!= NULL
) {
1974 srp_callback_parm
.vb
= SRP_VBASE_new(srpuserseed
);
1975 srp_callback_parm
.user
= NULL
;
1976 srp_callback_parm
.login
= NULL
;
1978 SRP_VBASE_init(srp_callback_parm
.vb
,
1979 srp_verifier_file
)) != SRP_NO_ERROR
) {
1981 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
1982 srp_verifier_file
, ret
);
1985 SSL_CTX_set_verify(ctx
, SSL_VERIFY_NONE
, verify_callback
);
1986 SSL_CTX_set_srp_cb_arg(ctx
, &srp_callback_parm
);
1987 SSL_CTX_set_srp_username_callback(ctx
, ssl_srp_server_param_cb
);
1990 if (CAfile
!= NULL
) {
1991 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
1994 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
1996 #ifndef OPENSSL_NO_OCSP
1997 if (s_tlsextstatus
) {
1998 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
1999 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2001 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2002 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2006 if (set_keylog_file(ctx
, keylog_file
))
2009 if (max_early_data
>= 0)
2010 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2012 BIO_printf(bio_s_out
, "ACCEPT\n");
2013 (void)BIO_flush(bio_s_out
);
2015 server_cb
= rev_body
;
2017 server_cb
= www_body
;
2019 server_cb
= sv_body
;
2021 if (socket_family
== AF_UNIX
2022 && unlink_unix_path
)
2025 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
,
2026 server_cb
, context
, naccept
);
2027 print_stats(bio_s_out
, ctx
);
2031 set_keylog_file(NULL
, NULL
);
2033 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2035 EVP_PKEY_free(s_key
);
2036 EVP_PKEY_free(s_dkey
);
2037 sk_X509_pop_free(s_chain
, X509_free
);
2038 sk_X509_pop_free(s_dchain
, X509_free
);
2040 OPENSSL_free(dpass
);
2043 X509_VERIFY_PARAM_free(vpm
);
2045 OPENSSL_free(tlscstatp
.host
);
2046 OPENSSL_free(tlscstatp
.port
);
2047 OPENSSL_free(tlscstatp
.path
);
2050 EVP_PKEY_free(s_key2
);
2051 #ifndef OPENSSL_NO_NEXTPROTONEG
2052 OPENSSL_free(next_proto
.data
);
2054 OPENSSL_free(alpn_ctx
.data
);
2055 ssl_excert_free(exc
);
2056 sk_OPENSSL_STRING_free(ssl_args
);
2057 SSL_CONF_CTX_free(cctx
);
2058 release_engine(engine
);
2059 BIO_free(bio_s_out
);
2061 BIO_free(bio_s_msg
);
2063 #ifdef CHARSET_EBCDIC
2064 BIO_meth_free(methods_ebcdic
);
2069 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2071 BIO_printf(bio
, "%4ld items in the session cache\n",
2072 SSL_CTX_sess_number(ssl_ctx
));
2073 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2074 SSL_CTX_sess_connect(ssl_ctx
));
2075 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2076 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2077 BIO_printf(bio
, "%4ld client connects that finished\n",
2078 SSL_CTX_sess_connect_good(ssl_ctx
));
2079 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2080 SSL_CTX_sess_accept(ssl_ctx
));
2081 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2082 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2083 BIO_printf(bio
, "%4ld server accepts that finished\n",
2084 SSL_CTX_sess_accept_good(ssl_ctx
));
2085 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2086 BIO_printf(bio
, "%4ld session cache misses\n",
2087 SSL_CTX_sess_misses(ssl_ctx
));
2088 BIO_printf(bio
, "%4ld session cache timeouts\n",
2089 SSL_CTX_sess_timeouts(ssl_ctx
));
2090 BIO_printf(bio
, "%4ld callback cache hits\n",
2091 SSL_CTX_sess_cb_hits(ssl_ctx
));
2092 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2093 SSL_CTX_sess_cache_full(ssl_ctx
),
2094 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2097 static int sv_body(int s
, int stype
, unsigned char *context
)
2106 struct timeval timeout
;
2107 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2110 struct timeval
*timeoutp
;
2113 buf
= app_malloc(bufsize
, "server buffer");
2115 if (!BIO_socket_nbio(s
, 1))
2116 ERR_print_errors(bio_err
);
2118 BIO_printf(bio_err
, "Turned on non blocking io\n");
2124 if (s_tlsextdebug
) {
2125 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2126 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2130 && !SSL_set_session_id_context(con
,
2131 context
, strlen((char *)context
))) {
2132 BIO_printf(bio_err
, "Error setting session id context\n");
2137 if (!SSL_clear(con
)) {
2138 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2142 #ifndef OPENSSL_NO_DTLS
2143 if (stype
== SOCK_DGRAM
) {
2145 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2147 if (enable_timeouts
) {
2149 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2150 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2153 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2154 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2158 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2159 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2160 DTLS_get_link_min_mtu(con
));
2165 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2166 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2167 BIO_printf(bio_err
, "Failed to set MTU\n");
2173 /* want to do MTU discovery */
2174 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2176 /* turn on cookie exchange */
2177 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2180 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2185 test
= BIO_new(BIO_f_nbio_test());
2186 sbio
= BIO_push(test
, sbio
);
2189 SSL_set_bio(con
, sbio
, sbio
);
2190 SSL_set_accept_state(con
);
2191 /* SSL_set_fd(con,s); */
2194 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2195 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2198 #ifndef OPENSSL_NO_SSL_TRACE
2200 SSL_set_msg_callback(con
, SSL_trace
);
2203 SSL_set_msg_callback(con
, msg_cb
);
2204 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2207 if (s_tlsextdebug
) {
2208 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2209 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2213 int write_header
= 1, edret
= SSL_READ_EARLY_ERROR
;
2216 while (edret
!= SSL_READ_EARLY_FINISH
) {
2218 edret
= SSL_read_early(con
, buf
, bufsize
, &readbytes
);
2219 if (edret
!= SSL_READ_EARLY_ERROR
)
2222 switch (SSL_get_error(con
, 0)) {
2223 case SSL_ERROR_WANT_WRITE
:
2224 case SSL_ERROR_WANT_ASYNC
:
2225 case SSL_ERROR_WANT_READ
:
2226 /* Just keep trying - busy waiting */
2229 BIO_printf(bio_err
, "Error reading early data\n");
2230 ERR_print_errors(bio_err
);
2234 if (readbytes
> 0) {
2236 BIO_printf(bio_s_out
, "Early data received:\n");
2239 raw_write_stdout(buf
, (unsigned int)readbytes
);
2240 (void)BIO_flush(bio_s_out
);
2244 BIO_printf(bio_s_out
, "No early data received\n");
2246 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2249 if (fileno_stdin() > s
)
2250 width
= fileno_stdin() + 1;
2254 int read_from_terminal
;
2255 int read_from_sslcon
;
2257 read_from_terminal
= 0;
2258 read_from_sslcon
= SSL_has_pending(con
)
2259 || (async
&& SSL_waiting_for_async(con
));
2261 if (!read_from_sslcon
) {
2263 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2264 openssl_fdset(fileno_stdin(), &readfds
);
2266 openssl_fdset(s
, &readfds
);
2268 * Note: under VMS with SOCKETSHR the second parameter is
2269 * currently of type (int *) whereas under other systems it is
2270 * (void *) if you don't have a cast it will choke the compiler:
2271 * if you do have a cast then you can either go for (int *) or
2274 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2276 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2277 * only on sockets. As a workaround we timeout the select every
2278 * second and check for any keypress. In a proper Windows
2279 * application we wouldn't do this because it is inefficient.
2283 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &tv
);
2284 if (has_stdin_waiting())
2285 read_from_terminal
= 1;
2286 if ((i
< 0) || (!i
&& !read_from_terminal
))
2289 if ((SSL_version(con
) == DTLS1_VERSION
) &&
2290 DTLSv1_get_timeout(con
, &timeout
))
2291 timeoutp
= &timeout
;
2295 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2297 if ((SSL_version(con
) == DTLS1_VERSION
)
2298 && DTLSv1_handle_timeout(con
) > 0) {
2299 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2304 if (FD_ISSET(fileno_stdin(), &readfds
))
2305 read_from_terminal
= 1;
2307 if (FD_ISSET(s
, &readfds
))
2308 read_from_sslcon
= 1;
2310 if (read_from_terminal
) {
2314 i
= raw_read_stdin(buf
, bufsize
/ 2);
2316 /* both loops are skipped when i <= 0 */
2317 for (j
= 0; j
< i
; j
++)
2320 for (j
= i
- 1; j
>= 0; j
--) {
2321 buf
[j
+ lf_num
] = buf
[j
];
2322 if (buf
[j
] == '\n') {
2325 buf
[j
+ lf_num
] = '\r';
2328 assert(lf_num
== 0);
2330 i
= raw_read_stdin(buf
, bufsize
);
2332 if (!s_quiet
&& !s_brief
) {
2333 if ((i
<= 0) || (buf
[0] == 'Q')) {
2334 BIO_printf(bio_s_out
, "DONE\n");
2335 (void)BIO_flush(bio_s_out
);
2337 close_accept_socket();
2341 if ((i
<= 0) || (buf
[0] == 'q')) {
2342 BIO_printf(bio_s_out
, "DONE\n");
2343 (void)BIO_flush(bio_s_out
);
2344 if (SSL_version(con
) != DTLS1_VERSION
)
2347 * close_accept_socket(); ret= -11;
2351 #ifndef OPENSSL_NO_HEARTBEATS
2352 if ((buf
[0] == 'B') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2353 BIO_printf(bio_err
, "HEARTBEATING\n");
2359 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2360 SSL_renegotiate(con
);
2361 i
= SSL_do_handshake(con
);
2362 printf("SSL_do_handshake -> %d\n", i
);
2366 * strcpy(buf,"server side RE-NEGOTIATE\n");
2369 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2371 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2373 SSL_renegotiate(con
);
2374 i
= SSL_do_handshake(con
);
2375 printf("SSL_do_handshake -> %d\n", i
);
2379 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2383 if ((buf
[0] == 'K' || buf
[0] == 'k')
2384 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2385 SSL_key_update(con
, buf
[0] == 'K' ?
2386 SSL_KEY_UPDATE_REQUESTED
2387 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2388 i
= SSL_do_handshake(con
);
2389 printf("SSL_do_handshake -> %d\n", i
);
2393 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2397 if (buf
[0] == 'P') {
2398 static const char *str
= "Lets print some clear text\n";
2399 BIO_write(SSL_get_wbio(con
), str
, strlen(str
));
2401 if (buf
[0] == 'S') {
2402 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2405 #ifdef CHARSET_EBCDIC
2406 ebcdic2ascii(buf
, buf
, i
);
2410 /* should do a select for the write */
2413 if (++count
== 100) {
2415 SSL_renegotiate(con
);
2418 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2419 #ifndef OPENSSL_NO_SRP
2420 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2421 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2422 SRP_user_pwd_free(srp_callback_parm
.user
);
2423 srp_callback_parm
.user
=
2424 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2425 srp_callback_parm
.login
);
2426 if (srp_callback_parm
.user
)
2427 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2428 srp_callback_parm
.user
->info
);
2430 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2431 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2434 switch (SSL_get_error(con
, k
)) {
2435 case SSL_ERROR_NONE
:
2437 case SSL_ERROR_WANT_ASYNC
:
2438 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2439 (void)BIO_flush(bio_s_out
);
2440 wait_for_async(con
);
2442 case SSL_ERROR_WANT_WRITE
:
2443 case SSL_ERROR_WANT_READ
:
2444 case SSL_ERROR_WANT_X509_LOOKUP
:
2445 BIO_printf(bio_s_out
, "Write BLOCK\n");
2446 (void)BIO_flush(bio_s_out
);
2448 case SSL_ERROR_WANT_ASYNC_JOB
:
2450 * This shouldn't ever happen in s_server. Treat as an error
2452 case SSL_ERROR_SYSCALL
:
2454 BIO_printf(bio_s_out
, "ERROR\n");
2455 (void)BIO_flush(bio_s_out
);
2456 ERR_print_errors(bio_err
);
2460 case SSL_ERROR_ZERO_RETURN
:
2461 BIO_printf(bio_s_out
, "DONE\n");
2462 (void)BIO_flush(bio_s_out
);
2474 if (read_from_sslcon
) {
2476 * init_ssl_connection handles all async events itself so if we're
2477 * waiting for async then we shouldn't go back into
2478 * init_ssl_connection
2480 if ((!async
|| !SSL_waiting_for_async(con
))
2481 && !SSL_is_init_finished(con
)) {
2482 i
= init_ssl_connection(con
);
2487 } else if (i
== 0) {
2493 i
= SSL_read(con
, (char *)buf
, bufsize
);
2494 #ifndef OPENSSL_NO_SRP
2495 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2496 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2497 SRP_user_pwd_free(srp_callback_parm
.user
);
2498 srp_callback_parm
.user
=
2499 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2500 srp_callback_parm
.login
);
2501 if (srp_callback_parm
.user
)
2502 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2503 srp_callback_parm
.user
->info
);
2505 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2506 i
= SSL_read(con
, (char *)buf
, bufsize
);
2509 switch (SSL_get_error(con
, i
)) {
2510 case SSL_ERROR_NONE
:
2511 #ifdef CHARSET_EBCDIC
2512 ascii2ebcdic(buf
, buf
, i
);
2514 raw_write_stdout(buf
, (unsigned int)i
);
2515 (void)BIO_flush(bio_s_out
);
2516 if (SSL_has_pending(con
))
2519 case SSL_ERROR_WANT_ASYNC
:
2520 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2521 (void)BIO_flush(bio_s_out
);
2522 wait_for_async(con
);
2524 case SSL_ERROR_WANT_WRITE
:
2525 case SSL_ERROR_WANT_READ
:
2526 BIO_printf(bio_s_out
, "Read BLOCK\n");
2527 (void)BIO_flush(bio_s_out
);
2529 case SSL_ERROR_WANT_ASYNC_JOB
:
2531 * This shouldn't ever happen in s_server. Treat as an error
2533 case SSL_ERROR_SYSCALL
:
2535 BIO_printf(bio_s_out
, "ERROR\n");
2536 (void)BIO_flush(bio_s_out
);
2537 ERR_print_errors(bio_err
);
2540 case SSL_ERROR_ZERO_RETURN
:
2541 BIO_printf(bio_s_out
, "DONE\n");
2542 (void)BIO_flush(bio_s_out
);
2551 BIO_printf(bio_s_out
, "shutting down SSL\n");
2552 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
2555 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2556 OPENSSL_clear_free(buf
, bufsize
);
2558 BIO_printf(bio_s_out
, "ACCEPT\n");
2559 (void)BIO_flush(bio_s_out
);
2563 static void close_accept_socket(void)
2565 BIO_printf(bio_err
, "shutdown accept socket\n");
2566 if (accept_socket
>= 0) {
2567 BIO_closesocket(accept_socket
);
2571 static int init_ssl_connection(SSL
*con
)
2578 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2579 const unsigned char *next_proto_neg
;
2580 unsigned next_proto_neg_len
;
2582 unsigned char *exportedkeymat
;
2585 #ifndef OPENSSL_NO_DTLS
2587 BIO_ADDR
*client
= NULL
;
2589 if ((client
= BIO_ADDR_new()) == NULL
) {
2590 BIO_printf(bio_err
, "ERROR - memory\n");
2593 i
= DTLSv1_listen(con
, client
);
2598 wbio
= SSL_get_wbio(con
);
2600 BIO_get_fd(wbio
, &fd
);
2603 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2604 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2605 BIO_ADDR_free(client
);
2608 BIO_ADDR_free(client
);
2610 i
= SSL_accept(con
);
2612 BIO_ADDR_free(client
);
2618 i
= SSL_accept(con
);
2621 retry
= BIO_sock_should_retry(i
);
2622 #ifdef CERT_CB_TEST_RETRY
2625 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2626 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2628 "LOOKUP from certificate callback during accept\n");
2629 i
= SSL_accept(con
);
2631 retry
= BIO_sock_should_retry(i
);
2636 #ifndef OPENSSL_NO_SRP
2638 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2639 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
2640 srp_callback_parm
.login
);
2641 SRP_user_pwd_free(srp_callback_parm
.user
);
2642 srp_callback_parm
.user
=
2643 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2644 srp_callback_parm
.login
);
2645 if (srp_callback_parm
.user
)
2646 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2647 srp_callback_parm
.user
->info
);
2649 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2650 i
= SSL_accept(con
);
2652 retry
= BIO_sock_should_retry(i
);
2655 } while (i
< 0 && SSL_waiting_for_async(con
));
2658 if ((dtlslisten
&& i
== 0)
2659 || (!dtlslisten
&& retry
)) {
2660 BIO_printf(bio_s_out
, "DELAY\n");
2664 BIO_printf(bio_err
, "ERROR\n");
2666 verify_err
= SSL_get_verify_result(con
);
2667 if (verify_err
!= X509_V_OK
) {
2668 BIO_printf(bio_err
, "verify error:%s\n",
2669 X509_verify_cert_error_string(verify_err
));
2671 /* Always print any error messages */
2672 ERR_print_errors(bio_err
);
2677 print_ssl_summary(con
);
2679 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
2681 peer
= SSL_get_peer_certificate(con
);
2683 BIO_printf(bio_s_out
, "Client certificate\n");
2684 PEM_write_bio_X509(bio_s_out
, peer
);
2685 X509_NAME_oneline(X509_get_subject_name(peer
), buf
, sizeof buf
);
2686 BIO_printf(bio_s_out
, "subject=%s\n", buf
);
2687 X509_NAME_oneline(X509_get_issuer_name(peer
), buf
, sizeof buf
);
2688 BIO_printf(bio_s_out
, "issuer=%s\n", buf
);
2693 if (SSL_get_shared_ciphers(con
, buf
, sizeof buf
) != NULL
)
2694 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
2695 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
2696 ssl_print_sigalgs(bio_s_out
, con
);
2697 #ifndef OPENSSL_NO_EC
2698 ssl_print_point_formats(bio_s_out
, con
);
2699 ssl_print_groups(bio_s_out
, con
, 0);
2701 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
2703 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2704 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
2705 if (next_proto_neg
) {
2706 BIO_printf(bio_s_out
, "NEXTPROTO is ");
2707 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
2708 BIO_printf(bio_s_out
, "\n");
2711 #ifndef OPENSSL_NO_SRTP
2713 SRTP_PROTECTION_PROFILE
*srtp_profile
2714 = SSL_get_selected_srtp_profile(con
);
2717 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
2718 srtp_profile
->name
);
2721 if (SSL_session_reused(con
))
2722 BIO_printf(bio_s_out
, "Reused session-id\n");
2723 BIO_printf(bio_s_out
, "Secure Renegotiation IS%s supported\n",
2724 SSL_get_secure_renegotiation_support(con
) ? "" : " NOT");
2725 if (keymatexportlabel
!= NULL
) {
2726 BIO_printf(bio_s_out
, "Keying material exporter:\n");
2727 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
2728 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
2729 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
2730 if (!SSL_export_keying_material(con
, exportedkeymat
,
2733 strlen(keymatexportlabel
),
2735 BIO_printf(bio_s_out
, " Error\n");
2737 BIO_printf(bio_s_out
, " Keying material: ");
2738 for (i
= 0; i
< keymatexportlen
; i
++)
2739 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
2740 BIO_printf(bio_s_out
, "\n");
2742 OPENSSL_free(exportedkeymat
);
2745 (void)BIO_flush(bio_s_out
);
2749 #ifndef OPENSSL_NO_DH
2750 static DH
*load_dh_param(const char *dhfile
)
2755 if ((bio
= BIO_new_file(dhfile
, "r")) == NULL
)
2757 ret
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2764 static int www_body(int s
, int stype
, unsigned char *context
)
2770 const SSL_CIPHER
*c
;
2771 BIO
*io
, *ssl_bio
, *sbio
;
2773 int total_bytes
= 0;
2778 /* Set width for a select call if needed */
2781 buf
= app_malloc(bufsize
, "server www buffer");
2782 io
= BIO_new(BIO_f_buffer());
2783 ssl_bio
= BIO_new(BIO_f_ssl());
2784 if ((io
== NULL
) || (ssl_bio
== NULL
))
2788 if (!BIO_socket_nbio(s
, 1))
2789 ERR_print_errors(bio_err
);
2791 BIO_printf(bio_err
, "Turned on non blocking io\n");
2794 /* lets make the output buffer a reasonable size */
2795 if (!BIO_set_write_buffer_size(io
, bufsize
))
2798 if ((con
= SSL_new(ctx
)) == NULL
)
2801 if (s_tlsextdebug
) {
2802 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2803 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2807 && !SSL_set_session_id_context(con
, context
,
2808 strlen((char *)context
)))
2811 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2815 test
= BIO_new(BIO_f_nbio_test());
2816 sbio
= BIO_push(test
, sbio
);
2818 SSL_set_bio(con
, sbio
, sbio
);
2819 SSL_set_accept_state(con
);
2821 /* SSL_set_fd(con,s); */
2822 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
2823 BIO_push(io
, ssl_bio
);
2824 #ifdef CHARSET_EBCDIC
2825 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
2829 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2830 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2833 #ifndef OPENSSL_NO_SSL_TRACE
2835 SSL_set_msg_callback(con
, SSL_trace
);
2838 SSL_set_msg_callback(con
, msg_cb
);
2839 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2843 i
= BIO_gets(io
, buf
, bufsize
- 1);
2844 if (i
< 0) { /* error */
2845 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
2847 ERR_print_errors(bio_err
);
2850 BIO_printf(bio_s_out
, "read R BLOCK\n");
2851 #ifndef OPENSSL_NO_SRP
2852 if (BIO_should_io_special(io
)
2853 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
2854 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2855 SRP_user_pwd_free(srp_callback_parm
.user
);
2856 srp_callback_parm
.user
=
2857 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2858 srp_callback_parm
.login
);
2859 if (srp_callback_parm
.user
)
2860 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2861 srp_callback_parm
.user
->info
);
2863 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2867 #if !defined(OPENSSL_SYS_MSDOS)
2872 } else if (i
== 0) { /* end of input */
2877 /* else we have data */
2878 if (((www
== 1) && (strncmp("GET ", buf
, 4) == 0)) ||
2879 ((www
== 2) && (strncmp("GET /stats ", buf
, 11) == 0))) {
2882 STACK_OF(SSL_CIPHER
) *sk
;
2883 static const char *space
= " ";
2885 if (www
== 1 && strncmp("GET /reneg", buf
, 10) == 0) {
2886 if (strncmp("GET /renegcert", buf
, 14) == 0)
2888 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2890 i
= SSL_renegotiate(con
);
2891 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
2892 /* Send the HelloRequest */
2893 i
= SSL_do_handshake(con
);
2895 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
2896 SSL_get_error(con
, i
));
2897 ERR_print_errors(bio_err
);
2900 /* Wait for a ClientHello to come back */
2902 openssl_fdset(s
, &readfds
);
2903 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
2904 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
2905 BIO_printf(bio_s_out
,
2906 "Error waiting for client response\n");
2907 ERR_print_errors(bio_err
);
2911 * We're not actually expecting any data here and we ignore
2912 * any that is sent. This is just to force the handshake that
2913 * we're expecting to come from the client. If they haven't
2914 * sent one there's not much we can do.
2916 BIO_gets(io
, buf
, bufsize
- 1);
2920 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2921 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2922 BIO_puts(io
, "<pre>\n");
2923 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
2925 for (i
= 0; i
< local_argc
; i
++) {
2927 for (myp
= local_argv
[i
]; *myp
; myp
++)
2930 BIO_puts(io
, "<");
2933 BIO_puts(io
, ">");
2936 BIO_puts(io
, "&");
2939 BIO_write(io
, myp
, 1);
2942 BIO_write(io
, " ", 1);
2947 "Secure Renegotiation IS%s supported\n",
2948 SSL_get_secure_renegotiation_support(con
) ?
2952 * The following is evil and should not really be done
2954 BIO_printf(io
, "Ciphers supported in s_server binary\n");
2955 sk
= SSL_get_ciphers(con
);
2956 j
= sk_SSL_CIPHER_num(sk
);
2957 for (i
= 0; i
< j
; i
++) {
2958 c
= sk_SSL_CIPHER_value(sk
, i
);
2959 BIO_printf(io
, "%-11s:%-25s ",
2960 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
2961 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
2965 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
2968 "---\nCiphers common between both SSL end points:\n");
2972 BIO_write(io
, space
, 26 - j
);
2975 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
2977 BIO_write(io
, p
, 1);
2984 ssl_print_sigalgs(io
, con
);
2985 #ifndef OPENSSL_NO_EC
2986 ssl_print_groups(io
, con
, 0);
2988 BIO_printf(io
, (SSL_session_reused(con
)
2989 ? "---\nReused, " : "---\nNew, "));
2990 c
= SSL_get_current_cipher(con
);
2991 BIO_printf(io
, "%s, Cipher is %s\n",
2992 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
2993 SSL_SESSION_print(io
, SSL_get_session(con
));
2994 BIO_printf(io
, "---\n");
2995 print_stats(io
, SSL_get_SSL_CTX(con
));
2996 BIO_printf(io
, "---\n");
2997 peer
= SSL_get_peer_certificate(con
);
2999 BIO_printf(io
, "Client certificate\n");
3000 X509_print(io
, peer
);
3001 PEM_write_bio_X509(io
, peer
);
3005 BIO_puts(io
, "no client certificate available\n");
3006 BIO_puts(io
, "</BODY></HTML>\r\n\r\n");
3008 } else if ((www
== 2 || www
== 3)
3009 && (strncmp("GET /", buf
, 5) == 0)) {
3012 static const char *text
=
3013 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3019 for (e
= p
; *e
!= '\0'; e
++) {
3025 dot
= (e
[0] == '.') ? 2 : 0;
3028 dot
= (e
[0] == '.') ? 3 : 0;
3031 dot
= (e
[0] == '/') ? -1 : 0;
3035 dot
= (e
[0] == '/') ? 1 : 0;
3037 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3042 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3049 BIO_printf(io
, "'%s' contains '..' reference\r\n", p
);
3055 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3059 /* if a directory, do the index thang */
3060 if (app_isdir(p
) > 0) {
3062 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3066 if ((file
= BIO_new_file(p
, "r")) == NULL
) {
3068 BIO_printf(io
, "Error opening '%s'\r\n", p
);
3069 ERR_print_errors(io
);
3074 BIO_printf(bio_err
, "FILE:%s\n", p
);
3078 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3079 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3080 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3082 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3085 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3089 i
= BIO_read(file
, buf
, bufsize
);
3095 BIO_printf(bio_err
, "%d\n", i
);
3096 if (total_bytes
> 3 * 1024) {
3098 BIO_printf(bio_err
, "RENEGOTIATE\n");
3099 SSL_renegotiate(con
);
3103 for (j
= 0; j
< i
;) {
3106 if (++count
== 13) {
3107 SSL_renegotiate(con
);
3110 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3112 if (!BIO_should_retry(io
)
3113 && !SSL_waiting_for_async(con
))
3116 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3130 i
= (int)BIO_flush(io
);
3132 if (!BIO_should_retry(io
))
3138 /* make sure we re-use sessions */
3139 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3143 BIO_printf(bio_s_out
, "ACCEPT\n");
3149 static int rev_body(int s
, int stype
, unsigned char *context
)
3155 BIO
*io
, *ssl_bio
, *sbio
;
3157 buf
= app_malloc(bufsize
, "server rev buffer");
3158 io
= BIO_new(BIO_f_buffer());
3159 ssl_bio
= BIO_new(BIO_f_ssl());
3160 if ((io
== NULL
) || (ssl_bio
== NULL
))
3163 /* lets make the output buffer a reasonable size */
3164 if (!BIO_set_write_buffer_size(io
, bufsize
))
3167 if ((con
= SSL_new(ctx
)) == NULL
)
3170 if (s_tlsextdebug
) {
3171 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3172 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3175 && !SSL_set_session_id_context(con
, context
,
3176 strlen((char *)context
))) {
3177 ERR_print_errors(bio_err
);
3181 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3182 SSL_set_bio(con
, sbio
, sbio
);
3183 SSL_set_accept_state(con
);
3185 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3186 BIO_push(io
, ssl_bio
);
3187 #ifdef CHARSET_EBCDIC
3188 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
3192 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
3193 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3196 #ifndef OPENSSL_NO_SSL_TRACE
3198 SSL_set_msg_callback(con
, SSL_trace
);
3201 SSL_set_msg_callback(con
, msg_cb
);
3202 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3206 i
= BIO_do_handshake(io
);
3209 if (!BIO_should_retry(io
)) {
3210 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3211 ERR_print_errors(bio_err
);
3214 #ifndef OPENSSL_NO_SRP
3215 if (BIO_should_io_special(io
)
3216 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3217 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3218 SRP_user_pwd_free(srp_callback_parm
.user
);
3219 srp_callback_parm
.user
=
3220 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3221 srp_callback_parm
.login
);
3222 if (srp_callback_parm
.user
)
3223 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3224 srp_callback_parm
.user
->info
);
3226 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3231 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3232 print_ssl_summary(con
);
3235 i
= BIO_gets(io
, buf
, bufsize
- 1);
3236 if (i
< 0) { /* error */
3237 if (!BIO_should_retry(io
)) {
3239 ERR_print_errors(bio_err
);
3242 BIO_printf(bio_s_out
, "read R BLOCK\n");
3243 #ifndef OPENSSL_NO_SRP
3244 if (BIO_should_io_special(io
)
3245 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3246 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3247 SRP_user_pwd_free(srp_callback_parm
.user
);
3248 srp_callback_parm
.user
=
3249 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3250 srp_callback_parm
.login
);
3251 if (srp_callback_parm
.user
)
3252 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3253 srp_callback_parm
.user
->info
);
3255 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3259 #if !defined(OPENSSL_SYS_MSDOS)
3264 } else if (i
== 0) { /* end of input */
3266 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3269 char *p
= buf
+ i
- 1;
3270 while (i
&& (*p
== '\n' || *p
== '\r')) {
3274 if (!s_ign_eof
&& (i
== 5) && (strncmp(buf
, "CLOSE", 5) == 0)) {
3276 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3279 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3281 BIO_write(io
, buf
, i
+ 1);
3286 if (!BIO_should_retry(io
))
3292 /* make sure we re-use sessions */
3293 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3302 #define MAX_SESSION_ID_ATTEMPTS 10
3303 static int generate_session_id(const SSL
*ssl
, unsigned char *id
,
3304 unsigned int *id_len
)
3306 unsigned int count
= 0;
3308 if (RAND_bytes(id
, *id_len
) <= 0)
3311 * Prefix the session_id with the required prefix. NB: If our prefix
3312 * is too long, clip it - but there will be worse effects anyway, eg.
3313 * the server could only possibly create 1 session ID (ie. the
3314 * prefix!) so all future session negotiations will fail due to
3317 memcpy(id
, session_id_prefix
,
3318 (strlen(session_id_prefix
) < *id_len
) ?
3319 strlen(session_id_prefix
) : *id_len
);
3321 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3322 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3323 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3329 * By default s_server uses an in-memory cache which caches SSL_SESSION
3330 * structures without any serialisation. This hides some bugs which only
3331 * become apparent in deployed servers. By implementing a basic external
3332 * session cache some issues can be debugged using s_server.
3335 typedef struct simple_ssl_session_st
{
3340 struct simple_ssl_session_st
*next
;
3341 } simple_ssl_session
;
3343 static simple_ssl_session
*first
= NULL
;
3345 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3347 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3350 SSL_SESSION_get_id(session
, &sess
->idlen
);
3351 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3352 if (sess
->derlen
< 0) {
3353 BIO_printf(bio_err
, "Error encoding session\n");
3358 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3359 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3361 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3362 OPENSSL_free(sess
->id
);
3363 OPENSSL_free(sess
->der
);
3369 /* Assume it still works. */
3370 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3371 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3372 OPENSSL_free(sess
->id
);
3373 OPENSSL_free(sess
->der
);
3380 BIO_printf(bio_err
, "New session added to external cache\n");
3384 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3387 simple_ssl_session
*sess
;
3389 for (sess
= first
; sess
; sess
= sess
->next
) {
3390 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3391 const unsigned char *p
= sess
->der
;
3392 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3393 return d2i_SSL_SESSION(NULL
, &p
, sess
->derlen
);
3396 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3400 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3402 simple_ssl_session
*sess
, *prev
= NULL
;
3403 const unsigned char *id
;
3405 id
= SSL_SESSION_get_id(session
, &idlen
);
3406 for (sess
= first
; sess
; sess
= sess
->next
) {
3407 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3409 prev
->next
= sess
->next
;
3412 OPENSSL_free(sess
->id
);
3413 OPENSSL_free(sess
->der
);
3421 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3423 SSL_CTX_set_session_cache_mode(sctx
,
3424 SSL_SESS_CACHE_NO_INTERNAL
|
3425 SSL_SESS_CACHE_SERVER
);
3426 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3427 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3428 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3431 static void free_sessions(void)
3433 simple_ssl_session
*sess
, *tsess
;
3434 for (sess
= first
; sess
;) {
3435 OPENSSL_free(sess
->id
);
3436 OPENSSL_free(sess
->der
);
3439 OPENSSL_free(tsess
);
3444 #endif /* OPENSSL_NO_SOCK */