ssl: refactoring CyaSSL_SetTmpDH_buffer_wrapper to reduce stack usage:

--- variable p moved to the heap (513 bytes saved)
--- variable p moved to the heap (513 bytes saved)
This commit is contained in:
Moisés Guimarães 2014-09-15 17:22:41 -03:00
parent 3281e0dfcf
commit cb9dafca26

View File

@ -3357,41 +3357,59 @@ static int CyaSSL_SetTmpDH_buffer_wrapper(CYASSL_CTX* ctx, CYASSL* ssl,
const unsigned char* buf, long sz, int format) const unsigned char* buf, long sz, int format)
{ {
buffer der; buffer der;
int ret; int ret = 0;
int weOwnDer = 0; int weOwnDer = 0;
word32 pSz = MAX_DH_SIZE;
word32 gSz = MAX_DH_SIZE;
#ifdef CYASSL_SMALL_STACK
byte* p = NULL;
byte* g = NULL;
#else
byte p[MAX_DH_SIZE]; byte p[MAX_DH_SIZE];
byte g[MAX_DH_SIZE]; byte g[MAX_DH_SIZE];
word32 pSz = sizeof(p); #endif
word32 gSz = sizeof(g);
der.buffer = (byte*)buf; der.buffer = (byte*)buf;
der.length = (word32)sz; der.length = (word32)sz;
if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM) #ifdef CYASSL_SMALL_STACK
return SSL_BAD_FILETYPE; p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (p == NULL || g == NULL) {
XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM)
ret = SSL_BAD_FILETYPE;
else {
if (format == SSL_FILETYPE_PEM) { if (format == SSL_FILETYPE_PEM) {
der.buffer = NULL; der.buffer = NULL;
ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap, NULL,NULL); ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap, NULL,NULL);
if (ret < 0) {
XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
return ret;
}
weOwnDer = 1; weOwnDer = 1;
} }
if (ret == 0) {
if (DhParamsLoad(der.buffer, der.length, p, &pSz, g, &gSz) < 0) if (DhParamsLoad(der.buffer, der.length, p, &pSz, g, &gSz) < 0)
ret = SSL_BAD_FILETYPE; ret = SSL_BAD_FILETYPE;
else { else if (ssl)
if (ssl)
ret = CyaSSL_SetTmpDH(ssl, p, pSz, g, gSz); ret = CyaSSL_SetTmpDH(ssl, p, pSz, g, gSz);
else else
ret = CyaSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz); ret = CyaSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
} }
}
if (weOwnDer) if (weOwnDer)
XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY); XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
#ifdef CYASSL_SMALL_STACK
XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret; return ret;
} }