Skip to content

Commit 5adf392

Browse files
authored
Merge pull request wolfSSL#9281 from effbiae/tlsx-with-ech
refactor to TLSX_ChangeSNIBegin/End
2 parents d86575c + 8969e5f commit 5adf392

File tree

1 file changed

+62
-107
lines changed

1 file changed

+62
-107
lines changed

src/tls.c

Lines changed: 62 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14978,26 +14978,26 @@ int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
1497814978
#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
1497914979

1498014980
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
14981-
/* because the size of ech depends on the size of other extensions we need to
14982-
* get the size with ech special and process ech last, return status */
14983-
static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
14984-
word16* pLength)
14981+
static int TLSX_EchChangeSNI(WOLFSSL* ssl, TLSX** pEchX,
14982+
char* serverName, TLSX** pServerNameX,
14983+
TLSX*** pExtensions)
1498514984
{
1498614985
int ret = 0;
1498714986
TLSX* echX = NULL;
1498814987
TLSX* serverNameX = NULL;
1498914988
TLSX** extensions = NULL;
14990-
WC_DECLARE_VAR(tmpServerName, char, MAX_PUBLIC_NAME_SZ, 0);
1499114989

1499214990
/* calculate the rest of the extensions length with inner ech */
1499314991
if (ssl->extensions)
1499414992
echX = TLSX_Find(ssl->extensions, TLSX_ECH);
1499514993

1499614994
if (echX == NULL && ssl->ctx && ssl->ctx->extensions)
14995+
/* if not NULL the semaphore will stop it from being counted */
1499714996
echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
1499814997

1499914998
/* if type is outer change sni to public name */
15000-
if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER &&
14999+
if (echX != NULL &&
15000+
((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER &&
1500115001
(ssl->options.echAccepted ||
1500215002
((WOLFSSL_ECH*)echX->data)->innerCount == 0)) {
1500315003
if (ssl->extensions) {
@@ -15017,53 +15017,72 @@ static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
1501715017
char* hostName = ((SNI*)serverNameX->data)->data.host_name;
1501815018
word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
1501915019

15020-
#ifdef WOLFSSL_SMALL_STACK
15021-
tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
15022-
DYNAMIC_TYPE_TMP_BUFFER);
15023-
if (tmpServerName == NULL)
15024-
return MEMORY_E;
15025-
#else
1502615020
/* truncate if too long */
1502715021
if (hostNameSz > MAX_PUBLIC_NAME_SZ)
1502815022
hostNameSz = MAX_PUBLIC_NAME_SZ;
15029-
#endif
1503015023

15031-
XMEMCPY(tmpServerName, hostName, hostNameSz);
15024+
XMEMCPY(serverName, hostName, hostNameSz);
1503215025
}
1503315026

1503415027
/* remove the inner server name */
1503515028
TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
1503615029

15037-
ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
15038-
((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
15039-
XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
15040-
ssl->heap);
15041-
1504215030
/* set the public name as the server name */
15043-
if (ret == WOLFSSL_SUCCESS)
15031+
if ((ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
15032+
((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
15033+
XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
15034+
ssl->heap)) == WOLFSSL_SUCCESS)
1504415035
ret = 0;
1504515036
}
15037+
*pServerNameX = serverNameX;
15038+
*pExtensions = extensions;
15039+
*pEchX = echX;
15040+
return ret;
15041+
}
1504615042

15047-
if (ret == 0 && ssl->extensions)
15048-
ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, pLength);
15049-
15050-
if (ret == 0 && ssl->ctx && ssl->ctx->extensions)
15051-
ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, pLength);
15043+
static int TLSX_EchRestoreSNI(WOLFSSL* ssl, char* serverName,
15044+
TLSX* serverNameX, TLSX** extensions)
15045+
{
15046+
int ret = 0;
1505215047

1505315048
if (serverNameX != NULL) {
1505415049
/* remove the public name SNI */
1505515050
TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
1505615051

15052+
/* restore the inner server name */
1505715053
ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
15058-
tmpServerName, XSTRLEN(tmpServerName), ssl->heap);
15054+
serverName, XSTRLEN(serverName), ssl->heap);
1505915055

15060-
/* restore the inner server name */
1506115056
if (ret == WOLFSSL_SUCCESS)
1506215057
ret = 0;
1506315058
}
15059+
return ret;
15060+
}
15061+
15062+
/* because the size of ech depends on the size of other extensions we need to
15063+
* get the size with ech special and process ech last, return status */
15064+
static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
15065+
word16* pLength)
15066+
{
15067+
int ret = 0, r = 0;
15068+
TLSX* echX = NULL;
15069+
TLSX* serverNameX = NULL;
15070+
TLSX** extensions = NULL;
15071+
WC_DECLARE_VAR(serverName, char, MAX_PUBLIC_NAME_SZ, 0);
1506415072

15065-
WC_FREE_VAR_EX(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
15073+
WC_ALLOC_VAR_EX(serverName, char, MAX_PUBLIC_NAME_SZ, NULL,
15074+
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
15075+
r = TLSX_EchChangeSNI(ssl, &echX, serverName, &serverNameX, &extensions);
15076+
if (r == 0 && ssl->extensions)
15077+
ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, pLength);
15078+
if (r == 0 && ret == 0 && ssl->ctx && ssl->ctx->extensions)
15079+
ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, pLength);
15080+
if (r == 0)
15081+
r = TLSX_EchRestoreSNI(ssl, serverName, serverNameX, extensions);
1506615082

15083+
WC_FREE_VAR_EX(serverName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
15084+
if (ret == 0 && r != 0)
15085+
ret = r;
1506715086
return ret;
1506815087
}
1506915088
#endif
@@ -15188,87 +15207,32 @@ int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word32* pLength)
1518815207
static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
1518915208
byte msgType, word16* pOffset)
1519015209
{
15191-
int ret = 0;
15210+
int r = 0, ret = 0;
1519215211
TLSX* echX = NULL;
1519315212
TLSX* serverNameX = NULL;
1519415213
TLSX** extensions = NULL;
15195-
WC_DECLARE_VAR(tmpServerName, char, MAX_PUBLIC_NAME_SZ, 0);
15196-
15197-
/* get the echX from either extensions or ctx */
15198-
if (ssl->extensions)
15199-
echX = TLSX_Find(ssl->extensions, TLSX_ECH);
15200-
15201-
if (echX == NULL && ssl->ctx && ssl->ctx->extensions) {
15202-
/* if not NULL the semaphore will stop it from being counted */
15203-
if (echX == NULL)
15204-
echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
15205-
}
15214+
WC_DECLARE_VAR(serverName, char, MAX_PUBLIC_NAME_SZ, 0);
1520615215

15207-
/* if type is outer change sni to public name */
15208-
if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER &&
15209-
(ssl->options.echAccepted ||
15210-
((WOLFSSL_ECH*)echX->data)->innerCount == 0)) {
15211-
if (ssl->extensions) {
15212-
serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
15213-
15214-
if (serverNameX != NULL)
15215-
extensions = &ssl->extensions;
15216-
}
15217-
15218-
if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
15219-
serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
15220-
extensions = &ssl->ctx->extensions;
15221-
}
15222-
15223-
/* store the inner server name */
15224-
if (serverNameX != NULL) {
15225-
char* hostName = ((SNI*)serverNameX->data)->data.host_name;
15226-
word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
15227-
15228-
#ifdef WOLFSSL_SMALL_STACK
15229-
tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
15230-
DYNAMIC_TYPE_TMP_BUFFER);
15231-
if (tmpServerName == NULL)
15232-
return MEMORY_E;
15233-
#else
15234-
/* truncate if too long */
15235-
if (hostNameSz > MAX_PUBLIC_NAME_SZ)
15236-
hostNameSz = MAX_PUBLIC_NAME_SZ;
15237-
#endif
15238-
15239-
XMEMCPY(tmpServerName, hostName, hostNameSz);
15240-
}
15241-
15242-
/* remove the inner server name */
15243-
TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
15244-
15245-
ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
15246-
((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
15247-
XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
15248-
ssl->heap);
15249-
15250-
/* set the public name as the server name */
15251-
if (ret == WOLFSSL_SUCCESS)
15252-
ret = 0;
15253-
}
15254-
15255-
if (echX != NULL) {
15216+
WC_ALLOC_VAR_EX(serverName, char, MAX_PUBLIC_NAME_SZ, NULL,
15217+
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
15218+
r = TLSX_EchChangeSNI(ssl, &echX, serverName, &serverNameX, &extensions);
15219+
ret = r;
15220+
if (ret == 0 && echX != NULL)
1525615221
/* turn ech on so it doesn't write, then write it last */
1525715222
TURN_ON(semaphore, TLSX_ToSemaphore(echX->type));
15258-
}
1525915223

1526015224
if (ret == 0 && ssl->extensions) {
1526115225
ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
15262-
msgType, pOffset);
15226+
msgType, pOffset);
1526315227
}
1526415228

