From 1ce6632f1874f769dc1876e25918c97c5f5f74b0 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 15 Aug 2021 15:21:14 +0200 Subject: [PATCH] Rename KeyValidationException -> KeyValidationError --- .../encryption_signing/SigningOptions.java | 26 ++++++++----------- ...Exception.java => KeyValidationError.java} | 4 +-- .../org/pgpainless/key/info/KeyRingInfo.java | 2 +- .../EncryptionOptionsTest.java | 4 +-- .../encryption_signing/SigningTest.java | 6 ++--- 5 files changed, 19 insertions(+), 23 deletions(-) rename pgpainless-core/src/main/java/org/pgpainless/exception/{KeyValidationException.java => KeyValidationError.java} (82%) diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java index 430b1820..3f4b52f3 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java @@ -35,7 +35,7 @@ import org.pgpainless.PGPainless; import org.pgpainless.algorithm.DocumentSignatureType; import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.exception.KeyCannotSignException; -import org.pgpainless.exception.KeyValidationException; +import org.pgpainless.exception.KeyValidationError; import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.SubkeyIdentifier; @@ -104,13 +104,13 @@ public final class SigningOptions { * @param signingKeys collection of signing keys * @param signatureType type of signature (binary, canonical text) * @return this - * @throws KeyValidationException if something is wrong with any of the keys + * @throws KeyValidationError if something is wrong with any of the keys * @throws PGPException if any of the keys cannot be unlocked or a signing method cannot be created */ public SigningOptions addInlineSignatures(SecretKeyRingProtector secrectKeyDecryptor, PGPSecretKeyRingCollection signingKeys, DocumentSignatureType signatureType) - throws KeyValidationException, PGPException { + throws KeyValidationError, PGPException { for (PGPSecretKeyRing signingKey : signingKeys) { addInlineSignature(secrectKeyDecryptor, signingKey, signatureType); } @@ -125,14 +125,14 @@ public final class SigningOptions { * @param secretKeyDecryptor decryptor to unlock the signing secret key * @param secretKey signing key * @param signatureType type of signature (binary, canonical text) - * @throws KeyValidationException if something is wrong with the key + * @throws KeyValidationError if something is wrong with the key * @throws PGPException if the key cannot be unlocked or the signing method cannot be created * @return this */ public SigningOptions addInlineSignature(SecretKeyRingProtector secretKeyDecryptor, PGPSecretKeyRing secretKey, DocumentSignatureType signatureType) - throws KeyValidationException, PGPException { + throws KeyValidationError, PGPException { return addInlineSignature(secretKeyDecryptor, secretKey, null, signatureType); } @@ -148,19 +148,17 @@ public final class SigningOptions { * @param userId user-id of the signer * @param signatureType signature type (binary, canonical text) * @return this - * @throws KeyValidationException if the key is invalid + * @throws KeyValidationError if the key is invalid * @throws PGPException if the key cannot be unlocked or the signing method cannot be created */ public SigningOptions addInlineSignature(SecretKeyRingProtector secretKeyDecryptor, PGPSecretKeyRing secretKey, String userId, DocumentSignatureType signatureType) - throws KeyValidationException, PGPException { + throws KeyValidationError, PGPException { KeyRingInfo keyRingInfo = new KeyRingInfo(secretKey, new Date()); - if (userId != null) { - if (!keyRingInfo.isUserIdValid(userId)) { - throw new KeyValidationException(userId, keyRingInfo.getLatestUserIdCertification(userId), keyRingInfo.getUserIdRevocation(userId)); - } + if (userId != null && !keyRingInfo.isUserIdValid(userId)) { + throw new KeyValidationError(userId, keyRingInfo.getLatestUserIdCertification(userId), keyRingInfo.getUserIdRevocation(userId)); } List signingPubKeys = keyRingInfo.getSigningSubkeys(); @@ -238,10 +236,8 @@ public final class SigningOptions { DocumentSignatureType signatureType) throws PGPException { KeyRingInfo keyRingInfo = new KeyRingInfo(secretKey, new Date()); - if (userId != null) { - if (!keyRingInfo.isUserIdValid(userId)) { - throw new KeyValidationException(userId, keyRingInfo.getLatestUserIdCertification(userId), keyRingInfo.getUserIdRevocation(userId)); - } + if (userId != null && !keyRingInfo.isUserIdValid(userId)) { + throw new KeyValidationError(userId, keyRingInfo.getLatestUserIdCertification(userId), keyRingInfo.getUserIdRevocation(userId)); } List signingPubKeys = keyRingInfo.getSigningSubkeys(); diff --git a/pgpainless-core/src/main/java/org/pgpainless/exception/KeyValidationException.java b/pgpainless-core/src/main/java/org/pgpainless/exception/KeyValidationError.java similarity index 82% rename from pgpainless-core/src/main/java/org/pgpainless/exception/KeyValidationException.java rename to pgpainless-core/src/main/java/org/pgpainless/exception/KeyValidationError.java index a025c6bf..c0fdb9e5 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/exception/KeyValidationException.java +++ b/pgpainless-core/src/main/java/org/pgpainless/exception/KeyValidationError.java @@ -17,9 +17,9 @@ package org.pgpainless.exception; import org.bouncycastle.openpgp.PGPSignature; -public class KeyValidationException extends AssertionError { +public class KeyValidationError extends AssertionError { - public KeyValidationException(String userId, PGPSignature userIdSig, PGPSignature userIdRevocation) { + public KeyValidationError(String userId, PGPSignature userIdSig, PGPSignature userIdRevocation) { super("User-ID '" + userId + "' is not valid: Sig: " + userIdSig + " Rev: " + userIdRevocation); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java index 6a5dbb81..98d92ee6 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java @@ -47,7 +47,7 @@ import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.PublicKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; -import org.pgpainless.exception.KeyValidationException; +import org.pgpainless.exception.KeyValidationError; import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.SubkeyIdentifier; import org.pgpainless.policy.Policy; diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java index 59a0f9cc..78617349 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java @@ -39,7 +39,7 @@ import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; -import org.pgpainless.exception.KeyValidationException; +import org.pgpainless.exception.KeyValidationError; import org.pgpainless.key.SubkeyIdentifier; import org.pgpainless.key.generation.KeySpec; import org.pgpainless.key.generation.type.KeyType; @@ -194,6 +194,6 @@ public class EncryptionOptionsTest { @Test public void testAddRecipient_withInvalidUserId() { EncryptionOptions options = new EncryptionOptions(); - assertThrows(KeyValidationException.class, () -> options.addRecipient(publicKeys, "invalid@user.id")); + assertThrows(KeyValidationError.class, () -> options.addRecipient(publicKeys, "invalid@user.id")); } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/SigningTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/SigningTest.java index a40567aa..1c2a9b8e 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/SigningTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/SigningTest.java @@ -42,7 +42,7 @@ import org.pgpainless.algorithm.DocumentSignatureType; import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.OpenPgpMetadata; -import org.pgpainless.exception.KeyValidationException; +import org.pgpainless.exception.KeyValidationError; import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.key.TestKeys; import org.pgpainless.key.info.KeyRingInfo; @@ -121,7 +121,7 @@ public class SigningTest { SigningOptions opts = new SigningOptions(); // "bob" is not a valid user-id - assertThrows(KeyValidationException.class, + assertThrows(KeyValidationError.class, () -> opts.addInlineSignature(protector, secretKeys, "bob", DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)); } @@ -138,7 +138,7 @@ public class SigningTest { SigningOptions opts = new SigningOptions(); // "alice" has been revoked - assertThrows(KeyValidationException.class, + assertThrows(KeyValidationError.class, () -> opts.addInlineSignature(protector, fSecretKeys, "alice", DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)); } }