1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-26 05:24:49 +02:00
pgpainless/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java

227 lines
9.2 KiB
Java
Raw Normal View History

/*
* Copyright 2018 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.decryption_verification;
2018-06-06 18:46:41 +02:00
import java.io.IOException;
import java.io.InputStream;
2020-08-24 14:55:06 +02:00
import java.util.ArrayList;
2021-04-26 13:38:12 +02:00
import java.util.Collections;
2018-06-06 18:46:41 +02:00
import java.util.HashSet;
import java.util.Iterator;
2020-08-24 14:55:06 +02:00
import java.util.List;
2018-06-06 18:46:41 +02:00
import java.util.Set;
2020-01-10 18:46:31 +01:00
import javax.annotation.Nonnull;
2021-04-26 13:38:12 +02:00
import org.bouncycastle.bcpg.MarkerPacket;
2020-08-24 14:55:06 +02:00
import org.bouncycastle.openpgp.PGPCompressedData;
2018-06-06 18:46:41 +02:00
import org.bouncycastle.openpgp.PGPException;
2020-08-24 14:55:06 +02:00
import org.bouncycastle.openpgp.PGPObjectFactory;
2018-06-06 18:46:41 +02:00
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
2021-04-26 13:38:12 +02:00
import org.bouncycastle.openpgp.PGPSecretKeyRing;
2018-06-06 18:46:41 +02:00
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
2020-08-24 14:55:06 +02:00
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
2020-12-27 01:56:18 +01:00
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.Passphrase;
2018-06-06 18:46:41 +02:00
public class DecryptionBuilder implements DecryptionBuilderInterface {
private InputStream inputStream;
private PGPSecretKeyRingCollection decryptionKeys;
2018-06-07 18:12:13 +02:00
private SecretKeyRingProtector decryptionKeyDecryptor;
private Passphrase decryptionPassphrase;
2020-08-24 14:55:06 +02:00
private List<PGPSignature> detachedSignatures;
2018-06-06 18:46:41 +02:00
private Set<PGPPublicKeyRing> verificationKeys = new HashSet<>();
private MissingPublicKeyCallback missingPublicKeyCallback = null;
2018-06-06 18:46:41 +02:00
2020-12-27 01:56:18 +01:00
private final KeyFingerPrintCalculator keyFingerPrintCalculator =
ImplementationFactory.getInstance().getKeyFingerprintCalculator();
2020-08-24 14:55:06 +02:00
2018-06-06 18:46:41 +02:00
@Override
public DecryptWith onInputStream(@Nonnull InputStream inputStream) {
2018-06-06 18:46:41 +02:00
this.inputStream = inputStream;
return new DecryptWithImpl();
}
class DecryptWithImpl implements DecryptWith {
@Override
2020-08-24 14:55:06 +02:00
public Verify decryptWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection secretKeyRings) {
2018-06-06 18:46:41 +02:00
DecryptionBuilder.this.decryptionKeys = secretKeyRings;
DecryptionBuilder.this.decryptionKeyDecryptor = decryptor;
2020-08-24 14:55:06 +02:00
return new VerifyImpl();
2018-06-06 18:46:41 +02:00
}
2021-04-26 13:38:12 +02:00
@Override
public Verify decryptWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing secretKeyRing) throws PGPException, IOException {
DecryptionBuilder.this.decryptionKeys = new PGPSecretKeyRingCollection(Collections.singleton(secretKeyRing));
DecryptionBuilder.this.decryptionKeyDecryptor = decryptor;
return new VerifyImpl();
}
@Override
public Verify decryptWith(@Nonnull Passphrase passphrase) {
if (passphrase.isEmpty()) {
throw new IllegalArgumentException("Passphrase MUST NOT be empty.");
}
DecryptionBuilder.this.decryptionPassphrase = passphrase;
return new VerifyImpl();
}
2018-06-06 18:46:41 +02:00
@Override
2020-08-24 14:55:06 +02:00
public Verify doNotDecrypt() {
2018-06-06 18:46:41 +02:00
DecryptionBuilder.this.decryptionKeys = null;
DecryptionBuilder.this.decryptionKeyDecryptor = null;
2020-08-24 14:55:06 +02:00
return new VerifyImpl();
}
}
class VerifyImpl implements Verify {
@Override
public VerifyWith verifyDetachedSignature(@Nonnull InputStream inputStream) throws IOException, PGPException {
2020-08-24 14:55:06 +02:00
List<PGPSignature> signatures = new ArrayList<>();
InputStream pgpIn = PGPUtil.getDecoderStream(inputStream);
PGPObjectFactory objectFactory = new PGPObjectFactory(
pgpIn, keyFingerPrintCalculator);
Object nextObject = objectFactory.nextObject();
while (nextObject != null) {
2021-04-26 13:38:12 +02:00
if (nextObject instanceof MarkerPacket) {
nextObject = objectFactory.nextObject();
continue;
}
2020-08-24 14:55:06 +02:00
if (nextObject instanceof PGPCompressedData) {
PGPCompressedData compressedData = (PGPCompressedData) nextObject;
objectFactory = new PGPObjectFactory(compressedData.getDataStream(), keyFingerPrintCalculator);
nextObject = objectFactory.nextObject();
continue;
}
if (nextObject instanceof PGPSignatureList) {
PGPSignatureList signatureList = (PGPSignatureList) nextObject;
for (PGPSignature s : signatureList) {
signatures.add(s);
}
}
if (nextObject instanceof PGPSignature) {
signatures.add((PGPSignature) nextObject);
}
nextObject = objectFactory.nextObject();
}
pgpIn.close();
return verifyDetachedSignatures(signatures);
}
@Override
public VerifyWith verifyDetachedSignatures(@Nonnull List<PGPSignature> signatures) {
2020-08-24 14:55:06 +02:00
DecryptionBuilder.this.detachedSignatures = signatures;
2018-06-06 18:46:41 +02:00
return new VerifyWithImpl();
}
2020-08-24 14:55:06 +02:00
@Override
public HandleMissingPublicKeys verifyWith(@Nonnull PGPPublicKeyRingCollection publicKeyRings) {
2020-08-24 14:55:06 +02:00
return new VerifyWithImpl().verifyWith(publicKeyRings);
}
@Override
public HandleMissingPublicKeys verifyWith(@Nonnull Set<OpenPgpV4Fingerprint> trustedFingerprints,
@Nonnull PGPPublicKeyRingCollection publicKeyRings) {
2020-08-24 14:55:06 +02:00
return new VerifyWithImpl().verifyWith(trustedFingerprints, publicKeyRings);
}
@Override
public HandleMissingPublicKeys verifyWith(@Nonnull Set<PGPPublicKeyRing> publicKeyRings) {
2020-08-24 14:55:06 +02:00
return new VerifyWithImpl().verifyWith(publicKeyRings);
}
@Override
public Build doNotVerify() {
DecryptionBuilder.this.verificationKeys = null;
return new BuildImpl();
}
2018-06-06 18:46:41 +02:00
}
class VerifyWithImpl implements VerifyWith {
2018-07-08 18:17:24 +02:00
@Override
public HandleMissingPublicKeys verifyWith(@Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) {
2018-07-08 18:17:24 +02:00
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
publicKeyRings.add(i.next());
}
return verifyWith(publicKeyRings);
}
2018-06-06 18:46:41 +02:00
@Override
public HandleMissingPublicKeys verifyWith(@Nonnull Set<OpenPgpV4Fingerprint> trustedKeyIds,
@Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) {
Set<PGPPublicKeyRing> publicKeyRings = keyRingCollectionToSet(publicKeyRingCollection);
2020-01-10 18:42:39 +01:00
removeUntrustedPublicKeys(publicKeyRings, trustedKeyIds);
return verifyWith(publicKeyRings);
}
2020-01-10 18:42:39 +01:00
private void removeUntrustedPublicKeys(Set<PGPPublicKeyRing> publicKeyRings, Set<OpenPgpV4Fingerprint> trustedKeyIds) {
for (PGPPublicKeyRing p : new HashSet<>(publicKeyRings)) {
if (!trustedKeyIds.contains(new OpenPgpV4Fingerprint(p))) {
publicKeyRings.remove(p);
}
}
}
private Set<PGPPublicKeyRing> keyRingCollectionToSet(PGPPublicKeyRingCollection publicKeyRingCollection) {
2018-06-06 18:46:41 +02:00
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
publicKeyRings.add(i.next());
2018-06-06 18:46:41 +02:00
}
return publicKeyRings;
2018-06-06 18:46:41 +02:00
}
@Override
public HandleMissingPublicKeys verifyWith(@Nonnull Set<PGPPublicKeyRing> publicKeyRings) {
2018-06-06 18:46:41 +02:00
DecryptionBuilder.this.verificationKeys = publicKeyRings;
2018-07-02 20:46:27 +02:00
return new HandleMissingPublicKeysImpl();
2018-06-06 18:46:41 +02:00
}
}
2018-07-02 20:46:27 +02:00
class HandleMissingPublicKeysImpl implements HandleMissingPublicKeys {
2018-06-06 18:46:41 +02:00
@Override
public Build handleMissingPublicKeysWith(@Nonnull MissingPublicKeyCallback callback) {
2018-06-06 18:46:41 +02:00
DecryptionBuilder.this.missingPublicKeyCallback = callback;
return new BuildImpl();
}
@Override
public Build ignoreMissingPublicKeys() {
2018-07-02 20:46:27 +02:00
DecryptionBuilder.this.missingPublicKeyCallback = null;
2018-06-06 18:46:41 +02:00
return new BuildImpl();
}
}
class BuildImpl implements Build {
@Override
2018-06-11 01:33:49 +02:00
public DecryptionStream build() throws IOException, PGPException {
2021-04-26 13:38:12 +02:00
return DecryptionStreamFactory.create(inputStream, decryptionKeys, decryptionKeyDecryptor,
decryptionPassphrase, detachedSignatures, verificationKeys, missingPublicKeyCallback);
2018-06-06 18:46:41 +02:00
}
}
}