Search in sources :

Example 1 with Masterkey

use of org.cryptomator.cryptolib.api.Masterkey in project cryptomator by cryptomator.

the class RecoveryKeyFactory method resetPasswordWithRecoveryKey.

/**
 * Creates a completely new masterkey using a recovery key.
 *
 * @param vaultPath Path to the storage location of a vault
 * @param recoveryKey A recovery key for this vault
 * @param newPassword The new password used to encrypt the keys
 * @throws IOException If the masterkey file could not be written
 * @throws IllegalArgumentException If the recoveryKey is invalid
 * @apiNote This is a long-running operation and should be invoked in a background thread
 */
public void resetPasswordWithRecoveryKey(Path vaultPath, String recoveryKey, CharSequence newPassword) throws IOException, IllegalArgumentException {
    final byte[] rawKey = decodeRecoveryKey(recoveryKey);
    try (var masterkey = new Masterkey(rawKey)) {
        Path masterkeyPath = vaultPath.resolve(MASTERKEY_FILENAME);
        if (Files.exists(masterkeyPath)) {
            byte[] oldMasterkeyBytes = Files.readAllBytes(masterkeyPath);
            // TODO: deduplicate with ChangePasswordController:
            Path backupKeyPath = vaultPath.resolve(MASTERKEY_FILENAME + BackupHelper.generateFileIdSuffix(oldMasterkeyBytes) + MASTERKEY_BACKUP_SUFFIX);
            Files.move(masterkeyPath, backupKeyPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
        }
        masterkeyFileAccess.persist(masterkey, masterkeyPath, newPassword);
    } finally {
        Arrays.fill(rawKey, (byte) 0x00);
    }
}
Also used : Path(java.nio.file.Path) Masterkey(org.cryptomator.cryptolib.api.Masterkey)

Example 2 with Masterkey

use of org.cryptomator.cryptolib.api.Masterkey in project cryptomator by cryptomator.

the class CreateNewVaultPasswordController method initializeVault.

private void initializeVault(Path path) throws IOException {
    // 1. write masterkey:
    Path masterkeyFilePath = path.resolve(MASTERKEY_FILENAME);
    try (Masterkey masterkey = Masterkey.generate(csprng)) {
        masterkeyFileAccess.persist(masterkey, masterkeyFilePath, newPasswordSceneController.passwordField.getCharacters());
        // 2. initialize vault:
        try {
            MasterkeyLoader loader = ignored -> masterkey.copy();
            CryptoFileSystemProperties fsProps = CryptoFileSystemProperties.cryptoFileSystemProperties().withCipherCombo(CryptorProvider.Scheme.SIV_CTRMAC).withKeyLoader(loader).build();
            CryptoFileSystemProvider.initialize(path, fsProps, DEFAULT_KEY_ID);
            // 3. write vault-internal readme file:
            String vaultReadmeFileName = resourceBundle.getString("addvault.new.readme.accessLocation.fileName");
            try (// 
            FileSystem fs = CryptoFileSystemProvider.newFileSystem(path, fsProps);
                WritableByteChannel ch = Files.newByteChannel(fs.getPath("/", vaultReadmeFileName), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
                ch.write(US_ASCII.encode(readmeGenerator.createVaultAccessLocationReadmeRtf()));
            }
        } catch (CryptoException e) {
            throw new IOException("Failed initialize vault.", e);
        }
    }
    // 4. write vault-external readme file:
    String storagePathReadmeFileName = resourceBundle.getString("addvault.new.readme.storageLocation.fileName");
    try (WritableByteChannel ch = Files.newByteChannel(path.resolve(storagePathReadmeFileName), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
        ch.write(US_ASCII.encode(readmeGenerator.createVaultStorageLocationReadmeRtf()));
    }
    LOG.info("Created vault at {}", path);
}
Also used : Path(java.nio.file.Path) Scene(javafx.scene.Scene) CryptoFileSystemProperties(org.cryptomator.cryptofs.CryptoFileSystemProperties) VaultListManager(org.cryptomator.common.vaults.VaultListManager) LoggerFactory(org.slf4j.LoggerFactory) CryptoFileSystemProvider(org.cryptomator.cryptofs.CryptoFileSystemProvider) CryptoException(org.cryptomator.cryptolib.api.CryptoException) Bindings(javafx.beans.binding.Bindings) FxmlFile(org.cryptomator.ui.common.FxmlFile) NewPasswordController(org.cryptomator.ui.common.NewPasswordController) Inject(javax.inject.Inject) SecureRandom(java.security.SecureRandom) Masterkey(org.cryptomator.cryptolib.api.Masterkey) MasterkeyFileAccess(org.cryptomator.cryptolib.common.MasterkeyFileAccess) ResourceBundle(java.util.ResourceBundle) Vault(org.cryptomator.common.vaults.Vault) ErrorComponent(org.cryptomator.ui.common.ErrorComponent) Named(javax.inject.Named) URI(java.net.URI) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Tasks(org.cryptomator.ui.common.Tasks) ObjectProperty(javafx.beans.property.ObjectProperty) MASTERKEY_FILENAME(org.cryptomator.common.Constants.MASTERKEY_FILENAME) Logger(org.slf4j.Logger) Files(java.nio.file.Files) FxController(org.cryptomator.ui.common.FxController) ObjectBinding(javafx.beans.binding.ObjectBinding) StandardOpenOption(java.nio.file.StandardOpenOption) CryptorProvider(org.cryptomator.cryptolib.api.CryptorProvider) IOException(java.io.IOException) FileSystem(java.nio.file.FileSystem) Lazy(dagger.Lazy) FxmlScene(org.cryptomator.ui.common.FxmlScene) RecoveryKeyFactory(org.cryptomator.ui.recoverykey.RecoveryKeyFactory) FXML(javafx.fxml.FXML) UncheckedIOException(java.io.UncheckedIOException) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) MasterkeyLoader(org.cryptomator.cryptolib.api.MasterkeyLoader) BooleanProperty(javafx.beans.property.BooleanProperty) ToggleGroup(javafx.scene.control.ToggleGroup) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) MasterkeyFileLoadingStrategy(org.cryptomator.ui.keyloading.masterkeyfile.MasterkeyFileLoadingStrategy) Stage(javafx.stage.Stage) WritableByteChannel(java.nio.channels.WritableByteChannel) Toggle(javafx.scene.control.Toggle) StringProperty(javafx.beans.property.StringProperty) ContentDisplay(javafx.scene.control.ContentDisplay) Masterkey(org.cryptomator.cryptolib.api.Masterkey) FileSystem(java.nio.file.FileSystem) WritableByteChannel(java.nio.channels.WritableByteChannel) CryptoFileSystemProperties(org.cryptomator.cryptofs.CryptoFileSystemProperties) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) CryptoException(org.cryptomator.cryptolib.api.CryptoException) MasterkeyLoader(org.cryptomator.cryptolib.api.MasterkeyLoader)

Aggregations

Path (java.nio.file.Path)2 Masterkey (org.cryptomator.cryptolib.api.Masterkey)2 Lazy (dagger.Lazy)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 URI (java.net.URI)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 US_ASCII (java.nio.charset.StandardCharsets.US_ASCII)1 FileSystem (java.nio.file.FileSystem)1 Files (java.nio.file.Files)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 SecureRandom (java.security.SecureRandom)1 ResourceBundle (java.util.ResourceBundle)1 ExecutorService (java.util.concurrent.ExecutorService)1 Bindings (javafx.beans.binding.Bindings)1 ObjectBinding (javafx.beans.binding.ObjectBinding)1 BooleanProperty (javafx.beans.property.BooleanProperty)1 ObjectProperty (javafx.beans.property.ObjectProperty)1 SimpleBooleanProperty (javafx.beans.property.SimpleBooleanProperty)1 StringProperty (javafx.beans.property.StringProperty)1