string SignDocument(X509Certificate2 signCertificate, System.IO.Stream inputStream, SignatureProductionPlace signatureProductionPlace, string timeStampUrl = "https://freetsa.org/tsr", SignatureFormat format = SignatureFormat.XAdES_T) { FirmaXadesNet.XadesService svc = new FirmaXadesNet.XadesService(); var parameters = new SignatureParameters() { SignatureMethod = SignatureMethod.RSAwithSHA256, SigningDate = DateTime.Now, SignaturePackaging = SignaturePackaging.ENVELOPED, InputMimeType = "text/xml", SignatureProductionPlace = signatureProductionPlace }; parameters.SignatureCommitments.Add(new SignatureCommitment(SignatureCommitmentType.ProofOfOrigin)); using (parameters.Signer = new Signer(signCertificate)) { var signedDocument = svc.Sign(inputStream, parameters); signedDocument.Document.PreserveWhitespace = true; UpgradeParameters xadesTparameters = new UpgradeParameters() { TimeStampClient = new TimeStampClient(timeStampUrl) }; if (format == SignatureFormat.XAdES_XL) { xadesTparameters.OCSPServers.Add(new OcspServer("http://srvdc06.crossroad.ltd/ocsp")); } XadesUpgraderService upgrader = new XadesUpgraderService(); upgrader.Upgrade(signedDocument, format, xadesTparameters); return(signedDocument.Document.OuterXml); } }
/// <summary> /// Firma un documento XML /// </summary> /// <param name="xml">docuemento XML</param> /// <param name="data_p12">llave criptografica del usuario</param> /// <param name="clave">clave de la llave criptografica</param> /// <returns></returns> public static string getXMLFirmadoWeb(string xml, byte[] data_p12, string clave) { XadesService xadesService = new XadesService(); SignatureParameters parametros = new SignatureParameters(); // PolĂtica de firma de factura-e 3.1 parametros.SignaturePolicyInfo = new SignaturePolicyInfo(); parametros.SignaturePolicyInfo.PolicyIdentifier = "https://tribunet.hacienda.go.cr/docs/esquemas/2016/v4.2/ResolucionComprobantesElectronicosDGT-R-48-2016_4.2.pdf"; parametros.SignaturePolicyInfo.PolicyHash = "Ohixl6upD6av8N7pEvDABhEL6hM="; parametros.SignaturePackaging = SignaturePackaging.ENVELOPED; parametros.DataFormat = new DataFormat(); parametros.DataFormat.MimeType = "text/xml"; parametros.SignerRole = new SignerRole(); parametros.SignerRole.ClaimedRoles.Add("emisor"); //selecciona el certificado del archivop12 var selectedCertificate = GetSelectedCertificate(data_p12, clave); using (parametros.Signer = new Signer(selectedCertificate)) { // convert string to stream byte[] byteArray = Encoding.UTF8.GetBytes(xml); //byte[] byteArray = Encoding.ASCII.GetBytes(contents); MemoryStream stream = new MemoryStream(byteArray); var docFirmado = xadesService.Sign(stream, parametros); return(docFirmado.Document.InnerXml); } }
void ValidateDocument(string xml) { FirmaXadesNet.XadesService svc = new FirmaXadesNet.XadesService(); XmlDocument doc = new XmlDocument { PreserveWhitespace = true }; doc.LoadXml(xml); var resultDoc = svc.Load(doc); var result2 = svc.Validate(resultDoc[0]); Assert.True(result2.IsValid); }
void ValidateDocumentSignatureOnly(string xml) { //signedDocument.Save(@"c:\temp\xades.xml"); FirmaXadesNet.XadesService svc = new FirmaXadesNet.XadesService(); XmlDocument doc = new XmlDocument { PreserveWhitespace = true }; doc.LoadXml(xml); var resultDoc = svc.Load(doc); var result = resultDoc[0].XadesSignature.XadesCheckSignature(Microsoft.Xades.XadesCheckSignatureMasks.AllChecks); Assert.True(result); }