Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoStringCompare
    Handle hoCredentials
    Boolean iStringMatch1
    Boolean iStringMatch2
    String sRegion
    Boolean iStringMatch3
    Variant vCert
    Handle hoCert
    Variant vCloud
    Handle hoCloud
    Handle hoCrypt
    Handle hoSignedAttrs
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
    If (Not(IsComObjectCreated(hoStringCompare))) Begin
        Send CreateComObject of hoStringCompare
    End
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Keep this credentials file outside public web content and source control.
    Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
    If (Not(IsComObjectCreated(hoCredentials))) Begin
        Send CreateComObject of hoCredentials
    End
    Set ComStrict Of hoCredentials To True
    Get ComLoadFile Of hoCredentials "qa_data/aws/kms.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private KMS configuration."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
    Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
        Showln "Missing or invalid access_key."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
    Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
        Showln "Missing or invalid secret_key."
        Procedure_Return
    End

    Move "us-west-2" To sRegion
    //  These convenience-method examples use an access-key/secret-key pair.
    //  Do not silently discard the token belonging to temporary credentials.
    Get ComHasMember Of hoCredentials "session_token" To bTemp1
    If (bTemp1 = True) Begin
        Showln "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "key_id" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
    Get ComJsonTypeOf Of hoCredentials "key_id" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
        Showln "Missing or invalid key_id."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/certs/myCert.cer" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load the public signing certificate."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoCloud
    If (Not(IsComObjectCreated(hoCloud))) Begin
        Send CreateComObject of hoCloud
    End
    //  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
    Get ComUpdateString Of hoCloud "service" "aws_kms" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set service."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    Get ComUpdateString Of hoCloud "access_key" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set access_key."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    Get ComUpdateString Of hoCloud "secret_key" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set secret_key."
        Procedure_Return
    End

    Get ComUpdateString Of hoCloud "region" sRegion To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set region."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "key_id" To sTemp1
    Get ComUpdateString Of hoCloud "key_id" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set key_id."
        Procedure_Return
    End

    Get pvComObject of hoCloud to vCloud
    Get ComSetCloudSigner Of hoCert vCloud To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not configure the AWS KMS signer; inspect diagnostics privately."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Get pvComObject of hoCert to vCert
    Get ComSetSigningCert Of hoCrypt vCert To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not select signing certificate."
        Procedure_Return
    End

    Set ComCadesEnabled Of hoCrypt To True
    Set ComHashAlgorithm Of hoCrypt To "sha256"
    Get Create (RefClass(cComChilkatJsonObject)) To hoSignedAttrs
    If (Not(IsComObjectCreated(hoSignedAttrs))) Begin
        Send CreateComObject of hoSignedAttrs
    End
    Get ComUpdateInt Of hoSignedAttrs "contentType" 1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set signed attribute."
        Procedure_Return
    End

    Get ComUpdateInt Of hoSignedAttrs "signingTime" 1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set signed attribute."
        Procedure_Return
    End

    Get ComUpdateInt Of hoSignedAttrs "messageDigest" 1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set signed attribute."
        Procedure_Return
    End

    Get ComUpdateInt Of hoSignedAttrs "signingCertificateV2" 1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set signed attribute."
        Procedure_Return
    End

    Get ComEmit Of hoSignedAttrs To sTemp1
    Set ComSigningAttributes Of hoCrypt To sTemp1
    //  The attached .p7m includes the original file. KMS signs the digest remotely.
    Get ComCreateP7M Of hoCrypt "qa_data/e-Invoice.xml" "qa_output/signed.p7m" To iSuccess
    If (iSuccess <> True) Begin
        Showln "CAdES signing failed; inspect diagnostics privately."
        Procedure_Return
    End

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


End_Procedure