Skip to content

Commit 0a31511

Browse files
committed
commit code samples
1 parent c41e62a commit 0a31511

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36401.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F0EB3137-9B02-4781-8770-250AAECA4DC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F0EB3137-9B02-4781-8770-250AAECA4DC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F0EB3137-9B02-4781-8770-250AAECA4DC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F0EB3137-9B02-4781-8770-250AAECA4DC6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {4CB798B6-B529-4ED0-8F9E-C5F26579318D}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>How_to_retrieve_signer_data_from_the_._p7m_files</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="9.0.8" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Security.Cryptography;
2+
using System.Security.Cryptography.Pkcs;
3+
using System.Security.Cryptography.X509Certificates;
4+
5+
namespace How_to_retrieve_signer_data_from_the_. p7m_files
6+
{
7+
internal class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
// Load the signed CMS (assumes signedCmsBytes contains the Signed CMS data)
12+
byte[] signedCmsBytes = File.ReadAllBytes(Path.GetFullPath("Data/XCORE-DocumentoTest.pdf.p7m"));
13+
14+
SignedCms signedCms = new SignedCms();
15+
16+
// Decode the Signed CMS data
17+
signedCms.Decode(signedCmsBytes);
18+
19+
// Verify the signature without considering the certificate chain
20+
signedCms.CheckSignature(true);
21+
22+
// Extract the original content
23+
byte[] originalMessage = signedCms.ContentInfo.Content;
24+
File.WriteAllBytes(Path.GetFullPath("Data/Decoded.pdf"), originalMessage);
25+
26+
// Extract signer information
27+
foreach (SignerInfo signerInfo in signedCms.SignerInfos)
28+
{
29+
// Get the signing certificate
30+
X509Certificate2 signerCertificate = signerInfo.Certificate;
31+
32+
// Extract signer's name
33+
string signerName = signerCertificate?.Subject ?? "Unknown Signer";
34+
Console.WriteLine($"Signer Name: {signerName}");
35+
36+
// Extract signing date (signing time attribute)
37+
Pkcs9SigningTime signingTime = null;
38+
foreach (var data in from CryptographicAttributeObject attr in signerInfo.SignedAttributes
39+
from AsnEncodedData data in attr.Values
40+
where data is Pkcs9SigningTime
41+
select data)
42+
{
43+
signingTime = (Pkcs9SigningTime)data;
44+
break;
45+
}
46+
47+
if (signingTime != null)
48+
{
49+
Console.WriteLine($"Signing Time: {signingTime.SigningTime}");
50+
}
51+
else
52+
{
53+
Console.WriteLine("Signing Time: Not available in the attributes.");
54+
}
55+
}
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)