PowerBuilder
PowerBuilder
Azure Key Vault Import Certificate
See more Azure Key Vault Examples
Imports a certificate into a specified Azure key vault.Imports an existing valid certificate, containing a private key, into Azure Key Vault. The certificate to be imported can be in either PFX or PEM format. If the certificate is in PEM format the PEM file must contain the key as well as x509 certificates. Key Vault will only accept a key in PKCS#8 format.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Json
string ls_PfxFilePath
oleobject loo_BdPfx
oleobject loo_Cert
oleobject loo_PrivKey
oleobject loo_Jwk
oleobject loo_SbKty
oleobject loo_SbCurve
oleobject loo_JsonBody
oleobject loo_Http
string ls_Url
oleobject loo_Resp
integer li_StatusCode
oleobject loo_JsonResp
string ls_StrVal
integer li_Lifetime_percentage
string ls_Action_type
string ls_Id
string ls_Kid
string ls_Sid
string ls_X5t
string ls_Cer
integer li_Enabled
integer li_Nbf
integer li_Exp
integer li_Created
integer li_Updated
string ls_RecoveryLevel
integer li_RecoverableDays
string ls_Id
integer li_Exportable
string ls_Kty
integer li_Key_size
integer li_Reuse_key
string ls_ContentType
string ls_Subject
integer li_Validity_months
integer li_Ca
string ls_Name
integer li_AttributesEnabled
integer li_AttributesCreated
integer li_AttributesUpdated
integer i
integer li_Count_i
li_Success = 0
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// See Azure Key Vault Get Certificates for a more detailed explanation
// for how Chilkat is automatically getting the OAuth2 access token for your application.
// Provide information needed for Chilkat to automatically get an OAuth2 access token as needed.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Json
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Json.UpdateString("client_id","APP_ID")
loo_Json.UpdateString("client_secret","APP_PASSWORD")
loo_Json.UpdateString("resource","https://vault.azure.net")
loo_Json.UpdateString("token_endpoint","https://login.microsoftonline.com/TENANT_ID/oauth2/token")
// Note: This example is using a relative file path. You can also specify a full file path, such as "C:/someDir/myCertAndKey.pfx"
// or a file path the makes sense on non-Windows operating systems..
ls_PfxFilePath = "qa_data/pfx/myCertAndKey.pfx"
// Load the PFX file to be imported to the Azure Key Vault.
loo_BdPfx = create oleobject
li_rc = loo_BdPfx.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_BdPfx.LoadFile(ls_PfxFilePath)
if li_Success = 0 then
Write-Debug "Failed to load the PFX file."
destroy loo_Json
destroy loo_BdPfx
return
end if
// We'll be sending a POST request like this:
// POST https://myvault.vault.azure.net//certificates/importCert01/import?api-version=7.4
//
// {
// "value": "MIIJ...",
// "pwd": "123",
// "policy": {
// "key_props": {
// "exportable": true,
// "kty": "RSA",
// "key_size": 2048,
// "reuse_key": false
// },
// "secret_props": {
// "contentType": "application/x-pkcs12"
// }
// }
// }
// Also load the PFX into the Chilkat certificate object so we can get
// information about the key type and size.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadPfxFile(ls_PfxFilePath,"pfx_password")
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Json
destroy loo_BdPfx
destroy loo_Cert
return
end if
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_Cert.GetPrivateKey(loo_PrivKey)
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Json
destroy loo_BdPfx
destroy loo_Cert
destroy loo_PrivKey
return
end if
// Get the private key as a JWK so we can get information about it..
loo_Jwk = create oleobject
li_rc = loo_Jwk.ConnectToNewObject("Chilkat.JsonObject")
loo_Jwk.Load(loo_PrivKey.GetJwk())
// Get the key type
loo_SbKty = create oleobject
li_rc = loo_SbKty.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbKty.Append(loo_Jwk.StringOf("kty"))
// If this is an EC key, get the curve name
loo_SbCurve = create oleobject
li_rc = loo_SbCurve.ConnectToNewObject("Chilkat.StringBuilder")
if loo_Jwk.HasMember("crv") = 1 then
loo_SbCurve.Append(loo_Jwk.StringOf("crv"))
end if
// Build the JSON that will be the body of the HTTP POST.
loo_JsonBody = create oleobject
li_rc = loo_JsonBody.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonBody.UpdateString("value",loo_BdPfx.GetEncoded("base64"))
loo_JsonBody.UpdateString("pwd","pfx_password")
loo_JsonBody.UpdateBool("policy.key_props.exportable",1)
loo_JsonBody.UpdateString("policy.key_props.kty",loo_SbKty.GetAsString())
if loo_SbKty.ContentsEqual("RSA",0) = 1 then
loo_JsonBody.UpdateInt("policy.key_props.key_size",loo_PrivKey.BitLength)
end if
if loo_SbKty.ContentsEqual("EC",0) = 1 then
loo_JsonBody.UpdateString("policy.key_props.crv",loo_SbCurve.GetAsString())
end if
loo_JsonBody.UpdateBool("policy.key_props.reuse_key",0)
loo_JsonBody.UpdateString("policy.secret_props.contentType","application/x-pkcs12")
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
// Instead of providing an actual access token, we give Chilkat the information that allows it to
// automatically fetch the access token using the OAuth2 client credentials flow.
loo_Http.AuthToken = loo_Json.Emit()
// Choose anything to be the name of your imported certificate.
loo_Http.SetUrlVar("certificateName","importCert01")
// Note: Replace "VAULT_NAME" with the name of your Azure key vault.
ls_Url = "https://VAULT_NAME.vault.azure.net/certificates/{$certificateName}/import?api-version=7.4"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpJson("POST",ls_Url,loo_JsonBody,"application/json",loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Json
destroy loo_BdPfx
destroy loo_Cert
destroy loo_PrivKey
destroy loo_Jwk
destroy loo_SbKty
destroy loo_SbCurve
destroy loo_JsonBody
destroy loo_Http
destroy loo_Resp
return
end if
li_StatusCode = loo_Resp.StatusCode
loo_JsonResp = create oleobject
li_rc = loo_JsonResp.ConnectToNewObject("Chilkat.JsonObject")
loo_Resp.GetBodyJson(loo_JsonResp)
loo_JsonResp.EmitCompact = 0
Write-Debug loo_JsonResp.Emit()
if li_StatusCode <> 200 then
Write-Debug "Failed."
destroy loo_Json
destroy loo_BdPfx
destroy loo_Cert
destroy loo_PrivKey
destroy loo_Jwk
destroy loo_SbKty
destroy loo_SbCurve
destroy loo_JsonBody
destroy loo_Http
destroy loo_Resp
destroy loo_JsonResp
return
end if
// A successful JSON response looks like this:
// {
// "id": "https://kvchilkat.vault.azure.net/certificates/importCert01/7140c8755ed14839b5d86a9f7e7f0497",
// "kid": "https://kvchilkat.vault.azure.net/keys/importCert01/7140c8755ed14839b5d86a9f7e7f0497",
// "sid": "https://kvchilkat.vault.azure.net/secrets/importCert01/7140c8755ed14839b5d86a9f7e7f0497",
// "x5t": "I_e3776K5Q_6PN1HHvJoI2ZGQRQ",
// "cer": "MIIG ... jTsi7yIY=",
// "attributes": {
// "enabled": true,
// "nbf": 1633996800,
// "exp": 1728691199,
// "created": 1697411128,
// "updated": 1697411128,
// "recoveryLevel": "CustomizedRecoverable+Purgeable",
// "recoverableDays": 7
// },
// "policy": {
// "id": "https://kvchilkat.vault.azure.net/certificates/importCert01/policy",
// "key_props": {
// "exportable": true,
// "kty": "RSA",
// "key_size": 4096,
// "reuse_key": false
// },
// "secret_props": {
// "contentType": "application/x-pkcs12"
// },
// "x509_props": {
// "subject": "CN=\"Chilkat Software, Inc.\", O=\"Chilkat Software, Inc.\", S=Illinois, C=US",
// "ekus": [
// "1.3.6.1.5.5.7.3.3"
// ],
// "key_usage": [
// "digitalSignature"
// ],
// "validity_months": 37,
// "basic_constraints": {
// "ca": false
// }
// },
// "lifetime_actions": [
// {
// "trigger": {
// "lifetime_percentage": 80
// },
// "action": {
// "action_type": "EmailContacts"
// }
// }
// ],
// "issuer": {
// "name": "Unknown"
// },
// "attributes": {
// "enabled": true,
// "created": 1697411128,
// "updated": 1697411128
// }
// }
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
ls_Id = loo_JsonResp.StringOf("id")
ls_Kid = loo_JsonResp.StringOf("kid")
ls_Sid = loo_JsonResp.StringOf("sid")
ls_X5t = loo_JsonResp.StringOf("x5t")
ls_Cer = loo_JsonResp.StringOf("cer")
li_Enabled = loo_JsonResp.BoolOf("attributes.enabled")
li_Nbf = loo_JsonResp.IntOf("attributes.nbf")
li_Exp = loo_JsonResp.IntOf("attributes.exp")
li_Created = loo_JsonResp.IntOf("attributes.created")
li_Updated = loo_JsonResp.IntOf("attributes.updated")
ls_RecoveryLevel = loo_JsonResp.StringOf("attributes.recoveryLevel")
li_RecoverableDays = loo_JsonResp.IntOf("attributes.recoverableDays")
ls_Id = loo_JsonResp.StringOf("policy.id")
li_Exportable = loo_JsonResp.BoolOf("policy.key_props.exportable")
ls_Kty = loo_JsonResp.StringOf("policy.key_props.kty")
li_Key_size = loo_JsonResp.IntOf("policy.key_props.key_size")
li_Reuse_key = loo_JsonResp.BoolOf("policy.key_props.reuse_key")
ls_ContentType = loo_JsonResp.StringOf("policy.secret_props.contentType")
ls_Subject = loo_JsonResp.StringOf("policy.x509_props.subject")
li_Validity_months = loo_JsonResp.IntOf("policy.x509_props.validity_months")
li_Ca = loo_JsonResp.BoolOf("policy.x509_props.basic_constraints.ca")
ls_Name = loo_JsonResp.StringOf("policy.issuer.name")
li_AttributesEnabled = loo_JsonResp.BoolOf("policy.attributes.enabled")
li_AttributesCreated = loo_JsonResp.IntOf("policy.attributes.created")
li_AttributesUpdated = loo_JsonResp.IntOf("policy.attributes.updated")
i = 0
li_Count_i = loo_JsonResp.SizeOfArray("policy.x509_props.ekus")
do while i < li_Count_i
loo_JsonResp.I = i
ls_StrVal = loo_JsonResp.StringOf("policy.x509_props.ekus[i]")
i = i + 1
loop
i = 0
li_Count_i = loo_JsonResp.SizeOfArray("policy.x509_props.key_usage")
do while i < li_Count_i
loo_JsonResp.I = i
ls_StrVal = loo_JsonResp.StringOf("policy.x509_props.key_usage[i]")
i = i + 1
loop
i = 0
li_Count_i = loo_JsonResp.SizeOfArray("policy.lifetime_actions")
do while i < li_Count_i
loo_JsonResp.I = i
li_Lifetime_percentage = loo_JsonResp.IntOf("policy.lifetime_actions[i].trigger.lifetime_percentage")
ls_Action_type = loo_JsonResp.StringOf("policy.lifetime_actions[i].action.action_type")
i = i + 1
loop
destroy loo_Json
destroy loo_BdPfx
destroy loo_Cert
destroy loo_PrivKey
destroy loo_Jwk
destroy loo_SbKty
destroy loo_SbCurve
destroy loo_JsonBody
destroy loo_Http
destroy loo_Resp
destroy loo_JsonResp