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 */
150 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
152 unsigned int max_psk_len
)
158 BIO_printf(bio_s_out
, "psk_server_cb\n");
160 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
164 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
165 (int)strlen(identity
), identity
);
167 /* here we could lookup the given identity e.g. from a database */
168 if (strcmp(identity
, psk_identity
) != 0) {
169 BIO_printf(bio_s_out
, "PSK error: client identity not found"
170 " (got '%s' expected '%s')\n", identity
, psk_identity
);
174 BIO_printf(bio_s_out
, "PSK client identity found\n");
176 /* convert the PSK key to binary */
177 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
179 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
183 if (key_len
> (int)max_psk_len
) {
185 "psk buffer of callback is too small (%d) for key (%ld)\n",
186 max_psk_len
, key_len
);
191 memcpy(psk
, key
, key_len
);
195 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
199 BIO_printf(bio_err
, "Error in PSK server callback\n");
200 (void)BIO_flush(bio_err
);
201 (void)BIO_flush(bio_s_out
);
206 #ifndef OPENSSL_NO_SRP
207 /* This is a context that we pass to callbacks */
208 typedef struct srpsrvparm_st
{
215 * This callback pretends to require some asynchronous logic in order to
216 * obtain a verifier. When the callback is called for a new connection we
217 * return with a negative value. This will provoke the accept etc to return
218 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
219 * (which would normally occur after a worker has finished) and we set the
222 static int ssl_srp_server_param_cb(SSL
*s
, int *ad
, void *arg
)
224 srpsrvparm
*p
= (srpsrvparm
*) arg
;
225 int ret
= SSL3_AL_FATAL
;
227 if (p
->login
== NULL
&& p
->user
== NULL
) {
228 p
->login
= SSL_get_srp_username(s
);
229 BIO_printf(bio_err
, "SRP username = \"%s\"\n", p
->login
);
233 if (p
->user
== NULL
) {
234 BIO_printf(bio_err
, "User %s doesn't exist\n", p
->login
);
238 if (SSL_set_srp_server_param
239 (s
, p
->user
->N
, p
->user
->g
, p
->user
->s
, p
->user
->v
,
240 p
->user
->info
) < 0) {
241 *ad
= SSL_AD_INTERNAL_ERROR
;
245 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
246 p
->login
, p
->user
->info
);
247 ret
= SSL_ERROR_NONE
;
250 SRP_user_pwd_free(p
->user
);
258 static int local_argc
= 0;
259 static char **local_argv
;
261 #ifdef CHARSET_EBCDIC
262 static int ebcdic_new(BIO
*bi
);
263 static int ebcdic_free(BIO
*a
);
264 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
265 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
266 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
267 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
268 static int ebcdic_puts(BIO
*bp
, const char *str
);
270 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
271 static BIO_METHOD
*methods_ebcdic
= NULL
;
273 /* This struct is "unwarranted chumminess with the compiler." */
279 static const BIO_METHOD
*BIO_f_ebcdic_filter()
281 if (methods_ebcdic
== NULL
) {
282 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
283 "EBCDIC/ASCII filter");
284 if (methods_ebcdic
== NULL
285 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
286 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
287 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
288 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
289 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
290 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
291 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
294 return methods_ebcdic
;
297 static int ebcdic_new(BIO
*bi
)
299 EBCDIC_OUTBUFF
*wbuf
;
301 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
302 wbuf
->alloced
= 1024;
303 wbuf
->buff
[0] = '\0';
305 BIO_set_data(bi
, wbuf
);
310 static int ebcdic_free(BIO
*a
)
312 EBCDIC_OUTBUFF
*wbuf
;
316 wbuf
= BIO_get_data(a
);
318 BIO_set_data(a
, NULL
);
324 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
327 BIO
*next
= BIO_next(b
);
329 if (out
== NULL
|| outl
== 0)
334 ret
= BIO_read(next
, out
, outl
);
336 ascii2ebcdic(out
, out
, ret
);
340 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
342 EBCDIC_OUTBUFF
*wbuf
;
343 BIO
*next
= BIO_next(b
);
347 if ((in
== NULL
) || (inl
<= 0))
352 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
354 if (inl
> (num
= wbuf
->alloced
)) {
355 num
= num
+ num
; /* double the size */
359 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
362 wbuf
->buff
[0] = '\0';
364 BIO_set_data(b
, wbuf
);
367 ebcdic2ascii(wbuf
->buff
, in
, inl
);
369 ret
= BIO_write(next
, wbuf
->buff
, inl
);
374 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
377 BIO
*next
= BIO_next(b
);
386 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
392 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
395 BIO
*next
= BIO_next(bp
);
399 /* return(BIO_gets(bp->next_bio,buf,size));*/
400 for (i
= 0; i
< size
- 1; ++i
) {
401 ret
= ebcdic_read(bp
, &buf
[i
], 1);
404 else if (buf
[i
] == '\n') {
411 return (ret
< 0 && i
== 0) ? ret
: i
;
414 static int ebcdic_puts(BIO
*bp
, const char *str
)
416 if (BIO_next(bp
) == NULL
)
418 return ebcdic_write(bp
, str
, strlen(str
));
422 /* This is a context that we pass to callbacks */
423 typedef struct tlsextctx_st
{
429 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
431 tlsextctx
*p
= (tlsextctx
*) arg
;
432 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
433 if (servername
&& p
->biodebug
)
434 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"%s\"\n",
438 return SSL_TLSEXT_ERR_NOACK
;
441 if (strcasecmp(servername
, p
->servername
))
442 return p
->extension_error
;
444 BIO_printf(p
->biodebug
, "Switching server context.\n");
445 SSL_set_SSL_CTX(s
, ctx2
);
448 return SSL_TLSEXT_ERR_OK
;
451 /* Structure passed to cert status callback */
453 typedef struct tlsextstatusctx_st
{
454 /* Default responder to use */
455 char *host
, *path
, *port
;
461 static tlsextstatusctx tlscstatp
= { NULL
, NULL
, NULL
, 0, -1, 0 };
463 #ifndef OPENSSL_NO_OCSP
465 * Certificate Status callback. This is called when a client includes a
466 * certificate status request extension. This is a simplified version. It
467 * examines certificates each time and makes one OCSP responder query for
468 * each request. A full version would store details such as the OCSP
469 * certificate IDs and minimise the number of OCSP responses by caching them
470 * until they were considered "expired".
473 static int cert_status_cb(SSL
*s
, void *arg
)
475 tlsextstatusctx
*srctx
= arg
;
476 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
478 unsigned char *rspder
= NULL
;
480 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
482 X509_STORE_CTX
*inctx
= NULL
;
484 OCSP_REQUEST
*req
= NULL
;
485 OCSP_RESPONSE
*resp
= NULL
;
486 OCSP_CERTID
*id
= NULL
;
487 STACK_OF(X509_EXTENSION
) *exts
;
488 int ret
= SSL_TLSEXT_ERR_NOACK
;
492 BIO_puts(bio_err
, "cert_status: callback called\n");
493 /* Build up OCSP query from server certificate */
494 x
= SSL_get_certificate(s
);
495 aia
= X509_get1_ocsp(x
);
497 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia
, 0),
498 &host
, &port
, &path
, &use_ssl
)) {
499 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
503 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
504 sk_OPENSSL_STRING_value(aia
, 0));
508 "cert_status: no AIA and no default responder URL\n");
514 use_ssl
= srctx
->use_ssl
;
517 inctx
= X509_STORE_CTX_new();
520 if (!X509_STORE_CTX_init(inctx
,
521 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s
)),
524 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
,
525 X509_get_issuer_name(x
));
527 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
530 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
531 X509_OBJECT_free(obj
);
534 req
= OCSP_REQUEST_new();
537 if (!OCSP_request_add0_id(req
, id
))
540 /* Add any extensions to the request */
541 SSL_get_tlsext_status_exts(s
, &exts
);
542 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
543 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
544 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
547 resp
= process_responder(req
, host
, path
, port
, use_ssl
, NULL
,
550 BIO_puts(bio_err
, "cert_status: error querying responder\n");
553 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
556 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
557 if (srctx
->verbose
) {
558 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
559 OCSP_RESPONSE_print(bio_err
, resp
, 2);
561 ret
= SSL_TLSEXT_ERR_OK
;
565 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
567 if (ret
!= SSL_TLSEXT_ERR_OK
)
568 ERR_print_errors(bio_err
);
573 X509_email_free(aia
);
575 OCSP_CERTID_free(id
);
576 OCSP_REQUEST_free(req
);
577 OCSP_RESPONSE_free(resp
);
578 X509_STORE_CTX_free(inctx
);
583 #ifndef OPENSSL_NO_NEXTPROTONEG
584 /* This is the context that we pass to next_proto_cb */
585 typedef struct tlsextnextprotoctx_st
{
588 } tlsextnextprotoctx
;
590 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
591 unsigned int *len
, void *arg
)
593 tlsextnextprotoctx
*next_proto
= arg
;
595 *data
= next_proto
->data
;
596 *len
= next_proto
->len
;
598 return SSL_TLSEXT_ERR_OK
;
600 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
602 /* This the context that we pass to alpn_cb */
603 typedef struct tlsextalpnctx_st
{
608 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
609 const unsigned char *in
, unsigned int inlen
, void *arg
)
611 tlsextalpnctx
*alpn_ctx
= arg
;
614 /* We can assume that |in| is syntactically valid. */
616 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
617 for (i
= 0; i
< inlen
;) {
619 BIO_write(bio_s_out
, ", ", 2);
620 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
623 BIO_write(bio_s_out
, "\n", 1);
626 if (SSL_select_next_proto
627 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
628 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
629 return SSL_TLSEXT_ERR_NOACK
;
633 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
634 BIO_write(bio_s_out
, *out
, *outlen
);
635 BIO_write(bio_s_out
, "\n", 1);
638 return SSL_TLSEXT_ERR_OK
;
641 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
643 /* disable resumption for sessions with forward secure ciphers */
644 return is_forward_secure
;
647 #ifndef OPENSSL_NO_SRP
648 static srpsrvparm srp_callback_parm
;
650 #ifndef OPENSSL_NO_SRTP
651 static char *srtp_profiles
= NULL
;
654 typedef enum OPTION_choice
{
655 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ENGINE
,
656 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
657 OPT_VERIFY
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
658 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
659 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
660 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
661 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
662 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
663 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
664 OPT_VERIFYCAFILE
, OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
665 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
666 OPT_STATUS_TIMEOUT
, OPT_STATUS_URL
, OPT_MSG
, OPT_MSGFILE
, OPT_TRACE
,
667 OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
, OPT_CRLF
,
668 OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
669 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_HINT
, OPT_PSK
, OPT_SRPVFILE
,
670 OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
, OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
,
671 OPT_SSL_CONFIG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
672 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
673 OPT_DTLS1_2
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
,
674 OPT_ID_PREFIX
, OPT_RAND
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
675 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
,
676 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
682 const OPTIONS s_server_options
[] = {
683 {"help", OPT_HELP
, '-', "Display this summary"},
684 {"port", OPT_PORT
, 'p',
685 "TCP/IP port to listen on for connections (default is " PORT
")"},
686 {"accept", OPT_ACCEPT
, 's',
687 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
689 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
691 {"4", OPT_4
, '-', "Use IPv4 only"},
692 {"6", OPT_6
, '-', "Use IPv6 only"},
694 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
696 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
697 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
698 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
699 "Turn on peer certificate verification, must have a cert"},
700 {"cert", OPT_CERT
, '<', "Certificate file to use; default is " TEST_CERT
},
701 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
702 {"serverinfo", OPT_SERVERINFO
, 's',
703 "PEM serverinfo file for certificate"},
704 {"certform", OPT_CERTFORM
, 'F',
705 "Certificate format (PEM or DER) PEM default"},
706 {"key", OPT_KEY
, '<',
707 "Private Key if not in -cert; default is " TEST_CERT
},
708 {"keyform", OPT_KEYFORM
, 'f',
709 "Key format (PEM, DER or ENGINE) PEM default"},
710 {"pass", OPT_PASS
, 's', "Private key file pass phrase source"},
711 {"dcert", OPT_DCERT
, '<',
712 "Second certificate file to use (usually for DSA)"},
713 {"dcertform", OPT_DCERTFORM
, 'F',
714 "Second certificate format (PEM or DER) PEM default"},
715 {"dkey", OPT_DKEY
, '<',
716 "Second private key file to use (usually for DSA)"},
717 {"dkeyform", OPT_DKEYFORM
, 'F',
718 "Second key format (PEM, DER or ENGINE) PEM default"},
719 {"dpass", OPT_DPASS
, 's', "Second private key file pass phrase source"},
720 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
721 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
722 {"debug", OPT_DEBUG
, '-', "Print more output"},
723 {"msg", OPT_MSG
, '-', "Show protocol messages"},
724 {"msgfile", OPT_MSGFILE
, '>',
725 "File to send output of -msg or -trace, instead of stdout"},
726 {"state", OPT_STATE
, '-', "Print the SSL states"},
727 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
728 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
729 {"no-CAfile", OPT_NOCAFILE
, '-',
730 "Do not load the default certificates file"},
731 {"no-CApath", OPT_NOCAPATH
, '-',
732 "Do not load certificates from the default certificates directory"},
733 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
734 {"quiet", OPT_QUIET
, '-', "No server output"},
735 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
736 "Disable caching and tickets if ephemeral (EC)DH is used"},
737 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
738 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
739 {"servername", OPT_SERVERNAME
, 's',
740 "Servername for HostName TLS extension"},
741 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
742 "mismatch send fatal alert (default warning alert)"},
743 {"cert2", OPT_CERT2
, '<',
744 "Certificate file to use for servername; default is" TEST_CERT2
},
745 {"key2", OPT_KEY2
, '<',
746 "-Private Key file to use for servername if not in -cert2"},
747 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
748 "Hex dump of all TLS extensions received"},
749 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
750 {"id_prefix", OPT_ID_PREFIX
, 's',
751 "Generate SSL/TLS session IDs prefixed by arg"},
752 {"rand", OPT_RAND
, 's',
753 "Load the file(s) into the random number generator"},
754 {"keymatexport", OPT_KEYMATEXPORT
, 's',
755 "Export keying material using label"},
756 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
757 "Export len bytes of keying material (default 20)"},
758 {"CRL", OPT_CRL
, '<', "CRL file to use"},
759 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
760 "Download CRL from distribution points"},
761 {"cert_chain", OPT_CERT_CHAIN
, '<',
762 "certificate chain file in PEM format"},
763 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
764 "second certificate chain file in PEM format"},
765 {"chainCApath", OPT_CHAINCAPATH
, '/',
766 "use dir as certificate store path to build CA certificate chain"},
767 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
768 "use dir as certificate store path to verify CA certificate"},
769 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
770 {"ext_cache", OPT_EXT_CACHE
, '-',
771 "Disable internal cache, setup and use external cache"},
772 {"CRLform", OPT_CRLFORM
, 'F', "CRL format (PEM or DER) PEM is default"},
773 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
774 "Close connection on verification error"},
775 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
776 "No verify output except verify errors"},
777 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build certificate chain"},
778 {"chainCAfile", OPT_CHAINCAFILE
, '<',
779 "CA file for certificate chain (PEM format)"},
780 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
781 "CA file for certificate verification (PEM format)"},
782 {"ign_eof", OPT_IGN_EOF
, '-', "ignore input eof (default when -quiet)"},
783 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input eof"},
784 #ifndef OPENSSL_NO_OCSP
785 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
786 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
787 "Print more output in certificate status callback"},
788 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
789 "Status request responder timeout"},
790 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
792 #ifndef OPENSSL_NO_SSL_TRACE
793 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
795 {"security_debug", OPT_SECURITY_DEBUG
, '-',
796 "Print output from SSL/TLS security framework"},
797 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
798 "Print more output from SSL/TLS security framework"},
799 {"brief", OPT_BRIEF
, '-',
800 "Restrict output to brief summary of connection parameters"},
801 {"rev", OPT_REV
, '-',
802 "act as a simple test server which just sends back with the received text reversed"},
803 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
804 {"ssl_config", OPT_SSL_CONFIG
, 's',
805 "Configure SSL_CTX using the configuration 'val'"},
806 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'n',
807 "Size used to split data for encrypt pipelines"},
808 {"max_pipelines", OPT_MAX_PIPELINES
, 'n',
809 "Maximum number of encrypt/decrypt pipelines to be used"},
810 {"read_buf", OPT_READ_BUF
, 'n',
811 "Default read buffer size to be used for connections"},
815 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
816 #ifndef OPENSSL_NO_PSK
817 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
818 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
820 #ifndef OPENSSL_NO_SRP
821 {"srpvfile", OPT_SRPVFILE
, '<', "The verifier file for SRP"},
822 {"srpuserseed", OPT_SRPUSERSEED
, 's',
823 "A seed string for a default user salt"},
825 #ifndef OPENSSL_NO_SSL3
826 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
828 #ifndef OPENSSL_NO_TLS1
829 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
831 #ifndef OPENSSL_NO_TLS1_1
832 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
834 #ifndef OPENSSL_NO_TLS1_2
835 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
837 #ifndef OPENSSL_NO_TLS1_3
838 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
840 #ifndef OPENSSL_NO_DTLS
841 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
842 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
843 {"mtu", OPT_MTU
, 'p', "Set link layer MTU"},
844 {"listen", OPT_LISTEN
, '-',
845 "Listen for a DTLS ClientHello with a cookie and then connect"},
847 #ifndef OPENSSL_NO_DTLS1
848 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
850 #ifndef OPENSSL_NO_DTLS1_2
851 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
853 #ifndef OPENSSL_NO_DH
854 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
856 #ifndef OPENSSL_NO_NEXTPROTONEG
857 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
858 "Set the advertised protocols for the NPN extension (comma-separated list)"},
860 #ifndef OPENSSL_NO_SRTP
861 {"use_srtp", OPT_SRTP_PROFILES
, 's',
862 "Offer SRTP key management with a colon-separated profile list"},
864 {"alpn", OPT_ALPN
, 's',
865 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
866 #ifndef OPENSSL_NO_ENGINE
867 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
869 {NULL
, OPT_EOF
, 0, NULL
}
872 #define IS_PROT_FLAG(o) \
873 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
874 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
876 int s_server_main(int argc
, char *argv
[])
878 ENGINE
*engine
= NULL
;
879 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
880 SSL_CONF_CTX
*cctx
= NULL
;
881 const SSL_METHOD
*meth
= TLS_server_method();
882 SSL_EXCERT
*exc
= NULL
;
883 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
884 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
885 STACK_OF(X509_CRL
) *crls
= NULL
;
886 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
887 X509_VERIFY_PARAM
*vpm
= NULL
;
888 const char *CApath
= NULL
, *CAfile
= NULL
, *chCApath
= NULL
, *chCAfile
= NULL
;
889 char *dpassarg
= NULL
, *dpass
= NULL
, *inrand
= NULL
;
890 char *passarg
= NULL
, *pass
= NULL
, *vfyCApath
= NULL
, *vfyCAfile
= NULL
;
891 char *crl_file
= NULL
, *prog
;
893 int unlink_unix_path
= 0;
895 do_server_cb server_cb
;
896 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
897 #ifndef OPENSSL_NO_DH
901 int nocert
= 0, ret
= 1;
902 int noCApath
= 0, noCAfile
= 0;
903 int s_cert_format
= FORMAT_PEM
, s_key_format
= FORMAT_PEM
;
904 int s_dcert_format
= FORMAT_PEM
, s_dkey_format
= FORMAT_PEM
;
905 int rev
= 0, naccept
= -1, sdebug
= 0;
906 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
;
907 int state
= 0, crl_format
= FORMAT_PEM
, crl_download
= 0;
909 char *port
= BUF_strdup(PORT
);
910 unsigned char *context
= NULL
;
912 EVP_PKEY
*s_key2
= NULL
;
913 X509
*s_cert2
= NULL
;
914 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
915 const char *ssl_config
= NULL
;
916 int read_buf_len
= 0;
917 #ifndef OPENSSL_NO_NEXTPROTONEG
918 const char *next_proto_neg_in
= NULL
;
919 tlsextnextprotoctx next_proto
= { NULL
, 0 };
921 const char *alpn_in
= NULL
;
922 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
923 #ifndef OPENSSL_NO_PSK
924 /* by default do not send a PSK identity hint */
925 static char *psk_identity_hint
= NULL
;
928 #ifndef OPENSSL_NO_SRP
929 char *srpuserseed
= NULL
;
930 char *srp_verifier_file
= NULL
;
932 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
933 int s_server_verify
= SSL_VERIFY_NONE
;
934 int s_server_session_id_context
= 1; /* anything will do */
935 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
936 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
937 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
938 #ifndef OPENSSL_NO_OCSP
939 int s_tlsextstatus
= 0;
941 int no_resume_ephemeral
= 0;
942 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
943 const char *s_serverinfo_file
= NULL
;
945 /* Init of few remaining global variables */
950 s_nbio
= s_nbio_test
= 0;
959 cctx
= SSL_CONF_CTX_new();
960 vpm
= X509_VERIFY_PARAM_new();
961 if (cctx
== NULL
|| vpm
== NULL
)
963 SSL_CONF_CTX_set_flags(cctx
,
964 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
966 prog
= opt_init(argc
, argv
, s_server_options
);
967 while ((o
= opt_next()) != OPT_EOF
) {
968 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
969 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
972 if (IS_NO_PROT_FLAG(o
))
974 if (prot_opt
== 1 && no_prot_opt
) {
976 "Cannot supply both a protocol flag and '-no_<prot>'\n");
983 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
986 opt_help(s_server_options
);
992 if (socket_family
== AF_UNIX
) {
993 OPENSSL_free(host
); host
= NULL
;
994 OPENSSL_free(port
); port
= NULL
;
997 socket_family
= AF_INET
;
1003 if (socket_family
== AF_UNIX
) {
1004 OPENSSL_free(host
); host
= NULL
;
1005 OPENSSL_free(port
); port
= NULL
;
1008 socket_family
= AF_INET6
;
1011 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1017 if (socket_family
== AF_UNIX
) {
1018 socket_family
= AF_UNSPEC
;
1021 OPENSSL_free(port
); port
= NULL
;
1022 OPENSSL_free(host
); host
= NULL
;
1023 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1025 "%s: -port argument malformed or ambiguous\n",
1032 if (socket_family
== AF_UNIX
) {
1033 socket_family
= AF_UNSPEC
;
1036 OPENSSL_free(port
); port
= NULL
;
1037 OPENSSL_free(host
); host
= NULL
;
1038 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1040 "%s: -accept argument malformed or ambiguous\n",
1047 socket_family
= AF_UNIX
;
1048 OPENSSL_free(host
); host
= BUF_strdup(opt_arg());
1049 OPENSSL_free(port
); port
= NULL
;
1052 unlink_unix_path
= 1;
1056 naccept
= atol(opt_arg());
1059 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1060 verify_args
.depth
= atoi(opt_arg());
1062 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1064 case OPT_UPPER_V_VERIFY
:
1066 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1067 SSL_VERIFY_CLIENT_ONCE
;
1068 verify_args
.depth
= atoi(opt_arg());
1071 "verify depth is %d, must return a certificate\n",
1075 context
= (unsigned char *)opt_arg();
1078 s_cert_file
= opt_arg();
1081 crl_file
= opt_arg();
1083 case OPT_CRL_DOWNLOAD
:
1086 case OPT_SERVERINFO
:
1087 s_serverinfo_file
= opt_arg();
1090 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_cert_format
))
1094 s_key_file
= opt_arg();
1097 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1101 passarg
= opt_arg();
1103 case OPT_CERT_CHAIN
:
1104 s_chain_file
= opt_arg();
1107 #ifndef OPENSSL_NO_DH
1112 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dcert_format
))
1116 s_dcert_file
= opt_arg();
1119 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &s_dkey_format
))
1123 dpassarg
= opt_arg();
1126 s_dkey_file
= opt_arg();
1128 case OPT_DCERT_CHAIN
:
1129 s_dchain_file
= opt_arg();
1140 case OPT_CHAINCAPATH
:
1141 chCApath
= opt_arg();
1143 case OPT_VERIFYCAPATH
:
1144 vfyCApath
= opt_arg();
1153 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1157 if (ssl_args
== NULL
)
1158 ssl_args
= sk_OPENSSL_STRING_new_null();
1159 if (ssl_args
== NULL
1160 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1161 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1162 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1167 if (!opt_verify(o
, vpm
))
1172 if (!args_excert(o
, &exc
))
1175 case OPT_VERIFY_RET_ERROR
:
1176 verify_args
.return_error
= 1;
1178 case OPT_VERIFY_QUIET
:
1179 verify_args
.quiet
= 1;
1181 case OPT_BUILD_CHAIN
:
1190 case OPT_CHAINCAFILE
:
1191 chCAfile
= opt_arg();
1193 case OPT_VERIFYCAFILE
:
1194 vfyCAfile
= opt_arg();
1200 s_nbio
= s_nbio_test
= 1;
1205 case OPT_NO_IGN_EOF
:
1211 case OPT_TLSEXTDEBUG
:
1215 #ifndef OPENSSL_NO_OCSP
1219 case OPT_STATUS_VERBOSE
:
1220 #ifndef OPENSSL_NO_OCSP
1221 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1224 case OPT_STATUS_TIMEOUT
:
1225 #ifndef OPENSSL_NO_OCSP
1227 tlscstatp
.timeout
= atoi(opt_arg());
1230 case OPT_STATUS_URL
:
1231 #ifndef OPENSSL_NO_OCSP
1233 if (!OCSP_parse_url(opt_arg(),
1236 &tlscstatp
.path
, &tlscstatp
.use_ssl
)) {
1237 BIO_printf(bio_err
, "Error parsing URL\n");
1246 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1249 #ifndef OPENSSL_NO_SSL_TRACE
1253 case OPT_SECURITY_DEBUG
:
1256 case OPT_SECURITY_DEBUG_VERBOSE
:
1269 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1272 #ifndef OPENSSL_NO_DH
1276 case OPT_NO_RESUME_EPHEMERAL
:
1277 no_resume_ephemeral
= 1;
1280 #ifndef OPENSSL_NO_PSK
1281 psk_identity_hint
= opt_arg();
1285 #ifndef OPENSSL_NO_PSK
1286 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1287 if (isxdigit(_UC(*p
)))
1289 BIO_printf(bio_err
, "Not a hex number '%s'\n", *argv
);
1295 #ifndef OPENSSL_NO_SRP
1296 srp_verifier_file
= opt_arg();
1297 if (min_version
< TLS1_VERSION
)
1298 min_version
= TLS1_VERSION
;
1301 case OPT_SRPUSERSEED
:
1302 #ifndef OPENSSL_NO_SRP
1303 srpuserseed
= opt_arg();
1304 if (min_version
< TLS1_VERSION
)
1305 min_version
= TLS1_VERSION
;
1320 case OPT_SSL_CONFIG
:
1321 ssl_config
= opt_arg();
1324 min_version
= SSL3_VERSION
;
1325 max_version
= SSL3_VERSION
;
1328 min_version
= TLS1_3_VERSION
;
1329 max_version
= TLS1_3_VERSION
;
1332 min_version
= TLS1_2_VERSION
;
1333 max_version
= TLS1_2_VERSION
;
1336 min_version
= TLS1_1_VERSION
;
1337 max_version
= TLS1_1_VERSION
;
1340 min_version
= TLS1_VERSION
;
1341 max_version
= TLS1_VERSION
;
1344 #ifndef OPENSSL_NO_DTLS
1345 meth
= DTLS_server_method();
1346 socket_type
= SOCK_DGRAM
;
1350 #ifndef OPENSSL_NO_DTLS
1351 meth
= DTLS_server_method();
1352 min_version
= DTLS1_VERSION
;
1353 max_version
= DTLS1_VERSION
;
1354 socket_type
= SOCK_DGRAM
;
1358 #ifndef OPENSSL_NO_DTLS
1359 meth
= DTLS_server_method();
1360 min_version
= DTLS1_2_VERSION
;
1361 max_version
= DTLS1_2_VERSION
;
1362 socket_type
= SOCK_DGRAM
;
1366 #ifndef OPENSSL_NO_DTLS
1367 enable_timeouts
= 1;
1371 #ifndef OPENSSL_NO_DTLS
1372 socket_mtu
= atol(opt_arg());
1376 #ifndef OPENSSL_NO_DTLS
1381 session_id_prefix
= opt_arg();
1384 engine
= setup_engine(opt_arg(), 1);
1389 case OPT_SERVERNAME
:
1390 tlsextcbp
.servername
= opt_arg();
1392 case OPT_SERVERNAME_FATAL
:
1393 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1396 s_cert_file2
= opt_arg();
1399 s_key_file2
= opt_arg();
1401 case OPT_NEXTPROTONEG
:
1402 # ifndef OPENSSL_NO_NEXTPROTONEG
1403 next_proto_neg_in
= opt_arg();
1407 alpn_in
= opt_arg();
1409 case OPT_SRTP_PROFILES
:
1410 #ifndef OPENSSL_NO_SRTP
1411 srtp_profiles
= opt_arg();
1414 case OPT_KEYMATEXPORT
:
1415 keymatexportlabel
= opt_arg();
1417 case OPT_KEYMATEXPORTLEN
:
1418 keymatexportlen
= atoi(opt_arg());
1423 case OPT_SPLIT_SEND_FRAG
:
1424 split_send_fragment
= atoi(opt_arg());
1425 if (split_send_fragment
== 0) {
1427 * Not allowed - set to a deliberately bad value so we get an
1428 * error message below
1430 split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
+ 1;
1433 case OPT_MAX_PIPELINES
:
1434 max_pipelines
= atoi(opt_arg());
1437 read_buf_len
= atoi(opt_arg());
1442 argc
= opt_num_rest();
1445 #ifndef OPENSSL_NO_DTLS
1446 if (www
&& socket_type
== SOCK_DGRAM
) {
1447 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1451 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1452 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1458 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1460 "Can't use unix sockets and datagrams together\n");
1465 if (split_send_fragment
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1466 BIO_printf(bio_err
, "Bad split send fragment size\n");
1470 if (max_pipelines
> SSL_MAX_PIPELINES
) {
1471 BIO_printf(bio_err
, "Bad max pipelines value\n");
1475 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1476 BIO_printf(bio_err
, "Error getting password\n");
1480 if (s_key_file
== NULL
)
1481 s_key_file
= s_cert_file
;
1483 if (s_key_file2
== NULL
)
1484 s_key_file2
= s_cert_file2
;
1486 if (!load_excert(&exc
))
1490 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1491 "server certificate private key file");
1493 ERR_print_errors(bio_err
);
1497 s_cert
= load_cert(s_cert_file
, s_cert_format
,
1498 "server certificate file");
1501 ERR_print_errors(bio_err
);
1505 if (!load_certs(s_chain_file
, &s_chain
, FORMAT_PEM
, NULL
,
1506 "server certificate chain"))
1510 if (tlsextcbp
.servername
) {
1511 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1512 "second server certificate private key file");
1514 ERR_print_errors(bio_err
);
1518 s_cert2
= load_cert(s_cert_file2
, s_cert_format
,
1519 "second server certificate file");
1522 ERR_print_errors(bio_err
);
1527 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1528 if (next_proto_neg_in
) {
1530 next_proto
.data
= next_protos_parse(&len
, next_proto_neg_in
);
1531 if (next_proto
.data
== NULL
)
1533 next_proto
.len
= len
;
1535 next_proto
.data
= NULL
;
1538 alpn_ctx
.data
= NULL
;
1541 alpn_ctx
.data
= next_protos_parse(&len
, alpn_in
);
1542 if (alpn_ctx
.data
== NULL
)
1549 crl
= load_crl(crl_file
, crl_format
);
1551 BIO_puts(bio_err
, "Error loading CRL\n");
1552 ERR_print_errors(bio_err
);
1555 crls
= sk_X509_CRL_new_null();
1556 if (!crls
|| !sk_X509_CRL_push(crls
, crl
)) {
1557 BIO_puts(bio_err
, "Error adding CRL\n");
1558 ERR_print_errors(bio_err
);
1566 if (s_dkey_file
== NULL
)
1567 s_dkey_file
= s_dcert_file
;
1569 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1570 0, dpass
, engine
, "second certificate private key file");
1572 ERR_print_errors(bio_err
);
1576 s_dcert
= load_cert(s_dcert_file
, s_dcert_format
,
1577 "second server certificate file");
1580 ERR_print_errors(bio_err
);
1583 if (s_dchain_file
) {
1584 if (!load_certs(s_dchain_file
, &s_dchain
, FORMAT_PEM
, NULL
,
1585 "second server certificate chain"))
1591 if (!app_RAND_load_file(NULL
, 1) && inrand
== NULL
1592 && !RAND_status()) {
1594 "warning, not much extra random data, consider using the -rand option\n");
1597 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
1598 app_RAND_load_files(inrand
));
1600 if (bio_s_out
== NULL
) {
1601 if (s_quiet
&& !s_debug
) {
1602 bio_s_out
= BIO_new(BIO_s_null());
1603 if (s_msg
&& !bio_s_msg
)
1604 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1606 if (bio_s_out
== NULL
)
1607 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1610 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
1616 s_dcert_file
= NULL
;
1618 s_cert_file2
= NULL
;
1622 ctx
= SSL_CTX_new(meth
);
1624 ERR_print_errors(bio_err
);
1628 ssl_ctx_security_debug(ctx
, sdebug
);
1630 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1631 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1633 ERR_print_errors(bio_err
);
1637 if (SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1639 if (SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1642 if (session_id_prefix
) {
1643 if (strlen(session_id_prefix
) >= 32)
1645 "warning: id_prefix is too long, only one new session will be possible\n");
1646 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1647 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1648 ERR_print_errors(bio_err
);
1651 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1653 SSL_CTX_set_quiet_shutdown(ctx
, 1);
1655 ssl_ctx_set_excert(ctx
, exc
);
1658 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1660 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1662 init_session_cache_ctx(ctx
);
1664 SSL_CTX_sess_set_cache_size(ctx
, 128);
1667 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1669 if (split_send_fragment
> 0) {
1670 SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
);
1672 if (max_pipelines
> 0) {
1673 SSL_CTX_set_max_pipelines(ctx
, max_pipelines
);
1676 if (read_buf_len
> 0) {
1677 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1679 #ifndef OPENSSL_NO_SRTP
1680 if (srtp_profiles
!= NULL
) {
1681 /* Returns 0 on success! */
1682 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1683 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1684 ERR_print_errors(bio_err
);
1690 if (!ctx_set_verify_locations(ctx
, CAfile
, CApath
, noCAfile
, noCApath
)) {
1691 ERR_print_errors(bio_err
);
1694 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1695 BIO_printf(bio_err
, "Error setting verify params\n");
1696 ERR_print_errors(bio_err
);
1700 ssl_ctx_add_crls(ctx
, crls
, 0);
1701 if (!config_ctx(cctx
, ssl_args
, ctx
))
1704 if (!ssl_load_stores(ctx
, vfyCApath
, vfyCAfile
, chCApath
, chCAfile
,
1705 crls
, crl_download
)) {
1706 BIO_printf(bio_err
, "Error loading store locations\n");
1707 ERR_print_errors(bio_err
);
1712 ctx2
= SSL_CTX_new(meth
);
1714 ERR_print_errors(bio_err
);
1720 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
1723 ssl_ctx_security_debug(ctx
, sdebug
);
1725 if (session_id_prefix
) {
1726 if (strlen(session_id_prefix
) >= 32)
1728 "warning: id_prefix is too long, only one new session will be possible\n");
1729 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
1730 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1731 ERR_print_errors(bio_err
);
1734 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1736 SSL_CTX_set_quiet_shutdown(ctx2
, 1);
1738 ssl_ctx_set_excert(ctx2
, exc
);
1741 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
1744 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
1746 init_session_cache_ctx(ctx2
);
1748 SSL_CTX_sess_set_cache_size(ctx2
, 128);
1751 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
1753 if (!ctx_set_verify_locations(ctx2
, CAfile
, CApath
, noCAfile
,
1755 ERR_print_errors(bio_err
);
1758 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
1759 BIO_printf(bio_err
, "Error setting verify params\n");
1760 ERR_print_errors(bio_err
);
1764 ssl_ctx_add_crls(ctx2
, crls
, 0);
1765 if (!config_ctx(cctx
, ssl_args
, ctx2
))
1768 #ifndef OPENSSL_NO_NEXTPROTONEG
1769 if (next_proto
.data
)
1770 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
1774 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
1776 #ifndef OPENSSL_NO_DH
1781 dh
= load_dh_param(dhfile
);
1782 else if (s_cert_file
)
1783 dh
= load_dh_param(s_cert_file
);
1786 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1788 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
1790 (void)BIO_flush(bio_s_out
);
1793 SSL_CTX_set_dh_auto(ctx
, 1);
1794 else if (!SSL_CTX_set_tmp_dh(ctx
, dh
)) {
1795 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1796 ERR_print_errors(bio_err
);
1803 DH
*dh2
= load_dh_param(s_cert_file2
);
1805 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
1806 (void)BIO_flush(bio_s_out
);
1813 SSL_CTX_set_dh_auto(ctx2
, 1);
1814 else if (!SSL_CTX_set_tmp_dh(ctx2
, dh
)) {
1815 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
1816 ERR_print_errors(bio_err
);
1825 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
1828 if (s_serverinfo_file
!= NULL
1829 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
1830 ERR_print_errors(bio_err
);
1834 if (ctx2
&& !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
1837 if (s_dcert
!= NULL
) {
1838 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
1842 if (no_resume_ephemeral
) {
1843 SSL_CTX_set_not_resumable_session_callback(ctx
,
1844 not_resumable_sess_cb
);
1847 SSL_CTX_set_not_resumable_session_callback(ctx2
,
1848 not_resumable_sess_cb
);
1850 #ifndef OPENSSL_NO_PSK
1851 if (psk_key
!= NULL
) {
1853 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
1854 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
1857 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
1858 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
1859 ERR_print_errors(bio_err
);
1864 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
1865 if (!SSL_CTX_set_session_id_context(ctx
,
1866 (void *)&s_server_session_id_context
,
1867 sizeof s_server_session_id_context
)) {
1868 BIO_printf(bio_err
, "error setting session id context\n");
1869 ERR_print_errors(bio_err
);
1873 /* Set DTLS cookie generation and verification callbacks */
1874 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
1875 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
1878 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
1879 if (!SSL_CTX_set_session_id_context(ctx2
,
1880 (void *)&s_server_session_id_context
,
1881 sizeof s_server_session_id_context
)) {
1882 BIO_printf(bio_err
, "error setting session id context\n");
1883 ERR_print_errors(bio_err
);
1886 tlsextcbp
.biodebug
= bio_s_out
;
1887 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
1888 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
1889 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
1890 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
1893 #ifndef OPENSSL_NO_SRP
1894 if (srp_verifier_file
!= NULL
) {
1895 srp_callback_parm
.vb
= SRP_VBASE_new(srpuserseed
);
1896 srp_callback_parm
.user
= NULL
;
1897 srp_callback_parm
.login
= NULL
;
1899 SRP_VBASE_init(srp_callback_parm
.vb
,
1900 srp_verifier_file
)) != SRP_NO_ERROR
) {
1902 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
1903 srp_verifier_file
, ret
);
1906 SSL_CTX_set_verify(ctx
, SSL_VERIFY_NONE
, verify_callback
);
1907 SSL_CTX_set_srp_cb_arg(ctx
, &srp_callback_parm
);
1908 SSL_CTX_set_srp_username_callback(ctx
, ssl_srp_server_param_cb
);
1911 if (CAfile
!= NULL
) {
1912 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
1915 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
1917 #ifndef OPENSSL_NO_OCSP
1918 if (s_tlsextstatus
) {
1919 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
1920 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
1922 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
1923 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
1928 BIO_printf(bio_s_out
, "ACCEPT\n");
1929 (void)BIO_flush(bio_s_out
);
1931 server_cb
= rev_body
;
1933 server_cb
= www_body
;
1935 server_cb
= sv_body
;
1937 if (socket_family
== AF_UNIX
1938 && unlink_unix_path
)
1941 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
,
1942 server_cb
, context
, naccept
);
1943 print_stats(bio_s_out
, ctx
);
1948 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
1950 EVP_PKEY_free(s_key
);
1951 EVP_PKEY_free(s_dkey
);
1952 sk_X509_pop_free(s_chain
, X509_free
);
1953 sk_X509_pop_free(s_dchain
, X509_free
);
1955 OPENSSL_free(dpass
);
1958 X509_VERIFY_PARAM_free(vpm
);
1960 OPENSSL_free(tlscstatp
.host
);
1961 OPENSSL_free(tlscstatp
.port
);
1962 OPENSSL_free(tlscstatp
.path
);
1965 EVP_PKEY_free(s_key2
);
1966 #ifndef OPENSSL_NO_NEXTPROTONEG
1967 OPENSSL_free(next_proto
.data
);
1969 OPENSSL_free(alpn_ctx
.data
);
1970 ssl_excert_free(exc
);
1971 sk_OPENSSL_STRING_free(ssl_args
);
1972 SSL_CONF_CTX_free(cctx
);
1973 release_engine(engine
);
1974 BIO_free(bio_s_out
);
1976 BIO_free(bio_s_msg
);
1978 #ifdef CHARSET_EBCDIC
1979 BIO_meth_free(methods_ebcdic
);
1984 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
1986 BIO_printf(bio
, "%4ld items in the session cache\n",
1987 SSL_CTX_sess_number(ssl_ctx
));
1988 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
1989 SSL_CTX_sess_connect(ssl_ctx
));
1990 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
1991 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
1992 BIO_printf(bio
, "%4ld client connects that finished\n",
1993 SSL_CTX_sess_connect_good(ssl_ctx
));
1994 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
1995 SSL_CTX_sess_accept(ssl_ctx
));
1996 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
1997 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
1998 BIO_printf(bio
, "%4ld server accepts that finished\n",
1999 SSL_CTX_sess_accept_good(ssl_ctx
));
2000 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2001 BIO_printf(bio
, "%4ld session cache misses\n",
2002 SSL_CTX_sess_misses(ssl_ctx
));
2003 BIO_printf(bio
, "%4ld session cache timeouts\n",
2004 SSL_CTX_sess_timeouts(ssl_ctx
));
2005 BIO_printf(bio
, "%4ld callback cache hits\n",
2006 SSL_CTX_sess_cb_hits(ssl_ctx
));
2007 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2008 SSL_CTX_sess_cache_full(ssl_ctx
),
2009 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2012 static int sv_body(int s
, int stype
, unsigned char *context
)
2021 struct timeval timeout
;
2022 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2025 struct timeval
*timeoutp
;
2028 buf
= app_malloc(bufsize
, "server buffer");
2030 if (!BIO_socket_nbio(s
, 1))
2031 ERR_print_errors(bio_err
);
2033 BIO_printf(bio_err
, "Turned on non blocking io\n");
2039 if (s_tlsextdebug
) {
2040 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2041 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2045 && !SSL_set_session_id_context(con
,
2046 context
, strlen((char *)context
))) {
2047 BIO_printf(bio_err
, "Error setting session id context\n");
2052 if (!SSL_clear(con
)) {
2053 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2057 #ifndef OPENSSL_NO_DTLS
2058 if (stype
== SOCK_DGRAM
) {
2060 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2062 if (enable_timeouts
) {
2064 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2065 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2068 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2069 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2073 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2074 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2075 DTLS_get_link_min_mtu(con
));
2080 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2081 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2082 BIO_printf(bio_err
, "Failed to set MTU\n");
2088 /* want to do MTU discovery */
2089 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2091 /* turn on cookie exchange */
2092 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2095 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2100 test
= BIO_new(BIO_f_nbio_test());
2101 sbio
= BIO_push(test
, sbio
);
2104 SSL_set_bio(con
, sbio
, sbio
);
2105 SSL_set_accept_state(con
);
2106 /* SSL_set_fd(con,s); */
2109 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2110 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2113 #ifndef OPENSSL_NO_SSL_TRACE
2115 SSL_set_msg_callback(con
, SSL_trace
);
2118 SSL_set_msg_callback(con
, msg_cb
);
2119 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2122 if (s_tlsextdebug
) {
2123 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2124 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2127 if (fileno_stdin() > s
)
2128 width
= fileno_stdin() + 1;
2132 int read_from_terminal
;
2133 int read_from_sslcon
;
2135 read_from_terminal
= 0;
2136 read_from_sslcon
= SSL_has_pending(con
)
2137 || (async
&& SSL_waiting_for_async(con
));
2139 if (!read_from_sslcon
) {
2141 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2142 openssl_fdset(fileno_stdin(), &readfds
);
2144 openssl_fdset(s
, &readfds
);
2146 * Note: under VMS with SOCKETSHR the second parameter is
2147 * currently of type (int *) whereas under other systems it is
2148 * (void *) if you don't have a cast it will choke the compiler:
2149 * if you do have a cast then you can either go for (int *) or
2152 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2154 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2155 * only on sockets. As a workaround we timeout the select every
2156 * second and check for any keypress. In a proper Windows
2157 * application we wouldn't do this because it is inefficient.
2161 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &tv
);
2162 if (has_stdin_waiting())
2163 read_from_terminal
= 1;
2164 if ((i
< 0) || (!i
&& !read_from_terminal
))
2167 if ((SSL_version(con
) == DTLS1_VERSION
) &&
2168 DTLSv1_get_timeout(con
, &timeout
))
2169 timeoutp
= &timeout
;
2173 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2175 if ((SSL_version(con
) == DTLS1_VERSION
)
2176 && DTLSv1_handle_timeout(con
) > 0) {
2177 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2182 if (FD_ISSET(fileno_stdin(), &readfds
))
2183 read_from_terminal
= 1;
2185 if (FD_ISSET(s
, &readfds
))
2186 read_from_sslcon
= 1;
2188 if (read_from_terminal
) {
2192 i
= raw_read_stdin(buf
, bufsize
/ 2);
2194 /* both loops are skipped when i <= 0 */
2195 for (j
= 0; j
< i
; j
++)
2198 for (j
= i
- 1; j
>= 0; j
--) {
2199 buf
[j
+ lf_num
] = buf
[j
];
2200 if (buf
[j
] == '\n') {
2203 buf
[j
+ lf_num
] = '\r';
2206 assert(lf_num
== 0);
2208 i
= raw_read_stdin(buf
, bufsize
);
2210 if (!s_quiet
&& !s_brief
) {
2211 if ((i
<= 0) || (buf
[0] == 'Q')) {
2212 BIO_printf(bio_s_out
, "DONE\n");
2213 (void)BIO_flush(bio_s_out
);
2215 close_accept_socket();
2219 if ((i
<= 0) || (buf
[0] == 'q')) {
2220 BIO_printf(bio_s_out
, "DONE\n");
2221 (void)BIO_flush(bio_s_out
);
2222 if (SSL_version(con
) != DTLS1_VERSION
)
2225 * close_accept_socket(); ret= -11;
2229 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2230 SSL_renegotiate(con
);
2231 i
= SSL_do_handshake(con
);
2232 printf("SSL_do_handshake -> %d\n", i
);
2236 * strcpy(buf,"server side RE-NEGOTIATE\n");
2239 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2241 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2243 SSL_renegotiate(con
);
2244 i
= SSL_do_handshake(con
);
2245 printf("SSL_do_handshake -> %d\n", i
);
2249 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2253 if (buf
[0] == 'P') {
2254 static const char *str
= "Lets print some clear text\n";
2255 BIO_write(SSL_get_wbio(con
), str
, strlen(str
));
2257 if (buf
[0] == 'S') {
2258 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2261 #ifdef CHARSET_EBCDIC
2262 ebcdic2ascii(buf
, buf
, i
);
2266 /* should do a select for the write */
2269 if (++count
== 100) {
2271 SSL_renegotiate(con
);
2274 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2275 #ifndef OPENSSL_NO_SRP
2276 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2277 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2278 SRP_user_pwd_free(srp_callback_parm
.user
);
2279 srp_callback_parm
.user
=
2280 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2281 srp_callback_parm
.login
);
2282 if (srp_callback_parm
.user
)
2283 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2284 srp_callback_parm
.user
->info
);
2286 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2287 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2290 switch (SSL_get_error(con
, k
)) {
2291 case SSL_ERROR_NONE
:
2293 case SSL_ERROR_WANT_ASYNC
:
2294 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2295 (void)BIO_flush(bio_s_out
);
2296 wait_for_async(con
);
2298 case SSL_ERROR_WANT_WRITE
:
2299 case SSL_ERROR_WANT_READ
:
2300 case SSL_ERROR_WANT_X509_LOOKUP
:
2301 BIO_printf(bio_s_out
, "Write BLOCK\n");
2302 (void)BIO_flush(bio_s_out
);
2304 case SSL_ERROR_WANT_ASYNC_JOB
:
2306 * This shouldn't ever happen in s_server. Treat as an error
2308 case SSL_ERROR_SYSCALL
:
2310 BIO_printf(bio_s_out
, "ERROR\n");
2311 (void)BIO_flush(bio_s_out
);
2312 ERR_print_errors(bio_err
);
2316 case SSL_ERROR_ZERO_RETURN
:
2317 BIO_printf(bio_s_out
, "DONE\n");
2318 (void)BIO_flush(bio_s_out
);
2330 if (read_from_sslcon
) {
2332 * init_ssl_connection handles all async events itself so if we're
2333 * waiting for async then we shouldn't go back into
2334 * init_ssl_connection
2336 if ((!async
|| !SSL_waiting_for_async(con
))
2337 && !SSL_is_init_finished(con
)) {
2338 i
= init_ssl_connection(con
);
2343 } else if (i
== 0) {
2349 i
= SSL_read(con
, (char *)buf
, bufsize
);
2350 #ifndef OPENSSL_NO_SRP
2351 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2352 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2353 SRP_user_pwd_free(srp_callback_parm
.user
);
2354 srp_callback_parm
.user
=
2355 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2356 srp_callback_parm
.login
);
2357 if (srp_callback_parm
.user
)
2358 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2359 srp_callback_parm
.user
->info
);
2361 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2362 i
= SSL_read(con
, (char *)buf
, bufsize
);
2365 switch (SSL_get_error(con
, i
)) {
2366 case SSL_ERROR_NONE
:
2367 #ifdef CHARSET_EBCDIC
2368 ascii2ebcdic(buf
, buf
, i
);
2370 raw_write_stdout(buf
, (unsigned int)i
);
2371 (void)BIO_flush(bio_s_out
);
2372 if (SSL_has_pending(con
))
2375 case SSL_ERROR_WANT_ASYNC
:
2376 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2377 (void)BIO_flush(bio_s_out
);
2378 wait_for_async(con
);
2380 case SSL_ERROR_WANT_WRITE
:
2381 case SSL_ERROR_WANT_READ
:
2382 BIO_printf(bio_s_out
, "Read BLOCK\n");
2383 (void)BIO_flush(bio_s_out
);
2385 case SSL_ERROR_WANT_ASYNC_JOB
:
2387 * This shouldn't ever happen in s_server. Treat as an error
2389 case SSL_ERROR_SYSCALL
:
2391 BIO_printf(bio_s_out
, "ERROR\n");
2392 (void)BIO_flush(bio_s_out
);
2393 ERR_print_errors(bio_err
);
2396 case SSL_ERROR_ZERO_RETURN
:
2397 BIO_printf(bio_s_out
, "DONE\n");
2398 (void)BIO_flush(bio_s_out
);
2407 BIO_printf(bio_s_out
, "shutting down SSL\n");
2408 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
2411 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2412 OPENSSL_clear_free(buf
, bufsize
);
2414 BIO_printf(bio_s_out
, "ACCEPT\n");
2415 (void)BIO_flush(bio_s_out
);
2419 static void close_accept_socket(void)
2421 BIO_printf(bio_err
, "shutdown accept socket\n");
2422 if (accept_socket
>= 0) {
2423 BIO_closesocket(accept_socket
);
2427 static int init_ssl_connection(SSL
*con
)
2434 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2435 const unsigned char *next_proto_neg
;
2436 unsigned next_proto_neg_len
;
2438 unsigned char *exportedkeymat
;
2441 #ifndef OPENSSL_NO_DTLS
2443 BIO_ADDR
*client
= NULL
;
2445 if ((client
= BIO_ADDR_new()) == NULL
) {
2446 BIO_printf(bio_err
, "ERROR - memory\n");
2449 i
= DTLSv1_listen(con
, client
);
2454 wbio
= SSL_get_wbio(con
);
2456 BIO_get_fd(wbio
, &fd
);
2459 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2460 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2461 BIO_ADDR_free(client
);
2464 BIO_ADDR_free(client
);
2466 i
= SSL_accept(con
);
2468 BIO_ADDR_free(client
);
2474 i
= SSL_accept(con
);
2477 retry
= BIO_sock_should_retry(i
);
2478 #ifdef CERT_CB_TEST_RETRY
2481 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2482 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2484 "LOOKUP from certificate callback during accept\n");
2485 i
= SSL_accept(con
);
2487 retry
= BIO_sock_should_retry(i
);
2492 #ifndef OPENSSL_NO_SRP
2494 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2495 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
2496 srp_callback_parm
.login
);
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_accept(con
);
2508 retry
= BIO_sock_should_retry(i
);
2511 } while (i
< 0 && SSL_waiting_for_async(con
));
2514 if ((dtlslisten
&& i
== 0)
2515 || (!dtlslisten
&& retry
)) {
2516 BIO_printf(bio_s_out
, "DELAY\n");
2520 BIO_printf(bio_err
, "ERROR\n");
2522 verify_err
= SSL_get_verify_result(con
);
2523 if (verify_err
!= X509_V_OK
) {
2524 BIO_printf(bio_err
, "verify error:%s\n",
2525 X509_verify_cert_error_string(verify_err
));
2527 /* Always print any error messages */
2528 ERR_print_errors(bio_err
);
2533 print_ssl_summary(con
);
2535 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
2537 peer
= SSL_get_peer_certificate(con
);
2539 BIO_printf(bio_s_out
, "Client certificate\n");
2540 PEM_write_bio_X509(bio_s_out
, peer
);
2541 X509_NAME_oneline(X509_get_subject_name(peer
), buf
, sizeof buf
);
2542 BIO_printf(bio_s_out
, "subject=%s\n", buf
);
2543 X509_NAME_oneline(X509_get_issuer_name(peer
), buf
, sizeof buf
);
2544 BIO_printf(bio_s_out
, "issuer=%s\n", buf
);
2549 if (SSL_get_shared_ciphers(con
, buf
, sizeof buf
) != NULL
)
2550 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
2551 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
2552 ssl_print_sigalgs(bio_s_out
, con
);
2553 #ifndef OPENSSL_NO_EC
2554 ssl_print_point_formats(bio_s_out
, con
);
2555 ssl_print_groups(bio_s_out
, con
, 0);
2557 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
2559 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2560 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
2561 if (next_proto_neg
) {
2562 BIO_printf(bio_s_out
, "NEXTPROTO is ");
2563 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
2564 BIO_printf(bio_s_out
, "\n");
2567 #ifndef OPENSSL_NO_SRTP
2569 SRTP_PROTECTION_PROFILE
*srtp_profile
2570 = SSL_get_selected_srtp_profile(con
);
2573 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
2574 srtp_profile
->name
);
2577 if (SSL_session_reused(con
))
2578 BIO_printf(bio_s_out
, "Reused session-id\n");
2579 BIO_printf(bio_s_out
, "Secure Renegotiation IS%s supported\n",
2580 SSL_get_secure_renegotiation_support(con
) ? "" : " NOT");
2581 if (keymatexportlabel
!= NULL
) {
2582 BIO_printf(bio_s_out
, "Keying material exporter:\n");
2583 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
2584 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
2585 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
2586 if (!SSL_export_keying_material(con
, exportedkeymat
,
2589 strlen(keymatexportlabel
),
2591 BIO_printf(bio_s_out
, " Error\n");
2593 BIO_printf(bio_s_out
, " Keying material: ");
2594 for (i
= 0; i
< keymatexportlen
; i
++)
2595 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
2596 BIO_printf(bio_s_out
, "\n");
2598 OPENSSL_free(exportedkeymat
);
2601 (void)BIO_flush(bio_s_out
);
2605 #ifndef OPENSSL_NO_DH
2606 static DH
*load_dh_param(const char *dhfile
)
2611 if ((bio
= BIO_new_file(dhfile
, "r")) == NULL
)
2613 ret
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2620 static int www_body(int s
, int stype
, unsigned char *context
)
2626 const SSL_CIPHER
*c
;
2627 BIO
*io
, *ssl_bio
, *sbio
;
2629 int total_bytes
= 0;
2634 /* Set width for a select call if needed */
2637 buf
= app_malloc(bufsize
, "server www buffer");
2638 io
= BIO_new(BIO_f_buffer());
2639 ssl_bio
= BIO_new(BIO_f_ssl());
2640 if ((io
== NULL
) || (ssl_bio
== NULL
))
2644 if (!BIO_socket_nbio(s
, 1))
2645 ERR_print_errors(bio_err
);
2647 BIO_printf(bio_err
, "Turned on non blocking io\n");
2650 /* lets make the output buffer a reasonable size */
2651 if (!BIO_set_write_buffer_size(io
, bufsize
))
2654 if ((con
= SSL_new(ctx
)) == NULL
)
2657 if (s_tlsextdebug
) {
2658 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2659 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2663 && !SSL_set_session_id_context(con
, context
,
2664 strlen((char *)context
)))
2667 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2671 test
= BIO_new(BIO_f_nbio_test());
2672 sbio
= BIO_push(test
, sbio
);
2674 SSL_set_bio(con
, sbio
, sbio
);
2675 SSL_set_accept_state(con
);
2677 /* SSL_set_fd(con,s); */
2678 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
2679 BIO_push(io
, ssl_bio
);
2680 #ifdef CHARSET_EBCDIC
2681 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
2685 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
2686 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
2689 #ifndef OPENSSL_NO_SSL_TRACE
2691 SSL_set_msg_callback(con
, SSL_trace
);
2694 SSL_set_msg_callback(con
, msg_cb
);
2695 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2699 i
= BIO_gets(io
, buf
, bufsize
- 1);
2700 if (i
< 0) { /* error */
2701 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
2703 ERR_print_errors(bio_err
);
2706 BIO_printf(bio_s_out
, "read R BLOCK\n");
2707 #ifndef OPENSSL_NO_SRP
2708 if (BIO_should_io_special(io
)
2709 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
2710 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2711 SRP_user_pwd_free(srp_callback_parm
.user
);
2712 srp_callback_parm
.user
=
2713 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
2714 srp_callback_parm
.login
);
2715 if (srp_callback_parm
.user
)
2716 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
2717 srp_callback_parm
.user
->info
);
2719 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
2723 #if !defined(OPENSSL_SYS_MSDOS)
2728 } else if (i
== 0) { /* end of input */
2733 /* else we have data */
2734 if (((www
== 1) && (strncmp("GET ", buf
, 4) == 0)) ||
2735 ((www
== 2) && (strncmp("GET /stats ", buf
, 11) == 0))) {
2738 STACK_OF(SSL_CIPHER
) *sk
;
2739 static const char *space
= " ";
2741 if (www
== 1 && strncmp("GET /reneg", buf
, 10) == 0) {
2742 if (strncmp("GET /renegcert", buf
, 14) == 0)
2744 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2746 i
= SSL_renegotiate(con
);
2747 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
2748 /* Send the HelloRequest */
2749 i
= SSL_do_handshake(con
);
2751 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
2752 SSL_get_error(con
, i
));
2753 ERR_print_errors(bio_err
);
2756 /* Wait for a ClientHello to come back */
2758 openssl_fdset(s
, &readfds
);
2759 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
2760 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
2761 BIO_printf(bio_s_out
,
2762 "Error waiting for client response\n");
2763 ERR_print_errors(bio_err
);
2767 * We're not actually expecting any data here and we ignore
2768 * any that is sent. This is just to force the handshake that
2769 * we're expecting to come from the client. If they haven't
2770 * sent one there's not much we can do.
2772 BIO_gets(io
, buf
, bufsize
- 1);
2776 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2777 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2778 BIO_puts(io
, "<pre>\n");
2779 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
2781 for (i
= 0; i
< local_argc
; i
++) {
2783 for (myp
= local_argv
[i
]; *myp
; myp
++)
2786 BIO_puts(io
, "<");
2789 BIO_puts(io
, ">");
2792 BIO_puts(io
, "&");
2795 BIO_write(io
, myp
, 1);
2798 BIO_write(io
, " ", 1);
2803 "Secure Renegotiation IS%s supported\n",
2804 SSL_get_secure_renegotiation_support(con
) ?
2808 * The following is evil and should not really be done
2810 BIO_printf(io
, "Ciphers supported in s_server binary\n");
2811 sk
= SSL_get_ciphers(con
);
2812 j
= sk_SSL_CIPHER_num(sk
);
2813 for (i
= 0; i
< j
; i
++) {
2814 c
= sk_SSL_CIPHER_value(sk
, i
);
2815 BIO_printf(io
, "%-11s:%-25s ",
2816 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
2817 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
2821 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
2824 "---\nCiphers common between both SSL end points:\n");
2828 BIO_write(io
, space
, 26 - j
);
2831 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
2833 BIO_write(io
, p
, 1);
2840 ssl_print_sigalgs(io
, con
);
2841 #ifndef OPENSSL_NO_EC
2842 ssl_print_groups(io
, con
, 0);
2844 BIO_printf(io
, (SSL_session_reused(con
)
2845 ? "---\nReused, " : "---\nNew, "));
2846 c
= SSL_get_current_cipher(con
);
2847 BIO_printf(io
, "%s, Cipher is %s\n",
2848 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
2849 SSL_SESSION_print(io
, SSL_get_session(con
));
2850 BIO_printf(io
, "---\n");
2851 print_stats(io
, SSL_get_SSL_CTX(con
));
2852 BIO_printf(io
, "---\n");
2853 peer
= SSL_get_peer_certificate(con
);
2855 BIO_printf(io
, "Client certificate\n");
2856 X509_print(io
, peer
);
2857 PEM_write_bio_X509(io
, peer
);
2861 BIO_puts(io
, "no client certificate available\n");
2862 BIO_puts(io
, "</BODY></HTML>\r\n\r\n");
2864 } else if ((www
== 2 || www
== 3)
2865 && (strncmp("GET /", buf
, 5) == 0)) {
2868 static const char *text
=
2869 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
2875 for (e
= p
; *e
!= '\0'; e
++) {
2881 dot
= (e
[0] == '.') ? 2 : 0;
2884 dot
= (e
[0] == '.') ? 3 : 0;
2887 dot
= (e
[0] == '/') ? -1 : 0;
2891 dot
= (e
[0] == '/') ? 1 : 0;
2893 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
2898 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
2905 BIO_printf(io
, "'%s' contains '..' reference\r\n", p
);
2911 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
2915 /* if a directory, do the index thang */
2916 if (app_isdir(p
) > 0) {
2918 BIO_printf(io
, "'%s' is a directory\r\n", p
);
2922 if ((file
= BIO_new_file(p
, "r")) == NULL
) {
2924 BIO_printf(io
, "Error opening '%s'\r\n", p
);
2925 ERR_print_errors(io
);
2930 BIO_printf(bio_err
, "FILE:%s\n", p
);
2934 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
2935 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
2936 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
2938 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2941 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
2945 i
= BIO_read(file
, buf
, bufsize
);
2951 BIO_printf(bio_err
, "%d\n", i
);
2952 if (total_bytes
> 3 * 1024) {
2954 BIO_printf(bio_err
, "RENEGOTIATE\n");
2955 SSL_renegotiate(con
);
2959 for (j
= 0; j
< i
;) {
2962 if (++count
== 13) {
2963 SSL_renegotiate(con
);
2966 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
2968 if (!BIO_should_retry(io
)
2969 && !SSL_waiting_for_async(con
))
2972 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
2986 i
= (int)BIO_flush(io
);
2988 if (!BIO_should_retry(io
))
2994 /* make sure we re-use sessions */
2995 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
2999 BIO_printf(bio_s_out
, "ACCEPT\n");
3005 static int rev_body(int s
, int stype
, unsigned char *context
)
3011 BIO
*io
, *ssl_bio
, *sbio
;
3013 buf
= app_malloc(bufsize
, "server rev buffer");
3014 io
= BIO_new(BIO_f_buffer());
3015 ssl_bio
= BIO_new(BIO_f_ssl());
3016 if ((io
== NULL
) || (ssl_bio
== NULL
))
3019 /* lets make the output buffer a reasonable size */
3020 if (!BIO_set_write_buffer_size(io
, bufsize
))
3023 if ((con
= SSL_new(ctx
)) == NULL
)
3026 if (s_tlsextdebug
) {
3027 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3028 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3031 && !SSL_set_session_id_context(con
, context
,
3032 strlen((char *)context
))) {
3033 ERR_print_errors(bio_err
);
3037 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3038 SSL_set_bio(con
, sbio
, sbio
);
3039 SSL_set_accept_state(con
);
3041 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3042 BIO_push(io
, ssl_bio
);
3043 #ifdef CHARSET_EBCDIC
3044 io
= BIO_push(BIO_new(BIO_f_ebcdic_filter()), io
);
3048 BIO_set_callback(SSL_get_rbio(con
), bio_dump_callback
);
3049 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3052 #ifndef OPENSSL_NO_SSL_TRACE
3054 SSL_set_msg_callback(con
, SSL_trace
);
3057 SSL_set_msg_callback(con
, msg_cb
);
3058 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3062 i
= BIO_do_handshake(io
);
3065 if (!BIO_should_retry(io
)) {
3066 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3067 ERR_print_errors(bio_err
);
3070 #ifndef OPENSSL_NO_SRP
3071 if (BIO_should_io_special(io
)
3072 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3073 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3074 SRP_user_pwd_free(srp_callback_parm
.user
);
3075 srp_callback_parm
.user
=
3076 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3077 srp_callback_parm
.login
);
3078 if (srp_callback_parm
.user
)
3079 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3080 srp_callback_parm
.user
->info
);
3082 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3087 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3088 print_ssl_summary(con
);
3091 i
= BIO_gets(io
, buf
, bufsize
- 1);
3092 if (i
< 0) { /* error */
3093 if (!BIO_should_retry(io
)) {
3095 ERR_print_errors(bio_err
);
3098 BIO_printf(bio_s_out
, "read R BLOCK\n");
3099 #ifndef OPENSSL_NO_SRP
3100 if (BIO_should_io_special(io
)
3101 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3102 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3103 SRP_user_pwd_free(srp_callback_parm
.user
);
3104 srp_callback_parm
.user
=
3105 SRP_VBASE_get1_by_user(srp_callback_parm
.vb
,
3106 srp_callback_parm
.login
);
3107 if (srp_callback_parm
.user
)
3108 BIO_printf(bio_s_out
, "LOOKUP done %s\n",
3109 srp_callback_parm
.user
->info
);
3111 BIO_printf(bio_s_out
, "LOOKUP not successful\n");
3115 #if !defined(OPENSSL_SYS_MSDOS)
3120 } else if (i
== 0) { /* end of input */
3122 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3125 char *p
= buf
+ i
- 1;
3126 while (i
&& (*p
== '\n' || *p
== '\r')) {
3130 if (!s_ign_eof
&& (i
== 5) && (strncmp(buf
, "CLOSE", 5) == 0)) {
3132 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3135 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3137 BIO_write(io
, buf
, i
+ 1);
3142 if (!BIO_should_retry(io
))
3148 /* make sure we re-use sessions */
3149 SSL_set_shutdown(con
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
3158 #define MAX_SESSION_ID_ATTEMPTS 10
3159 static int generate_session_id(const SSL
*ssl
, unsigned char *id
,
3160 unsigned int *id_len
)
3162 unsigned int count
= 0;
3164 if (RAND_bytes(id
, *id_len
) <= 0)
3167 * Prefix the session_id with the required prefix. NB: If our prefix
3168 * is too long, clip it - but there will be worse effects anyway, eg.
3169 * the server could only possibly create 1 session ID (ie. the
3170 * prefix!) so all future session negotiations will fail due to
3173 memcpy(id
, session_id_prefix
,
3174 (strlen(session_id_prefix
) < *id_len
) ?
3175 strlen(session_id_prefix
) : *id_len
);
3177 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3178 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3179 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3185 * By default s_server uses an in-memory cache which caches SSL_SESSION
3186 * structures without any serialisation. This hides some bugs which only
3187 * become apparent in deployed servers. By implementing a basic external
3188 * session cache some issues can be debugged using s_server.
3191 typedef struct simple_ssl_session_st
{
3196 struct simple_ssl_session_st
*next
;
3197 } simple_ssl_session
;
3199 static simple_ssl_session
*first
= NULL
;
3201 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3203 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3206 SSL_SESSION_get_id(session
, &sess
->idlen
);
3207 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3208 if (sess
->derlen
< 0) {
3209 BIO_printf(bio_err
, "Error encoding session\n");
3214 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3215 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3217 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3218 OPENSSL_free(sess
->id
);
3219 OPENSSL_free(sess
->der
);
3225 /* Assume it still works. */
3226 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3227 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3228 OPENSSL_free(sess
->id
);
3229 OPENSSL_free(sess
->der
);
3236 BIO_printf(bio_err
, "New session added to external cache\n");
3240 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3243 simple_ssl_session
*sess
;
3245 for (sess
= first
; sess
; sess
= sess
->next
) {
3246 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3247 const unsigned char *p
= sess
->der
;
3248 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3249 return d2i_SSL_SESSION(NULL
, &p
, sess
->derlen
);
3252 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3256 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3258 simple_ssl_session
*sess
, *prev
= NULL
;
3259 const unsigned char *id
;
3261 id
= SSL_SESSION_get_id(session
, &idlen
);
3262 for (sess
= first
; sess
; sess
= sess
->next
) {
3263 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3265 prev
->next
= sess
->next
;
3268 OPENSSL_free(sess
->id
);
3269 OPENSSL_free(sess
->der
);
3277 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3279 SSL_CTX_set_session_cache_mode(sctx
,
3280 SSL_SESS_CACHE_NO_INTERNAL
|
3281 SSL_SESS_CACHE_SERVER
);
3282 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3283 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3284 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3287 static void free_sessions(void)
3289 simple_ssl_session
*sess
, *tsess
;
3290 for (sess
= first
; sess
;) {
3291 OPENSSL_free(sess
->id
);
3292 OPENSSL_free(sess
->der
);
3295 OPENSSL_free(tsess
);
3300 #endif /* OPENSSL_NO_SOCK */