1526515229
if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
1526615230
ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
15267-
msgType, pOffset);
15231+
msgType, pOffset);
1526815232
}
1526915233

1527015234
/* only write if have a shot at acceptance */
15271-
if (echX != NULL &&
15235+
if (ret == 0 && echX != NULL &&
1527215236
(ssl->options.echAccepted ||
1527315237
((WOLFSSL_ECH*)echX->data)->innerCount == 0)) {
1527415238
if (echX != NULL) {
@@ -15287,21 +15251,12 @@ static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
1528715251
}
1528815252
}
1528915253

15290-
if (serverNameX != NULL) {
15291-
int r;
15292-
/* remove the public name SNI */
15293-
TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
15294-
15295-
/* restore the inner server name */
15296-
r = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME, tmpServerName,
15297-
XSTRLEN(tmpServerName), ssl->heap);
15298-
15299-
if (ret == 0 && r != WOLFSSL_SUCCESS)
15300-
ret = r;
15301-
}
15302-
15303-
WC_FREE_VAR_EX(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
15254+
if (r == 0)
15255+
r = TLSX_EchRestoreSNI(ssl, serverName, serverNameX, extensions);
15256+
WC_FREE_VAR_EX(serverName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
1530415257

15258+
if (ret == 0 && r != 0)
15259+
ret = r;
1530515260
return ret;
1530615261
}
1530715262
#endif

0 commit comments

Comments
 (0)