Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>How_to_retrieve_signer_data_from_the_._p7m_files</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="9.0.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
}

}
Loading