diff --git a/jsign-crypto/src/main/java/net/jsign/KeyStoreType.java b/jsign-crypto/src/main/java/net/jsign/KeyStoreType.java index bb41a41d..53472ea9 100644 --- a/jsign-crypto/src/main/java/net/jsign/KeyStoreType.java +++ b/jsign-crypto/src/main/java/net/jsign/KeyStoreType.java @@ -119,6 +119,9 @@ void validate(KeyStoreBuilder params) { if (params.keystore() == null) { throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set"); } + if (!params.createFile(params.keystore()).exists()) { + throw new IllegalArgumentException("The keystore " + params.keystore() + " couldn't be found"); + } } }, @@ -129,6 +132,9 @@ void validate(KeyStoreBuilder params) { if (params.keystore() == null) { throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set"); } + if (!params.createFile(params.keystore()).exists()) { + throw new IllegalArgumentException("The keystore " + params.keystore() + " couldn't be found"); + } } }, @@ -139,6 +145,9 @@ void validate(KeyStoreBuilder params) { if (params.keystore() == null) { throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set"); } + if (!params.createFile(params.keystore()).exists()) { + throw new IllegalArgumentException("The keystore " + params.keystore() + " couldn't be found"); + } } }, @@ -629,10 +638,6 @@ KeyStore getKeystore(KeyStoreBuilder params, Provider provider) throws KeyStoreE throw new KeyStoreException("keystore type '" + name() + "' is not supported" + (provider != null ? " with security provider " + provider.getName() : ""), e); } - if (fileBased && (params.keystore() == null || !params.createFile(params.keystore()).exists())) { - throw new KeyStoreException("The keystore " + params.keystore() + " couldn't be found"); - } - try { try (FileInputStream in = fileBased ? new FileInputStream(params.createFile(params.keystore())) : null) { ks.load(in, params.storepass() != null ? params.storepass().toCharArray() : null); diff --git a/jsign-crypto/src/test/java/net/jsign/KeyStoreBuilderTest.java b/jsign-crypto/src/test/java/net/jsign/KeyStoreBuilderTest.java index 2b34ae2c..bcb029b2 100644 --- a/jsign-crypto/src/test/java/net/jsign/KeyStoreBuilderTest.java +++ b/jsign-crypto/src/test/java/net/jsign/KeyStoreBuilderTest.java @@ -337,6 +337,11 @@ public void testBuildJKS() throws Exception { Exception e = assertThrows(IllegalArgumentException.class, builder::build); assertEquals("message", "keystore parameter must be set", e.getMessage()); + builder.keystore("target/test-classes/keystores/missing.jks"); + + e = assertThrows(IllegalArgumentException.class, builder::build); + assertEquals("message", "The keystore target/test-classes/keystores/missing.jks couldn't be found", e.getMessage()); + builder.keystore("target/test-classes/keystores/keystore.jks"); KeyStore keystore = builder.build(); @@ -350,6 +355,11 @@ public void testBuildJCEKS() throws Exception { Exception e = assertThrows(IllegalArgumentException.class, builder::build); assertEquals("message", "keystore parameter must be set", e.getMessage()); + builder.keystore("target/test-classes/keystores/missing.jceks"); + + e = assertThrows(IllegalArgumentException.class, builder::build); + assertEquals("message", "The keystore target/test-classes/keystores/missing.jceks couldn't be found", e.getMessage()); + builder.keystore("target/test-classes/keystores/keystore.jceks"); KeyStore keystore = builder.build(); @@ -363,6 +373,11 @@ public void testBuildPKCS12() throws Exception { Exception e = assertThrows(IllegalArgumentException.class, builder::build); assertEquals("message", "keystore parameter must be set", e.getMessage()); + builder.keystore("target/test-classes/keystores/missing.p12"); + + e = assertThrows(IllegalArgumentException.class, builder::build); + assertEquals("message", "The keystore target/test-classes/keystores/missing.p12 couldn't be found", e.getMessage()); + builder.keystore("target/test-classes/keystores/keystore.p12"); KeyStore keystore = builder.build();