1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-22 19:08:00 +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 {
private val cache: MutableMap<Long, Passphrase>
private val cache: MutableMap<Long?, Passphrase>
private val protector: SecretKeyRingProtector
private val provider: SecretKeyPassphraseProvider?
@ -152,12 +152,12 @@ class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphras
*/
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]
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)

View file

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

View file

@ -14,9 +14,9 @@ import org.pgpainless.util.Passphrase
*
* 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
* @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]. */
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
SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(long keyId) {
public Passphrase getPassphraseFor(Long keyId) {
// is called in interactive mode
return Passphrase.fromPassword(passphrase);
}
@Override
public boolean hasPassphrase(long keyId) {
public boolean hasPassphrase(Long keyId) {
return true;
}
};
@ -95,13 +95,13 @@ public class MissingPassphraseForDecryptionTest {
SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(long keyId) {
public Passphrase getPassphraseFor(Long keyId) {
fail("MUST NOT get called in non-interactive mode.");
return null;
}
@Override
public boolean hasPassphrase(long keyId) {
public boolean hasPassphrase(Long keyId) {
return true;
}
};

View file

@ -120,13 +120,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
public void missingPassphraseFirst() throws PGPException, IOException {
SecretKeyRingProtector protector1 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() {
@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.");
return null;
}
@Override
public boolean hasPassphrase(long keyId) {
public boolean hasPassphrase(Long keyId) {
return false;
}
});
@ -150,13 +150,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
SecretKeyRingProtector protector1 = SecretKeyRingProtector.unlockEachKeyWith(p1, k1);
SecretKeyRingProtector protector2 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() {
@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.");
return null;
}
@Override
public boolean hasPassphrase(long keyId) {
public boolean hasPassphrase(Long keyId) {
return false;
}
});
@ -178,13 +178,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
public void messagePassphraseFirst() throws PGPException, IOException {
SecretKeyPassphraseProvider provider = new SecretKeyPassphraseProvider() {
@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.");
return null;
}
@Override
public boolean hasPassphrase(long keyId) {
public boolean hasPassphrase(Long keyId) {
return false;
}
};

View file

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

View file

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

View file

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