Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.6.0+

Azure Key Vault: Import a PFX Certificate with the REST API

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 HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.

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.

Explicitly selects a nonexportable private-key policy and disables key reuse. Existing certificate policy constraints can reject incompatible imports; choose a new name or deliberately reconcile policy. This does not delete the original local PFX. The standard base64 request contains the entire PFX, protected in transit by verified TLS; the PFX password comes from private configuration.

Microsoft reference

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
stringCompare: HCkStringBuilder;
config: HCkJsonObject;
stringMatch1: Boolean;
validate1: HCkStringBuilder;
alphabet1: HCkStringBuilder;
charIndex1: Integer;
vaultName: HCkStringBuilder;
letters: HCkStringBuilder;
origin: HCkStringBuilder;
http: HCkHttp;
request: HCkHttpRequest;
response: HCkHttpResponse;
result: HCkJsonObject;
token: HCkJsonObject;
stringMatch2: Boolean;
stringMatch3: Boolean;
expires: HCkDateTime;
now: HCkDateTime;
stringMatch4: Boolean;
validate2: HCkStringBuilder;
alphabet2: HCkStringBuilder;
charIndex2: Integer;
cert: HCkCert;
privateKey: HCkPrivateKey;
publicKey: HCkPublicKey;
publicJwk: PWideChar;
keyInfo: HCkJsonObject;
supported: Boolean;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
stringMatch8: Boolean;
stringMatch9: Boolean;
pfxBytes: HCkBinData;
body: HCkJsonObject;
stringMatch10: Boolean;
importUrl: HCkStringBuilder;
certificateBase: HCkStringBuilder;
bundlePrefix: HCkStringBuilder;
stringMatch11: Boolean;
stringMatch12: Boolean;
stringMatch13: Boolean;
certificateVersionId: HCkStringBuilder;
certificateVersion: PWideChar;
validate7: HCkStringBuilder;
alphabet7: HCkStringBuilder;
charIndex7: Integer;
keyPrefix: HCkStringBuilder;
keyTailId: HCkStringBuilder;
keyTail: PWideChar;
keyTailParts: HCkStringBuilder;
keyTailName: PWideChar;
keyTailVersion: PWideChar;
validate8: HCkStringBuilder;
alphabet8: HCkStringBuilder;
charIndex8: Integer;
validate9: HCkStringBuilder;
alphabet9: HCkStringBuilder;
charIndex9: Integer;
keyTailCanonical: HCkStringBuilder;
stringMatch14: Boolean;
certificateId: PWideChar;
keyId: PWideChar;
remoteCert: HCkCert;
sourceDer: PWideChar;
importedDer: PWideChar;
stringMatch15: Boolean;

begin
success := False;

