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

CkPhp
<?php

include("chilkat.php");

$success = false;

$stringCompare = new CkStringBuilder();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this configuration private, outside public web content and source control.
$config = new CkJsonObject();
$config->put_Strict(true);
$success = $config->LoadFile('qa_data/azure/keyvault.json');
if ($success != true) {
    print 'Could not load private Key Vault configuration.' . "\n";
    exit;
}

$stringCompare->SetString($config->stringOf('vault_name'));
$stringMatch1 = $stringCompare->ContentsEqual('',true);
if (($config->JsonTypeOf('vault_name') != 1) or ($stringMatch1 == true)) {
    print 'Missing or invalid vault_name.' . "\n";
    exit;
}

$validate1 = new CkStringBuilder();
$alphabet1 = new CkStringBuilder();
$success = $validate1->Append($config->stringOf('vault_name'));
if ($success != true) {
    print 'Could not validate vault_name.' . "\n";
    exit;
}

$success = $alphabet1->Append('abcdefghijklmnopqrstuvwxyz0123456789-');
if ($success != true) {
    print 'Could not initialize validation.' . "\n";
    exit;
}

if (($validate1->get_Length() < 3) or ($validate1->get_Length() > 24)) {
    print 'Invalid vault_name length.' . "\n";
    exit;
}

$charIndex1 = 0;
while (($charIndex1 < $validate1->get_Length())) {
    if ($alphabet1->Contains($validate1->getRange($charIndex1,1,false),true) != true) {
        print 'Invalid character in vault_name.' . "\n";
        exit;
    }

    $charIndex1 = $charIndex1 + 1;
}

$vaultName = new CkStringBuilder();
$vaultName->Append($config->stringOf('vault_name'));
$letters = new CkStringBuilder();
$letters->Append('abcdefghijklmnopqrstuvwxyz');
if (($letters->Contains($vaultName->getRange(0,1,false),true) != true) or ($vaultName->EndsWith('-',true) == true) or ($vaultName->Contains('--',true) == true)) {
    print 'Invalid vault_name.' . "\n";
    exit;
}

$origin = new CkStringBuilder();
$success = $origin->Append('https://');
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $origin->Append($config->stringOf('vault_name'));
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $origin->Append('.vault.azure.net');
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$stringCompare->SetString($config->stringOf('cert_name'));
$stringMatch2 = $stringCompare->ContentsEqual('',true);
if (($config->JsonTypeOf('cert_name') != 1) or ($stringMatch2 == true)) {
    print 'Missing or invalid cert_name.' . "\n";
    exit;
}

$validate2 = new CkStringBuilder();
$alphabet2 = new CkStringBuilder();
$success = $validate2->Append($config->stringOf('cert_name'));
if ($success != true) {
    print 'Could not validate cert_name.' . "\n";
    exit;
}

$success = $alphabet2->Append('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if ($success != true) {
    print 'Could not initialize validation.' . "\n";
    exit;
}

if (($validate2->get_Length() < 1) or ($validate2->get_Length() > 127)) {
    print 'Invalid cert_name length.' . "\n";
    exit;
}

$charIndex2 = 0;
while (($charIndex2 < $validate2->get_Length())) {
    if ($alphabet2->Contains($validate2->getRange($charIndex2,1,false),true) != true) {
        print 'Invalid character in cert_name.' . "\n";
        exit;
    }

    $charIndex2 = $charIndex2 + 1;
}

$stringCompare->SetString($config->stringOf('client_id'));
$stringMatch3 = $stringCompare->ContentsEqual('',true);
if (($config->JsonTypeOf('client_id') != 1) or ($stringMatch3 == true)) {
    print 'Missing or invalid client_id.' . "\n";
    exit;
}

$stringCompare->SetString($config->stringOf('client_secret'));
$stringMatch4 = $stringCompare->ContentsEqual('',true);
if (($config->JsonTypeOf('client_secret') != 1) or ($stringMatch4 == true)) {
    print 'Missing or invalid client_secret.' . "\n";
    exit;
}

