Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) IPS MX Signature - Digitally Sign MX DocumentSee more XML Digital Signatures ExamplesDemonstrates how to digitally sign ISO 20022 SWIFT MX messages. Note: This example requires Chilkat v9.5.0.89 or later. Additional internal functionality was added to auto-recognize SWIFT MX messages and do what is appropriate to satisfy the SWIFT MX specifications.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoXmlToSign Handle hoGen Handle hoObject1 Variant vCert Handle hoCert Variant vSbXml Handle hoSbXml Handle hoVerifier Handle hoVerifyCert Variant vPubKey Handle hoPubKey Integer iNumSigs Integer iVerifyIdx Boolean iVerified String sTemp1 // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Move True To iSuccess // First create the XML to be signed, or load it from a file, or a string, // To load XML from a file: Get Create (RefClass(cComChilkatXml)) To hoXmlToSign If (Not(IsComObjectCreated(hoXmlToSign))) Begin Send CreateComObject of hoXmlToSign End Get ComLoadXmlFile Of hoXmlToSign "c:/someDir/mx_document.xml" To iSuccess // Or to load XML from a string Get ComLoadXml Of hoXmlToSign "..." To iSuccess // Or create the XML directly. Send ComClear To hoXmlToSign // Use this online tool to generate code from sample XML: // Generate Code to Create XML Set ComTag Of hoXmlToSign To "DataPDU" Get ComAddAttribute Of hoXmlToSign "xmlns" "urn:cma:stp:xsd:stp.1.0" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "Body|AppHdr" True "xmlns" "urn:iso:std:iso:20022:tech:xsd:head.001.001.01" To iSuccess Send ComUpdateChildContent To hoXmlToSign "Body|AppHdr|Fr|FIId|FinInstnId|BICFI" "ZZZZZZZZ" Send ComUpdateChildContent To hoXmlToSign "Body|AppHdr|To|FIId|FinInstnId|BICFI" "YYYYYYYYYY" Send ComUpdateChildContent To hoXmlToSign "Body|AppHdr|BizMsgIdr" "ZZZZZZZZAXXX999999999999999999999" Send ComUpdateChildContent To hoXmlToSign "Body|AppHdr|MsgDefIdr" "pacs.008.001.08" Send ComUpdateChildContent To hoXmlToSign "Body|AppHdr|BizSvc" "IPS" Send ComUpdateChildContent To hoXmlToSign "Body|AppHdr|CreDt" "2017-09-13T18:18:00Z" Get ComUpdateAttrAt Of hoXmlToSign "Body|Document" True "xmlns" "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08" To iSuccess Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|GrpHdr|MsgId" "ZZZZZZZZAXXX999999999999999999999" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|GrpHdr|CreDtTm" "2017-09-13T18:18:00" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|GrpHdr|NbOfTxs" "1" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|GrpHdr|SttlmInf|SttlmMtd" "CLRG" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|EndToEndId" "NOTPROVIDED" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|TxId" "ZZZZZZZZAXXX999999999999999999999" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|ClrChanl" "RTNS" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|LclInstrm|Prtry" "CSCT" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|CtgyPurp|Prtry" "001" Get ComUpdateAttrAt Of hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt" True "Ccy" "JOD" To iSuccess Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt" "71.12" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmDt" "2018-01-14" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|ChrgBr" "SLEV" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstgAgt|FinInstnId|BICFI" "ZZZZZZZZ" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstdAgt|FinInstnId|BICFI" "UBSIJOA0" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Dbtr|Nm" "John Johnson" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAcct|Id|IBAN" "JO22CITI00000000000555555555" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|BICFI" "ZZZZZZZZ" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|Id" "200004" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|SchmeNm|Prtry" "1700099999" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgtAcct|Id|IBAN" "JO66CITI22222222222222222222" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|BICFI" "UBSIJOA0" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|Id" "210027" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|SchmeNm|Prtry" "1400199999" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgtAcct|Id|IBAN" "JO44UBSI33333333333333333333" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Cdtr|Nm" "Omega Jones" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAcct|Id|IBAN" "JO95UBSI00000000000777777777" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstrForNxtAgt|InstrInf" "/BNF/Details" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Purp|Prtry" "5814" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf" "SOMEINFORMATIONABOUTPAYMENT-1" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[1]" "SOMEINFORMATIONABOUTPAYMENT-2" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[2]" "SOMEINFORMATIONABOUTPAYMENT-3" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Cdtr|TaxId" "9900083901" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Dbtr|TaxId" "1000387561" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd" "EDV UCUN ODENIR" Send ComUpdateChildContent To hoXmlToSign "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd[1]" "EXTRA INFO" // The following XML is to be signed: // <?xml version="1.0" encoding="UTF-8"?> // <DataPDU xmlns="urn:cma:stp:xsd:stp.1.0"> // <Body> // <AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01"> // <Fr> // <FIId> // <FinInstnId> // <BICFI>ZZZZZZZZ</BICFI> // </FinInstnId> // </FIId> // </Fr> // <To> // <FIId> // <FinInstnId> // <BICFI>YYYYYYYYYY</BICFI> // </FinInstnId> // </FIId> // </To> // <BizMsgIdr>ZZZZZZZZAXXX999999999999999999999</BizMsgIdr> // <MsgDefIdr>pacs.008.001.08</MsgDefIdr> // <BizSvc>IPS</BizSvc> // <CreDt>2017-09-13T18:18:00Z</CreDt> // </AppHdr> // <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08"> // <FIToFICstmrCdtTrf> // <GrpHdr> // <MsgId>ZZZZZZZZAXXX999999999999999999999</MsgId> // <CreDtTm>2017-09-13T18:18:00</CreDtTm> // <NbOfTxs>1</NbOfTxs> // <SttlmInf> // <SttlmMtd>CLRG</SttlmMtd> // </SttlmInf> // </GrpHdr> // <CdtTrfTxInf> // <PmtId> // <EndToEndId>NOTPROVIDED</EndToEndId> // <TxId>ZZZZZZZZAXXX999999999999999999999</TxId> // </PmtId> // <PmtTpInf> // <ClrChanl>RTNS</ClrChanl> // <LclInstrm> // <Prtry>CSCT</Prtry> // </LclInstrm> // <CtgyPurp> // <Prtry>001</Prtry> // </CtgyPurp> // </PmtTpInf> // <IntrBkSttlmAmt Ccy="JOD">71.12</IntrBkSttlmAmt> // <IntrBkSttlmDt>2018-01-14</IntrBkSttlmDt> // <ChrgBr>SLEV</ChrgBr> // <InstgAgt> // <FinInstnId> // <BICFI>ZZZZZZZZ</BICFI> // </FinInstnId> // </InstgAgt> // <InstdAgt> // <FinInstnId> // <BICFI>UBSIJOA0</BICFI> // </FinInstnId> // </InstdAgt> // <Dbtr> // <Nm>John Johnson</Nm> // </Dbtr> // <DbtrAcct> // <Id> // <IBAN>JO22CITI00000000000555555555</IBAN> // </Id> // </DbtrAcct> // <DbtrAgt> // <FinInstnId> // <BICFI>ZZZZZZZZ</BICFI> // <Othr> // <Id>200004</Id> // <SchmeNm> // <Prtry>1700089999</Prtry> // </SchmeNm> // </Othr> // </FinInstnId> // </DbtrAgt> // <DbtrAgtAcct> // <Id> // <IBAN>JO66CITI22222222222222222222</IBAN> // </Id> // </DbtrAgtAcct> // <CdtrAgt> // <FinInstnId> // <BICFI>UBSIJOA0</BICFI> // <Othr> // <Id>210027</Id> // <SchmeNm> // <Prtry>1400199999</Prtry> // </SchmeNm> // </Othr> // </FinInstnId> // </CdtrAgt> // <CdtrAgtAcct> // <Id> // <IBAN>JO44UBSI33333333333333333333</IBAN> // </Id> // </CdtrAgtAcct> // <Cdtr> // <Nm>Omega Jones</Nm> // </Cdtr> // <CdtrAcct> // <Id> // <IBAN>JO95UBSI00000000000777777777</IBAN> // </Id> // </CdtrAcct> // <InstrForNxtAgt> // <InstrInf>/BNF/Details</InstrInf> // </InstrForNxtAgt> // <Purp> // <Prtry>5814</Prtry> // </Purp> // <RgltryRptg> // <Dtls> // <Inf>SOMEINFORMATIONABOUTPAYMENT-1</Inf> // <Inf>SOMEINFORMATIONABOUTPAYMENT-2</Inf> // <Inf>SOMEINFORMATIONABOUTPAYMENT-3</Inf> // </Dtls> // </RgltryRptg> // <Tax> // <Cdtr> // <TaxId>9900083901</TaxId> // </Cdtr> // <Dbtr> // <TaxId>1000387561</TaxId> // </Dbtr> // </Tax> // <RmtInf> // <Ustrd>EDV UCUN ODENIR</Ustrd> // <Ustrd>EXTRA INFO</Ustrd> // </RmtInf> // </CdtTrfTxInf> // </FIToFICstmrCdtTrf> // </Document> // </Body> // </DataPDU> Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen If (Not(IsComObjectCreated(hoGen))) Begin Send CreateComObject of hoGen End Set ComSigLocation Of hoGen To "DataPDU|Body|AppHdr|Sgntr" Set ComSigLocationMod Of hoGen To 0 Set ComSigNamespacePrefix Of hoGen To "ds" Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#" Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N" Set ComSignedInfoDigestMethod Of hoGen To "sha256" // Set the KeyInfoId before adding references.. Set ComKeyInfoId Of hoGen To "_f9f2c543-e50a-4a50-bd91-50155d27f7e2" // Create an Object to be added to the Signature. Get Create (RefClass(cComChilkatXml)) To hoObject1 If (Not(IsComObjectCreated(hoObject1))) Begin Send CreateComObject of hoObject1 End Set ComTag Of hoObject1 To "xades:QualifyingProperties" Get ComAddAttribute Of hoObject1 "xmlns:xades" "http://uri.etsi.org/01903/v1.3.2#" To iSuccess Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties" True "Id" "_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops" To iSuccess Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime" "TO BE GENERATED BY CHILKAT" Get ComGetXml Of hoObject1 To sTemp1 Get ComAddObject Of hoGen "" sTemp1 "" "" To iSuccess // -------- Reference 1 -------- Get ComAddSameDocRef Of hoGen "_f9f2c543-e50a-4a50-bd91-50155d27f7e2" "sha256" "EXCL_C14N" "" "" To iSuccess // -------- Reference 2 -------- Get ComAddObjectRef Of hoGen "_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops" "sha256" "EXCL_C14N" "" "http://uri.etsi.org/01903/v1.3.2#SignedProperties" To iSuccess // -------- Reference 3 -------- Get ComAddSameDocRef Of hoGen "" "sha256" "EXCL_C14N" "" "" To iSuccess // Provide a certificate + private key. (PFX password is test123) Get Create (RefClass(cComChilkatCert)) To hoCert If (Not(IsComObjectCreated(hoCert))) Begin Send CreateComObject of hoCert End Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoCert To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoCert to vCert Get ComSetX509Cert Of hoGen vCert True To iSuccess Set ComKeyInfoType Of hoGen To "X509Data" Set ComX509Type Of hoGen To "IssuerSerial" // Load XML to be signed... Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml If (Not(IsComObjectCreated(hoSbXml))) Begin Send CreateComObject of hoSbXml End Get pvComObject of hoSbXml to vSbXml Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess // Can alternatively use "CompactSignedXml" Set ComBehaviors Of hoGen To "IndentedSignature,LocalSigningTime" // Sign the XML... Get pvComObject of hoSbXml to vSbXml Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoGen To sTemp1 Showln sTemp1 Procedure_Return End // ----------------------------------------------- // Save the signed XML to a file. Get ComWriteFile Of hoSbXml "qa_output/mx_signed.xml" "utf-8" False To iSuccess Get ComGetAsString Of hoSbXml To sTemp1 Showln sTemp1 // ---------------------------------------- // Verify the signatures we just produced... Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier If (Not(IsComObjectCreated(hoVerifier))) Begin Send CreateComObject of hoVerifier End Get pvComObject of hoSbXml to vSbXml Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End // Important: The above signature did not include the full X.509 certificate. // You must call verifier.SetPublicKey to provide the public key of the certificate required for validation. Get Create (RefClass(cComChilkatCert)) To hoVerifyCert If (Not(IsComObjectCreated(hoVerifyCert))) Begin Send CreateComObject of hoVerifyCert End Get ComLoadFromFile Of hoVerifyCert "qa_data/certs/cert_test123.cer" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoVerifyCert To sTemp1 Showln sTemp1 Procedure_Return End Get ComExportPublicKey Of hoVerifyCert To vPubKey If (IsComObject(vPubKey)) Begin Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey Set pvComObject Of hoPubKey To vPubKey End Get ComSetPublicKey Of hoVerifier vPubKey To iSuccess Send Destroy of hoPubKey Get ComNumSignatures Of hoVerifier To iNumSigs Move 0 To iVerifyIdx While (iVerifyIdx < iNumSigs) Set ComSelector Of hoVerifier To iVerifyIdx Get ComVerifySignature Of hoVerifier True To iVerified If (iVerified <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End Move (iVerifyIdx + 1) To iVerifyIdx Loop Showln "All signatures were successfully verified." End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.