コード例 #1
0
        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();
        }
        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));
        }
コード例 #3
0
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //Get the template PDF file stream from assembly.
            Stream documentStream = typeof(DigitalSignature).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.digital_signature_template.pdf");
            //Load the PDF document from stream.
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream);
            //Get the .pfx certificate file stream.
            Stream certificateStream = typeof(DigitalSignature).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.certificate.pfx");
            //Get the signature field to add digital signature.
            PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[6] as PdfLoadedSignatureField;
            //Get the signature bounds.
            RectangleF bounds = signatureField.Bounds;

            //Create PDF certificate using certificate stream and password.
            PdfCertificate pdfCertificate = new PdfCertificate(certificateStream, "password123");

            //Add certificate to first page of the document.
            PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], pdfCertificate, "", signatureField);

            signature.ContactInfo  = "*****@*****.**";
            signature.LocationInfo = "Honolulu, Hawaii";
            signature.Reason       = "I am author of this document.";

            //Set the cryptographic standard to signature settings.
            if ((bool)cms.IsChecked)
            {
                signature.Settings.CryptographicStandard = CryptographicStandard.CMS;
            }
            else
            {
                signature.Settings.CryptographicStandard = CryptographicStandard.CADES;
            }

            //Set the digest algorithm to signature settings.
            if ((bool)sha1.IsChecked)
            {
                signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1;
            }
            else if ((bool)sha256.IsChecked)
            {
                signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256;
            }
            else if ((bool)sha384.IsChecked)
            {
                signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA384;
            }
            else if ((bool)sha512.IsChecked)
            {
                signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA512;
            }

            //Get the signature field appearance graphics.
            PdfGraphics graphics = signature.Appearance.Normal.Graphics;

            if (graphics != null)
            {
                //Draw the rectangle in appearance graphics.
                graphics.DrawRectangle(PdfPens.Black, bounds);

                //Get the image file stream from assembly.
                Stream imageStream = typeof(DigitalSignature).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.signature.png");
                //Create the PDF bitmap image from stream.
                PdfBitmap bitmap = new PdfBitmap(imageStream, true);
                //Draw image to appearance graphics.
                graphics.DrawImage(bitmap, new RectangleF(2, 1, 30, 30));

                //Get certificate subject name.
                string subject = pdfCertificate.SubjectName;

                //Create a new font instance and draw a text to appearance graphics.
                PdfFont         font     = new PdfStandardFont(PdfFontFamily.Helvetica, 7);
                RectangleF      textRect = new RectangleF(45, 0, bounds.Width - 45, bounds.Height);
                PdfStringFormat format   = new PdfStringFormat(PdfTextAlignment.Justify);
                graphics.DrawString("Digitally signed by " + subject + " \r\nReason: Testing signature \r\nLocation: USA", font, PdfBrushes.Black, textRect, format);
            }

            //Set the digital signature to signing the field.
            signatureField.Signature = signature;

            //Creating the stream object.
            using (MemoryStream stream = new MemoryStream())
            {
                //Save and close the document.
                loadedDocument.Save(stream);
                loadedDocument.Close();

                stream.Position = 0;
                //Save the output stream as a file using file picker.
                PdfUtil.Save("DigitalSignature.pdf", stream);
            }
        }