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 686a10ccd2
commit e3c98daa64
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);
DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent);
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)
key.setCreationTimeSeconds(getCreationTimeSeconds());
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
// catch it.
if (!downCursor.pub.equals(pub))
throw new KeyCrypterException("Could not decrypt bytes");
throw new KeyCrypterException.PublicPrivateMismatch("Could not decrypt bytes");
return checkNotNull(downCursor.priv);
}

View file

@ -28,11 +28,25 @@ package org.bitcoinj.crypto;
public class KeyCrypterException extends RuntimeException {
private static final long serialVersionUID = -4441989608332681377L;
public KeyCrypterException(String s) {
super(s);
public KeyCrypterException(String message) {
super(message);
}
public KeyCrypterException(String s, Throwable throwable) {
super(s, throwable);
public KeyCrypterException(String message, Throwable 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);
// 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()))
throw new KeyCrypterException("Provided AES key is wrong");
throw new KeyCrypterException.PublicPrivateMismatch("Provided AES key is wrong");
chain.lookaheadSize = lookaheadSize;
// 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.