1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 03:17:58 +01:00

Source PGPObjectFactory from ImplementationProvider

This commit is contained in:
Paul Schaub 2021-12-14 14:43:16 +01:00
parent 1681f3934f
commit 60f7a9d9ec
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 51 additions and 11 deletions

View file

@ -4,6 +4,7 @@
package org.pgpainless.implementation;
import java.io.InputStream;
import java.security.KeyPair;
import java.util.Date;
@ -11,10 +12,12 @@ 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.PGPSecretKey;
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;
@ -145,6 +148,16 @@ public class BcImplementationFactory extends ImplementationFactory {
return new BcSessionKeyDataDecryptorFactory(sessionKey);
}
@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 {

View file

@ -4,11 +4,13 @@
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.PGPSecretKey;
@ -107,6 +109,10 @@ public abstract class ImplementationFactory {
public abstract SessionKeyDataDecryptorFactory provideSessionKeyDataDecryptorFactory(PGPSessionKey sessionKey);
public abstract PGPObjectFactory getPGPObjectFactory(InputStream inputStream);
public abstract PGPObjectFactory getPGPObjectFactory(byte[] bytes);
@Override
public String toString() {
return getClass().getSimpleName();

View file

@ -4,15 +4,18 @@
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.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSessionKey;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
@ -132,4 +135,14 @@ public class JceImplementationFactory extends ImplementationFactory {
public SessionKeyDataDecryptorFactory provideSessionKeyDataDecryptorFactory(PGPSessionKey sessionKey) {
return new JceSessionKeyDataDecryptorFactoryBuilder().build(sessionKey);
}
@Override
public PGPObjectFactory getPGPObjectFactory(InputStream inputStream) {
return new JcaPGPObjectFactory(inputStream);
}
@Override
public PGPObjectFactory getPGPObjectFactory(byte[] bytes) {
return new JcaPGPObjectFactory(bytes);
}
}

View file

@ -12,8 +12,7 @@ import java.io.IOException;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.junit.jupiter.api.Test;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.implementation.ImplementationFactory;
@ -30,17 +29,21 @@ public class ImportExportKeyTest {
ImplementationFactory.setFactoryImplementation(implementationFactory);
PGPPublicKeyRing publicKeys = TestKeys.getJulietPublicKeyRing();
BcKeyFingerprintCalculator calc = new BcKeyFingerprintCalculator();
KeyFingerPrintCalculator calc = ImplementationFactory.getInstance().getKeyFingerprintCalculator();
byte[] bytes = publicKeys.getEncoded();
PGPPublicKeyRing parsed = new PGPPublicKeyRing(bytes, calc);
assertArrayEquals(publicKeys.getEncoded(), parsed.getEncoded());
}
@Test
public void testExportImportSecretKeyRing() throws IOException, PGPException {
@ParameterizedTest
@MethodSource("org.pgpainless.util.TestImplementationFactoryProvider#provideImplementationFactories")
public void testExportImportSecretKeyRing(ImplementationFactory implementationFactory) throws IOException, PGPException {
ImplementationFactory.setFactoryImplementation(implementationFactory);
PGPSecretKeyRing secretKeys = TestKeys.getRomeoSecretKeyRing();
KeyFingerPrintCalculator calc = ImplementationFactory.getInstance().getKeyFingerprintCalculator();
byte[] bytes = secretKeys.getEncoded();
PGPSecretKeyRing parsed = new PGPSecretKeyRing(bytes, new BcKeyFingerprintCalculator());
PGPSecretKeyRing parsed = new PGPSecretKeyRing(bytes, calc);
assertArrayEquals(secretKeys.getEncoded(), parsed.getEncoded());
assertEquals(secretKeys.getPublicKey().getKeyID(), parsed.getPublicKey().getKeyID());
}

View file

@ -23,11 +23,13 @@ import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.TestKeys;
public class ArmorUtilsTest {
@ -142,8 +144,10 @@ public class ArmorUtilsTest {
"-----END PGP MESSAGE-----\n", out.toString());
}
@Test
public void decodeExampleTest() throws IOException, PGPException {
@ParameterizedTest
@MethodSource("org.pgpainless.util.TestImplementationFactoryProvider#provideImplementationFactories")
public void decodeExampleTest(ImplementationFactory implementationFactory) throws IOException, PGPException {
ImplementationFactory.setFactoryImplementation(implementationFactory);
String armored = "-----BEGIN PGP MESSAGE-----\n" +
"Version: OpenPrivacy 0.99\n" +
"\n" +
@ -152,9 +156,10 @@ public class ArmorUtilsTest {
"=njUN\n" +
"-----END PGP MESSAGE-----";
InputStream inputStream = PGPUtil.getDecoderStream(new ByteArrayInputStream(armored.getBytes(StandardCharsets.UTF_8)));
PGPObjectFactory factory = new BcPGPObjectFactory(inputStream);
PGPObjectFactory factory = ImplementationFactory.getInstance().getPGPObjectFactory(inputStream);
PGPCompressedData compressed = (PGPCompressedData) factory.nextObject();
factory = new BcPGPObjectFactory(compressed.getDataStream());
factory = ImplementationFactory.getInstance().getPGPObjectFactory(compressed.getDataStream());
PGPLiteralData literal = (PGPLiteralData) factory.nextObject();
ByteArrayOutputStream out = new ByteArrayOutputStream();
assertEquals("_CONSOLE", literal.getFileName());