Beispiel #1
0
        public byte[] signWithPfxFile(string pfxFile, string pinCode, byte[] tobeSignBytes)
        {
            BaseSignedData bs = new BaseSignedData();

            tr.gov.tubitak.uekae.esya.api.cmssignature.ISignable content = new SignableByteArray(tobeSignBytes);
            bs.addContent(content);

            //Since SigningTime attribute is optional,add it to optional attributes list
            List <IAttribute> optionalAttributes = new List <IAttribute>();

            optionalAttributes.Add(new SigningTimeAttr(DateTime.UtcNow));

            Dictionary <string, object> params_ = new Dictionary <string, object>();
            ValidationPolicy            policy  = getPolicy();

            //necessary for certificate validation.By default,certificate validation is done
            params_[EParameters.P_CERT_VALIDATION_POLICY] = policy;

            //if the user does not want certificate validation,he can add
            //P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false
            params_[EParameters.P_VALIDATE_CERTIFICATE_BEFORE_SIGNING] = false;
            PfxSigner    signer = new PfxSigner(SignatureAlg.RSA_SHA256.getName(), pfxFile, pinCode);
            ECertificate signatureCertificate = signer.getSignersCertificate();

            bs.addSigner(ESignatureType.TYPE_BES, signatureCertificate, signer, optionalAttributes, params_);
            return(bs.getEncoded());
        }
Beispiel #2
0
        public void extractSignedFile(string signedFile, System.IO.FileStream output)
        {
            byte[]         signedFileByes = ReadFile(signedFile);
            BaseSignedData bs             = new BaseSignedData(signedFileByes);

            byte[] contentBytes = bs.getSignedData().getBytes();
            output.Write(contentBytes, 0, contentBytes.Length);
            output.Flush();
            output.Close();
        }
Beispiel #3
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;
            }));
        }