Ejemplo n.º 1
0
        public void VerifySignature()
        {
            var signature         = TestdataLoader.ReadFromResource("signature.p7s");
            var signedData        = TestdataLoader.ReadFromResource("signedData.xml");
            var signatureVerifier = new SignatureVerifier();
            var certificate       = signatureVerifier.Validate(signedData, signature);

            certificate.Should().NotBeNull();
        }
Ejemplo n.º 2
0
        public void ReadAsiceWithCadesManifestAndSignature()
        {
            var          signingCertificates = TestdataLoader.ReadCertificatesForTest();
            const string contentFile         = "filename.txt";
            const string content             = "Lorem ipsum";

            using (var outputStream = new MemoryStream())
            {
                using (var textFileStream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    using (var asiceBuilder = AsiceBuilder.Create(outputStream, MessageDigestAlgorithm.SHA256Desig, signingCertificates))
                    {
                        asiceBuilder.AddFile(textFileStream, contentFile, MimeType.ForString("text/plain"));
                        asiceBuilder.Build().Should().NotBeNull();
                    }

                using (var readStream = new MemoryStream(outputStream.ToArray()))
                    using (var zip = new ZipArchive(readStream))
                    {
                        var asicePackage = AsiceReadModel.Create(zip);
                        var entries      = asicePackage.Entries;
                        entries.Count().Should().Be(1);
                        var cadesManifest = asicePackage.CadesManifest;
                        cadesManifest.Should().NotBeNull();

                        cadesManifest.Digests.Count.Should().Be(1);
                        asicePackage.DigestVerifier.Should().NotBeNull();
                        cadesManifest.SignatureFileName.Should().NotBeNull();
                        asicePackage.Signatures.Should().NotBeNull();
                        asicePackage.Signatures.Containers.Count().Should().Be(1);

                        var firstEntry = entries.First();
                        using (var entryStream = firstEntry.OpenStream())
                            using (var memoryStream = new MemoryStream())
                            {
                                entryStream.CopyTo(memoryStream);
                                Encoding.UTF8.GetString(memoryStream.ToArray()).Should().Be(content);
                            }

                        var verificationResult = asicePackage.DigestVerifier.Verification();
                        verificationResult.Should().NotBeNull();
                        verificationResult.AllValid.Should().BeTrue();
                    }
            }
        }
Ejemplo n.º 3
0
        public void CreateArchive()
        {
            byte[] zippedData;
            using (var zippedOutStream = new MemoryStream())
            {
                using (var archive = AsiceArchive.Create(
                           zippedOutStream,
                           CadesManifestCreator.CreateWithSignatureFile(),
                           TestdataLoader.ReadCertificatesForTest(),
                           MessageDigestAlgorithm.SHA256Desig))
                    using (var fileStream = File.OpenRead(FileNameTestPdf))
                    {
                        archive.AddEntry(
                            fileStream,
                            new FileRef(FileNameTestPdf, MimeType.ForString(MimeTypes.GetMimeType(FileNameTestPdf))));
                    }

                zippedData = zippedOutStream.ToArray();
                zippedData.Should().NotBeNull();
                zippedData.Should().HaveCountGreaterThan(0);
            }

            using (var zipInput = new MemoryStream(zippedData))
                using (var zippedArchive = new ZipArchive(zipInput, ZipArchiveMode.Read))
                {
                    zippedArchive.Entries.Should().HaveCount(4);
                    zippedArchive.Entries.First(e => e.FullName.Equals(FileNameTestPdf, StringComparison.CurrentCulture)).Should().NotBeNull();
                    zippedArchive.Entries.First(e => e.FullName.Equals(AsiceConstants.CadesManifestFilename, StringComparison.CurrentCulture)).Should().NotBeNull();
                    zippedArchive.Entries.First(e => e.FullName.Equals(AsiceConstants.FileNameMimeType, StringComparison.CurrentCulture)).Should()
                    .NotBeNull();

                    var mimeTypeEntry = zippedArchive.GetEntry(AsiceConstants.FileNameMimeType);
                    using (var entryStream = mimeTypeEntry.Open())
                        using (var copyStream = new MemoryStream())
                        {
                            entryStream.CopyTo(copyStream);
                            Encoding.UTF8.GetString(copyStream.ToArray()).Should().Be(AsiceConstants.ContentTypeASiCe);
                        }

                    // Verifies that a CADES manifest has been generated
                    using (var entryStream = zippedArchive.GetEntry(AsiceConstants.CadesManifestFilename).Open())
                        using (var copyStream = new MemoryStream())
                        {
                            entryStream.CopyTo(copyStream);

                            var manifestXml = Encoding.UTF8.GetString(copyStream.ToArray());
                            manifestXml.Should().NotBeNull();
                            this.log.Info($"Manifest: {manifestXml}");
                        }

                    var signatureFile = zippedArchive.Entries
                                        .First(e => e.FullName.StartsWith("META-INF", StringComparison.CurrentCulture) &&
                                               e.FullName.EndsWith(".p7s", StringComparison.CurrentCulture));
                    signatureFile.Should().NotBeNull();

                    // Verifies the signature file
                    using (var entryStream = signatureFile.Open())
                        using (var copyStream = new MemoryStream())
                        {
                            entryStream.CopyTo(copyStream);
                            var signatureContent = copyStream.ToArray();
                            signatureContent.Should().HaveCountGreaterThan(0);
                        }
                }

            var tempFileName = Path.GetTempFileName();

            using (var zippedStream = new MemoryStream(zippedData))
                using (var outputFileStream = File.OpenWrite(tempFileName))
                {
                    zippedStream.CopyTo(outputFileStream);
                }

            this.log.Info($"Wrote package to '{tempFileName}'");
        }