2018-06-13 17:26:48 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.decryption_verification;
|
2018-06-04 14:50:09 +02:00
|
|
|
|
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;
|
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;
|
2018-06-04 14:50:09 +02:00
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
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;
|
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<>();
|
2018-07-18 18:23:06 +02:00
|
|
|
private MissingPublicKeyCallback missingPublicKeyCallback = null;
|
2018-06-06 18:46:41 +02:00
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
private final KeyFingerPrintCalculator keyFingerPrintCalculator = new BcKeyFingerprintCalculator();
|
|
|
|
|
2018-06-06 18:46:41 +02:00
|
|
|
@Override
|
|
|
|
public DecryptWith onInputStream(InputStream inputStream) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@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(InputStream inputStream) throws IOException, PGPException {
|
|
|
|
List<PGPSignature> signatures = new ArrayList<>();
|
|
|
|
InputStream pgpIn = PGPUtil.getDecoderStream(inputStream);
|
|
|
|
PGPObjectFactory objectFactory = new PGPObjectFactory(
|
|
|
|
pgpIn, keyFingerPrintCalculator);
|
|
|
|
Object nextObject = objectFactory.nextObject();
|
|
|
|
while (nextObject != null) {
|
|
|
|
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(List<PGPSignature> signatures) {
|
|
|
|
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(@org.jetbrains.annotations.NotNull PGPPublicKeyRingCollection publicKeyRings) {
|
|
|
|
return new VerifyWithImpl().verifyWith(publicKeyRings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public HandleMissingPublicKeys verifyWith(@org.jetbrains.annotations.NotNull Set<OpenPgpV4Fingerprint> trustedFingerprints, @org.jetbrains.annotations.NotNull PGPPublicKeyRingCollection publicKeyRings) {
|
|
|
|
return new VerifyWithImpl().verifyWith(trustedFingerprints, publicKeyRings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public HandleMissingPublicKeys verifyWith(@org.jetbrains.annotations.NotNull Set<PGPPublicKeyRing> publicKeyRings) {
|
|
|
|
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
|
2018-07-31 20:09:16 +02:00
|
|
|
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
|
2018-07-31 20:09:16 +02:00
|
|
|
public HandleMissingPublicKeys verifyWith(@Nonnull Set<OpenPgpV4Fingerprint> trustedKeyIds,
|
|
|
|
@Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) {
|
2020-01-10 15:12:04 +01:00
|
|
|
Set<PGPPublicKeyRing> publicKeyRings = keyRingCollectionToSet(publicKeyRingCollection);
|
2020-01-10 18:42:39 +01:00
|
|
|
removeUntrustedPublicKeys(publicKeyRings, trustedKeyIds);
|
2020-01-10 15:12:04 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
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(); ) {
|
2020-01-10 15:12:04 +01:00
|
|
|
publicKeyRings.add(i.next());
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
2020-01-10 15:12:04 +01:00
|
|
|
return publicKeyRings;
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-31 20:09:16 +02:00
|
|
|
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
|
2018-07-31 20:09:16 +02:00
|
|
|
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 {
|
2018-06-10 17:55:51 +02:00
|
|
|
return DecryptionStreamFactory.create(inputStream,
|
2020-08-24 14:55:06 +02:00
|
|
|
decryptionKeys, decryptionKeyDecryptor, detachedSignatures, verificationKeys, missingPublicKeyCallback);
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-04 14:50:09 +02:00
|
|
|
}
|