mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 10:19:39 +02:00
Port test
This commit is contained in:
parent
0b4f1a0f01
commit
a54382a78e
5 changed files with 29 additions and 29 deletions
|
@ -430,7 +430,7 @@ class OpenPgpMessageInputStream(
|
||||||
}
|
}
|
||||||
if (decryptWithPrivateKey(
|
if (decryptWithPrivateKey(
|
||||||
esks,
|
esks,
|
||||||
privateKey.unlockedKey,
|
privateKey.keyPair,
|
||||||
SubkeyIdentifier(
|
SubkeyIdentifier(
|
||||||
secretKey.openPGPKey.pgpSecretKeyRing, secretKey.keyIdentifier),
|
secretKey.openPGPKey.pgpSecretKeyRing, secretKey.keyIdentifier),
|
||||||
pkesk)) {
|
pkesk)) {
|
||||||
|
@ -458,7 +458,7 @@ class OpenPgpMessageInputStream(
|
||||||
|
|
||||||
val privateKey = decryptionKey.unlock(protector)
|
val privateKey = decryptionKey.unlock(protector)
|
||||||
if (decryptWithPrivateKey(
|
if (decryptWithPrivateKey(
|
||||||
esks, privateKey.unlockedKey, SubkeyIdentifier(decryptionKey), pkesk)) {
|
esks, privateKey.keyPair, SubkeyIdentifier(decryptionKey), pkesk)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -489,7 +489,7 @@ class OpenPgpMessageInputStream(
|
||||||
} catch (e: PGPException) {
|
} catch (e: PGPException) {
|
||||||
throw WrongPassphraseException(secretKey.keyIdentifier, e)
|
throw WrongPassphraseException(secretKey.keyIdentifier, e)
|
||||||
}
|
}
|
||||||
if (decryptWithPrivateKey(esks, privateKey.unlockedKey, decryptionKeyId, pkesk)) {
|
if (decryptWithPrivateKey(esks, privateKey.keyPair, decryptionKeyId, pkesk)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.pgpainless.util.Passphrase
|
||||||
|
|
||||||
class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
||||||
private val _encryptionMethods: MutableSet<PGPKeyEncryptionMethodGenerator> = mutableSetOf()
|
private val _encryptionMethods: MutableSet<PGPKeyEncryptionMethodGenerator> = mutableSetOf()
|
||||||
|
private val _encryptionKeys: MutableSet<OpenPGPComponentKey> = mutableSetOf()
|
||||||
private val _encryptionKeyIdentifiers: MutableSet<SubkeyIdentifier> = mutableSetOf()
|
private val _encryptionKeyIdentifiers: MutableSet<SubkeyIdentifier> = mutableSetOf()
|
||||||
private val _keyRingInfo: MutableMap<SubkeyIdentifier, KeyRingInfo> = mutableMapOf()
|
private val _keyRingInfo: MutableMap<SubkeyIdentifier, KeyRingInfo> = mutableMapOf()
|
||||||
private val _keyViews: MutableMap<SubkeyIdentifier, KeyAccessor> = mutableMapOf()
|
private val _keyViews: MutableMap<SubkeyIdentifier, KeyAccessor> = mutableMapOf()
|
||||||
|
@ -40,6 +41,9 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
||||||
val encryptionKeyIdentifiers
|
val encryptionKeyIdentifiers
|
||||||
get() = _encryptionKeyIdentifiers.toSet()
|
get() = _encryptionKeyIdentifiers.toSet()
|
||||||
|
|
||||||
|
val encryptionKeys
|
||||||
|
get() = _encryptionKeys.toSet()
|
||||||
|
|
||||||
val keyRingInfo
|
val keyRingInfo
|
||||||
get() = _keyRingInfo.toMap()
|
get() = _keyRingInfo.toMap()
|
||||||
|
|
||||||
|
@ -326,6 +330,7 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addRecipientKey(key: OpenPGPComponentKey, wildcardKeyId: Boolean) {
|
private fun addRecipientKey(key: OpenPGPComponentKey, wildcardKeyId: Boolean) {
|
||||||
|
_encryptionKeys.add(key)
|
||||||
_encryptionKeyIdentifiers.add(SubkeyIdentifier(key))
|
_encryptionKeyIdentifiers.add(SubkeyIdentifier(key))
|
||||||
addEncryptionMethod(
|
addEncryptionMethod(
|
||||||
ImplementationFactory.getInstance()
|
ImplementationFactory.getInstance()
|
||||||
|
|
|
@ -450,8 +450,7 @@ class SigningOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
val generator: PGPSignatureGenerator =
|
val generator: PGPSignatureGenerator =
|
||||||
createSignatureGenerator(
|
createSignatureGenerator(signingKey.keyPair.privateKey, hashAlgorithm, signatureType)
|
||||||
signingKey.unlockedKey.privateKey, hashAlgorithm, signatureType)
|
|
||||||
|
|
||||||
// Subpackets
|
// Subpackets
|
||||||
val hashedSubpackets =
|
val hashedSubpackets =
|
||||||
|
|
|
@ -61,7 +61,7 @@ class UnlockSecretKey {
|
||||||
|
|
||||||
if (PGPainless.getPolicy().isEnableKeyParameterValidation()) {
|
if (PGPainless.getPolicy().isEnableKeyParameterValidation()) {
|
||||||
PublicKeyParameterValidationUtil.verifyPublicKeyParameterIntegrity(
|
PublicKeyParameterValidationUtil.verifyPublicKeyParameterIntegrity(
|
||||||
privateKey.unlockedKey.privateKey, privateKey.unlockedKey.publicKey)
|
privateKey.keyPair.privateKey, privateKey.keyPair.publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
return privateKey
|
return privateKey
|
||||||
|
|
|
@ -16,11 +16,10 @@ import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
|
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
|
||||||
|
import org.bouncycastle.openpgp.api.OpenPGPKey;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -28,7 +27,6 @@ import org.pgpainless.PGPainless;
|
||||||
import org.pgpainless.algorithm.KeyFlag;
|
import org.pgpainless.algorithm.KeyFlag;
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||||
import org.pgpainless.exception.KeyException;
|
import org.pgpainless.exception.KeyException;
|
||||||
import org.pgpainless.key.SubkeyIdentifier;
|
|
||||||
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.eddsa_legacy.EdDSALegacyCurve;
|
import org.pgpainless.key.generation.type.eddsa_legacy.EdDSALegacyCurve;
|
||||||
|
@ -40,11 +38,11 @@ import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class EncryptionOptionsTest {
|
public class EncryptionOptionsTest {
|
||||||
|
|
||||||
private static PGPSecretKeyRing secretKeys;
|
private static OpenPGPKey secretKeys;
|
||||||
private static PGPPublicKeyRing publicKeys;
|
private static OpenPGPCertificate publicKeys;
|
||||||
private static SubkeyIdentifier primaryKey;
|
private static OpenPGPCertificate.OpenPGPComponentKey primaryKey;
|
||||||
private static SubkeyIdentifier encryptComms;
|
private static OpenPGPCertificate.OpenPGPComponentKey encryptComms;
|
||||||
private static SubkeyIdentifier encryptStorage;
|
private static OpenPGPCertificate.OpenPGPComponentKey encryptStorage;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void generateKey() {
|
public static void generateKey() {
|
||||||
|
@ -56,15 +54,14 @@ public class EncryptionOptionsTest {
|
||||||
.addSubkey(KeySpec.getBuilder(KeyType.XDH_LEGACY(XDHLegacySpec._X25519), KeyFlag.ENCRYPT_STORAGE)
|
.addSubkey(KeySpec.getBuilder(KeyType.XDH_LEGACY(XDHLegacySpec._X25519), KeyFlag.ENCRYPT_STORAGE)
|
||||||
.build())
|
.build())
|
||||||
.addUserId("test@pgpainless.org")
|
.addUserId("test@pgpainless.org")
|
||||||
.build()
|
.build();
|
||||||
.getPGPSecretKeyRing();
|
|
||||||
|
|
||||||
publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
publicKeys = secretKeys.toCertificate();
|
||||||
|
|
||||||
Iterator<PGPPublicKey> iterator = publicKeys.iterator();
|
Iterator<OpenPGPCertificate.OpenPGPComponentKey> iterator = publicKeys.getKeys().iterator();
|
||||||
primaryKey = new SubkeyIdentifier(publicKeys, iterator.next().getKeyID());
|
primaryKey = iterator.next();
|
||||||
encryptComms = new SubkeyIdentifier(publicKeys, iterator.next().getKeyID());
|
encryptComms = iterator.next();
|
||||||
encryptStorage = new SubkeyIdentifier(publicKeys, iterator.next().getKeyID());
|
encryptStorage = iterator.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -91,7 +88,7 @@ public class EncryptionOptionsTest {
|
||||||
EncryptionOptions options = EncryptionOptions.encryptCommunications();
|
EncryptionOptions options = EncryptionOptions.encryptCommunications();
|
||||||
options.addRecipient(publicKeys);
|
options.addRecipient(publicKeys);
|
||||||
|
|
||||||
Set<SubkeyIdentifier> encryptionKeys = options.getEncryptionKeyIdentifiers();
|
Set<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys = options.getEncryptionKeys();
|
||||||
assertEquals(1, encryptionKeys.size());
|
assertEquals(1, encryptionKeys.size());
|
||||||
assertEquals(encryptComms, encryptionKeys.iterator().next());
|
assertEquals(encryptComms, encryptionKeys.iterator().next());
|
||||||
}
|
}
|
||||||
|
@ -101,7 +98,7 @@ public class EncryptionOptionsTest {
|
||||||
EncryptionOptions options = EncryptionOptions.encryptDataAtRest();
|
EncryptionOptions options = EncryptionOptions.encryptDataAtRest();
|
||||||
options.addRecipient(publicKeys);
|
options.addRecipient(publicKeys);
|
||||||
|
|
||||||
Set<SubkeyIdentifier> encryptionKeys = options.getEncryptionKeyIdentifiers();
|
Set<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys = options.getEncryptionKeys();
|
||||||
assertEquals(1, encryptionKeys.size());
|
assertEquals(1, encryptionKeys.size());
|
||||||
assertEquals(encryptStorage, encryptionKeys.iterator().next());
|
assertEquals(encryptStorage, encryptionKeys.iterator().next());
|
||||||
}
|
}
|
||||||
|
@ -111,7 +108,7 @@ public class EncryptionOptionsTest {
|
||||||
EncryptionOptions options = new EncryptionOptions();
|
EncryptionOptions options = new EncryptionOptions();
|
||||||
options.addRecipient(publicKeys, EncryptionOptions.encryptToAllCapableSubkeys());
|
options.addRecipient(publicKeys, EncryptionOptions.encryptToAllCapableSubkeys());
|
||||||
|
|
||||||
Set<SubkeyIdentifier> encryptionKeys = options.getEncryptionKeyIdentifiers();
|
Set<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys = options.getEncryptionKeys();
|
||||||
|
|
||||||
assertEquals(2, encryptionKeys.size());
|
assertEquals(2, encryptionKeys.size());
|
||||||
assertTrue(encryptionKeys.contains(encryptComms));
|
assertTrue(encryptionKeys.contains(encryptComms));
|
||||||
|
@ -136,12 +133,11 @@ public class EncryptionOptionsTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAddRecipient_KeyWithoutEncryptionKeyFails() {
|
public void testAddRecipient_KeyWithoutEncryptionKeyFails() {
|
||||||
EncryptionOptions options = new EncryptionOptions();
|
EncryptionOptions options = new EncryptionOptions();
|
||||||
PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing()
|
OpenPGPKey secretKeys = PGPainless.buildKeyRing()
|
||||||
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
|
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
|
||||||
.addUserId("test@pgpainless.org")
|
.addUserId("test@pgpainless.org")
|
||||||
.build()
|
.build();
|
||||||
.getPGPSecretKeyRing();
|
OpenPGPCertificate publicKeys = secretKeys.toCertificate();
|
||||||
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
|
||||||
|
|
||||||
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, () -> options.addRecipient(publicKeys));
|
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, () -> options.addRecipient(publicKeys));
|
||||||
}
|
}
|
||||||
|
@ -175,7 +171,7 @@ public class EncryptionOptionsTest {
|
||||||
.getPGPSecretKeyRing());
|
.getPGPSecretKeyRing());
|
||||||
|
|
||||||
PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(
|
PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(
|
||||||
Arrays.asList(publicKeys, secondKeyRing));
|
Arrays.asList(publicKeys.getPGPPublicKeyRing(), secondKeyRing));
|
||||||
|
|
||||||
EncryptionOptions options = new EncryptionOptions();
|
EncryptionOptions options = new EncryptionOptions();
|
||||||
options.addRecipients(collection, EncryptionOptions.encryptToFirstSubkey());
|
options.addRecipients(collection, EncryptionOptions.encryptToFirstSubkey());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue