mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-22 19:08:00 +01:00
Kotlin conversion: SecretKeyPassphraseProvider and subclasses
This commit also adds a workaround to build.gradle which enables proper Java interop for Kotlin interfaces with default implementations
This commit is contained in:
parent
5cb6d6e41d
commit
e3f51fbf56
9 changed files with 84 additions and 131 deletions
|
@ -67,6 +67,13 @@ allprojects {
|
|||
fileMode = 0644
|
||||
}
|
||||
|
||||
// Compatibility of default implementations in kotlin interfaces with Java implementations.
|
||||
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs += ["-Xjvm-default=all-compatibility"]
|
||||
}
|
||||
}
|
||||
|
||||
project.ext {
|
||||
rootConfigDir = new File(rootDir, 'config')
|
||||
gitCommit = getGitCommit()
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Classes related to OpenPGP secret key password protection.
|
||||
*/
|
||||
package org.pgpainless.key.protection;
|
|
@ -1,42 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key.protection.passphrase_provider;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link SecretKeyPassphraseProvider} that holds a map of different {@link Passphrase passphrases}.
|
||||
* It will return the right passphrase depending on the key-id.
|
||||
*
|
||||
* Note: This provider might return null!
|
||||
* TODO: Make this null-safe and throw an exception instead?
|
||||
*/
|
||||
public class MapBasedPassphraseProvider implements SecretKeyPassphraseProvider {
|
||||
|
||||
private final Map<Long, Passphrase> map;
|
||||
|
||||
/**
|
||||
* Create a new map based passphrase provider.
|
||||
*
|
||||
* @param passphraseMap map of key-ids and passphrases
|
||||
*/
|
||||
public MapBasedPassphraseProvider(Map<Long, Passphrase> passphraseMap) {
|
||||
this.map = passphraseMap;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Passphrase getPassphraseFor(long keyId) {
|
||||
return map.get(keyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPassphrase(long keyId) {
|
||||
return map.containsKey(keyId);
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key.protection.passphrase_provider;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
/**
|
||||
* Interface to allow the user to provide a {@link Passphrase} for an encrypted OpenPGP secret key.
|
||||
*/
|
||||
public interface SecretKeyPassphraseProvider {
|
||||
|
||||
/**
|
||||
* Return a passphrase for the given secret key.
|
||||
* If no record is found, return null.
|
||||
* Note: In case of an unprotected secret key, this method must may not return null, but a {@link Passphrase} with
|
||||
* a content of null.
|
||||
*
|
||||
* @param secretKey secret key
|
||||
* @return passphrase or null, if no passphrase record is found.
|
||||
*/
|
||||
@Nullable default Passphrase getPassphraseFor(PGPSecretKey secretKey) {
|
||||
return getPassphraseFor(secretKey.getKeyID());
|
||||
}
|
||||
/**
|
||||
* Return a passphrase for the given key. If no record has been found, return null.
|
||||
* Note: In case of an unprotected secret key, this method must may not return null, but a {@link Passphrase} with
|
||||
* a content of null.
|
||||
*
|
||||
* @param keyId if of the secret key
|
||||
* @return passphrase or null, if no passphrase record has been found.
|
||||
*/
|
||||
@Nullable Passphrase getPassphraseFor(long keyId);
|
||||
|
||||
boolean hasPassphrase(long keyId);
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key.protection.passphrase_provider;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link SecretKeyPassphraseProvider} that holds a single {@link Passphrase}.
|
||||
*/
|
||||
public class SolitaryPassphraseProvider implements SecretKeyPassphraseProvider {
|
||||
|
||||
private final Passphrase passphrase;
|
||||
|
||||
public SolitaryPassphraseProvider(Passphrase passphrase) {
|
||||
this.passphrase = passphrase;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Passphrase getPassphraseFor(long keyId) {
|
||||
// always return the same passphrase.
|
||||
return passphrase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPassphrase(long keyId) {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Passphrase Provider classes.
|
||||
*/
|
||||
package org.pgpainless.key.protection.passphrase_provider;
|
|
@ -0,0 +1,21 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key.protection.passphrase_provider
|
||||
|
||||
import org.pgpainless.util.Passphrase
|
||||
|
||||
/**
|
||||
* Implementation of the [SecretKeyPassphraseProvider] that holds a map of key-IDs and respective [Passphrase].
|
||||
* It will return the right passphrase depending on the key-id.
|
||||
*
|
||||
* Note: This provider might return null!
|
||||
* TODO: Make this null-safe and throw an exception instead?
|
||||
*/
|
||||
class MapBasedPassphraseProvider(val map: Map<Long, Passphrase>) : SecretKeyPassphraseProvider {
|
||||
|
||||
override fun getPassphraseFor(keyId: Long): Passphrase? = map[keyId]
|
||||
|
||||
override fun hasPassphrase(keyId: Long): Boolean = map.containsKey(keyId)
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key.protection.passphrase_provider
|
||||
|
||||
import org.bouncycastle.openpgp.PGPSecretKey
|
||||
import org.pgpainless.util.Passphrase
|
||||
|
||||
/**
|
||||
* Interface to allow the user to provide a [Passphrase] for an encrypted OpenPGP secret key.
|
||||
*/
|
||||
interface SecretKeyPassphraseProvider {
|
||||
|
||||
/**
|
||||
* Return a passphrase for the given secret key.
|
||||
* If no record is found, return null.
|
||||
* Note: In case of an unprotected secret key, this method must may not return null, but a [Passphrase] with
|
||||
* a content of null.
|
||||
*
|
||||
* @param secretKey secret key
|
||||
* @return passphrase or null, if no passphrase record is found.
|
||||
*/
|
||||
fun getPassphraseFor(secretKey: PGPSecretKey): Passphrase? {
|
||||
return getPassphraseFor(secretKey.keyID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a passphrase for the given key. If no record has been found, return null.
|
||||
* Note: In case of an unprotected secret key, this method must may not return null, but a [Passphrase] with
|
||||
* a content of null.
|
||||
*
|
||||
* @param keyId if of the secret key
|
||||
* @return passphrase or null, if no passphrase record has been found.
|
||||
*/
|
||||
fun getPassphraseFor(keyId: Long): Passphrase?
|
||||
|
||||
fun hasPassphrase(keyId: Long): Boolean
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key.protection.passphrase_provider
|
||||
|
||||
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 hasPassphrase(keyId: Long): Boolean = true
|
||||
}
|
Loading…
Reference in a new issue