2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
17 /* Included before async.h to avoid some warnings */
21 #include <openssl/e_os2.h>
22 #include <openssl/async.h>
23 #include <openssl/ssl.h>
24 #include <openssl/decoder.h>
26 #ifndef OPENSSL_NO_SOCK
29 * With IPv6, it looks like Digital has mixed up the proper order of
30 * recursive header file inclusion, resulting in the compiler complaining
31 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
32 * needed to have fileno() declared correctly... So let's define u_int
34 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
36 typedef unsigned int u_int
;
39 #include <openssl/bn.h>
42 #include <openssl/err.h>
43 #include <openssl/pem.h>
44 #include <openssl/x509.h>
45 #include <openssl/rand.h>
46 #include <openssl/ocsp.h>
48 # include <openssl/dh.h>
50 #include <openssl/rsa.h>
54 #include <openssl/ebcdic.h>
56 #include "internal/sockets.h"
58 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
);
59 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
);
60 static int www_body(int s
, int stype
, int prot
, unsigned char *context
);
61 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
);
62 static void close_accept_socket(void);
63 static int init_ssl_connection(SSL
*s
);
64 static void print_stats(BIO
*bp
, SSL_CTX
*ctx
);
65 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
66 unsigned int *id_len
);
67 static void init_session_cache_ctx(SSL_CTX
*sctx
);
68 static void free_sessions(void);
69 static void print_connection_info(SSL
*con
);
71 static const int bufsize
= 16 * 1024;
72 static int accept_socket
= -1;
74 #define TEST_CERT "server.pem"
75 #define TEST_CERT2 "server2.pem"
77 static int s_nbio
= 0;
78 static int s_nbio_test
= 0;
79 static int s_crlf
= 0;
80 static SSL_CTX
*ctx
= NULL
;
81 static SSL_CTX
*ctx2
= NULL
;
84 static BIO
*bio_s_out
= NULL
;
85 static BIO
*bio_s_msg
= NULL
;
86 static int s_debug
= 0;
87 static int s_tlsextdebug
= 0;
89 static int s_quiet
= 0;
90 static int s_ign_eof
= 0;
91 static int s_brief
= 0;
93 static char *keymatexportlabel
= NULL
;
94 static int keymatexportlen
= 20;
98 static int use_sendfile
= 0;
100 static const char *session_id_prefix
= NULL
;
102 #ifndef OPENSSL_NO_DTLS
103 static int enable_timeouts
= 0;
104 static long socket_mtu
;
108 * We define this but make it always be 0 in no-dtls builds to simplify the
111 static int dtlslisten
= 0;
112 static int stateless
= 0;
114 static int early_data
= 0;
115 static SSL_SESSION
*psksess
= NULL
;
117 static char *psk_identity
= "Client_identity";
118 char *psk_key
= NULL
; /* by default PSK is not used */
120 static char http_server_binmode
= 0; /* for now: 0/1 = default/binary */
122 #ifndef OPENSSL_NO_PSK
123 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
125 unsigned int max_psk_len
)
131 BIO_printf(bio_s_out
, "psk_server_cb\n");
133 if (!SSL_is_dtls(ssl
) && SSL_version(ssl
) >= TLS1_3_VERSION
) {
135 * This callback is designed for use in (D)TLSv1.2 (or below). It is
136 * possible to use a single callback for all protocol versions - but it
137 * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we
138 * have psk_find_session_cb.
143 if (identity
== NULL
) {
144 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
148 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
149 (int)strlen(identity
), identity
);
151 /* here we could lookup the given identity e.g. from a database */
152 if (strcmp(identity
, psk_identity
) != 0) {
153 BIO_printf(bio_s_out
, "PSK warning: client identity not what we expected"
154 " (got '%s' expected '%s')\n", identity
, psk_identity
);
157 BIO_printf(bio_s_out
, "PSK client identity found\n");
160 /* convert the PSK key to binary */
161 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
163 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
167 if (key_len
> (int)max_psk_len
) {
169 "psk buffer of callback is too small (%d) for key (%ld)\n",
170 max_psk_len
, key_len
);
175 memcpy(psk
, key
, key_len
);
179 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
183 BIO_printf(bio_err
, "Error in PSK server callback\n");
184 (void)BIO_flush(bio_err
);
185 (void)BIO_flush(bio_s_out
);
190 static int psk_find_session_cb(SSL
*ssl
, const unsigned char *identity
,
191 size_t identity_len
, SSL_SESSION
**sess
)
193 SSL_SESSION
*tmpsess
= NULL
;
196 const SSL_CIPHER
*cipher
= NULL
;
198 if (strlen(psk_identity
) != identity_len
199 || memcmp(psk_identity
, identity
, identity_len
) != 0) {
204 if (psksess
!= NULL
) {
205 SSL_SESSION_up_ref(psksess
);
210 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
212 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
217 /* We default to SHA256 */
218 cipher
= SSL_CIPHER_find(ssl
, tls13_aes128gcmsha256_id
);
219 if (cipher
== NULL
) {
220 BIO_printf(bio_err
, "Error finding suitable ciphersuite\n");
225 tmpsess
= SSL_SESSION_new();
227 || !SSL_SESSION_set1_master_key(tmpsess
, key
, key_len
)
228 || !SSL_SESSION_set_cipher(tmpsess
, cipher
)
229 || !SSL_SESSION_set_protocol_version(tmpsess
, SSL_version(ssl
))) {
239 #ifndef OPENSSL_NO_SRP
240 static srpsrvparm srp_callback_parm
;
243 static int local_argc
= 0;
244 static char **local_argv
;
246 #ifdef CHARSET_EBCDIC
247 static int ebcdic_new(BIO
*bi
);
248 static int ebcdic_free(BIO
*a
);
249 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
250 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
251 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
252 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
253 static int ebcdic_puts(BIO
*bp
, const char *str
);
255 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
256 static BIO_METHOD
*methods_ebcdic
= NULL
;
258 /* This struct is "unwarranted chumminess with the compiler." */
264 static const BIO_METHOD
*BIO_f_ebcdic_filter()
266 if (methods_ebcdic
== NULL
) {
267 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
268 "EBCDIC/ASCII filter");
269 if (methods_ebcdic
== NULL
270 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
271 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
272 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
273 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
274 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
275 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
276 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
279 return methods_ebcdic
;
282 static int ebcdic_new(BIO
*bi
)
284 EBCDIC_OUTBUFF
*wbuf
;
286 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
287 wbuf
->alloced
= 1024;
288 wbuf
->buff
[0] = '\0';
290 BIO_set_data(bi
, wbuf
);
295 static int ebcdic_free(BIO
*a
)
297 EBCDIC_OUTBUFF
*wbuf
;
301 wbuf
= BIO_get_data(a
);
303 BIO_set_data(a
, NULL
);
309 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
312 BIO
*next
= BIO_next(b
);
314 if (out
== NULL
|| outl
== 0)
319 ret
= BIO_read(next
, out
, outl
);
321 ascii2ebcdic(out
, out
, ret
);
325 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
327 EBCDIC_OUTBUFF
*wbuf
;
328 BIO
*next
= BIO_next(b
);
332 if ((in
== NULL
) || (inl
<= 0))
337 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
339 if (inl
> (num
= wbuf
->alloced
)) {
340 num
= num
+ num
; /* double the size */
344 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
347 wbuf
->buff
[0] = '\0';
349 BIO_set_data(b
, wbuf
);
352 ebcdic2ascii(wbuf
->buff
, in
, inl
);
354 ret
= BIO_write(next
, wbuf
->buff
, inl
);
359 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
362 BIO
*next
= BIO_next(b
);
371 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
377 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
380 BIO
*next
= BIO_next(bp
);
384 /* return(BIO_gets(bp->next_bio,buf,size));*/
385 for (i
= 0; i
< size
- 1; ++i
) {
386 ret
= ebcdic_read(bp
, &buf
[i
], 1);
389 else if (buf
[i
] == '\n') {
396 return (ret
< 0 && i
== 0) ? ret
: i
;
399 static int ebcdic_puts(BIO
*bp
, const char *str
)
401 if (BIO_next(bp
) == NULL
)
403 return ebcdic_write(bp
, str
, strlen(str
));
407 /* This is a context that we pass to callbacks */
408 typedef struct tlsextctx_st
{
414 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
416 tlsextctx
*p
= (tlsextctx
*) arg
;
417 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
419 if (servername
!= NULL
&& p
->biodebug
!= NULL
) {
420 const char *cp
= servername
;
423 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"");
424 while ((uc
= *cp
++) != 0)
425 BIO_printf(p
->biodebug
,
426 (((uc
) & ~127) == 0) && isprint(uc
) ? "%c" : "\\x%02x", uc
);
427 BIO_printf(p
->biodebug
, "\"\n");
430 if (p
->servername
== NULL
)
431 return SSL_TLSEXT_ERR_NOACK
;
433 if (servername
!= NULL
) {
434 if (OPENSSL_strcasecmp(servername
, p
->servername
))
435 return p
->extension_error
;
437 BIO_printf(p
->biodebug
, "Switching server context.\n");
438 SSL_set_SSL_CTX(s
, ctx2
);
441 return SSL_TLSEXT_ERR_OK
;
444 /* Structure passed to cert status callback */
445 typedef struct tlsextstatusctx_st
{
447 /* File to load OCSP Response from (or NULL if no file) */
449 /* Default responder to use */
450 char *host
, *path
, *port
;
451 char *proxy
, *no_proxy
;
456 static tlsextstatusctx tlscstatp
= { -1 };
458 #ifndef OPENSSL_NO_OCSP
461 * Helper function to get an OCSP_RESPONSE from a responder. This is a
462 * simplified version. It examines certificates each time and makes one OCSP
463 * responder query for each request. A full version would store details such as
464 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
465 * them until they were considered "expired".
467 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
468 OCSP_RESPONSE
**resp
)
470 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
471 char *proxy
= NULL
, *no_proxy
= NULL
;
473 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
475 X509_STORE_CTX
*inctx
= NULL
;
477 OCSP_REQUEST
*req
= NULL
;
478 OCSP_CERTID
*id
= NULL
;
479 STACK_OF(X509_EXTENSION
) *exts
;
480 int ret
= SSL_TLSEXT_ERR_NOACK
;
483 /* Build up OCSP query from server certificate */
484 x
= SSL_get_certificate(s
);
485 aia
= X509_get1_ocsp(x
);
487 if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia
, 0), &use_ssl
,
488 NULL
, &host
, &port
, NULL
, &path
, NULL
, NULL
)) {
489 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
493 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
494 sk_OPENSSL_STRING_value(aia
, 0));
496 if (srctx
->host
== NULL
) {
498 "cert_status: no AIA and no default responder URL\n");
504 use_ssl
= srctx
->use_ssl
;
506 proxy
= srctx
->proxy
;
507 no_proxy
= srctx
->no_proxy
;
509 inctx
= X509_STORE_CTX_new();
512 if (!X509_STORE_CTX_init(inctx
,
513 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s
)),
516 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
,
517 X509_get_issuer_name(x
));
519 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
522 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
523 X509_OBJECT_free(obj
);
526 req
= OCSP_REQUEST_new();
529 if (!OCSP_request_add0_id(req
, id
))
532 /* Add any extensions to the request */
533 SSL_get_tlsext_status_exts(s
, &exts
);
534 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
535 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
536 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
539 *resp
= process_responder(req
, host
, port
, path
, proxy
, no_proxy
,
540 use_ssl
, NULL
/* headers */, srctx
->timeout
);
542 BIO_puts(bio_err
, "cert_status: error querying responder\n");
546 ret
= SSL_TLSEXT_ERR_OK
;
550 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
553 * If we parsed aia we need to free; otherwise they were copied and we
560 X509_email_free(aia
);
562 OCSP_CERTID_free(id
);
563 OCSP_REQUEST_free(req
);
564 X509_STORE_CTX_free(inctx
);
569 * Certificate Status callback. This is called when a client includes a
570 * certificate status request extension. The response is either obtained from a
571 * file, or from an OCSP responder.
573 static int cert_status_cb(SSL
*s
, void *arg
)
575 tlsextstatusctx
*srctx
= arg
;
576 OCSP_RESPONSE
*resp
= NULL
;
577 unsigned char *rspder
= NULL
;
579 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
582 BIO_puts(bio_err
, "cert_status: callback called\n");
584 if (srctx
->respin
!= NULL
) {
585 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
586 if (derbio
== NULL
) {
587 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
590 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
593 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
597 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
598 if (ret
!= SSL_TLSEXT_ERR_OK
)
602 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
606 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
607 if (srctx
->verbose
) {
608 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
609 OCSP_RESPONSE_print(bio_err
, resp
, 2);
612 ret
= SSL_TLSEXT_ERR_OK
;
615 if (ret
!= SSL_TLSEXT_ERR_OK
)
616 ERR_print_errors(bio_err
);
618 OCSP_RESPONSE_free(resp
);
624 #ifndef OPENSSL_NO_NEXTPROTONEG
625 /* This is the context that we pass to next_proto_cb */
626 typedef struct tlsextnextprotoctx_st
{
629 } tlsextnextprotoctx
;
631 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
632 unsigned int *len
, void *arg
)
634 tlsextnextprotoctx
*next_proto
= arg
;
636 *data
= next_proto
->data
;
637 *len
= next_proto
->len
;
639 return SSL_TLSEXT_ERR_OK
;
641 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
643 /* This the context that we pass to alpn_cb */
644 typedef struct tlsextalpnctx_st
{
649 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
650 const unsigned char *in
, unsigned int inlen
, void *arg
)
652 tlsextalpnctx
*alpn_ctx
= arg
;
655 /* We can assume that |in| is syntactically valid. */
657 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
658 for (i
= 0; i
< inlen
;) {
660 BIO_write(bio_s_out
, ", ", 2);
661 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
664 BIO_write(bio_s_out
, "\n", 1);
667 if (SSL_select_next_proto
668 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
669 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
670 return SSL_TLSEXT_ERR_ALERT_FATAL
;
674 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
675 BIO_write(bio_s_out
, *out
, *outlen
);
676 BIO_write(bio_s_out
, "\n", 1);
679 return SSL_TLSEXT_ERR_OK
;
682 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
684 /* disable resumption for sessions with forward secure ciphers */
685 return is_forward_secure
;
688 typedef enum OPTION_choice
{
691 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
692 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
693 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
694 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
695 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
696 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
697 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
698 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
700 OPT_CASTORE
, OPT_NOCASTORE
, OPT_CHAINCASTORE
, OPT_VERIFYCASTORE
,
701 OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
702 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
703 OPT_STATUS_TIMEOUT
, OPT_PROXY
, OPT_NO_PROXY
, OPT_STATUS_URL
,
704 OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
705 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
706 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
707 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_IDENTITY
, OPT_PSK_HINT
, OPT_PSK
,
708 OPT_PSK_SESS
, OPT_SRPVFILE
, OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
,
709 OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
, OPT_SSL_CONFIG
,
710 OPT_MAX_SEND_FRAG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
711 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
712 OPT_DTLS1_2
, OPT_SCTP
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
, OPT_STATELESS
,
713 OPT_ID_PREFIX
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
714 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
, OPT_SENDFILE
,
715 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
716 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_RECV_MAX_EARLY
, OPT_EARLY_DATA
,
717 OPT_S_NUM_TICKETS
, OPT_ANTI_REPLAY
, OPT_NO_ANTI_REPLAY
, OPT_SCTP_LABEL_BUG
,
718 OPT_HTTP_SERVER_BINMODE
, OPT_NOCANAMES
, OPT_IGNORE_UNEXPECTED_EOF
, OPT_KTLS
,
727 const OPTIONS s_server_options
[] = {
728 OPT_SECTION("General"),
729 {"help", OPT_HELP
, '-', "Display this summary"},
730 {"ssl_config", OPT_SSL_CONFIG
, 's',
731 "Configure SSL_CTX using the given configuration value"},
732 #ifndef OPENSSL_NO_SSL_TRACE
733 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
735 #ifndef OPENSSL_NO_ENGINE
736 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
739 OPT_SECTION("Network"),
740 {"port", OPT_PORT
, 'p',
741 "TCP/IP port to listen on for connections (default is " PORT
")"},
742 {"accept", OPT_ACCEPT
, 's',
743 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
745 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
746 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
748 {"4", OPT_4
, '-', "Use IPv4 only"},
749 {"6", OPT_6
, '-', "Use IPv6 only"},
750 #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
751 {"tfo", OPT_TFO
, '-', "Listen for TCP Fast Open connections"},
754 OPT_SECTION("Identity"),
755 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
756 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
757 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
758 {"CAstore", OPT_CASTORE
, ':', "URI to store of CA's"},
759 {"no-CAfile", OPT_NOCAFILE
, '-',
760 "Do not load the default certificates file"},
761 {"no-CApath", OPT_NOCAPATH
, '-',
762 "Do not load certificates from the default certificates directory"},
763 {"no-CAstore", OPT_NOCASTORE
, '-',
764 "Do not load certificates from the default certificates store URI"},
765 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
766 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
767 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
768 "Turn on peer certificate verification, must have a cert"},
769 {"nameopt", OPT_NAMEOPT
, 's', "Certificate subject/issuer name printing options"},
770 {"cert", OPT_CERT
, '<', "Server certificate file to use; default " TEST_CERT
},
771 {"cert2", OPT_CERT2
, '<',
772 "Certificate file to use for servername; default " TEST_CERT2
},
773 {"certform", OPT_CERTFORM
, 'F',
774 "Server certificate file format (PEM/DER/P12); has no effect"},
775 {"cert_chain", OPT_CERT_CHAIN
, '<',
776 "Server certificate chain file in PEM format"},
777 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build server certificate chain"},
778 {"serverinfo", OPT_SERVERINFO
, 's',
779 "PEM serverinfo file for certificate"},
780 {"key", OPT_KEY
, 's',
781 "Private key file to use; default is -cert file or else" TEST_CERT
},
782 {"key2", OPT_KEY2
, '<',
783 "-Private Key file to use for servername if not in -cert2"},
784 {"keyform", OPT_KEYFORM
, 'f', "Key format (ENGINE, other values ignored)"},
785 {"pass", OPT_PASS
, 's', "Private key and cert file pass phrase source"},
786 {"dcert", OPT_DCERT
, '<',
787 "Second server certificate file to use (usually for DSA)"},
788 {"dcertform", OPT_DCERTFORM
, 'F',
789 "Second server certificate file format (PEM/DER/P12); has no effect"},
790 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
791 "second server certificate chain file in PEM format"},
792 {"dkey", OPT_DKEY
, '<',
793 "Second private key file to use (usually for DSA)"},
794 {"dkeyform", OPT_DKEYFORM
, 'F',
795 "Second key file format (ENGINE, other values ignored)"},
796 {"dpass", OPT_DPASS
, 's',
797 "Second private key and cert file pass phrase source"},
798 {"dhparam", OPT_DHPARAM
, '<', "DH parameters file to use"},
799 {"servername", OPT_SERVERNAME
, 's',
800 "Servername for HostName TLS extension"},
801 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
802 "On servername mismatch send fatal alert (default warning alert)"},
803 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
804 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
805 {"quiet", OPT_QUIET
, '-', "No server output"},
806 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
807 "Disable caching and tickets if ephemeral (EC)DH is used"},
808 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
809 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
810 {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF
, '-',
811 "Do not treat lack of close_notify from a peer as an error"},
812 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
813 "Hex dump of all TLS extensions received"},
814 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
815 {"id_prefix", OPT_ID_PREFIX
, 's',
816 "Generate SSL/TLS session IDs prefixed by arg"},
817 {"keymatexport", OPT_KEYMATEXPORT
, 's',
818 "Export keying material using label"},
819 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
820 "Export len bytes of keying material; default 20"},
821 {"CRL", OPT_CRL
, '<', "CRL file to use"},
822 {"CRLform", OPT_CRLFORM
, 'F', "CRL file format (PEM or DER); default PEM"},
823 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
824 "Download CRLs from distribution points in certificate CDP entries"},
825 {"chainCAfile", OPT_CHAINCAFILE
, '<',
826 "CA file for certificate chain (PEM format)"},
827 {"chainCApath", OPT_CHAINCAPATH
, '/',
828 "use dir as certificate store path to build CA certificate chain"},
829 {"chainCAstore", OPT_CHAINCASTORE
, ':',
830 "use URI as certificate store to build CA certificate chain"},
831 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
832 "CA file for certificate verification (PEM format)"},
833 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
834 "use dir as certificate store path to verify CA certificate"},
835 {"verifyCAstore", OPT_VERIFYCASTORE
, ':',
836 "use URI as certificate store to verify CA certificate"},
837 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
838 {"ext_cache", OPT_EXT_CACHE
, '-',
839 "Disable internal cache, set up and use external cache"},
840 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
841 "Close connection on verification error"},
842 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
843 "No verify output except verify errors"},
844 {"ign_eof", OPT_IGN_EOF
, '-', "Ignore input EOF (default when -quiet)"},
845 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input EOF"},
847 #ifndef OPENSSL_NO_OCSP
849 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
850 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
851 "Print more output in certificate status callback"},
852 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
853 "Status request responder timeout"},
854 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
855 {"proxy", OPT_PROXY
, 's',
856 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
857 {"no_proxy", OPT_NO_PROXY
, 's',
858 "List of addresses of servers not to use HTTP(S) proxy for"},
860 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
861 {"status_file", OPT_STATUS_FILE
, '<',
862 "File containing DER encoded OCSP Response"},
865 OPT_SECTION("Debug"),
866 {"security_debug", OPT_SECURITY_DEBUG
, '-',
867 "Print output from SSL/TLS security framework"},
868 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
869 "Print more output from SSL/TLS security framework"},
870 {"brief", OPT_BRIEF
, '-',
871 "Restrict output to brief summary of connection parameters"},
872 {"rev", OPT_REV
, '-',
873 "act as an echo server that sends back received text reversed"},
874 {"debug", OPT_DEBUG
, '-', "Print more output"},
875 {"msg", OPT_MSG
, '-', "Show protocol messages"},
876 {"msgfile", OPT_MSGFILE
, '>',
877 "File to send output of -msg or -trace, instead of stdout"},
878 {"state", OPT_STATE
, '-', "Print the SSL states"},
879 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
880 {"max_pipelines", OPT_MAX_PIPELINES
, 'p',
881 "Maximum number of encrypt/decrypt pipelines to be used"},
882 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
883 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
885 OPT_SECTION("Network"),
886 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
887 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
888 {"mtu", OPT_MTU
, 'p', "Set link-layer MTU"},
889 {"read_buf", OPT_READ_BUF
, 'p',
890 "Default read buffer size to be used for connections"},
891 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'p',
892 "Size used to split data for encrypt pipelines"},
893 {"max_send_frag", OPT_MAX_SEND_FRAG
, 'p', "Maximum Size of send frames "},
895 OPT_SECTION("Server identity"),
896 {"psk_identity", OPT_PSK_IDENTITY
, 's', "PSK identity to expect"},
897 #ifndef OPENSSL_NO_PSK
898 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
900 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
901 {"psk_session", OPT_PSK_SESS
, '<', "File to read PSK SSL session from"},
902 #ifndef OPENSSL_NO_SRP
903 {"srpvfile", OPT_SRPVFILE
, '<', "(deprecated) The verifier file for SRP"},
904 {"srpuserseed", OPT_SRPUSERSEED
, 's',
905 "(deprecated) A seed string for a default user salt"},
908 OPT_SECTION("Protocol and version"),
909 {"max_early_data", OPT_MAX_EARLY
, 'n',
910 "The maximum number of bytes of early data as advertised in tickets"},
911 {"recv_max_early_data", OPT_RECV_MAX_EARLY
, 'n',
912 "The maximum number of bytes of early data (hard limit)"},
913 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
914 {"num_tickets", OPT_S_NUM_TICKETS
, 'n',
915 "The number of TLSv1.3 session tickets that a server will automatically issue" },
916 {"anti_replay", OPT_ANTI_REPLAY
, '-', "Switch on anti-replay protection (default)"},
917 {"no_anti_replay", OPT_NO_ANTI_REPLAY
, '-', "Switch off anti-replay protection"},
918 {"http_server_binmode", OPT_HTTP_SERVER_BINMODE
, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)"},
919 {"no_ca_names", OPT_NOCANAMES
, '-',
920 "Disable TLS Extension CA Names"},
921 {"stateless", OPT_STATELESS
, '-', "Require TLSv1.3 cookies"},
922 #ifndef OPENSSL_NO_SSL3
923 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
925 #ifndef OPENSSL_NO_TLS1
926 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
928 #ifndef OPENSSL_NO_TLS1_1
929 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
931 #ifndef OPENSSL_NO_TLS1_2
932 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
934 #ifndef OPENSSL_NO_TLS1_3
935 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
937 #ifndef OPENSSL_NO_DTLS
938 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
939 {"listen", OPT_LISTEN
, '-',
940 "Listen for a DTLS ClientHello with a cookie and then connect"},
942 #ifndef OPENSSL_NO_DTLS1
943 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
945 #ifndef OPENSSL_NO_DTLS1_2
946 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
948 #ifndef OPENSSL_NO_SCTP
949 {"sctp", OPT_SCTP
, '-', "Use SCTP"},
950 {"sctp_label_bug", OPT_SCTP_LABEL_BUG
, '-', "Enable SCTP label length bug"},
952 #ifndef OPENSSL_NO_SRTP
953 {"use_srtp", OPT_SRTP_PROFILES
, 's',
954 "Offer SRTP key management with a colon-separated profile list"},
956 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
957 #ifndef OPENSSL_NO_NEXTPROTONEG
958 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
959 "Set the advertised protocols for the NPN extension (comma-separated list)"},
961 {"alpn", OPT_ALPN
, 's',
962 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
963 #ifndef OPENSSL_NO_KTLS
964 {"ktls", OPT_KTLS
, '-', "Enable Kernel TLS for sending and receiving"},
965 {"sendfile", OPT_SENDFILE
, '-', "Use sendfile to response file with -WWW"},
976 #define IS_PROT_FLAG(o) \
977 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
978 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
980 int s_server_main(int argc
, char *argv
[])
982 ENGINE
*engine
= NULL
;
983 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
984 SSL_CONF_CTX
*cctx
= NULL
;
985 const SSL_METHOD
*meth
= TLS_server_method();
986 SSL_EXCERT
*exc
= NULL
;
987 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
988 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
989 STACK_OF(X509_CRL
) *crls
= NULL
;
990 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
991 X509_VERIFY_PARAM
*vpm
= NULL
;
992 const char *CApath
= NULL
, *CAfile
= NULL
, *CAstore
= NULL
;
993 const char *chCApath
= NULL
, *chCAfile
= NULL
, *chCAstore
= NULL
;
994 char *dpassarg
= NULL
, *dpass
= NULL
;
995 char *passarg
= NULL
, *pass
= NULL
;
996 char *vfyCApath
= NULL
, *vfyCAfile
= NULL
, *vfyCAstore
= NULL
;
997 char *crl_file
= NULL
, *prog
;
999 int unlink_unix_path
= 0;
1001 do_server_cb server_cb
;
1002 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
1003 char *dhfile
= NULL
;
1005 int nocert
= 0, ret
= 1;
1006 int noCApath
= 0, noCAfile
= 0, noCAstore
= 0;
1007 int s_cert_format
= FORMAT_UNDEF
, s_key_format
= FORMAT_UNDEF
;
1008 int s_dcert_format
= FORMAT_UNDEF
, s_dkey_format
= FORMAT_UNDEF
;
1009 int rev
= 0, naccept
= -1, sdebug
= 0;
1010 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
, protocol
= 0;
1011 int state
= 0, crl_format
= FORMAT_UNDEF
, crl_download
= 0;
1014 unsigned char *context
= NULL
;
1016 EVP_PKEY
*s_key2
= NULL
;
1017 X509
*s_cert2
= NULL
;
1018 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
1019 const char *ssl_config
= NULL
;
1020 int read_buf_len
= 0;
1021 #ifndef OPENSSL_NO_NEXTPROTONEG
1022 const char *next_proto_neg_in
= NULL
;
1023 tlsextnextprotoctx next_proto
= { NULL
, 0 };
1025 const char *alpn_in
= NULL
;
1026 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
1027 #ifndef OPENSSL_NO_PSK
1028 /* by default do not send a PSK identity hint */
1029 char *psk_identity_hint
= NULL
;
1032 #ifndef OPENSSL_NO_SRP
1033 char *srpuserseed
= NULL
;
1034 char *srp_verifier_file
= NULL
;
1036 #ifndef OPENSSL_NO_SRTP
1037 char *srtp_profiles
= NULL
;
1039 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
1040 int s_server_verify
= SSL_VERIFY_NONE
;
1041 int s_server_session_id_context
= 1; /* anything will do */
1042 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
1043 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
1044 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
1045 #ifndef OPENSSL_NO_OCSP
1046 int s_tlsextstatus
= 0;
1048 int no_resume_ephemeral
= 0;
1049 unsigned int max_send_fragment
= 0;
1050 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
1051 const char *s_serverinfo_file
= NULL
;
1052 const char *keylog_file
= NULL
;
1053 int max_early_data
= -1, recv_max_early_data
= -1;
1054 char *psksessf
= NULL
;
1055 int no_ca_names
= 0;
1056 #ifndef OPENSSL_NO_SCTP
1057 int sctp_label_bug
= 0;
1059 int ignore_unexpected_eof
= 0;
1060 #ifndef OPENSSL_NO_KTLS
1061 int enable_ktls
= 0;
1065 /* Init of few remaining global variables */
1070 s_nbio
= s_nbio_test
= 0;
1080 port
= OPENSSL_strdup(PORT
);
1081 cctx
= SSL_CONF_CTX_new();
1082 vpm
= X509_VERIFY_PARAM_new();
1083 if (port
== NULL
|| cctx
== NULL
|| vpm
== NULL
)
1085 SSL_CONF_CTX_set_flags(cctx
,
1086 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1088 prog
= opt_init(argc
, argv
, s_server_options
);
1089 while ((o
= opt_next()) != OPT_EOF
) {
1090 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1091 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1094 if (IS_NO_PROT_FLAG(o
))
1096 if (prot_opt
== 1 && no_prot_opt
) {
1098 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1105 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1108 opt_help(s_server_options
);
1114 if (socket_family
== AF_UNIX
) {
1115 OPENSSL_free(host
); host
= NULL
;
1116 OPENSSL_free(port
); port
= NULL
;
1119 socket_family
= AF_INET
;
1125 if (socket_family
== AF_UNIX
) {
1126 OPENSSL_free(host
); host
= NULL
;
1127 OPENSSL_free(port
); port
= NULL
;
1130 socket_family
= AF_INET6
;
1133 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1139 if (socket_family
== AF_UNIX
) {
1140 socket_family
= AF_UNSPEC
;
1143 OPENSSL_free(port
); port
= NULL
;
1144 OPENSSL_free(host
); host
= NULL
;
1145 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1147 "%s: -port argument malformed or ambiguous\n",
1154 if (socket_family
== AF_UNIX
) {
1155 socket_family
= AF_UNSPEC
;
1158 OPENSSL_free(port
); port
= NULL
;
1159 OPENSSL_free(host
); host
= NULL
;
1160 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1162 "%s: -accept argument malformed or ambiguous\n",
1169 socket_family
= AF_UNIX
;
1170 OPENSSL_free(host
); host
= OPENSSL_strdup(opt_arg());
1173 OPENSSL_free(port
); port
= NULL
;
1176 unlink_unix_path
= 1;
1180 naccept
= atol(opt_arg());
1183 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1184 verify_args
.depth
= atoi(opt_arg());
1186 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1188 case OPT_UPPER_V_VERIFY
:
1190 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1191 SSL_VERIFY_CLIENT_ONCE
;
1192 verify_args
.depth
= atoi(opt_arg());
1195 "verify depth is %d, must return a certificate\n",
1199 context
= (unsigned char *)opt_arg();
1202 s_cert_file
= opt_arg();
1205 if (!set_nameopt(opt_arg()))
1209 crl_file
= opt_arg();
1211 case OPT_CRL_DOWNLOAD
:
1214 case OPT_SERVERINFO
:
1215 s_serverinfo_file
= opt_arg();
1218 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_cert_format
))
1222 s_key_file
= opt_arg();
1225 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1229 passarg
= opt_arg();
1231 case OPT_CERT_CHAIN
:
1232 s_chain_file
= opt_arg();
1238 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dcert_format
))
1242 s_dcert_file
= opt_arg();
1245 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dkey_format
))
1249 dpassarg
= opt_arg();
1252 s_dkey_file
= opt_arg();
1254 case OPT_DCERT_CHAIN
:
1255 s_dchain_file
= opt_arg();
1266 case OPT_CHAINCAPATH
:
1267 chCApath
= opt_arg();
1269 case OPT_VERIFYCAPATH
:
1270 vfyCApath
= opt_arg();
1273 CAstore
= opt_arg();
1278 case OPT_CHAINCASTORE
:
1279 chCAstore
= opt_arg();
1281 case OPT_VERIFYCASTORE
:
1282 vfyCAstore
= opt_arg();
1291 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1295 case OPT_S_NUM_TICKETS
:
1296 case OPT_ANTI_REPLAY
:
1297 case OPT_NO_ANTI_REPLAY
:
1298 if (ssl_args
== NULL
)
1299 ssl_args
= sk_OPENSSL_STRING_new_null();
1300 if (ssl_args
== NULL
1301 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1302 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1303 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1308 if (!opt_verify(o
, vpm
))
1313 if (!args_excert(o
, &exc
))
1316 case OPT_VERIFY_RET_ERROR
:
1317 verify_args
.return_error
= 1;
1319 case OPT_VERIFY_QUIET
:
1320 verify_args
.quiet
= 1;
1322 case OPT_BUILD_CHAIN
:
1331 case OPT_CHAINCAFILE
:
1332 chCAfile
= opt_arg();
1334 case OPT_VERIFYCAFILE
:
1335 vfyCAfile
= opt_arg();
1341 s_nbio
= s_nbio_test
= 1;
1346 case OPT_NO_IGN_EOF
:
1352 case OPT_TLSEXTDEBUG
:
1356 #ifndef OPENSSL_NO_OCSP
1360 case OPT_STATUS_VERBOSE
:
1361 #ifndef OPENSSL_NO_OCSP
1362 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1365 case OPT_STATUS_TIMEOUT
:
1366 #ifndef OPENSSL_NO_OCSP
1368 tlscstatp
.timeout
= atoi(opt_arg());
1372 #ifndef OPENSSL_NO_OCSP
1373 tlscstatp
.proxy
= opt_arg();
1377 #ifndef OPENSSL_NO_OCSP
1378 tlscstatp
.no_proxy
= opt_arg();
1381 case OPT_STATUS_URL
:
1382 #ifndef OPENSSL_NO_OCSP
1384 if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp
.use_ssl
, NULL
,
1385 &tlscstatp
.host
, &tlscstatp
.port
, NULL
,
1386 &tlscstatp
.path
, NULL
, NULL
)) {
1387 BIO_printf(bio_err
, "Error parsing -status_url argument\n");
1392 case OPT_STATUS_FILE
:
1393 #ifndef OPENSSL_NO_OCSP
1395 tlscstatp
.respin
= opt_arg();
1402 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1403 if (bio_s_msg
== NULL
) {
1404 BIO_printf(bio_err
, "Error writing file %s\n", opt_arg());
1409 #ifndef OPENSSL_NO_SSL_TRACE
1413 case OPT_SECURITY_DEBUG
:
1416 case OPT_SECURITY_DEBUG_VERBOSE
:
1429 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1434 case OPT_NO_RESUME_EPHEMERAL
:
1435 no_resume_ephemeral
= 1;
1437 case OPT_PSK_IDENTITY
:
1438 psk_identity
= opt_arg();
1441 #ifndef OPENSSL_NO_PSK
1442 psk_identity_hint
= opt_arg();
1446 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1447 if (isxdigit(_UC(*p
)))
1449 BIO_printf(bio_err
, "Not a hex number '%s'\n", psk_key
);
1454 psksessf
= opt_arg();
1457 #ifndef OPENSSL_NO_SRP
1458 srp_verifier_file
= opt_arg();
1459 if (min_version
< TLS1_VERSION
)
1460 min_version
= TLS1_VERSION
;
1463 case OPT_SRPUSERSEED
:
1464 #ifndef OPENSSL_NO_SRP
1465 srpuserseed
= opt_arg();
1466 if (min_version
< TLS1_VERSION
)
1467 min_version
= TLS1_VERSION
;
1482 case OPT_SSL_CONFIG
:
1483 ssl_config
= opt_arg();
1486 min_version
= SSL3_VERSION
;
1487 max_version
= SSL3_VERSION
;
1490 min_version
= TLS1_3_VERSION
;
1491 max_version
= TLS1_3_VERSION
;
1494 min_version
= TLS1_2_VERSION
;
1495 max_version
= TLS1_2_VERSION
;
1498 min_version
= TLS1_1_VERSION
;
1499 max_version
= TLS1_1_VERSION
;
1502 min_version
= TLS1_VERSION
;
1503 max_version
= TLS1_VERSION
;
1506 #ifndef OPENSSL_NO_DTLS
1507 meth
= DTLS_server_method();
1508 socket_type
= SOCK_DGRAM
;
1512 #ifndef OPENSSL_NO_DTLS
1513 meth
= DTLS_server_method();
1514 min_version
= DTLS1_VERSION
;
1515 max_version
= DTLS1_VERSION
;
1516 socket_type
= SOCK_DGRAM
;
1520 #ifndef OPENSSL_NO_DTLS
1521 meth
= DTLS_server_method();
1522 min_version
= DTLS1_2_VERSION
;
1523 max_version
= DTLS1_2_VERSION
;
1524 socket_type
= SOCK_DGRAM
;
1528 #ifndef OPENSSL_NO_SCTP
1529 protocol
= IPPROTO_SCTP
;
1532 case OPT_SCTP_LABEL_BUG
:
1533 #ifndef OPENSSL_NO_SCTP
1538 #ifndef OPENSSL_NO_DTLS
1539 enable_timeouts
= 1;
1543 #ifndef OPENSSL_NO_DTLS
1544 socket_mtu
= atol(opt_arg());
1548 #ifndef OPENSSL_NO_DTLS
1556 session_id_prefix
= opt_arg();
1559 #ifndef OPENSSL_NO_ENGINE
1560 engine
= setup_engine(opt_arg(), s_debug
);
1567 case OPT_PROV_CASES
:
1568 if (!opt_provider(o
))
1571 case OPT_SERVERNAME
:
1572 tlsextcbp
.servername
= opt_arg();
1574 case OPT_SERVERNAME_FATAL
:
1575 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1578 s_cert_file2
= opt_arg();
1581 s_key_file2
= opt_arg();
1583 case OPT_NEXTPROTONEG
:
1584 # ifndef OPENSSL_NO_NEXTPROTONEG
1585 next_proto_neg_in
= opt_arg();
1589 alpn_in
= opt_arg();
1591 case OPT_SRTP_PROFILES
:
1592 #ifndef OPENSSL_NO_SRTP
1593 srtp_profiles
= opt_arg();
1596 case OPT_KEYMATEXPORT
:
1597 keymatexportlabel
= opt_arg();
1599 case OPT_KEYMATEXPORTLEN
:
1600 keymatexportlen
= atoi(opt_arg());
1605 case OPT_MAX_SEND_FRAG
:
1606 max_send_fragment
= atoi(opt_arg());
1608 case OPT_SPLIT_SEND_FRAG
:
1609 split_send_fragment
= atoi(opt_arg());
1611 case OPT_MAX_PIPELINES
:
1612 max_pipelines
= atoi(opt_arg());
1615 read_buf_len
= atoi(opt_arg());
1617 case OPT_KEYLOG_FILE
:
1618 keylog_file
= opt_arg();
1621 max_early_data
= atoi(opt_arg());
1622 if (max_early_data
< 0) {
1623 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1627 case OPT_RECV_MAX_EARLY
:
1628 recv_max_early_data
= atoi(opt_arg());
1629 if (recv_max_early_data
< 0) {
1630 BIO_printf(bio_err
, "Invalid value for recv_max_early_data\n");
1634 case OPT_EARLY_DATA
:
1636 if (max_early_data
== -1)
1637 max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
1639 case OPT_HTTP_SERVER_BINMODE
:
1640 http_server_binmode
= 1;
1646 #ifndef OPENSSL_NO_KTLS
1651 #ifndef OPENSSL_NO_KTLS
1655 case OPT_IGNORE_UNEXPECTED_EOF
:
1656 ignore_unexpected_eof
= 1;
1664 /* No extra arguments. */
1665 if (!opt_check_rest_arg(NULL
))
1668 if (!app_RAND_load())
1671 #ifndef OPENSSL_NO_NEXTPROTONEG
1672 if (min_version
== TLS1_3_VERSION
&& next_proto_neg_in
!= NULL
) {
1673 BIO_printf(bio_err
, "Cannot supply -nextprotoneg with TLSv1.3\n");
1677 #ifndef OPENSSL_NO_DTLS
1678 if (www
&& socket_type
== SOCK_DGRAM
) {
1679 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1683 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1684 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1689 if (tfo
&& socket_type
!= SOCK_STREAM
) {
1690 BIO_printf(bio_err
, "Can only use -tfo with TLS\n");
1694 if (stateless
&& socket_type
!= SOCK_STREAM
) {
1695 BIO_printf(bio_err
, "Can only use --stateless with TLS\n");
1700 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1702 "Can't use unix sockets and datagrams together\n");
1706 if (early_data
&& (www
> 0 || rev
)) {
1708 "Can't use -early_data in combination with -www, -WWW, -HTTP, or -rev\n");
1712 #ifndef OPENSSL_NO_SCTP
1713 if (protocol
== IPPROTO_SCTP
) {
1714 if (socket_type
!= SOCK_DGRAM
) {
1715 BIO_printf(bio_err
, "Can't use -sctp without DTLS\n");
1718 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1719 socket_type
= SOCK_STREAM
;
1723 #ifndef OPENSSL_NO_KTLS
1724 if (use_sendfile
&& enable_ktls
== 0) {
1725 BIO_printf(bio_out
, "Warning: -sendfile depends on -ktls, enabling -ktls now.\n");
1729 if (use_sendfile
&& www
<= 1) {
1730 BIO_printf(bio_err
, "Can't use -sendfile without -WWW or -HTTP\n");
1735 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1736 BIO_printf(bio_err
, "Error getting password\n");
1740 if (s_key_file
== NULL
)
1741 s_key_file
= s_cert_file
;
1743 if (s_key_file2
== NULL
)
1744 s_key_file2
= s_cert_file2
;
1746 if (!load_excert(&exc
))
1750 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1751 "server certificate private key");
1755 s_cert
= load_cert_pass(s_cert_file
, s_cert_format
, 1, pass
,
1756 "server certificate");
1760 if (s_chain_file
!= NULL
) {
1761 if (!load_certs(s_chain_file
, 0, &s_chain
, NULL
,
1762 "server certificate chain"))
1766 if (tlsextcbp
.servername
!= NULL
) {
1767 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1768 "second server certificate private key");
1772 s_cert2
= load_cert_pass(s_cert_file2
, s_cert_format
, 1, pass
,
1773 "second server certificate");
1775 if (s_cert2
== NULL
)
1779 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1780 if (next_proto_neg_in
) {
1781 next_proto
.data
= next_protos_parse(&next_proto
.len
, next_proto_neg_in
);
1782 if (next_proto
.data
== NULL
)
1786 alpn_ctx
.data
= NULL
;
1788 alpn_ctx
.data
= next_protos_parse(&alpn_ctx
.len
, alpn_in
);
1789 if (alpn_ctx
.data
== NULL
)
1793 if (crl_file
!= NULL
) {
1795 crl
= load_crl(crl_file
, crl_format
, 0, "CRL");
1798 crls
= sk_X509_CRL_new_null();
1799 if (crls
== NULL
|| !sk_X509_CRL_push(crls
, crl
)) {
1800 BIO_puts(bio_err
, "Error adding CRL\n");
1801 ERR_print_errors(bio_err
);
1807 if (s_dcert_file
!= NULL
) {
1809 if (s_dkey_file
== NULL
)
1810 s_dkey_file
= s_dcert_file
;
1812 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1813 0, dpass
, engine
, "second certificate private key");
1817 s_dcert
= load_cert_pass(s_dcert_file
, s_dcert_format
, 1, dpass
,
1818 "second server certificate");
1820 if (s_dcert
== NULL
) {
1821 ERR_print_errors(bio_err
);
1824 if (s_dchain_file
!= NULL
) {
1825 if (!load_certs(s_dchain_file
, 0, &s_dchain
, NULL
,
1826 "second server certificate chain"))
1832 if (bio_s_out
== NULL
) {
1833 if (s_quiet
&& !s_debug
) {
1834 bio_s_out
= BIO_new(BIO_s_null());
1835 if (s_msg
&& bio_s_msg
== NULL
) {
1836 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1837 if (bio_s_msg
== NULL
) {
1838 BIO_printf(bio_err
, "Out of memory\n");
1843 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1847 if (bio_s_out
== NULL
)
1853 s_dcert_file
= NULL
;
1855 s_cert_file2
= NULL
;
1859 ctx
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
1861 ERR_print_errors(bio_err
);
1865 SSL_CTX_clear_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1868 ssl_ctx_security_debug(ctx
, sdebug
);
1870 if (!config_ctx(cctx
, ssl_args
, ctx
))
1874 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1875 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1877 ERR_print_errors(bio_err
);
1881 #ifndef OPENSSL_NO_SCTP
1882 if (protocol
== IPPROTO_SCTP
&& sctp_label_bug
== 1)
1883 SSL_CTX_set_mode(ctx
, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
);
1886 if (min_version
!= 0
1887 && SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1889 if (max_version
!= 0
1890 && SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1893 if (session_id_prefix
) {
1894 if (strlen(session_id_prefix
) >= 32)
1896 "warning: id_prefix is too long, only one new session will be possible\n");
1897 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1898 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1899 ERR_print_errors(bio_err
);
1902 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1905 ssl_ctx_set_excert(ctx
, exc
);
1908 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1910 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1912 init_session_cache_ctx(ctx
);
1914 SSL_CTX_sess_set_cache_size(ctx
, 128);
1917 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1921 SSL_CTX_set_options(ctx
, SSL_OP_DISABLE_TLSEXT_CA_NAMES
);
1924 if (ignore_unexpected_eof
)
1925 SSL_CTX_set_options(ctx
, SSL_OP_IGNORE_UNEXPECTED_EOF
);
1926 #ifndef OPENSSL_NO_KTLS
1928 SSL_CTX_set_options(ctx
, SSL_OP_ENABLE_KTLS
);
1931 if (max_send_fragment
> 0
1932 && !SSL_CTX_set_max_send_fragment(ctx
, max_send_fragment
)) {
1933 BIO_printf(bio_err
, "%s: Max send fragment size %u is out of permitted range\n",
1934 prog
, max_send_fragment
);
1938 if (split_send_fragment
> 0
1939 && !SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
)) {
1940 BIO_printf(bio_err
, "%s: Split send fragment size %u is out of permitted range\n",
1941 prog
, split_send_fragment
);
1944 if (max_pipelines
> 0
1945 && !SSL_CTX_set_max_pipelines(ctx
, max_pipelines
)) {
1946 BIO_printf(bio_err
, "%s: Max pipelines %u is out of permitted range\n",
1947 prog
, max_pipelines
);
1951 if (read_buf_len
> 0) {
1952 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1954 #ifndef OPENSSL_NO_SRTP
1955 if (srtp_profiles
!= NULL
) {
1956 /* Returns 0 on success! */
1957 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1958 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1959 ERR_print_errors(bio_err
);
1965 if (!ctx_set_verify_locations(ctx
, CAfile
, noCAfile
, CApath
, noCApath
,
1966 CAstore
, noCAstore
)) {
1967 ERR_print_errors(bio_err
);
1970 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1971 BIO_printf(bio_err
, "Error setting verify params\n");
1972 ERR_print_errors(bio_err
);
1976 ssl_ctx_add_crls(ctx
, crls
, 0);
1978 if (!ssl_load_stores(ctx
,
1979 vfyCApath
, vfyCAfile
, vfyCAstore
,
1980 chCApath
, chCAfile
, chCAstore
,
1981 crls
, crl_download
)) {
1982 BIO_printf(bio_err
, "Error loading store locations\n");
1983 ERR_print_errors(bio_err
);
1988 ctx2
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
1990 ERR_print_errors(bio_err
);
1996 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
1999 ssl_ctx_security_debug(ctx2
, sdebug
);
2001 if (session_id_prefix
) {
2002 if (strlen(session_id_prefix
) >= 32)
2004 "warning: id_prefix is too long, only one new session will be possible\n");
2005 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
2006 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
2007 ERR_print_errors(bio_err
);
2010 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
2013 ssl_ctx_set_excert(ctx2
, exc
);
2016 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
2019 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
2021 init_session_cache_ctx(ctx2
);
2023 SSL_CTX_sess_set_cache_size(ctx2
, 128);
2026 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
2028 if (!ctx_set_verify_locations(ctx2
, CAfile
, noCAfile
, CApath
,
2029 noCApath
, CAstore
, noCAstore
)) {
2030 ERR_print_errors(bio_err
);
2033 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
2034 BIO_printf(bio_err
, "Error setting verify params\n");
2035 ERR_print_errors(bio_err
);
2039 ssl_ctx_add_crls(ctx2
, crls
, 0);
2040 if (!config_ctx(cctx
, ssl_args
, ctx2
))
2043 #ifndef OPENSSL_NO_NEXTPROTONEG
2044 if (next_proto
.data
)
2045 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
2049 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
2052 EVP_PKEY
*dhpkey
= NULL
;
2055 dhpkey
= load_keyparams(dhfile
, FORMAT_UNDEF
, 0, "DH", "DH parameters");
2056 else if (s_cert_file
!= NULL
)
2057 dhpkey
= load_keyparams_suppress(s_cert_file
, FORMAT_UNDEF
, 0, "DH",
2058 "DH parameters", 1);
2060 if (dhpkey
!= NULL
) {
2061 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2063 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
2065 (void)BIO_flush(bio_s_out
);
2067 if (dhpkey
== NULL
) {
2068 SSL_CTX_set_dh_auto(ctx
, 1);
2071 * We need 2 references: one for use by ctx and one for use by
2074 if (!EVP_PKEY_up_ref(dhpkey
)) {
2075 EVP_PKEY_free(dhpkey
);
2078 if (!SSL_CTX_set0_tmp_dh_pkey(ctx
, dhpkey
)) {
2079 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2080 ERR_print_errors(bio_err
);
2081 /* Free 2 references */
2082 EVP_PKEY_free(dhpkey
);
2083 EVP_PKEY_free(dhpkey
);
2089 if (dhfile
!= NULL
) {
2090 EVP_PKEY
*dhpkey2
= load_keyparams_suppress(s_cert_file2
,
2093 "DH parameters", 1);
2095 if (dhpkey2
!= NULL
) {
2096 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2097 (void)BIO_flush(bio_s_out
);
2099 EVP_PKEY_free(dhpkey
);
2103 if (dhpkey
== NULL
) {
2104 SSL_CTX_set_dh_auto(ctx2
, 1);
2105 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2
, dhpkey
)) {
2106 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2107 ERR_print_errors(bio_err
);
2108 EVP_PKEY_free(dhpkey
);
2113 EVP_PKEY_free(dhpkey
);
2116 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
2119 if (s_serverinfo_file
!= NULL
2120 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
2121 ERR_print_errors(bio_err
);
2126 && !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
2129 if (s_dcert
!= NULL
) {
2130 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
2134 if (no_resume_ephemeral
) {
2135 SSL_CTX_set_not_resumable_session_callback(ctx
,
2136 not_resumable_sess_cb
);
2139 SSL_CTX_set_not_resumable_session_callback(ctx2
,
2140 not_resumable_sess_cb
);
2142 #ifndef OPENSSL_NO_PSK
2143 if (psk_key
!= NULL
) {
2145 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
2146 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
2149 if (psk_identity_hint
!= NULL
) {
2150 if (min_version
== TLS1_3_VERSION
) {
2151 BIO_printf(bio_s_out
, "PSK warning: there is NO identity hint in TLSv1.3\n");
2153 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
2154 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
2155 ERR_print_errors(bio_err
);
2161 if (psksessf
!= NULL
) {
2162 BIO
*stmp
= BIO_new_file(psksessf
, "r");
2165 BIO_printf(bio_err
, "Can't open PSK session file %s\n", psksessf
);
2166 ERR_print_errors(bio_err
);
2169 psksess
= PEM_read_bio_SSL_SESSION(stmp
, NULL
, 0, NULL
);
2171 if (psksess
== NULL
) {
2172 BIO_printf(bio_err
, "Can't read PSK session file %s\n", psksessf
);
2173 ERR_print_errors(bio_err
);
2179 if (psk_key
!= NULL
|| psksess
!= NULL
)
2180 SSL_CTX_set_psk_find_session_callback(ctx
, psk_find_session_cb
);
2182 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
2183 if (!SSL_CTX_set_session_id_context(ctx
,
2184 (void *)&s_server_session_id_context
,
2185 sizeof(s_server_session_id_context
))) {
2186 BIO_printf(bio_err
, "error setting session id context\n");
2187 ERR_print_errors(bio_err
);
2191 /* Set DTLS cookie generation and verification callbacks */
2192 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
2193 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
2195 /* Set TLS1.3 cookie generation and verification callbacks */
2196 SSL_CTX_set_stateless_cookie_generate_cb(ctx
, generate_stateless_cookie_callback
);
2197 SSL_CTX_set_stateless_cookie_verify_cb(ctx
, verify_stateless_cookie_callback
);
2200 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
2201 if (!SSL_CTX_set_session_id_context(ctx2
,
2202 (void *)&s_server_session_id_context
,
2203 sizeof(s_server_session_id_context
))) {
2204 BIO_printf(bio_err
, "error setting session id context\n");
2205 ERR_print_errors(bio_err
);
2208 tlsextcbp
.biodebug
= bio_s_out
;
2209 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
2210 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
2211 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
2212 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
2215 #ifndef OPENSSL_NO_SRP
2216 if (srp_verifier_file
!= NULL
) {
2217 if (!set_up_srp_verifier_file(ctx
, &srp_callback_parm
, srpuserseed
,
2222 if (CAfile
!= NULL
) {
2223 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
2226 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
2228 #ifndef OPENSSL_NO_OCSP
2229 if (s_tlsextstatus
) {
2230 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
2231 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2233 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2234 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2238 if (set_keylog_file(ctx
, keylog_file
))
2241 if (max_early_data
>= 0)
2242 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2243 if (recv_max_early_data
>= 0)
2244 SSL_CTX_set_recv_max_early_data(ctx
, recv_max_early_data
);
2247 server_cb
= rev_body
;
2249 server_cb
= www_body
;
2251 server_cb
= sv_body
;
2253 if (socket_family
== AF_UNIX
2254 && unlink_unix_path
)
2258 BIO_printf(bio_s_out
, "Listening for TFO\n");
2259 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
, protocol
,
2260 server_cb
, context
, naccept
, bio_s_out
, tfo
);
2261 print_stats(bio_s_out
, ctx
);
2265 SSL_SESSION_free(psksess
);
2266 set_keylog_file(NULL
, NULL
);
2268 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2270 EVP_PKEY_free(s_key
);
2271 EVP_PKEY_free(s_dkey
);
2272 OSSL_STACK_OF_X509_free(s_chain
);
2273 OSSL_STACK_OF_X509_free(s_dchain
);
2275 OPENSSL_free(dpass
);
2278 X509_VERIFY_PARAM_free(vpm
);
2280 OPENSSL_free(tlscstatp
.host
);
2281 OPENSSL_free(tlscstatp
.port
);
2282 OPENSSL_free(tlscstatp
.path
);
2285 EVP_PKEY_free(s_key2
);
2286 #ifndef OPENSSL_NO_NEXTPROTONEG
2287 OPENSSL_free(next_proto
.data
);
2289 OPENSSL_free(alpn_ctx
.data
);
2290 ssl_excert_free(exc
);
2291 sk_OPENSSL_STRING_free(ssl_args
);
2292 SSL_CONF_CTX_free(cctx
);
2293 release_engine(engine
);
2294 BIO_free(bio_s_out
);
2296 BIO_free(bio_s_msg
);
2298 #ifdef CHARSET_EBCDIC
2299 BIO_meth_free(methods_ebcdic
);
2304 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2306 BIO_printf(bio
, "%4ld items in the session cache\n",
2307 SSL_CTX_sess_number(ssl_ctx
));
2308 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2309 SSL_CTX_sess_connect(ssl_ctx
));
2310 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2311 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2312 BIO_printf(bio
, "%4ld client connects that finished\n",
2313 SSL_CTX_sess_connect_good(ssl_ctx
));
2314 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2315 SSL_CTX_sess_accept(ssl_ctx
));
2316 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2317 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2318 BIO_printf(bio
, "%4ld server accepts that finished\n",
2319 SSL_CTX_sess_accept_good(ssl_ctx
));
2320 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2321 BIO_printf(bio
, "%4ld session cache misses\n",
2322 SSL_CTX_sess_misses(ssl_ctx
));
2323 BIO_printf(bio
, "%4ld session cache timeouts\n",
2324 SSL_CTX_sess_timeouts(ssl_ctx
));
2325 BIO_printf(bio
, "%4ld callback cache hits\n",
2326 SSL_CTX_sess_cb_hits(ssl_ctx
));
2327 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2328 SSL_CTX_sess_cache_full(ssl_ctx
),
2329 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2332 static long int count_reads_callback(BIO
*bio
, int cmd
, const char *argp
, size_t len
,
2333 int argi
, long argl
, int ret
, size_t *processed
)
2335 unsigned int *p_counter
= (unsigned int *)BIO_get_callback_arg(bio
);
2338 case BIO_CB_READ
: /* No break here */
2340 if (p_counter
!= NULL
)
2348 BIO_set_callback_arg(bio
, (char *)bio_s_out
);
2349 ret
= (int)bio_dump_callback(bio
, cmd
, argp
, len
, argi
, argl
, ret
, processed
);
2350 BIO_set_callback_arg(bio
, (char *)p_counter
);
2356 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
)
2365 struct timeval timeout
;
2366 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
2367 struct timeval
*timeoutp
;
2369 #ifndef OPENSSL_NO_DTLS
2370 # ifndef OPENSSL_NO_SCTP
2371 int isdtls
= (stype
== SOCK_DGRAM
|| prot
== IPPROTO_SCTP
);
2373 int isdtls
= (stype
== SOCK_DGRAM
);
2377 buf
= app_malloc(bufsize
, "server buffer");
2379 if (!BIO_socket_nbio(s
, 1))
2380 ERR_print_errors(bio_err
);
2382 BIO_printf(bio_err
, "Turned on non blocking io\n");
2391 if (s_tlsextdebug
) {
2392 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2393 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2397 && !SSL_set_session_id_context(con
, context
,
2398 strlen((char *)context
))) {
2399 BIO_printf(bio_err
, "Error setting session id context\n");
2404 if (!SSL_clear(con
)) {
2405 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2409 #ifndef OPENSSL_NO_DTLS
2411 # ifndef OPENSSL_NO_SCTP
2412 if (prot
== IPPROTO_SCTP
)
2413 sbio
= BIO_new_dgram_sctp(s
, BIO_NOCLOSE
);
2416 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2418 BIO_printf(bio_err
, "Unable to create BIO\n");
2419 ERR_print_errors(bio_err
);
2423 if (enable_timeouts
) {
2425 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2426 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2429 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2430 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2434 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2435 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2436 DTLS_get_link_min_mtu(con
));
2441 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2442 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2443 BIO_printf(bio_err
, "Failed to set MTU\n");
2449 /* want to do MTU discovery */
2450 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2452 # ifndef OPENSSL_NO_SCTP
2453 if (prot
!= IPPROTO_SCTP
)
2455 /* Turn on cookie exchange. Not necessary for SCTP */
2456 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2459 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2462 BIO_printf(bio_err
, "Unable to create BIO\n");
2463 ERR_print_errors(bio_err
);
2470 test
= BIO_new(BIO_f_nbio_test());
2472 BIO_printf(bio_err
, "Unable to create BIO\n");
2477 sbio
= BIO_push(test
, sbio
);
2480 SSL_set_bio(con
, sbio
, sbio
);
2481 SSL_set_accept_state(con
);
2482 /* SSL_set_fd(con,s); */
2484 BIO_set_callback_ex(SSL_get_rbio(con
), count_reads_callback
);
2486 #ifndef OPENSSL_NO_SSL_TRACE
2488 SSL_set_msg_callback(con
, SSL_trace
);
2491 SSL_set_msg_callback(con
, msg_cb
);
2492 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2495 if (s_tlsextdebug
) {
2496 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2497 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2501 int write_header
= 1, edret
= SSL_READ_EARLY_DATA_ERROR
;
2504 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
2506 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
2507 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
2510 switch (SSL_get_error(con
, 0)) {
2511 case SSL_ERROR_WANT_WRITE
:
2512 case SSL_ERROR_WANT_ASYNC
:
2513 case SSL_ERROR_WANT_READ
:
2514 /* Just keep trying - busy waiting */
2517 BIO_printf(bio_err
, "Error reading early data\n");
2518 ERR_print_errors(bio_err
);
2522 if (readbytes
> 0) {
2524 BIO_printf(bio_s_out
, "Early data received:\n");
2527 raw_write_stdout(buf
, (unsigned int)readbytes
);
2528 (void)BIO_flush(bio_s_out
);
2532 if (SSL_get_early_data_status(con
) == SSL_EARLY_DATA_NOT_SENT
)
2533 BIO_printf(bio_s_out
, "No early data received\n");
2535 BIO_printf(bio_s_out
, "Early data was rejected\n");
2537 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2539 if (SSL_is_init_finished(con
))
2540 print_connection_info(con
);
2543 if (fileno_stdin() > s
)
2544 width
= fileno_stdin() + 1;
2548 int read_from_terminal
;
2549 int read_from_sslcon
;
2551 read_from_terminal
= 0;
2552 read_from_sslcon
= SSL_has_pending(con
)
2553 || (async
&& SSL_waiting_for_async(con
));
2555 if (!read_from_sslcon
) {
2557 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2558 openssl_fdset(fileno_stdin(), &readfds
);
2560 openssl_fdset(s
, &readfds
);
2562 * Note: under VMS with SOCKETSHR the second parameter is
2563 * currently of type (int *) whereas under other systems it is
2564 * (void *) if you don't have a cast it will choke the compiler:
2565 * if you do have a cast then you can either go for (int *) or
2568 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2570 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2571 * only on sockets. As a workaround we timeout the select every
2572 * second and check for any keypress. In a proper Windows
2573 * application we wouldn't do this because it is inefficient.
2576 timeout
.tv_usec
= 0;
2577 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &timeout
);
2578 if (has_stdin_waiting())
2579 read_from_terminal
= 1;
2580 if ((i
< 0) || (!i
&& !read_from_terminal
))
2583 if (SSL_is_dtls(con
) && DTLSv1_get_timeout(con
, &timeout
))
2584 timeoutp
= &timeout
;
2588 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2590 if ((SSL_is_dtls(con
)) && DTLSv1_handle_timeout(con
) > 0)
2591 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2595 if (FD_ISSET(fileno_stdin(), &readfds
))
2596 read_from_terminal
= 1;
2598 if (FD_ISSET(s
, &readfds
))
2599 read_from_sslcon
= 1;
2601 if (read_from_terminal
) {
2605 i
= raw_read_stdin(buf
, bufsize
/ 2);
2607 /* both loops are skipped when i <= 0 */
2608 for (j
= 0; j
< i
; j
++)
2611 for (j
= i
- 1; j
>= 0; j
--) {
2612 buf
[j
+ lf_num
] = buf
[j
];
2613 if (buf
[j
] == '\n') {
2616 buf
[j
+ lf_num
] = '\r';
2619 assert(lf_num
== 0);
2621 i
= raw_read_stdin(buf
, bufsize
);
2624 if (!s_quiet
&& !s_brief
) {
2625 if ((i
<= 0) || (buf
[0] == 'Q')) {
2626 BIO_printf(bio_s_out
, "DONE\n");
2627 (void)BIO_flush(bio_s_out
);
2629 close_accept_socket();
2633 if ((i
<= 0) || (buf
[0] == 'q')) {
2634 BIO_printf(bio_s_out
, "DONE\n");
2635 (void)BIO_flush(bio_s_out
);
2636 if (SSL_version(con
) != DTLS1_VERSION
)
2639 * close_accept_socket(); ret= -11;
2643 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2644 SSL_renegotiate(con
);
2645 i
= SSL_do_handshake(con
);
2646 printf("SSL_do_handshake -> %d\n", i
);
2650 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2652 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2654 SSL_renegotiate(con
);
2655 i
= SSL_do_handshake(con
);
2656 printf("SSL_do_handshake -> %d\n", i
);
2660 if ((buf
[0] == 'K' || buf
[0] == 'k')
2661 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2662 SSL_key_update(con
, buf
[0] == 'K' ?
2663 SSL_KEY_UPDATE_REQUESTED
2664 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2665 i
= SSL_do_handshake(con
);
2666 printf("SSL_do_handshake -> %d\n", i
);
2670 if (buf
[0] == 'c' && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2671 SSL_set_verify(con
, SSL_VERIFY_PEER
, NULL
);
2672 i
= SSL_verify_client_post_handshake(con
);
2674 printf("Failed to initiate request\n");
2675 ERR_print_errors(bio_err
);
2677 i
= SSL_do_handshake(con
);
2678 printf("SSL_do_handshake -> %d\n", i
);
2683 if (buf
[0] == 'P') {
2684 static const char str
[] = "Lets print some clear text\n";
2685 BIO_write(SSL_get_wbio(con
), str
, sizeof(str
) -1);
2687 if (buf
[0] == 'S') {
2688 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2691 #ifdef CHARSET_EBCDIC
2692 ebcdic2ascii(buf
, buf
, i
);
2696 /* should do a select for the write */
2699 if (++count
== 100) {
2701 SSL_renegotiate(con
);
2704 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2705 #ifndef OPENSSL_NO_SRP
2706 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2707 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2709 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2711 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2714 switch (SSL_get_error(con
, k
)) {
2715 case SSL_ERROR_NONE
:
2717 case SSL_ERROR_WANT_ASYNC
:
2718 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2719 (void)BIO_flush(bio_s_out
);
2720 wait_for_async(con
);
2722 case SSL_ERROR_WANT_WRITE
:
2723 case SSL_ERROR_WANT_READ
:
2724 case SSL_ERROR_WANT_X509_LOOKUP
:
2725 BIO_printf(bio_s_out
, "Write BLOCK\n");
2726 (void)BIO_flush(bio_s_out
);
2728 case SSL_ERROR_WANT_ASYNC_JOB
:
2730 * This shouldn't ever happen in s_server. Treat as an error
2732 case SSL_ERROR_SYSCALL
:
2734 BIO_printf(bio_s_out
, "ERROR\n");
2735 (void)BIO_flush(bio_s_out
);
2736 ERR_print_errors(bio_err
);
2740 case SSL_ERROR_ZERO_RETURN
:
2741 BIO_printf(bio_s_out
, "DONE\n");
2742 (void)BIO_flush(bio_s_out
);
2754 if (read_from_sslcon
) {
2756 * init_ssl_connection handles all async events itself so if we're
2757 * waiting for async then we shouldn't go back into
2758 * init_ssl_connection
2760 if ((!async
|| !SSL_waiting_for_async(con
))
2761 && !SSL_is_init_finished(con
)) {
2763 * Count number of reads during init_ssl_connection.
2764 * It helps us to distinguish configuration errors from errors
2765 * caused by a client.
2767 unsigned int read_counter
= 0;
2769 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)&read_counter
);
2770 i
= init_ssl_connection(con
);
2771 BIO_set_callback_arg(SSL_get_rbio(con
), NULL
);
2774 * If initialization fails without reads, then
2775 * there was a fatal error in configuration.
2777 if (i
<= 0 && read_counter
== 0) {
2784 } else if (i
== 0) {
2790 i
= SSL_read(con
, (char *)buf
, bufsize
);
2791 #ifndef OPENSSL_NO_SRP
2792 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2793 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2795 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2797 i
= SSL_read(con
, (char *)buf
, bufsize
);
2800 switch (SSL_get_error(con
, i
)) {
2801 case SSL_ERROR_NONE
:
2802 #ifdef CHARSET_EBCDIC
2803 ascii2ebcdic(buf
, buf
, i
);
2805 raw_write_stdout(buf
, (unsigned int)i
);
2806 (void)BIO_flush(bio_s_out
);
2807 if (SSL_has_pending(con
))
2810 case SSL_ERROR_WANT_ASYNC
:
2811 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2812 (void)BIO_flush(bio_s_out
);
2813 wait_for_async(con
);
2815 case SSL_ERROR_WANT_WRITE
:
2816 case SSL_ERROR_WANT_READ
:
2817 BIO_printf(bio_s_out
, "Read BLOCK\n");
2818 (void)BIO_flush(bio_s_out
);
2820 case SSL_ERROR_WANT_ASYNC_JOB
:
2822 * This shouldn't ever happen in s_server. Treat as an error
2824 case SSL_ERROR_SYSCALL
:
2826 BIO_printf(bio_s_out
, "ERROR\n");
2827 (void)BIO_flush(bio_s_out
);
2828 ERR_print_errors(bio_err
);
2831 case SSL_ERROR_ZERO_RETURN
:
2832 BIO_printf(bio_s_out
, "DONE\n");
2833 (void)BIO_flush(bio_s_out
);
2842 BIO_printf(bio_s_out
, "shutting down SSL\n");
2843 do_ssl_shutdown(con
);
2846 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2847 OPENSSL_clear_free(buf
, bufsize
);
2851 static void close_accept_socket(void)
2853 BIO_printf(bio_err
, "shutdown accept socket\n");
2854 if (accept_socket
>= 0) {
2855 BIO_closesocket(accept_socket
);
2859 static int is_retryable(SSL
*con
, int i
)
2861 int err
= SSL_get_error(con
, i
);
2863 /* If it's not a fatal error, it must be retryable */
2864 return (err
!= SSL_ERROR_SSL
)
2865 && (err
!= SSL_ERROR_SYSCALL
)
2866 && (err
!= SSL_ERROR_ZERO_RETURN
);
2869 static int init_ssl_connection(SSL
*con
)
2875 if (dtlslisten
|| stateless
) {
2876 BIO_ADDR
*client
= NULL
;
2879 if ((client
= BIO_ADDR_new()) == NULL
) {
2880 BIO_printf(bio_err
, "ERROR - memory\n");
2883 i
= DTLSv1_listen(con
, client
);
2885 i
= SSL_stateless(con
);
2892 wbio
= SSL_get_wbio(con
);
2894 BIO_get_fd(wbio
, &fd
);
2897 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2898 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2899 BIO_ADDR_free(client
);
2903 (void)BIO_ctrl_set_connected(wbio
, client
);
2904 BIO_ADDR_free(client
);
2909 i
= SSL_accept(con
);
2911 BIO_ADDR_free(client
);
2915 i
= SSL_accept(con
);
2918 retry
= is_retryable(con
, i
);
2919 #ifdef CERT_CB_TEST_RETRY
2922 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2923 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2925 "LOOKUP from certificate callback during accept\n");
2926 i
= SSL_accept(con
);
2928 retry
= is_retryable(con
, i
);
2933 #ifndef OPENSSL_NO_SRP
2935 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2936 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
2937 srp_callback_parm
.login
);
2939 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2941 i
= SSL_accept(con
);
2943 retry
= is_retryable(con
, i
);
2946 } while (i
< 0 && SSL_waiting_for_async(con
));
2950 if (((dtlslisten
|| stateless
) && i
== 0)
2951 || (!dtlslisten
&& !stateless
&& retry
)) {
2952 BIO_printf(bio_s_out
, "DELAY\n");
2956 BIO_printf(bio_err
, "ERROR\n");
2958 verify_err
= SSL_get_verify_result(con
);
2959 if (verify_err
!= X509_V_OK
) {
2960 BIO_printf(bio_err
, "verify error:%s\n",
2961 X509_verify_cert_error_string(verify_err
));
2963 /* Always print any error messages */
2964 ERR_print_errors(bio_err
);
2968 print_connection_info(con
);
2972 static void print_connection_info(SSL
*con
)
2977 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2978 const unsigned char *next_proto_neg
;
2979 unsigned next_proto_neg_len
;
2981 unsigned char *exportedkeymat
;
2985 print_ssl_summary(con
);
2987 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
2989 peer
= SSL_get0_peer_certificate(con
);
2991 BIO_printf(bio_s_out
, "Client certificate\n");
2992 PEM_write_bio_X509(bio_s_out
, peer
);
2993 dump_cert_text(bio_s_out
, peer
);
2997 if (SSL_get_shared_ciphers(con
, buf
, sizeof(buf
)) != NULL
)
2998 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
2999 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
3000 ssl_print_sigalgs(bio_s_out
, con
);
3001 #ifndef OPENSSL_NO_EC
3002 ssl_print_point_formats(bio_s_out
, con
);
3003 ssl_print_groups(bio_s_out
, con
, 0);
3005 print_ca_names(bio_s_out
, con
);
3006 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
3008 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3009 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
3010 if (next_proto_neg
) {
3011 BIO_printf(bio_s_out
, "NEXTPROTO is ");
3012 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
3013 BIO_printf(bio_s_out
, "\n");
3016 #ifndef OPENSSL_NO_SRTP
3018 SRTP_PROTECTION_PROFILE
*srtp_profile
3019 = SSL_get_selected_srtp_profile(con
);
3022 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
3023 srtp_profile
->name
);
3026 if (SSL_session_reused(con
))
3027 BIO_printf(bio_s_out
, "Reused session-id\n");
3029 ssl_print_secure_renegotiation_notes(bio_s_out
, con
);
3031 if ((SSL_get_options(con
) & SSL_OP_NO_RENEGOTIATION
))
3032 BIO_printf(bio_s_out
, "Renegotiation is DISABLED\n");
3034 if (keymatexportlabel
!= NULL
) {
3035 BIO_printf(bio_s_out
, "Keying material exporter:\n");
3036 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
3037 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
3038 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
3039 if (SSL_export_keying_material(con
, exportedkeymat
,
3042 strlen(keymatexportlabel
),
3044 BIO_printf(bio_s_out
, " Error\n");
3046 BIO_printf(bio_s_out
, " Keying material: ");
3047 for (i
= 0; i
< keymatexportlen
; i
++)
3048 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
3049 BIO_printf(bio_s_out
, "\n");
3051 OPENSSL_free(exportedkeymat
);
3053 #ifndef OPENSSL_NO_KTLS
3054 if (BIO_get_ktls_send(SSL_get_wbio(con
)))
3055 BIO_printf(bio_err
, "Using Kernel TLS for sending\n");
3056 if (BIO_get_ktls_recv(SSL_get_rbio(con
)))
3057 BIO_printf(bio_err
, "Using Kernel TLS for receiving\n");
3060 (void)BIO_flush(bio_s_out
);
3063 static int www_body(int s
, int stype
, int prot
, unsigned char *context
)
3065 char *buf
= NULL
, *p
;
3069 const SSL_CIPHER
*c
;
3070 BIO
*io
, *ssl_bio
, *sbio
;
3072 int total_bytes
= 0;
3075 #ifndef OPENSSL_NO_KTLS
3076 int use_sendfile_for_req
= use_sendfile
;
3080 #ifdef CHARSET_EBCDIC
3084 /* Set width for a select call if needed */
3087 /* as we use BIO_gets(), and it always null terminates data, we need
3088 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3089 p
= buf
= app_malloc(bufsize
+ 1, "server www buffer");
3090 io
= BIO_new(BIO_f_buffer());
3091 ssl_bio
= BIO_new(BIO_f_ssl());
3092 if ((io
== NULL
) || (ssl_bio
== NULL
))
3096 if (!BIO_socket_nbio(s
, 1))
3097 ERR_print_errors(bio_err
);
3099 BIO_printf(bio_err
, "Turned on non blocking io\n");
3102 /* lets make the output buffer a reasonable size */
3103 if (!BIO_set_write_buffer_size(io
, bufsize
))
3106 if ((con
= SSL_new(ctx
)) == NULL
)
3109 if (s_tlsextdebug
) {
3110 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3111 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3115 && !SSL_set_session_id_context(con
, context
,
3116 strlen((char *)context
))) {
3121 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3130 test
= BIO_new(BIO_f_nbio_test());
3137 sbio
= BIO_push(test
, sbio
);
3139 SSL_set_bio(con
, sbio
, sbio
);
3140 SSL_set_accept_state(con
);
3142 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3143 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3144 BIO_push(io
, ssl_bio
);
3146 #ifdef CHARSET_EBCDIC
3147 filter
= BIO_new(BIO_f_ebcdic_filter());
3151 io
= BIO_push(filter
, io
);
3155 BIO_set_callback_ex(SSL_get_rbio(con
), bio_dump_callback
);
3156 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3159 #ifndef OPENSSL_NO_SSL_TRACE
3161 SSL_set_msg_callback(con
, SSL_trace
);
3164 SSL_set_msg_callback(con
, msg_cb
);
3165 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3169 i
= BIO_gets(io
, buf
, bufsize
+ 1);
3170 if (i
< 0) { /* error */
3171 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
3173 ERR_print_errors(bio_err
);
3176 BIO_printf(bio_s_out
, "read R BLOCK\n");
3177 #ifndef OPENSSL_NO_SRP
3178 if (BIO_should_io_special(io
)
3179 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3180 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3182 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3190 } else if (i
== 0) { /* end of input */
3195 /* else we have data */
3196 if ((www
== 1 && HAS_PREFIX(buf
, "GET "))
3197 || (www
== 2 && HAS_PREFIX(buf
, "GET /stats "))) {
3199 STACK_OF(SSL_CIPHER
) *sk
;
3200 static const char *space
= " ";
3202 if (www
== 1 && HAS_PREFIX(buf
, "GET /reneg")) {
3203 if (HAS_PREFIX(buf
, "GET /renegcert"))
3205 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
3207 i
= SSL_renegotiate(con
);
3208 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
3209 /* Send the HelloRequest */
3210 i
= SSL_do_handshake(con
);
3212 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
3213 SSL_get_error(con
, i
));
3214 ERR_print_errors(bio_err
);
3217 /* Wait for a ClientHello to come back */
3219 openssl_fdset(s
, &readfds
);
3220 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
3221 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
3222 BIO_printf(bio_s_out
,
3223 "Error waiting for client response\n");
3224 ERR_print_errors(bio_err
);
3228 * We're not actually expecting any data here and we ignore
3229 * any that is sent. This is just to force the handshake that
3230 * we're expecting to come from the client. If they haven't
3231 * sent one there's not much we can do.
3233 BIO_gets(io
, buf
, bufsize
+ 1);
3237 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3238 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
3239 BIO_puts(io
, "<pre>\n");
3240 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
3242 for (i
= 0; i
< local_argc
; i
++) {
3245 for (myp
= local_argv
[i
]; *myp
; myp
++)
3248 BIO_puts(io
, "<");
3251 BIO_puts(io
, ">");
3254 BIO_puts(io
, "&");
3257 BIO_write(io
, myp
, 1);
3260 BIO_write(io
, " ", 1);
3264 ssl_print_secure_renegotiation_notes(io
, con
);
3267 * The following is evil and should not really be done
3269 BIO_printf(io
, "Ciphers supported in s_server binary\n");
3270 sk
= SSL_get_ciphers(con
);
3271 j
= sk_SSL_CIPHER_num(sk
);
3272 for (i
= 0; i
< j
; i
++) {
3273 c
= sk_SSL_CIPHER_value(sk
, i
);
3274 BIO_printf(io
, "%-11s:%-25s ",
3275 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3276 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
3280 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
3283 "---\nCiphers common between both SSL end points:\n");
3287 BIO_write(io
, space
, 26 - j
);
3290 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
3292 BIO_write(io
, p
, 1);
3299 ssl_print_sigalgs(io
, con
);
3300 #ifndef OPENSSL_NO_EC
3301 ssl_print_groups(io
, con
, 0);
3303 print_ca_names(io
, con
);
3304 BIO_printf(io
, (SSL_session_reused(con
)
3305 ? "---\nReused, " : "---\nNew, "));
3306 c
= SSL_get_current_cipher(con
);
3307 BIO_printf(io
, "%s, Cipher is %s\n",
3308 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3309 SSL_SESSION_print(io
, SSL_get_session(con
));
3310 BIO_printf(io
, "---\n");
3311 print_stats(io
, SSL_get_SSL_CTX(con
));
3312 BIO_printf(io
, "---\n");
3313 peer
= SSL_get0_peer_certificate(con
);
3315 BIO_printf(io
, "Client certificate\n");
3316 X509_print(io
, peer
);
3317 PEM_write_bio_X509(io
, peer
);
3320 BIO_puts(io
, "no client certificate available\n");
3322 BIO_puts(io
, "</pre></BODY></HTML>\r\n\r\n");
3324 } else if ((www
== 2 || www
== 3) && CHECK_AND_SKIP_PREFIX(p
, "GET /")) {
3327 static const char *text
=
3328 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3331 for (e
= p
; *e
!= '\0'; e
++) {
3336 /* Windows drive. We treat this the same way as ".." */
3343 dot
= (e
[0] == '.') ? 2 : 0;
3346 dot
= (e
[0] == '.') ? 3 : 0;
3349 dot
= (e
[0] == '/' || e
[0] == '\\') ? -1 : 0;
3353 dot
= (e
[0] == '/' || e
[0] == '\\') ? 1 : 0;
3355 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3360 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3367 BIO_printf(io
, "'%s' contains '..' or ':'\r\n", p
);
3371 if (*p
== '/' || *p
== '\\') {
3373 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3377 /* if a directory, do the index thang */
3378 if (app_isdir(p
) > 0) {
3380 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3384 opmode
= (http_server_binmode
== 1) ? "rb" : "r";
3385 if ((file
= BIO_new_file(p
, opmode
)) == NULL
) {
3387 BIO_printf(io
, "Error opening '%s' mode='%s'\r\n", p
, opmode
);
3388 ERR_print_errors(io
);
3393 BIO_printf(bio_err
, "FILE:%s\n", p
);
3397 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3398 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3399 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3401 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3404 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3407 #ifndef OPENSSL_NO_KTLS
3408 if (use_sendfile_for_req
&& !BIO_get_ktls_send(SSL_get_wbio(con
))) {
3409 BIO_printf(bio_err
, "Warning: sendfile requested but KTLS is not available\n");
3410 use_sendfile_for_req
= 0;
3412 if (use_sendfile_for_req
) {
3419 BIO_get_fp(file
, &fp
);
3421 if (fstat(fd
, &st
) < 0) {
3422 BIO_printf(io
, "Error fstat '%s'\r\n", p
);
3423 ERR_print_errors(io
);
3427 filesize
= st
.st_size
;
3428 if (((int)BIO_flush(io
)) < 0)
3432 i
= SSL_sendfile(con
, fd
, offset
, filesize
, 0);
3434 BIO_printf(io
, "Error SSL_sendfile '%s'\r\n", p
);
3435 ERR_print_errors(io
);
3442 if (filesize
<= 0) {
3444 BIO_printf(bio_err
, "KTLS SENDFILE '%s' OK\n", p
);
3453 i
= BIO_read(file
, buf
, bufsize
);
3459 BIO_printf(bio_err
, "%d\n", i
);
3460 if (total_bytes
> 3 * 1024) {
3462 BIO_printf(bio_err
, "RENEGOTIATE\n");
3463 SSL_renegotiate(con
);
3467 for (j
= 0; j
< i
;) {
3471 SSL_renegotiate(con
);
3473 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3475 if (!BIO_should_retry(io
)
3476 && !SSL_waiting_for_async(con
)) {
3479 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3494 i
= (int)BIO_flush(io
);
3496 if (!BIO_should_retry(io
))
3502 /* make sure we re-use sessions */
3503 do_ssl_shutdown(con
);
3512 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
)
3518 BIO
*io
, *ssl_bio
, *sbio
;
3519 #ifdef CHARSET_EBCDIC
3523 /* as we use BIO_gets(), and it always null terminates data, we need
3524 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3525 buf
= app_malloc(bufsize
+ 1, "server rev buffer");
3526 io
= BIO_new(BIO_f_buffer());
3527 ssl_bio
= BIO_new(BIO_f_ssl());
3528 if ((io
== NULL
) || (ssl_bio
== NULL
))
3531 /* lets make the output buffer a reasonable size */
3532 if (!BIO_set_write_buffer_size(io
, bufsize
))
3535 if ((con
= SSL_new(ctx
)) == NULL
)
3538 if (s_tlsextdebug
) {
3539 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3540 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3543 && !SSL_set_session_id_context(con
, context
,
3544 strlen((char *)context
))) {
3546 ERR_print_errors(bio_err
);
3550 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3553 ERR_print_errors(bio_err
);
3557 SSL_set_bio(con
, sbio
, sbio
);
3558 SSL_set_accept_state(con
);
3560 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3561 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3562 BIO_push(io
, ssl_bio
);
3564 #ifdef CHARSET_EBCDIC
3565 filter
= BIO_new(BIO_f_ebcdic_filter());
3569 io
= BIO_push(filter
, io
);
3573 BIO_set_callback_ex(SSL_get_rbio(con
), bio_dump_callback
);
3574 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3577 #ifndef OPENSSL_NO_SSL_TRACE
3579 SSL_set_msg_callback(con
, SSL_trace
);
3582 SSL_set_msg_callback(con
, msg_cb
);
3583 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3587 i
= BIO_do_handshake(io
);
3590 if (!BIO_should_retry(io
)) {
3591 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3592 ERR_print_errors(bio_err
);
3595 #ifndef OPENSSL_NO_SRP
3596 if (BIO_should_io_special(io
)
3597 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3598 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3600 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3606 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3607 print_ssl_summary(con
);
3610 i
= BIO_gets(io
, buf
, bufsize
+ 1);
3611 if (i
< 0) { /* error */
3612 if (!BIO_should_retry(io
)) {
3614 ERR_print_errors(bio_err
);
3617 BIO_printf(bio_s_out
, "read R BLOCK\n");
3618 #ifndef OPENSSL_NO_SRP
3619 if (BIO_should_io_special(io
)
3620 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3621 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3623 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3631 } else if (i
== 0) { /* end of input */
3633 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3636 char *p
= buf
+ i
- 1;
3637 while (i
&& (*p
== '\n' || *p
== '\r')) {
3641 if (!s_ign_eof
&& i
== 5 && HAS_PREFIX(buf
, "CLOSE")) {
3643 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3646 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3648 BIO_write(io
, buf
, i
+ 1);
3653 if (!BIO_should_retry(io
))
3659 /* make sure we re-use sessions */
3660 do_ssl_shutdown(con
);
3670 #define MAX_SESSION_ID_ATTEMPTS 10
3671 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
3672 unsigned int *id_len
)
3674 unsigned int count
= 0;
3675 unsigned int session_id_prefix_len
= strlen(session_id_prefix
);
3678 if (RAND_bytes(id
, *id_len
) <= 0)
3681 * Prefix the session_id with the required prefix. NB: If our prefix
3682 * is too long, clip it - but there will be worse effects anyway, eg.
3683 * the server could only possibly create 1 session ID (ie. the
3684 * prefix!) so all future session negotiations will fail due to
3687 memcpy(id
, session_id_prefix
,
3688 (session_id_prefix_len
< *id_len
) ?
3689 session_id_prefix_len
: *id_len
);
3691 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3692 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3693 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3699 * By default s_server uses an in-memory cache which caches SSL_SESSION
3700 * structures without any serialization. This hides some bugs which only
3701 * become apparent in deployed servers. By implementing a basic external
3702 * session cache some issues can be debugged using s_server.
3705 typedef struct simple_ssl_session_st
{
3710 struct simple_ssl_session_st
*next
;
3711 } simple_ssl_session
;
3713 static simple_ssl_session
*first
= NULL
;
3715 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3717 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3720 SSL_SESSION_get_id(session
, &sess
->idlen
);
3721 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3722 if (sess
->derlen
< 0) {
3723 BIO_printf(bio_err
, "Error encoding session\n");
3728 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3729 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3731 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3732 OPENSSL_free(sess
->id
);
3733 OPENSSL_free(sess
->der
);
3739 /* Assume it still works. */
3740 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3741 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3742 OPENSSL_free(sess
->id
);
3743 OPENSSL_free(sess
->der
);
3750 BIO_printf(bio_err
, "New session added to external cache\n");
3754 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3757 simple_ssl_session
*sess
;
3759 for (sess
= first
; sess
; sess
= sess
->next
) {
3760 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3761 const unsigned char *p
= sess
->der
;
3762 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3763 return d2i_SSL_SESSION(NULL
, &p
, sess
->derlen
);
3766 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3770 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3772 simple_ssl_session
*sess
, *prev
= NULL
;
3773 const unsigned char *id
;
3775 id
= SSL_SESSION_get_id(session
, &idlen
);
3776 for (sess
= first
; sess
; sess
= sess
->next
) {
3777 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3779 prev
->next
= sess
->next
;
3782 OPENSSL_free(sess
->id
);
3783 OPENSSL_free(sess
->der
);
3791 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3793 SSL_CTX_set_session_cache_mode(sctx
,
3794 SSL_SESS_CACHE_NO_INTERNAL
|
3795 SSL_SESS_CACHE_SERVER
);
3796 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3797 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3798 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3801 static void free_sessions(void)
3803 simple_ssl_session
*sess
, *tsess
;
3804 for (sess
= first
; sess
;) {
3805 OPENSSL_free(sess
->id
);
3806 OPENSSL_free(sess
->der
);
3809 OPENSSL_free(tsess
);
3814 #endif /* OPENSSL_NO_SOCK */