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
(PowerBuilder) 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.
integer li_rc integer li_Success oleobject loo_XmlToSign oleobject loo_Gen oleobject loo_Object1 oleobject loo_Cert oleobject loo_SbXml oleobject loo_Verifier oleobject loo_VerifyCert oleobject loo_PubKey integer li_NumSigs integer li_VerifyIdx integer li_Verified // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. li_Success = 1 // First create the XML to be signed, or load it from a file, or a string, // To load XML from a file: loo_XmlToSign = create oleobject // Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 li_rc = loo_XmlToSign.ConnectToNewObject("Chilkat.Xml") if li_rc < 0 then destroy loo_XmlToSign MessageBox("Error","Connecting to COM object failed") return end if li_Success = loo_XmlToSign.LoadXmlFile("c:/someDir/mx_document.xml") // Or to load XML from a string li_Success = loo_XmlToSign.LoadXml("...") // Or create the XML directly. loo_XmlToSign.Clear() // Use this online tool to generate code from sample XML: // Generate Code to Create XML loo_XmlToSign.Tag = "DataPDU" loo_XmlToSign.AddAttribute("xmlns","urn:cma:stp:xsd:stp.1.0") loo_XmlToSign.UpdateAttrAt("Body|AppHdr",1,"xmlns","urn:iso:std:iso:20022:tech:xsd:head.001.001.01") loo_XmlToSign.UpdateChildContent("Body|AppHdr|Fr|FIId|FinInstnId|BICFI","ZZZZZZZZ") loo_XmlToSign.UpdateChildContent("Body|AppHdr|To|FIId|FinInstnId|BICFI","YYYYYYYYYY") loo_XmlToSign.UpdateChildContent("Body|AppHdr|BizMsgIdr","ZZZZZZZZAXXX999999999999999999999") loo_XmlToSign.UpdateChildContent("Body|AppHdr|MsgDefIdr","pacs.008.001.08") loo_XmlToSign.UpdateChildContent("Body|AppHdr|BizSvc","IPS") loo_XmlToSign.UpdateChildContent("Body|AppHdr|CreDt","2017-09-13T18:18:00Z") loo_XmlToSign.UpdateAttrAt("Body|Document",1,"xmlns","urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|MsgId","ZZZZZZZZAXXX999999999999999999999") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|CreDtTm","2017-09-13T18:18:00") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|NbOfTxs","1") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|SttlmInf|SttlmMtd","CLRG") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|EndToEndId","NOTPROVIDED") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|TxId","ZZZZZZZZAXXX999999999999999999999") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|ClrChanl","RTNS") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|LclInstrm|Prtry","CSCT") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|CtgyPurp|Prtry","001") loo_XmlToSign.UpdateAttrAt("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt",1,"Ccy","JOD") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt","71.12") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmDt","2018-01-14") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|ChrgBr","SLEV") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstgAgt|FinInstnId|BICFI","ZZZZZZZZ") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstdAgt|FinInstnId|BICFI","UBSIJOA0") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Dbtr|Nm","John Johnson") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAcct|Id|IBAN","JO22CITI00000000000555555555") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|BICFI","ZZZZZZZZ") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|Id","200004") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|SchmeNm|Prtry","1700099999") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgtAcct|Id|IBAN","JO66CITI22222222222222222222") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|BICFI","UBSIJOA0") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|Id","210027") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|SchmeNm|Prtry","1400199999") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgtAcct|Id|IBAN","JO44UBSI33333333333333333333") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Cdtr|Nm","Omega Jones") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAcct|Id|IBAN","JO95UBSI00000000000777777777") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstrForNxtAgt|InstrInf","/BNF/Details") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Purp|Prtry","5814") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf","SOMEINFORMATIONABOUTPAYMENT-1") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[1]","SOMEINFORMATIONABOUTPAYMENT-2") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[2]","SOMEINFORMATIONABOUTPAYMENT-3") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Cdtr|TaxId","9900083901") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Dbtr|TaxId","1000387561") loo_XmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd","EDV UCUN ODENIR") loo_XmlToSign.UpdateChildContent("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> loo_Gen = create oleobject // Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0 li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen") loo_Gen.SigLocation = "DataPDU|Body|AppHdr|Sgntr" loo_Gen.SigLocationMod = 0 loo_Gen.SigNamespacePrefix = "ds" loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" loo_Gen.SignedInfoCanonAlg = "EXCL_C14N" loo_Gen.SignedInfoDigestMethod = "sha256" // Set the KeyInfoId before adding references.. loo_Gen.KeyInfoId = "_f9f2c543-e50a-4a50-bd91-50155d27f7e2" // Create an Object to be added to the Signature. loo_Object1 = create oleobject // Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 li_rc = loo_Object1.ConnectToNewObject("Chilkat.Xml") loo_Object1.Tag = "xades:QualifyingProperties" loo_Object1.AddAttribute("xmlns:xades","http://uri.etsi.org/01903/v1.3.2#") loo_Object1.UpdateAttrAt("xades:SignedProperties",1,"Id","_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops") loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT") loo_Gen.AddObject("",loo_Object1.GetXml(),"","") // -------- Reference 1 -------- loo_Gen.AddSameDocRef("_f9f2c543-e50a-4a50-bd91-50155d27f7e2","sha256","EXCL_C14N","","") // -------- Reference 2 -------- loo_Gen.AddObjectRef("_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops","sha256","EXCL_C14N","","http://uri.etsi.org/01903/v1.3.2#SignedProperties") // -------- Reference 3 -------- loo_Gen.AddSameDocRef("","sha256","EXCL_C14N","","") // Provide a certificate + private key. (PFX password is test123) loo_Cert = create oleobject // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert") li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") if li_Success <> 1 then Write-Debug loo_Cert.LastErrorText destroy loo_XmlToSign destroy loo_Gen destroy loo_Object1 destroy loo_Cert return end if loo_Gen.SetX509Cert(loo_Cert,1) loo_Gen.KeyInfoType = "X509Data" loo_Gen.X509Type = "IssuerSerial" // Load XML to be signed... loo_SbXml = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder") loo_XmlToSign.GetXmlSb(loo_SbXml) // Can alternatively use "CompactSignedXml" loo_Gen.Behaviors = "IndentedSignature,LocalSigningTime" // Sign the XML... li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml) if li_Success <> 1 then Write-Debug loo_Gen.LastErrorText destroy loo_XmlToSign destroy loo_Gen destroy loo_Object1 destroy loo_Cert destroy loo_SbXml return end if // ----------------------------------------------- // Save the signed XML to a file. li_Success = loo_SbXml.WriteFile("qa_output/mx_signed.xml","utf-8",0) Write-Debug loo_SbXml.GetAsString() // ---------------------------------------- // Verify the signatures we just produced... loo_Verifier = create oleobject // Use "Chilkat_9_5_0.XmlDSig" for versions of Chilkat < 10.0.0 li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig") li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml) if li_Success <> 1 then Write-Debug loo_Verifier.LastErrorText destroy loo_XmlToSign destroy loo_Gen destroy loo_Object1 destroy loo_Cert destroy loo_SbXml destroy loo_Verifier return end if // 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. loo_VerifyCert = create oleobject // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 li_rc = loo_VerifyCert.ConnectToNewObject("Chilkat.Cert") li_Success = loo_VerifyCert.LoadFromFile("qa_data/certs/cert_test123.cer") if li_Success <> 1 then Write-Debug loo_VerifyCert.LastErrorText destroy loo_XmlToSign destroy loo_Gen destroy loo_Object1 destroy loo_Cert destroy loo_SbXml destroy loo_Verifier destroy loo_VerifyCert return end if loo_PubKey = loo_VerifyCert.ExportPublicKey() loo_Verifier.SetPublicKey(loo_PubKey) destroy loo_PubKey li_NumSigs = loo_Verifier.NumSignatures li_VerifyIdx = 0 do while li_VerifyIdx < li_NumSigs loo_Verifier.Selector = li_VerifyIdx li_Verified = loo_Verifier.VerifySignature(1) if li_Verified <> 1 then Write-Debug loo_Verifier.LastErrorText destroy loo_XmlToSign destroy loo_Gen destroy loo_Object1 destroy loo_Cert destroy loo_SbXml destroy loo_Verifier destroy loo_VerifyCert return end if li_VerifyIdx = li_VerifyIdx + 1 loop Write-Debug "All signatures were successfully verified." destroy loo_XmlToSign destroy loo_Gen destroy loo_Object1 destroy loo_Cert destroy loo_SbXml destroy loo_Verifier destroy loo_VerifyCert |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.