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

Replace all remaining usages of PGPainless.generateKeyRing()

This commit is contained in:
Paul Schaub 2025-04-07 14:09:21 +02:00
parent 2d6675ec06
commit 654756c919
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 64 additions and 62 deletions

View file

@ -8,13 +8,12 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.IOException;
import java.util.Iterator;
import javax.annotation.Nullable;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -60,13 +59,14 @@ public class PassphraseProtectedKeyTest {
@Test
public void testReturnsNonNullDecryptorForSubkeys() throws PGPException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("alice", "passphrase")
.getPGPSecretKeyRing();
SecretKeyRingProtector protector = PasswordBasedSecretKeyRingProtector.forKey(secretKeys, Passphrase.fromPassword("passphrase"));
for (Iterator<PGPPublicKey> it = secretKeys.getPublicKeys(); it.hasNext(); ) {
PGPPublicKey subkey = it.next();
PGPainless api = PGPainless.getInstance();
OpenPGPKey key = api.generateKey()
.modernKeyRing("alice <alice@example.org>", "passphrase");
SecretKeyRingProtector protector = PasswordBasedSecretKeyRingProtector.forKey(key, Passphrase.fromPassword("passphrase"));
for (OpenPGPCertificate.OpenPGPComponentKey subkey : key.getPublicKeys().values()) {
assertNotNull(protector.getEncryptor(subkey));
assertNotNull(protector.getDecryptor(subkey.getKeyID()));
assertNotNull(protector.getDecryptor(subkey.getKeyIdentifier()));
assertNotNull(protector.getDecryptor(subkey.getKeyIdentifier().getKeyId()));
}
}
}

View file

@ -20,7 +20,8 @@ public class UnlockSecretKeyTest {
@Test
public void testUnlockSecretKey() throws PGPException {
PGPSecretKeyRing secretKeyRing = PGPainless.generateKeyRing()
PGPainless api = PGPainless.getInstance();
PGPSecretKeyRing secretKeyRing = api.generateKey()
.simpleEcKeyRing("alice@wonderland.lit", "heureka!")
.getPGPSecretKeyRing();
PGPSecretKey secretKey = secretKeyRing.getSecretKey();

View file

@ -14,7 +14,8 @@ import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector;
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVectorGenerator;
import org.bouncycastle.openpgp.api.OpenPGPImplementation;
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.HashAlgorithm;
@ -40,13 +41,12 @@ public class KeyRingUtilTest {
@Test
public void testInjectCertification() throws PGPException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.modernKeyRing("Alice")
.getPGPSecretKeyRing();
PGPainless api = PGPainless.getInstance();
OpenPGPKey key = api.generateKey().modernKeyRing("Alice");
// test preconditions
assertFalse(secretKeys.getPublicKey().getUserAttributes().hasNext());
int sigCount = CollectionUtils.iteratorToList(secretKeys.getPublicKey().getSignatures()).size();
assertFalse(key.getPrimaryKey().getPGPPublicKey().getUserAttributes().hasNext());
int sigCount = CollectionUtils.iteratorToList(key.getPrimaryKey().getPGPPublicKey().getSignatures()).size();
// Create "image"
byte[] image = new byte[512];
@ -57,15 +57,15 @@ public class KeyRingUtilTest {
// create sig
PGPSignatureGenerator sigGen = new PGPSignatureGenerator(
OpenPGPImplementation.getInstance().pgpContentSignerBuilder(
secretKeys.getPublicKey().getAlgorithm(), HashAlgorithm.SHA512.getAlgorithmId()
), secretKeys.getPublicKey());
api.getImplementation().pgpContentSignerBuilder(
key.getPrimaryKey().getPGPPublicKey().getAlgorithm(), HashAlgorithm.SHA512.getAlgorithmId()
), key.getPrimaryKey().getPGPPublicKey());
sigGen.init(
SignatureType.POSITIVE_CERTIFICATION.getCode(),
UnlockSecretKey.unlockSecretKey(secretKeys.getSecretKey(), SecretKeyRingProtector.unprotectedKeys()));
PGPSignature signature = sigGen.generateCertification(userAttr, secretKeys.getPublicKey());
UnlockSecretKey.unlockSecretKey(key.getPrimarySecretKey().getPGPSecretKey(), SecretKeyRingProtector.unprotectedKeys()));
PGPSignature signature = sigGen.generateCertification(userAttr, key.getPrimaryKey().getPGPPublicKey());
// inject sig
secretKeys = KeyRingUtils.injectCertification(secretKeys, userAttr, signature);
PGPSecretKeyRing secretKeys = KeyRingUtils.injectCertification(key.getPGPSecretKeyRing(), userAttr, signature);
assertTrue(secretKeys.getPublicKey().getUserAttributes().hasNext());
assertEquals(userAttr, secretKeys.getPublicKey().getUserAttributes().next());
@ -74,9 +74,9 @@ public class KeyRingUtilTest {
@Test
public void testKeysPlusPublicKey() {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("Alice")
.getPGPSecretKeyRing();
PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys);
PGPainless api = PGPainless.getInstance();
OpenPGPKey key = api.generateKey().modernKeyRing("Alice");
OpenPGPCertificate certificate = key.toCertificate();
PGPKeyPair keyPair = KeyRingBuilder.generateKeyPair(KeySpec.getBuilder(
KeyType.ECDH(EllipticCurve._P256), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE).build(),
@ -84,10 +84,10 @@ public class KeyRingUtilTest {
PGPPublicKey pubkey = keyPair.getPublicKey();
assertFalse(pubkey.isMasterKey());
PGPSecretKeyRing secretKeysPlus = KeyRingUtils.keysPlusPublicKey(secretKeys, pubkey);
PGPSecretKeyRing secretKeysPlus = KeyRingUtils.keysPlusPublicKey(key.getPGPSecretKeyRing(), pubkey);
assertNotNull(secretKeysPlus.getPublicKey(pubkey.getKeyID()));
PGPPublicKeyRing publicKeysPlus = KeyRingUtils.keysPlusPublicKey(publicKeys, pubkey);
PGPPublicKeyRing publicKeysPlus = KeyRingUtils.keysPlusPublicKey(certificate.getPGPPublicKeyRing(), pubkey);
assertNotNull(publicKeysPlus.getPublicKey(pubkey.getKeyID()));
}
}

View file

@ -13,8 +13,8 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
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.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -149,13 +149,14 @@ public class WeakRSAKeyTest {
public void cannotGenerateWeakKeyWithDefaultPolicyTest() {
String userId = "Alice <alice@pgpainless.org>";
assertThrows(IllegalArgumentException.class, () ->
PGPainless.generateKeyRing()
PGPainless.getInstance().generateKey()
.rsaKeyRing(userId, RsaLength._1024, Passphrase.emptyPassphrase()));
}
@Test
public void cannotSignWithWeakKey() throws IOException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(WEAK_RSA_KEY);
PGPainless api = PGPainless.getInstance();
OpenPGPKey secretKeys = api.readKey().parseKey(WEAK_RSA_KEY);
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
SigningOptions signingOptions = SigningOptions.get();
@ -167,14 +168,15 @@ public class WeakRSAKeyTest {
@Test
public void encryptDecryptRoundTripWithWeakRSAKey() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(WEAK_RSA_KEY);
PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys);
PGPainless api = PGPainless.getInstance();
OpenPGPKey secretKeys = api.readKey().parseKey(WEAK_RSA_KEY);
OpenPGPCertificate publicKeys = secretKeys.toCertificate();
ByteArrayOutputStream encryptOut = new ByteArrayOutputStream();
EncryptionOptions encryptionOptions = EncryptionOptions.encryptCommunications()
EncryptionOptions encryptionOptions = EncryptionOptions.encryptCommunications(api)
.addRecipient(publicKeys);
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
EncryptionStream encryptionStream = api.generateMessage()
.onOutputStream(encryptOut)
.withOptions(ProducerOptions.encrypt(encryptionOptions));

View file

@ -6,6 +6,7 @@ package org.pgpainless.signature;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -21,15 +22,12 @@ import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPOnePassSignature;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.api.OpenPGPImplementation;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.TestTemplate;
@ -53,20 +51,19 @@ public class OnePassSignatureBracketingTest {
@ExtendWith(TestAllImplementations.class)
public void onePassSignaturePacketsAndSignaturesAreBracketedTest()
throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
PGPSecretKeyRing key1 = PGPainless.generateKeyRing().modernKeyRing("Alice")
.getPGPSecretKeyRing();
PGPSecretKeyRing key2 = PGPainless.generateKeyRing().modernKeyRing("Bob")
.getPGPSecretKeyRing();
PGPPublicKeyRing cert1 = PGPainless.extractCertificate(key1);
OpenPGPKey key1 = api.generateKey().modernKeyRing("Alice");
OpenPGPKey key2 = api.generateKey().modernKeyRing("Bob");
OpenPGPCertificate cert1 = key1.toCertificate();
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
EncryptionStream encryptionStream = api.generateMessage()
.onOutputStream(out)
.withOptions(ProducerOptions.signAndEncrypt(
EncryptionOptions.encryptCommunications()
EncryptionOptions.encryptCommunications(api)
.addRecipient(cert1),
SigningOptions.get()
SigningOptions.get(api)
.addInlineSignature(SecretKeyRingProtector.unprotectedKeys(), key1, DocumentSignatureType.BINARY_DOCUMENT)
.addInlineSignature(SecretKeyRingProtector.unprotectedKeys(), key2, DocumentSignatureType.BINARY_DOCUMENT)
).setAsciiArmor(true));
@ -78,7 +75,7 @@ public class OnePassSignatureBracketingTest {
ByteArrayInputStream ciphertextIn = new ByteArrayInputStream(out.toByteArray());
InputStream inputStream = PGPUtil.getDecoderStream(ciphertextIn);
PGPObjectFactory objectFactory = OpenPGPImplementation.getInstance().pgpObjectFactory(inputStream);
PGPObjectFactory objectFactory = api.getImplementation().pgpObjectFactory(inputStream);
PGPOnePassSignatureList onePassSignatures = null;
PGPSignatureList signatures = null;
@ -93,11 +90,12 @@ public class OnePassSignatureBracketingTest {
for (PGPEncryptedData encryptedData : encryptedDataList) {
if (encryptedData instanceof PGPPublicKeyEncryptedData) {
PGPPublicKeyEncryptedData publicKeyEncryptedData = (PGPPublicKeyEncryptedData) encryptedData;
PGPSecretKey secretKey = key1.getSecretKey(publicKeyEncryptedData.getKeyID());
PGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(secretKey, SecretKeyRingProtector.unprotectedKeys());
PublicKeyDataDecryptorFactory decryptorFactory = OpenPGPImplementation.getInstance().publicKeyDataDecryptorFactory(privateKey);
OpenPGPKey.OpenPGPSecretKey secretKey = key1.getSecretKey(publicKeyEncryptedData.getKeyIdentifier());
OpenPGPKey.OpenPGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(secretKey, SecretKeyRingProtector.unprotectedKeys());
PublicKeyDataDecryptorFactory decryptorFactory = api.getImplementation()
.publicKeyDataDecryptorFactory(privateKey.getKeyPair().getPrivateKey());
InputStream decryptionStream = publicKeyEncryptedData.getDataStream(decryptorFactory);
objectFactory = OpenPGPImplementation.getInstance().pgpObjectFactory(decryptionStream);
objectFactory = api.getImplementation().pgpObjectFactory(decryptionStream);
continue outerloop;
}
}
@ -107,7 +105,7 @@ public class OnePassSignatureBracketingTest {
} else if (next instanceof PGPCompressedData) {
PGPCompressedData compressed = (PGPCompressedData) next;
InputStream decompressor = compressed.getDataStream();
objectFactory = OpenPGPImplementation.getInstance().pgpObjectFactory(decompressor);
objectFactory = api.getImplementation().pgpObjectFactory(decompressor);
continue outerloop;
} else if (next instanceof PGPLiteralData) {
continue outerloop;
@ -131,6 +129,7 @@ public class OnePassSignatureBracketingTest {
// eg. (OPS1, OPS2, LiteralData, Sig2, Sig1)
PGPOnePassSignature onePassSignature = onePassSignatures.get(i);
PGPSignature signature = signatures.get(signatures.size() - 1 - i);
assertTrue(signature.hasKeyIdentifier(onePassSignature.getKeyIdentifier()));
assertEquals(onePassSignature.getKeyID(), signature.getKeyID());
byte[] encoded = onePassSignature.getEncoded();

View file

@ -30,7 +30,7 @@ public class ThirdPartyCertificationSignatureBuilderTest {
@Test
public void testInvalidSignatureTypeThrows() {
PGPainless api = PGPainless.getInstance();
OpenPGPKey secretKeys = PGPainless.generateKeyRing()
OpenPGPKey secretKeys = api.generateKey()
.modernKeyRing("Alice");
assertThrows(IllegalArgumentException.class, () ->
new ThirdPartyCertificationSignatureBuilder(
@ -43,10 +43,10 @@ public class ThirdPartyCertificationSignatureBuilderTest {
@Test
public void testUserIdCertification() throws PGPException {
PGPainless api = PGPainless.getInstance();
OpenPGPKey secretKeys = PGPainless.generateKeyRing()
OpenPGPKey secretKeys = api.generateKey()
.modernKeyRing("Alice");
OpenPGPCertificate bobsPublicKeys = PGPainless.generateKeyRing().modernKeyRing("Bob")
OpenPGPCertificate bobsPublicKeys = api.generateKey().modernKeyRing("Bob")
.toCertificate();
ThirdPartyCertificationSignatureBuilder signatureBuilder = new ThirdPartyCertificationSignatureBuilder(
@ -63,7 +63,7 @@ public class ThirdPartyCertificationSignatureBuilderTest {
OpenPGPSignature certification = signatureBuilder.build(bobsPublicKeys, "Bob");
PGPSignature signature = certification.getSignature();
assertEquals(SignatureType.GENERIC_CERTIFICATION, SignatureType.valueOf(signature.getSignatureType()));
assertEquals(SignatureType.GENERIC_CERTIFICATION, SignatureType.requireFromCode(signature.getSignatureType()));
assertTrue(KeyIdentifier.matches(signature.getKeyIdentifiers(), secretKeys.getKeyIdentifier(), true));
assertArrayEquals(
secretKeys.getPrimaryKey().getPGPPublicKey().getFingerprint(),

View file

@ -35,7 +35,7 @@ public class ThirdPartyDirectKeySignatureBuilderTest {
@Test
public void testDirectKeySignatureBuilding() throws PGPException {
PGPainless api = PGPainless.getInstance();
OpenPGPKey secretKeys = PGPainless.generateKeyRing()
OpenPGPKey secretKeys = api.generateKey()
.modernKeyRing("Alice");
DirectKeySelfSignatureBuilder dsb = new DirectKeySelfSignatureBuilder(
@ -70,7 +70,7 @@ public class ThirdPartyDirectKeySignatureBuilderTest {
assertNotNull(signature);
assertEquals(directKeySig.getSignature(), signature);
assertEquals(SignatureType.DIRECT_KEY, SignatureType.valueOf(signature.getSignatureType()));
assertEquals(SignatureType.DIRECT_KEY, SignatureType.requireFromCode(signature.getSignatureType()));
assertEquals(Collections.singletonList(KeyFlag.CERTIFY_OTHER), SignatureSubpacketsUtil.parseKeyFlags(signature));
assertEquals(Collections.singleton(HashAlgorithm.SHA512), SignatureSubpacketsUtil.parsePreferredHashAlgorithms(signature));
assertEquals(Collections.singleton(CompressionAlgorithm.ZIP), SignatureSubpacketsUtil.parsePreferredCompressionAlgorithms(signature));

View file

@ -17,9 +17,9 @@ public class ArmorTest {
@Test
public void armor() throws IOException {
byte[] data = PGPainless.generateKeyRing()
PGPainless api = PGPainless.getInstance();
byte[] data = api.generateKey()
.modernKeyRing("Alice")
.getPGPSecretKeyRing()
.getEncoded();
byte[] knownGoodArmor = ArmorUtils.toAsciiArmoredString(data)
.replace("Version: PGPainless\n", "") // armor command does not add version anymore