Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ long wolfSSL_TXT_DB_write(WOLFSSL_BIO *out, WOLFSSL_TXT_DB *db)
{
const WOLF_STACK_OF(WOLFSSL_STRING)* data;
long totalLen = 0;
char buf[512]; /* Should be more than enough for a single row */
char* bufEnd = buf + sizeof(buf);
WC_DECLARE_VAR(buf, char, 512, NULL); /* enough for a single row */
char* bufEnd;
int i;

WOLFSSL_ENTER("wolfSSL_TXT_DB_write");
Expand All @@ -160,6 +160,10 @@ long wolfSSL_TXT_DB_write(WOLFSSL_BIO *out, WOLFSSL_TXT_DB *db)
return WOLFSSL_FAILURE;
}

WC_ALLOC_VAR_EX(buf, char, 512, NULL, DYNAMIC_TYPE_TMP_BUFFER,
return WOLFSSL_FAILURE);
bufEnd = buf + 512;

data = db->data;
while (data) {
char** fields = (char**)data->data.string;
Expand All @@ -168,6 +172,7 @@ long wolfSSL_TXT_DB_write(WOLFSSL_BIO *out, WOLFSSL_TXT_DB *db)

if (!fields) {
WOLFSSL_MSG("Missing row");
WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}

Expand All @@ -186,6 +191,7 @@ long wolfSSL_TXT_DB_write(WOLFSSL_BIO *out, WOLFSSL_TXT_DB *db)
}
else {
WOLFSSL_MSG("Data row is too big");
WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}
}
Expand All @@ -194,24 +200,29 @@ long wolfSSL_TXT_DB_write(WOLFSSL_BIO *out, WOLFSSL_TXT_DB *db)
}
else {
WOLFSSL_MSG("Data row is too big");
WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}
}
if (idx > buf)
idx[-1] = '\n';
else
else {
WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}
sz = (int)(idx - buf);

if (wolfSSL_BIO_write(out, buf, sz) != sz) {
WOLFSSL_MSG("wolfSSL_BIO_write error");
WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}
totalLen += sz;

data = data->next;
}

WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return totalLen;
}

Expand Down
26 changes: 15 additions & 11 deletions src/dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,46 +212,50 @@ static int CreateDtls12Cookie(const WOLFSSL* ssl, const WolfSSL_CH* ch,
byte* cookie)
{
int ret;
Hmac cookieHmac;
WC_DECLARE_VAR(cookieHmac, Hmac, 1, ssl->heap);

if (ssl->buffers.dtlsCookieSecret.buffer == NULL ||
ssl->buffers.dtlsCookieSecret.length == 0) {
WOLFSSL_MSG("Missing DTLS 1.2 cookie secret");
return COOKIE_ERROR;
}

ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId);
WC_ALLOC_VAR_EX(cookieHmac, Hmac, 1, ssl->heap, DYNAMIC_TYPE_HMAC,
return MEMORY_E);

ret = wc_HmacInit(cookieHmac, ssl->heap, ssl->devId);
if (ret == 0) {
ret = wc_HmacSetKey(&cookieHmac, DTLS_COOKIE_TYPE,
ret = wc_HmacSetKey(cookieHmac, DTLS_COOKIE_TYPE,
ssl->buffers.dtlsCookieSecret.buffer,
ssl->buffers.dtlsCookieSecret.length);
if (ret == 0) {
/* peerLock not necessary. Still in handshake phase. */
ret = wc_HmacUpdate(&cookieHmac,
ret = wc_HmacUpdate(cookieHmac,
(const byte*)ssl->buffers.dtlsCtx.peer.sa,
ssl->buffers.dtlsCtx.peer.sz);
}
if (ret == 0)
ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->pv, OPAQUE16_LEN);
ret = wc_HmacUpdate(cookieHmac, (byte*)ch->pv, OPAQUE16_LEN);
if (ret == 0)
ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->random, RAN_LEN);
ret = wc_HmacUpdate(cookieHmac, (byte*)ch->random, RAN_LEN);
if (ret == 0) {
ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->sessionId.elements,
ret = wc_HmacUpdate(cookieHmac, (byte*)ch->sessionId.elements,
ch->sessionId.size);
}
if (ret == 0) {
ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->cipherSuite.elements,
ret = wc_HmacUpdate(cookieHmac, (byte*)ch->cipherSuite.elements,
ch->cipherSuite.size);
}
if (ret == 0) {
ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->compression.elements,
ret = wc_HmacUpdate(cookieHmac, (byte*)ch->compression.elements,
ch->compression.size);
}
if (ret == 0)
ret = wc_HmacFinal(&cookieHmac, cookie);
wc_HmacFree(&cookieHmac);
ret = wc_HmacFinal(cookieHmac, cookie);
wc_HmacFree(cookieHmac);
}

