public ActionResult DigitalSignatureValidation(string InsideBrowser) { SignatureValidationMesssage message = new SignatureValidationMesssage(); FileStream fileStreamInput = new FileStream(ResolveApplicationDataPath("DigitalSignature.pdf"), FileMode.Open, FileAccess.Read); //Load an existing signed PDF document PdfLoadedDocument ldoc = new PdfLoadedDocument(fileStreamInput); //Get signature field. PdfLoadedSignatureField lSigFld = ldoc.Form.Fields[0] as PdfLoadedSignatureField; //X509Certificate2Collection to check the signer's identity using root certificates. X509CertificateCollection collection = new X509CertificateCollection(); //Read the certificate file. FileStream pfxFile = new FileStream(ResolveApplicationDataPath(@"PDF.pfx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); byte[] data = new byte[pfxFile.Length]; pfxFile.Read(data, 0, data.Length); //Create new X509Certificate2 with the root certificate. X509Certificate2 certificate = new X509Certificate2(data, "password123"); //Add the certificate to the collection. collection.Add(certificate); //Validate signature and get the validation result PdfSignatureValidationResult result = lSigFld.ValidateSignature(collection); StringBuilder builder = new StringBuilder(); builder.AppendLine("Signature is " + result.SignatureStatus); builder.AppendLine(); builder.AppendLine("--------Validation Summary--------"); builder.AppendLine(); //Checks whether the document is modified or not bool isModified = result.IsDocumentModified; if (isModified) { builder.AppendLine("The document has been altered or corrupted since the signature was applied."); } else { builder.AppendLine("The document has not been modified since the signature was applied."); } //Signature details builder.AppendLine("Digitally signed by " + lSigFld.Signature.Certificate.IssuerName); builder.AppendLine("Valid From : " + lSigFld.Signature.Certificate.ValidFrom); builder.AppendLine("Valid To : " + lSigFld.Signature.Certificate.ValidTo); builder.AppendLine("Signature Algorithm : " + result.SignatureAlgorithm); builder.AppendLine("Hash Algorithm : " + result.DigestAlgorithm); //Revocation validation details builder.AppendLine("OCSP revocation status : " + result.RevocationResult.OcspRevocationStatus); if (result.RevocationResult.OcspRevocationStatus == RevocationStatus.None && result.RevocationResult.IsRevokedCRL) { builder.AppendLine("CRL is revoked."); } //Close the document ldoc.Close(true); message.Message = builder.ToString(); return(View("DigitalSignatureValidation", message)); }
void OnButtonClicked(object sender, EventArgs e) { label2.Hidden = false; StringBuilder builder = new StringBuilder(); Stream docStream = typeof(DigitalSignatureValidation).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets.SignedDocument.pdf"); //Load the PDF document into the loaded document object. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Get signature field PdfLoadedSignatureField lSigFld = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; //X509Certificate2Collection to check the signer's identity using root certificates X509CertificateCollection collection = new X509CertificateCollection(); //Get the certificate stream from .pfx file. Stream certificateStream = typeof(DigitalSignatureValidation).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets.PDF.pfx"); byte[] data = new byte[certificateStream.Length]; certificateStream.Read(data, 0, data.Length); //Create new X509Certificate2 with the root certificate X509Certificate2 certificate = new X509Certificate2(data, "password123"); //Add the certificate to the collection collection.Add(certificate); //Validate signature and get the validation result PdfSignatureValidationResult result = lSigFld.ValidateSignature(collection); builder.AppendLine("Signature is " + result.SignatureStatus); builder.AppendLine(); builder.AppendLine("--------Validation Summary--------"); builder.AppendLine(); //Checks whether the document is modified or not bool isModified = result.IsDocumentModified; if (isModified) { builder.AppendLine("The document has been altered or corrupted since the signature was applied."); } else { builder.AppendLine("The document has not been modified since the signature was applied."); } //Signature details builder.AppendLine("Digitally signed by " + lSigFld.Signature.Certificate.IssuerName); builder.AppendLine("Valid From : " + lSigFld.Signature.Certificate.ValidFrom); builder.AppendLine("Valid To : " + lSigFld.Signature.Certificate.ValidTo); builder.AppendLine("Signature Algorithm : " + result.SignatureAlgorithm); builder.AppendLine("Hash Algorithm : " + result.DigestAlgorithm); //Revocation validation details builder.AppendLine("OCSP revocation status : " + result.RevocationResult.OcspRevocationStatus); if (result.RevocationResult.OcspRevocationStatus == RevocationStatus.None && result.RevocationResult.IsRevokedCRL) { builder.AppendLine("CRL is revoked."); } //Close the PDF document loadedDocument.Close(true); label2.Text = builder.ToString(); }