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

Azure Key Vault: Import a PFX with Cert.UploadToCloud

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 cert_name and string pfx_password; load a PFX containing the leaf certificate and its private key from qa_data/pfx/myCertAndKey.pfx. The sample accepts RSA 2048/3072/4096 and NIST EC P-256/P-384/P-521. Import creates a new version if the name already exists. Keep a protected backup and reconcile uncertain failures before retrying. Requires certificates/import. Keep PFX material, request bodies, and diagnostics private; only resource identifiers are printed. The returned leaf certificate is checked against the source.

This shortcut uses the helper's import policy. It does not set the explicit nonexportable policy shown in the low-level Http example; inspect the resulting policy and use the REST example when exportability or key reuse must be controlled. Tags are omitted so certificate identity details are not copied into metadata.

Microsoft reference

Chilkat Objective-C Downloads

Objective-C
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <CkoCert.h>
#import <CkoPrivateKey.h>
#import <CkoPublicKey.h>
#import <NSString.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: @"client_id"]];
BOOL stringMatch3 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"client_id"] intValue] != 1) || (stringMatch3 == YES)) {
    NSLog(@"%@",@"Missing or invalid client_id.");
    return;
}

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

[stringCompare SetString: [config StringOf: @"tenant_id"]];
BOOL stringMatch5 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"tenant_id"] intValue] != 1) || (stringMatch5 == 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;
}

if ([[config JsonTypeOf: @"pfx_password"] intValue] != 1) {
    NSLog(@"%@",@"Missing pfx_password string; empty is allowed for an unprotected PFX.");
    return;
}

CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/pfx/myCertAndKey.pfx" password: [config StringOf: @"pfx_password"]];
if (success != YES) {
    NSLog(@"%@",@"Could not load PFX; inspect diagnostics privately.");
    return;
}

CkoPrivateKey *privateKey = [[CkoPrivateKey alloc] init];
success = [cert GetPrivateKey: privateKey];
if (success != YES) {
    NSLog(@"%@",@"PFX must contain a usable private key.");
    return;
}

//  Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
CkoPublicKey *publicKey = [[CkoPublicKey alloc] init];
success = [privateKey ToPublicKey: publicKey];
if (success != YES) {
    NSLog(@"%@",@"Could not get the public key.");
    return;
}

NSString *publicJwk = [publicKey GetJwk];
if (publicKey.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Could not inspect the public key.");
    return;
}

CkoJsonObject *keyInfo = [[CkoJsonObject alloc] init];
keyInfo.Strict = YES;
success = [keyInfo Load: publicJwk];
if (success != YES) {
    NSLog(@"%@",@"Invalid public JWK.");
    return;
}

BOOL supported = NO;
[stringCompare SetString: [keyInfo StringOf: @"kty"]];
BOOL stringMatch6 = [stringCompare ContentsEqual: @"RSA" caseSensitive: YES];
if (stringMatch6 == YES) {
    supported = ([privateKey.BitLength intValue] == 2048) || ([privateKey.BitLength intValue] == 3072) || ([privateKey.BitLength intValue] == 4096);
}

[stringCompare SetString: [keyInfo StringOf: @"kty"]];
BOOL stringMatch7 = [stringCompare ContentsEqual: @"EC" caseSensitive: YES];
if (stringMatch7 == YES) {
    [stringCompare SetString: [keyInfo StringOf: @"crv"]];
    BOOL stringMatch8 = [stringCompare ContentsEqual: @"P-256" caseSensitive: YES];
    [stringCompare SetString: [keyInfo StringOf: @"crv"]];
    BOOL stringMatch9 = [stringCompare ContentsEqual: @"P-384" caseSensitive: YES];
    [stringCompare SetString: [keyInfo StringOf: @"crv"]];
    BOOL stringMatch10 = [stringCompare ContentsEqual: @"P-521" caseSensitive: YES];
    if ((stringMatch8 == YES) || (stringMatch9 == YES) || (stringMatch10 == YES)) {
        supported = YES;
    }

}

if (supported != YES) {
    NSLog(@"%@",@"This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521.");
    return;
}

CkoJsonObject *options = [[CkoJsonObject alloc] init];
success = [options UpdateString: @"service" value: @"azure-keyvault"];
if (success != YES) {
    NSLog(@"%@",@"Could not set service.");
    return;
}

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

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

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

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

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

CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.Strict = YES;
//  UploadToCloud uses azure-keyvault; SetCloudSigner uses azure_keyvault.
success = [cert UploadToCloud: options jsonOut: result];
if (success != YES) {
    NSLog(@"%@",@"Import failed or its outcome is uncertain. Check the vault before retrying.");
    return;
}

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

success = [certificateBase Append: @"/certificates/"];
if (success != YES) {
    NSLog(@"%@",@"Could not construct URL or identifier.");
    return;
}

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

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

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

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

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

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

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

if ([certificateVersionId StartsWith: [bundlePrefix GetAsString] caseSensitive: YES] != YES) {
    NSLog(@"%@",@"Returned identifier is outside the expected vault/resource.");
    return;
}

NSString *certificateVersion = [certificateVersionId GetRange: bundlePrefix.Length numChars: [NSNumber numberWithInt: ([certificateVersionId.Length intValue] - [bundlePrefix.Length intValue])] removeFlag: NO];
CkoStringBuilder *validate7 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet7 = [[CkoStringBuilder alloc] init];
success = [validate7 Append: certificateVersion];
if (success != YES) {
    NSLog(@"%@",@"Could not validate resource identifier.");
    return;
}

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

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

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

    charIndex7 = charIndex7 + 1;
}

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

success = [keyPrefix Append: @"/keys/"];
if (success != YES) {
    NSLog(@"%@",@"Could not construct URL or identifier.");
    return;
}

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

if ([keyTailId StartsWith: [keyPrefix GetAsString] caseSensitive: YES] != YES) {
    NSLog(@"%@",@"Returned identifier is outside the expected vault/resource.");
    return;
}

NSString *keyTail = [keyTailId GetRange: keyPrefix.Length numChars: [NSNumber numberWithInt: ([keyTailId.Length intValue] - [keyPrefix.Length intValue])] removeFlag: NO];
CkoStringBuilder *keyTailParts = [[CkoStringBuilder alloc] init];
success = [keyTailParts Append: keyTail];
if (success != YES) {
    NSLog(@"%@",@"Could not construct URL or identifier.");
    return;
}

NSString *keyTailName = [keyTailParts GetBefore: @"/" removeFlag: NO];
NSString *keyTailVersion = [keyTailParts GetAfterFinal: @"/" removeFlag: NO];
CkoStringBuilder *validate8 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet8 = [[CkoStringBuilder alloc] init];
success = [validate8 Append: keyTailName];
if (success != YES) {
    NSLog(@"%@",@"Could not validate resource name.");
    return;
}

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

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

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

    charIndex8 = charIndex8 + 1;
}

CkoStringBuilder *validate9 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet9 = [[CkoStringBuilder alloc] init];
success = [validate9 Append: keyTailVersion];
if (success != YES) {
    NSLog(@"%@",@"Could not validate resource version.");
    return;
}

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

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

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

    charIndex9 = charIndex9 + 1;
}

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

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

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

[stringCompare SetString: keyTail];
BOOL stringMatch14 = [stringCompare ContentsEqual: [keyTailCanonical GetAsString] caseSensitive: YES];
if (stringMatch14 != YES) {
    NSLog(@"%@",@"Expected a versioned resource identifier.");
    return;
}

NSString *certificateId = [result StringOf: @"id"];
NSString *keyId = [result StringOf: @"kid"];
CkoCert *remoteCert = [[CkoCert alloc] init];
success = [remoteCert LoadFromBase64: [result StringOf: @"cer"]];
if (success != YES) {
    NSLog(@"%@",@"Could not load the returned public certificate.");
    return;
}

NSString *sourceDer = [cert GetEncoded];
if (cert.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Could not encode source certificate.");
    return;
}

NSString *importedDer = [remoteCert GetEncoded];
if (remoteCert.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Could not encode imported certificate.");
    return;
}

[stringCompare SetString: sourceDer];
BOOL stringMatch15 = [stringCompare ContentsEqual: importedDer caseSensitive: YES];
if (stringMatch15 != YES) {
    NSLog(@"%@",@"Imported certificate does not match the PFX leaf certificate.");
    return;
}

NSLog(@"%@%@",@"Imported certificate ID: ",certificateId);
NSLog(@"%@%@",@"Backing key ID: ",keyId);