Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(739) SignatureECDSAN destroying private key #740

Merged
merged 1 commit into from
Jan 8, 2025
Merged
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
11 changes: 9 additions & 2 deletions src/main/java/com/jcraft/jsch/KeyPairECDSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,16 @@ public int getKeySize() {

@Override
public byte[] getSignature(byte[] data) {
byte[] keyCopy = null;
try {
Class<? extends SignatureECDSA> c =
Class.forName(JSch.getConfig("ecdsa-sha2-" + Util.byte2str(name)))
.asSubclass(SignatureECDSA.class);
SignatureECDSA ecdsa = c.getDeclaredConstructor().newInstance();
ecdsa.init();
ecdsa.setPrvKey(prv_array);
// https://github.com/mwiede/jsch/issues/739 : prv_array could be destroyed by ecdsa signing
keyCopy = Arrays.copyOf(prv_array, prv_array.length);
ecdsa.setPrvKey(keyCopy);

ecdsa.update(data);
byte[] sig = ecdsa.sign();
Expand All @@ -364,6 +367,8 @@ public byte[] getSignature(byte[] data) {
if (instLogger.getLogger().isEnabled(Logger.ERROR)) {
instLogger.getLogger().log(Logger.ERROR, "failed to generate signature", e);
}
} finally {
Util.bzero(keyCopy);
}
return null;
}
Expand All @@ -390,7 +395,9 @@ public Signature getVerifier() {
r_array = tmp[0];
s_array = tmp[1];
}
ecdsa.setPubKey(r_array, s_array);
// https://github.com/mwiede/jsch/issues/739 : keys could be destroyed by ecdsa verification
ecdsa.setPubKey(Arrays.copyOf(r_array, r_array.length),
Arrays.copyOf(s_array, s_array.length));
return ecdsa;
} catch (Exception e) {
if (instLogger.getLogger().isEnabled(Logger.ERROR)) {
Expand Down
Loading