Search in sources :

Example 1 with FilesystemKeyPair

use of com.quorum.tessera.config.keypairs.FilesystemKeyPair in project tessera by ConsenSys.

the class FileKeyGeneratorTest method generateFromKeyDataLockedPrivateKey.

@Test
public void generateFromKeyDataLockedPrivateKey() throws IOException {
    when(passwordReader.requestUserPassword()).thenReturn("PASSWORD".toCharArray());
    final Path tempFolder = Files.createTempDirectory(UUID.randomUUID().toString());
    final String keyFilesName = tempFolder.resolve(UUID.randomUUID().toString()).toString();
    doReturn(keyPair).when(encryptor).generateNewKeys();
    final ArgonOptions argonOptions = new ArgonOptions("id", 1, 1, 1);
    final PrivateKeyData encryptedPrivateKey = new PrivateKeyData(null, null, null, null, argonOptions);
    doReturn(encryptedPrivateKey).when(keyEncryptor).encryptPrivateKey(any(PrivateKey.class), any(), eq(null));
    final PrivateKeyData encryptedKey = new PrivateKeyData(null, "snonce", "salt", "sbox", argonOptions);
    doReturn(encryptedKey).when(keyEncryptor).encryptPrivateKey(any(PrivateKey.class), any(), eq(null));
    final FilesystemKeyPair generated = generator.generate(keyFilesName, null, null);
    final KeyDataConfig pkd = generated.getInlineKeypair().getPrivateKeyConfig();
    assertThat(generated.getPublicKey()).isEqualTo("cHVibGljS2V5");
    assertThat(pkd.getSbox()).isEqualTo("sbox");
    assertThat(pkd.getSnonce()).isEqualTo("snonce");
    assertThat(pkd.getAsalt()).isEqualTo("salt");
    assertThat(pkd.getType()).isEqualTo(PrivateKeyType.LOCKED);
    verify(keyEncryptor).encryptPrivateKey(any(PrivateKey.class), any(), eq(null));
    verify(encryptor).generateNewKeys();
}
Also used : Path(java.nio.file.Path) KeyDataConfig(com.quorum.tessera.config.KeyDataConfig) ArgonOptions(com.quorum.tessera.config.ArgonOptions) PrivateKey(com.quorum.tessera.encryption.PrivateKey) PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) FilesystemKeyPair(com.quorum.tessera.config.keypairs.FilesystemKeyPair) Test(org.junit.Test)

Example 2 with FilesystemKeyPair

use of com.quorum.tessera.config.keypairs.FilesystemKeyPair in project tessera by ConsenSys.

the class FileKeyGeneratorTest method generateFromKeyDataUnlockedPrivateKey.

@Test
public void generateFromKeyDataUnlockedPrivateKey() throws IOException {
    doReturn(keyPair).when(encryptor).generateNewKeys();
    String filename = UUID.randomUUID().toString();
    final Path tmpDir = Files.createTempDirectory("keygen").toAbsolutePath().resolve(filename);
    final FilesystemKeyPair generated = generator.generate(tmpDir.toString(), null, null);
    assertThat(generated).isInstanceOf(FilesystemKeyPair.class);
    assertThat(generated.getPublicKey()).isEqualTo("cHVibGljS2V5");
    assertThat(generated.getPrivateKey()).isEqualTo("cHJpdmF0ZUtleQ==");
    assertThat(generated.getInlineKeypair().getPrivateKeyConfig().getType()).isEqualTo(UNLOCKED);
    verify(encryptor).generateNewKeys();
}
Also used : Path(java.nio.file.Path) FilesystemKeyPair(com.quorum.tessera.config.keypairs.FilesystemKeyPair) Test(org.junit.Test)

Example 3 with FilesystemKeyPair

use of com.quorum.tessera.config.keypairs.FilesystemKeyPair in project tessera by ConsenSys.

the class PicoCliDelegateTest method keygenThenExit.

@Test
public void keygenThenExit() throws Exception {
    FilesystemKeyPair keypair = mock(FilesystemKeyPair.class);
    when(keyGenerator.generate(anyString(), eq(null), eq(null))).thenReturn(keypair);
    final CliResult result = cliDelegate.execute("-keygen", "--encryptor.type", "NACL");
    assertThat(result).isNotNull();
    assertThat(result.isSuppressStartup()).isTrue();
    verify(keyGenerator).generate(anyString(), eq(null), eq(null));
}
Also used : FilesystemKeyPair(com.quorum.tessera.config.keypairs.FilesystemKeyPair) CliResult(com.quorum.tessera.cli.CliResult) Test(org.junit.Test)

Example 4 with FilesystemKeyPair

use of com.quorum.tessera.config.keypairs.FilesystemKeyPair in project tessera by ConsenSys.

the class PicoCliDelegateTest method suppressStartupForKeygenOptionWithFileOutputOptions.

@Test
public void suppressStartupForKeygenOptionWithFileOutputOptions() throws Exception {
    Path publicKeyPath = Files.createTempFile(UUID.randomUUID().toString(), "");
    Path privateKeyPath = Files.createTempFile(UUID.randomUUID().toString(), "");
    Files.write(privateKeyPath, Arrays.asList("SOMEDATA"));
    Files.write(publicKeyPath, Arrays.asList("SOMEDATA"));
    FilesystemKeyPair keypair = new FilesystemKeyPair(publicKeyPath, privateKeyPath, null);
    when(keyGenerator.generate(anyString(), eq(null), eq(null))).thenReturn(keypair);
    final Path configFile = Paths.get(getClass().getResource("/sample-config.json").toURI());
    final Path configOutputPath = configFile.resolveSibling(UUID.randomUUID().toString() + ".json");
    final CliResult cliResult = cliDelegate.execute("-keygen", "-configfile", configFile.toString(), "-output", configOutputPath.toString());
    assertThat(cliResult.isSuppressStartup()).isTrue();
}
Also used : Path(java.nio.file.Path) FilesystemKeyPair(com.quorum.tessera.config.keypairs.FilesystemKeyPair) CliResult(com.quorum.tessera.cli.CliResult) Test(org.junit.Test)

Example 5 with FilesystemKeyPair

use of com.quorum.tessera.config.keypairs.FilesystemKeyPair 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

FilesystemKeyPair (com.quorum.tessera.config.keypairs.FilesystemKeyPair)11 Test (org.junit.Test)10 Path (java.nio.file.Path)8 CliResult (com.quorum.tessera.cli.CliResult)6 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)3 HashMap (java.util.HashMap)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 CliException (com.quorum.tessera.cli.CliException)2 KeyDataConfig (com.quorum.tessera.config.KeyDataConfig)2 PrivateKeyData (com.quorum.tessera.config.PrivateKeyData)2 ConstraintViolationException (jakarta.validation.ConstraintViolationException)2 UncheckedIOException (java.io.UncheckedIOException)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 ArgonOptions (com.quorum.tessera.config.ArgonOptions)1 KeyData (com.quorum.tessera.config.KeyData)1 KeyPair (com.quorum.tessera.encryption.KeyPair)1 PrivateKey (com.quorum.tessera.encryption.PrivateKey)1