2 * Copyright 1995-2025 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
12 #include "internal/e_os.h"
19 /* Included before async.h to avoid some warnings */
23 #include <openssl/e_os2.h>
24 #include <openssl/async.h>
25 #include <openssl/ssl.h>
26 #include <openssl/decoder.h>
27 #include "internal/sockets.h" /* for openssl_fdset() */
29 #ifndef OPENSSL_NO_SOCK
32 * With IPv6, it looks like Digital has mixed up the proper order of
33 * recursive header file inclusion, resulting in the compiler complaining
34 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
35 * needed to have fileno() declared correctly... So let's define u_int
37 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
39 typedef unsigned int u_int
;
42 #include <openssl/bn.h>
45 #include <openssl/err.h>
46 #include <openssl/pem.h>
47 #include <openssl/x509.h>
48 #include <openssl/rand.h>
49 #include <openssl/ocsp.h>
51 # include <openssl/dh.h>
53 #include <openssl/rsa.h>
57 #include <openssl/ebcdic.h>
59 #include "internal/sockets.h"
61 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
);
62 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
);
63 static int www_body(int s
, int stype
, int prot
, unsigned char *context
);
64 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
);
65 static void close_accept_socket(void);
66 static int init_ssl_connection(SSL
*s
);
67 static void print_stats(BIO
*bp
, SSL_CTX
*ctx
);
68 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
69 unsigned int *id_len
);
70 static void init_session_cache_ctx(SSL_CTX
*sctx
);
71 static void free_sessions(void);
72 static void print_connection_info(SSL
*con
);
74 static const int bufsize
= 16 * 1024;
75 static int accept_socket
= -1;
77 #define TEST_CERT "server.pem"
78 #define TEST_CERT2 "server2.pem"
80 static int s_nbio
= 0;
81 static int s_nbio_test
= 0;
82 static int s_crlf
= 0;
83 static SSL_CTX
*ctx
= NULL
;
84 static SSL_CTX
*ctx2
= NULL
;
87 static BIO
*bio_s_out
= NULL
;
88 static BIO
*bio_s_msg
= NULL
;
89 static int s_debug
= 0;
90 static int s_tlsextdebug
= 0;
92 static int s_quiet
= 0;
93 static int s_ign_eof
= 0;
94 static int s_brief
= 0;
96 static char *keymatexportlabel
= NULL
;
97 static int keymatexportlen
= 20;
101 static int use_sendfile
= 0;
102 static int use_zc_sendfile
= 0;
104 static const char *session_id_prefix
= NULL
;
106 static const unsigned char cert_type_rpk
[] = { TLSEXT_cert_type_rpk
, TLSEXT_cert_type_x509
};
107 static int enable_client_rpk
= 0;
109 #ifndef OPENSSL_NO_DTLS
110 static int enable_timeouts
= 0;
111 static long socket_mtu
;
115 * We define this but make it always be 0 in no-dtls builds to simplify the
118 static int dtlslisten
= 0;
119 static int stateless
= 0;
121 static int early_data
= 0;
122 static SSL_SESSION
*psksess
= NULL
;
124 static char *psk_identity
= "Client_identity";
125 char *psk_key
= NULL
; /* by default PSK is not used */
127 static char http_server_binmode
= 0; /* for now: 0/1 = default/binary */
129 #ifndef OPENSSL_NO_PSK
130 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
132 unsigned int max_psk_len
)
138 BIO_printf(bio_s_out
, "psk_server_cb\n");
140 if (!SSL_is_dtls(ssl
) && SSL_version(ssl
) >= TLS1_3_VERSION
) {
142 * This callback is designed for use in (D)TLSv1.2 (or below). It is
143 * possible to use a single callback for all protocol versions - but it
144 * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we
145 * have psk_find_session_cb.
150 if (identity
== NULL
) {
151 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
155 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
156 (int)strlen(identity
), identity
);
158 /* here we could lookup the given identity e.g. from a database */
159 if (strcmp(identity
, psk_identity
) != 0) {
160 BIO_printf(bio_s_out
, "PSK warning: client identity not what we expected"
161 " (got '%s' expected '%s')\n", identity
, psk_identity
);
164 BIO_printf(bio_s_out
, "PSK client identity found\n");
167 /* convert the PSK key to binary */
168 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
170 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
174 if (key_len
> (int)max_psk_len
) {
176 "psk buffer of callback is too small (%d) for key (%ld)\n",
177 max_psk_len
, key_len
);
182 memcpy(psk
, key
, key_len
);
186 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
190 BIO_printf(bio_err
, "Error in PSK server callback\n");
191 (void)BIO_flush(bio_err
);
192 (void)BIO_flush(bio_s_out
);
197 static int psk_find_session_cb(SSL
*ssl
, const unsigned char *identity
,
198 size_t identity_len
, SSL_SESSION
**sess
)
200 SSL_SESSION
*tmpsess
= NULL
;
203 const SSL_CIPHER
*cipher
= NULL
;
205 if (strlen(psk_identity
) != identity_len
206 || memcmp(psk_identity
, identity
, identity_len
) != 0) {
211 if (psksess
!= NULL
) {
212 if (!SSL_SESSION_up_ref(psksess
))
219 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
221 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
226 /* We default to SHA256 */
227 cipher
= SSL_CIPHER_find(ssl
, tls13_aes128gcmsha256_id
);
228 if (cipher
== NULL
) {
229 BIO_printf(bio_err
, "Error finding suitable ciphersuite\n");
234 tmpsess
= SSL_SESSION_new();
236 || !SSL_SESSION_set1_master_key(tmpsess
, key
, key_len
)
237 || !SSL_SESSION_set_cipher(tmpsess
, cipher
)
238 || !SSL_SESSION_set_protocol_version(tmpsess
, SSL_version(ssl
))) {
240 SSL_SESSION_free(tmpsess
);
249 #ifndef OPENSSL_NO_SRP
250 static srpsrvparm srp_callback_parm
;
253 static int local_argc
= 0;
254 static char **local_argv
;
256 #ifdef CHARSET_EBCDIC
257 static int ebcdic_new(BIO
*bi
);
258 static int ebcdic_free(BIO
*a
);
259 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
260 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
261 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
262 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
263 static int ebcdic_puts(BIO
*bp
, const char *str
);
265 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
266 static BIO_METHOD
*methods_ebcdic
= NULL
;
268 /* This struct is "unwarranted chumminess with the compiler." */
274 static const BIO_METHOD
*BIO_f_ebcdic_filter(void)
276 if (methods_ebcdic
== NULL
) {
277 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
278 "EBCDIC/ASCII filter");
279 if (methods_ebcdic
== NULL
280 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
281 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
282 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
283 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
284 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
285 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
286 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
289 return methods_ebcdic
;
292 static int ebcdic_new(BIO
*bi
)
294 EBCDIC_OUTBUFF
*wbuf
;
296 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
297 wbuf
->alloced
= 1024;
298 wbuf
->buff
[0] = '\0';
300 BIO_set_data(bi
, wbuf
);
305 static int ebcdic_free(BIO
*a
)
307 EBCDIC_OUTBUFF
*wbuf
;
311 wbuf
= BIO_get_data(a
);
313 BIO_set_data(a
, NULL
);
319 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
322 BIO
*next
= BIO_next(b
);
324 if (out
== NULL
|| outl
== 0)
329 ret
= BIO_read(next
, out
, outl
);
331 ascii2ebcdic(out
, out
, ret
);
335 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
337 EBCDIC_OUTBUFF
*wbuf
;
338 BIO
*next
= BIO_next(b
);
342 if ((in
== NULL
) || (inl
<= 0))
347 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
349 if (inl
> (num
= wbuf
->alloced
)) {
350 num
= num
+ num
; /* double the size */
354 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
357 wbuf
->buff
[0] = '\0';
359 BIO_set_data(b
, wbuf
);
362 ebcdic2ascii(wbuf
->buff
, in
, inl
);
364 ret
= BIO_write(next
, wbuf
->buff
, inl
);
369 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
372 BIO
*next
= BIO_next(b
);
381 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
387 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
390 BIO
*next
= BIO_next(bp
);
394 /* return(BIO_gets(bp->next_bio,buf,size));*/
395 for (i
= 0; i
< size
- 1; ++i
) {
396 ret
= ebcdic_read(bp
, &buf
[i
], 1);
399 else if (buf
[i
] == '\n') {
406 return (ret
< 0 && i
== 0) ? ret
: i
;
409 static int ebcdic_puts(BIO
*bp
, const char *str
)
411 if (BIO_next(bp
) == NULL
)
413 return ebcdic_write(bp
, str
, strlen(str
));
417 /* This is a context that we pass to callbacks */
418 typedef struct tlsextctx_st
{
424 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
426 tlsextctx
*p
= (tlsextctx
*) arg
;
427 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
429 if (servername
!= NULL
&& p
->biodebug
!= NULL
) {
430 const char *cp
= servername
;
433 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"");
434 while ((uc
= *cp
++) != 0)
435 BIO_printf(p
->biodebug
,
436 (((uc
) & ~127) == 0) && isprint(uc
) ? "%c" : "\\x%02x", uc
);
437 BIO_printf(p
->biodebug
, "\"\n");
440 if (p
->servername
== NULL
)
441 return SSL_TLSEXT_ERR_NOACK
;
443 if (servername
!= NULL
) {
444 if (OPENSSL_strcasecmp(servername
, p
->servername
))
445 return p
->extension_error
;
447 BIO_printf(p
->biodebug
, "Switching server context.\n");
448 SSL_set_SSL_CTX(s
, ctx2
);
451 return SSL_TLSEXT_ERR_OK
;
454 /* Structure passed to cert status callback */
455 typedef struct tlsextstatusctx_st
{
457 /* File to load OCSP Response from (or NULL if no file) */
459 /* Default responder to use */
460 char *host
, *path
, *port
;
461 char *proxy
, *no_proxy
;
466 static tlsextstatusctx tlscstatp
= { -1 };
468 #ifndef OPENSSL_NO_OCSP
471 * Helper function to get an OCSP_RESPONSE from a responder. This is a
472 * simplified version. It examines certificates each time and makes one OCSP
473 * responder query for each request. A full version would store details such as
474 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
475 * them until they were considered "expired".
477 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
478 OCSP_RESPONSE
**resp
)
480 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
481 char *proxy
= NULL
, *no_proxy
= NULL
;
483 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
484 X509
*x
= NULL
, *cert
;
486 STACK_OF(X509
) *chain
= NULL
;
488 X509_STORE_CTX
*inctx
= NULL
;
490 OCSP_REQUEST
*req
= NULL
;
491 OCSP_CERTID
*id
= NULL
;
492 STACK_OF(X509_EXTENSION
) *exts
;
493 int ret
= SSL_TLSEXT_ERR_NOACK
;
496 /* Build up OCSP query from server certificate */
497 x
= SSL_get_certificate(s
);
498 iname
= X509_get_issuer_name(x
);
499 aia
= X509_get1_ocsp(x
);
501 if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia
, 0), &use_ssl
,
502 NULL
, &host
, &port
, NULL
, &path
, NULL
, NULL
)) {
503 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
507 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
508 sk_OPENSSL_STRING_value(aia
, 0));
510 if (srctx
->host
== NULL
) {
512 "cert_status: no AIA and no default responder URL\n");
518 use_ssl
= srctx
->use_ssl
;
520 proxy
= srctx
->proxy
;
521 no_proxy
= srctx
->no_proxy
;
523 ssl_ctx
= SSL_get_SSL_CTX(s
);
524 if (!SSL_CTX_get0_chain_certs(ssl_ctx
, &chain
))
526 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
527 /* check the untrusted certificate chain (-cert_chain option) */
528 cert
= sk_X509_value(chain
, i
);
529 if (X509_name_cmp(iname
, X509_get_subject_name(cert
)) == 0) {
530 /* the issuer certificate is found */
531 id
= OCSP_cert_to_id(NULL
, x
, cert
);
536 inctx
= X509_STORE_CTX_new();
539 if (!X509_STORE_CTX_init(inctx
, SSL_CTX_get_cert_store(ssl_ctx
),
542 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
, iname
);
544 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
547 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
548 X509_OBJECT_free(obj
);
552 req
= OCSP_REQUEST_new();
555 if (!OCSP_request_add0_id(req
, id
))
558 /* Add any extensions to the request */
559 SSL_get_tlsext_status_exts(s
, &exts
);
560 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
561 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
562 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
565 *resp
= process_responder(req
, host
, port
, path
, proxy
, no_proxy
,
566 use_ssl
, NULL
/* headers */, srctx
->timeout
);
568 BIO_puts(bio_err
, "cert_status: error querying responder\n");
572 ret
= SSL_TLSEXT_ERR_OK
;
576 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
579 * If we parsed aia we need to free; otherwise they were copied and we
586 X509_email_free(aia
);
588 OCSP_CERTID_free(id
);
589 OCSP_REQUEST_free(req
);
590 X509_STORE_CTX_free(inctx
);
595 * Certificate Status callback. This is called when a client includes a
596 * certificate status request extension. The response is either obtained from a
597 * file, or from an OCSP responder.
599 static int cert_status_cb(SSL
*s
, void *arg
)
601 tlsextstatusctx
*srctx
= arg
;
602 OCSP_RESPONSE
*resp
= NULL
;
603 unsigned char *rspder
= NULL
;
605 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
608 BIO_puts(bio_err
, "cert_status: callback called\n");
610 if (srctx
->respin
!= NULL
) {
611 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
612 if (derbio
== NULL
) {
613 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
616 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
619 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
623 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
624 if (ret
!= SSL_TLSEXT_ERR_OK
)
628 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
632 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
633 if (srctx
->verbose
) {
634 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
635 OCSP_RESPONSE_print(bio_err
, resp
, 2);
638 ret
= SSL_TLSEXT_ERR_OK
;
641 if (ret
!= SSL_TLSEXT_ERR_OK
)
642 ERR_print_errors(bio_err
);
644 OCSP_RESPONSE_free(resp
);
650 #ifndef OPENSSL_NO_NEXTPROTONEG
651 /* This is the context that we pass to next_proto_cb */
652 typedef struct tlsextnextprotoctx_st
{
655 } tlsextnextprotoctx
;
657 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
658 unsigned int *len
, void *arg
)
660 tlsextnextprotoctx
*next_proto
= arg
;
662 *data
= next_proto
->data
;
663 *len
= (unsigned int)next_proto
->len
;
665 return SSL_TLSEXT_ERR_OK
;
667 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
669 /* This the context that we pass to alpn_cb */
670 typedef struct tlsextalpnctx_st
{
675 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
676 const unsigned char *in
, unsigned int inlen
, void *arg
)
678 tlsextalpnctx
*alpn_ctx
= arg
;
681 /* We can assume that |in| is syntactically valid. */
683 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
684 for (i
= 0; i
< inlen
;) {
686 BIO_write(bio_s_out
, ", ", 2);
687 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
690 BIO_write(bio_s_out
, "\n", 1);
693 if (SSL_select_next_proto
694 ((unsigned char **)out
, outlen
, alpn_ctx
->data
,
695 (unsigned int)alpn_ctx
->len
, in
, inlen
) != OPENSSL_NPN_NEGOTIATED
) {
696 return SSL_TLSEXT_ERR_ALERT_FATAL
;
700 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
701 BIO_write(bio_s_out
, *out
, *outlen
);
702 BIO_write(bio_s_out
, "\n", 1);
705 return SSL_TLSEXT_ERR_OK
;
708 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
710 /* disable resumption for sessions with forward secure ciphers */
711 return is_forward_secure
;
714 typedef enum OPTION_choice
{
717 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
718 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
719 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
720 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
721 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
722 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
723 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
724 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
726 OPT_CASTORE
, OPT_NOCASTORE
, OPT_CHAINCASTORE
, OPT_VERIFYCASTORE
,
727 OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
728 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
729 OPT_STATUS_TIMEOUT
, OPT_PROXY
, OPT_NO_PROXY
, OPT_STATUS_URL
,
730 OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
731 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
732 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
733 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_IDENTITY
, OPT_PSK_HINT
, OPT_PSK
,
734 OPT_PSK_SESS
, OPT_SRPVFILE
, OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
,
735 OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
, OPT_SSL_CONFIG
,
736 OPT_MAX_SEND_FRAG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
737 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
738 OPT_DTLS1_2
, OPT_SCTP
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
, OPT_STATELESS
,
739 OPT_ID_PREFIX
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
740 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
, OPT_SENDFILE
,
741 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
742 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_RECV_MAX_EARLY
, OPT_EARLY_DATA
,
743 OPT_S_NUM_TICKETS
, OPT_ANTI_REPLAY
, OPT_NO_ANTI_REPLAY
, OPT_SCTP_LABEL_BUG
,
744 OPT_HTTP_SERVER_BINMODE
, OPT_NOCANAMES
, OPT_IGNORE_UNEXPECTED_EOF
, OPT_KTLS
,
746 OPT_TFO
, OPT_CERT_COMP
,
747 OPT_ENABLE_SERVER_RPK
,
748 OPT_ENABLE_CLIENT_RPK
,
756 const OPTIONS s_server_options
[] = {
757 OPT_SECTION("General"),
758 {"help", OPT_HELP
, '-', "Display this summary"},
759 {"ssl_config", OPT_SSL_CONFIG
, 's',
760 "Configure SSL_CTX using the given configuration value"},
761 #ifndef OPENSSL_NO_SSL_TRACE
762 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
764 #ifndef OPENSSL_NO_ENGINE
765 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
768 OPT_SECTION("Network"),
769 {"port", OPT_PORT
, 'p',
770 "TCP/IP port to listen on for connections (default is " PORT
")"},
771 {"accept", OPT_ACCEPT
, 's',
772 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
774 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
775 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
777 {"4", OPT_4
, '-', "Use IPv4 only"},
778 {"6", OPT_6
, '-', "Use IPv6 only"},
779 #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
780 {"tfo", OPT_TFO
, '-', "Listen for TCP Fast Open connections"},
783 OPT_SECTION("Identity"),
784 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
785 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
786 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
787 {"CAstore", OPT_CASTORE
, ':', "URI to store of CA's"},
788 {"no-CAfile", OPT_NOCAFILE
, '-',
789 "Do not load the default certificates file"},
790 {"no-CApath", OPT_NOCAPATH
, '-',
791 "Do not load certificates from the default certificates directory"},
792 {"no-CAstore", OPT_NOCASTORE
, '-',
793 "Do not load certificates from the default certificates store URI"},
794 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
795 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
796 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
797 "Turn on peer certificate verification, must have a cert"},
798 {"nameopt", OPT_NAMEOPT
, 's', "Certificate subject/issuer name printing options"},
799 {"cert", OPT_CERT
, '<', "Server certificate file to use; default " TEST_CERT
},
800 {"cert2", OPT_CERT2
, '<',
801 "Certificate file to use for servername; default " TEST_CERT2
},
802 {"certform", OPT_CERTFORM
, 'F',
803 "Server certificate file format (PEM/DER/P12); has no effect"},
804 {"cert_chain", OPT_CERT_CHAIN
, '<',
805 "Server certificate chain file in PEM format"},
806 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build server certificate chain"},
807 {"serverinfo", OPT_SERVERINFO
, 's',
808 "PEM serverinfo file for certificate"},
809 {"key", OPT_KEY
, 's',
810 "Private key file to use; default is -cert file or else" TEST_CERT
},
811 {"key2", OPT_KEY2
, '<',
812 "-Private Key file to use for servername if not in -cert2"},
813 {"keyform", OPT_KEYFORM
, 'f', "Key format (ENGINE, other values ignored)"},
814 {"pass", OPT_PASS
, 's', "Private key and cert file pass phrase source"},
815 {"dcert", OPT_DCERT
, '<',
816 "Second server certificate file to use (usually for DSA)"},
817 {"dcertform", OPT_DCERTFORM
, 'F',
818 "Second server certificate file format (PEM/DER/P12); has no effect"},
819 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
820 "second server certificate chain file in PEM format"},
821 {"dkey", OPT_DKEY
, '<',
822 "Second private key file to use (usually for DSA)"},
823 {"dkeyform", OPT_DKEYFORM
, 'f',
824 "Second key file format (ENGINE, other values ignored)"},
825 {"dpass", OPT_DPASS
, 's',
826 "Second private key and cert file pass phrase source"},
827 {"dhparam", OPT_DHPARAM
, '<', "DH parameters file to use"},
828 {"servername", OPT_SERVERNAME
, 's',
829 "Servername for HostName TLS extension"},
830 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
831 "On servername mismatch send fatal alert (default warning alert)"},
832 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
833 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
834 {"quiet", OPT_QUIET
, '-', "No server output"},
835 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
836 "Disable caching and tickets if ephemeral (EC)DH is used"},
837 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
838 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
839 {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF
, '-',
840 "Do not treat lack of close_notify from a peer as an error"},
841 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
842 "Hex dump of all TLS extensions received"},
843 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
844 {"id_prefix", OPT_ID_PREFIX
, 's',
845 "Generate SSL/TLS session IDs prefixed by arg"},
846 {"keymatexport", OPT_KEYMATEXPORT
, 's',
847 "Export keying material using label"},
848 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
849 "Export len bytes of keying material; default 20"},
850 {"CRL", OPT_CRL
, '<', "CRL file to use"},
851 {"CRLform", OPT_CRLFORM
, 'F', "CRL file format (PEM or DER); default PEM"},
852 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
853 "Download CRLs from distribution points in certificate CDP entries"},
854 {"chainCAfile", OPT_CHAINCAFILE
, '<',
855 "CA file for certificate chain (PEM format)"},
856 {"chainCApath", OPT_CHAINCAPATH
, '/',
857 "use dir as certificate store path to build CA certificate chain"},
858 {"chainCAstore", OPT_CHAINCASTORE
, ':',
859 "use URI as certificate store to build CA certificate chain"},
860 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
861 "CA file for certificate verification (PEM format)"},
862 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
863 "use dir as certificate store path to verify CA certificate"},
864 {"verifyCAstore", OPT_VERIFYCASTORE
, ':',
865 "use URI as certificate store to verify CA certificate"},
866 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
867 {"ext_cache", OPT_EXT_CACHE
, '-',
868 "Disable internal cache, set up and use external cache"},
869 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
870 "Close connection on verification error"},
871 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
872 "No verify output except verify errors"},
873 {"ign_eof", OPT_IGN_EOF
, '-', "Ignore input EOF (default when -quiet)"},
874 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input EOF"},
875 #ifndef OPENSSL_NO_COMP_ALG
876 {"cert_comp", OPT_CERT_COMP
, '-', "Pre-compress server certificates"},
879 #ifndef OPENSSL_NO_OCSP
881 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
882 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
883 "Print more output in certificate status callback"},
884 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
885 "Status request responder timeout"},
886 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
887 {"proxy", OPT_PROXY
, 's',
888 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
889 {"no_proxy", OPT_NO_PROXY
, 's',
890 "List of addresses of servers not to use HTTP(S) proxy for"},
892 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
893 {"status_file", OPT_STATUS_FILE
, '<',
894 "File containing DER encoded OCSP Response"},
897 OPT_SECTION("Debug"),
898 {"security_debug", OPT_SECURITY_DEBUG
, '-',
899 "Print output from SSL/TLS security framework"},
900 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
901 "Print more output from SSL/TLS security framework"},
902 {"brief", OPT_BRIEF
, '-',
903 "Restrict output to brief summary of connection parameters"},
904 {"rev", OPT_REV
, '-',
905 "act as an echo server that sends back received text reversed"},
906 {"debug", OPT_DEBUG
, '-', "Print more output"},
907 {"msg", OPT_MSG
, '-', "Show protocol messages"},
908 {"msgfile", OPT_MSGFILE
, '>',
909 "File to send output of -msg or -trace, instead of stdout"},
910 {"state", OPT_STATE
, '-', "Print the SSL states"},
911 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
912 {"max_pipelines", OPT_MAX_PIPELINES
, 'p',
913 "Maximum number of encrypt/decrypt pipelines to be used"},
914 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
915 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
917 OPT_SECTION("Network"),
918 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
919 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
920 {"mtu", OPT_MTU
, 'p', "Set link-layer MTU"},
921 {"read_buf", OPT_READ_BUF
, 'p',
922 "Default read buffer size to be used for connections"},
923 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'p',
924 "Size used to split data for encrypt pipelines"},
925 {"max_send_frag", OPT_MAX_SEND_FRAG
, 'p', "Maximum Size of send frames "},
927 OPT_SECTION("Server identity"),
928 {"psk_identity", OPT_PSK_IDENTITY
, 's', "PSK identity to expect"},
929 #ifndef OPENSSL_NO_PSK
930 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
932 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
933 {"psk_session", OPT_PSK_SESS
, '<', "File to read PSK SSL session from"},
934 #ifndef OPENSSL_NO_SRP
935 {"srpvfile", OPT_SRPVFILE
, '<', "(deprecated) The verifier file for SRP"},
936 {"srpuserseed", OPT_SRPUSERSEED
, 's',
937 "(deprecated) A seed string for a default user salt"},
940 OPT_SECTION("Protocol and version"),
941 {"max_early_data", OPT_MAX_EARLY
, 'n',
942 "The maximum number of bytes of early data as advertised in tickets"},
943 {"recv_max_early_data", OPT_RECV_MAX_EARLY
, 'n',
944 "The maximum number of bytes of early data (hard limit)"},
945 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
946 {"num_tickets", OPT_S_NUM_TICKETS
, 'n',
947 "The number of TLSv1.3 session tickets that a server will automatically issue" },
948 {"anti_replay", OPT_ANTI_REPLAY
, '-', "Switch on anti-replay protection (default)"},
949 {"no_anti_replay", OPT_NO_ANTI_REPLAY
, '-', "Switch off anti-replay protection"},
950 {"http_server_binmode", OPT_HTTP_SERVER_BINMODE
, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)"},
951 {"no_ca_names", OPT_NOCANAMES
, '-',
952 "Disable TLS Extension CA Names"},
953 {"stateless", OPT_STATELESS
, '-', "Require TLSv1.3 cookies"},
954 #ifndef OPENSSL_NO_SSL3
955 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
957 #ifndef OPENSSL_NO_TLS1
958 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
960 #ifndef OPENSSL_NO_TLS1_1
961 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
963 #ifndef OPENSSL_NO_TLS1_2
964 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
966 #ifndef OPENSSL_NO_TLS1_3
967 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
969 #ifndef OPENSSL_NO_DTLS
970 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
971 {"listen", OPT_LISTEN
, '-',
972 "Listen for a DTLS ClientHello with a cookie and then connect"},
974 #ifndef OPENSSL_NO_DTLS1
975 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
977 #ifndef OPENSSL_NO_DTLS1_2
978 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
980 #ifndef OPENSSL_NO_SCTP
981 {"sctp", OPT_SCTP
, '-', "Use SCTP"},
982 {"sctp_label_bug", OPT_SCTP_LABEL_BUG
, '-', "Enable SCTP label length bug"},
984 #ifndef OPENSSL_NO_SRTP
985 {"use_srtp", OPT_SRTP_PROFILES
, 's',
986 "Offer SRTP key management with a colon-separated profile list"},
988 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
989 #ifndef OPENSSL_NO_NEXTPROTONEG
990 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
991 "Set the advertised protocols for the NPN extension (comma-separated list)"},
993 {"alpn", OPT_ALPN
, 's',
994 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
995 #ifndef OPENSSL_NO_KTLS
996 {"ktls", OPT_KTLS
, '-', "Enable Kernel TLS for sending and receiving"},
997 {"sendfile", OPT_SENDFILE
, '-', "Use sendfile to response file with -WWW"},
998 {"zerocopy_sendfile", OPT_USE_ZC_SENDFILE
, '-', "Use zerocopy mode of KTLS sendfile"},
1000 {"enable_server_rpk", OPT_ENABLE_SERVER_RPK
, '-', "Enable raw public keys (RFC7250) from the server"},
1001 {"enable_client_rpk", OPT_ENABLE_CLIENT_RPK
, '-', "Enable raw public keys (RFC7250) from the client"},
1010 #define IS_PROT_FLAG(o) \
1011 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
1012 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
1014 int s_server_main(int argc
, char *argv
[])
1016 ENGINE
*engine
= NULL
;
1017 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
1018 SSL_CONF_CTX
*cctx
= NULL
;
1019 const SSL_METHOD
*meth
= TLS_server_method();
1020 SSL_EXCERT
*exc
= NULL
;
1021 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
1022 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
1023 STACK_OF(X509_CRL
) *crls
= NULL
;
1024 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
1025 X509_VERIFY_PARAM
*vpm
= NULL
;
1026 const char *CApath
= NULL
, *CAfile
= NULL
, *CAstore
= NULL
;
1027 const char *chCApath
= NULL
, *chCAfile
= NULL
, *chCAstore
= NULL
;
1028 char *dpassarg
= NULL
, *dpass
= NULL
;
1029 char *passarg
= NULL
, *pass
= NULL
;
1030 char *vfyCApath
= NULL
, *vfyCAfile
= NULL
, *vfyCAstore
= NULL
;
1031 char *crl_file
= NULL
, *prog
;
1033 int unlink_unix_path
= 0;
1035 do_server_cb server_cb
;
1036 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
1037 char *dhfile
= NULL
;
1039 int nocert
= 0, ret
= 1;
1040 int noCApath
= 0, noCAfile
= 0, noCAstore
= 0;
1041 int s_cert_format
= FORMAT_UNDEF
, s_key_format
= FORMAT_UNDEF
;
1042 int s_dcert_format
= FORMAT_UNDEF
, s_dkey_format
= FORMAT_UNDEF
;
1043 int rev
= 0, naccept
= -1, sdebug
= 0;
1044 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
, protocol
= 0;
1045 int state
= 0, crl_format
= FORMAT_UNDEF
, crl_download
= 0;
1048 unsigned char *context
= NULL
;
1050 EVP_PKEY
*s_key2
= NULL
;
1051 X509
*s_cert2
= NULL
;
1052 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
1053 const char *ssl_config
= NULL
;
1054 int read_buf_len
= 0;
1055 #ifndef OPENSSL_NO_NEXTPROTONEG
1056 const char *next_proto_neg_in
= NULL
;
1057 tlsextnextprotoctx next_proto
= { NULL
, 0 };
1059 const char *alpn_in
= NULL
;
1060 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
1061 #ifndef OPENSSL_NO_PSK
1062 /* by default do not send a PSK identity hint */
1063 char *psk_identity_hint
= NULL
;
1066 #ifndef OPENSSL_NO_SRP
1067 char *srpuserseed
= NULL
;
1068 char *srp_verifier_file
= NULL
;
1070 #ifndef OPENSSL_NO_SRTP
1071 char *srtp_profiles
= NULL
;
1073 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
1074 int s_server_verify
= SSL_VERIFY_NONE
;
1075 int s_server_session_id_context
= 1; /* anything will do */
1076 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
1077 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
1078 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
1079 #ifndef OPENSSL_NO_OCSP
1080 int s_tlsextstatus
= 0;
1082 int no_resume_ephemeral
= 0;
1083 unsigned int max_send_fragment
= 0;
1084 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
1085 const char *s_serverinfo_file
= NULL
;
1086 const char *keylog_file
= NULL
;
1087 int max_early_data
= -1, recv_max_early_data
= -1;
1088 char *psksessf
= NULL
;
1089 int no_ca_names
= 0;
1090 #ifndef OPENSSL_NO_SCTP
1091 int sctp_label_bug
= 0;
1093 int ignore_unexpected_eof
= 0;
1094 #ifndef OPENSSL_NO_KTLS
1095 int enable_ktls
= 0;
1099 int enable_server_rpk
= 0;
1101 /* Init of few remaining global variables */
1106 s_nbio
= s_nbio_test
= 0;
1115 use_zc_sendfile
= 0;
1117 port
= OPENSSL_strdup(PORT
);
1118 cctx
= SSL_CONF_CTX_new();
1119 vpm
= X509_VERIFY_PARAM_new();
1120 if (port
== NULL
|| cctx
== NULL
|| vpm
== NULL
)
1122 SSL_CONF_CTX_set_flags(cctx
,
1123 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1125 prog
= opt_init(argc
, argv
, s_server_options
);
1126 while ((o
= opt_next()) != OPT_EOF
) {
1127 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1128 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1131 if (IS_NO_PROT_FLAG(o
))
1133 if (prot_opt
== 1 && no_prot_opt
) {
1135 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1142 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1145 opt_help(s_server_options
);
1151 if (socket_family
== AF_UNIX
) {
1152 OPENSSL_free(host
); host
= NULL
;
1153 OPENSSL_free(port
); port
= NULL
;
1156 socket_family
= AF_INET
;
1162 if (socket_family
== AF_UNIX
) {
1163 OPENSSL_free(host
); host
= NULL
;
1164 OPENSSL_free(port
); port
= NULL
;
1167 socket_family
= AF_INET6
;
1170 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1176 if (socket_family
== AF_UNIX
) {
1177 socket_family
= AF_UNSPEC
;
1180 OPENSSL_free(port
); port
= NULL
;
1181 OPENSSL_free(host
); host
= NULL
;
1182 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1184 "%s: -port argument malformed or ambiguous\n",
1191 if (socket_family
== AF_UNIX
) {
1192 socket_family
= AF_UNSPEC
;
1195 OPENSSL_free(port
); port
= NULL
;
1196 OPENSSL_free(host
); host
= NULL
;
1197 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1199 "%s: -accept argument malformed or ambiguous\n",
1206 socket_family
= AF_UNIX
;
1207 OPENSSL_free(host
); host
= OPENSSL_strdup(opt_arg());
1210 OPENSSL_free(port
); port
= NULL
;
1213 unlink_unix_path
= 1;
1217 naccept
= atol(opt_arg());
1220 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1221 verify_args
.depth
= atoi(opt_arg());
1223 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1225 case OPT_UPPER_V_VERIFY
:
1227 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1228 SSL_VERIFY_CLIENT_ONCE
;
1229 verify_args
.depth
= atoi(opt_arg());
1232 "verify depth is %d, must return a certificate\n",
1236 context
= (unsigned char *)opt_arg();
1239 s_cert_file
= opt_arg();
1242 if (!set_nameopt(opt_arg()))
1246 crl_file
= opt_arg();
1248 case OPT_CRL_DOWNLOAD
:
1251 case OPT_SERVERINFO
:
1252 s_serverinfo_file
= opt_arg();
1255 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_cert_format
))
1259 s_key_file
= opt_arg();
1262 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1266 passarg
= opt_arg();
1268 case OPT_CERT_CHAIN
:
1269 s_chain_file
= opt_arg();
1275 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dcert_format
))
1279 s_dcert_file
= opt_arg();
1282 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dkey_format
))
1286 dpassarg
= opt_arg();
1289 s_dkey_file
= opt_arg();
1291 case OPT_DCERT_CHAIN
:
1292 s_dchain_file
= opt_arg();
1303 case OPT_CHAINCAPATH
:
1304 chCApath
= opt_arg();
1306 case OPT_VERIFYCAPATH
:
1307 vfyCApath
= opt_arg();
1310 CAstore
= opt_arg();
1315 case OPT_CHAINCASTORE
:
1316 chCAstore
= opt_arg();
1318 case OPT_VERIFYCASTORE
:
1319 vfyCAstore
= opt_arg();
1328 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1332 case OPT_S_NUM_TICKETS
:
1333 case OPT_ANTI_REPLAY
:
1334 case OPT_NO_ANTI_REPLAY
:
1335 if (ssl_args
== NULL
)
1336 ssl_args
= sk_OPENSSL_STRING_new_null();
1337 if (ssl_args
== NULL
1338 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1339 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1340 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1345 if (!opt_verify(o
, vpm
))
1350 if (!args_excert(o
, &exc
))
1353 case OPT_VERIFY_RET_ERROR
:
1354 verify_args
.return_error
= 1;
1356 case OPT_VERIFY_QUIET
:
1357 verify_args
.quiet
= 1;
1359 case OPT_BUILD_CHAIN
:
1368 case OPT_CHAINCAFILE
:
1369 chCAfile
= opt_arg();
1371 case OPT_VERIFYCAFILE
:
1372 vfyCAfile
= opt_arg();
1378 s_nbio
= s_nbio_test
= 1;
1383 case OPT_NO_IGN_EOF
:
1389 case OPT_TLSEXTDEBUG
:
1393 #ifndef OPENSSL_NO_OCSP
1397 case OPT_STATUS_VERBOSE
:
1398 #ifndef OPENSSL_NO_OCSP
1399 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1402 case OPT_STATUS_TIMEOUT
:
1403 #ifndef OPENSSL_NO_OCSP
1405 tlscstatp
.timeout
= atoi(opt_arg());
1409 #ifndef OPENSSL_NO_OCSP
1410 tlscstatp
.proxy
= opt_arg();
1414 #ifndef OPENSSL_NO_OCSP
1415 tlscstatp
.no_proxy
= opt_arg();
1418 case OPT_STATUS_URL
:
1419 #ifndef OPENSSL_NO_OCSP
1421 if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp
.use_ssl
, NULL
,
1422 &tlscstatp
.host
, &tlscstatp
.port
, NULL
,
1423 &tlscstatp
.path
, NULL
, NULL
)) {
1424 BIO_printf(bio_err
, "Error parsing -status_url argument\n");
1429 case OPT_STATUS_FILE
:
1430 #ifndef OPENSSL_NO_OCSP
1432 tlscstatp
.respin
= opt_arg();
1439 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1440 if (bio_s_msg
== NULL
) {
1441 BIO_printf(bio_err
, "Error writing file %s\n", opt_arg());
1446 #ifndef OPENSSL_NO_SSL_TRACE
1450 case OPT_SECURITY_DEBUG
:
1453 case OPT_SECURITY_DEBUG_VERBOSE
:
1466 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1471 case OPT_NO_RESUME_EPHEMERAL
:
1472 no_resume_ephemeral
= 1;
1474 case OPT_PSK_IDENTITY
:
1475 psk_identity
= opt_arg();
1478 #ifndef OPENSSL_NO_PSK
1479 psk_identity_hint
= opt_arg();
1483 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1484 if (isxdigit(_UC(*p
)))
1486 BIO_printf(bio_err
, "Not a hex number '%s'\n", psk_key
);
1491 psksessf
= opt_arg();
1494 #ifndef OPENSSL_NO_SRP
1495 srp_verifier_file
= opt_arg();
1496 if (min_version
< TLS1_VERSION
)
1497 min_version
= TLS1_VERSION
;
1500 case OPT_SRPUSERSEED
:
1501 #ifndef OPENSSL_NO_SRP
1502 srpuserseed
= opt_arg();
1503 if (min_version
< TLS1_VERSION
)
1504 min_version
= TLS1_VERSION
;
1519 case OPT_SSL_CONFIG
:
1520 ssl_config
= opt_arg();
1523 min_version
= SSL3_VERSION
;
1524 max_version
= SSL3_VERSION
;
1527 min_version
= TLS1_3_VERSION
;
1528 max_version
= TLS1_3_VERSION
;
1531 min_version
= TLS1_2_VERSION
;
1532 max_version
= TLS1_2_VERSION
;
1535 min_version
= TLS1_1_VERSION
;
1536 max_version
= TLS1_1_VERSION
;
1539 min_version
= TLS1_VERSION
;
1540 max_version
= TLS1_VERSION
;
1543 #ifndef OPENSSL_NO_DTLS
1544 meth
= DTLS_server_method();
1545 socket_type
= SOCK_DGRAM
;
1549 #ifndef OPENSSL_NO_DTLS
1550 meth
= DTLS_server_method();
1551 min_version
= DTLS1_VERSION
;
1552 max_version
= DTLS1_VERSION
;
1553 socket_type
= SOCK_DGRAM
;
1557 #ifndef OPENSSL_NO_DTLS
1558 meth
= DTLS_server_method();
1559 min_version
= DTLS1_2_VERSION
;
1560 max_version
= DTLS1_2_VERSION
;
1561 socket_type
= SOCK_DGRAM
;
1565 #ifndef OPENSSL_NO_SCTP
1566 protocol
= IPPROTO_SCTP
;
1569 case OPT_SCTP_LABEL_BUG
:
1570 #ifndef OPENSSL_NO_SCTP
1575 #ifndef OPENSSL_NO_DTLS
1576 enable_timeouts
= 1;
1580 #ifndef OPENSSL_NO_DTLS
1581 socket_mtu
= atol(opt_arg());
1585 #ifndef OPENSSL_NO_DTLS
1593 session_id_prefix
= opt_arg();
1596 #ifndef OPENSSL_NO_ENGINE
1597 engine
= setup_engine(opt_arg(), s_debug
);
1604 case OPT_PROV_CASES
:
1605 if (!opt_provider(o
))
1608 case OPT_SERVERNAME
:
1609 tlsextcbp
.servername
= opt_arg();
1611 case OPT_SERVERNAME_FATAL
:
1612 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1615 s_cert_file2
= opt_arg();
1618 s_key_file2
= opt_arg();
1620 case OPT_NEXTPROTONEG
:
1621 # ifndef OPENSSL_NO_NEXTPROTONEG
1622 next_proto_neg_in
= opt_arg();
1626 alpn_in
= opt_arg();
1628 case OPT_SRTP_PROFILES
:
1629 #ifndef OPENSSL_NO_SRTP
1630 srtp_profiles
= opt_arg();
1633 case OPT_KEYMATEXPORT
:
1634 keymatexportlabel
= opt_arg();
1636 case OPT_KEYMATEXPORTLEN
:
1637 keymatexportlen
= atoi(opt_arg());
1642 case OPT_MAX_SEND_FRAG
:
1643 max_send_fragment
= atoi(opt_arg());
1645 case OPT_SPLIT_SEND_FRAG
:
1646 split_send_fragment
= atoi(opt_arg());
1648 case OPT_MAX_PIPELINES
:
1649 max_pipelines
= atoi(opt_arg());
1652 read_buf_len
= atoi(opt_arg());
1654 case OPT_KEYLOG_FILE
:
1655 keylog_file
= opt_arg();
1658 max_early_data
= atoi(opt_arg());
1659 if (max_early_data
< 0) {
1660 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1664 case OPT_RECV_MAX_EARLY
:
1665 recv_max_early_data
= atoi(opt_arg());
1666 if (recv_max_early_data
< 0) {
1667 BIO_printf(bio_err
, "Invalid value for recv_max_early_data\n");
1671 case OPT_EARLY_DATA
:
1673 if (max_early_data
== -1)
1674 max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
1676 case OPT_HTTP_SERVER_BINMODE
:
1677 http_server_binmode
= 1;
1683 #ifndef OPENSSL_NO_KTLS
1688 #ifndef OPENSSL_NO_KTLS
1692 case OPT_USE_ZC_SENDFILE
:
1693 #ifndef OPENSSL_NO_KTLS
1694 use_zc_sendfile
= 1;
1697 case OPT_IGNORE_UNEXPECTED_EOF
:
1698 ignore_unexpected_eof
= 1;
1706 case OPT_ENABLE_SERVER_RPK
:
1707 enable_server_rpk
= 1;
1709 case OPT_ENABLE_CLIENT_RPK
:
1710 enable_client_rpk
= 1;
1715 /* No extra arguments. */
1716 if (!opt_check_rest_arg(NULL
))
1719 if (!app_RAND_load())
1722 #ifndef OPENSSL_NO_NEXTPROTONEG
1723 if (min_version
== TLS1_3_VERSION
&& next_proto_neg_in
!= NULL
) {
1724 BIO_printf(bio_err
, "Cannot supply -nextprotoneg with TLSv1.3\n");
1728 #ifndef OPENSSL_NO_DTLS
1729 if (www
&& socket_type
== SOCK_DGRAM
) {
1730 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1734 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1735 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1739 if (rev
&& socket_type
== SOCK_DGRAM
) {
1740 BIO_printf(bio_err
, "Can't use -rev with DTLS\n");
1745 if (tfo
&& socket_type
!= SOCK_STREAM
) {
1746 BIO_printf(bio_err
, "Can only use -tfo with TLS\n");
1750 if (stateless
&& socket_type
!= SOCK_STREAM
) {
1751 BIO_printf(bio_err
, "Can only use --stateless with TLS\n");
1756 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1758 "Can't use unix sockets and datagrams together\n");
1762 if (early_data
&& rev
) {
1764 "Can't use -early_data in combination with -rev\n");
1768 #ifndef OPENSSL_NO_SCTP
1769 if (protocol
== IPPROTO_SCTP
) {
1770 if (socket_type
!= SOCK_DGRAM
) {
1771 BIO_printf(bio_err
, "Can't use -sctp without DTLS\n");
1774 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1775 socket_type
= SOCK_STREAM
;
1779 #ifndef OPENSSL_NO_KTLS
1780 if (use_zc_sendfile
&& !use_sendfile
) {
1781 BIO_printf(bio_out
, "Warning: -zerocopy_sendfile depends on -sendfile, enabling -sendfile now.\n");
1785 if (use_sendfile
&& enable_ktls
== 0) {
1786 BIO_printf(bio_out
, "Warning: -sendfile depends on -ktls, enabling -ktls now.\n");
1790 if (use_sendfile
&& www
<= 1) {
1791 BIO_printf(bio_err
, "Can't use -sendfile without -WWW or -HTTP\n");
1796 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1797 BIO_printf(bio_err
, "Error getting password\n");
1801 if (s_key_file
== NULL
)
1802 s_key_file
= s_cert_file
;
1804 if (s_key_file2
== NULL
)
1805 s_key_file2
= s_cert_file2
;
1807 if (!load_excert(&exc
))
1811 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1812 "server certificate private key");
1816 s_cert
= load_cert_pass(s_cert_file
, s_cert_format
, 1, pass
,
1817 "server certificate");
1821 if (s_chain_file
!= NULL
) {
1822 if (!load_certs(s_chain_file
, 0, &s_chain
, NULL
,
1823 "server certificate chain"))
1827 if (tlsextcbp
.servername
!= NULL
) {
1828 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1829 "second server certificate private key");
1833 s_cert2
= load_cert_pass(s_cert_file2
, s_cert_format
, 1, pass
,
1834 "second server certificate");
1836 if (s_cert2
== NULL
)
1840 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1841 if (next_proto_neg_in
) {
1842 next_proto
.data
= next_protos_parse(&next_proto
.len
, next_proto_neg_in
);
1843 if (next_proto
.data
== NULL
)
1847 alpn_ctx
.data
= NULL
;
1849 alpn_ctx
.data
= next_protos_parse(&alpn_ctx
.len
, alpn_in
);
1850 if (alpn_ctx
.data
== NULL
)
1854 if (crl_file
!= NULL
) {
1856 crl
= load_crl(crl_file
, crl_format
, 0, "CRL");
1859 crls
= sk_X509_CRL_new_null();
1860 if (crls
== NULL
|| !sk_X509_CRL_push(crls
, crl
)) {
1861 BIO_puts(bio_err
, "Error adding CRL\n");
1862 ERR_print_errors(bio_err
);
1868 if (s_dcert_file
!= NULL
) {
1870 if (s_dkey_file
== NULL
)
1871 s_dkey_file
= s_dcert_file
;
1873 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1874 0, dpass
, engine
, "second certificate private key");
1878 s_dcert
= load_cert_pass(s_dcert_file
, s_dcert_format
, 1, dpass
,
1879 "second server certificate");
1881 if (s_dcert
== NULL
) {
1882 ERR_print_errors(bio_err
);
1885 if (s_dchain_file
!= NULL
) {
1886 if (!load_certs(s_dchain_file
, 0, &s_dchain
, NULL
,
1887 "second server certificate chain"))
1893 if (bio_s_out
== NULL
) {
1894 if (s_quiet
&& !s_debug
) {
1895 bio_s_out
= BIO_new(BIO_s_null());
1896 if (s_msg
&& bio_s_msg
== NULL
) {
1897 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1898 if (bio_s_msg
== NULL
) {
1899 BIO_printf(bio_err
, "Out of memory\n");
1904 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1908 if (bio_s_out
== NULL
)
1914 s_dcert_file
= NULL
;
1916 s_cert_file2
= NULL
;
1920 ctx
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
1922 ERR_print_errors(bio_err
);
1926 SSL_CTX_clear_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1929 ssl_ctx_security_debug(ctx
, sdebug
);
1931 if (!config_ctx(cctx
, ssl_args
, ctx
))
1935 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1936 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1938 ERR_print_errors(bio_err
);
1942 #ifndef OPENSSL_NO_SCTP
1943 if (protocol
== IPPROTO_SCTP
&& sctp_label_bug
== 1)
1944 SSL_CTX_set_mode(ctx
, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
);
1947 if (min_version
!= 0
1948 && SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1950 if (max_version
!= 0
1951 && SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1954 if (session_id_prefix
) {
1955 if (strlen(session_id_prefix
) >= 32)
1957 "warning: id_prefix is too long, only one new session will be possible\n");
1958 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1959 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1960 ERR_print_errors(bio_err
);
1963 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1966 ssl_ctx_set_excert(ctx
, exc
);
1969 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1971 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1973 init_session_cache_ctx(ctx
);
1975 SSL_CTX_sess_set_cache_size(ctx
, 128);
1978 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1982 SSL_CTX_set_options(ctx
, SSL_OP_DISABLE_TLSEXT_CA_NAMES
);
1985 if (ignore_unexpected_eof
)
1986 SSL_CTX_set_options(ctx
, SSL_OP_IGNORE_UNEXPECTED_EOF
);
1987 #ifndef OPENSSL_NO_KTLS
1989 SSL_CTX_set_options(ctx
, SSL_OP_ENABLE_KTLS
);
1990 if (use_zc_sendfile
)
1991 SSL_CTX_set_options(ctx
, SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE
);
1994 if (max_send_fragment
> 0
1995 && !SSL_CTX_set_max_send_fragment(ctx
, max_send_fragment
)) {
1996 BIO_printf(bio_err
, "%s: Max send fragment size %u is out of permitted range\n",
1997 prog
, max_send_fragment
);
2001 if (split_send_fragment
> 0
2002 && !SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
)) {
2003 BIO_printf(bio_err
, "%s: Split send fragment size %u is out of permitted range\n",
2004 prog
, split_send_fragment
);
2007 if (max_pipelines
> 0
2008 && !SSL_CTX_set_max_pipelines(ctx
, max_pipelines
)) {
2009 BIO_printf(bio_err
, "%s: Max pipelines %u is out of permitted range\n",
2010 prog
, max_pipelines
);
2014 if (read_buf_len
> 0) {
2015 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
2017 #ifndef OPENSSL_NO_SRTP
2018 if (srtp_profiles
!= NULL
) {
2019 /* Returns 0 on success! */
2020 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
2021 BIO_printf(bio_err
, "Error setting SRTP profile\n");
2022 ERR_print_errors(bio_err
);
2028 if (!ctx_set_verify_locations(ctx
, CAfile
, noCAfile
, CApath
, noCApath
,
2029 CAstore
, noCAstore
)) {
2030 ERR_print_errors(bio_err
);
2033 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
2034 BIO_printf(bio_err
, "Error setting verify params\n");
2035 ERR_print_errors(bio_err
);
2039 ssl_ctx_add_crls(ctx
, crls
, 0);
2041 if (!ssl_load_stores(ctx
,
2042 vfyCApath
, vfyCAfile
, vfyCAstore
,
2043 chCApath
, chCAfile
, chCAstore
,
2044 crls
, crl_download
)) {
2045 BIO_printf(bio_err
, "Error loading store locations\n");
2046 ERR_print_errors(bio_err
);
2051 ctx2
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
2053 ERR_print_errors(bio_err
);
2059 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
2062 ssl_ctx_security_debug(ctx2
, sdebug
);
2064 if (session_id_prefix
) {
2065 if (strlen(session_id_prefix
) >= 32)
2067 "warning: id_prefix is too long, only one new session will be possible\n");
2068 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
2069 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
2070 ERR_print_errors(bio_err
);
2073 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
2076 ssl_ctx_set_excert(ctx2
, exc
);
2079 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
2082 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
2084 init_session_cache_ctx(ctx2
);
2086 SSL_CTX_sess_set_cache_size(ctx2
, 128);
2089 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
2091 if (!ctx_set_verify_locations(ctx2
, CAfile
, noCAfile
, CApath
,
2092 noCApath
, CAstore
, noCAstore
)) {
2093 ERR_print_errors(bio_err
);
2096 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
2097 BIO_printf(bio_err
, "Error setting verify params\n");
2098 ERR_print_errors(bio_err
);
2102 ssl_ctx_add_crls(ctx2
, crls
, 0);
2103 if (!config_ctx(cctx
, ssl_args
, ctx2
))
2106 #ifndef OPENSSL_NO_NEXTPROTONEG
2107 if (next_proto
.data
)
2108 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
2112 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
2115 EVP_PKEY
*dhpkey
= NULL
;
2118 dhpkey
= load_keyparams(dhfile
, FORMAT_UNDEF
, 0, "DH", "DH parameters");
2119 else if (s_cert_file
!= NULL
)
2120 dhpkey
= load_keyparams_suppress(s_cert_file
, FORMAT_UNDEF
, 0, "DH",
2121 "DH parameters", 1);
2123 if (dhpkey
!= NULL
) {
2124 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2126 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
2128 (void)BIO_flush(bio_s_out
);
2130 if (dhpkey
== NULL
) {
2131 SSL_CTX_set_dh_auto(ctx
, 1);
2134 * We need 2 references: one for use by ctx and one for use by
2137 if (!EVP_PKEY_up_ref(dhpkey
)) {
2138 EVP_PKEY_free(dhpkey
);
2141 if (!SSL_CTX_set0_tmp_dh_pkey(ctx
, dhpkey
)) {
2142 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2143 ERR_print_errors(bio_err
);
2144 /* Free 2 references */
2145 EVP_PKEY_free(dhpkey
);
2146 EVP_PKEY_free(dhpkey
);
2152 if (dhfile
!= NULL
) {
2153 EVP_PKEY
*dhpkey2
= load_keyparams_suppress(s_cert_file2
,
2156 "DH parameters", 1);
2158 if (dhpkey2
!= NULL
) {
2159 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2160 (void)BIO_flush(bio_s_out
);
2162 EVP_PKEY_free(dhpkey
);
2166 if (dhpkey
== NULL
) {
2167 SSL_CTX_set_dh_auto(ctx2
, 1);
2168 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2
, dhpkey
)) {
2169 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2170 ERR_print_errors(bio_err
);
2171 EVP_PKEY_free(dhpkey
);
2176 EVP_PKEY_free(dhpkey
);
2179 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
2182 if (s_serverinfo_file
!= NULL
2183 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
2184 ERR_print_errors(bio_err
);
2189 && !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
2192 if (s_dcert
!= NULL
) {
2193 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
2197 if (no_resume_ephemeral
) {
2198 SSL_CTX_set_not_resumable_session_callback(ctx
,
2199 not_resumable_sess_cb
);
2202 SSL_CTX_set_not_resumable_session_callback(ctx2
,
2203 not_resumable_sess_cb
);
2205 #ifndef OPENSSL_NO_PSK
2206 if (psk_key
!= NULL
) {
2208 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
2209 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
2212 if (psk_identity_hint
!= NULL
) {
2213 if (min_version
== TLS1_3_VERSION
) {
2214 BIO_printf(bio_s_out
, "PSK warning: there is NO identity hint in TLSv1.3\n");
2216 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
2217 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
2218 ERR_print_errors(bio_err
);
2224 if (psksessf
!= NULL
) {
2225 BIO
*stmp
= BIO_new_file(psksessf
, "r");
2228 BIO_printf(bio_err
, "Can't open PSK session file %s\n", psksessf
);
2229 ERR_print_errors(bio_err
);
2232 psksess
= PEM_read_bio_SSL_SESSION(stmp
, NULL
, 0, NULL
);
2234 if (psksess
== NULL
) {
2235 BIO_printf(bio_err
, "Can't read PSK session file %s\n", psksessf
);
2236 ERR_print_errors(bio_err
);
2242 if (psk_key
!= NULL
|| psksess
!= NULL
)
2243 SSL_CTX_set_psk_find_session_callback(ctx
, psk_find_session_cb
);
2245 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
2246 if (!SSL_CTX_set_session_id_context(ctx
,
2247 (void *)&s_server_session_id_context
,
2248 sizeof(s_server_session_id_context
))) {
2249 BIO_printf(bio_err
, "error setting session id context\n");
2250 ERR_print_errors(bio_err
);
2254 /* Set DTLS cookie generation and verification callbacks */
2255 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
2256 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
2258 /* Set TLS1.3 cookie generation and verification callbacks */
2259 SSL_CTX_set_stateless_cookie_generate_cb(ctx
, generate_stateless_cookie_callback
);
2260 SSL_CTX_set_stateless_cookie_verify_cb(ctx
, verify_stateless_cookie_callback
);
2263 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
2264 if (!SSL_CTX_set_session_id_context(ctx2
,
2265 (void *)&s_server_session_id_context
,
2266 sizeof(s_server_session_id_context
))) {
2267 BIO_printf(bio_err
, "error setting session id context\n");
2268 ERR_print_errors(bio_err
);
2271 tlsextcbp
.biodebug
= bio_s_out
;
2272 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
2273 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
2274 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
2275 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
2278 #ifndef OPENSSL_NO_SRP
2279 if (srp_verifier_file
!= NULL
) {
2280 if (!set_up_srp_verifier_file(ctx
, &srp_callback_parm
, srpuserseed
,
2285 if (CAfile
!= NULL
) {
2286 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
2289 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
2291 #ifndef OPENSSL_NO_OCSP
2292 if (s_tlsextstatus
) {
2293 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
2294 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2296 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2297 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2301 if (set_keylog_file(ctx
, keylog_file
))
2304 if (max_early_data
>= 0)
2305 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2306 if (recv_max_early_data
>= 0)
2307 SSL_CTX_set_recv_max_early_data(ctx
, recv_max_early_data
);
2310 BIO_printf(bio_s_out
, "Compressing certificates\n");
2311 if (!SSL_CTX_compress_certs(ctx
, 0))
2312 BIO_printf(bio_s_out
, "Error compressing certs on ctx\n");
2313 if (ctx2
!= NULL
&& !SSL_CTX_compress_certs(ctx2
, 0))
2314 BIO_printf(bio_s_out
, "Error compressing certs on ctx2\n");
2316 if (enable_server_rpk
)
2317 if (!SSL_CTX_set1_server_cert_type(ctx
, cert_type_rpk
, sizeof(cert_type_rpk
))) {
2318 BIO_printf(bio_s_out
, "Error setting server certificate types\n");
2321 if (enable_client_rpk
)
2322 if (!SSL_CTX_set1_client_cert_type(ctx
, cert_type_rpk
, sizeof(cert_type_rpk
))) {
2323 BIO_printf(bio_s_out
, "Error setting server certificate types\n");
2328 server_cb
= rev_body
;
2330 server_cb
= www_body
;
2332 server_cb
= sv_body
;
2334 if (socket_family
== AF_UNIX
2335 && unlink_unix_path
)
2339 BIO_printf(bio_s_out
, "Listening for TFO\n");
2340 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
, protocol
,
2341 server_cb
, context
, naccept
, bio_s_out
, tfo
);
2342 print_stats(bio_s_out
, ctx
);
2346 SSL_SESSION_free(psksess
);
2347 set_keylog_file(NULL
, NULL
);
2349 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2351 EVP_PKEY_free(s_key
);
2352 EVP_PKEY_free(s_dkey
);
2353 OSSL_STACK_OF_X509_free(s_chain
);
2354 OSSL_STACK_OF_X509_free(s_dchain
);
2356 OPENSSL_free(dpass
);
2359 X509_VERIFY_PARAM_free(vpm
);
2361 OPENSSL_free(tlscstatp
.host
);
2362 OPENSSL_free(tlscstatp
.port
);
2363 OPENSSL_free(tlscstatp
.path
);
2366 EVP_PKEY_free(s_key2
);
2367 #ifndef OPENSSL_NO_NEXTPROTONEG
2368 OPENSSL_free(next_proto
.data
);
2370 OPENSSL_free(alpn_ctx
.data
);
2371 ssl_excert_free(exc
);
2372 sk_OPENSSL_STRING_free(ssl_args
);
2373 SSL_CONF_CTX_free(cctx
);
2374 release_engine(engine
);
2375 BIO_free(bio_s_out
);
2377 BIO_free(bio_s_msg
);
2379 #ifdef CHARSET_EBCDIC
2380 BIO_meth_free(methods_ebcdic
);
2385 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2387 BIO_printf(bio
, "%4ld items in the session cache\n",
2388 SSL_CTX_sess_number(ssl_ctx
));
2389 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2390 SSL_CTX_sess_connect(ssl_ctx
));
2391 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2392 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2393 BIO_printf(bio
, "%4ld client connects that finished\n",
2394 SSL_CTX_sess_connect_good(ssl_ctx
));
2395 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2396 SSL_CTX_sess_accept(ssl_ctx
));
2397 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2398 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2399 BIO_printf(bio
, "%4ld server accepts that finished\n",
2400 SSL_CTX_sess_accept_good(ssl_ctx
));
2401 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2402 BIO_printf(bio
, "%4ld session cache misses\n",
2403 SSL_CTX_sess_misses(ssl_ctx
));
2404 BIO_printf(bio
, "%4ld session cache timeouts\n",
2405 SSL_CTX_sess_timeouts(ssl_ctx
));
2406 BIO_printf(bio
, "%4ld callback cache hits\n",
2407 SSL_CTX_sess_cb_hits(ssl_ctx
));
2408 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2409 SSL_CTX_sess_cache_full(ssl_ctx
),
2410 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2413 static long int count_reads_callback(BIO
*bio
, int cmd
, const char *argp
, size_t len
,
2414 int argi
, long argl
, int ret
, size_t *processed
)
2416 unsigned int *p_counter
= (unsigned int *)BIO_get_callback_arg(bio
);
2419 case BIO_CB_READ
: /* No break here */
2421 if (p_counter
!= NULL
)
2429 BIO_set_callback_arg(bio
, (char *)bio_s_out
);
2430 ret
= (int)bio_dump_callback(bio
, cmd
, argp
, len
, argi
, argl
, ret
, processed
);
2431 BIO_set_callback_arg(bio
, (char *)p_counter
);
2437 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
)
2446 struct timeval timeout
;
2447 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
2448 struct timeval
*timeoutp
;
2450 #ifndef OPENSSL_NO_DTLS
2451 # ifndef OPENSSL_NO_SCTP
2452 int isdtls
= (stype
== SOCK_DGRAM
|| prot
== IPPROTO_SCTP
);
2454 int isdtls
= (stype
== SOCK_DGRAM
);
2458 buf
= app_malloc(bufsize
, "server buffer");
2460 if (!BIO_socket_nbio(s
, 1))
2461 ERR_print_errors(bio_err
);
2463 BIO_printf(bio_err
, "Turned on non blocking io\n");
2472 if (s_tlsextdebug
) {
2473 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2474 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2478 && !SSL_set_session_id_context(con
, context
,
2479 (unsigned int)strlen((char *)context
))) {
2480 BIO_printf(bio_err
, "Error setting session id context\n");
2485 if (!SSL_clear(con
)) {
2486 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2490 #ifndef OPENSSL_NO_DTLS
2492 # ifndef OPENSSL_NO_SCTP
2493 if (prot
== IPPROTO_SCTP
)
2494 sbio
= BIO_new_dgram_sctp(s
, BIO_NOCLOSE
);
2497 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2499 BIO_printf(bio_err
, "Unable to create BIO\n");
2500 ERR_print_errors(bio_err
);
2504 if (enable_timeouts
) {
2506 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2507 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2510 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2511 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2515 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2516 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2517 DTLS_get_link_min_mtu(con
));
2522 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2523 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2524 BIO_printf(bio_err
, "Failed to set MTU\n");
2530 /* want to do MTU discovery */
2531 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2533 # ifndef OPENSSL_NO_SCTP
2534 if (prot
!= IPPROTO_SCTP
)
2536 /* Turn on cookie exchange. Not necessary for SCTP */
2537 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2540 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2543 BIO_printf(bio_err
, "Unable to create BIO\n");
2544 ERR_print_errors(bio_err
);
2551 test
= BIO_new(BIO_f_nbio_test());
2553 BIO_printf(bio_err
, "Unable to create BIO\n");
2558 sbio
= BIO_push(test
, sbio
);
2561 SSL_set_bio(con
, sbio
, sbio
);
2562 SSL_set_accept_state(con
);
2563 /* SSL_set_fd(con,s); */
2565 BIO_set_callback_ex(SSL_get_rbio(con
), count_reads_callback
);
2567 #ifndef OPENSSL_NO_SSL_TRACE
2569 SSL_set_msg_callback(con
, SSL_trace
);
2572 SSL_set_msg_callback(con
, msg_cb
);
2573 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2576 if (s_tlsextdebug
) {
2577 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2578 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2582 int write_header
= 1, edret
= SSL_READ_EARLY_DATA_ERROR
;
2585 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
2587 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
2588 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
2591 switch (SSL_get_error(con
, 0)) {
2592 case SSL_ERROR_WANT_WRITE
:
2593 case SSL_ERROR_WANT_ASYNC
:
2594 case SSL_ERROR_WANT_READ
:
2595 /* Just keep trying - busy waiting */
2598 BIO_printf(bio_err
, "Error reading early data\n");
2599 ERR_print_errors(bio_err
);
2603 if (readbytes
> 0) {
2605 BIO_printf(bio_s_out
, "Early data received:\n");
2608 raw_write_stdout(buf
, (unsigned int)readbytes
);
2609 (void)BIO_flush(bio_s_out
);
2613 if (SSL_get_early_data_status(con
) == SSL_EARLY_DATA_NOT_SENT
)
2614 BIO_printf(bio_s_out
, "No early data received\n");
2616 BIO_printf(bio_s_out
, "Early data was rejected\n");
2618 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2620 if (SSL_is_init_finished(con
))
2621 print_connection_info(con
);
2624 if (fileno_stdin() > s
)
2625 width
= fileno_stdin() + 1;
2630 int read_from_terminal
;
2631 int read_from_sslcon
;
2633 read_from_terminal
= 0;
2634 read_from_sslcon
= SSL_has_pending(con
)
2635 || (async
&& SSL_waiting_for_async(con
));
2637 if (!read_from_sslcon
) {
2639 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2640 openssl_fdset(fileno_stdin(), &readfds
);
2642 openssl_fdset(s
, &readfds
);
2644 * Note: under VMS with SOCKETSHR the second parameter is
2645 * currently of type (int *) whereas under other systems it is
2646 * (void *) if you don't have a cast it will choke the compiler:
2647 * if you do have a cast then you can either go for (int *) or
2650 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2652 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2653 * only on sockets. As a workaround we timeout the select every
2654 * second and check for any keypress. In a proper Windows
2655 * application we wouldn't do this because it is inefficient.
2658 timeout
.tv_usec
= 0;
2659 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &timeout
);
2660 if (has_stdin_waiting())
2661 read_from_terminal
= 1;
2662 if ((i
< 0) || (!i
&& !read_from_terminal
))
2665 if (SSL_is_dtls(con
) && DTLSv1_get_timeout(con
, &timeout
))
2666 timeoutp
= &timeout
;
2670 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2672 if ((SSL_is_dtls(con
)) && DTLSv1_handle_timeout(con
) > 0)
2673 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2677 if (FD_ISSET(fileno_stdin(), &readfds
))
2678 read_from_terminal
= 1;
2680 if (FD_ISSET(s
, &readfds
))
2681 read_from_sslcon
= 1;
2683 if (read_from_terminal
) {
2687 i
= raw_read_stdin(buf
, bufsize
/ 2);
2689 /* both loops are skipped when i <= 0 */
2690 for (j
= 0; j
< i
; j
++)
2693 for (j
= i
- 1; j
>= 0; j
--) {
2694 buf
[j
+ lf_num
] = buf
[j
];
2695 if (buf
[j
] == '\n') {
2698 buf
[j
+ lf_num
] = '\r';
2701 assert(lf_num
== 0);
2703 i
= raw_read_stdin(buf
, bufsize
);
2706 if (!s_quiet
&& !s_brief
) {
2707 if ((i
<= 0) || (buf
[0] == 'Q')) {
2708 BIO_printf(bio_s_out
, "DONE\n");
2709 (void)BIO_flush(bio_s_out
);
2711 close_accept_socket();
2715 if ((i
<= 0) || (buf
[0] == 'q')) {
2716 BIO_printf(bio_s_out
, "DONE\n");
2717 (void)BIO_flush(bio_s_out
);
2718 if (SSL_version(con
) != DTLS1_VERSION
)
2721 * close_accept_socket(); ret= -11;
2725 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2726 SSL_renegotiate(con
);
2727 i
= SSL_do_handshake(con
);
2728 printf("SSL_do_handshake -> %d\n", i
);
2731 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2733 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2735 SSL_renegotiate(con
);
2736 i
= SSL_do_handshake(con
);
2737 printf("SSL_do_handshake -> %d\n", i
);
2740 if ((buf
[0] == 'K' || buf
[0] == 'k')
2741 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2742 SSL_key_update(con
, buf
[0] == 'K' ?
2743 SSL_KEY_UPDATE_REQUESTED
2744 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2745 i
= SSL_do_handshake(con
);
2746 printf("SSL_do_handshake -> %d\n", i
);
2749 if (buf
[0] == 'c' && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2750 SSL_set_verify(con
, SSL_VERIFY_PEER
, NULL
);
2751 i
= SSL_verify_client_post_handshake(con
);
2753 printf("Failed to initiate request\n");
2754 ERR_print_errors(bio_err
);
2756 i
= SSL_do_handshake(con
);
2757 printf("SSL_do_handshake -> %d\n", i
);
2761 if (buf
[0] == 'P') {
2762 static const char str
[] = "Lets print some clear text\n";
2763 BIO_write(SSL_get_wbio(con
), str
, sizeof(str
) -1);
2765 if (buf
[0] == 'S') {
2766 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2769 #ifdef CHARSET_EBCDIC
2770 ebcdic2ascii(buf
, buf
, i
);
2774 /* should do a select for the write */
2777 if (++count
== 100) {
2779 SSL_renegotiate(con
);
2782 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2783 #ifndef OPENSSL_NO_SRP
2784 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2785 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2787 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2789 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2792 switch (SSL_get_error(con
, k
)) {
2793 case SSL_ERROR_NONE
:
2795 case SSL_ERROR_WANT_ASYNC
:
2796 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2797 (void)BIO_flush(bio_s_out
);
2798 wait_for_async(con
);
2800 case SSL_ERROR_WANT_WRITE
:
2801 case SSL_ERROR_WANT_READ
:
2802 case SSL_ERROR_WANT_X509_LOOKUP
:
2803 BIO_printf(bio_s_out
, "Write BLOCK\n");
2804 (void)BIO_flush(bio_s_out
);
2806 case SSL_ERROR_WANT_ASYNC_JOB
:
2808 * This shouldn't ever happen in s_server. Treat as an error
2810 case SSL_ERROR_SYSCALL
:
2812 BIO_printf(bio_s_out
, "ERROR\n");
2813 (void)BIO_flush(bio_s_out
);
2814 ERR_print_errors(bio_err
);
2818 case SSL_ERROR_ZERO_RETURN
:
2819 BIO_printf(bio_s_out
, "DONE\n");
2820 (void)BIO_flush(bio_s_out
);
2832 if (read_from_sslcon
) {
2834 * init_ssl_connection handles all async events itself so if we're
2835 * waiting for async then we shouldn't go back into
2836 * init_ssl_connection
2838 if ((!async
|| !SSL_waiting_for_async(con
))
2839 && !SSL_is_init_finished(con
)) {
2841 * Count number of reads during init_ssl_connection.
2842 * It helps us to distinguish configuration errors from errors
2843 * caused by a client.
2845 unsigned int read_counter
= 0;
2847 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)&read_counter
);
2848 i
= init_ssl_connection(con
);
2849 BIO_set_callback_arg(SSL_get_rbio(con
), NULL
);
2852 * If initialization fails without reads, then
2853 * there was a fatal error in configuration.
2855 if (i
<= 0 && read_counter
== 0) {
2862 } else if (i
== 0) {
2868 i
= SSL_read(con
, (char *)buf
, bufsize
);
2869 #ifndef OPENSSL_NO_SRP
2870 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2871 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2873 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2875 i
= SSL_read(con
, (char *)buf
, bufsize
);
2878 switch (SSL_get_error(con
, i
)) {
2879 case SSL_ERROR_NONE
:
2880 #ifdef CHARSET_EBCDIC
2881 ascii2ebcdic(buf
, buf
, i
);
2883 raw_write_stdout(buf
, (unsigned int)i
);
2884 (void)BIO_flush(bio_s_out
);
2885 if (SSL_has_pending(con
))
2888 case SSL_ERROR_WANT_ASYNC
:
2889 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2890 (void)BIO_flush(bio_s_out
);
2891 wait_for_async(con
);
2893 case SSL_ERROR_WANT_WRITE
:
2894 case SSL_ERROR_WANT_READ
:
2895 BIO_printf(bio_s_out
, "Read BLOCK\n");
2896 (void)BIO_flush(bio_s_out
);
2898 case SSL_ERROR_WANT_ASYNC_JOB
:
2900 * This shouldn't ever happen in s_server. Treat as an error
2902 case SSL_ERROR_SYSCALL
:
2904 BIO_printf(bio_s_out
, "ERROR\n");
2905 (void)BIO_flush(bio_s_out
);
2906 ERR_print_errors(bio_err
);
2909 case SSL_ERROR_ZERO_RETURN
:
2910 BIO_printf(bio_s_out
, "DONE\n");
2911 (void)BIO_flush(bio_s_out
);
2920 BIO_printf(bio_s_out
, "shutting down SSL\n");
2921 do_ssl_shutdown(con
);
2924 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2925 OPENSSL_clear_free(buf
, bufsize
);
2929 static void close_accept_socket(void)
2931 BIO_printf(bio_err
, "shutdown accept socket\n");
2932 if (accept_socket
>= 0) {
2933 BIO_closesocket(accept_socket
);
2937 static int is_retryable(SSL
*con
, int i
)
2939 int err
= SSL_get_error(con
, i
);
2941 /* If it's not a fatal error, it must be retryable */
2942 return (err
!= SSL_ERROR_SSL
)
2943 && (err
!= SSL_ERROR_SYSCALL
)
2944 && (err
!= SSL_ERROR_ZERO_RETURN
);
2947 static int init_ssl_connection(SSL
*con
)
2953 if (dtlslisten
|| stateless
) {
2954 BIO_ADDR
*client
= NULL
;
2957 if ((client
= BIO_ADDR_new()) == NULL
) {
2958 BIO_printf(bio_err
, "ERROR - memory\n");
2961 i
= DTLSv1_listen(con
, client
);
2963 i
= SSL_stateless(con
);
2970 wbio
= SSL_get_wbio(con
);
2972 BIO_get_fd(wbio
, &fd
);
2975 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2976 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2977 BIO_ADDR_free(client
);
2981 (void)BIO_ctrl_set_connected(wbio
, client
);
2982 BIO_ADDR_free(client
);
2987 i
= SSL_accept(con
);
2989 BIO_ADDR_free(client
);
2993 i
= SSL_accept(con
);
2996 retry
= is_retryable(con
, i
);
2997 #ifdef CERT_CB_TEST_RETRY
3000 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
3001 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
3003 "LOOKUP from certificate callback during accept\n");
3004 i
= SSL_accept(con
);
3006 retry
= is_retryable(con
, i
);
3011 #ifndef OPENSSL_NO_SRP
3013 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
3014 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
3015 srp_callback_parm
.login
);
3017 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3019 i
= SSL_accept(con
);
3021 retry
= is_retryable(con
, i
);
3024 } while (i
< 0 && SSL_waiting_for_async(con
));
3028 if (((dtlslisten
|| stateless
) && i
== 0)
3029 || (!dtlslisten
&& !stateless
&& retry
)) {
3030 BIO_printf(bio_s_out
, "DELAY\n");
3034 BIO_printf(bio_err
, "ERROR\n");
3036 verify_err
= SSL_get_verify_result(con
);
3037 if (verify_err
!= X509_V_OK
) {
3038 BIO_printf(bio_err
, "verify error:%s\n",
3039 X509_verify_cert_error_string(verify_err
));
3041 /* Always print any error messages */
3042 ERR_print_errors(bio_err
);
3046 print_connection_info(con
);
3050 static void print_connection_info(SSL
*con
)
3055 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3056 const unsigned char *next_proto_neg
;
3057 unsigned next_proto_neg_len
;
3059 unsigned char *exportedkeymat
;
3063 print_ssl_summary(con
);
3065 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
3067 peer
= SSL_get0_peer_certificate(con
);
3069 BIO_printf(bio_s_out
, "Client certificate\n");
3070 PEM_write_bio_X509(bio_s_out
, peer
);
3071 dump_cert_text(bio_s_out
, peer
);
3074 /* Only display RPK information if configured */
3075 if (SSL_get_negotiated_server_cert_type(con
) == TLSEXT_cert_type_rpk
)
3076 BIO_printf(bio_s_out
, "Server-to-client raw public key negotiated\n");
3077 if (SSL_get_negotiated_client_cert_type(con
) == TLSEXT_cert_type_rpk
)
3078 BIO_printf(bio_s_out
, "Client-to-server raw public key negotiated\n");
3079 if (enable_client_rpk
) {
3080 EVP_PKEY
*client_rpk
= SSL_get0_peer_rpk(con
);
3082 if (client_rpk
!= NULL
) {
3083 BIO_printf(bio_s_out
, "Client raw public key\n");
3084 EVP_PKEY_print_public(bio_s_out
, client_rpk
, 2, NULL
);
3088 if (SSL_get_shared_ciphers(con
, buf
, sizeof(buf
)) != NULL
)
3089 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
3090 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
3091 ssl_print_sigalgs(bio_s_out
, con
);
3092 #ifndef OPENSSL_NO_EC
3093 ssl_print_point_formats(bio_s_out
, con
);
3094 ssl_print_groups(bio_s_out
, con
, 0);
3096 print_ca_names(bio_s_out
, con
);
3097 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
3099 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3100 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
3101 if (next_proto_neg
) {
3102 BIO_printf(bio_s_out
, "NEXTPROTO is ");
3103 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
3104 BIO_printf(bio_s_out
, "\n");
3107 #ifndef OPENSSL_NO_SRTP
3109 SRTP_PROTECTION_PROFILE
*srtp_profile
3110 = SSL_get_selected_srtp_profile(con
);
3113 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
3114 srtp_profile
->name
);
3117 if (SSL_session_reused(con
))
3118 BIO_printf(bio_s_out
, "Reused session-id\n");
3120 ssl_print_secure_renegotiation_notes(bio_s_out
, con
);
3122 if ((SSL_get_options(con
) & SSL_OP_NO_RENEGOTIATION
))
3123 BIO_printf(bio_s_out
, "Renegotiation is DISABLED\n");
3125 if (keymatexportlabel
!= NULL
) {
3126 BIO_printf(bio_s_out
, "Keying material exporter:\n");
3127 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
3128 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
3129 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
3130 if (SSL_export_keying_material(con
, exportedkeymat
,
3133 strlen(keymatexportlabel
),
3135 BIO_printf(bio_s_out
, " Error\n");
3137 BIO_printf(bio_s_out
, " Keying material: ");
3138 for (i
= 0; i
< keymatexportlen
; i
++)
3139 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
3140 BIO_printf(bio_s_out
, "\n");
3142 OPENSSL_free(exportedkeymat
);
3144 #ifndef OPENSSL_NO_KTLS
3145 if (BIO_get_ktls_send(SSL_get_wbio(con
)))
3146 BIO_printf(bio_err
, "Using Kernel TLS for sending\n");
3147 if (BIO_get_ktls_recv(SSL_get_rbio(con
)))
3148 BIO_printf(bio_err
, "Using Kernel TLS for receiving\n");
3151 (void)BIO_flush(bio_s_out
);
3154 static int www_body(int s
, int stype
, int prot
, unsigned char *context
)
3156 char *buf
= NULL
, *p
;
3160 const SSL_CIPHER
*c
;
3161 BIO
*io
, *ssl_bio
, *sbio
, *edio
;
3163 int total_bytes
= 0;
3166 #ifndef OPENSSL_NO_KTLS
3167 int use_sendfile_for_req
= use_sendfile
;
3171 #ifdef CHARSET_EBCDIC
3175 /* Set width for a select call if needed */
3178 /* as we use BIO_gets(), and it always null terminates data, we need
3179 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3180 p
= buf
= app_malloc(bufsize
+ 1, "server www buffer");
3181 io
= BIO_new(BIO_f_buffer());
3182 ssl_bio
= BIO_new(BIO_f_ssl());
3183 edio
= BIO_new(BIO_s_mem());
3184 if ((io
== NULL
) || (ssl_bio
== NULL
) || (edio
== NULL
))
3188 if (!BIO_socket_nbio(s
, 1))
3189 ERR_print_errors(bio_err
);
3191 BIO_printf(bio_err
, "Turned on non blocking io\n");
3194 /* lets make the output buffer a reasonable size */
3195 if (BIO_set_write_buffer_size(io
, bufsize
) <= 0)
3198 if ((con
= SSL_new(ctx
)) == NULL
)
3201 if (s_tlsextdebug
) {
3202 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3203 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3207 && !SSL_set_session_id_context(con
, context
,
3208 (unsigned int)strlen((char *)context
))) {
3213 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3222 test
= BIO_new(BIO_f_nbio_test());
3229 sbio
= BIO_push(test
, sbio
);
3231 SSL_set_bio(con
, sbio
, sbio
);
3232 SSL_set_accept_state(con
);
3234 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3235 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3236 BIO_push(io
, ssl_bio
);
3238 #ifdef CHARSET_EBCDIC
3239 filter
= BIO_new(BIO_f_ebcdic_filter());
3243 io
= BIO_push(filter
, io
);
3245 filter
= BIO_new(BIO_f_ebcdic_filter());
3249 edio
= BIO_push(filter
, edio
);
3253 BIO_set_callback_ex(SSL_get_rbio(con
), bio_dump_callback
);
3254 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3257 #ifndef OPENSSL_NO_SSL_TRACE
3259 SSL_set_msg_callback(con
, SSL_trace
);
3262 SSL_set_msg_callback(con
, msg_cb
);
3263 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3267 int edret
= SSL_READ_EARLY_DATA_ERROR
;
3270 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
3272 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
3273 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
3276 switch (SSL_get_error(con
, 0)) {
3277 case SSL_ERROR_WANT_WRITE
:
3278 case SSL_ERROR_WANT_ASYNC
:
3279 case SSL_ERROR_WANT_READ
:
3280 /* Just keep trying - busy waiting */
3283 BIO_printf(bio_err
, "Error reading early data\n");
3284 ERR_print_errors(bio_err
);
3289 BIO_write(edio
, buf
, (int)readbytes
);
3294 i
= BIO_gets(!BIO_eof(edio
) ? edio
: io
, buf
, bufsize
+ 1);
3295 if (i
< 0) { /* error */
3296 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
3298 ERR_print_errors(bio_err
);
3301 BIO_printf(bio_s_out
, "read R BLOCK\n");
3302 #ifndef OPENSSL_NO_SRP
3303 if (BIO_should_io_special(io
)
3304 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3305 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3307 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3315 } else if (i
== 0) { /* end of input */
3320 /* else we have data */
3321 if ((www
== 1 && HAS_PREFIX(buf
, "GET "))
3322 || (www
== 2 && HAS_PREFIX(buf
, "GET /stats "))) {
3324 STACK_OF(SSL_CIPHER
) *sk
;
3325 static const char *space
= " ";
3327 if (www
== 1 && HAS_PREFIX(buf
, "GET /reneg")) {
3328 if (HAS_PREFIX(buf
, "GET /renegcert"))
3330 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
3332 i
= SSL_renegotiate(con
);
3333 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
3334 /* Send the HelloRequest */
3335 i
= SSL_do_handshake(con
);
3337 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
3338 SSL_get_error(con
, i
));
3339 ERR_print_errors(bio_err
);
3342 /* Wait for a ClientHello to come back */
3344 openssl_fdset(s
, &readfds
);
3345 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
3346 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
3347 BIO_printf(bio_s_out
,
3348 "Error waiting for client response\n");
3349 ERR_print_errors(bio_err
);
3353 * We're not actually expecting any data here and we ignore
3354 * any that is sent. This is just to force the handshake that
3355 * we're expecting to come from the client. If they haven't
3356 * sent one there's not much we can do.
3358 BIO_gets(io
, buf
, bufsize
+ 1);
3362 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3363 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
3364 BIO_puts(io
, "<pre>\n");
3365 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
3367 for (i
= 0; i
< local_argc
; i
++) {
3370 for (myp
= local_argv
[i
]; *myp
; myp
++)
3373 BIO_puts(io
, "<");
3376 BIO_puts(io
, ">");
3379 BIO_puts(io
, "&");
3382 BIO_write(io
, myp
, 1);
3385 BIO_write(io
, " ", 1);
3389 ssl_print_secure_renegotiation_notes(io
, con
);
3392 * The following is evil and should not really be done
3394 BIO_printf(io
, "Ciphers supported in s_server binary\n");
3395 sk
= SSL_get_ciphers(con
);
3396 j
= sk_SSL_CIPHER_num(sk
);
3397 for (i
= 0; i
< j
; i
++) {
3398 c
= sk_SSL_CIPHER_value(sk
, i
);
3399 BIO_printf(io
, "%-11s:%-25s ",
3400 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3401 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
3405 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
3408 "---\nCiphers common between both SSL end points:\n");
3412 BIO_write(io
, space
, 26 - j
);
3415 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
3417 BIO_write(io
, p
, 1);
3424 ssl_print_sigalgs(io
, con
);
3425 #ifndef OPENSSL_NO_EC
3426 ssl_print_groups(io
, con
, 0);
3428 print_ca_names(io
, con
);
3429 BIO_printf(io
, (SSL_session_reused(con
)
3430 ? "---\nReused, " : "---\nNew, "));
3431 c
= SSL_get_current_cipher(con
);
3432 BIO_printf(io
, "%s, Cipher is %s\n",
3433 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3434 SSL_SESSION_print(io
, SSL_get_session(con
));
3435 BIO_printf(io
, "---\n");
3436 print_stats(io
, SSL_get_SSL_CTX(con
));
3437 BIO_printf(io
, "---\n");
3438 peer
= SSL_get0_peer_certificate(con
);
3440 BIO_printf(io
, "Client certificate\n");
3441 X509_print(io
, peer
);
3442 PEM_write_bio_X509(io
, peer
);
3445 BIO_puts(io
, "no client certificate available\n");
3447 BIO_puts(io
, "</pre></BODY></HTML>\r\n\r\n");
3449 } else if ((www
== 2 || www
== 3) && CHECK_AND_SKIP_PREFIX(p
, "GET /")) {
3452 static const char *text
=
3453 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3456 for (e
= p
; *e
!= '\0'; e
++) {
3461 /* Windows drive. We treat this the same way as ".." */
3468 dot
= (e
[0] == '.') ? 2 : 0;
3471 dot
= (e
[0] == '.') ? 3 : 0;
3474 dot
= (e
[0] == '/' || e
[0] == '\\') ? -1 : 0;
3478 dot
= (e
[0] == '/' || e
[0] == '\\') ? 1 : 0;
3480 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3485 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3492 BIO_printf(io
, "'%s' contains '..' or ':'\r\n", p
);
3496 if (*p
== '/' || *p
== '\\') {
3498 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3502 /* if a directory, do the index thang */
3503 if (app_isdir(p
) > 0) {
3505 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3509 opmode
= (http_server_binmode
== 1) ? "rb" : "r";
3510 if ((file
= BIO_new_file(p
, opmode
)) == NULL
) {
3512 BIO_printf(io
, "Error opening '%s' mode='%s'\r\n", p
, opmode
);
3513 ERR_print_errors(io
);
3518 BIO_printf(bio_err
, "FILE:%s\n", p
);
3522 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3523 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3524 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3526 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3529 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3532 #ifndef OPENSSL_NO_KTLS
3533 if (use_sendfile_for_req
&& !BIO_get_ktls_send(SSL_get_wbio(con
))) {
3534 BIO_printf(bio_err
, "Warning: sendfile requested but KTLS is not available\n");
3535 use_sendfile_for_req
= 0;
3537 if (use_sendfile_for_req
) {
3544 BIO_get_fp(file
, &fp
);
3546 if (fstat(fd
, &st
) < 0) {
3547 BIO_printf(io
, "Error fstat '%s'\r\n", p
);
3548 ERR_print_errors(io
);
3552 filesize
= st
.st_size
;
3553 if (((int)BIO_flush(io
)) < 0)
3557 i
= SSL_sendfile(con
, fd
, offset
, filesize
, 0);
3559 BIO_printf(io
, "Error SSL_sendfile '%s'\r\n", p
);
3560 ERR_print_errors(io
);
3567 if (filesize
<= 0) {
3569 BIO_printf(bio_err
, "KTLS SENDFILE '%s' OK\n", p
);
3578 i
= BIO_read(file
, buf
, bufsize
);
3584 BIO_printf(bio_err
, "%d\n", i
);
3585 if (total_bytes
> 3 * 1024) {
3587 BIO_printf(bio_err
, "RENEGOTIATE\n");
3588 SSL_renegotiate(con
);
3592 for (j
= 0; j
< i
;) {
3596 SSL_renegotiate(con
);
3598 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3600 if (!BIO_should_retry(io
)
3601 && !SSL_waiting_for_async(con
)) {
3604 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3619 i
= (int)BIO_flush(io
);
3621 if (!BIO_should_retry(io
))
3627 /* make sure we reuse sessions */
3628 do_ssl_shutdown(con
);
3638 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
)
3644 BIO
*io
, *ssl_bio
, *sbio
;
3645 #ifdef CHARSET_EBCDIC
3649 /* as we use BIO_gets(), and it always null terminates data, we need
3650 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3651 buf
= app_malloc(bufsize
+ 1, "server rev buffer");
3652 io
= BIO_new(BIO_f_buffer());
3653 ssl_bio
= BIO_new(BIO_f_ssl());
3654 if ((io
== NULL
) || (ssl_bio
== NULL
))
3657 /* lets make the output buffer a reasonable size */
3658 if (BIO_set_write_buffer_size(io
, bufsize
) <= 0)
3661 if ((con
= SSL_new(ctx
)) == NULL
)
3664 if (s_tlsextdebug
) {
3665 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3666 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3669 && !SSL_set_session_id_context(con
, context
,
3670 (unsigned int)strlen((char *)context
))) {
3672 ERR_print_errors(bio_err
);
3676 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3679 ERR_print_errors(bio_err
);
3683 SSL_set_bio(con
, sbio
, sbio
);
3684 SSL_set_accept_state(con
);
3686 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3687 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3688 BIO_push(io
, ssl_bio
);
3690 #ifdef CHARSET_EBCDIC
3691 filter
= BIO_new(BIO_f_ebcdic_filter());
3695 io
= BIO_push(filter
, io
);
3699 BIO_set_callback_ex(SSL_get_rbio(con
), bio_dump_callback
);
3700 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3703 #ifndef OPENSSL_NO_SSL_TRACE
3705 SSL_set_msg_callback(con
, SSL_trace
);
3708 SSL_set_msg_callback(con
, msg_cb
);
3709 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3713 i
= BIO_do_handshake(io
);
3716 if (!BIO_should_retry(io
)) {
3717 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3718 ERR_print_errors(bio_err
);
3721 #ifndef OPENSSL_NO_SRP
3722 if (BIO_should_io_special(io
)
3723 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3724 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3726 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3732 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3733 print_ssl_summary(con
);
3736 i
= BIO_gets(io
, buf
, bufsize
+ 1);
3737 if (i
< 0) { /* error */
3738 if (!BIO_should_retry(io
)) {
3740 ERR_print_errors(bio_err
);
3743 BIO_printf(bio_s_out
, "read R BLOCK\n");
3744 #ifndef OPENSSL_NO_SRP
3745 if (BIO_should_io_special(io
)
3746 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3747 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3749 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3757 } else if (i
== 0) { /* end of input */
3759 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3762 char *p
= buf
+ i
- 1;
3763 while (i
&& (*p
== '\n' || *p
== '\r')) {
3767 if (!s_ign_eof
&& i
== 5 && HAS_PREFIX(buf
, "CLOSE")) {
3769 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3772 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3774 BIO_write(io
, buf
, i
+ 1);
3779 if (!BIO_should_retry(io
))
3785 /* make sure we reuse sessions */
3786 do_ssl_shutdown(con
);
3796 #define MAX_SESSION_ID_ATTEMPTS 10
3797 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
3798 unsigned int *id_len
)
3800 unsigned int count
= 0;
3801 unsigned int session_id_prefix_len
= (unsigned int)strlen(session_id_prefix
);
3804 if (RAND_bytes(id
, *id_len
) <= 0)
3807 * Prefix the session_id with the required prefix. NB: If our prefix
3808 * is too long, clip it - but there will be worse effects anyway, eg.
3809 * the server could only possibly create 1 session ID (ie. the
3810 * prefix!) so all future session negotiations will fail due to
3813 memcpy(id
, session_id_prefix
,
3814 (session_id_prefix_len
< *id_len
) ?
3815 session_id_prefix_len
: *id_len
);
3817 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3818 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3819 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3825 * By default s_server uses an in-memory cache which caches SSL_SESSION
3826 * structures without any serialization. This hides some bugs which only
3827 * become apparent in deployed servers. By implementing a basic external
3828 * session cache some issues can be debugged using s_server.
3831 typedef struct simple_ssl_session_st
{
3836 struct simple_ssl_session_st
*next
;
3837 } simple_ssl_session
;
3839 static simple_ssl_session
*first
= NULL
;
3841 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3843 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3846 SSL_SESSION_get_id(session
, &sess
->idlen
);
3847 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3848 if (sess
->derlen
< 0) {
3849 BIO_printf(bio_err
, "Error encoding session\n");
3854 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3855 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3857 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3858 OPENSSL_free(sess
->id
);
3859 OPENSSL_free(sess
->der
);
3865 /* Assume it still works. */
3866 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3867 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3868 OPENSSL_free(sess
->id
);
3869 OPENSSL_free(sess
->der
);
3876 BIO_printf(bio_err
, "New session added to external cache\n");
3880 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3883 simple_ssl_session
*sess
;
3885 for (sess
= first
; sess
; sess
= sess
->next
) {
3886 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3887 const unsigned char *p
= sess
->der
;
3888 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3889 return d2i_SSL_SESSION_ex(NULL
, &p
, sess
->derlen
, app_get0_libctx(),
3893 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3897 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3899 simple_ssl_session
*sess
, *prev
= NULL
;
3900 const unsigned char *id
;
3902 id
= SSL_SESSION_get_id(session
, &idlen
);
3903 for (sess
= first
; sess
; sess
= sess
->next
) {
3904 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3906 prev
->next
= sess
->next
;
3909 OPENSSL_free(sess
->id
);
3910 OPENSSL_free(sess
->der
);
3918 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3920 SSL_CTX_set_session_cache_mode(sctx
,
3921 SSL_SESS_CACHE_NO_INTERNAL
|
3922 SSL_SESS_CACHE_SERVER
);
3923 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3924 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3925 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3928 static void free_sessions(void)
3930 simple_ssl_session
*sess
, *tsess
;
3931 for (sess
= first
; sess
;) {
3932 OPENSSL_free(sess
->id
);
3933 OPENSSL_free(sess
->der
);
3936 OPENSSL_free(tsess
);
3941 #endif /* OPENSSL_NO_SOCK */