Skip to content

Commit 8489b6f

Browse files
tls ech padding improvements
1 parent 3181e2b commit 8489b6f

5 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/ssl_ech.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
/* create the hpke key and ech config to send to clients */
3333
int wolfSSL_CTX_GenerateEchConfig(WOLFSSL_CTX* ctx, const char* publicName,
3434
word16 kemId, word16 kdfId, word16 aeadId)
35+
{
36+
return wolfSSL_CTX_GenerateEchConfigEx(ctx, publicName, kemId, kdfId,
37+
aeadId, 0);
38+
}
39+
40+
/* create the hpke key and ech config to send to clients
41+
* maximum_name_length may also be set for a more stable padding length */
42+
int wolfSSL_CTX_GenerateEchConfigEx(WOLFSSL_CTX* ctx, const char* publicName,
43+
word16 kemId, word16 kdfId, word16 aeadId, byte maxNameLen)
3544
{
3645
int ret = 0;
3746
WOLFSSL_EchConfig* newConfig;
@@ -129,8 +138,8 @@ int wolfSSL_CTX_GenerateEchConfig(WOLFSSL_CTX* ctx, const char* publicName,
129138
ret = MEMORY_E;
130139
}
131140
else {
132-
XMEMCPY(newConfig->publicName, publicName,
133-
XSTRLEN(publicName) + 1);
141+
XMEMCPY(newConfig->publicName, publicName, XSTRLEN(publicName) + 1);
142+
newConfig->maxNameLen = maxNameLen;
134143
}
135144
}
136145

@@ -399,8 +408,8 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
399408
output += 2;
400409
}
401410

402-
/* set maximum name length to 0 */
403-
*output = 0;
411+
/* maximum name len */
412+
*output = config->maxNameLen;
404413
output++;
405414

406415
/* publicName len */
@@ -411,7 +420,7 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
411420
XMEMCPY(output, config->publicName, publicNameLen);
412421
output += publicNameLen;
413422

414-
/* terminating zeros */
423+
/* no extensions, print zeros */
415424
c16toa(0, output);
416425
/* output += 2; */
417426

@@ -599,12 +608,13 @@ int SetEchConfigsEx(WOLFSSL_EchConfig** outputConfigs, void* heap,
599608
echConfig += 4;
600609
}
601610

602-
/* ignore the maximum name length */
611+
/* maxNameLen */
603612
idx++;
604613
if (idx >= length) {
605614
ret = BUFFER_E;
606615
break;
607616
}
617+
workingConfig->maxNameLen = *echConfig;
608618
echConfig++;
609619

610620
/* publicNameLen */

src/tls13.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4786,8 +4786,22 @@ int SendTls13ClientHello(WOLFSSL* ssl)
47864786
if (ret != 0)
47874787
return ret;
47884788

4789+
/* calculate padding (RFC 9849, section 6.1.3) */
4790+
if (args->ech->privateName != NULL) {
4791+
word16 nameLen = (word16)XSTRLEN(args->ech->privateName);
4792+
if (nameLen > args->ech->echConfig->maxNameLen)
4793+
args->ech->paddingLen = 0;
4794+
else
4795+
args->ech->paddingLen =
4796+
(word16)args->ech->echConfig->maxNameLen - nameLen;
4797+
}
4798+
else {
4799+
args->ech->paddingLen = args->ech->echConfig->maxNameLen + 9;
4800+
}
4801+
args->ech->paddingLen += 31 -
4802+
((args->length + args->ech->paddingLen - 1) % 32);
4803+
47894804
/* set innerClientHelloLen to ClientHelloInner + padding + tag */
4790-
args->ech->paddingLen = 31 - ((args->length - 1) % 32);
47914805
args->ech->innerClientHelloLen = args->length +
47924806
args->ech->paddingLen + args->ech->hpke->Nt;
47934807
if (args->ech->innerClientHelloLen > 0xFFFF)

tests/api.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14822,8 +14822,10 @@ static int test_ech_server_ctx_ready(WOLFSSL_CTX* ctx)
1482214822
{
1482314823
int ret;
1482414824

14825-
ret = wolfSSL_CTX_GenerateEchConfig(ctx, echCbTestPublicName,
14826-
echCbTestKemID, echCbTestKdfID, echCbTestAeadID);
14825+
/* +20 for this isn't significant, it just exercises the padding code */
14826+
ret = wolfSSL_CTX_GenerateEchConfigEx(ctx, echCbTestPublicName,
14827+
echCbTestKemID, echCbTestKdfID, echCbTestAeadID,
14828+
XSTRLEN(echCbTestPublicName) + 20);
1482714829
if (ret != WOLFSSL_SUCCESS)
1482814830
return TEST_FAIL;
1482914831

wolfssl/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,6 +3137,7 @@ typedef struct WOLFSSL_EchConfig {
31373137
byte configId;
31383138
byte numCipherSuites;
31393139
byte receiverPubkey[HPKE_Npk_MAX];
3140+
byte maxNameLen;
31403141
} WOLFSSL_EchConfig;
31413142

31423143
typedef struct WOLFSSL_ECH {

wolfssl/ssl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,9 @@ WOLFSSL_API WOLFSSL_METHOD *wolfSSLv23_method(void);
12181218
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
12191219
WOLFSSL_API int wolfSSL_CTX_GenerateEchConfig(WOLFSSL_CTX* ctx,
12201220
const char* publicName, word16 kemId, word16 kdfId, word16 aeadId);
1221+
WOLFSSL_API int wolfSSL_CTX_GenerateEchConfigEx(WOLFSSL_CTX* ctx,
1222+
const char* publicName, word16 kemId, word16 kdfId, word16 aeadId,
1223+
byte maxNameLen);
12211224

12221225
WOLFSSL_API int wolfSSL_CTX_SetEchConfigsBase64(WOLFSSL_CTX* ctx,
12231226
const char* echConfigs64, word32 echConfigs64Len);

0 commit comments

Comments
 (0)