WC_FREE_VAR_EX(cookieHmac, ssl->heap, DYNAMIC_TYPE_HMAC);
return ret;
}

Expand Down
23 changes: 15 additions & 8 deletions src/dtls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,29 +489,33 @@ int Dtls13HashClientHello(const WOLFSSL* ssl, byte* hash, int* hashSz,
/* msg_type(1) + length (3) */
byte header[OPAQUE32_LEN];
int ret;
wc_HashAlg hashCtx;
WC_DECLARE_VAR(hashCtx, wc_HashAlg, 1, ssl->heap);
int type = wolfSSL_GetHmacType_ex(specs);

if (type < 0)
return type;

WC_ALLOC_VAR_EX(hashCtx, wc_HashAlg, 1, ssl->heap, DYNAMIC_TYPE_HASHES,
return MEMORY_E);

header[0] = (byte)client_hello;
c32to24(length, header + 1);

ret = wc_HashInit_ex(&hashCtx, (enum wc_HashType)type, ssl->heap, ssl->devId);
ret = wc_HashInit_ex(hashCtx, (enum wc_HashType)type, ssl->heap, ssl->devId);
if (ret == 0) {
ret = wc_HashUpdate(&hashCtx, (enum wc_HashType)type, header, OPAQUE32_LEN);
ret = wc_HashUpdate(hashCtx, (enum wc_HashType)type, header, OPAQUE32_LEN);
if (ret == 0)
ret = wc_HashUpdate(&hashCtx, (enum wc_HashType)type, body, length);
ret = wc_HashUpdate(hashCtx, (enum wc_HashType)type, body, length);
if (ret == 0)
ret = wc_HashFinal(&hashCtx, (enum wc_HashType)type, hash);
ret = wc_HashFinal(hashCtx, (enum wc_HashType)type, hash);
if (ret == 0) {
*hashSz = wc_HashGetDigestSize((enum wc_HashType)type);
if (*hashSz < 0)
ret = *hashSz;
}
wc_HashFree(&hashCtx, (enum wc_HashType)type);
wc_HashFree(hashCtx, (enum wc_HashType)type);
}
WC_FREE_VAR_EX(hashCtx, ssl->heap, DYNAMIC_TYPE_HASHES);
return ret;
}

Expand Down Expand Up @@ -2131,8 +2135,10 @@ static const byte snLabel[SN_LABEL_SZ + 1] = "sn";
*/
int Dtls13DeriveSnKeys(WOLFSSL* ssl, int provision)
{
byte key_dig[MAX_PRF_DIG];
int ret = 0;
WC_DECLARE_VAR(key_dig, byte, MAX_PRF_DIG, ssl->heap);
WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST,
return MEMORY_E);

