1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 06:11:08 +01:00

Check MDC when stream is closed

This commit is contained in:
Paul Schaub 2021-02-17 21:04:05 +01:00
parent d2a581de9b
commit ea89289852
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 289 additions and 5 deletions

View file

@ -18,10 +18,12 @@ package org.pgpainless.decryption_verification;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.openpgp.PGPException;
import org.pgpainless.util.IntegrityProtectedInputStream;
public class DecryptionStream extends InputStream {
@ -30,10 +32,13 @@ public class DecryptionStream extends InputStream {
private final InputStream inputStream;
private final OpenPgpMetadata.Builder resultBuilder;
private boolean isClosed = false;
private List<IntegrityProtectedInputStream> integrityProtectedInputStreamList;
DecryptionStream(@Nonnull InputStream wrapped, @Nonnull OpenPgpMetadata.Builder resultBuilder) {
DecryptionStream(@Nonnull InputStream wrapped, @Nonnull OpenPgpMetadata.Builder resultBuilder,
List<IntegrityProtectedInputStream> integrityProtectedInputStreamList) {
this.inputStream = wrapped;
this.resultBuilder = resultBuilder;
this.integrityProtectedInputStreamList = integrityProtectedInputStreamList;
}
@Override
@ -55,6 +60,9 @@ public class DecryptionStream extends InputStream {
public void close() throws IOException {
inputStream.close();
maybeVerifyDetachedSignatures();
for (IntegrityProtectedInputStream s : integrityProtectedInputStreamList) {
s.close();
}
this.isClosed = true;
}

View file

@ -17,6 +17,7 @@ package org.pgpainless.decryption_verification;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -56,6 +57,7 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.IntegrityProtectedInputStream;
import org.pgpainless.util.Passphrase;
public final class DecryptionStreamFactory {
@ -73,6 +75,7 @@ public final class DecryptionStreamFactory {
private static final PGPContentVerifierBuilderProvider verifierBuilderProvider = ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider();
private static final KeyFingerPrintCalculator keyFingerprintCalculator = ImplementationFactory.getInstance().getKeyFingerprintCalculator();
private final Map<OpenPgpV4Fingerprint, OnePassSignature> verifiableOnePassSignatures = new HashMap<>();
private final List<IntegrityProtectedInputStream> integrityProtectedStreams = new ArrayList<>();
private DecryptionStreamFactory(@Nullable PGPSecretKeyRingCollection decryptionKeys,
@Nullable SecretKeyRingProtector decryptor,
@ -114,7 +117,7 @@ public final class DecryptionStreamFactory {
PGPUtil.getDecoderStream(inputStream), keyFingerprintCalculator);
pgpInputStream = factory.processPGPPackets(objectFactory);
}
return new DecryptionStream(pgpInputStream, factory.resultBuilder);
return new DecryptionStream(pgpInputStream, factory.resultBuilder, factory.integrityProtectedStreams);
}
private InputStream processPGPPackets(@Nonnull PGPObjectFactory objectFactory) throws IOException, PGPException {
@ -253,11 +256,11 @@ public final class DecryptionStreamFactory {
throw new PGPException("Decryption failed - No suitable decryption key or passphrase found");
}
PublicKeyDataDecryptorFactory keyDecryptor = ImplementationFactory.getInstance()
PublicKeyDataDecryptorFactory dataDecryptor = ImplementationFactory.getInstance()
.getPublicKeyDataDecryptorFactory(decryptionKey);
SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm
.fromId(encryptedSessionKey.getSymmetricAlgorithm(keyDecryptor));
.fromId(encryptedSessionKey.getSymmetricAlgorithm(dataDecryptor));
if (symmetricKeyAlgorithm == SymmetricKeyAlgorithm.NULL) {
throw new PGPException("Data is not encrypted.");
}
@ -267,7 +270,13 @@ public final class DecryptionStreamFactory {
resultBuilder.setIntegrityProtected(encryptedSessionKey.isIntegrityProtected());
return encryptedSessionKey.getDataStream(keyDecryptor);
if (encryptedSessionKey.isIntegrityProtected()) {
IntegrityProtectedInputStream integrityProtected =
new IntegrityProtectedInputStream(encryptedSessionKey.getDataStream(dataDecryptor), encryptedSessionKey);
integrityProtectedStreams.add(integrityProtected);
return integrityProtected;
}
return encryptedSessionKey.getDataStream(dataDecryptor);
}
private void initOnePassSignatures(@Nonnull PGPOnePassSignatureList onePassSignatureList) throws PGPException {

View file

@ -0,0 +1,51 @@
/*
* Copyright 2021 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.util;
import java.io.IOException;
import java.io.InputStream;
import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPException;
public class IntegrityProtectedInputStream extends InputStream {
private final InputStream inputStream;
private final PGPEncryptedData encryptedData;
public IntegrityProtectedInputStream(InputStream inputStream, PGPEncryptedData encryptedData) {
this.inputStream = inputStream;
this.encryptedData = encryptedData;
}
@Override
public int read() throws IOException {
return inputStream.read();
}
@Override
public void close() throws IOException {
if (encryptedData.isIntegrityProtected()) {
try {
if (!encryptedData.verify()) {
throw new PGPException("Modification Detection failed.");
}
} catch (PGPException e) {
throw new IOException(e);
}
}
}
}