Skip to content

Commit

Permalink
Added two new tests for KeyStoreService and one test for CmsEncryptio…
Browse files Browse the repository at this point in the history
…nServiceImpl
  • Loading branch information
Thendo20 committed Jul 1, 2024
1 parent 83cac9a commit 19b3408
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 19b3408

Please sign in to comment.