FirmaSAT 10.50.0

Functions | Index
basFirmaSAT.bas File Reference
The VBA/VB6 interface to FirmaSAT.

Functions

satAsciify
Replace non-ASCII characters in an XML document with XML numeric character references (where permitted). More...
satBytesLen
Find length of byte array. More...
satCheckKeyAndCert
Verify that the public key in an X.509 certificate matches the private key. More...
satComments
Get additional information about the core DLL module. More...
satCompileTime
Get date and time the core DLL module was last compiled. More...
satErrorLookup
Look up message corresponding to error code. More...
satExtractDigestFromSignature
Extract the hex-encoded message digest from the signature (sello) in a signed XML file. More...
satFixBOM
Add a UTF-8 byte order mark (BOM) to a file if not already present. More...
satFormatErrorMessage
Return an error message string for the last error. More...
satGetCertAsString
Get the certificate data as a base64 string. More...
satGetCertExpiry
Get the expiry date of the X.509 certificate in ISO time format. More...
satGetCertNumber
Get serial number of the X.509 certificate in "special" SAT format. More...
satGetCertStart
Get the start date of the X.509 certificate in ISO time format. More...
satGetKeyAsPEMString
Get private key data as a PEM-formatted string. More...
satGetKeyAsString
Get unencrypted private key data as a base64 string. More...
satGetXmlAttribute
Extract attribute data for a given element in an XML file. More...
satInsertCert
Insert certificate information into an XML document and output to a new file. More...
satInsertCertToString
Insert certificate information into an XML document and output to memory. More...
satLastError
Get last error message set by previous function. More...
satLicenceType
Get licence type as a string. More...
satMakeDigestFromXml
Compute the message digest of pipe string (cadena) from an XML file. More...
satMakePipeStringFromXml
Form the pipe string (cadena) from an XML file. More...
satMakeSignatureFromXml
Create the signature as a base64 string ready to be inserted as a Sello node. More...
satModuleName
Get full path name of core DLL module. More...
satNewKeyFile
Save keyfile with a new password. More...
satPlatform
Get platform the core DLL was compiled for. More...
satQueryCert
Query an X.509 certificate. More...
satSignXml
Sign an XML file. More...
satSignXmlToBytes
Sign an XML document and output to memory. More...
satSignXmlToString
Sign an XML document and output to a string in memory. More...
satUuid
Generate a Universally Unique IDentifier (UUID) compliant with RFC 4122. More...
satValidateXml
Validate an XML file against SAT specifications. More...
satVerifySignature
Verify the signature (sello) in an XML file. More...
satVersion
Get version number of core DLL. More...
satWritePfxFile
Create a PFX (PKCS-12) file in PEM format suitable for a Cancelación. More...
satXmlReceiptVersion
Find version number of Comprobante element or ID number for other document types. More...
tfdExtractDigestFromSignature
Extract message digest from the selloSAT node in Timbre Fiscal Digital of CFDI document. More...
tfdMakeDigestFromXml
Compute message digest of cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file. More...
tfdMakePipeStringFromXml
Form the cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file. More...
tfdMakeSignatureFromXml
Create the selloSAT signature as a base64 string from TFD data in CFDI XML document. More...
tfdVerifySignature
Verify the selloSAT signature in TFD element. More...

Function Descriptions

satAsciify

Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).

Syntax

[VBA]
Public Function satAsciify ( _
    strXmlFile As String _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).

Return Value

String: XML document as a string with non-ASCII characters replaced by XML character references.

Remarks

In almost all cases, the output contains only US-ASCII characters and can safely be used as input to other functions without concern for character encoding issues. For example, the character "ó" is replaced by the XML character reference "ó". 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".

Example

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&#xF3;n" RegimenFiscal="622"></cfdi:Emisor>
' ... [etc]

satBytesLen

Find length of byte array.

Syntax

[VBA]
Public Function satBytesLen ( _
    ab() As Byte _
) As Long

Parameters

ab
Input byte array.

Return Value

Long: Number of bytes in array.

Remarks

Safe to use even if array is empty.

