Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set 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.
set json [new_CkJsonObject]
CkJsonObject_UpdateString $json "client_id" "APP_ID"
CkJsonObject_UpdateString $json "client_secret" "APP_PASSWORD"
CkJsonObject_UpdateString $json "resource" "https://vault.azure.net"
CkJsonObject_UpdateString $json "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..
set pfxFilePath "qa_data/pfx/myCertAndKey.pfx"
# Load the PFX file to be imported to the Azure Key Vault.
set bdPfx [new_CkBinData]
set success [CkBinData_LoadFile $bdPfx $pfxFilePath]
if {$success == 0} then {
puts "Failed to load the PFX file."
delete_CkJsonObject $json
delete_CkBinData $bdPfx
exit
}
# 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.
set cert [new_CkCert]
set success [CkCert_LoadPfxFile $cert $pfxFilePath "pfx_password"]
if {$success == 0} then {
puts [CkCert_lastErrorText $cert]
delete_CkJsonObject $json
delete_CkBinData $bdPfx
delete_CkCert $cert
exit
}
set privKey [new_CkPrivateKey]
set success [CkCert_GetPrivateKey $cert $privKey]
if {$success == 0} then {
puts [CkCert_lastErrorText $cert]
delete_CkJsonObject $json
delete_CkBinData $bdPfx
delete_CkCert $cert
delete_CkPrivateKey $privKey
exit
}
# Get the private key as a JWK so we can get information about it..
set jwk [new_CkJsonObject]
CkJsonObject_Load $jwk [CkPrivateKey_getJwk $privKey]
# Get the key type
set sbKty [new_CkStringBuilder]
CkStringBuilder_Append $sbKty [CkJsonObject_stringOf $jwk "kty"]
# If this is an EC key, get the curve name
set sbCurve [new_CkStringBuilder]
if {[CkJsonObject_HasMember $jwk "crv"] == 1} then {
CkStringBuilder_Append $sbCurve [CkJsonObject_stringOf $jwk "crv"]
}
# Build the JSON that will be the body of the HTTP POST.
set jsonBody [new_CkJsonObject]
CkJsonObject_UpdateString $jsonBody "value" [CkBinData_getEncoded $bdPfx "base64"]
CkJsonObject_UpdateString $jsonBody "pwd" "pfx_password"
CkJsonObject_UpdateBool $jsonBody "policy.key_props.exportable" 1
CkJsonObject_UpdateString $jsonBody "policy.key_props.kty" [CkStringBuilder_getAsString $sbKty]
if {[CkStringBuilder_ContentsEqual $sbKty "RSA" 0] == 1} then {
CkJsonObject_UpdateInt $jsonBody "policy.key_props.key_size" [CkPrivateKey_get_BitLength $privKey]
}
if {[CkStringBuilder_ContentsEqual $sbKty "EC" 0] == 1} then {
CkJsonObject_UpdateString $jsonBody "policy.key_props.crv" [CkStringBuilder_getAsString $sbCurve]
}
CkJsonObject_UpdateBool $jsonBody "policy.key_props.reuse_key" 0
CkJsonObject_UpdateString $jsonBody "policy.secret_props.contentType" "application/x-pkcs12"
set http [new_CkHttp]
# 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.
CkHttp_put_AuthToken $http [CkJsonObject_emit $json]
# Choose anything to be the name of your imported certificate.
CkHttp_SetUrlVar $http "certificateName" "importCert01"
# Note: Replace "VAULT_NAME" with the name of your Azure key vault.
set url "https://VAULT_NAME.vault.azure.net/certificates/{$certificateName}/import?api-version=7.4"
set resp [new_CkHttpResponse]
set success [CkHttp_HttpJson $http "POST" $url $jsonBody "application/json" $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkJsonObject $json
delete_CkBinData $bdPfx
delete_CkCert $cert
delete_CkPrivateKey $privKey
delete_CkJsonObject $jwk
delete_CkStringBuilder $sbKty
delete_CkStringBuilder $sbCurve
delete_CkJsonObject $jsonBody
delete_CkHttp $http
delete_CkHttpResponse $resp
exit
}
set statusCode [CkHttpResponse_get_StatusCode $resp]
set jsonResp [new_CkJsonObject]
CkHttpResponse_GetBodyJson $resp $jsonResp
CkJsonObject_put_EmitCompact $jsonResp 0
puts [CkJsonObject_emit $jsonResp]
if {$statusCode != 200} then {
puts "Failed."
delete_CkJsonObject $json
delete_CkBinData $bdPfx
delete_CkCert $cert
delete_CkPrivateKey $privKey
delete_CkJsonObject $jwk
delete_CkStringBuilder $sbKty
delete_CkStringBuilder $sbCurve
delete_CkJsonObject $jsonBody
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $jsonResp
exit
}
# 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
set id [CkJsonObject_stringOf $jsonResp "id"]
set kid [CkJsonObject_stringOf $jsonResp "kid"]
set sid [CkJsonObject_stringOf $jsonResp "sid"]
set x5t [CkJsonObject_stringOf $jsonResp "x5t"]
set cer [CkJsonObject_stringOf $jsonResp "cer"]
set Enabled [CkJsonObject_BoolOf $jsonResp "attributes.enabled"]
set Nbf [CkJsonObject_IntOf $jsonResp "attributes.nbf"]
set Exp [CkJsonObject_IntOf $jsonResp "attributes.exp"]
set Created [CkJsonObject_IntOf $jsonResp "attributes.created"]
set Updated [CkJsonObject_IntOf $jsonResp "attributes.updated"]
set RecoveryLevel [CkJsonObject_stringOf $jsonResp "attributes.recoveryLevel"]
set RecoverableDays [CkJsonObject_IntOf $jsonResp "attributes.recoverableDays"]
set Id [CkJsonObject_stringOf $jsonResp "policy.id"]
set Exportable [CkJsonObject_BoolOf $jsonResp "policy.key_props.exportable"]
set Kty [CkJsonObject_stringOf $jsonResp "policy.key_props.kty"]
set Key_size [CkJsonObject_IntOf $jsonResp "policy.key_props.key_size"]
set Reuse_key [CkJsonObject_BoolOf $jsonResp "policy.key_props.reuse_key"]
set ContentType [CkJsonObject_stringOf $jsonResp "policy.secret_props.contentType"]
set Subject [CkJsonObject_stringOf $jsonResp "policy.x509_props.subject"]
set Validity_months [CkJsonObject_IntOf $jsonResp "policy.x509_props.validity_months"]
set Ca [CkJsonObject_BoolOf $jsonResp "policy.x509_props.basic_constraints.ca"]
set Name [CkJsonObject_stringOf $jsonResp "policy.issuer.name"]
set AttributesEnabled [CkJsonObject_BoolOf $jsonResp "policy.attributes.enabled"]
set AttributesCreated [CkJsonObject_IntOf $jsonResp "policy.attributes.created"]
set AttributesUpdated [CkJsonObject_IntOf $jsonResp "policy.attributes.updated"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonResp "policy.x509_props.ekus"]
while {$i < $count_i} {
CkJsonObject_put_I $jsonResp $i
set strVal [CkJsonObject_stringOf $jsonResp "policy.x509_props.ekus[i]"]
set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonResp "policy.x509_props.key_usage"]
while {$i < $count_i} {
CkJsonObject_put_I $jsonResp $i
set strVal [CkJsonObject_stringOf $jsonResp "policy.x509_props.key_usage[i]"]
set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonResp "policy.lifetime_actions"]
while {$i < $count_i} {
CkJsonObject_put_I $jsonResp $i
set Lifetime_percentage [CkJsonObject_IntOf $jsonResp "policy.lifetime_actions[i].trigger.lifetime_percentage"]
set Action_type [CkJsonObject_stringOf $jsonResp "policy.lifetime_actions[i].action.action_type"]
set i [expr $i + 1]
}
delete_CkJsonObject $json
delete_CkBinData $bdPfx
delete_CkCert $cert
delete_CkPrivateKey $privKey
delete_CkJsonObject $jwk
delete_CkStringBuilder $sbKty
delete_CkStringBuilder $sbCurve
delete_CkJsonObject $jsonBody
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $jsonResp