From 64867cfb922ec7e68f12884d8152ee17b5c98f84 Mon Sep 17 00:00:00 2001 From: Yosuke Shimizu Date: Thu, 19 Mar 2026 09:54:58 +0900 Subject: [PATCH 1/4] Add wc_HashFree() to release the resource --- src/internal.c | 57 ++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/src/internal.c b/src/internal.c index d1d23d96e..0b3820ab0 100644 --- a/src/internal.c +++ b/src/internal.c @@ -14090,10 +14090,12 @@ static int BuildUserAuthRequestRsa(WOLFSSH* ssh, if (ret == WS_SUCCESS) { WMEMSET(digest, 0, sizeof(digest)); ret = wc_HashInit(&hash, hashId); - if (ret == WS_SUCCESS) + if (ret == WS_SUCCESS) { ret = HashUpdate(&hash, hashId, checkData, checkDataSz); - if (ret == WS_SUCCESS) - ret = wc_HashFinal(&hash, hashId, digest); + if (ret == WS_SUCCESS) + ret = wc_HashFinal(&hash, hashId, digest); + wc_HashFree(&hash, hashId); + } } if (ret == WS_SUCCESS) { @@ -14324,11 +14326,12 @@ static int BuildUserAuthRequestRsaCert(WOLFSSH* ssh, WMEMSET(digest, 0, sizeof(digest)); ret = wc_HashInit(&hash, hashId); - if (ret == WS_SUCCESS) + if (ret == WS_SUCCESS) { ret = HashUpdate(&hash, hashId, checkData, checkDataSz); - if (ret == WS_SUCCESS) - ret = wc_HashFinal(&hash, hashId, digest); - + if (ret == WS_SUCCESS) + ret = wc_HashFinal(&hash, hashId, digest); + wc_HashFree(&hash, hashId); + } if (ret == WS_SUCCESS) { c32toa(keySig->sigSz + 7 + LENGTH_SZ * 2, output + begin); begin += LENGTH_SZ; @@ -14547,16 +14550,18 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh, if (ret == WS_SUCCESS) { WLOG(WS_LOG_INFO, "Signing hash with ECDSA."); ret = wc_HashInit(&hash, hashId); - if (ret == WS_SUCCESS) + if (ret == WS_SUCCESS) { ret = HashUpdate(&hash, hashId, checkData, checkDataSz); - if (ret == WS_SUCCESS) - ret = wc_HashFinal(&hash, hashId, digest); - if (ret == WS_SUCCESS) - ret = wc_ecc_sign_hash(digest, digestSz, sig_ptr, &sigSz, - ssh->rng, &keySig->ks.ecc.key); - if (ret != WS_SUCCESS) { - WLOG(WS_LOG_DEBUG, "SUAR: Bad ECC Sign"); - ret = WS_ECC_E; + if (ret == WS_SUCCESS) + ret = wc_HashFinal(&hash, hashId, digest); + if (ret == WS_SUCCESS) + ret = wc_ecc_sign_hash(digest, digestSz, sig_ptr, &sigSz, + ssh->rng, &keySig->ks.ecc.key); + if (ret != WS_SUCCESS) { + WLOG(WS_LOG_DEBUG, "SUAR: Bad ECC Sign"); + ret = WS_ECC_E; + } + wc_HashFree(&hash, hashId); } } @@ -14787,16 +14792,18 @@ static int BuildUserAuthRequestEccCert(WOLFSSH* ssh, if (ret == WS_SUCCESS) { WLOG(WS_LOG_INFO, "Signing hash with ECDSA cert."); ret = wc_HashInit(&hash, hashId); - if (ret == WS_SUCCESS) + if (ret == WS_SUCCESS) { ret = HashUpdate(&hash, hashId, checkData, checkDataSz); - if (ret == WS_SUCCESS) - ret = wc_HashFinal(&hash, hashId, digest); - if (ret == WS_SUCCESS) - ret = wc_ecc_sign_hash(digest, digestSz, sig, &sigSz, - ssh->rng, &keySig->ks.ecc.key); - if (ret != WS_SUCCESS) { - WLOG(WS_LOG_DEBUG, "SUAR: Bad ECC Cert Sign"); - ret = WS_ECC_E; + if (ret == WS_SUCCESS) + ret = wc_HashFinal(&hash, hashId, digest); + if (ret == WS_SUCCESS) + ret = wc_ecc_sign_hash(digest, digestSz, sig, &sigSz, + ssh->rng, &keySig->ks.ecc.key); + if (ret != WS_SUCCESS) { + WLOG(WS_LOG_DEBUG, "SUAR: Bad ECC Cert Sign"); + ret = WS_ECC_E; + } + wc_HashFree(&hash, hashId); } } From 8e5f314e5256462c9796be0ca4925bb7b58faef2 Mon Sep 17 00:00:00 2001 From: Yosuke Shimizu Date: Thu, 19 Mar 2026 10:19:47 +0900 Subject: [PATCH 2/4] Add Ed25519 key clean up --- src/internal.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/internal.c b/src/internal.c index 0b3820ab0..599b0f662 100644 --- a/src/internal.c +++ b/src/internal.c @@ -5014,8 +5014,13 @@ static int ParseEd25519PubKey(WOLFSSH *ssh, if (ret == WS_SUCCESS) { ret = wc_ed25519_import_public(encA, encASz, &sigKeyBlock_ptr->sk.ed25519.key); - if (ret != 0) - ret = WS_ED25519_E; + } + + if (ret == 0) { + sigKeyBlock_ptr->keyAllocated = 1; + } + else { + ret = WS_ED25519_E; } return ret; } @@ -5328,6 +5333,11 @@ static void FreePubKey(struct wolfSSH_sigKeyBlock *p) wc_ecc_free(&p->sk.ecc.key); #endif } + else if (p->useEd25519) { + #ifndef WOLFSSH_NO_ED25519 + wc_ed25519_free(p->sk.ed25519.key); + #endif + } p->keyAllocated = 0; } } From 631a21983d53179c42ab8e79c54bde3b69e69d30 Mon Sep 17 00:00:00 2001 From: yosuke-wolfssl Date: Thu, 19 Mar 2026 10:38:28 +0900 Subject: [PATCH 3/4] fix the missing pointer Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/internal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal.c b/src/internal.c index 599b0f662..a49f27115 100644 --- a/src/internal.c +++ b/src/internal.c @@ -5335,7 +5335,7 @@ static void FreePubKey(struct wolfSSH_sigKeyBlock *p) } else if (p->useEd25519) { #ifndef WOLFSSH_NO_ED25519 - wc_ed25519_free(p->sk.ed25519.key); + wc_ed25519_free(&p->sk.ed25519.key); #endif } p->keyAllocated = 0; From 7bb982a5bb6c154a555ed5378459515e04ec98e0 Mon Sep 17 00:00:00 2001 From: Yosuke Shimizu Date: Thu, 19 Mar 2026 10:56:52 +0900 Subject: [PATCH 4/4] Revert the evaluation timing not to hide the other error causes --- src/internal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/internal.c b/src/internal.c index a49f27115..f59a4359f 100644 --- a/src/internal.c +++ b/src/internal.c @@ -5014,14 +5014,14 @@ static int ParseEd25519PubKey(WOLFSSH *ssh, if (ret == WS_SUCCESS) { ret = wc_ed25519_import_public(encA, encASz, &sigKeyBlock_ptr->sk.ed25519.key); + if (ret != 0) { + ret = WS_ED25519_E; + } } if (ret == 0) { sigKeyBlock_ptr->keyAllocated = 1; } - else { - ret = WS_ED25519_E; - } return ret; } #else