Skip to content

Commit d029aed

Browse files
shrink ech config decoding for Arduino
1 parent f6bc398 commit d029aed

1 file changed

Lines changed: 37 additions & 35 deletions

File tree

src/ssl_ech.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,52 @@ int wolfSSL_CTX_GenerateEchConfigEx(WOLFSSL_CTX* ctx, const char* publicName,
175175
return ret;
176176
}
177177

178-
int wolfSSL_CTX_SetEchConfigsBase64(WOLFSSL_CTX* ctx, const char* echConfigs64,
179-
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)
180181
{
181182
int ret = 0;
182-
word32 decodedLen = echConfigs64Len * 3 / 4 + 1;
183-
byte* decodedConfigs;
183+
byte* buf;
184+
word32 len = echConfigs64Len * 3 / 4 + 1;
184185

185-
if (ctx == NULL || echConfigs64 == NULL || echConfigs64Len == 0)
186+
if (echConfigs64 == NULL || echConfigs64Len == 0)
186187
return BAD_FUNC_ARG;
187188

188-
decodedConfigs = (byte*)XMALLOC(decodedLen, ctx->heap,
189-
DYNAMIC_TYPE_TMP_BUFFER);
189+
buf = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TMP_BUFFER);
190190

191-
if (decodedConfigs == NULL)
191+
if (buf == NULL)
192192
return MEMORY_E;
193193

194-
decodedConfigs[decodedLen - 1] = 0;
194+
buf[len - 1] = 0;
195195

196196
/* decode the echConfigs */
197-
ret = Base64_Decode((const byte*)echConfigs64, echConfigs64Len,
198-
decodedConfigs, &decodedLen);
197+
ret = Base64_Decode((const byte*)echConfigs64, echConfigs64Len, buf, &len);
199198

200199
if (ret != 0) {
201-
XFREE(decodedConfigs, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
200+
XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
202201
return ret;
203202
}
204203

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+
205224
ret = wolfSSL_CTX_SetEchConfigs(ctx, decodedConfigs, decodedLen);
206225

207226
XFREE(decodedConfigs, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -258,34 +277,17 @@ void wolfSSL_CTX_SetEchEnable(WOLFSSL_CTX* ctx, byte enable)
258277
int wolfSSL_SetEchConfigsBase64(WOLFSSL* ssl, const char* echConfigs64,
259278
word32 echConfigs64Len)
260279
{
261-
int ret = 0;
262-
word32 decodedLen = echConfigs64Len * 3 / 4 + 1;
280+
int ret;
281+
word32 decodedLen;
263282
byte* decodedConfigs;
264283

265-
if (ssl == NULL || echConfigs64 == NULL || echConfigs64Len == 0)
284+
if (ssl == NULL)
266285
return BAD_FUNC_ARG;
267286

268-
/* already have ech configs */
269-
if (ssl->echConfigs != NULL) {
270-
return WOLFSSL_FATAL_ERROR;
271-
}
272-
273-
decodedConfigs = (byte*)XMALLOC(decodedLen, ssl->heap,
274-
DYNAMIC_TYPE_TMP_BUFFER);
275-
276-
if (decodedConfigs == NULL)
277-
return MEMORY_E;
278-
279-
decodedConfigs[decodedLen - 1] = 0;
280-
281-
/* decode the echConfigs */
282-
ret = Base64_Decode((const byte*)echConfigs64, echConfigs64Len,
283-
decodedConfigs, &decodedLen);
284-
285-
if (ret != 0) {
286-
XFREE(decodedConfigs, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
287+
ret = DecodeEchConfigsBase64(ssl->heap, echConfigs64, echConfigs64Len,
288+
&decodedConfigs, &decodedLen);
289+
if (ret != 0)
287290
return ret;
288-
}
289291

290292
ret = wolfSSL_SetEchConfigs(ssl, decodedConfigs, decodedLen);
291293

0 commit comments

Comments
 (0)