if (provision & PROVISION_CLIENT) {
WOLFSSL_MSG("Derive SN Client key");
Expand All @@ -2159,8 +2165,9 @@ int Dtls13DeriveSnKeys(WOLFSSL* ssl, int provision)
end:
ForceZero(key_dig, MAX_PRF_DIG);
#ifdef WOLFSSL_CHECK_MEM_ZERO
wc_MemZero_Check(key_dig, sizeof(key_dig));
wc_MemZero_Check(key_dig, MAX_PRF_DIG);
#endif
WC_FREE_VAR_EX(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
return ret;
}

Expand Down
48 changes: 32 additions & 16 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -18581,55 +18581,71 @@ static int SetStaticEphemeralKey(WOLFSSL_CTX* ctx,
#ifdef HAVE_ECC
if (keyAlgo == WC_PK_TYPE_NONE) {
word32 idx = 0;
ecc_key eccKey;
ret = wc_ecc_init_ex(&eccKey, heap, INVALID_DEVID);
WC_DECLARE_VAR(eccKey, ecc_key, 1, heap);
WC_ALLOC_VAR_EX(eccKey, ecc_key, 1, heap, DYNAMIC_TYPE_ECC,
ret = MEMORY_E);
if (ret == 0)
ret = wc_ecc_init_ex(eccKey, heap, INVALID_DEVID);
if (ret == 0) {
ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &eccKey, keySz);
ret = wc_EccPrivateKeyDecode(keyBuf, &idx, eccKey, keySz);
if (ret == 0)
keyAlgo = WC_PK_TYPE_ECDH;
wc_ecc_free(&eccKey);
wc_ecc_free(eccKey);
}
WC_FREE_VAR_EX(eccKey, heap, DYNAMIC_TYPE_ECC);
}
#endif
#if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
if (keyAlgo == WC_PK_TYPE_NONE) {
word32 idx = 0;
DhKey dhKey;
ret = wc_InitDhKey_ex(&dhKey, heap, INVALID_DEVID);
WC_DECLARE_VAR(dhKey, DhKey, 1, heap);
WC_ALLOC_VAR_EX(dhKey, DhKey, 1, heap, DYNAMIC_TYPE_DH,
ret = MEMORY_E);
if (ret == 0)
ret = wc_InitDhKey_ex(dhKey, heap, INVALID_DEVID);
if (ret == 0) {
ret = wc_DhKeyDecode(keyBuf, &idx, &dhKey, keySz);
ret = wc_DhKeyDecode(keyBuf, &idx, dhKey, keySz);
if (ret == 0)
keyAlgo = WC_PK_TYPE_DH;
wc_FreeDhKey(&dhKey);
wc_FreeDhKey(dhKey);
}
WC_FREE_VAR_EX(dhKey, heap, DYNAMIC_TYPE_DH);
}
#endif
#ifdef HAVE_CURVE25519
if (keyAlgo == WC_PK_TYPE_NONE) {
word32 idx = 0;
curve25519_key x25519Key;
ret = wc_curve25519_init_ex(&x25519Key, heap, INVALID_DEVID);
WC_DECLARE_VAR(x25519Key, curve25519_key, 1, heap);
WC_ALLOC_VAR_EX(x25519Key, curve25519_key, 1, heap,
DYNAMIC_TYPE_CURVE25519, ret = MEMORY_E);
if (ret == 0)
ret = wc_curve25519_init_ex(x25519Key, heap, INVALID_DEVID);
if (ret == 0) {
ret = wc_Curve25519PrivateKeyDecode(keyBuf, &idx,
&x25519Key, keySz);
x25519Key, keySz);
if (ret == 0)
keyAlgo = WC_PK_TYPE_CURVE25519;
wc_curve25519_free(&x25519Key);
wc_curve25519_free(x25519Key);
}
WC_FREE_VAR_EX(x25519Key, heap, DYNAMIC_TYPE_CURVE25519);
}
#endif
#ifdef HAVE_CURVE448
if (keyAlgo == WC_PK_TYPE_NONE) {
word32 idx = 0;
curve448_key x448Key;
ret = wc_curve448_init(&x448Key);
WC_DECLARE_VAR(x448Key, curve448_key, 1, heap);
WC_ALLOC_VAR_EX(x448Key, curve448_key, 1, heap,
DYNAMIC_TYPE_CURVE448, ret = MEMORY_E);
if (ret == 0)
ret = wc_curve448_init(x448Key);
if (ret == 0) {
ret = wc_Curve448PrivateKeyDecode(keyBuf, &idx, &x448Key,
ret = wc_Curve448PrivateKeyDecode(keyBuf, &idx, x448Key,
keySz);
if (ret == 0)
keyAlgo = WC_PK_TYPE_CURVE448;
wc_curve448_free(&x448Key);
wc_curve448_free(x448Key);
}
WC_FREE_VAR_EX(x448Key, heap, DYNAMIC_TYPE_CURVE448);
}
#endif

Expand Down
Loading
Loading