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

AWS KMS: Create an Attached CAdES-BES Signature

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.

Creates an attached CMS/PKCS#7 .p7m with SHA-256 and the signingCertificateV2 signed attribute. This is a CAdES-BES example, without a TSA timestamp or long-term validation evidence. The signed input can be any file, not only XML. It is not an XMLDSig signature.

AWS Sign reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
string ls_Region
long li_StringMatch3
oleobject loo_Cert
oleobject loo_Cloud
oleobject loo_Crypt
oleobject loo_SignedAttrs

li_Success = 0

loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_StringCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this credentials file outside public web content and source control.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/kms.json")
if li_Success <> 1 then
    Write-Debug "Could not load private KMS configuration."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

ls_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.
if loo_Credentials.HasMember("session_token") = 1 then
    Write-Debug "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("key_id"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("key_id") <> 1) OR (li_StringMatch3 = 1) then
    Write-Debug "Missing or invalid key_id."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("qa_data/certs/myCert.cer")
if li_Success <> 1 then
    Write-Debug "Could not load the public signing certificate."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    return
end if

loo_Cloud = create oleobject
li_rc = loo_Cloud.ConnectToNewObject("Chilkat.JsonObject")

//  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
li_Success = loo_Cloud.UpdateString("service","aws_kms")
if li_Success <> 1 then
    Write-Debug "Could not set service."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("access_key",loo_Credentials.StringOf("access_key"))
if li_Success <> 1 then
    Write-Debug "Could not set access_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("secret_key",loo_Credentials.StringOf("secret_key"))
if li_Success <> 1 then
    Write-Debug "Could not set secret_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("region",ls_Region)
if li_Success <> 1 then
    Write-Debug "Could not set region."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("key_id",loo_Credentials.StringOf("key_id"))
if li_Success <> 1 then
    Write-Debug "Could not set key_id."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cert.SetCloudSigner(loo_Cloud)
if li_Success <> 1 then
    Write-Debug "Could not configure the AWS KMS signer; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

li_Success = loo_Crypt.SetSigningCert(loo_Cert)
if li_Success <> 1 then
    Write-Debug "Could not select signing certificate."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Crypt
    return
end if

loo_Crypt.CadesEnabled = 1
loo_Crypt.HashAlgorithm = "sha256"
loo_SignedAttrs = create oleobject
li_rc = loo_SignedAttrs.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_SignedAttrs.UpdateInt("contentType",1)
if li_Success <> 1 then
    Write-Debug "Could not set signed attribute."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Crypt
    destroy loo_SignedAttrs
    return
end if

li_Success = loo_SignedAttrs.UpdateInt("signingTime",1)
if li_Success <> 1 then
    Write-Debug "Could not set signed attribute."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Crypt
    destroy loo_SignedAttrs
    return
end if

li_Success = loo_SignedAttrs.UpdateInt("messageDigest",1)
if li_Success <> 1 then
    Write-Debug "Could not set signed attribute."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Crypt
    destroy loo_SignedAttrs
    return
end if

li_Success = loo_SignedAttrs.UpdateInt("signingCertificateV2",1)
if li_Success <> 1 then
    Write-Debug "Could not set signed attribute."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Crypt
    destroy loo_SignedAttrs
    return
end if

loo_Crypt.SigningAttributes = loo_SignedAttrs.Emit()
//  The attached .p7m includes the original file. KMS signs the digest remotely.
li_Success = loo_Crypt.CreateP7M("qa_data/e-Invoice.xml","qa_output/signed.p7m")
if li_Success <> 1 then
    Write-Debug "CAdES signing failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Crypt
    destroy loo_SignedAttrs
    return
end if

Write-Debug "Attached CAdES-BES signature saved to qa_output/signed.p7m"


destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_Cloud
destroy loo_Crypt
destroy loo_SignedAttrs