public SecureLibraryLoader(ILocation loc, SecureLibraryManifest manifest, string publicKeyXml, IFileLoader fileLoader, ILogger logger) { m_Rsa = RSA.Create(); m_Rsa.FromXmlString(publicKeyXml); m_Loc = loc; m_Manifest = manifest; m_FileLoader = fileLoader; m_Logger = logger; }
public void ModifiedLibraryFileTest() { var fs = new MockFileSystem(); fs.AddDirectory("C:\\dir\\lib"); fs.AddDirectory("C:\\dir\\lib\\_themes"); fs.AddDirectory("C:\\dir\\lib\\_plugins"); fs.AddFile("C:\\dir\\lib\\_components\\comp1\\file1.txt", new MockFileData("f1")); fs.AddFile("C:\\dir\\lib\\_components\\comp1\\file2.txt", new MockFileData("f2")); byte[] GetSignature(string path) { var buffer = fs.File.ReadAllBytes(path); return(m_Rsa.SignData(buffer, 0, buffer.Length, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)); } var secLibMan = new SecureLibraryManifest(); secLibMan.Components = new SecureLibraryItem[] { new SecureLibraryItem() { Name = "comp1", Files = new SecureLibraryItemFile[] { new SecureLibraryItemFile() { Name = Location.FromPath("file1.txt"), Signature = GetSignature("C:\\dir\\lib\\_components\\comp1\\file1.txt") }, new SecureLibraryItemFile() { Name = Location.FromPath("file2.txt"), Signature = GetSignature("C:\\dir\\lib\\_components\\comp1\\file2.txt") } } } }; using (var writer = fs.File.CreateText("C:\\dir\\lib\\lib.manifest")) { new UserSettingsService().StoreSettings(secLibMan, writer, new BaseValueSerializer <ILocation>(l => l.ToId(), null)); } var cleaner = new SecureLibraryCleaner("C:\\dir\\lib\\lib.manifest", m_Rsa.ToXmlString(false), fs); fs.File.WriteAllText("C:\\dir\\lib\\_components\\comp1\\file2.txt", "f2-mod"); Assert.ThrowsAsync <LibraryFileModifiedException>(() => cleaner.ClearDirectory(Location.FromPath("C:\\dir\\lib"))); }
public SecureLibraryCleaner(string manifestPath, string publicKeyXml, IFileSystem fileSystem) { m_FileSystem = fileSystem; if (m_FileSystem.File.Exists(manifestPath)) { m_ManifestFilePath = manifestPath; m_Rsa = RSA.Create(); m_Rsa.FromXmlString(publicKeyXml); using (var textReader = m_FileSystem.File.OpenText(manifestPath)) { m_Manifest = new UserSettingsService().ReadSettings <SecureLibraryManifest>( textReader, new BaseValueSerializer <ILocation>(null, x => Location.FromString(x))); } } }
public void ManifestDirMismatchTest() { var fs = new MockFileSystem(); fs.AddDirectory("C:\\dir\\lib1"); fs.AddDirectory("C:\\dir\\lib\\_themes"); fs.AddDirectory("C:\\dir\\lib\\_plugins"); fs.AddFile("C:\\dir\\lib\\_components\\comp1\\file1.txt", new MockFileData("f1")); var secLibMan = new SecureLibraryManifest(); using (var writer = fs.File.CreateText("C:\\dir\\lib1\\lib.manifest")) { new UserSettingsService().StoreSettings(secLibMan, writer, new BaseValueSerializer <ILocation>(l => l.ToId(), null)); } var cleaner = new SecureLibraryCleaner("C:\\dir\\lib1\\lib.manifest", m_Rsa.ToXmlString(false), fs); Assert.ThrowsAsync <LibraryDirectoryManifestMismatchException>(() => cleaner.ClearDirectory(Location.FromPath("C:\\dir\\lib"))); }
private async Task <SecureLibraryManifest> CreateManifest(ILocation libFolder, RSA rsaWrite, Version vers) { var components = new Dictionary <string, List <SecureLibraryItemFile> >(StringComparer.CurrentCultureIgnoreCase); var themes = new Dictionary <string, List <SecureLibraryItemFile> >(StringComparer.CurrentCultureIgnoreCase); var plugins = new Dictionary <string, List <SecureLibraryItemFile> >(StringComparer.CurrentCultureIgnoreCase); await foreach (var file in m_Loader.LoadFolder(libFolder, null)) { if (file.Location.Segments.Count >= 2) { var itemType = file.Location.Segments[0]; var itemName = file.Location.Segments[1]; Dictionary <string, List <SecureLibraryItemFile> > thisComp = null; switch (itemType.ToLower()) { case Location.Library.ComponentsFolderName: thisComp = components; break; case Location.Library.ThemesFolderName: thisComp = themes; break; case Location.Library.PluginsFolderName: thisComp = plugins; break; default: continue; } List <SecureLibraryItemFile> files; if (!thisComp.TryGetValue(itemName, out files)) { files = new List <SecureLibraryItemFile>(); thisComp.Add(itemName, files); } var signature = rsaWrite.SignData(file.Content, HashAlgorithmName.SHA256, RSASignaturePadding.Pss); var fileManifest = new SecureLibraryItemFile() { Name = file.Location.GetRelative(new Location("", "", new string[] { itemType, itemName })), Signature = signature }; files.Add(fileManifest); } } var manifest = new SecureLibraryManifest() { Version = vers, Components = components.Select(x => new SecureLibraryItem() { Name = x.Key, Files = x.Value.ToArray() }).ToArray(), Themes = themes.Select(x => new SecureLibraryItem() { Name = x.Key, Files = x.Value.ToArray() }).ToArray(), Plugins = plugins.Select(x => new SecureLibraryItem() { Name = x.Key, Files = x.Value.ToArray() }).ToArray() }; return(manifest); }
public async Task FullDirCleanTest() { var fs = new MockFileSystem(); fs.AddDirectory("C:\\dir\\lib"); fs.AddFile("C:\\dir\\file1.txt", new MockFileData("f0")); fs.AddFile("C:\\dir\\lib\\_components\\comp1\\file1.txt", new MockFileData("f1")); fs.AddFile("C:\\dir\\lib\\_components\\comp1\\subdir1\\file1.txt", new MockFileData("sb1f1")); fs.AddFile("C:\\dir\\lib\\_themes\\theme1\\file2.txt", new MockFileData("f2")); fs.AddFile("C:\\dir\\lib\\_themes\\theme1\\subdir2\\file2.txt", new MockFileData("sb2f2")); fs.AddFile("C:\\dir\\lib\\_plugins\\plugin1\\file3.txt", new MockFileData("f3")); fs.AddFile("C:\\dir\\lib\\_plugins\\plugin1\\subdir3\\subdir4\\file3.txt", new MockFileData("sb3sb4f3")); byte[] GetSignature(string path) { var buffer = fs.File.ReadAllBytes(path); return(m_Rsa.SignData(buffer, 0, buffer.Length, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)); } var secLibMan = new SecureLibraryManifest(); secLibMan.Components = new SecureLibraryItem[] { new SecureLibraryItem() { Name = "comp1", Files = new SecureLibraryItemFile[] { new SecureLibraryItemFile() { Name = Location.FromPath("file1.txt"), Signature = GetSignature("C:\\dir\\lib\\_components\\comp1\\file1.txt") }, new SecureLibraryItemFile() { Name = Location.FromPath("subdir1\\file1.txt"), Signature = GetSignature("C:\\dir\\lib\\_components\\comp1\\subdir1\\file1.txt") } } } }; secLibMan.Themes = new SecureLibraryItem[] { new SecureLibraryItem() { Name = "theme1", Files = new SecureLibraryItemFile[] { new SecureLibraryItemFile() { Name = Location.FromPath("file2.txt"), Signature = GetSignature("C:\\dir\\lib\\_themes\\theme1\\file2.txt") }, new SecureLibraryItemFile() { Name = Location.FromPath("subdir2\\file2.txt"), Signature = GetSignature("C:\\dir\\lib\\_themes\\theme1\\subdir2\\file2.txt") } } } }; secLibMan.Plugins = new SecureLibraryItem[] { new SecureLibraryItem() { Name = "plugin1", Files = new SecureLibraryItemFile[] { new SecureLibraryItemFile() { Name = Location.FromPath("file3.txt"), Signature = GetSignature("C:\\dir\\lib\\_plugins\\plugin1\\file3.txt") }, new SecureLibraryItemFile() { Name = Location.FromPath("subdir3\\subdir4\\file3.txt"), Signature = GetSignature("C:\\dir\\lib\\_plugins\\plugin1\\subdir3\\subdir4\\file3.txt") } } } }; using (var writer = fs.File.CreateText("C:\\dir\\lib\\lib.manifest")) { new UserSettingsService().StoreSettings(secLibMan, writer, new BaseValueSerializer <ILocation>(l => l.ToId(), null)); } var cleaner = new SecureLibraryCleaner("C:\\dir\\lib\\lib.manifest", m_Rsa.ToXmlString(false), fs); await cleaner.ClearDirectory(Location.FromPath("C:\\dir\\lib")); Assert.AreEqual(2, fs.AllDirectories.Count()); Assert.IsTrue(fs.AllDirectories.Contains("C:\\")); Assert.IsTrue(fs.AllDirectories.Contains("C:\\dir")); Assert.AreEqual(1, fs.AllFiles.Count()); Assert.AreEqual("C:\\dir\\file1.txt", fs.AllFiles.First()); }