I am creating a module to be able to generate the XML files of a CFDI. One of the points is to generate a digital stamp, I have followed the steps of different guides that describe in different languages the process that you must have with the algorithms SHA-1
and RSA
.
In my code, I even have two processes implemented, one with the library opensslkey
and one with native C# libraries, both return the same result. The problem is that the validators tell me that the seal is malformed or invalid.
Do you have any idea why that could be? (following my code).
private string generaSello()
{
string strSello = string.Empty;
string strPathLlave = CFDI.Properties.Settings.Default.llave;
string strLlavePwd = CFDI.Properties.Settings.Default.clave;
string strCadenaOriginal = generaCadena(); // Aquí ya haber generado la cadena original
System.Security.SecureString passwordSeguro = new System.Security.SecureString();
passwordSeguro.Clear();
foreach (char c in strLlavePwd.ToCharArray())
passwordSeguro.AppendChar(c);
byte[] llavePrivadaBytes = System.IO.File.ReadAllBytes(strPathLlave);
RSACryptoServiceProvider rsa = opensslkey.DecodeEncryptedPrivateKeyInfo(llavePrivadaBytes, passwordSeguro);
SHA1Managed sha = new SHA1Managed();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(generaCadena());
byte[] digest = sha.ComputeHash(bytes);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
RSAFormatter.SetHashAlgorithm("SHA1");
byte[] SignedHashValue = RSAFormatter.CreateSignature(digest);
SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider();
byte[] bytesFirmados = rsa.SignData(System.Text.Encoding.UTF8.GetBytes(strCadenaOriginal), hasher);
strSello = Convert.ToBase64String(bytesFirmados); // Y aquí está el sello
string r = Convert.ToBase64String(SignedHashValue);// Y aquí está el sello 2
return strSello;
//return r;
}
This is an example of the formed XML:
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd" version="3.2" folio="0" fecha="2017-04-18T18:19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MA8GA1UECxMIcHJvZHVjdG8wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKS/beUVy6E3aODaNuLd2S3PXaQre0tGxmYTeUxa55x2t/7919ttgOpKF6hPF5KvlYh4ztqQqP4yEV+HjH7yy/2d/+e7t+J61jTrbdLqT3WD0+s5fCL6JOrF4hqy//EGdfvYftdGRNrZH+dAjWWml2S/hrN9aUxraS5qqO1b7btlAgMBAAGjHTAbMAwGA1UdEwEB/wQCMAAwCwYDVR0PBAQDAgbAMA0GCSqGSIb3DQEBBQUAA4IBAQACPXAWZX2DuKiZVv35RS1WFKgT2ubUO9C+byfZapV6ZzYNOiA4KmpkqHU/bkZHqKjR+R59hoYhVdn+ClUIliZf2ChHh8s0a0vBRNJ3IHfA1akWdzocYZLXjz3m0Er31BY+uS3qWUtPsONGVDyZL6IUBBUlFoecQhP9AO39er8zIbeU2b0MMBJxCt4vbDKFvT9i3V0Puoo+kmmkf15D2rBGR+drd8H8Yg8TDGFKf2zKmRsgT7nIeou6WpfYp570WIvLJQY+fsMp334D05Up5ykYSAxUGa30RdUzA4rxN5hT+W9whWVGD88TD33Nw55uNRUcRO3ZUVHmdWRG+GjhlfsD" subTotal="1" TipoCambio="1.00" Moneda="Peso Mexicano" total="1" tipoDeComprobante="ingreso" metodoDePago="02,99" LugarExpedicion="Av. Cerro Gordo del Campestre 201 301, Las Quintas, 37125, León, León de los Aldama, Guanajuato, México" NumCtaPago="NO IDENTIFICADO">
<cfdi:Emisor rfc="DRD091113A70" nombre="DESARROLLOS RESIDENCIALES DE LEON SA DE CV">
<cfdi:DomicilioFiscal calle="Av. Cerro Gordo del Campestre" noExterior="201" noInterior="301" colonia="Las quintas" localidad="León de los Aldama" municipio="León" estado="Guanajuato" pais="México" codigoPostal="37125" />
<cfdi:RegimenFiscal Regimen="PERSONA MORAL REGIMEN GENERAL" />
</cfdi:Emisor>
<cfdi:Receptor rfc="AAHA6301152K1" nombre="ABRAHAM ARAIZA HERNANDEZ">
<cfdi:Domicilio calle="VIA AUREA" noExterior="440" colonia="VILLA MAGNA" municipio="SAN LUIS POTOSÍ" estado="SAN LUIS POTOSÍ" pais="MEXICO" codigoPostal="78413" />
</cfdi:Receptor>
<cfdi:Conceptos>
<cfdi:Concepto cantidad="1" unidad="NO APLICA" descripcion="VENTA DE TERRENO&#xA;LOTE 36 MZ 64 DE LA PRIVADA MAYORAZGO LA GAVIA DE CONFORMIDAD CON EL FIDEICOMISO DE BANCO DEL BAJIO NO. 10153-10-34 DE FECHA 24 DE ENERO DE 2011" valorUnitario="1" importe="1" />
</cfdi:Conceptos>
<cfdi:Impuestos />
</cfdi:Comprobante>
Look, I recommend that you compare your string (the one you send to generate the seal) with the one that the validator is generating:
NOTE: You use version 3.2... apparently we are already at 3.3, I don't know if your detail goes there. And that is summed up in a structure error, which would lead to an error in the formation of the original chain and therefore an error in the seal.
String
||3.2|2017-04-18T18:19:17|income|0|1|1.00|Mexican Peso|1|02.99|Av. Cerro Gordo del Campestre 201 301, Las Quintas, 37125, León, León de los Aldama, Guanajuato, Mexico|UNIDENTIFIED|DRD091113A70|RESIDENTIAL DEVELOPMENTS OF LEON SA DE CV|Av. Cerro Gordo del Campestre|201|301|Las quintas|León de los Aldama|León|Guanajuato|Mexico|37125|MORAL PERSON GENERAL REGIME|AAHA6301152K1|ABRAHAM ARAIZA HERNANDEZ|VIA AUREA|440|VILLA MAGNA|SAN LUIS POTOSÍ|SAN LUIS POTOSÍ|MEXICO|78413|1|NOT APPLICABLE|SALE OF LAND LOT 36 MZ 64 OF PRIVATE MAYORAZGO LA GAVIA IN ACCORDANCE WITH BANCO DEL BAJIO TRUST NO. 10153-10-34 DATED JANUARY 24, 2011|1|1||
Stamp
iMTGK5FHmLpudWYIxLP99/h44JMErjg1bNLtblp/YvVAVTPMiMlwEF9Y+44wHOVeQ2u36xnl1aVVCL9T8fyUQwFRCmd9PtF WdAmSQrpHK914gomk31W+Hunrdrwj0Asewyn/wl1fVaJvrYxbeV2vVr7+
Validations
One of the errors says that the folio is incorrect:
cfdi:Proof: The value of the "FOLIO" attribute is not valid, it does not comply with the structure defined by the SAT, it must be greater than zero.
Another one of the errors says:
The certificate does not belong to the issuer of the document.
I think this validation is important.