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
(PureBasic) Create XML Signature using Java KeyStore (.jks)Demonstrates how to create an XML digital signature using a certificate and private key from a Java KeyStore (.jks)
IncludeFile "CkXml.pb" IncludeFile "CkXmlDSigGen.pb" IncludeFile "CkJavaKeyStore.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkCertChain.pb" IncludeFile "CkCert.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. ; The SOAP XML to be signed in this example contains the following: ; <?xml version="1.0" encoding="UTF-8" standalone="no" ?> ; <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> ; <SOAP-ENV:Header> ; <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1"></wsse:Security> ; </SOAP-ENV:Header> ; <SOAP-ENV:Body xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" SOAP-SEC:id="Body"> ; <z:FooBar xmlns:z="http://example.com" /> ; </SOAP-ENV:Body> ; </SOAP-ENV:Envelope> ; ; Build the XML to sign. ; Use this online tool to generate the code from sample XML: ; Generate Code to Create XML xml.i = CkXml::ckCreate() If xml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xml, "SOAP-ENV:Envelope") CkXml::ckAddAttribute(xml,"xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/") CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"SOAP-ENV:mustUnderstand","1") CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Body",1,"xmlns:SOAP-SEC","http://schemas.xmlsoap.org/soap/security/2000-12") CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Body",1,"SOAP-SEC:id","Body") CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Body|z:FooBar",1,"xmlns:z","http://example.com") ; Load a JavaKeyStore file containing the certificate + private key. jks.i = CkJavaKeyStore::ckCreate() If jks.i = 0 Debug "Failed to create object." ProcedureReturn EndIf password.s = "secret" success.i = CkJavaKeyStore::ckLoadFile(jks,password,"qa_data/jks/test_secret.jks") If success <> 1 Debug CkJavaKeyStore::ckLastErrorText(jks) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) ProcedureReturn EndIf ; Make sure we have a private key. If CkJavaKeyStore::ckNumPrivateKeys(jks) < 1 Debug "No private key available." CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) ProcedureReturn EndIf ; ------------------------------------------------------------------------- ; Get the certificate chain associated with the 1st (and probably only) private key in the JKS. chain.i = CkJavaKeyStore::ckGetCertChain(jks,0) If CkJavaKeyStore::ckLastMethodSuccess(jks) <> 1 Debug CkJavaKeyStore::ckLastErrorText(jks) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) ProcedureReturn EndIf cert.i = CkCertChain::ckGetCert(chain,0) If CkCertChain::ckLastMethodSuccess(chain) <> 1 Debug CkCertChain::ckLastErrorText(chain) CkCertChain::ckDispose(chain) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) ProcedureReturn EndIf CkCertChain::ckDispose(chain) ; Verify again that this cert has a private key. If CkCert::ckHasPrivateKey(cert) <> 1 Debug "Certificate has no associated private key." CkCert::ckDispose(cert) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) ProcedureReturn EndIf ; Prepare for signing... ; Use this online tool to generate the following code from an already-signed XML sample: ; Generate Code to Create an XML Signature gen.i = CkXmlDSigGen::ckCreate() If gen.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Indicate where the Signature will be inserted. CkXmlDSigGen::setCkSigLocation(gen, "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security") ; Add a reference to the fragment of the XML to be signed. CkXmlDSigGen::ckAddSameDocRef(gen,"Body","sha1","EXCL_C14N","","") ; (You can read about the SignedInfoPrefixList in the online reference documentation. It's optional..) CkXmlDSigGen::setCkSignedInfoPrefixList(gen, "wsse SOAP-ENV") ; Provide the private key for signing via the certificate, and indicate that ; we want the base64 of the certificate embedded in the KeyInfo. CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data") CkXmlDSigGen::setCkX509Type(gen, "Certificate") ; Note: Because our certificate was loaded from a JKS which also contained the private key, ; Chilkat automatically knows and has the private key associated with the certificate. ; We set bUsePrivateKey to tell the SetX509Cert method to automatically use the private key ; associated with the certificate for signing. bUsePrivateKey.i = 1 success = CkXmlDSigGen::ckSetX509Cert(gen,cert,bUsePrivateKey) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkCert::ckDispose(cert) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) CkXmlDSigGen::ckDispose(gen) ProcedureReturn EndIf CkCert::ckDispose(cert) ; Everything's specified. Now create and insert the Signature sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckGetXmlSb(xml,sbXml) success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) CkXmlDSigGen::ckDispose(gen) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; Examine the XML with the digital signature inserted Debug CkStringBuilder::ckGetAsString(sbXml) CkXml::ckDispose(xml) CkJavaKeyStore::ckDispose(jks) CkXmlDSigGen::ckDispose(gen) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.