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

AWS KMS: Sign a PDF with 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.

Creates a visible signature on page 1. The displayed time is local signing time, not a trusted timestamp. This example does not claim LTV or PAdES conformance; OCSP evidence, certificate validation, and a trusted timestamp require additional configuration and validation. It intentionally does not label the result LTV-enabled merely because signing succeeded.

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_Pdf
oleobject loo_Options

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_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf.LoadFile("qa_data/pdf/helloWorld.pdf")
if li_Success <> 1 then
    Write-Debug "Could not load PDF."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    return
end if

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

li_Success = loo_Options.UpdateInt("page",1)
if li_Success <> 1 then
    Write-Debug "Could not set signature page."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

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

li_Success = loo_Options.UpdateString("appearance.y","top")
if li_Success <> 1 then
    Write-Debug "Could not set appearance.y."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

li_Success = loo_Options.UpdateString("appearance.x","left")
if li_Success <> 1 then
    Write-Debug "Could not set appearance.x."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

li_Success = loo_Options.UpdateString("appearance.fontScale","10.0")
if li_Success <> 1 then
    Write-Debug "Could not set appearance.fontScale."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

li_Success = loo_Options.UpdateString("appearance.text[0]","Digitally signed by: cert_cn")
if li_Success <> 1 then
    Write-Debug "Could not set appearance.text[0]."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

li_Success = loo_Options.UpdateString("appearance.text[1]","current_dt")
if li_Success <> 1 then
    Write-Debug "Could not set appearance.text[1]."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

li_Success = loo_Pdf.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_Pdf
    destroy loo_Options
    return
end if

li_Success = loo_Pdf.SignPdf(loo_Options,"qa_output/hello_kms_signed.pdf")
if li_Success <> 1 then
    Write-Debug "PDF signing failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_Pdf
    destroy loo_Options
    return
end if

Write-Debug "Signed PDF saved to qa_output/hello_kms_signed.pdf"


destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_Cloud
destroy loo_Pdf
destroy loo_Options