@Test public void lockedKeySucceedsWithPasswordsMatching() { PrivateKeyData privateKeyData = new PrivateKeyData( null, "dwixVoY+pOI2FMuu4k0jLqN/naQiTzWe", "JoPVq9G6NdOb+Ugv+HnUeA==", "6Jd/MXn29fk6jcrFYGPb75l7sDJae06I3Y1Op+bZSZqlYXsMpa/8lLE29H0sX3yw", new ArgonOptions("id", 1, 1024, 1)); final KeyDataConfig kdc = new KeyDataConfig( new PrivateKeyData( null, "dwixVoY+pOI2FMuu4k0jLqN/naQiTzWe", "JoPVq9G6NdOb+Ugv+HnUeA==", "6Jd/MXn29fk6jcrFYGPb75l7sDJae06I3Y1Op+bZSZqlYXsMpa/8lLE29H0sX3yw", new ArgonOptions("id", 1, 1024, 1)), PrivateKeyType.LOCKED); PrivateKey privateKey = mock(PrivateKey.class); when(privateKey.getKeyBytes()).thenReturn("SUCCESS".getBytes()); when(keyEncryptor.decryptPrivateKey(privateKeyData, "testpassword".toCharArray())).thenReturn(privateKey); final PrivateKey result = command.getExistingKey(kdc, singletonList("testpassword".toCharArray())); assertThat(result.getKeyBytes()).isEqualTo("SUCCESS".getBytes()); verify(keyEncryptor).decryptPrivateKey(privateKeyData, "testpassword".toCharArray()); }
PrivateKey getExistingKey(final KeyDataConfig kdc, final List<char[]> passwords) { if (kdc.getType() == PrivateKeyType.UNLOCKED) { byte[] privateKeyData = Base64.getDecoder().decode(kdc.getValue().getBytes(UTF_8)); return PrivateKey.from(privateKeyData); } else { for (final char[] pass : passwords) { try { return PrivateKey.from(keyEncryptor.decryptPrivateKey(kdc.getPrivateKeyData(), pass).getKeyBytes()); } catch (final Exception e) { LOGGER.debug("Password failed to decrypt. Trying next if available."); } } throw new IllegalArgumentException("Locked key but no valid password given"); } }
KeyUpdateCommand implements Callable<CliResult> { PrivateKey getExistingKey(final KeyDataConfig kdc, final List<char[]> passwords) { if (kdc.getType() == PrivateKeyType.UNLOCKED) { byte[] privateKeyData = Base64.getDecoder().decode(kdc.getValue().getBytes(UTF_8)); return PrivateKey.from(privateKeyData); } else { for (final char[] pass : passwords) { try { return PrivateKey.from(keyEncryptor.decryptPrivateKey(kdc.getPrivateKeyData(), pass).getKeyBytes()); } catch (final Exception e) { LOGGER.debug("Password failed to decrypt. Trying next if available."); } } throw new IllegalArgumentException("Locked key but no valid password given"); } } }
KeyUpdateCommand implements Callable<CliResult> { PrivateKey getExistingKey(final KeyDataConfig kdc, final List<char[]> passwords) { if (kdc.getType() == PrivateKeyType.UNLOCKED) { byte[] privateKeyData = Base64.getDecoder().decode(kdc.getValue().getBytes(UTF_8)); return PrivateKey.from(privateKeyData); } else { for (final char[] pass : passwords) { try { return PrivateKey.from(keyEncryptor.decryptPrivateKey(kdc.getPrivateKeyData(), pass).getKeyBytes()); } catch (final Exception e) { LOGGER.debug("Password failed to decrypt. Trying next if available."); } } throw new IllegalArgumentException("Locked key but no valid password given"); } } KeyUpdateCommand(KeyEncryptorFactory keyEncryptorFactory, PasswordReader passwordReader); }
KeyUpdateCommand implements Callable<CliResult> { PrivateKey getExistingKey(final KeyDataConfig kdc, final List<char[]> passwords) { if (kdc.getType() == PrivateKeyType.UNLOCKED) { byte[] privateKeyData = Base64.getDecoder().decode(kdc.getValue().getBytes(UTF_8)); return PrivateKey.from(privateKeyData); } else { for (final char[] pass : passwords) { try { return PrivateKey.from(keyEncryptor.decryptPrivateKey(kdc.getPrivateKeyData(), pass).getKeyBytes()); } catch (final Exception e) { LOGGER.debug("Password failed to decrypt. Trying next if available."); } } throw new IllegalArgumentException("Locked key but no valid password given"); } } KeyUpdateCommand(KeyEncryptorFactory keyEncryptorFactory, PasswordReader passwordReader); @Override CliResult call(); CliResult execute(); }
KeyUpdateCommand implements Callable<CliResult> { PrivateKey getExistingKey(final KeyDataConfig kdc, final List<char[]> passwords) { if (kdc.getType() == PrivateKeyType.UNLOCKED) { byte[] privateKeyData = Base64.getDecoder().decode(kdc.getValue().getBytes(UTF_8)); return PrivateKey.from(privateKeyData); } else { for (final char[] pass : passwords) { try { return PrivateKey.from(keyEncryptor.decryptPrivateKey(kdc.getPrivateKeyData(), pass).getKeyBytes()); } catch (final Exception e) { LOGGER.debug("Password failed to decrypt. Trying next if available."); } } throw new IllegalArgumentException("Locked key but no valid password given"); } } KeyUpdateCommand(KeyEncryptorFactory keyEncryptorFactory, PasswordReader passwordReader); @Override CliResult call(); CliResult execute(); @CommandLine.Option(names = "--keys.keyData.privateKeyPath", required = true)
    public Path privateKeyPath; @CommandLine.Option(names = "--keys.keyData.config.data.aopts.algorithm", defaultValue = "i")
    public String algorithm; @CommandLine.Option(names = "--keys.keyData.config.data.aopts.iterations", defaultValue = "10")
    public Integer iterations; @CommandLine.Option(names = "--keys.keyData.config.data.aopts.memory", defaultValue = "1048576")
    public Integer memory; @CommandLine.Option(names = "--keys.keyData.config.data.aopts.parallelism", defaultValue = "4")
    public Integer parallelism; @CommandLine.Option(names = {"--keys.passwords"})
    public String password; @CommandLine.Option(names = {"--keys.passwordFile"})
    public Path passwordFile; @CommandLine.Option(
            names = {"--configfile", "-configfile"},
            description = "Path to node configuration file")
    public Config config; @CommandLine.Mixin public EncryptorOptions encryptorOptions; @CommandLine.Mixin public DebugOptions debugOptions; }