Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// 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.
let json = chilkat::JsonObject::new();
let _ = json.update_string("client_id", "APP_ID");
let _ = json.update_string("client_secret", "APP_PASSWORD");
let _ = json.update_string("resource", "https://vault.azure.net");
let _ = json.update_string("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..
let pfx_file_path = "qa_data/pfx/myCertAndKey.pfx".to_string();
// Load the PFX file to be imported to the Azure Key Vault.
let bd_pfx = chilkat::BinData::new();
if bd_pfx.load_file(&pfx_file_path).is_err() {
println!("Failed to load the PFX file.");
return;
}
// 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.
let cert = chilkat::Cert::new();
if cert.load_pfx_file(&pfx_file_path, "pfx_password").is_err() {
println!("{}", cert.last_error_text());
return;
}
let priv_key = chilkat::PrivateKey::new();
if cert.get_private_key(&priv_key).is_err() {
println!("{}", cert.last_error_text());
return;
}
// Get the private key as a JWK so we can get information about it..
let jwk = chilkat::JsonObject::new();
let _ = jwk.load(&priv_key.get_jwk().unwrap_or_default());
// Get the key type
let sb_kty = chilkat::StringBuilder::new();
let _ = sb_kty.append(&jwk.string_of("kty").unwrap_or_default());
// If this is an EC key, get the curve name
let sb_curve = chilkat::StringBuilder::new();
if jwk.has_member("crv") {
let _ = sb_curve.append(&jwk.string_of("crv").unwrap_or_default());
}
// Build the JSON that will be the body of the HTTP POST.
let json_body = chilkat::JsonObject::new();
let _ = json_body.update_string("value", &bd_pfx.get_encoded("base64").unwrap_or_default());
let _ = json_body.update_string("pwd", "pfx_password");
let _ = json_body.update_bool("policy.key_props.exportable", true);
let _ = json_body.update_string("policy.key_props.kty", &sb_kty.get_as_string().unwrap_or_default());
if sb_kty.contents_equal("RSA", false) {
let _ = json_body.update_int("policy.key_props.key_size", priv_key.bit_length());
}
if sb_kty.contents_equal("EC", false) {
let _ = json_body.update_string("policy.key_props.crv", &sb_curve.get_as_string().unwrap_or_default());
}
let _ = json_body.update_bool("policy.key_props.reuse_key", false);
let _ = json_body.update_string("policy.secret_props.contentType", "application/x-pkcs12");
let http = chilkat::Http::new();
// 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.
http.set_auth_token(&json.emit().unwrap_or_default());
// Choose anything to be the name of your imported certificate.
let _ = http.set_url_var("certificateName", "importCert01");
// Note: Replace "VAULT_NAME" with the name of your Azure key vault.
let url = "https://VAULT_NAME.vault.azure.net/certificates/{$certificateName}/import?api-version=7.4".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", &url, &json_body, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let status_code = resp.status_code();
let json_resp = chilkat::JsonObject::new();
let _ = resp.get_body_json(&json_resp);
json_resp.set_emit_compact(false);
println!("{}", json_resp.emit().unwrap_or_default());
if status_code != 200 {
println!("Failed.");
return;
}
// 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
let id = json_resp.string_of("id").unwrap_or_default();
let kid = json_resp.string_of("kid").unwrap_or_default();
let sid = json_resp.string_of("sid").unwrap_or_default();
let x5t = json_resp.string_of("x5t").unwrap_or_default();
let cer = json_resp.string_of("cer").unwrap_or_default();
let enabled = json_resp.bool_of("attributes.enabled");
let nbf = json_resp.int_of("attributes.nbf");
let exp = json_resp.int_of("attributes.exp");
let created = json_resp.int_of("attributes.created");
let updated = json_resp.int_of("attributes.updated");
let recovery_level = json_resp.string_of("attributes.recoveryLevel").unwrap_or_default();
let recoverable_days = json_resp.int_of("attributes.recoverableDays");
let id = json_resp.string_of("policy.id").unwrap_or_default();
let exportable = json_resp.bool_of("policy.key_props.exportable");
let kty = json_resp.string_of("policy.key_props.kty").unwrap_or_default();
let key_size = json_resp.int_of("policy.key_props.key_size");
let reuse_key = json_resp.bool_of("policy.key_props.reuse_key");
let content_type = json_resp.string_of("policy.secret_props.contentType").unwrap_or_default();
let subject = json_resp.string_of("policy.x509_props.subject").unwrap_or_default();
let validity_months = json_resp.int_of("policy.x509_props.validity_months");
let ca = json_resp.bool_of("policy.x509_props.basic_constraints.ca");
let name = json_resp.string_of("policy.issuer.name").unwrap_or_default();
let attributes_enabled = json_resp.bool_of("policy.attributes.enabled");
let attributes_created = json_resp.int_of("policy.attributes.created");
let attributes_updated = json_resp.int_of("policy.attributes.updated");
let mut i = 0;
let mut count_i = json_resp.size_of_array("policy.x509_props.ekus");
while i < count_i {
json_resp.set_i(i);
let _ = json_resp.string_of("policy.x509_props.ekus[i]").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_resp.size_of_array("policy.x509_props.key_usage");
while i < count_i {
json_resp.set_i(i);
let _ = json_resp.string_of("policy.x509_props.key_usage[i]").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_resp.size_of_array("policy.lifetime_actions");
while i < count_i {
json_resp.set_i(i);
let _ = json_resp.int_of("policy.lifetime_actions[i].trigger.lifetime_percentage");
let _ = json_resp.string_of("policy.lifetime_actions[i].action.action_type").unwrap_or_default();
i = i + 1;
}