public static bool createEnvelopedBes(string pinNo, string signXML, String outXML, bool bInTest) { bool res = false; cardPinNo = pinNo; TestEnvelopedSignatureInitialize(); try { // here is our custom envelope xml // XmlDocument envelopeDoc = newEnvelope("edefter.xml"); XmlDocument envelopeDoc = Conn.newEnvelope(signXML); XmlElement exts = (XmlElement)envelopeDoc.GetElementsByTagName("ext:UBLExtensions").Item(0); XmlElement ext = (XmlElement)exts.GetElementsByTagName("ext:UBLExtension").Item(0); XmlElement extContent = (XmlElement)ext.GetElementsByTagName("ext:ExtensionContent").Item(0); UriBuilder ub = new UriBuilder(Conn.ROOT_DIR + "efatura\\config\\"); // create context with working dir Context context = new Context(ub.Uri); //UriBuilder ub2 = new UriBuilder(Conn.ROOT_DIR + "efatura\\config\\xmlsignature-config.xml"); context.Config = new Config(Conn.ROOT_DIR + "efatura\\config\\xmlsignature-config.xml"); // define where signature belongs to context.Document = envelopeDoc; // create signature according to context, // with default type (XADES_BES) XMLSignature signature = new XMLSignature(context, false); String setID = "Signature_" + envelopeDoc.GetElementsByTagName("cbc:ID").Item(0).InnerText; signature.Id = setID; signature.SigningTime = DateTime.Now; // attach signature to envelope //envelopeDoc.DocumentElement.AppendChild(signature.Element); extContent.AppendChild(signature.Element); //add transforms for efatura Transforms transforms = new Transforms(context); transforms.addTransform(new Transform(context, TransformType.ENVELOPED.Url)); // add document as reference, //signature.addDocument("#data1", "text/xml", false); signature.addDocument("", "text/xml", transforms, DigestMethod.SHA_256, false); ECertificate certificate = SmartCardManager.getInstance().getEInvoiceCertificate(cardPinNo);// getSignatureCertificate(true, false); if (certificate.isMaliMuhurCertificate()) { ValidationPolicy policy = new ValidationPolicy(); String policyPath = Conn.ROOT_DIR + "efatura\\config\\certval-policy-malimuhur.xml"; policy = PolicyReader.readValidationPolicy(policyPath); ValidationSystem vs = CertificateValidation.createValidationSystem(policy); context.setCertValidationSystem(vs); } else { ValidationPolicy policy = new ValidationPolicy(); String policyPath = Conn.ROOT_DIR + "efatura\\config\\certval-policy.xml"; policy = PolicyReader.readValidationPolicy(policyPath); ValidationSystem vs = CertificateValidation.createValidationSystem(policy); context.setCertValidationSystem(vs); } if (CertValidation.validateCertificate(certificate) || bInTest) { BaseSigner signer = SmartCardManager.getInstance().getSigner(cardPinNo, certificate); X509Certificate2 msCert = certificate.asX509Certificate2(); signature.addKeyInfo(msCert.PublicKey.Key); signature.addKeyInfo(certificate); KeyInfo keyInfo = signature.createOrGetKeyInfo(); int elementCount = keyInfo.ElementCount; for (int k = 0; k < elementCount; k++) { KeyInfoElement kiElement = keyInfo.get(k); if (kiElement.GetType().IsAssignableFrom(typeof(X509Data))) { X509Data x509Data = (X509Data)kiElement; X509SubjectName x509SubjectName = new X509SubjectName(context, certificate.getSubject().stringValue()); x509Data.add(x509SubjectName); break; } } //signature.addKeyInfo(certificate); signature.SignedInfo.CanonicalizationMethod = C14nMethod.EXCLUSIVE_WITH_COMMENTS; signature.sign(signer); // this time we dont use signature.write because we need to write // whole document instead of signature using (Stream s = new FileStream(outXML, FileMode.Create)) { try { envelopeDoc.Save(s); s.Flush(); s.Close(); res = true; } catch (Exception e) { res = false; MessageBox.Show("Dosya kaydedilirken hata oluştu " + e.Message.ToString()); s.Close(); } } } } catch (Exception e) { res = false; MessageBox.Show("Hata Oluştu \r\n" + e.Message.ToString()); } return(res); }
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; })); }