1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-27 06:42:05 +01:00

In PasswordBasedSecretKeyRingProtector.forKey(ring, passphrase): Return passphrase also for subkeys

Fixes #97, thanks @DenBond7
This commit is contained in:
Paul Schaub 2021-03-18 21:28:08 +01:00
parent 7bd12fe5d4
commit 8c97b6ead1
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 24 additions and 2 deletions

View file

@ -15,11 +15,13 @@
*/ */
package org.pgpainless.key.protection; package org.pgpainless.key.protection;
import java.util.Iterator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyRing; import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
@ -55,9 +57,12 @@ public class PasswordBasedSecretKeyRingProtector implements SecretKeyRingProtect
@Override @Override
@Nullable @Nullable
public Passphrase getPassphraseFor(Long keyId) { public Passphrase getPassphraseFor(Long keyId) {
if (keyRing.getPublicKey().getKeyID() == keyId) { for (Iterator<PGPPublicKey> it = keyRing.getPublicKeys(); it.hasNext(); ) {
PGPPublicKey key = it.next();
if (key.getKeyID() == keyId) {
return passphrase; return passphrase;
} }
}
return null; return null;
} }
}; };

View file

@ -18,10 +18,16 @@ package org.pgpainless.key.protection;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider; import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
@ -56,4 +62,15 @@ public class PassphraseProtectedKeyTest {
assertNull(protector.getEncryptor(TestKeys.JULIET_KEY_ID)); assertNull(protector.getEncryptor(TestKeys.JULIET_KEY_ID));
assertNull(protector.getDecryptor(TestKeys.JULIET_KEY_ID)); assertNull(protector.getDecryptor(TestKeys.JULIET_KEY_ID));
} }
@Test
public void testReturnsNonNullDecryptorForSubkeys() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("alice", "passphrase");
SecretKeyRingProtector protector = PasswordBasedSecretKeyRingProtector.forKey(secretKeys, Passphrase.fromPassword("passphrase"));
for (Iterator<PGPPublicKey> it = secretKeys.getPublicKeys(); it.hasNext(); ) {
PGPPublicKey subkey = it.next();
assertNotNull(protector.getEncryptor(subkey.getKeyID()));
assertNotNull(protector.getDecryptor(subkey.getKeyID()));
}
}
} }