$stringCompare->SetString($config->stringOf('tenant_id'));
$stringMatch5 = $stringCompare->ContentsEqual('',true);
if (($config->JsonTypeOf('tenant_id') != 1) or ($stringMatch5 == true)) {
    print 'Missing or invalid tenant_id.' . "\n";
    exit;
}

$validate4 = new CkStringBuilder();
$alphabet4 = new CkStringBuilder();
$success = $validate4->Append($config->stringOf('tenant_id'));
if ($success != true) {
    print 'Could not validate tenant GUID.' . "\n";
    exit;
}

$success = $alphabet4->Append('0123456789abcdefABCDEF-');
if ($success != true) {
    print 'Could not initialize validation.' . "\n";
    exit;
}

if (($validate4->get_Length() < 36) or ($validate4->get_Length() > 36)) {
    print 'Invalid tenant GUID length.' . "\n";
    exit;
}

$charIndex4 = 0;
while (($charIndex4 < $validate4->get_Length())) {
    if ($alphabet4->Contains($validate4->getRange($charIndex4,1,false),true) != true) {
        print 'Invalid character in tenant GUID.' . "\n";
        exit;
    }

    $charIndex4 = $charIndex4 + 1;
}

if ($config->JsonTypeOf('pfx_password') != 1) {
    print 'Missing pfx_password string; empty is allowed for an unprotected PFX.' . "\n";
    exit;
}

$cert = new CkCert();
$success = $cert->LoadPfxFile('qa_data/pfx/myCertAndKey.pfx',$config->stringOf('pfx_password'));
if ($success != true) {
    print 'Could not load PFX; inspect diagnostics privately.' . "\n";
    exit;
}

$privateKey = new CkPrivateKey();
$success = $cert->GetPrivateKey($privateKey);
if ($success != true) {
    print 'PFX must contain a usable private key.' . "\n";
    exit;
}

//  Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
$publicKey = new CkPublicKey();
$success = $privateKey->ToPublicKey($publicKey);
if ($success != true) {
    print 'Could not get the public key.' . "\n";
    exit;
}

$publicJwk = $publicKey->getJwk();
if ($publicKey->get_LastMethodSuccess() != true) {
    print 'Could not inspect the public key.' . "\n";
    exit;
}

$keyInfo = new CkJsonObject();
$keyInfo->put_Strict(true);
$success = $keyInfo->Load($publicJwk);
if ($success != true) {
    print 'Invalid public JWK.' . "\n";
    exit;
}

$supported = false;
$stringCompare->SetString($keyInfo->stringOf('kty'));
$stringMatch6 = $stringCompare->ContentsEqual('RSA',true);
if ($stringMatch6 == true) {
    $supported = ($privateKey->get_BitLength() == 2048) or ($privateKey->get_BitLength() == 3072) or ($privateKey->get_BitLength() == 4096);
}

$stringCompare->SetString($keyInfo->stringOf('kty'));
$stringMatch7 = $stringCompare->ContentsEqual('EC',true);
if ($stringMatch7 == true) {
    $stringCompare->SetString($keyInfo->stringOf('crv'));
    $stringMatch8 = $stringCompare->ContentsEqual('P-256',true);
    $stringCompare->SetString($keyInfo->stringOf('crv'));
    $stringMatch9 = $stringCompare->ContentsEqual('P-384',true);
    $stringCompare->SetString($keyInfo->stringOf('crv'));
    $stringMatch10 = $stringCompare->ContentsEqual('P-521',true);
    if (($stringMatch8 == true) or ($stringMatch9 == true) or ($stringMatch10 == true)) {
        $supported = true;
    }

}

if ($supported != true) {
    print 'This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521.' . "\n";
    exit;
}

$options = new CkJsonObject();
$success = $options->UpdateString('service','azure-keyvault');
if ($success != true) {
    print 'Could not set service.' . "\n";
    exit;
}

