Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Azure Key Vault Import CertificateSee more Azure Key Vault ExamplesImports 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. Note: This example requires Chilkat v9.5.0.96 or later. For more information, see https://learn.microsoft.com/en-us/rest/api/keyvault/certificates/import-certificate/import-certificate?tabs=HTTP
#include <C_CkJsonObjectW.h> #include <C_CkBinDataW.h> #include <C_CkCertW.h> #include <C_CkPrivateKeyW.h> #include <C_CkStringBuilderW.h> #include <C_CkHttpW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkJsonObjectW json; const wchar_t *pfxFilePath; HCkBinDataW bdPfx; BOOL success; HCkCertW cert; HCkPrivateKeyW privKey; HCkJsonObjectW jwk; HCkStringBuilderW sbKty; HCkStringBuilderW sbCurve; HCkJsonObjectW jsonBody; HCkHttpW http; const wchar_t *url; HCkHttpResponseW resp; int statusCode; HCkJsonObjectW jsonResp; const wchar_t *strVal; int Lifetime_percentage; const wchar_t *Action_type; const wchar_t *id; const wchar_t *kid; const wchar_t *sid; const wchar_t *x5t; const wchar_t *cer; BOOL Enabled; int Nbf; int Exp; int Created; int Updated; const wchar_t *RecoveryLevel; int RecoverableDays; const wchar_t *Id; BOOL Exportable; const wchar_t *Kty; int Key_size; BOOL Reuse_key; const wchar_t *ContentType; const wchar_t *Subject; int Validity_months; BOOL Ca; const wchar_t *Name; BOOL AttributesEnabled; int AttributesCreated; int AttributesUpdated; int i; int count_i; // 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 = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"client_id",L"APP_ID"); CkJsonObjectW_UpdateString(json,L"client_secret",L"APP_PASSWORD"); CkJsonObjectW_UpdateString(json,L"resource",L"https://vault.azure.net"); CkJsonObjectW_UpdateString(json,L"token_endpoint",L"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 = L"qa_data/pfx/myCertAndKey.pfx"; // Load the PFX file to be imported to the Azure Key Vault. bdPfx = CkBinDataW_Create(); success = CkBinDataW_LoadFile(bdPfx,pfxFilePath); if (success == FALSE) { wprintf(L"Failed to load the PFX file.\n"); CkJsonObjectW_Dispose(json); CkBinDataW_Dispose(bdPfx); 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. cert = CkCertW_Create(); success = CkCertW_LoadPfxFile(cert,pfxFilePath,L"pfx_password"); if (success == FALSE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkJsonObjectW_Dispose(json); CkBinDataW_Dispose(bdPfx); CkCertW_Dispose(cert); return; } privKey = CkCertW_ExportPrivateKey(cert); if (CkCertW_getLastMethodSuccess(cert) == FALSE) { wprintf(L"The certificate does not have a private key.\n"); CkJsonObjectW_Dispose(json); CkBinDataW_Dispose(bdPfx); CkCertW_Dispose(cert); return; } // Get the private key as a JWK so we can get information about it.. jwk = CkJsonObjectW_Create(); CkJsonObjectW_Load(jwk,CkPrivateKeyW_getJwk(privKey)); // Get the key type sbKty = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbKty,CkJsonObjectW_stringOf(jwk,L"kty")); // If this is an EC key, get the curve name sbCurve = CkStringBuilderW_Create(); if (CkJsonObjectW_HasMember(jwk,L"crv") == TRUE) { CkStringBuilderW_Append(sbCurve,CkJsonObjectW_stringOf(jwk,L"crv")); } // Build the JSON that will be the body of the HTTP POST. jsonBody = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jsonBody,L"value",CkBinDataW_getEncoded(bdPfx,L"base64")); CkJsonObjectW_UpdateString(jsonBody,L"pwd",L"pfx_password"); CkJsonObjectW_UpdateBool(jsonBody,L"policy.key_props.exportable",TRUE); CkJsonObjectW_UpdateString(jsonBody,L"policy.key_props.kty",CkStringBuilderW_getAsString(sbKty)); if (CkStringBuilderW_ContentsEqual(sbKty,L"RSA",FALSE) == TRUE) { CkJsonObjectW_UpdateInt(jsonBody,L"policy.key_props.key_size",CkPrivateKeyW_getBitLength(privKey)); } if (CkStringBuilderW_ContentsEqual(sbKty,L"EC",FALSE) == TRUE) { CkJsonObjectW_UpdateString(jsonBody,L"policy.key_props.crv",CkStringBuilderW_getAsString(sbCurve)); } CkJsonObjectW_UpdateBool(jsonBody,L"policy.key_props.reuse_key",FALSE); CkJsonObjectW_UpdateString(jsonBody,L"policy.secret_props.contentType",L"application/x-pkcs12"); CkPrivateKeyW_Dispose(privKey); http = CkHttpW_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. CkHttpW_putAuthToken(http,CkJsonObjectW_emit(json)); // Choose anything to be the name of your imported certificate. CkHttpW_SetUrlVar(http,L"certificateName",L"importCert01"); // Note: Replace "VAULT_NAME" with the name of your Azure key vault. url = L"https://VAULT_NAME.vault.azure.net/certificates/{$certificateName}/import?api-version=7.4"; resp = CkHttpW_PostJson3(http,url,L"application/json",jsonBody); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { // This means something failed before we got a response. wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(json); CkBinDataW_Dispose(bdPfx); CkCertW_Dispose(cert); CkJsonObjectW_Dispose(jwk); CkStringBuilderW_Dispose(sbKty); CkStringBuilderW_Dispose(sbCurve); CkJsonObjectW_Dispose(jsonBody); CkHttpW_Dispose(http); return; } statusCode = CkHttpResponseW_getStatusCode(resp); jsonResp = CkJsonObjectW_Create(); CkHttpResponseW_GetBodyJson(resp,jsonResp); CkHttpResponseW_Dispose(resp); CkJsonObjectW_putEmitCompact(jsonResp,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(jsonResp)); if (statusCode != 200) { wprintf(L"Failed.\n"); CkJsonObjectW_Dispose(json); CkBinDataW_Dispose(bdPfx); CkCertW_Dispose(cert); CkJsonObjectW_Dispose(jwk); CkStringBuilderW_Dispose(sbKty); CkStringBuilderW_Dispose(sbCurve); CkJsonObjectW_Dispose(jsonBody); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonResp); 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 id = CkJsonObjectW_stringOf(jsonResp,L"id"); kid = CkJsonObjectW_stringOf(jsonResp,L"kid"); sid = CkJsonObjectW_stringOf(jsonResp,L"sid"); x5t = CkJsonObjectW_stringOf(jsonResp,L"x5t"); cer = CkJsonObjectW_stringOf(jsonResp,L"cer"); Enabled = CkJsonObjectW_BoolOf(jsonResp,L"attributes.enabled"); Nbf = CkJsonObjectW_IntOf(jsonResp,L"attributes.nbf"); Exp = CkJsonObjectW_IntOf(jsonResp,L"attributes.exp"); Created = CkJsonObjectW_IntOf(jsonResp,L"attributes.created"); Updated = CkJsonObjectW_IntOf(jsonResp,L"attributes.updated"); RecoveryLevel = CkJsonObjectW_stringOf(jsonResp,L"attributes.recoveryLevel"); RecoverableDays = CkJsonObjectW_IntOf(jsonResp,L"attributes.recoverableDays"); Id = CkJsonObjectW_stringOf(jsonResp,L"policy.id"); Exportable = CkJsonObjectW_BoolOf(jsonResp,L"policy.key_props.exportable"); Kty = CkJsonObjectW_stringOf(jsonResp,L"policy.key_props.kty"); Key_size = CkJsonObjectW_IntOf(jsonResp,L"policy.key_props.key_size"); Reuse_key = CkJsonObjectW_BoolOf(jsonResp,L"policy.key_props.reuse_key"); ContentType = CkJsonObjectW_stringOf(jsonResp,L"policy.secret_props.contentType"); Subject = CkJsonObjectW_stringOf(jsonResp,L"policy.x509_props.subject"); Validity_months = CkJsonObjectW_IntOf(jsonResp,L"policy.x509_props.validity_months"); Ca = CkJsonObjectW_BoolOf(jsonResp,L"policy.x509_props.basic_constraints.ca"); Name = CkJsonObjectW_stringOf(jsonResp,L"policy.issuer.name"); AttributesEnabled = CkJsonObjectW_BoolOf(jsonResp,L"policy.attributes.enabled"); AttributesCreated = CkJsonObjectW_IntOf(jsonResp,L"policy.attributes.created"); AttributesUpdated = CkJsonObjectW_IntOf(jsonResp,L"policy.attributes.updated"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jsonResp,L"policy.x509_props.ekus"); while (i < count_i) { CkJsonObjectW_putI(jsonResp,i); strVal = CkJsonObjectW_stringOf(jsonResp,L"policy.x509_props.ekus[i]"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jsonResp,L"policy.x509_props.key_usage"); while (i < count_i) { CkJsonObjectW_putI(jsonResp,i); strVal = CkJsonObjectW_stringOf(jsonResp,L"policy.x509_props.key_usage[i]"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jsonResp,L"policy.lifetime_actions"); while (i < count_i) { CkJsonObjectW_putI(jsonResp,i); Lifetime_percentage = CkJsonObjectW_IntOf(jsonResp,L"policy.lifetime_actions[i].trigger.lifetime_percentage"); Action_type = CkJsonObjectW_stringOf(jsonResp,L"policy.lifetime_actions[i].action.action_type"); i = i + 1; } CkJsonObjectW_Dispose(json); CkBinDataW_Dispose(bdPfx); CkCertW_Dispose(cert); CkJsonObjectW_Dispose(jwk); CkStringBuilderW_Dispose(sbKty); CkStringBuilderW_Dispose(sbCurve); CkJsonObjectW_Dispose(jsonBody); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.