1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-19 10:52:05 +01:00

Revert PassphraseProvider API change

This commit is contained in:
Paul Schaub 2023-11-15 19:23:52 +01:00
parent cf638da130
commit 481dfac455
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
10 changed files with 31 additions and 31 deletions

View file

@ -21,7 +21,7 @@ import org.pgpainless.util.Passphrase
*/ */
class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphraseProvider { class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphraseProvider {
private val cache: MutableMap<Long, Passphrase> private val cache: MutableMap<Long?, Passphrase>
private val protector: SecretKeyRingProtector private val protector: SecretKeyRingProtector
private val provider: SecretKeyPassphraseProvider? private val provider: SecretKeyPassphraseProvider?
@ -152,12 +152,12 @@ class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphras
*/ */
fun forgetPassphrase(key: PGPPublicKey) = apply { forgetPassphrase(key.keyID) } fun forgetPassphrase(key: PGPPublicKey) = apply { forgetPassphrase(key.keyID) }
override fun getPassphraseFor(keyId: Long): Passphrase? { override fun getPassphraseFor(keyId: Long?): Passphrase? {
return if (hasPassphrase(keyId)) cache[keyId] return if (hasPassphrase(keyId)) cache[keyId]
else provider?.getPassphraseFor(keyId)?.also { cache[keyId] = it } else provider?.getPassphraseFor(keyId)?.also { cache[keyId] = it }
} }
override fun hasPassphrase(keyId: Long) = cache[keyId]?.isValid ?: false override fun hasPassphrase(keyId: Long?) = cache[keyId]?.isValid ?: false
override fun hasPassphraseFor(keyId: Long) = hasPassphrase(keyId) override fun hasPassphraseFor(keyId: Long) = hasPassphrase(keyId)

View file

@ -38,12 +38,12 @@ class PasswordBasedSecretKeyRingProtector : BaseSecretKeyRingProtector {
): PasswordBasedSecretKeyRingProtector { ): PasswordBasedSecretKeyRingProtector {
return object : SecretKeyPassphraseProvider { return object : SecretKeyPassphraseProvider {
override fun getPassphraseFor(keyId: Long): Passphrase? { override fun getPassphraseFor(keyId: Long?): Passphrase? {
return if (hasPassphrase(keyId)) passphrase else null return if (hasPassphrase(keyId)) passphrase else null
} }
override fun hasPassphrase(keyId: Long): Boolean { override fun hasPassphrase(keyId: Long?): Boolean {
return keyRing.getPublicKey(keyId) != null return keyId != null && keyRing.getPublicKey(keyId) != null
} }
} }
.let { PasswordBasedSecretKeyRingProtector(it) } .let { PasswordBasedSecretKeyRingProtector(it) }
@ -59,11 +59,11 @@ class PasswordBasedSecretKeyRingProtector : BaseSecretKeyRingProtector {
passphrase: Passphrase passphrase: Passphrase
): PasswordBasedSecretKeyRingProtector { ): PasswordBasedSecretKeyRingProtector {
return object : SecretKeyPassphraseProvider { return object : SecretKeyPassphraseProvider {
override fun getPassphraseFor(keyId: Long): Passphrase? { override fun getPassphraseFor(keyId: Long?): Passphrase? {
return if (hasPassphrase(keyId)) passphrase else null return if (hasPassphrase(keyId)) passphrase else null
} }
override fun hasPassphrase(keyId: Long): Boolean { override fun hasPassphrase(keyId: Long?): Boolean {
return keyId == singleKeyId return keyId == singleKeyId
} }
} }

View file

@ -14,9 +14,9 @@ import org.pgpainless.util.Passphrase
* *
* TODO: Make this null-safe and throw an exception instead? * TODO: Make this null-safe and throw an exception instead?
*/ */
class MapBasedPassphraseProvider(val map: Map<Long, Passphrase>) : SecretKeyPassphraseProvider { class MapBasedPassphraseProvider(val map: Map<Long?, Passphrase>) : SecretKeyPassphraseProvider {
override fun getPassphraseFor(keyId: Long): Passphrase? = map[keyId] override fun getPassphraseFor(keyId: Long?): Passphrase? = map[keyId]
override fun hasPassphrase(keyId: Long): Boolean = map.containsKey(keyId) override fun hasPassphrase(keyId: Long?): Boolean = map.containsKey(keyId)
} }

View file

@ -30,7 +30,7 @@ interface SecretKeyPassphraseProvider {
* @param keyId if of the secret key * @param keyId if of the secret key
* @return passphrase or null, if no passphrase record has been found. * @return passphrase or null, if no passphrase record has been found.
*/ */
fun getPassphraseFor(keyId: Long): Passphrase? fun getPassphraseFor(keyId: Long?): Passphrase?
fun hasPassphrase(keyId: Long): Boolean fun hasPassphrase(keyId: Long?): Boolean
} }

View file

@ -9,7 +9,7 @@ import org.pgpainless.util.Passphrase
/** Implementation of the [SecretKeyPassphraseProvider] that holds a single [Passphrase]. */ /** Implementation of the [SecretKeyPassphraseProvider] that holds a single [Passphrase]. */
class SolitaryPassphraseProvider(val passphrase: Passphrase?) : SecretKeyPassphraseProvider { class SolitaryPassphraseProvider(val passphrase: Passphrase?) : SecretKeyPassphraseProvider {
override fun getPassphraseFor(keyId: Long): Passphrase? = passphrase override fun getPassphraseFor(keyId: Long?): Passphrase? = passphrase
override fun hasPassphrase(keyId: Long): Boolean = true override fun hasPassphrase(keyId: Long?): Boolean = true
} }

View file

@ -63,13 +63,13 @@ public class MissingPassphraseForDecryptionTest {
// interactive callback // interactive callback
SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() { SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
// is called in interactive mode // is called in interactive mode
return Passphrase.fromPassword(passphrase); return Passphrase.fromPassword(passphrase);
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return true; return true;
} }
}; };
@ -95,13 +95,13 @@ public class MissingPassphraseForDecryptionTest {
SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() { SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
fail("MUST NOT get called in non-interactive mode."); fail("MUST NOT get called in non-interactive mode.");
return null; return null;
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return true; return true;
} }
}; };

