Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2613,6 +2613,9 @@ int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
if (extensions == NULL || data == NULL)
return BAD_FUNC_ARG;

if ((type == WOLFSSL_SNI_HOST_NAME) && (size >= WOLFSSL_HOST_NAME_MAX))
Comment thread
douzzer marked this conversation as resolved.
return BAD_LENGTH_E;

if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
return MEMORY_E;

Expand Down Expand Up @@ -13445,7 +13448,6 @@ void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)

#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
#define GREASE_ECH_SIZE 160
#define MAX_PUBLIC_NAME_SZ 256
#define TLS_INFO_CONST_STRING "tls ech"
#define TLS_INFO_CONST_STRING_SZ 7

Expand Down Expand Up @@ -16101,14 +16103,10 @@ static int TLSX_EchChangeSNI(WOLFSSL* ssl, TLSX** pEchX,
char* hostName = ((SNI*)serverNameX->data)->data.host_name;
word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;

/* truncate if too long */
if (hostNameSz > MAX_PUBLIC_NAME_SZ)
hostNameSz = MAX_PUBLIC_NAME_SZ;

XMEMCPY(serverName, hostName, hostNameSz);
/* Guarantee NUL termination after truncation so that
* TLSX_EchRestoreSNI's XSTRLEN cannot read past the buffer. */
serverName[hostNameSz - 1] = '\0';
if (hostNameSz > WOLFSSL_HOST_NAME_MAX)
ret = BAD_LENGTH_E;
else
XMEMCPY(serverName, hostName, hostNameSz);
}

/* only swap the SNI if one was found; extensions is non-NULL if an
Comment thread
douzzer marked this conversation as resolved.
Expand Down Expand Up @@ -16161,9 +16159,9 @@ static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
TLSX* echX = NULL;
TLSX* serverNameX = NULL;
TLSX** extensions = NULL;
WC_DECLARE_VAR(serverName, char, MAX_PUBLIC_NAME_SZ, 0);
WC_DECLARE_VAR(serverName, char, WOLFSSL_HOST_NAME_MAX, 0);

WC_ALLOC_VAR_EX(serverName, char, MAX_PUBLIC_NAME_SZ, NULL,
WC_ALLOC_VAR_EX(serverName, char, WOLFSSL_HOST_NAME_MAX, NULL,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
r = TLSX_EchChangeSNI(ssl, &echX, serverName, &serverNameX, &extensions);
if (r == 0 && ssl->extensions)
Expand Down Expand Up @@ -16303,9 +16301,9 @@ static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
TLSX* echX = NULL;
TLSX* serverNameX = NULL;
TLSX** extensions = NULL;
WC_DECLARE_VAR(serverName, char, MAX_PUBLIC_NAME_SZ, 0);
WC_DECLARE_VAR(serverName, char, WOLFSSL_HOST_NAME_MAX, 0);

WC_ALLOC_VAR_EX(serverName, char, MAX_PUBLIC_NAME_SZ, NULL,
WC_ALLOC_VAR_EX(serverName, char, WOLFSSL_HOST_NAME_MAX, NULL,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
r = TLSX_EchChangeSNI(ssl, &echX, serverName, &serverNameX, &extensions);
ret = r;
Expand Down
12 changes: 7 additions & 5 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -15202,13 +15202,15 @@ static int test_wolfSSL_Tls13_ECH_long_SNI(void)
ExpectIntEQ(wolfSSL_SetEchConfigs(test_ctx.c_ssl, echCbTestConfigs,
echCbTestConfigsLen), WOLFSSL_SUCCESS);

/* Set the over-long SNI as the inner hostname */
/* Try to set the over-long SNI as the inner hostname -- after the fix, this
* is expected to fail.
*/
ExpectIntEQ(wolfSSL_UseSNI(test_ctx.c_ssl, WOLFSSL_SNI_HOST_NAME,
longName, (word16)XSTRLEN(longName)), WOLFSSL_SUCCESS);
longName, (word16)XSTRLEN(longName)), BAD_LENGTH_E);

/* The handshake triggers TLSX_EchChangeSNI / TLSX_EchRestoreSNI.
* Before the fix this would stack-buffer-overflow in XSTRLEN.
* The connection may fail (SNI mismatch) but must not crash. */
/* Before the fix, the handshake would trigger TLSX_EchChangeSNI /
* TLSX_EchRestoreSNI, which would then stack-buffer-overflow in XSTRLEN.
*/
(void)test_ssl_memio_do_handshake(&test_ctx, 10, NULL);

test_ssl_memio_cleanup(&test_ctx);
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ struct WOLFSSL_ASN1_STRING {

#define WOLFSSL_MAX_SNAME 40

#define WOLFSSL_HOST_NAME_MAX 256

#define WOLFSSL_ASN1_DYNAMIC 0x1
#define WOLFSSL_ASN1_DYNAMIC_DATA 0x2
Expand Down Expand Up @@ -861,7 +862,6 @@ struct WOLFSSL_X509_STORE {
#define WOLFSSL_USE_CHECK_TIME 0x2
#define WOLFSSL_NO_CHECK_TIME 0x200000
#define WOLFSSL_PARTIAL_CHAIN 0x80000
#define WOLFSSL_HOST_NAME_MAX 256

#define WOLFSSL_VPARAM_DEFAULT 0x1
#define WOLFSSL_VPARAM_OVERWRITE 0x2
Expand Down
Loading