Search in sources :

Example 1 with InvalidPassphraseException

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

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

the class UpgradeStrategy method upgrade.

/**
	 * Upgrades a vault. Might take a moment, should be run in a background thread.
	 */
public void upgrade(Vault vault, CharSequence passphrase) throws UpgradeFailedException {
    LOG.info("Upgrading {} from {} to {}.", vault.getPath(), vaultVersionBeforeUpgrade, vaultVersionAfterUpgrade);
    Cryptor cryptor = null;
    try {
        final Path masterkeyFile = vault.getPath().resolve(MASTERKEY_FILENAME);
        final byte[] masterkeyFileContents = Files.readAllBytes(masterkeyFile);
        cryptor = cryptorProvider.createFromKeyFile(KeyFile.parse(masterkeyFileContents), passphrase, vaultVersionBeforeUpgrade);
        // create backup, as soon as we know the password was correct:
        final Path masterkeyBackupFile = vault.getPath().resolve(MASTERKEY_BACKUP_FILENAME);
        Files.copy(masterkeyFile, masterkeyBackupFile, StandardCopyOption.REPLACE_EXISTING);
        LOG.info("Backuped masterkey.");
        // do stuff:
        upgrade(vault, cryptor);
        // write updated masterkey file:
        final byte[] upgradedMasterkeyFileContents = cryptor.writeKeysToMasterkeyFile(passphrase, vaultVersionAfterUpgrade).serialize();
        // path may have changed
        final Path masterkeyFileAfterUpgrade = vault.getPath().resolve(MASTERKEY_FILENAME);
        Files.write(masterkeyFileAfterUpgrade, upgradedMasterkeyFileContents, StandardOpenOption.TRUNCATE_EXISTING);
        LOG.info("Updated masterkey.");
    } catch (InvalidPassphraseException e) {
        throw new UpgradeFailedException(localization.getString("unlock.errorMessage.wrongPassword"));
    } catch (UnsupportedVaultFormatException e) {
        if (e.getDetectedVersion() == Integer.MAX_VALUE) {
            LOG.warn("Version MAC authentication error in vault {}", vault.getPath());
            throw new UpgradeFailedException(localization.getString("unlock.errorMessage.unauthenticVersionMac"));
        } else {
            LOG.warn("Upgrade failed.", e);
            throw new UpgradeFailedException("Upgrade failed. Details in log message.");
        }
    } catch (IOException e) {
        LOG.warn("Upgrade failed.", e);
        throw new UpgradeFailedException("Upgrade failed. Details in log message.");
    } finally {
        if (cryptor != null) {
            cryptor.destroy();
        }
    }
}
Also used : Path(java.nio.file.Path) InvalidPassphraseException(org.cryptomator.cryptolib.api.InvalidPassphraseException) Cryptor(org.cryptomator.cryptolib.api.Cryptor) IOException(java.io.IOException) UnsupportedVaultFormatException(org.cryptomator.cryptolib.api.UnsupportedVaultFormatException)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 InvalidPassphraseException (org.cryptomator.cryptolib.api.InvalidPassphraseException)2 FXML (javafx.fxml.FXML)1 CryptoException (org.cryptomator.cryptolib.api.CryptoException)1 Cryptor (org.cryptomator.cryptolib.api.Cryptor)1 UnsupportedVaultFormatException (org.cryptomator.cryptolib.api.UnsupportedVaultFormatException)1