store passphrase in memory as securestring

This commit is contained in:
Craig Raw 2020-08-06 10:28:20 +02:00
parent eb07a7ffa3
commit 1f7be6c7d5

View file

@ -18,7 +18,7 @@ public class DeterministicSeed implements EncryptableItem {
private long creationTimeSeconds; private long creationTimeSeconds;
//Session only storage //Session only storage
private transient String passphrase; private transient SecureString passphrase;
public DeterministicSeed(CharSequence mnemonicString, String passphrase, long creationTimeSeconds, Type type) { public DeterministicSeed(CharSequence mnemonicString, String passphrase, long creationTimeSeconds, Type type) {
this(decodeMnemonicCode(mnemonicString), passphrase, creationTimeSeconds, type); this(decodeMnemonicCode(mnemonicString), passphrase, creationTimeSeconds, type);
@ -26,7 +26,7 @@ public class DeterministicSeed implements EncryptableItem {
public DeterministicSeed(List<String> mnemonic, String passphrase, long creationTimeSeconds, Type type) { public DeterministicSeed(List<String> mnemonic, String passphrase, long creationTimeSeconds, Type type) {
this(mnemonic, needsPassphrase(passphrase), creationTimeSeconds, type); this(mnemonic, needsPassphrase(passphrase), creationTimeSeconds, type);
this.passphrase = passphrase; this.passphrase = (passphrase == null ? null : new SecureString(passphrase));
} }
public DeterministicSeed(List<String> mnemonic, boolean needsPassphrase, long creationTimeSeconds, Type type) { public DeterministicSeed(List<String> mnemonic, boolean needsPassphrase, long creationTimeSeconds, Type type) {
@ -84,11 +84,12 @@ public class DeterministicSeed implements EncryptableItem {
} }
this.encryptedMnemonicCode = null; this.encryptedMnemonicCode = null;
this.needsPassphrase = needsPassphrase(passphrase); this.needsPassphrase = needsPassphrase(passphrase);
this.passphrase = new SecureString(passphrase);
this.creationTimeSeconds = creationTimeSeconds; this.creationTimeSeconds = creationTimeSeconds;
this.type = Type.BIP39; this.type = Type.BIP39;
} }
public static boolean needsPassphrase(String passphrase) { private static boolean needsPassphrase(String passphrase) {
return passphrase != null && !passphrase.isEmpty(); return passphrase != null && !passphrase.isEmpty();
} }
@ -96,14 +97,18 @@ public class DeterministicSeed implements EncryptableItem {
return needsPassphrase; return needsPassphrase;
} }
public String getPassphrase() { public SecureString getPassphrase() {
return passphrase; return passphrase;
} }
public void setPassphrase(String passphrase) { public void setPassphrase(SecureString passphrase) {
this.passphrase = passphrase; this.passphrase = passphrase;
} }
public void setPassphrase(String passphrase) {
this.passphrase = new SecureString(passphrase);
}
private static byte[] getEntropy(SecureRandom random, int bits) { private static byte[] getEntropy(SecureRandom random, int bits) {
if(bits > MAX_SEED_ENTROPY_BITS) { if(bits > MAX_SEED_ENTROPY_BITS) {
throw new IllegalArgumentException("Requested entropy size too large"); throw new IllegalArgumentException("Requested entropy size too large");
@ -139,7 +144,7 @@ public class DeterministicSeed implements EncryptableItem {
throw new MnemonicException("Passphrase required but not provided"); throw new MnemonicException("Passphrase required but not provided");
} }
return type.toSeed(mnemonicCode, passphrase); return type.toSeed(mnemonicCode, passphrase == null ? null : passphrase.asString());
} }
@Override @Override
@ -266,7 +271,7 @@ public class DeterministicSeed implements EncryptableItem {
mnemonicCode.clear(); mnemonicCode.clear();
} }
if(passphrase != null) { if(passphrase != null) {
passphrase = ""; passphrase = new SecureString("");
} }
} }