public void SignPackage()
        {
            var dss               = new DigitalSignatureService();
            var templatePackage   = new TemplatePackage(dss);
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = templatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var file  = Path.GetFullPath(@".\Packaging\Unsigned\UnsignedPackage.mstx");
            var file2 = Path.GetFullPath(@".\Packaging\ToSign.mstx");

            File.Copy(file, file2);

            using (Package package = Package.Open(file2, FileMode.Open))
            {
                dss.SignPackage(package, cert);
            }

            using (Package package = Package.Open(file2, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var isSigned         = dss.IsSigned(package);
                var isSignatureValid = dss.VerifySignatures(package);

                Assert.True(isSigned);
                Assert.True(isSignatureValid);
            }

            File.Delete(file2);
        }
Пример #2
0
        public void PackAndSign_CertFromFile_AbsouluteInOutPath()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile  = Path.Combine(Environment.CurrentDirectory, @"Packaging\SampleContent.txt");
            var outFile = @"C:\temp\Packaging\SignedContent.package";

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);
            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
Пример #3
0
        public void PackAndSign_CertFromFile_RelativeInOutPath()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile  = @"Packaging\SampleContent.txt";
            var outFile = @"Packaging\SignedContent.package";

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);
            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
Пример #4
0
        private static void EnsureTestCertificateInStore()
        {
            SecureString ss = GetTestCertPassword();

            if (TemplatePackage.LoadCert("B584589A382B2AD20B54D2DD1634BB487792A970") == null)
            {
                X509Certificate2 c = new X509Certificate2(@"Packaging\TestCert.pfx", ss, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadWrite);
                store.Add(c);
                store.Close();
            }
        }
Пример #5
0
        public void ValidateSignatureTamperedPackage()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile  = @"Packaging\SampleContent.txt";
            var outFile = @"Packaging\ToExtract.package";

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            ModifyContent(outFile, "SampleContent.txt");

            Assert.False(TemplatePackage.ValidateSignatures(outFile));

            File.Delete(outFile);
        }
Пример #6
0
        public void ExtractFileCurrentDir()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile        = @"Packaging\SampleContent.txt";
            var outFile       = @"ToExtract.package";
            var extractionDir = string.Empty;

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            TemplatePackage.Extract(outFile, extractionDir);

            Assert.True(File.Exists(outFile));

            File.Delete(outFile);
        }
Пример #7
0
        public async Task ExtractFileCurrentDirAsync()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile        = @"Packaging\SampleContent.txt";
            var outFile       = @"ToExtract.package";
            var extractionDir = string.Empty;

            await TemplatePackage.PackAndSignAsync(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            await TemplatePackage.ExtractAsync(outFile, extractionDir, null, CancellationToken.None, true);

            Assert.True(File.Exists(outFile));

            File.Delete(outFile);
        }
Пример #8
0
        public void ExtractFileAndPacksInCurrentDir()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            File.Copy(@"Packaging\SampleContent.txt", Path.Combine(Environment.CurrentDirectory, "NewFile.txt"), true);
            var inFile        = "NewFile.txt";
            var outFile       = @"ToExtract.package";
            var extractionDir = Environment.CurrentDirectory;

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            TemplatePackage.Extract(outFile, extractionDir);

            Assert.True(Directory.Exists(extractionDir));
            Assert.True(File.Exists(Path.Combine(extractionDir, Path.GetFileName(inFile))));

            File.Delete(outFile);
        }
Пример #9
0
        public void ExtractAbsoluteDirs()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile        = Path.Combine(Environment.CurrentDirectory, @"Packaging\SampleContent.txt");
            var outFile       = @"C:\Temp\MyPackage\ToExtract.package";
            var extractionDir = @"C:\Temp\NewContent\Extracted";

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            TemplatePackage.Extract(outFile, extractionDir);

            Assert.True(Directory.Exists(extractionDir));
            Assert.True(File.Exists(Path.Combine(extractionDir, @"Packaging\SampleContent.txt")));

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }
Пример #10
0
        public void ExtractRelativeDirs()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile        = @"Packaging\SampleContent.txt";
            var outFile       = @"Packaging\ToExtract.package";
            var extractionDir = "NewDirToExtract";

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            TemplatePackage.Extract(outFile, extractionDir);

            Assert.True(Directory.Exists(extractionDir));
            Assert.True(File.Exists(Path.Combine(extractionDir, inFile)));

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }
Пример #11
0
        public void PackAndSign_FolderExtractToAbsoluteDir()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            int filesInCurrentFolder = new DirectoryInfo(Environment.CurrentDirectory).GetFiles("*", SearchOption.AllDirectories).Count();
            var inFolder             = Environment.CurrentDirectory;
            var outDir = @"C:\Temp\OutFolder\Extraction";

            string signedFile = TemplatePackage.PackAndSign(inFolder, cert);

            TemplatePackage.Extract(signedFile, outDir);

            int filesInExtractionFolder = new DirectoryInfo(outDir).GetFiles("*", SearchOption.AllDirectories).Count();

            Assert.Equal(filesInCurrentFolder, filesInExtractionFolder);

            File.Delete(signedFile);
            Directory.Delete(outDir, true);
        }
Пример #12
0
        public async Task PackAndSign_FolderAsync()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            int filesInCurrentFolder = new DirectoryInfo(Environment.CurrentDirectory).GetFiles("*", SearchOption.AllDirectories).Count();
            var inFolder             = Environment.CurrentDirectory;
            var outDir = @"OutFolder\Extraction";

            string signedFile = await TemplatePackage.PackAndSignAsync(inFolder, cert);

            await TemplatePackage.ExtractAsync(signedFile, outDir, null, CancellationToken.None, true);

            int filesInExtractionFolder = new DirectoryInfo(outDir).GetFiles("*", SearchOption.AllDirectories).Count();

            Assert.Equal(filesInCurrentFolder, filesInExtractionFolder);

            File.Delete(signedFile);
            Directory.Delete(outDir, true);
        }
Пример #13
0
        public void ExtractFileTampered()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

            var inFile        = @"Packaging\SampleContent.txt";
            var outFile       = @"Packaging\ToExtract.package";
            var extractionDir = "SubDir";

            TemplatePackage.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            ModifyContent(outFile, "SampleContent.txt");

            Exception ex = Assert.Throws <InvalidSignatureException>(() =>
            {
                TemplatePackage.Extract(outFile, extractionDir);
            });

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }