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

Even more migration and code compiles again

This commit is contained in:
Paul Schaub 2025-02-11 16:17:48 +01:00
parent c039ab543a
commit 070879ee02
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
50 changed files with 368 additions and 245 deletions

View file

@ -21,6 +21,7 @@ import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
@ -176,12 +177,13 @@ public class InvestigateMultiSEIPMessageHandlingTest {
@Test
public void testDecryptAndVerifyDetectsAppendedSEIPData() throws IOException, PGPException {
PGPSecretKeyRing ring1 = PGPainless.readKeyRing().secretKeyRing(KEY1);
PGPSecretKeyRing ring2 = PGPainless.readKeyRing().secretKeyRing(KEY2);
PGPainless api = PGPainless.getInstance();
OpenPGPKey ring1 = api.readKey().parseKey(KEY1);
OpenPGPKey ring2 = api.readKey().parseKey(KEY2);
ConsumerOptions options = new ConsumerOptions()
.addVerificationCert(PGPainless.extractCertificate(ring1))
.addVerificationCert(PGPainless.extractCertificate(ring2))
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(ring2)
.addVerificationCert(ring2)
.addDecryptionKey(ring1);
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()

View file

@ -124,7 +124,7 @@ public class OnePassSignatureVerificationWithPartialLengthLiteralDataRegressionT
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(cert)
.addDecryptionKey(secretKeys));

View file

@ -544,7 +544,7 @@ public class AsciiArmorCRCTests {
assertThrows(IOException.class, () -> {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions().addDecryptionKey(
.withOptions(ConsumerOptions.get().addDecryptionKey(
key, SecretKeyRingProtector.unlockAnyKeyWith(passphrase)
));

View file

@ -52,7 +52,7 @@ public class DecryptAndVerifyMessageTest {
public void decryptMessageAndVerifySignatureTest() throws Exception {
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addDecryptionKey(juliet)
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(juliet));
@ -87,7 +87,7 @@ public class DecryptAndVerifyMessageTest {
public void decryptMessageAndReadBeyondEndTest() throws Exception {
final String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
final ConsumerOptions options = new ConsumerOptions()
final ConsumerOptions options = ConsumerOptions.get()
.addDecryptionKey(juliet)
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(juliet));
@ -105,7 +105,7 @@ public class DecryptAndVerifyMessageTest {
public void decryptMessageAndVerifySignatureByteByByteTest() throws Exception {
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addDecryptionKey(juliet)
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(juliet));

View file

@ -128,7 +128,7 @@ public class DecryptHiddenRecipientMessageTest {
"=1knQ\n" +
"-----END PGP MESSAGE-----\n";
ByteArrayInputStream messageIn = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addDecryptionKey(secretKeys);
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()

View file

@ -177,7 +177,7 @@ public class IgnoreUnknownSignatureVersionsTest {
private MessageMetadata verifySignature(PGPPublicKeyRing cert, String BASE_CASE) throws PGPException, IOException {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify().onInputStream(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(cert)
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(BASE_CASE.getBytes(StandardCharsets.UTF_8))));

View file

@ -16,11 +16,13 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.util.io.Streams;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -62,13 +64,13 @@ public class MissingPassphraseForDecryptionTest {
// interactive callback
SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
public Passphrase getPassphraseFor(@NotNull KeyIdentifier keyIdentifier) {
// is called in interactive mode
return Passphrase.fromPassword(passphrase);
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@NotNull KeyIdentifier keyIdentifier) {
return true;
}
};
@ -95,13 +97,13 @@ public class MissingPassphraseForDecryptionTest {
SecretKeyPassphraseProvider callback = new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
public Passphrase getPassphraseFor(@NotNull KeyIdentifier keyIdentifier) {
fail("MUST NOT get called in non-interactive mode.");
return null;
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@NotNull KeyIdentifier keyIdentifier) {
return true;
}
};

View file

@ -12,6 +12,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
@ -23,6 +24,8 @@ import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider;
import org.pgpainless.util.Passphrase;
import javax.annotation.Nonnull;
public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
private static PGPSecretKeyRing k1;
@ -120,13 +123,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
public void missingPassphraseFirst() throws PGPException, IOException {
SecretKeyRingProtector protector1 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
public Passphrase getPassphraseFor(@Nonnull KeyIdentifier keyIdentifier) {
fail("Although the first PKESK is for k1, we should have skipped it and tried k2 first, which has passphrase available.");
return null;
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@Nonnull KeyIdentifier keyIdentifier) {
return false;
}
});
@ -134,7 +137,7 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ENCRYPTED_FOR_K1_K2.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(k1, protector1)
.addDecryptionKey(k2, protector2));
@ -150,20 +153,20 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
SecretKeyRingProtector protector1 = SecretKeyRingProtector.unlockEachKeyWith(p1, k1);
SecretKeyRingProtector protector2 = new CachingSecretKeyRingProtector(new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
public Passphrase getPassphraseFor(@Nonnull KeyIdentifier keyIdentifier) {
fail("This callback should not get called, since the first PKESK is for k1, which has a passphrase available.");
return null;
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@Nonnull KeyIdentifier keyIdentifier) {
return false;
}
});
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ENCRYPTED_FOR_K1_K2.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(k1, protector1)
.addDecryptionKey(k2, protector2));
@ -178,13 +181,13 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
public void messagePassphraseFirst() throws PGPException, IOException {
SecretKeyPassphraseProvider provider = new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
public Passphrase getPassphraseFor(@Nonnull KeyIdentifier keyIdentifier) {
fail("Since we provide a decryption passphrase, we should not try to decrypt any key.");
return null;
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@Nonnull KeyIdentifier keyIdentifier) {
return false;
}
};
@ -192,7 +195,7 @@ public class PostponeDecryptionUsingKeyWithMissingPassphraseTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ENCRYPTED_FOR_K2_PASS_K1.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addMessagePassphrase(PASSPHRASE)
.addDecryptionKey(k1, protector)
.addDecryptionKey(k2, protector));

View file

@ -180,7 +180,7 @@ public class PreventDecryptionUsingNonEncryptionKeyTest {
ByteArrayInputStream msgIn = new ByteArrayInputStream(MSG.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(msgIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys));
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys));
Streams.drain(decryptionStream);
decryptionStream.close();
@ -196,7 +196,7 @@ public class PreventDecryptionUsingNonEncryptionKeyTest {
ByteArrayInputStream msgIn = new ByteArrayInputStream(MSG.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(msgIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys));
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys));
Streams.drain(decryptionStream);
decryptionStream.close();
@ -215,6 +215,6 @@ public class PreventDecryptionUsingNonEncryptionKeyTest {
assertThrows(MissingDecryptionMethodException.class, () ->
PGPainless.decryptAndOrVerify()
.onInputStream(msgIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys)));
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys)));
}
}

