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

Fix some tests

This commit is contained in:
Paul Schaub 2025-02-04 15:59:26 +01:00
parent e20beb6755
commit 2c0edf9588
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 22 additions and 24 deletions

View file

@ -4,11 +4,13 @@
package org.pgpainless.algorithm
enum class EncryptionPurpose {
import org.bouncycastle.bcpg.sig.KeyFlags
enum class EncryptionPurpose(val code: Int) {
/** The stream will encrypt communication that goes over the wire. E.g. EMail, Chat... */
COMMUNICATIONS,
COMMUNICATIONS(KeyFlags.ENCRYPT_COMMS),
/** The stream will encrypt data at rest. E.g. Encrypted backup... */
STORAGE,
STORAGE(KeyFlags.ENCRYPT_STORAGE),
/** The stream will use keys with either flags to encrypt the data. */
ANY
ANY(KeyFlags.ENCRYPT_COMMS or KeyFlags.ENCRYPT_STORAGE)
}

View file

@ -97,10 +97,10 @@ class KeyRingInfo(
/** List of valid public subkeys. */
val validSubkeys: List<PGPPublicKey> =
keys.pgpKeyRing.publicKeys.asSequence().filter { isKeyValidlyBound(it.keyID) }.toList()
keys.publicKeys.values.filter { it.isBoundAt(referenceDate) }.map { it.pgpPublicKey }
/** List of valid user-IDs. */
val validUserIds: List<String> = userIds.filter { isUserIdBound(it) }
val validUserIds: List<String> = keys.getValidUserIds(referenceDate).map { it.userId }
/** List of valid and expired user-IDs. */
val validAndExpiredUserIds: List<String> =
@ -136,7 +136,7 @@ class KeyRingInfo(
val creationDate: Date = publicKey.creationTime!!
/** Latest date at which the key was modified (either by adding a subkey or self-signature). */
val lastModified: Date = getMostRecentSignature()?.creationTime ?: getLatestKeyCreationDate()
val lastModified: Date = keys.lastModificationDate
/** True, if the underlying keyring is a [PGPSecretKeyRing]. */
val isSecretKey: Boolean = keys.pgpKeyRing is PGPSecretKeyRing
@ -195,10 +195,11 @@ class KeyRingInfo(
/** List of all subkeys that can be used to sign a message. */
val signingSubkeys: List<PGPPublicKey> =
validSubkeys.filter { getKeyFlagsOf(it.keyID).contains(KeyFlag.SIGN_DATA) }
keys.getSigningKeys(referenceDate).map { it.pgpPublicKey }
/** Whether the key is usable for encryption. */
val isUsableForEncryption: Boolean = isUsableForEncryption(EncryptionPurpose.ANY)
val isUsableForEncryption: Boolean =
keys.getComponentKeysWithFlag(referenceDate, EncryptionPurpose.ANY.code).isNotEmpty()
/**
* Whether the key is capable of signing messages. This field is also true, if the key contains
@ -417,7 +418,7 @@ class KeyRingInfo(
* @return latest key creation time
*/
fun getLatestKeyCreationDate(): Date =
validSubkeys.maxByOrNull { creationDate }?.creationTime
keys.getValidKeys(referenceDate).maxByOrNull { it.creationTime }?.creationTime
?: throw AssertionError("Apparently there is no validly bound key in this key ring.")
/**
@ -426,7 +427,7 @@ class KeyRingInfo(
* @return latest self-certification for the given user-ID.
*/
fun getLatestUserIdCertification(userId: CharSequence): PGPSignature? =
signatures.userIdCertifications[userId]
keys.getUserId(userId.toString())?.getCertification(referenceDate)?.signature
/**
* Return the latest revocation self-signature for the given user-ID
@ -434,7 +435,7 @@ class KeyRingInfo(
* @return latest user-ID revocation for the given user-ID
*/
fun getUserIdRevocation(userId: CharSequence): PGPSignature? =
signatures.userIdRevocations[userId]
keys.getUserId(userId.toString())?.getRevocation(referenceDate)?.signature
/**
* Return the current binding signature for the subkey with the given key-ID.

View file

@ -13,6 +13,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.EncryptionPurpose;
@ -28,16 +29,15 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CustomPublicKeyDataDecryptorFactoryTest {
@Test
@Disabled
public void testDecryptionWithEmulatedHardwareDecryptionCallback()
throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
throws PGPException, IOException {
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing().modernKeyRing("Alice");
PGPPublicKeyRing cert = PGPainless.extractCertificate(secretKey);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKey);

View file

@ -183,7 +183,7 @@ public class ModifyKeys {
* The provided expiration date will be set on each user-id certification signature.
*/
@Test
public void setKeyExpirationDate() throws PGPException {
public void setKeyExpirationDate() {
Date expirationDate = DateUtil.parseUTCDate("2030-06-24 12:44:56 UTC");
SecretKeyRingProtector protector = SecretKeyRingProtector

View file

@ -6,13 +6,10 @@ package org.pgpainless.key.generation;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
@ -29,8 +26,7 @@ import org.pgpainless.util.DateUtil;
public class GenerateKeyWithCustomCreationDateTest {
@Test
public void generateKeyWithCustomCreationDateTest()
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
public void generateKeyWithCustomCreationDateTest() {
Date creationDate = DateUtil.parseUTCDate("2018-06-11 14:12:09 UTC");
PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing()
.addSubkey(KeySpec.getBuilder(KeyType.XDH_LEGACY(XDHLegacySpec._X25519), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE))
@ -49,7 +45,7 @@ public class GenerateKeyWithCustomCreationDateTest {
}
@Test
public void generateSubkeyWithFutureKeyCreationDate() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
public void generateSubkeyWithFutureKeyCreationDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 20);
Date future = calendar.getTime();

View file

@ -219,8 +219,7 @@ public class KeyRingInfoTest {
@TestTemplate
@ExtendWith(TestAllImplementations.class)
public void testGetKeysWithFlagsAndExpiry()
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
public void testGetKeysWithFlagsAndExpiry() {
PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(