1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 03:17:58 +01:00

Introduce VerificationHelper class and export signature mode in decrypt operation

This commit is contained in:
Paul Schaub 2023-04-18 18:53:50 +02:00
parent 06c924d41d
commit e3bacdbe35
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 57 additions and 53 deletions

View file

@ -129,7 +129,7 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest {
String romeosVerif = readStringFromFile(anotherVerificationsFile); String romeosVerif = readStringFromFile(anotherVerificationsFile);
assertEquals(julietsVerif, romeosVerif); assertEquals(julietsVerif, romeosVerif);
assertFalse(julietsVerif.isEmpty()); assertFalse(julietsVerif.isEmpty());
assertEquals(103, julietsVerif.length()); // 103 is number of symbols in [DATE, FINGER, FINGER] for V4 assertEquals(115, julietsVerif.length()); // 115 is number of symbols in [DATE, FINGER, FINGER, MODE] for V4
} }
@Test @Test
@ -274,7 +274,7 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest {
assertEquals(plaintext, out.toString()); assertEquals(plaintext, out.toString());
String verificationString = readStringFromFile(verifications); String verificationString = readStringFromFile(verifications);
assertEquals("2022-11-09T17:22:48Z C0DCEC44B1A173664B05DABCECD0BF863F65C9A5 A2EC077FC977E15DD799EFF92C0D3C123CF51C08\n", assertEquals("2022-11-09T17:22:48Z C0DCEC44B1A173664B05DABCECD0BF863F65C9A5 A2EC077FC977E15DD799EFF92C0D3C123CF51C08 mode:binary\n",
verificationString); verificationString);
} }

View file

@ -147,7 +147,7 @@ public class DecryptImpl implements Decrypt {
List<Verification> verificationList = new ArrayList<>(); List<Verification> verificationList = new ArrayList<>();
for (SignatureVerification signatureVerification : metadata.getVerifiedInlineSignatures()) { for (SignatureVerification signatureVerification : metadata.getVerifiedInlineSignatures()) {
verificationList.add(map(signatureVerification)); verificationList.add(VerificationHelper.mapVerification(signatureVerification));
} }
SessionKey sessionKey = null; SessionKey sessionKey = null;
@ -163,10 +163,4 @@ public class DecryptImpl implements Decrypt {
} }
}; };
} }
private Verification map(SignatureVerification sigVerification) {
return new Verification(sigVerification.getSignature().getCreationTime(),
sigVerification.getSigningKey().getSubkeyFingerprint().toString(),
sigVerification.getSigningKey().getPrimaryKeyFingerprint().toString());
}
} }

View file

@ -12,7 +12,6 @@ import java.util.List;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
@ -21,7 +20,6 @@ import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.decryption_verification.SignatureVerification; import org.pgpainless.decryption_verification.SignatureVerification;
import org.pgpainless.exception.MalformedOpenPgpMessageException; import org.pgpainless.exception.MalformedOpenPgpMessageException;
import sop.Verification; import sop.Verification;
import sop.enums.SignatureMode;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
import sop.operation.DetachedVerify; import sop.operation.DetachedVerify;
@ -78,7 +76,7 @@ public class DetachedVerifyImpl implements DetachedVerify {
List<Verification> verificationList = new ArrayList<>(); List<Verification> verificationList = new ArrayList<>();
for (SignatureVerification signatureVerification : metadata.getVerifiedDetachedSignatures()) { for (SignatureVerification signatureVerification : metadata.getVerifiedDetachedSignatures()) {
verificationList.add(map(signatureVerification)); verificationList.add(VerificationHelper.mapVerification(signatureVerification));
} }
if (!options.getCertificateSource().getExplicitCertificates().isEmpty()) { if (!options.getCertificateSource().getExplicitCertificates().isEmpty()) {
@ -92,23 +90,4 @@ public class DetachedVerifyImpl implements DetachedVerify {
throw new SOPGPException.BadData(e); throw new SOPGPException.BadData(e);
} }
} }
private Verification map(SignatureVerification sigVerification) {
return new Verification(sigVerification.getSignature().getCreationTime(),
sigVerification.getSigningKey().getSubkeyFingerprint().toString(),
sigVerification.getSigningKey().getPrimaryKeyFingerprint().toString(),
getMode(sigVerification.getSignature()),
null);
}
private static SignatureMode getMode(PGPSignature signature) {
if (signature.getSignatureType() == PGPSignature.BINARY_DOCUMENT) {
return SignatureMode.binary;
}
if (signature.getSignatureType() == PGPSignature.CANONICAL_TEXT_DOCUMENT) {
return SignatureMode.text;
}
return null;
}
} }