View file

@ -30,7 +30,7 @@ public class SignedMessageVerificationWithoutCertIsStillSignedTest {
@Test
public void verifyMissingVerificationCertOptionStillResultsInMessageIsSigned() throws IOException, PGPException {
ConsumerOptions withoutVerificationCert = new ConsumerOptions();
ConsumerOptions withoutVerificationCert = ConsumerOptions.get();
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)))
.withOptions(withoutVerificationCert);

View file

@ -57,7 +57,7 @@ public class VerifyDetachedSignatureTest {
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(signedContent.getBytes(StandardCharsets.UTF_8)))
.withOptions(
new ConsumerOptions()
ConsumerOptions.get()
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(signature.getBytes(StandardCharsets.UTF_8)))
.addVerificationCerts(PGPainless.readKeyRing().keyRingCollection(pubkey, true).getPgpPublicKeyRingCollection())
.setMultiPassStrategy(new InMemoryMultiPassStrategy())
@ -132,7 +132,7 @@ public class VerifyDetachedSignatureTest {
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(signedContent.getBytes(StandardCharsets.UTF_8)))
.withOptions(
new ConsumerOptions()
ConsumerOptions.get()
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(signature.getBytes(StandardCharsets.UTF_8)))
.addVerificationCerts(PGPainless.readKeyRing().keyRingCollection(pubkey, true).getPgpPublicKeyRingCollection())
.setMultiPassStrategy(new InMemoryMultiPassStrategy())

View file

