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 Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
credentials: HCkJsonObject;
stringMatch1: Boolean;
stringMatch2: Boolean;
region: PWideChar;
stringMatch3: Boolean;
cert: HCkCert;
cloud: HCkJsonObject;
crypt: HCkCrypt2;
signedAttrs: HCkJsonObject;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/kms.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private KMS configuration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'access_key'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'secret_key'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
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 (CkJsonObject_HasMember(credentials,'session_token') = True) then
begin
Memo1.Lines.Add('This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'key_id'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'key_id') <> 1) or (stringMatch3 = True)) then
begin
Memo1.Lines.Add('Missing or invalid key_id.');
Exit;
end;
cert := CkCert_Create();
success := CkCert_LoadFromFile(cert,'qa_data/certs/myCert.cer');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load the public signing certificate.');
Exit;
end;
cloud := CkJsonObject_Create();
// SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
success := CkJsonObject_UpdateString(cloud,'service','aws_kms');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set service.');
Exit;
end;
success := CkJsonObject_UpdateString(cloud,'access_key',CkJsonObject__stringOf(credentials,'access_key'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set access_key.');
Exit;
end;
success := CkJsonObject_UpdateString(cloud,'secret_key',CkJsonObject__stringOf(credentials,'secret_key'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set secret_key.');
Exit;
end;
success := CkJsonObject_UpdateString(cloud,'region',region);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set region.');
Exit;
end;
success := CkJsonObject_UpdateString(cloud,'key_id',CkJsonObject__stringOf(credentials,'key_id'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_id.');
Exit;
end;
success := CkCert_SetCloudSigner(cert,cloud);
if (success <> True) then
begin
Memo1.Lines.Add('Could not configure the AWS KMS signer; inspect diagnostics privately.');
Exit;
end;
crypt := CkCrypt2_Create();
success := CkCrypt2_SetSigningCert(crypt,cert);
if (success <> True) then
begin
Memo1.Lines.Add('Could not select signing certificate.');
Exit;
end;
CkCrypt2_putCadesEnabled(crypt,True);
CkCrypt2_putHashAlgorithm(crypt,'sha256');
signedAttrs := CkJsonObject_Create();
success := CkJsonObject_UpdateInt(signedAttrs,'contentType',1);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set signed attribute.');
Exit;
end;
success := CkJsonObject_UpdateInt(signedAttrs,'signingTime',1);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set signed attribute.');
Exit;
end;
success := CkJsonObject_UpdateInt(signedAttrs,'messageDigest',1);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set signed attribute.');
Exit;
end;
success := CkJsonObject_UpdateInt(signedAttrs,'signingCertificateV2',1);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set signed attribute.');
Exit;
end;
CkCrypt2_putSigningAttributes(crypt,CkJsonObject__emit(signedAttrs));
// The attached .p7m includes the original file. KMS signs the digest remotely.
success := CkCrypt2_CreateP7M(crypt,'qa_data/e-Invoice.xml','qa_output/signed.p7m');
if (success <> True) then
begin
Memo1.Lines.Add('CAdES signing failed; inspect diagnostics privately.');
Exit;
end;
Memo1.Lines.Add('Attached CAdES-BES signature saved to qa_output/signed.p7m');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkCert_Dispose(cert);
CkJsonObject_Dispose(cloud);
CkCrypt2_Dispose(crypt);
CkJsonObject_Dispose(signedAttrs);