33using System . Security . Cryptography . X509Certificates ;
44using System . Text ;
55
6- string dataPath = Path . GetFullPath ( @"Data/" ) ;
7-
8- // Load signed PDF document
6+ //Open the signed PDF document.
97using PdfLoadedDocument ldoc = new PdfLoadedDocument ( Path . GetFullPath ( @"Data/Input.pdf" ) ) ;
10-
11- // Get signature field
8+ //Retrieve the signature field from the document form.
129PdfLoadedSignatureField lSigFld = ldoc . Form . Fields [ 0 ] as PdfLoadedSignatureField ;
13-
14- // Root and Intermediate Certificates
10+ //Create a collection to store the root and intermediate certificates.
1511X509CertificateCollection collection = new X509CertificateCollection ( ) ;
16-
1712string [ ] certificates =
1813{
1914 "Root.cer" ,
2015 "Intermediate0.cer" ,
2116 "Intermediate1.cer"
2217} ;
23-
18+ //Load certificates and add them to the certificate collection.
2419foreach ( string certFile in certificates )
2520{
26- byte [ ] certData = File . ReadAllBytes ( Path . Combine ( dataPath , certFile ) ) ;
27-
28- X509Certificate2 certificate = new X509Certificate2 ( certData ) ;
29-
30- collection . Add ( certificate ) ;
21+ collection . Add ( new X509Certificate2 ( File . ReadAllBytes ( Path . GetFullPath ( @"Data/" + certFile ) ) ) ) ;
3122}
32-
33- // Validate signature
23+ //Verify the digital signature using the certificate chain.
3424PdfSignatureValidationResult result = lSigFld . ValidateSignature ( collection ) ;
35-
3625StringBuilder builder = new StringBuilder ( ) ;
37-
3826builder . AppendLine ( "Signature is " + result . SignatureStatus ) ;
3927builder . AppendLine ( ) ;
4028builder . AppendLine ( "----------Validation Summary----------" ) ;
4129builder . AppendLine ( ) ;
42-
43- // Modified check
30+ //Check whether the signed document has been modified.
4431if ( result . IsDocumentModified )
4532{
4633 builder . AppendLine ( "The document has been altered or corrupted since the signature was applied." ) ;
4936{
5037 builder . AppendLine ( "The document has not been modified since the signature was applied." ) ;
5138}
52-
53- // Certificate details
39+ //Add signer certificate information to the report.
5440builder . AppendLine ( "Digitally signed by: " + lSigFld . Signature . Certificate . IssuerName ) ;
55-
5641builder . AppendLine ( "Valid From: " + lSigFld . Signature . Certificate . ValidFrom ) ;
57-
5842builder . AppendLine ( "Valid To: " + lSigFld . Signature . Certificate . ValidTo ) ;
59-
6043builder . AppendLine ( "Signature Algorithm: " + result . SignatureAlgorithm ) ;
61-
6244builder . AppendLine ( "Hash Algorithm: " + result . DigestAlgorithm ) ;
63-
64- // Revocation details
45+ //Include revocation status details.
6546builder . AppendLine ( "OCSP Revocation Status: " + result . RevocationResult . OcspRevocationStatus ) ;
66-
6747if ( result . RevocationResult . OcspRevocationStatus == RevocationStatus . None && result . RevocationResult . IsRevokedCRL )
6848{
6949 builder . AppendLine ( "CRL is revoked." ) ;
7050}
71-
7251builder . AppendLine ( ) ;
7352builder . AppendLine ( "--------Revocation Information---------" ) ;
7453builder . AppendLine ( ) ;
75-
54+ //Extract OCSP and CRL validation details for each signer certificate.
7655foreach ( PdfSignerCertificate signerCertificate in result . SignerCertificates )
7756{
7857 if ( signerCertificate . OcspCertificate != null )
7958 {
8059 builder . AppendLine ( "------------OCSP Certificate-------------" ) ;
81-
8260 foreach ( X509Certificate2 item in signerCertificate . OcspCertificate . Certificates )
8361 {
8462 builder . AppendLine ( "The OCSP Response was signed by: " + item . SubjectName . Name ) ;
8563 }
86-
8764 builder . AppendLine ( "Is Embedded: " + signerCertificate . OcspCertificate . IsEmbedded ) ;
88-
8965 builder . AppendLine ( "Valid From: " + signerCertificate . OcspCertificate . ValidFrom ) ;
90-
9166 builder . AppendLine ( "Valid To: " + signerCertificate . OcspCertificate . ValidTo ) ;
92-
9367 builder . AppendLine ( ) ;
9468 }
9569
9670 if ( signerCertificate . CrlCertificate != null )
9771 {
9872 builder . AppendLine ( "------------CRL Certificate--------------" ) ;
99-
10073 foreach ( X509Certificate2 item in signerCertificate . CrlCertificate . Certificates )
10174 {
10275 builder . AppendLine ( "The CRL was signed by: " + item . SubjectName . Name ) ;
10376 }
104-
10577 builder . AppendLine ( "Is Embedded: " + signerCertificate . CrlCertificate . IsEmbedded ) ;
10678 builder . AppendLine ( "Valid From: " + signerCertificate . CrlCertificate . ValidFrom ) ;
10779 builder . AppendLine ( "Valid To: " + signerCertificate . CrlCertificate . ValidTo ) ;
10880 builder . AppendLine ( ) ;
10981 }
11082}
111- //Save to file
112- File . WriteAllText ( Path . GetFullPath ( @"Output/Output.txt" ) , builder . ToString ( ) ) ;
83+ //Write the validation report to a text file.
84+ File . WriteAllText ( Path . GetFullPath ( @"Output/Output.txt" ) , builder . ToString ( ) ) ;
0 commit comments