Azure Key Vault: Create a Self-Signed Certificate and Poll
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 and certificate_subject, such as CN=example.test. Creates a nonexportable RSA-2048 certificate key with 12-month validity and no key reuse. Requires certificates/create and certificates/get. HTTP 202 means accepted, not complete; polling is bounded to 30 requests and a 120-second checked deadline, with per-request timeouts. Issuer Self creates a self-signed certificate, unsuitable as a publicly trusted TLS identity. Retrieves the completed target and exports only its public DER.
Chilkat B4X Downloads
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
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = False
Dim request As ChilkatHttpRequest
request.Initialize
request.HttpVerb = "GET"
Dim response As ChilkatHttpResponse
response.Initialize
Dim result As ChilkatJsonObject
result.Initialize
result.Strict = True
' First run Get a Key Vault access token.
Dim token As ChilkatJsonObject
token.Initialize
token.Strict = True
success = token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
If success <> True Then
Log("Could not read the private token file.")
Return
End If
stringCompare.SetString(token.StringOf("access_token"))
Dim stringMatch2 As Boolean = stringCompare.ContentsEqual("", True)
If (token.JsonTypeOf("access_token") <> 1) Or (stringMatch2 = True) Then
Log("Missing or invalid access_token.")
Return
End If
stringCompare.SetString(token.StringOf("expires_at"))
Dim stringMatch3 As Boolean = stringCompare.ContentsEqual("", True)
If (token.JsonTypeOf("expires_at") <> 1) Or (stringMatch3 = True) Then
Log("Missing or invalid expires_at.")
Return
End If
Dim expires As ChilkatDateTime
expires.Initialize
Dim now As ChilkatDateTime
now.Initialize
success = expires.SetFromTimestamp(token.StringOf("expires_at"))
If success <> True Then
Log("Invalid token expiration.")
Return
End If
http.AuthToken = token.StringOf("access_token")
stringCompare.SetString(config.StringOf("cert_name"))
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("cert_name") <> 1) Or (stringMatch4 = True) Then
Log("Missing or invalid cert_name.")
Return
End If
Dim validate40 As ChilkatStringBuilder
validate40.Initialize
Dim alphabet40 As ChilkatStringBuilder
alphabet40.Initialize
success = validate40.Append(config.StringOf("cert_name"))
If success <> True Then
Log("Could not validate cert_name.")
Return
End If
success = alphabet40.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
Log("Could not initialize validation.")
Return
End If
If (validate40.Length < 1) Or (validate40.Length > 127) Then
Log("Invalid cert_name length.")
Return
End If
Dim charIndex40 As Int = 0
Do While (charIndex40 < validate40.Length)
If alphabet40.Contains(validate40.GetRange(charIndex40, 1, False), True) <> True Then
Log("Invalid character in cert_name.")
Return
End If
charIndex40 = charIndex40 + 1
Loop
Dim resourceBase As ChilkatStringBuilder
resourceBase.Initialize
success = resourceBase.Append(origin.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = resourceBase.Append("/certificates/")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = resourceBase.Append(config.StringOf("cert_name"))
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
stringCompare.SetString(config.StringOf("certificate_subject"))
Dim stringMatch5 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("certificate_subject") <> 1) Or (stringMatch5 = True) Then
Log("Missing or invalid certificate_subject.")
Return
End If
Dim files As ChilkatFileAccess
files.Initialize
If files.FileExists("qa_output/created-certificate.cer") = True Then
Log("Output exists; choose another private output path.")
Return
End If
Dim body As ChilkatJsonObject
body.Initialize
success = body.UpdateString("policy.key_props.kty", "RSA")
If success <> True Then
Log("Could not set policy.key_props.kty.")
Return
End If
success = body.UpdateInt("policy.key_props.key_size", 2048)
If success <> True Then
Log("Could not set key size.")
Return
End If
success = body.UpdateBool("policy.key_props.exportable", False)
If success <> True Then
Log("Could not set exportability.")
Return
End If
success = body.UpdateBool("policy.key_props.reuse_key", False)
If success <> True Then
Log("Could not set reuse policy.")
Return
End If
success = body.UpdateString("policy.secret_props.contentType", "application/x-pkcs12")
If success <> True Then
Log("Could not set policy.secret_props.contentType.")
Return
End If
success = body.UpdateString("policy.x509_props.subject", config.StringOf("certificate_subject"))
If success <> True Then
Log("Could not set policy.x509_props.subject.")
Return
End If
success = body.UpdateInt("policy.x509_props.validity_months", 12)
If success <> True Then
Log("Could not set validity.")
Return
End If
success = body.UpdateString("policy.issuer.name", "Self")
If success <> True Then
Log("Could not set policy.issuer.name.")
Return
End If
Dim createUrl As ChilkatStringBuilder
createUrl.Initialize
success = createUrl.Append(resourceBase.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = createUrl.Append("/create?api-version=2026-04-01")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = now.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read current time.")
Return
End If
If expires.DiffSeconds(now) <= 60 Then
Log("Token is expired or near expiration. Obtain a new token before continuing.")
Return
End If
success = http.HttpJson("POST", createUrl.GetAsString, body, "application/json", response)
If success <> True Then
Log("Request failed; reconcile uncertain writes before retrying.")
Return
End If
If response.StatusCode <> 202 Then
Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Invalid JSON response.")
Return
End If
Dim pendingUrl As ChilkatStringBuilder
pendingUrl.Initialize
success = pendingUrl.Append(resourceBase.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = pendingUrl.Append("/pending?api-version=2026-04-01")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim started As ChilkatDateTime
started.Initialize
Dim pollNow As ChilkatDateTime
pollNow.Initialize
success = started.SetFromCurrentSystemTime
If success <> True Then
Log("Could not start polling deadline.")
Return
End If
Dim ready As Boolean = False
Dim polls As Int = 0
Do While ((ready <> True) And (polls < 30))
polls = polls + 1
success = pollNow.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read time.")
Return
End If
If pollNow.DiffSeconds(started) >= 120 Then
Log("Polling deadline reached; operation may still be pending. Resume GET polling, do not recreate.")
Return
End If
success = now.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read current time.")
Return
End If
If expires.DiffSeconds(now) <= 60 Then
Log("Token is expired or near expiration. Obtain a new token before continuing.")
Return
End If
request.HttpVerb = "GET"
success = http.HttpReq(pendingUrl.GetAsString, request, response)
If success <> True Then
Log("Request did not complete; inspect diagnostics privately.")
Return
End If
If response.StatusCode <> 200 Then
Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Invalid JSON response.")
Return
End If
stringCompare.SetString(result.StringOf("status"))
Dim stringMatch6 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("status") <> 1) Or (stringMatch6 = True) Then
Log("Missing or invalid status.")
Return
End If
stringCompare.SetString(result.StringOf("status"))
Dim stringMatch7 As Boolean = stringCompare.ContentsEqual("inProgress", True)
stringCompare.SetString(result.StringOf("status"))
Dim stringMatch8 As Boolean = stringCompare.ContentsEqual("completed", True)
If (stringMatch7 <> True) And (stringMatch8 <> True) Then
Log("Certificate operation failed, was cancelled, or returned an unknown status.")
Return
End If
stringCompare.SetString(result.StringOf("status"))
Dim stringMatch9 As Boolean = stringCompare.ContentsEqual("completed", True)
If stringMatch9 = True Then
ready = True
End If
If ready <> True Then
http.SleepMs(2000)
End If
Loop
If ready <> True Then
Log("Poll limit reached; resume polling without repeating creation.")
Return
End If
stringCompare.SetString(result.StringOf("target"))
Dim stringMatch10 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("target") <> 1) Or (stringMatch10 = True) Then
Log("Missing or invalid target.")
Return
End If
Dim certPrefix As ChilkatStringBuilder
certPrefix.Initialize
success = certPrefix.Append(resourceBase.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = certPrefix.Append("/")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim createdVersionId As ChilkatStringBuilder
createdVersionId.Initialize
success = createdVersionId.Append(result.StringOf("target"))
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
If createdVersionId.StartsWith(certPrefix.GetAsString, True) <> True Then
Log("Returned identifier is outside the expected vault/resource.")
Return
End If
Dim createdVersion As String = createdVersionId.GetRange(certPrefix.Length, createdVersionId.Length - certPrefix.Length, False)
Dim validate41 As ChilkatStringBuilder
validate41.Initialize
Dim alphabet41 As ChilkatStringBuilder
alphabet41.Initialize
success = validate41.Append(createdVersion)
If success <> True Then
Log("Could not validate resource identifier.")
Return
End If
success = alphabet41.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
Log("Could not initialize validation.")
Return
End If
If (validate41.Length < 1) Or (validate41.Length > 127) Then
Log("Invalid resource identifier length.")
Return
End If
Dim charIndex41 As Int = 0
Do While (charIndex41 < validate41.Length)
If alphabet41.Contains(validate41.GetRange(charIndex41, 1, False), True) <> True Then
Log("Invalid character in resource identifier.")
Return
End If
charIndex41 = charIndex41 + 1
Loop
Dim completedId As String = result.StringOf("target")
Dim createdUrl As ChilkatStringBuilder
createdUrl.Initialize
success = createdUrl.Append(completedId)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = createdUrl.Append("?api-version=2026-04-01")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = now.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read current time.")
Return
End If
If expires.DiffSeconds(now) <= 60 Then
Log("Token is expired or near expiration. Obtain a new token before continuing.")
Return
End If
request.HttpVerb = "GET"
success = http.HttpReq(createdUrl.GetAsString, request, response)
If success <> True Then
Log("Request did not complete; inspect diagnostics privately.")
Return
End If
If response.StatusCode <> 200 Then
Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Invalid JSON response.")
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("id"))
Dim stringMatch12 As Boolean = stringCompare.ContentsEqual(completedId, True)
If stringMatch12 <> True Then
Log("Completed certificate response ID mismatch.")
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 certificate As ChilkatCert
certificate.Initialize("certificate")
success = certificate.LoadFromBase64(result.StringOf("cer"))
If success <> True Then
Log("Invalid certificate.")
Return
End If
success = certificate.ExportCertDerFile("qa_output/created-certificate.cer")
If success <> True Then
Log("Could not export public certificate.")
Return
End If
Log("Example completed.")