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

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.

Microsoft reference

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

Dim stringCompare As ChilkatStringBuilder
stringCompare.Initialize
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Keep this configuration private, outside public web content and source control.
Dim config As ChilkatJsonObject
config.Initialize
config.Strict = True
success = config.LoadFile("qa_data/azure/keyvault.json")
If success <> True Then
    Log("Could not load private Key Vault configuration.")
    Return
End If

stringCompare.SetString(config.StringOf("vault_name"))
Dim stringMatch1 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = True) Then
    Log("Missing or invalid vault_name.")
    Return
End If

Dim validate1 As ChilkatStringBuilder
validate1.Initialize
Dim alphabet1 As ChilkatStringBuilder
alphabet1.Initialize
success = validate1.Append(config.StringOf("vault_name"))
If success <> True Then
    Log("Could not validate vault_name.")
    Return
End If

success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate1.Length < 3) Or (validate1.Length > 24) Then
    Log("Invalid vault_name length.")
    Return
End If

Dim charIndex1 As Int = 0
Do While (charIndex1 < validate1.Length)
    If alphabet1.Contains(validate1.GetRange(charIndex1, 1, False), True) <> True Then
        Log("Invalid character in vault_name.")
        Return
    End If

    charIndex1 = charIndex1 + 1
Loop
Dim vaultName As ChilkatStringBuilder
vaultName.Initialize
vaultName.Append(config.StringOf("vault_name"))
Dim letters As ChilkatStringBuilder
letters.Initialize
letters.Append("abcdefghijklmnopqrstuvwxyz")
If (letters.Contains(vaultName.GetRange(0, 1, False), True) <> True) Or (vaultName.EndsWith("-", True) = True) Or (vaultName.Contains("--", True) = True) Then
    Log("Invalid vault_name.")
    Return
End If

Dim origin As ChilkatStringBuilder
origin.Initialize
success = origin.Append("https://")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = origin.Append(config.StringOf("vault_name"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = origin.Append(".vault.azure.net")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

stringCompare.SetString(config.StringOf("cert_name"))
Dim stringMatch2 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("cert_name") <> 1) Or (stringMatch2 = True) Then
    Log("Missing or invalid cert_name.")
    Return
End If

Dim validate2 As ChilkatStringBuilder
validate2.Initialize
Dim alphabet2 As ChilkatStringBuilder
alphabet2.Initialize
success = validate2.Append(config.StringOf("cert_name"))
If success <> True Then
    Log("Could not validate cert_name.")
    Return
End If

success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate2.Length < 1) Or (validate2.Length > 127) Then
    Log("Invalid cert_name length.")
    Return
End If

Dim charIndex2 As Int = 0
Do While (charIndex2 < validate2.Length)
    If alphabet2.Contains(validate2.GetRange(charIndex2, 1, False), True) <> True Then
        Log("Invalid character in cert_name.")
        Return
    End If

    charIndex2 = charIndex2 + 1
Loop
stringCompare.SetString(config.StringOf("client_id"))
Dim stringMatch3 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("client_id") <> 1) Or (stringMatch3 = True) Then
    Log("Missing or invalid client_id.")
    Return
End If

stringCompare.SetString(config.StringOf("client_secret"))
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("client_secret") <> 1) Or (stringMatch4 = True) Then
    Log("Missing or invalid client_secret.")
    Return
End If

stringCompare.SetString(config.StringOf("tenant_id"))
Dim stringMatch5 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch5 = True) Then
    Log("Missing or invalid tenant_id.")
    Return
End If

Dim validate4 As ChilkatStringBuilder
validate4.Initialize
Dim alphabet4 As ChilkatStringBuilder
alphabet4.Initialize
success = validate4.Append(config.StringOf("tenant_id"))
If success <> True Then
    Log("Could not validate tenant GUID.")
    Return
End If

