Azure Key Vault: Get the Latest Certificate and Its Key ID
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. Omitting the version retrieves the latest version; it does not search backward for an enabled or unexpired version. Loads the base64 DER cer as a public certificate. The returned id, kid, and sid identify different resources. Use the actual returned versioned kid for key operations; do not construct it from a certificate version. Requires certificates/get, not secrets/get; no private key is downloaded.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkDateTimeW.h>
#include <CkCertW.h>
void ChilkatSample(void)
{
bool success = false;
CkStringBuilderW stringCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this configuration private, outside public web content and source control.
CkJsonObjectW config;
config.put_Strict(true);
success = config.LoadFile(L"qa_data/azure/keyvault.json");
if (success != true) {
wprintf(L"Could not load private Key Vault configuration.\n");
return;
}
stringCompare.SetString(config.stringOf(L"vault_name"));
bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"vault_name") != 1) || (stringMatch1 == true)) {
wprintf(L"Missing or invalid vault_name.\n");
return;
}
CkStringBuilderW validate1;
CkStringBuilderW alphabet1;
success = validate1.Append(config.stringOf(L"vault_name"));
if (success != true) {
wprintf(L"Could not validate vault_name.\n");
return;
}
success = alphabet1.Append(L"abcdefghijklmnopqrstuvwxyz0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate1.get_Length() < 3) || (validate1.get_Length() > 24)) {
wprintf(L"Invalid vault_name length.\n");
return;
}
int charIndex1 = 0;
while ((charIndex1 < validate1.get_Length())) {
if (alphabet1.Contains(validate1.getRange(charIndex1,1,false),true) != true) {
wprintf(L"Invalid character in vault_name.\n");
return;
}
charIndex1 = charIndex1 + 1;
}
CkStringBuilderW vaultName;
vaultName.Append(config.stringOf(L"vault_name"));
CkStringBuilderW letters;
letters.Append(L"abcdefghijklmnopqrstuvwxyz");
if ((letters.Contains(vaultName.getRange(0,1,false),true) != true) || (vaultName.EndsWith(L"-",true) == true) || (vaultName.Contains(L"--",true) == true)) {
wprintf(L"Invalid vault_name.\n");
return;
}
CkStringBuilderW origin;
success = origin.Append(L"https://");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = origin.Append(config.stringOf(L"vault_name"));
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = origin.Append(L".vault.azure.net");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_FollowRedirects(false);
http.put_ConnectTimeout(20);
http.put_ReadTimeout(60);
http.put_VerboseLogging(false);
CkHttpRequestW request;
request.put_HttpVerb(L"GET");
CkHttpResponseW response;
CkJsonObjectW result;
result.put_Strict(true);
// First run Get a Key Vault access token.
CkJsonObjectW token;
token.put_Strict(true);
success = token.LoadFile(L"qa_data/tokens/azureKeyVaultToken.json");
if (success != true) {
wprintf(L"Could not read the private token file.\n");
return;
}
stringCompare.SetString(token.stringOf(L"access_token"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((token.JsonTypeOf(L"access_token") != 1) || (stringMatch2 == true)) {
wprintf(L"Missing or invalid access_token.\n");
return;
}
stringCompare.SetString(token.stringOf(L"expires_at"));
bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
if ((token.JsonTypeOf(L"expires_at") != 1) || (stringMatch3 == true)) {
wprintf(L"Missing or invalid expires_at.\n");
return;
}
CkDateTimeW expires;
CkDateTimeW now;
success = expires.SetFromTimestamp(token.stringOf(L"expires_at"));
if (success != true) {
wprintf(L"Invalid token expiration.\n");
return;
}
http.put_AuthToken(token.stringOf(L"access_token"));
stringCompare.SetString(config.stringOf(L"cert_name"));
bool stringMatch4 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"cert_name") != 1) || (stringMatch4 == true)) {
wprintf(L"Missing or invalid cert_name.\n");
return;
}
CkStringBuilderW validate2;
CkStringBuilderW alphabet2;
success = validate2.Append(config.stringOf(L"cert_name"));
if (success != true) {
wprintf(L"Could not validate cert_name.\n");
return;
}
success = alphabet2.Append(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate2.get_Length() < 1) || (validate2.get_Length() > 127)) {
wprintf(L"Invalid cert_name length.\n");
return;
}
int charIndex2 = 0;
while ((charIndex2 < validate2.get_Length())) {
if (alphabet2.Contains(validate2.getRange(charIndex2,1,false),true) != true) {
wprintf(L"Invalid character in cert_name.\n");
return;
}
charIndex2 = charIndex2 + 1;
}
CkStringBuilderW certificateBase;
success = certificateBase.Append(origin.getAsString());
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = certificateBase.Append(L"/certificates/");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = certificateBase.Append(config.stringOf(L"cert_name"));
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
CkStringBuilderW bundlePrefix;
success = bundlePrefix.Append(certificateBase.getAsString());
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = bundlePrefix.Append(L"/");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
CkStringBuilderW getUrl;
success = getUrl.Append(certificateBase.getAsString());
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = getUrl.Append(L"?api-version=2026-04-01");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = now.SetFromCurrentSystemTime();
if (success != true) {
wprintf(L"Could not read current time.\n");
return;
}
if (expires.DiffSeconds(now) <= 60) {
wprintf(L"Token is expired or near expiration. Obtain a new token before continuing.\n");
return;
}
success = http.HttpReq(getUrl.getAsString(),request,response);
if (success != true) {
wprintf(L"Request did not complete; inspect diagnostics privately.\n");
return;
}
if (response.get_StatusCode() != 200) {
wprintf(L"HTTP status: %d\n",response.get_StatusCode());
wprintf(L"Request ID: %s\n",response.getHeaderField(L"x-ms-request-id"));
// Do not print full response bodies, tokens, or diagnostic logs.
wprintf(L"Request failed. Inspect private diagnostics; reconcile writes before retrying.\n");
return;
}
success = result.Load(response.bodyStr());
if (success != true) {
wprintf(L"Expected a well-formed JSON response.\n");
return;
}
stringCompare.SetString(result.stringOf(L"id"));
bool stringMatch5 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"id") != 1) || (stringMatch5 == true)) {
wprintf(L"Missing or invalid id.\n");
return;
}
stringCompare.SetString(result.stringOf(L"kid"));
bool stringMatch6 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"kid") != 1) || (stringMatch6 == true)) {
wprintf(L"Missing or invalid kid.\n");
return;
}
stringCompare.SetString(result.stringOf(L"cer"));
bool stringMatch7 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"cer") != 1) || (stringMatch7 == true)) {
wprintf(L"Missing or invalid cer.\n");
return;
}
CkStringBuilderW certificateVersionId;
success = certificateVersionId.Append(result.stringOf(L"id"));
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
if (certificateVersionId.StartsWith(bundlePrefix.getAsString(),true) != true) {
wprintf(L"Returned identifier is outside the expected vault/resource.\n");
return;
}
const wchar_t *certificateVersion = certificateVersionId.getRange(bundlePrefix.get_Length(),certificateVersionId.get_Length() - bundlePrefix.get_Length(),false);
CkStringBuilderW validate7;
CkStringBuilderW alphabet7;
success = validate7.Append(certificateVersion);
if (success != true) {
wprintf(L"Could not validate resource identifier.\n");
return;
}
success = alphabet7.Append(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate7.get_Length() < 1) || (validate7.get_Length() > 127)) {
wprintf(L"Invalid resource identifier length.\n");
return;
}
int charIndex7 = 0;
while ((charIndex7 < validate7.get_Length())) {
if (alphabet7.Contains(validate7.getRange(charIndex7,1,false),true) != true) {
wprintf(L"Invalid character in resource identifier.\n");
return;
}
charIndex7 = charIndex7 + 1;
}
CkStringBuilderW keyPrefix;
success = keyPrefix.Append(origin.getAsString());
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = keyPrefix.Append(L"/keys/");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
CkStringBuilderW keyTailId;
success = keyTailId.Append(result.stringOf(L"kid"));
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
if (keyTailId.StartsWith(keyPrefix.getAsString(),true) != true) {
wprintf(L"Returned identifier is outside the expected vault/resource.\n");
return;
}
const wchar_t *keyTail = keyTailId.getRange(keyPrefix.get_Length(),keyTailId.get_Length() - keyPrefix.get_Length(),false);
CkStringBuilderW keyTailParts;
success = keyTailParts.Append(keyTail);
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
const wchar_t *keyTailName = keyTailParts.getBefore(L"/",false);
const wchar_t *keyTailVersion = keyTailParts.getAfterFinal(L"/",false);
CkStringBuilderW validate8;
CkStringBuilderW alphabet8;
success = validate8.Append(keyTailName);
if (success != true) {
wprintf(L"Could not validate resource name.\n");
return;
}
success = alphabet8.Append(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate8.get_Length() < 1) || (validate8.get_Length() > 127)) {
wprintf(L"Invalid resource name length.\n");
return;
}
int charIndex8 = 0;
while ((charIndex8 < validate8.get_Length())) {
if (alphabet8.Contains(validate8.getRange(charIndex8,1,false),true) != true) {
wprintf(L"Invalid character in resource name.\n");
return;
}
charIndex8 = charIndex8 + 1;
}
CkStringBuilderW validate9;
CkStringBuilderW alphabet9;
success = validate9.Append(keyTailVersion);
if (success != true) {
wprintf(L"Could not validate resource version.\n");
return;
}
success = alphabet9.Append(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate9.get_Length() < 1) || (validate9.get_Length() > 127)) {
wprintf(L"Invalid resource version length.\n");
return;
}
int charIndex9 = 0;
while ((charIndex9 < validate9.get_Length())) {
if (alphabet9.Contains(validate9.getRange(charIndex9,1,false),true) != true) {
wprintf(L"Invalid character in resource version.\n");
return;
}
charIndex9 = charIndex9 + 1;
}
CkStringBuilderW keyTailCanonical;
success = keyTailCanonical.Append(keyTailName);
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = keyTailCanonical.Append(L"/");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = keyTailCanonical.Append(keyTailVersion);
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
stringCompare.SetString(keyTail);
bool stringMatch8 = stringCompare.ContentsEqual(keyTailCanonical.getAsString(),true);
if (stringMatch8 != true) {
wprintf(L"Expected a versioned resource identifier.\n");
return;
}
const wchar_t *certificateId = result.stringOf(L"id");
const wchar_t *keyId = result.stringOf(L"kid");
CkCertW remoteCert;
success = remoteCert.LoadFromBase64(result.stringOf(L"cer"));
if (success != true) {
wprintf(L"Could not load the returned public certificate.\n");
return;
}
wprintf(L"Certificate ID: %s\n",certificateId);
wprintf(L"Certificate version: %s\n",certificateVersion);
wprintf(L"Backing key ID: %s\n",keyId);
wprintf(L"Subject: %s\n",remoteCert.subjectCN());
// Keep id, kid, and sid separate. Never derive a key version from a certificate version.
}