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

Objective-C
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoCert.h>
#import <CkoPdf.h>

BOOL success = NO;

CkoStringBuilder *stringCompare = [[CkoStringBuilder alloc] init];
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this credentials file outside public web content and source control.
CkoJsonObject *credentials = [[CkoJsonObject alloc] init];
credentials.Strict = YES;
success = [credentials LoadFile: @"qa_data/aws/kms.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not load private KMS configuration.");
    return;
}

[stringCompare SetString: [credentials StringOf: @"access_key"]];
BOOL stringMatch1 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"access_key"] intValue] != 1) || (stringMatch1 == YES)) {
    NSLog(@"%@",@"Missing or invalid access_key.");
    return;
}

[stringCompare SetString: [credentials StringOf: @"secret_key"]];
BOOL stringMatch2 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"secret_key"] intValue] != 1) || (stringMatch2 == YES)) {
    NSLog(@"%@",@"Missing or invalid secret_key.");
    return;
}

NSString *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"] == YES) {
    NSLog(@"%@",@"This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.");
    return;
}

[stringCompare SetString: [credentials StringOf: @"key_id"]];
BOOL stringMatch3 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"key_id"] intValue] != 1) || (stringMatch3 == YES)) {
    NSLog(@"%@",@"Missing or invalid key_id.");
    return;
}

CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadFromFile: @"qa_data/certs/myCert.cer"];
if (success != YES) {
    NSLog(@"%@",@"Could not load the public signing certificate.");
    return;
}

CkoJsonObject *cloud = [[CkoJsonObject alloc] init];
//  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
success = [cloud UpdateString: @"service" value: @"aws_kms"];
if (success != YES) {
    NSLog(@"%@",@"Could not set service.");
    return;
}

success = [cloud UpdateString: @"access_key" value: [credentials StringOf: @"access_key"]];
if (success != YES) {
    NSLog(@"%@",@"Could not set access_key.");
    return;
}

success = [cloud UpdateString: @"secret_key" value: [credentials StringOf: @"secret_key"]];
if (success != YES) {
    NSLog(@"%@",@"Could not set secret_key.");
    return;
}

success = [cloud UpdateString: @"region" value: region];
if (success != YES) {
    NSLog(@"%@",@"Could not set region.");
    return;
}

success = [cloud UpdateString: @"key_id" value: [credentials StringOf: @"key_id"]];
if (success != YES) {
    NSLog(@"%@",@"Could not set key_id.");
    return;
}

success = [cert SetCloudSigner: cloud];
if (success != YES) {
    NSLog(@"%@",@"Could not configure the AWS KMS signer; inspect diagnostics privately.");
    return;
}

CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/helloWorld.pdf"];
if (success != YES) {
    NSLog(@"%@",@"Could not load PDF.");
    return;
}

CkoJsonObject *options = [[CkoJsonObject alloc] init];
success = [options UpdateInt: @"page" value: [NSNumber numberWithInt: 1]];
if (success != YES) {
    NSLog(@"%@",@"Could not set signature page.");
    return;
}

success = [options UpdateString: @"hashAlgorithm" value: @"sha256"];
if (success != YES) {
    NSLog(@"%@",@"Could not set hashAlgorithm.");
    return;
}

success = [options UpdateString: @"appearance.y" value: @"top"];
if (success != YES) {
    NSLog(@"%@",@"Could not set appearance.y.");
    return;
}

success = [options UpdateString: @"appearance.x" value: @"left"];
if (success != YES) {
    NSLog(@"%@",@"Could not set appearance.x.");
    return;
}

success = [options UpdateString: @"appearance.fontScale" value: @"10.0"];
if (success != YES) {
    NSLog(@"%@",@"Could not set appearance.fontScale.");
    return;
}

success = [options UpdateString: @"appearance.text[0]" value: @"Digitally signed by: cert_cn"];
if (success != YES) {
    NSLog(@"%@",@"Could not set appearance.text[0].");
    return;
}

success = [options UpdateString: @"appearance.text[1]" value: @"current_dt"];
if (success != YES) {
    NSLog(@"%@",@"Could not set appearance.text[1].");
    return;
}

success = [pdf SetSigningCert: cert];
if (success != YES) {
    NSLog(@"%@",@"Could not select signing certificate.");
    return;
}

success = [pdf SignPdf: options outFilePath: @"qa_output/hello_kms_signed.pdf"];
if (success != YES) {
    NSLog(@"%@",@"PDF signing failed; inspect diagnostics privately.");
    return;
}

NSLog(@"%@",@"Signed PDF saved to qa_output/hello_kms_signed.pdf");