KeyCrypterException: Introduce dedicated PublicPrivateMismatch exception for when a private key or seed is decrypted, it doesn't match its public key.

This commit is contained in:
Andreas Schildbach
2019-03-31 12:05:26 +02:00
parent c572bb52d7
commit 38323538b4
3 changed files with 21 additions and 7 deletions

View File

@@ -377,7 +377,7 @@ public class DeterministicKey extends ECKey {
BigInteger privKey = findOrDeriveEncryptedPrivateKey(keyCrypter, aesKey); BigInteger privKey = findOrDeriveEncryptedPrivateKey(keyCrypter, aesKey);
DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent); DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent);
if (!Arrays.equals(key.getPubKey(), getPubKey())) if (!Arrays.equals(key.getPubKey(), getPubKey()))
throw new KeyCrypterException("Provided AES key is wrong"); throw new KeyCrypterException.PublicPrivateMismatch("Provided AES key is wrong");
if (parent == null) if (parent == null)
key.setCreationTimeSeconds(getCreationTimeSeconds()); key.setCreationTimeSeconds(getCreationTimeSeconds());
return key; return key;
@@ -436,7 +436,7 @@ public class DeterministicKey extends ECKey {
// If it's not, it means we tried decrypting with an invalid password and earlier checks e.g. for padding didn't // If it's not, it means we tried decrypting with an invalid password and earlier checks e.g. for padding didn't
// catch it. // catch it.
if (!downCursor.pub.equals(pub)) if (!downCursor.pub.equals(pub))
throw new KeyCrypterException("Could not decrypt bytes"); throw new KeyCrypterException.PublicPrivateMismatch("Could not decrypt bytes");
return checkNotNull(downCursor.priv); return checkNotNull(downCursor.priv);
} }

View File

@@ -28,11 +28,25 @@ package org.bitcoinj.crypto;
public class KeyCrypterException extends RuntimeException { public class KeyCrypterException extends RuntimeException {
private static final long serialVersionUID = -4441989608332681377L; private static final long serialVersionUID = -4441989608332681377L;
public KeyCrypterException(String s) { public KeyCrypterException(String message) {
super(s); super(message);
} }
public KeyCrypterException(String s, Throwable throwable) { public KeyCrypterException(String message, Throwable throwable) {
super(s, throwable); super(message, throwable);
}
/**
* This exception is thrown when a private key or seed is decrypted, it doesn't match its public key any
* more. This likely means the wrong decryption key has been used.
*/
public static class PublicPrivateMismatch extends KeyCrypterException {
public PublicPrivateMismatch(String message) {
super(message);
}
public PublicPrivateMismatch(String message, Throwable throwable) {
super(message, throwable);
}
} }
} }

View File

@@ -1014,7 +1014,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
DeterministicKeyChain chain = makeKeyChainFromSeed(decSeed, getAccountPath(), outputScriptType); DeterministicKeyChain chain = makeKeyChainFromSeed(decSeed, getAccountPath(), outputScriptType);
// Now double check that the keys match to catch the case where the key is wrong but padding didn't catch it. // Now double check that the keys match to catch the case where the key is wrong but padding didn't catch it.
if (!chain.getWatchingKey().getPubKeyPoint().equals(getWatchingKey().getPubKeyPoint())) if (!chain.getWatchingKey().getPubKeyPoint().equals(getWatchingKey().getPubKeyPoint()))
throw new KeyCrypterException("Provided AES key is wrong"); throw new KeyCrypterException.PublicPrivateMismatch("Provided AES key is wrong");
chain.lookaheadSize = lookaheadSize; chain.lookaheadSize = lookaheadSize;
// Now copy the (pubkey only) leaf keys across to avoid rederiving them. The private key bytes are missing // Now copy the (pubkey only) leaf keys across to avoid rederiving them. The private key bytes are missing
// anyway so there's nothing to decrypt. // anyway so there's nothing to decrypt.