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
22 changes: 11 additions & 11 deletions src/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1892,23 +1892,23 @@ static int DoAsn1Key(const byte* in, word32 inSz, byte** out,
newKey = (byte*)WMALLOC(inSz, heap, DYNTYPE_PRIVKEY);
if (newKey == NULL) {
ret = WS_MEMORY_E;
return ret;
}
}
else if (*outSz < inSz) {
WLOG(WS_LOG_DEBUG, "DER private key output size too small");
ret = WS_BUFFER_E;
}
else {
if (*outSz < inSz) {
WLOG(WS_LOG_DEBUG, "DER private key output size too small");
ret = WS_BUFFER_E;
return ret;
}
newKey = *out;
}

*out = newKey;
*outSz = inSz;
WMEMCPY(newKey, in, inSz);
*outType = (const byte*)IdToName(ret);
*outTypeSz = (word32)WSTRLEN((const char*)*outType);
if (ret > 0) {
*out = newKey;
*outSz = inSz;
WMEMCPY(newKey, in, inSz);
*outType = (const byte*)IdToName(ret);
*outTypeSz = (word32)WSTRLEN((const char*)*outType);
}
}

wolfSSH_KEY_clean(key);
Expand Down
24 changes: 24 additions & 0 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,30 @@ static void test_wolfSSH_ReadKey(void)
WFREE(key, NULL, DYNTYPE_FILE);
WFREE(derKey, NULL, 0);

/* SSL DER Format, ssh-rsa, private, caller buffer too small.
* Exercises the DoAsn1Key error path that must not leak the key. */
derKey = NULL;
derKeySz = 0;
ret = ConvertHexToBin(serverKeyRsaDer, &derKey, &derKeySz,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
AssertIntEQ(ret, 0);
keyCheck = (byte*)WMALLOC(derKeySz, NULL, DYNTYPE_FILE);
AssertNotNull(keyCheck);
key = keyCheck;
keySz = derKeySz - 1;
keyType = NULL;
keyTypeSz = 0;
ret = wolfSSH_ReadKey_buffer(derKey, derKeySz, WOLFSSH_FORMAT_ASN1,
&key, &keySz, &keyType, &keyTypeSz, NULL);
AssertIntEQ(ret, WS_BUFFER_E);
AssertTrue(key == keyCheck);
/* output params left untouched on the error path */
AssertIntEQ(keySz, derKeySz - 1);
AssertNull(keyType);
AssertIntEQ(keyTypeSz, 0);
WFREE(keyCheck, NULL, DYNTYPE_FILE);
WFREE(derKey, NULL, 0);

/* OpenSSH Format, ssh-rsa, public, need alloc */
key = NULL;
keySz = 0;
Expand Down
Loading