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.
Chilkat Visual Basic 6.0 Downloads
Dim success As Long
success = 0
Dim stringCompare As New ChilkatStringBuilder
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this credentials file outside public web content and source control.
Dim credentials As New ChilkatJsonObject
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/kms.json")
If (success <> 1) Then
Debug.Print "Could not load private KMS configuration."
Exit Sub
End If
success = stringCompare.SetString(credentials.StringOf("access_key"))
Dim stringMatch1 As Long
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
Debug.Print "Missing or invalid access_key."
Exit Sub
End If
success = stringCompare.SetString(credentials.StringOf("secret_key"))
Dim stringMatch2 As Long
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
Debug.Print "Missing or invalid secret_key."
Exit Sub
End If
Dim region As String
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
Debug.Print "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
Exit Sub
End If
success = stringCompare.SetString(credentials.StringOf("key_id"))
Dim stringMatch3 As Long
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("key_id") <> 1) Or (stringMatch3 = 1)) Then
Debug.Print "Missing or invalid key_id."
Exit Sub
End If
Dim cert As New ChilkatCert
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
If (success <> 1) Then
Debug.Print "Could not load the public signing certificate."
Exit Sub
End If
Dim cloud As New ChilkatJsonObject
' SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
success = cloud.UpdateString("service","aws_kms")
If (success <> 1) Then
Debug.Print "Could not set service."
Exit Sub
End If
success = cloud.UpdateString("access_key",credentials.StringOf("access_key"))
If (success <> 1) Then
Debug.Print "Could not set access_key."
Exit Sub
End If
success = cloud.UpdateString("secret_key",credentials.StringOf("secret_key"))
If (success <> 1) Then
Debug.Print "Could not set secret_key."
Exit Sub
End If
success = cloud.UpdateString("region",region)
If (success <> 1) Then
Debug.Print "Could not set region."
Exit Sub
End If
success = cloud.UpdateString("key_id",credentials.StringOf("key_id"))
If (success <> 1) Then
Debug.Print "Could not set key_id."
Exit Sub
End If
success = cert.SetCloudSigner(cloud)
If (success <> 1) Then
Debug.Print "Could not configure the AWS KMS signer; inspect diagnostics privately."
Exit Sub
End If
Dim crypt As New ChilkatCrypt2
success = crypt.SetSigningCert(cert)
If (success <> 1) Then
Debug.Print "Could not select signing certificate."
Exit Sub
End If
crypt.CadesEnabled = 1
crypt.HashAlgorithm = "sha256"
Dim signedAttrs As New ChilkatJsonObject
success = signedAttrs.UpdateInt("contentType",1)
If (success <> 1) Then
Debug.Print "Could not set signed attribute."
Exit Sub
End If
success = signedAttrs.UpdateInt("signingTime",1)
If (success <> 1) Then
Debug.Print "Could not set signed attribute."
Exit Sub
End If
success = signedAttrs.UpdateInt("messageDigest",1)
If (success <> 1) Then
Debug.Print "Could not set signed attribute."
Exit Sub
End If
success = signedAttrs.UpdateInt("signingCertificateV2",1)
If (success <> 1) Then
Debug.Print "Could not set signed attribute."
Exit Sub
End If
crypt.SigningAttributes = signedAttrs.Emit()
' The attached .p7m includes the original file. KMS signs the digest remotely.
success = crypt.CreateP7M("qa_data/e-Invoice.xml","qa_output/signed.p7m")
If (success <> 1) Then
Debug.Print "CAdES signing failed; inspect diagnostics privately."
Exit Sub
End If
Debug.Print "Attached CAdES-BES signature saved to qa_output/signed.p7m"