mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-14 12:49:39 +02:00
Remove deprecated class PGPKeyRing
This commit is contained in:
parent
e535bd2f55
commit
980782e629
11 changed files with 91 additions and 247 deletions
|
@ -44,7 +44,6 @@ import org.pgpainless.decryption_verification.DecryptionStream;
|
|||
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.generation.KeySpec;
|
||||
import org.pgpainless.key.generation.type.ElGamal_GENERAL;
|
||||
import org.pgpainless.key.generation.type.KeyType;
|
||||
|
@ -52,6 +51,7 @@ import org.pgpainless.key.generation.type.length.ElGamalLength;
|
|||
import org.pgpainless.key.generation.type.length.RsaLength;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
import org.pgpainless.util.BCUtil;
|
||||
|
||||
public class EncryptDecryptTest {
|
||||
|
@ -71,8 +71,8 @@ public class EncryptDecryptTest {
|
|||
@Test
|
||||
public void freshKeysRsaToElGamalTest()
|
||||
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing()
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing()
|
||||
.withSubKey(KeySpec.getBuilder(ElGamal_GENERAL.withLength(ElGamalLength._3072)).withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS).withDefaultAlgorithms())
|
||||
.withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._4096)).withKeyFlags(KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER).withDefaultAlgorithms())
|
||||
.withPrimaryUserId("juliet@capulet.lit").withoutPassphrase().build();
|
||||
|
@ -83,8 +83,8 @@ public class EncryptDecryptTest {
|
|||
@Test
|
||||
public void freshKeysRsaToRsaTest()
|
||||
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
|
||||
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
@ -92,8 +92,8 @@ public class EncryptDecryptTest {
|
|||
@Test
|
||||
public void freshKeysEcToEcTest()
|
||||
throws IOException, PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
||||
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
@ -101,8 +101,8 @@ public class EncryptDecryptTest {
|
|||
@Test
|
||||
public void freshKeysEcToRsaTest()
|
||||
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
|
||||
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
@ -110,26 +110,25 @@ public class EncryptDecryptTest {
|
|||
@Test
|
||||
public void freshKeysRsaToEcTest()
|
||||
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
||||
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existingRsaKeysTest() throws IOException, PGPException {
|
||||
PGPKeyRing sender = new PGPKeyRing(TestKeys.getJulietPublicKeyRing(), TestKeys.getJulietSecretKeyRing());
|
||||
PGPKeyRing recipient = new PGPKeyRing(TestKeys.getRomeoPublicKeyRing(), TestKeys.getRomeoSecretKeyRing());
|
||||
PGPSecretKeyRing sender = TestKeys.getJulietSecretKeyRing();
|
||||
PGPSecretKeyRing recipient = TestKeys.getRomeoSecretKeyRing();
|
||||
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
private void encryptDecryptForSecretKeyRings(PGPKeyRing sender, PGPKeyRing recipient)
|
||||
private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing senderSec, PGPSecretKeyRing recipientSec)
|
||||
throws PGPException, IOException {
|
||||
PGPSecretKeyRing recipientSec = recipient.getSecretKeys();
|
||||
PGPSecretKeyRing senderSec = sender.getSecretKeys();
|
||||
PGPPublicKeyRing recipientPub = recipient.getPublicKeys();
|
||||
PGPPublicKeyRing senderPub = sender.getPublicKeys();
|
||||
|
||||
PGPPublicKeyRing recipientPub = KeyRingUtils.publicKeyRingFrom(recipientSec);
|
||||
PGPPublicKeyRing senderPub = KeyRingUtils.publicKeyRingFrom(senderSec);
|
||||
|
||||
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();
|
||||
|
||||
|
@ -188,7 +187,7 @@ public class EncryptDecryptTest {
|
|||
|
||||
@Test
|
||||
public void testDetachedSignatureCreationAndVerification() throws IOException, PGPException {
|
||||
PGPKeyRing signingKeys = new PGPKeyRing(TestKeys.getJulietPublicKeyRing(), TestKeys.getJulietSecretKeyRing());
|
||||
PGPSecretKeyRing signingKeys = TestKeys.getJulietSecretKeyRing();
|
||||
SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector();
|
||||
byte[] data = testMessage.getBytes();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
|
||||
|
@ -196,7 +195,7 @@ public class EncryptDecryptTest {
|
|||
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(dummyOut)
|
||||
.doNotEncrypt()
|
||||
.createDetachedSignature()
|
||||
.signWith(keyRingProtector, signingKeys.getSecretKeys())
|
||||
.signWith(keyRingProtector, signingKeys)
|
||||
.noArmor();
|
||||
Streams.pipeAll(inputStream, signer);
|
||||
signer.close();
|
||||
|
@ -217,7 +216,7 @@ public class EncryptDecryptTest {
|
|||
DecryptionStream verifier = PGPainless.decryptAndOrVerify().onInputStream(inputStream)
|
||||
.doNotDecrypt()
|
||||
.verifyDetachedSignature(new ByteArrayInputStream(armorSig.getBytes()))
|
||||
.verifyWith(Collections.singleton(signingKeys.getPublicKeys()))
|
||||
.verifyWith(Collections.singleton(KeyRingUtils.publicKeyRingFrom(signingKeys)))
|
||||
.ignoreMissingPublicKeys()
|
||||
.build();
|
||||
dummyOut = new ByteArrayOutputStream();
|
||||
|
@ -230,14 +229,14 @@ public class EncryptDecryptTest {
|
|||
|
||||
@Test
|
||||
public void testOnePassSignatureCreationAndVerification() throws IOException, PGPException {
|
||||
PGPKeyRing signingKeys = new PGPKeyRing(TestKeys.getJulietPublicKeyRing(), TestKeys.getJulietSecretKeyRing());
|
||||
PGPSecretKeyRing signingKeys = TestKeys.getJulietSecretKeyRing();
|
||||
SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector();
|
||||
byte[] data = testMessage.getBytes();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
|
||||
ByteArrayOutputStream signOut = new ByteArrayOutputStream();
|
||||
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(signOut)
|
||||
.doNotEncrypt()
|
||||
.signWith(keyRingProtector, signingKeys.getSecretKeys())
|
||||
.signWith(keyRingProtector, signingKeys)
|
||||
.asciiArmor();
|
||||
Streams.pipeAll(inputStream, signer);
|
||||
signer.close();
|
||||
|
@ -249,7 +248,7 @@ public class EncryptDecryptTest {
|
|||
inputStream = new ByteArrayInputStream(signOut.toByteArray());
|
||||
DecryptionStream verifier = PGPainless.decryptAndOrVerify().onInputStream(inputStream)
|
||||
.doNotDecrypt()
|
||||
.verifyWith(Collections.singleton(signingKeys.getPublicKeys()))
|
||||
.verifyWith(Collections.singleton(KeyRingUtils.publicKeyRingFrom(signingKeys)))
|
||||
.ignoreMissingPublicKeys()
|
||||
.build();
|
||||
signOut = new ByteArrayOutputStream();
|
||||
|
|
|
@ -33,10 +33,10 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|||
import org.bouncycastle.util.io.Streams;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.generation.type.length.RsaLength;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
|
||||
/**
|
||||
* Class used to determine the length of cipher-text depending on used algorithms.
|
||||
|
@ -50,8 +50,8 @@ public class LengthTest {
|
|||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
LOGGER.log(Level.FINER, "\nEC -> EC");
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
|
@ -61,10 +61,8 @@ public class LengthTest {
|
|||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
LOGGER.log(Level.FINER, "\nRSA-2048 -> RSA-2048");
|
||||
@SuppressWarnings("deprecation")
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
||||
@SuppressWarnings("deprecation")
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
|
@ -73,8 +71,8 @@ public class LengthTest {
|
|||
throws PGPException,
|
||||
IOException {
|
||||
LOGGER.log(Level.FINER, "\nRSA-4096 -> RSA-4096");
|
||||
PGPKeyRing sender = PGPainless.readKeyRing().keyRing(TestKeys.JULIET_PUB, TestKeys.JULIET_SEC);
|
||||
PGPKeyRing recipient = PGPainless.readKeyRing().keyRing(TestKeys.ROMEO_PUB, TestKeys.ROMEO_SEC);
|
||||
PGPSecretKeyRing sender = PGPainless.readKeyRing().secretKeyRing(TestKeys.JULIET_SEC);
|
||||
PGPSecretKeyRing recipient = PGPainless.readKeyRing().secretKeyRing(TestKeys.ROMEO_SEC);
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
|
@ -82,9 +80,8 @@ public class LengthTest {
|
|||
public void rsaEc() throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
|
||||
NoSuchProviderException {
|
||||
LOGGER.log(Level.FINER, "\nRSA-2048 -> EC");
|
||||
@SuppressWarnings("deprecation")
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
|
@ -93,20 +90,18 @@ public class LengthTest {
|
|||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
LOGGER.log(Level.FINER, "\nEC -> RSA-2048");
|
||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
|
||||
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
|
||||
@SuppressWarnings("deprecation")
|
||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
||||
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
private void encryptDecryptForSecretKeyRings(PGPKeyRing sender, PGPKeyRing recipient)
|
||||
private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing senderSec, PGPSecretKeyRing recipientSec)
|
||||
throws PGPException,
|
||||
IOException {
|
||||
PGPSecretKeyRing recipientSec = recipient.getSecretKeys();
|
||||
PGPSecretKeyRing senderSec = sender.getSecretKeys();
|
||||
PGPPublicKeyRing recipientPub = recipient.getPublicKeys();
|
||||
PGPPublicKeyRing senderPub = sender.getPublicKeys();
|
||||
PGPPublicKeyRing recipientPub = KeyRingUtils.publicKeyRingFrom(recipientSec);
|
||||
PGPPublicKeyRing senderPub = KeyRingUtils.publicKeyRingFrom(senderSec);
|
||||
|
||||
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();
|
||||
|
||||
|
|
|
@ -24,11 +24,12 @@ import java.util.logging.Logger;
|
|||
|
||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
|
||||
public class GenerateKeyTest {
|
||||
|
||||
|
@ -36,17 +37,18 @@ public class GenerateKeyTest {
|
|||
|
||||
@Test
|
||||
public void generateKey() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
||||
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("fresh@encrypted.key", "password123");
|
||||
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("fresh@encrypted.key", "password123");
|
||||
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream armor = new ArmoredOutputStream(bytes);
|
||||
keyRing.getPublicKeys().encode(armor);
|
||||
secretKeys.encode(armor);
|
||||
armor.close();
|
||||
String publicKey = new String(bytes.toByteArray());
|
||||
|
||||
bytes = new ByteArrayOutputStream();
|
||||
armor = new ArmoredOutputStream(bytes);
|
||||
keyRing.getSecretKeys().encode(armor);
|
||||
secretKeys.encode(armor);
|
||||
armor.close();
|
||||
String privateKey = new String(bytes.toByteArray());
|
||||
|
||||
|
@ -55,9 +57,9 @@ public class GenerateKeyTest {
|
|||
"Fingerprint: %s\n" +
|
||||
"Key-ID: %s\n" +
|
||||
"%s\n" +
|
||||
"%s\n", keyRing.getPublicKeys().getPublicKey().getUserIDs().next(),
|
||||
new OpenPgpV4Fingerprint(keyRing.getPublicKeys()),
|
||||
keyRing.getPublicKeys().getPublicKey().getKeyID(),
|
||||
"%s\n", secretKeys.getPublicKey().getUserIDs().next(),
|
||||
new OpenPgpV4Fingerprint(publicKeys),
|
||||
publicKeys.getPublicKey().getKeyID(),
|
||||
publicKey, privateKey));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,18 +26,19 @@ import java.util.Iterator;
|
|||
|
||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.generation.type.KeyType;
|
||||
import org.pgpainless.key.generation.type.length.RsaLength;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
|
||||
public class GenerateKeyWithAdditionalUserIdTest {
|
||||
|
||||
@Test
|
||||
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
||||
PGPKeyRing keyRing = PGPainless.generateKeyRing()
|
||||
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
|
||||
.withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072))
|
||||
.withDefaultKeyFlags()
|
||||
.withDefaultAlgorithms())
|
||||
|
@ -47,8 +48,9 @@ public class GenerateKeyWithAdditionalUserIdTest {
|
|||
.withAdditionalUserId("\ttrimThis@user.id ")
|
||||
.withoutPassphrase()
|
||||
.build();
|
||||
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
||||
|
||||
Iterator<String> userIds = keyRing.getPublicKeys().getPublicKey().getUserIDs();
|
||||
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
|
||||
assertEquals("primary@user.id", userIds.next());
|
||||
assertEquals("additional@user.id", userIds.next());
|
||||
assertEquals("additional2@user.id", userIds.next());
|
||||
|
@ -57,7 +59,7 @@ public class GenerateKeyWithAdditionalUserIdTest {
|
|||
|
||||
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream armor = new ArmoredOutputStream(byteOut);
|
||||
keyRing.getSecretKeys().encode(armor);
|
||||
secretKeys.encode(armor);
|
||||
armor.close();
|
||||
|
||||
// echo this | gpg --list-packets
|
||||
|
|
|
@ -30,7 +30,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||
|
@ -40,8 +39,7 @@ public class AddUserIdTest {
|
|||
|
||||
@Test
|
||||
public void addUserIdToExistingKeyRing() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
|
||||
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit", "rabb1th0le");
|
||||
PGPSecretKeyRing secretKeys = keyRing.getSecretKeys();
|
||||
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit", "rabb1th0le");
|
||||
|
||||
Iterator<String> userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs();
|
||||
assertEquals("alice@wonderland.lit", userIds.next());
|
||||
|
|
|
@ -38,14 +38,13 @@ import org.junit.jupiter.api.Test;
|
|||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import org.pgpainless.encryption_signing.EncryptionStream;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.protection.KeyRingProtectionSettings;
|
||||
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
public class ChangeSecretKeyRingPassphraseTest {
|
||||
|
||||
private final PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("password@encryp.ted", "weakPassphrase");
|
||||
private final PGPSecretKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("password@encryp.ted", "weakPassphrase");
|
||||
|
||||
public ChangeSecretKeyRingPassphraseTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
|
||||
}
|
||||
|
@ -53,16 +52,16 @@ public class ChangeSecretKeyRingPassphraseTest {
|
|||
@Test
|
||||
public void changePassphraseOfWholeKeyRingTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
||||
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys())
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
|
||||
.changePassphraseFromOldPassphrase(Passphrase.fromPassword("weakPassphrase"))
|
||||
.withSecureDefaultSettings()
|
||||
.toNewPassphrase(Passphrase.fromPassword("1337p455phr453"))
|
||||
.done();
|
||||
|
||||
PGPKeyRing changedPassphraseKeyRing = new PGPKeyRing(secretKeys);
|
||||
PGPSecretKeyRing changedPassphraseKeyRing = secretKeys;
|
||||
|
||||
assertEquals(KeyRingProtectionSettings.secureDefaultSettings().getEncryptionAlgorithm().getAlgorithmId(),
|
||||
changedPassphraseKeyRing.getSecretKeys().getSecretKey().getKeyEncryptionAlgorithm());
|
||||
changedPassphraseKeyRing.getSecretKey().getKeyEncryptionAlgorithm());
|
||||
|
||||
assertThrows(PGPException.class, () ->
|
||||
signDummyMessageWithKeysAndPassphrase(changedPassphraseKeyRing, Passphrase.emptyPassphrase()),
|
||||
|
@ -78,16 +77,16 @@ public class ChangeSecretKeyRingPassphraseTest {
|
|||
|
||||
@Test
|
||||
public void changePassphraseOfWholeKeyRingToEmptyPassphrase() throws PGPException, IOException {
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys())
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
|
||||
.changePassphraseFromOldPassphrase(Passphrase.fromPassword("weakPassphrase"))
|
||||
.withSecureDefaultSettings()
|
||||
.toNoPassphrase()
|
||||
.done();
|
||||
|
||||
PGPKeyRing changedPassphraseKeyRing = new PGPKeyRing(secretKeys);
|
||||
PGPSecretKeyRing changedPassphraseKeyRing = secretKeys;
|
||||
|
||||
assertEquals(SymmetricKeyAlgorithm.NULL.getAlgorithmId(),
|
||||
changedPassphraseKeyRing.getSecretKeys().getSecretKey().getKeyEncryptionAlgorithm());
|
||||
changedPassphraseKeyRing.getSecretKey().getKeyEncryptionAlgorithm());
|
||||
|
||||
signDummyMessageWithKeysAndPassphrase(changedPassphraseKeyRing, Passphrase.emptyPassphrase());
|
||||
}
|
||||
|
@ -95,14 +94,14 @@ public class ChangeSecretKeyRingPassphraseTest {
|
|||
@Test
|
||||
public void changePassphraseOfSingleSubkeyToNewPassphrase() throws PGPException {
|
||||
|
||||
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys().getSecretKeys();
|
||||
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys();
|
||||
PGPSecretKey primaryKey = keys.next();
|
||||
PGPSecretKey subKey = keys.next();
|
||||
|
||||
extractPrivateKey(primaryKey, Passphrase.fromPassword("weakPassphrase"));
|
||||
extractPrivateKey(subKey, Passphrase.fromPassword("weakPassphrase"));
|
||||
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys())
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
|
||||
.changeSubKeyPassphraseFromOldPassphrase(subKey.getPublicKey().getKeyID(),
|
||||
Passphrase.fromPassword("weakPassphrase"))
|
||||
.withSecureDefaultSettings()
|
||||
|
@ -129,11 +128,11 @@ public class ChangeSecretKeyRingPassphraseTest {
|
|||
|
||||
@Test
|
||||
public void changePassphraseOfSingleSubkeyToEmptyPassphrase() throws PGPException {
|
||||
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys().getSecretKeys();
|
||||
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys();
|
||||
PGPSecretKey primaryKey = keys.next();
|
||||
PGPSecretKey subKey = keys.next();
|
||||
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys())
|
||||
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
|
||||
.changeSubKeyPassphraseFromOldPassphrase(primaryKey.getKeyID(), Passphrase.fromPassword("weakPassphrase"))
|
||||
.withSecureDefaultSettings()
|
||||
.toNoPassphrase()
|
||||
|
@ -177,12 +176,12 @@ public class ChangeSecretKeyRingPassphraseTest {
|
|||
secretKey.extractPrivateKey(decryptor);
|
||||
}
|
||||
|
||||
private void signDummyMessageWithKeysAndPassphrase(PGPKeyRing keyRing, Passphrase passphrase) throws IOException, PGPException {
|
||||
private void signDummyMessageWithKeysAndPassphrase(PGPSecretKeyRing keyRing, Passphrase passphrase) throws IOException, PGPException {
|
||||
String dummyMessage = "dummy";
|
||||
ByteArrayOutputStream dummy = new ByteArrayOutputStream();
|
||||
EncryptionStream stream = PGPainless.encryptAndOrSign().onOutputStream(dummy)
|
||||
.doNotEncrypt()
|
||||
.signWith(PasswordBasedSecretKeyRingProtector.forKey(keyRing.getSecretKeys(), passphrase), keyRing.getSecretKeys())
|
||||
.signWith(PasswordBasedSecretKeyRingProtector.forKey(keyRing, passphrase), keyRing)
|
||||
.noArmor();
|
||||
|
||||
Streams.pipeAll(new ByteArrayInputStream(dummyMessage.getBytes()), stream);
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.pgpainless.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
@ -23,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
|||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Iterator;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
@ -37,10 +35,10 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.key.collection.PGPKeyRing;
|
||||
import org.pgpainless.key.generation.KeySpec;
|
||||
import org.pgpainless.key.generation.type.KeyType;
|
||||
import org.pgpainless.key.generation.type.length.RsaLength;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
|
||||
public class BCUtilTest {
|
||||
|
||||
|
@ -50,12 +48,12 @@ public class BCUtilTest {
|
|||
public void keyRingToCollectionTest()
|
||||
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
PGPKeyRing ring = PGPainless.generateKeyRing()
|
||||
PGPSecretKeyRing sec = PGPainless.generateKeyRing()
|
||||
.withSubKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms())
|
||||
.withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms())
|
||||
.withPrimaryUserId("donald@duck.tails").withoutPassphrase().build();
|
||||
PGPSecretKeyRing sec = ring.getSecretKeys();
|
||||
PGPPublicKeyRing pub = ring.getPublicKeys();
|
||||
|
||||
PGPPublicKeyRing pub = KeyRingUtils.publicKeyRingFrom(sec);
|
||||
|
||||
LOGGER.log(Level.FINER, "Main ID: " + sec.getPublicKey().getKeyID() + " " + pub.getPublicKey().getKeyID());
|
||||
|
||||
|
@ -107,11 +105,11 @@ public class BCUtilTest {
|
|||
public void removeUnsignedKeysTest()
|
||||
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
|
||||
@SuppressWarnings("deprecation")
|
||||
PGPKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024);
|
||||
PGPKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory");
|
||||
PGPSecretKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024);
|
||||
PGPSecretKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory");
|
||||
|
||||
PGPSecretKey subKey = null;
|
||||
Iterator<PGPSecretKey> sit = mallory.getSecretKeys().getSecretKeys();
|
||||
Iterator<PGPSecretKey> sit = mallory.getSecretKeys();
|
||||
while (sit.hasNext()) {
|
||||
PGPSecretKey s = sit.next();
|
||||
if (!s.isMasterKey()) {
|
||||
|
@ -122,29 +120,12 @@ public class BCUtilTest {
|
|||
|
||||
assertNotNull(subKey);
|
||||
|
||||
PGPSecretKeyRing alice_mallory = PGPSecretKeyRing.insertSecretKey(alice.getSecretKeys(), subKey);
|
||||
PGPSecretKeyRing alice_mallory = PGPSecretKeyRing.insertSecretKey(alice, subKey);
|
||||
|
||||
// Check, if alice_mallory contains mallory's key
|
||||
assertNotNull(alice_mallory.getSecretKey(subKey.getKeyID()));
|
||||
|
||||
PGPSecretKeyRing cleaned = BCUtil.removeUnassociatedKeysFromKeyRing(alice_mallory, alice.getPublicKeys().getPublicKey());
|
||||
PGPSecretKeyRing cleaned = BCUtil.removeUnassociatedKeysFromKeyRing(alice_mallory, alice.getPublicKey());
|
||||
assertNull(cleaned.getSecretKey(subKey.getKeyID()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeUnsignedKeysECTest()
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
PGPKeyRing ring = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit");
|
||||
PGPPublicKeyRing publicKeys = ring.getPublicKeys();
|
||||
PGPSecretKeyRing secretKeys = ring.getSecretKeys();
|
||||
PGPSecretKeyRing secCleaned = ring.getSecretKeys();
|
||||
|
||||
assertArrayEquals(secretKeys.getEncoded(), secCleaned.getEncoded());
|
||||
|
||||
PGPPublicKeyRing pubCleaned = BCUtil.removeUnassociatedKeysFromKeyRing(publicKeys, publicKeys.getPublicKey());
|
||||
|
||||
assertArrayEquals(publicKeys.getEncoded(), pubCleaned.getEncoded());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue