1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 14:22:05 +01:00

Merge pull request #34 from pgpainless/removePgpKeyRing

Remove deprecated class PGPKeyRing
This commit is contained in:
Paul Schaub 2020-12-08 19:25:43 +01:00 committed by GitHub
commit 00f01bd031
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 91 additions and 247 deletions

View file

@ -1,77 +0,0 @@
/*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.key.OpenPgpV4Fingerprint;
@Deprecated
public class PGPKeyRing {
private PGPPublicKeyRing publicKeys;
private PGPSecretKeyRing secretKeys;
public PGPKeyRing(@Nonnull PGPPublicKeyRing publicKeys, @Nonnull PGPSecretKeyRing secretKeys) {
if (publicKeys.getPublicKey().getKeyID() != secretKeys.getPublicKey().getKeyID()) {
throw new IllegalArgumentException("publicKeys and secretKeys must have the same master key.");
}
this.publicKeys = publicKeys;
this.secretKeys = secretKeys;
}
public PGPKeyRing(@Nonnull PGPPublicKeyRing publicKeys) {
this.publicKeys = publicKeys;
}
public PGPKeyRing(@Nonnull PGPSecretKeyRing secretKeys) {
this.secretKeys = secretKeys;
}
public long getKeyId() {
return getMasterKey().getKeyID();
}
public @Nonnull PGPPublicKey getMasterKey() {
PGPPublicKey publicKey = hasSecretKeys() ? secretKeys.getPublicKey() : publicKeys.getPublicKey();
if (!publicKey.isMasterKey()) {
throw new IllegalStateException("Expected master key is not a master key");
}
return publicKey;
}
public @Nonnull OpenPgpV4Fingerprint getV4Fingerprint() {
return new OpenPgpV4Fingerprint(getMasterKey());
}
public boolean hasSecretKeys() {
return secretKeys != null;
}
public @Nullable PGPPublicKeyRing getPublicKeys() {
return publicKeys;
}
public @Nullable PGPSecretKeyRing getSecretKeys() {
return secretKeys;
}
}

View file

@ -35,7 +35,6 @@ import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPKeyRingGenerator; import org.bouncycastle.openpgp.PGPKeyRingGenerator;
import org.bouncycastle.openpgp.PGPPrivateKey; import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
@ -53,7 +52,6 @@ import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder;
import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SignatureType; import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.curve.EllipticCurve; import org.pgpainless.key.generation.type.curve.EllipticCurve;
import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.key.generation.type.length.RsaLength;
@ -70,7 +68,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
private Set<String> additionalUserIds = new LinkedHashSet<>(); private Set<String> additionalUserIds = new LinkedHashSet<>();
private Passphrase passphrase; private Passphrase passphrase;
public PGPKeyRing simpleRsaKeyRing(@Nonnull UserId userId, @Nonnull RsaLength length) public PGPSecretKeyRing simpleRsaKeyRing(@Nonnull UserId userId, @Nonnull RsaLength length)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleRsaKeyRing(userId.toString(), length); return simpleRsaKeyRing(userId.toString(), length);
} }
@ -84,12 +82,12 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
* *
* @return {@link PGPSecretKeyRing} containing the KeyPair. * @return {@link PGPSecretKeyRing} containing the KeyPair.
*/ */
public PGPKeyRing simpleRsaKeyRing(@Nonnull String userId, @Nonnull RsaLength length) public PGPSecretKeyRing simpleRsaKeyRing(@Nonnull String userId, @Nonnull RsaLength length)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleRsaKeyRing(userId, length, null); return simpleRsaKeyRing(userId, length, null);
} }
public PGPKeyRing simpleRsaKeyRing(@Nonnull UserId userId, @Nonnull RsaLength rsaLength, String password) public PGPSecretKeyRing simpleRsaKeyRing(@Nonnull UserId userId, @Nonnull RsaLength rsaLength, String password)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleRsaKeyRing(userId.toString(), rsaLength, password); return simpleRsaKeyRing(userId.toString(), rsaLength, password);
} }
@ -104,7 +102,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
* *
* @return {@link PGPSecretKeyRing} containing the KeyPair. * @return {@link PGPSecretKeyRing} containing the KeyPair.
*/ */
public PGPKeyRing simpleRsaKeyRing(@Nonnull String userId, @Nonnull RsaLength length, String password) public PGPSecretKeyRing simpleRsaKeyRing(@Nonnull String userId, @Nonnull RsaLength length, String password)
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
WithAdditionalUserIdOrPassphrase builder = this WithAdditionalUserIdOrPassphrase builder = this
.withMasterKey( .withMasterKey(
@ -120,7 +118,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
} }
} }
public PGPKeyRing simpleEcKeyRing(@Nonnull UserId userId) public PGPSecretKeyRing simpleEcKeyRing(@Nonnull UserId userId)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleEcKeyRing(userId.toString()); return simpleEcKeyRing(userId.toString());
} }
@ -134,12 +132,12 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
* *
* @return {@link PGPSecretKeyRing} containing the key pairs. * @return {@link PGPSecretKeyRing} containing the key pairs.
*/ */
public PGPKeyRing simpleEcKeyRing(@Nonnull String userId) public PGPSecretKeyRing simpleEcKeyRing(@Nonnull String userId)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleEcKeyRing(userId, null); return simpleEcKeyRing(userId, null);
} }
public PGPKeyRing simpleEcKeyRing(@Nonnull UserId userId, String password) public PGPSecretKeyRing simpleEcKeyRing(@Nonnull UserId userId, String password)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleEcKeyRing(userId.toString(), password); return simpleEcKeyRing(userId.toString(), password);
} }
@ -154,7 +152,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
* *
* @return {@link PGPSecretKeyRing} containing the key pairs. * @return {@link PGPSecretKeyRing} containing the key pairs.
*/ */
public PGPKeyRing simpleEcKeyRing(@Nonnull String userId, String password) public PGPSecretKeyRing simpleEcKeyRing(@Nonnull String userId, String password)
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
WithAdditionalUserIdOrPassphrase builder = this WithAdditionalUserIdOrPassphrase builder = this
.withSubKey( .withSubKey(
@ -249,7 +247,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
private PBESecretKeyEncryptor secretKeyEncryptor; private PBESecretKeyEncryptor secretKeyEncryptor;
@Override @Override
public PGPKeyRing build() throws NoSuchAlgorithmException, PGPException, public PGPSecretKeyRing build() throws NoSuchAlgorithmException, PGPException,
InvalidAlgorithmParameterException { InvalidAlgorithmParameterException {
digestCalculator = buildDigestCalculator(); digestCalculator = buildDigestCalculator();
secretKeyEncryptor = buildSecretKeyEncryptor(); secretKeyEncryptor = buildSecretKeyEncryptor();
@ -300,15 +298,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
} }
secretKeyRing = new PGPSecretKeyRing(secretKeyList); secretKeyRing = new PGPSecretKeyRing(secretKeyList);
// extract public key ring from secret keys return secretKeyRing;
List<PGPPublicKey> publicKeyList = new ArrayList<>();
Iterator<PGPPublicKey> publicKeys = secretKeyRing.getPublicKeys();
while (publicKeys.hasNext()) {
publicKeyList.add(publicKeys.next());
}
PGPPublicKeyRing publicKeyRing = new PGPPublicKeyRing(publicKeyList);
return new PGPKeyRing(publicKeyRing, secretKeyRing);
} }
private PGPKeyRingGenerator buildRingGenerator(PGPKeyPair certKey, private PGPKeyRingGenerator buildRingGenerator(PGPKeyPair certKey,

View file

@ -15,12 +15,12 @@
*/ */
package org.pgpainless.key.generation; package org.pgpainless.key.generation;
import javax.annotation.Nonnull;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.pgpainless.key.collection.PGPKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.key.util.UserId; import org.pgpainless.key.util.UserId;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
@ -59,8 +59,7 @@ public interface KeyRingBuilderInterface {
interface Build { interface Build {
PGPKeyRing build() throws NoSuchAlgorithmException, PGPException, PGPSecretKeyRing build() throws NoSuchAlgorithmException, PGPException,
InvalidAlgorithmParameterException; InvalidAlgorithmParameterException;
} }
} }

View file

@ -15,12 +15,11 @@
*/ */
package org.pgpainless.key.parsing; package org.pgpainless.key.parsing;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
@ -29,7 +28,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil; import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.pgpainless.key.collection.PGPKeyRing;
public class KeyRingReader { public class KeyRingReader {
@ -85,27 +83,6 @@ public class KeyRingReader {
return secretKeyRingCollection(asciiArmored.getBytes(UTF8)); return secretKeyRingCollection(asciiArmored.getBytes(UTF8));
} }
@Deprecated
public PGPKeyRing keyRing(@Nullable InputStream publicIn, @Nullable InputStream secretIn) throws IOException, PGPException {
return readKeyRing(publicIn, secretIn);
}
@Deprecated
public PGPKeyRing keyRing(@Nullable byte[] publicBytes, @Nullable byte[] secretBytes) throws IOException, PGPException {
return keyRing(
publicBytes != null ? new ByteArrayInputStream(publicBytes) : null,
secretBytes != null ? new ByteArrayInputStream(secretBytes) : null
);
}
@Deprecated
public PGPKeyRing keyRing(@Nullable String asciiPublic, @Nullable String asciiSecret) throws IOException, PGPException {
return keyRing(
asciiPublic != null ? asciiPublic.getBytes(UTF8) : null,
asciiSecret != null ? asciiSecret.getBytes(UTF8) : null
);
}
/* /*
STATIC METHODS STATIC METHODS
*/ */
@ -136,16 +113,6 @@ public class KeyRingReader {
new BcKeyFingerprintCalculator()); new BcKeyFingerprintCalculator());
} }
@Deprecated
public static PGPKeyRing readKeyRing(@Nullable InputStream publicIn, @Nullable InputStream secretIn) throws IOException, PGPException {
validateStreamsNotBothNull(publicIn, secretIn);
PGPPublicKeyRing publicKeys = maybeReadPublicKeys(publicIn);
PGPSecretKeyRing secretKeys = maybeReadSecretKeys(secretIn);
return asPGPKeyRing(publicKeys, secretKeys);
}
private static void validateStreamsNotBothNull(InputStream publicIn, InputStream secretIn) { private static void validateStreamsNotBothNull(InputStream publicIn, InputStream secretIn) {
if (publicIn == null && secretIn == null) { if (publicIn == null && secretIn == null) {
throw new NullPointerException("publicIn and secretIn cannot be BOTH null."); throw new NullPointerException("publicIn and secretIn cannot be BOTH null.");
@ -165,15 +132,4 @@ public class KeyRingReader {
} }
return null; return null;
} }
@Deprecated
private static PGPKeyRing asPGPKeyRing(PGPPublicKeyRing publicKeys, PGPSecretKeyRing secretKeys) {
if (secretKeys == null) {
return new PGPKeyRing(publicKeys);
}
if (publicKeys == null) {
return new PGPKeyRing(secretKeys);
}
return new PGPKeyRing(publicKeys, secretKeys);
}
} }

