diff --git a/pgpainless-core/src/main/java/org/pgpainless/implementation/BcCryptoEngineImplementation.java b/pgpainless-core/src/main/java/org/pgpainless/implementation/BcImplementationFactory.java similarity index 91% rename from pgpainless-core/src/main/java/org/pgpainless/implementation/BcCryptoEngineImplementation.java rename to pgpainless-core/src/main/java/org/pgpainless/implementation/BcImplementationFactory.java index 66829d07..7b75f422 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/implementation/BcCryptoEngineImplementation.java +++ b/pgpainless-core/src/main/java/org/pgpainless/implementation/BcImplementationFactory.java @@ -1,3 +1,18 @@ +/* + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.pgpainless.implementation; import java.security.KeyPair; @@ -40,7 +55,7 @@ import org.pgpainless.algorithm.PublicKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.util.Passphrase; -public class BcCryptoEngineImplementation implements CryptoEngineImplementation { +public class BcImplementationFactory extends ImplementationFactory { @Override public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) diff --git a/pgpainless-core/src/main/java/org/pgpainless/implementation/CryptoEngineImplementation.java b/pgpainless-core/src/main/java/org/pgpainless/implementation/CryptoEngineImplementation.java deleted file mode 100644 index 154abb65..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/implementation/CryptoEngineImplementation.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.pgpainless.implementation; - -import java.io.IOException; -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; -import java.security.spec.InvalidKeySpecException; -import java.util.Date; - -import org.bouncycastle.crypto.AsymmetricCipherKeyPair; -import org.bouncycastle.openpgp.PGPException; -import org.bouncycastle.openpgp.PGPKeyPair; -import org.bouncycastle.openpgp.PGPPrivateKey; -import org.bouncycastle.openpgp.PGPPublicKey; -import org.bouncycastle.openpgp.PGPSecretKey; -import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; -import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory; -import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator; -import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; -import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; -import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; -import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider; -import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder; -import org.bouncycastle.openpgp.operator.PGPDigestCalculator; -import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider; -import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory; -import org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator; -import org.pgpainless.algorithm.HashAlgorithm; -import org.pgpainless.algorithm.PublicKeyAlgorithm; -import org.pgpainless.algorithm.SymmetricKeyAlgorithm; -import org.pgpainless.util.Passphrase; - -public interface CryptoEngineImplementation { - - PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException; - - default PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, - Passphrase passphrase) - throws PGPException { - return getPBESecretKeyEncryptor(symmetricKeyAlgorithm, - getPGPDigestCalculator(HashAlgorithm.SHA1), passphrase); - } - - PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, - PGPDigestCalculator digestCalculator, - Passphrase passphrase); - - PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException; - - PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException; - - default PGPDigestCalculator getPGPDigestCalculator(HashAlgorithm algorithm) throws PGPException { - return getPGPDigestCalculator(algorithm.getAlgorithmId()); - } - - default PGPDigestCalculator getPGPDigestCalculator(int algorithm) throws PGPException { - return getPGPDigestCalculatorProvider().get(algorithm); - } - - PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider(); - - default PGPContentSignerBuilder getPGPContentSignerBuilder(PublicKeyAlgorithm keyAlgorithm, HashAlgorithm hashAlgorithm) { - return getPGPContentSignerBuilder(keyAlgorithm.getAlgorithmId(), hashAlgorithm.getAlgorithmId()); - } - - PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm); - - KeyFingerPrintCalculator getKeyFingerprintCalculator(); - - PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException; - - PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey); - - PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key); - - PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase); - - default PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(SymmetricKeyAlgorithm symmetricKeyAlgorithm) { - return getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId()); - } - - PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm); - - PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException; - - PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, AsymmetricCipherKeyPair keyPair, Date creationDate) throws PGPException, NoSuchAlgorithmException, IOException, InvalidKeySpecException; - - PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, - HashAlgorithm hashAlgorithm, - int s2kCount, - Passphrase passphrase) throws PGPException; -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java b/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java index 293f7bee..6a716b6c 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java @@ -1,15 +1,141 @@ +/* + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.pgpainless.implementation; -public class ImplementationFactory { +import java.io.IOException; +import java.security.KeyPair; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.util.Date; - private static CryptoEngineImplementation FACTORY_IMPLEMENTATION = new BcCryptoEngineImplementation(); +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPKeyPair; +import org.bouncycastle.openpgp.PGPPrivateKey; +import org.bouncycastle.openpgp.PGPPublicKey; +import org.bouncycastle.openpgp.PGPSecretKey; +import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; +import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory; +import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator; +import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; +import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; +import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider; +import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder; +import org.bouncycastle.openpgp.operator.PGPDigestCalculator; +import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider; +import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory; +import org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator; +import org.pgpainless.algorithm.HashAlgorithm; +import org.pgpainless.algorithm.PublicKeyAlgorithm; +import org.pgpainless.algorithm.SymmetricKeyAlgorithm; +import org.pgpainless.util.Passphrase; - public static void setFactoryImplementation(CryptoEngineImplementation implementation) { +public abstract class ImplementationFactory { + + private static ImplementationFactory FACTORY_IMPLEMENTATION = new BcImplementationFactory(); + + public static void setFactoryImplementation(ImplementationFactory implementation) { FACTORY_IMPLEMENTATION = implementation; } - public static CryptoEngineImplementation getInstance() { + public static ImplementationFactory getInstance() { return FACTORY_IMPLEMENTATION; } + public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, + Passphrase passphrase) + throws PGPException { + return getPBESecretKeyEncryptor(symmetricKeyAlgorithm, + getPGPDigestCalculator(HashAlgorithm.SHA1), passphrase); + } + + public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException { + return FACTORY_IMPLEMENTATION.getPBESecretKeyEncryptor(secretKey, passphrase); + } + + public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, PGPDigestCalculator digestCalculator, Passphrase passphrase) { + return FACTORY_IMPLEMENTATION.getPBESecretKeyEncryptor(symmetricKeyAlgorithm, digestCalculator, passphrase); + } + + public PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException { + return FACTORY_IMPLEMENTATION.getPBESecretKeyDecryptor(passphrase); + } + + public PGPDigestCalculator getPGPDigestCalculator(HashAlgorithm algorithm) throws PGPException { + return getPGPDigestCalculator(algorithm.getAlgorithmId()); + } + + public PGPDigestCalculator getPGPDigestCalculator(int algorithm) throws PGPException { + return getPGPDigestCalculatorProvider().get(algorithm); + } + + public PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException { + return FACTORY_IMPLEMENTATION.getPGPDigestCalculatorProvider(); + } + + public PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider() { + return FACTORY_IMPLEMENTATION.getPGPContentVerifierBuilderProvider(); + } + + public PGPContentSignerBuilder getPGPContentSignerBuilder(PublicKeyAlgorithm keyAlgorithm, HashAlgorithm hashAlgorithm) { + return getPGPContentSignerBuilder(keyAlgorithm.getAlgorithmId(), hashAlgorithm.getAlgorithmId()); + } + + public PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm) { + return FACTORY_IMPLEMENTATION.getPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm); + } + + public KeyFingerPrintCalculator getKeyFingerprintCalculator() { + return FACTORY_IMPLEMENTATION.getKeyFingerprintCalculator(); + } + + public PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException { + return FACTORY_IMPLEMENTATION.getPBEDataDecryptorFactory(passphrase); + } + + public PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey) { + return FACTORY_IMPLEMENTATION.getPublicKeyDataDecryptorFactory(privateKey); + } + + public PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key) { + return FACTORY_IMPLEMENTATION.getPublicKeyKeyEncryptionMethodGenerator(key); + } + + public PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase) { + return FACTORY_IMPLEMENTATION.getPBEKeyEncryptionMethodGenerator(passphrase); + } + + public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(SymmetricKeyAlgorithm symmetricKeyAlgorithm) { + return getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId()); + } + + public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm) { + return FACTORY_IMPLEMENTATION.getPGPDataEncryptorBuilder(symmetricKeyAlgorithm); + } + + public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException { + return FACTORY_IMPLEMENTATION.getPGPKeyPair(algorithm, keyPair, creationDate); + } + + public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, AsymmetricCipherKeyPair keyPair, Date creationDate) throws PGPException, NoSuchAlgorithmException, IOException, InvalidKeySpecException { + return FACTORY_IMPLEMENTATION.getPGPKeyPair(algorithm, keyPair, creationDate); + } + + public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, HashAlgorithm hashAlgorithm, int s2kCount, Passphrase passphrase) throws PGPException { + return FACTORY_IMPLEMENTATION.getPBESecretKeyEncryptor(encryptionAlgorithm, hashAlgorithm, s2kCount, passphrase); + } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/implementation/JceCryptoEngineImplementation.java b/pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java similarity index 92% rename from pgpainless-core/src/main/java/org/pgpainless/implementation/JceCryptoEngineImplementation.java rename to pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java index b957d790..59a2ccd4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/implementation/JceCryptoEngineImplementation.java +++ b/pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java @@ -1,3 +1,18 @@ +/* + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.pgpainless.implementation; import java.io.IOException; @@ -47,9 +62,8 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.provider.ProviderFactory; import org.pgpainless.util.Passphrase; -public class JceCryptoEngineImplementation implements CryptoEngineImplementation { +public class JceImplementationFactory extends ImplementationFactory { - @Override public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException { return new JcePBESecretKeyEncryptorBuilder(secretKey.getKeyEncryptionAlgorithm()) @@ -57,21 +71,18 @@ public class JceCryptoEngineImplementation implements CryptoEngineImplementation .build(passphrase.getChars()); } - @Override public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, PGPDigestCalculator digestCalculator, Passphrase passphrase) { return new JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId(), digestCalculator) .setProvider(ProviderFactory.getProvider()) .build(passphrase.getChars()); } - @Override public PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException { return new JcePBESecretKeyDecryptorBuilder(getPGPDigestCalculatorProvider()) .setProvider(ProviderFactory.getProvider()) .build(passphrase.getChars()); } - @Override public PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException { return new JcaPGPDigestCalculatorProviderBuilder() @@ -79,25 +90,21 @@ public class JceCryptoEngineImplementation implements CryptoEngineImplementation .build(); } - @Override public PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider() { return new JcaPGPContentVerifierBuilderProvider() .setProvider(ProviderFactory.getProvider()); } - @Override public PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm) { return new JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm) .setProvider(ProviderFactory.getProvider()); } - @Override public KeyFingerPrintCalculator getKeyFingerprintCalculator() { return new JcaKeyFingerprintCalculator() .setProvider(ProviderFactory.getProvider()); } - @Override public PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException { return new JcePBEDataDecryptorFactoryBuilder(getPGPDigestCalculatorProvider()) @@ -105,42 +112,35 @@ public class JceCryptoEngineImplementation implements CryptoEngineImplementation .build(passphrase.getChars()); } - @Override public PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey) { return new JcePublicKeyDataDecryptorFactoryBuilder() .setProvider(ProviderFactory.getProvider()) .build(privateKey); } - @Override public PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key) { return new JcePublicKeyKeyEncryptionMethodGenerator(key) .setProvider(ProviderFactory.getProvider()); } - @Override public PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase) { return new JcePBEKeyEncryptionMethodGenerator(passphrase.getChars()) .setProvider(ProviderFactory.getProvider()); } - @Override public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm) { return new JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm) .setProvider(ProviderFactory.getProvider()); } - @Override public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException { return new JcaPGPKeyPair(algorithm.getAlgorithmId(), keyPair, creationDate); } - @Override public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, AsymmetricCipherKeyPair keyPair, Date creationDate) throws PGPException, NoSuchAlgorithmException, IOException, InvalidKeySpecException { return new JcaPGPKeyPair(algorithm.getAlgorithmId(), bcToJceKeyPair(keyPair), creationDate); } - @Override public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, HashAlgorithm hashAlgorithm, int s2kCount, Passphrase passphrase) throws PGPException { return new JcePBESecretKeyEncryptorBuilder( encryptionAlgorithm.getAlgorithmId(),