Sello
node. More...selloSAT
signature as a base64 string from TFD data in CFDI XML document. More...selloSAT
signature in TFD element. More...Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).
Public Function satAsciify ( _ strXmlFile As String _ ) As String
"ó"
.
In certain cases, some characters in an XML document cannot be replaced by a numeric character reference,
for example where they are used in an element or attribute name, such as Año="2016"
.Debug.Print satAsciify("cfdv33a-base.xml") ' <?xml version="1.0" encoding="ISO-8859-1"?> ' <cfdi:Comprobante ... > ' <cfdi:Emisor Rfc=" AAA010101AAA" Nombre="Esta es una demostración" RegimenFiscal="622"></cfdi:Emisor> ' ... [etc]
Find length of byte array.
Public Function satBytesLen ( _ ab() As Byte _ ) As Long
Dim ab() As Byte Debug.Print satBytesLen(ab) ' Expecting 0 ReDim ab(10) ' NB actually 11 elements (0..10) Debug.Print satBytesLen(ab) ' 11 ab = vbNullString ' Set to empty array Debug.Print satBytesLen(ab) ' 0
Verify that the public key in an X.509 certificate matches the private key.
Public Function satCheckKeyAndCert ( _ strKeyFile As String, _ strPassword As String, _ strCertFile As String _ ) As Long
certificado
nodeGet additional information about the core DLL module.
Public Function satComments() As String
Licensed Developer Edition | Edicion de Desarrollador Licenciado.
Get date and time the core DLL module was last compiled.
Public Function satCompileTime() As String
Sep 8 2021 14:33:00
Look up message corresponding to error code.
Public Function satErrorLookup ( _ nErrCode As Long _ ) As String
Debug.Print satErrorLookup(27)
' Invalid XML format/No valido formato XML (BAD_XML_ERROR)
Extract the hex-encoded message digest from the signature (sello) in a signed XML file.
Public Function satExtractDigestFromSignature ( _ strXmlFile As String, _ Optional strCertFile As String = "" _ ) As String
Debug.Print satExtractDigestFromSignature("cfdv33a-signed.xml")
' 0FF1274E51FBB090489588D832BB1B5B36543302DECBB0A5490839B8C99E8755
Add a UTF-8 byte order mark (BOM) to a file if not already present.
Public Function satFixBOM ( _ strOutputFile As String, _ szInputFile As String _ ) As Long
Return an error message string for the last error.
Public Function satFormatErrorMessage ( _ nErrCode As Long _ ) As String
Error ({errCode}): {errorlookup}[: {lasterror}]
' Error (-28): XML restriction is violated/XML restriccion es violada (XML_FACET_ERROR): ' Bad attribute/atributo mal [iedu:instEducativas/@CURP] (line 30): ' 'JUAN01010101GTOHMD0' is too long/es demasiado largo, maximum length/longitud maxima=18
Get the certificate data as a base64 string.
Public Function satGetCertAsString ( _ strFileName As String _ ) As String
Certificado
node.Certificado
node from a .CER file.
This string value can be passed instead of the filename for the szCertFile
parameter in other functions.
Debug.Print satGetCertAsString("emisor.cer")
' MIIF+TCCA+GgAwIBAgIUMzAwMDEwMDAw ...
Get the expiry date of the X.509 certificate in ISO time format.
Public Function satGetCertExpiry ( _ strFileName As String _ ) As String
Certificado
node.'yyyy-mm-ddThh:nn:ssZ'
.
Debug.Print satGetCertExpiry("emisor.cer")
' 2021-05-18T03:54:56Z
notAfter
.Get serial number of the X.509 certificate in "special" SAT format.
Public Function satGetCertNumber ( _ strFileName As String _ ) As String
Certificado
node.
Debug.Print satGetCertNumber("emisor.cer")
' 30001000000300023708
serialNumber
.Get the start date of the X.509 certificate in ISO time format.
Public Function satGetCertStart ( _ strFileName As String _ ) As String
Certificado
node.'yyyy-mm-ddThh:nn:ssZ'
.notBefore
.Get private key data as a PEM-formatted string.
Public Function satGetKeyAsPEMString ( _ strFileName As String, _ strPassword As String _ ) As String
szKeyFile
parameter in other functions.Debug.Print satGetKeyAsPEMString("emisor.key", "12345678a") ' -----BEGIN ENCRYPTED PRIVATE KEY----- ' MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIAgEAAoIBAQACAggA ' ... ' -----END ENCRYPTED PRIVATE KEY-----
Get unencrypted private key data as a base64 string.
Public Function satGetKeyAsString ( _ strFileName As String, _ strPassword As String _ ) As String
llaveCertificado
node in a Cancelacion
element.
Debug.Print Debug.Print satGetKeyAsString("emisor.key", "12345678a")
' PFJTQUtleVZhbHVlPjxNb2R1bHVzPmwxUnl3Y2dRaU ...
Extract attribute data for a given element in an XML file.
Public Function satGetXmlAttribute ( _ strXmlFile As String, _ strAttributeName As String, _ strElementName As String _ ) As String
"!NO MATCH!"
if not found; or empty string on error
(the empty string may be the correct result if attr="", so check for an error using satLastError).strElementName
as the empty string ""
to specify the root element.
Set both strElementName
and strAttributeName
to ""
to find the name of the root element.Debug.Print satGetXmlAttribute("cfdv33a-base.xml", "UUID", "CfdiRelacionado") ' ED1752FE-E865-4FF2-BFE1-0F552E770DC9 Debug.Print satGetXmlAttribute("cfdv33a-base.xml", "BadAttribute", "CfdiRelacionado") ' !NO MATCH! Debug.Print satGetXmlAttribute("cfdv33a-base.xml", "Version", "") ' 3.3 Debug.Print satGetXmlAttribute("cfdv33a-base.xml", "", "") ' cfdi:Comprobante Debug.Print satGetXmlAttribute("cfdv33a-base.xml", "Importe", "//Conceptos/Concepto[2]/Impuestos//Retencion") ' 264 Debug.Print satGetXmlAttribute("cfdv33a-base.xml", "Importe", "//Impuestos/Retenciones/Retencion[2]") ' 500
Insert certificate information into an XML document and output to a new file.
Public Function satInsertCert ( _ strOutputFile As String, _ strXmlFile As String, _ strCertFile As String, _ Optional nOptions As Long = 0 _ ) As Long
certificado
node.Or
operator). Default (0) is to add BOM, empty elements in form <foo></foo>
.SAT_XML_EMPTYELEMTAG
to write all empty elements in the empty-element tag form <foo />
. The default is the start-end tag pair form <foo></foo>
SAT_FILE_NO_BOM
do not add byte-order mark (BOM) to output file. Default = add BOM. noCertificado
and Certificado
nodes overwritten by new values.
Any existing file with same name as the szOutputFile
argument will be overwritten without warning. The input and output files can be the same.Insert certificate information into an XML document and output to memory.
Public Function satInsertCertToString ( _ strXmlFile As String, _ strCertFile As String, _ Optional nOptions As Long = 0 _ ) As String
SAT_XML_EMPTYELEMTAG
to write all empty elements in the empty-element tag form <foo/>
.NoCertificado
attribute.Dim newstring As String Debug.Print "[" & satGetXmlAttribute("cfdv33a-base-nocertnum.xml", "NoCertificado", "cfdi:Comprobante") & "]" ' [] newstring = satInsertCertToString("cfdv33a-base-nocertnum.xml", "emisor.cer") Debug.Print "[" & satGetXmlAttribute(newstring, "NoCertificado", "cfdi:Comprobante") & "]" ' [30001000000300023708] Dim n As Long ' No need to pass certificate details when signing - they are already completed n = satSignXml("cfdv33a-signed_new.xml", CStr(newstring), "emisor.key", "12345678a")
Get last error message set by previous function.
Public Function satLastError() As String
' Attempt to sign but input file does not comply with CFDi specifications Debug.Print satSignXml("cfdv33a-signed_output.xml", "cfdv33a-badspec.xml", "emisor.key", "12345678a", "emisor.cer") ' -10 Debug.Print satErrorLookup(-10) ' Required data not found/Datos necesarios no encontrados (MISSING_ERROR) Debug.Print satLastError() ' Attribute 'Importe' is mandatory/Atributo es obligatorio
Get licence type as a string.
Public Function satLicenceType() As String
"D"
= Developer "T"
= Trial.Compute the message digest of pipe string (cadena) from an XML file.
Public Function satMakeDigestFromXml ( _ strXmlFile As String, _ Optional nOptions As Long = 0 _ ) As String
Debug.Print satMakeDigestFromXml("cfdv33a-base.xml")
' 0ff1274e51fbb090489588d832bb1b5b36543302decbb0a5490839b8c99e8755
Form the pipe string (cadena) from an XML file.
Public Function satMakePipeStringFromXml ( _ strXmlFile As String, _ Optional nOptions As Long = 0 _ ) As String
SAT_ENCODE_LATIN1
to encode in Latin1. The default (0) is SAT_ENCODE_UTF8
for UTF-8.
Add SAT_TFD
to create the cadena original del Timbre Fiscal Digital instead.
Add SAT_XML_OVERRIDE_REQD
to override strict checks for required nodes (advanced users).
Debug.Print satMakePipeStringFromXml("cfdv33a-base.xml")
' ||3.3|A|123ABC|2017-12-04T01:23:59|02|...[cut]...||
Create the signature as a base64 string ready to be inserted as a Sello
node.
Public Function satMakeSignatureFromXml ( _ strXmlFile As String, _ strKeyFile As String, _ strPassword As String, _ Optional nOptions As Long = 0 _ ) As String
Debug.Print satMakeSignatureFromXml("cfdv33a-base.xml", "emisor.key", "12345678a")
' JQJd6rbiMZj1tZVb1Ta8l88bE7pTDm/aAl ...
Get full path name of core DLL module.
Public Function satModuleName() As String
Debug.Print satModuleName()
' C:\WINDOWS\SYSTEM32\diFirmaSAT2.dll
Save keyfile with a new password.
Public Function satNewKeyFile ( _ strOutputFile As String, _ strNewPassword As String, _ strKeyFile As String, _ strKeyPassword As String, _ Optional nOptions As Long = 0 _ ) As Long
SAT_FORMAT_PEM
to output key file in PEM textual format. The default (0) is binary DER encoded.Get platform the core DLL was compiled for.
Public Function satPlatform() As String
Query an X.509 certificate.
Public Function satQueryCert ( _ strFileName As String, _ strQuery As String _ ) As String
Certificado
node.rfc | organizationName | companyName | notAfter | notBefore | serialNumber | sigAlg | keySizerfc to get the subject's RFC (expect 12 or 13 characters)
"sha256WithRSAEncryption"
)"2048"
)fname = "aaa010101aaa_CSD_01.cer" Debug.Print satQueryCert(fname, "rfc") ' AAA010101AAA Debug.Print satQueryCert(fname, "organizationName") ' Servicio de Administración Tributaria
Sign an XML file.
Public Function satSignXml ( _ szOutputFile As String, _ szInputFile As String, _ szKeyFile As String, _ szPassword As String, _ Optional szCertFile As String = "", _ Optional nOptions As Long = 0 _ ) As Long
Sello
, Certificado
and noCertificado
nodes completed.Sello
and (optionally) Certificado
and noCertificado
nodes.SAT_FILE_NO_BOM
to create an output file without a UTF-8 BOM.SAT_FILE_BIGFILE
to speed up the processing of large files.SAT_XML_EMPTYELEMTAG
to create an output file using single empty-element tags "<foo/>"
instead of default start-end tag pair form "<foo></foo>"
.SAT_XML_OVERRIDE_REQD
to override strict checks for required nodes (advanced users).Sello
node overwritten by a new signature value.
Any existing file called szOutputFile
will be overwritten without warning.
If a certificate file szCertFile
is specified then the Certificado
and NoCertificado
nodes will be overwritten in the output file with the values in the certificate.
If a certificate file is not specified then the Certificado
value in the input XML will be used.
For CFD v3.3 the NoCertificado
attribute in the input must be set to the correct certificate serial number before signing.
It is an error (NO_MATCH_ERROR
) if the private key and certificate do not match.n = satSignXml("cfdv33a-signed_new.xml", "cfdv33a-base.xml", "emisor.key", "12345678a", "emisor.cer")
Sign an XML document and output to memory.
Public Function satSignXmlToBytes ( _ strXmlData As String, _ strKeyFile As String, _ strPassword As String, _ strCertFile As String, _ nOptions As Long _ ) As Byte()
SAT_XML_EMPTYELEMTAG
to create an output file using single empty-element tags "<foo/>"
instead of default start-end tag pair form "<foo></foo>"
.SAT_XML_OVERRIDE_REQD
to override strict checks for required nodes (advanced users).NO_MATCH_ERROR
) if the private key and certificate do not match.Dim xmlbytes() As Byte xmlbytes = satSignXmlToBytes(xmlstring, keyfiledata, password, certfiledata)
Sign an XML document and output to a string in memory.
Public Function satSignXmlToString ( _ strXmlData As String, _ strKeyFile As String, _ strPassword As String, _ Optional strCertFile As String = "", _ Optional nOptions As Long = 0 _ ) As String
SAT_XML_EMPTYELEMTAG
to create an output file using single empty-element tags "<foo/>"
instead of default start-end tag pair form "<foo></foo>"
.SAT_XML_OVERRIDE_REQD
to override strict checks for required nodes (advanced users).NO_MATCH_ERROR
) if the private key and certificate do not match.Generate a Universally Unique IDentifier (UUID) compliant with RFC 4122.
Public Function satUuid() As String
Debug.Print satUuid()
' 984dc15f-a929-4cb4-af92-b5782d758161
Validate an XML file against SAT specifications.
Public Function satValidateXml ( _ strXmlFile As String, _ Optional nOptions As Long = SAT_XML_STRICT _ ) As Long
SAT_XML_LOOSE
to loosen strict checks on XML restrictions. Default (0) is SAT_XML_STRICT
.Verify the signature (sello) in an XML file.
Public Function satVerifySignature ( _ strXmlFile As String, _ Optional strCertFile As String = vbNullString _ ) As Long
Debug.Print satVerifySignature("cfdv33a-signed.xml") ' 0 Debug.Print satVerifySignature("cfdv33a-badsig.xml") ' -15 Debug.Print satErrorLookup(-15) ' Decryption error/De error de descifrado (DECRYPT_ERROR)
Get version number of core DLL.
Public Function satVersion() As Long
Major*10000 + Minor*100 + Release
. For example, version 9.1.2 would return 90102
.Create a PFX (PKCS-12) file in PEM format suitable for a Cancelación.
Public Function satWritePfxFile ( _ strOutputFile As String, _ strPfxPassword As String, _ strKeyFile As String, _ strKeyPassword As String, _ strCertFile As String, _ Optional nOptions As Long = 0 _ ) As Long
SAT_FORMAT_PEM
for a PKCS12 file in PEM textual form,
or SAT_FORMAT_BINARY
for a PKCS12/PFX file in binary DER form.
The default (0) output is plain base64.Find version number of Comprobante element or ID number for other document types.
Public Function satXmlReceiptVersion ( _ strXmlFile As String, _ Optional nOptions As Long = 0 _ ) As Long
SAT_GEN_DIGALG
to retrieve default digest algorithm for the document.33 = Comprobante document with Version="3.3" 32 = Comprobante document with version="3.2" 22 = Comprobante document with version="2.2" 1010 = Retenciones document with Version="1.0" 2011/2013 = CatalogoCuentas document with Version="1.1"/"1.3" 2111/2113 = BalanzaComprobacion document with Version="1.1"/"1.3" 2211/2213 = PolizasPeriodo document with Version="1.1"/"1.3" 2312/2313 = AuxiliarFolios document with Version="1.2"/"1.3" 2411/2413 = AuxiliarCtas document with Version="1.1"/"1.3" 2511 = SelloDigitalContElec document with Version="1.1" 4011 = ControlesVolumetricos document with Version="1.1"Return values with
nOptions=SAT_GEN_DIGALG
:
1 = SHA-1 is default digest algorithm for document 256 = SHA-256 is default digest algorithm for document
Extract message digest from the selloSAT node in Timbre Fiscal Digital of CFDI document.
Public Function tfdExtractDigestFromSignature ( _ strXmlFile As String, _ strCertFile As String _ ) As String
Debug.Print tfdExtractDigestFromSignature("cfdv33a-signed-tfd.xml", "pac.cer")
' E94E472557C2460BF9178125AA8D56DA1B4565A814240F1945D25BB396FDA1ED
Compute message digest of cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.
Public Function tfdMakeDigestFromXml ( _ strXmlFile As String, _ Optional nOptions As Long = 0 _ ) As String
Debug.Print tfdMakeDigestFromXml("cfdv33a-signed-tfd.xml")
' e94e472557c2460bf9178125aa8d56da1b4565a814240f1945d25bb396fda1ed
Form the cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.
Public Function tfdMakePipeStringFromXml ( _ strXmlFile As String _ ) As String
Create the selloSAT
signature as a base64 string from TFD data in CFDI XML document.
Public Function tfdMakeSignatureFromXml ( _ strXmlFile As String, _ strKeyFile As String, _ strPassword As String, _ Optional nOptions As Long = 0 _ ) As String
Verify the selloSAT
signature in TFD element.
Public Function tfdVerifySignature ( _ strXmlFile As String, _ strCertFile As String _ ) As Long