stringCompare := CkStringBuilder_Create();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this configuration private, outside public web content and source control.
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/keyvault.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load private Key Vault configuration.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'vault_name'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'vault_name') <> 1) or (stringMatch1 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid vault_name.');
    Exit;
  end;
validate1 := CkStringBuilder_Create();
alphabet1 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate1,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate vault_name.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet1,'abcdefghijklmnopqrstuvwxyz0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate1) < 3) or (CkStringBuilder_getLength(validate1) > 24)) then
  begin
    Memo1.Lines.Add('Invalid vault_name length.');
    Exit;
  end;
charIndex1 := 0;
while (charIndex1 < CkStringBuilder_getLength(validate1)) do
  begin
    if (CkStringBuilder_Contains(alphabet1,CkStringBuilder__getRange(validate1,charIndex1,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in vault_name.');
        Exit;
      end;
    charIndex1 := charIndex1 + 1;
  end;

vaultName := CkStringBuilder_Create();
CkStringBuilder_Append(vaultName,CkJsonObject__stringOf(config,'vault_name'));
letters := CkStringBuilder_Create();
CkStringBuilder_Append(letters,'abcdefghijklmnopqrstuvwxyz');
if ((CkStringBuilder_Contains(letters,CkStringBuilder__getRange(vaultName,0,1,False),True) <> True) or (CkStringBuilder_EndsWith(vaultName,'-',True) = True) or (CkStringBuilder_Contains(vaultName,'--',True) = True)) then
  begin
    Memo1.Lines.Add('Invalid vault_name.');
    Exit;
  end;
origin := CkStringBuilder_Create();
success := CkStringBuilder_Append(origin,'https://');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(origin,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(origin,'.vault.azure.net');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
request := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(request,'GET');
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
//  First run Get a Key Vault access token.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/azureKeyVaultToken.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read the private token file.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'access_token'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (stringMatch2 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid access_token.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'expires_at'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'expires_at') <> 1) or (stringMatch3 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid expires_at.');
    Exit;
  end;
expires := CkDateTime_Create();
now := CkDateTime_Create();
success := CkDateTime_SetFromTimestamp(expires,CkJsonObject__stringOf(token,'expires_at'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Invalid token expiration.');
    Exit;
  end;
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'cert_name'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'cert_name') <> 1) or (stringMatch4 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid cert_name.');
    Exit;
  end;
validate2 := CkStringBuilder_Create();
alphabet2 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate2,CkJsonObject__stringOf(config,'cert_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate cert_name.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet2,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate2) < 1) or (CkStringBuilder_getLength(validate2) > 127)) then
  begin
    Memo1.Lines.Add('Invalid cert_name length.');
    Exit;
  end;
charIndex2 := 0;
while (charIndex2 < CkStringBuilder_getLength(validate2)) do
  begin
    if (CkStringBuilder_Contains(alphabet2,CkStringBuilder__getRange(validate2,charIndex2,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in cert_name.');
        Exit;
      end;
    charIndex2 := charIndex2 + 1;
  end;

if (CkJsonObject_JsonTypeOf(config,'pfx_password') <> 1) then
  begin
    Memo1.Lines.Add('Missing pfx_password string; empty is allowed for an unprotected PFX.');
    Exit;
  end;
cert := CkCert_Create();
success := CkCert_LoadPfxFile(cert,'qa_data/pfx/myCertAndKey.pfx',CkJsonObject__stringOf(config,'pfx_password'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load PFX; inspect diagnostics privately.');
    Exit;
  end;
privateKey := CkPrivateKey_Create();
success := CkCert_GetPrivateKey(cert,privateKey);
if (success <> True) then
  begin
    Memo1.Lines.Add('PFX must contain a usable private key.');
    Exit;
  end;
//  Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
publicKey := CkPublicKey_Create();
success := CkPrivateKey_ToPublicKey(privateKey,publicKey);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not get the public key.');
    Exit;
  end;
publicJwk := CkPublicKey__getJwk(publicKey);
if (CkPublicKey_getLastMethodSuccess(publicKey) <> True) then
  begin
    Memo1.Lines.Add('Could not inspect the public key.');
    Exit;
  end;
keyInfo := CkJsonObject_Create();
CkJsonObject_putStrict(keyInfo,True);
success := CkJsonObject_Load(keyInfo,publicJwk);
if (success <> True) then
  begin
    Memo1.Lines.Add('Invalid public JWK.');
    Exit;
  end;
supported := False;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'kty'));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'RSA',True);
if (stringMatch5 = True) then
  begin
    supported := (CkPrivateKey_getBitLength(privateKey) = 2048) or (CkPrivateKey_getBitLength(privateKey) = 3072) or (CkPrivateKey_getBitLength(privateKey) = 4096);
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'kty'));
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'EC',True);
if (stringMatch6 = True) then
  begin
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'crv'));
    stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'P-256',True);
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'crv'));
    stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'P-384',True);
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'crv'));
    stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'P-521',True);
    if ((stringMatch7 = True) or (stringMatch8 = True) or (stringMatch9 = True)) then
      begin
        supported := True;
      end;
  end;
if (supported <> True) then
  begin
    Memo1.Lines.Add('This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521.');
    Exit;
  end;
pfxBytes := CkBinData_Create();
success := CkBinData_LoadFile(pfxBytes,'qa_data/pfx/myCertAndKey.pfx');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read the PFX.');
    Exit;
  end;
