use of org.cryptomator.cryptolib.api.Cryptor in project cryptofs by cryptomator.
the class CryptoBasicFileAttributesTest method setup.
@Before
public void setup() throws IOException {
cryptor = Mockito.mock(Cryptor.class);
FileHeaderCryptor headerCryptor = Mockito.mock(FileHeaderCryptor.class);
FileContentCryptor contentCryptor = Mockito.mock(FileContentCryptor.class);
Mockito.when(cryptor.fileHeaderCryptor()).thenReturn(headerCryptor);
Mockito.when(headerCryptor.headerSize()).thenReturn(88);
Mockito.when(cryptor.fileContentCryptor()).thenReturn(contentCryptor);
Mockito.when(contentCryptor.cleartextChunkSize()).thenReturn(32 * 1024);
Mockito.when(contentCryptor.ciphertextChunkSize()).thenReturn(16 + 32 * 1024 + 32);
ciphertextFilePath = Mockito.mock(Path.class);
FileSystem fs = Mockito.mock(FileSystem.class);
Mockito.when(ciphertextFilePath.getFileSystem()).thenReturn(fs);
FileSystemProvider fsProvider = Mockito.mock(FileSystemProvider.class);
Mockito.when(fs.provider()).thenReturn(fsProvider);
delegateAttr = Mockito.mock(BasicFileAttributes.class);
}
use of org.cryptomator.cryptolib.api.Cryptor 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();
}
}
}
use of org.cryptomator.cryptolib.api.Cryptor in project cryptofs by cryptomator.
the class Version6MigratorTest method testMigrate.
@Test
public void testMigrate() throws IOException {
String oldPassword = Normalizer.normalize("ä", Form.NFD);
String newPassword = Normalizer.normalize("ä", Form.NFC);
Assert.assertNotEquals(oldPassword, newPassword);
KeyFile beforeMigration = cryptorProvider.createNew().writeKeysToMasterkeyFile(oldPassword, 5);
Assert.assertEquals(5, beforeMigration.getVersion());
Files.write(masterkeyFile, beforeMigration.serialize());
Migrator migrator = new Version6Migrator(cryptorProvider);
migrator.migrate(pathToVault, "masterkey.cryptomator", oldPassword);
KeyFile afterMigration = KeyFile.parse(Files.readAllBytes(masterkeyFile));
Assert.assertEquals(6, afterMigration.getVersion());
try (Cryptor cryptor = cryptorProvider.createFromKeyFile(afterMigration, newPassword, 6)) {
Assert.assertNotNull(cryptor);
}
Assert.assertTrue(Files.exists(masterkeyBackupFile));
KeyFile backupKey = KeyFile.parse(Files.readAllBytes(masterkeyBackupFile));
Assert.assertEquals(5, backupKey.getVersion());
}
use of org.cryptomator.cryptolib.api.Cryptor in project cryptofs by cryptomator.
the class CryptoFileAttributeProviderTest method setup.
@Before
public void setup() throws IOException {
cryptor = Mockito.mock(Cryptor.class);
ciphertextFilePath = Mockito.mock(Path.class);
FileSystem fs = Mockito.mock(FileSystem.class);
Mockito.when(ciphertextFilePath.getFileSystem()).thenReturn(fs);
FileSystemProvider provider = Mockito.mock(FileSystemProvider.class);
Mockito.when(fs.provider()).thenReturn(provider);
BasicFileAttributes basicAttr = Mockito.mock(BasicFileAttributes.class);
PosixFileAttributes posixAttr = Mockito.mock(PosixFileAttributes.class);
DosFileAttributes dosAttr = Mockito.mock(DosFileAttributes.class);
Mockito.when(provider.readAttributes(Mockito.same(ciphertextFilePath), Mockito.same(BasicFileAttributes.class), Mockito.any())).thenReturn(basicAttr);
Mockito.when(provider.readAttributes(Mockito.same(ciphertextFilePath), Mockito.same(PosixFileAttributes.class), Mockito.any())).thenReturn(posixAttr);
Mockito.when(provider.readAttributes(Mockito.same(ciphertextFilePath), Mockito.same(DosFileAttributes.class), Mockito.any())).thenReturn(dosAttr);
}
use of org.cryptomator.cryptolib.api.Cryptor in project cryptofs by cryptomator.
the class Version6Migrator method migrate.
@Override
public void migrate(Path vaultRoot, String masterkeyFilename, CharSequence passphrase) throws InvalidPassphraseException, UnsupportedVaultFormatException, IOException {
LOG.info("Upgrading {} from version 5 to version 6.", vaultRoot);
Path masterkeyFile = vaultRoot.resolve(masterkeyFilename);
byte[] fileContentsBeforeUpgrade = Files.readAllBytes(masterkeyFile);
KeyFile keyFile = KeyFile.parse(fileContentsBeforeUpgrade);
try (Cryptor cryptor = cryptorProvider.createFromKeyFile(keyFile, passphrase, 5)) {
// create backup, as soon as we know the password was correct:
Path masterkeyBackupFile = vaultRoot.resolve(masterkeyFilename + Constants.MASTERKEY_BACKUP_SUFFIX);
Files.copy(masterkeyFile, masterkeyBackupFile, StandardCopyOption.REPLACE_EXISTING);
LOG.info("Backed up masterkey from {} to {}.", masterkeyFile.getFileName(), masterkeyBackupFile.getFileName());
// rewrite masterkey file with normalized passphrase:
byte[] fileContentsAfterUpgrade = cryptor.writeKeysToMasterkeyFile(Normalizer.normalize(passphrase, Form.NFC), 6).serialize();
Files.write(masterkeyFile, fileContentsAfterUpgrade, StandardOpenOption.TRUNCATE_EXISTING);
LOG.info("Updated masterkey.");
}
LOG.info("Upgraded {} from version 5 to version 6.", vaultRoot);
}
Aggregations