View file

@ -13,7 +13,6 @@ import java.util.List;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
@ -24,7 +23,6 @@ import org.pgpainless.exception.MalformedOpenPgpMessageException;
import org.pgpainless.exception.MissingDecryptionMethodException; import org.pgpainless.exception.MissingDecryptionMethodException;
import sop.ReadyWithResult; import sop.ReadyWithResult;
import sop.Verification; import sop.Verification;
import sop.enums.SignatureMode;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
import sop.operation.InlineVerify; import sop.operation.InlineVerify;
@ -76,7 +74,7 @@ public class InlineVerifyImpl implements InlineVerify {
metadata.getVerifiedInlineSignatures(); metadata.getVerifiedInlineSignatures();
for (SignatureVerification signatureVerification : verifications) { for (SignatureVerification signatureVerification : verifications) {
verificationList.add(map(signatureVerification)); verificationList.add(VerificationHelper.mapVerification(signatureVerification));
} }
if (!options.getCertificateSource().getExplicitCertificates().isEmpty()) { if (!options.getCertificateSource().getExplicitCertificates().isEmpty()) {
@ -94,23 +92,4 @@ public class InlineVerifyImpl implements InlineVerify {
} }
}; };
} }
private Verification map(SignatureVerification sigVerification) {
return new Verification(sigVerification.getSignature().getCreationTime(),
sigVerification.getSigningKey().getSubkeyFingerprint().toString(),
sigVerification.getSigningKey().getPrimaryKeyFingerprint().toString(),
getMode(sigVerification.getSignature()),
null);
}
private static SignatureMode getMode(PGPSignature signature) {
if (signature.getSignatureType() == PGPSignature.BINARY_DOCUMENT) {
return SignatureMode.binary;
}
if (signature.getSignatureType() == PGPSignature.CANONICAL_TEXT_DOCUMENT) {
return SignatureMode.text;
}
return null;
}
} }

View file

@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.sop;
import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.decryption_verification.SignatureVerification;
import sop.Verification;
import sop.enums.SignatureMode;
/**
* Helper class for shared methods related to {@link Verification Verifications}.
*/
public class VerificationHelper {
/**
* Map a {@link SignatureVerification} object to a {@link Verification}.
*
* @param sigVerification signature verification
* @return verification
*/
public static Verification mapVerification(SignatureVerification sigVerification) {
return new Verification(
sigVerification.getSignature().getCreationTime(),
sigVerification.getSigningKey().getSubkeyFingerprint().toString(),
sigVerification.getSigningKey().getPrimaryKeyFingerprint().toString(),
getMode(sigVerification.getSignature()),
null);
}
/**
* Map an OpenPGP signature type to a {@link SignatureMode} enum.
* Note: This method only maps {@link PGPSignature#BINARY_DOCUMENT} and {@link PGPSignature#CANONICAL_TEXT_DOCUMENT}.
* Other values are mapped to <pre>null</pre>.
*
* @param signature signature
* @return signature mode enum or null
*/
private static SignatureMode getMode(PGPSignature signature) {
if (signature.getSignatureType() == PGPSignature.BINARY_DOCUMENT) {
return SignatureMode.binary;
}
if (signature.getSignatureType() == PGPSignature.CANONICAL_TEXT_DOCUMENT) {
return SignatureMode.text;
}
return null;
}
}