$success = $options->UpdateString('auth.client_id',$config->stringOf('client_id'));
if ($success != true) {
    print 'Could not set auth.client_id.' . "\n";
    exit;
}

$success = $options->UpdateString('auth.client_secret',$config->stringOf('client_secret'));
if ($success != true) {
    print 'Could not set auth.client_secret.' . "\n";
    exit;
}

$success = $options->UpdateString('auth.tenant_id',$config->stringOf('tenant_id'));
if ($success != true) {
    print 'Could not set auth.tenant_id.' . "\n";
    exit;
}

$success = $options->UpdateString('vault_name',$config->stringOf('vault_name'));
if ($success != true) {
    print 'Could not set vault_name.' . "\n";
    exit;
}

$success = $options->UpdateString('cert_name',$config->stringOf('cert_name'));
if ($success != true) {
    print 'Could not set cert_name.' . "\n";
    exit;
}

$result = new CkJsonObject();
$result->put_Strict(true);
//  UploadToCloud uses azure-keyvault; SetCloudSigner uses azure_keyvault.
$success = $cert->UploadToCloud($options,$result);
if ($success != true) {
    print 'Import failed or its outcome is uncertain. Check the vault before retrying.' . "\n";
    exit;
}

$certificateBase = new CkStringBuilder();
$success = $certificateBase->Append($origin->getAsString());
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $certificateBase->Append('/certificates/');
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $certificateBase->Append($config->stringOf('cert_name'));
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$bundlePrefix = new CkStringBuilder();
$success = $bundlePrefix->Append($certificateBase->getAsString());
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $bundlePrefix->Append('/');
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$stringCompare->SetString($result->stringOf('id'));
$stringMatch11 = $stringCompare->ContentsEqual('',true);
if (($result->JsonTypeOf('id') != 1) or ($stringMatch11 == true)) {
    print 'Missing or invalid id.' . "\n";
    exit;
}

$stringCompare->SetString($result->stringOf('kid'));
$stringMatch12 = $stringCompare->ContentsEqual('',true);
if (($result->JsonTypeOf('kid') != 1) or ($stringMatch12 == true)) {
    print 'Missing or invalid kid.' . "\n";
    exit;
}

$stringCompare->SetString($result->stringOf('cer'));
$stringMatch13 = $stringCompare->ContentsEqual('',true);
if (($result->JsonTypeOf('cer') != 1) or ($stringMatch13 == true)) {
    print 'Missing or invalid cer.' . "\n";
    exit;
}

$certificateVersionId = new CkStringBuilder();
$success = $certificateVersionId->Append($result->stringOf('id'));
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

if ($certificateVersionId->StartsWith($bundlePrefix->getAsString(),true) != true) {
    print 'Returned identifier is outside the expected vault/resource.' . "\n";
    exit;
}

$certificateVersion = $certificateVersionId->getRange($bundlePrefix->get_Length(),$certificateVersionId->get_Length() - $bundlePrefix->get_Length(),false);
$validate7 = new CkStringBuilder();
$alphabet7 = new CkStringBuilder();
$success = $validate7->Append($certificateVersion);
if ($success != true) {
    print 'Could not validate resource identifier.' . "\n";
    exit;
}

$success = $alphabet7->Append('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if ($success != true) {
    print 'Could not initialize validation.' . "\n";
    exit;
}

if (($validate7->get_Length() < 1) or ($validate7->get_Length() > 127)) {
    print 'Invalid resource identifier length.' . "\n";
    exit;
}

$charIndex7 = 0;
while (($charIndex7 < $validate7->get_Length())) {
    if ($alphabet7->Contains($validate7->getRange($charIndex7,1,false),true) != true) {
        print 'Invalid character in resource identifier.' . "\n";
        exit;
    }

    $charIndex7 = $charIndex7 + 1;
}

$keyPrefix = new CkStringBuilder();
$success = $keyPrefix->Append($origin->getAsString());
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $keyPrefix->Append('/keys/');
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$keyTailId = new CkStringBuilder();
$success = $keyTailId->Append($result->stringOf('kid'));
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

