Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double verification error #66 #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ final class MDCValidatingInputStream extends FilterInputStream {
private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory
.getLogger(MDCValidatingInputStream.class);

private final PGPPublicKeyEncryptedData pbe;

private boolean mdcChecked;

/**
* Creates a <code>MDCValidatingInputStream</code> by assigning the argument
* <code>inputStream</code> to the field <code>this.inputStream</code> and <code>pbe</code> to <code>this.pbe</code> so as to remember it for
Expand All @@ -21,9 +25,6 @@ final class MDCValidatingInputStream extends FilterInputStream {
* @param inputStream the underlying input stream
* @param pbe the pgp public key encrypted data to verify message integrity
*/

private final PGPPublicKeyEncryptedData pbe;

MDCValidatingInputStream(InputStream inputStream, PGPPublicKeyEncryptedData pbe) {
super(inputStream);
this.pbe = pbe;
Expand Down Expand Up @@ -61,6 +62,11 @@ public int read(@Nonnull byte[] b, int off, int len) throws IOException {
* @throws IOException Error while reading input stream or if MDC fails
*/
private void validateMDC() throws IOException {
if (mdcChecked) {
return;
}
mdcChecked = true;

try {
if (pbe.isIntegrityProtected()) {
if (!pbe.verify()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import static org.junit.Assume.assumeNotNull;
import static org.mockito.Mockito.mock;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfig;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.testtooling.Configs;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.testtooling.ExampleMessages;
Expand Down Expand Up @@ -144,6 +145,32 @@ public void decryptNoSignatureValidation_withUnsignedData_works() throws Excepti
}
}

@Test()
public void decryptNoSignatureValidation_withWrapperStream_works() throws Exception {

try (InputStream ciphertext = new ByteArrayInputStream(
ExampleMessages.IMPORTANT_QUOTE_NOT_SIGNED_NOT_COMPRESSED.getBytes(
"US-ASCII"))) {
final InputStream plaintextStream = BouncyGPG.decryptAndVerifyStream()
.withConfig(Configs.keyringConfigFromResourceForRecipient())
.andIgnoreSignatures()
.fromEncryptedInputStream(ciphertext);

final InputStream wrapperStream = new InputStream() {
@Override
public int read() throws IOException {
return plaintextStream.read();
}
};

final String plainText = inputStreamToText(wrapperStream);

assertThat(plainText, equalTo(ExampleMessages.IMPORTANT_QUOTE_TEXT));
plaintextStream.close();
wrapperStream.close();
}
}

@Test(expected = IOException.class)
public void decryptAndValidateSignature_withUnsignedData_throws() throws Exception {

Expand Down