mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 22:32:07 +01:00
Further sourcing of PGPObjectFactory from ImplementationProvider
This commit is contained in:
parent
60f7a9d9ec
commit
a66b45c3d2
9 changed files with 35 additions and 49 deletions
|
@ -37,7 +37,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|||
import org.bouncycastle.openpgp.PGPSessionKey;
|
||||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.bouncycastle.openpgp.PGPUtil;
|
||||
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
||||
import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory;
|
||||
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
|
||||
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
|
||||
|
@ -59,9 +58,9 @@ import org.pgpainless.key.SubkeyIdentifier;
|
|||
import org.pgpainless.key.info.KeyRingInfo;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.UnlockSecretKey;
|
||||
import org.pgpainless.signature.SignatureUtils;
|
||||
import org.pgpainless.signature.consumer.DetachedSignatureCheck;
|
||||
import org.pgpainless.signature.consumer.OnePassSignatureCheck;
|
||||
import org.pgpainless.signature.SignatureUtils;
|
||||
import org.pgpainless.util.CRCingArmoredInputStreamWrapper;
|
||||
import org.pgpainless.util.PGPUtilWrapper;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
@ -85,8 +84,6 @@ public final class DecryptionStreamFactory {
|
|||
|
||||
private static final PGPContentVerifierBuilderProvider verifierBuilderProvider =
|
||||
ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider();
|
||||
private static final KeyFingerPrintCalculator keyFingerprintCalculator =
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator();
|
||||
private IntegrityProtectedInputStream integrityProtectedEncryptedInputStream;
|
||||
|
||||
|
||||
|
@ -150,7 +147,7 @@ public final class DecryptionStreamFactory {
|
|||
}
|
||||
}
|
||||
|
||||
objectFactory = new PGPObjectFactory(decoderStream, keyFingerprintCalculator);
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decoderStream);
|
||||
// Parse OpenPGP message
|
||||
inputStream = processPGPPackets(objectFactory, 1);
|
||||
} catch (EOFException e) {
|
||||
|
@ -162,7 +159,7 @@ public final class DecryptionStreamFactory {
|
|||
LOGGER.debug("The message appears to not be an OpenPGP message. This is probably data signed with detached signatures?");
|
||||
bufferedIn.reset();
|
||||
decoderStream = bufferedIn;
|
||||
objectFactory = new PGPObjectFactory(decoderStream, keyFingerprintCalculator);
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decoderStream);
|
||||
inputStream = wrapInVerifySignatureStream(bufferedIn, objectFactory);
|
||||
} catch (IOException e) {
|
||||
if (e.getMessage().contains("invalid armor") || e.getMessage().contains("invalid header encountered")) {
|
||||
|
@ -170,7 +167,7 @@ public final class DecryptionStreamFactory {
|
|||
LOGGER.debug("The message is apparently not armored.");
|
||||
bufferedIn.reset();
|
||||
decoderStream = bufferedIn;
|
||||
objectFactory = new PGPObjectFactory(decoderStream, keyFingerprintCalculator);
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decoderStream);
|
||||
inputStream = wrapInVerifySignatureStream(bufferedIn, objectFactory);
|
||||
} else {
|
||||
throw e;
|
||||
|
@ -219,13 +216,13 @@ public final class DecryptionStreamFactory {
|
|||
if (sessionKey != null) {
|
||||
integrityProtectedEncryptedInputStream = decryptWithProvidedSessionKey(pgpEncryptedDataList, sessionKey);
|
||||
InputStream decodedDataStream = PGPUtil.getDecoderStream(integrityProtectedEncryptedInputStream);
|
||||
PGPObjectFactory factory = new PGPObjectFactory(decodedDataStream, keyFingerprintCalculator);
|
||||
PGPObjectFactory factory = ImplementationFactory.getInstance().getPGPObjectFactory(decodedDataStream);
|
||||
return processPGPPackets(factory, ++depth);
|
||||
}
|
||||
|
||||
InputStream decryptedDataStream = decryptSessionKey(pgpEncryptedDataList);
|
||||
InputStream decodedDataStream = PGPUtil.getDecoderStream(decryptedDataStream);
|
||||
PGPObjectFactory factory = new PGPObjectFactory(decodedDataStream, keyFingerprintCalculator);
|
||||
PGPObjectFactory factory = ImplementationFactory.getInstance().getPGPObjectFactory(decodedDataStream);
|
||||
return processPGPPackets(factory, ++depth);
|
||||
}
|
||||
|
||||
|
@ -269,7 +266,7 @@ public final class DecryptionStreamFactory {
|
|||
|
||||
InputStream inflatedDataStream = pgpCompressedData.getDataStream();
|
||||
InputStream decodedDataStream = PGPUtil.getDecoderStream(inflatedDataStream);
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(decodedDataStream, keyFingerprintCalculator);
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decodedDataStream);
|
||||
|
||||
return processPGPPackets(objectFactory, ++depth);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import org.bouncycastle.openpgp.PGPOnePassSignatureList;
|
|||
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
|
||||
import org.bouncycastle.openpgp.PGPUtil;
|
||||
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
||||
import org.pgpainless.implementation.ImplementationFactory;
|
||||
import org.pgpainless.util.ArmorUtils;
|
||||
|
||||
|
@ -101,8 +100,7 @@ public final class MessageInspector {
|
|||
}
|
||||
|
||||
private static void processMessage(InputStream dataIn, EncryptionInfo info) throws PGPException, IOException {
|
||||
KeyFingerPrintCalculator calculator = ImplementationFactory.getInstance().getKeyFingerprintCalculator();
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(dataIn, calculator);
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(dataIn);
|
||||
|
||||
Object next;
|
||||
while ((next = objectFactory.nextObject()) != null) {
|
||||
|
@ -131,7 +129,8 @@ public final class MessageInspector {
|
|||
if (next instanceof PGPCompressedData) {
|
||||
PGPCompressedData compressed = (PGPCompressedData) next;
|
||||
InputStream decompressed = compressed.getDataStream();
|
||||
objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(decompressed), calculator);
|
||||
InputStream decoded = PGPUtil.getDecoderStream(decompressed);
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decoded);
|
||||
}
|
||||
|
||||
if (next instanceof PGPLiteralData) {
|
||||
|
|
|
@ -71,7 +71,7 @@ public final class ClearsignedMessageUtil {
|
|||
out.close();
|
||||
}
|
||||
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(in, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(in);
|
||||
PGPSignatureList signatures = (PGPSignatureList) objectFactory.nextObject();
|
||||
|
||||
return signatures;
|
||||
|
|
|
@ -47,7 +47,7 @@ public class PGPKeyRingCollection {
|
|||
public PGPKeyRingCollection(@Nonnull InputStream in, boolean isSilent) throws IOException, PGPException {
|
||||
// Double getDecoderStream because of #96
|
||||
InputStream decoderStream = ArmorUtils.getDecoderStream(in);
|
||||
PGPObjectFactory pgpFact = new PGPObjectFactory(decoderStream, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory pgpFact = ImplementationFactory.getInstance().getPGPObjectFactory(decoderStream);
|
||||
Object obj;
|
||||
|
||||
List<PGPSecretKeyRing> secretKeyRings = new ArrayList<>();
|
||||
|
|
|
@ -108,9 +108,8 @@ public class KeyRingReader {
|
|||
* @return public key ring
|
||||
*/
|
||||
public static PGPPublicKeyRing readPublicKeyRing(@Nonnull InputStream inputStream, int maxIterations) throws IOException {
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(
|
||||
ArmorUtils.getDecoderStream(inputStream),
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(
|
||||
ArmorUtils.getDecoderStream(inputStream));
|
||||
int i = 0;
|
||||
Object next;
|
||||
do {
|
||||
|
@ -146,9 +145,8 @@ public class KeyRingReader {
|
|||
*/
|
||||
public static PGPPublicKeyRingCollection readPublicKeyRingCollection(@Nonnull InputStream inputStream, int maxIterations)
|
||||
throws IOException, PGPException {
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(
|
||||
ArmorUtils.getDecoderStream(inputStream),
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(
|
||||
ArmorUtils.getDecoderStream(inputStream));
|
||||
|
||||
List<PGPPublicKeyRing> rings = new ArrayList<>();
|
||||
int i = 0;
|
||||
|
@ -191,9 +189,7 @@ public class KeyRingReader {
|
|||
*/
|
||||
public static PGPSecretKeyRing readSecretKeyRing(@Nonnull InputStream inputStream, int maxIterations) throws IOException {
|
||||
InputStream decoderStream = ArmorUtils.getDecoderStream(inputStream);
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(
|
||||
decoderStream,
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decoderStream);
|
||||
int i = 0;
|
||||
Object next;
|
||||
do {
|
||||
|
@ -230,9 +226,8 @@ public class KeyRingReader {
|
|||
public static PGPSecretKeyRingCollection readSecretKeyRingCollection(@Nonnull InputStream inputStream,
|
||||
int maxIterations)
|
||||
throws IOException, PGPException {
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(
|
||||
ArmorUtils.getDecoderStream(inputStream),
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(
|
||||
ArmorUtils.getDecoderStream(inputStream));
|
||||
|
||||
List<PGPSecretKeyRing> rings = new ArrayList<>();
|
||||
int i = 0;
|
||||
|
|
|
@ -238,16 +238,14 @@ public final class SignatureUtils {
|
|||
public static List<PGPSignature> readSignatures(InputStream inputStream, int maxIterations) throws IOException, PGPException {
|
||||
List<PGPSignature> signatures = new ArrayList<>();
|
||||
InputStream pgpIn = ArmorUtils.getDecoderStream(inputStream);
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(
|
||||
pgpIn, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(pgpIn);
|
||||
|
||||
int i = 0;
|
||||
Object nextObject;
|
||||
while (i++ < maxIterations && (nextObject = objectFactory.nextObject()) != null) {
|
||||
if (nextObject instanceof PGPCompressedData) {
|
||||
PGPCompressedData compressedData = (PGPCompressedData) nextObject;
|
||||
objectFactory = new PGPObjectFactory(compressedData.getDataStream(),
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(compressedData.getDataStream());
|
||||
}
|
||||
|
||||
if (nextObject instanceof PGPSignatureList) {
|
||||
|
|
|
@ -18,15 +18,19 @@ import java.util.Date;
|
|||
import org.bouncycastle.openpgp.PGPLiteralData;
|
||||
import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
|
||||
import org.bouncycastle.openpgp.PGPObjectFactory;
|
||||
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.pgpainless.implementation.ImplementationFactory;
|
||||
import org.pgpainless.util.PGPUtilWrapper;
|
||||
|
||||
public class PGPUtilWrapperTest {
|
||||
|
||||
@Test
|
||||
public void testGetDecoderStream() throws IOException {
|
||||
@ParameterizedTest
|
||||
@MethodSource("org.pgpainless.util.TestImplementationFactoryProvider#provideImplementationFactories")
|
||||
public void testGetDecoderStream(ImplementationFactory implementationFactory) throws IOException {
|
||||
ImplementationFactory.setFactoryImplementation(implementationFactory);
|
||||
|
||||
ByteArrayInputStream msg = new ByteArrayInputStream("Foo\nBar".getBytes(StandardCharsets.UTF_8));
|
||||
PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
|
||||
|
||||
|
@ -36,7 +40,7 @@ public class PGPUtilWrapperTest {
|
|||
literalDataGenerator.close();
|
||||
|
||||
InputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
PGPObjectFactory objectFactory = new BcPGPObjectFactory(in);
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(in);
|
||||
PGPLiteralData literalData = (PGPLiteralData) objectFactory.nextObject();
|
||||
InputStream litIn = literalData.getDataStream();
|
||||
BufferedInputStream bufIn = new BufferedInputStream(litIn);
|
||||
|
|
|
@ -19,9 +19,6 @@ import org.bouncycastle.openpgp.PGPException;
|
|||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
|
||||
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
|
||||
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder;
|
||||
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
@ -168,14 +165,12 @@ public class ChangeSecretKeyRingPassphraseTest {
|
|||
* @throws PGPException if passphrase is wrong
|
||||
*/
|
||||
private void extractPrivateKey(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException {
|
||||
PGPDigestCalculatorProvider digestCalculatorProvider = new BcPGPDigestCalculatorProvider();
|
||||
if (passphrase.isEmpty() && secretKey.getKeyEncryptionAlgorithm() != SymmetricKeyAlgorithm.NULL.getAlgorithmId()) {
|
||||
throw new PGPException("Cannot unlock encrypted private key with empty passphrase.");
|
||||
} else if (!passphrase.isEmpty() && secretKey.getKeyEncryptionAlgorithm() == SymmetricKeyAlgorithm.NULL.getAlgorithmId()) {
|
||||
throw new PGPException("Cannot unlock unprotected private key with non-empty passphrase.");
|
||||
}
|
||||
PBESecretKeyDecryptor decryptor = passphrase.isEmpty() ? null : new BcPBESecretKeyDecryptorBuilder(digestCalculatorProvider)
|
||||
.build(passphrase.getChars());
|
||||
PBESecretKeyDecryptor decryptor = passphrase.isEmpty() ? null : ImplementationFactory.getInstance().getPBESecretKeyDecryptor(passphrase);
|
||||
|
||||
UnlockSecretKey.unlockSecretKey(secretKey, decryptor);
|
||||
}
|
||||
|
|
|
@ -31,9 +31,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.bouncycastle.openpgp.PGPSignatureList;
|
||||
import org.bouncycastle.openpgp.PGPUtil;
|
||||
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
|
||||
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
|
||||
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
@ -79,7 +77,7 @@ public class OnePassSignatureBracketingTest {
|
|||
ByteArrayInputStream ciphertextIn = new ByteArrayInputStream(out.toByteArray());
|
||||
|
||||
InputStream inputStream = PGPUtil.getDecoderStream(ciphertextIn);
|
||||
PGPObjectFactory objectFactory = new BcPGPObjectFactory(inputStream);
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(inputStream);
|
||||
|
||||
PGPOnePassSignatureList onePassSignatures = null;
|
||||
PGPSignatureList signatures = null;
|
||||
|
@ -96,9 +94,9 @@ public class OnePassSignatureBracketingTest {
|
|||
PGPPublicKeyEncryptedData publicKeyEncryptedData = (PGPPublicKeyEncryptedData) encryptedData;
|
||||
PGPSecretKey secretKey = key1.getSecretKey(publicKeyEncryptedData.getKeyID());
|
||||
PGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(secretKey, SecretKeyRingProtector.unprotectedKeys());
|
||||
PublicKeyDataDecryptorFactory decryptorFactory = new BcPublicKeyDataDecryptorFactory(privateKey);
|
||||
PublicKeyDataDecryptorFactory decryptorFactory = ImplementationFactory.getInstance().getPublicKeyDataDecryptorFactory(privateKey);
|
||||
InputStream decryptionStream = publicKeyEncryptedData.getDataStream(decryptorFactory);
|
||||
objectFactory = new BcPGPObjectFactory(decryptionStream);
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decryptionStream);
|
||||
continue outerloop;
|
||||
}
|
||||
}
|
||||
|
@ -108,7 +106,7 @@ public class OnePassSignatureBracketingTest {
|
|||
} else if (next instanceof PGPCompressedData) {
|
||||
PGPCompressedData compressed = (PGPCompressedData) next;
|
||||
InputStream decompressor = compressed.getDataStream();
|
||||
objectFactory = new PGPObjectFactory(decompressor, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(decompressor);
|
||||
continue outerloop;
|
||||
} else if (next instanceof PGPLiteralData) {
|
||||
continue outerloop;
|
||||
|
|
Loading…
Reference in a new issue