1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-18 02:12:06 +01:00

More SignatureSubpacketsUtilTest methods

This commit is contained in:
Paul Schaub 2021-10-04 14:00:23 +02:00
parent bccf384dbf
commit 96755a82a5
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -15,6 +15,7 @@
*/
package org.pgpainless.signature;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -30,8 +31,14 @@ import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.bouncycastle.bcpg.sig.Exportable;
import org.bouncycastle.bcpg.sig.IntendedRecipientFingerprint;
import org.bouncycastle.bcpg.sig.NotationData;
import org.bouncycastle.bcpg.sig.RevocationKey;
import org.bouncycastle.bcpg.sig.TrustSignature;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
@ -55,7 +62,7 @@ import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
public class SignatureSubpacketsUtilTest {
@Test
public void test() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
public void testGetKeyExpirationTimeAsDate() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.modernKeyRing("Expire", null);
Date expiration = Date.from(new Date().toInstant().plus(365, ChronoUnit.DAYS));
@ -153,6 +160,136 @@ public class SignatureSubpacketsUtilTest {
assertFalse(featureSet.contains(Feature.VERSION_5_PUBLIC_KEY));
}
@Test
public void getSignatureTargetIsNull() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignature withoutSignatureTarget = generator.generateCertification(secretKeys.getPublicKey());
assertNull(SignatureSubpacketsUtil.getSignatureTarget(withoutSignatureTarget));
}
@Test
public void testGetUnhashedNotationData() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignatureSubpacketGenerator unhashed = new PGPSignatureSubpacketGenerator();
unhashed.addNotationData(true, true, "test@notation.data", "notation-value");
unhashed.addNotationData(true, true, "test@notation.data", "another-value");
unhashed.addNotationData(true, true, "another@notation.data", "Hello-World!");
generator.setUnhashedSubpackets(unhashed.generate());
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
List<NotationData> notations = SignatureSubpacketsUtil.getUnhashedNotationData(signature);
assertEquals(3, notations.size());
assertEquals("test@notation.data", notations.get(0).getNotationName());
assertEquals("test@notation.data", notations.get(1).getNotationName());
assertEquals("another@notation.data", notations.get(2).getNotationName());
assertEquals("notation-value", notations.get(0).getNotationValue());
assertEquals("another-value", notations.get(1).getNotationValue());
assertEquals("Hello-World!", notations.get(2).getNotationValue());
notations = SignatureSubpacketsUtil.getUnhashedNotationData(signature, "test@notation.data");
assertEquals(2, notations.size());
assertEquals("notation-value", notations.get(0).getNotationValue());
assertEquals("another-value", notations.get(1).getNotationValue());
notations = SignatureSubpacketsUtil.getUnhashedNotationData(signature, "invalid");
assertEquals(0, notations.size());
}
@Test
public void testGetRevocationKeyIsNull() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
assertNull(SignatureSubpacketsUtil.getRevocationKey(signature));
}
@Test
public void testGetRevocationKey() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
hashed.addRevocationKey(true, secretKeys.getPublicKey().getAlgorithm(), secretKeys.getPublicKey().getFingerprint());
generator.setHashedSubpackets(hashed.generate());
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
RevocationKey revocationKey = SignatureSubpacketsUtil.getRevocationKey(signature);
assertArrayEquals(secretKeys.getPublicKey().getFingerprint(), revocationKey.getFingerprint());
assertEquals(secretKeys.getPublicKey().getAlgorithm(), revocationKey.getAlgorithm());
}
@Test
public void testGetIntendedRecipientFingerprintsEmpty() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
assertEquals(0, SignatureSubpacketsUtil.getIntendedRecipientFingerprints(signature).size());
}
@Test
public void testGetIntendedRecipientFingerprints() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
hashed.addIntendedRecipientFingerprint(true, secretKeys.getPublicKey());
hashed.addIntendedRecipientFingerprint(true, TestKeys.getCryptiePublicKeyRing().getPublicKey());
generator.setHashedSubpackets(hashed.generate());
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
List<IntendedRecipientFingerprint> intendedRecipientFingerprints = SignatureSubpacketsUtil.getIntendedRecipientFingerprints(signature);
assertEquals(2, intendedRecipientFingerprints.size());
assertArrayEquals(secretKeys.getPublicKey().getFingerprint(), intendedRecipientFingerprints.get(0).getFingerprint());
assertArrayEquals(TestKeys.getCryptiePublicKeyRing().getPublicKey().getFingerprint(), intendedRecipientFingerprints.get(1).getFingerprint());
}
@Test
public void testGetExportableCertification() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
hashed.setExportable(true, true);
generator.setHashedSubpackets(hashed.generate());
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
Exportable exportable = SignatureSubpacketsUtil.getExportableCertification(signature);
assertNotNull(exportable);
assertTrue(exportable.isExportable());
}
@Test
public void testGetTrustSignature() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
PGPPrivateKey certKey = UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys());
PGPSignatureGenerator generator = getSignatureGenerator(certKey, SignatureType.CASUAL_CERTIFICATION);
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
hashed.setTrust(true, 10, 3);
generator.setHashedSubpackets(hashed.generate());
PGPSignature signature = generator.generateCertification(secretKeys.getPublicKey());
TrustSignature trustSignature = SignatureSubpacketsUtil.getTrustSignature(signature);
assertEquals(10, trustSignature.getDepth());
assertEquals(3, trustSignature.getTrustAmount());
}
private PGPSignatureGenerator getSignatureGenerator(PGPPrivateKey signingKey,
SignatureType signatureType) throws PGPException {
PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(