Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.6.0+

Azure Key Vault: Create an Attached CAdES-BES Signature

See more Azure Key Vault Examples

Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.

Uses Chilkat's cloud convenience integration with client_id, client_secret, tenant_id (directory GUID), vault_name, and cert_name in private configuration. The installed Chilkat library manages authentication and service API versions for this helper; this example does not select a REST api-version. Use the explicit Http examples when that control is required. Keep the runtime current and inspect diagnostics privately.

Set explicit cert_version and load its matching public certificate from qa_data/certs/myCert.cer. Keep the version pinned rather than implicitly following certificate rotation. The backing key must permit signing, and the application needs certificates/get and keys/sign. The private key remains in Key Vault; the document is assembled locally and its digest is signed remotely. A cryptographically valid signature does not establish certificate trust, revocation status, or a trusted signing time.

Creates an attached .p7m with SHA-256 and signingCertificateV2. This example does not add a timestamp or long-term validation evidence.

Microsoft reference

Chilkat Objective-C Downloads

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

BOOL success = NO;

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

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

CkoStringBuilder *validate1 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet1 = [[CkoStringBuilder alloc] init];
success = [validate1 Append: [config StringOf: @"vault_name"]];
if (success != YES) {
    NSLog(@"%@",@"Could not validate vault_name.");
    return;
}

success = [alphabet1 Append: @"abcdefghijklmnopqrstuvwxyz0123456789-"];
if (success != YES) {
    NSLog(@"%@",@"Could not initialize validation.");
    return;
}

if (([validate1.Length intValue] < 3) || ([validate1.Length intValue] > 24)) {
    NSLog(@"%@",@"Invalid vault_name length.");
    return;
}

int charIndex1 = 0;
while ((charIndex1 < [validate1.Length intValue])) {
    if ([alphabet1 Contains: [validate1 GetRange: [NSNumber numberWithInt: charIndex1] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Invalid character in vault_name.");
        return;
    }

    charIndex1 = charIndex1 + 1;
}

CkoStringBuilder *vaultName = [[CkoStringBuilder alloc] init];
[vaultName Append: [config StringOf: @"vault_name"]];
CkoStringBuilder *letters = [[CkoStringBuilder alloc] init];
[letters Append: @"abcdefghijklmnopqrstuvwxyz"];
if (([letters Contains: [vaultName GetRange: [NSNumber numberWithInt: 0] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) || ([vaultName EndsWith: @"-" caseSensitive: YES] == YES) || ([vaultName Contains: @"--" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Invalid vault_name.");
    return;
}

CkoStringBuilder *origin = [[CkoStringBuilder alloc] init];
success = [origin Append: @"https://"];
if (success != YES) {
    NSLog(@"%@",@"Could not construct URL or identifier.");
    return;
}

success = [origin Append: [config StringOf: @"vault_name"]];
if (success != YES) {
    NSLog(@"%@",@"Could not construct URL or identifier.");
    return;
}

success = [origin Append: @".vault.azure.net"];
if (success != YES) {
    NSLog(@"%@",@"Could not construct URL or identifier.");
    return;
}

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

CkoStringBuilder *validate2 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet2 = [[CkoStringBuilder alloc] init];
success = [validate2 Append: [config StringOf: @"cert_name"]];
if (success != YES) {
    NSLog(@"%@",@"Could not validate cert_name.");
    return;
}

success = [alphabet2 Append: @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"];
if (success != YES) {
    NSLog(@"%@",@"Could not initialize validation.");
    return;
}

if (([validate2.Length intValue] < 1) || ([validate2.Length intValue] > 127)) {
    NSLog(@"%@",@"Invalid cert_name length.");
    return;
}

int charIndex2 = 0;
while ((charIndex2 < [validate2.Length intValue])) {
    if ([alphabet2 Contains: [validate2 GetRange: [NSNumber numberWithInt: charIndex2] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Invalid character in cert_name.");
        return;
    }

    charIndex2 = charIndex2 + 1;
}

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

CkoStringBuilder *validate3 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet3 = [[CkoStringBuilder alloc] init];
success = [validate3 Append: [config StringOf: @"cert_version"]];
if (success != YES) {
    NSLog(@"%@",@"Could not validate cert_version.");
    return;
}

success = [alphabet3 Append: @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"];
if (success != YES) {
    NSLog(@"%@",@"Could not initialize validation.");
    return;
}

if (([validate3.Length intValue] < 1) || ([validate3.Length intValue] > 127)) {
    NSLog(@"%@",@"Invalid cert_version length.");
    return;
}

int charIndex3 = 0;
while ((charIndex3 < [validate3.Length intValue])) {
    if ([alphabet3 Contains: [validate3 GetRange: [NSNumber numberWithInt: charIndex3] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Invalid character in cert_version.");
        return;
    }

    charIndex3 = charIndex3 + 1;
}

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

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

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

CkoStringBuilder *validate4 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet4 = [[CkoStringBuilder alloc] init];
success = [validate4 Append: [config StringOf: @"tenant_id"]];
if (success != YES) {
    NSLog(@"%@",@"Could not validate tenant GUID.");
    return;
}

success = [alphabet4 Append: @"0123456789abcdefABCDEF-"];
if (success != YES) {
    NSLog(@"%@",@"Could not initialize validation.");
    return;
}

if (([validate4.Length intValue] < 36) || ([validate4.Length intValue] > 36)) {
    NSLog(@"%@",@"Invalid tenant GUID length.");
    return;
}

int charIndex4 = 0;
while ((charIndex4 < [validate4.Length intValue])) {
    if ([alphabet4 Contains: [validate4 GetRange: [NSNumber numberWithInt: charIndex4] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Invalid character in tenant GUID.");
        return;
    }

    charIndex4 = charIndex4 + 1;
}

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];
success = [cloud UpdateString: @"service" value: @"azure_keyvault"];
if (success != YES) {
    NSLog(@"%@",@"Could not set service.");
    return;
}

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

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

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

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

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

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

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

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

crypt.CadesEnabled = YES;
crypt.HashAlgorithm = @"sha256";
CkoJsonObject *signedAttrs = [[CkoJsonObject alloc] init];
success = [signedAttrs UpdateInt: @"contentType" value: [NSNumber numberWithInt: 1]];
if (success != YES) {
    NSLog(@"%@",@"Could not set signed attribute.");
    return;
}

success = [signedAttrs UpdateInt: @"signingTime" value: [NSNumber numberWithInt: 1]];
if (success != YES) {
    NSLog(@"%@",@"Could not set signed attribute.");
    return;
}

success = [signedAttrs UpdateInt: @"messageDigest" value: [NSNumber numberWithInt: 1]];
if (success != YES) {
    NSLog(@"%@",@"Could not set signed attribute.");
    return;
}

success = [signedAttrs UpdateInt: @"signingCertificateV2" value: [NSNumber numberWithInt: 1]];
if (success != YES) {
    NSLog(@"%@",@"Could not set signed attribute.");
    return;
}

crypt.SigningAttributes = [signedAttrs Emit];
//  The attached .p7m includes the original file. Key Vault signs the digest remotely.
success = [crypt CreateP7M: @"qa_data/e-Invoice.xml" p7mPath: @"qa_output/signed.p7m"];
if (success != YES) {
    NSLog(@"%@",@"CAdES signing failed; inspect diagnostics privately.");
    return;
}

NSLog(@"%@",@"Attached CAdES-BES signature saved to qa_output/signed.p7m");