@ -62,7 +62,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void noConstraintsVerifyInlineSig() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(certificate);
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(inlineSigned))
@ -74,7 +74,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void noConstraintsVerifyDetachedSig() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(certificate)
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(detachedSignature));
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
@ -87,7 +87,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void notBeforeT1DoesNotRejectInlineSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotBefore(T1)
.addVerificationCert(certificate);
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
@ -99,7 +99,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void notBeforeT1DoesNotRejectDetachedSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotBefore(T1)
.addVerificationCert(certificate)
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(detachedSignature));
@ -112,7 +112,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void verifyNotBeforeT2DoesRejectInlineSignatureMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotBefore(T2)
.addVerificationCert(certificate);
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
@ -124,7 +124,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void verifyNotBeforeT2DoesRejectDetachedSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotBefore(T2)
.addVerificationCert(certificate)
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(detachedSignature));
@ -137,7 +137,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void verifyNotAfterT1DoesNotRejectInlineSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotAfter(T1)
.addVerificationCert(certificate);
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
@ -149,7 +149,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void verifyNotAfterT1DoesRejectDetachedSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotAfter(T1)
.addVerificationCert(certificate)
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(detachedSignature));
@ -162,7 +162,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void verifyNotAfterT0DoesRejectInlineSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotAfter(T0)
.addVerificationCert(certificate);
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
@ -174,7 +174,7 @@ public class VerifyNotBeforeNotAfterTest {
@Test
public void verifyNotAfterT0DoesRejectDetachedSigMadeAtT1() throws PGPException, IOException {
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.verifyNotAfter(T0)
.addVerificationCert(certificate)
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(detachedSignature));

View file

@ -35,7 +35,7 @@ class VerifyVersion3SignaturePacketTest {
void verifyDetachedVersion3Signature() throws PGPException, IOException {
PGPSignature version3Signature = generateV3Signature();
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(TestKeys.getEmilPublicKeyRing())
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(version3Signature.getEncoded()));

View file

@ -13,21 +13,23 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.bouncycastle.bcpg.KeyIdentifier;
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;
import org.pgpainless.algorithm.DocumentSignatureType;
import org.pgpainless.algorithm.OpenPGPKeyVersion;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.encryption_signing.SigningOptions;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.util.KeyRingUtils;
import javax.annotation.Nonnull;
/**
* Test functionality of the {@link MissingPublicKeyCallback} which is called when during signature verification,
@ -38,11 +40,12 @@ public class VerifyWithMissingPublicKeyCallbackTest {
@Test
public void testMissingPublicKeyCallback() throws PGPException, IOException {
PGPSecretKeyRing signingSecKeys = PGPainless.generateKeyRing().modernKeyRing("alice")
.getPGPSecretKeyRing();
PGPainless api = PGPainless.getInstance();
OpenPGPKey signingSecKeys = api.generateKey(OpenPGPKeyVersion.v4).modernKeyRing("alice");
OpenPGPCertificate.OpenPGPComponentKey signingKey =
new KeyRingInfo(signingSecKeys).getSigningSubkeys().get(0);
PGPPublicKeyRing signingPubKeys = KeyRingUtils.publicKeyRingFrom(signingSecKeys);
signingSecKeys.getSigningKeys().get(0);
OpenPGPCertificate signingPubKeys = signingSecKeys.toCertificate();
PGPPublicKeyRing unrelatedKeys = TestKeys.getJulietPublicKeyRing();
String msg = "Arguing that you don't care about the right to privacy because you have nothing to hide" +
@ -51,7 +54,7 @@ public class VerifyWithMissingPublicKeyCallbackTest {
EncryptionStream signingStream = PGPainless.encryptAndOrSign().onOutputStream(signOut)
.withOptions(ProducerOptions.sign(new SigningOptions().addInlineSignature(
SecretKeyRingProtector.unprotectedKeys(),
signingSecKeys, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT
signingSecKeys.getPGPSecretKeyRing(), DocumentSignatureType.CANONICAL_TEXT_DOCUMENT
)));
Streams.pipeAll(new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8)), signingStream);
signingStream.close();
@ -62,8 +65,8 @@ public class VerifyWithMissingPublicKeyCallbackTest {
.addVerificationCert(unrelatedKeys)
.setMissingCertificateCallback(new MissingPublicKeyCallback() {
@Override
public PGPPublicKeyRing onMissingPublicKeyEncountered(long keyId) {
assertEquals(signingKey.getKeyIdentifier().getKeyId(), keyId, "Signing key-ID mismatch.");
public OpenPGPCertificate onMissingPublicKeyEncountered(@Nonnull KeyIdentifier keyIdentifier) {
assertEquals(signingKey.getKeyIdentifier(), keyIdentifier, "Signing key-ID mismatch.");
return signingPubKeys;
}
}));

View file

@ -97,7 +97,7 @@ public class ChangeSecretKeyRingPassphraseTest {
extractPrivateKey(subKey, Passphrase.fromPassword("weakPassphrase"));
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
.changeSubKeyPassphraseFromOldPassphrase(subKey.getPublicKey().getKeyID(),
.changeSubKeyPassphraseFromOldPassphrase(subKey.getPublicKey().getKeyIdentifier(),
Passphrase.fromPassword("weakPassphrase"))
.withSecureDefaultSettings()
.toNewPassphrase(Passphrase.fromPassword("subKeyPassphrase"))
@ -130,7 +130,7 @@ public class ChangeSecretKeyRingPassphraseTest {
PGPSecretKey subKey = keys.next();
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
.changeSubKeyPassphraseFromOldPassphrase(subKey.getKeyID(), Passphrase.fromPassword("weakPassphrase"))
.changeSubKeyPassphraseFromOldPassphrase(subKey.getKeyIdentifier(), Passphrase.fromPassword("weakPassphrase"))
.withSecureDefaultSettings()
.toNoPassphrase()
.done();

View file

@ -13,11 +13,13 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -30,13 +32,13 @@ public class CachingSecretKeyRingProtectorTest {
// Dummy passphrase callback that returns the doubled key-id as passphrase
private final SecretKeyPassphraseProvider dummyCallback = new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
long doubled = keyId * 2;
public Passphrase getPassphraseFor(@NotNull KeyIdentifier keyIdentifier) {
long doubled = keyIdentifier.getKeyId() * 2;
return Passphrase.fromPassword(Long.toString(doubled));
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@NotNull KeyIdentifier keyIdentifier) {
return true;
}
};
@ -49,15 +51,15 @@ public class CachingSecretKeyRingProtectorTest {
}
@Test
public void noCallbackReturnsNullForUnknownKeyId() {
public void noCallbackReturnsNullForUnknownKeyId() throws PGPException {
assertNull(protector.getDecryptor(123L));
assertNull(protector.getEncryptor(123L));
}
@Test
public void testAddPassphrase() {
public void testAddPassphrase() throws PGPException {
Passphrase passphrase = Passphrase.fromPassword("HelloWorld");
protector.addPassphrase(123L, passphrase);
protector.addPassphrase(new KeyIdentifier(123L), passphrase);
assertEquals(passphrase, protector.getPassphraseFor(123L));
assertNotNull(protector.getEncryptor(123L));
assertNotNull(protector.getDecryptor(123L));
@ -75,7 +77,7 @@ public class CachingSecretKeyRingProtectorTest {
}
@Test
public void testAddPassphraseForKeyRing() {
public void testAddPassphraseForKeyRing() throws PGPException {
PGPSecretKeyRing keys = PGPainless.generateKeyRing()
.modernKeyRing("test@test.test", "Passphrase123")
.getPGPSecretKeyRing();

View file

@ -11,6 +11,7 @@ import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
@ -22,10 +23,10 @@ public class MapBasedPassphraseProviderTest {
@Test
public void testMapBasedProvider() throws IOException, PGPException {
Map<Long, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(1L, Passphrase.fromPassword("tiger"));
passphraseMap.put(123123123L, Passphrase.fromPassword("snake"));
passphraseMap.put(69696969L, Passphrase.emptyPassphrase());
Map<KeyIdentifier, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(new KeyIdentifier(1L), Passphrase.fromPassword("tiger"));
passphraseMap.put(new KeyIdentifier(123123123L), Passphrase.fromPassword("snake"));
passphraseMap.put(new KeyIdentifier(69696969L), Passphrase.emptyPassphrase());
MapBasedPassphraseProvider provider = new MapBasedPassphraseProvider(passphraseMap);
assertEquals(Passphrase.fromPassword("tiger"), provider.getPassphraseFor(1L));
@ -35,7 +36,7 @@ public class MapBasedPassphraseProviderTest {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(secretKeys.getSecretKey().getKeyID(), TestKeys.CRYPTIE_PASSPHRASE);
passphraseMap.put(secretKeys.getSecretKey().getKeyIdentifier(), TestKeys.CRYPTIE_PASSPHRASE);
provider = new MapBasedPassphraseProvider(passphraseMap);
assertEquals(TestKeys.CRYPTIE_PASSPHRASE, provider.getPassphraseFor(secretKeys.getSecretKey()));

View file

@ -10,9 +10,11 @@ import static org.junit.jupiter.api.Assertions.assertNull;
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.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys;
@ -29,8 +31,8 @@ public class PassphraseProtectedKeyTest {
new SecretKeyPassphraseProvider() {
@Nullable
@Override
public Passphrase getPassphraseFor(Long keyId) {
if (keyId == TestKeys.CRYPTIE_KEY_ID) {
public Passphrase getPassphraseFor(@NotNull KeyIdentifier keyIdentifier) {
if (keyIdentifier.getKeyId() == TestKeys.CRYPTIE_KEY_ID) {
return new Passphrase(TestKeys.CRYPTIE_PASSWORD.toCharArray());
} else {
return null;
@ -38,19 +40,19 @@ public class PassphraseProtectedKeyTest {
}
@Override
public boolean hasPassphrase(Long keyId) {
return keyId == TestKeys.CRYPTIE_KEY_ID;
public boolean hasPassphrase(@NotNull KeyIdentifier keyIdentifier) {
return keyIdentifier.getKeyId() == TestKeys.CRYPTIE_KEY_ID;
}
});
@Test
public void testReturnsNonNullDecryptorEncryptorForPassword() {
public void testReturnsNonNullDecryptorEncryptorForPassword() throws PGPException {
assertNotNull(protector.getEncryptor(TestKeys.CRYPTIE_KEY_ID));
assertNotNull(protector.getDecryptor(TestKeys.CRYPTIE_KEY_ID));
}
@Test
public void testReturnsNullDecryptorEncryptorForNoPassword() {
public void testReturnsNullDecryptorEncryptorForNoPassword() throws PGPException {
assertNull(protector.getEncryptor(TestKeys.JULIET_KEY_ID));
assertNull(protector.getDecryptor(TestKeys.JULIET_KEY_ID));
}

View file

@ -15,10 +15,12 @@ import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
@ -85,8 +87,8 @@ public class SecretKeyRingProtectorTest {
@Test
public void testFromPassphraseMap() {
Map<Long, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(1L, Passphrase.emptyPassphrase());
Map<KeyIdentifier, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(new KeyIdentifier(1L), Passphrase.emptyPassphrase());
CachingSecretKeyRingProtector protector =
(CachingSecretKeyRingProtector) SecretKeyRingProtector.fromPassphraseMap(passphraseMap);
@ -102,17 +104,17 @@ public class SecretKeyRingProtectorTest {
@Test
public void testMissingPassphraseCallback() {
Map<Long, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(1L, Passphrase.emptyPassphrase());
Map<KeyIdentifier, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(new KeyIdentifier(1L), Passphrase.emptyPassphrase());
CachingSecretKeyRingProtector protector = new CachingSecretKeyRingProtector(passphraseMap,
KeyRingProtectionSettings.secureDefaultSettings(), new SecretKeyPassphraseProvider() {
@Override
public Passphrase getPassphraseFor(Long keyId) {
public Passphrase getPassphraseFor(@NotNull KeyIdentifier keyIdentifier) {
return Passphrase.fromPassword("missingP455w0rd");
}
@Override
public boolean hasPassphrase(Long keyId) {
public boolean hasPassphrase(@NotNull KeyIdentifier keyIdentifier) {
return true;
}
});

View file

@ -6,6 +6,7 @@ package org.pgpainless.key.protection;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.bouncycastle.openpgp.PGPException;
import org.junit.jupiter.api.Test;
public class UnprotectedKeysProtectorTest {
@ -13,12 +14,12 @@ public class UnprotectedKeysProtectorTest {
private final UnprotectedKeysProtector protector = new UnprotectedKeysProtector();
@Test
public void testKeyProtectorReturnsNullDecryptor() {
public void testKeyProtectorReturnsNullDecryptor() throws PGPException {
assertNull(protector.getDecryptor(0L));
}
@Test
public void testKeyProtectorReturnsNullEncryptor() {
public void testKeyProtectorReturnsNullEncryptor() throws PGPException {
assertNull(protector.getEncryptor(0L));
}
}

View file

@ -48,7 +48,7 @@ public class MultiPassphraseSymmetricEncryptionTest {
for (Passphrase passphrase : new Passphrase[] {Passphrase.fromPassword("p2"), Passphrase.fromPassword("p1")}) {
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertext))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addMessagePassphrase(passphrase));
ByteArrayOutputStream plaintextOut = new ByteArrayOutputStream();

View file

@ -65,7 +65,7 @@ public class SymmetricEncryptionTest {
// Test symmetric decryption
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertext))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addMessagePassphrase(encryptionPassphrase));
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
@ -82,7 +82,7 @@ public class SymmetricEncryptionTest {
new SolitaryPassphraseProvider(Passphrase.fromPassword(TestKeys.CRYPTIE_PASSWORD)));
decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertext))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(decryptionKeys, protector));
decrypted = new ByteArrayOutputStream();
@ -110,7 +110,7 @@ public class SymmetricEncryptionTest {
assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertextOut.toByteArray()))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION)
.addMessagePassphrase(Passphrase.fromPassword("meldir"))));
}