PowerBuilder
PowerBuilder
Extract SignatureValue from Signed XML
See more XML Digital Signatures Examples
Demonstrates how to extract the signature value from signed XML.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
string ls_FilePath
oleobject loo_Sb
string ls_Base64Sig
oleobject loo_Xml
oleobject loo_XSigVal
li_Success = 0
// This example will work with the following signed XML, which as been reformatted for readability.
// <?xml version="1.0" encoding="utf-8"?>
// <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">
// <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
// <ds:SignedInfo>
// <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
// <InclusiveNamespaces xmlns="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="wsse SOAP-ENV"/>
// </ds:CanonicalizationMethod>
// <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
// <ds:Reference URI="#Body">
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>OwgHPZNfDkXnZsjpfzXqAcT3RV3HzmTsEy2bP44FJ0M=</ds:DigestValue>
// </ds:Reference>
// </ds:SignedInfo>
// <ds:SignatureValue>C+7FWngU....DJFBcdg==</ds:SignatureValue>
// <ds:KeyInfo>
// <ds:KeyValue>
// <ds:RSAKeyValue>
// <ds:Modulus>sXeRhM55P13.....NAcibRw==</ds:Modulus>
// <ds:Exponent>AQAB</ds:Exponent>
// </ds:RSAKeyValue>
// </ds:KeyValue>
// </ds:KeyInfo>
// </ds:Signature>
// </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>
// -----------------------------------------------------------------------------------------
// There are a number of ways we can get the contents of the <ds:SignatureValue>...</ds:SignatureValue>
ls_FilePath = "qa_data/xml_dsig_testing/sample_to_show_how_to_extract_sig.xml"
// If you know there will only be a single SignatureValue in the XML, you don't need to parse XML.
// Just do quick and simple string search..
// For example:
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_Sb
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Sb.LoadFile(ls_FilePath,"utf-8")
if li_Success = 0 then
Write-Debug "Failed to load file."
destroy loo_Sb
return
end if
ls_Base64Sig = loo_Sb.GetBetween("SignatureValue>","</")
Write-Debug "base64Sig = " + ls_Base64Sig
// -----------------------------------------------------------------------------------------
// If the XML might contain multiple signatures, but the signatures will always be located in the
// same place, then you can (beforehand) copy a sample signed XML into Chilkat's online tool
// at Generate Parsing Code from XML
// to get the path to the SignatureValue. For example, the line of code generated for the SignatureValue
// for the above XML is:
// string ds_SignatureValue = xml.GetChildContent("SOAP-ENV:Header|wsse:Security|ds:Signature|ds:SignatureValue");
// So you can simply use that path..
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
li_Success = loo_Xml.LoadXmlFile(ls_FilePath)
// Assume success..
ls_Base64Sig = loo_Xml.GetChildContent("SOAP-ENV:Header|wsse:Security|ds:Signature|ds:SignatureValue")
Write-Debug "base64Sig = " + ls_Base64Sig
// -----------------------------------------------------------------------------------------
// Or perhaps you don't know where the signature is located in the XML.
// You can search for the tag..
loo_XSigVal = loo_Xml.SearchForTag(loo_Xml,"*:SignatureValue")
if loo_Xml.LastMethodSuccess = 1 then
Write-Debug "base64Sig = " + loo_XSigVal.Content
destroy loo_XSigVal
else
Write-Debug "No SignatureValue found."
end if
destroy loo_Sb
destroy loo_Xml