From 8dd45896502605f69a392a80df0852e108035440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Fri, 27 Jun 2014 15:55:45 -0300 Subject: [PATCH] asn: refactoring SetName to reduce stack usage: 1152 bytes - pointers size moved to the heap. --- variable names moved to the heap (1152 bytes saved) --- ctaocrypt/src/asn.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index 175a7e28c..692e0c78f 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -5254,8 +5254,19 @@ static int SetCa(byte* output) /* encode CertName into output, return total bytes written */ static int SetName(byte* output, CertName* name) { - int totalBytes = 0, i, idx; - EncodedName names[NAME_ENTRIES]; + int totalBytes = 0, i, idx; +#ifdef CYASSL_SMALL_STACK + EncodedName* names = NULL; +#else + EncodedName names[NAME_ENTRIES]; +#endif + +#ifdef CYASSL_SMALL_STACK + names = (EncodedName*)XMALLOC(sizeof(EncodedName) * NAME_ENTRIES, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (names == NULL) + return MEMORY_E; +#endif for (i = 0; i < NAME_ENTRIES; i++) { const char* nameStr = GetOneName(name, i); @@ -5297,8 +5308,12 @@ static int SetName(byte* output, CertName* name) setSz = SetSet(thisLen, set); thisLen += setSz; - if (thisLen > (int)sizeof(names[i].encoded)) + if (thisLen > (int)sizeof(names[i].encoded)) { +#ifdef CYASSL_SMALL_STACK + XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return BUFFER_E; + } /* store it */ idx = 0; @@ -5348,8 +5363,12 @@ static int SetName(byte* output, CertName* name) /* header */ idx = SetSequence(totalBytes, output); totalBytes += idx; - if (totalBytes > ASN_NAME_MAX) + if (totalBytes > ASN_NAME_MAX) { +#ifdef CYASSL_SMALL_STACK + XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return BUFFER_E; + } for (i = 0; i < NAME_ENTRIES; i++) { if (names[i].used) { @@ -5357,6 +5376,11 @@ static int SetName(byte* output, CertName* name) idx += names[i].totalLen; } } + +#ifdef CYASSL_SMALL_STACK + XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + return totalBytes; }