Search in sources :

Example 1 with Cryptor

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);
}
Also used : Path(java.nio.file.Path) FileHeaderCryptor(org.cryptomator.cryptolib.api.FileHeaderCryptor) FileHeaderCryptor(org.cryptomator.cryptolib.api.FileHeaderCryptor) FileContentCryptor(org.cryptomator.cryptolib.api.FileContentCryptor) Cryptor(org.cryptomator.cryptolib.api.Cryptor) FileSystemProvider(java.nio.file.spi.FileSystemProvider) FileSystem(java.nio.file.FileSystem) FileContentCryptor(org.cryptomator.cryptolib.api.FileContentCryptor) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Before(org.junit.Before)

Example 2 with Cryptor

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();
        }
    }
}
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)

Example 3 with Cryptor

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());
}
Also used : Cryptor(org.cryptomator.cryptolib.api.Cryptor) KeyFile(org.cryptomator.cryptolib.api.KeyFile) Migrator(org.cryptomator.cryptofs.migration.api.Migrator) Test(org.junit.Test)

Example 4 with Cryptor

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);
}
Also used : Path(java.nio.file.Path) Cryptor(org.cryptomator.cryptolib.api.Cryptor) FileSystemProvider(java.nio.file.spi.FileSystemProvider) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) FileSystem(java.nio.file.FileSystem) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Before(org.junit.Before)

Example 5 with Cryptor

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);
}
Also used : Path(java.nio.file.Path) Cryptor(org.cryptomator.cryptolib.api.Cryptor) KeyFile(org.cryptomator.cryptolib.api.KeyFile)

Aggregations

Cryptor (org.cryptomator.cryptolib.api.Cryptor)7 Path (java.nio.file.Path)6 IOException (java.io.IOException)2 FileSystem (java.nio.file.FileSystem)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 FileSystemProvider (java.nio.file.spi.FileSystemProvider)2 KeyFile (org.cryptomator.cryptolib.api.KeyFile)2 Before (org.junit.Before)2 Provides (dagger.Provides)1 NotDirectoryException (java.nio.file.NotDirectoryException)1 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)1 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)1 Migrator (org.cryptomator.cryptofs.migration.api.Migrator)1 FileContentCryptor (org.cryptomator.cryptolib.api.FileContentCryptor)1 FileHeaderCryptor (org.cryptomator.cryptolib.api.FileHeaderCryptor)1 InvalidPassphraseException (org.cryptomator.cryptolib.api.InvalidPassphraseException)1 UnsupportedVaultFormatException (org.cryptomator.cryptolib.api.UnsupportedVaultFormatException)1 Test (org.junit.Test)1