Digital Signature using Any DSC Token in C#

Introduction 

 
This blog will give you a brief on how to create a Digital Signature using C# and a desktop executable application.
 
The Digital Signature will be performed at the client system. The Digital Signature token (DSC Token) is installed in the client local system.
 

Why use an executable file?

 
We are unable to read the client-side digital signature tokens from web applications due to security concerns, and all other possibilities are blocked. Therefore, we are proceeding with a local windows application (i.e. the Executable file (.exe)).
 
Check the below mentioned references to proceed:
  1. using iTextSharp.text.pdf;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using System.Security.Cryptography.X509Certificates;  
  6. using Org.BouncyCastle.Security;  
  7. using Org.BouncyCastle.X509;  
  8. using X509Certificate = Org.BouncyCastle.X509.X509Certificate;  
  9. using iTextSharp.text.pdf.security;  
All the above-mentioned references are used for accessing DSC, Signing DSC, Reading PDF and Creating new signed PDF.
 
Create a variable for X509Certificate2 
  1. X509Certificate2 certClient = null;   
Get all the DSC users registered to local store and for current user using X509Store, PFB 
  1. X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);  
 X509Store "st" will collect all the certificates
  1. st.Open(OpenFlags.MaxAllowed); 
  2. X509Certificate2Collection collection = st.Certificates; 
Collection will get all the certificates.
 
Select the certificate.
 
If we have multiple certificates registered in local, and if we want to pick the valid certificates using the name, please follow the below process:
 
Send "Name" as the DSC token owner name. You can check if the DSC token is expired or not. After all these checks, assign the value to "certClient".
  1. for (int i = 0; i < collection.Count; i++)  
  2. {                 
  3.     foreach (X509Certificate2 cert in collection)  
  4.     {  
  5.         certClient = cert;  
  6.         username = certClient.Subject;  
  7.         ErrorLogs(username);  
  8.         startdate = certClient.GetEffectiveDateString();  
  9.         enddate = certClient.GetExpirationDateString();  
  10.         if (collection[i].Subject.Contains("Name"))  
  11.         {  
  12.             certClient = collection[i];  
  13.         }  
  14.     }  
  15. }  
  16. st.Close();  
Close the X509Store. 
  1. //Get Certificate Chain    
  2. IList<X509Certificate> chain = new List<X509Certificate>();    
  3. X509Chain x509Chain = new X509Chain();    
  4. x509Chain.Build(certClient);    
  5. foreach (X509ChainElement x509ChainElement in x509Chain.ChainElements)  
  6. {  
  7.     chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));  
  8. }
Select the file to be signed (input file)
  1. string filename = @"C:\Users\admin\Desktop\sample.pdf";  
Assign the file to a PDF reader to edit the file.
  1. PdfReader inputPdf = new PdfReader(filename);  
Create a new file to save the signed pdf.
  1. FileStream signedPdf = new FileStream(@"C:\Users\admin\Desktop\Sample_signed.pdf", FileMode.Create);  
PdfStamper will create the stamp on the "inputPdf" and create a new "signedPdf"
  1. PdfStamper pdfStamper = PdfStamper.CreateSignature(inputPdf, signedPdf, '\0');  
Signature is encrypted using SHA-256 
  1. IExternalSignature externalSignature = new X509Certificate2Signature(certClient, "SHA-256");  
 This will create a signature text in the input pdf
  1. PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;  
Other information can be added and displayed in the signature section with signature.
  1. signatureAppearance.Reason = "My Signature";  
  2. signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(0, 00, 200, 100), inputPdf.NumberOfPages, "Signature");  
  3. signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;                            
  4. MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, nullnullnull, 0,CryptoStandard.CMS);  
PdfReader and pdfStamper will be closed after the signature is done.
  1. inputPdf.Close();  
  2. pdfStamper.Close();  
Above details are verified and working.