Signed request for cancellation of CFDI and Retenciones
How to sign a cancellation request with the issuer's private key?
Sovos Mexico offers a cancellation Web Service which does not require the private key of the issuer's CSD. For this webservice, the request must include a digital signature according to the XML-Signature standard.
Procedure
The XML must be generated with the following data:
- Request date
- Issuer's RFC
-
Fiscal folio to be canceled (UUID):
- For CFDI: from 1 up to 500
- For Retenciones CFDI: from 1 up to 10,000
XML structure for the cancellation of a CFDI, where the reason for cancellation is indicated
<Cancelacion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Fecha="2022-01-06T12:15:44" RfcEmisor="EKU9003173C9" xmlns="http://cancelacfd.sat.gob.mx">
<Folios>
<Folio UUID="B46DFB03-EABC-4214-9519-F8AB34745101" Motivo="01" FolioSustitucion="8ba9dfb6-0535-41e0-9ca8-eb6428e09e94"/>
</Folios>
<Folios>
<Folio UUID="91D0F271-2456-44DE-B4C9-2125F9B6C5E1" Motivo="02"/>
</Folios>
<Folios>
<Folio UUID="0C69BA9F-C177-4C08-982F-0C86EBBBF99C" Motivo="02"/>
</Folios>
</Cancelacion>
XML structure for the cancellation of a Retenciones CFDI, where the reason for cancellation is indicated
<Cancelacion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Fecha="2021-12-22T19:44:12" RfcEmisor="EKU9003173C9" xmlns="http://www.sat.gob.mx/esquemas/retencionpago/1">
<Folios>
<Folio UUID="FE89B270-B48E-4B2A-A595-E792A779C0FD" Motivo="02"/>
</Folios>
</Cancelacion>
Generate a signature node with the XML-Signature standard for the XML with the issuer's private key.
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>j+98lhLKWYEsxwb4v2Z7oM0JK2M=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>dhVVdI2WL6CTWby6ltQaezPoGcaoeL1w2ASj+GeaOai8Jh43kWFPYeHPp4JA3e7lD6pXwnzPrhR9XRWb4iAze+DB1eIbrkJX9ImwZTWWg4y12kDkywPuVd/dNsCaIl2CGRvHJf2H7vQS2NELzkhlj4ITzlJgFnaEuLpc/whpIxuiGcCB8MvkedeWQn+5Nb4J/SksKcYQOgm3rpRXFutDFlnINQGht7Ev+CbDEBiCk87GQT3NXzGWcWF6wxFxp0pweh/CkQ6Cnrfkk4zYpDgXpn8zDMFHKs8XP8w3sHLO7xVJP0rnbSmKUJBD9bWgUtLk+W/38pTYXV+7OBYvyjdsEA==</SignatureValue>
<KeyInfo>
<X509Data>
<X509IssuerSerial>
<X509IssuerName>OID.1.2.840.113549.1.9.2=responsable: ACDMA-SAT, OID.2.5.4.45=2.5.4.45, L=COYOACAN, S=CIUDAD DE MEXICO, C=MX, PostalCode=06370, STREET=3ra cerrada de cadiz, E=oscar.martinez@sat.gob.mx, OU=SAT-IES Authority, O=SERVICIO DE ADMINISTRACION TRIBUTARIA, CN=AC UAT</X509IssuerName>
<X509SerialNumber>292233162870206001759766198444326234574038512436</X509SerialNumber>
</X509IssuerSerial>
<X509Certificate>MIIFuzCCA6OgAwIBAgIUMzAwMDEwMDAwMDA0MDAwMDI0MzQwDQYJKoZIhvcNAQELBQAwggErMQ8wDQYDVQQDDAZBQyBVQVQxLjAsBgNVBAoMJVNFUlZJQ0lPIERFIEFETUlOSVNUUkFDSU9OIFRSSUJVVEFSSUExGjAYBgNVBAsMEVNBVC1JRVMgQXV0aG9yaXR5MSgwJgYJKoZIhvcNAQkBFhlvc2Nhci5tYXJ0aW5lekBzYXQuZ29iLm14MR0wGwYDVQQJDBQzcmEgY2VycmFkYSBkZSBjYW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vF5rpeCZP5Dw504RZkYDl2suRz+wa1sNBVpbnBJEK0fQcN3IftBwsgNFdFhUtCyw3lus1SSJbPxjLHS6FcZZ51YSeIfcNXOAuTqdimusaXq15GrSrCOkM6n2jfj2sMJYM2HXaXJ6rGTEgYmhYdwxWtil6RfZB+fGQ/H9I9WLnl4KTZUS6C9+NLHh4FPDhSk19fpS2S/56aqgFoGAkXAYt9Fy5ECaPcULIfJ1DEbsXKyRdCv3JY89+0MNkOdaDnsemS2o5Gl08zI4iYtt3L40gAZ60NPh31kVLnYNsmvfNxYyKp+AeJtDHyW9w7ftM0Hoi+BuRmcAQSKFV3pk8j51la+jrRBrAUv8blbRcQ5BiZUwJzHFEKIwTsRGoRyEx96sNnB03n6GTwjIGz92SmLdNl95r9rkvp+2m4S6q1lPuXaFg7DGBrXWC8iyqeWE2iobdwIIuXPTMVqQb12m1dAkJVRO5NdHnP/MpqOvOgLqoZBNHGyBg4Gqm4sCJHCxA1c8Elfa2RQTCk0tAzllL4vOnI1GHkGJn65xokGsaU4B4D36xh7eWrfj4/pgWHmtoDAYa8wzSwo2GVCZOs+mtEgOQB91/g==</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
Insert the signature in the original XML; Below is the example for CFDI:
<Cancelacion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Fecha="2022-01-06T12:15:44" RfcEmisor="EKU9003173C9" xmlns="http://cancelacfd.sat.gob.mx"><Folios><Folio UUID="B46DFB03-EABC-4214-9519-F8AB34745101" Motivo="01" FolioSustitucion="8ba9dfb6-0535-41e0-9ca8-eb6428e09e94" /></Folios><Folios><Folio UUID="91D0F271-2456-44DE-B4C9-2125F9B6C5E1" Motivo="02" /></Folios><Folios><Folio UUID="0C69BA9F-C177-4C08-982F-0C86EBBBF99C" Motivo="02" /></Folios><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>j+98lhLKWYEsxwb4v2Z7oM0JK2M=</DigestValue></Reference></SignedInfo><SignatureValue>dhVVdI2WL6CTWby6ltQaezPoGcaoeL1w2ASj+GeaOai8Jh43kWFPYeHPp4JA3e7lD6pXwnzPrhR9XRWb4iAze+DB1eIbrkJX9ImwZTWWg4y12kDkywPuVd/dNsCaIl2CGRvHJf2H7vQS2NELzkhlj4ITzlJgFnaEuLpc/whpIxuiGcCB8MvkedeWQn+5Nb4J/SksKcYQOgm3rpRXFutDFlnINQGht7Ev+CbDEBiCk87GQT3NXzGWcWF6wxFxp0pweh/CkQ6Cnrfkk4zYpDgXpn8zDMFHKs8XP8w3sHLO7xVJP0rnbSmKUJBD9bWgUtLk+W/38pTYXV+7OBYvyjdsEA==</SignatureValue><KeyInfo><X509Data><X509IssuerSerial><X509IssuerName>OID.1.2.840.113549.1.9.2=responsable: ACDMA-SAT, OID.2.5.4.45=2.5.4.45, L=COYOACAN, S=CIUDAD DE MEXICO, C=MX, PostalCode=06370, STREET=3ra cerrada de cadiz, E=oscar.martinez@sat.gob.mx, OU=SAT-IES Authority, O=SERVICIO DE ADMINISTRACION TRIBUTARIA, CN=AC UAT</X509IssuerName><X509SerialNumber>292233162870206001759766198444326234574038512436</X509SerialNumber></X509IssuerSerial><X509Certificate>MIIFuzCCA6OgAwIBAgIUMzAwMDEwMDAwMDA0MDAwMDI0MzQwDQYJKoZIhvcNAQELBQAwggErMQ8wDQYDVQQDDAZBQyBVQVQxLjAsBgNVBAoMJVNFUlZJQ0lPIERFIEFETUlOSVNUUkFDSU9OIFRSSUJVVEFSSUExGjAYBgNVBAsMEVNBVC1JRVMgQXV0aG9yaXR5MSgwJgYJKoZIhv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ICAQBcpj1TjT4jiinIujIdAlFzE6kRwYJCnDG08zSp4kSnShjxADGEXH2chehKMV0FY7c4njA5eDGdA/G2OCTPvF5rpeCZP5Dw504RZkYDl2suRz+wa1sNBVpbnBJEK0fQcN3IftBwsgNFdFhUtCyw3lus1SSJbPxjLHS6FcZZ51YSeIfcNXOAuTqdimusaXq15GrSrCOkM6n2jfj2sMJYM2HXaXJ6rGTEgYmhYdwxWtil6RfZB+fGQ/H9I9WLnl4KTZUS6C9+NLHh4FPDhSk19fpS2S/56aqgFoGAkXAYt9Fy5ECaPcULIfJ1DEbsXKyRdCv3JY89+0MNkOdaDnsemS2o5Gl08zI4iYtt3L40gAZ60NPh31kVLnYNsmvfNxYyKp+AeJtDHyW9w7ftM0Hoi+BuRmcAQSKFV3pk8j51la+jrRBrAUv8blbRcQ5BiZUwJzHFEKIwTsRGoRyEx96sNnB03n6GTwjIGz92SmLdNl95r9rkvp+2m4S6q1lPuXaFg7DGBrXWC8iyqeWE2iobdwIIuXPTMVqQb12m1dAkJVRO5NdHnP/MpqOvOgLqoZBNHGyBg4Gqm4sCJHCxA1c8Elfa2RQTCk0tAzllL4vOnI1GHkGJn65xokGsaU4B4D36xh7eWrfj4/pgWHmtoDAYa8wzSwo2GVCZOs+mtEgOQB91/g==</X509Certificate></X509Data></KeyInfo></Signature></Cancelacion>
Sample of implementation using Microsoft .NET platform
Below is a sample of the implementation in C# to perform this type of signature.
Code sample
This sample requires the Microsoft .NET Framework 5.0. It references the assemblies
System.dll
, System.Security.dll
, System.Xml.dll
and System.Xml.Linq.dll
.
/*
The MIT License (MIT)
Copyright (c) 2013 Reachcore, Una división de Grupo Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Se autoriza por la presente, de forma gratuita, a cualquier
persona que haya obtenido una copia de este software y
archivos asociados de documentación (el "Software"), para tratar en el
Software sin restricción, incluyendo sin ninguna limitación en lo que concierne
los derechos para usar, copiar, modificar, fusionar, publicar,
distribuir, sublicenciar, y / o vender copias de este
Software, y para permitir a las personas a las que se les proporcione el Software para
hacer lo mismo, sujeto a las siguientes condiciones:
El aviso de copyright anterior y este aviso de permiso
tendrá que ser incluido en todas las copias o partes sustanciales de
este Software.
EL SOFTWARE SE ENTREGA "TAL CUAL", SIN GARANTÍA DE NINGÚN
TIPO, EXPRESA O IMPLÍCITA, INCLUYENDO PERO SIN LIMITARSE A GARANTÍAS DE
MERCANTIBILIDAD, CAPACIDAD DE HACER Y DE NO INFRACCIÓN DE COPYRIGHT. EN NINGÚN
CASO LOS AUTORES O TITULARES DEL COPYRIGHT SERÁN RESPONSABLES DE
NINGUNA RECLAMACIÓN, DAÑOS U OTRAS RESPONSABILIDADES,
YA SEA EN UN LITIGIO, AGRAVIO O DE OTRO MODO,
DERIVADAS DE, OCASIONADAS POR CULPA DE O EN CONEXION CON EL
SOFTWARE O SU USO U OTRO TIPO DE ACCIONES EN EL SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using System.IO;
using System.Security.Cryptography.Xml;
namespace XmlSignatureExample
{
///
/// Ejemplo de generación de XML para solicitud de cancelación firmada.
///
class Program
{
static void Main(string[] args)
{
// --------------------------------------------------------------------------
// I. INPUT PARAMETERS
// --------------------------------------------------------------------------
// Issuer's RFC of the CFDI to be canceled
const string rfcEmisor = "EKU9003173C9";
// PFX file Bytes (PKCS#12 format) of an issued SAT's CSD
byte[] pfxBlob = File.ReadAllBytes("EKU9003173C9.pfx");
// Password of the PFX file
const string pfxPassword = "12345678a";
// List of UUID to cancel (up to 500 folios)
List foliosFiscales = new List { Guid.Parse("C5D56CB5-6E35-4CD0-A32E-CFD2B5453995"), Guid.Parse("D4888EF9-3BE2-4B78-B079-F5206D0BA453") };
// --------------------------------------------------------------------------
// II. BUILD ORIGINAL REQUEST XML WITH CANCELLATION DATA
// --------------------------------------------------------------------------
XNamespace satCancelacionXmlNamespace = "http://cancelacfd.sat.gob.mx";
var xmlSolicitud = new XElement(satCancelacionXmlNamespace + "Cancelacion",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
new XAttribute("Fecha", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")),
new XAttribute("RfcEmisor", rfcEmisor),
from uuid in foliosFiscales
select new XElement(satCancelacionXmlNamespace + "Folios",
new XElement(satCancelacionXmlNamespace + "Folio",
new XAttribute("UUID", uuid.ToString().ToUpper()),
new XAttribute("Motivo", "02"),
new XAttribute("FolioSustitucion", ""))));
XmlDocument originalXmlDocument = new XmlDocument() { PreserveWhitespace = false };
originalXmlDocument.LoadXml(xmlSolicitud.ToString());
// --------------------------------------------------------------------------
// III. GENERATE ELEMENT USING CSD OF ISSUER
// --------------------------------------------------------------------------
XmlElement signatureElement = GenerateXmlSignature(originalXmlDocument, pfxBlob, pfxPassword);
// --------------------------------------------------------------------------
// IV. EMBEDDING THE ELEMENT WITHIN THE ORIGINAL XML DOCUMENT
// --------------------------------------------------------------------------
originalXmlDocument.DocumentElement.AppendChild(originalXmlDocument.ImportNode(signatureElement, true));
// --------------------------------------------------------------------------
// V. THE RESULTING XML IS THE SIGNED REQUEST FOR CANCELLATION
// --------------------------------------------------------------------------
Console.WriteLine(originalXmlDocument.OuterXml);
}
public static XmlElement GenerateXmlSignature(XmlDocument originalXmlDocument, byte[] pfx, String pfxPassword)
{
X509Certificate2 cert = new X509Certificate2(pfx, pfxPassword);
CspParameters rsaParameters = new CspParameters();
rsaParameters.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider Key = new RSACryptoServiceProvider(cert.PrivateKey.KeySize, rsaParameters);
SignedXml signedXml = new SignedXml(originalXmlDocument) { SigningKey = Key };
Reference reference = new Reference() { Uri = String.Empty };
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
KeyInfoX509Data kdata = new KeyInfoX509Data(cert);
kdata.AddIssuerSerial(cert.Issuer, cert.SerialNumber);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(kdata);
signedXml.KeyInfo = keyInfo;
signedXml.AddReference(reference);
signedXml.ComputeSignature();
return signedXml.GetXml();
}
}
}
Regresar