Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL cPfxFilePath
LOCAL oBdPfx
LOCAL oCert
LOCAL oPrivKey
LOCAL oJwk
LOCAL oSbKty
LOCAL oSbCurve
LOCAL oJsonBody
LOCAL oHttp
LOCAL cUrl
LOCAL oResp
LOCAL nStatusCode
LOCAL oJsonResp
LOCAL cStrVal
LOCAL nLifetime_percentage
LOCAL cAction_type
LOCAL cId
LOCAL cKid
LOCAL cSid
LOCAL cX5t
LOCAL cCer
LOCAL nEnabled
LOCAL nNbf
LOCAL nExp
LOCAL nCreated
LOCAL nUpdated
LOCAL cRecoveryLevel
LOCAL nRecoverableDays
LOCAL cId
LOCAL nExportable
LOCAL cKty
LOCAL nKey_size
LOCAL nReuse_key
LOCAL cContentType
LOCAL cSubject
LOCAL nValidity_months
LOCAL nCa
LOCAL cName
LOCAL nAttributesEnabled
LOCAL nAttributesCreated
LOCAL nAttributesUpdated
LOCAL i
LOCAL nCount_i
nSuccess := 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.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("client_id", "APP_ID")
oJson:UpdateString("client_secret", "APP_PASSWORD")
oJson:UpdateString("resource", "https://vault.azure.net")
oJson: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..
cPfxFilePath := "qa_data/pfx/myCertAndKey.pfx"
// Load the PFX file to be imported to the Azure Key Vault.
oBdPfx := CreateObject("Chilkat.BinData")
nSuccess := oBdPfx:LoadFile(cPfxFilePath)
IF (nSuccess == 0)
? "Failed to load the PFX file."
oJson:destroy()
oBdPfx:destroy()
RETURN
ENDIF
// 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.
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile(cPfxFilePath, "pfx_password")
IF (nSuccess == 0)
? oCert:LastErrorText
oJson:destroy()
oBdPfx:destroy()
oCert:destroy()
RETURN
ENDIF
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oCert:GetPrivateKey(oPrivKey)
IF (nSuccess == 0)
? oCert:LastErrorText
oJson:destroy()
oBdPfx:destroy()
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// Get the private key as a JWK so we can get information about it..
oJwk := CreateObject("Chilkat.JsonObject")
oJwk:Load(oPrivKey:GetJwk())
// Get the key type
oSbKty := CreateObject("Chilkat.StringBuilder")
oSbKty:Append(oJwk:StringOf("kty"))
// If this is an EC key, get the curve name
oSbCurve := CreateObject("Chilkat.StringBuilder")
IF (oJwk:HasMember("crv") == 1)
oSbCurve:Append(oJwk:StringOf("crv"))
ENDIF
// Build the JSON that will be the body of the HTTP POST.
oJsonBody := CreateObject("Chilkat.JsonObject")
oJsonBody:UpdateString("value", oBdPfx:GetEncoded("base64"))
oJsonBody:UpdateString("pwd", "pfx_password")
oJsonBody:UpdateBool("policy.key_props.exportable", 1)
oJsonBody:UpdateString("policy.key_props.kty", oSbKty:GetAsString())
IF (oSbKty:ContentsEqual("RSA", 0) == 1)
oJsonBody:UpdateInt("policy.key_props.key_size", oPrivKey:BitLength)
ENDIF
IF (oSbKty:ContentsEqual("EC", 0) == 1)
oJsonBody:UpdateString("policy.key_props.crv", oSbCurve:GetAsString())
ENDIF
oJsonBody:UpdateBool("policy.key_props.reuse_key", 0)
oJsonBody:UpdateString("policy.secret_props.contentType", "application/x-pkcs12")
oHttp := CreateObject("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.
oHttp:AuthToken := oJson:Emit()
// Choose anything to be the name of your imported certificate.
oHttp:SetUrlVar("certificateName", "importCert01")
// Note: Replace "VAULT_NAME" with the name of your Azure key vault.
cUrl := "https://VAULT_NAME.vault.azure.net/certificates/{$certificateName}/import?api-version=7.4"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", cUrl, oJsonBody, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oJson:destroy()
oBdPfx:destroy()
oCert:destroy()
oPrivKey:destroy()
oJwk:destroy()
oSbKty:destroy()
oSbCurve:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
nStatusCode := oResp:StatusCode
oJsonResp := CreateObject("Chilkat.JsonObject")
oResp:GetBodyJson(oJsonResp)
oJsonResp:EmitCompact := 0
? oJsonResp:Emit()
IF (nStatusCode != 200)
? "Failed."
oJson:destroy()
oBdPfx:destroy()
oCert:destroy()
oPrivKey:destroy()
oJwk:destroy()
oSbKty:destroy()
oSbCurve:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
oJsonResp:destroy()
RETURN
ENDIF
// 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
cId := oJsonResp:StringOf("id")
cKid := oJsonResp:StringOf("kid")
cSid := oJsonResp:StringOf("sid")
cX5t := oJsonResp:StringOf("x5t")
cCer := oJsonResp:StringOf("cer")
nEnabled := oJsonResp:BoolOf("attributes.enabled")
nNbf := oJsonResp:IntOf("attributes.nbf")
nExp := oJsonResp:IntOf("attributes.exp")
nCreated := oJsonResp:IntOf("attributes.created")
nUpdated := oJsonResp:IntOf("attributes.updated")
cRecoveryLevel := oJsonResp:StringOf("attributes.recoveryLevel")
nRecoverableDays := oJsonResp:IntOf("attributes.recoverableDays")
cId := oJsonResp:StringOf("policy.id")
nExportable := oJsonResp:BoolOf("policy.key_props.exportable")
cKty := oJsonResp:StringOf("policy.key_props.kty")
nKey_size := oJsonResp:IntOf("policy.key_props.key_size")
nReuse_key := oJsonResp:BoolOf("policy.key_props.reuse_key")
cContentType := oJsonResp:StringOf("policy.secret_props.contentType")
cSubject := oJsonResp:StringOf("policy.x509_props.subject")
nValidity_months := oJsonResp:IntOf("policy.x509_props.validity_months")
nCa := oJsonResp:BoolOf("policy.x509_props.basic_constraints.ca")
cName := oJsonResp:StringOf("policy.issuer.name")
nAttributesEnabled := oJsonResp:BoolOf("policy.attributes.enabled")
nAttributesCreated := oJsonResp:IntOf("policy.attributes.created")
nAttributesUpdated := oJsonResp:IntOf("policy.attributes.updated")
i := 0
nCount_i := oJsonResp:SizeOfArray("policy.x509_props.ekus")
DO WHILE i < nCount_i
oJsonResp:I := i
cStrVal := oJsonResp:StringOf("policy.x509_props.ekus[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResp:SizeOfArray("policy.x509_props.key_usage")
DO WHILE i < nCount_i
oJsonResp:I := i
cStrVal := oJsonResp:StringOf("policy.x509_props.key_usage[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResp:SizeOfArray("policy.lifetime_actions")
DO WHILE i < nCount_i
oJsonResp:I := i
nLifetime_percentage := oJsonResp:IntOf("policy.lifetime_actions[i].trigger.lifetime_percentage")
cAction_type := oJsonResp:StringOf("policy.lifetime_actions[i].action.action_type")
i := i + 1
ENDDO
oJson:destroy()
oBdPfx:destroy()
oCert:destroy()
oPrivKey:destroy()
oJwk:destroy()
oSbKty:destroy()
oSbCurve:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
oJsonResp:destroy()