mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-22 19:08:00 +01:00
Kotlin conversion: ProviderFactory
This commit is contained in:
parent
8351223614
commit
11c1c54111
8 changed files with 63 additions and 92 deletions
|
@ -1,23 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.provider;
|
|
||||||
|
|
||||||
import java.security.Provider;
|
|
||||||
|
|
||||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implementation of {@link ProviderFactory} using a {@link BouncyCastleProvider}.
|
|
||||||
*/
|
|
||||||
public final class BouncyCastleProviderFactory extends ProviderFactory {
|
|
||||||
|
|
||||||
private static final Provider provider = new BouncyCastleProvider();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Provider _getProvider() {
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,46 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.provider;
|
|
||||||
|
|
||||||
import java.security.Provider;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allow the use of different {@link Provider} implementations to provide cryptographic primitives by setting
|
|
||||||
* a {@link ProviderFactory} singleton.
|
|
||||||
* By default, the class is initialized with a {@link BouncyCastleProviderFactory}.
|
|
||||||
* <br>
|
|
||||||
* To make use of your own custom {@link Provider}, call {@link #setFactory(ProviderFactory)}, passing your
|
|
||||||
* own custom {@link ProviderFactory} instance.
|
|
||||||
*/
|
|
||||||
public abstract class ProviderFactory {
|
|
||||||
|
|
||||||
private static ProviderFactory FACTORY;
|
|
||||||
|
|
||||||
protected abstract Provider _getProvider();
|
|
||||||
|
|
||||||
protected String _getProviderName() {
|
|
||||||
return _getProvider().getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setFactory(ProviderFactory factory) {
|
|
||||||
ProviderFactory.FACTORY = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ProviderFactory getFactory() {
|
|
||||||
if (FACTORY == null) {
|
|
||||||
FACTORY = new BouncyCastleProviderFactory();
|
|
||||||
}
|
|
||||||
return FACTORY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Provider getProvider() {
|
|
||||||
return ProviderFactory.getFactory()._getProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getProviderName() {
|
|
||||||
return ProviderFactory.getFactory()._getProviderName();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classes that allow setting a custom implementation of {@link java.security.Provider}.
|
|
||||||
*/
|
|
||||||
package org.pgpainless.provider;
|
|
|
@ -31,20 +31,20 @@ import java.util.*
|
||||||
class JceImplementationFactory : ImplementationFactory() {
|
class JceImplementationFactory : ImplementationFactory() {
|
||||||
override val pgpDigestCalculatorProvider: PGPDigestCalculatorProvider =
|
override val pgpDigestCalculatorProvider: PGPDigestCalculatorProvider =
|
||||||
JcaPGPDigestCalculatorProviderBuilder()
|
JcaPGPDigestCalculatorProviderBuilder()
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build()
|
.build()
|
||||||
override val pgpContentVerifierBuilderProvider: PGPContentVerifierBuilderProvider =
|
override val pgpContentVerifierBuilderProvider: PGPContentVerifierBuilderProvider =
|
||||||
JcaPGPContentVerifierBuilderProvider()
|
JcaPGPContentVerifierBuilderProvider()
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
override val keyFingerprintCalculator: KeyFingerPrintCalculator =
|
override val keyFingerprintCalculator: KeyFingerPrintCalculator =
|
||||||
JcaKeyFingerprintCalculator()
|
JcaKeyFingerprintCalculator()
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
|
|
||||||
override fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm,
|
override fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm,
|
||||||
digestCalculator: PGPDigestCalculator,
|
digestCalculator: PGPDigestCalculator,
|
||||||
passphrase: Passphrase): PBESecretKeyEncryptor =
|
passphrase: Passphrase): PBESecretKeyEncryptor =
|
||||||
JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.algorithmId, digestCalculator)
|
JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.algorithmId, digestCalculator)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build(passphrase.getChars())
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
override fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm,
|
override fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm,
|
||||||
|
@ -55,44 +55,44 @@ class JceImplementationFactory : ImplementationFactory() {
|
||||||
encryptionAlgorithm.algorithmId,
|
encryptionAlgorithm.algorithmId,
|
||||||
getPGPDigestCalculator(hashAlgorithm),
|
getPGPDigestCalculator(hashAlgorithm),
|
||||||
s2kCount)
|
s2kCount)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build(passphrase.getChars())
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
override fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor =
|
override fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor =
|
||||||
JcePBESecretKeyDecryptorBuilder(pgpDigestCalculatorProvider)
|
JcePBESecretKeyDecryptorBuilder(pgpDigestCalculatorProvider)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build(passphrase.getChars())
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
override fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder =
|
override fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder =
|
||||||
JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm)
|
JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
|
|
||||||
override fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory =
|
override fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory =
|
||||||
JcePBEDataDecryptorFactoryBuilder(pgpDigestCalculatorProvider)
|
JcePBEDataDecryptorFactoryBuilder(pgpDigestCalculatorProvider)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build(passphrase.getChars())
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
override fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory =
|
override fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory =
|
||||||
JcePublicKeyDataDecryptorFactoryBuilder()
|
JcePublicKeyDataDecryptorFactoryBuilder()
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build(privateKey)
|
.build(privateKey)
|
||||||
|
|
||||||
override fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory =
|
override fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory =
|
||||||
JceSessionKeyDataDecryptorFactoryBuilder()
|
JceSessionKeyDataDecryptorFactoryBuilder()
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
.build(sessionKey)
|
.build(sessionKey)
|
||||||
|
|
||||||
override fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator =
|
override fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator =
|
||||||
JcePublicKeyKeyEncryptionMethodGenerator(key)
|
JcePublicKeyKeyEncryptionMethodGenerator(key)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
|
|
||||||
override fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator =
|
override fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator =
|
||||||
JcePBEKeyEncryptionMethodGenerator(passphrase.getChars())
|
JcePBEKeyEncryptionMethodGenerator(passphrase.getChars())
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
|
|
||||||
override fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder =
|
override fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder =
|
||||||
JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm)
|
JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm)
|
||||||
.setProvider(ProviderFactory.getProvider())
|
.setProvider(ProviderFactory.provider)
|
||||||
|
|
||||||
override fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair =
|
override fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair =
|
||||||
JcaPGPKeyPair(publicKeyAlgorithm.algorithmId, keyPair, creationDate)
|
JcaPGPKeyPair(publicKeyAlgorithm.algorithmId, keyPair, creationDate)
|
||||||
|
|
|
@ -222,7 +222,7 @@ class KeyRingBuilder : KeyRingBuilderInterface<KeyRingBuilder> {
|
||||||
fun generateKeyPair(spec: KeySpec): PGPKeyPair {
|
fun generateKeyPair(spec: KeySpec): PGPKeyPair {
|
||||||
spec.keyType.let { type ->
|
spec.keyType.let { type ->
|
||||||
// Create raw Key Pair
|
// Create raw Key Pair
|
||||||
val keyPair = KeyPairGenerator.getInstance(type.name, ProviderFactory.getProvider())
|
val keyPair = KeyPairGenerator.getInstance(type.name, ProviderFactory.provider)
|
||||||
.also { it.initialize(type.algorithmSpec) }
|
.also { it.initialize(type.algorithmSpec) }
|
||||||
.generateKeyPair()
|
.generateKeyPair()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.provider
|
||||||
|
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider
|
||||||
|
import java.security.Provider
|
||||||
|
|
||||||
|
class BouncyCastleProviderFactory : ProviderFactory() {
|
||||||
|
override val securityProvider: Provider = BouncyCastleProvider()
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.provider
|
||||||
|
|
||||||
|
import java.security.Provider
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow the use of different [Provider] implementations to provide cryptographic primitives by setting
|
||||||
|
* a [ProviderFactory] singleton.
|
||||||
|
* By default, the class is initialized with a [BouncyCastleProviderFactory].
|
||||||
|
* To make use of your own custom [Provider], call [setFactory], passing your
|
||||||
|
* own custom [ProviderFactory] instance.
|
||||||
|
*/
|
||||||
|
abstract class ProviderFactory {
|
||||||
|
|
||||||
|
protected abstract val securityProvider: Provider
|
||||||
|
protected open val securityProviderName: String
|
||||||
|
get() = securityProvider.name
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// singleton instance
|
||||||
|
@JvmStatic
|
||||||
|
var factory: ProviderFactory = BouncyCastleProviderFactory()
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
val provider: Provider
|
||||||
|
@JvmName("getProvider")
|
||||||
|
get() = factory.securityProvider
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
val providerName: String
|
||||||
|
get() = factory.securityProviderName
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,7 +21,7 @@ public class ProviderFactoryTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Provider _getProvider() {
|
protected Provider getSecurityProvider() {
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue