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.
Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this credentials file outside public web content and source control.
set credentials = Server.CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/kms.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private KMS configuration.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(credentials.StringOf("access_key"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_key.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(credentials.StringOf("secret_key"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid secret_key.") & "</pre>"
Response.End
End If
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 (credentials.HasMember("session_token") = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(credentials.StringOf("key_id"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("key_id") <> 1) Or (stringMatch3 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid key_id.") & "</pre>"
Response.End
End If
set cert = Server.CreateObject("Chilkat.Cert")
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load the public signing certificate.") & "</pre>"
Response.End
End If
set cloud = Server.CreateObject("Chilkat.JsonObject")
' SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
success = cloud.UpdateString("service","aws_kms")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set service.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("access_key",credentials.StringOf("access_key"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set access_key.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("secret_key",credentials.StringOf("secret_key"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set secret_key.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("region",region)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set region.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("key_id",credentials.StringOf("key_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set key_id.") & "</pre>"
Response.End
End If
success = cert.SetCloudSigner(cloud)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not configure the AWS KMS signer; inspect diagnostics privately.") & "</pre>"
Response.End
End If
set pdf = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/helloWorld.pdf")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load PDF.") & "</pre>"
Response.End
End If
set options = Server.CreateObject("Chilkat.JsonObject")
success = options.UpdateInt("page",1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set signature page.") & "</pre>"
Response.End
End If
success = options.UpdateString("hashAlgorithm","sha256")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set hashAlgorithm.") & "</pre>"
Response.End
End If
success = options.UpdateString("appearance.y","top")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set appearance.y.") & "</pre>"
Response.End
End If
success = options.UpdateString("appearance.x","left")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set appearance.x.") & "</pre>"
Response.End
End If
success = options.UpdateString("appearance.fontScale","10.0")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set appearance.fontScale.") & "</pre>"
Response.End
End If
success = options.UpdateString("appearance.text[0]","Digitally signed by: cert_cn")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set appearance.text[0].") & "</pre>"
Response.End
End If
success = options.UpdateString("appearance.text[1]","current_dt")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set appearance.text[1].") & "</pre>"
Response.End
End If
success = pdf.SetSigningCert(cert)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not select signing certificate.") & "</pre>"
Response.End
End If
success = pdf.SignPdf(options,"qa_output/hello_kms_signed.pdf")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "PDF signing failed; inspect diagnostics privately.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Signed PDF saved to qa_output/hello_kms_signed.pdf") & "</pre>"
%>
</body>
</html>