1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

Rework some more tests

This commit is contained in:
Paul Schaub 2025-03-24 13:44:55 +01:00
parent d5151b804e
commit 6a9fb3f6df
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 75 additions and 68 deletions

View file

@ -32,18 +32,21 @@ import org.pgpainless.signature.subpackets.CertificationSubpackets;
import org.pgpainless.util.CollectionUtils; import org.pgpainless.util.CollectionUtils;
import org.pgpainless.util.DateUtil; import org.pgpainless.util.DateUtil;
import javax.annotation.Nonnull;
public class CertifyCertificateTest { public class CertifyCertificateTest {
@Test @Test
public void testUserIdCertification() throws PGPException, IOException { public void testUserIdCertification() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys(); SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
OpenPGPKey alice = PGPainless.generateKeyRing().modernKeyRing("Alice <alice@pgpainless.org>"); OpenPGPKey alice = api.generateKey().modernKeyRing("Alice <alice@pgpainless.org>");
String bobUserId = "Bob <bob@pgpainless.org>"; String bobUserId = "Bob <bob@pgpainless.org>";
OpenPGPKey bob = PGPainless.generateKeyRing().modernKeyRing(bobUserId); OpenPGPKey bob = api.generateKey().modernKeyRing(bobUserId);
OpenPGPCertificate bobCertificate = bob.toCertificate(); OpenPGPCertificate bobCertificate = bob.toCertificate();
CertifyCertificate.CertificationResult result = PGPainless.certify() CertifyCertificate.CertificationResult result = api.generateCertification()
.userIdOnCertificate(bobUserId, bobCertificate) .userIdOnCertificate(bobUserId, bobCertificate)
.withKey(alice, protector) .withKey(alice, protector)
.build(); .build();
@ -51,11 +54,11 @@ public class CertifyCertificateTest {
assertNotNull(result); assertNotNull(result);
PGPSignature signature = result.getPgpSignature(); PGPSignature signature = result.getPgpSignature();
assertNotNull(signature); assertNotNull(signature);
assertEquals(SignatureType.GENERIC_CERTIFICATION, SignatureType.valueOf(signature.getSignatureType())); assertEquals(SignatureType.GENERIC_CERTIFICATION, SignatureType.requireFromCode(signature.getSignatureType()));
assertEquals(alice.getPrimaryKey().getPGPPublicKey().getKeyID(), signature.getKeyID()); assertEquals(alice.getPrimaryKey().getPGPPublicKey().getKeyID(), signature.getKeyID());
assertTrue(SignatureVerifier.verifyUserIdCertification( assertTrue(SignatureVerifier.verifyUserIdCertification(
bobUserId, signature, alice.getPrimaryKey().getPGPPublicKey(), bob.getPrimaryKey().getPGPPublicKey(), PGPainless.getPolicy(), DateUtil.now())); bobUserId, signature, alice.getPrimaryKey().getPGPPublicKey(), bob.getPrimaryKey().getPGPPublicKey(), api.getAlgorithmPolicy(), DateUtil.now()));
OpenPGPCertificate bobCertified = result.getCertifiedCertificate(); OpenPGPCertificate bobCertified = result.getCertifiedCertificate();
PGPPublicKey bobCertifiedKey = bobCertified.getPrimaryKey().getPGPPublicKey(); PGPPublicKey bobCertifiedKey = bobCertified.getPrimaryKey().getPGPPublicKey();
@ -71,13 +74,14 @@ public class CertifyCertificateTest {
@Test @Test
public void testKeyDelegation() throws PGPException, IOException { public void testKeyDelegation() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys(); SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
OpenPGPKey alice = PGPainless.generateKeyRing().modernKeyRing("Alice <alice@pgpainless.org>"); OpenPGPKey alice = api.generateKey().modernKeyRing("Alice <alice@pgpainless.org>");
OpenPGPKey bob = PGPainless.generateKeyRing().modernKeyRing("Bob <bob@pgpainless.org>"); OpenPGPKey bob = api.generateKey().modernKeyRing("Bob <bob@pgpainless.org>");
OpenPGPCertificate bobCertificate = bob.toCertificate(); OpenPGPCertificate bobCertificate = bob.toCertificate();
CertifyCertificate.CertificationResult result = PGPainless.certify() CertifyCertificate.CertificationResult result = api.generateCertification()
.certificate(bobCertificate, Trustworthiness.fullyTrusted().introducer()) .certificate(bobCertificate, Trustworthiness.fullyTrusted().introducer())
.withKey(alice, protector) .withKey(alice, protector)
.build(); .build();
@ -86,7 +90,7 @@ public class CertifyCertificateTest {
OpenPGPSignature signature = result.getCertification(); OpenPGPSignature signature = result.getCertification();
PGPSignature pgpSignature = signature.getSignature(); PGPSignature pgpSignature = signature.getSignature();
assertNotNull(signature); assertNotNull(signature);
assertEquals(SignatureType.DIRECT_KEY, SignatureType.valueOf(pgpSignature.getSignatureType())); assertEquals(SignatureType.DIRECT_KEY, SignatureType.requireFromCode(pgpSignature.getSignatureType()));
assertEquals(alice.getPrimaryKey().getPGPPublicKey().getKeyID(), pgpSignature.getKeyID()); assertEquals(alice.getPrimaryKey().getPGPPublicKey().getKeyID(), pgpSignature.getKeyID());
TrustSignature trustSignaturePacket = pgpSignature.getHashedSubPackets().getTrust(); TrustSignature trustSignaturePacket = pgpSignature.getHashedSubPackets().getTrust();
assertNotNull(trustSignaturePacket); assertNotNull(trustSignaturePacket);
@ -96,7 +100,7 @@ public class CertifyCertificateTest {
assertFalse(trustworthiness.canIntroduce(1)); assertFalse(trustworthiness.canIntroduce(1));
assertTrue(SignatureVerifier.verifyDirectKeySignature( assertTrue(SignatureVerifier.verifyDirectKeySignature(
pgpSignature, alice.getPrimaryKey().getPGPPublicKey(), bob.getPrimaryKey().getPGPPublicKey(), PGPainless.getPolicy(), DateUtil.now())); pgpSignature, alice.getPrimaryKey().getPGPPublicKey(), bob.getPrimaryKey().getPGPPublicKey(), api.getAlgorithmPolicy(), DateUtil.now()));
OpenPGPCertificate bobCertified = result.getCertifiedCertificate(); OpenPGPCertificate bobCertified = result.getCertifiedCertificate();
PGPPublicKey bobCertifiedKey = bobCertified.getPrimaryKey().getPGPPublicKey(); PGPPublicKey bobCertifiedKey = bobCertified.getPrimaryKey().getPGPPublicKey();
@ -111,20 +115,21 @@ public class CertifyCertificateTest {
@Test @Test
public void testPetNameCertification() { public void testPetNameCertification() {
OpenPGPKey aliceKey = PGPainless.generateKeyRing() PGPainless api = PGPainless.getInstance();
OpenPGPKey aliceKey = api.generateKey()
.modernKeyRing("Alice <alice@pgpainless.org>"); .modernKeyRing("Alice <alice@pgpainless.org>");
OpenPGPKey bobKey = PGPainless.generateKeyRing() OpenPGPKey bobKey = api.generateKey()
.modernKeyRing("Bob <bob@pgpainless.org>"); .modernKeyRing("Bob <bob@pgpainless.org>");
OpenPGPCertificate bobCert = bobKey.toCertificate(); OpenPGPCertificate bobCert = bobKey.toCertificate();
String petName = "Bobby"; String petName = "Bobby";
CertifyCertificate.CertificationResult result = PGPainless.certify() CertifyCertificate.CertificationResult result = api.generateCertification()
.userIdOnCertificate(petName, bobCert) .userIdOnCertificate(petName, bobCert)
.withKey(aliceKey, SecretKeyRingProtector.unprotectedKeys()) .withKey(aliceKey, SecretKeyRingProtector.unprotectedKeys())
.buildWithSubpackets(new CertificationSubpackets.Callback() { .buildWithSubpackets(new CertificationSubpackets.Callback() {
@Override @Override
public void modifyHashedSubpackets(CertificationSubpackets hashedSubpackets) { public void modifyHashedSubpackets(@Nonnull CertificationSubpackets hashedSubpackets) {
hashedSubpackets.setExportable(false); hashedSubpackets.setExportable(false);
} }
}); });
@ -135,25 +140,26 @@ public class CertifyCertificateTest {
assertEquals(CertificationType.GENERIC.asSignatureType().getCode(), signature.getSignatureType()); assertEquals(CertificationType.GENERIC.asSignatureType().getCode(), signature.getSignatureType());
OpenPGPCertificate certWithPetName = result.getCertifiedCertificate(); OpenPGPCertificate certWithPetName = result.getCertifiedCertificate();
KeyRingInfo info = PGPainless.inspectKeyRing(certWithPetName); KeyRingInfo info = api.inspect(certWithPetName);
assertTrue(info.getUserIds().contains(petName)); assertTrue(info.getUserIds().contains(petName));
assertFalse(info.getValidUserIds().contains(petName)); assertFalse(info.getValidUserIds().contains(petName));
} }
@Test @Test
public void testScopedDelegation() { public void testScopedDelegation() {
OpenPGPKey aliceKey = PGPainless.generateKeyRing() PGPainless api = PGPainless.getInstance();
OpenPGPKey aliceKey = api.generateKey()
.modernKeyRing("Alice <alice@pgpainless.org>"); .modernKeyRing("Alice <alice@pgpainless.org>");
OpenPGPKey caKey = PGPainless.generateKeyRing() OpenPGPKey caKey = api.generateKey()
.modernKeyRing("CA <ca@example.com>"); .modernKeyRing("CA <ca@example.com>");
OpenPGPCertificate caCert = caKey.toCertificate(); OpenPGPCertificate caCert = caKey.toCertificate();
CertifyCertificate.CertificationResult result = PGPainless.certify() CertifyCertificate.CertificationResult result = api.generateCertification()
.certificate(caCert, Trustworthiness.fullyTrusted().introducer()) .certificate(caCert, Trustworthiness.fullyTrusted().introducer())
.withKey(aliceKey, SecretKeyRingProtector.unprotectedKeys()) .withKey(aliceKey, SecretKeyRingProtector.unprotectedKeys())
.buildWithSubpackets(new CertificationSubpackets.Callback() { .buildWithSubpackets(new CertificationSubpackets.Callback() {
@Override @Override
public void modifyHashedSubpackets(CertificationSubpackets hashedSubpackets) { public void modifyHashedSubpackets(@Nonnull CertificationSubpackets hashedSubpackets) {
hashedSubpackets.setRegularExpression("^.*<.+@example.com>.*$"); hashedSubpackets.setRegularExpression("^.*<.+@example.com>.*$");
} }
}); });

View file

@ -5,8 +5,8 @@
package org.pgpainless.key.generation; package org.pgpainless.key.generation;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
@ -99,14 +99,16 @@ public class StupidAlgorithmPreferenceEncryptionTest {
@Test @Test
public void testEncryptionIsNotUnencrypted() throws PGPException, IOException { public void testEncryptionIsNotUnencrypted() throws PGPException, IOException {
PGPSecretKeyRing stupidKey = PGPainless.readKeyRing().secretKeyRing(STUPID_KEY); PGPainless api = PGPainless.getInstance();
PGPPublicKeyRing certificate = PGPainless.extractCertificate(stupidKey); OpenPGPKey stupidKey = api.readKey().parseKey(STUPID_KEY);
OpenPGPCertificate certificate = stupidKey.toCertificate();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign() EncryptionStream encryptionStream = api.generateMessage()
.onOutputStream(out) .onOutputStream(out)
.withOptions(ProducerOptions.encrypt( .withOptions(ProducerOptions.encrypt(
EncryptionOptions.get().addRecipient(certificate) EncryptionOptions.get(api).addRecipient(certificate),
api
)); ));
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8)); encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));
@ -114,7 +116,7 @@ public class StupidAlgorithmPreferenceEncryptionTest {
EncryptionResult metadata = encryptionStream.getResult(); EncryptionResult metadata = encryptionStream.getResult();
assertTrue(metadata.isEncryptedFor(certificate)); assertTrue(metadata.isEncryptedFor(certificate));
assertEquals(PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm(), assertEquals(api.getAlgorithmPolicy().getSymmetricKeyEncryptionAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm(),
metadata.getEncryptionAlgorithm()); metadata.getEncryptionAlgorithm());
} }
} }

View file

@ -10,10 +10,8 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.api.OpenPGPCertificate; import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
@ -24,7 +22,6 @@ import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.MessageMetadata; import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.exception.SignatureValidationException; import org.pgpainless.exception.SignatureValidationException;
import org.pgpainless.policy.Policy;
import org.pgpainless.util.TestAllImplementations; import org.pgpainless.util.TestAllImplementations;
public class KeyRevocationTest { public class KeyRevocationTest {
@ -152,8 +149,9 @@ public class KeyRevocationTest {
"u5SfXaTsbMeVQJNdjCNsHq2bOXPGLw==\n" + "u5SfXaTsbMeVQJNdjCNsHq2bOXPGLw==\n" +
"=2BW4\n" + "=2BW4\n" +
"-----END PGP ARMORED FILE-----\n"; "-----END PGP ARMORED FILE-----\n";
PGPainless api = PGPainless.getInstance();
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(key); OpenPGPCertificate publicKeys = api.readKey().parseCertificate(key);
PGPSignature t0 = SignatureUtils.readSignatures(sigT0).get(0); PGPSignature t0 = SignatureUtils.readSignatures(sigT0).get(0);
PGPSignature t1t2 = SignatureUtils.readSignatures(sigT1T2).get(0); PGPSignature t1t2 = SignatureUtils.readSignatures(sigT1T2).get(0);
PGPSignature t2t3 = SignatureUtils.readSignatures(sigT2T3).get(0); PGPSignature t2t3 = SignatureUtils.readSignatures(sigT2T3).get(0);
@ -161,16 +159,16 @@ public class KeyRevocationTest {
assertThrows(SignatureValidationException.class, () -> verify(t0, assertThrows(SignatureValidationException.class, () -> verify(t0,
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
publicKeys, PGPainless.getPolicy(), new Date())); publicKeys, api));
assertThrows(SignatureValidationException.class, () -> verify(t1t2, assertThrows(SignatureValidationException.class, () -> verify(t1t2,
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
publicKeys, PGPainless.getPolicy(), new Date())); publicKeys, api));
assertThrows(SignatureValidationException.class, () -> verify(t2t3, assertThrows(SignatureValidationException.class, () -> verify(t2t3,
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
publicKeys, PGPainless.getPolicy(), new Date())); publicKeys, api));
assertThrows(SignatureValidationException.class, () -> verify(t3now, assertThrows(SignatureValidationException.class, () -> verify(t3now,
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
publicKeys, PGPainless.getPolicy(), new Date())); publicKeys, api));
} }
/** /**
@ -258,19 +256,19 @@ public class KeyRevocationTest {
"=MOaJ\n" + "=MOaJ\n" +
"-----END PGP ARMORED FILE-----\n"; "-----END PGP ARMORED FILE-----\n";
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(key); PGPainless api = PGPainless.getInstance();
OpenPGPCertificate publicKeys = api.readKey().parseCertificate(key);
PGPSignature signature = SignatureUtils.readSignatures(sig).get(0); PGPSignature signature = SignatureUtils.readSignatures(sig).get(0);
verify(signature, verify(signature,
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
publicKeys, PGPainless.getPolicy(), new Date()); publicKeys, api);
} }
private void verify(PGPSignature signature, InputStream dataIn, PGPPublicKeyRing cert, Policy policy, Date validationDate) throws PGPException, IOException { private void verify(PGPSignature signature, InputStream dataIn, OpenPGPCertificate certificate, PGPainless api)
PGPainless api = PGPainless.getInstance(); throws PGPException, IOException {
OpenPGPCertificate certificate = api.toCertificate(cert);
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify() DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(dataIn) .onInputStream(dataIn)
.withOptions(ConsumerOptions.get(api) .withOptions(ConsumerOptions.get(api)