io: refactoring EmbedGenerateCookie to reduce stack usage:

--- use ShaHash instead of InitSha, ShaUpdate and ShaFinal (sizeof(Sha) saved)

io: refactoring EmbedOcspLookup to reduce stack usage:
--- variable domainName moved to the heap (80 bytes saved)
--- variable path moved to the heap (80 bytes saved)
This commit is contained in:
Moisés Guimarães
2014-07-23 11:36:16 -03:00
parent 01ef6c3390
commit 7dfb9e2d5f

View File

@@ -475,7 +475,6 @@ int EmbedGenerateCookie(CYASSL* ssl, byte *buf, int sz, void *ctx)
int sd = ssl->wfd; int sd = ssl->wfd;
struct sockaddr_storage peer; struct sockaddr_storage peer;
XSOCKLENT peerSz = sizeof(peer); XSOCKLENT peerSz = sizeof(peer);
Sha sha;
byte digest[SHA_DIGEST_SIZE]; byte digest[SHA_DIGEST_SIZE];
int ret = 0; int ret = 0;
@@ -486,12 +485,10 @@ int EmbedGenerateCookie(CYASSL* ssl, byte *buf, int sz, void *ctx)
CYASSL_MSG("getpeername failed in EmbedGenerateCookie"); CYASSL_MSG("getpeername failed in EmbedGenerateCookie");
return GEN_COOKIE_E; return GEN_COOKIE_E;
} }
ret = InitSha(&sha); ret = ShaHash((byte*)&peer, peerSz, digest);
if (ret != 0) if (ret != 0)
return ret; return ret;
ShaUpdate(&sha, (byte*)&peer, peerSz);
ShaFinal(&sha, digest);
if (sz > SHA_DIGEST_SIZE) if (sz > SHA_DIGEST_SIZE)
sz = SHA_DIGEST_SIZE; sz = SHA_DIGEST_SIZE;
@@ -839,12 +836,18 @@ static int process_http_response(int sfd, byte** respBuf,
int EmbedOcspLookup(void* ctx, const char* url, int urlSz, int EmbedOcspLookup(void* ctx, const char* url, int urlSz,
byte* ocspReqBuf, int ocspReqSz, byte** ocspRespBuf) byte* ocspReqBuf, int ocspReqSz, byte** ocspRespBuf)
{ {
char domainName[80], path[80];
int httpBufSz; int httpBufSz;
SOCKET_T sfd = 0; SOCKET_T sfd = 0;
word16 port; word16 port;
int ocspRespSz = 0; int ocspRespSz = 0;
byte* httpBuf = NULL; byte* httpBuf = NULL;
#ifdef CYASSL_SMALL_STACK
char* path;
char* domainName;
#else
char path[80];
char domainName[80];
#endif
(void)ctx; (void)ctx;
@@ -858,8 +861,24 @@ int EmbedOcspLookup(void* ctx, const char* url, int urlSz,
return -1; return -1;
} }
#ifdef CYASSL_SMALL_STACK
path = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (path == NULL)
return MEMORY_E;
domainName = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (domainName == NULL) {
XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
if (decode_url(url, urlSz, domainName, path, &port) < 0) { if (decode_url(url, urlSz, domainName, path, &port) < 0) {
CYASSL_MSG("Unable to decode OCSP URL"); CYASSL_MSG("Unable to decode OCSP URL");
#ifdef CYASSL_SMALL_STACK
XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return -1; return -1;
} }
@@ -870,6 +889,10 @@ int EmbedOcspLookup(void* ctx, const char* url, int urlSz,
if (httpBuf == NULL) { if (httpBuf == NULL) {
CYASSL_MSG("Unable to create OCSP response buffer"); CYASSL_MSG("Unable to create OCSP response buffer");
#ifdef CYASSL_SMALL_STACK
XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return -1; return -1;
} }
@@ -889,17 +912,30 @@ int EmbedOcspLookup(void* ctx, const char* url, int urlSz,
close(sfd); close(sfd);
if (ocspRespSz == 0) { if (ocspRespSz == 0) {
CYASSL_MSG("OCSP response was not OK, no OCSP response"); CYASSL_MSG("OCSP response was not OK, no OCSP response");
XFREE(httpBuf, NULL, DYNAMIC_TYPE_IN_BUFFER); XFREE(httpBuf, NULL, DYNAMIC_TYPE_IN_BUFFER);
#ifdef CYASSL_SMALL_STACK
XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return -1; return -1;
} }
} else { } else {
CYASSL_MSG("OCSP Responder connection failed"); CYASSL_MSG("OCSP Responder connection failed");
close(sfd); close(sfd);
XFREE(httpBuf, NULL, DYNAMIC_TYPE_IN_BUFFER); XFREE(httpBuf, NULL, DYNAMIC_TYPE_IN_BUFFER);
#ifdef CYASSL_SMALL_STACK
XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return -1; return -1;
} }
XFREE(httpBuf, NULL, DYNAMIC_TYPE_IN_BUFFER); XFREE(httpBuf, NULL, DYNAMIC_TYPE_IN_BUFFER);
#ifdef CYASSL_SMALL_STACK
XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ocspRespSz; return ocspRespSz;
} }