Ejemplo n.º 1
0
 public void PackAndSign_CertNotFound()
 {
     Exception ex = Assert.Throws <SignCertNotFoundException>(() =>
     {
         TemplatePackage.PackAndSign(@"Packaging\SampleContent.txt", "SignedContent.package", "CERT_NOT_FOUND", MediaTypeNames.Text.Plain);
     });
 }
        internal static void Create(string inputPath, string outfile, string certThumbprint, TextWriter output, TextWriter error)
        {
            try
            {
                if (Directory.Exists(inputPath))
                {
                    output.WriteCommandHeader($"Creating template package from folder {inputPath}.");
                    if (!string.IsNullOrWhiteSpace(certThumbprint))
                    {
                        output.WriteCommandText($"The template package will be signed using the cert matching {certThumbprint} as thumbprint.");
                        TemplatePackage.PackAndSign(inputPath, outfile, certThumbprint, "text/plain");
                        output.WriteCommandText($"Templates package file '{outfile}' successfully created.");
                    }
                    else
                    {
                        output.WriteCommandText($"No cert thumbprint provided, the template package will not be signed.");
                        TemplatePackage.Pack(inputPath, outfile, "text/plain");
                    }

                    output.WriteCommandText($"Templates package file '{outfile}' successfully created.");
                }
                else
                {
                    error.WriteCommandText($"{inputPath} is not a valid folder to create a Templates Package.");
                }
            }
            catch (Exception ex)
            {
                error.WriteException(ex, "Unexpected exception creating templates package.");
            }
        }
Ejemplo n.º 3
0
        public void PackAndSign_WithThumbprint()
        {
            EnsureTestCertificateInStore();

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

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

            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 12
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);
        }