Skip to content

Commit 00f3b55

Browse files
committed
[refactor] Cleanup exception to error mapping
1 parent d341085 commit 00f3b55

File tree

10 files changed

+90
-54
lines changed

10 files changed

+90
-54
lines changed

src/main/java/ro/kuberam/libs/java/crypto/CryptoError.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,49 @@
1919
*/
2020
package ro.kuberam.libs.java.crypto;
2121

22+
import javax.annotation.Nullable;
23+
import javax.crypto.BadPaddingException;
24+
import javax.crypto.IllegalBlockSizeException;
25+
import javax.crypto.NoSuchPaddingException;
26+
import java.security.InvalidKeyException;
27+
import java.security.KeyStoreException;
28+
import java.security.NoSuchAlgorithmException;
29+
import java.security.NoSuchProviderException;
30+
import java.security.spec.InvalidKeySpecException;
31+
2232
public enum CryptoError {
2333

24-
UNKNOWN_ALGORITH("crypto:unknown-algorithm", "The specified algorithm is not supported."),
34+
UNKNOWN_ALGORITHM("crypto:unknown-algorithm", "The specified algorithm is not supported.", NoSuchAlgorithmException.class),
2535
UNKNOWN_PROVIDER("crypto:unknown-provider", "The specified provider is not available."),
2636
SIGNATURE_TYPE("crypto:signature-type", "The specified signature type is not supported."),
2737
UNREADABLE_KEYSTORE("crypto:unreadable-keystore", "I/O error while reading keystore, or the password is incorrect."),
2838
DENIED_KEYSTORE("crypto:denied-keystore", "Permission denied to read keystore."),
2939
KEYSTORE_URL("crypto:keystore-url", "The keystore URL is invalid."),
30-
KEYSTORE_TYPE("crypto:keystore-type", "The keystore type is not supported."),
40+
KEYSTORE_TYPE("crypto:keystore-type", "The keystore type is not supported.", KeyStoreException.class),
3141
ALIAS_KEY("crypto:alias-key", "Cannot find key for alias in given keystore."),
3242
SIGNATURE_ELEMENT("crypto:signature-element", "Cannot find Signature element."),
33-
INEXISTENT_PADDING("crypto:inexistent-padding", "No such padding."),
34-
INCORRECT_PADDING("crypto:incorrect-padding", "Incorrect padding."),
43+
INEXISTENT_PADDING("crypto:inexistent-padding", "No such padding.", NoSuchPaddingException.class),
44+
INCORRECT_PADDING("crypto:incorrect-padding", "Incorrect padding.", BadPaddingException.class),
3545
ENCRYPTION_TYPE("crypto:encryption-type", "The encryption type is not supported."),
36-
INVALID_CRYPTO_KEY("crypto:invalid-crypto-key", "The cryptographic key is invalid."),
37-
BLOCK_SIZE("crypto:block-size", "Illegal block size."),
46+
INVALID_CRYPTO_KEY("crypto:invalid-crypto-key", "The cryptographic key is invalid.", InvalidKeySpecException.class, InvalidKeyException.class),
47+
BLOCK_SIZE("crypto:block-size", "Illegal block size.", IllegalBlockSizeException.class),
3848
DECRYPTION_TYPE("crypto:decryption-type", "The decryption type is not supported."),
39-
NO_PROVIDER("crypto:no-provider", "The provider is not set."),
49+
NO_PROVIDER("crypto:no-provider", "The provider is not set.", NoSuchProviderException.class),
4050
INPUT_RESOURCES("crypto.input-resources", "The 'enveloped' and 'enveloping' signatures have to be applied to only one resource."),
4151
INCORRECT_INITIALIZATION_VECTOR("crypto:incorrect-initialization-vector", "The initialization vector is not correct");
4252

43-
private String code;
44-
private String message;
53+
private final String code;
54+
private final String message;
55+
@Nullable final Class<? extends Throwable>[] describesExceptions;
56+
57+
CryptoError(final String code, final String message) {
58+
this(code, message, null);
59+
}
4560

46-
CryptoError(String code, String message) {
61+
CryptoError(final String code, final String message, @Nullable final Class<? extends Throwable>... describesExceptions) {
4762
this.code = code;
4863
this.message = message;
64+
this.describesExceptions = describesExceptions;
4965
}
5066

5167
public String getCode() {
@@ -58,6 +74,23 @@ public String getMessage() {
5874

5975
public String getDescription() {
6076
return this.code + ", " + this.message;
61-
}
62-
77+
}
78+
79+
public static @Nullable CryptoError describeException(@Nullable final Throwable t) {
80+
if (t == null) {
81+
return null;
82+
}
83+
84+
for (final CryptoError cryptoError : values()) {
85+
if (cryptoError.describesExceptions != null) {
86+
for (final Class<? extends Throwable> describesException : cryptoError.describesExceptions) {
87+
if (describesException == t.getClass()) {
88+
return cryptoError;
89+
}
90+
}
91+
}
92+
}
93+
94+
return null;
95+
}
6396
}

src/main/java/ro/kuberam/libs/java/crypto/CryptoException.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,37 @@
1919
*/
2020
package ro.kuberam.libs.java.crypto;
2121

22+
import javax.annotation.Nullable;
23+
2224
public class CryptoException extends Exception {
23-
/**
24-
*
25-
*/
2625
private static final long serialVersionUID = -2606956271206243301L;
27-
private CryptoError cryptoError;
26+
@Nullable private final CryptoError cryptoError;
2827

29-
public CryptoException(CryptoError cryptoError) {
28+
public CryptoException(final CryptoError cryptoError) {
3029
super(cryptoError.getDescription());
3130
this.cryptoError = cryptoError;
3231
}
3332

34-
public CryptoException(CryptoError cryptoError, Throwable cause) {
33+
public CryptoException(final CryptoError cryptoError, final Throwable cause) {
3534
super(cryptoError.getDescription(), cause);
3635
this.cryptoError = cryptoError;
3736
}
3837

39-
public CryptoException(Throwable cause) {
40-
super(CryptoError.valueOf(cause.getClass().getSimpleName()).getDescription(), cause);
41-
}
38+
public CryptoException(final Throwable cause) {
39+
super(getDesc(cause), cause);
40+
this.cryptoError = CryptoError.describeException(cause);
41+
}
42+
43+
private static String getDesc(final Throwable cause) {
44+
final CryptoError cryptoError = CryptoError.describeException(cause);
45+
if (cryptoError != null) {
46+
return cryptoError.getDescription();
47+
} else {
48+
return cause.getMessage();
49+
}
50+
}
4251

52+
@Nullable
4353
public CryptoError getCryptoError() {
4454
return cryptoError;
4555
}

src/main/java/ro/kuberam/libs/java/crypto/Parameters.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public String getCanonicalizationAlgorithm() {
4242

4343
public void setCanonicalizationAlgorithm(final String canonicalizationAlgorithm) throws CryptoException {
4444
if (!contains(CANONICALIZATION_ALGORITHM_VALUES, canonicalizationAlgorithm)) {
45-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
45+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
4646
}
4747

4848
if (canonicalizationAlgorithm.equals("exclusive")) {
@@ -62,7 +62,7 @@ public String getDigestAlgorithm() {
6262

6363
public void setDigestAlgorithm(final String digestAlgorithm) throws CryptoException {
6464
if (!contains(DIGEST_ALGORITHM_VALUES, digestAlgorithm)) {
65-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
65+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
6666
}
6767

6868
if (digestAlgorithm.equals("SHA256")) {
@@ -80,7 +80,7 @@ public String getSignatureAlgorithm() {
8080

8181
public void setSignatureAlgorithm(final String signatureAlgorithm) throws CryptoException {
8282
if (!contains(SIGNATURE_ALGORITHM_VALUES, signatureAlgorithm)) {
83-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
83+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
8484
}
8585

8686
if (signatureAlgorithm.equals("DSA_SHA1")) {

src/main/java/ro/kuberam/libs/java/crypto/digest/Hash.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import java.io.InputStream;
2424
import java.nio.charset.StandardCharsets;
2525
import java.security.*;
26-
import java.util.Base64;
27-
import java.util.Optional;
28-
26+
import java.util.*;
2927
import javax.annotation.Nullable;
3028

3129
import org.apache.logging.log4j.LogManager;
@@ -116,7 +114,7 @@ private static MessageDigest getMessageDigester(final String algorithm, @Nullabl
116114
return MessageDigest.getInstance(algorithm);
117115
}
118116
} catch (final NoSuchAlgorithmException e) {
119-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
117+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM, e);
120118
} catch (final NoSuchProviderException e) {
121119
throw new CryptoException(CryptoError.UNKNOWN_PROVIDER, e);
122120
}

src/main/java/ro/kuberam/libs/java/crypto/digitalSignature/GenerateXmlSignature.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static String generate(Document inputDoc, String mechanismType, String ca
103103
NodeList nodes = (NodeList) expr.evaluate(inputDoc, XPathConstants.NODESET);
104104
if (nodes.getLength() < 1) {
105105
// TODO this error message has to replaced
106-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
106+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
107107
}
108108

109109
// Node nodeToSign = nodes.item(0);
@@ -219,14 +219,14 @@ public static String generate(Document inputDoc, String mechanismType, String ca
219219
return serializer.writeToString(sigParent);
220220
}
221221
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
222-
throw new CryptoException(CryptoError.NoSuchAlgorithmException, e);
222+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM, e);
223223
} catch (CertificateException e) {
224224
// TODO error code needs improving
225-
throw new CryptoException(CryptoError.InvalidKeySpecException, e);
225+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
226226
} catch (KeyStoreException e) {
227227
throw new CryptoException(CryptoError.UNREADABLE_KEYSTORE, e);
228228
} catch (UnrecoverableKeyException | KeyException e) {
229-
throw new CryptoException(CryptoError.InvalidKeySpecException, e);
229+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
230230
} catch (ParserConfigurationException | XPathExpressionException | MarshalException e) {
231231
throw new IOException(e);
232232
}
@@ -268,7 +268,7 @@ private static String getCanonicalizationAlgorithmUri(String canonicalizationAlg
268268
return CanonicalizationMethod.INCLUSIVE;
269269

270270
default:
271-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
271+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
272272
}
273273
}
274274

@@ -285,7 +285,7 @@ private static String getDigestAlgorithmURI(String digestAlgorithm) throws Crypt
285285
return DigestMethod.SHA1;
286286

287287
default:
288-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
288+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
289289
}
290290
}
291291

@@ -299,7 +299,7 @@ private static String getSignatureAlgorithmURI(String signatureAlgorithm) throws
299299
return SignatureMethod.RSA_SHA1;
300300

301301
default:
302-
throw new CryptoException(CryptoError.NoSuchAlgorithmException);
302+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM);
303303
}
304304
}
305305
}

src/main/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static byte[] operation(byte[] input, String secretKey, String transforma
8181
try {
8282
cipher.init(operationType, skeySpec);
8383
} catch (InvalidKeyException e) {
84-
throw new CryptoException(CryptoError.InvalidKeySpecException, e);
84+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
8585
}
8686
}
8787

src/test/java/ro/kuberam/libs/java/crypto/ParametersTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testCanonicalizationAlgorithmWrongValue() {
3434
parameters.setCanonicalizationAlgorithm("inclusive-with-commentss");
3535
fail("Algorithm should have been unknown");
3636
} catch (CryptoException e) {
37-
assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError());
37+
assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError());
3838
}
3939
}
4040

@@ -52,7 +52,7 @@ public void testDigestAlgorithmWrongValue() {
5252
parameters.setDigestAlgorithm("SHA1008");
5353
fail("Algorithm should have been unknown");
5454
} catch (CryptoException e) {
55-
assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError());
55+
assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError());
5656
}
5757
}
5858

@@ -71,7 +71,7 @@ public void testSignatureAlgorithmWrongValue() {
7171
parameters.setSignatureAlgorithm("RSA_SHA1008");
7272
fail("Algorithm should have been unknown");
7373
} catch (CryptoException e) {
74-
assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError());
74+
assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError());
7575
}
7676
}
7777

src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryWithWrongAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void hashBinaryWithWrongAlgorithm() throws IOException {
3939
final String result = Hash.hashBinary(input, "SHA-17", "base64");
4040
fail("Algorithm should have been unknown");
4141
} catch (CryptoException e) {
42-
assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError());
42+
assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError());
4343

4444
}
4545
}

src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryWithWrongAlgorithmAndDefaultFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void hashBinaryWithWrongAlgorithm() throws IOException {
3939
Hash.hashBinary(input, "SHA-17");
4040
fail("algorithm should have been unknown");
4141
} catch (CryptoException e) {
42-
assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError());
42+
assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError());
4343
}
4444
}
4545
}

src/test/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryptionTest.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.IOException;
2626

2727
import org.junit.BeforeClass;
28-
import org.junit.Ignore;
2928
import org.junit.Test;
3029

3130
import ro.kuberam.libs.java.crypto.CryptoError;
@@ -41,24 +40,20 @@ public class SymmetricEncryptionTest extends CryptoModuleTests {
4140
private static String iv;
4241

4342
@BeforeClass
44-
public static void beforeClassFunction() {
45-
try {
46-
iv = Hash.hashString("initialization vector", "MD5", "hex");
47-
} catch (CryptoException e) {
48-
e.printStackTrace();
49-
}
43+
public static void beforeClassFunction() throws CryptoException {
44+
iv = Hash.hashString("initialization vector", "MD5", null, "hex");
5045
}
5146

5247
@Test
53-
public void aesAlgorithmAndDefaultProviderAndCbcMode() throws Exception {
48+
public void aesAlgorithmAndDefaultProviderAndCbcMode() throws CryptoException, IOException {
5449
byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmCbcMode, iv, "");
5550
byte[] decryptionResult = SymmetricEncryption.decrypt(encryptionResult, key, aesAlgorithmCbcMode, iv, "");
5651

5752
assertEquals(longString, new String(decryptionResult, UTF_8));
5853
}
5954

6055
@Test
61-
public void aesAlgorithmAndCbcMode() throws Exception {
56+
public void aesAlgorithmAndCbcMode() throws CryptoException, IOException {
6257
byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmCbcMode, iv,
6358
sunProvider);
6459
byte[] decryptionResult = SymmetricEncryption.decrypt(encryptionResult, key, aesAlgorithmCbcMode, iv,
@@ -68,21 +63,21 @@ public void aesAlgorithmAndCbcMode() throws Exception {
6863
}
6964

7065
@Test
71-
public void aesAlgorithmAndDefaultProviderAndEcbMode() throws Exception {
66+
public void aesAlgorithmAndDefaultProviderAndEcbMode() throws CryptoException, IOException {
7267
byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmEcbMode, "", "");
7368
byte[] decryptionResult = SymmetricEncryption.decrypt(encryptionResult, key, aesAlgorithmEcbMode, "", "");
7469

7570
assertEquals(longString, new String(decryptionResult, UTF_8));
7671
}
7772

78-
@Ignore
73+
//@Ignore
7974
@Test
80-
public void aesAlgorithmAndWrongKeyAndDefaultProviderAndCbcMode() throws IOException, CryptoException {
75+
public void aesAlgorithmAndWrongKeyAndDefaultProviderAndCbcMode() throws IOException {
8176
try {
8277
byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmCbcMode, iv, "");
8378
SymmetricEncryption.decrypt(encryptionResult, wrongKey, aesAlgorithmCbcMode, iv, "");
8479
} catch (CryptoException e) {
85-
assertEquals(CryptoError.InvalidKeySpecException, e.getCryptoError());
80+
assertEquals(CryptoError.INVALID_CRYPTO_KEY, e.getCryptoError());
8681
}
8782
}
8883
}

0 commit comments

Comments
 (0)