body := CkJsonObject_Create();
success := CkJsonObject_UpdateString(body,'value',CkBinData__getEncoded(pfxBytes,'base64'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set value.');
    Exit;
  end;
success := CkJsonObject_UpdateString(body,'pwd',CkJsonObject__stringOf(config,'pfx_password'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set pwd.');
    Exit;
  end;
success := CkJsonObject_UpdateString(body,'policy.key_props.kty',CkJsonObject__stringOf(keyInfo,'kty'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set policy.key_props.kty.');
    Exit;
  end;
success := CkJsonObject_UpdateBool(body,'policy.key_props.exportable',False);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set nonexportable policy.');
    Exit;
  end;
success := CkJsonObject_UpdateBool(body,'policy.key_props.reuse_key',False);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set key reuse policy.');
    Exit;
  end;
success := CkJsonObject_UpdateString(body,'policy.secret_props.contentType','application/x-pkcs12');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set policy.secret_props.contentType.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'kty'));
stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'RSA',True);
if (stringMatch10 = True) then
  begin
    success := CkJsonObject_UpdateInt(body,'policy.key_props.key_size',CkPrivateKey_getBitLength(privateKey));
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not set RSA key size.');
        Exit;
      end;
  end
else
  begin
    success := CkJsonObject_UpdateString(body,'policy.key_props.crv',CkJsonObject__stringOf(keyInfo,'crv'));
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not set policy.key_props.crv.');
        Exit;
      end;
  end;
importUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(importUrl,CkStringBuilder__getAsString(origin));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(importUrl,'/certificates/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(importUrl,CkJsonObject__stringOf(config,'cert_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(importUrl,'/import?api-version=2026-04-01');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read current time.');
    Exit;
  end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
  begin
    Memo1.Lines.Add('Token is expired or near expiration. Obtain a new token before continuing.');
    Exit;
  end;
success := CkHttp_HttpJson(http,'POST',CkStringBuilder__getAsString(importUrl),body,'application/json',response);
if (success <> True) then
  begin
    Memo1.Lines.Add('Request failed or its outcome is uncertain. Reconcile a write before retrying.');
    Exit;
  end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
  begin
    Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
    Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
    //  Do not print full response bodies, tokens, or diagnostic logs.
    Memo1.Lines.Add('Request failed. Inspect private diagnostics; reconcile writes before retrying.');
    Exit;
  end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a well-formed JSON response.');
    Exit;
  end;
certificateBase := CkStringBuilder_Create();
success := CkStringBuilder_Append(certificateBase,CkStringBuilder__getAsString(origin));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(certificateBase,'/certificates/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(certificateBase,CkJsonObject__stringOf(config,'cert_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
bundlePrefix := CkStringBuilder_Create();
success := CkStringBuilder_Append(bundlePrefix,CkStringBuilder__getAsString(certificateBase));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(bundlePrefix,'/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'id'));
stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'id') <> 1) or (stringMatch11 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid id.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'kid'));
stringMatch12 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'kid') <> 1) or (stringMatch12 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid kid.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'cer'));
stringMatch13 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'cer') <> 1) or (stringMatch13 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid cer.');
    Exit;
  end;
certificateVersionId := CkStringBuilder_Create();
success := CkStringBuilder_Append(certificateVersionId,CkJsonObject__stringOf(result,'id'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
if (CkStringBuilder_StartsWith(certificateVersionId,CkStringBuilder__getAsString(bundlePrefix),True) <> True) then
  begin
    Memo1.Lines.Add('Returned identifier is outside the expected vault/resource.');
    Exit;
  end;
certificateVersion := CkStringBuilder__getRange(certificateVersionId,CkStringBuilder_getLength(bundlePrefix),CkStringBuilder_getLength(certificateVersionId) - CkStringBuilder_getLength(bundlePrefix),False);
validate7 := CkStringBuilder_Create();
alphabet7 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate7,certificateVersion);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate resource identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet7,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate7) < 1) or (CkStringBuilder_getLength(validate7) > 127)) then
  begin
    Memo1.Lines.Add('Invalid resource identifier length.');
    Exit;
  end;
