Skip to content

Commit 7bde5c1

Browse files
Clear passphrase bytes after use (#609)
Mimics the behavior of `decrypt()` in `PKCS5KeyFile.java`.
1 parent 3c85b86 commit 7bde5c1

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

src/main/java/com/hierynomus/sshj/userauth/keyprovider/OpenSSHKeyV1KeyFile.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ private void initializeCipher(String kdfName, byte[] kdfOptions, Cipher cipher)
143143
CharBuffer charBuffer = CharBuffer.wrap(pwdf.reqPassword(null));
144144
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
145145
passphrase = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
146+
Arrays.fill(charBuffer.array(), '\u0000');
147+
Arrays.fill(byteBuffer.array(), (byte) 0);
146148
}
147149
byte[] keyiv = new byte[48];
148150
new BCrypt().pbkdf(passphrase, opts.readBytes(), opts.readUInt32AsInt(), keyiv);
151+
Arrays.fill(passphrase, (byte) 0);
149152
byte[] key = Arrays.copyOfRange(keyiv, 0, 32);
150153
byte[] iv = Arrays.copyOfRange(keyiv, 32, 48);
151154
cipher.init(Cipher.Mode.Decrypt, key, iv);

src/test/java/net/schmizz/sshj/keyprovider/OpenSSHKeyFileTest.java

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
import java.text.DateFormat;
4848
import java.text.ParseException;
4949
import java.text.SimpleDateFormat;
50-
import java.util.Arrays;
51-
import java.util.Date;
52-
import java.util.Map;
53-
import java.util.Scanner;
50+
import java.util.*;
5451

5552
import static org.hamcrest.CoreMatchers.containsString;
5653
import static org.hamcrest.CoreMatchers.equalTo;
@@ -70,6 +67,44 @@ public class OpenSSHKeyFileTest {
7067
final char[] correctPassphrase = "test_passphrase".toCharArray();
7168
final char[] incorrectPassphrase = new char[]{' '};
7269

70+
private static class WipeTrackingPasswordFinder implements PasswordFinder {
71+
private int reqCounter = 0;
72+
73+
final private String password;
74+
final private boolean withRetry;
75+
final private ArrayList<char[]> toWipe = new ArrayList<>();
76+
77+
WipeTrackingPasswordFinder(String password, Boolean withRetry) {
78+
this.password = password;
79+
this.withRetry = withRetry;
80+
}
81+
82+
@Override
83+
public char[] reqPassword(Resource<?> resource) {
84+
char[] passwordChars;
85+
if (withRetry && reqCounter < 3) {
86+
reqCounter++;
87+
// Return an incorrect password three times before returning the correct one.
88+
passwordChars = (password + "incorrect").toCharArray();
89+
} else {
90+
passwordChars = password.toCharArray();
91+
}
92+
toWipe.add(passwordChars);
93+
return passwordChars;
94+
}
95+
96+
@Override
97+
public boolean shouldRetry(Resource<?> resource) {
98+
return withRetry && reqCounter <= 3;
99+
}
100+
101+
public void assertWiped() {
102+
for (char[] passwordChars : toWipe) {
103+
assertArrayEquals(new char[passwordChars.length], passwordChars);
104+
}
105+
}
106+
};
107+
73108
final PasswordFinder onlyGivesWhenReady = new PasswordFinder() {
74109
@Override
75110
public char[] reqPassword(Resource resource) {
@@ -249,27 +284,11 @@ public void shouldLoadECDSAPrivateKeyAsOpenSSHV1() throws IOException {
249284

250285
private void checkOpenSSHKeyV1(String key, final String password, boolean withRetry) throws IOException {
251286
OpenSSHKeyV1KeyFile keyFile = new OpenSSHKeyV1KeyFile();
252-
keyFile.init(new File(key), new PasswordFinder() {
253-
private int reqCounter = 0;
254-
255-
@Override
256-
public char[] reqPassword(Resource<?> resource) {
257-
if (withRetry && reqCounter < 3) {
258-
reqCounter++;
259-
// Return an incorrect password three times before returning the correct one.
260-
return (password + "incorrect").toCharArray();
261-
} else {
262-
return password.toCharArray();
263-
}
264-
}
265-
266-
@Override
267-
public boolean shouldRetry(Resource<?> resource) {
268-
return withRetry && reqCounter <= 3;
269-
}
270-
});
287+
WipeTrackingPasswordFinder pwf = new WipeTrackingPasswordFinder(password, withRetry);
288+
keyFile.init(new File(key), pwf);
271289
PrivateKey aPrivate = keyFile.getPrivate();
272290
assertThat(aPrivate.getAlgorithm(), equalTo("EdDSA"));
291+
pwf.assertWiped();
273292
}
274293

275294
@Test

0 commit comments

Comments
 (0)