PureBasic
PureBasic
Create XML Signature using Java KeyStore (.jks)
See more XML Digital Signatures Examples
Demonstrates how to create an XML digital signature using a certificate and private key from a Java KeyStore (.jks)Chilkat PureBasic Downloads
IncludeFile "CkXml.pb"
IncludeFile "CkXmlDSigGen.pb"
IncludeFile "CkJavaKeyStore.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkCertChain.pb"
IncludeFile "CkCert.pb"
Procedure ChilkatExample()
success.i = 0
; 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 = CkJavaKeyStore::ckLoadFile(jks,password,"qa_data/jks/test_secret.jks")
If success = 0
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 = CkCertChain::ckCreate()
If chain.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJavaKeyStore::ckCertChainAt(jks,0,chain)
If success = 0
Debug CkJavaKeyStore::ckLastErrorText(jks)
CkXml::ckDispose(xml)
CkJavaKeyStore::ckDispose(jks)
CkCertChain::ckDispose(chain)
ProcedureReturn
EndIf
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkCertChain::ckCertAt(chain,0,cert)
If success = 0
Debug CkCertChain::ckLastErrorText(chain)
CkXml::ckDispose(xml)
CkJavaKeyStore::ckDispose(jks)
CkCertChain::ckDispose(chain)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
; Verify again that this cert has a private key.
If CkCert::ckHasPrivateKey(cert) <> 1
Debug "Certificate has no associated private key."
CkXml::ckDispose(xml)
CkJavaKeyStore::ckDispose(jks)
CkCertChain::ckDispose(chain)
CkCert::ckDispose(cert)
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)
CkXml::ckDispose(xml)
CkJavaKeyStore::ckDispose(jks)
CkCertChain::ckDispose(chain)
CkCert::ckDispose(cert)
CkXmlDSigGen::ckDispose(gen)
ProcedureReturn
EndIf
; 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 = 0
Debug CkXmlDSigGen::ckLastErrorText(gen)
CkXml::ckDispose(xml)
CkJavaKeyStore::ckDispose(jks)
CkCertChain::ckDispose(chain)
CkCert::ckDispose(cert)
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)
CkCertChain::ckDispose(chain)
CkCert::ckDispose(cert)
CkXmlDSigGen::ckDispose(gen)
CkStringBuilder::ckDispose(sbXml)
ProcedureReturn
EndProcedure