Skip to content

Commit 7c13fd0

Browse files
committed
Task-929640- committed the sample
1 parent 0a31511 commit 7c13fd0

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Extract-PDF-Content-and-Certificate-Metadata-from-p7m-Files/Extract-PDF-Content-and-Certificate-Metadata-from-p7m-Files.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Extract_PDF_Content_and_Certificate_Metadata_from_p7m_Files</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
13+
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="9.0.10" />
14+
</ItemGroup>
15+
16+
</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 ConsoleApp
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("../../../TestCert.pdf.p7s");
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("D://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)