diff --git a/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files.sln b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files.sln new file mode 100644 index 00000000..72ffeef4 --- /dev/null +++ b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36401.2 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "How-to-retrieve-signer-data-from-the-. p7m-files", "How-to-retrieve-signer-data-from-the-. p7m-files\How-to-retrieve-signer-data-from-the-. p7m-files.csproj", "{F0EB3137-9B02-4781-8770-250AAECA4DC6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F0EB3137-9B02-4781-8770-250AAECA4DC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0EB3137-9B02-4781-8770-250AAECA4DC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0EB3137-9B02-4781-8770-250AAECA4DC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0EB3137-9B02-4781-8770-250AAECA4DC6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4CB798B6-B529-4ED0-8F9E-C5F26579318D} + EndGlobalSection +EndGlobal diff --git a/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/Data/XCORE-DocumentoTest.pdf.p7m b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/Data/XCORE-DocumentoTest.pdf.p7m new file mode 100644 index 00000000..5e4cd0ba Binary files /dev/null and b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/Data/XCORE-DocumentoTest.pdf.p7m differ diff --git a/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files.csproj b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files.csproj new file mode 100644 index 00000000..5c7cfc79 --- /dev/null +++ b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + How_to_retrieve_signer_data_from_the_._p7m_files + enable + enable + + + + + + + diff --git a/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/Program.cs b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/Program.cs new file mode 100644 index 00000000..4cc47eed --- /dev/null +++ b/Digital Signature/How-to-retrieve-signer-data-from-the-. p7m-files/How-to-retrieve-signer-data-from-the-. p7m-files/Program.cs @@ -0,0 +1,59 @@ +using System.Security.Cryptography; +using System.Security.Cryptography.Pkcs; +using System.Security.Cryptography.X509Certificates; + +namespace How_to_retrieve_signer_data_from_the_. p7m_files +{ + internal class Program + { + static void Main(string[] args) + { + // Load the signed CMS (assumes signedCmsBytes contains the Signed CMS data) + byte[] signedCmsBytes = File.ReadAllBytes(Path.GetFullPath("Data/XCORE-DocumentoTest.pdf.p7m")); + + SignedCms signedCms = new SignedCms(); + + // Decode the Signed CMS data + signedCms.Decode(signedCmsBytes); + + // Verify the signature without considering the certificate chain + signedCms.CheckSignature(true); + + // Extract the original content + byte[] originalMessage = signedCms.ContentInfo.Content; + File.WriteAllBytes(Path.GetFullPath("Data/Decoded.pdf"), originalMessage); + + // Extract signer information + foreach (SignerInfo signerInfo in signedCms.SignerInfos) + { + // Get the signing certificate + X509Certificate2 signerCertificate = signerInfo.Certificate; + + // Extract signer's name + string signerName = signerCertificate?.Subject ?? "Unknown Signer"; + Console.WriteLine($"Signer Name: {signerName}"); + + // Extract signing date (signing time attribute) + Pkcs9SigningTime signingTime = null; + foreach (var data in from CryptographicAttributeObject attr in signerInfo.SignedAttributes + from AsnEncodedData data in attr.Values + where data is Pkcs9SigningTime + select data) + { + signingTime = (Pkcs9SigningTime)data; + break; + } + + if (signingTime != null) + { + Console.WriteLine($"Signing Time: {signingTime.SigningTime}"); + } + else + { + Console.WriteLine("Signing Time: Not available in the attributes."); + } + } + } + } + +}