charIndex7 := 0;
while (charIndex7 < CkStringBuilder_getLength(validate7)) do
  begin
    if (CkStringBuilder_Contains(alphabet7,CkStringBuilder__getRange(validate7,charIndex7,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in resource identifier.');
        Exit;
      end;
    charIndex7 := charIndex7 + 1;
  end;

keyPrefix := CkStringBuilder_Create();
success := CkStringBuilder_Append(keyPrefix,CkStringBuilder__getAsString(origin));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(keyPrefix,'/keys/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
keyTailId := CkStringBuilder_Create();
success := CkStringBuilder_Append(keyTailId,CkJsonObject__stringOf(result,'kid'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
if (CkStringBuilder_StartsWith(keyTailId,CkStringBuilder__getAsString(keyPrefix),True) <> True) then
  begin
    Memo1.Lines.Add('Returned identifier is outside the expected vault/resource.');
    Exit;
  end;
keyTail := CkStringBuilder__getRange(keyTailId,CkStringBuilder_getLength(keyPrefix),CkStringBuilder_getLength(keyTailId) - CkStringBuilder_getLength(keyPrefix),False);
keyTailParts := CkStringBuilder_Create();
success := CkStringBuilder_Append(keyTailParts,keyTail);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
keyTailName := CkStringBuilder__getBefore(keyTailParts,'/',False);
keyTailVersion := CkStringBuilder__getAfterFinal(keyTailParts,'/',False);
validate8 := CkStringBuilder_Create();
alphabet8 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate8,keyTailName);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate resource name.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet8,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate8) < 1) or (CkStringBuilder_getLength(validate8) > 127)) then
  begin
    Memo1.Lines.Add('Invalid resource name length.');
    Exit;
  end;
charIndex8 := 0;
while (charIndex8 < CkStringBuilder_getLength(validate8)) do
  begin
    if (CkStringBuilder_Contains(alphabet8,CkStringBuilder__getRange(validate8,charIndex8,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in resource name.');
        Exit;
      end;
    charIndex8 := charIndex8 + 1;
  end;

validate9 := CkStringBuilder_Create();
alphabet9 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate9,keyTailVersion);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate resource version.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet9,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate9) < 1) or (CkStringBuilder_getLength(validate9) > 127)) then
  begin
    Memo1.Lines.Add('Invalid resource version length.');
    Exit;
  end;
charIndex9 := 0;
while (charIndex9 < CkStringBuilder_getLength(validate9)) do
  begin
    if (CkStringBuilder_Contains(alphabet9,CkStringBuilder__getRange(validate9,charIndex9,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in resource version.');
        Exit;
      end;
    charIndex9 := charIndex9 + 1;
  end;

keyTailCanonical := CkStringBuilder_Create();
success := CkStringBuilder_Append(keyTailCanonical,keyTailName);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(keyTailCanonical,'/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(keyTailCanonical,keyTailVersion);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,keyTail);
stringMatch14 := CkStringBuilder_ContentsEqual(stringCompare,CkStringBuilder__getAsString(keyTailCanonical),True);
if (stringMatch14 <> True) then
  begin
    Memo1.Lines.Add('Expected a versioned resource identifier.');
    Exit;
  end;
certificateId := CkJsonObject__stringOf(result,'id');
keyId := CkJsonObject__stringOf(result,'kid');
remoteCert := CkCert_Create();
success := CkCert_LoadFromBase64(remoteCert,CkJsonObject__stringOf(result,'cer'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load the returned public certificate.');
    Exit;
  end;
sourceDer := CkCert__getEncoded(cert);
if (CkCert_getLastMethodSuccess(cert) <> True) then
  begin
    Memo1.Lines.Add('Could not encode source certificate.');
    Exit;
  end;
importedDer := CkCert__getEncoded(remoteCert);
if (CkCert_getLastMethodSuccess(remoteCert) <> True) then
  begin
    Memo1.Lines.Add('Could not encode imported certificate.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,sourceDer);
stringMatch15 := CkStringBuilder_ContentsEqual(stringCompare,importedDer,True);
if (stringMatch15 <> True) then
  begin
    Memo1.Lines.Add('Imported certificate does not match the PFX leaf certificate.');
    Exit;
  end;
Memo1.Lines.Add('Imported certificate ID: ' + certificateId);
Memo1.Lines.Add('Backing key ID: ' + keyId);

CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(validate1);
CkStringBuilder_Dispose(alphabet1);
CkStringBuilder_Dispose(vaultName);
CkStringBuilder_Dispose(letters);
CkStringBuilder_Dispose(origin);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(request);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(token);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkStringBuilder_Dispose(validate2);
CkStringBuilder_Dispose(alphabet2);
CkCert_Dispose(cert);
CkPrivateKey_Dispose(privateKey);
CkPublicKey_Dispose(publicKey);
CkJsonObject_Dispose(keyInfo);
CkBinData_Dispose(pfxBytes);
CkJsonObject_Dispose(body);
CkStringBuilder_Dispose(importUrl);
CkStringBuilder_Dispose(certificateBase);
CkStringBuilder_Dispose(bundlePrefix);
CkStringBuilder_Dispose(certificateVersionId);
CkStringBuilder_Dispose(validate7);
CkStringBuilder_Dispose(alphabet7);
CkStringBuilder_Dispose(keyPrefix);
CkStringBuilder_Dispose(keyTailId);
CkStringBuilder_Dispose(keyTailParts);
CkStringBuilder_Dispose(validate8);
CkStringBuilder_Dispose(alphabet8);
CkStringBuilder_Dispose(validate9);
CkStringBuilder_Dispose(alphabet9);
CkStringBuilder_Dispose(keyTailCanonical);
CkCert_Dispose(remoteCert);