diff --git a/pgpainless-core/src/main/java/org/pgpainless/provider/BouncyCastleProviderFactory.java b/pgpainless-core/src/main/java/org/pgpainless/provider/BouncyCastleProviderFactory.java deleted file mode 100644 index 535b9b8e..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/provider/BouncyCastleProviderFactory.java +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-FileCopyrightText: 2020 Paul Schaub -// -// 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; - } - -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/provider/ProviderFactory.java b/pgpainless-core/src/main/java/org/pgpainless/provider/ProviderFactory.java deleted file mode 100644 index 1859d342..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/provider/ProviderFactory.java +++ /dev/null @@ -1,46 +0,0 @@ -// SPDX-FileCopyrightText: 2020 Paul Schaub -// -// 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}. - *
- * 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(); - } - -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/provider/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/provider/package-info.java deleted file mode 100644 index b5faa3f8..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/provider/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -// SPDX-FileCopyrightText: 2020 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -/** - * Classes that allow setting a custom implementation of {@link java.security.Provider}. - */ -package org.pgpainless.provider; diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/implementation/JceImplementationFactory.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/implementation/JceImplementationFactory.kt index 0684fa24..9a4d8e41 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/implementation/JceImplementationFactory.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/implementation/JceImplementationFactory.kt @@ -31,20 +31,20 @@ import java.util.* class JceImplementationFactory : ImplementationFactory() { override val pgpDigestCalculatorProvider: PGPDigestCalculatorProvider = JcaPGPDigestCalculatorProviderBuilder() - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build() override val pgpContentVerifierBuilderProvider: PGPContentVerifierBuilderProvider = JcaPGPContentVerifierBuilderProvider() - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) override val keyFingerprintCalculator: KeyFingerPrintCalculator = JcaKeyFingerprintCalculator() - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) override fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm, digestCalculator: PGPDigestCalculator, passphrase: Passphrase): PBESecretKeyEncryptor = JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.algorithmId, digestCalculator) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build(passphrase.getChars()) override fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm, @@ -55,44 +55,44 @@ class JceImplementationFactory : ImplementationFactory() { encryptionAlgorithm.algorithmId, getPGPDigestCalculator(hashAlgorithm), s2kCount) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build(passphrase.getChars()) override fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor = JcePBESecretKeyDecryptorBuilder(pgpDigestCalculatorProvider) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build(passphrase.getChars()) override fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder = JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) override fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory = JcePBEDataDecryptorFactoryBuilder(pgpDigestCalculatorProvider) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build(passphrase.getChars()) override fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory = JcePublicKeyDataDecryptorFactoryBuilder() - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build(privateKey) override fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory = JceSessionKeyDataDecryptorFactoryBuilder() - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) .build(sessionKey) override fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator = JcePublicKeyKeyEncryptionMethodGenerator(key) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) override fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator = JcePBEKeyEncryptionMethodGenerator(passphrase.getChars()) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) override fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder = JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm) - .setProvider(ProviderFactory.getProvider()) + .setProvider(ProviderFactory.provider) override fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair = JcaPGPKeyPair(publicKeyAlgorithm.algorithmId, keyPair, creationDate) diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt index 530f1e55..f194638b 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt @@ -222,7 +222,7 @@ class KeyRingBuilder : KeyRingBuilderInterface { fun generateKeyPair(spec: KeySpec): PGPKeyPair { spec.keyType.let { type -> // 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) } .generateKeyPair() diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/provider/BouncyCastleProviderFactory.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/provider/BouncyCastleProviderFactory.kt new file mode 100644 index 00000000..4654f529 --- /dev/null +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/provider/BouncyCastleProviderFactory.kt @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// 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() +} \ No newline at end of file diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/provider/ProviderFactory.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/provider/ProviderFactory.kt new file mode 100644 index 00000000..3fe127f2 --- /dev/null +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/provider/ProviderFactory.kt @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// 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 + } +} \ No newline at end of file diff --git a/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java b/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java index a859008a..5489a11c 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java @@ -21,7 +21,7 @@ public class ProviderFactoryTest { }; @Override - protected Provider _getProvider() { + protected Provider getSecurityProvider() { return provider; }