Exemple #1
0
        public static bool validatePDF(string filename, ref MessageReport msg)
        {
            try
            {
                X509CertificateParser parser = new X509CertificateParser();

                if (certificates.Count() == 0)
                {
                    certificates.Add(parser.ReadCertificate(new FileStream(ROOT1, FileMode.Open)));
                    certificates.Add(parser.ReadCertificate(new FileStream(ROOT2, FileMode.Open)));
                    certificates.Add(parser.ReadCertificate(new FileStream(ROOT3, FileMode.Open)));
                }


                PdfReader pdfReader = new PdfReader(filename);

                AcroFields    acroFields     = pdfReader.AcroFields;
                List <String> signatureNames = acroFields.GetSignatureNames();

                msg.signaturesCount = signatureNames.Count();

                if (signatureNames.Count == 0)
                {
                    msg.StatusText = "Δεν βρέθηκαν ψηφιακές υπογραφές στο έγγραφο!";
                    return(false);
                }

                SignaturePermissions    perms   = null;
                MessageReport.Signature sigInfo = new MessageReport.Signature();


                foreach (String name in signatureNames)
                {
                    sigInfo = InspectSignature(acroFields, name, perms);
                    UpdateSignatureResults(ref sigInfo);         //produce human friendly result text

                    msg.Signatures.Add(sigInfo);
                }
                msg.StatusText = String.Format("Ο έλεγχος ολοκληρώθηκε επιτυχώς. Βρέθηκαν {0} ψηφιακές υπογραφές στο έγγραφο!", msg.signaturesCount);
                return(true);
            }
            catch
            {
                return(false);
            }
        }         //end validatePDF
Exemple #2
0
        private static void UpdateSignatureResults(ref MessageReport.Signature s)
        {
            MessageReport.Results r = new MessageReport.Results();
            string hm = string.Empty;

            r.result = false;

            hm = "Το αρχείο ";

            if (s.Certificate.isHardCertificate == false)
            {
                hm += "δεν ";
            }
            hm += "έχει υπογραφεί με χρήση πιστοποιητικού σκληρής αποθήκευσης της ΑΠΕΔ,  ";

            hm += "το οποίο ";
            if (s.isValidDateSigning == false)
            {
                hm += "δεν ";
            }
            hm += " ήταν έγκυρο κατά την ημερομηνία υπογραφής. ";

            hm += "Το αρχείο  ";
            if (s.isIntegral)
            {
                hm += "δεν ";
            }
            hm += " έχει τροποποιηθεί μετά την υπογραφή του και  ";

            if (s.isTimestampped == false)
            {
                hm += "δεν ";
            }
            hm += " φέρει ενσωματωμένη χρονοσφραγίδα. ";

            if (s.isValidDateSigning && s.isIntegral && s.isTimestampped && s.Certificate.isHardCertificate)
            {
                r.result = true;
            }

            r.resultText = hm;

            s.Results = r;
        }
Exemple #3
0
        private static PdfPKCS7 VerifySignature(AcroFields fields, String name, ref MessageReport.Signature sigInfo)
        {
            sigInfo.isCoveringWholeDocument = fields.SignatureCoversWholeDocument(name);

            PdfPKCS7 pkcs7 = fields.VerifySignature(name);

            sigInfo.isIntegral = pkcs7.Verify();

            X509Certificate[] certs = pkcs7.SignCertificateChain;
            DateTime          cal   = pkcs7.SignDate;

            IList <VerificationException> errors = CertificateVerification.VerifyCertificates(certs, certificates, null, cal);

            if (errors == null)
            {
                Console.WriteLine("Certificates verified against the KeyStore");
            }
            else
            {
                foreach (object error in errors)
                {
                    Console.WriteLine(error);
                }
            }
            for (int i = 0; i < certs.Length; ++i)
            {
                X509Certificate cert = certs[i];
            }
            X509Certificate signCert   = certs[0];
            X509Certificate issuerCert = (certs.Length > 1 ? certs[1] : null);

            sigInfo.Certificate = GetCertificateInfo(signCert, cal.ToLocalTime());

            sigInfo.isValidDateSigning = CheckRevocation(pkcs7, signCert, issuerCert, cal);

            sigInfo.isValidToday = CheckRevocation(pkcs7, signCert, issuerCert, DateTime.Now.AddDays(-1));



            return(pkcs7);
        }
        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;
        }
        public static bool validatePDF(string filename, ref  MessageReport msg)
        {
            try
                    {

                        X509CertificateParser parser = new X509CertificateParser();

                        if (certificates.Count() == 0)
                        {
                            certificates.Add(parser.ReadCertificate(new FileStream(ROOT1, FileMode.Open)));
                            certificates.Add(parser.ReadCertificate(new FileStream(ROOT2, FileMode.Open)));
                            certificates.Add(parser.ReadCertificate(new FileStream(ROOT3, FileMode.Open)));

                        }

                        PdfReader pdfReader = new PdfReader(filename);

                        AcroFields acroFields = pdfReader.AcroFields;
                        List<String> signatureNames = acroFields.GetSignatureNames();

                        msg.signaturesCount = signatureNames.Count();

                        if (signatureNames.Count == 0)
                        {
                            msg.StatusText = "Δεν βρέθηκαν ψηφιακές υπογραφές στο έγγραφο!";
                            return false;
                        }

                        SignaturePermissions perms = null;
                        MessageReport.Signature sigInfo = new MessageReport.Signature();

                        foreach (String name in signatureNames)
                        {

                            sigInfo = InspectSignature(acroFields, name, perms);
                            UpdateSignatureResults(ref sigInfo); //produce human friendly result text

                            msg.Signatures.Add(sigInfo);

                        }
                        msg.StatusText = String.Format("Ο έλεγχος ολοκληρώθηκε επιτυχώς. Βρέθηκαν {0} ψηφιακές υπογραφές στο έγγραφο!", msg.signaturesCount);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
        }
Exemple #6
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);
        }