Search in sources :

Example 11 with PrivateKeyData

use of com.quorum.tessera.config.PrivateKeyData in project tessera by ConsenSys.

the class InlineKeypairTest method updatingPasswordsAttemptsToDecryptAgain.

@Test
public void updatingPasswordsAttemptsToDecryptAgain() {
    PrivateKeyData privateKeyData = mock(PrivateKeyData.class);
    final KeyDataConfig privKeyDataConfig = mock(KeyDataConfig.class);
    when(privKeyDataConfig.getPrivateKeyData()).thenReturn(privateKeyData);
    when(privKeyDataConfig.getType()).thenReturn(PrivateKeyType.LOCKED);
    when(keyEncryptor.decryptPrivateKey(privateKeyData, "wrong-password".toCharArray())).thenThrow(new EncryptorException("WHAT YOU TALKING ABOUT WILLIS"));
    final InlineKeypair inlineKeypair = new InlineKeypair("public", privKeyDataConfig, keyEncryptor);
    inlineKeypair.withPassword("wrong-password".toCharArray());
    String result = inlineKeypair.getPrivateKey();
    assertThat(result).isEqualTo("NACL_FAILURE");
    // change password and attempt again
    inlineKeypair.withPassword("testpassword".toCharArray());
    PrivateKey privateKey = mock(PrivateKey.class);
    when(privateKey.encodeToBase64()).thenReturn("SUCCESS");
    when(keyEncryptor.decryptPrivateKey(privateKeyData, "testpassword".toCharArray())).thenReturn(privateKey);
    assertThat(inlineKeypair.getPrivateKey()).isEqualTo("SUCCESS");
    verify(keyEncryptor).decryptPrivateKey(privateKeyData, "wrong-password".toCharArray());
    verify(keyEncryptor).decryptPrivateKey(privateKeyData, "testpassword".toCharArray());
}
Also used : KeyDataConfig(com.quorum.tessera.config.KeyDataConfig) PrivateKey(com.quorum.tessera.encryption.PrivateKey) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) EncryptorException(com.quorum.tessera.encryption.EncryptorException) Test(org.junit.Test)

Example 12 with PrivateKeyData

use of com.quorum.tessera.config.PrivateKeyData in project tessera by ConsenSys.

the class InlineKeypairTest method incorrectPasswordGetsCorrectFailureToken.

@Test
public void incorrectPasswordGetsCorrectFailureToken() {
    PrivateKeyData privateKeyData = mock(PrivateKeyData.class);
    final KeyDataConfig privKeyDataConfig = mock(KeyDataConfig.class);
    when(privKeyDataConfig.getPrivateKeyData()).thenReturn(privateKeyData);
    when(privKeyDataConfig.getType()).thenReturn(PrivateKeyType.LOCKED);
    when(keyEncryptor.decryptPrivateKey(privateKeyData, "wrong-password".toCharArray())).thenThrow(new EncryptorException("WHAT YOU TALKING ABOUT WILLIS"));
    final InlineKeypair inlineKeypair = new InlineKeypair("public", privKeyDataConfig, keyEncryptor);
    inlineKeypair.withPassword("wrong-password".toCharArray());
    String result = inlineKeypair.getPrivateKey();
    assertThat(String.valueOf(inlineKeypair.getPassword())).isEqualTo("wrong-password");
    assertThat(result).isEqualTo("NACL_FAILURE");
}
Also used : KeyDataConfig(com.quorum.tessera.config.KeyDataConfig) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) EncryptorException(com.quorum.tessera.encryption.EncryptorException) Test(org.junit.Test)

Example 13 with PrivateKeyData

use of com.quorum.tessera.config.PrivateKeyData in project tessera by ConsenSys.

the class KeyEncryptorIT method encryptAndDecryptOnKeyIsSuccessful.

@Test
public void encryptAndDecryptOnKeyIsSuccessful() {
    ArgonOptions argonOptions = new ArgonOptions("i", 10, 1048576, 4);
    final PrivateKeyData privateKeyData = keyEncryptor.encryptPrivateKey(privateKey, password, argonOptions);
    final PrivateKey decryptedKey = keyEncryptor.decryptPrivateKey(privateKeyData, password);
    assertThat(decryptedKey).isEqualTo(privateKey);
}
Also used : ArgonOptions(com.quorum.tessera.config.ArgonOptions) PrivateKey(com.quorum.tessera.encryption.PrivateKey) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) Test(org.junit.Test)

Example 14 with PrivateKeyData

use of com.quorum.tessera.config.PrivateKeyData in project tessera by ConsenSys.

the class KeyEncryptorTest method encryptingKeyReturnsCorrectJson.

