3232/* create the hpke key and ech config to send to clients */
3333int 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
@@ -166,33 +175,52 @@ int wolfSSL_CTX_GenerateEchConfig(WOLFSSL_CTX* ctx, const char* publicName,
166175 return ret ;
167176}
168177
169- int wolfSSL_CTX_SetEchConfigsBase64 (WOLFSSL_CTX * ctx , const char * echConfigs64 ,
170- word32 echConfigs64Len )
178+ /* base64-decode echConfigs into a freshly allocated buffer */
179+ static int DecodeEchConfigsBase64 (void * heap , const char * echConfigs64 ,
180+ word32 echConfigs64Len , byte * * decodedConfigs , word32 * decodedLen )
171181{
172182 int ret = 0 ;
173- word32 decodedLen = echConfigs64Len * 3 / 4 + 1 ;
174- byte * decodedConfigs ;
183+ byte * buf ;
184+ word32 len = echConfigs64Len * 3 / 4 + 1 ;
175185
176- if (ctx == NULL || echConfigs64 == NULL || echConfigs64Len == 0 )
186+ if (echConfigs64 == NULL || echConfigs64Len == 0 )
177187 return BAD_FUNC_ARG ;
178188
179- decodedConfigs = (byte * )XMALLOC (decodedLen , ctx -> heap ,
180- DYNAMIC_TYPE_TMP_BUFFER );
189+ buf = (byte * )XMALLOC (len , heap , DYNAMIC_TYPE_TMP_BUFFER );
181190
182- if (decodedConfigs == NULL )
191+ if (buf == NULL )
183192 return MEMORY_E ;
184193
185- decodedConfigs [ decodedLen - 1 ] = 0 ;
194+ buf [ len - 1 ] = 0 ;
186195
187196 /* decode the echConfigs */
188- ret = Base64_Decode ((const byte * )echConfigs64 , echConfigs64Len ,
189- decodedConfigs , & decodedLen );
197+ ret = Base64_Decode ((const byte * )echConfigs64 , echConfigs64Len , buf , & len );
190198
191199 if (ret != 0 ) {
192- XFREE (decodedConfigs , ctx -> heap , DYNAMIC_TYPE_TMP_BUFFER );
200+ XFREE (buf , heap , DYNAMIC_TYPE_TMP_BUFFER );
193201 return ret ;
194202 }
195203
204+ * decodedConfigs = buf ;
205+ * decodedLen = len ;
206+ return 0 ;
207+ }
208+
209+ int wolfSSL_CTX_SetEchConfigsBase64 (WOLFSSL_CTX * ctx , const char * echConfigs64 ,
210+ word32 echConfigs64Len )
211+ {
212+ int ret ;
213+ word32 decodedLen ;
214+ byte * decodedConfigs ;
215+
216+ if (ctx == NULL )
217+ return BAD_FUNC_ARG ;
218+
219+ ret = DecodeEchConfigsBase64 (ctx -> heap , echConfigs64 , echConfigs64Len ,
220+ & decodedConfigs , & decodedLen );
221+ if (ret != 0 )
222+ return ret ;
223+
196224 ret = wolfSSL_CTX_SetEchConfigs (ctx , decodedConfigs , decodedLen );
197225
198226 XFREE (decodedConfigs , ctx -> heap , DYNAMIC_TYPE_TMP_BUFFER );
@@ -249,34 +277,17 @@ void wolfSSL_CTX_SetEchEnable(WOLFSSL_CTX* ctx, byte enable)
249277int wolfSSL_SetEchConfigsBase64 (WOLFSSL * ssl , const char * echConfigs64 ,
250278 word32 echConfigs64Len )
251279{
252- int ret = 0 ;
253- word32 decodedLen = echConfigs64Len * 3 / 4 + 1 ;
280+ int ret ;
281+ word32 decodedLen ;
254282 byte * decodedConfigs ;
255283
256- if (ssl == NULL || echConfigs64 == NULL || echConfigs64Len == 0 )
284+ if (ssl == NULL )
257285 return BAD_FUNC_ARG ;
258286
259- /* already have ech configs */
260- if (ssl -> echConfigs != NULL ) {
261- return WOLFSSL_FATAL_ERROR ;
262- }
263-
264- decodedConfigs = (byte * )XMALLOC (decodedLen , ssl -> heap ,
265- DYNAMIC_TYPE_TMP_BUFFER );
266-
267- if (decodedConfigs == NULL )
268- return MEMORY_E ;
269-
270- decodedConfigs [decodedLen - 1 ] = 0 ;
271-
272- /* decode the echConfigs */
273- ret = Base64_Decode ((const byte * )echConfigs64 , echConfigs64Len ,
274- decodedConfigs , & decodedLen );
275-
276- if (ret != 0 ) {
277- XFREE (decodedConfigs , ssl -> heap , DYNAMIC_TYPE_TMP_BUFFER );
287+ ret = DecodeEchConfigsBase64 (ssl -> heap , echConfigs64 , echConfigs64Len ,
288+ & decodedConfigs , & decodedLen );
289+ if (ret != 0 )
278290 return ret ;
279- }
280291
281292 ret = wolfSSL_SetEchConfigs (ssl , decodedConfigs , decodedLen );
282293
@@ -418,8 +429,8 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
418429 output += 2 ;
419430 }
420431
421- /* set maximum name length to 0 */
422- * output = 0 ;
432+ /* maximum name len */
433+ * output = config -> maxNameLen ;
423434 output ++ ;
424435
425436 /* publicName len */
@@ -430,7 +441,7 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
430441 XMEMCPY (output , config -> publicName , publicNameLen );
431442 output += publicNameLen ;
432443
433- /* terminating zeros */
444+ /* no extensions, print zeros */
434445 c16toa (0 , output );
435446 /* output += 2; */
436447
@@ -656,11 +667,12 @@ int SetEchConfigsEx(WOLFSSL_EchConfig** outputConfigs, void* heap,
656667 idx += 4 ;
657668 }
658669
659- /* ignore maximum name length */
670+ /* maxNameLen */
660671 if (idx + 1 > length ) {
661672 ret = BUFFER_E ;
662673 break ;
663674 }
675+ workingConfig -> maxNameLen = echConfig [idx ];
664676 idx += 1 ;
665677
666678 /* publicName */
@@ -701,7 +713,7 @@ int SetEchConfigsEx(WOLFSSL_EchConfig** outputConfigs, void* heap,
701713 }
702714
703715 ret = EchConfigCheckExtensions (echConfig + idx , extensionsLen );
704- if (ret < 0 )
716+ if (ret < 0 && ret != WC_NO_ERR_TRACE ( UNSUPPORTED_EXTENSION ) )
705717 break ;
706718
707719 /* KEM, ciphersuite, or mandatory extension not supported, free this
0 commit comments