PowerBuilder
PowerBuilder
XML Signature Multiple External References
See more XML Digital Signatures Examples
Demonstrates how to add multiple external references to an XML digital signature.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Gen
oleobject loo_Object1
oleobject loo_Http
oleobject loo_Bd
string ls_Url
oleobject loo_Cert
oleobject loo_SbXml
oleobject loo_Verifier
integer li_Verified
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example creates the following signed XML with multiple external references:
// <?xml version="1.0" encoding="UTF-8"?>
// <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4">
// <ds:SignedInfo>
// <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
// <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
// <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref0" URI="https://isus.ezdrowie.gov.pl/fhir/Patient/123/_history/456">
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </ds:Reference>
// <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref1" URI="https://isus.ezdrowie.gov.pl/fhir/Encounter/124/_history/22">
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </ds:Reference>
// <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref2" URI="https://isus.ezdrowie.gov.pl/fhir/Condition/125/_history/1">
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </ds:Reference>
// <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref3" URI="https://isus.ezdrowie.gov.pl/fhir/Procedure/126/_history/1">
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </ds:Reference>
// <ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops">
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
// </ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </ds:Reference>
// </ds:SignedInfo>
// <ds:SignatureValue Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-sigvalue">...</ds:SignatureValue>
// <ds:KeyInfo>
// <ds:X509Data>
// <ds:X509Certificate>...</ds:X509Certificate>
// <ds:X509SubjectName>...</ds:X509SubjectName>
// <ds:X509IssuerSerial>
// <ds:X509IssuerName>...</ds:X509IssuerName>
// <ds:X509SerialNumber>...</ds:X509SerialNumber>
// </ds:X509IssuerSerial>
// </ds:X509Data>
// </ds:KeyInfo>
// <ds:Object>
// <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" Target="#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4">
// <xades:SignedProperties Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops">
// <xades:SignedSignatureProperties>
// <xades:SigningTime>2021-06-02T07:10:13.306+02:00</xades:SigningTime>
// <xades:SigningCertificate>
// <xades:Cert>
// <xades:CertDigest>
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </xades:CertDigest>
// <xades:IssuerSerial>
// <ds:X509IssuerName>...</ds:X509IssuerName>
// <ds:X509SerialNumber>...</ds:X509SerialNumber>
// </xades:IssuerSerial>
// </xades:Cert>
// <xades:Cert>
// <xades:CertDigest>
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </xades:CertDigest>
// <xades:IssuerSerial>
// <ds:X509IssuerName>...</ds:X509IssuerName>
// <ds:X509SerialNumber>...</ds:X509SerialNumber>
// </xades:IssuerSerial>
// </xades:Cert>
// <xades:Cert>
// <xades:CertDigest>
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
// <ds:DigestValue>...</ds:DigestValue>
// </xades:CertDigest>
// <xades:IssuerSerial>
// <ds:X509IssuerName>...</ds:X509IssuerName>
// <ds:X509SerialNumber>...</ds:X509SerialNumber>
// </xades:IssuerSerial>
// </xades:Cert>
// </xades:SigningCertificate>
// </xades:SignedSignatureProperties>
// </xades:SignedProperties>
// </xades:QualifyingProperties>
// </ds:Object>
// </ds:Signature>
//
li_Success = 1
loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")
if li_rc < 0 then
destroy loo_Gen
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Gen.SigLocation = ""
loo_Gen.SigLocationMod = 0
// Note: ID's simply need to be unique values in the signed XML document. They don't necessarily need to be GUID/UUID formatted.
loo_Gen.SigId = "xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4"
loo_Gen.SigNamespacePrefix = "ds"
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SigValueId = "xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-sigvalue"
loo_Gen.SignedInfoCanonAlg = "C14N"
loo_Gen.SignedInfoDigestMethod = "sha256"
// Create an Object to be added to the Signature.
loo_Object1 = create oleobject
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.AddAttribute("xmlns:xades141","http://uri.etsi.org/01903/v1.4.1#")
loo_Object1.AddAttribute("Target","#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4")
loo_Object1.UpdateAttrAt("xades:SignedProperties",1,"Id","xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT")
// This assumes there are 3 certs in the certificate chain: The signing certificate, an intermediate root, and the root CA.
// Remove the lines with "Cert[2]" if there is no intermediate root...
loo_Object1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2","TO BE GENERATED BY CHILKAT")
loo_Gen.AddObject("",loo_Object1.GetXml(),"","")
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
ls_Url = "https://isus.ezdrowie.gov.pl/fhir/Patient/123/_history/456"
li_Success = loo_Http.QuickGetBd(ls_Url,loo_Bd)
if li_Success <> 1 then
Write-Debug loo_Http.LastErrorText
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
return
end if
// -------- Reference 1 --------
loo_Gen.AddExternalBinaryRef(ls_Url,loo_Bd,"sha256","")
loo_Gen.SetRefIdAttr(ls_Url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref0")
// -------- Reference 2 --------
loo_Bd.Clear()
ls_Url = "https://isus.ezdrowie.gov.pl/fhir/Encounter/789/_history/1"
li_Success = loo_Http.QuickGetBd(ls_Url,loo_Bd)
if li_Success <> 1 then
Write-Debug loo_Http.LastErrorText
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
return
end if
loo_Gen.AddExternalBinaryRef(ls_Url,loo_Bd,"sha256","")
loo_Gen.SetRefIdAttr(ls_Url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref1")
// -------- Reference 3 --------
loo_Bd.Clear()
ls_Url = "https://isus.ezdrowie.gov.pl/fhir/Condition/123456/_history/1"
li_Success = loo_Http.QuickGetBd(ls_Url,loo_Bd)
if li_Success <> 1 then
Write-Debug loo_Http.LastErrorText
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
return
end if
loo_Gen.AddExternalBinaryRef(ls_Url,loo_Bd,"sha256","")
loo_Gen.SetRefIdAttr(ls_Url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref2")
// -------- Reference 4 --------
loo_Bd.Clear()
ls_Url = "https://isus.ezdrowie.gov.pl/fhir/Procedure/123456/_history/1"
li_Success = loo_Http.QuickGetBd(ls_Url,loo_Bd)
if li_Success <> 1 then
Write-Debug loo_Http.LastErrorText
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
return
end if
loo_Gen.AddExternalBinaryRef(ls_Url,loo_Bd,"sha256","")
loo_Gen.SetRefIdAttr(ls_Url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref3")
// -------- Reference 5 --------
loo_Gen.AddObjectRef("xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops","sha256","C14N","","http://uri.etsi.org/01903#SignedProperties")
// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
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_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
destroy loo_Cert
return
end if
loo_Gen.SetX509Cert(loo_Cert,1)
loo_Gen.KeyInfoType = "X509Data"
loo_Gen.X509Type = "IssuerSerial,SubjectName,Certificate"
// This will be an enveloping signature where the Signature element
// is the XML document root, the signed data is contained within Object
// tag(s) within the Signature.
// Therefore, pass an empty sbXml to CreateXmlDsigSb.
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")
loo_Gen.Behaviors = "IndentedSignature"
// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
Write-Debug loo_Gen.LastErrorText
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
destroy loo_Cert
destroy loo_SbXml
return
end if
// -----------------------------------------------
// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("qa_output/signedXml.xml","utf-8",0)
Write-Debug loo_SbXml.GetAsString()
// ----------------------------------------
// Verify the signature we just produced...
loo_Verifier = create oleobject
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_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
destroy loo_Cert
destroy loo_SbXml
destroy loo_Verifier
return
end if
li_Verified = loo_Verifier.VerifySignature(1)
if li_Verified <> 1 then
Write-Debug loo_Verifier.LastErrorText
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
destroy loo_Cert
destroy loo_SbXml
destroy loo_Verifier
return
end if
Write-Debug "This signature was successfully verified."
destroy loo_Gen
destroy loo_Object1
destroy loo_Http
destroy loo_Bd
destroy loo_Cert
destroy loo_SbXml
destroy loo_Verifier