Example

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

satCheckKeyAndCert

Verify that the public key in an X.509 certificate matches the private key.

Syntax

[VBA]
Public Function satCheckKeyAndCert ( _
    strKeyFile As String, _
    strPassword As String, _
    strCertFile As String _
) As Long

Parameters

strKeyFile
Encrypted key file
strPassword
Password for encrypted key file.
strCertFile
X.509 certificate file or XML file with a certificado node

Return Value

Long: Zero (0) if keys match, or a nonzero error code (use satErrorLookup to check).

Remarks

This will also verify that the password is correct for the key file.

satComments

Get additional information about the core DLL module.

Syntax

[VBA]
Public Function satComments() As String

Return Value

String: Comments string.

Example

Licensed Developer Edition | Edicion de Desarrollador Licenciado.

satCompileTime

Get date and time the core DLL module was last compiled.

Syntax

[VBA]
Public Function satCompileTime() As String

Return Value

String: Date and time string.

Example

Sep  8 2021 14:33:00

satErrorLookup

Look up message corresponding to error code.

Syntax

[VBA]
Public Function satErrorLookup ( _
    nErrCode As Long _
) As String

Parameters

nErrCode
Code number (may be positive or negative).

Return Value

String: Corresponding error message.

Remarks

Check also satFormatErrorMessage.

Example

Debug.Print satErrorLookup(27)
' Invalid XML format/No valido formato XML (BAD_XML_ERROR)

satExtractDigestFromSignature

Extract the hex-encoded message digest from the signature (sello) in a signed XML file.

Syntax

