use of org.cryptomator.cryptolib.api.UnsupportedVaultFormatException in project cryptofs by cryptomator.
the class MigratorsTest method testMigrateUnsupportedVaultFormat.
@Test(expected = IllegalStateException.class)
@SuppressWarnings("deprecation")
public void testMigrateUnsupportedVaultFormat() throws NoApplicableMigratorException, InvalidPassphraseException, IOException {
Migrator migrator = Mockito.mock(Migrator.class);
Migrators migrators = new Migrators(new HashMap<Migration, Migrator>() {
{
put(Migration.ZERO_TO_ONE, migrator);
}
});
Mockito.doThrow(new UnsupportedVaultFormatException(Integer.MAX_VALUE, 1)).when(migrator).migrate(pathToVault, "masterkey.cryptomator", "secret");
migrators.migrate(pathToVault, "masterkey.cryptomator", "secret");
}
use of org.cryptomator.cryptolib.api.UnsupportedVaultFormatException in project cryptofs by cryptomator.
the class Migrators method migrate.
/**
* Performs the actual migration. This task may take a while and this method will block.
*
* @param pathToVault Path to the vault's root
* @param masterkeyFilename Name of the masterkey file located in the vault
* @param passphrase The passphrase needed to unlock the vault
* @throws NoApplicableMigratorException If the vault can not be migrated, because no migrator could be found
* @throws InvalidPassphraseException If the passphrase could not be used to unlock the vault
* @throws IOException if an I/O error occurs migrating the vault
*/
public void migrate(Path pathToVault, String masterkeyFilename, CharSequence passphrase) throws NoApplicableMigratorException, InvalidPassphraseException, IOException {
Path masterKeyPath = pathToVault.resolve(masterkeyFilename);
byte[] keyFileContents = Files.readAllBytes(masterKeyPath);
KeyFile keyFile = KeyFile.parse(keyFileContents);
try {
Migrator migrator = findApplicableMigrator(keyFile.getVersion()).orElseThrow(NoApplicableMigratorException::new);
migrator.migrate(pathToVault, masterkeyFilename, passphrase);
} catch (UnsupportedVaultFormatException e) {
// might be a tampered masterkey file, as this exception is also thrown if the vault version MAC is not authentic.
throw new IllegalStateException("Vault version checked beforehand but not supported by migrator.");
}
}
use of org.cryptomator.cryptolib.api.UnsupportedVaultFormatException 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();
}
}
}
Aggregations