public void CreateCadesManifest() { var cadesManifestCreator = CadesManifestCreator.CreateWithoutSignatureFile(); var digestAlgorithm = MessageDigestAlgorithm.SHA256Desig; var fileEntry = new AsicePackageEntry("my.pdf", MimeType.ForString("application/pdf"), digestAlgorithm); fileEntry.Digest = new DigestContainer(new byte[] { 0, 0, 1 }, digestAlgorithm); var entries = new[] { fileEntry }; var manifest = cadesManifestCreator.CreateManifest(entries); manifest.Should().NotBeNull() .And .BeOfType <ManifestContainer>(); manifest.Data.Should().NotBeNull(); manifest.FileName.Should().Be(AsiceConstants.CadesManifestFilename); var xmlManifest = DeserializeManifest(manifest.Data.ToArray()); xmlManifest.Should().NotBeNull(); xmlManifest.SigReference.Should().BeNull(); xmlManifest.DataObjectReference.Should().HaveCount(1); var dataObjectRef = xmlManifest.DataObjectReference[0]; dataObjectRef.Should().NotBeNull(); dataObjectRef.MimeType.Should().Be(fileEntry.Type.ToString()); dataObjectRef.DigestValue.Should().Equal(fileEntry.Digest.GetDigest()); dataObjectRef.URI.Should().Be(fileEntry.FileName); }
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(); } } }
public void CreateCadesManifestIncludingSignature() { var cadesManifestCreator = CadesManifestCreator.CreateWithSignatureFile(); var fileEntry = new AsicePackageEntry("my.pdf", MimeType.ForString("application/pdf"), MessageDigestAlgorithm.SHA256); fileEntry.Digest = new DigestContainer(new byte[] { 0, 0, 1 }, MessageDigestAlgorithm.SHA256); var manifest = cadesManifestCreator.CreateManifest(new[] { fileEntry }); manifest.Should().NotBeNull() .And .BeOfType <ManifestContainer>(); manifest.FileName.Should().Be(AsiceConstants.CadesManifestFilename); var xmlManifest = DeserializeManifest(manifest.Data.ToArray()); xmlManifest.Should().NotBeNull(); xmlManifest.SigReference.Should().NotBeNull(); xmlManifest.SigReference.MimeType.Should().Be(AsiceConstants.ContentTypeSignature); xmlManifest.DataObjectReference.Should().HaveCount(1); }
public void CreateCadesManifestIncludingSignature() { var cadesManifestCreator = CadesManifestCreator.CreateWithSignatureFile(); var fileEntry = new AsicePackageEntry("P.00987654321.001.P001.65013_File1.xml", MimeType.ForString("application/xml"), MessageDigestAlgorithm.SHA256Enc); fileEntry.Digest = new DigestContainer(new byte[] { 0, 0, 1 }, MessageDigestAlgorithm.SHA256Enc); var fileEntry2 = new AsicePackageEntry("ApprovalData/ApprovalData1.xml", MimeType.ForString("application/xml"), MessageDigestAlgorithm.SHA256Desig); fileEntry2.Digest = new DigestContainer(new byte[] { 0, 0, 1 }, MessageDigestAlgorithm.SHA256Desig); var manifest = cadesManifestCreator.CreateManifest(new[] { fileEntry, fileEntry2 }); manifest.Should().NotBeNull() .And .BeOfType <ManifestContainer>(); manifest.FileName.Should().Be(AsiceConstants.CadesManifestFilename); File.WriteAllBytes(@"c:\temp\manifest.xml", manifest.Data.ToArray()); var xmlManifest = DeserializeManifest(manifest.Data.ToArray()); xmlManifest.Should().NotBeNull(); xmlManifest.SigReference.Should().NotBeNull(); xmlManifest.SigReference.MimeType.Should().Be(AsiceConstants.ContentTypeSignature); xmlManifest.DataObjectReference.Should().HaveCount(2); }
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}'"); }
public static MimeType ExtractMimeType(string fileName) { var file = fileName ?? throw new ArgumentNullException(nameof(fileName)); return(MimeType.ForString(ExtractType(file))); }