Пример #1
0
        public SignedDataValidationResult validateSign(string signedFile)
        {
            Dictionary <string, object> params_ = new Dictionary <string, object>();

            params_[EParameters.P_CERT_VALIDATION_POLICY] = getPolicy();
            //if (externalContent != null)
            //    params_[EParameters.P_EXTERNAL_CONTENT] = externalContent;

            //Use only reference and their corresponding value to validate signature
            params_[EParameters.P_FORCE_STRICT_REFERENCE_USE] = true;
            //Ignore grace period which means allow usage of CRL published before signature time
            //params_[EParameters.P_IGNORE_GRACE] = true;

            //Use multiple policies if you want to use different policies to validate different types of certificate
            // CertValidationPolicies certificateValidationPolicies = new CertValidationPolicies();
            // certificateValidationPolicies.register(CertValidationPolicies.CertificateType.DEFAULT.ToString(), TestConstants.getPolicy());
            // ValidationPolicy maliMuhurPolicy = PolicyReader.readValidationPolicy(new FileStream(TestConstants.getDirectory()+"/config/certval-policy-malimuhur.xml", FileMode.Open, FileAccess.Read));
            // certificateValidationPolicies.register(CertValidationPolicies.CertificateType.MaliMuhurCertificate.ToString(), maliMuhurPolicy);
            // params_[EParameters.P_CERT_VALIDATION_POLICIES]= certificateValidationPolicies;

            SignedDataValidation sdv = new SignedDataValidation();

            byte[] signedFileByes           = ReadFile(signedFile);
            SignedDataValidationResult sdvr = sdv.verify(signedFileByes, params_);

            return(sdvr);
        }
Пример #2
0
        public Task <List <SignatureInfo> > CheckSignaturesAsync(byte[] input)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (!IsSignedData(input) && !IsSignedPDF(input))
                {
                    throw new SignatureNotFoundException("İmza bilgisi bulunamdı.");
                }

                List <SignatureInfo> signInfo = new List <SignatureInfo>();

                if (IsSignedData(input))
                {
                    BaseSignedData bs = new BaseSignedData(input);
                    Dictionary <string, object> params_ = new Dictionary <string, object>();
                    params_[EParameters.P_CERT_VALIDATION_POLICY] = _policy;
                    params_[EParameters.P_FORCE_STRICT_REFERENCE_USE] = true;

                    SignedDataValidation sdv = new SignedDataValidation();
                    SignedDataValidationResult sdvr = sdv.verify(input, params_);


                    for (int i = 0; i < sdvr.getSDValidationResults().Count; i++)
                    {
                        var item = sdvr.getSDValidationResults()[i];
                        var signatureType = bs.getSignerList()[i].getType().name();
                        var certificate = item.getSignerCertificate();
                        var name = certificate.getSubject().getCommonNameAttribute();
                        var identity = certificate.getSubject().getSerialNumberAttribute();
                        var serialnumber = certificate.getSerialNumber().ToString();
                        var issuer = certificate.getIssuer().getCommonNameAttribute();



                        bool isvalid = false;

                        if (item.getSignatureStatus() == Types.Signature_Status.VALID)
                        {
                            isvalid = true;
                        }

                        var info = new SignatureInfo()
                        {
                            Identity = identity, Name = name, IsValid = isvalid, Issuer = issuer, SerialNumber = serialnumber, SignatureType = signatureType
                        };

                        if (certificate.getNotAfter().HasValue)
                        {
                            info.ValidUntil = certificate.getNotAfter().Value;
                        }

                        if (certificate.getNotBefore().HasValue)
                        {
                            info.ValidFrom = certificate.getNotBefore().Value;
                        }

                        var signaturealgorithm = SignatureAlg.fromAlgorithmIdentifier(certificate.getSignatureAlgorithm()).first().getName();
                        var publickeyalgorithm = SignatureAlg.fromAlgorithmIdentifier(certificate.getPublicKeyAlgorithm()).first().getName();

                        var publicKey = certificate.asX509Certificate2().GetPublicKeyString();

                        info.PublicKey = publicKey;
                        info.SignatureAlgorithm = signaturealgorithm;
                        info.PublicKeyAlgorithm = publickeyalgorithm;

                        info.IsTimeStampedCertificate = certificate.isTimeStampingCertificate();
                        info.IsQualifiedCertificate = certificate.isQualifiedCertificate();

                        if (item.getSigningTime().HasValue)
                        {
                            info.SignedOn = item.getSigningTime().Value;
                        }

                        signInfo.Add(info);
                    }
                }

                if (IsSignedPDF(input))
                {
                    PdfReader reader = new PdfReader(input);
                    AcroFields af = reader.AcroFields;

                    var names = af.GetSignatureNames();

                    for (int i = 0; i < names.Count; ++i)
                    {
                        String name = (string)names[i];
                        PdfPKCS7 pk = af.VerifySignature(name);


                        var cert = new ECertificate(pk.SigningCertificate.GetEncoded());

                        var signatureType = "ES_XL";
                        var commonname = cert.getSubject().getCommonNameAttribute();
                        var identity = cert.getSubject().getSerialNumberAttribute();
                        var serialnumber = cert.getSerialNumber().ToString();
                        var issuer = cert.getIssuer().getCommonNameAttribute();

                        var info = new SignatureInfo()
                        {
                            Identity = identity, Name = commonname, IsValid = pk.SigningCertificate.IsValidNow, Issuer = issuer, SerialNumber = serialnumber, SignatureType = signatureType
                        };

                        if (cert.getNotAfter().HasValue)
                        {
                            info.ValidUntil = cert.getNotAfter().Value;
                        }

                        if (cert.getNotBefore().HasValue)
                        {
                            info.ValidFrom = cert.getNotBefore().Value;
                        }

                        var signaturealgorithm = SignatureAlg.fromAlgorithmIdentifier(cert.getSignatureAlgorithm()).first().getName();
                        var publickeyalgorithm = SignatureAlg.fromAlgorithmIdentifier(cert.getPublicKeyAlgorithm()).first().getName();

                        var publicKey = cert.asX509Certificate2().GetPublicKeyString();

                        info.PublicKey = publicKey;
                        info.SignatureAlgorithm = signaturealgorithm;
                        info.PublicKeyAlgorithm = publickeyalgorithm;

                        info.IsTimeStampedCertificate = cert.isTimeStampingCertificate();
                        info.IsQualifiedCertificate = cert.isQualifiedCertificate();

                        if (cert.isQualifiedCertificate())
                        {
                            info.SignedOn = pk.SignDate;
                        }
                        else if (cert.isTimeStampingCertificate())
                        {
                            info.SignedOn = pk.TimeStampDate;
                        }

                        signInfo.Add(info);
                    }
                }

                return signInfo;
            }));
        }