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 Ruby Downloads
require 'chilkat'
success = false
stringCompare = Chilkat::CkStringBuilder.new()
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Keep this configuration private, outside public web content and source control.
config = Chilkat::CkJsonObject.new()
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
end
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
end
validate1 = Chilkat::CkStringBuilder.new()
alphabet1 = Chilkat::CkStringBuilder.new()
success = validate1.Append(config.stringOf("vault_name"))
if (success != true)
print "Could not validate vault_name." + "\n";
exit
end
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate1.get_Length() < 3) or (validate1.get_Length() > 24))
print "Invalid vault_name length." + "\n";
exit
end
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
end
charIndex1 = charIndex1 + 1
end
vaultName = Chilkat::CkStringBuilder.new()
vaultName.Append(config.stringOf("vault_name"))
letters = Chilkat::CkStringBuilder.new()
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
end
origin = Chilkat::CkStringBuilder.new()
success = origin.Append("https://")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = origin.Append(config.stringOf("vault_name"))
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = origin.Append(".vault.azure.net")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
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
end
validate2 = Chilkat::CkStringBuilder.new()
alphabet2 = Chilkat::CkStringBuilder.new()
success = validate2.Append(config.stringOf("cert_name"))
if (success != true)
print "Could not validate cert_name." + "\n";
exit
end
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate2.get_Length() < 1) or (validate2.get_Length() > 127))
print "Invalid cert_name length." + "\n";
exit
end
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
end
charIndex2 = charIndex2 + 1
end
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
end
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
end
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
end
validate4 = Chilkat::CkStringBuilder.new()
alphabet4 = Chilkat::CkStringBuilder.new()
success = validate4.Append(config.stringOf("tenant_id"))
if (success != true)
print "Could not validate tenant GUID." + "\n";
exit
end
success = alphabet4.Append("0123456789abcdefABCDEF-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate4.get_Length() < 36) or (validate4.get_Length() > 36))
print "Invalid tenant GUID length." + "\n";
exit
end
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
end
charIndex4 = charIndex4 + 1
end
if (config.JsonTypeOf("pfx_password") != 1)
print "Missing pfx_password string; empty is allowed for an unprotected PFX." + "\n";
exit
end
cert = Chilkat::CkCert.new()
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
end
privateKey = Chilkat::CkPrivateKey.new()
success = cert.GetPrivateKey(privateKey)
if (success != true)
print "PFX must contain a usable private key." + "\n";
exit
end
# Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
publicKey = Chilkat::CkPublicKey.new()
success = privateKey.ToPublicKey(publicKey)
if (success != true)
print "Could not get the public key." + "\n";
exit
end
publicJwk = publicKey.getJwk()
if (publicKey.get_LastMethodSuccess() != true)
print "Could not inspect the public key." + "\n";
exit
end
keyInfo = Chilkat::CkJsonObject.new()
keyInfo.put_Strict(true)
success = keyInfo.Load(publicJwk)
if (success != true)
print "Invalid public JWK." + "\n";
exit
end
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)
end
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
end
end
if (supported != true)
print "This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521." + "\n";
exit
end
options = Chilkat::CkJsonObject.new()
success = options.UpdateString("service","azure-keyvault")
if (success != true)
print "Could not set service." + "\n";
exit
end
success = options.UpdateString("auth.client_id",config.stringOf("client_id"))
if (success != true)
print "Could not set auth.client_id." + "\n";
exit
end
success = options.UpdateString("auth.client_secret",config.stringOf("client_secret"))
if (success != true)
print "Could not set auth.client_secret." + "\n";
exit
end
success = options.UpdateString("auth.tenant_id",config.stringOf("tenant_id"))
if (success != true)
print "Could not set auth.tenant_id." + "\n";
exit
end
success = options.UpdateString("vault_name",config.stringOf("vault_name"))
if (success != true)
print "Could not set vault_name." + "\n";
exit
end
success = options.UpdateString("cert_name",config.stringOf("cert_name"))
if (success != true)
print "Could not set cert_name." + "\n";
exit
end
result = Chilkat::CkJsonObject.new()
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
end
certificateBase = Chilkat::CkStringBuilder.new()
success = certificateBase.Append(origin.getAsString())
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = certificateBase.Append("/certificates/")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = certificateBase.Append(config.stringOf("cert_name"))
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
bundlePrefix = Chilkat::CkStringBuilder.new()
success = bundlePrefix.Append(certificateBase.getAsString())
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = bundlePrefix.Append("/")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
stringCompare.SetString(result.stringOf("id"))
stringMatch11 = stringCompare.ContentsEqual("",true)
if ((result.JsonTypeOf("id") != 1) or (stringMatch11 == true))
print "Missing or invalid id." + "\n";
exit
end
stringCompare.SetString(result.stringOf("kid"))
stringMatch12 = stringCompare.ContentsEqual("",true)
if ((result.JsonTypeOf("kid") != 1) or (stringMatch12 == true))
print "Missing or invalid kid." + "\n";
exit
end
stringCompare.SetString(result.stringOf("cer"))
stringMatch13 = stringCompare.ContentsEqual("",true)
if ((result.JsonTypeOf("cer") != 1) or (stringMatch13 == true))
print "Missing or invalid cer." + "\n";
exit
end
certificateVersionId = Chilkat::CkStringBuilder.new()
success = certificateVersionId.Append(result.stringOf("id"))
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
if (certificateVersionId.StartsWith(bundlePrefix.getAsString(),true) != true)
print "Returned identifier is outside the expected vault/resource." + "\n";
exit
end
certificateVersion = certificateVersionId.getRange(bundlePrefix.get_Length(),certificateVersionId.get_Length() - bundlePrefix.get_Length(),false)
validate7 = Chilkat::CkStringBuilder.new()
alphabet7 = Chilkat::CkStringBuilder.new()
success = validate7.Append(certificateVersion)
if (success != true)
print "Could not validate resource identifier." + "\n";
exit
end
success = alphabet7.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate7.get_Length() < 1) or (validate7.get_Length() > 127))
print "Invalid resource identifier length." + "\n";
exit
end
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
end
charIndex7 = charIndex7 + 1
end
keyPrefix = Chilkat::CkStringBuilder.new()
success = keyPrefix.Append(origin.getAsString())
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = keyPrefix.Append("/keys/")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
keyTailId = Chilkat::CkStringBuilder.new()
success = keyTailId.Append(result.stringOf("kid"))
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
if (keyTailId.StartsWith(keyPrefix.getAsString(),true) != true)
print "Returned identifier is outside the expected vault/resource." + "\n";
exit
end
keyTail = keyTailId.getRange(keyPrefix.get_Length(),keyTailId.get_Length() - keyPrefix.get_Length(),false)
keyTailParts = Chilkat::CkStringBuilder.new()
success = keyTailParts.Append(keyTail)
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
keyTailName = keyTailParts.getBefore("/",false)
keyTailVersion = keyTailParts.getAfterFinal("/",false)
validate8 = Chilkat::CkStringBuilder.new()
alphabet8 = Chilkat::CkStringBuilder.new()
success = validate8.Append(keyTailName)
if (success != true)
print "Could not validate resource name." + "\n";
exit
end
success = alphabet8.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate8.get_Length() < 1) or (validate8.get_Length() > 127))
print "Invalid resource name length." + "\n";
exit
end
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
end
charIndex8 = charIndex8 + 1
end
validate9 = Chilkat::CkStringBuilder.new()
alphabet9 = Chilkat::CkStringBuilder.new()
success = validate9.Append(keyTailVersion)
if (success != true)
print "Could not validate resource version." + "\n";
exit
end
success = alphabet9.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate9.get_Length() < 1) or (validate9.get_Length() > 127))
print "Invalid resource version length." + "\n";
exit
end
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
end
charIndex9 = charIndex9 + 1
end
keyTailCanonical = Chilkat::CkStringBuilder.new()
success = keyTailCanonical.Append(keyTailName)
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = keyTailCanonical.Append("/")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = keyTailCanonical.Append(keyTailVersion)
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
stringCompare.SetString(keyTail)
stringMatch14 = stringCompare.ContentsEqual(keyTailCanonical.getAsString(),true)
if (stringMatch14 != true)
print "Expected a versioned resource identifier." + "\n";
exit
end
certificateId = result.stringOf("id")
keyId = result.stringOf("kid")
remoteCert = Chilkat::CkCert.new()
success = remoteCert.LoadFromBase64(result.stringOf("cer"))
if (success != true)
print "Could not load the returned public certificate." + "\n";
exit
end
sourceDer = cert.getEncoded()
if (cert.get_LastMethodSuccess() != true)
print "Could not encode source certificate." + "\n";
exit
end
importedDer = remoteCert.getEncoded()
if (remoteCert.get_LastMethodSuccess() != true)
print "Could not encode imported certificate." + "\n";
exit
end
stringCompare.SetString(sourceDer)
stringMatch15 = stringCompare.ContentsEqual(importedDer,true)
if (stringMatch15 != true)
print "Imported certificate does not match the PFX leaf certificate." + "\n";
exit
end
print "Imported certificate ID: " + certificateId + "\n";
print "Backing key ID: " + keyId + "\n";