if ($keyTailId->StartsWith($keyPrefix->getAsString(),true) != true) {
    print 'Returned identifier is outside the expected vault/resource.' . "\n";
    exit;
}

$keyTail = $keyTailId->getRange($keyPrefix->get_Length(),$keyTailId->get_Length() - $keyPrefix->get_Length(),false);
$keyTailParts = new CkStringBuilder();
$success = $keyTailParts->Append($keyTail);
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$keyTailName = $keyTailParts->getBefore('/',false);
$keyTailVersion = $keyTailParts->getAfterFinal('/',false);
$validate8 = new CkStringBuilder();
$alphabet8 = new CkStringBuilder();
$success = $validate8->Append($keyTailName);
if ($success != true) {
    print 'Could not validate resource name.' . "\n";
    exit;
}

$success = $alphabet8->Append('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if ($success != true) {
    print 'Could not initialize validation.' . "\n";
    exit;
}

if (($validate8->get_Length() < 1) or ($validate8->get_Length() > 127)) {
    print 'Invalid resource name length.' . "\n";
    exit;
}

$charIndex8 = 0;
while (($charIndex8 < $validate8->get_Length())) {
    if ($alphabet8->Contains($validate8->getRange($charIndex8,1,false),true) != true) {
        print 'Invalid character in resource name.' . "\n";
        exit;
    }

    $charIndex8 = $charIndex8 + 1;
}

$validate9 = new CkStringBuilder();
$alphabet9 = new CkStringBuilder();
$success = $validate9->Append($keyTailVersion);
if ($success != true) {
    print 'Could not validate resource version.' . "\n";
    exit;
}

$success = $alphabet9->Append('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if ($success != true) {
    print 'Could not initialize validation.' . "\n";
    exit;
}

if (($validate9->get_Length() < 1) or ($validate9->get_Length() > 127)) {
    print 'Invalid resource version length.' . "\n";
    exit;
}

$charIndex9 = 0;
while (($charIndex9 < $validate9->get_Length())) {
    if ($alphabet9->Contains($validate9->getRange($charIndex9,1,false),true) != true) {
        print 'Invalid character in resource version.' . "\n";
        exit;
    }

    $charIndex9 = $charIndex9 + 1;
}

$keyTailCanonical = new CkStringBuilder();
$success = $keyTailCanonical->Append($keyTailName);
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $keyTailCanonical->Append('/');
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$success = $keyTailCanonical->Append($keyTailVersion);
if ($success != true) {
    print 'Could not construct URL or identifier.' . "\n";
    exit;
}

$stringCompare->SetString($keyTail);
$stringMatch14 = $stringCompare->ContentsEqual($keyTailCanonical->getAsString(),true);
if ($stringMatch14 != true) {
    print 'Expected a versioned resource identifier.' . "\n";
    exit;
}

$certificateId = $result->stringOf('id');
$keyId = $result->stringOf('kid');
$remoteCert = new CkCert();
$success = $remoteCert->LoadFromBase64($result->stringOf('cer'));
if ($success != true) {
    print 'Could not load the returned public certificate.' . "\n";
    exit;
}

$sourceDer = $cert->getEncoded();
if ($cert->get_LastMethodSuccess() != true) {
    print 'Could not encode source certificate.' . "\n";
    exit;
}

$importedDer = $remoteCert->getEncoded();
if ($remoteCert->get_LastMethodSuccess() != true) {
    print 'Could not encode imported certificate.' . "\n";
    exit;
}

$stringCompare->SetString($sourceDer);
$stringMatch15 = $stringCompare->ContentsEqual($importedDer,true);
if ($stringMatch15 != true) {
    print 'Imported certificate does not match the PFX leaf certificate.' . "\n";
    exit;
}

print 'Imported certificate ID: ' . $certificateId . "\n";
print 'Backing key ID: ' . $keyId . "\n";

?>