@Test
public void encryptingKeyReturnsCorrectJson() {
    final PrivateKey key = PrivateKey.from(new byte[] { 1, 2, 3, 4, 5 });
    final char[] password = "pass".toCharArray();
    final ArgonResult result = new ArgonResult(new com.quorum.tessera.argon2.ArgonOptions("i", 1, 1, 1), new byte[] {}, new byte[] {});
    doReturn(result).when(argon2).hash(eq(password), any(byte[].class));
    doReturn(new Nonce(new byte[] {})).when(encryptor).randomNonce();
    doReturn(new byte[] {}).when(encryptor).sealAfterPrecomputation(any(byte[].class), any(Nonce.class), any(SharedKey.class));
    final PrivateKeyData privateKey = this.keyEncryptor.encryptPrivateKey(key, password, null);
    final ArgonOptions aopts = privateKey.getArgonOptions();
    assertThat(privateKey.getSbox()).isNotNull();
    assertThat(privateKey.getAsalt()).isNotNull();
    assertThat(privateKey.getSnonce()).isNotNull();
    assertThat(aopts).isNotNull();
    assertThat(aopts.getMemory()).isNotNull();
    assertThat(aopts.getParallelism()).isNotNull();
    assertThat(aopts.getIterations()).isNotNull();
    assertThat(aopts.getAlgorithm()).isNotNull();
    verify(argon2).hash(eq(password), any(byte[].class));
    verify(encryptor).randomNonce();
    verify(encryptor).sealAfterPrecomputation(any(byte[].class), any(Nonce.class), any(SharedKey.class));
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) ArgonOptions(com.quorum.tessera.config.ArgonOptions) PrivateKey(com.quorum.tessera.encryption.PrivateKey) ArgonResult(com.quorum.tessera.argon2.ArgonResult) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) SharedKey(com.quorum.tessera.encryption.SharedKey) Test(org.junit.Test)

Example 15 with PrivateKeyData

use of com.quorum.tessera.config.PrivateKeyData in project tessera by ConsenSys.

the class KeyEncryptorTest method correntJsonGivesDecryptedKey.

@Test
public void correntJsonGivesDecryptedKey() {
    final char[] password = "pass".toCharArray();
    final ArgonOptions argonOptions = new ArgonOptions("i", 1, 1, 1);
    final PrivateKeyData lockedPrivateKey = new PrivateKeyData("", "", "uZAfjmMwEepP8kzZCnmH6g==", "", argonOptions);
    doReturn(new byte[] { 1, 2, 3 }).when(this.encryptor).openAfterPrecomputation(any(byte[].class), any(Nonce.class), any(SharedKey.class));
    doReturn(new ArgonResult(null, new byte[] {}, new byte[] { 4, 5, 6 })).when(this.argon2).hash(any(com.quorum.tessera.argon2.ArgonOptions.class), eq(password), any(byte[].class));
    final PrivateKey key = this.keyEncryptor.decryptPrivateKey(lockedPrivateKey, password);
    assertThat(key.getKeyBytes()).isEqualTo(new byte[] { 1, 2, 3 });
    verify(this.encryptor).openAfterPrecomputation(any(byte[].class), any(Nonce.class), any(SharedKey.class));
    verify(this.argon2).hash(any(com.quorum.tessera.argon2.ArgonOptions.class), eq(password), any(byte[].class));
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) ArgonOptions(com.quorum.tessera.config.ArgonOptions) ArgonResult(com.quorum.tessera.argon2.ArgonResult) PrivateKey(com.quorum.tessera.encryption.PrivateKey) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) SharedKey(com.quorum.tessera.encryption.SharedKey) Test(org.junit.Test)

Aggregations

PrivateKeyData (com.quorum.tessera.config.PrivateKeyData)15 Test (org.junit.Test)12 KeyDataConfig (com.quorum.tessera.config.KeyDataConfig)8 PrivateKey (com.quorum.tessera.encryption.PrivateKey)8 ArgonOptions (com.quorum.tessera.config.ArgonOptions)7 ArgonResult (com.quorum.tessera.argon2.ArgonResult)4 Nonce (com.quorum.tessera.encryption.Nonce)4 Path (java.nio.file.Path)4 EncryptorException (com.quorum.tessera.encryption.EncryptorException)3 SharedKey (com.quorum.tessera.encryption.SharedKey)3 FilesystemKeyPair (com.quorum.tessera.config.keypairs.FilesystemKeyPair)2 KeyData (com.quorum.tessera.config.KeyData)1 ValidBase64 (com.quorum.tessera.config.constraints.ValidBase64)1 KeyPair (com.quorum.tessera.encryption.KeyPair)1 NotNull (jakarta.validation.constraints.NotNull)1 Pattern (jakarta.validation.constraints.Pattern)1 Size (jakarta.validation.constraints.Size)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1