success = alphabet4.Append("0123456789abcdefABCDEF-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate4.Length < 36) Or (validate4.Length > 36) Then
    Log("Invalid tenant GUID length.")
    Return
End If

Dim charIndex4 As Int = 0
Do While (charIndex4 < validate4.Length)
    If alphabet4.Contains(validate4.GetRange(charIndex4, 1, False), True) <> True Then
        Log("Invalid character in tenant GUID.")
        Return
    End If

    charIndex4 = charIndex4 + 1
Loop
If config.JsonTypeOf("pfx_password") <> 1 Then
    Log("Missing pfx_password string; empty is allowed for an unprotected PFX.")
    Return
End If

Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadPfxFile("qa_data/pfx/myCertAndKey.pfx", config.StringOf("pfx_password"))
If success <> True Then
    Log("Could not load PFX; inspect diagnostics privately.")
    Return
End If

Dim privateKey As ChilkatPrivateKey
privateKey.Initialize("privateKey")
success = cert.GetPrivateKey(privateKey)
If success <> True Then
    Log("PFX must contain a usable private key.")
    Return
End If

'  Inspect only the PUBLIC JWK. Never print or export a private JWK for key inspection.
Dim publicKey As ChilkatPublicKey
publicKey.Initialize
success = privateKey.ToPublicKey(publicKey)
If success <> True Then
    Log("Could not get the public key.")
    Return
End If

Dim publicJwk As String = publicKey.GetJwk
If publicKey.LastMethodSuccess <> True Then
    Log("Could not inspect the public key.")
    Return
End If

Dim keyInfo As ChilkatJsonObject
keyInfo.Initialize
keyInfo.Strict = True
success = keyInfo.Load(publicJwk)
If success <> True Then
    Log("Invalid public JWK.")
    Return
End If

Dim supported As Boolean = False
stringCompare.SetString(keyInfo.StringOf("kty"))
Dim stringMatch6 As Boolean = stringCompare.ContentsEqual("RSA", True)
If stringMatch6 = True Then
    supported = (privateKey.BitLength = 2048) Or (privateKey.BitLength = 3072) Or (privateKey.BitLength = 4096)
End If

stringCompare.SetString(keyInfo.StringOf("kty"))
Dim stringMatch7 As Boolean = stringCompare.ContentsEqual("EC", True)
If stringMatch7 = True Then
    stringCompare.SetString(keyInfo.StringOf("crv"))
    Dim stringMatch8 As Boolean = stringCompare.ContentsEqual("P-256", True)
    stringCompare.SetString(keyInfo.StringOf("crv"))
    Dim stringMatch9 As Boolean = stringCompare.ContentsEqual("P-384", True)
    stringCompare.SetString(keyInfo.StringOf("crv"))
    Dim stringMatch10 As Boolean = stringCompare.ContentsEqual("P-521", True)
    If (stringMatch8 = True) Or (stringMatch9 = True) Or (stringMatch10 = True) Then
        supported = True
    End If

End If

If supported <> True Then
    Log("This example supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521.")
    Return
End If

Dim options As ChilkatJsonObject
options.Initialize
success = options.UpdateString("service", "azure-keyvault")
If success <> True Then
    Log("Could not set service.")
    Return
End If

success = options.UpdateString("auth.client_id", config.StringOf("client_id"))
If success <> True Then
    Log("Could not set auth.client_id.")
    Return
End If

success = options.UpdateString("auth.client_secret", config.StringOf("client_secret"))
If success <> True Then
    Log("Could not set auth.client_secret.")
    Return
End If

success = options.UpdateString("auth.tenant_id", config.StringOf("tenant_id"))
If success <> True Then
    Log("Could not set auth.tenant_id.")
    Return
End If

success = options.UpdateString("vault_name", config.StringOf("vault_name"))
If success <> True Then
    Log("Could not set vault_name.")
    Return
End If

success = options.UpdateString("cert_name", config.StringOf("cert_name"))
If success <> True Then
    Log("Could not set cert_name.")
    Return
End If

Dim result As ChilkatJsonObject
result.Initialize
result.Strict = True
'  UploadToCloud uses azure-keyvault; SetCloudSigner uses azure_keyvault.
success = cert.UploadToCloud(options, result)
If success <> True Then
    Log("Import failed or its outcome is uncertain. Check the vault before retrying.")
    Return
End If

Dim certificateBase As ChilkatStringBuilder
certificateBase.Initialize
success = certificateBase.Append(origin.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = certificateBase.Append("/certificates/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = certificateBase.Append(config.StringOf("cert_name"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

Dim bundlePrefix As ChilkatStringBuilder
bundlePrefix.Initialize
success = bundlePrefix.Append(certificateBase.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = bundlePrefix.Append("/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

stringCompare.SetString(result.StringOf("id"))
Dim stringMatch11 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("id") <> 1) Or (stringMatch11 = True) Then
    Log("Missing or invalid id.")
    Return
End If

stringCompare.SetString(result.StringOf("kid"))
Dim stringMatch12 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("kid") <> 1) Or (stringMatch12 = True) Then
    Log("Missing or invalid kid.")
    Return
End If

stringCompare.SetString(result.StringOf("cer"))
Dim stringMatch13 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("cer") <> 1) Or (stringMatch13 = True) Then
    Log("Missing or invalid cer.")
    Return
End If

Dim certificateVersionId As ChilkatStringBuilder
certificateVersionId.Initialize
success = certificateVersionId.Append(result.StringOf("id"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

If certificateVersionId.StartsWith(bundlePrefix.GetAsString, True) <> True Then
    Log("Returned identifier is outside the expected vault/resource.")
    Return
End If

Dim certificateVersion As String = certificateVersionId.GetRange(bundlePrefix.Length, certificateVersionId.Length - bundlePrefix.Length, False)
Dim validate7 As ChilkatStringBuilder
validate7.Initialize
Dim alphabet7 As ChilkatStringBuilder
alphabet7.Initialize
success = validate7.Append(certificateVersion)
If success <> True Then
    Log("Could not validate resource identifier.")
    Return
End If

success = alphabet7.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate7.Length < 1) Or (validate7.Length > 127) Then
    Log("Invalid resource identifier length.")
    Return
End If

Dim charIndex7 As Int = 0
Do While (charIndex7 < validate7.Length)
    If alphabet7.Contains(validate7.GetRange(charIndex7, 1, False), True) <> True Then
        Log("Invalid character in resource identifier.")
        Return
    End If

    charIndex7 = charIndex7 + 1
Loop
Dim keyPrefix As ChilkatStringBuilder
keyPrefix.Initialize
success = keyPrefix.Append(origin.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = keyPrefix.Append("/keys/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

Dim keyTailId As ChilkatStringBuilder
keyTailId.Initialize
success = keyTailId.Append(result.StringOf("kid"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

If keyTailId.StartsWith(keyPrefix.GetAsString, True) <> True Then
    Log("Returned identifier is outside the expected vault/resource.")
    Return
End If

Dim keyTail As String = keyTailId.GetRange(keyPrefix.Length, keyTailId.Length - keyPrefix.Length, False)
Dim keyTailParts As ChilkatStringBuilder
keyTailParts.Initialize
success = keyTailParts.Append(keyTail)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

Dim keyTailName As String = keyTailParts.GetBefore("/", False)
Dim keyTailVersion As String = keyTailParts.GetAfterFinal("/", False)
Dim validate8 As ChilkatStringBuilder
validate8.Initialize
Dim alphabet8 As ChilkatStringBuilder
alphabet8.Initialize
success = validate8.Append(keyTailName)
If success <> True Then
    Log("Could not validate resource name.")
    Return
End If

success = alphabet8.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate8.Length < 1) Or (validate8.Length > 127) Then
    Log("Invalid resource name length.")
    Return
End If

Dim charIndex8 As Int = 0
Do While (charIndex8 < validate8.Length)
    If alphabet8.Contains(validate8.GetRange(charIndex8, 1, False), True) <> True Then
        Log("Invalid character in resource name.")
        Return
    End If

    charIndex8 = charIndex8 + 1
Loop
Dim validate9 As ChilkatStringBuilder
validate9.Initialize
Dim alphabet9 As ChilkatStringBuilder
alphabet9.Initialize
success = validate9.Append(keyTailVersion)
If success <> True Then
    Log("Could not validate resource version.")
    Return
End If

success = alphabet9.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate9.Length < 1) Or (validate9.Length > 127) Then
    Log("Invalid resource version length.")
    Return
End If

Dim charIndex9 As Int = 0
Do While (charIndex9 < validate9.Length)
    If alphabet9.Contains(validate9.GetRange(charIndex9, 1, False), True) <> True Then
        Log("Invalid character in resource version.")
        Return
    End If

    charIndex9 = charIndex9 + 1
Loop
Dim keyTailCanonical As ChilkatStringBuilder
keyTailCanonical.Initialize
success = keyTailCanonical.Append(keyTailName)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = keyTailCanonical.Append("/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = keyTailCanonical.Append(keyTailVersion)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

stringCompare.SetString(keyTail)
Dim stringMatch14 As Boolean = stringCompare.ContentsEqual(keyTailCanonical.GetAsString, True)
If stringMatch14 <> True Then
    Log("Expected a versioned resource identifier.")
    Return
End If

Dim certificateId As String = result.StringOf("id")
Dim keyId As String = result.StringOf("kid")
Dim remoteCert As ChilkatCert
remoteCert.Initialize("remoteCert")
success = remoteCert.LoadFromBase64(result.StringOf("cer"))
If success <> True Then
    Log("Could not load the returned public certificate.")
    Return
End If

Dim sourceDer As String = cert.GetEncoded
If cert.LastMethodSuccess <> True Then
    Log("Could not encode source certificate.")
    Return
End If

Dim importedDer As String = remoteCert.GetEncoded
If remoteCert.LastMethodSuccess <> True Then
    Log("Could not encode imported certificate.")
    Return
End If

stringCompare.SetString(sourceDer)
Dim stringMatch15 As Boolean = stringCompare.ContentsEqual(importedDer, True)
If stringMatch15 <> True Then
    Log("Imported certificate does not match the PFX leaf certificate.")
    Return
End If

Log("Imported certificate ID: " & certificateId)
Log("Backing key ID: " & keyId)