dtls13: support stateless cookie exchange on blocking socket
This commit is contained in:
parent
0b525a52c4
commit
b3ecdd2ecb
133
src/tls13.c
133
src/tls13.c
@ -11574,6 +11574,81 @@ const char* wolfSSL_get_cipher_name_by_hash(WOLFSSL* ssl, const char* hash)
|
||||
|
||||
|
||||
#ifndef NO_WOLFSSL_SERVER
|
||||
#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
|
||||
static void DtlsResetState(WOLFSSL *ssl)
|
||||
{
|
||||
/* Reset the state so that we can statelessly await the
|
||||
* ClientHello that contains the cookie. */
|
||||
|
||||
/* Reset DTLS window */
|
||||
w64Zero(&ssl->dtls13Epochs[0].nextSeqNumber);
|
||||
w64Zero(&ssl->dtls13Epochs[0].nextPeerSeqNumber);
|
||||
XMEMSET(ssl->dtls13Epochs[0].window, 0,
|
||||
sizeof(ssl->dtls13Epochs[0].window));
|
||||
|
||||
ssl->keys.dtls_expected_peer_handshake_number = 0;
|
||||
ssl->keys.dtls_handshake_number = 0;
|
||||
|
||||
ssl->msgsReceived.got_client_hello = 0;
|
||||
#ifdef WOLFSSL_SEND_HRR_COOKIE
|
||||
/* Remove cookie so that it will get computed again */
|
||||
TLSX_Remove(&ssl->extensions, TLSX_COOKIE, ssl->heap);
|
||||
#endif
|
||||
|
||||
/* Reset states */
|
||||
ssl->options.serverState = NULL_STATE;
|
||||
ssl->options.clientState = NULL_STATE;
|
||||
ssl->options.connectState = CONNECT_BEGIN;
|
||||
ssl->options.acceptState = ACCEPT_BEGIN;
|
||||
ssl->options.handShakeState = NULL_STATE;
|
||||
|
||||
Dtls13FreeFsmResources(ssl);
|
||||
}
|
||||
|
||||
static int DtlsAcceptStateless(WOLFSSL *ssl)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!ssl->options.dtls)
|
||||
return 0;
|
||||
|
||||
switch (ssl->options.acceptState) {
|
||||
case TLS13_ACCEPT_BEGIN:
|
||||
|
||||
while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
|
||||
ret = ProcessReply(ssl);
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!IsAtLeastTLSv1_3(ssl->version))
|
||||
return wolfSSL_accept(ssl);
|
||||
|
||||
ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE;
|
||||
WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
|
||||
FALL_THROUGH;
|
||||
|
||||
case TLS13_ACCEPT_CLIENT_HELLO_DONE:
|
||||
if (ssl->options.serverState ==
|
||||
SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
|
||||
ret = SendTls13ServerHello(ssl, hello_retry_request);
|
||||
if (ret == 0 || ret == WANT_WRITE)
|
||||
DtlsResetState(ssl);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE;
|
||||
WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE");
|
||||
FALL_THROUGH;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* WOLFSSL_DTLS13 */
|
||||
|
||||
/* The server accepting a connection from a client.
|
||||
* The protocol version is expecting to be TLS v1.3.
|
||||
* If the client downgrades, and older versions of the protocol are compiled
|
||||
@ -11761,6 +11836,19 @@ int wolfSSL_accept_TLSv13(WOLFSSL* ssl)
|
||||
}
|
||||
#endif /* WOLFSSL_DTLS13 */
|
||||
|
||||
#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
|
||||
if (ssl->options.dtls && ssl->options.sendCookie) {
|
||||
while (ssl->options.serverState < SERVER_HELLO_COMPLETE) {
|
||||
ret = DtlsAcceptStateless(ssl);
|
||||
if (ret != 0) {
|
||||
ssl->error = ret;
|
||||
WOLFSSL_ERROR(ssl->error);
|
||||
return WOLFSSL_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) */
|
||||
|
||||
switch (ssl->options.acceptState) {
|
||||
|
||||
#ifdef HAVE_SECURE_RENEGOTIATION
|
||||
@ -11800,40 +11888,6 @@ int wolfSSL_accept_TLSv13(WOLFSSL* ssl)
|
||||
WOLFSSL_ERROR(ssl->error);
|
||||
return WOLFSSL_FATAL_ERROR;
|
||||
}
|
||||
#ifdef WOLFSSL_DTLS13
|
||||
if (ssl->options.dtls && wolfSSL_dtls_get_using_nonblock(ssl)) {
|
||||
/* Reset the state so that we can statelessly await the
|
||||
* ClientHello that contains the cookie. Return a WANT_READ
|
||||
* to the user so that we don't drop UDP messages in the
|
||||
* network callbacks. */
|
||||
|
||||
/* Reset DTLS window */
|
||||
w64Zero(&ssl->dtls13Epochs[0].nextSeqNumber);
|
||||
w64Zero(&ssl->dtls13Epochs[0].nextPeerSeqNumber);
|
||||
XMEMSET(ssl->dtls13Epochs[0].window, 0,
|
||||
sizeof(ssl->dtls13Epochs[0].window));
|
||||
|
||||
ssl->keys.dtls_expected_peer_handshake_number = 0;
|
||||
ssl->keys.dtls_handshake_number = 0;
|
||||
|
||||
ssl->msgsReceived.got_client_hello = 0;
|
||||
#ifdef WOLFSSL_SEND_HRR_COOKIE
|
||||
/* Remove cookie so that it will get computed again */
|
||||
TLSX_Remove(&ssl->extensions, TLSX_COOKIE, ssl->heap);
|
||||
#endif
|
||||
|
||||
/* Reset states */
|
||||
ssl->options.serverState = NULL_STATE;
|
||||
ssl->options.clientState = NULL_STATE;
|
||||
ssl->options.connectState = CONNECT_BEGIN;
|
||||
ssl->options.acceptState = ACCEPT_BEGIN;
|
||||
ssl->options.handShakeState = NULL_STATE;
|
||||
|
||||
ssl->error = WANT_READ;
|
||||
WOLFSSL_ERROR(ssl->error);
|
||||
return WOLFSSL_FATAL_ERROR;
|
||||
}
|
||||
#endif /* WOLFSSL_DTLS13 */
|
||||
}
|
||||
|
||||
ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE;
|
||||
@ -11879,6 +11933,12 @@ int wolfSSL_accept_TLSv13(WOLFSSL* ssl)
|
||||
}
|
||||
}
|
||||
|
||||
ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE;
|
||||
WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
|
||||
FALL_THROUGH;
|
||||
|
||||
case TLS13_ACCEPT_SECOND_REPLY_DONE :
|
||||
|
||||
#ifdef WOLFSSL_DTLS
|
||||
if (ssl->chGoodCb != NULL) {
|
||||
int cbret = ssl->chGoodCb(ssl, ssl->chGoodCtx);
|
||||
@ -11890,11 +11950,6 @@ int wolfSSL_accept_TLSv13(WOLFSSL* ssl)
|
||||
}
|
||||
#endif
|
||||
|
||||
ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE;
|
||||
WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
|
||||
FALL_THROUGH;
|
||||
|
||||
case TLS13_ACCEPT_SECOND_REPLY_DONE :
|
||||
if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) {
|
||||
WOLFSSL_ERROR(ssl->error);
|
||||
return WOLFSSL_FATAL_ERROR;
|
||||
|
98
tests/api.c
98
tests/api.c
@ -56080,7 +56080,6 @@ static int test_wolfSSL_dtls_AEAD_limit(void)
|
||||
return -2;
|
||||
|
||||
printf(resultFmt, passed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
@ -56090,6 +56089,102 @@ static int test_wolfSSL_dtls_AEAD_limit(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) && \
|
||||
defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
|
||||
static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
|
||||
{
|
||||
int fd, ret;
|
||||
byte ch_msg[] = {
|
||||
0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
|
||||
0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
|
||||
0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
|
||||
0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
|
||||
0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
|
||||
0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
|
||||
0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
|
||||
0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
|
||||
0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
|
||||
0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
|
||||
0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
|
||||
0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
|
||||
0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
|
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xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
|
||||
0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
|
||||
0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
|
||||
0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
|
||||
0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
|
||||
0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
|
||||
0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
|
||||
0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
|
||||
0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
|
||||
0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
|
||||
0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
|
||||
0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
|
||||
0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
|
||||
0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
|
||||
0x31, 0x68, 0x4c
|
||||
};
|
||||
|
||||
fd = wolfSSL_get_fd(ssl);
|
||||
ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
|
||||
AssertIntGT(ret, 0);
|
||||
/* consume the HRR otherwise handshake will fail */
|
||||
ret = recv(fd, ch_msg, sizeof(ch_msg), 0);
|
||||
AssertIntGT(ret, 0);
|
||||
}
|
||||
|
||||
static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
|
||||
{
|
||||
int ret;
|
||||
ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
|
||||
AssertIntEQ(ret, WOLFSSL_SUCCESS);
|
||||
}
|
||||
|
||||
static int test_wolfSSL_dtls_stateless(void)
|
||||
{
|
||||
callback_functions client_cbs, server_cbs;
|
||||
|
||||
XMEMSET(&client_cbs, 0, sizeof(client_cbs));
|
||||
XMEMSET(&server_cbs, 0, sizeof(server_cbs));
|
||||
client_cbs.doUdp = server_cbs.doUdp = 1;
|
||||
client_cbs.method = wolfDTLSv1_3_client_method;
|
||||
server_cbs.method = wolfDTLSv1_3_server_method;
|
||||
|
||||
client_cbs.ssl_ready = test_wolfSSL_dtls_send_ch;
|
||||
server_cbs.ssl_ready = test_wolfSSL_dtls_enable_hrrcookie;
|
||||
test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
|
||||
|
||||
if (!client_cbs.return_code)
|
||||
return -1;
|
||||
if (!server_cbs.return_code)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
static int test_wolfSSL_dtls_stateless(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
|
||||
* HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
|
||||
|
||||
#if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
|
||||
!defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
|
||||
!defined(WOLFSSL_NO_CLIENT_AUTH))
|
||||
@ -58803,6 +58898,7 @@ TEST_CASE testCases[] = {
|
||||
TEST_DECL(test_wolfSSL_dtls_fragments),
|
||||
TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
|
||||
TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
|
||||
TEST_DECL(test_wolfSSL_dtls_stateless),
|
||||
TEST_DECL(test_generate_cookie),
|
||||
TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
|
||||
TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
|
||||
|
Loading…
x
Reference in New Issue
Block a user