Search in sources :

Example 1 with CryptoException

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

the class ChangePasswordController method finish.

@FXML
public void finish() {
    try {
        CharSequence oldPassphrase = oldPasswordField.getCharacters();
        CharSequence newPassphrase = newPasswordController.passwordField.getCharacters();
        Path masterkeyPath = vault.getPath().resolve(MASTERKEY_FILENAME);
        byte[] oldMasterkeyBytes = Files.readAllBytes(masterkeyPath);
        byte[] newMasterkeyBytes = masterkeyFileAccess.changePassphrase(oldMasterkeyBytes, oldPassphrase, newPassphrase);
        Path backupKeyPath = vault.getPath().resolve(MASTERKEY_FILENAME + BackupHelper.generateFileIdSuffix(oldMasterkeyBytes) + MASTERKEY_BACKUP_SUFFIX);
        Files.move(masterkeyPath, backupKeyPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
        Files.write(masterkeyPath, newMasterkeyBytes, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
        LOG.info("Successfully changed password for {}", vault.getDisplayName());
        updatePasswordInSystemkeychain();
        window.close();
    } catch (InvalidPassphraseException e) {
        Animations.createShakeWindowAnimation(window).play();
        oldPasswordField.selectAll();
        oldPasswordField.requestFocus();
    } catch (IOException | CryptoException e) {
        LOG.error("Password change failed. Unable to perform operation.", e);
        errorComponent.cause(e).window(window).returnToScene(window.getScene()).build().showErrorScene();
    }
}
Also used : Path(java.nio.file.Path) InvalidPassphraseException(org.cryptomator.cryptolib.api.InvalidPassphraseException) IOException(java.io.IOException) CryptoException(org.cryptomator.cryptolib.api.CryptoException) FXML(javafx.fxml.FXML)

Example 2 with CryptoException

use of org.cryptomator.cryptolib.api.CryptoException 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

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 FXML (javafx.fxml.FXML)2 CryptoException (org.cryptomator.cryptolib.api.CryptoException)2 Lazy (dagger.Lazy)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