2021-01-03 15:52:33 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-12-27 01:56:18 +01:00
|
|
|
package org.pgpainless.implementation;
|
|
|
|
|
2021-01-03 15:52:33 +01:00
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.util.Date;
|
2020-12-27 01:56:18 +01:00
|
|
|
|
2021-01-03 15:52:33 +01:00
|
|
|
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;
|
2020-12-27 01:56:18 +01:00
|
|
|
|
2021-01-03 15:52:33 +01:00
|
|
|
public abstract class ImplementationFactory {
|
|
|
|
|
|
|
|
private static ImplementationFactory FACTORY_IMPLEMENTATION = new BcImplementationFactory();
|
|
|
|
|
|
|
|
public static void setFactoryImplementation(ImplementationFactory implementation) {
|
2020-12-27 01:56:18 +01:00
|
|
|
FACTORY_IMPLEMENTATION = implementation;
|
|
|
|
}
|
|
|
|
|
2021-01-03 15:52:33 +01:00
|
|
|
public static ImplementationFactory getInstance() {
|
2020-12-27 01:56:18 +01:00
|
|
|
return FACTORY_IMPLEMENTATION;
|
|
|
|
}
|
|
|
|
|
2021-01-03 15:52:33 +01:00
|
|
|
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
|
|
|
Passphrase passphrase)
|
|
|
|
throws PGPException {
|
|
|
|
return getPBESecretKeyEncryptor(symmetricKeyAlgorithm,
|
|
|
|
getPGPDigestCalculator(HashAlgorithm.SHA1), passphrase);
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException;
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
|
|
|
PGPDigestCalculator digestCalculator,
|
|
|
|
Passphrase passphrase);
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException;
|
2021-01-03 15:52:33 +01:00
|
|
|
|
|
|
|
public PGPDigestCalculator getPGPDigestCalculator(HashAlgorithm algorithm) throws PGPException {
|
|
|
|
return getPGPDigestCalculator(algorithm.getAlgorithmId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public PGPDigestCalculator getPGPDigestCalculator(int algorithm) throws PGPException {
|
|
|
|
return getPGPDigestCalculatorProvider().get(algorithm);
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException;
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider();
|
2021-01-03 15:52:33 +01:00
|
|
|
|
|
|
|
public PGPContentSignerBuilder getPGPContentSignerBuilder(PublicKeyAlgorithm keyAlgorithm, HashAlgorithm hashAlgorithm) {
|
|
|
|
return getPGPContentSignerBuilder(keyAlgorithm.getAlgorithmId(), hashAlgorithm.getAlgorithmId());
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm);
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract KeyFingerPrintCalculator getKeyFingerprintCalculator();
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException;
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey);
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key);
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase);
|
2021-01-03 15:52:33 +01:00
|
|
|
|
|
|
|
public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(SymmetricKeyAlgorithm symmetricKeyAlgorithm) {
|
|
|
|
return getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId());
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm);
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException;
|
2021-01-03 15:52:33 +01:00
|
|
|
|
2021-01-23 01:09:55 +01:00
|
|
|
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm,
|
|
|
|
HashAlgorithm hashAlgorithm, int s2kCount,
|
|
|
|
Passphrase passphrase) throws PGPException;
|
2021-07-03 12:44:21 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getClass().getSimpleName();
|
|
|
|
}
|
2020-12-27 01:56:18 +01:00
|
|
|
}
|