1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 22:31:09 +01:00

Add isSignedOnly() to MessageInspector

This method can be used to determine, whether the message was created using gpg --sign --armor.
It will return false if the message is signed and encrypted, since we cannot determine signed status while the message is encrypted.
Fixes #188
This commit is contained in:
Paul Schaub 2021-10-01 14:12:10 +02:00
parent 8ec8a55f10
commit 5869996059
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 85 additions and 3 deletions

View file

@ -27,6 +27,7 @@ import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.pgpainless.implementation.ImplementationFactory;
@ -40,7 +41,12 @@ public final class MessageInspector {
public static class EncryptionInfo {
private final List<Long> keyIds = new ArrayList<>();
private boolean isPassphraseEncrypted = false;
private boolean isSignedOnly = false;
/**
* Return a list of recipient key ids for whom the message is encrypted.
* @return recipient key ids
*/
public List<Long> getKeyIds() {
return Collections.unmodifiableList(keyIds);
}
@ -48,6 +54,24 @@ public final class MessageInspector {
public boolean isPassphraseEncrypted() {
return isPassphraseEncrypted;
}
/**
* Return true, if the message is encrypted.
*
* @return true if encrypted
*/
public boolean isEncrypted() {
return isPassphraseEncrypted || !keyIds.isEmpty();
}
/**
* Return true, if the message is not encrypted, but signed using {@link org.bouncycastle.openpgp.PGPOnePassSignature OnePassSignatures}.
*
* @return true if message is signed only
*/
public boolean isSignedOnly() {
return isSignedOnly;
}
}
private MessageInspector() {
@ -67,16 +91,24 @@ public final class MessageInspector {
InputStream decoded = ArmorUtils.getDecoderStream(dataIn);
EncryptionInfo info = new EncryptionInfo();
collectDecryptionKeyIDs(decoded, info);
processMessage(decoded, info);
return info;
}
private static void collectDecryptionKeyIDs(InputStream dataIn, EncryptionInfo info) throws PGPException {
private static void processMessage(InputStream dataIn, EncryptionInfo info) throws PGPException {
PGPObjectFactory objectFactory = new PGPObjectFactory(dataIn,
ImplementationFactory.getInstance().getKeyFingerprintCalculator());
for (Object next : objectFactory) {
if (next instanceof PGPOnePassSignatureList) {
PGPOnePassSignatureList signatures = (PGPOnePassSignatureList) next;
if (!signatures.isEmpty()) {
info.isSignedOnly = true;
return;
}
}
if (next instanceof PGPEncryptedDataList) {
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) next;
for (PGPEncryptedData encryptedData : encryptedDataList) {
@ -92,7 +124,7 @@ public final class MessageInspector {
if (next instanceof PGPCompressedData) {
PGPCompressedData compressed = (PGPCompressedData) next;
InputStream decompressed = compressed.getDataStream();
collectDecryptionKeyIDs(decompressed, info);
processMessage(decompressed, info);
}
if (next instanceof PGPLiteralData) {