internal static async Task CreateAsync(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.");
                        await TemplatePackage.PackAndSignAsync(inputPath, outfile, certThumbprint, "text/plain").ConfigureAwait(false);

                        output.WriteCommandText($"Templates package file '{outfile}' successfully created.");
                    }
                    else
                    {
                        output.WriteCommandText($"No cert thumbprint provided, the template package will not be signed.");
                        await TemplatePackage.PackAsync(inputPath, outfile, "text/plain").ConfigureAwait(false);
                    }

                    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.");
            }
        }
Example #2
0
 public async Task PackAndSign_CertNotFoundAsync()
 {
     SignCertNotFoundException ex = await Assert.ThrowsAsync <SignCertNotFoundException>(async() =>
     {
         await TemplatePackage.PackAndSignAsync(@"Packaging\SampleContent.txt", "SignedContent.package", "CERT_NOT_FOUND", MediaTypeNames.Text.Plain);
     });
 }
Example #3
0
        public async Task PackAndSign_WithThumbprintAsync()
        {
            EnsureTestCertificateInStore();

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

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

            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
Example #4
0
        public async Task PackAndSign_CertFromFile_AbsouluteInOutPathAsync()
        {
            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";

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

            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
Example #5
0
        public async Task PackAndSign_CertFromFile_RelativeInOutPathAsync()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

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

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

            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
Example #6
0
        public async Task ValidateSignatureTamperedPackageAsync()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

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

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

            ModifyContent(outFile, "SampleContent.txt");

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

            File.Delete(outFile);
        }
Example #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);

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

            File.Delete(outFile);
        }
Example #8
0
        public async Task ExtractFileAndPacksInCurrentDirAsync()
        {
            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;

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

            await TemplatePackage.ExtractAsync(outFile, extractionDir);

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

            File.Delete(outFile);
        }
Example #9
0
        public async Task ExtractAbsoluteDirsAsync()
        {
            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";

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

            await TemplatePackage.ExtractAsync(outFile, extractionDir);

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

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }
Example #10
0
        public async Task ExtractRelativeDirsAsync()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = TemplatePackage.LoadCert(@"Packaging\TestCert.pfx", certPass);

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

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

            await TemplatePackage.ExtractAsync(outFile, extractionDir);

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

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }
Example #11
0
        public async Task PackAndSign_FolderExtractToAbsoluteDirAsync()
        {
            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 = await TemplatePackage.PackAndSignAsync(inFolder, cert);

            await TemplatePackage.ExtractAsync(signedFile, outDir);

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

            Assert.Equal(filesInCurrentFolder, filesInExtractionFolder);

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

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

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

            ModifyContent(outFile, "SampleContent.txt");

            InvalidSignatureException ex = await Assert.ThrowsAsync <InvalidSignatureException>(async() =>
            {
                await TemplatePackage.ExtractAsync(outFile, extractionDir);
            });

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