Sample code for 30+ languages & platforms
DataFlex

Sign Mexico Pedimento

See more Misc Examples

Add a signature to a Mexico pedimento file.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Boolean iBCRLF
    Handle hoSb
    String sMd5_base64
    Variant vPrivKey
    Handle hoPrivKey
    Handle hoXml
    Handle hoAsn
    Handle hoRsa
    Variant vBdSig
    Handle hoBdSig
    Handle hoCert
    String sSerialHex
    Handle hoSbSerial
    Integer iNumReplaced
    String sTemp1

    Move False To iSuccess

    // 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.
    Move True To iBCRLF
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    // Use CRLF line endings.
    Get ComAppendLine Of hoSb "500|1|3621|4199800|400||" iBCRLF To iSuccess
    Get ComAppendLine Of hoSb "601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||" iBCRLF To iSuccess
    Get ComAppendLine Of hoSb "507|4199800|IM|2006-7888">" iBCRLF To iSuccess
    Get ComAppendLine Of hoSb "507|4199800|MS|2">" iBCRLF To iSuccess

    // Generate the MD5 hash of what we have so far..
    Get ComGetHash Of hoSb "md5" "base64" "utf-8" To sMd5_base64
    Showln "MD5 hash = " sMd5_base64

    // Complete the original file.
    // After signing, we'll update the BASE64_SIGNATURE and CERT_SERIAL
    Get ComAppendLine Of hoSb "800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL|" iBCRLF To iSuccess
    Get ComAppendLine Of hoSb "801|M3621037.222|1|5|011|" iBCRLF To iSuccess

    // We're going to sign the MD5 hash using the private key.
    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadAnyFormatFile Of hoPrivKey "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" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Generate the ASN.1 to be signed.

    // <sequence>
    //     <sequence>
    //         <oid>1.2.840.113549.2.5</oid>
    //         <null/>
    //     </sequence>
    //     <octets>SwxHfaJhG+N3pPqay6UzVA==</octets>
    // </sequence>

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "sequence"
    Send ComUpdateChildContent To hoXml "sequence|oid" "1.2.840.113549.2.5"
    Send ComUpdateChildContent To hoXml "sequence|null" ""
    Send ComUpdateChildContent To hoXml "octets" sMd5_base64

    Get Create (RefClass(cComChilkatAsn)) To hoAsn
    If (Not(IsComObjectCreated(hoAsn))) Begin
        Send CreateComObject of hoAsn
    End
    Get ComGetXml Of hoXml To sTemp1
    Get ComLoadAsnXml Of hoAsn sTemp1 To iSuccess
    Get ComGetEncodedDer Of hoAsn "base64" To sTemp1
    Showln "ASN.1 = " sTemp1

    // Sign with the private key.
    Get Create (RefClass(cComChilkatRsa)) To hoRsa
    If (Not(IsComObjectCreated(hoRsa))) Begin
        Send CreateComObject of hoRsa
    End
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComUsePrivateKey Of hoRsa vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Create the opaque signature.
    Get Create (RefClass(cComChilkatBinData)) To hoBdSig
    If (Not(IsComObjectCreated(hoBdSig))) Begin
        Send CreateComObject of hoBdSig
    End
    Get ComGetEncodedDer Of hoAsn "base64" To sTemp1
    Get ComAppendEncoded Of hoBdSig sTemp1 "base64" To iSuccess
    Get pvComObject of hoBdSig to vBdSig
    Get ComSignRawBd Of hoRsa vBdSig To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // 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|

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSerialNumber Of hoCert To sSerialHex
    // The serial in hex form looks like this:   3330303031303030303030353030303033343136
    // Decode to us-ascii.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbSerial
    If (Not(IsComObjectCreated(hoSbSerial))) Begin
        Send CreateComObject of hoSbSerial
    End
    Get ComDecodeAndAppend Of hoSbSerial sSerialHex "hex" "us-ascii" To iSuccess
    Get ComGetAsString Of hoSbSerial To sTemp1
    Showln "serial number in us-ascii: " sTemp1

    Get ComGetAsString Of hoSbSerial To sTemp1
    Get ComReplace Of hoSb "CERT_SERIAL" sTemp1 To iNumReplaced
    Get ComGetEncoded Of hoBdSig "base64" To sTemp1
    Get ComReplace Of hoSb "BASE64_SIGNATURE" sTemp1 To iNumReplaced

    Showln "------------------------------------"
    Showln "Result:"
    Get ComGetAsString Of hoSb To sTemp1
    Showln sTemp1


End_Procedure