Make sop decrypt throw for unencrypted data

This commit is contained in:
Paul Schaub 2022-06-23 11:47:48 +02:00
parent efc0cb357b
commit 14531f0050
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 22 additions and 0 deletions

View File

@ -149,6 +149,10 @@ public class DecryptImpl implements Decrypt {
decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult();
if (!metadata.isEncrypted()) {
throw new SOPGPException.BadData("Data is not encrypted.");
}
List<Verification> verificationList = new ArrayList<>();
for (SignatureVerification signatureVerification : metadata.getVerifiedInbandSignatures()) {
verificationList.add(map(signatureVerification));

View File

@ -515,4 +515,22 @@ public class EncryptDecryptRoundTripTest {
assertThrows(SOPGPException.BadData.class, () ->
sop.decrypt().withSessionKey(wrongSessionKey).ciphertext(ciphertext));
}
@Test
public void decryptNonEncryptedDataFailsBadData() throws IOException {
byte[] signed = sop.inlineSign()
.key(aliceKey)
.withKeyPassword(alicePassword)
.data(message)
.getBytes();
assertThrows(SOPGPException.BadData.class, () ->
sop.decrypt()
.verifyWithCert(aliceCert)
.withKey(aliceKey)
.withKeyPassword(alicePassword)
.ciphertext(signed)
.toByteArrayAndResult()
);
}
}