Skip to content

Commit

Permalink
Adjusted to new masterkey API
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Mar 16, 2021
1 parent d5772f1 commit e9c02b8
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- dependencies -->
<cryptolib.version>2.0.0-beta5</cryptolib.version>
<cryptolib.version>2.0.0-beta6</cryptolib.version>
<jwt.version>3.12.0</jwt.version>
<dagger.version>2.31</dagger.version>
<guava.version>30.1-jre</guava.version>
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/cryptomator/cryptofs/CryptoFileSystems.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,10 @@ public CryptoFileSystemImpl create(CryptoFileSystemProvider provider, Path pathT

var configLoader = VaultConfig.decode(token);
var keyId = configLoader.getKeyId();
byte[] rawKey = new byte[0];
try (Masterkey key = properties.keyLoader(keyId.getScheme()).loadKey(keyId)) {
rawKey = key.getEncoded();
var config = configLoader.verify(rawKey, Constants.VAULT_VERSION);
var config = configLoader.verify(key.getEncoded(), Constants.VAULT_VERSION);
var adjustedProperties = adjustForCapabilities(pathToVault, properties);
var keyCopy = Masterkey.createFromRaw(key.getEncoded()); // TODO replace with key.clone() eventually
var cryptor = config.getCipherCombo().getCryptorProvider(csprng).withKey(keyCopy);
var cryptor = config.getCipherCombo().getCryptorProvider(csprng).withKey(key.clone());
try {
checkVaultRootExistence(pathToVault, cryptor);
return fileSystems.compute(normalizedPathToVault, (path, fs) -> {
Expand All @@ -69,8 +66,6 @@ public CryptoFileSystemImpl create(CryptoFileSystemProvider provider, Path pathT
cryptor.destroy();
throw e;
}
} finally {
Arrays.fill(rawKey, (byte) 0x00);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class Windows {
public void setupClass(@TempDir Path tmpDir) throws IOException, MasterkeyLoadingFailedException {
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme(Mockito.any())).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(tmpDir, properties, URI.create("test:key"));
fileSystem = CryptoFileSystemProvider.newFileSystem(tmpDir, properties);
Expand Down Expand Up @@ -143,7 +143,7 @@ public void beforeAll() throws IOException, MasterkeyLoadingFailedException {
Files.createDirectories(vaultPath);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
var properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(vaultPath, properties, URI.create("test:key"));
fileSystem = new CryptoFileSystemProvider().newFileSystem(vaultPath, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class CryptoFileSystemProviderIntegrationTest {
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class WithLimitedPaths {

private byte[] rawKey = new byte[64];
private MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
private CryptoFileSystem fs;
private Path shortFilePath;
Expand All @@ -86,7 +85,7 @@ class WithLimitedPaths {
@BeforeAll
public void setup(@TempDir Path tmpDir) throws IOException, MasterkeyLoadingFailedException {
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(rawKey));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = cryptoFileSystemProperties() //
.withFlags() //
.withMasterkeyFilename("masterkey.cryptomator") //
Expand Down Expand Up @@ -195,8 +194,8 @@ public void setup() throws IOException, MasterkeyLoadingFailedException {
keyLoader2 = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader1.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader2.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader1.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(key1));
Mockito.when(keyLoader2.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(key2));
Mockito.when(keyLoader1.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(key1));
Mockito.when(keyLoader2.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(key2));
pathToVault1 = tmpFs.getPath("/vaultDir1");
pathToVault2 = tmpFs.getPath("/vaultDir2");
Files.createDirectory(pathToVault1);
Expand Down Expand Up @@ -537,7 +536,7 @@ public void setup(@TempDir Path tmpDir) throws IOException, MasterkeyLoadingFail
Files.createDirectories(pathToVault);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
var properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
fs = CryptoFileSystemProvider.newFileSystem(pathToVault, properties);
Expand Down Expand Up @@ -630,7 +629,7 @@ public void setup(@TempDir Path tmpDir) throws IOException, MasterkeyLoadingFail
Files.createDirectories(pathToVault);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
var properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
fs = CryptoFileSystemProvider.newFileSystem(pathToVault, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static final Stream<InvocationWhichShouldFail> shouldFailWithRelativePat
@SuppressWarnings("deprecation")
public void setup() throws MasterkeyLoadingFailedException {
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
when(keyLoader.loadKey(Mockito.any())).thenReturn(new Masterkey(new byte[64]));

CryptoFileSystemProviderComponent component = mock(CryptoFileSystemProviderComponent.class);
when(component.fileSystems()).thenReturn(fileSystems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testCreateWithPathToVaultFromNonDefaultProvider() throws IOException
try {
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(tempDir, properties, URI.create("test:key"));
FileSystem fileSystem = CryptoFileSystemProvider.newFileSystem(tempDir, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ public class CryptoFileSystemsTest {

private MockedStatic<VaultConfig> vaultConficClass;
private MockedStatic<Files> filesClass;
private MockedStatic<Masterkey> masterkeyClass;

private final CryptoFileSystems inTest = new CryptoFileSystems(cryptoFileSystemComponentBuilder, capabilityChecker, csprng);

@BeforeEach
public void setup() throws IOException, MasterkeyLoadingFailedException {
vaultConficClass = Mockito.mockStatic(VaultConfig.class);
filesClass = Mockito.mockStatic(Files.class);
masterkeyClass = Mockito.mockStatic(Masterkey.class);

when(pathToVault.normalize()).thenReturn(normalizedPathToVault);
when(normalizedPathToVault.resolve("vault.cryptomator")).thenReturn(configFilePath);
Expand All @@ -80,8 +78,8 @@ public void setup() throws IOException, MasterkeyLoadingFailedException {
when(configLoader.getKeyId()).thenReturn(URI.create("test:key"));
when(keyLoader.loadKey(Mockito.any())).thenReturn(masterkey);
when(masterkey.getEncoded()).thenReturn(rawKey);
when(masterkey.clone()).thenReturn(clonedMasterkey);
when(configLoader.verify(rawKey, Constants.VAULT_VERSION)).thenReturn(vaultConfig);
masterkeyClass.when(() -> Masterkey.createFromRaw(rawKey)).thenReturn(clonedMasterkey);
when(cryptorProvider.withKey(clonedMasterkey)).thenReturn(cryptor);
when(vaultConfig.getCipherCombo()).thenReturn(cipherCombo);
when(cipherCombo.getCryptorProvider(csprng)).thenReturn(cryptorProvider);
Expand All @@ -105,7 +103,6 @@ public void setup() throws IOException, MasterkeyLoadingFailedException {
public void tearDown() {
vaultConficClass.close();
filesClass.close();
masterkeyClass.close();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void setupClass(@TempDir Path tmpDir) throws IOException, Masterke
Files.createDirectory(pathToVault);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
fileSystem = new CryptoFileSystemProvider().newFileSystem(create(pathToVault), properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ReadmeCodeSamplesTest {
public void testReadmeCodeSampleUsingFileSystemConstructionMethodA(@TempDir Path storageLocation) throws IOException, MasterkeyLoadingFailedException {
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(storageLocation, properties, URI.create("test:key"));
FileSystem fileSystem = CryptoFileSystemProvider.newFileSystem(storageLocation, properties);
Expand All @@ -45,7 +45,7 @@ public void testReadmeCodeSampleUsingFileSystemConstructionMethodB(@TempDir Path
URI uri = CryptoFileSystemUri.create(storageLocation);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(storageLocation, properties, URI.create("test:key"));
FileSystem fileSystem = FileSystems.newFileSystem(uri, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void setupClass(@TempDir Path tmpDir) throws IOException, Masterke
Files.createDirectory(pathToVault);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
fileSystem = new CryptoFileSystemProvider().newFileSystem(create(pathToVault), properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setup() throws IOException, MasterkeyLoadingFailedException {
Files.createDirectory(pathToVault);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
fileSystem = new CryptoFileSystemProvider().newFileSystem(create(pathToVault), properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void setupClass() throws IOException, MasterkeyLoadingFailedExcept
Files.createDirectory(pathToVault);
MasterkeyLoader keyLoader = Mockito.mock(MasterkeyLoader.class);
Mockito.when(keyLoader.supportsScheme("test")).thenReturn(true);
Mockito.when(keyLoader.loadKey(Mockito.any())).thenReturn(Masterkey.createFromRaw(new byte[64]));
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(new byte[64]));
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoaders(keyLoader).build();
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
fileSystem = new CryptoFileSystemProvider().newFileSystem(create(pathToVault), properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testMigrate() throws IOException, CryptoException {
String newPassword = Normalizer.normalize("ä", Form.NFC);
Assertions.assertNotEquals(oldPassword, newPassword);

Masterkey masterkey = Masterkey.createNew(csprng);
Masterkey masterkey = Masterkey.generate(csprng);
MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(new byte[0], csprng);
masterkeyFileAccess.persist(masterkey, masterkeyFile, oldPassword, 5);
byte[] beforeMigration = Files.readAllBytes(masterkeyFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void setup() throws IOException {
Files.createDirectory(dataDir);
Files.createDirectory(metaDir);

Masterkey masterkey = Masterkey.createNew(csprng);
Masterkey masterkey = Masterkey.generate(csprng);
MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(new byte[0], csprng);
masterkeyFileAccess.persist(masterkey, masterkeyFile, "test", 6);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void teardown() throws IOException {

@Test
public void testMigrate() throws CryptoException, IOException {
Masterkey masterkey = Masterkey.createNew(csprng);
Masterkey masterkey = Masterkey.generate(csprng);
MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(new byte[0], csprng);
masterkeyFileAccess.persist(masterkey, masterkeyFile, "topsecret", 7);
Assumptions.assumeFalse(Files.exists(vaultConfigFile));
Expand Down

0 comments on commit e9c02b8

Please sign in to comment.