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) XML Signature Multiple External ReferencesDemonstrates how to add multiple external references to an XML digital signature.
IncludeFile "CkBinData.pb" IncludeFile "CkHttp.pb" IncludeFile "CkXmlDSigGen.pb" IncludeFile "CkXmlDSig.pb" IncludeFile "CkXml.pb" IncludeFile "CkCert.pb" IncludeFile "CkStringBuilder.pb" Procedure ChilkatExample() ; 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> ; success.i = 1 gen.i = CkXmlDSigGen::ckCreate() If gen.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXmlDSigGen::setCkSigLocation(gen, "") CkXmlDSigGen::setCkSigLocationMod(gen, 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. CkXmlDSigGen::setCkSigId(gen, "xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4") CkXmlDSigGen::setCkSigNamespacePrefix(gen, "ds") CkXmlDSigGen::setCkSigNamespaceUri(gen, "http://www.w3.org/2000/09/xmldsig#") CkXmlDSigGen::setCkSigValueId(gen, "xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-sigvalue") CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "C14N") CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha256") ; Create an Object to be added to the Signature. object1.i = CkXml::ckCreate() If object1.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(object1, "xades:QualifyingProperties") CkXml::ckAddAttribute(object1,"xmlns:xades","http://uri.etsi.org/01903/v1.3.2#") CkXml::ckAddAttribute(object1,"xmlns:xades141","http://uri.etsi.org/01903/v1.4.1#") CkXml::ckAddAttribute(object1,"Target","#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4") CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties",1,"Id","xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops") CkXml::ckUpdateChildContent(object1,"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... CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2","TO BE GENERATED BY CHILKAT") CkXmlDSigGen::ckAddObject(gen,"",CkXml::ckGetXml(object1),"","") http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bd.i = CkBinData::ckCreate() If bd.i = 0 Debug "Failed to create object." ProcedureReturn EndIf url.s = "https://isus.ezdrowie.gov.pl/fhir/Patient/123/_history/456" success = CkHttp::ckQuickGetBd(http,url,bd) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) ProcedureReturn EndIf ; -------- Reference 1 -------- CkXmlDSigGen::ckAddExternalBinaryRef(gen,url,bd,"sha256","") CkXmlDSigGen::ckSetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref0") ; -------- Reference 2 -------- CkBinData::ckClear(bd) url = "https://isus.ezdrowie.gov.pl/fhir/Encounter/789/_history/1" success = CkHttp::ckQuickGetBd(http,url,bd) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) ProcedureReturn EndIf CkXmlDSigGen::ckAddExternalBinaryRef(gen,url,bd,"sha256","") CkXmlDSigGen::ckSetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref1") ; -------- Reference 3 -------- CkBinData::ckClear(bd) url = "https://isus.ezdrowie.gov.pl/fhir/Condition/123456/_history/1" success = CkHttp::ckQuickGetBd(http,url,bd) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) ProcedureReturn EndIf CkXmlDSigGen::ckAddExternalBinaryRef(gen,url,bd,"sha256","") CkXmlDSigGen::ckSetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref2") ; -------- Reference 4 -------- CkBinData::ckClear(bd) url = "https://isus.ezdrowie.gov.pl/fhir/Procedure/123456/_history/1" success = CkHttp::ckQuickGetBd(http,url,bd) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) ProcedureReturn EndIf CkXmlDSigGen::ckAddExternalBinaryRef(gen,url,bd,"sha256","") CkXmlDSigGen::ckSetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref3") ; -------- Reference 5 -------- CkXmlDSigGen::ckAddObjectRef(gen,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops","sha256","C14N","","http://uri.etsi.org/01903#SignedProperties") ; Provide a certificate + private key. (PFX password is test123) cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123") If success <> 1 Debug CkCert::ckLastErrorText(cert) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) CkCert::ckDispose(cert) ProcedureReturn EndIf CkXmlDSigGen::ckSetX509Cert(gen,cert,1) CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data") CkXmlDSigGen::setCkX509Type(gen, "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. sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature") ; Sign the XML... success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; ----------------------------------------------- ; Save the signed XML to a file. success = CkStringBuilder::ckWriteFile(sbXml,"qa_output/signedXml.xml","utf-8",0) Debug CkStringBuilder::ckGetAsString(sbXml) ; ---------------------------------------- ; Verify the signature we just produced... verifier.i = CkXmlDSig::ckCreate() If verifier.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml) If success <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf verified.i = CkXmlDSig::ckVerifySignature(verifier,1) If verified <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf Debug "This signature was successfully verified." CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkHttp::ckDispose(http) CkBinData::ckDispose(bd) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.