Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.6.0+

AWS KMS: Sign a SOAP Body with XMLDSig and SHA-256

See more AWS KMS Examples

Uses the current AWS KMS API. Configure the region and keep credentials in private qa_data/aws/kms.json, with nonempty string fields access_key and secret_key. The examples do not obtain or refresh credentials. Keep diagnostics private; they may contain credentials or sensitive key data. Create the output directory before running file-writing examples.

This convenience-method example configures an access-key/secret-key pair. It explicitly rejects a supplied session_token instead of silently omitting it: token support for this helper configuration has not been established here. Prefer temporary role credentials in production using a documented token-capable integration. The separate Rest examples support a session token. No credentials or private key material are printed.

Load a local public certificate whose public key matches an enabled asymmetric KMS key with KeyUsage=SIGN_VERIFY. These certificate signing examples target RSA or NIST EC signing keys; they do not demonstrate every KMS algorithm (such as ML-DSA). Set the nonempty string key_id in the private configuration to the actual key ID or ARN in the configured region. A key ARN avoids alias retargeting. The caller needs kms:Sign and access through the key policy. KMS holds the private key and signs the digest; Chilkat creates the document locally. This does not upload the whole document to KMS. A valid signature alone does not establish certificate trust, revocation status, or a trusted signing time.

Signs the SOAP Body by its ID using exclusive canonicalization and SHA-256 for both the reference digest and SignedInfo signature. Embeds the public certificate and verifies the signature and reference before saving. This is XMLDSig, not XAdES: no XAdES qualifying properties are created. Adapt WS-Security namespaces and profile requirements to the receiving service; the unsigned SOAP headers are not covered by this Body-only signature.

AWS Sign reference

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @stringCompare int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @stringCompare OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    --  Keep this credentials file outside public web content and source control.
    DECLARE @credentials int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT

    EXEC sp_OASetProperty @credentials, 'Strict', 1
    EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/kms.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private KMS configuration.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch1 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch1 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@stringMatch1 = 1)
      BEGIN

        PRINT 'Missing or invalid access_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch2 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch2 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
    IF (@iTmp0 <> 1) or (@stringMatch2 = 1)
      BEGIN

        PRINT 'Missing or invalid secret_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    DECLARE @region nvarchar(4000)
    SELECT @region = 'us-west-2'
    --  These convenience-method examples use an access-key/secret-key pair.
    --  Do not silently discard the token belonging to temporary credentials.
    EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
    IF @iTmp0 = 1
      BEGIN

        PRINT 'This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'key_id'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch3 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch3 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'key_id'
    IF (@iTmp0 <> 1) or (@stringMatch3 = 1)
      BEGIN

        PRINT 'Missing or invalid key_id.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'qa_data/certs/myCert.cer'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load the public signing certificate.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    DECLARE @cloud int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @cloud OUT

    --  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
    EXEC sp_OAMethod @cloud, 'UpdateString', @success OUT, 'service', 'aws_kms'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set service.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @cloud, 'UpdateString', @success OUT, 'access_key', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not set access_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OAMethod @cloud, 'UpdateString', @success OUT, 'secret_key', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not set secret_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        RETURN
      END
    EXEC sp_OAMethod @cloud, 'UpdateString', @success OUT, 'region', @region
    IF @success <> 1
      BEGIN

        PRINT 'Could not set region.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'key_id'
    EXEC sp_OAMethod @cloud, 'UpdateString', @success OUT, 'key_id', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not set key_id.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        RETURN
      END
    EXEC sp_OAMethod @cert, 'SetCloudSigner', @success OUT, @cloud
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure the AWS KMS signer; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        RETURN
      END
    --  Create the XML to be signed.
    DECLARE @sbXml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXml OUT

    DECLARE @bCrlf int
    SELECT @bCrlf = 1
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '    <SOAP-ENV:Header>', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '        <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>', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '    </SOAP-ENV:Header>', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '    <SOAP-ENV:Body xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" SOAP-SEC:id="Body">', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '        <z:FooBar xmlns:z="http://example.com" />', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '    </SOAP-ENV:Body>', @bCrlf
    EXEC sp_OAMethod @sbXml, 'AppendLine', @success OUT, '</SOAP-ENV:Envelope>', @bCrlf

    DECLARE @gen int
    EXEC @hr = sp_OACreate 'Chilkat.XmlDSigGen', @gen OUT

    EXEC sp_OASetProperty @gen, 'SigLocation', 'SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security'
    EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'EXCL_C14N'
    EXEC sp_OASetProperty @gen, 'SignedInfoDigestMethod', 'sha256'
    EXEC sp_OASetProperty @gen, 'SignedInfoPrefixList', 'wsse SOAP-ENV'
    EXEC sp_OASetProperty @gen, 'KeyInfoType', 'X509Data'
    EXEC sp_OASetProperty @gen, 'X509Type', 'Certificate'
    --  The reference selects the element with SOAP-SEC:id="Body".
    EXEC sp_OAMethod @gen, 'AddSameDocRef', @success OUT, 'Body', 'sha256', 'EXCL_C14N', '', ''
    IF @success <> 1
      BEGIN

        PRINT 'Could not add the SOAP Body reference.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @gen
        RETURN
      END
    EXEC sp_OAMethod @gen, 'SetX509Cert', @success OUT, @cert, 1
    IF @success <> 1
      BEGIN

        PRINT 'Could not select signing certificate.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @gen
        RETURN
      END
    EXEC sp_OAMethod @gen, 'CreateXmlDSigSb', @success OUT, @sbXml
    IF @success <> 1
      BEGIN

        PRINT 'XML signing failed; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @gen
        RETURN
      END
    --  Verify the signature and references before saving. This does not validate trust.
    DECLARE @verifier int
    EXEC @hr = sp_OACreate 'Chilkat.XmlDSig', @verifier OUT

    EXEC sp_OAMethod @verifier, 'LoadSignatureSb', @success OUT, @sbXml
    IF @success <> 1
      BEGIN

        PRINT 'Could not load generated signature.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @verifier
        RETURN
      END
    EXEC sp_OAMethod @verifier, 'VerifySignature', @success OUT, 1
    IF @success <> 1
      BEGIN

        PRINT 'Generated XML signature or reference verification failed.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @verifier
        RETURN
      END
    EXEC sp_OAMethod @sbXml, 'WriteFile', @success OUT, 'qa_output/kms_signed.xml', 'utf-8', 0
    IF @success <> 1
      BEGIN

        PRINT 'Could not save signed XML.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @cloud
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @verifier
        RETURN
      END

    PRINT 'Verified XMLDSig saved to qa_output/kms_signed.xml'

    EXEC @hr = sp_OADestroy @stringCompare
    EXEC @hr = sp_OADestroy @credentials
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @cloud
    EXEC @hr = sp_OADestroy @sbXml
    EXEC @hr = sp_OADestroy @gen
    EXEC @hr = sp_OADestroy @verifier


END
GO