[VBA]
Public Function satExtractDigestFromSignature ( _
    strXmlFile As String, _
    Optional strCertFile As String = "" _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strCertFile
Certificate file (optional).

Return Value

String: Message digest in hex format or empty string on error.

Remarks

Use tfdExtractDigestFromSignature to operate on the Timbre Fiscal Digital instead.

Example

Debug.Print satExtractDigestFromSignature("cfdv33a-signed.xml")
' 0FF1274E51FBB090489588D832BB1B5B36543302DECBB0A5490839B8C99E8755

satFixBOM

Add a UTF-8 byte order mark (BOM) to a file if not already present.

Syntax

[VBA]
Public Function satFixBOM ( _
    strOutputFile As String, _
    szInputFile As String _
) As Long

Parameters

strOutputFile
Name of new output file to be created.
szInputFile
Name of input file to be processed.

Return Value

Long: Zero (0) on success, or a nonzero error code (use satErrorLookup to check).

satFormatErrorMessage

Return an error message string for the last error.

Syntax

[VBA]
Public Function satFormatErrorMessage ( _
    nErrCode As Long _
) As String

Parameters

nErrCode
Error code returned by last call.

Return Value

String: Error message as a string Error ({errCode}): {errorlookup}[: {lasterror}]

Example

' 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

satGetCertAsString

Get the certificate data as a base64 string.

Syntax

[VBA]
Public Function satGetCertAsString ( _
    strFileName As String _
) As String

Parameters

strFileName
X.509 certificate file or XML file with Certificado node.

Return Value

String: Certificate data as a string of base64 characters.

Remarks

Use to obtain the value for the Certificado node from a .CER file. This string value can be passed instead of the filename for the szCertFile parameter in other functions.

Example

Debug.Print satGetCertAsString("emisor.cer")
' MIIF+TCCA+GgAwIBAgIUMzAwMDEwMDAw ...

satGetCertExpiry

Get the expiry date of the X.509 certificate in ISO time format.

Syntax

[VBA]
Public Function satGetCertExpiry ( _
    strFileName As String _
) As String

Parameters

strFileName
X.509 certificate file or XML file with Certificado node.

Return Value

String: Certificate expiry date in ISO time format 'yyyy-mm-ddThh:nn:ssZ'.

Remarks

The time is GMT (UTC, Zulu time), not local.

Example

Debug.Print satGetCertExpiry("emisor.cer")
' 2021-05-18T03:54:56Z

Deprecated

Prefer satQueryCert with query notAfter.

satGetCertNumber

Get serial number of the X.509 certificate in "special" SAT format.

Syntax

[VBA]
Public Function satGetCertNumber ( _
    strFileName As String _
) As String

Parameters

strFileName
X.509 certificate file or XML file with Certificado node.

Return Value

String: Certificate serial number (20 decimal digits encoded in ASCII).

Example

Debug.Print satGetCertNumber("emisor.cer")
' 30001000000300023708

Deprecated

Prefer satQueryCert with query serialNumber.

satGetCertStart

Get the start date of the X.509 certificate in ISO time format.

Syntax

[VBA]
Public Function satGetCertStart ( _
    strFileName As String _
) As String

Parameters

strFileName
X.509 certificate file or XML file with Certificado node.

Return Value

String: Certificate start date in ISO time format 'yyyy-mm-ddThh:nn:ssZ'.

Remarks

The time is GMT (UTC, Zulu time), not local.

Deprecated

Use satQueryCert with query notBefore.

satGetKeyAsPEMString

Get private key data as a PEM-formatted string.

Syntax

[VBA]
Public Function satGetKeyAsPEMString ( _
    strFileName As String, _
    strPassword As String _
) As String

Parameters

strFileName
Encrypted key file
strPassword
Password for encrypted key file.

Return Value

String: Encrypted private key in PEM format.

Remarks

This string value can be passed instead of the filename for the szKeyFile parameter in other functions.

Example

Debug.Print satGetKeyAsPEMString("emisor.key", "12345678a")
' -----BEGIN ENCRYPTED PRIVATE KEY-----
' MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIAgEAAoIBAQACAggA
' ...
' -----END ENCRYPTED PRIVATE KEY-----

satGetKeyAsString

Get unencrypted private key data as a base64 string.

Syntax

[VBA]
Public Function satGetKeyAsString ( _
    strFileName As String, _
    strPassword As String _
) As String

Parameters

strFileName
Encrypted key file
strPassword
Password for encrypted key file.

Return Value

String: Unencrypted private key in base64-encoded string form suitable for a llaveCertificado node in a Cancelacion element.

Remarks

CAUTION: This outputs your private key in unencrypted form. Using it is a huge security risk. Be careful.

Example

Debug.Print Debug.Print satGetKeyAsString("emisor.key", "12345678a")
' PFJTQUtleVZhbHVlPjxNb2R1bHVzPmwxUnl3Y2dRaU ...

satGetXmlAttribute

Extract attribute data for a given element in an XML file.

Syntax

[VBA]
Public Function satGetXmlAttribute ( _
    strXmlFile As String, _
    strAttributeName As String, _
    strElementName As String _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strAttributeName
Name of attribute to find.
strElementName
Name of element to find or (simplified) xpath expression.

Return Value

String: Attribute value, if found; or "!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).

Remarks

Set strElementName as the empty string "" to specify the root element. Set both strElementName and strAttributeName to "" to find the name of the root element.

Example

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

satInsertCert

Insert certificate information into an XML document and output to a new file.

Syntax

[VBA]
Public Function satInsertCert ( _
    strOutputFile As String, _
    strXmlFile As String, _
    strCertFile As String, _
    Optional nOptions As Long = 0 _
) As Long

Parameters

strOutputFile
Name of new output file to be created.
strXmlFile
Password for encrypted key file.
strCertFile
X.509 certificate file or XML file with a certificado node.
nOptions
Add any of the following options (use the 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.

Return Value

Long: Zero (0) if successful, or a nonzero error code (use satErrorLookup to check).

Remarks

This will create an output XML document copied from the input with the 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.

satInsertCertToString

Insert certificate information into an XML document and output to memory.

Syntax

[VBA]
Public Function satInsertCertToString ( _
    strXmlFile As String, _
    strCertFile As String, _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strCertFile
X.509 certificate file (or a string containing certificate data in PEM or plain base64 form).
nOptions
Use SAT_XML_EMPTYELEMTAG to write all empty elements in the empty-element tag form <foo/>.

Return Value

String: XML document as a string with certificado details completed.

Remarks

Use this function prior to calling satSignXml to complete required NoCertificado attribute.

Example

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")

satLastError

Get last error message set by previous function.

Syntax

[VBA]
Public Function satLastError() As String

Return Value

String: Final error message from last call (may be empty).

Remarks

Check also satFormatErrorMessage.

Example

' 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

satLicenceType

Get licence type as a string.

Syntax

[VBA]
Public Function satLicenceType() As String

Return Value

String: "D" = Developer "T" = Trial.

satMakeDigestFromXml

Compute the message digest of pipe string (cadena) from an XML file.

Syntax

[VBA]
Public Function satMakeDigestFromXml ( _
    strXmlFile As String, _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
nOptions
Option flags (optional).

Return Value

String: Message digest in hex format or empty string on error.

Remarks

Use tfdMakeDigestFromXml to operate on the Timbre Fiscal Digital instead.

Example

Debug.Print satMakeDigestFromXml("cfdv33a-base.xml")
' 0ff1274e51fbb090489588d832bb1b5b36543302decbb0a5490839b8c99e8755

satMakePipeStringFromXml

Form the pipe string (cadena) from an XML file.

Syntax

[VBA]
Public Function satMakePipeStringFromXml ( _
    strXmlFile As String, _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
nOptions
Use 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).

Return Value

String: pipe string in UTF-8 encoding or empty string on error.

Example

Debug.Print satMakePipeStringFromXml("cfdv33a-base.xml")
' ||3.3|A|123ABC|2017-12-04T01:23:59|02|...[cut]...||

satMakeSignatureFromXml

Create the signature as a base64 string ready to be inserted as a Sello node.

Syntax

[VBA]
Public Function satMakeSignatureFromXml ( _
    strXmlFile As String, _
    strKeyFile As String, _
    strPassword As String, _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strKeyFile
Encrypted key file.
strPassword
Password for encrypted key file.
nOptions
Option flags (optional).

Return Value

String: Signature in base64 format, or empty string on error (use satLastError to check).

Remarks

Use tfdMakeSignatureFromXml to operate on the Timbre Fiscal Digital instead.

Example

Debug.Print satMakeSignatureFromXml("cfdv33a-base.xml", "emisor.key", "12345678a")
' JQJd6rbiMZj1tZVb1Ta8l88bE7pTDm/aAl ...

satModuleName

Get full path name of core DLL module.

Syntax

[VBA]
Public Function satModuleName() As String

Return Value

String: File path and name.

Example

Debug.Print satModuleName()
' C:\WINDOWS\SYSTEM32\diFirmaSAT2.dll

satNewKeyFile

Save keyfile with a new password.

Syntax

[VBA]
Public Function satNewKeyFile ( _
    strOutputFile As String, _
    strNewPassword As String, _
    strKeyFile As String, _
    strKeyPassword As String, _
    Optional nOptions As Long = 0 _
) As Long

Parameters

strOutputFile
Name of new output file to be created.
strNewPassword
Password for new key file.
strKeyFile
Name of input key file (or a string containing the key in PEM form).
strKeyPassword
Password for existing key file.
nOptions
Use SAT_FORMAT_PEM to output key file in PEM textual format. The default (0) is binary DER encoded.

Return Value

Long: Zero (0) on success, or a nonzero error code (use satErrorLookup to check).

satPlatform

Get platform the core DLL was compiled for.

Syntax

[VBA]
Public Function satPlatform() As String

Return Value

String: "Win32" or "Win64".

satQueryCert

Query an X.509 certificate.

Syntax

[VBA]
Public Function satQueryCert ( _
    strFileName As String, _
    strQuery As String _
) As String

Parameters

strFileName
X.509 certificate file or XML file with Certificado node.
strQuery
Query string (case insensitive). Valid queries are:
rfc | organizationName | companyName | notAfter | notBefore | serialNumber | sigAlg | keySize
rfc to get the subject's RFC (expect 12 or 13 characters)
orgName or organizationName to get the issuer's organization name (expect "Servicio de Administración Tributaria")
companyName to get organization name of subject. This should be your company name.
notAfter to get the expiry date (ISO format, GMT)
notBefore to get the start date (ISO format, GMT)
serialNumber to get the 20-digit SAT-specific serial number
sigAlg to get the algorithm used to sign the certificate (e.g. "sha256WithRSAEncryption")
keySize to get the size in bits of the certificate's public key (e.g. "2048")

Return Value

String: String containing the result or an empty string if not found or error.

Example

fname = "aaa010101aaa_CSD_01.cer"
Debug.Print satQueryCert(fname, "rfc")
' AAA010101AAA
Debug.Print satQueryCert(fname, "organizationName")
' Servicio de Administración Tributaria

satSignXml

Sign an XML file.

Syntax

[VBA]
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

Parameters

szOutputFile
Name of new output file to be created with Sello, Certificado and noCertificado nodes completed.
szInputFile
Name of input XML file to be processed with empty Sello and (optionally) Certificado and noCertificado nodes.
szKeyFile
Name of encrypted private key file.
szPassword
Password for encrypted key file.
szCertFile
(optional) name of X.509 certificate file to be included in output XML.
nOptions
Add one or more of:
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).

Return Value

Long: Zero on success, or a nonzero error code (use satErrorLookup to check).

Remarks

This will create an output XML document copied from the input with the 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.

Example

n = satSignXml("cfdv33a-signed_new.xml", "cfdv33a-base.xml", "emisor.key", "12345678a", "emisor.cer")

satSignXmlToBytes

Sign an XML document and output to memory.

Syntax

[VBA]
Public Function satSignXmlToBytes ( _
    strXmlData As String, _
    strKeyFile As String, _
    strPassword As String, _
    strCertFile As String, _
    nOptions As Long _
) As Byte()

Parameters

strXmlData
Name of input XML file to be processed (or a string containing XML data).
strKeyFile
Encrypted private key file (or a string containing key data in PEM form).
strPassword
Password for encrypted key file.
strCertFile
(optional) X.509 certificate file (or a string containing certificate data in PEM or plain base64 form).
nOptions
Add 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).

Return Value

Byte(): Signed XML data in a byte array.

Remarks

The output is always encoded in UTF-8. It is an error (NO_MATCH_ERROR) if the private key and certificate do not match.

Example

Dim xmlbytes() As Byte
xmlbytes = satSignXmlToBytes(xmlstring, keyfiledata, password, certfiledata)

satSignXmlToString

Sign an XML document and output to a string in memory.

Syntax

[VBA]
Public Function satSignXmlToString ( _
    strXmlData As String, _
    strKeyFile As String, _
    strPassword As String, _
    Optional strCertFile As String = "", _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlData
Name of input XML file to be processed (or a string containing XML data).
strKeyFile
Encrypted private key file (or a string containing key data in PEM form).
strPassword
Password for encrypted key file.
strCertFile
(optional) X.509 certificate file (or a string containing certificate data in PEM or plain base64 form).
nOptions
Add 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).

Return Value

String: String containing signed XML data.

Remarks

The output string is always encoded in UTF-8. This may print "funny" (prefer satSignXmlToBytes). It is an error (NO_MATCH_ERROR) if the private key and certificate do not match.

satUuid

Generate a Universally Unique IDentifier (UUID) compliant with RFC 4122.

Syntax

[VBA]
Public Function satUuid() As String

Return Value

String: UUID string of exactly 36 characters.

Remarks

The output value will be different each time.

Example

Debug.Print satUuid()
' 984dc15f-a929-4cb4-af92-b5782d758161

satValidateXml

Validate an XML file against SAT specifications.

Syntax

[VBA]
Public Function satValidateXml ( _
    strXmlFile As String, _
    Optional nOptions As Long = SAT_XML_STRICT _
) As Long

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
nOptions
Use SAT_XML_LOOSE to loosen strict checks on XML restrictions. Default (0) is SAT_XML_STRICT.

Return Value

Long: Zero if successful, or a nonzero error code (use satErrorLookup to check).

satVerifySignature

Verify the signature (sello) in an XML file.

Syntax

[VBA]
Public Function satVerifySignature ( _
    strXmlFile As String, _
    Optional strCertFile As String = vbNullString _
) As Long

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strCertFile
Certificate file (optional).

Return Value

Long: Zero if verified OK, or a nonzero error code (use satErrorLookup to check).

Remarks

Use tfdVerifySignature to operate on the Timbre Fiscal Digital instead.

Example

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)

satVersion

Get version number of core DLL.

Syntax

[VBA]
Public Function satVersion() As Long

Return Value

Long: Version number as an integer in form Major*10000 + Minor*100 + Release. For example, version 9.1.2 would return 90102.

satWritePfxFile

Create a PFX (PKCS-12) file in PEM format suitable for a Cancelación.

Syntax

[VBA]
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

Parameters

strOutputFile
Name of new output file to be created.
strPfxPassword
Password to open new PFX file.
strKeyFile
Name of input key file (or a string containing the key in PEM form).
strKeyPassword
Password for existing key file.
strCertFile
Name of X.509 certificate file.
nOptions
Use 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.

Return Value

Long: Zero (0) on success, or a nonzero error code (use satErrorLookup to check).

satXmlReceiptVersion

Find version number of Comprobante element or ID number for other document types.

Syntax

[VBA]
Public Function satXmlReceiptVersion ( _
    strXmlFile As String, _
    Optional nOptions As Long = 0 _
) As Long

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
nOptions
Use SAT_GEN_DIGALG to retrieve default digest algorithm for the document.

Return Value

Long: Version number or ID number, or a negative error code (use satErrorLookup to check).

Remarks

Possible return values:
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

tfdExtractDigestFromSignature

Extract message digest from the selloSAT node in Timbre Fiscal Digital of CFDI document.

Syntax

[VBA]
Public Function tfdExtractDigestFromSignature ( _
    strXmlFile As String, _
    strCertFile As String _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data)
strCertFile
X.509 certificate file of PAC who signed the TFD (mandatory)

Return Value

String: Message digest in hex format or empty string on error.

Example

Debug.Print tfdExtractDigestFromSignature("cfdv33a-signed-tfd.xml", "pac.cer")
' E94E472557C2460BF9178125AA8D56DA1B4565A814240F1945D25BB396FDA1ED

tfdMakeDigestFromXml

Compute message digest of cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.

Syntax

[VBA]
Public Function tfdMakeDigestFromXml ( _
    strXmlFile As String, _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
nOptions
Option flags (optional).

Return Value

String: Message digest in hex format or empty string on error.

Example

Debug.Print tfdMakeDigestFromXml("cfdv33a-signed-tfd.xml")
' e94e472557c2460bf9178125aa8d56da1b4565a814240f1945d25bb396fda1ed

tfdMakePipeStringFromXml

Form the cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.

Syntax

[VBA]
Public Function tfdMakePipeStringFromXml ( _
    strXmlFile As String _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data)

Return Value

String: Pipe string in UTF-8 encoding or empty string on error.

tfdMakeSignatureFromXml

Create the selloSAT signature as a base64 string from TFD data in CFDI XML document.

Syntax

[VBA]
Public Function tfdMakeSignatureFromXml ( _
    strXmlFile As String, _
    strKeyFile As String, _
    strPassword As String, _
    Optional nOptions As Long = 0 _
) As String

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strKeyFile
Encrypted key file.
strPassword
Password for encrypted key file.
nOptions
Option flags (optional).

Return Value

String: Signature in base64 format or empty string on error (use satLastError for more information).

tfdVerifySignature

Verify the selloSAT signature in TFD element.

Syntax

[VBA]
Public Function tfdVerifySignature ( _
    strXmlFile As String, _
    strCertFile As String _
) As Long

Parameters

strXmlFile
Name of input XML file to be processed (or a string containing XML data).
strCertFile
Certificate file (mandatory).

Return Value

Long: Zero if signature is verified, or a nonzero error code (use satErrorLookup to check).

Index

Functions