@Test public void providingPathThatExistsThrowsError() throws IOException { final Path tempFolder = Files.createTempDirectory(UUID.randomUUID().toString()); final String keyFilesName = tempFolder.resolve("key").toString(); tempFolder.toFile().setWritable(false); doReturn(keyPair).when(encryptor).generateNewKeys(); doReturn(new PrivateKeyData("", "", "", "", new ArgonOptions("", 1, 1, 1))) .when(keyEncryptor) .encryptPrivateKey(any(PrivateKey.class), any(), eq(null)); final Throwable throwable = catchThrowable(() -> generator.generate(keyFilesName, null, null)); assertThat(throwable).isInstanceOf(UncheckedIOException.class); assertThat(Files.exists(tempFolder.resolve("key.pub"))).isFalse(); assertThat(Files.exists(tempFolder.resolve("key.key"))).isFalse(); verify(encryptor).generateNewKeys(); }
@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; }
FileKeyGenerator implements KeyGenerator { @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; } }
FileKeyGenerator implements KeyGenerator { @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; } FileKeyGenerator(
            final Encryptor encryptor, final KeyEncryptor keyEncryptor, final PasswordReader passwordReader); }
FileKeyGenerator implements KeyGenerator { @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; } FileKeyGenerator(
            final Encryptor encryptor, final KeyEncryptor keyEncryptor, final PasswordReader passwordReader); @Override FilesystemKeyPair generate(
            final String filename, final ArgonOptions encryptionOptions, final KeyVaultOptions keyVaultOptions); }
FileKeyGenerator implements KeyGenerator { @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; } FileKeyGenerator(
            final Encryptor encryptor, final KeyEncryptor keyEncryptor, final PasswordReader passwordReader); @Override FilesystemKeyPair generate(
            final String filename, final ArgonOptions encryptionOptions, final KeyVaultOptions keyVaultOptions);  }