Search in sources :

Example 16 with KeyData

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

the class KeyDataUtilTest method marshalDirectKeyPair.

@Test
public void marshalDirectKeyPair() {
    DirectKeyPair directKeyPair = new DirectKeyPair("PUBLIC_KEY", "PRIVATE_KEY");
    KeyData result = KeyDataUtil.marshal(directKeyPair);
    assertThat(result.getPublicKey()).isEqualTo("PUBLIC_KEY");
    assertThat(result.getPrivateKey()).isEqualTo("PRIVATE_KEY");
}
Also used : KeyData(com.quorum.tessera.config.KeyData) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) Test(org.junit.Test)

Example 17 with KeyData

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

the class KeyDataUtilTest method isLocked.

@Test
public void isLocked() {
    KeyData keyData = new KeyData();
    keyData.setPublicKey("PUBLIC_KEY");
    KeyDataConfig keyDataConfig = new KeyDataConfig(mock(PrivateKeyData.class), PrivateKeyType.LOCKED);
    keyData.setConfig(keyDataConfig);
    assertThat(KeyDataUtil.isLocked(keyData)).isTrue();
    keyData.setConfig(null);
    assertThat(KeyDataUtil.isLocked(keyData)).isFalse();
    keyData.setConfig(new KeyDataConfig());
    assertThat(KeyDataUtil.isLocked(keyData)).isFalse();
}
Also used : KeyDataConfig(com.quorum.tessera.config.KeyDataConfig) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) KeyData(com.quorum.tessera.config.KeyData) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) Test(org.junit.Test)

Example 18 with KeyData

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

the class KeyDataUtilTest method marshalInlineKeypair.

@Test
public void marshalInlineKeypair() {
    KeyEncryptor keyEncryptor = mock(KeyEncryptor.class);
    KeyDataConfig keyDataConfig = mock(KeyDataConfig.class);
    InlineKeypair keyPair = new InlineKeypair("PUBLIC_KEY", keyDataConfig, keyEncryptor);
    KeyData result = KeyDataUtil.marshal(keyPair);
    assertThat(result.getPublicKey()).isEqualTo("PUBLIC_KEY");
    assertThat(result.getConfig()).isSameAs(keyDataConfig);
}
Also used : KeyDataConfig(com.quorum.tessera.config.KeyDataConfig) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) KeyData(com.quorum.tessera.config.KeyData) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) Test(org.junit.Test)

Example 19 with KeyData

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

the class KeyDataMarshallerTest method defaultMarshal.

@Test
public void defaultMarshal() {
    KeyDataMarshaller k = new DefaultKeyDataMarshaller();
    DirectKeyPair configKeyPair = new DirectKeyPair("PUBLIC", "PRIVATE");
    KeyData keyData = k.marshal(configKeyPair);
    assertThat(keyData).isNotNull();
    assertThat(keyData.getPublicKey()).isEqualTo("PUBLIC");
    assertThat(keyData.getPrivateKey()).isEqualTo("PRIVATE");
}
Also used : DirectKeyPair(com.quorum.tessera.config.keypairs.DirectKeyPair) KeyData(com.quorum.tessera.config.KeyData) Test(org.junit.Test)

Example 20 with KeyData

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

the class FileKeyGenerator method generate.

@Override
public FilesystemKeyPair generate(final String filename, final ArgonOptions encryptionOptions, final KeyVaultOptions keyVaultOptions) {
    final char[] password = this.passwordReader.requestUserPassword();
    final KeyPair generated = this.encryptor.generateNewKeys();
    final String publicKeyBase64 = Base64.getEncoder().encodeToString(generated.getPublicKey().getKeyBytes());
    final KeyData finalKeys = new KeyData();
    final KeyDataConfig keyDataConfig;
    if (password.length > 0) {
        final PrivateKeyData encryptedPrivateKey = this.keyEncryptor.encryptPrivateKey(generated.getPrivateKey(), password, encryptionOptions);
        keyDataConfig = new KeyDataConfig(new PrivateKeyData(null, encryptedPrivateKey.getSnonce(), encryptedPrivateKey.getAsalt(), encryptedPrivateKey.getSbox(), encryptedPrivateKey.getArgonOptions()), LOCKED);
        LOGGER.info("Newly generated private key has been encrypted");
    } else {
        String keyData = Base64.getEncoder().encodeToString(generated.getPrivateKey().getKeyBytes());
        keyDataConfig = new KeyDataConfig(new PrivateKeyData(keyData, null, null, null, null), UNLOCKED);
    }
    finalKeys.setConfig(keyDataConfig);
    finalKeys.setPrivateKey(generated.getPrivateKey().encodeToBase64());
    finalKeys.setPublicKey(publicKeyBase64);
    final String privateKeyJson = JaxbUtil.marshalToString(finalKeys.getConfig());
    final Path resolvedPath = Paths.get(filename).toAbsolutePath();
    final Path parentPath;
    if (EMPTY_FILENAME.equals(filename)) {
        parentPath = resolvedPath;
    } else {
        parentPath = resolvedPath.getParent();
    }
    final Path publicKeyPath = parentPath.resolve(filename + ".pub");
    final Path privateKeyPath = parentPath.resolve(filename + ".key");
    IOCallback.execute(() -> Files.write(publicKeyPath, publicKeyBase64.getBytes(UTF_8), CREATE_NEW));
    IOCallback.execute(() -> Files.write(privateKeyPath, privateKeyJson.getBytes(UTF_8), CREATE_NEW));
    LOGGER.info("Saved public key to {}", publicKeyPath.toAbsolutePath().toString());
    LOGGER.info("Saved private key to {}", privateKeyPath.toAbsolutePath().toString());
    final FilesystemKeyPair keyPair = new FilesystemKeyPair(publicKeyPath, privateKeyPath, keyEncryptor);
    keyPair.withPassword(password);
    return keyPair;
}
Also used : Path(java.nio.file.Path) KeyDataConfig(com.quorum.tessera.config.KeyDataConfig) KeyPair(com.quorum.tessera.encryption.KeyPair) FilesystemKeyPair(com.quorum.tessera.config.keypairs.FilesystemKeyPair) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) FilesystemKeyPair(com.quorum.tessera.config.keypairs.FilesystemKeyPair) KeyData(com.quorum.tessera.config.KeyData) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData)

Aggregations

KeyData (com.quorum.tessera.config.KeyData)34 Test (org.junit.Test)32 PrivateKeyData (com.quorum.tessera.config.PrivateKeyData)31 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)10 KeyDataConfig (com.quorum.tessera.config.KeyDataConfig)7 Path (java.nio.file.Path)7 Config (com.quorum.tessera.config.Config)1 KeyConfiguration (com.quorum.tessera.config.KeyConfiguration)1 DirectKeyPair (com.quorum.tessera.config.keypairs.DirectKeyPair)1 FilesystemKeyPair (com.quorum.tessera.config.keypairs.FilesystemKeyPair)1 KeyPair (com.quorum.tessera.encryption.KeyPair)1 ArrayList (java.util.ArrayList)1