Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, HttpResponse, StringBuilder, JsonObject, PrivateKey, Cert, Http, BinData;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
json: HCkJsonObject;
pfxFilePath: PWideChar;
bdPfx: HCkBinData;
cert: HCkCert;
privKey: HCkPrivateKey;
jwk: HCkJsonObject;
sbKty: HCkStringBuilder;
sbCurve: HCkStringBuilder;
jsonBody: HCkJsonObject;
http: HCkHttp;
url: PWideChar;
resp: HCkHttpResponse;
statusCode: Integer;
jsonResp: HCkJsonObject;
strVal: PWideChar;
Lifetime_percentage: Integer;
Action_type: PWideChar;
id: PWideChar;
kid: PWideChar;
sid: PWideChar;
x5t: PWideChar;
cer: PWideChar;
Enabled: Boolean;
Nbf: Integer;
Exp: Integer;
Created: Integer;
Updated: Integer;
RecoveryLevel: PWideChar;
RecoverableDays: Integer;
Id: PWideChar;
Exportable: Boolean;
Kty: PWideChar;
Key_size: Integer;
Reuse_key: Boolean;
ContentType: PWideChar;
Subject: PWideChar;
Validity_months: Integer;
Ca: Boolean;
Name: PWideChar;
AttributesEnabled: Boolean;
AttributesCreated: Integer;
AttributesUpdated: Integer;
i: Integer;
count_i: Integer;
begin
success := False;
// 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.
json := CkJsonObject_Create();
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..
pfxFilePath := 'qa_data/pfx/myCertAndKey.pfx';
// Load the PFX file to be imported to the Azure Key Vault.
bdPfx := CkBinData_Create();
success := CkBinData_LoadFile(bdPfx,pfxFilePath);
if (success = False) then
begin
Memo1.Lines.Add('Failed to load the PFX file.');
Exit;
end;
// 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.
cert := CkCert_Create();
success := CkCert_LoadPfxFile(cert,pfxFilePath,'pfx_password');
if (success = False) then
begin
Memo1.Lines.Add(CkCert__lastErrorText(cert));
Exit;
end;
privKey := CkPrivateKey_Create();
success := CkCert_GetPrivateKey(cert,privKey);
if (success = False) then
begin
Memo1.Lines.Add(CkCert__lastErrorText(cert));
Exit;
end;
// Get the private key as a JWK so we can get information about it..
jwk := CkJsonObject_Create();
CkJsonObject_Load(jwk,CkPrivateKey__getJwk(privKey));
// Get the key type
sbKty := CkStringBuilder_Create();
CkStringBuilder_Append(sbKty,CkJsonObject__stringOf(jwk,'kty'));
// If this is an EC key, get the curve name
sbCurve := CkStringBuilder_Create();
if (CkJsonObject_HasMember(jwk,'crv') = True) then
begin
CkStringBuilder_Append(sbCurve,CkJsonObject__stringOf(jwk,'crv'));
end;
// Build the JSON that will be the body of the HTTP POST.
jsonBody := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonBody,'value',CkBinData__getEncoded(bdPfx,'base64'));
CkJsonObject_UpdateString(jsonBody,'pwd','pfx_password');
CkJsonObject_UpdateBool(jsonBody,'policy.key_props.exportable',True);
CkJsonObject_UpdateString(jsonBody,'policy.key_props.kty',CkStringBuilder__getAsString(sbKty));
if (CkStringBuilder_ContentsEqual(sbKty,'RSA',False) = True) then
begin
CkJsonObject_UpdateInt(jsonBody,'policy.key_props.key_size',CkPrivateKey_getBitLength(privKey));
end;
if (CkStringBuilder_ContentsEqual(sbKty,'EC',False) = True) then
begin
CkJsonObject_UpdateString(jsonBody,'policy.key_props.crv',CkStringBuilder__getAsString(sbCurve));
end;
CkJsonObject_UpdateBool(jsonBody,'policy.key_props.reuse_key',False);
CkJsonObject_UpdateString(jsonBody,'policy.secret_props.contentType','application/x-pkcs12');
http := CkHttp_Create();
// 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_putAuthToken(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.
url := 'https://VAULT_NAME.vault.azure.net/certificates/{$certificateName}/import?api-version=7.4';
resp := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST',url,jsonBody,'application/json',resp);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
statusCode := CkHttpResponse_getStatusCode(resp);
jsonResp := CkJsonObject_Create();
CkHttpResponse_GetBodyJson(resp,jsonResp);
CkJsonObject_putEmitCompact(jsonResp,False);
Memo1.Lines.Add(CkJsonObject__emit(jsonResp));
if (statusCode <> 200) then
begin
Memo1.Lines.Add('Failed.');
Exit;
end;
// 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
id := CkJsonObject__stringOf(jsonResp,'id');
kid := CkJsonObject__stringOf(jsonResp,'kid');
sid := CkJsonObject__stringOf(jsonResp,'sid');
x5t := CkJsonObject__stringOf(jsonResp,'x5t');
cer := CkJsonObject__stringOf(jsonResp,'cer');
Enabled := CkJsonObject_BoolOf(jsonResp,'attributes.enabled');
Nbf := CkJsonObject_IntOf(jsonResp,'attributes.nbf');
Exp := CkJsonObject_IntOf(jsonResp,'attributes.exp');
Created := CkJsonObject_IntOf(jsonResp,'attributes.created');
Updated := CkJsonObject_IntOf(jsonResp,'attributes.updated');
RecoveryLevel := CkJsonObject__stringOf(jsonResp,'attributes.recoveryLevel');
RecoverableDays := CkJsonObject_IntOf(jsonResp,'attributes.recoverableDays');
Id := CkJsonObject__stringOf(jsonResp,'policy.id');
Exportable := CkJsonObject_BoolOf(jsonResp,'policy.key_props.exportable');
Kty := CkJsonObject__stringOf(jsonResp,'policy.key_props.kty');
Key_size := CkJsonObject_IntOf(jsonResp,'policy.key_props.key_size');
Reuse_key := CkJsonObject_BoolOf(jsonResp,'policy.key_props.reuse_key');
ContentType := CkJsonObject__stringOf(jsonResp,'policy.secret_props.contentType');
Subject := CkJsonObject__stringOf(jsonResp,'policy.x509_props.subject');
Validity_months := CkJsonObject_IntOf(jsonResp,'policy.x509_props.validity_months');
Ca := CkJsonObject_BoolOf(jsonResp,'policy.x509_props.basic_constraints.ca');
Name := CkJsonObject__stringOf(jsonResp,'policy.issuer.name');
AttributesEnabled := CkJsonObject_BoolOf(jsonResp,'policy.attributes.enabled');
AttributesCreated := CkJsonObject_IntOf(jsonResp,'policy.attributes.created');
AttributesUpdated := CkJsonObject_IntOf(jsonResp,'policy.attributes.updated');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResp,'policy.x509_props.ekus');
while i < count_i do
begin
CkJsonObject_putI(jsonResp,i);
strVal := CkJsonObject__stringOf(jsonResp,'policy.x509_props.ekus[i]');
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResp,'policy.x509_props.key_usage');
while i < count_i do
begin
CkJsonObject_putI(jsonResp,i);
strVal := CkJsonObject__stringOf(jsonResp,'policy.x509_props.key_usage[i]');
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResp,'policy.lifetime_actions');
while i < count_i do
begin
CkJsonObject_putI(jsonResp,i);
Lifetime_percentage := CkJsonObject_IntOf(jsonResp,'policy.lifetime_actions[i].trigger.lifetime_percentage');
Action_type := CkJsonObject__stringOf(jsonResp,'policy.lifetime_actions[i].action.action_type');
i := i + 1;
end;
CkJsonObject_Dispose(json);
CkBinData_Dispose(bdPfx);
CkCert_Dispose(cert);
CkPrivateKey_Dispose(privKey);
CkJsonObject_Dispose(jwk);
CkStringBuilder_Dispose(sbKty);
CkStringBuilder_Dispose(sbCurve);
CkJsonObject_Dispose(jsonBody);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(jsonResp);
end;