public virtual void UpdatePdfSignatureAppearance(IPdfPreSigningSession pdfPreSigningSession, PdfReader pdfReader, PdfSignatureAppearance pdfSignatureAppearance)
        {
            pdfSignatureAppearance.Location = pdfPreSigningSession.Location;
            pdfSignatureAppearance.Reason   = pdfPreSigningSession.Reason;

            pdfSignatureAppearance.CryptoDictionary = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED)
            {
                Reason   = pdfSignatureAppearance.Reason,
                Location = pdfSignatureAppearance.Location,
                Date     = new PdfDate(pdfSignatureAppearance.SignDate)
            };
        }
        private PdfPKCS7 GetPublicKeyManager(IPdfPreSigningSession sessionData)
        {
            var chain        = new X509Chain();
            var certificates = new List <Org.BouncyCastle.X509.X509Certificate>();

            chain.ChainPolicy.RevocationFlag    = X509RevocationFlag.EndCertificateOnly;
            chain.ChainPolicy.RevocationMode    = X509RevocationMode.NoCheck;
            chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
            chain.Build(sessionData.UserCertificate);

            foreach (var chainElement in chain.ChainElements)
            {
                certificates.Add(DotNetUtilities.FromX509Certificate(chainElement.Certificate));
            }

            return(new PdfPKCS7(null, certificates, sessionData.HashAlgorithm, false));
        }
        public virtual IAuthenticatedAttributes CreateAuthenticatedAttribute(IPdfPreSigningSession pdfPreSigningSession)
        {
            var pdfContentBytes = File.ReadAllBytes(pdfPreSigningSession.FilePath);

            using (var PdfReader = new PdfReader(pdfContentBytes))
            {
                using (var pdfStamper = PdfStamper.CreateSignature(PdfReader, null, '\0', pdfPreSigningSession.FilePath, true))
                {
                    var authenticatedAttributes = new AuthenticatedAttributes();
                    var publicKeyManager        = GetPublicKeyManager(pdfPreSigningSession);

                    authenticatedAttributes.PdfSignatureAppearance = pdfStamper.SignatureAppearance;

                    UpdatePdfSignatureAppearance(pdfPreSigningSession, PdfReader, authenticatedAttributes.PdfSignatureAppearance);

                    authenticatedAttributes
                    .PdfSignatureAppearance
                    .PreClose(new Dictionary <PdfName, int> {
                        { PdfName.CONTENTS, SIGNATURE_ESTIMATED_SIZE * 2 + 2 }
                    });

                    var pdfSignatureAppearanceData = authenticatedAttributes
                                                     .PdfSignatureAppearance
                                                     .GetRangeStream();

                    authenticatedAttributes.DigestedData = DigestAlgorithms
                                                           .Digest
                                                           (
                        pdfSignatureAppearanceData,
                        pdfPreSigningSession.HashAlgorithm
                                                           );

                    authenticatedAttributes.Value = publicKeyManager
                                                    .getAuthenticatedAttributeBytes
                                                    (
                        authenticatedAttributes.DigestedData,
                        null,
                        null,
                        CryptoStandard.CMS
                                                    );

                    return(authenticatedAttributes);
                }
            }
        }