Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(C) XML Signature Multiple External References

Demonstrates how to add multiple external references to an XML digital signature.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkXmlDSigGen.h>
#include <C_CkXml.h>
#include <C_CkHttp.h>
#include <C_CkBinData.h>
#include <C_CkCert.h>
#include <C_CkStringBuilder.h>
#include <C_CkXmlDSig.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlDSigGen gen;
    HCkXml object1;
    HCkHttp http;
    HCkBinData bd;
    const char *url;
    HCkCert cert;
    HCkStringBuilder sbXml;
    HCkXmlDSig verifier;
    BOOL verified;

    // 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 = TRUE;
    gen = CkXmlDSigGen_Create();

    CkXmlDSigGen_putSigLocation(gen,"");
    CkXmlDSigGen_putSigLocationMod(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_putSigId(gen,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4");
    CkXmlDSigGen_putSigNamespacePrefix(gen,"ds");
    CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGen_putSigValueId(gen,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-sigvalue");
    CkXmlDSigGen_putSignedInfoCanonAlg(gen,"C14N");
    CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha256");

    // Create an Object to be added to the Signature.
    object1 = CkXml_Create();
    CkXml_putTag(object1,"xades:QualifyingProperties");
    CkXml_AddAttribute(object1,"xmlns:xades","http://uri.etsi.org/01903/v1.3.2#");
    CkXml_AddAttribute(object1,"xmlns:xades141","http://uri.etsi.org/01903/v1.4.1#");
    CkXml_AddAttribute(object1,"Target","#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4");
    CkXml_UpdateAttrAt(object1,"xades:SignedProperties",TRUE,"Id","xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops");
    CkXml_UpdateChildContent(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_UpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",TRUE,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[1]|xades:CertDigest|ds:DigestMethod",TRUE,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[1]|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[1]|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[1]|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[2]|xades:CertDigest|ds:DigestMethod",TRUE,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[2]|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[2]|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT");
    CkXml_UpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert[2]|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT");

    CkXmlDSigGen_AddObject(gen,"",CkXml_getXml(object1),"","");

    http = CkHttp_Create();
    bd = CkBinData_Create();

    url = "https://isus.ezdrowie.gov.pl/fhir/Patient/123/_history/456";
    success = CkHttp_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        return;
    }

    // -------- Reference 1 --------
    CkXmlDSigGen_AddExternalBinaryRef(gen,url,bd,"sha256","");
    CkXmlDSigGen_SetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref0");

    // -------- Reference 2 --------
    CkBinData_Clear(bd);
    url = "https://isus.ezdrowie.gov.pl/fhir/Encounter/789/_history/1";
    success = CkHttp_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        return;
    }

    CkXmlDSigGen_AddExternalBinaryRef(gen,url,bd,"sha256","");
    CkXmlDSigGen_SetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref1");

    // -------- Reference 3 --------
    CkBinData_Clear(bd);
    url = "https://isus.ezdrowie.gov.pl/fhir/Condition/123456/_history/1";
    success = CkHttp_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        return;
    }

    CkXmlDSigGen_AddExternalBinaryRef(gen,url,bd,"sha256","");
    CkXmlDSigGen_SetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref2");

    // -------- Reference 4 --------
    CkBinData_Clear(bd);
    url = "https://isus.ezdrowie.gov.pl/fhir/Procedure/123456/_history/1";
    success = CkHttp_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        return;
    }

    CkXmlDSigGen_AddExternalBinaryRef(gen,url,bd,"sha256","");
    CkXmlDSigGen_SetRefIdAttr(gen,url,"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref3");

    // -------- Reference 5 --------
    CkXmlDSigGen_AddObjectRef(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 = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123");
    if (success != TRUE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        CkCert_Dispose(cert);
        return;
    }

    CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGen_putKeyInfoType(gen,"X509Data");
    CkXmlDSigGen_putX509Type(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 = CkStringBuilder_Create();

    CkXmlDSigGen_putBehaviors(gen,"IndentedSignature");

    // Sign the XML...
    success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        printf("%s\n",CkXmlDSigGen_lastErrorText(gen));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        CkCert_Dispose(cert);
        CkStringBuilder_Dispose(sbXml);
        return;
    }

    // -----------------------------------------------

    // Save the signed XML to a file.
    success = CkStringBuilder_WriteFile(sbXml,"qa_output/signedXml.xml","utf-8",FALSE);

    printf("%s\n",CkStringBuilder_getAsString(sbXml));

    // ----------------------------------------
    // Verify the signature we just produced...
    verifier = CkXmlDSig_Create();
    success = CkXmlDSig_LoadSignatureSb(verifier,sbXml);
    if (success != TRUE) {
        printf("%s\n",CkXmlDSig_lastErrorText(verifier));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        CkCert_Dispose(cert);
        CkStringBuilder_Dispose(sbXml);
        CkXmlDSig_Dispose(verifier);
        return;
    }

    verified = CkXmlDSig_VerifySignature(verifier,TRUE);
    if (verified != TRUE) {
        printf("%s\n",CkXmlDSig_lastErrorText(verifier));
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(object1);
        CkHttp_Dispose(http);
        CkBinData_Dispose(bd);
        CkCert_Dispose(cert);
        CkStringBuilder_Dispose(sbXml);
        CkXmlDSig_Dispose(verifier);
        return;
    }

    printf("This signature was successfully verified.\n");


    CkXmlDSigGen_Dispose(gen);
    CkXml_Dispose(object1);
    CkHttp_Dispose(http);
    CkBinData_Dispose(bd);
    CkCert_Dispose(cert);
    CkStringBuilder_Dispose(sbXml);
    CkXmlDSig_Dispose(verifier);

    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.