Пример #1
0
        private SignaturePermissions GetSignatureInfo(AcroFields fields, string name, SignaturePermissions perms, List <SignatureInfo> signatureInfoList)
        {
            var si = new SignatureInfo();

            PdfPKCS7 pkcs7 = fields.VerifySignature(name);

            X509Certificate cert    = pkcs7.SigningCertificate;
            PdfDictionary   sigDict = fields.GetSignatureDictionary(name);
            PdfString       contact = sigDict.GetAsString(PdfName.CONTACTINFO);

            si.Signer = CertificateInfo.GetSubjectFields(cert).GetField("CN");

            if (contact != null)
            {
                si.ContactInfo = contact.ToString();
            }

            si.SignedOn       = pkcs7.SignDate;
            si.Location       = pkcs7.Location;
            si.Issuer         = cert.IssuerDN.ToString();
            si.Subject        = cert.SubjectDN.ToString();
            si.CertValidFrom  = cert.NotBefore;
            si.CertValidTo    = cert.NotAfter;
            si.Reason         = pkcs7.Reason;
            si.IntegrityCheck = pkcs7.Verify();
            signatureInfoList.Add(si);

            perms = new SignaturePermissions(sigDict, perms);

            return(perms);
        }
Пример #2
0
        public SignaturePermissions InspectSignature(AcroFields fields, String name, SignaturePermissions perms)
        {
            IList <AcroFields.FieldPosition> fps = fields.GetFieldPositions(name);

            if (fps != null && fps.Count > 0)
            {
                AcroFields.FieldPosition fp = fps[0];
                Rectangle pos = fp.position;
                if (pos.Width == 0 || pos.Height == 0)
                {
                    Console.WriteLine("Invisible signature");
                }
                else
                {
                    Console.WriteLine("Field on page {0}; llx: {1}, lly: {2}, urx: {3}; ury: {4}",
                                      fp.page, pos.Left, pos.Bottom, pos.Right, pos.Top);
                }
            }

            PdfPKCS7 pkcs7 = VerifySignature(fields, name);

            Console.WriteLine("Digest algorithm: " + pkcs7.GetHashAlgorithm());
            Console.WriteLine("Encryption algorithm: " + pkcs7.GetEncryptionAlgorithm());
            Console.WriteLine("Filter subtype: " + pkcs7.GetFilterSubtype());
            X509Certificate cert = pkcs7.SigningCertificate;

            Console.WriteLine("Name of the signer: " + CertificateInfo.GetSubjectFields(cert).GetField("CN"));
            if (pkcs7.SignName != null)
            {
                Console.WriteLine("Alternative name of the signer: " + pkcs7.SignName);
            }

            Console.WriteLine("Signed on: " + pkcs7.SignDate.ToString("yyyy-MM-dd HH:mm:ss.ff"));
            if (!pkcs7.TimeStampDate.Equals(DateTime.MaxValue))
            {
                Console.WriteLine("TimeStamp: " + pkcs7.TimeStampDate.ToString("yyyy-MM-dd HH:mm:ss.ff"));
                TimeStampToken ts = pkcs7.TimeStampToken;
                Console.WriteLine("TimeStamp service: " + ts.TimeStampInfo.Tsa);
                Console.WriteLine("Timestamp verified? " + pkcs7.VerifyTimestampImprint());
            }
            Console.WriteLine("Location: " + pkcs7.Location);
            Console.WriteLine("Reason: " + pkcs7.Reason);
            PdfDictionary sigDict = fields.GetSignatureDictionary(name);
            PdfString     contact = sigDict.GetAsString(PdfName.CONTACTINFO);

            if (contact != null)
            {
                Console.WriteLine("Contact info: " + contact);
            }
            perms = new SignaturePermissions(sigDict, perms);
            Console.WriteLine("Signature type: " + (perms.Certification ? "certification" : "approval"));
            Console.WriteLine("Filling out fields allowed: " + perms.FillInAllowed);
            Console.WriteLine("Adding annotations allowed: " + perms.AnnotationsAllowed);
            foreach (SignaturePermissions.FieldLock Lock in perms.FieldLocks)
            {
                Console.WriteLine("Lock: " + Lock);
            }
            return(perms);
        }
