VintaSoft Imaging .NET SDK v8.7
Vintasoft.Imaging.Pdf.Tree.DigitalSignatures Namespace / PdfSignatureInformation Class
Members Example



In This Topic
    PdfSignatureInformation Class
    In This Topic
    Contains information about a digital signature.
    Object Model
    PdfSignatureInformation ClassPdfBasicObject ClassPdfDocument ClassPdfIndirectReference ClassPdfDocumentRevision Class
    Syntax
    'Declaration
     
    Public Class PdfSignatureInformation 
       Inherits Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase
    public class PdfSignatureInformation : Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase 
    public __gc class PdfSignatureInformation : public Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase 
    public ref class PdfSignatureInformation : public Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase 
    Remarks
    To verify signature need create PKCS signature object (GetSignature) and use PdfPkcsSignature.Verify method.
    Example
    This example shows how to display an information about the digital signatures and verify the digital signatures of PDF document.
    ''' <summary>
    ''' Displays and verifies signatures of PDF document.
    ''' </summary>
    ''' <param name="pdfFilename">The filename of PDF document.</param>
    Public Shared Sub VerifyDocumentSignatures(pdfFilename As String)
    	' open PDF document
    	Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
    		' if document does not have interactive form
    		If document.InteractiveForm Is Nothing Then
    			System.Console.WriteLine("Signature fields are not found.")
    			Return
    		End If
                
    		' get an array of signature fields of document
    		Dim signatureFields As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormSignatureField() = document.InteractiveForm.GetSignatureFields()
    		' if document does not have signature fields
    		If signatureFields.Length = 0 Then
    			System.Console.WriteLine("Signture fields are not found.")
    			Return
    		End If
                
    		' for each signature field
    		For i As Integer = 0 To signatureFields.Length - 1
    			' get reference to the signature field
    			Dim signatureField As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormSignatureField = signatureFields(i)
    			' print signature field name
    			System.Console.WriteLine(String.Format("[{0}]Signaure field: {1}", i + 1, signatureField.FullyQualifiedName))
                
    			' get information about signature
    			Dim signatureInfo As Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfSignatureInformation = signatureField.SignatureInfo
    			' if signature information is empty
    			If signatureInfo Is Nothing Then
    				System.Console.WriteLine("Empty signature field.")
    			Else
    				' if signature information is NOT empty
    				' print signature filter
    				System.Console.WriteLine(String.Format("Filter      : {0} ({1})", signatureInfo.Filter, signatureInfo.SubFilter))
    				' print the signer name
    				If signatureInfo.SignerName IsNot Nothing Then
    					System.Console.WriteLine(String.Format("Signed by   : {0}", signatureInfo.SignerName))
    				End If
    				' print the signature reason
    				If signatureInfo.Reason IsNot Nothing Then
    					System.Console.WriteLine(String.Format("Reason      : {0}", signatureInfo.Reason))
    				End If
    				' print the signature location
    				If signatureInfo.Location IsNot Nothing Then
    					System.Console.WriteLine(String.Format("Location    : {0}", signatureInfo.Location))
    				End If
    				' print the signer contact info
    				If signatureInfo.ContactInfo IsNot Nothing Then
    					System.Console.WriteLine(String.Format("Contact Info: {0}", signatureInfo.SignerName))
    				End If
    				' print the signing date
    				If signatureInfo.SigningTime <> System.DateTime.MinValue Then
    					System.Console.WriteLine(String.Format("Signig Date : {0}", signatureInfo.SigningTime.ToString("f")))
    				End If
                
    				' get PKCS signature
    				Dim [error] As Boolean = False
    				Dim signature As Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfPkcsSignature = Nothing
    				Try
    					signature = signatureInfo.GetSignature()
    				Catch e As System.Exception
    					[error] = True
    					System.Console.WriteLine("PKCS signature parsing error: {0}", e.Message)
    				End Try
    				If [error] Then
    					Continue For
    				End If
                
    				' print name of signature algorithm
    				System.Console.WriteLine(String.Format("Algorithm   : {0}", signature.SignatureAlgorithmName))
                
    				' print information about signature certificate chain
    				System.Console.WriteLine("Sign certificate chain:")
    				Dim signCertChain As System.Security.Cryptography.X509Certificates.X509Certificate2() = signature.SigningCertificateChain
    				Dim padding As String = ""
    				For Each cert As System.Security.Cryptography.X509Certificates.X509Certificate2 In signCertChain
    					padding += "    "
    					System.Console.WriteLine("{0}Serial number: {1}", padding, cert.SerialNumber)
    					System.Console.WriteLine("{0}Issuer       : {1}", padding, cert.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, True))
    					System.Console.WriteLine("{0}Subject      : {1}", padding, cert.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, False))
    				Next
                
                
    				' verify digital signature
    				VerifyDigitalSignature(signatureInfo, signature)
    			End If
    			System.Console.WriteLine()
    		Next
    	End Using
    End Sub
                
    ''' <summary>
    ''' Verifies the digital signature.
    ''' </summary>
    ''' <param name="signature">The signature.</param>
    ''' <param name="signatureInfo">The signature information.</param>
    ''' <returns><b>true</b> if signature is valid; otherwise, <b>false</b>.</returns>
    Public Shared Function VerifyDigitalSignature(signatureInfo As Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfSignatureInformation, signature As Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfPkcsSignature) As Boolean
    	System.Console.WriteLine("Verifying signature...")
                
    	Dim signatureVerifyResult As Boolean = False
    	Dim certificateVerifyResult As Boolean = False
    	Dim signatureCoversWholeDocument As Boolean = False
    	Dim chain As System.Security.Cryptography.X509Certificates.X509Chain = Nothing
                
                
    	' verify signature
                
    	Try
    		' check that signature covers the whole document
    		signatureCoversWholeDocument = signatureInfo.SignatureCoversWholeDocument()
                
    		' verify PKCS signature
    		signatureVerifyResult = signature.Verify()
                
    		' build and verify certificate chain
    		Dim certificate As System.Security.Cryptography.X509Certificates.X509Certificate2 = signature.SigningCertificate
    		chain = New System.Security.Cryptography.X509Certificates.X509Chain()
    		certificateVerifyResult = chain.Build(certificate)
    	Catch verificationException As System.Exception
    		System.Console.WriteLine("Verification failed: {0}", verificationException.Message)
    		Return False
    	End Try
                
                
    	' print signature verification result
                
    	' if PKCS signature verification is failed OR
    	' signature does not cover the whole document
    	If Not signatureVerifyResult OrElse Not signatureCoversWholeDocument Then
    		System.Console.WriteLine("Signature is INVALID.")
    	' if certificate verification is failed
    	ElseIf Not certificateVerifyResult Then
    		System.Console.WriteLine("Signature validity is UNKNOWN.")
    	Else
    		System.Console.WriteLine("Signature is VALID.")
    	End If
                
                
    	' print signature verification details
                
    	' if signature verification is successful
    	If signatureVerifyResult Then
    		' if signature covers the whole document
    		If signatureCoversWholeDocument Then
    			System.Console.WriteLine("    Signature verification: Document has not been modified since this signature was applied.")
    		Else
    			' if signature does NOT cover the whole document
    			System.Console.WriteLine("    Signature verification: The revision of the document that was covered by this signature has not been altered; however, there have been subsequent changes to the document.")
    		End If
    	Else
    		' if signature verification is NOT successful
    		System.Console.WriteLine("    Signature verification: Document has been modified or corrupted since it was signed.")
    	End If
                
                
    	' print certeficate verification details
                
    	' if certificate chain is present
    	If chain IsNot Nothing Then
    		' if certificate verification is successful
    		If certificateVerifyResult Then
    			System.Console.WriteLine("    Certificate verification: Signer's certificate is valid.")
    		Else
    			' if certificate verification is NOT successful
    			' print certificate verification status
    			System.Console.WriteLine("    Certificate verification: Signer's certificate is invalid:")
    			For Each status As System.Security.Cryptography.X509Certificates.X509ChainStatus In chain.ChainStatus
    				System.Console.Write(String.Format("        {0}: {1}", status.Status, status.StatusInformation))
    			Next
    		End If
    	End If
                
    	Return signatureVerifyResult AndAlso signatureCoversWholeDocument AndAlso certificateVerifyResult
    End Function
    /// <summary>
    /// Displays and verifies signatures of PDF document.
    /// </summary>
    /// <param name="pdfFilename">The filename of PDF document.</param>
    public static void VerifyDocumentSignatures(string pdfFilename)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
        {
            // if document does not have interactive form
            if (document.InteractiveForm == null)
            {
                System.Console.WriteLine("Signature fields are not found.");
                return;
            }
                
            // get an array of signature fields of document
            Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormSignatureField[] signatureFields =
                document.InteractiveForm.GetSignatureFields();
            // if document does not have signature fields
            if (signatureFields.Length == 0)
            {
                System.Console.WriteLine("Signture fields are not found.");
                return;
            }
                
            // for each signature field
            for (int i = 0; i < signatureFields.Length; i++)
            {
                // get reference to the signature field
                Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormSignatureField signatureField = signatureFields[i];
                // print signature field name
                System.Console.WriteLine(string.Format("[{0}]Signaure field: {1}", i + 1, signatureField.FullyQualifiedName));
                
                // get information about signature
                Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfSignatureInformation signatureInfo = signatureField.SignatureInfo;
                // if signature information is empty
                if (signatureInfo == null)
                {
                    System.Console.WriteLine("Empty signature field.");
                }
                // if signature information is NOT empty
                else
                {
                    // print signature filter
                    System.Console.WriteLine(string.Format("Filter      : {0} ({1})", signatureInfo.Filter, signatureInfo.SubFilter));
                    // print the signer name
                    if (signatureInfo.SignerName != null)
                        System.Console.WriteLine(string.Format("Signed by   : {0}", signatureInfo.SignerName));
                    // print the signature reason
                    if (signatureInfo.Reason != null)
                        System.Console.WriteLine(string.Format("Reason      : {0}", signatureInfo.Reason));
                    // print the signature location
                    if (signatureInfo.Location != null)
                        System.Console.WriteLine(string.Format("Location    : {0}", signatureInfo.Location));
                    // print the signer contact info
                    if (signatureInfo.ContactInfo != null)
                        System.Console.WriteLine(string.Format("Contact Info: {0}", signatureInfo.SignerName));
                    // print the signing date
                    if (signatureInfo.SigningTime != System.DateTime.MinValue)
                        System.Console.WriteLine(string.Format("Signig Date : {0}", signatureInfo.SigningTime.ToString("f")));
                
                    // get PKCS signature
                    bool error = false;
                    Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfPkcsSignature signature = null;
                    try
                    {
                        signature = signatureInfo.GetSignature();
                    }
                    catch (System.Exception e)
                    {
                        error = true;
                        System.Console.WriteLine("PKCS signature parsing error: {0}", e.Message);
                    }
                    if (error)
                        continue;
                
                    // print name of signature algorithm
                    System.Console.WriteLine(string.Format("Algorithm   : {0}", signature.SignatureAlgorithmName));
                
                    // print information about signature certificate chain
                    System.Console.WriteLine("Sign certificate chain:");
                    System.Security.Cryptography.X509Certificates.X509Certificate2[] signCertChain = 
                        signature.SigningCertificateChain;
                    string padding = "";
                    foreach (System.Security.Cryptography.X509Certificates.X509Certificate2 cert in signCertChain)
                    {
                        padding += "    ";
                        System.Console.WriteLine("{0}Serial number: {1}", padding, cert.SerialNumber);
                        System.Console.WriteLine("{0}Issuer       : {1}", padding, cert.GetNameInfo(
                            System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, true));
                        System.Console.WriteLine("{0}Subject      : {1}", padding, cert.GetNameInfo(
                            System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, false));
                    }
                
                
                    // verify digital signature
                    VerifyDigitalSignature(signatureInfo, signature);
                }
                System.Console.WriteLine();
            }
        }
    }
                
    /// <summary>
    /// Verifies the digital signature.
    /// </summary>
    /// <param name="signature">The signature.</param>
    /// <param name="signatureInfo">The signature information.</param>
    /// <returns><b>true</b> if signature is valid; otherwise, <b>false</b>.</returns>
    public static bool VerifyDigitalSignature(
        Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfSignatureInformation signatureInfo,
        Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfPkcsSignature signature)
    {
        System.Console.WriteLine("Verifying signature...");
                
        bool signatureVerifyResult = false;
        bool certificateVerifyResult = false;
        bool signatureCoversWholeDocument = false;
        System.Security.Cryptography.X509Certificates.X509Chain chain = null;
                
                
        // verify signature
                
        try
        {
            // check that signature covers the whole document
            signatureCoversWholeDocument = signatureInfo.SignatureCoversWholeDocument();
                
            // verify PKCS signature
            signatureVerifyResult = signature.Verify();
                
            // build and verify certificate chain
            System.Security.Cryptography.X509Certificates.X509Certificate2 certificate = signature.SigningCertificate;
            chain = new System.Security.Cryptography.X509Certificates.X509Chain();
            certificateVerifyResult = chain.Build(certificate);
        }
        catch (System.Exception verificationException)
        {
            System.Console.WriteLine("Verification failed: {0}", verificationException.Message);
            return false;
        }
                
                
        // print signature verification result
                
        // if PKCS signature verification is failed OR
        // signature does not cover the whole document
        if (!signatureVerifyResult || !signatureCoversWholeDocument)
            System.Console.WriteLine("Signature is INVALID.");
        // if certificate verification is failed
        else if (!certificateVerifyResult)
            System.Console.WriteLine("Signature validity is UNKNOWN.");
        else
            System.Console.WriteLine("Signature is VALID.");
                
                
        // print signature verification details
                
        // if signature verification is successful
        if (signatureVerifyResult)
        {
            // if signature covers the whole document
            if (signatureCoversWholeDocument)
                System.Console.WriteLine("    Signature verification: Document has not been modified since this signature was applied.");
            // if signature does NOT cover the whole document
            else
                System.Console.WriteLine("    Signature verification: The revision of the document that was covered by this signature has not been altered; however, there have been subsequent changes to the document.");
        }
        // if signature verification is NOT successful
        else
        {
            System.Console.WriteLine("    Signature verification: Document has been modified or corrupted since it was signed.");
        }
                
                
        // print certeficate verification details
                
        // if certificate chain is present
        if (chain != null)
        {
            // if certificate verification is successful
            if (certificateVerifyResult)
            {
                System.Console.WriteLine("    Certificate verification: Signer's certificate is valid.");
            }
            // if certificate verification is NOT successful
            else
            {
                // print certificate verification status
                System.Console.WriteLine("    Certificate verification: Signer's certificate is invalid:");
                foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                    System.Console.Write(string.Format("        {0}: {1}", status.Status, status.StatusInformation));
            }
        }
                
        return signatureVerifyResult && signatureCoversWholeDocument && certificateVerifyResult;
    }
    Inheritance Hierarchy

    System.Object
       Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeBase
          Vintasoft.Imaging.Pdf.Tree.DigitalSignatures.PdfSignatureInformation

    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also