Skip to content

Commit f424d7a

Browse files
committed
Unchecked wc_InitRsaKey Return Value
Check the return value of `wc_InitRsaKey()`. It will initialize the structure provided the pointer is non-null. Since the key is on the stack, the later call to `wc_FreeRsaKey()` will succeed as well. Modified the check for the encoded signature size inside the block where it is set; that check also updates the return value. Affected function: SignHashRsa. Issue: F-212
1 parent b0e424e commit f424d7a

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

src/agent.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -681,24 +681,25 @@ static int SignHashRsa(WOLFSSH_AGENT_KEY_RSA* rawKey, enum wc_HashType hashType,
681681
{
682682
RsaKey key;
683683
byte encSig[MAX_ENCODED_SIG_SZ];
684-
int encSigSz;
685-
int ret = 0;
686-
687-
wc_InitRsaKey(&key, heap);
688-
mp_read_unsigned_bin(&key.n, rawKey->n, rawKey->nSz);
689-
mp_read_unsigned_bin(&key.e, rawKey->e, rawKey->eSz);
690-
mp_read_unsigned_bin(&key.d, rawKey->d, rawKey->dSz);
691-
mp_read_unsigned_bin(&key.p, rawKey->p, rawKey->pSz);
692-
mp_read_unsigned_bin(&key.q, rawKey->q, rawKey->qSz);
693-
mp_read_unsigned_bin(&key.u, rawKey->iqmp, rawKey->iqmpSz);
694-
695-
encSigSz = wc_EncodeSignature(encSig, digest, digestSz,
696-
wc_HashGetOID(hashType));
697-
if (encSigSz <= 0) {
698-
WLOG(WS_LOG_DEBUG, "Bad Encode Sig");
699-
ret = WS_CRYPTO_FAILED;
684+
int encSigSz, ret;
685+
686+
ret = wc_InitRsaKey(&key, heap);
687+
if (ret == 0) {
688+
mp_read_unsigned_bin(&key.n, rawKey->n, rawKey->nSz);
689+
mp_read_unsigned_bin(&key.e, rawKey->e, rawKey->eSz);
690+
mp_read_unsigned_bin(&key.d, rawKey->d, rawKey->dSz);
691+
mp_read_unsigned_bin(&key.p, rawKey->p, rawKey->pSz);
692+
mp_read_unsigned_bin(&key.q, rawKey->q, rawKey->qSz);
693+
mp_read_unsigned_bin(&key.u, rawKey->iqmp, rawKey->iqmpSz);
694+
695+
encSigSz = wc_EncodeSignature(encSig, digest, digestSz,
696+
wc_HashGetOID(hashType));
697+
if (encSigSz <= 0) {
698+
WLOG(WS_LOG_DEBUG, "Bad Encode Sig");
699+
ret = WS_CRYPTO_FAILED;
700+
}
700701
}
701-
else {
702+
if (ret == 0) {
702703
WLOG(WS_LOG_INFO, "Signing hash with RSA.");
703704
*sigSz = wc_RsaSSL_Sign(encSig, encSigSz, sig, *sigSz, &key, rng);
704705
if (*sigSz <= 0) {

0 commit comments

Comments
 (0)