Пример #3
0
        /**
         * Signs a PDF where space was already reserved.
         * @param reader the original PDF
         * @param fieldName the field to sign. It must be the last field
         * @param outs the output PDF
         * @param externalSignatureContainer the signature container doing the actual signing. Only the
         * method ExternalSignatureContainer.sign is used
         * @throws DocumentException
         * @throws IOException
         * @throws GeneralSecurityException
         */
        public static void SignDeferred(PdfReader reader, String fieldName, Stream outs, IExternalSignatureContainer externalSignatureContainer)
        {
            AcroFields    af = reader.AcroFields;
            PdfDictionary v  = af.GetSignatureDictionary(fieldName);

            if (v == null)
            {
                throw new DocumentException("No field");
            }
            if (!af.SignatureCoversWholeDocument(fieldName))
            {
                throw new DocumentException("Not the last signature");
            }
            PdfArray b = v.GetAsArray(PdfName.BYTERANGE);

            long[] gaps = b.AsLongArray();
            if (b.Size != 4 || gaps[0] != 0)
            {
                throw new DocumentException("Single exclusion space supported");
            }
            IRandomAccessSource readerSource = reader.SafeFile.CreateSourceView();
            Stream rg = new RASInputStream(new RandomAccessSourceFactory().CreateRanged(readerSource, gaps));

            byte[] signedContent  = externalSignatureContainer.Sign(rg);
            int    spaceAvailable = (int)(gaps[2] - gaps[1]) - 2;

            if ((spaceAvailable & 1) != 0)
            {
                throw new DocumentException("Gap is not a multiple of 2");
            }
            spaceAvailable /= 2;
            if (spaceAvailable < signedContent.Length)
            {
                throw new DocumentException("Not enough space");
            }
            StreamUtil.CopyBytes(readerSource, 0, gaps[1] + 1, outs);
            ByteBuffer bb = new ByteBuffer(spaceAvailable * 2);

            foreach (byte bi in signedContent)
            {
                bb.AppendHex(bi);
            }
            int remain = (spaceAvailable - signedContent.Length) * 2;

            for (int k = 0; k < remain; ++k)
            {
                bb.Append((byte)48);
            }
            bb.WriteTo(outs);
            StreamUtil.CopyBytes(readerSource, gaps[2] - 1, gaps[3] + 1, outs);
        }
        /**
         * Call this method to have LTV information added to the {@link PdfStamper}
         * given in the constructor.
         */
        public void enable(IOcspClient ocspClient, ICrlClient crlClient)
        {
            AcroFields fields    = pdfStamper.AcroFields;
            bool       encrypted = pdfStamper.Reader.IsEncrypted();

            List <String> names = fields.GetSignatureNames();

            foreach (String name in names)
            {
                PdfPKCS7        pdfPKCS7            = fields.VerifySignature(name);
                PdfDictionary   signatureDictionary = fields.GetSignatureDictionary(name);
                X509Certificate certificate         = pdfPKCS7.SigningCertificate;
                addLtvForChain(certificate, ocspClient, crlClient, getSignatureHashKey(signatureDictionary, encrypted));
            }

            outputDss();
        }
Пример #5
0
        private static MessageReport.Signature InspectSignature(AcroFields fields, String name, SignaturePermissions perms)
        {
            MessageReport.Signature sigInfo = new MessageReport.Signature();

            IList <AcroFields.FieldPosition> fps = fields.GetFieldPositions(name);

            if (fps != null && fps.Count > 0)
            {
                AcroFields.FieldPosition fp = fps[0];
                Rectangle pos = fp.position;
                if (pos.Width == 0 || pos.Height == 0)
                {
                    sigInfo.visible = false;
                }
                else
                {
                    sigInfo.visible = true;
                }
            }

            PdfPKCS7 pkcs7 = VerifySignature(fields, name, ref sigInfo);

            sigInfo.digestAlgorithm     = pkcs7.GetHashAlgorithm();
            sigInfo.encryptionAlgorithm = pkcs7.GetEncryptionAlgorithm();
            sigInfo.isRevocationValid   = pkcs7.IsRevocationValid();


            X509Certificate cert = pkcs7.SigningCertificate;

            sigInfo.signerName = CertificateInfo.GetSubjectFields(cert).GetField("CN");

            if (pkcs7.SignName != null)
            {
                sigInfo.signerName = pkcs7.SignName;
            }

            sigInfo.signDate = pkcs7.SignDate.ToString("yyyy-MM-dd HH:mm:ss.ff");

            if (!pkcs7.TimeStampDate.Equals(DateTime.MaxValue))
            {
                sigInfo.isTimestampped = true;
                sigInfo.timestampDate  = pkcs7.TimeStampDate.ToString("yyyy-MM-dd HH:mm:ss.ff");

                TimeStampToken ts = pkcs7.TimeStampToken;
                sigInfo.timestampName = ts.TimeStampInfo.Tsa.ToString();
            }

            sigInfo.signLocation = pkcs7.Location;
            sigInfo.signReason   = pkcs7.Reason;

            PdfDictionary sigDict = fields.GetSignatureDictionary(name);
            PdfString     contact = sigDict.GetAsString(PdfName.CONTACTINFO);

            if (contact != null)
            {
                Console.WriteLine("Contact info: " + contact);
            }
            perms = new SignaturePermissions(sigDict, perms);

            sigInfo.signatureType = (perms.Certification ? "certification" : "approval");


            return(sigInfo);
        }