1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-04 19:45:59 +01:00

UnlockSecretKey: Do not try to get decryptor for unencrypted keys

This commit is contained in:
Paul Schaub 2021-05-31 15:38:47 +02:00
parent 73f6c54fa2
commit ce4869e15a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -20,6 +20,7 @@ import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.pgpainless.exception.WrongPassphraseException; import org.pgpainless.exception.WrongPassphraseException;
import org.pgpainless.key.info.KeyInfo;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
public class UnlockSecretKey { public class UnlockSecretKey {
@ -27,7 +28,10 @@ public class UnlockSecretKey {
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector) public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector)
throws WrongPassphraseException { throws WrongPassphraseException {
try { try {
PBESecretKeyDecryptor decryptor = protector.getDecryptor(secretKey.getKeyID()); PBESecretKeyDecryptor decryptor = null;
if (KeyInfo.isEncrypted(secretKey)) {
decryptor = protector.getDecryptor(secretKey.getKeyID());
}
return secretKey.extractPrivateKey(decryptor); return secretKey.extractPrivateKey(decryptor);
} catch (PGPException e) { } catch (PGPException e) {
throw new WrongPassphraseException(secretKey.getKeyID(), e); throw new WrongPassphraseException(secretKey.getKeyID(), e);