View file

@ -120,13 +120,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
public void missingPassphraseFirst() throws PGPException, IOException { public void missingPassphraseFirst() throws PGPException, IOException {
SecretKeyRingProtector protector1 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() { SecretKeyRingProtector protector1 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
fail("Although the first PKESK is for k1, we should have skipped it and tried k2 first, which has passphrase available."); fail("Although the first PKESK is for k1, we should have skipped it and tried k2 first, which has passphrase available.");
return null; return null;
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return false; return false;
} }
}); });
@ -150,13 +150,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
SecretKeyRingProtector protector1 = SecretKeyRingProtector.unlockEachKeyWith(p1, k1); SecretKeyRingProtector protector1 = SecretKeyRingProtector.unlockEachKeyWith(p1, k1);
SecretKeyRingProtector protector2 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() { SecretKeyRingProtector protector2 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
fail("This callback should not get called, since the first PKESK is for k1, which has a passphrase available."); fail("This callback should not get called, since the first PKESK is for k1, which has a passphrase available.");
return null; return null;
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return false; return false;
} }
}); });
@ -178,13 +178,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
public void messagePassphraseFirst() throws PGPException, IOException { public void messagePassphraseFirst() throws PGPException, IOException {
SecretKeyPassphraseProvider provider = new SecretKeyPassphraseProvider() { SecretKeyPassphraseProvider provider = new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
fail("Since we provide a decryption passphrase, we should not try to decrypt any key."); fail("Since we provide a decryption passphrase, we should not try to decrypt any key.");
return null; return null;
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return false; return false;
} }
}; };

View file

@ -32,13 +32,13 @@ public class CachingSecretKeyRingProtectorTest {
// Dummy passphrase callback that returns the doubled key-id as passphrase // Dummy passphrase callback that returns the doubled key-id as passphrase
private final SecretKeyPassphraseProvider dummyCallback = new SecretKeyPassphraseProvider() { private final SecretKeyPassphraseProvider dummyCallback = new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
long doubled = keyId * 2; long doubled = keyId * 2;
return Passphrase.fromPassword(Long.toString(doubled)); return Passphrase.fromPassword(Long.toString(doubled));
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return true; return true;
} }
}; };

View file

@ -31,7 +31,7 @@ public class PassphraseProtectedKeyTest {
new SecretKeyPassphraseProvider() { new SecretKeyPassphraseProvider() {
@Nullable @Nullable
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
if (keyId == TestKeys.CRYPTIE_KEY_ID) { if (keyId == TestKeys.CRYPTIE_KEY_ID) {
return new Passphrase(TestKeys.CRYPTIE_PASSWORD.toCharArray()); return new Passphrase(TestKeys.CRYPTIE_PASSWORD.toCharArray());
} else { } else {
@ -40,7 +40,7 @@ public class PassphraseProtectedKeyTest {
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return keyId == TestKeys.CRYPTIE_KEY_ID; return keyId == TestKeys.CRYPTIE_KEY_ID;
} }
}); });

View file

@ -108,12 +108,12 @@ public class SecretKeyRingProtectorTest {
CachingSecretKeyRingProtector protector = new CachingSecretKeyRingProtector(passphraseMap, CachingSecretKeyRingProtector protector = new CachingSecretKeyRingProtector(passphraseMap,
KeyRingProtectionSettings.secureDefaultSettings(), new SecretKeyPassphraseProvider() { KeyRingProtectionSettings.secureDefaultSettings(), new SecretKeyPassphraseProvider() {
@Override @Override
public Passphrase getPassphraseFor(long keyId) { public Passphrase getPassphraseFor(Long keyId) {
return Passphrase.fromPassword("missingP455w0rd"); return Passphrase.fromPassword("missingP455w0rd");
} }
@Override @Override
public boolean hasPassphrase(long keyId) { public boolean hasPassphrase(Long keyId) {
return true; return true;
} }
}); });