View file

@ -44,7 +44,6 @@ import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.KeySpec; import org.pgpainless.key.generation.KeySpec;
import org.pgpainless.key.generation.type.ElGamal_GENERAL; import org.pgpainless.key.generation.type.ElGamal_GENERAL;
import org.pgpainless.key.generation.type.KeyType; 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.generation.type.length.RsaLength;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.BCUtil; import org.pgpainless.util.BCUtil;
public class EncryptDecryptTest { public class EncryptDecryptTest {
@ -71,8 +71,8 @@ public class EncryptDecryptTest {
@Test @Test
public void freshKeysRsaToElGamalTest() public void freshKeysRsaToElGamalTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
PGPKeyRing recipient = PGPainless.generateKeyRing() PGPSecretKeyRing recipient = PGPainless.generateKeyRing()
.withSubKey(KeySpec.getBuilder(ElGamal_GENERAL.withLength(ElGamalLength._3072)).withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS).withDefaultAlgorithms()) .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()) .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._4096)).withKeyFlags(KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER).withDefaultAlgorithms())
.withPrimaryUserId("juliet@capulet.lit").withoutPassphrase().build(); .withPrimaryUserId("juliet@capulet.lit").withoutPassphrase().build();
@ -83,8 +83,8 @@ public class EncryptDecryptTest {
@Test @Test
public void freshKeysRsaToRsaTest() public void freshKeysRsaToRsaTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -92,8 +92,8 @@ public class EncryptDecryptTest {
@Test @Test
public void freshKeysEcToEcTest() public void freshKeysEcToEcTest()
throws IOException, PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { throws IOException, PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit"); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit"); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -101,8 +101,8 @@ public class EncryptDecryptTest {
@Test @Test
public void freshKeysEcToRsaTest() public void freshKeysEcToRsaTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit"); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -110,26 +110,25 @@ public class EncryptDecryptTest {
@Test @Test
public void freshKeysRsaToEcTest() public void freshKeysRsaToEcTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit"); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Test @Test
public void existingRsaKeysTest() throws IOException, PGPException { public void existingRsaKeysTest() throws IOException, PGPException {
PGPKeyRing sender = new PGPKeyRing(TestKeys.getJulietPublicKeyRing(), TestKeys.getJulietSecretKeyRing()); PGPSecretKeyRing sender = TestKeys.getJulietSecretKeyRing();
PGPKeyRing recipient = new PGPKeyRing(TestKeys.getRomeoPublicKeyRing(), TestKeys.getRomeoSecretKeyRing()); PGPSecretKeyRing recipient = TestKeys.getRomeoSecretKeyRing();
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
private void encryptDecryptForSecretKeyRings(PGPKeyRing sender, PGPKeyRing recipient) private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing senderSec, PGPSecretKeyRing recipientSec)
throws PGPException, IOException { throws PGPException, IOException {
PGPSecretKeyRing recipientSec = recipient.getSecretKeys();
PGPSecretKeyRing senderSec = sender.getSecretKeys(); PGPPublicKeyRing recipientPub = KeyRingUtils.publicKeyRingFrom(recipientSec);
PGPPublicKeyRing recipientPub = recipient.getPublicKeys(); PGPPublicKeyRing senderPub = KeyRingUtils.publicKeyRingFrom(senderSec);
PGPPublicKeyRing senderPub = sender.getPublicKeys();
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector(); SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();
@ -188,7 +187,7 @@ public class EncryptDecryptTest {
@Test @Test
public void testDetachedSignatureCreationAndVerification() throws IOException, PGPException { public void testDetachedSignatureCreationAndVerification() throws IOException, PGPException {
PGPKeyRing signingKeys = new PGPKeyRing(TestKeys.getJulietPublicKeyRing(), TestKeys.getJulietSecretKeyRing()); PGPSecretKeyRing signingKeys = TestKeys.getJulietSecretKeyRing();
SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector(); SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector();
byte[] data = testMessage.getBytes(); byte[] data = testMessage.getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data); ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
@ -196,7 +195,7 @@ public class EncryptDecryptTest {
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(dummyOut) EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(dummyOut)
.doNotEncrypt() .doNotEncrypt()
.createDetachedSignature() .createDetachedSignature()
.signWith(keyRingProtector, signingKeys.getSecretKeys()) .signWith(keyRingProtector, signingKeys)
.noArmor(); .noArmor();
Streams.pipeAll(inputStream, signer); Streams.pipeAll(inputStream, signer);
signer.close(); signer.close();
@ -217,7 +216,7 @@ public class EncryptDecryptTest {
DecryptionStream verifier = PGPainless.decryptAndOrVerify().onInputStream(inputStream) DecryptionStream verifier = PGPainless.decryptAndOrVerify().onInputStream(inputStream)
.doNotDecrypt() .doNotDecrypt()
.verifyDetachedSignature(new ByteArrayInputStream(armorSig.getBytes())) .verifyDetachedSignature(new ByteArrayInputStream(armorSig.getBytes()))
.verifyWith(Collections.singleton(signingKeys.getPublicKeys())) .verifyWith(Collections.singleton(KeyRingUtils.publicKeyRingFrom(signingKeys)))
.ignoreMissingPublicKeys() .ignoreMissingPublicKeys()
.build(); .build();
dummyOut = new ByteArrayOutputStream(); dummyOut = new ByteArrayOutputStream();
@ -230,14 +229,14 @@ public class EncryptDecryptTest {
@Test @Test
public void testOnePassSignatureCreationAndVerification() throws IOException, PGPException { public void testOnePassSignatureCreationAndVerification() throws IOException, PGPException {
PGPKeyRing signingKeys = new PGPKeyRing(TestKeys.getJulietPublicKeyRing(), TestKeys.getJulietSecretKeyRing()); PGPSecretKeyRing signingKeys = TestKeys.getJulietSecretKeyRing();
SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector(); SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector();
byte[] data = testMessage.getBytes(); byte[] data = testMessage.getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data); ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
ByteArrayOutputStream signOut = new ByteArrayOutputStream(); ByteArrayOutputStream signOut = new ByteArrayOutputStream();
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(signOut) EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(signOut)
.doNotEncrypt() .doNotEncrypt()
.signWith(keyRingProtector, signingKeys.getSecretKeys()) .signWith(keyRingProtector, signingKeys)
.asciiArmor(); .asciiArmor();
Streams.pipeAll(inputStream, signer); Streams.pipeAll(inputStream, signer);
signer.close(); signer.close();
@ -249,7 +248,7 @@ public class EncryptDecryptTest {
inputStream = new ByteArrayInputStream(signOut.toByteArray()); inputStream = new ByteArrayInputStream(signOut.toByteArray());
DecryptionStream verifier = PGPainless.decryptAndOrVerify().onInputStream(inputStream) DecryptionStream verifier = PGPainless.decryptAndOrVerify().onInputStream(inputStream)
.doNotDecrypt() .doNotDecrypt()
.verifyWith(Collections.singleton(signingKeys.getPublicKeys())) .verifyWith(Collections.singleton(KeyRingUtils.publicKeyRingFrom(signingKeys)))
.ignoreMissingPublicKeys() .ignoreMissingPublicKeys()
.build(); .build();
signOut = new ByteArrayOutputStream(); signOut = new ByteArrayOutputStream();

View file

@ -33,10 +33,10 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector; 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. * 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, throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException { IOException {
LOGGER.log(Level.FINER, "\nEC -> EC"); LOGGER.log(Level.FINER, "\nEC -> EC");
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld"); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv"); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -61,10 +61,8 @@ public class LengthTest {
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException { IOException {
LOGGER.log(Level.FINER, "\nRSA-2048 -> RSA-2048"); LOGGER.log(Level.FINER, "\nRSA-2048 -> RSA-2048");
@SuppressWarnings("deprecation") PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
@SuppressWarnings("deprecation")
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -73,8 +71,8 @@ public class LengthTest {
throws PGPException, throws PGPException,
IOException { IOException {
LOGGER.log(Level.FINER, "\nRSA-4096 -> RSA-4096"); LOGGER.log(Level.FINER, "\nRSA-4096 -> RSA-4096");
PGPKeyRing sender = PGPainless.readKeyRing().keyRing(TestKeys.JULIET_PUB, TestKeys.JULIET_SEC); PGPSecretKeyRing sender = PGPainless.readKeyRing().secretKeyRing(TestKeys.JULIET_SEC);
PGPKeyRing recipient = PGPainless.readKeyRing().keyRing(TestKeys.ROMEO_PUB, TestKeys.ROMEO_SEC); PGPSecretKeyRing recipient = PGPainless.readKeyRing().secretKeyRing(TestKeys.ROMEO_SEC);
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -82,9 +80,8 @@ public class LengthTest {
public void rsaEc() throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, public void rsaEc() throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
NoSuchProviderException { NoSuchProviderException {
LOGGER.log(Level.FINER, "\nRSA-2048 -> EC"); LOGGER.log(Level.FINER, "\nRSA-2048 -> EC");
@SuppressWarnings("deprecation") PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
encryptDecryptForSecretKeyRings(sender, recipient); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@ -93,20 +90,18 @@ public class LengthTest {
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException { IOException {
LOGGER.log(Level.FINER, "\nEC -> RSA-2048"); 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") @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); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Ignore @Ignore
private void encryptDecryptForSecretKeyRings(PGPKeyRing sender, PGPKeyRing recipient) private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing senderSec, PGPSecretKeyRing recipientSec)
throws PGPException, throws PGPException,
IOException { IOException {
PGPSecretKeyRing recipientSec = recipient.getSecretKeys(); PGPPublicKeyRing recipientPub = KeyRingUtils.publicKeyRingFrom(recipientSec);
PGPSecretKeyRing senderSec = sender.getSecretKeys(); PGPPublicKeyRing senderPub = KeyRingUtils.publicKeyRingFrom(senderSec);
PGPPublicKeyRing recipientPub = recipient.getPublicKeys();
PGPPublicKeyRing senderPub = sender.getPublicKeys();
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector(); SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();

View file

@ -24,11 +24,12 @@ import java.util.logging.Logger;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.util.KeyRingUtils;
public class GenerateKeyTest { public class GenerateKeyTest {
@ -36,17 +37,18 @@ public class GenerateKeyTest {
@Test @Test
public void generateKey() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { 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(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(bytes); ArmoredOutputStream armor = new ArmoredOutputStream(bytes);
keyRing.getPublicKeys().encode(armor); secretKeys.encode(armor);
armor.close(); armor.close();
String publicKey = new String(bytes.toByteArray()); String publicKey = new String(bytes.toByteArray());
bytes = new ByteArrayOutputStream(); bytes = new ByteArrayOutputStream();
armor = new ArmoredOutputStream(bytes); armor = new ArmoredOutputStream(bytes);
keyRing.getSecretKeys().encode(armor); secretKeys.encode(armor);
armor.close(); armor.close();
String privateKey = new String(bytes.toByteArray()); String privateKey = new String(bytes.toByteArray());
@ -55,9 +57,9 @@ public class GenerateKeyTest {
"Fingerprint: %s\n" + "Fingerprint: %s\n" +
"Key-ID: %s\n" + "Key-ID: %s\n" +
"%s\n" + "%s\n" +
"%s\n", keyRing.getPublicKeys().getPublicKey().getUserIDs().next(), "%s\n", secretKeys.getPublicKey().getUserIDs().next(),
new OpenPgpV4Fingerprint(keyRing.getPublicKeys()), new OpenPgpV4Fingerprint(publicKeys),
keyRing.getPublicKeys().getPublicKey().getKeyID(), publicKeys.getPublicKey().getKeyID(),
publicKey, privateKey)); publicKey, privateKey));
} }
} }

View file

@ -26,18 +26,19 @@ import java.util.Iterator;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.util.KeyRingUtils;
public class GenerateKeyWithAdditionalUserIdTest { public class GenerateKeyWithAdditionalUserIdTest {
@Test @Test
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
PGPKeyRing keyRing = PGPainless.generateKeyRing() PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)) .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072))
.withDefaultKeyFlags() .withDefaultKeyFlags()
.withDefaultAlgorithms()) .withDefaultAlgorithms())
@ -47,8 +48,9 @@ public class GenerateKeyWithAdditionalUserIdTest {
.withAdditionalUserId("\ttrimThis@user.id ") .withAdditionalUserId("\ttrimThis@user.id ")
.withoutPassphrase() .withoutPassphrase()
.build(); .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("primary@user.id", userIds.next());
assertEquals("additional@user.id", userIds.next()); assertEquals("additional@user.id", userIds.next());
assertEquals("additional2@user.id", userIds.next()); assertEquals("additional2@user.id", userIds.next());
@ -57,7 +59,7 @@ public class GenerateKeyWithAdditionalUserIdTest {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(byteOut); ArmoredOutputStream armor = new ArmoredOutputStream(byteOut);
keyRing.getSecretKeys().encode(armor); secretKeys.encode(armor);
armor.close(); armor.close();
// echo this | gpg --list-packets // echo this | gpg --list-packets

View file

@ -30,7 +30,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector; import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
@ -40,8 +39,7 @@ public class AddUserIdTest {
@Test @Test
public void addUserIdToExistingKeyRing() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { public void addUserIdToExistingKeyRing() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit", "rabb1th0le"); PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit", "rabb1th0le");
PGPSecretKeyRing secretKeys = keyRing.getSecretKeys();
Iterator<String> userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs(); Iterator<String> userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs();
assertEquals("alice@wonderland.lit", userIds.next()); assertEquals("alice@wonderland.lit", userIds.next());

View file

@ -38,14 +38,13 @@ import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.encryption_signing.EncryptionStream; import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.protection.KeyRingProtectionSettings; import org.pgpainless.key.protection.KeyRingProtectionSettings;
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector; import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
public class ChangeSecretKeyRingPassphraseTest { 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 { public ChangeSecretKeyRingPassphraseTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
} }
@ -53,16 +52,16 @@ public class ChangeSecretKeyRingPassphraseTest {
@Test @Test
public void changePassphraseOfWholeKeyRingTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { public void changePassphraseOfWholeKeyRingTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys()) PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
.changePassphraseFromOldPassphrase(Passphrase.fromPassword("weakPassphrase")) .changePassphraseFromOldPassphrase(Passphrase.fromPassword("weakPassphrase"))
.withSecureDefaultSettings() .withSecureDefaultSettings()
.toNewPassphrase(Passphrase.fromPassword("1337p455phr453")) .toNewPassphrase(Passphrase.fromPassword("1337p455phr453"))
.done(); .done();
PGPKeyRing changedPassphraseKeyRing = new PGPKeyRing(secretKeys); PGPSecretKeyRing changedPassphraseKeyRing = secretKeys;
assertEquals(KeyRingProtectionSettings.secureDefaultSettings().getEncryptionAlgorithm().getAlgorithmId(), assertEquals(KeyRingProtectionSettings.secureDefaultSettings().getEncryptionAlgorithm().getAlgorithmId(),
changedPassphraseKeyRing.getSecretKeys().getSecretKey().getKeyEncryptionAlgorithm()); changedPassphraseKeyRing.getSecretKey().getKeyEncryptionAlgorithm());
assertThrows(PGPException.class, () -> assertThrows(PGPException.class, () ->
signDummyMessageWithKeysAndPassphrase(changedPassphraseKeyRing, Passphrase.emptyPassphrase()), signDummyMessageWithKeysAndPassphrase(changedPassphraseKeyRing, Passphrase.emptyPassphrase()),
@ -78,16 +77,16 @@ public class ChangeSecretKeyRingPassphraseTest {
@Test @Test
public void changePassphraseOfWholeKeyRingToEmptyPassphrase() throws PGPException, IOException { public void changePassphraseOfWholeKeyRingToEmptyPassphrase() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys()) PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
.changePassphraseFromOldPassphrase(Passphrase.fromPassword("weakPassphrase")) .changePassphraseFromOldPassphrase(Passphrase.fromPassword("weakPassphrase"))
.withSecureDefaultSettings() .withSecureDefaultSettings()
.toNoPassphrase() .toNoPassphrase()
.done(); .done();
PGPKeyRing changedPassphraseKeyRing = new PGPKeyRing(secretKeys); PGPSecretKeyRing changedPassphraseKeyRing = secretKeys;
assertEquals(SymmetricKeyAlgorithm.NULL.getAlgorithmId(), assertEquals(SymmetricKeyAlgorithm.NULL.getAlgorithmId(),
changedPassphraseKeyRing.getSecretKeys().getSecretKey().getKeyEncryptionAlgorithm()); changedPassphraseKeyRing.getSecretKey().getKeyEncryptionAlgorithm());
signDummyMessageWithKeysAndPassphrase(changedPassphraseKeyRing, Passphrase.emptyPassphrase()); signDummyMessageWithKeysAndPassphrase(changedPassphraseKeyRing, Passphrase.emptyPassphrase());
} }
@ -95,14 +94,14 @@ public class ChangeSecretKeyRingPassphraseTest {
@Test @Test
public void changePassphraseOfSingleSubkeyToNewPassphrase() throws PGPException { public void changePassphraseOfSingleSubkeyToNewPassphrase() throws PGPException {
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys().getSecretKeys(); Iterator<PGPSecretKey> keys = keyRing.getSecretKeys();
PGPSecretKey primaryKey = keys.next(); PGPSecretKey primaryKey = keys.next();
PGPSecretKey subKey = keys.next(); PGPSecretKey subKey = keys.next();
extractPrivateKey(primaryKey, Passphrase.fromPassword("weakPassphrase")); extractPrivateKey(primaryKey, Passphrase.fromPassword("weakPassphrase"));
extractPrivateKey(subKey, Passphrase.fromPassword("weakPassphrase")); extractPrivateKey(subKey, Passphrase.fromPassword("weakPassphrase"));
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys()) PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
.changeSubKeyPassphraseFromOldPassphrase(subKey.getPublicKey().getKeyID(), .changeSubKeyPassphraseFromOldPassphrase(subKey.getPublicKey().getKeyID(),
Passphrase.fromPassword("weakPassphrase")) Passphrase.fromPassword("weakPassphrase"))
.withSecureDefaultSettings() .withSecureDefaultSettings()
@ -129,11 +128,11 @@ public class ChangeSecretKeyRingPassphraseTest {
@Test @Test
public void changePassphraseOfSingleSubkeyToEmptyPassphrase() throws PGPException { public void changePassphraseOfSingleSubkeyToEmptyPassphrase() throws PGPException {
Iterator<PGPSecretKey> keys = keyRing.getSecretKeys().getSecretKeys(); Iterator<PGPSecretKey> keys = keyRing.getSecretKeys();
PGPSecretKey primaryKey = keys.next(); PGPSecretKey primaryKey = keys.next();
PGPSecretKey subKey = keys.next(); PGPSecretKey subKey = keys.next();
PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing.getSecretKeys()) PGPSecretKeyRing secretKeys = PGPainless.modifyKeyRing(keyRing)
.changeSubKeyPassphraseFromOldPassphrase(primaryKey.getKeyID(), Passphrase.fromPassword("weakPassphrase")) .changeSubKeyPassphraseFromOldPassphrase(primaryKey.getKeyID(), Passphrase.fromPassword("weakPassphrase"))
.withSecureDefaultSettings() .withSecureDefaultSettings()
.toNoPassphrase() .toNoPassphrase()
@ -177,12 +176,12 @@ public class ChangeSecretKeyRingPassphraseTest {
secretKey.extractPrivateKey(decryptor); 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"; String dummyMessage = "dummy";
ByteArrayOutputStream dummy = new ByteArrayOutputStream(); ByteArrayOutputStream dummy = new ByteArrayOutputStream();
EncryptionStream stream = PGPainless.encryptAndOrSign().onOutputStream(dummy) EncryptionStream stream = PGPainless.encryptAndOrSign().onOutputStream(dummy)
.doNotEncrypt() .doNotEncrypt()
.signWith(PasswordBasedSecretKeyRingProtector.forKey(keyRing.getSecretKeys(), passphrase), keyRing.getSecretKeys()) .signWith(PasswordBasedSecretKeyRingProtector.forKey(keyRing, passphrase), keyRing)
.noArmor(); .noArmor();
Streams.pipeAll(new ByteArrayInputStream(dummyMessage.getBytes()), stream); Streams.pipeAll(new ByteArrayInputStream(dummyMessage.getBytes()), stream);

View file

@ -15,7 +15,6 @@
*/ */
package org.pgpainless.util; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; 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.io.IOException;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Iterator; import java.util.Iterator;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -37,10 +35,10 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.KeySpec; import org.pgpainless.key.generation.KeySpec;
import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.util.KeyRingUtils;
public class BCUtilTest { public class BCUtilTest {
@ -50,12 +48,12 @@ public class BCUtilTest {
public void keyRingToCollectionTest() public void keyRingToCollectionTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
IOException { IOException {
PGPKeyRing ring = PGPainless.generateKeyRing() PGPSecretKeyRing sec = PGPainless.generateKeyRing()
.withSubKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms()) .withSubKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms())
.withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms()) .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms())
.withPrimaryUserId("donald@duck.tails").withoutPassphrase().build(); .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()); LOGGER.log(Level.FINER, "Main ID: " + sec.getPublicKey().getKeyID() + " " + pub.getPublicKey().getKeyID());
@ -107,11 +105,11 @@ public class BCUtilTest {
public void removeUnsignedKeysTest() public void removeUnsignedKeysTest()
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
PGPKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024); PGPSecretKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024);
PGPKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory"); PGPSecretKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory");
PGPSecretKey subKey = null; PGPSecretKey subKey = null;
Iterator<PGPSecretKey> sit = mallory.getSecretKeys().getSecretKeys(); Iterator<PGPSecretKey> sit = mallory.getSecretKeys();
while (sit.hasNext()) { while (sit.hasNext()) {
PGPSecretKey s = sit.next(); PGPSecretKey s = sit.next();
if (!s.isMasterKey()) { if (!s.isMasterKey()) {
@ -122,29 +120,12 @@ public class BCUtilTest {
assertNotNull(subKey); 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 // Check, if alice_mallory contains mallory's key
assertNotNull(alice_mallory.getSecretKey(subKey.getKeyID())); 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())); 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());
}
} }