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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
set config = Server.CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private Key Vault configuration.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("vault_name"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid vault_name.") & "</pre>"
Response.End
End If
set validate1 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet1 = Server.CreateObject("Chilkat.StringBuilder")
success = validate1.Append(config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate vault_name.") & "</pre>"
Response.End
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid vault_name length.") & "</pre>"
Response.End
End If
charIndex1 = 0
Do While (charIndex1 < validate1.Length)
If (alphabet1.Contains(validate1.GetRange(charIndex1,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in vault_name.") & "</pre>"
Response.End
End If
charIndex1 = charIndex1 + 1
Loop
set vaultName = Server.CreateObject("Chilkat.StringBuilder")
success = vaultName.Append(config.StringOf("vault_name"))
set letters = Server.CreateObject("Chilkat.StringBuilder")
success = letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,0),1) <> 1) Or (vaultName.EndsWith("-",1) = 1) Or (vaultName.Contains("--",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid vault_name.") & "</pre>"
Response.End
End If
set origin = Server.CreateObject("Chilkat.StringBuilder")
success = origin.Append("https://")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = origin.Append(config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = origin.Append(".vault.azure.net")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("cert_name"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("cert_name") <> 1) Or (stringMatch2 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid cert_name.") & "</pre>"
Response.End
End If
set validate2 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet2 = Server.CreateObject("Chilkat.StringBuilder")
success = validate2.Append(config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate cert_name.") & "</pre>"
Response.End
End If
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate2.Length < 1) Or (validate2.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid cert_name length.") & "</pre>"
Response.End
End If
charIndex2 = 0
Do While (charIndex2 < validate2.Length)
If (alphabet2.Contains(validate2.GetRange(charIndex2,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in cert_name.") & "</pre>"
Response.End
End If
charIndex2 = charIndex2 + 1
Loop
success = stringCompare.SetString(config.StringOf("client_id"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_id") <> 1) Or (stringMatch3 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid client_id.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("client_secret"))
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_secret") <> 1) Or (stringMatch4 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid client_secret.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("tenant_id"))
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch5 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid tenant_id.") & "</pre>"
Response.End
End If
set validate4 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet4 = Server.CreateObject("Chilkat.StringBuilder")
success = validate4.Append(config.StringOf("tenant_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate tenant GUID.") & "</pre>"
Response.End
End If
success = alphabet4.Append("0123456789abcdefABCDEF-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate4.Length < 36) Or (validate4.Length > 36)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid tenant GUID length.") & "</pre>"
Response.End
End If
charIndex4 = 0
Do While (charIndex4 < validate4.Length)
If (alphabet4.Contains(validate4.GetRange(charIndex4,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in tenant GUID.") & "</pre>"
Response.End
End If
charIndex4 = charIndex4 + 1
Loop
If (config.JsonTypeOf("pfx_password") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing pfx_password string; empty is allowed for an unprotected PFX.") & "</pre>"
Response.End
End If
set cert = Server.CreateObject("Chilkat.Cert")
success = cert.LoadPfxFile("qa_data/pfx/myCertAndKey.pfx",config.StringOf("pfx_password"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load PFX; inspect diagnostics privately.") & "</pre>"
Response.End
End If
set privateKey = Server.CreateObject("Chilkat.PrivateKey")
success = cert.GetPrivateKey(privateKey)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "PFX must contain a usable private key.") & "</pre>"
Response.End
End If
' Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
set publicKey = Server.CreateObject("Chilkat.PublicKey")
success = privateKey.ToPublicKey(publicKey)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not get the public key.") & "</pre>"
Response.End
End If
publicJwk = publicKey.GetJwk()
If (publicKey.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not inspect the public key.") & "</pre>"
Response.End
End If
set keyInfo = Server.CreateObject("Chilkat.JsonObject")
keyInfo.Strict = 1
success = keyInfo.Load(publicJwk)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid public JWK.") & "</pre>"
Response.End
End If
supported = 0
success = stringCompare.SetString(keyInfo.StringOf("kty"))
stringMatch6 = stringCompare.ContentsEqual("RSA",1)
If (stringMatch6 = 1) Then
supported = (privateKey.BitLength = 2048) Or (privateKey.BitLength = 3072) Or (privateKey.BitLength = 4096)
End If
success = stringCompare.SetString(keyInfo.StringOf("kty"))
stringMatch7 = stringCompare.ContentsEqual("EC",1)
If (stringMatch7 = 1) Then
success = stringCompare.SetString(keyInfo.StringOf("crv"))
stringMatch8 = stringCompare.ContentsEqual("P-256",1)
success = stringCompare.SetString(keyInfo.StringOf("crv"))
stringMatch9 = stringCompare.ContentsEqual("P-384",1)
success = stringCompare.SetString(keyInfo.StringOf("crv"))
stringMatch10 = stringCompare.ContentsEqual("P-521",1)
If ((stringMatch8 = 1) Or (stringMatch9 = 1) Or (stringMatch10 = 1)) Then
supported = 1
End If
End If
If (supported <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521.") & "</pre>"
Response.End
End If
set options = Server.CreateObject("Chilkat.JsonObject")
success = options.UpdateString("service","azure-keyvault")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set service.") & "</pre>"
Response.End
End If
success = options.UpdateString("auth.client_id",config.StringOf("client_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set auth.client_id.") & "</pre>"
Response.End
End If
success = options.UpdateString("auth.client_secret",config.StringOf("client_secret"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set auth.client_secret.") & "</pre>"
Response.End
End If
success = options.UpdateString("auth.tenant_id",config.StringOf("tenant_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set auth.tenant_id.") & "</pre>"
Response.End
End If
success = options.UpdateString("vault_name",config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set vault_name.") & "</pre>"
Response.End
End If
success = options.UpdateString("cert_name",config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set cert_name.") & "</pre>"
Response.End
End If
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1
' UploadToCloud uses azure-keyvault; SetCloudSigner uses azure_keyvault.
success = cert.UploadToCloud(options,result)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Import failed or its outcome is uncertain. Check the vault before retrying.") & "</pre>"
Response.End
End If
set certificateBase = Server.CreateObject("Chilkat.StringBuilder")
success = certificateBase.Append(origin.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = certificateBase.Append("/certificates/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = certificateBase.Append(config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
set bundlePrefix = Server.CreateObject("Chilkat.StringBuilder")
success = bundlePrefix.Append(certificateBase.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = bundlePrefix.Append("/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("id"))
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("id") <> 1) Or (stringMatch11 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid id.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("kid"))
stringMatch12 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("kid") <> 1) Or (stringMatch12 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid kid.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("cer"))
stringMatch13 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("cer") <> 1) Or (stringMatch13 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid cer.") & "</pre>"
Response.End
End If
set certificateVersionId = Server.CreateObject("Chilkat.StringBuilder")
success = certificateVersionId.Append(result.StringOf("id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
If (certificateVersionId.StartsWith(bundlePrefix.GetAsString(),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Returned identifier is outside the expected vault/resource.") & "</pre>"
Response.End
End If
certificateVersion = certificateVersionId.GetRange(bundlePrefix.Length,certificateVersionId.Length - bundlePrefix.Length,0)
set validate7 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet7 = Server.CreateObject("Chilkat.StringBuilder")
success = validate7.Append(certificateVersion)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate resource identifier.") & "</pre>"
Response.End
End If
success = alphabet7.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate7.Length < 1) Or (validate7.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid resource identifier length.") & "</pre>"
Response.End
End If
charIndex7 = 0
Do While (charIndex7 < validate7.Length)
If (alphabet7.Contains(validate7.GetRange(charIndex7,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in resource identifier.") & "</pre>"
Response.End
End If
charIndex7 = charIndex7 + 1
Loop
set keyPrefix = Server.CreateObject("Chilkat.StringBuilder")
success = keyPrefix.Append(origin.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = keyPrefix.Append("/keys/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
set keyTailId = Server.CreateObject("Chilkat.StringBuilder")
success = keyTailId.Append(result.StringOf("kid"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
If (keyTailId.StartsWith(keyPrefix.GetAsString(),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Returned identifier is outside the expected vault/resource.") & "</pre>"
Response.End
End If
keyTail = keyTailId.GetRange(keyPrefix.Length,keyTailId.Length - keyPrefix.Length,0)
set keyTailParts = Server.CreateObject("Chilkat.StringBuilder")
success = keyTailParts.Append(keyTail)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
keyTailName = keyTailParts.GetBefore("/",0)
keyTailVersion = keyTailParts.GetAfterFinal("/",0)
set validate8 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet8 = Server.CreateObject("Chilkat.StringBuilder")
success = validate8.Append(keyTailName)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate resource name.") & "</pre>"
Response.End
End If
success = alphabet8.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate8.Length < 1) Or (validate8.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid resource name length.") & "</pre>"
Response.End
End If
charIndex8 = 0
Do While (charIndex8 < validate8.Length)
If (alphabet8.Contains(validate8.GetRange(charIndex8,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in resource name.") & "</pre>"
Response.End
End If
charIndex8 = charIndex8 + 1
Loop
set validate9 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet9 = Server.CreateObject("Chilkat.StringBuilder")
success = validate9.Append(keyTailVersion)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate resource version.") & "</pre>"
Response.End
End If
success = alphabet9.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate9.Length < 1) Or (validate9.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid resource version length.") & "</pre>"
Response.End
End If
charIndex9 = 0
Do While (charIndex9 < validate9.Length)
If (alphabet9.Contains(validate9.GetRange(charIndex9,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in resource version.") & "</pre>"
Response.End
End If
charIndex9 = charIndex9 + 1
Loop
set keyTailCanonical = Server.CreateObject("Chilkat.StringBuilder")
success = keyTailCanonical.Append(keyTailName)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = keyTailCanonical.Append("/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = keyTailCanonical.Append(keyTailVersion)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(keyTail)
stringMatch14 = stringCompare.ContentsEqual(keyTailCanonical.GetAsString(),1)
If (stringMatch14 <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a versioned resource identifier.") & "</pre>"
Response.End
End If
certificateId = result.StringOf("id")
keyId = result.StringOf("kid")
set remoteCert = Server.CreateObject("Chilkat.Cert")
success = remoteCert.LoadFromBase64(result.StringOf("cer"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load the returned public certificate.") & "</pre>"
Response.End
End If
sourceDer = cert.GetEncoded()
If (cert.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not encode source certificate.") & "</pre>"
Response.End
End If
importedDer = remoteCert.GetEncoded()
If (remoteCert.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not encode imported certificate.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(sourceDer)
stringMatch15 = stringCompare.ContentsEqual(importedDer,1)
If (stringMatch15 <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Imported certificate does not match the PFX leaf certificate.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Imported certificate ID: " & certificateId) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Backing key ID: " & keyId) & "</pre>"
%>
</body>
</html>