diff --git a/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/cmsencryption/CmsEncryptionServiceImplTest.java b/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/cmsencryption/CmsEncryptionServiceImplTest.java index 3c0168b08..490748a62 100644 --- a/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/cmsencryption/CmsEncryptionServiceImplTest.java +++ b/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/cmsencryption/CmsEncryptionServiceImplTest.java @@ -1,6 +1,11 @@ package de.adorsys.datasafe.encrypiton.impl.cmsencryption; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; import com.google.common.io.ByteStreams; +import com.google.common.io.Resources; import de.adorsys.datasafe.encrypiton.api.cmsencryption.CMSEncryptionService; import de.adorsys.datasafe.encrypiton.api.keystore.KeyStoreService; import de.adorsys.datasafe.encrypiton.api.types.encryption.CmsEncryptionConfig; @@ -28,17 +33,10 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.RandomAccessFile; +import java.io.*; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.security.Key; @@ -145,6 +143,48 @@ void cmsStreamEnvelopeEncryptAndDecryptTest() { assertThat(TEST_MESSAGE_CONTENT).isEqualTo(new String(actualResult)); } + @Test + @SneakyThrows + void cmsStreamEnvelopeEncryptAndDecryptTestCustom() { + ReadKeyPassword readKeyPassword = ReadKeyPasswordTestFactory.getForString("readkeypassword"); + ReadStorePassword readStorePassword = new ReadStorePassword("readstorepassword"); + + KeyStoreAuth keyStoreAuth = new KeyStoreAuth(readStorePassword, readKeyPassword); + KeyCreationConfig config = KeyCreationConfig.builder() + .signing(KeyCreationConfig.SigningKeyCreationCfg.builder().algo("RSA").size(2048).sigAlgo( "SHA256withRSA").curve("null").build()) + .encrypting(KeyCreationConfig.EncryptingKeyCreationCfg.builder().algo("RSA").size(2048).sigAlgo("SHA256withRSA").curve("null").build()) + .build(); + + KeyStore keyStore = keyStoreService.createKeyStore(keyStoreAuth, config); + KeyStoreAccess keyStoreAccess = new KeyStoreAccess(keyStore, keyStoreAuth); + + PublicKeyIDWithPublicKey publicKeyIDWithPublicKey = keyStoreService.getPublicKeys(keyStoreAccess).get(0); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + KeyStoreAccess keyStoreAccessSender = getKeyStoreAccess("Sender"); + + OutputStream encryptionStream = cmsEncryptionService.buildEncryptionOutputStream( + outputStream, + Collections.singleton(new PublicKeyIDWithPublicKey( + publicKeyIDWithPublicKey.getKeyID(), + publicKeyIDWithPublicKey.getPublicKey() + )), + getKeyPair(keyStoreAccessSender, "Sender") + ); + + encryptionStream.write(TEST_MESSAGE_CONTENT.getBytes()); + encryptionStream.close(); + + byte[] byteArray = outputStream.toByteArray(); + + ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray); + InputStream decryptionStream = cmsEncryptionService.buildDecryptionInputStream( + inputStream, keyIds -> getKeys(keyIds, keyStoreAccess) + ); + byte[] actualResult = toByteArray(decryptionStream); + + assertThat(TEST_MESSAGE_CONTENT).isEqualTo(new String(actualResult)); + } @Test @SneakyThrows diff --git a/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/keystore/KeyStoreServiceTest.java b/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/keystore/KeyStoreServiceTest.java index c003b1e54..e4718f208 100644 --- a/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/keystore/KeyStoreServiceTest.java +++ b/datasafe-encryption/datasafe-encryption-impl/src/test/java/de/adorsys/datasafe/encrypiton/impl/keystore/KeyStoreServiceTest.java @@ -69,7 +69,24 @@ void createKeyStoreEmptyConfig() throws Exception { // One additional secret key being generated for path encryption and one for private doc encryption. Assertions.assertEquals(4, list.size()); } + @Test + void updateKeyStoreReadKeyPassword() throws Exception { + KeyCreationConfig config = KeyCreationConfig.builder().signKeyNumber(0).encKeyNumber(1).build(); + KeyStore keyStore = keyStoreService.createKeyStore(keyStoreAuth, config); + KeyStoreAuth newKeystoreAuth = new KeyStoreAuth(new ReadStorePassword("newstorepass"), new ReadKeyPassword("newkeypass".toCharArray())); + KeyStore updatedKeyStore = keyStoreService.updateKeyStoreReadKeyPassword(keyStore, keyStoreAuth, newKeystoreAuth); + Assertions.assertEquals("newkeypass", newKeystoreAuth.getReadKeyPassword().getValue()); + } + @Test + void addPasswordBasedSecretKey() { + KeyStore keyStore = keyStoreService.createKeyStore(keyStoreAuth, KeyCreationConfig.builder().build()); + KeyStoreAccess keyStoreAccess = new KeyStoreAccess(keyStore, keyStoreAuth); + keyStoreService.addPasswordBasedSecretKey(keyStoreAccess, "alias", "secret".toCharArray()); + SecretKey secretKey = keyStoreService.getSecretKey(keyStoreAccess, new KeyID("alias")); + + Assertions.assertEquals("secret", new String(secretKey.getEncoded())); + } @Test void getPublicKeys() { KeyStore keyStore = keyStoreService.createKeyStore(keyStoreAuth, KeyCreationConfig.builder().build());