Sample code for 30+ languages & platforms
PureBasic

Create XAdES for Malaysia E-Invoice

See more Malaysia MyInvois Examples

This example signs XML to create the required XAdES for Malaysia E-Invoice as described at the following web pages:

https://sdk.myinvois.hasil.gov.my/signature/
https://sdk.myinvois.hasil.gov.my/signature-creation/

Note: This example requires Chilkat 10.0.0 or later.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkXmlDSigGen.pb"
IncludeFile "CkXmlDSig.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

    success = 1

    ; Load XML such as the following to be signed:

    ; <?xml version="1.0" encoding="utf-8"?>
    ; <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
    ;     <ext:UBLExtensions>
    ;         <ext:UBLExtension>
    ;             <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI>
    ;             <ext:ExtensionContent>
    ;                 <sig:UBLDocumentSignatures xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">
    ;                     <sac:SignatureInformation>
    ;                         <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID>
    ;                         <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoice</sbc:ReferencedSignatureID>
    ;                     </sac:SignatureInformation>
    ;                 </sig:UBLDocumentSignatures>
    ;             </ext:ExtensionContent>
    ;         </ext:UBLExtension>
    ;     </ext:UBLExtensions>
    ;     <cbc:ID>IV00000105</cbc:ID>
    ;     <cbc:IssueDate>2024-07-06</cbc:IssueDate>
    ;     <cbc:IssueTime>15:06:00Z</cbc:IssueTime>
    ;     <cbc:InvoiceTypeCode listVersionID="1.1">1</cbc:InvoiceTypeCode>
    ;     <cbc:DocumentCurrencyCode>MYR</cbc:DocumentCurrencyCode>
    ;     <cac:BillingReference>
    ;         <cac:AdditionalDocumentReference>
    ;             <cbc:ID>IV00000105</cbc:ID>
    ;         </cac:AdditionalDocumentReference>
    ;     </cac:BillingReference>
    ;     <cac:Signature>
    ;         <cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID>
    ;         <cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod>
    ;     </cac:Signature>
    ;     <cac:AccountingSupplierParty>
    ;         <cac:Party>
    ;             <cbc:IndustryClassificationCode name="Other information technology service activities n.e.c.">62099</cbc:IndustryClassificationCode>
    ;             <cac:PartyIdentification>
    ;                 <cbc:ID schemeID="TIN">C99999999999</cbc:ID>
    ;             </cac:PartyIdentification>
    ;             <cac:PartyIdentification>
    ;                 <cbc:ID schemeID="BRN">200801099999</cbc:ID>
    ;             </cac:PartyIdentification>
    ;             <cac:PostalAddress>
    ;                 <cbc:CityName>Cheras</cbc:CityName>
    ;                 <cbc:PostalZone>56000</cbc:PostalZone>
    ;                 <cbc:CountrySubentityCode>14</cbc:CountrySubentityCode>
    ;                 <cac:AddressLine>
    ;                     <cbc:Line>A-3, 11, Jalan 2/199a,</cbc:Line>
    ;                 </cac:AddressLine>
    ;                 <cac:AddressLine>
    ;                     <cbc:Line>Cheras, 56000 Cheras</cbc:Line>
    ;                 </cac:AddressLine>
    ;                 <cac:AddressLine>
    ;                     <cbc:Line>Wilayah Persekutuan Kuala Lumpur</cbc:Line>
    ;                 </cac:AddressLine>
    ;                 <cac:Country>
    ;                     <cbc:IdentificationCode listAgencyID="6" listID="ISO3166-1">MYS</cbc:IdentificationCode>
    ;                 </cac:Country>
    ;             </cac:PostalAddress>
    ;             <cac:PartyLegalEntity>
    ;                 <cbc:RegistrationName>ABC SYSTEMS SERVICES</cbc:RegistrationName>
    ;             </cac:PartyLegalEntity>
    ;             <cac:Contact>
    ;                 <cbc:Telephone>019-626 9999</cbc:Telephone>
    ;                 <cbc:ElectronicMail>Info@abc.com.my</cbc:ElectronicMail>
    ;             </cac:Contact>
    ;         </cac:Party>
    ;     </cac:AccountingSupplierParty>
    ;     <cac:AccountingCustomerParty>
    ;         <cac:Party>
    ;             <cac:PartyIdentification>
    ;                 <cbc:ID schemeID="TIN">C99999999999</cbc:ID>
    ;             </cac:PartyIdentification>
    ;             <cac:PartyIdentification>
    ;                 <cbc:ID schemeID="BRN">200801099999</cbc:ID>
    ;             </cac:PartyIdentification>
    ;             <cac:PostalAddress>
    ;                 <cbc:CityName>Cheras</cbc:CityName>
    ;                 <cbc:PostalZone>56000</cbc:PostalZone>
    ;                 <cbc:CountrySubentityCode>14</cbc:CountrySubentityCode>
    ;                 <cac:AddressLine>
    ;                     <cbc:Line>A-3, 11, Jalan 2/199a,</cbc:Line>
    ;                 </cac:AddressLine>
    ;                 <cac:AddressLine>
    ;                     <cbc:Line>Cheras, 56000 Cheras</cbc:Line>
    ;                 </cac:AddressLine>
    ;                 <cac:AddressLine>
    ;                     <cbc:Line>Cheras, 56000 Cheras</cbc:Line>
    ;                 </cac:AddressLine>
    ;                 <cac:Country>
    ;                     <cbc:IdentificationCode listAgencyID="6" listID="ISO3166-1">MYS</cbc:IdentificationCode>
    ;                 </cac:Country>
    ;             </cac:PostalAddress>
    ;             <cac:PartyLegalEntity>
    ;                 <cbc:RegistrationName>ABC SYSTEMS SERVICES</cbc:RegistrationName>
    ;             </cac:PartyLegalEntity>
    ;             <cac:Contact>
    ;                 <cbc:Telephone>019-626 9999</cbc:Telephone>
    ;                 <cbc:ElectronicMail>Info@abc.com.my</cbc:ElectronicMail>
    ;             </cac:Contact>
    ;         </cac:Party>
    ;     </cac:AccountingCustomerParty>
    ;     <cac:TaxTotal>
    ;         <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
    ;         <cac:TaxSubtotal>
    ;             <cbc:TaxableAmount currencyID="MYR">40</cbc:TaxableAmount>
    ;             <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
    ;             <cac:TaxCategory>
    ;                 <cbc:ID>6</cbc:ID>
    ;                 <cbc:Percent>0</cbc:Percent>
    ;                 <cac:TaxScheme>
    ;                     <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">OTH</cbc:ID>
    ;                 </cac:TaxScheme>
    ;             </cac:TaxCategory>
    ;         </cac:TaxSubtotal>
    ;     </cac:TaxTotal>
    ;     <cac:LegalMonetaryTotal>
    ;         <cbc:LineExtensionAmount currencyID="MYR">40</cbc:LineExtensionAmount>
    ;         <cbc:TaxExclusiveAmount currencyID="MYR">40</cbc:TaxExclusiveAmount>
    ;         <cbc:TaxInclusiveAmount currencyID="MYR">49</cbc:TaxInclusiveAmount>
    ;         <cbc:AllowanceTotalAmount currencyID="MYR">0</cbc:AllowanceTotalAmount>
    ;         <cbc:PayableAmount currencyID="MYR">49</cbc:PayableAmount>
    ;     </cac:LegalMonetaryTotal>
    ;     <cac:InvoiceLine>
    ;         <cbc:ID>1</cbc:ID>
    ;         <cbc:InvoicedQuantity unitCode="H87">2</cbc:InvoicedQuantity>
    ;         <cbc:LineExtensionAmount currencyID="MYR">40</cbc:LineExtensionAmount>
    ;         <cac:TaxTotal>
    ;             <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
    ;             <cac:TaxSubtotal>
    ;                 <cbc:TaxableAmount currencyID="MYR">40</cbc:TaxableAmount>
    ;                 <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
    ;                 <cac:TaxCategory>
    ;                     <cbc:ID>6</cbc:ID>
    ;                     <cbc:Percent>0</cbc:Percent>
    ;                     <cac:TaxScheme>
    ;                         <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">OTH</cbc:ID>
    ;                     </cac:TaxScheme>
    ;                 </cac:TaxCategory>
    ;             </cac:TaxSubtotal>
    ;         </cac:TaxTotal>
    ;         <cac:Item>
    ;             <cbc:Description>Computer Monitor 24 inch</cbc:Description>
    ;             <cac:CommodityClassification>
    ;                 <cbc:ItemClassificationCode listID="CLASS">3</cbc:ItemClassificationCode>
    ;             </cac:CommodityClassification>
    ;         </cac:Item>
    ;         <cac:Price>
    ;             <cbc:PriceAmount currencyID="MYR">20</cbc:PriceAmount>
    ;         </cac:Price>
    ;         <cac:ItemPriceExtension>
    ;             <cbc:Amount currencyID="MYR">40</cbc:Amount>
    ;         </cac:ItemPriceExtension>
    ;     </cac:InvoiceLine>
    ; </Invoice>

    xmlToSign.i = CkXml::ckCreate()
    If xmlToSign.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXml::ckLoadXmlFile(xmlToSign,"qa_data/xml/myinvois_notYetSigned.xml")
    If success = 0
        Debug "Failed to load the XML to be signed."
        CkXml::ckDispose(xmlToSign)
        ProcedureReturn
    EndIf

    gen.i = CkXmlDSigGen::ckCreate()
    If gen.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; ------------------------------------------------------------------------------------------------
    ; This behavior is required for the peculiarities of the MyInvois implementation.
    ; It requires Chilkat 10.0.0 or later.
    CkXmlDSigGen::setCkBehaviors(gen, "MyInvois")
    ; ------------------------------------------------------------------------------------------------

    CkXmlDSigGen::setCkSigLocation(gen, "Invoice|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation")
    CkXmlDSigGen::setCkSigLocationMod(gen, 0)
    CkXmlDSigGen::setCkSigId(gen, "signature")
    CkXmlDSigGen::setCkSigNamespacePrefix(gen, "ds")
    CkXmlDSigGen::setCkSigNamespaceUri(gen, "http://www.w3.org/2000/09/xmldsig#")
    CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "C14N_11")
    CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha256")

    ; Create an Object to be added to the Signature.
    object1.i = CkXml::ckCreate()
    If object1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(object1, "xades:QualifyingProperties")
    CkXml::ckAddAttribute(object1,"xmlns:xades","http://uri.etsi.org/01903/v1.3.2#")
    CkXml::ckAddAttribute(object1,"Target","signature")
    CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties",1,"Id","id-xades-signed-props")
    CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT")
    CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256")
    CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT")
    CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT")
    CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT")

    CkXmlDSigGen::ckAddObject(gen,"",CkXml::ckGetXml(object1),"","")

    ; -------- Reference 1 --------
    xml1.i = CkXml::ckCreate()
    If xml1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml1, "ds:Transforms")
    CkXml::ckUpdateAttrAt(xml1,"ds:Transform",1,"Algorithm","http://www.w3.org/TR/1999/REC-xpath-19991116")
    CkXml::ckUpdateChildContent(xml1,"ds:Transform|ds:XPath","not(//ancestor-or-self::ext:UBLExtensions)")
    CkXml::ckUpdateAttrAt(xml1,"ds:Transform[1]",1,"Algorithm","http://www.w3.org/TR/1999/REC-xpath-19991116")
    CkXml::ckUpdateChildContent(xml1,"ds:Transform[1]|ds:XPath","not(//ancestor-or-self::cac:Signature)")
    CkXml::ckUpdateAttrAt(xml1,"ds:Transform[2]",1,"Algorithm","http://www.w3.org/2006/12/xml-c14n11")

    CkXmlDSigGen::ckAddSameDocRef2(gen,"","sha256",xml1,"")
    CkXmlDSigGen::ckSetRefIdAttr(gen,"","id-doc-signed-data")

    ; -------- Reference 2 --------
    CkXmlDSigGen::ckAddObjectRef(gen,"id-xades-signed-props","sha256","","","http://www.w3.org/2000/09/xmldsig#SignatureProperties")

    ; Provide a certificate + private key. (PFX password is test123)
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123")
    If success <> 1
        Debug CkCert::ckLastErrorText(cert)
        CkXml::ckDispose(xmlToSign)
        CkXmlDSigGen::ckDispose(gen)
        CkXml::ckDispose(object1)
        CkXml::ckDispose(xml1)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    CkXmlDSigGen::ckSetX509Cert(gen,cert,1)

    CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data")
    CkXmlDSigGen::setCkX509Type(gen, "Certificate")

    ; Load XML to be signed...
    sbXml.i = CkStringBuilder::ckCreate()
    If sbXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkEmitCompact(xmlToSign, 1)
    CkXml::ckGetXmlSb(xmlToSign,sbXml)

    ; Sign the XML...
    success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml)
    If success <> 1
        Debug CkXmlDSigGen::ckLastErrorText(gen)
        CkXml::ckDispose(xmlToSign)
        CkXmlDSigGen::ckDispose(gen)
        CkXml::ckDispose(object1)
        CkXml::ckDispose(xml1)
        CkCert::ckDispose(cert)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    ; -----------------------------------------------

    ; Save the signed XML to a file.
    success = CkStringBuilder::ckWriteFile(sbXml,"c:/temp/qa_output/signedXml.xml","utf-8",0)

    Debug CkStringBuilder::ckGetAsString(sbXml)

    ; ----------------------------------------
    ; Verify the signatures we just produced...
    verifier.i = CkXmlDSig::ckCreate()
    If verifier.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml)
    If success <> 1
        Debug CkXmlDSig::ckLastErrorText(verifier)
        CkXml::ckDispose(xmlToSign)
        CkXmlDSigGen::ckDispose(gen)
        CkXml::ckDispose(object1)
        CkXml::ckDispose(xml1)
        CkCert::ckDispose(cert)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        ProcedureReturn
    EndIf

    ; ----------------------------------------
    ; Make sure to indicate that we are verifying a MyInvois signature
    ; This is because MyInvois does things in a non-standard way..
    CkXmlDSig::setCkUncommonOptions(verifier, "MyInvois")
    ; ----------------------------------------

    numSigs.i = CkXmlDSig::ckNumSignatures(verifier)
    verifyIdx.i = 0
    While verifyIdx < numSigs
        CkXmlDSig::setCkSelector(verifier, verifyIdx)
        verified.i = CkXmlDSig::ckVerifySignature(verifier,1)
        If verified <> 1
            Debug CkXmlDSig::ckLastErrorText(verifier)
            CkXml::ckDispose(xmlToSign)
            CkXmlDSigGen::ckDispose(gen)
            CkXml::ckDispose(object1)
            CkXml::ckDispose(xml1)
            CkCert::ckDispose(cert)
            CkStringBuilder::ckDispose(sbXml)
            CkXmlDSig::ckDispose(verifier)
            ProcedureReturn
        EndIf

        verifyIdx = verifyIdx + 1
    Wend
    Debug "All signatures were successfully verified."


    CkXml::ckDispose(xmlToSign)
    CkXmlDSigGen::ckDispose(gen)
    CkXml::ckDispose(object1)
    CkXml::ckDispose(xml1)
    CkCert::ckDispose(cert)
    CkStringBuilder::ckDispose(sbXml)
    CkXmlDSig::ckDispose(verifier)


    ProcedureReturn
EndProcedure