2 * Copyright 1995-2024 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;
99 static int use_zc_sendfile
= 0;
101 static const char *session_id_prefix
= NULL
;
103 static const unsigned char cert_type_rpk
[] = { TLSEXT_cert_type_rpk
, TLSEXT_cert_type_x509
};
104 static int enable_client_rpk
= 0;
106 #ifndef OPENSSL_NO_DTLS
107 static int enable_timeouts
= 0;
108 static long socket_mtu
;
112 * We define this but make it always be 0 in no-dtls builds to simplify the
115 static int dtlslisten
= 0;
116 static int stateless
= 0;
118 static int early_data
= 0;
119 static SSL_SESSION
*psksess
= NULL
;
121 static char *psk_identity
= "Client_identity";
122 char *psk_key
= NULL
; /* by default PSK is not used */
124 static char http_server_binmode
= 0; /* for now: 0/1 = default/binary */
126 #ifndef OPENSSL_NO_PSK
127 static unsigned int psk_server_cb(SSL
*ssl
, const char *identity
,
129 unsigned int max_psk_len
)
135 BIO_printf(bio_s_out
, "psk_server_cb\n");
137 if (!SSL_is_dtls(ssl
) && SSL_version(ssl
) >= TLS1_3_VERSION
) {
139 * This callback is designed for use in (D)TLSv1.2 (or below). It is
140 * possible to use a single callback for all protocol versions - but it
141 * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we
142 * have psk_find_session_cb.
147 if (identity
== NULL
) {
148 BIO_printf(bio_err
, "Error: client did not send PSK identity\n");
152 BIO_printf(bio_s_out
, "identity_len=%d identity=%s\n",
153 (int)strlen(identity
), identity
);
155 /* here we could lookup the given identity e.g. from a database */
156 if (strcmp(identity
, psk_identity
) != 0) {
157 BIO_printf(bio_s_out
, "PSK warning: client identity not what we expected"
158 " (got '%s' expected '%s')\n", identity
, psk_identity
);
161 BIO_printf(bio_s_out
, "PSK client identity found\n");
164 /* convert the PSK key to binary */
165 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
167 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
171 if (key_len
> (int)max_psk_len
) {
173 "psk buffer of callback is too small (%d) for key (%ld)\n",
174 max_psk_len
, key_len
);
179 memcpy(psk
, key
, key_len
);
183 BIO_printf(bio_s_out
, "fetched PSK len=%ld\n", key_len
);
187 BIO_printf(bio_err
, "Error in PSK server callback\n");
188 (void)BIO_flush(bio_err
);
189 (void)BIO_flush(bio_s_out
);
194 static int psk_find_session_cb(SSL
*ssl
, const unsigned char *identity
,
195 size_t identity_len
, SSL_SESSION
**sess
)
197 SSL_SESSION
*tmpsess
= NULL
;
200 const SSL_CIPHER
*cipher
= NULL
;
202 if (strlen(psk_identity
) != identity_len
203 || memcmp(psk_identity
, identity
, identity_len
) != 0) {
208 if (psksess
!= NULL
) {
209 SSL_SESSION_up_ref(psksess
);
214 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
216 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
221 /* We default to SHA256 */
222 cipher
= SSL_CIPHER_find(ssl
, tls13_aes128gcmsha256_id
);
223 if (cipher
== NULL
) {
224 BIO_printf(bio_err
, "Error finding suitable ciphersuite\n");
229 tmpsess
= SSL_SESSION_new();
231 || !SSL_SESSION_set1_master_key(tmpsess
, key
, key_len
)
232 || !SSL_SESSION_set_cipher(tmpsess
, cipher
)
233 || !SSL_SESSION_set_protocol_version(tmpsess
, SSL_version(ssl
))) {
235 SSL_SESSION_free(tmpsess
);
244 #ifndef OPENSSL_NO_SRP
245 static srpsrvparm srp_callback_parm
;
248 static int local_argc
= 0;
249 static char **local_argv
;
251 #ifdef CHARSET_EBCDIC
252 static int ebcdic_new(BIO
*bi
);
253 static int ebcdic_free(BIO
*a
);
254 static int ebcdic_read(BIO
*b
, char *out
, int outl
);
255 static int ebcdic_write(BIO
*b
, const char *in
, int inl
);
256 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
257 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
);
258 static int ebcdic_puts(BIO
*bp
, const char *str
);
260 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
261 static BIO_METHOD
*methods_ebcdic
= NULL
;
263 /* This struct is "unwarranted chumminess with the compiler." */
269 static const BIO_METHOD
*BIO_f_ebcdic_filter(void)
271 if (methods_ebcdic
== NULL
) {
272 methods_ebcdic
= BIO_meth_new(BIO_TYPE_EBCDIC_FILTER
,
273 "EBCDIC/ASCII filter");
274 if (methods_ebcdic
== NULL
275 || !BIO_meth_set_write(methods_ebcdic
, ebcdic_write
)
276 || !BIO_meth_set_read(methods_ebcdic
, ebcdic_read
)
277 || !BIO_meth_set_puts(methods_ebcdic
, ebcdic_puts
)
278 || !BIO_meth_set_gets(methods_ebcdic
, ebcdic_gets
)
279 || !BIO_meth_set_ctrl(methods_ebcdic
, ebcdic_ctrl
)
280 || !BIO_meth_set_create(methods_ebcdic
, ebcdic_new
)
281 || !BIO_meth_set_destroy(methods_ebcdic
, ebcdic_free
))
284 return methods_ebcdic
;
287 static int ebcdic_new(BIO
*bi
)
289 EBCDIC_OUTBUFF
*wbuf
;
291 wbuf
= app_malloc(sizeof(*wbuf
) + 1024, "ebcdic wbuf");
292 wbuf
->alloced
= 1024;
293 wbuf
->buff
[0] = '\0';
295 BIO_set_data(bi
, wbuf
);
300 static int ebcdic_free(BIO
*a
)
302 EBCDIC_OUTBUFF
*wbuf
;
306 wbuf
= BIO_get_data(a
);
308 BIO_set_data(a
, NULL
);
314 static int ebcdic_read(BIO
*b
, char *out
, int outl
)
317 BIO
*next
= BIO_next(b
);
319 if (out
== NULL
|| outl
== 0)
324 ret
= BIO_read(next
, out
, outl
);
326 ascii2ebcdic(out
, out
, ret
);
330 static int ebcdic_write(BIO
*b
, const char *in
, int inl
)
332 EBCDIC_OUTBUFF
*wbuf
;
333 BIO
*next
= BIO_next(b
);
337 if ((in
== NULL
) || (inl
<= 0))
342 wbuf
= (EBCDIC_OUTBUFF
*) BIO_get_data(b
);
344 if (inl
> (num
= wbuf
->alloced
)) {
345 num
= num
+ num
; /* double the size */
349 wbuf
= app_malloc(sizeof(*wbuf
) + num
, "grow ebcdic wbuf");
352 wbuf
->buff
[0] = '\0';
354 BIO_set_data(b
, wbuf
);
357 ebcdic2ascii(wbuf
->buff
, in
, inl
);
359 ret
= BIO_write(next
, wbuf
->buff
, inl
);
364 static long ebcdic_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
367 BIO
*next
= BIO_next(b
);
376 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
382 static int ebcdic_gets(BIO
*bp
, char *buf
, int size
)
385 BIO
*next
= BIO_next(bp
);
389 /* return(BIO_gets(bp->next_bio,buf,size));*/
390 for (i
= 0; i
< size
- 1; ++i
) {
391 ret
= ebcdic_read(bp
, &buf
[i
], 1);
394 else if (buf
[i
] == '\n') {
401 return (ret
< 0 && i
== 0) ? ret
: i
;
404 static int ebcdic_puts(BIO
*bp
, const char *str
)
406 if (BIO_next(bp
) == NULL
)
408 return ebcdic_write(bp
, str
, strlen(str
));
412 /* This is a context that we pass to callbacks */
413 typedef struct tlsextctx_st
{
419 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
421 tlsextctx
*p
= (tlsextctx
*) arg
;
422 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
424 if (servername
!= NULL
&& p
->biodebug
!= NULL
) {
425 const char *cp
= servername
;
428 BIO_printf(p
->biodebug
, "Hostname in TLS extension: \"");
429 while ((uc
= *cp
++) != 0)
430 BIO_printf(p
->biodebug
,
431 (((uc
) & ~127) == 0) && isprint(uc
) ? "%c" : "\\x%02x", uc
);
432 BIO_printf(p
->biodebug
, "\"\n");
435 if (p
->servername
== NULL
)
436 return SSL_TLSEXT_ERR_NOACK
;
438 if (servername
!= NULL
) {
439 if (OPENSSL_strcasecmp(servername
, p
->servername
))
440 return p
->extension_error
;
442 BIO_printf(p
->biodebug
, "Switching server context.\n");
443 SSL_set_SSL_CTX(s
, ctx2
);
446 return SSL_TLSEXT_ERR_OK
;
449 /* Structure passed to cert status callback */
450 typedef struct tlsextstatusctx_st
{
452 /* File to load OCSP Response from (or NULL if no file) */
454 /* Default responder to use */
455 char *host
, *path
, *port
;
456 char *proxy
, *no_proxy
;
461 static tlsextstatusctx tlscstatp
= { -1 };
463 #ifndef OPENSSL_NO_OCSP
466 * Helper function to get an OCSP_RESPONSE from a responder. This is a
467 * simplified version. It examines certificates each time and makes one OCSP
468 * responder query for each request. A full version would store details such as
469 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
470 * them until they were considered "expired".
472 static int get_ocsp_resp_from_responder(SSL
*s
, tlsextstatusctx
*srctx
,
473 OCSP_RESPONSE
**resp
)
475 char *host
= NULL
, *port
= NULL
, *path
= NULL
;
476 char *proxy
= NULL
, *no_proxy
= NULL
;
478 STACK_OF(OPENSSL_STRING
) *aia
= NULL
;
479 X509
*x
= NULL
, *cert
;
481 STACK_OF(X509
) *chain
= NULL
;
483 X509_STORE_CTX
*inctx
= NULL
;
485 OCSP_REQUEST
*req
= NULL
;
486 OCSP_CERTID
*id
= NULL
;
487 STACK_OF(X509_EXTENSION
) *exts
;
488 int ret
= SSL_TLSEXT_ERR_NOACK
;
491 /* Build up OCSP query from server certificate */
492 x
= SSL_get_certificate(s
);
493 iname
= X509_get_issuer_name(x
);
494 aia
= X509_get1_ocsp(x
);
496 if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia
, 0), &use_ssl
,
497 NULL
, &host
, &port
, NULL
, &path
, NULL
, NULL
)) {
498 BIO_puts(bio_err
, "cert_status: can't parse AIA URL\n");
502 BIO_printf(bio_err
, "cert_status: AIA URL: %s\n",
503 sk_OPENSSL_STRING_value(aia
, 0));
505 if (srctx
->host
== NULL
) {
507 "cert_status: no AIA and no default responder URL\n");
513 use_ssl
= srctx
->use_ssl
;
515 proxy
= srctx
->proxy
;
516 no_proxy
= srctx
->no_proxy
;
518 ssl_ctx
= SSL_get_SSL_CTX(s
);
519 if (!SSL_CTX_get0_chain_certs(ssl_ctx
, &chain
))
521 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
522 /* check the untrusted certificate chain (-cert_chain option) */
523 cert
= sk_X509_value(chain
, i
);
524 if (X509_name_cmp(iname
, X509_get_subject_name(cert
)) == 0) {
525 /* the issuer certificate is found */
526 id
= OCSP_cert_to_id(NULL
, x
, cert
);
531 inctx
= X509_STORE_CTX_new();
534 if (!X509_STORE_CTX_init(inctx
, SSL_CTX_get_cert_store(ssl_ctx
),
537 obj
= X509_STORE_CTX_get_obj_by_subject(inctx
, X509_LU_X509
, iname
);
539 BIO_puts(bio_err
, "cert_status: Can't retrieve issuer certificate.\n");
542 id
= OCSP_cert_to_id(NULL
, x
, X509_OBJECT_get0_X509(obj
));
543 X509_OBJECT_free(obj
);
547 req
= OCSP_REQUEST_new();
550 if (!OCSP_request_add0_id(req
, id
))
553 /* Add any extensions to the request */
554 SSL_get_tlsext_status_exts(s
, &exts
);
555 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
556 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
557 if (!OCSP_REQUEST_add_ext(req
, ext
, -1))
560 *resp
= process_responder(req
, host
, port
, path
, proxy
, no_proxy
,
561 use_ssl
, NULL
/* headers */, srctx
->timeout
);
563 BIO_puts(bio_err
, "cert_status: error querying responder\n");
567 ret
= SSL_TLSEXT_ERR_OK
;
571 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
574 * If we parsed aia we need to free; otherwise they were copied and we
581 X509_email_free(aia
);
583 OCSP_CERTID_free(id
);
584 OCSP_REQUEST_free(req
);
585 X509_STORE_CTX_free(inctx
);
590 * Certificate Status callback. This is called when a client includes a
591 * certificate status request extension. The response is either obtained from a
592 * file, or from an OCSP responder.
594 static int cert_status_cb(SSL
*s
, void *arg
)
596 tlsextstatusctx
*srctx
= arg
;
597 OCSP_RESPONSE
*resp
= NULL
;
598 unsigned char *rspder
= NULL
;
600 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
603 BIO_puts(bio_err
, "cert_status: callback called\n");
605 if (srctx
->respin
!= NULL
) {
606 BIO
*derbio
= bio_open_default(srctx
->respin
, 'r', FORMAT_ASN1
);
607 if (derbio
== NULL
) {
608 BIO_puts(bio_err
, "cert_status: Cannot open OCSP response file\n");
611 resp
= d2i_OCSP_RESPONSE_bio(derbio
, NULL
);
614 BIO_puts(bio_err
, "cert_status: Error reading OCSP response\n");
618 ret
= get_ocsp_resp_from_responder(s
, srctx
, &resp
);
619 if (ret
!= SSL_TLSEXT_ERR_OK
)
623 rspderlen
= i2d_OCSP_RESPONSE(resp
, &rspder
);
627 SSL_set_tlsext_status_ocsp_resp(s
, rspder
, rspderlen
);
628 if (srctx
->verbose
) {
629 BIO_puts(bio_err
, "cert_status: ocsp response sent:\n");
630 OCSP_RESPONSE_print(bio_err
, resp
, 2);
633 ret
= SSL_TLSEXT_ERR_OK
;
636 if (ret
!= SSL_TLSEXT_ERR_OK
)
637 ERR_print_errors(bio_err
);
639 OCSP_RESPONSE_free(resp
);
645 #ifndef OPENSSL_NO_NEXTPROTONEG
646 /* This is the context that we pass to next_proto_cb */
647 typedef struct tlsextnextprotoctx_st
{
650 } tlsextnextprotoctx
;
652 static int next_proto_cb(SSL
*s
, const unsigned char **data
,
653 unsigned int *len
, void *arg
)
655 tlsextnextprotoctx
*next_proto
= arg
;
657 *data
= next_proto
->data
;
658 *len
= next_proto
->len
;
660 return SSL_TLSEXT_ERR_OK
;
662 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
664 /* This the context that we pass to alpn_cb */
665 typedef struct tlsextalpnctx_st
{
670 static int alpn_cb(SSL
*s
, const unsigned char **out
, unsigned char *outlen
,
671 const unsigned char *in
, unsigned int inlen
, void *arg
)
673 tlsextalpnctx
*alpn_ctx
= arg
;
676 /* We can assume that |in| is syntactically valid. */
678 BIO_printf(bio_s_out
, "ALPN protocols advertised by the client: ");
679 for (i
= 0; i
< inlen
;) {
681 BIO_write(bio_s_out
, ", ", 2);
682 BIO_write(bio_s_out
, &in
[i
+ 1], in
[i
]);
685 BIO_write(bio_s_out
, "\n", 1);
688 if (SSL_select_next_proto
689 ((unsigned char **)out
, outlen
, alpn_ctx
->data
, alpn_ctx
->len
, in
,
690 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
691 return SSL_TLSEXT_ERR_ALERT_FATAL
;
695 BIO_printf(bio_s_out
, "ALPN protocols selected: ");
696 BIO_write(bio_s_out
, *out
, *outlen
);
697 BIO_write(bio_s_out
, "\n", 1);
700 return SSL_TLSEXT_ERR_OK
;
703 static int not_resumable_sess_cb(SSL
*s
, int is_forward_secure
)
705 /* disable resumption for sessions with forward secure ciphers */
706 return is_forward_secure
;
709 typedef enum OPTION_choice
{
712 OPT_4
, OPT_6
, OPT_ACCEPT
, OPT_PORT
, OPT_UNIX
, OPT_UNLINK
, OPT_NACCEPT
,
713 OPT_VERIFY
, OPT_NAMEOPT
, OPT_UPPER_V_VERIFY
, OPT_CONTEXT
, OPT_CERT
, OPT_CRL
,
714 OPT_CRL_DOWNLOAD
, OPT_SERVERINFO
, OPT_CERTFORM
, OPT_KEY
, OPT_KEYFORM
,
715 OPT_PASS
, OPT_CERT_CHAIN
, OPT_DHPARAM
, OPT_DCERTFORM
, OPT_DCERT
,
716 OPT_DKEYFORM
, OPT_DPASS
, OPT_DKEY
, OPT_DCERT_CHAIN
, OPT_NOCERT
,
717 OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
, OPT_NO_CACHE
,
718 OPT_EXT_CACHE
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
719 OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
, OPT_CHAINCAFILE
,
721 OPT_CASTORE
, OPT_NOCASTORE
, OPT_CHAINCASTORE
, OPT_VERIFYCASTORE
,
722 OPT_NBIO
, OPT_NBIO_TEST
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
723 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_STATUS_VERBOSE
,
724 OPT_STATUS_TIMEOUT
, OPT_PROXY
, OPT_NO_PROXY
, OPT_STATUS_URL
,
725 OPT_STATUS_FILE
, OPT_MSG
, OPT_MSGFILE
,
726 OPT_TRACE
, OPT_SECURITY_DEBUG
, OPT_SECURITY_DEBUG_VERBOSE
, OPT_STATE
,
727 OPT_CRLF
, OPT_QUIET
, OPT_BRIEF
, OPT_NO_DHE
,
728 OPT_NO_RESUME_EPHEMERAL
, OPT_PSK_IDENTITY
, OPT_PSK_HINT
, OPT_PSK
,
729 OPT_PSK_SESS
, OPT_SRPVFILE
, OPT_SRPUSERSEED
, OPT_REV
, OPT_WWW
,
730 OPT_UPPER_WWW
, OPT_HTTP
, OPT_ASYNC
, OPT_SSL_CONFIG
,
731 OPT_MAX_SEND_FRAG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
, OPT_READ_BUF
,
732 OPT_SSL3
, OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
733 OPT_DTLS1_2
, OPT_SCTP
, OPT_TIMEOUT
, OPT_MTU
, OPT_LISTEN
, OPT_STATELESS
,
734 OPT_ID_PREFIX
, OPT_SERVERNAME
, OPT_SERVERNAME_FATAL
,
735 OPT_CERT2
, OPT_KEY2
, OPT_NEXTPROTONEG
, OPT_ALPN
, OPT_SENDFILE
,
736 OPT_SRTP_PROFILES
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
,
737 OPT_KEYLOG_FILE
, OPT_MAX_EARLY
, OPT_RECV_MAX_EARLY
, OPT_EARLY_DATA
,
738 OPT_S_NUM_TICKETS
, OPT_ANTI_REPLAY
, OPT_NO_ANTI_REPLAY
, OPT_SCTP_LABEL_BUG
,
739 OPT_HTTP_SERVER_BINMODE
, OPT_NOCANAMES
, OPT_IGNORE_UNEXPECTED_EOF
, OPT_KTLS
,
741 OPT_TFO
, OPT_CERT_COMP
,
742 OPT_ENABLE_SERVER_RPK
,
743 OPT_ENABLE_CLIENT_RPK
,
751 const OPTIONS s_server_options
[] = {
752 OPT_SECTION("General"),
753 {"help", OPT_HELP
, '-', "Display this summary"},
754 {"ssl_config", OPT_SSL_CONFIG
, 's',
755 "Configure SSL_CTX using the given configuration value"},
756 #ifndef OPENSSL_NO_SSL_TRACE
757 {"trace", OPT_TRACE
, '-', "trace protocol messages"},
759 #ifndef OPENSSL_NO_ENGINE
760 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
763 OPT_SECTION("Network"),
764 {"port", OPT_PORT
, 'p',
765 "TCP/IP port to listen on for connections (default is " PORT
")"},
766 {"accept", OPT_ACCEPT
, 's',
767 "TCP/IP optional host and port to listen on for connections (default is *:" PORT
")"},
769 {"unix", OPT_UNIX
, 's', "Unix domain socket to accept on"},
770 {"unlink", OPT_UNLINK
, '-', "For -unix, unlink existing socket first"},
772 {"4", OPT_4
, '-', "Use IPv4 only"},
773 {"6", OPT_6
, '-', "Use IPv6 only"},
774 #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
775 {"tfo", OPT_TFO
, '-', "Listen for TCP Fast Open connections"},
778 OPT_SECTION("Identity"),
779 {"context", OPT_CONTEXT
, 's', "Set session ID context"},
780 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
781 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
782 {"CAstore", OPT_CASTORE
, ':', "URI to store of CA's"},
783 {"no-CAfile", OPT_NOCAFILE
, '-',
784 "Do not load the default certificates file"},
785 {"no-CApath", OPT_NOCAPATH
, '-',
786 "Do not load certificates from the default certificates directory"},
787 {"no-CAstore", OPT_NOCASTORE
, '-',
788 "Do not load certificates from the default certificates store URI"},
789 {"nocert", OPT_NOCERT
, '-', "Don't use any certificates (Anon-DH)"},
790 {"verify", OPT_VERIFY
, 'n', "Turn on peer certificate verification"},
791 {"Verify", OPT_UPPER_V_VERIFY
, 'n',
792 "Turn on peer certificate verification, must have a cert"},
793 {"nameopt", OPT_NAMEOPT
, 's', "Certificate subject/issuer name printing options"},
794 {"cert", OPT_CERT
, '<', "Server certificate file to use; default " TEST_CERT
},
795 {"cert2", OPT_CERT2
, '<',
796 "Certificate file to use for servername; default " TEST_CERT2
},
797 {"certform", OPT_CERTFORM
, 'F',
798 "Server certificate file format (PEM/DER/P12); has no effect"},
799 {"cert_chain", OPT_CERT_CHAIN
, '<',
800 "Server certificate chain file in PEM format"},
801 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build server certificate chain"},
802 {"serverinfo", OPT_SERVERINFO
, 's',
803 "PEM serverinfo file for certificate"},
804 {"key", OPT_KEY
, 's',
805 "Private key file to use; default is -cert file or else" TEST_CERT
},
806 {"key2", OPT_KEY2
, '<',
807 "-Private Key file to use for servername if not in -cert2"},
808 {"keyform", OPT_KEYFORM
, 'f', "Key format (ENGINE, other values ignored)"},
809 {"pass", OPT_PASS
, 's', "Private key and cert file pass phrase source"},
810 {"dcert", OPT_DCERT
, '<',
811 "Second server certificate file to use (usually for DSA)"},
812 {"dcertform", OPT_DCERTFORM
, 'F',
813 "Second server certificate file format (PEM/DER/P12); has no effect"},
814 {"dcert_chain", OPT_DCERT_CHAIN
, '<',
815 "second server certificate chain file in PEM format"},
816 {"dkey", OPT_DKEY
, '<',
817 "Second private key file to use (usually for DSA)"},
818 {"dkeyform", OPT_DKEYFORM
, 'f',
819 "Second key file format (ENGINE, other values ignored)"},
820 {"dpass", OPT_DPASS
, 's',
821 "Second private key and cert file pass phrase source"},
822 {"dhparam", OPT_DHPARAM
, '<', "DH parameters file to use"},
823 {"servername", OPT_SERVERNAME
, 's',
824 "Servername for HostName TLS extension"},
825 {"servername_fatal", OPT_SERVERNAME_FATAL
, '-',
826 "On servername mismatch send fatal alert (default warning alert)"},
827 {"nbio_test", OPT_NBIO_TEST
, '-', "Test with the non-blocking test bio"},
828 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
829 {"quiet", OPT_QUIET
, '-', "No server output"},
830 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL
, '-',
831 "Disable caching and tickets if ephemeral (EC)DH is used"},
832 {"www", OPT_WWW
, '-', "Respond to a 'GET /' with a status page"},
833 {"WWW", OPT_UPPER_WWW
, '-', "Respond to a 'GET with the file ./path"},
834 {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF
, '-',
835 "Do not treat lack of close_notify from a peer as an error"},
836 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
837 "Hex dump of all TLS extensions received"},
838 {"HTTP", OPT_HTTP
, '-', "Like -WWW but ./path includes HTTP headers"},
839 {"id_prefix", OPT_ID_PREFIX
, 's',
840 "Generate SSL/TLS session IDs prefixed by arg"},
841 {"keymatexport", OPT_KEYMATEXPORT
, 's',
842 "Export keying material using label"},
843 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
844 "Export len bytes of keying material; default 20"},
845 {"CRL", OPT_CRL
, '<', "CRL file to use"},
846 {"CRLform", OPT_CRLFORM
, 'F', "CRL file format (PEM or DER); default PEM"},
847 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
848 "Download CRLs from distribution points in certificate CDP entries"},
849 {"chainCAfile", OPT_CHAINCAFILE
, '<',
850 "CA file for certificate chain (PEM format)"},
851 {"chainCApath", OPT_CHAINCAPATH
, '/',
852 "use dir as certificate store path to build CA certificate chain"},
853 {"chainCAstore", OPT_CHAINCASTORE
, ':',
854 "use URI as certificate store to build CA certificate chain"},
855 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
856 "CA file for certificate verification (PEM format)"},
857 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
858 "use dir as certificate store path to verify CA certificate"},
859 {"verifyCAstore", OPT_VERIFYCASTORE
, ':',
860 "use URI as certificate store to verify CA certificate"},
861 {"no_cache", OPT_NO_CACHE
, '-', "Disable session cache"},
862 {"ext_cache", OPT_EXT_CACHE
, '-',
863 "Disable internal cache, set up and use external cache"},
864 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
865 "Close connection on verification error"},
866 {"verify_quiet", OPT_VERIFY_QUIET
, '-',
867 "No verify output except verify errors"},
868 {"ign_eof", OPT_IGN_EOF
, '-', "Ignore input EOF (default when -quiet)"},
869 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Do not ignore input EOF"},
870 #ifndef OPENSSL_NO_COMP_ALG
871 {"cert_comp", OPT_CERT_COMP
, '-', "Pre-compress server certificates"},
874 #ifndef OPENSSL_NO_OCSP
876 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
877 {"status_verbose", OPT_STATUS_VERBOSE
, '-',
878 "Print more output in certificate status callback"},
879 {"status_timeout", OPT_STATUS_TIMEOUT
, 'n',
880 "Status request responder timeout"},
881 {"status_url", OPT_STATUS_URL
, 's', "Status request fallback URL"},
882 {"proxy", OPT_PROXY
, 's',
883 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
884 {"no_proxy", OPT_NO_PROXY
, 's',
885 "List of addresses of servers not to use HTTP(S) proxy for"},
887 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
888 {"status_file", OPT_STATUS_FILE
, '<',
889 "File containing DER encoded OCSP Response"},
892 OPT_SECTION("Debug"),
893 {"security_debug", OPT_SECURITY_DEBUG
, '-',
894 "Print output from SSL/TLS security framework"},
895 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
896 "Print more output from SSL/TLS security framework"},
897 {"brief", OPT_BRIEF
, '-',
898 "Restrict output to brief summary of connection parameters"},
899 {"rev", OPT_REV
, '-',
900 "act as an echo server that sends back received text reversed"},
901 {"debug", OPT_DEBUG
, '-', "Print more output"},
902 {"msg", OPT_MSG
, '-', "Show protocol messages"},
903 {"msgfile", OPT_MSGFILE
, '>',
904 "File to send output of -msg or -trace, instead of stdout"},
905 {"state", OPT_STATE
, '-', "Print the SSL states"},
906 {"async", OPT_ASYNC
, '-', "Operate in asynchronous mode"},
907 {"max_pipelines", OPT_MAX_PIPELINES
, 'p',
908 "Maximum number of encrypt/decrypt pipelines to be used"},
909 {"naccept", OPT_NACCEPT
, 'p', "Terminate after #num connections"},
910 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
912 OPT_SECTION("Network"),
913 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
914 {"timeout", OPT_TIMEOUT
, '-', "Enable timeouts"},
915 {"mtu", OPT_MTU
, 'p', "Set link-layer MTU"},
916 {"read_buf", OPT_READ_BUF
, 'p',
917 "Default read buffer size to be used for connections"},
918 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'p',
919 "Size used to split data for encrypt pipelines"},
920 {"max_send_frag", OPT_MAX_SEND_FRAG
, 'p', "Maximum Size of send frames "},
922 OPT_SECTION("Server identity"),
923 {"psk_identity", OPT_PSK_IDENTITY
, 's', "PSK identity to expect"},
924 #ifndef OPENSSL_NO_PSK
925 {"psk_hint", OPT_PSK_HINT
, 's', "PSK identity hint to use"},
927 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
928 {"psk_session", OPT_PSK_SESS
, '<', "File to read PSK SSL session from"},
929 #ifndef OPENSSL_NO_SRP
930 {"srpvfile", OPT_SRPVFILE
, '<', "(deprecated) The verifier file for SRP"},
931 {"srpuserseed", OPT_SRPUSERSEED
, 's',
932 "(deprecated) A seed string for a default user salt"},
935 OPT_SECTION("Protocol and version"),
936 {"max_early_data", OPT_MAX_EARLY
, 'n',
937 "The maximum number of bytes of early data as advertised in tickets"},
938 {"recv_max_early_data", OPT_RECV_MAX_EARLY
, 'n',
939 "The maximum number of bytes of early data (hard limit)"},
940 {"early_data", OPT_EARLY_DATA
, '-', "Attempt to read early data"},
941 {"num_tickets", OPT_S_NUM_TICKETS
, 'n',
942 "The number of TLSv1.3 session tickets that a server will automatically issue" },
943 {"anti_replay", OPT_ANTI_REPLAY
, '-', "Switch on anti-replay protection (default)"},
944 {"no_anti_replay", OPT_NO_ANTI_REPLAY
, '-', "Switch off anti-replay protection"},
945 {"http_server_binmode", OPT_HTTP_SERVER_BINMODE
, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)"},
946 {"no_ca_names", OPT_NOCANAMES
, '-',
947 "Disable TLS Extension CA Names"},
948 {"stateless", OPT_STATELESS
, '-', "Require TLSv1.3 cookies"},
949 #ifndef OPENSSL_NO_SSL3
950 {"ssl3", OPT_SSL3
, '-', "Just talk SSLv3"},
952 #ifndef OPENSSL_NO_TLS1
953 {"tls1", OPT_TLS1
, '-', "Just talk TLSv1"},
955 #ifndef OPENSSL_NO_TLS1_1
956 {"tls1_1", OPT_TLS1_1
, '-', "Just talk TLSv1.1"},
958 #ifndef OPENSSL_NO_TLS1_2
959 {"tls1_2", OPT_TLS1_2
, '-', "just talk TLSv1.2"},
961 #ifndef OPENSSL_NO_TLS1_3
962 {"tls1_3", OPT_TLS1_3
, '-', "just talk TLSv1.3"},
964 #ifndef OPENSSL_NO_DTLS
965 {"dtls", OPT_DTLS
, '-', "Use any DTLS version"},
966 {"listen", OPT_LISTEN
, '-',
967 "Listen for a DTLS ClientHello with a cookie and then connect"},
969 #ifndef OPENSSL_NO_DTLS1
970 {"dtls1", OPT_DTLS1
, '-', "Just talk DTLSv1"},
972 #ifndef OPENSSL_NO_DTLS1_2
973 {"dtls1_2", OPT_DTLS1_2
, '-', "Just talk DTLSv1.2"},
975 #ifndef OPENSSL_NO_SCTP
976 {"sctp", OPT_SCTP
, '-', "Use SCTP"},
977 {"sctp_label_bug", OPT_SCTP_LABEL_BUG
, '-', "Enable SCTP label length bug"},
979 #ifndef OPENSSL_NO_SRTP
980 {"use_srtp", OPT_SRTP_PROFILES
, 's',
981 "Offer SRTP key management with a colon-separated profile list"},
983 {"no_dhe", OPT_NO_DHE
, '-', "Disable ephemeral DH"},
984 #ifndef OPENSSL_NO_NEXTPROTONEG
985 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
986 "Set the advertised protocols for the NPN extension (comma-separated list)"},
988 {"alpn", OPT_ALPN
, 's',
989 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
990 #ifndef OPENSSL_NO_KTLS
991 {"ktls", OPT_KTLS
, '-', "Enable Kernel TLS for sending and receiving"},
992 {"sendfile", OPT_SENDFILE
, '-', "Use sendfile to response file with -WWW"},
993 {"zerocopy_sendfile", OPT_USE_ZC_SENDFILE
, '-', "Use zerocopy mode of KTLS sendfile"},
995 {"enable_server_rpk", OPT_ENABLE_SERVER_RPK
, '-', "Enable raw public keys (RFC7250) from the server"},
996 {"enable_client_rpk", OPT_ENABLE_CLIENT_RPK
, '-', "Enable raw public keys (RFC7250) from the client"},
1005 #define IS_PROT_FLAG(o) \
1006 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
1007 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
1009 int s_server_main(int argc
, char *argv
[])
1011 ENGINE
*engine
= NULL
;
1012 EVP_PKEY
*s_key
= NULL
, *s_dkey
= NULL
;
1013 SSL_CONF_CTX
*cctx
= NULL
;
1014 const SSL_METHOD
*meth
= TLS_server_method();
1015 SSL_EXCERT
*exc
= NULL
;
1016 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
1017 STACK_OF(X509
) *s_chain
= NULL
, *s_dchain
= NULL
;
1018 STACK_OF(X509_CRL
) *crls
= NULL
;
1019 X509
*s_cert
= NULL
, *s_dcert
= NULL
;
1020 X509_VERIFY_PARAM
*vpm
= NULL
;
1021 const char *CApath
= NULL
, *CAfile
= NULL
, *CAstore
= NULL
;
1022 const char *chCApath
= NULL
, *chCAfile
= NULL
, *chCAstore
= NULL
;
1023 char *dpassarg
= NULL
, *dpass
= NULL
;
1024 char *passarg
= NULL
, *pass
= NULL
;
1025 char *vfyCApath
= NULL
, *vfyCAfile
= NULL
, *vfyCAstore
= NULL
;
1026 char *crl_file
= NULL
, *prog
;
1028 int unlink_unix_path
= 0;
1030 do_server_cb server_cb
;
1031 int vpmtouched
= 0, build_chain
= 0, no_cache
= 0, ext_cache
= 0;
1032 char *dhfile
= NULL
;
1034 int nocert
= 0, ret
= 1;
1035 int noCApath
= 0, noCAfile
= 0, noCAstore
= 0;
1036 int s_cert_format
= FORMAT_UNDEF
, s_key_format
= FORMAT_UNDEF
;
1037 int s_dcert_format
= FORMAT_UNDEF
, s_dkey_format
= FORMAT_UNDEF
;
1038 int rev
= 0, naccept
= -1, sdebug
= 0;
1039 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
, protocol
= 0;
1040 int state
= 0, crl_format
= FORMAT_UNDEF
, crl_download
= 0;
1043 unsigned char *context
= NULL
;
1045 EVP_PKEY
*s_key2
= NULL
;
1046 X509
*s_cert2
= NULL
;
1047 tlsextctx tlsextcbp
= { NULL
, NULL
, SSL_TLSEXT_ERR_ALERT_WARNING
};
1048 const char *ssl_config
= NULL
;
1049 int read_buf_len
= 0;
1050 #ifndef OPENSSL_NO_NEXTPROTONEG
1051 const char *next_proto_neg_in
= NULL
;
1052 tlsextnextprotoctx next_proto
= { NULL
, 0 };
1054 const char *alpn_in
= NULL
;
1055 tlsextalpnctx alpn_ctx
= { NULL
, 0 };
1056 #ifndef OPENSSL_NO_PSK
1057 /* by default do not send a PSK identity hint */
1058 char *psk_identity_hint
= NULL
;
1061 #ifndef OPENSSL_NO_SRP
1062 char *srpuserseed
= NULL
;
1063 char *srp_verifier_file
= NULL
;
1065 #ifndef OPENSSL_NO_SRTP
1066 char *srtp_profiles
= NULL
;
1068 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
1069 int s_server_verify
= SSL_VERIFY_NONE
;
1070 int s_server_session_id_context
= 1; /* anything will do */
1071 const char *s_cert_file
= TEST_CERT
, *s_key_file
= NULL
, *s_chain_file
= NULL
;
1072 const char *s_cert_file2
= TEST_CERT2
, *s_key_file2
= NULL
;
1073 char *s_dcert_file
= NULL
, *s_dkey_file
= NULL
, *s_dchain_file
= NULL
;
1074 #ifndef OPENSSL_NO_OCSP
1075 int s_tlsextstatus
= 0;
1077 int no_resume_ephemeral
= 0;
1078 unsigned int max_send_fragment
= 0;
1079 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
1080 const char *s_serverinfo_file
= NULL
;
1081 const char *keylog_file
= NULL
;
1082 int max_early_data
= -1, recv_max_early_data
= -1;
1083 char *psksessf
= NULL
;
1084 int no_ca_names
= 0;
1085 #ifndef OPENSSL_NO_SCTP
1086 int sctp_label_bug
= 0;
1088 int ignore_unexpected_eof
= 0;
1089 #ifndef OPENSSL_NO_KTLS
1090 int enable_ktls
= 0;
1094 int enable_server_rpk
= 0;
1096 /* Init of few remaining global variables */
1101 s_nbio
= s_nbio_test
= 0;
1110 use_zc_sendfile
= 0;
1112 port
= OPENSSL_strdup(PORT
);
1113 cctx
= SSL_CONF_CTX_new();
1114 vpm
= X509_VERIFY_PARAM_new();
1115 if (port
== NULL
|| cctx
== NULL
|| vpm
== NULL
)
1117 SSL_CONF_CTX_set_flags(cctx
,
1118 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CMDLINE
);
1120 prog
= opt_init(argc
, argv
, s_server_options
);
1121 while ((o
= opt_next()) != OPT_EOF
) {
1122 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1123 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1126 if (IS_NO_PROT_FLAG(o
))
1128 if (prot_opt
== 1 && no_prot_opt
) {
1130 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1137 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1140 opt_help(s_server_options
);
1146 if (socket_family
== AF_UNIX
) {
1147 OPENSSL_free(host
); host
= NULL
;
1148 OPENSSL_free(port
); port
= NULL
;
1151 socket_family
= AF_INET
;
1157 if (socket_family
== AF_UNIX
) {
1158 OPENSSL_free(host
); host
= NULL
;
1159 OPENSSL_free(port
); port
= NULL
;
1162 socket_family
= AF_INET6
;
1165 BIO_printf(bio_err
, "%s: IPv6 domain sockets unsupported\n", prog
);
1171 if (socket_family
== AF_UNIX
) {
1172 socket_family
= AF_UNSPEC
;
1175 OPENSSL_free(port
); port
= NULL
;
1176 OPENSSL_free(host
); host
= NULL
;
1177 if (BIO_parse_hostserv(opt_arg(), NULL
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1179 "%s: -port argument malformed or ambiguous\n",
1186 if (socket_family
== AF_UNIX
) {
1187 socket_family
= AF_UNSPEC
;
1190 OPENSSL_free(port
); port
= NULL
;
1191 OPENSSL_free(host
); host
= NULL
;
1192 if (BIO_parse_hostserv(opt_arg(), &host
, &port
, BIO_PARSE_PRIO_SERV
) < 1) {
1194 "%s: -accept argument malformed or ambiguous\n",
1201 socket_family
= AF_UNIX
;
1202 OPENSSL_free(host
); host
= OPENSSL_strdup(opt_arg());
1205 OPENSSL_free(port
); port
= NULL
;
1208 unlink_unix_path
= 1;
1212 naccept
= atol(opt_arg());
1215 s_server_verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
1216 verify_args
.depth
= atoi(opt_arg());
1218 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1220 case OPT_UPPER_V_VERIFY
:
1222 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1223 SSL_VERIFY_CLIENT_ONCE
;
1224 verify_args
.depth
= atoi(opt_arg());
1227 "verify depth is %d, must return a certificate\n",
1231 context
= (unsigned char *)opt_arg();
1234 s_cert_file
= opt_arg();
1237 if (!set_nameopt(opt_arg()))
1241 crl_file
= opt_arg();
1243 case OPT_CRL_DOWNLOAD
:
1246 case OPT_SERVERINFO
:
1247 s_serverinfo_file
= opt_arg();
1250 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_cert_format
))
1254 s_key_file
= opt_arg();
1257 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_key_format
))
1261 passarg
= opt_arg();
1263 case OPT_CERT_CHAIN
:
1264 s_chain_file
= opt_arg();
1270 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dcert_format
))
1274 s_dcert_file
= opt_arg();
1277 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &s_dkey_format
))
1281 dpassarg
= opt_arg();
1284 s_dkey_file
= opt_arg();
1286 case OPT_DCERT_CHAIN
:
1287 s_dchain_file
= opt_arg();
1298 case OPT_CHAINCAPATH
:
1299 chCApath
= opt_arg();
1301 case OPT_VERIFYCAPATH
:
1302 vfyCApath
= opt_arg();
1305 CAstore
= opt_arg();
1310 case OPT_CHAINCASTORE
:
1311 chCAstore
= opt_arg();
1313 case OPT_VERIFYCASTORE
:
1314 vfyCAstore
= opt_arg();
1323 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1327 case OPT_S_NUM_TICKETS
:
1328 case OPT_ANTI_REPLAY
:
1329 case OPT_NO_ANTI_REPLAY
:
1330 if (ssl_args
== NULL
)
1331 ssl_args
= sk_OPENSSL_STRING_new_null();
1332 if (ssl_args
== NULL
1333 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1334 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1335 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1340 if (!opt_verify(o
, vpm
))
1345 if (!args_excert(o
, &exc
))
1348 case OPT_VERIFY_RET_ERROR
:
1349 verify_args
.return_error
= 1;
1351 case OPT_VERIFY_QUIET
:
1352 verify_args
.quiet
= 1;
1354 case OPT_BUILD_CHAIN
:
1363 case OPT_CHAINCAFILE
:
1364 chCAfile
= opt_arg();
1366 case OPT_VERIFYCAFILE
:
1367 vfyCAfile
= opt_arg();
1373 s_nbio
= s_nbio_test
= 1;
1378 case OPT_NO_IGN_EOF
:
1384 case OPT_TLSEXTDEBUG
:
1388 #ifndef OPENSSL_NO_OCSP
1392 case OPT_STATUS_VERBOSE
:
1393 #ifndef OPENSSL_NO_OCSP
1394 s_tlsextstatus
= tlscstatp
.verbose
= 1;
1397 case OPT_STATUS_TIMEOUT
:
1398 #ifndef OPENSSL_NO_OCSP
1400 tlscstatp
.timeout
= atoi(opt_arg());
1404 #ifndef OPENSSL_NO_OCSP
1405 tlscstatp
.proxy
= opt_arg();
1409 #ifndef OPENSSL_NO_OCSP
1410 tlscstatp
.no_proxy
= opt_arg();
1413 case OPT_STATUS_URL
:
1414 #ifndef OPENSSL_NO_OCSP
1416 if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp
.use_ssl
, NULL
,
1417 &tlscstatp
.host
, &tlscstatp
.port
, NULL
,
1418 &tlscstatp
.path
, NULL
, NULL
)) {
1419 BIO_printf(bio_err
, "Error parsing -status_url argument\n");
1424 case OPT_STATUS_FILE
:
1425 #ifndef OPENSSL_NO_OCSP
1427 tlscstatp
.respin
= opt_arg();
1434 bio_s_msg
= BIO_new_file(opt_arg(), "w");
1435 if (bio_s_msg
== NULL
) {
1436 BIO_printf(bio_err
, "Error writing file %s\n", opt_arg());
1441 #ifndef OPENSSL_NO_SSL_TRACE
1445 case OPT_SECURITY_DEBUG
:
1448 case OPT_SECURITY_DEBUG_VERBOSE
:
1461 s_quiet
= s_brief
= verify_args
.quiet
= 1;
1466 case OPT_NO_RESUME_EPHEMERAL
:
1467 no_resume_ephemeral
= 1;
1469 case OPT_PSK_IDENTITY
:
1470 psk_identity
= opt_arg();
1473 #ifndef OPENSSL_NO_PSK
1474 psk_identity_hint
= opt_arg();
1478 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1479 if (isxdigit(_UC(*p
)))
1481 BIO_printf(bio_err
, "Not a hex number '%s'\n", psk_key
);
1486 psksessf
= opt_arg();
1489 #ifndef OPENSSL_NO_SRP
1490 srp_verifier_file
= opt_arg();
1491 if (min_version
< TLS1_VERSION
)
1492 min_version
= TLS1_VERSION
;
1495 case OPT_SRPUSERSEED
:
1496 #ifndef OPENSSL_NO_SRP
1497 srpuserseed
= opt_arg();
1498 if (min_version
< TLS1_VERSION
)
1499 min_version
= TLS1_VERSION
;
1514 case OPT_SSL_CONFIG
:
1515 ssl_config
= opt_arg();
1518 min_version
= SSL3_VERSION
;
1519 max_version
= SSL3_VERSION
;
1522 min_version
= TLS1_3_VERSION
;
1523 max_version
= TLS1_3_VERSION
;
1526 min_version
= TLS1_2_VERSION
;
1527 max_version
= TLS1_2_VERSION
;
1530 min_version
= TLS1_1_VERSION
;
1531 max_version
= TLS1_1_VERSION
;
1534 min_version
= TLS1_VERSION
;
1535 max_version
= TLS1_VERSION
;
1538 #ifndef OPENSSL_NO_DTLS
1539 meth
= DTLS_server_method();
1540 socket_type
= SOCK_DGRAM
;
1544 #ifndef OPENSSL_NO_DTLS
1545 meth
= DTLS_server_method();
1546 min_version
= DTLS1_VERSION
;
1547 max_version
= DTLS1_VERSION
;
1548 socket_type
= SOCK_DGRAM
;
1552 #ifndef OPENSSL_NO_DTLS
1553 meth
= DTLS_server_method();
1554 min_version
= DTLS1_2_VERSION
;
1555 max_version
= DTLS1_2_VERSION
;
1556 socket_type
= SOCK_DGRAM
;
1560 #ifndef OPENSSL_NO_SCTP
1561 protocol
= IPPROTO_SCTP
;
1564 case OPT_SCTP_LABEL_BUG
:
1565 #ifndef OPENSSL_NO_SCTP
1570 #ifndef OPENSSL_NO_DTLS
1571 enable_timeouts
= 1;
1575 #ifndef OPENSSL_NO_DTLS
1576 socket_mtu
= atol(opt_arg());
1580 #ifndef OPENSSL_NO_DTLS
1588 session_id_prefix
= opt_arg();
1591 #ifndef OPENSSL_NO_ENGINE
1592 engine
= setup_engine(opt_arg(), s_debug
);
1599 case OPT_PROV_CASES
:
1600 if (!opt_provider(o
))
1603 case OPT_SERVERNAME
:
1604 tlsextcbp
.servername
= opt_arg();
1606 case OPT_SERVERNAME_FATAL
:
1607 tlsextcbp
.extension_error
= SSL_TLSEXT_ERR_ALERT_FATAL
;
1610 s_cert_file2
= opt_arg();
1613 s_key_file2
= opt_arg();
1615 case OPT_NEXTPROTONEG
:
1616 # ifndef OPENSSL_NO_NEXTPROTONEG
1617 next_proto_neg_in
= opt_arg();
1621 alpn_in
= opt_arg();
1623 case OPT_SRTP_PROFILES
:
1624 #ifndef OPENSSL_NO_SRTP
1625 srtp_profiles
= opt_arg();
1628 case OPT_KEYMATEXPORT
:
1629 keymatexportlabel
= opt_arg();
1631 case OPT_KEYMATEXPORTLEN
:
1632 keymatexportlen
= atoi(opt_arg());
1637 case OPT_MAX_SEND_FRAG
:
1638 max_send_fragment
= atoi(opt_arg());
1640 case OPT_SPLIT_SEND_FRAG
:
1641 split_send_fragment
= atoi(opt_arg());
1643 case OPT_MAX_PIPELINES
:
1644 max_pipelines
= atoi(opt_arg());
1647 read_buf_len
= atoi(opt_arg());
1649 case OPT_KEYLOG_FILE
:
1650 keylog_file
= opt_arg();
1653 max_early_data
= atoi(opt_arg());
1654 if (max_early_data
< 0) {
1655 BIO_printf(bio_err
, "Invalid value for max_early_data\n");
1659 case OPT_RECV_MAX_EARLY
:
1660 recv_max_early_data
= atoi(opt_arg());
1661 if (recv_max_early_data
< 0) {
1662 BIO_printf(bio_err
, "Invalid value for recv_max_early_data\n");
1666 case OPT_EARLY_DATA
:
1668 if (max_early_data
== -1)
1669 max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
1671 case OPT_HTTP_SERVER_BINMODE
:
1672 http_server_binmode
= 1;
1678 #ifndef OPENSSL_NO_KTLS
1683 #ifndef OPENSSL_NO_KTLS
1687 case OPT_USE_ZC_SENDFILE
:
1688 #ifndef OPENSSL_NO_KTLS
1689 use_zc_sendfile
= 1;
1692 case OPT_IGNORE_UNEXPECTED_EOF
:
1693 ignore_unexpected_eof
= 1;
1701 case OPT_ENABLE_SERVER_RPK
:
1702 enable_server_rpk
= 1;
1704 case OPT_ENABLE_CLIENT_RPK
:
1705 enable_client_rpk
= 1;
1710 /* No extra arguments. */
1711 if (!opt_check_rest_arg(NULL
))
1714 if (!app_RAND_load())
1717 #ifndef OPENSSL_NO_NEXTPROTONEG
1718 if (min_version
== TLS1_3_VERSION
&& next_proto_neg_in
!= NULL
) {
1719 BIO_printf(bio_err
, "Cannot supply -nextprotoneg with TLSv1.3\n");
1723 #ifndef OPENSSL_NO_DTLS
1724 if (www
&& socket_type
== SOCK_DGRAM
) {
1725 BIO_printf(bio_err
, "Can't use -HTTP, -www or -WWW with DTLS\n");
1729 if (dtlslisten
&& socket_type
!= SOCK_DGRAM
) {
1730 BIO_printf(bio_err
, "Can only use -listen with DTLS\n");
1734 if (rev
&& socket_type
== SOCK_DGRAM
) {
1735 BIO_printf(bio_err
, "Can't use -rev with DTLS\n");
1740 if (tfo
&& socket_type
!= SOCK_STREAM
) {
1741 BIO_printf(bio_err
, "Can only use -tfo with TLS\n");
1745 if (stateless
&& socket_type
!= SOCK_STREAM
) {
1746 BIO_printf(bio_err
, "Can only use --stateless with TLS\n");
1751 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1753 "Can't use unix sockets and datagrams together\n");
1757 if (early_data
&& (www
> 0 || rev
)) {
1759 "Can't use -early_data in combination with -www, -WWW, -HTTP, or -rev\n");
1763 #ifndef OPENSSL_NO_SCTP
1764 if (protocol
== IPPROTO_SCTP
) {
1765 if (socket_type
!= SOCK_DGRAM
) {
1766 BIO_printf(bio_err
, "Can't use -sctp without DTLS\n");
1769 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1770 socket_type
= SOCK_STREAM
;
1774 #ifndef OPENSSL_NO_KTLS
1775 if (use_zc_sendfile
&& !use_sendfile
) {
1776 BIO_printf(bio_out
, "Warning: -zerocopy_sendfile depends on -sendfile, enabling -sendfile now.\n");
1780 if (use_sendfile
&& enable_ktls
== 0) {
1781 BIO_printf(bio_out
, "Warning: -sendfile depends on -ktls, enabling -ktls now.\n");
1785 if (use_sendfile
&& www
<= 1) {
1786 BIO_printf(bio_err
, "Can't use -sendfile without -WWW or -HTTP\n");
1791 if (!app_passwd(passarg
, dpassarg
, &pass
, &dpass
)) {
1792 BIO_printf(bio_err
, "Error getting password\n");
1796 if (s_key_file
== NULL
)
1797 s_key_file
= s_cert_file
;
1799 if (s_key_file2
== NULL
)
1800 s_key_file2
= s_cert_file2
;
1802 if (!load_excert(&exc
))
1806 s_key
= load_key(s_key_file
, s_key_format
, 0, pass
, engine
,
1807 "server certificate private key");
1811 s_cert
= load_cert_pass(s_cert_file
, s_cert_format
, 1, pass
,
1812 "server certificate");
1816 if (s_chain_file
!= NULL
) {
1817 if (!load_certs(s_chain_file
, 0, &s_chain
, NULL
,
1818 "server certificate chain"))
1822 if (tlsextcbp
.servername
!= NULL
) {
1823 s_key2
= load_key(s_key_file2
, s_key_format
, 0, pass
, engine
,
1824 "second server certificate private key");
1828 s_cert2
= load_cert_pass(s_cert_file2
, s_cert_format
, 1, pass
,
1829 "second server certificate");
1831 if (s_cert2
== NULL
)
1835 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1836 if (next_proto_neg_in
) {
1837 next_proto
.data
= next_protos_parse(&next_proto
.len
, next_proto_neg_in
);
1838 if (next_proto
.data
== NULL
)
1842 alpn_ctx
.data
= NULL
;
1844 alpn_ctx
.data
= next_protos_parse(&alpn_ctx
.len
, alpn_in
);
1845 if (alpn_ctx
.data
== NULL
)
1849 if (crl_file
!= NULL
) {
1851 crl
= load_crl(crl_file
, crl_format
, 0, "CRL");
1854 crls
= sk_X509_CRL_new_null();
1855 if (crls
== NULL
|| !sk_X509_CRL_push(crls
, crl
)) {
1856 BIO_puts(bio_err
, "Error adding CRL\n");
1857 ERR_print_errors(bio_err
);
1863 if (s_dcert_file
!= NULL
) {
1865 if (s_dkey_file
== NULL
)
1866 s_dkey_file
= s_dcert_file
;
1868 s_dkey
= load_key(s_dkey_file
, s_dkey_format
,
1869 0, dpass
, engine
, "second certificate private key");
1873 s_dcert
= load_cert_pass(s_dcert_file
, s_dcert_format
, 1, dpass
,
1874 "second server certificate");
1876 if (s_dcert
== NULL
) {
1877 ERR_print_errors(bio_err
);
1880 if (s_dchain_file
!= NULL
) {
1881 if (!load_certs(s_dchain_file
, 0, &s_dchain
, NULL
,
1882 "second server certificate chain"))
1888 if (bio_s_out
== NULL
) {
1889 if (s_quiet
&& !s_debug
) {
1890 bio_s_out
= BIO_new(BIO_s_null());
1891 if (s_msg
&& bio_s_msg
== NULL
) {
1892 bio_s_msg
= dup_bio_out(FORMAT_TEXT
);
1893 if (bio_s_msg
== NULL
) {
1894 BIO_printf(bio_err
, "Out of memory\n");
1899 bio_s_out
= dup_bio_out(FORMAT_TEXT
);
1903 if (bio_s_out
== NULL
)
1909 s_dcert_file
= NULL
;
1911 s_cert_file2
= NULL
;
1915 ctx
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
1917 ERR_print_errors(bio_err
);
1921 SSL_CTX_clear_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1924 ssl_ctx_security_debug(ctx
, sdebug
);
1926 if (!config_ctx(cctx
, ssl_args
, ctx
))
1930 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1931 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1933 ERR_print_errors(bio_err
);
1937 #ifndef OPENSSL_NO_SCTP
1938 if (protocol
== IPPROTO_SCTP
&& sctp_label_bug
== 1)
1939 SSL_CTX_set_mode(ctx
, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG
);
1942 if (min_version
!= 0
1943 && SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1945 if (max_version
!= 0
1946 && SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1949 if (session_id_prefix
) {
1950 if (strlen(session_id_prefix
) >= 32)
1952 "warning: id_prefix is too long, only one new session will be possible\n");
1953 if (!SSL_CTX_set_generate_session_id(ctx
, generate_session_id
)) {
1954 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
1955 ERR_print_errors(bio_err
);
1958 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
1961 ssl_ctx_set_excert(ctx
, exc
);
1964 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1966 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_OFF
);
1968 init_session_cache_ctx(ctx
);
1970 SSL_CTX_sess_set_cache_size(ctx
, 128);
1973 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1977 SSL_CTX_set_options(ctx
, SSL_OP_DISABLE_TLSEXT_CA_NAMES
);
1980 if (ignore_unexpected_eof
)
1981 SSL_CTX_set_options(ctx
, SSL_OP_IGNORE_UNEXPECTED_EOF
);
1982 #ifndef OPENSSL_NO_KTLS
1984 SSL_CTX_set_options(ctx
, SSL_OP_ENABLE_KTLS
);
1985 if (use_zc_sendfile
)
1986 SSL_CTX_set_options(ctx
, SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE
);
1989 if (max_send_fragment
> 0
1990 && !SSL_CTX_set_max_send_fragment(ctx
, max_send_fragment
)) {
1991 BIO_printf(bio_err
, "%s: Max send fragment size %u is out of permitted range\n",
1992 prog
, max_send_fragment
);
1996 if (split_send_fragment
> 0
1997 && !SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
)) {
1998 BIO_printf(bio_err
, "%s: Split send fragment size %u is out of permitted range\n",
1999 prog
, split_send_fragment
);
2002 if (max_pipelines
> 0
2003 && !SSL_CTX_set_max_pipelines(ctx
, max_pipelines
)) {
2004 BIO_printf(bio_err
, "%s: Max pipelines %u is out of permitted range\n",
2005 prog
, max_pipelines
);
2009 if (read_buf_len
> 0) {
2010 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
2012 #ifndef OPENSSL_NO_SRTP
2013 if (srtp_profiles
!= NULL
) {
2014 /* Returns 0 on success! */
2015 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
2016 BIO_printf(bio_err
, "Error setting SRTP profile\n");
2017 ERR_print_errors(bio_err
);
2023 if (!ctx_set_verify_locations(ctx
, CAfile
, noCAfile
, CApath
, noCApath
,
2024 CAstore
, noCAstore
)) {
2025 ERR_print_errors(bio_err
);
2028 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
2029 BIO_printf(bio_err
, "Error setting verify params\n");
2030 ERR_print_errors(bio_err
);
2034 ssl_ctx_add_crls(ctx
, crls
, 0);
2036 if (!ssl_load_stores(ctx
,
2037 vfyCApath
, vfyCAfile
, vfyCAstore
,
2038 chCApath
, chCAfile
, chCAstore
,
2039 crls
, crl_download
)) {
2040 BIO_printf(bio_err
, "Error loading store locations\n");
2041 ERR_print_errors(bio_err
);
2046 ctx2
= SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth
);
2048 ERR_print_errors(bio_err
);
2054 BIO_printf(bio_s_out
, "Setting secondary ctx parameters\n");
2057 ssl_ctx_security_debug(ctx2
, sdebug
);
2059 if (session_id_prefix
) {
2060 if (strlen(session_id_prefix
) >= 32)
2062 "warning: id_prefix is too long, only one new session will be possible\n");
2063 if (!SSL_CTX_set_generate_session_id(ctx2
, generate_session_id
)) {
2064 BIO_printf(bio_err
, "error setting 'id_prefix'\n");
2065 ERR_print_errors(bio_err
);
2068 BIO_printf(bio_err
, "id_prefix '%s' set.\n", session_id_prefix
);
2071 ssl_ctx_set_excert(ctx2
, exc
);
2074 SSL_CTX_set_info_callback(ctx2
, apps_ssl_info_callback
);
2077 SSL_CTX_set_session_cache_mode(ctx2
, SSL_SESS_CACHE_OFF
);
2079 init_session_cache_ctx(ctx2
);
2081 SSL_CTX_sess_set_cache_size(ctx2
, 128);
2084 SSL_CTX_set_mode(ctx2
, SSL_MODE_ASYNC
);
2086 if (!ctx_set_verify_locations(ctx2
, CAfile
, noCAfile
, CApath
,
2087 noCApath
, CAstore
, noCAstore
)) {
2088 ERR_print_errors(bio_err
);
2091 if (vpmtouched
&& !SSL_CTX_set1_param(ctx2
, vpm
)) {
2092 BIO_printf(bio_err
, "Error setting verify params\n");
2093 ERR_print_errors(bio_err
);
2097 ssl_ctx_add_crls(ctx2
, crls
, 0);
2098 if (!config_ctx(cctx
, ssl_args
, ctx2
))
2101 #ifndef OPENSSL_NO_NEXTPROTONEG
2102 if (next_proto
.data
)
2103 SSL_CTX_set_next_protos_advertised_cb(ctx
, next_proto_cb
,
2107 SSL_CTX_set_alpn_select_cb(ctx
, alpn_cb
, &alpn_ctx
);
2110 EVP_PKEY
*dhpkey
= NULL
;
2113 dhpkey
= load_keyparams(dhfile
, FORMAT_UNDEF
, 0, "DH", "DH parameters");
2114 else if (s_cert_file
!= NULL
)
2115 dhpkey
= load_keyparams_suppress(s_cert_file
, FORMAT_UNDEF
, 0, "DH",
2116 "DH parameters", 1);
2118 if (dhpkey
!= NULL
) {
2119 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2121 BIO_printf(bio_s_out
, "Using default temp DH parameters\n");
2123 (void)BIO_flush(bio_s_out
);
2125 if (dhpkey
== NULL
) {
2126 SSL_CTX_set_dh_auto(ctx
, 1);
2129 * We need 2 references: one for use by ctx and one for use by
2132 if (!EVP_PKEY_up_ref(dhpkey
)) {
2133 EVP_PKEY_free(dhpkey
);
2136 if (!SSL_CTX_set0_tmp_dh_pkey(ctx
, dhpkey
)) {
2137 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2138 ERR_print_errors(bio_err
);
2139 /* Free 2 references */
2140 EVP_PKEY_free(dhpkey
);
2141 EVP_PKEY_free(dhpkey
);
2147 if (dhfile
!= NULL
) {
2148 EVP_PKEY
*dhpkey2
= load_keyparams_suppress(s_cert_file2
,
2151 "DH parameters", 1);
2153 if (dhpkey2
!= NULL
) {
2154 BIO_printf(bio_s_out
, "Setting temp DH parameters\n");
2155 (void)BIO_flush(bio_s_out
);
2157 EVP_PKEY_free(dhpkey
);
2161 if (dhpkey
== NULL
) {
2162 SSL_CTX_set_dh_auto(ctx2
, 1);
2163 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2
, dhpkey
)) {
2164 BIO_puts(bio_err
, "Error setting temp DH parameters\n");
2165 ERR_print_errors(bio_err
);
2166 EVP_PKEY_free(dhpkey
);
2171 EVP_PKEY_free(dhpkey
);
2174 if (!set_cert_key_stuff(ctx
, s_cert
, s_key
, s_chain
, build_chain
))
2177 if (s_serverinfo_file
!= NULL
2178 && !SSL_CTX_use_serverinfo_file(ctx
, s_serverinfo_file
)) {
2179 ERR_print_errors(bio_err
);
2184 && !set_cert_key_stuff(ctx2
, s_cert2
, s_key2
, NULL
, build_chain
))
2187 if (s_dcert
!= NULL
) {
2188 if (!set_cert_key_stuff(ctx
, s_dcert
, s_dkey
, s_dchain
, build_chain
))
2192 if (no_resume_ephemeral
) {
2193 SSL_CTX_set_not_resumable_session_callback(ctx
,
2194 not_resumable_sess_cb
);
2197 SSL_CTX_set_not_resumable_session_callback(ctx2
,
2198 not_resumable_sess_cb
);
2200 #ifndef OPENSSL_NO_PSK
2201 if (psk_key
!= NULL
) {
2203 BIO_printf(bio_s_out
, "PSK key given, setting server callback\n");
2204 SSL_CTX_set_psk_server_callback(ctx
, psk_server_cb
);
2207 if (psk_identity_hint
!= NULL
) {
2208 if (min_version
== TLS1_3_VERSION
) {
2209 BIO_printf(bio_s_out
, "PSK warning: there is NO identity hint in TLSv1.3\n");
2211 if (!SSL_CTX_use_psk_identity_hint(ctx
, psk_identity_hint
)) {
2212 BIO_printf(bio_err
, "error setting PSK identity hint to context\n");
2213 ERR_print_errors(bio_err
);
2219 if (psksessf
!= NULL
) {
2220 BIO
*stmp
= BIO_new_file(psksessf
, "r");
2223 BIO_printf(bio_err
, "Can't open PSK session file %s\n", psksessf
);
2224 ERR_print_errors(bio_err
);
2227 psksess
= PEM_read_bio_SSL_SESSION(stmp
, NULL
, 0, NULL
);
2229 if (psksess
== NULL
) {
2230 BIO_printf(bio_err
, "Can't read PSK session file %s\n", psksessf
);
2231 ERR_print_errors(bio_err
);
2237 if (psk_key
!= NULL
|| psksess
!= NULL
)
2238 SSL_CTX_set_psk_find_session_callback(ctx
, psk_find_session_cb
);
2240 SSL_CTX_set_verify(ctx
, s_server_verify
, verify_callback
);
2241 if (!SSL_CTX_set_session_id_context(ctx
,
2242 (void *)&s_server_session_id_context
,
2243 sizeof(s_server_session_id_context
))) {
2244 BIO_printf(bio_err
, "error setting session id context\n");
2245 ERR_print_errors(bio_err
);
2249 /* Set DTLS cookie generation and verification callbacks */
2250 SSL_CTX_set_cookie_generate_cb(ctx
, generate_cookie_callback
);
2251 SSL_CTX_set_cookie_verify_cb(ctx
, verify_cookie_callback
);
2253 /* Set TLS1.3 cookie generation and verification callbacks */
2254 SSL_CTX_set_stateless_cookie_generate_cb(ctx
, generate_stateless_cookie_callback
);
2255 SSL_CTX_set_stateless_cookie_verify_cb(ctx
, verify_stateless_cookie_callback
);
2258 SSL_CTX_set_verify(ctx2
, s_server_verify
, verify_callback
);
2259 if (!SSL_CTX_set_session_id_context(ctx2
,
2260 (void *)&s_server_session_id_context
,
2261 sizeof(s_server_session_id_context
))) {
2262 BIO_printf(bio_err
, "error setting session id context\n");
2263 ERR_print_errors(bio_err
);
2266 tlsextcbp
.biodebug
= bio_s_out
;
2267 SSL_CTX_set_tlsext_servername_callback(ctx2
, ssl_servername_cb
);
2268 SSL_CTX_set_tlsext_servername_arg(ctx2
, &tlsextcbp
);
2269 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
2270 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
2273 #ifndef OPENSSL_NO_SRP
2274 if (srp_verifier_file
!= NULL
) {
2275 if (!set_up_srp_verifier_file(ctx
, &srp_callback_parm
, srpuserseed
,
2280 if (CAfile
!= NULL
) {
2281 SSL_CTX_set_client_CA_list(ctx
, SSL_load_client_CA_file(CAfile
));
2284 SSL_CTX_set_client_CA_list(ctx2
, SSL_load_client_CA_file(CAfile
));
2286 #ifndef OPENSSL_NO_OCSP
2287 if (s_tlsextstatus
) {
2288 SSL_CTX_set_tlsext_status_cb(ctx
, cert_status_cb
);
2289 SSL_CTX_set_tlsext_status_arg(ctx
, &tlscstatp
);
2291 SSL_CTX_set_tlsext_status_cb(ctx2
, cert_status_cb
);
2292 SSL_CTX_set_tlsext_status_arg(ctx2
, &tlscstatp
);
2296 if (set_keylog_file(ctx
, keylog_file
))
2299 if (max_early_data
>= 0)
2300 SSL_CTX_set_max_early_data(ctx
, max_early_data
);
2301 if (recv_max_early_data
>= 0)
2302 SSL_CTX_set_recv_max_early_data(ctx
, recv_max_early_data
);
2305 BIO_printf(bio_s_out
, "Compressing certificates\n");
2306 if (!SSL_CTX_compress_certs(ctx
, 0))
2307 BIO_printf(bio_s_out
, "Error compressing certs on ctx\n");
2308 if (ctx2
!= NULL
&& !SSL_CTX_compress_certs(ctx2
, 0))
2309 BIO_printf(bio_s_out
, "Error compressing certs on ctx2\n");
2311 if (enable_server_rpk
)
2312 if (!SSL_CTX_set1_server_cert_type(ctx
, cert_type_rpk
, sizeof(cert_type_rpk
))) {
2313 BIO_printf(bio_s_out
, "Error setting server certificate types\n");
2316 if (enable_client_rpk
)
2317 if (!SSL_CTX_set1_client_cert_type(ctx
, cert_type_rpk
, sizeof(cert_type_rpk
))) {
2318 BIO_printf(bio_s_out
, "Error setting server certificate types\n");
2323 server_cb
= rev_body
;
2325 server_cb
= www_body
;
2327 server_cb
= sv_body
;
2329 if (socket_family
== AF_UNIX
2330 && unlink_unix_path
)
2334 BIO_printf(bio_s_out
, "Listening for TFO\n");
2335 do_server(&accept_socket
, host
, port
, socket_family
, socket_type
, protocol
,
2336 server_cb
, context
, naccept
, bio_s_out
, tfo
);
2337 print_stats(bio_s_out
, ctx
);
2341 SSL_SESSION_free(psksess
);
2342 set_keylog_file(NULL
, NULL
);
2344 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
2346 EVP_PKEY_free(s_key
);
2347 EVP_PKEY_free(s_dkey
);
2348 OSSL_STACK_OF_X509_free(s_chain
);
2349 OSSL_STACK_OF_X509_free(s_dchain
);
2351 OPENSSL_free(dpass
);
2354 X509_VERIFY_PARAM_free(vpm
);
2356 OPENSSL_free(tlscstatp
.host
);
2357 OPENSSL_free(tlscstatp
.port
);
2358 OPENSSL_free(tlscstatp
.path
);
2361 EVP_PKEY_free(s_key2
);
2362 #ifndef OPENSSL_NO_NEXTPROTONEG
2363 OPENSSL_free(next_proto
.data
);
2365 OPENSSL_free(alpn_ctx
.data
);
2366 ssl_excert_free(exc
);
2367 sk_OPENSSL_STRING_free(ssl_args
);
2368 SSL_CONF_CTX_free(cctx
);
2369 release_engine(engine
);
2370 BIO_free(bio_s_out
);
2372 BIO_free(bio_s_msg
);
2374 #ifdef CHARSET_EBCDIC
2375 BIO_meth_free(methods_ebcdic
);
2380 static void print_stats(BIO
*bio
, SSL_CTX
*ssl_ctx
)
2382 BIO_printf(bio
, "%4ld items in the session cache\n",
2383 SSL_CTX_sess_number(ssl_ctx
));
2384 BIO_printf(bio
, "%4ld client connects (SSL_connect())\n",
2385 SSL_CTX_sess_connect(ssl_ctx
));
2386 BIO_printf(bio
, "%4ld client renegotiates (SSL_connect())\n",
2387 SSL_CTX_sess_connect_renegotiate(ssl_ctx
));
2388 BIO_printf(bio
, "%4ld client connects that finished\n",
2389 SSL_CTX_sess_connect_good(ssl_ctx
));
2390 BIO_printf(bio
, "%4ld server accepts (SSL_accept())\n",
2391 SSL_CTX_sess_accept(ssl_ctx
));
2392 BIO_printf(bio
, "%4ld server renegotiates (SSL_accept())\n",
2393 SSL_CTX_sess_accept_renegotiate(ssl_ctx
));
2394 BIO_printf(bio
, "%4ld server accepts that finished\n",
2395 SSL_CTX_sess_accept_good(ssl_ctx
));
2396 BIO_printf(bio
, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx
));
2397 BIO_printf(bio
, "%4ld session cache misses\n",
2398 SSL_CTX_sess_misses(ssl_ctx
));
2399 BIO_printf(bio
, "%4ld session cache timeouts\n",
2400 SSL_CTX_sess_timeouts(ssl_ctx
));
2401 BIO_printf(bio
, "%4ld callback cache hits\n",
2402 SSL_CTX_sess_cb_hits(ssl_ctx
));
2403 BIO_printf(bio
, "%4ld cache full overflows (%ld allowed)\n",
2404 SSL_CTX_sess_cache_full(ssl_ctx
),
2405 SSL_CTX_sess_get_cache_size(ssl_ctx
));
2408 static long int count_reads_callback(BIO
*bio
, int cmd
, const char *argp
, size_t len
,
2409 int argi
, long argl
, int ret
, size_t *processed
)
2411 unsigned int *p_counter
= (unsigned int *)BIO_get_callback_arg(bio
);
2414 case BIO_CB_READ
: /* No break here */
2416 if (p_counter
!= NULL
)
2424 BIO_set_callback_arg(bio
, (char *)bio_s_out
);
2425 ret
= (int)bio_dump_callback(bio
, cmd
, argp
, len
, argi
, argl
, ret
, processed
);
2426 BIO_set_callback_arg(bio
, (char *)p_counter
);
2432 static int sv_body(int s
, int stype
, int prot
, unsigned char *context
)
2441 struct timeval timeout
;
2442 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
2443 struct timeval
*timeoutp
;
2445 #ifndef OPENSSL_NO_DTLS
2446 # ifndef OPENSSL_NO_SCTP
2447 int isdtls
= (stype
== SOCK_DGRAM
|| prot
== IPPROTO_SCTP
);
2449 int isdtls
= (stype
== SOCK_DGRAM
);
2453 buf
= app_malloc(bufsize
, "server buffer");
2455 if (!BIO_socket_nbio(s
, 1))
2456 ERR_print_errors(bio_err
);
2458 BIO_printf(bio_err
, "Turned on non blocking io\n");
2467 if (s_tlsextdebug
) {
2468 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2469 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2473 && !SSL_set_session_id_context(con
, context
,
2474 strlen((char *)context
))) {
2475 BIO_printf(bio_err
, "Error setting session id context\n");
2480 if (!SSL_clear(con
)) {
2481 BIO_printf(bio_err
, "Error clearing SSL connection\n");
2485 #ifndef OPENSSL_NO_DTLS
2487 # ifndef OPENSSL_NO_SCTP
2488 if (prot
== IPPROTO_SCTP
)
2489 sbio
= BIO_new_dgram_sctp(s
, BIO_NOCLOSE
);
2492 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2494 BIO_printf(bio_err
, "Unable to create BIO\n");
2495 ERR_print_errors(bio_err
);
2499 if (enable_timeouts
) {
2501 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2502 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2505 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2506 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2510 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2511 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2512 DTLS_get_link_min_mtu(con
));
2517 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2518 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2519 BIO_printf(bio_err
, "Failed to set MTU\n");
2525 /* want to do MTU discovery */
2526 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2528 # ifndef OPENSSL_NO_SCTP
2529 if (prot
!= IPPROTO_SCTP
)
2531 /* Turn on cookie exchange. Not necessary for SCTP */
2532 SSL_set_options(con
, SSL_OP_COOKIE_EXCHANGE
);
2535 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2538 BIO_printf(bio_err
, "Unable to create BIO\n");
2539 ERR_print_errors(bio_err
);
2546 test
= BIO_new(BIO_f_nbio_test());
2548 BIO_printf(bio_err
, "Unable to create BIO\n");
2553 sbio
= BIO_push(test
, sbio
);
2556 SSL_set_bio(con
, sbio
, sbio
);
2557 SSL_set_accept_state(con
);
2558 /* SSL_set_fd(con,s); */
2560 BIO_set_callback_ex(SSL_get_rbio(con
), count_reads_callback
);
2562 #ifndef OPENSSL_NO_SSL_TRACE
2564 SSL_set_msg_callback(con
, SSL_trace
);
2567 SSL_set_msg_callback(con
, msg_cb
);
2568 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
2571 if (s_tlsextdebug
) {
2572 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2573 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
2577 int write_header
= 1, edret
= SSL_READ_EARLY_DATA_ERROR
;
2580 while (edret
!= SSL_READ_EARLY_DATA_FINISH
) {
2582 edret
= SSL_read_early_data(con
, buf
, bufsize
, &readbytes
);
2583 if (edret
!= SSL_READ_EARLY_DATA_ERROR
)
2586 switch (SSL_get_error(con
, 0)) {
2587 case SSL_ERROR_WANT_WRITE
:
2588 case SSL_ERROR_WANT_ASYNC
:
2589 case SSL_ERROR_WANT_READ
:
2590 /* Just keep trying - busy waiting */
2593 BIO_printf(bio_err
, "Error reading early data\n");
2594 ERR_print_errors(bio_err
);
2598 if (readbytes
> 0) {
2600 BIO_printf(bio_s_out
, "Early data received:\n");
2603 raw_write_stdout(buf
, (unsigned int)readbytes
);
2604 (void)BIO_flush(bio_s_out
);
2608 if (SSL_get_early_data_status(con
) == SSL_EARLY_DATA_NOT_SENT
)
2609 BIO_printf(bio_s_out
, "No early data received\n");
2611 BIO_printf(bio_s_out
, "Early data was rejected\n");
2613 BIO_printf(bio_s_out
, "\nEnd of early data\n");
2615 if (SSL_is_init_finished(con
))
2616 print_connection_info(con
);
2619 if (fileno_stdin() > s
)
2620 width
= fileno_stdin() + 1;
2625 int read_from_terminal
;
2626 int read_from_sslcon
;
2628 read_from_terminal
= 0;
2629 read_from_sslcon
= SSL_has_pending(con
)
2630 || (async
&& SSL_waiting_for_async(con
));
2632 if (!read_from_sslcon
) {
2634 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2635 openssl_fdset(fileno_stdin(), &readfds
);
2637 openssl_fdset(s
, &readfds
);
2639 * Note: under VMS with SOCKETSHR the second parameter is
2640 * currently of type (int *) whereas under other systems it is
2641 * (void *) if you don't have a cast it will choke the compiler:
2642 * if you do have a cast then you can either go for (int *) or
2645 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2647 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2648 * only on sockets. As a workaround we timeout the select every
2649 * second and check for any keypress. In a proper Windows
2650 * application we wouldn't do this because it is inefficient.
2653 timeout
.tv_usec
= 0;
2654 i
= select(width
, (void *)&readfds
, NULL
, NULL
, &timeout
);
2655 if (has_stdin_waiting())
2656 read_from_terminal
= 1;
2657 if ((i
< 0) || (!i
&& !read_from_terminal
))
2660 if (SSL_is_dtls(con
) && DTLSv1_get_timeout(con
, &timeout
))
2661 timeoutp
= &timeout
;
2665 i
= select(width
, (void *)&readfds
, NULL
, NULL
, timeoutp
);
2667 if ((SSL_is_dtls(con
)) && DTLSv1_handle_timeout(con
) > 0)
2668 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2672 if (FD_ISSET(fileno_stdin(), &readfds
))
2673 read_from_terminal
= 1;
2675 if (FD_ISSET(s
, &readfds
))
2676 read_from_sslcon
= 1;
2678 if (read_from_terminal
) {
2682 i
= raw_read_stdin(buf
, bufsize
/ 2);
2684 /* both loops are skipped when i <= 0 */
2685 for (j
= 0; j
< i
; j
++)
2688 for (j
= i
- 1; j
>= 0; j
--) {
2689 buf
[j
+ lf_num
] = buf
[j
];
2690 if (buf
[j
] == '\n') {
2693 buf
[j
+ lf_num
] = '\r';
2696 assert(lf_num
== 0);
2698 i
= raw_read_stdin(buf
, bufsize
);
2701 if (!s_quiet
&& !s_brief
) {
2702 if ((i
<= 0) || (buf
[0] == 'Q')) {
2703 BIO_printf(bio_s_out
, "DONE\n");
2704 (void)BIO_flush(bio_s_out
);
2706 close_accept_socket();
2710 if ((i
<= 0) || (buf
[0] == 'q')) {
2711 BIO_printf(bio_s_out
, "DONE\n");
2712 (void)BIO_flush(bio_s_out
);
2713 if (SSL_version(con
) != DTLS1_VERSION
)
2716 * close_accept_socket(); ret= -11;
2720 if ((buf
[0] == 'r') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2721 SSL_renegotiate(con
);
2722 i
= SSL_do_handshake(con
);
2723 printf("SSL_do_handshake -> %d\n", i
);
2726 if ((buf
[0] == 'R') && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2728 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
2730 SSL_renegotiate(con
);
2731 i
= SSL_do_handshake(con
);
2732 printf("SSL_do_handshake -> %d\n", i
);
2735 if ((buf
[0] == 'K' || buf
[0] == 'k')
2736 && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2737 SSL_key_update(con
, buf
[0] == 'K' ?
2738 SSL_KEY_UPDATE_REQUESTED
2739 : SSL_KEY_UPDATE_NOT_REQUESTED
);
2740 i
= SSL_do_handshake(con
);
2741 printf("SSL_do_handshake -> %d\n", i
);
2744 if (buf
[0] == 'c' && ((buf
[1] == '\n') || (buf
[1] == '\r'))) {
2745 SSL_set_verify(con
, SSL_VERIFY_PEER
, NULL
);
2746 i
= SSL_verify_client_post_handshake(con
);
2748 printf("Failed to initiate request\n");
2749 ERR_print_errors(bio_err
);
2751 i
= SSL_do_handshake(con
);
2752 printf("SSL_do_handshake -> %d\n", i
);
2756 if (buf
[0] == 'P') {
2757 static const char str
[] = "Lets print some clear text\n";
2758 BIO_write(SSL_get_wbio(con
), str
, sizeof(str
) -1);
2760 if (buf
[0] == 'S') {
2761 print_stats(bio_s_out
, SSL_get_SSL_CTX(con
));
2764 #ifdef CHARSET_EBCDIC
2765 ebcdic2ascii(buf
, buf
, i
);
2769 /* should do a select for the write */
2772 if (++count
== 100) {
2774 SSL_renegotiate(con
);
2777 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2778 #ifndef OPENSSL_NO_SRP
2779 while (SSL_get_error(con
, k
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2780 BIO_printf(bio_s_out
, "LOOKUP renego during write\n");
2782 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2784 k
= SSL_write(con
, &(buf
[l
]), (unsigned int)i
);
2787 switch (SSL_get_error(con
, k
)) {
2788 case SSL_ERROR_NONE
:
2790 case SSL_ERROR_WANT_ASYNC
:
2791 BIO_printf(bio_s_out
, "Write BLOCK (Async)\n");
2792 (void)BIO_flush(bio_s_out
);
2793 wait_for_async(con
);
2795 case SSL_ERROR_WANT_WRITE
:
2796 case SSL_ERROR_WANT_READ
:
2797 case SSL_ERROR_WANT_X509_LOOKUP
:
2798 BIO_printf(bio_s_out
, "Write BLOCK\n");
2799 (void)BIO_flush(bio_s_out
);
2801 case SSL_ERROR_WANT_ASYNC_JOB
:
2803 * This shouldn't ever happen in s_server. Treat as an error
2805 case SSL_ERROR_SYSCALL
:
2807 BIO_printf(bio_s_out
, "ERROR\n");
2808 (void)BIO_flush(bio_s_out
);
2809 ERR_print_errors(bio_err
);
2813 case SSL_ERROR_ZERO_RETURN
:
2814 BIO_printf(bio_s_out
, "DONE\n");
2815 (void)BIO_flush(bio_s_out
);
2827 if (read_from_sslcon
) {
2829 * init_ssl_connection handles all async events itself so if we're
2830 * waiting for async then we shouldn't go back into
2831 * init_ssl_connection
2833 if ((!async
|| !SSL_waiting_for_async(con
))
2834 && !SSL_is_init_finished(con
)) {
2836 * Count number of reads during init_ssl_connection.
2837 * It helps us to distinguish configuration errors from errors
2838 * caused by a client.
2840 unsigned int read_counter
= 0;
2842 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)&read_counter
);
2843 i
= init_ssl_connection(con
);
2844 BIO_set_callback_arg(SSL_get_rbio(con
), NULL
);
2847 * If initialization fails without reads, then
2848 * there was a fatal error in configuration.
2850 if (i
<= 0 && read_counter
== 0) {
2857 } else if (i
== 0) {
2863 i
= SSL_read(con
, (char *)buf
, bufsize
);
2864 #ifndef OPENSSL_NO_SRP
2865 while (SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
2866 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
2868 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
2870 i
= SSL_read(con
, (char *)buf
, bufsize
);
2873 switch (SSL_get_error(con
, i
)) {
2874 case SSL_ERROR_NONE
:
2875 #ifdef CHARSET_EBCDIC
2876 ascii2ebcdic(buf
, buf
, i
);
2878 raw_write_stdout(buf
, (unsigned int)i
);
2879 (void)BIO_flush(bio_s_out
);
2880 if (SSL_has_pending(con
))
2883 case SSL_ERROR_WANT_ASYNC
:
2884 BIO_printf(bio_s_out
, "Read BLOCK (Async)\n");
2885 (void)BIO_flush(bio_s_out
);
2886 wait_for_async(con
);
2888 case SSL_ERROR_WANT_WRITE
:
2889 case SSL_ERROR_WANT_READ
:
2890 BIO_printf(bio_s_out
, "Read BLOCK\n");
2891 (void)BIO_flush(bio_s_out
);
2893 case SSL_ERROR_WANT_ASYNC_JOB
:
2895 * This shouldn't ever happen in s_server. Treat as an error
2897 case SSL_ERROR_SYSCALL
:
2899 BIO_printf(bio_s_out
, "ERROR\n");
2900 (void)BIO_flush(bio_s_out
);
2901 ERR_print_errors(bio_err
);
2904 case SSL_ERROR_ZERO_RETURN
:
2905 BIO_printf(bio_s_out
, "DONE\n");
2906 (void)BIO_flush(bio_s_out
);
2915 BIO_printf(bio_s_out
, "shutting down SSL\n");
2916 do_ssl_shutdown(con
);
2919 BIO_printf(bio_s_out
, "CONNECTION CLOSED\n");
2920 OPENSSL_clear_free(buf
, bufsize
);
2924 static void close_accept_socket(void)
2926 BIO_printf(bio_err
, "shutdown accept socket\n");
2927 if (accept_socket
>= 0) {
2928 BIO_closesocket(accept_socket
);
2932 static int is_retryable(SSL
*con
, int i
)
2934 int err
= SSL_get_error(con
, i
);
2936 /* If it's not a fatal error, it must be retryable */
2937 return (err
!= SSL_ERROR_SSL
)
2938 && (err
!= SSL_ERROR_SYSCALL
)
2939 && (err
!= SSL_ERROR_ZERO_RETURN
);
2942 static int init_ssl_connection(SSL
*con
)
2948 if (dtlslisten
|| stateless
) {
2949 BIO_ADDR
*client
= NULL
;
2952 if ((client
= BIO_ADDR_new()) == NULL
) {
2953 BIO_printf(bio_err
, "ERROR - memory\n");
2956 i
= DTLSv1_listen(con
, client
);
2958 i
= SSL_stateless(con
);
2965 wbio
= SSL_get_wbio(con
);
2967 BIO_get_fd(wbio
, &fd
);
2970 if (!wbio
|| BIO_connect(fd
, client
, 0) == 0) {
2971 BIO_printf(bio_err
, "ERROR - unable to connect\n");
2972 BIO_ADDR_free(client
);
2976 (void)BIO_ctrl_set_connected(wbio
, client
);
2977 BIO_ADDR_free(client
);
2982 i
= SSL_accept(con
);
2984 BIO_ADDR_free(client
);
2988 i
= SSL_accept(con
);
2991 retry
= is_retryable(con
, i
);
2992 #ifdef CERT_CB_TEST_RETRY
2995 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
2996 && SSL_get_state(con
) == TLS_ST_SR_CLNT_HELLO
) {
2998 "LOOKUP from certificate callback during accept\n");
2999 i
= SSL_accept(con
);
3001 retry
= is_retryable(con
, i
);
3006 #ifndef OPENSSL_NO_SRP
3008 && SSL_get_error(con
, i
) == SSL_ERROR_WANT_X509_LOOKUP
) {
3009 BIO_printf(bio_s_out
, "LOOKUP during accept %s\n",
3010 srp_callback_parm
.login
);
3012 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3014 i
= SSL_accept(con
);
3016 retry
= is_retryable(con
, i
);
3019 } while (i
< 0 && SSL_waiting_for_async(con
));
3023 if (((dtlslisten
|| stateless
) && i
== 0)
3024 || (!dtlslisten
&& !stateless
&& retry
)) {
3025 BIO_printf(bio_s_out
, "DELAY\n");
3029 BIO_printf(bio_err
, "ERROR\n");
3031 verify_err
= SSL_get_verify_result(con
);
3032 if (verify_err
!= X509_V_OK
) {
3033 BIO_printf(bio_err
, "verify error:%s\n",
3034 X509_verify_cert_error_string(verify_err
));
3036 /* Always print any error messages */
3037 ERR_print_errors(bio_err
);
3041 print_connection_info(con
);
3045 static void print_connection_info(SSL
*con
)
3050 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3051 const unsigned char *next_proto_neg
;
3052 unsigned next_proto_neg_len
;
3054 unsigned char *exportedkeymat
;
3058 print_ssl_summary(con
);
3060 PEM_write_bio_SSL_SESSION(bio_s_out
, SSL_get_session(con
));
3062 peer
= SSL_get0_peer_certificate(con
);
3064 BIO_printf(bio_s_out
, "Client certificate\n");
3065 PEM_write_bio_X509(bio_s_out
, peer
);
3066 dump_cert_text(bio_s_out
, peer
);
3069 /* Only display RPK information if configured */
3070 if (SSL_get_negotiated_server_cert_type(con
) == TLSEXT_cert_type_rpk
)
3071 BIO_printf(bio_s_out
, "Server-to-client raw public key negotiated\n");
3072 if (SSL_get_negotiated_client_cert_type(con
) == TLSEXT_cert_type_rpk
)
3073 BIO_printf(bio_s_out
, "Client-to-server raw public key negotiated\n");
3074 if (enable_client_rpk
) {
3075 EVP_PKEY
*client_rpk
= SSL_get0_peer_rpk(con
);
3077 if (client_rpk
!= NULL
) {
3078 BIO_printf(bio_s_out
, "Client raw public key\n");
3079 EVP_PKEY_print_public(bio_s_out
, client_rpk
, 2, NULL
);
3083 if (SSL_get_shared_ciphers(con
, buf
, sizeof(buf
)) != NULL
)
3084 BIO_printf(bio_s_out
, "Shared ciphers:%s\n", buf
);
3085 str
= SSL_CIPHER_get_name(SSL_get_current_cipher(con
));
3086 ssl_print_sigalgs(bio_s_out
, con
);
3087 #ifndef OPENSSL_NO_EC
3088 ssl_print_point_formats(bio_s_out
, con
);
3089 ssl_print_groups(bio_s_out
, con
, 0);
3091 print_ca_names(bio_s_out
, con
);
3092 BIO_printf(bio_s_out
, "CIPHER is %s\n", (str
!= NULL
) ? str
: "(NONE)");
3094 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3095 SSL_get0_next_proto_negotiated(con
, &next_proto_neg
, &next_proto_neg_len
);
3096 if (next_proto_neg
) {
3097 BIO_printf(bio_s_out
, "NEXTPROTO is ");
3098 BIO_write(bio_s_out
, next_proto_neg
, next_proto_neg_len
);
3099 BIO_printf(bio_s_out
, "\n");
3102 #ifndef OPENSSL_NO_SRTP
3104 SRTP_PROTECTION_PROFILE
*srtp_profile
3105 = SSL_get_selected_srtp_profile(con
);
3108 BIO_printf(bio_s_out
, "SRTP Extension negotiated, profile=%s\n",
3109 srtp_profile
->name
);
3112 if (SSL_session_reused(con
))
3113 BIO_printf(bio_s_out
, "Reused session-id\n");
3115 ssl_print_secure_renegotiation_notes(bio_s_out
, con
);
3117 if ((SSL_get_options(con
) & SSL_OP_NO_RENEGOTIATION
))
3118 BIO_printf(bio_s_out
, "Renegotiation is DISABLED\n");
3120 if (keymatexportlabel
!= NULL
) {
3121 BIO_printf(bio_s_out
, "Keying material exporter:\n");
3122 BIO_printf(bio_s_out
, " Label: '%s'\n", keymatexportlabel
);
3123 BIO_printf(bio_s_out
, " Length: %i bytes\n", keymatexportlen
);
3124 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
3125 if (SSL_export_keying_material(con
, exportedkeymat
,
3128 strlen(keymatexportlabel
),
3130 BIO_printf(bio_s_out
, " Error\n");
3132 BIO_printf(bio_s_out
, " Keying material: ");
3133 for (i
= 0; i
< keymatexportlen
; i
++)
3134 BIO_printf(bio_s_out
, "%02X", exportedkeymat
[i
]);
3135 BIO_printf(bio_s_out
, "\n");
3137 OPENSSL_free(exportedkeymat
);
3139 #ifndef OPENSSL_NO_KTLS
3140 if (BIO_get_ktls_send(SSL_get_wbio(con
)))
3141 BIO_printf(bio_err
, "Using Kernel TLS for sending\n");
3142 if (BIO_get_ktls_recv(SSL_get_rbio(con
)))
3143 BIO_printf(bio_err
, "Using Kernel TLS for receiving\n");
3146 (void)BIO_flush(bio_s_out
);
3149 static int www_body(int s
, int stype
, int prot
, unsigned char *context
)
3151 char *buf
= NULL
, *p
;
3155 const SSL_CIPHER
*c
;
3156 BIO
*io
, *ssl_bio
, *sbio
;
3158 int total_bytes
= 0;
3161 #ifndef OPENSSL_NO_KTLS
3162 int use_sendfile_for_req
= use_sendfile
;
3166 #ifdef CHARSET_EBCDIC
3170 /* Set width for a select call if needed */
3173 /* as we use BIO_gets(), and it always null terminates data, we need
3174 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3175 p
= buf
= app_malloc(bufsize
+ 1, "server www buffer");
3176 io
= BIO_new(BIO_f_buffer());
3177 ssl_bio
= BIO_new(BIO_f_ssl());
3178 if ((io
== NULL
) || (ssl_bio
== NULL
))
3182 if (!BIO_socket_nbio(s
, 1))
3183 ERR_print_errors(bio_err
);
3185 BIO_printf(bio_err
, "Turned on non blocking io\n");
3188 /* lets make the output buffer a reasonable size */
3189 if (BIO_set_write_buffer_size(io
, bufsize
) <= 0)
3192 if ((con
= SSL_new(ctx
)) == NULL
)
3195 if (s_tlsextdebug
) {
3196 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3197 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3201 && !SSL_set_session_id_context(con
, context
,
3202 strlen((char *)context
))) {
3207 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3216 test
= BIO_new(BIO_f_nbio_test());
3223 sbio
= BIO_push(test
, sbio
);
3225 SSL_set_bio(con
, sbio
, sbio
);
3226 SSL_set_accept_state(con
);
3228 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3229 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3230 BIO_push(io
, ssl_bio
);
3232 #ifdef CHARSET_EBCDIC
3233 filter
= BIO_new(BIO_f_ebcdic_filter());
3237 io
= BIO_push(filter
, io
);
3241 BIO_set_callback_ex(SSL_get_rbio(con
), bio_dump_callback
);
3242 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3245 #ifndef OPENSSL_NO_SSL_TRACE
3247 SSL_set_msg_callback(con
, SSL_trace
);
3250 SSL_set_msg_callback(con
, msg_cb
);
3251 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3255 i
= BIO_gets(io
, buf
, bufsize
+ 1);
3256 if (i
< 0) { /* error */
3257 if (!BIO_should_retry(io
) && !SSL_waiting_for_async(con
)) {
3259 ERR_print_errors(bio_err
);
3262 BIO_printf(bio_s_out
, "read R BLOCK\n");
3263 #ifndef OPENSSL_NO_SRP
3264 if (BIO_should_io_special(io
)
3265 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3266 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3268 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3276 } else if (i
== 0) { /* end of input */
3281 /* else we have data */
3282 if ((www
== 1 && HAS_PREFIX(buf
, "GET "))
3283 || (www
== 2 && HAS_PREFIX(buf
, "GET /stats "))) {
3285 STACK_OF(SSL_CIPHER
) *sk
;
3286 static const char *space
= " ";
3288 if (www
== 1 && HAS_PREFIX(buf
, "GET /reneg")) {
3289 if (HAS_PREFIX(buf
, "GET /renegcert"))
3291 SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
,
3293 i
= SSL_renegotiate(con
);
3294 BIO_printf(bio_s_out
, "SSL_renegotiate -> %d\n", i
);
3295 /* Send the HelloRequest */
3296 i
= SSL_do_handshake(con
);
3298 BIO_printf(bio_s_out
, "SSL_do_handshake() Retval %d\n",
3299 SSL_get_error(con
, i
));
3300 ERR_print_errors(bio_err
);
3303 /* Wait for a ClientHello to come back */
3305 openssl_fdset(s
, &readfds
);
3306 i
= select(width
, (void *)&readfds
, NULL
, NULL
, NULL
);
3307 if (i
<= 0 || !FD_ISSET(s
, &readfds
)) {
3308 BIO_printf(bio_s_out
,
3309 "Error waiting for client response\n");
3310 ERR_print_errors(bio_err
);
3314 * We're not actually expecting any data here and we ignore
3315 * any that is sent. This is just to force the handshake that
3316 * we're expecting to come from the client. If they haven't
3317 * sent one there's not much we can do.
3319 BIO_gets(io
, buf
, bufsize
+ 1);
3323 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3324 BIO_puts(io
, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
3325 BIO_puts(io
, "<pre>\n");
3326 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
3328 for (i
= 0; i
< local_argc
; i
++) {
3331 for (myp
= local_argv
[i
]; *myp
; myp
++)
3334 BIO_puts(io
, "<");
3337 BIO_puts(io
, ">");
3340 BIO_puts(io
, "&");
3343 BIO_write(io
, myp
, 1);
3346 BIO_write(io
, " ", 1);
3350 ssl_print_secure_renegotiation_notes(io
, con
);
3353 * The following is evil and should not really be done
3355 BIO_printf(io
, "Ciphers supported in s_server binary\n");
3356 sk
= SSL_get_ciphers(con
);
3357 j
= sk_SSL_CIPHER_num(sk
);
3358 for (i
= 0; i
< j
; i
++) {
3359 c
= sk_SSL_CIPHER_value(sk
, i
);
3360 BIO_printf(io
, "%-11s:%-25s ",
3361 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3362 if ((((i
+ 1) % 2) == 0) && (i
+ 1 != j
))
3366 p
= SSL_get_shared_ciphers(con
, buf
, bufsize
);
3369 "---\nCiphers common between both SSL end points:\n");
3373 BIO_write(io
, space
, 26 - j
);
3376 BIO_write(io
, ((i
% 3) ? " " : "\n"), 1);
3378 BIO_write(io
, p
, 1);
3385 ssl_print_sigalgs(io
, con
);
3386 #ifndef OPENSSL_NO_EC
3387 ssl_print_groups(io
, con
, 0);
3389 print_ca_names(io
, con
);
3390 BIO_printf(io
, (SSL_session_reused(con
)
3391 ? "---\nReused, " : "---\nNew, "));
3392 c
= SSL_get_current_cipher(con
);
3393 BIO_printf(io
, "%s, Cipher is %s\n",
3394 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3395 SSL_SESSION_print(io
, SSL_get_session(con
));
3396 BIO_printf(io
, "---\n");
3397 print_stats(io
, SSL_get_SSL_CTX(con
));
3398 BIO_printf(io
, "---\n");
3399 peer
= SSL_get0_peer_certificate(con
);
3401 BIO_printf(io
, "Client certificate\n");
3402 X509_print(io
, peer
);
3403 PEM_write_bio_X509(io
, peer
);
3406 BIO_puts(io
, "no client certificate available\n");
3408 BIO_puts(io
, "</pre></BODY></HTML>\r\n\r\n");
3410 } else if ((www
== 2 || www
== 3) && CHECK_AND_SKIP_PREFIX(p
, "GET /")) {
3413 static const char *text
=
3414 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3417 for (e
= p
; *e
!= '\0'; e
++) {
3422 /* Windows drive. We treat this the same way as ".." */
3429 dot
= (e
[0] == '.') ? 2 : 0;
3432 dot
= (e
[0] == '.') ? 3 : 0;
3435 dot
= (e
[0] == '/' || e
[0] == '\\') ? -1 : 0;
3439 dot
= (e
[0] == '/' || e
[0] == '\\') ? 1 : 0;
3441 dot
= (dot
== 3) || (dot
== -1); /* filename contains ".."
3446 BIO_printf(io
, "'%s' is an invalid file name\r\n", p
);
3453 BIO_printf(io
, "'%s' contains '..' or ':'\r\n", p
);
3457 if (*p
== '/' || *p
== '\\') {
3459 BIO_printf(io
, "'%s' is an invalid path\r\n", p
);
3463 /* if a directory, do the index thang */
3464 if (app_isdir(p
) > 0) {
3466 BIO_printf(io
, "'%s' is a directory\r\n", p
);
3470 opmode
= (http_server_binmode
== 1) ? "rb" : "r";
3471 if ((file
= BIO_new_file(p
, opmode
)) == NULL
) {
3473 BIO_printf(io
, "Error opening '%s' mode='%s'\r\n", p
, opmode
);
3474 ERR_print_errors(io
);
3479 BIO_printf(bio_err
, "FILE:%s\n", p
);
3483 if (((i
> 5) && (strcmp(&(p
[i
- 5]), ".html") == 0)) ||
3484 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".php") == 0)) ||
3485 ((i
> 4) && (strcmp(&(p
[i
- 4]), ".htm") == 0)))
3487 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3490 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3493 #ifndef OPENSSL_NO_KTLS
3494 if (use_sendfile_for_req
&& !BIO_get_ktls_send(SSL_get_wbio(con
))) {
3495 BIO_printf(bio_err
, "Warning: sendfile requested but KTLS is not available\n");
3496 use_sendfile_for_req
= 0;
3498 if (use_sendfile_for_req
) {
3505 BIO_get_fp(file
, &fp
);
3507 if (fstat(fd
, &st
) < 0) {
3508 BIO_printf(io
, "Error fstat '%s'\r\n", p
);
3509 ERR_print_errors(io
);
3513 filesize
= st
.st_size
;
3514 if (((int)BIO_flush(io
)) < 0)
3518 i
= SSL_sendfile(con
, fd
, offset
, filesize
, 0);
3520 BIO_printf(io
, "Error SSL_sendfile '%s'\r\n", p
);
3521 ERR_print_errors(io
);
3528 if (filesize
<= 0) {
3530 BIO_printf(bio_err
, "KTLS SENDFILE '%s' OK\n", p
);
3539 i
= BIO_read(file
, buf
, bufsize
);
3545 BIO_printf(bio_err
, "%d\n", i
);
3546 if (total_bytes
> 3 * 1024) {
3548 BIO_printf(bio_err
, "RENEGOTIATE\n");
3549 SSL_renegotiate(con
);
3553 for (j
= 0; j
< i
;) {
3557 SSL_renegotiate(con
);
3559 k
= BIO_write(io
, &(buf
[j
]), i
- j
);
3561 if (!BIO_should_retry(io
)
3562 && !SSL_waiting_for_async(con
)) {
3565 BIO_printf(bio_s_out
, "rwrite W BLOCK\n");
3580 i
= (int)BIO_flush(io
);
3582 if (!BIO_should_retry(io
))
3588 /* make sure we reuse sessions */
3589 do_ssl_shutdown(con
);
3598 static int rev_body(int s
, int stype
, int prot
, unsigned char *context
)
3604 BIO
*io
, *ssl_bio
, *sbio
;
3605 #ifdef CHARSET_EBCDIC
3609 /* as we use BIO_gets(), and it always null terminates data, we need
3610 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3611 buf
= app_malloc(bufsize
+ 1, "server rev buffer");
3612 io
= BIO_new(BIO_f_buffer());
3613 ssl_bio
= BIO_new(BIO_f_ssl());
3614 if ((io
== NULL
) || (ssl_bio
== NULL
))
3617 /* lets make the output buffer a reasonable size */
3618 if (BIO_set_write_buffer_size(io
, bufsize
) <= 0)
3621 if ((con
= SSL_new(ctx
)) == NULL
)
3624 if (s_tlsextdebug
) {
3625 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
3626 SSL_set_tlsext_debug_arg(con
, bio_s_out
);
3629 && !SSL_set_session_id_context(con
, context
,
3630 strlen((char *)context
))) {
3632 ERR_print_errors(bio_err
);
3636 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
3639 ERR_print_errors(bio_err
);
3643 SSL_set_bio(con
, sbio
, sbio
);
3644 SSL_set_accept_state(con
);
3646 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3647 BIO_set_ssl(ssl_bio
, con
, BIO_CLOSE
);
3648 BIO_push(io
, ssl_bio
);
3650 #ifdef CHARSET_EBCDIC
3651 filter
= BIO_new(BIO_f_ebcdic_filter());
3655 io
= BIO_push(filter
, io
);
3659 BIO_set_callback_ex(SSL_get_rbio(con
), bio_dump_callback
);
3660 BIO_set_callback_arg(SSL_get_rbio(con
), (char *)bio_s_out
);
3663 #ifndef OPENSSL_NO_SSL_TRACE
3665 SSL_set_msg_callback(con
, SSL_trace
);
3668 SSL_set_msg_callback(con
, msg_cb
);
3669 SSL_set_msg_callback_arg(con
, bio_s_msg
? bio_s_msg
: bio_s_out
);
3673 i
= BIO_do_handshake(io
);
3676 if (!BIO_should_retry(io
)) {
3677 BIO_puts(bio_err
, "CONNECTION FAILURE\n");
3678 ERR_print_errors(bio_err
);
3681 #ifndef OPENSSL_NO_SRP
3682 if (BIO_should_io_special(io
)
3683 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3684 BIO_printf(bio_s_out
, "LOOKUP renego during accept\n");
3686 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3692 BIO_printf(bio_err
, "CONNECTION ESTABLISHED\n");
3693 print_ssl_summary(con
);
3696 i
= BIO_gets(io
, buf
, bufsize
+ 1);
3697 if (i
< 0) { /* error */
3698 if (!BIO_should_retry(io
)) {
3700 ERR_print_errors(bio_err
);
3703 BIO_printf(bio_s_out
, "read R BLOCK\n");
3704 #ifndef OPENSSL_NO_SRP
3705 if (BIO_should_io_special(io
)
3706 && BIO_get_retry_reason(io
) == BIO_RR_SSL_X509_LOOKUP
) {
3707 BIO_printf(bio_s_out
, "LOOKUP renego during read\n");
3709 lookup_srp_user(&srp_callback_parm
, bio_s_out
);
3717 } else if (i
== 0) { /* end of input */
3719 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3722 char *p
= buf
+ i
- 1;
3723 while (i
&& (*p
== '\n' || *p
== '\r')) {
3727 if (!s_ign_eof
&& i
== 5 && HAS_PREFIX(buf
, "CLOSE")) {
3729 BIO_printf(bio_err
, "CONNECTION CLOSED\n");
3732 BUF_reverse((unsigned char *)buf
, NULL
, i
);
3734 BIO_write(io
, buf
, i
+ 1);
3739 if (!BIO_should_retry(io
))
3745 /* make sure we reuse sessions */
3746 do_ssl_shutdown(con
);
3756 #define MAX_SESSION_ID_ATTEMPTS 10
3757 static int generate_session_id(SSL
*ssl
, unsigned char *id
,
3758 unsigned int *id_len
)
3760 unsigned int count
= 0;
3761 unsigned int session_id_prefix_len
= strlen(session_id_prefix
);
3764 if (RAND_bytes(id
, *id_len
) <= 0)
3767 * Prefix the session_id with the required prefix. NB: If our prefix
3768 * is too long, clip it - but there will be worse effects anyway, eg.
3769 * the server could only possibly create 1 session ID (ie. the
3770 * prefix!) so all future session negotiations will fail due to
3773 memcpy(id
, session_id_prefix
,
3774 (session_id_prefix_len
< *id_len
) ?
3775 session_id_prefix_len
: *id_len
);
3777 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
3778 (++count
< MAX_SESSION_ID_ATTEMPTS
));
3779 if (count
>= MAX_SESSION_ID_ATTEMPTS
)
3785 * By default s_server uses an in-memory cache which caches SSL_SESSION
3786 * structures without any serialization. This hides some bugs which only
3787 * become apparent in deployed servers. By implementing a basic external
3788 * session cache some issues can be debugged using s_server.
3791 typedef struct simple_ssl_session_st
{
3796 struct simple_ssl_session_st
*next
;
3797 } simple_ssl_session
;
3799 static simple_ssl_session
*first
= NULL
;
3801 static int add_session(SSL
*ssl
, SSL_SESSION
*session
)
3803 simple_ssl_session
*sess
= app_malloc(sizeof(*sess
), "get session");
3806 SSL_SESSION_get_id(session
, &sess
->idlen
);
3807 sess
->derlen
= i2d_SSL_SESSION(session
, NULL
);
3808 if (sess
->derlen
< 0) {
3809 BIO_printf(bio_err
, "Error encoding session\n");
3814 sess
->id
= OPENSSL_memdup(SSL_SESSION_get_id(session
, NULL
), sess
->idlen
);
3815 sess
->der
= app_malloc(sess
->derlen
, "get session buffer");
3817 BIO_printf(bio_err
, "Out of memory adding to external cache\n");
3818 OPENSSL_free(sess
->id
);
3819 OPENSSL_free(sess
->der
);
3825 /* Assume it still works. */
3826 if (i2d_SSL_SESSION(session
, &p
) != sess
->derlen
) {
3827 BIO_printf(bio_err
, "Unexpected session encoding length\n");
3828 OPENSSL_free(sess
->id
);
3829 OPENSSL_free(sess
->der
);
3836 BIO_printf(bio_err
, "New session added to external cache\n");
3840 static SSL_SESSION
*get_session(SSL
*ssl
, const unsigned char *id
, int idlen
,
3843 simple_ssl_session
*sess
;
3845 for (sess
= first
; sess
; sess
= sess
->next
) {
3846 if (idlen
== (int)sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3847 const unsigned char *p
= sess
->der
;
3848 BIO_printf(bio_err
, "Lookup session: cache hit\n");
3849 return d2i_SSL_SESSION_ex(NULL
, &p
, sess
->derlen
, app_get0_libctx(),
3853 BIO_printf(bio_err
, "Lookup session: cache miss\n");
3857 static void del_session(SSL_CTX
*sctx
, SSL_SESSION
*session
)
3859 simple_ssl_session
*sess
, *prev
= NULL
;
3860 const unsigned char *id
;
3862 id
= SSL_SESSION_get_id(session
, &idlen
);
3863 for (sess
= first
; sess
; sess
= sess
->next
) {
3864 if (idlen
== sess
->idlen
&& !memcmp(sess
->id
, id
, idlen
)) {
3866 prev
->next
= sess
->next
;
3869 OPENSSL_free(sess
->id
);
3870 OPENSSL_free(sess
->der
);
3878 static void init_session_cache_ctx(SSL_CTX
*sctx
)
3880 SSL_CTX_set_session_cache_mode(sctx
,
3881 SSL_SESS_CACHE_NO_INTERNAL
|
3882 SSL_SESS_CACHE_SERVER
);
3883 SSL_CTX_sess_set_new_cb(sctx
, add_session
);
3884 SSL_CTX_sess_set_get_cb(sctx
, get_session
);
3885 SSL_CTX_sess_set_remove_cb(sctx
, del_session
);
3888 static void free_sessions(void)
3890 simple_ssl_session
*sess
, *tsess
;
3891 for (sess
= first
; sess
;) {
3892 OPENSSL_free(sess
->id
);
3893 OPENSSL_free(sess
->der
);
3896 OPENSSL_free(tsess
);
3901 #endif /* OPENSSL_NO_SOCK */