diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/SMPPropertyEnum.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/SMPPropertyEnum.java index fc876932097d67ef1f2690536d54c142e539d5db..2580fa73057645588c8d2961c78c1828664366ab 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/SMPPropertyEnum.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/SMPPropertyEnum.java @@ -21,6 +21,8 @@ public enum SMPPropertyEnum { ENCRYPTION_FILENAME("encryption.key.filename","encryptionPrivateKey.private","Path to the folder containing all the configuration files (keystore and sig0 key)"), + KEYSTORE_PASSWORD_DECRYPTED("smp.keystore.password.decrypted","","Only for backup purposes. This password was automatically created. Store password somewhere save and delete this entry!"), + SML_KEYSTORE_PASSWORD("bdmsl.integration.keystore.password","","Deprecated "), SML_KEYSTORE_PATH("bdmsl.integration.keystore.path","","Deprecated "), SIGNATURE_KEYSTORE_PASSWORD("xmldsig.keystore.password","","Deprecated "), diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIKeystoreService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIKeystoreService.java index 67bc0b7ffb65abcf421c6a6967a06b30afed8f45..d75b26baf282469ed4186ee1dcee2a7229c0b68d 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIKeystoreService.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIKeystoreService.java @@ -40,7 +40,7 @@ public class UIKeystoreService { @Value("${encryption.key.filename}") private String encryptionFilename; - private String smpKeyStorePasswordDecripted; + private String smpKeyStorePasswordDecrypted; private Map<String, Key> keystoreKeys; private Map<String, X509Certificate> keystoreCertificates; @@ -60,7 +60,7 @@ public class UIKeystoreService { LOG.info("initialize from configuration folder:"+configurationDir +", enc file: "+encryptionFilename+", keystore " + smpKeyStoreFilename); if (configurationDir==null || encryptionFilename==null){ - LOG.info("Configuration folder and/or encryption filename are not set in database!"); + LOG.warn("Configuration folder and/or encryption filename are not set in database!"); return; } @@ -68,15 +68,21 @@ public class UIKeystoreService { File file = new File(configurationDir + File.separator + encryptionFilename); File keystoreFilePath = new File(configurationDir + File.separator + smpKeyStoreFilename ); if (!file.exists()){ + LOG.error("Encryption key file '{}' does not exists!", file.getAbsolutePath()); return; } - smpKeyStorePasswordDecripted = SecurityUtils.decrypt(file,smpKeyStorePasswordEncrypted); + if (!keystoreFilePath.exists()){ + LOG.error("Keystore file '{}' does not exists!", keystoreFilePath.getAbsolutePath()); + return; + } + + smpKeyStorePasswordDecrypted = SecurityUtils.decrypt(file,smpKeyStorePasswordEncrypted); // Load the KeyStore and get the signing key and certificate. try (InputStream keystoreInputStream = new FileInputStream(keystoreFilePath)) { KeyStore keyStore = KeyStore.getInstance("JKS"); - keyStore.load(keystoreInputStream, smpKeyStorePasswordDecripted.toCharArray()); + keyStore.load(keystoreInputStream, smpKeyStorePasswordDecrypted.toCharArray()); for (String alias : list(keyStore.aliases())) { @@ -88,7 +94,7 @@ public class UIKeystoreService { } private void loadKeyAndCert(KeyStore keyStore, String alias) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { - Key key = keyStore.getKey(alias, smpKeyStorePasswordDecripted.toCharArray()); + Key key = keyStore.getKey(alias, smpKeyStorePasswordDecrypted.toCharArray()); Certificate certificate = keyStore.getCertificate(alias); if (key == null || certificate == null || !(certificate instanceof X509Certificate)) { throw new IllegalStateException("Wrong entry type found in keystore, only certificates with keypair are accepted, entry alias: " + alias); diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/utils/SecurityUtilsTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/utils/SecurityUtilsTest.java index 9394946f84bd0b6b4862f9fac9dd47055d19f915..c46b24c4affe88b6eba87629d4cccc167756589c 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/utils/SecurityUtilsTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/utils/SecurityUtilsTest.java @@ -2,7 +2,7 @@ package eu.europa.ec.edelivery.smp.utils; import org.junit.Assert; import org.junit.Test; -import org.springframework.util.StringUtils; + import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; @@ -16,10 +16,6 @@ import static org.junit.Assert.*; public class SecurityUtilsTest { - - @Test - public void mergeKeystores() { - } @Test public void generatePrivateSymmetricKey() throws IOException { @@ -41,7 +37,7 @@ public class SecurityUtilsTest { } @Test - public void encrypt() { + public void encrypt() throws IOException { // given File f = generateRandomPrivateKey(); String password = "TEST11002password1@!."+System.currentTimeMillis(); @@ -53,9 +49,9 @@ public class SecurityUtilsTest { assertNotEquals(password, encPassword); } @Test - public void encryptt() { + public void encryptWithSetupKey() { // given - File f = new File("/cef/code/smp/smp-server-library/src/test/resources/keystores/encryptionKey.key"); + File f = new File("src/test/resources/keystores/encryptionKey.key"); String password = "test123"; // when @@ -67,7 +63,7 @@ public class SecurityUtilsTest { @Test - public void decrypt() { + public void decrypt() throws IOException { // given File f = generateRandomPrivateKey(); String password = "TEST11002password1@!." + System.currentTimeMillis(); @@ -81,11 +77,11 @@ public class SecurityUtilsTest { } - private File generateRandomPrivateKey(){ - String tempPrivateKey = "enckey_"+ System.currentTimeMillis() + ".private"; - Path resourceDirectory = Paths.get("target", tempPrivateKey); - File resource = resourceDirectory.toFile(); - SecurityUtils.generatePrivateSymmetricKey(resourceDirectory.toFile()); + private File generateRandomPrivateKey() throws IOException{ + File resource = File.createTempFile( "test-key", ".key"); + resource.deleteOnExit(); + + SecurityUtils.generatePrivateSymmetricKey(resource); return resource; } diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/PropertiesConfig.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/PropertiesConfig.java index 42ec4440a7c325590b3e8e31cb3b7b86c9d0b9de..da33c3b09b7174e56cf34e613a26ace4c8b87b47 100644 --- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/PropertiesConfig.java +++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/PropertiesConfig.java @@ -44,6 +44,11 @@ import static eu.europa.ec.edelivery.smp.utils.SecurityUtils.*; /** * Created by Flavio Santos + * Class read properties from configuration file if exists. Than it use datasource (default by JNDI + * if not defined in property file jdbc/smpDatasource) to read application properties. Because this class is + * invoked before datasource is initialiyzed by default - it creates it's own database connection. + * Also it uses hibernate to handle dates for Configuration table. + * */ @Configuration @ComponentScan(basePackages = { @@ -158,6 +163,8 @@ public class PropertiesConfig { storeDBEntry(em, SMPPropertyEnum.CONFIGURATION_DIR, settingsFolder.getPath()); initProperties.setProperty(SMPPropertyEnum.CONFIGURATION_DIR.getProperty(), settingsFolder.getPath()); String newKeyPassword = RandomStringUtils.random(8, true, true); + storeDBEntry(em, SMPPropertyEnum.KEYSTORE_PASSWORD_DECRYPTED, newKeyPassword); + // store encryption filename File fEncryption = new File(settingsFolder, SMPPropertyEnum.ENCRYPTION_FILENAME.getDefValue()); diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/config/PropertiesTestConfig.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/config/PropertiesTestConfig.java index 0d8bab165a6cd1d9a488fff23b53fb5e25e3798f..fe048fba8099d6d14e3c1b9107dbb23686849a2b 100644 --- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/config/PropertiesTestConfig.java +++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/config/PropertiesTestConfig.java @@ -34,7 +34,7 @@ public class PropertiesTestConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { Path resourceDirectory = Paths.get("src", "test", "resources", "keystores"); - String path = resourceDirectory.toString(); + String path = resourceDirectory.toFile().getAbsolutePath(); PropertySourcesPlaceholderConfigurer propertiesConfig = new PropertySourcesPlaceholderConfigurer();