public static void DecryptAndOpenFile(IRuntimeFileInfo encryptedDocument, Passphrase passphrase, ProgressContext progress, Action <string, ProgressContext> failure = null) { string tempPath = Path.GetTempPath(); string decryptedFileName; lastUsedKey = passphrase.DerivedPassphrase; if (!TryDecrypt(encryptedDocument, tempPath, lastUsedKey, progress, out decryptedFileName)) { failure("Could not open file", progress); return; } string fullPathToDecryptedFile = Path.Combine(tempPath, decryptedFileName); IRuntimeFileInfo decryptedFile = OS.Current.FileInfo(fullPathToDecryptedFile); NSDictionary userInfo = new NSDictionary(Launcher.TargetFileUserInfoKey, decryptedFile.FullName); NSNotification notification = NSNotification.FromName(Launcher.FileDecryptedNotification, new NSObject(), userInfo); NSNotificationCenter.DefaultCenter.PostNotification(notification); ILauncher launcher = OS.Current.Launch(fullPathToDecryptedFile); launcher.Exited += (sender, e) => { fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext()); }; fileSystemState.Add(new ActiveFile(encryptedDocument, decryptedFile, lastUsedKey, ActiveFileStatus.AssumedOpenAndDecrypted, launcher)); //fileSystemState.Save (); }
public static void TestCheckActiveFilesKeyIsNotSetWithoutKnownKey() { DateTime utcNow = OS.Current.UtcNow; FakeRuntimeFileInfo.AddFile(_encryptedFile1, utcNow, utcNow, utcNow, FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)); Passphrase passphrase = new Passphrase("a"); AxCryptFile.Decrypt(OS.Current.FileInfo(_encryptedFile1), OS.Current.FileInfo(_decryptedFile1), passphrase.DerivedPassphrase, AxCryptOptions.None, new ProgressContext()); ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedFile1), OS.Current.FileInfo(_decryptedFile1), passphrase.DerivedPassphrase, ActiveFileStatus.AssumedOpenAndDecrypted, null); _fileSystemState.Add(activeFile); _fileSystemState.Save(); _fileSystemState.Dispose(); _fileSystemState = new FileSystemState(); _fileSystemState.Load(OS.Current.FileInfo(_fileSystemStateFilePath)); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(_decryptedFile1); decryptedFileInfo.SetFileTimes(utcNow.AddSeconds(30), utcNow.AddSeconds(30), utcNow.AddSeconds(30)); SetupAssembly.FakeRuntimeEnvironment.TimeFunction = (() => { return(utcNow.AddMinutes(1)); }); bool changedWasRaised = false; _fileSystemState.Changed += ((object sender, ActiveFileChangedEventArgs e) => { changedWasRaised = true; }); activeFile = _fileSystemState.FindEncryptedPath(_encryptedFile1); Assert.That(activeFile.Key, Is.Null, "The key should be null after loading of new FileSystemState"); _fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext()); Assert.That(changedWasRaised, Is.False, "The ActiveFile should be not be modified because the file was modified as well and thus cannot be deleted."); activeFile = _fileSystemState.FindEncryptedPath(_encryptedFile1); Assert.That(activeFile.Key, Is.Null, "The key should still be null after the checking of active files."); Assert.That(activeFile.Status.HasMask(ActiveFileStatus.AssumedOpenAndDecrypted), Is.True, "The file should still be there."); Assert.That(activeFile.ThumbprintMatch(passphrase.DerivedPassphrase), Is.True, "The active file should still be known to be decryptable with the original passphrase."); }
public static void TestCheckActiveFilesIsNotLocked() { DateTime utcNow = OS.Current.UtcNow; DateTime utcYesterday = utcNow.AddDays(-1); FakeRuntimeFileInfo.AddFile(_encryptedFile1, utcNow, utcNow, utcNow, Stream.Null); FakeRuntimeFileInfo.AddFile(_decryptedFile1, utcYesterday, utcYesterday, utcYesterday, Stream.Null); ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedFile1), OS.Current.FileInfo(_decryptedFile1), new AesKey(), ActiveFileStatus.NotDecrypted, null); SetupAssembly.FakeRuntimeEnvironment.TimeFunction = (() => { return(utcNow.AddMinutes(10)); }); bool changedWasRaised = false; _fileSystemState.Add(activeFile); _fileSystemState.Changed += ((object sender, ActiveFileChangedEventArgs e) => { changedWasRaised = true; }); _fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext()); Assert.That(changedWasRaised, Is.True, "The file should be detected as decrypted being created."); }