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.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
#include <CkPrivateKeyW.h>
#include <CkPublicKeyW.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;
}
stringCompare.SetString(config.stringOf(L"cert_name"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"cert_name") != 1) || (stringMatch2 == 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;
}
stringCompare.SetString(config.stringOf(L"client_id"));
bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"client_id") != 1) || (stringMatch3 == true)) {
wprintf(L"Missing or invalid client_id.\n");
return;
}
stringCompare.SetString(config.stringOf(L"client_secret"));
bool stringMatch4 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"client_secret") != 1) || (stringMatch4 == true)) {
wprintf(L"Missing or invalid client_secret.\n");
return;
}
stringCompare.SetString(config.stringOf(L"tenant_id"));
bool stringMatch5 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"tenant_id") != 1) || (stringMatch5 == true)) {
wprintf(L"Missing or invalid tenant_id.\n");
return;
}
CkStringBuilderW validate4;
CkStringBuilderW alphabet4;
success = validate4.Append(config.stringOf(L"tenant_id"));
if (success != true) {
wprintf(L"Could not validate tenant GUID.\n");
return;
}
success = alphabet4.Append(L"0123456789abcdefABCDEF-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate4.get_Length() < 36) || (validate4.get_Length() > 36)) {
wprintf(L"Invalid tenant GUID length.\n");
return;
}
int charIndex4 = 0;
while ((charIndex4 < validate4.get_Length())) {
if (alphabet4.Contains(validate4.getRange(charIndex4,1,false),true) != true) {
wprintf(L"Invalid character in tenant GUID.\n");
return;
}
charIndex4 = charIndex4 + 1;
}
if (config.JsonTypeOf(L"pfx_password") != 1) {
wprintf(L"Missing pfx_password string; empty is allowed for an unprotected PFX.\n");
return;
}
CkCertW cert;
success = cert.LoadPfxFile(L"qa_data/pfx/myCertAndKey.pfx",config.stringOf(L"pfx_password"));
if (success != true) {
wprintf(L"Could not load PFX; inspect diagnostics privately.\n");
return;
}
CkPrivateKeyW privateKey;
success = cert.GetPrivateKey(privateKey);
if (success != true) {
wprintf(L"PFX must contain a usable private key.\n");
return;
}
// Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
CkPublicKeyW publicKey;
success = privateKey.ToPublicKey(publicKey);
if (success != true) {
wprintf(L"Could not get the public key.\n");
return;
}
const wchar_t *publicJwk = publicKey.getJwk();
if (publicKey.get_LastMethodSuccess() != true) {
wprintf(L"Could not inspect the public key.\n");
return;
}
CkJsonObjectW keyInfo;
keyInfo.put_Strict(true);
success = keyInfo.Load(publicJwk);
if (success != true) {
wprintf(L"Invalid public JWK.\n");
return;
}
bool supported = false;
stringCompare.SetString(keyInfo.stringOf(L"kty"));
bool stringMatch6 = stringCompare.ContentsEqual(L"RSA",true);
if (stringMatch6 == true) {
supported = (privateKey.get_BitLength() == 2048) || (privateKey.get_BitLength() == 3072) || (privateKey.get_BitLength() == 4096);
}
stringCompare.SetString(keyInfo.stringOf(L"kty"));
bool stringMatch7 = stringCompare.ContentsEqual(L"EC",true);
if (stringMatch7 == true) {
stringCompare.SetString(keyInfo.stringOf(L"crv"));
bool stringMatch8 = stringCompare.ContentsEqual(L"P-256",true);
stringCompare.SetString(keyInfo.stringOf(L"crv"));
bool stringMatch9 = stringCompare.ContentsEqual(L"P-384",true);
stringCompare.SetString(keyInfo.stringOf(L"crv"));
bool stringMatch10 = stringCompare.ContentsEqual(L"P-521",true);
if ((stringMatch8 == true) || (stringMatch9 == true) || (stringMatch10 == true)) {
supported = true;
}
}
if (supported != true) {
wprintf(L"This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521.\n");
return;
}
CkJsonObjectW options;
success = options.UpdateString(L"service",L"azure-keyvault");
if (success != true) {
wprintf(L"Could not set service.\n");
return;
}
success = options.UpdateString(L"auth.client_id",config.stringOf(L"client_id"));
if (success != true) {
wprintf(L"Could not set auth.client_id.\n");
return;
}
success = options.UpdateString(L"auth.client_secret",config.stringOf(L"client_secret"));
if (success != true) {
wprintf(L"Could not set auth.client_secret.\n");
return;
}
success = options.UpdateString(L"auth.tenant_id",config.stringOf(L"tenant_id"));
if (success != true) {
wprintf(L"Could not set auth.tenant_id.\n");
return;
}
success = options.UpdateString(L"vault_name",config.stringOf(L"vault_name"));
if (success != true) {
wprintf(L"Could not set vault_name.\n");
return;
}
success = options.UpdateString(L"cert_name",config.stringOf(L"cert_name"));
if (success != true) {
wprintf(L"Could not set cert_name.\n");
return;
}
CkJsonObjectW result;
result.put_Strict(true);
// UploadToCloud uses azure-keyvault; SetCloudSigner uses azure_keyvault.
success = cert.UploadToCloud(options,result);
if (success != true) {
wprintf(L"Import failed or its outcome is uncertain. Check the vault before retrying.\n");
return;
}
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;
}
stringCompare.SetString(result.stringOf(L"id"));
bool stringMatch11 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"id") != 1) || (stringMatch11 == true)) {
wprintf(L"Missing or invalid id.\n");
return;
}
stringCompare.SetString(result.stringOf(L"kid"));
bool stringMatch12 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"kid") != 1) || (stringMatch12 == true)) {
wprintf(L"Missing or invalid kid.\n");
return;
}
stringCompare.SetString(result.stringOf(L"cer"));
bool stringMatch13 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"cer") != 1) || (stringMatch13 == 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 stringMatch14 = stringCompare.ContentsEqual(keyTailCanonical.getAsString(),true);
if (stringMatch14 != 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;
}
const wchar_t *sourceDer = cert.getEncoded();
if (cert.get_LastMethodSuccess() != true) {
wprintf(L"Could not encode source certificate.\n");
return;
}
const wchar_t *importedDer = remoteCert.getEncoded();
if (remoteCert.get_LastMethodSuccess() != true) {
wprintf(L"Could not encode imported certificate.\n");
return;
}
stringCompare.SetString(sourceDer);
bool stringMatch15 = stringCompare.ContentsEqual(importedDer,true);
if (stringMatch15 != true) {
wprintf(L"Imported certificate does not match the PFX leaf certificate.\n");
return;
}
wprintf(L"Imported certificate ID: %s\n",certificateId);
wprintf(L"Backing key ID: %s\n",keyId);
}