AutoIt
AutoIt
Sign Mexico Pedimento
See more Misc Examples
Add a signature to a Mexico pedimento file.Chilkat AutoIt Downloads
Local $bSuccess = False
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; This is the contents before signing:
; 500|1|3621|4199800|400||
; 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
; 507|4199800|IM|2006-7888">
; 507|4199800|MS|2">
; 800|4199800|1">
; 801|M3621037.222|1|5|011|
; This is the contents after signing
; 500|1|3621|4199800|400||
; 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
; 507|4199800|IM|2006-7888">
; 507|4199800|MS|2">
; 800|4199800|1|fhP2Ker54D2+3+UZch23F0E72 .... 9qNSPIuAqpj524qLZbbA==|30001000000500003416|
; 801|M3621037.222|1|5|011|
; First create the text to be signed.
Local $bCRLF = True
$oSb = ObjCreate("Chilkat.StringBuilder")
; Use CRLF line endings.
$oSb.AppendLine("500|1|3621|4199800|400||",$bCRLF)
$oSb.AppendLine("601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||",$bCRLF)
$oSb.AppendLine("507|4199800|IM|2006-7888">",$bCRLF)
$oSb.AppendLine("507|4199800|MS|2">",$bCRLF)
; Generate the MD5 hash of what we have so far..
Local $sMd5_base64 = $oSb.GetHash("md5","base64","utf-8")
ConsoleWrite("MD5 hash = " & $sMd5_base64 & @CRLF)
; Complete the original file.
; After signing, we'll update the BASE64_SIGNATURE and CERT_SERIAL
$oSb.AppendLine("800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL|",$bCRLF)
$oSb.AppendLine("801|M3621037.222|1|5|011|",$bCRLF)
; We're going to sign the MD5 hash using the private key.
$oPrivKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oPrivKey.LoadAnyFormatFile("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.key","12345678a")
If ($bSuccess = False) Then
ConsoleWrite($oPrivKey.LastErrorText & @CRLF)
Exit
EndIf
; Generate the ASN.1 to be signed.
; <sequence>
; <sequence>
; <oid>1.2.840.113549.2.5</oid>
; <null/>
; </sequence>
; <octets>SwxHfaJhG+N3pPqay6UzVA==</octets>
; </sequence>
$oXml = ObjCreate("Chilkat.Xml")
$oXml.Tag = "sequence"
$oXml.UpdateChildContent "sequence|oid","1.2.840.113549.2.5"
$oXml.UpdateChildContent "sequence|null",""
$oXml.UpdateChildContent "octets",$sMd5_base64
$oAsn = ObjCreate("Chilkat.Asn")
$oAsn.LoadAsnXml($oXml.GetXml())
ConsoleWrite("ASN.1 = " & $oAsn.GetEncodedDer("base64") & @CRLF)
; Sign with the private key.
$oRsa = ObjCreate("Chilkat.Rsa")
$bSuccess = $oRsa.UsePrivateKey($oPrivKey)
If ($bSuccess = False) Then
ConsoleWrite($oRsa.LastErrorText & @CRLF)
Exit
EndIf
; Create the opaque signature.
$oBdSig = ObjCreate("Chilkat.BinData")
$oBdSig.AppendEncoded($oAsn.GetEncodedDer("base64"),"base64")
$bSuccess = $oRsa.SignRawBd($oBdSig)
If ($bSuccess = False) Then
ConsoleWrite($oRsa.LastErrorText & @CRLF)
Exit
EndIf
; bd now contains the opaque signature, which embeds the ASN.1, which contains the MD5 hash.
; We're going to add this line:
; 800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL_NUM|
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadFromFile("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.cer")
If ($bSuccess = False) Then
ConsoleWrite($oCert.LastErrorText & @CRLF)
Exit
EndIf
Local $serialHex = $oCert.SerialNumber
; The serial in hex form looks like this: 3330303031303030303030353030303033343136
; Decode to us-ascii.
$oSbSerial = ObjCreate("Chilkat.StringBuilder")
$oSbSerial.DecodeAndAppend($serialHex,"hex","us-ascii")
ConsoleWrite("serial number in us-ascii: " & $oSbSerial.GetAsString() & @CRLF)
Local $iNumReplaced = $oSb.Replace("CERT_SERIAL",$oSbSerial.GetAsString())
$iNumReplaced = $oSb.Replace("BASE64_SIGNATURE",$oBdSig.GetEncoded("base64"))
ConsoleWrite("------------------------------------" & @CRLF)
ConsoleWrite("Result:" & @CRLF)
ConsoleWrite($oSb.GetAsString() & @CRLF)