mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-16 01:12:05 +01:00
Kotlin conversion: ImplementationFactory classes
This commit is contained in:
parent
c3cf671367
commit
bdb6aeb4f1
11 changed files with 299 additions and 430 deletions
|
@ -1,154 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.implementation;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.security.KeyPair;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
|
||||||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
|
||||||
import org.bouncycastle.openpgp.PGPKeyPair;
|
|
||||||
import org.bouncycastle.openpgp.PGPObjectFactory;
|
|
||||||
import org.bouncycastle.openpgp.PGPPrivateKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPSessionKey;
|
|
||||||
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
|
|
||||||
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.PublicKeyDataDecryptorFactory;
|
|
||||||
import org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator;
|
|
||||||
import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPBEDataDecryptorFactory;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPBEKeyEncryptionMethodGenerator;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyEncryptorBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyConverter;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator;
|
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcSessionKeyDataDecryptorFactory;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
|
|
||||||
import org.pgpainless.algorithm.HashAlgorithm;
|
|
||||||
import org.pgpainless.algorithm.PublicKeyAlgorithm;
|
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
||||||
import org.pgpainless.util.Passphrase;
|
|
||||||
|
|
||||||
public class BcImplementationFactory extends ImplementationFactory {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
|
||||||
PGPDigestCalculator digestCalculator,
|
|
||||||
Passphrase passphrase) {
|
|
||||||
return new BcPBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId(), digestCalculator)
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) {
|
|
||||||
return new BcPBESecretKeyDecryptorBuilder(getPGPDigestCalculatorProvider())
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BcPGPDigestCalculatorProvider getPGPDigestCalculatorProvider() {
|
|
||||||
return new BcPGPDigestCalculatorProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider() {
|
|
||||||
return new BcPGPContentVerifierBuilderProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm) {
|
|
||||||
return new BcPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public KeyFingerPrintCalculator getKeyFingerprintCalculator() {
|
|
||||||
return new BcKeyFingerprintCalculator();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) {
|
|
||||||
return new BcPBEDataDecryptorFactory(passphrase.getChars(), getPGPDigestCalculatorProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey) {
|
|
||||||
return new BcPublicKeyDataDecryptorFactory(privateKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SessionKeyDataDecryptorFactory getSessionKeyDataDecryptorFactory(PGPSessionKey sessionKey) {
|
|
||||||
return new BcSessionKeyDataDecryptorFactory(sessionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key) {
|
|
||||||
return new BcPublicKeyKeyEncryptionMethodGenerator(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase) {
|
|
||||||
return new BcPBEKeyEncryptionMethodGenerator(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm) {
|
|
||||||
return new BcPGPDataEncryptorBuilder(symmetricKeyAlgorithm);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate)
|
|
||||||
throws PGPException {
|
|
||||||
return new BcPGPKeyPair(algorithm.getAlgorithmId(), jceToBcKeyPair(algorithm, keyPair, creationDate), creationDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, HashAlgorithm hashAlgorithm, int s2kCount, Passphrase passphrase) throws PGPException {
|
|
||||||
return new BcPBESecretKeyEncryptorBuilder(
|
|
||||||
encryptionAlgorithm.getAlgorithmId(),
|
|
||||||
getPGPDigestCalculator(hashAlgorithm),
|
|
||||||
s2kCount)
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPObjectFactory getPGPObjectFactory(byte[] bytes) {
|
|
||||||
return new BcPGPObjectFactory(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPObjectFactory getPGPObjectFactory(InputStream inputStream) {
|
|
||||||
return new BcPGPObjectFactory(inputStream);
|
|
||||||
}
|
|
||||||
|
|
||||||
private AsymmetricCipherKeyPair jceToBcKeyPair(PublicKeyAlgorithm algorithm,
|
|
||||||
KeyPair keyPair,
|
|
||||||
Date creationDate) throws PGPException {
|
|
||||||
BcPGPKeyConverter converter = new BcPGPKeyConverter();
|
|
||||||
|
|
||||||
PGPKeyPair pair = new JcaPGPKeyPair(algorithm.getAlgorithmId(), keyPair, creationDate);
|
|
||||||
AsymmetricKeyParameter publicKey = converter.getPublicKey(pair.getPublicKey());
|
|
||||||
AsymmetricKeyParameter privateKey = converter.getPrivateKey(pair.getPrivateKey());
|
|
||||||
|
|
||||||
return new AsymmetricCipherKeyPair(publicKey, privateKey);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,119 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.implementation;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.security.KeyPair;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
|
||||||
import org.bouncycastle.openpgp.PGPKeyPair;
|
|
||||||
import org.bouncycastle.openpgp.PGPObjectFactory;
|
|
||||||
import org.bouncycastle.openpgp.PGPPrivateKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPSessionKey;
|
|
||||||
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.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
|
|
||||||
import org.pgpainless.algorithm.HashAlgorithm;
|
|
||||||
import org.pgpainless.algorithm.PublicKeyAlgorithm;
|
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
||||||
import org.pgpainless.util.Passphrase;
|
|
||||||
import org.pgpainless.util.SessionKey;
|
|
||||||
|
|
||||||
public abstract class ImplementationFactory {
|
|
||||||
|
|
||||||
private static ImplementationFactory FACTORY_IMPLEMENTATION;
|
|
||||||
|
|
||||||
public static void setFactoryImplementation(ImplementationFactory implementation) {
|
|
||||||
FACTORY_IMPLEMENTATION = implementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ImplementationFactory getInstance() {
|
|
||||||
if (FACTORY_IMPLEMENTATION == null) {
|
|
||||||
FACTORY_IMPLEMENTATION = new BcImplementationFactory();
|
|
||||||
}
|
|
||||||
return FACTORY_IMPLEMENTATION;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
|
||||||
PGPDigestCalculator digestCalculator,
|
|
||||||
Passphrase passphrase);
|
|
||||||
|
|
||||||
public abstract PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException;
|
|
||||||
|
|
||||||
public PGPDigestCalculator getV4FingerprintCalculator() throws PGPException {
|
|
||||||
return getPGPDigestCalculator(HashAlgorithm.SHA1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPDigestCalculator getPGPDigestCalculator(HashAlgorithm algorithm) throws PGPException {
|
|
||||||
return getPGPDigestCalculator(algorithm.getAlgorithmId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPDigestCalculator getPGPDigestCalculator(int algorithm) throws PGPException {
|
|
||||||
return getPGPDigestCalculatorProvider().get(algorithm);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException;
|
|
||||||
|
|
||||||
public abstract PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider();
|
|
||||||
|
|
||||||
public PGPContentSignerBuilder getPGPContentSignerBuilder(PublicKeyAlgorithm keyAlgorithm, HashAlgorithm hashAlgorithm) {
|
|
||||||
return getPGPContentSignerBuilder(keyAlgorithm.getAlgorithmId(), hashAlgorithm.getAlgorithmId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm);
|
|
||||||
|
|
||||||
public abstract KeyFingerPrintCalculator getKeyFingerprintCalculator();
|
|
||||||
|
|
||||||
public abstract PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException;
|
|
||||||
|
|
||||||
public abstract PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey);
|
|
||||||
|
|
||||||
public SessionKeyDataDecryptorFactory getSessionKeyDataDecryptorFactory(SessionKey sessionKey) {
|
|
||||||
PGPSessionKey pgpSessionKey = new PGPSessionKey(
|
|
||||||
sessionKey.getAlgorithm().getAlgorithmId(),
|
|
||||||
sessionKey.getKey()
|
|
||||||
);
|
|
||||||
return getSessionKeyDataDecryptorFactory(pgpSessionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract SessionKeyDataDecryptorFactory getSessionKeyDataDecryptorFactory(PGPSessionKey sessionKey);
|
|
||||||
|
|
||||||
public abstract PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key);
|
|
||||||
|
|
||||||
public abstract PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase);
|
|
||||||
|
|
||||||
public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(SymmetricKeyAlgorithm symmetricKeyAlgorithm) {
|
|
||||||
return getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm);
|
|
||||||
|
|
||||||
public abstract PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException;
|
|
||||||
|
|
||||||
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm,
|
|
||||||
HashAlgorithm hashAlgorithm, int s2kCount,
|
|
||||||
Passphrase passphrase) throws PGPException;
|
|
||||||
|
|
||||||
public abstract PGPObjectFactory getPGPObjectFactory(InputStream inputStream);
|
|
||||||
|
|
||||||
public abstract PGPObjectFactory getPGPObjectFactory(byte[] bytes);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return getClass().getSimpleName();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,141 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.implementation;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.security.KeyPair;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
|
||||||
import org.bouncycastle.openpgp.PGPKeyPair;
|
|
||||||
import org.bouncycastle.openpgp.PGPObjectFactory;
|
|
||||||
import org.bouncycastle.openpgp.PGPPrivateKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPSessionKey;
|
|
||||||
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.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator;
|
|
||||||
import org.bouncycastle.openpgp.operator.jcajce.JceSessionKeyDataDecryptorFactoryBuilder;
|
|
||||||
import org.pgpainless.algorithm.HashAlgorithm;
|
|
||||||
import org.pgpainless.algorithm.PublicKeyAlgorithm;
|
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
||||||
import org.pgpainless.provider.ProviderFactory;
|
|
||||||
import org.pgpainless.util.Passphrase;
|
|
||||||
|
|
||||||
public class JceImplementationFactory extends ImplementationFactory {
|
|
||||||
|
|
||||||
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, PGPDigestCalculator digestCalculator, Passphrase passphrase) {
|
|
||||||
return new JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId(), digestCalculator)
|
|
||||||
.setProvider(ProviderFactory.getProvider())
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException {
|
|
||||||
return new JcePBESecretKeyDecryptorBuilder(getPGPDigestCalculatorProvider())
|
|
||||||
.setProvider(ProviderFactory.getProvider())
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPDigestCalculatorProvider getPGPDigestCalculatorProvider()
|
|
||||||
throws PGPException {
|
|
||||||
return new JcaPGPDigestCalculatorProviderBuilder()
|
|
||||||
.setProvider(ProviderFactory.getProvider())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider() {
|
|
||||||
return new JcaPGPContentVerifierBuilderProvider()
|
|
||||||
.setProvider(ProviderFactory.getProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm) {
|
|
||||||
return new JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm)
|
|
||||||
.setProvider(ProviderFactory.getProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
public KeyFingerPrintCalculator getKeyFingerprintCalculator() {
|
|
||||||
return new JcaKeyFingerprintCalculator()
|
|
||||||
.setProvider(ProviderFactory.getProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase)
|
|
||||||
throws PGPException {
|
|
||||||
return new JcePBEDataDecryptorFactoryBuilder(getPGPDigestCalculatorProvider())
|
|
||||||
.setProvider(ProviderFactory.getProvider())
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey) {
|
|
||||||
return new JcePublicKeyDataDecryptorFactoryBuilder()
|
|
||||||
.setProvider(ProviderFactory.getProvider())
|
|
||||||
.build(privateKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SessionKeyDataDecryptorFactory getSessionKeyDataDecryptorFactory(PGPSessionKey sessionKey) {
|
|
||||||
return new JceSessionKeyDataDecryptorFactoryBuilder()
|
|
||||||
.build(sessionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key) {
|
|
||||||
return new JcePublicKeyKeyEncryptionMethodGenerator(key)
|
|
||||||
.setProvider(ProviderFactory.getProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase) {
|
|
||||||
return new JcePBEKeyEncryptionMethodGenerator(passphrase.getChars())
|
|
||||||
.setProvider(ProviderFactory.getProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm) {
|
|
||||||
return new JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm)
|
|
||||||
.setProvider(ProviderFactory.getProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException {
|
|
||||||
return new JcaPGPKeyPair(algorithm.getAlgorithmId(), keyPair, creationDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, HashAlgorithm hashAlgorithm, int s2kCount, Passphrase passphrase) throws PGPException {
|
|
||||||
return new JcePBESecretKeyEncryptorBuilder(
|
|
||||||
encryptionAlgorithm.getAlgorithmId(),
|
|
||||||
getPGPDigestCalculator(hashAlgorithm),
|
|
||||||
s2kCount)
|
|
||||||
.setProvider(ProviderFactory.getProvider())
|
|
||||||
.build(passphrase.getChars());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPObjectFactory getPGPObjectFactory(InputStream inputStream) {
|
|
||||||
return new PGPObjectFactory(inputStream, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PGPObjectFactory getPGPObjectFactory(byte[] bytes) {
|
|
||||||
return new PGPObjectFactory(bytes, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implementation factory classes to be able to switch out the underlying crypto engine implementation.
|
|
||||||
*/
|
|
||||||
package org.pgpainless.implementation;
|
|
|
@ -120,7 +120,7 @@ public class PublicKeyParameterValidationUtil {
|
||||||
signatureGenerator.update(data);
|
signatureGenerator.update(data);
|
||||||
PGPSignature sig = signatureGenerator.generate();
|
PGPSignature sig = signatureGenerator.generate();
|
||||||
|
|
||||||
sig.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), publicKey);
|
sig.init(ImplementationFactory.getInstance().getPgpContentVerifierBuilderProvider(), publicKey);
|
||||||
sig.update(data);
|
sig.update(data);
|
||||||
return sig.verify();
|
return sig.verify();
|
||||||
} catch (PGPException e) {
|
} catch (PGPException e) {
|
||||||
|
|
|
@ -495,7 +495,7 @@ public abstract class SignatureValidator {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
signature.init(ImplementationFactory.getInstance()
|
signature.init(ImplementationFactory.getInstance()
|
||||||
.getPGPContentVerifierBuilderProvider(), primaryKey);
|
.getPgpContentVerifierBuilderProvider(), primaryKey);
|
||||||
boolean valid = signature.verifyCertification(primaryKey, subkey);
|
boolean valid = signature.verifyCertification(primaryKey, subkey);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
throw new SignatureValidationException("Signature is not correct.");
|
throw new SignatureValidationException("Signature is not correct.");
|
||||||
|
@ -519,7 +519,7 @@ public abstract class SignatureValidator {
|
||||||
@Override
|
@Override
|
||||||
public void verify(PGPSignature signature) throws SignatureValidationException {
|
public void verify(PGPSignature signature) throws SignatureValidationException {
|
||||||
try {
|
try {
|
||||||
signature.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), subkey);
|
signature.init(ImplementationFactory.getInstance().getPgpContentVerifierBuilderProvider(), subkey);
|
||||||
boolean valid = signature.verifyCertification(primaryKey, subkey);
|
boolean valid = signature.verifyCertification(primaryKey, subkey);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
throw new SignatureValidationException("Primary Key Binding Signature is not correct.");
|
throw new SignatureValidationException("Primary Key Binding Signature is not correct.");
|
||||||
|
@ -544,7 +544,7 @@ public abstract class SignatureValidator {
|
||||||
@Override
|
@Override
|
||||||
public void verify(PGPSignature signature) throws SignatureValidationException {
|
public void verify(PGPSignature signature) throws SignatureValidationException {
|
||||||
try {
|
try {
|
||||||
signature.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), signer);
|
signature.init(ImplementationFactory.getInstance().getPgpContentVerifierBuilderProvider(), signer);
|
||||||
boolean valid;
|
boolean valid;
|
||||||
if (signer.getKeyID() == signee.getKeyID() || signature.getSignatureType() == PGPSignature.DIRECT_KEY) {
|
if (signer.getKeyID() == signee.getKeyID() || signature.getSignatureType() == PGPSignature.DIRECT_KEY) {
|
||||||
valid = signature.verifyCertification(signee);
|
valid = signature.verifyCertification(signee);
|
||||||
|
@ -615,7 +615,7 @@ public abstract class SignatureValidator {
|
||||||
public void verify(PGPSignature signature) throws SignatureValidationException {
|
public void verify(PGPSignature signature) throws SignatureValidationException {
|
||||||
try {
|
try {
|
||||||
signature.init(ImplementationFactory.getInstance()
|
signature.init(ImplementationFactory.getInstance()
|
||||||
.getPGPContentVerifierBuilderProvider(), certifyingKey);
|
.getPgpContentVerifierBuilderProvider(), certifyingKey);
|
||||||
boolean valid = signature.verifyCertification(userId, certifiedKey);
|
boolean valid = signature.verifyCertification(userId, certifiedKey);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
throw new SignatureValidationException("Signature over user-id '" + userId +
|
throw new SignatureValidationException("Signature over user-id '" + userId +
|
||||||
|
@ -645,7 +645,7 @@ public abstract class SignatureValidator {
|
||||||
public void verify(PGPSignature signature) throws SignatureValidationException {
|
public void verify(PGPSignature signature) throws SignatureValidationException {
|
||||||
try {
|
try {
|
||||||
signature.init(ImplementationFactory.getInstance()
|
signature.init(ImplementationFactory.getInstance()
|
||||||
.getPGPContentVerifierBuilderProvider(), certifyingKey);
|
.getPgpContentVerifierBuilderProvider(), certifyingKey);
|
||||||
boolean valid = signature.verifyCertification(userAttributes, certifiedKey);
|
boolean valid = signature.verifyCertification(userAttributes, certifiedKey);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
throw new SignatureValidationException("Signature over user-attribute vector is not correct.");
|
throw new SignatureValidationException("Signature over user-attribute vector is not correct.");
|
||||||
|
|
|
@ -384,7 +384,7 @@ public final class SignatureVerifier {
|
||||||
PGPPublicKey signingKey)
|
PGPPublicKey signingKey)
|
||||||
throws SignatureValidationException {
|
throws SignatureValidationException {
|
||||||
try {
|
try {
|
||||||
signature.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), signingKey);
|
signature.init(ImplementationFactory.getInstance().getPgpContentVerifierBuilderProvider(), signingKey);
|
||||||
int read;
|
int read;
|
||||||
byte[] buf = new byte[8192];
|
byte[] buf = new byte[8192];
|
||||||
byte lastByte = -1;
|
byte lastByte = -1;
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.implementation
|
||||||
|
|
||||||
|
import org.bouncycastle.crypto.AsymmetricCipherKeyPair
|
||||||
|
import org.bouncycastle.openpgp.*
|
||||||
|
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory
|
||||||
|
import org.bouncycastle.openpgp.operator.*
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPBEDataDecryptorFactory
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPBEKeyEncryptionMethodGenerator
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyEncryptorBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyConverter
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator
|
||||||
|
import org.bouncycastle.openpgp.operator.bc.BcSessionKeyDataDecryptorFactory
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair
|
||||||
|
import org.pgpainless.algorithm.HashAlgorithm
|
||||||
|
import org.pgpainless.algorithm.PublicKeyAlgorithm
|
||||||
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||||
|
import org.pgpainless.util.Passphrase
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.security.KeyPair
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class BcImplementationFactory : ImplementationFactory() {
|
||||||
|
override val pgpDigestCalculatorProvider: BcPGPDigestCalculatorProvider = BcPGPDigestCalculatorProvider()
|
||||||
|
override val pgpContentVerifierBuilderProvider: BcPGPContentVerifierBuilderProvider = BcPGPContentVerifierBuilderProvider()
|
||||||
|
override val keyFingerprintCalculator: BcKeyFingerprintCalculator = BcKeyFingerprintCalculator()
|
||||||
|
|
||||||
|
override fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm,
|
||||||
|
digestCalculator: PGPDigestCalculator,
|
||||||
|
passphrase: Passphrase): PBESecretKeyEncryptor =
|
||||||
|
BcPBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.algorithmId, digestCalculator)
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm,
|
||||||
|
hashAlgorithm: HashAlgorithm,
|
||||||
|
s2kCount: Int,
|
||||||
|
passphrase: Passphrase): PBESecretKeyEncryptor =
|
||||||
|
BcPBESecretKeyEncryptorBuilder(
|
||||||
|
encryptionAlgorithm.algorithmId,
|
||||||
|
getPGPDigestCalculator(hashAlgorithm),
|
||||||
|
s2kCount)
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor =
|
||||||
|
BcPBESecretKeyDecryptorBuilder(pgpDigestCalculatorProvider)
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder =
|
||||||
|
BcPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm)
|
||||||
|
|
||||||
|
override fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory =
|
||||||
|
BcPBEDataDecryptorFactory(passphrase.getChars(), pgpDigestCalculatorProvider)
|
||||||
|
|
||||||
|
override fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory =
|
||||||
|
BcPublicKeyDataDecryptorFactory(privateKey)
|
||||||
|
|
||||||
|
override fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory =
|
||||||
|
BcSessionKeyDataDecryptorFactory(sessionKey)
|
||||||
|
|
||||||
|
override fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator =
|
||||||
|
BcPublicKeyKeyEncryptionMethodGenerator(key)
|
||||||
|
|
||||||
|
override fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator =
|
||||||
|
BcPBEKeyEncryptionMethodGenerator(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder =
|
||||||
|
BcPGPDataEncryptorBuilder(symmetricKeyAlgorithm)
|
||||||
|
|
||||||
|
override fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair =
|
||||||
|
BcPGPKeyPair(
|
||||||
|
publicKeyAlgorithm.algorithmId,
|
||||||
|
jceToBcKeyPair(publicKeyAlgorithm, keyPair, creationDate),
|
||||||
|
creationDate)
|
||||||
|
|
||||||
|
override fun getPGPObjectFactory(inputStream: InputStream): PGPObjectFactory = BcPGPObjectFactory(inputStream)
|
||||||
|
|
||||||
|
private fun jceToBcKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm,
|
||||||
|
keyPair: KeyPair,
|
||||||
|
creationDate: Date): AsymmetricCipherKeyPair =
|
||||||
|
BcPGPKeyConverter().let { converter ->
|
||||||
|
JcaPGPKeyPair(publicKeyAlgorithm.algorithmId, keyPair, creationDate).let { pair ->
|
||||||
|
AsymmetricCipherKeyPair(converter.getPublicKey(pair.publicKey), converter.getPrivateKey(pair.privateKey))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.implementation
|
||||||
|
|
||||||
|
import org.bouncycastle.openpgp.*
|
||||||
|
import org.bouncycastle.openpgp.operator.*
|
||||||
|
import org.pgpainless.algorithm.HashAlgorithm
|
||||||
|
import org.pgpainless.algorithm.PublicKeyAlgorithm
|
||||||
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||||
|
import org.pgpainless.util.Passphrase
|
||||||
|
import org.pgpainless.util.SessionKey
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.security.KeyPair
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
abstract class ImplementationFactory {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
private var instance: ImplementationFactory = BcImplementationFactory()
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun getInstance() = instance
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun setFactoryImplementation(implementation: ImplementationFactory) = apply {
|
||||||
|
instance = implementation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract val pgpDigestCalculatorProvider: PGPDigestCalculatorProvider
|
||||||
|
abstract val pgpContentVerifierBuilderProvider: PGPContentVerifierBuilderProvider
|
||||||
|
abstract val keyFingerprintCalculator: KeyFingerPrintCalculator
|
||||||
|
|
||||||
|
val v4FingerprintCalculator: PGPDigestCalculator
|
||||||
|
get() = getPGPDigestCalculator(HashAlgorithm.SHA1)
|
||||||
|
|
||||||
|
@Throws(PGPException::class)
|
||||||
|
abstract fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm,
|
||||||
|
digestCalculator: PGPDigestCalculator,
|
||||||
|
passphrase: Passphrase): PBESecretKeyEncryptor
|
||||||
|
|
||||||
|
@Throws(PGPException::class)
|
||||||
|
abstract fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor
|
||||||
|
|
||||||
|
@Throws(PGPException::class)
|
||||||
|
abstract fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm, hashAlgorithm: HashAlgorithm,
|
||||||
|
s2kCount: Int, passphrase: Passphrase): PBESecretKeyEncryptor
|
||||||
|
|
||||||
|
fun getPGPDigestCalculator(hashAlgorithm: HashAlgorithm): PGPDigestCalculator =
|
||||||
|
getPGPDigestCalculator(hashAlgorithm.algorithmId)
|
||||||
|
|
||||||
|
fun getPGPDigestCalculator(hashAlgorithm: Int): PGPDigestCalculator =
|
||||||
|
pgpDigestCalculatorProvider.get(hashAlgorithm)
|
||||||
|
|
||||||
|
fun getPGPContentSignerBuilder(keyAlgorithm: PublicKeyAlgorithm, hashAlgorithm: HashAlgorithm): PGPContentSignerBuilder =
|
||||||
|
getPGPContentSignerBuilder(keyAlgorithm.algorithmId, hashAlgorithm.algorithmId)
|
||||||
|
|
||||||
|
abstract fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder
|
||||||
|
|
||||||
|
@Throws(PGPException::class)
|
||||||
|
abstract fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory
|
||||||
|
|
||||||
|
abstract fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory
|
||||||
|
|
||||||
|
fun getSessionKeyDataDecryptorFactory(sessionKey: SessionKey): SessionKeyDataDecryptorFactory =
|
||||||
|
getSessionKeyDataDecryptorFactory(PGPSessionKey(sessionKey.algorithm.algorithmId, sessionKey.key))
|
||||||
|
|
||||||
|
abstract fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory
|
||||||
|
|
||||||
|
abstract fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator
|
||||||
|
|
||||||
|
abstract fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator
|
||||||
|
|
||||||
|
fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: SymmetricKeyAlgorithm): PGPDataEncryptorBuilder =
|
||||||
|
getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.algorithmId)
|
||||||
|
|
||||||
|
abstract fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder
|
||||||
|
|
||||||
|
@Throws(PGPException::class)
|
||||||
|
abstract fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair
|
||||||
|
|
||||||
|
fun getPGPObjectFactory(bytes: ByteArray): PGPObjectFactory =
|
||||||
|
getPGPObjectFactory(bytes.inputStream())
|
||||||
|
|
||||||
|
abstract fun getPGPObjectFactory(inputStream: InputStream): PGPObjectFactory
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return javaClass.simpleName
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.implementation
|
||||||
|
|
||||||
|
import org.bouncycastle.openpgp.*
|
||||||
|
import org.bouncycastle.openpgp.operator.*
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator
|
||||||
|
import org.bouncycastle.openpgp.operator.jcajce.JceSessionKeyDataDecryptorFactoryBuilder
|
||||||
|
import org.pgpainless.algorithm.HashAlgorithm
|
||||||
|
import org.pgpainless.algorithm.PublicKeyAlgorithm
|
||||||
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||||
|
import org.pgpainless.provider.ProviderFactory
|
||||||
|
import org.pgpainless.util.Passphrase
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.security.KeyPair
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class JceImplementationFactory : ImplementationFactory() {
|
||||||
|
override val pgpDigestCalculatorProvider: PGPDigestCalculatorProvider =
|
||||||
|
JcaPGPDigestCalculatorProviderBuilder()
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build()
|
||||||
|
override val pgpContentVerifierBuilderProvider: PGPContentVerifierBuilderProvider =
|
||||||
|
JcaPGPContentVerifierBuilderProvider()
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
override val keyFingerprintCalculator: KeyFingerPrintCalculator =
|
||||||
|
JcaKeyFingerprintCalculator()
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
|
||||||
|
override fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm,
|
||||||
|
digestCalculator: PGPDigestCalculator,
|
||||||
|
passphrase: Passphrase): PBESecretKeyEncryptor =
|
||||||
|
JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.algorithmId, digestCalculator)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm,
|
||||||
|
hashAlgorithm: HashAlgorithm,
|
||||||
|
s2kCount: Int,
|
||||||
|
passphrase: Passphrase): PBESecretKeyEncryptor =
|
||||||
|
JcePBESecretKeyEncryptorBuilder(
|
||||||
|
encryptionAlgorithm.algorithmId,
|
||||||
|
getPGPDigestCalculator(hashAlgorithm),
|
||||||
|
s2kCount)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor =
|
||||||
|
JcePBESecretKeyDecryptorBuilder(pgpDigestCalculatorProvider)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder =
|
||||||
|
JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
|
||||||
|
override fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory =
|
||||||
|
JcePBEDataDecryptorFactoryBuilder(pgpDigestCalculatorProvider)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build(passphrase.getChars())
|
||||||
|
|
||||||
|
override fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory =
|
||||||
|
JcePublicKeyDataDecryptorFactoryBuilder()
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build(privateKey)
|
||||||
|
|
||||||
|
override fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory =
|
||||||
|
JceSessionKeyDataDecryptorFactoryBuilder()
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
.build(sessionKey)
|
||||||
|
|
||||||
|
override fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator =
|
||||||
|
JcePublicKeyKeyEncryptionMethodGenerator(key)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
|
||||||
|
override fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator =
|
||||||
|
JcePBEKeyEncryptionMethodGenerator(passphrase.getChars())
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
|
||||||
|
override fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder =
|
||||||
|
JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm)
|
||||||
|
.setProvider(ProviderFactory.getProvider())
|
||||||
|
|
||||||
|
override fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair =
|
||||||
|
JcaPGPKeyPair(publicKeyAlgorithm.algorithmId, keyPair, creationDate)
|
||||||
|
|
||||||
|
override fun getPGPObjectFactory(inputStream: InputStream): PGPObjectFactory =
|
||||||
|
PGPObjectFactory(inputStream, keyFingerprintCalculator)
|
||||||
|
}
|
|
@ -68,7 +68,7 @@ public class ThirdPartyCertificationSignatureBuilderTest {
|
||||||
assertFalse(exportable.isExportable());
|
assertFalse(exportable.isExportable());
|
||||||
|
|
||||||
// test sig correctness
|
// test sig correctness
|
||||||
certification.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), secretKeys.getPublicKey());
|
certification.init(ImplementationFactory.getInstance().getPgpContentVerifierBuilderProvider(), secretKeys.getPublicKey());
|
||||||
assertTrue(certification.verifyCertification("Bob", bobsPublicKeys.getPublicKey()));
|
assertTrue(certification.verifyCertification("Bob", bobsPublicKeys.getPublicKey()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue