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

Tests: Avoid usage of now deprecated functionality

This commit is contained in:
Paul Schaub 2025-03-08 10:56:55 +01:00
parent 42c262a99f
commit 34633cfeac
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
14 changed files with 119 additions and 101 deletions

View file

@ -54,7 +54,7 @@ public final class GnuPGDummyKeyUtil {
int mode = s2K.getProtectionMode();
// TODO: Is GNU_DUMMY_S2K appropriate?
if (type == S2K.GNU_DUMMY_S2K && mode == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD) {
SubkeyIdentifier hardwareBackedKey = new SubkeyIdentifier(secretKeys, secretKey.getKeyID());
SubkeyIdentifier hardwareBackedKey = new SubkeyIdentifier(secretKeys, secretKey.getKeyIdentifier());
hardwareBackedKeys.add(hardwareBackedKey);
}
}

View file

@ -10,6 +10,8 @@ import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSecretKey
import org.bouncycastle.openpgp.api.OpenPGPCertificate
import org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey
class OpenPgpV4Fingerprint : OpenPgpFingerprint {
@ -17,6 +19,10 @@ class OpenPgpV4Fingerprint : OpenPgpFingerprint {
constructor(bytes: ByteArray) : super(bytes)
constructor(key: OpenPGPCertificate) : super(key.fingerprint)
constructor(key: OpenPGPComponentKey) : super(key.pgpPublicKey)
constructor(key: PGPPublicKey) : super(key)
constructor(key: PGPSecretKey) : super(key)

View file

@ -31,10 +31,12 @@ interface SecretKeyPassphraseProvider {
* @param keyId if of the secret key
* @return passphrase or null, if no passphrase record has been found.
*/
@Deprecated("Pass in a KeyIdentifier instead.")
fun getPassphraseFor(keyId: Long): Passphrase? = getPassphraseFor(KeyIdentifier(keyId))
fun getPassphraseFor(keyIdentifier: KeyIdentifier): Passphrase?
@Deprecated("Pass in a KeyIdentifier instead.")
fun hasPassphrase(keyId: Long): Boolean = hasPassphrase(KeyIdentifier(keyId))
fun hasPassphrase(keyIdentifier: KeyIdentifier): Boolean

View file

@ -15,8 +15,8 @@ import java.nio.charset.StandardCharsets;
import java.util.List;
import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -181,7 +181,7 @@ public class AsciiArmorCRCTests {
"=AAAA\n" +
"-----END PGP PRIVATE KEY BLOCK-----";
assertThrows(IOException.class, () -> PGPainless.readKeyRing().secretKeyRing(KEY));
assertThrows(IOException.class, () -> PGPainless.getInstance().readKey().parseKeysOrCertificates(KEY));
}
@Test
@ -229,7 +229,7 @@ public class AsciiArmorCRCTests {
"=AAAA\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
assertThrows(IOException.class, () -> PGPainless.readKeyRing().publicKeyRing(CERT));
assertThrows(IOException.class, () -> PGPainless.getInstance().readKey().parseCertificate(CERT));
}
@Test
@ -364,7 +364,7 @@ public class AsciiArmorCRCTests {
"xqAY9Bwizt4FWgXuLm1a4+So4V9j1TRCXd12Uc2l2RNmgDE=\n" +
"-----END PGP PRIVATE KEY BLOCK-----";
PGPSecretKeyRing key = PGPainless.readKeyRing().secretKeyRing(KEY);
OpenPGPKey key = PGPainless.getInstance().readKey().parseKey(KEY);
assertNotNull(key);
assertEquals(new OpenPgpV4Fingerprint("D1A66E1A23B182C9980F788CFBFCC82A015E7330"), new OpenPgpV4Fingerprint(key));
}
@ -540,7 +540,7 @@ public class AsciiArmorCRCTests {
"=FdCC\n" +
"-----END PGP MESSAGE-----\n";
PGPSecretKeyRing key = PGPainless.readKeyRing().secretKeyRing(ASCII_KEY);
OpenPGPKey key = PGPainless.getInstance().readKey().parseKey(ASCII_KEY);
assertThrows(IOException.class, () -> {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)))

View file

@ -275,7 +275,7 @@ public class GnuPGDummyKeyUtilTest {
.divertPrivateKeysToCard(GnuPGDummyKeyUtil.KeyFilter.any());
Set<SubkeyIdentifier> expected = new HashSet<>();
for (PGPSecretKey key : secretKeys.getPGPSecretKeyRing()) {
expected.add(new SubkeyIdentifier(secretKeys.getPGPSecretKeyRing(), key.getKeyID()));
expected.add(new SubkeyIdentifier(secretKeys.getPGPSecretKeyRing(), key.getKeyIdentifier()));
}
Set<SubkeyIdentifier> hardwareBackedKeys = GnuPGDummyKeyUtil

View file

@ -16,9 +16,9 @@ import java.security.NoSuchAlgorithmException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
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;
@ -60,25 +60,23 @@ public class BcHashContextSignerTest {
@Test
public void signContextWithEdDSAKeys() throws PGPException, NoSuchAlgorithmException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
OpenPGPKey secretKeys = PGPainless.getInstance().readKey().parseKey(KEY);
signWithKeys(secretKeys);
}
@Test
public void signContextWithRSAKeys() throws PGPException, NoSuchAlgorithmException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleRsaKeyRing("Sigfried", RsaLength._3072)
.getPGPSecretKeyRing();
OpenPGPKey secretKeys = PGPainless.generateKeyRing().simpleRsaKeyRing("Sigfried", RsaLength._3072);
signWithKeys(secretKeys);
}
@Test
public void signContextWithEcKeys() throws PGPException, NoSuchAlgorithmException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("Sigfried")
.getPGPSecretKeyRing();
OpenPGPKey secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("Sigfried");
signWithKeys(secretKeys);
}
private void signWithKeys(PGPSecretKeyRing secretKeys) throws PGPException, NoSuchAlgorithmException, IOException {
private void signWithKeys(OpenPGPKey secretKeys) throws PGPException, NoSuchAlgorithmException, IOException {
for (HashAlgorithm hashAlgorithm : new HashAlgorithm[] {
HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512
}) {
@ -86,9 +84,9 @@ public class BcHashContextSignerTest {
}
}
private void signFromContext(PGPSecretKeyRing secretKeys, HashAlgorithm hashAlgorithm)
private void signFromContext(OpenPGPKey secretKeys, HashAlgorithm hashAlgorithm)
throws PGPException, NoSuchAlgorithmException, IOException {
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKeys);
OpenPGPCertificate certificate = secretKeys.toCertificate();
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream messageIn = new ByteArrayInputStream(messageBytes);
@ -110,13 +108,13 @@ public class BcHashContextSignerTest {
assertTrue(metadata.isVerifiedSigned());
}
private PGPSignature signMessage(byte[] message, HashAlgorithm hashAlgorithm, PGPSecretKeyRing secretKeys)
private PGPSignature signMessage(byte[] message, HashAlgorithm hashAlgorithm, OpenPGPKey secretKeys)
throws NoSuchAlgorithmException {
// Prepare the hash context
// This would be done by the caller application
MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithmName(), new BouncyCastleProvider());
messageDigest.update(message);
return BcHashContextSigner.signHashContext(messageDigest, SignatureType.BINARY_DOCUMENT, secretKeys, SecretKeyRingProtector.unprotectedKeys());
return BcHashContextSigner.signHashContext(messageDigest, SignatureType.BINARY_DOCUMENT, secretKeys.getPGPSecretKeyRing(), SecretKeyRingProtector.unprotectedKeys());
}
}

View file

@ -7,6 +7,7 @@ package org.pgpainless.encryption_signing;
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;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -148,7 +149,7 @@ public class EncryptDecryptTest {
assertFalse(encryptionResult.getRecipients().isEmpty());
for (SubkeyIdentifier encryptionKey : encryptionResult.getRecipients()) {
assertTrue(KeyRingUtils.keyRingContainsKeyWithId(recipientPub, encryptionKey.getKeyId()));
assertNotNull(recipientPub.getPublicKey(encryptionKey.getKeyIdentifier()));
}
assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getEncryptionAlgorithm());

View file

@ -8,6 +8,7 @@ import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -19,7 +20,7 @@ public class _64DigitFingerprintTest {
String prettyPrint = "76543210 ABCDEFAB 01AB23CD 1C0FFEE1 1EEFF0C1 DC32BA10 BAFEDCBA 01234567";
OpenPgpFingerprint parsed = OpenPgpFingerprint.parse(prettyPrint);
assertTrue(parsed instanceof _64DigitFingerprint);
assertInstanceOf(_64DigitFingerprint.class, parsed);
assertEquals(prettyPrint, parsed.prettyPrint());
assertEquals(-1, parsed.getVersion());
}
@ -30,7 +31,7 @@ public class _64DigitFingerprintTest {
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof _64DigitFingerprint);
assertInstanceOf(_64DigitFingerprint.class, fingerprint);
assertEquals(hex, fingerprint.toString());
OpenPgpV5Fingerprint v5 = new OpenPgpV5Fingerprint(binary);

View file

@ -22,6 +22,7 @@ import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
@ -117,8 +118,8 @@ public class KeyRingInfoTest {
assertEquals(revocationDate.getTime(), rInfo.getRevocationDate().getTime(), 5);
assertEquals(revocationDate.getTime(), rInfo.getLastModified().getTime(), 5);
assertFalse(pInfo.isKeyValidlyBound(1230));
assertFalse(sInfo.isKeyValidlyBound(1230));
assertFalse(pInfo.isKeyValidlyBound(new KeyIdentifier(1230)));
assertFalse(sInfo.isKeyValidlyBound(new KeyIdentifier(1230)));
}
@Test
@ -162,7 +163,9 @@ public class KeyRingInfoTest {
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys), info.getSecretKey().getPGPSecretKey());
OpenPGPKey.OpenPGPSecretKey primarySecretKey = info.getSecretKey();
assertNotNull(primarySecretKey);
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys), primarySecretKey.getPGPSecretKey());
info = PGPainless.inspectKeyRing(publicKeys);
assertNull(info.getSecretKey());
@ -173,7 +176,7 @@ public class KeyRingInfoTest {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
assertEquals(KeyRingUtils.requirePrimaryPublicKeyFrom(secretKeys), info.getPublicKey().getPGPPublicKey());
assertEquals(KeyRingUtils.requirePrimaryPublicKeyFrom(secretKeys), info.getPrimaryKey().getPGPPublicKey());
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys),
KeyRingUtils.requireSecretKeyFrom(secretKeys, secretKeys.getPublicKey().getKeyID()));
@ -213,8 +216,8 @@ public class KeyRingInfoTest {
"=gU+0\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(withDummyS2K);
assertTrue(new KeyInfo(secretKeys.getSecretKey()).hasDummyS2K());
OpenPGPKey secretKeys = PGPainless.getInstance().readKey().parseKey(withDummyS2K);
assertTrue(new KeyInfo(secretKeys.getPrimarySecretKey().getPGPSecretKey()).hasDummyS2K());
}
@TestTemplate
@ -350,11 +353,11 @@ public class KeyRingInfoTest {
"crH02GDG8CotAnEHkLTz9GPO80q8mowzBV0EtHsXb4TeAFw5T5Qd0a5I+wk=\n" +
"=Vcb3\n" +
"-----END PGP ARMORED FILE-----\n";
PGPPublicKeyRing keys = PGPainless.readKeyRing().publicKeyRing(KEY);
OpenPGPCertificate certificate = PGPainless.getInstance().readKey().parseCertificate(KEY);
KeyRingInfo info = new KeyRingInfo(keys, DateUtil.parseUTCDate("2021-10-10 00:00:00 UTC"));
KeyRingInfo info = PGPainless.inspectKeyRing(certificate, DateUtil.parseUTCDate("2021-10-10 00:00:00 UTC"));
// Subkey is hard revoked
assertFalse(info.isKeyValidlyBound(5364407983539305061L));
assertFalse(info.isKeyValidlyBound(new KeyIdentifier(5364407983539305061L)));
}
@Test
@ -430,14 +433,14 @@ public class KeyRingInfoTest {
"=7Feh\n" +
"-----END PGP ARMORED FILE-----\n";
PGPPublicKeyRing keys = PGPainless.readKeyRing().publicKeyRing(KEY);
final long subkeyId = 5364407983539305061L;
OpenPGPCertificate certificate = PGPainless.getInstance().readKey().parseCertificate(KEY);
final KeyIdentifier subkeyId = new KeyIdentifier(5364407983539305061L);
KeyRingInfo inspectDuringRevokedPeriod = new KeyRingInfo(keys, DateUtil.parseUTCDate("2019-01-02 00:00:00 UTC"));
KeyRingInfo inspectDuringRevokedPeriod = PGPainless.inspectKeyRing(certificate, DateUtil.parseUTCDate("2019-01-02 00:00:00 UTC"));
assertFalse(inspectDuringRevokedPeriod.isKeyValidlyBound(subkeyId));
assertNotNull(inspectDuringRevokedPeriod.getSubkeyRevocationSignature(subkeyId));
KeyRingInfo inspectAfterRebinding = new KeyRingInfo(keys, DateUtil.parseUTCDate("2020-01-02 00:00:00 UTC"));
KeyRingInfo inspectAfterRebinding = PGPainless.inspectKeyRing(certificate, DateUtil.parseUTCDate("2020-01-02 00:00:00 UTC"));
assertTrue(inspectAfterRebinding.isKeyValidlyBound(subkeyId));
}
@ -514,11 +517,11 @@ public class KeyRingInfoTest {
"=MhJL\n" +
"-----END PGP ARMORED FILE-----\n";
PGPPublicKeyRing keys = PGPainless.readKeyRing().publicKeyRing(KEY);
OpenPGPCertificate keys = PGPainless.getInstance().readKey().parseCertificate(KEY);
KeyRingInfo info = PGPainless.inspectKeyRing(keys);
// Primary key is hard revoked
assertFalse(info.isKeyValidlyBound(keys.getPublicKey().getKeyID()));
assertFalse(info.isKeyValidlyBound(keys.getKeyIdentifier()));
assertFalse(info.isFullyEncrypted());
}
@ -531,7 +534,7 @@ public class KeyRingInfoTest {
OpenPgpV4Fingerprint primaryKeyFingerprint = new OpenPgpV4Fingerprint(secretKeys);
OpenPGPKey.OpenPGPSecretKey primaryKey = info.getSecretKey(primaryKeyFingerprint);
assertNotNull(primaryKey);
assertEquals(key.getPrimarySecretKey().getKeyIdentifier(), primaryKey.getKeyIdentifier());
}
@ -597,10 +600,10 @@ public class KeyRingInfoTest {
"=7gbt\n" +
"-----END PGP PRIVATE KEY BLOCK-----";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
final long pkid = 6643807985200014832L;
final long skid1 = -2328413746552029063L;
final long skid2 = -3276877650571760552L;
OpenPGPKey secretKeys = PGPainless.getInstance().readKey().parseKey(KEY);
final KeyIdentifier pkid = new KeyIdentifier(6643807985200014832L);
final KeyIdentifier skid1 = new KeyIdentifier(-2328413746552029063L);
final KeyIdentifier skid2 = new KeyIdentifier(-3276877650571760552L);
Set<HashAlgorithm> preferredHashAlgorithms = new LinkedHashSet<>(
Arrays.asList(HashAlgorithm.SHA512, HashAlgorithm.SHA384, HashAlgorithm.SHA256, HashAlgorithm.SHA224));
Set<CompressionAlgorithm> preferredCompressionAlgorithms = new LinkedHashSet<>(
@ -612,7 +615,7 @@ public class KeyRingInfoTest {
// Bob is an invalid userId
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms(new KeyIdentifier(123L)));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms("Alice"));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(pkid));
@ -622,7 +625,7 @@ public class KeyRingInfoTest {
// Bob is an invalid userId
assertThrows(NoSuchElementException.class, () -> info.getPreferredCompressionAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(NoSuchElementException.class, () -> info.getPreferredCompressionAlgorithms(123L));
assertThrows(NoSuchElementException.class, () -> info.getPreferredCompressionAlgorithms(new KeyIdentifier(123L)));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms("Alice"));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(pkid));
@ -632,7 +635,7 @@ public class KeyRingInfoTest {
// Bob is an invalid userId
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms(new KeyIdentifier(123L)));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms("Alice"));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(pkid));
@ -691,11 +694,11 @@ public class KeyRingInfoTest {
"=A3B8\n" +
"-----END PGP PUBLIC KEY BLOCK-----\n";
PGPPublicKeyRing certificate = PGPainless.readKeyRing().publicKeyRing(KEY);
OpenPGPCertificate certificate = PGPainless.getInstance().readKey().parseCertificate(KEY);
OpenPgpV4Fingerprint unboundKey = new OpenPgpV4Fingerprint("D622C916384E0F6D364907E55D918BBD521CCD10");
KeyRingInfo info = PGPainless.inspectKeyRing(certificate);
assertFalse(info.isKeyValidlyBound(unboundKey.getKeyId()));
assertFalse(info.isKeyValidlyBound(unboundKey.getKeyIdentifier()));
List<OpenPGPCertificate.OpenPGPComponentKey> encryptionSubkeys = info.getEncryptionSubkeys(EncryptionPurpose.ANY);
assertTrue(encryptionSubkeys.stream()
@ -709,11 +712,11 @@ public class KeyRingInfoTest {
.noneMatch(f -> f.equals(unboundKey)),
"Unbound subkey MUST NOT be considered a valid signing subkey");
assertTrue(info.getKeyFlagsOf(unboundKey.getKeyId()).isEmpty());
assertTrue(info.getKeyFlagsOf(unboundKey.getKeyIdentifier()).isEmpty());
Date latestModification = info.getLastModified();
Date latestKeyCreation = info.getLatestKeyCreationDate();
Date unboundKeyCreation = certificate.getPublicKey(unboundKey.getKeyId()).getCreationTime();
Date unboundKeyCreation = certificate.getKey(unboundKey.getKeyIdentifier()).getCreationTime();
assertTrue(unboundKeyCreation.after(latestModification));
assertTrue(unboundKeyCreation.after(latestKeyCreation));
}
@ -770,7 +773,7 @@ public class KeyRingInfoTest {
"=ZRAy\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
OpenPGPKey secretKeys = PGPainless.getInstance().readKey().parseKey(KEY);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
List<String> emails = info.getEmailAddresses();
@ -802,7 +805,7 @@ public class KeyRingInfoTest {
"=nFoO\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
PGPPublicKeyRing cert = PGPainless.readKeyRing().publicKeyRing(CERT);
OpenPGPCertificate cert = PGPainless.getInstance().readKey().parseCertificate(CERT);
KeyRingInfo info = PGPainless.inspectKeyRing(cert);
assertTrue(info.isUsableForEncryption());
}
@ -830,7 +833,7 @@ public class KeyRingInfoTest {
"=etPP\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(CERT);
OpenPGPCertificate publicKeys = PGPainless.getInstance().readKey().parseCertificate(CERT);
KeyRingInfo info = PGPainless.inspectKeyRing(publicKeys);
assertTrue(info.isUsableForEncryption(EncryptionPurpose.COMMUNICATIONS));
@ -866,7 +869,7 @@ public class KeyRingInfoTest {
"AQCjeV+3VT+u1movwIYv4XkzB6gB+B2C+DK9nvG5sXZhBg==\n" +
"=uqmO\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(CERT);
OpenPGPCertificate publicKeys = PGPainless.getInstance().readKey().parseCertificate(CERT);
KeyRingInfo info = PGPainless.inspectKeyRing(publicKeys);
assertFalse(info.isUsableForEncryption());

View file

@ -13,6 +13,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
@ -40,9 +41,9 @@ public class AddSubKeyTest {
throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
List<Long> keyIdsBefore = new ArrayList<>();
List<KeyIdentifier> keyIdentifiersBefore = new ArrayList<>();
for (Iterator<PGPPublicKey> it = secretKeys.getPublicKeys(); it.hasNext(); ) {
keyIdsBefore.add(it.next().getKeyID());
keyIdentifiersBefore.add(it.next().getKeyIdentifier());
}
secretKeys = PGPainless.modifyKeyRing(secretKeys)
@ -52,21 +53,21 @@ public class AddSubKeyTest {
PasswordBasedSecretKeyRingProtector.forKey(secretKeys, Passphrase.fromPassword("password123")))
.done();
List<Long> keyIdsAfter = new ArrayList<>();
List<KeyIdentifier> keyIdentifiersAfter = new ArrayList<>();
for (Iterator<PGPPublicKey> it = secretKeys.getPublicKeys(); it.hasNext(); ) {
keyIdsAfter.add(it.next().getKeyID());
keyIdentifiersAfter.add(it.next().getKeyIdentifier());
}
assertNotEquals(keyIdsAfter, keyIdsBefore);
assertNotEquals(keyIdentifiersAfter, keyIdentifiersBefore);
keyIdsAfter.removeAll(keyIdsBefore);
long subKeyId = keyIdsAfter.get(0);
keyIdentifiersAfter.removeAll(keyIdentifiersBefore);
KeyIdentifier subKeyIdentifier = keyIdentifiersAfter.get(0);
PGPSecretKey subKey = secretKeys.getSecretKey(subKeyId);
PGPSecretKey subKey = secretKeys.getSecretKey(subKeyIdentifier);
SecretKeyRingProtector protector = SecretKeyRingProtector.unlockEachKeyWith(
Passphrase.fromPassword("subKeyPassphrase"), secretKeys);
UnlockSecretKey.unlockSecretKey(subKey, protector);
KeyRingInfo info = new KeyRingInfo(secretKeys);
assertEquals(Collections.singletonList(KeyFlag.SIGN_DATA), info.getKeyFlagsOf(subKeyId));
assertEquals(Collections.singletonList(KeyFlag.SIGN_DATA), info.getKeyFlagsOf(subKeyIdentifier));
}
}

View file

@ -17,6 +17,7 @@ import java.util.NoSuchElementException;
import openpgp.DateExtensionsKt;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
@ -95,7 +96,8 @@ public class AddUserIdTest {
"=bk4o\r\n" +
"-----END PGP PRIVATE KEY BLOCK-----\r\n";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(ARMORED_PRIVATE_KEY);
OpenPGPKey key = PGPainless.getInstance().readKey().parseKey(ARMORED_PRIVATE_KEY);
PGPSecretKeyRing secretKeys = key.getPGPSecretKeyRing();
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
Iterator<String> userIds = info.getValidUserIds().iterator();
assertEquals("<user@example.com>", userIds.next());
@ -119,7 +121,7 @@ public class AddUserIdTest {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.modernKeyRing("Alice")
.getPGPSecretKeyRing();
UserId bob = UserId.newBuilder().withName("Bob").noEmail().noComment().build();
UserId bob = UserId.builder().withName("Bob").noEmail().noComment().build();
assertNotEquals("Bob", PGPainless.inspectKeyRing(secretKeys).getPrimaryUserId());

View file

@ -51,27 +51,29 @@ public class CachingSecretKeyRingProtectorTest {
}
@Test
public void noCallbackReturnsNullForUnknownKeyId() throws PGPException {
assertNull(protector.getDecryptor(123L));
public void noCallbackReturnsNullForUnknownKeyId() {
assertNull(protector.getDecryptor(new KeyIdentifier(123L)));
}
@Test
public void testAddPassphrase() throws PGPException {
public void testAddPassphrase() {
KeyIdentifier k123 = new KeyIdentifier(123L);
Passphrase passphrase = Passphrase.fromPassword("HelloWorld");
protector.addPassphrase(new KeyIdentifier(123L), passphrase);
assertEquals(passphrase, protector.getPassphraseFor(123L));
assertNotNull(protector.getDecryptor(123L));
protector.addPassphrase(k123, passphrase);
assertEquals(passphrase, protector.getPassphraseFor(k123));
assertNotNull(protector.getDecryptor(k123));
assertNull(protector.getPassphraseFor(999L));
assertNull(protector.getPassphraseFor(new KeyIdentifier(999L)));
}
@Test
public void testForgetPassphrase() {
KeyIdentifier k123 = new KeyIdentifier(123L);
Passphrase passphrase = Passphrase.fromPassword("amnesiac");
protector.addPassphrase(123L, passphrase);
assertEquals(passphrase, protector.getPassphraseFor(123L));
protector.forgetPassphrase(123L);
assertNull(protector.getPassphraseFor(123L));
protector.addPassphrase(k123, passphrase);
assertEquals(passphrase, protector.getPassphraseFor(k123));
protector.forgetPassphrase(k123);
assertNull(protector.getPassphraseFor(k123));
}
@Test
@ -87,11 +89,11 @@ public class CachingSecretKeyRingProtectorTest {
PGPSecretKey key = it.next();
assertEquals(passphrase, protector.getPassphraseFor(key));
assertNotNull(protector.getEncryptor(key.getPublicKey()));
assertNotNull(protector.getDecryptor(key.getKeyID()));
assertNotNull(protector.getDecryptor(key.getKeyIdentifier()));
}
long nonMatching = findNonMatchingKeyId(keys);
assertNull(protector.getPassphraseFor(nonMatching));
assertNull(protector.getPassphraseFor(new KeyIdentifier(nonMatching)));
protector.forgetPassphrase(keys);
it = keys.getSecretKeys();
@ -99,7 +101,7 @@ public class CachingSecretKeyRingProtectorTest {
PGPSecretKey key = it.next();
assertNull(protector.getPassphraseFor(key));
assertNull(protector.getEncryptor(key.getPublicKey()));
assertNull(protector.getDecryptor(key.getKeyID()));
assertNull(protector.getDecryptor(key.getKeyIdentifier()));
}
}
@ -123,13 +125,13 @@ public class CachingSecretKeyRingProtectorTest {
CachingSecretKeyRingProtector withCallback = new CachingSecretKeyRingProtector(dummyCallback);
for (int i = -5; i <= 5; i++) {
long x = i * 5;
long doubled = x * 2;
KeyIdentifier x = new KeyIdentifier(i * 5);
KeyIdentifier doubled = new KeyIdentifier(x.getKeyId() * 2);
Passphrase passphrase = withCallback.getPassphraseFor(x);
assertNotNull(passphrase);
assertNotNull(passphrase.getChars());
assertEquals(doubled, Long.parseLong(new String(passphrase.getChars())));
assertEquals(doubled, new KeyIdentifier(Long.parseLong(new String(passphrase.getChars()))));
}
}

View file

@ -29,10 +29,10 @@ public class MapBasedPassphraseProviderTest {
passphraseMap.put(new KeyIdentifier(69696969L), Passphrase.emptyPassphrase());
MapBasedPassphraseProvider provider = new MapBasedPassphraseProvider(passphraseMap);
assertEquals(Passphrase.fromPassword("tiger"), provider.getPassphraseFor(1L));
assertEquals(Passphrase.fromPassword("snake"), provider.getPassphraseFor(123123123L));
assertEquals(Passphrase.emptyPassphrase(), provider.getPassphraseFor(69696969L));
assertNull(provider.getPassphraseFor(555L));
assertEquals(Passphrase.fromPassword("tiger"), provider.getPassphraseFor(new KeyIdentifier(1L)));
assertEquals(Passphrase.fromPassword("snake"), provider.getPassphraseFor(new KeyIdentifier((123123123L))));
assertEquals(Passphrase.emptyPassphrase(), provider.getPassphraseFor(new KeyIdentifier(69696969L)));
assertNull(provider.getPassphraseFor(new KeyIdentifier(555L)));
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
passphraseMap = new ConcurrentHashMap<>();

View file

@ -41,7 +41,7 @@ public class SecretKeyRingProtectorTest {
SecretKeyRingProtector protector =
SecretKeyRingProtector.unlockEachKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKeys);
for (PGPSecretKey secretKey : secretKeys) {
PBESecretKeyDecryptor decryptor = protector.getDecryptor(secretKey.getKeyID());
PBESecretKeyDecryptor decryptor = protector.getDecryptor(secretKey.getKeyIdentifier());
assertNotNull(decryptor);
secretKey.extractPrivateKey(decryptor);
}
@ -49,10 +49,10 @@ public class SecretKeyRingProtectorTest {
"SecurePassword")
.getPGPSecretKeyRing();
for (PGPSecretKey unrelatedKey : unrelatedKeys) {
PBESecretKeyDecryptor decryptor = protector.getDecryptor(unrelatedKey.getKeyID());
PBESecretKeyDecryptor decryptor = protector.getDecryptor(unrelatedKey.getKeyIdentifier());
assertNull(decryptor);
assertThrows(PGPException.class,
() -> unrelatedKey.extractPrivateKey(protector.getDecryptor(unrelatedKey.getKeyID())));
() -> unrelatedKey.extractPrivateKey(protector.getDecryptor(unrelatedKey.getKeyIdentifier())));
}
}
@ -61,8 +61,8 @@ public class SecretKeyRingProtectorTest {
Random random = new Random();
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
for (int i = 0; i < 10; i++) {
Long keyId = random.nextLong();
assertNull(protector.getDecryptor(keyId));
KeyIdentifier keyIdentifier = new KeyIdentifier(random.nextLong());
assertNull(protector.getDecryptor(keyIdentifier));
}
}
@ -78,27 +78,29 @@ public class SecretKeyRingProtectorTest {
SecretKeyRingProtector protector =
SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKey);
assertNotNull(protector.getDecryptor(secretKey.getKeyID()));
assertNotNull(protector.getDecryptor(secretKey.getKeyIdentifier()));
assertNotNull(protector.getEncryptor(secretKey.getPublicKey()));
assertNull(protector.getEncryptor(subKey.getPublicKey()));
assertNull(protector.getDecryptor(subKey.getKeyID()));
assertNull(protector.getDecryptor(subKey.getKeyIdentifier()));
}
@Test
public void testFromPassphraseMap() {
Map<KeyIdentifier, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(new KeyIdentifier(1L), Passphrase.emptyPassphrase());
KeyIdentifier k1 = new KeyIdentifier(1L);
KeyIdentifier k5 = new KeyIdentifier(5L);
passphraseMap.put(k1, Passphrase.emptyPassphrase());
CachingSecretKeyRingProtector protector =
(CachingSecretKeyRingProtector) SecretKeyRingProtector.fromPassphraseMap(passphraseMap);
assertNotNull(protector.getPassphraseFor(1L));
assertNull(protector.getPassphraseFor(5L));
assertNotNull(protector.getPassphraseFor(k1));
assertNull(protector.getPassphraseFor(k5));
protector.addPassphrase(5L, Passphrase.fromPassword("pa55w0rd"));
protector.forgetPassphrase(1L);
protector.addPassphrase(k5, Passphrase.fromPassword("pa55w0rd"));
protector.forgetPassphrase(k1);
assertNull(protector.getPassphraseFor(1L));
assertNotNull(protector.getPassphraseFor(5L));
assertNull(protector.getPassphraseFor(k1));
assertNotNull(protector.getPassphraseFor(k5));
}
@Test
@ -118,7 +120,7 @@ public class SecretKeyRingProtectorTest {
}
});
assertEquals(Passphrase.emptyPassphrase(), protector.getPassphraseFor(1L));
assertEquals(Passphrase.fromPassword("missingP455w0rd"), protector.getPassphraseFor(3L));
assertEquals(Passphrase.emptyPassphrase(), protector.getPassphraseFor(new KeyIdentifier(1L)));
assertEquals(Passphrase.fromPassword("missingP455w0rd"), protector.getPassphraseFor(new KeyIdentifier(3L)));
}
}