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:
parent
d5151b804e
commit
6a9fb3f6df
4 changed files with 75 additions and 68 deletions
|
@ -32,18 +32,21 @@ import org.pgpainless.signature.subpackets.CertificationSubpackets;
|
|||
import org.pgpainless.util.CollectionUtils;
|
||||
import org.pgpainless.util.DateUtil;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class CertifyCertificateTest {
|
||||
|
||||
@Test
|
||||
public void testUserIdCertification() throws PGPException, IOException {
|
||||
PGPainless api = PGPainless.getInstance();
|
||||
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>";
|
||||
OpenPGPKey bob = PGPainless.generateKeyRing().modernKeyRing(bobUserId);
|
||||
OpenPGPKey bob = api.generateKey().modernKeyRing(bobUserId);
|
||||
|
||||
OpenPGPCertificate bobCertificate = bob.toCertificate();
|
||||
|
||||
CertifyCertificate.CertificationResult result = PGPainless.certify()
|
||||
CertifyCertificate.CertificationResult result = api.generateCertification()
|
||||
.userIdOnCertificate(bobUserId, bobCertificate)
|
||||
.withKey(alice, protector)
|
||||
.build();
|
||||
|
@ -51,11 +54,11 @@ public class CertifyCertificateTest {
|
|||
assertNotNull(result);
|
||||
PGPSignature signature = result.getPgpSignature();
|
||||
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());
|
||||
|
||||
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();
|
||||
PGPPublicKey bobCertifiedKey = bobCertified.getPrimaryKey().getPGPPublicKey();
|
||||
|
@ -71,13 +74,14 @@ public class CertifyCertificateTest {
|
|||
|
||||
@Test
|
||||
public void testKeyDelegation() throws PGPException, IOException {
|
||||
PGPainless api = PGPainless.getInstance();
|
||||
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
|
||||
OpenPGPKey alice = PGPainless.generateKeyRing().modernKeyRing("Alice <alice@pgpainless.org>");
|
||||
OpenPGPKey bob = PGPainless.generateKeyRing().modernKeyRing("Bob <bob@pgpainless.org>");
|
||||
OpenPGPKey alice = api.generateKey().modernKeyRing("Alice <alice@pgpainless.org>");
|
||||
OpenPGPKey bob = api.generateKey().modernKeyRing("Bob <bob@pgpainless.org>");
|
||||
|
||||
OpenPGPCertificate bobCertificate = bob.toCertificate();
|
||||
|
||||
CertifyCertificate.CertificationResult result = PGPainless.certify()
|
||||
CertifyCertificate.CertificationResult result = api.generateCertification()
|
||||
.certificate(bobCertificate, Trustworthiness.fullyTrusted().introducer())
|
||||
.withKey(alice, protector)
|
||||
.build();
|
||||
|
@ -86,7 +90,7 @@ public class CertifyCertificateTest {
|
|||
OpenPGPSignature signature = result.getCertification();
|
||||
PGPSignature pgpSignature = signature.getSignature();
|
||||
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());
|
||||
TrustSignature trustSignaturePacket = pgpSignature.getHashedSubPackets().getTrust();
|
||||
assertNotNull(trustSignaturePacket);
|
||||
|
@ -96,7 +100,7 @@ public class CertifyCertificateTest {
|
|||
assertFalse(trustworthiness.canIntroduce(1));
|
||||
|
||||
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();
|
||||
PGPPublicKey bobCertifiedKey = bobCertified.getPrimaryKey().getPGPPublicKey();
|
||||
|
@ -111,20 +115,21 @@ public class CertifyCertificateTest {
|
|||
|
||||
@Test
|
||||
public void testPetNameCertification() {
|
||||
OpenPGPKey aliceKey = PGPainless.generateKeyRing()
|
||||
PGPainless api = PGPainless.getInstance();
|
||||
OpenPGPKey aliceKey = api.generateKey()
|
||||
.modernKeyRing("Alice <alice@pgpainless.org>");
|
||||
OpenPGPKey bobKey = PGPainless.generateKeyRing()
|
||||
OpenPGPKey bobKey = api.generateKey()
|
||||
.modernKeyRing("Bob <bob@pgpainless.org>");
|
||||
|
||||
OpenPGPCertificate bobCert = bobKey.toCertificate();
|
||||
String petName = "Bobby";
|
||||
|
||||
CertifyCertificate.CertificationResult result = PGPainless.certify()
|
||||
CertifyCertificate.CertificationResult result = api.generateCertification()
|
||||
.userIdOnCertificate(petName, bobCert)
|
||||
.withKey(aliceKey, SecretKeyRingProtector.unprotectedKeys())
|
||||
.buildWithSubpackets(new CertificationSubpackets.Callback() {
|
||||
@Override
|
||||
public void modifyHashedSubpackets(CertificationSubpackets hashedSubpackets) {
|
||||
public void modifyHashedSubpackets(@Nonnull CertificationSubpackets hashedSubpackets) {
|
||||
hashedSubpackets.setExportable(false);
|
||||
}
|
||||
});
|
||||
|
@ -135,25 +140,26 @@ public class CertifyCertificateTest {
|
|||
assertEquals(CertificationType.GENERIC.asSignatureType().getCode(), signature.getSignatureType());
|
||||
|
||||
OpenPGPCertificate certWithPetName = result.getCertifiedCertificate();
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(certWithPetName);
|
||||
KeyRingInfo info = api.inspect(certWithPetName);
|
||||
assertTrue(info.getUserIds().contains(petName));
|
||||
assertFalse(info.getValidUserIds().contains(petName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScopedDelegation() {
|
||||
OpenPGPKey aliceKey = PGPainless.generateKeyRing()
|
||||
PGPainless api = PGPainless.getInstance();
|
||||
OpenPGPKey aliceKey = api.generateKey()
|
||||
.modernKeyRing("Alice <alice@pgpainless.org>");
|
||||
OpenPGPKey caKey = PGPainless.generateKeyRing()
|
||||
OpenPGPKey caKey = api.generateKey()
|
||||
.modernKeyRing("CA <ca@example.com>");
|
||||
OpenPGPCertificate caCert = caKey.toCertificate();
|
||||
|
||||
CertifyCertificate.CertificationResult result = PGPainless.certify()
|
||||
CertifyCertificate.CertificationResult result = api.generateCertification()
|
||||
.certificate(caCert, Trustworthiness.fullyTrusted().introducer())
|
||||
.withKey(aliceKey, SecretKeyRingProtector.unprotectedKeys())
|
||||
.buildWithSubpackets(new CertificationSubpackets.Callback() {
|
||||
@Override
|
||||
public void modifyHashedSubpackets(CertificationSubpackets hashedSubpackets) {
|
||||
public void modifyHashedSubpackets(@Nonnull CertificationSubpackets hashedSubpackets) {
|
||||
hashedSubpackets.setRegularExpression("^.*<.+@example.com>.*$");
|
||||
}
|
||||
});
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
package org.pgpainless.key.generation;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
|
||||
import org.bouncycastle.openpgp.api.OpenPGPKey;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.KeyFlag;
|
||||
|
@ -99,14 +99,16 @@ public class StupidAlgorithmPreferenceEncryptionTest {
|
|||
|
||||
@Test
|
||||
public void testEncryptionIsNotUnencrypted() throws PGPException, IOException {
|
||||
PGPSecretKeyRing stupidKey = PGPainless.readKeyRing().secretKeyRing(STUPID_KEY);
|
||||
PGPPublicKeyRing certificate = PGPainless.extractCertificate(stupidKey);
|
||||
PGPainless api = PGPainless.getInstance();
|
||||
OpenPGPKey stupidKey = api.readKey().parseKey(STUPID_KEY);
|
||||
OpenPGPCertificate certificate = stupidKey.toCertificate();
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
|
||||
EncryptionStream encryptionStream = api.generateMessage()
|
||||
.onOutputStream(out)
|
||||
.withOptions(ProducerOptions.encrypt(
|
||||
EncryptionOptions.get().addRecipient(certificate)
|
||||
EncryptionOptions.get(api).addRecipient(certificate),
|
||||
api
|
||||
));
|
||||
|
||||
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));
|
||||
|
@ -114,7 +116,7 @@ public class StupidAlgorithmPreferenceEncryptionTest {
|
|||
|
||||
EncryptionResult metadata = encryptionStream.getResult();
|
||||
assertTrue(metadata.isEncryptedFor(certificate));
|
||||
assertEquals(PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm(),
|
||||
assertEquals(api.getAlgorithmPolicy().getSymmetricKeyEncryptionAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm(),
|
||||
metadata.getEncryptionAlgorithm());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,8 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
|
||||
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.MessageMetadata;
|
||||
import org.pgpainless.exception.SignatureValidationException;
|
||||
import org.pgpainless.policy.Policy;
|
||||
import org.pgpainless.util.TestAllImplementations;
|
||||
|
||||
public class KeyRevocationTest {
|
||||
|
@ -152,8 +149,9 @@ public class KeyRevocationTest {
|
|||
"u5SfXaTsbMeVQJNdjCNsHq2bOXPGLw==\n" +
|
||||
"=2BW4\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 t1t2 = SignatureUtils.readSignatures(sigT1T2).get(0);
|
||||
PGPSignature t2t3 = SignatureUtils.readSignatures(sigT2T3).get(0);
|
||||
|
@ -161,16 +159,16 @@ public class KeyRevocationTest {
|
|||
|
||||
assertThrows(SignatureValidationException.class, () -> verify(t0,
|
||||
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
|
||||
publicKeys, PGPainless.getPolicy(), new Date()));
|
||||
publicKeys, api));
|
||||
assertThrows(SignatureValidationException.class, () -> verify(t1t2,
|
||||
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
|
||||
publicKeys, PGPainless.getPolicy(), new Date()));
|
||||
publicKeys, api));
|
||||
assertThrows(SignatureValidationException.class, () -> verify(t2t3,
|
||||
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
|
||||
publicKeys, PGPainless.getPolicy(), new Date()));
|
||||
publicKeys, api));
|
||||
assertThrows(SignatureValidationException.class, () -> verify(t3now,
|
||||
new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)),
|
||||
publicKeys, PGPainless.getPolicy(), new Date()));
|
||||
publicKeys, api));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -258,19 +256,19 @@ public class KeyRevocationTest {
|
|||
"=MOaJ\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);
|
||||
|
||||
verify(signature,
|
||||
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 {
|
||||
PGPainless api = PGPainless.getInstance();
|
||||
OpenPGPCertificate certificate = api.toCertificate(cert);
|
||||
|
||||
private void verify(PGPSignature signature, InputStream dataIn, OpenPGPCertificate certificate, PGPainless api)
|
||||
throws PGPException, IOException {
|
||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(dataIn)
|
||||
.withOptions(ConsumerOptions.get(api)
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue