azure-security-keyvault-keys
Loading...
Searching...
No Matches
Azure Key Vault key client library for C++

Azure Key Vault is a cloud service that provides secure storage of keys for encrypting your data. Multiple keys, and multiple versions of the same key, can be kept in the Azure Key Vault. Cryptographic keys in Azure Key Vault are represented as JSON Web Key (JWK) objects.

Azure Key Vault Managed HSM is a fully-managed, highly-available, single-tenant, standards-compliant cloud service that enables you to safeguard cryptographic keys for your cloud applications using FIPS 140-2 Level 3 validated HSMs.

The Azure Key Vault keys library client supports RSA keys and Elliptic Curve (EC) keys, each with corresponding support in hardware security modules (HSM). It offers operations to create, retrieve, update, delete, purge, backup, restore, and list the keys and its versions.

Source code | VCPKG | API reference documentation | Product documentation | Samples

Getting started

Include the package

The easiest way to acquire the C++ SDK is leveraging vcpkg package manager. See the corresponding Azure SDK for C++ readme section.

To install Azure Security Key Vault Keys package via vcpkg:

> vcpkg install azure-security-keyvault-keys-cpp

Then, use in your CMake file:

find_package(azure-security-keyvault-keys-cpp CONFIG REQUIRED)
target_link_libraries(<your project name> PRIVATE Azure::azure-security-keyvault-keys)

Prerequisites

See the final two steps in the next section for details on creating the Key Vault with the Azure CLI.

Authenticate the client

In order to interact with the Key Vault service, you'll need to create an instance of the KeyClient class. You need a vault URL, which you may see as "DNS Name" in the portal, and client secret credentials (client ID, client secret, tenant ID) to instantiate a client object.

Client Secret Credential authentication is being used in this Getting Started section, but you can find more ways to authenticate with Azure Identity. To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, you should install the Azure Identity package:

# Windows
vcpkg.exe install azure-identity-cpp
# Linux
./vcpkg install azure-identity-cpp

Create/Get credentials

Use the Azure CLI snippet below to create/get client secret credentials.

  • Create a service principal and configure its access to Azure resources:
    az ad sp create-for-rbac -n <your-application-name> --skip-assignment
    Output:
    {
    "appId": "generated-app-ID",
    "displayName": "dummy-app-name",
    "name": "http://dummy-app-name",
    "password": "random-password",
    "tenant": "tenant-ID"
    }
  • Take note of the service principal objectId
    az ad sp show --id <appId> --query objectId
    Output:
    "<your-service-principal-object-id>"
  • Use the returned credentials above to set AZURE_CLIENT_ID (appId), AZURE_CLIENT_SECRET (password), and AZURE_TENANT_ID (tenant) environment variables. The following example shows a way to do this in Powershell:

    $Env:AZURE_CLIENT_ID="generated-app-ID"
    $Env:AZURE_CLIENT_SECRET="random-password"
    $Env:AZURE_TENANT_ID="tenant-ID"
  • Grant the above mentioned application authorization to perform key operations on the Azure Key Vault:

    az keyvault set-policy --name <your-key-vault-name> --spn $Env:AZURE_CLIENT_ID --key-permissions backup delete get list create encrypt decrypt update

‍–key-permissions: Accepted values: backup, create, decrypt, delete, encrypt, get, import, list, purge, recover, restore, sign, unwrapKey, update, verify, wrapKey

If you have enabled role-based access control (RBAC) for Key Vault instead, you can find roles like "Key Vault Crypto Officer" in our RBAC guide. If you are managing your keys using Managed HSM, read about its access control that supports different built-in roles isolated from Azure Resource Manager (ARM).

  • Use the above mentioned Azure Key Vault name to retrieve details of your Vault which also contains your Azure Key Vault URL:

    az keyvault show --name <your-key-vault-name>
  • Create the Azure Key Vault or Managed HSM and grant the above mentioned application authorization to perform administrative operations on the Managed HSM (replace <your-resource-group-name> and <your-key-vault-name> with your own unique names and <your-service-principal-object-id> with the value from above):

If you are creating a standard Key Vault resource, use the following CLI command:

az keyvault create --resource-group <your-resource-group-name> --name <your-key-vault-name>

If you are creating a Managed HSM resource, use the following CLI command:

az keyvault create --hsm-name <your-key-vault-name> --resource-group <your-resource-group-name> --administrators <your-service-principal-object-id> --location <your-azure-location>

Activate your managed HSM

This section only applies if you are creating a Managed HSM. All data plane commands are disabled until the HSM is activated. You will not be able to create keys or assign roles. Only the designated administrators that were assigned during the create command can activate the HSM. To activate the HSM you must download the security domain.

To activate your HSM you need:

  • Minimum 3 RSA key-pairs (maximum 10)
  • Specify minimum number of keys required to decrypt the security domain (quorum)

To activate the HSM you send at least 3 (maximum 10) RSA public keys to the HSM. The HSM encrypts the security domain with these keys and sends it back. Once this security domain is successfully downloaded, your HSM is ready to use. You also need to specify quorum, which is the minimum number of private keys required to decrypt the security domain.

The example below shows how to use openssl to generate 3 self signed certificate.

openssl req -newkey rsa:2048 -nodes -keyout cert_0.key -x509 -days 365 -out cert_0.cer
openssl req -newkey rsa:2048 -nodes -keyout cert_1.key -x509 -days 365 -out cert_1.cer
openssl req -newkey rsa:2048 -nodes -keyout cert_2.key -x509 -days 365 -out cert_2.cer

Use the az keyvault security-domain download command to download the security domain and activate your managed HSM. The example below uses 3 RSA key pairs (only public keys are needed for this command) and sets the quorum to 2.

az keyvault security-domain download --hsm-name <your-key-vault-name> --sd-wrapping-keys ./certs/cert_0.cer ./certs/cert_1.cer ./certs/cert_2.cer --sd-quorum 2 --security-domain-file ContosoMHSM-SD.json

Create KeyClient

Once you've replaced your-vault-url with the above returned URI, you can create the KeyClient:

auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
// Create a new key client using the default credential from Azure Identity.
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
KeyClient keyClient(keyVaultUrl, credential);;
// Create a new key using the key client.
client.CreateKey("key-name", KeyVaultKeyType::Rsa);
// Retrieve a key using the key client.
key = client.GetKey("key-name");

Create CryptographyClient

Once you've created a KeyVaultKey in the Azure Key Vault, you can also create the CryptographyClient:

// Create a new cryptography client using the default credential from Azure Identity.
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
CryptographyClient cryptoClient(key.Id, credential);

Key concepts

KeyVaultKey

Azure Key Vault supports multiple key types and algorithms, and enables the use of hardware security modules (HSM) for high value keys.

KeyClient

A KeyClient providing synchronous operations exists in the SDK. Once you've initialized a KeyClient, you can interact with the primary resource types in Azure Key Vault.

CryptographyClient

A CryptographyClient providing synchronous operations exists in the SDK. Once you've initialized a CryptographyClient, you can use it to perform cryptographic operations with keys stored in Azure Key Vault.

Note

Microsoft recommends you not use CBC without first ensuring the integrity of the cipher text using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.

Optional initialization vector (IV). If you pass your own IV, make sure you use a cryptographically random, non-repeating IV. If null, a cryptographically random IV will be choosing using {RandomNumberGenerator | whatever cryptorng your language provides}.

Thread safety

We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.

Additional concepts

Replaceable HTTP transport adapter | Long-running operations |

Examples

The Azure.Security.KeyVault.Keys package supports synchronous APIs.

The following section provides several code snippets using the client created above, covering some of the most common Azure Key Vault key service related tasks:

Examples

Create a key

Create a key to be stored in the Azure Key Vault. If a key with the same name already exists, then a new version of the key is created.

// Create a key. Note that you can specify the type of key
// i.e. Elliptic curve, Hardware Elliptic Curve, RSA
auto key = client.CreateKey("key-name", KeyVaultKeyType::Rsa);
std::cout << key.Name();
std::cout << key.KeyType.ToString();
// Create a software RSA key
auto rsaCreateKey = CreateRsaKeyOptions("rsa-key-name", false);
KeyVaultKey rsaKey = client.CreateRsaKey(rsaCreateKey);
std::cout << rsaKey.Name();
std::cout << rsaKey.KeyType.ToString();
// Create a hardware Elliptic Curve key
// Because only premium Azure Key Vault supports HSM backed keys , please ensure your Azure Key Vault
// SKU is premium when you set "hardwareProtected" value to true
auto echsmkey = new CreateEcKeyOptions("ec-key-name", true);
KeyVaultKey ecKey = client.CreateEcKey(echsmkey);
std::cout << ecKey.Name();
std::cout << ecKey.KeyType.ToString();

Retrieve a key

GetKey retrieves a key previously stored in the Azure Key Vault.

KeyVaultKey key = client.GetKey("key-name");
std::cout << key.Name();
std::cout << key.KeyType.ToString();

Update an existing key

UpdateKeyProperties updates a key previously stored in the Azure Key Vault.

KeyVaultKey key = client.CreateKey("key-name", KeyVaultKeyType::Rsa);
// You can specify additional application-specific metadata in the form of tags.
key.Properties.Tags["foo"] = "updated tag";
KeyVaultKey updatedKey = client.UpdateKeyProperties(key.Properties);
std::cout << updatedKey.Name();
std::cout << updatedKey.Properties.Version;
std::cout << updatedKey.Properties.UpdatedOn->ToString();

Delete a key

StartDeleteKey starts a long-running operation to delete a key previously stored in the Azure Key Vault. You can retrieve the key immediately without waiting for the operation to complete. When soft-delete is not enabled for the Azure Key Vault, this operation permanently deletes the key.

DeleteKeyOperation operation = client.StartDeleteKey("key-name");
DeletedKey key = operation.Value();
std::cout << key.Name();
std::cout << key.DeletedOn->ToString();

Delete and purge a key

You will need to wait for the long-running operation to complete before trying to purge or recover the key.

DeleteKeyOperation operation = client.StartDeleteKey("key-name");
// You only need to wait for completion if you want to purge or recover the key.
while (!operation.IsDone())
{
std::this_thread::sleep_for(std::chrono::seconds(2));
operation.Poll();
}
DeletedKey key = operation.Value();
client.PurgeDeletedKey(key.Name());

List Keys

This example lists all the keys in the specified Azure Key Vault.

for (auto keys = client.GetPropertiesOfKeys(); keys.HasPage(); keys.MoveToNextPage())
{
for (auto const& key : keys.Items)
{
std::cout << key.Name;
}
}

Troubleshooting

General

When you interact with the Azure Key Vault key client library using the C++ SDK, errors returned by the service correspond to the same HTTP status codes returned for REST API requests.

For example, if you try to retrieve a key that doesn't exist in your Azure Key Vault, a 404 error is returned, indicating "Not Found".

try
{
KeyVaultKey key = client.GetKey("some_key").Value;
}
catch (const Azure::Core::RequestFailedException& ex)
{
std::cout << std::underlying_type<Azure::Core::Http::HttpStatusCode>::type(ex.StatusCode);
}

You will notice that additional information is logged, like the client request ID of the operation.

Message:
Azure.RequestFailedException : Service request failed.
Status: 404 (Not Found)
Content:
{"error":{"code":"KeyNotFound","message":"Key not found: some_key"}}
Headers:
Cache-Control: no-cache
Pragma: no-cache
Server: Microsoft-IIS/10.0
x-ms-keyvault-region: westus
x-ms-request-id: 625f870e-10ea-41e5-8380-282e5cf768f2
x-ms-keyvault-service-version: 1.1.0.866
x-ms-keyvault-network-info: addr=131.107.174.199;act_addr_fam=InterNetwork;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Strict-Transport-Security: max-age=31536000;includeSubDomains
X-Content-Type-Options: nosniff
Date: Tue, 18 Jun 2019 16:02:11 GMT
Content-Length: 75
Content-Type: application/json; charset=utf-8
Expires: -1

Next steps

Several Azure Key Vault keys client library samples are available to you in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Azure Key Vault:

  • sample1_hello_world.md - for working with Azure Key Vault, including:
    • Create a key
    • Get an existing key
    • Update an existing key
    • Delete a key
  • sample2_backup_and_restore.md - Contains the code snippets working with Azure Key Vault keys, including:
    • Backup and recover a key
  • sample3_get_keys.md - Example code for working with Azure Key Vault keys, including:
    • Create keys
    • List all keys in the Key Vault
    • Update keys in the Key Vault
    • List versions of a specified key
    • Delete keys from the Key Vault
    • List deleted keys in the Key Vault
  • sample4_encrypt_decrypt.md - Example code for performing cryptographic operations with Azure Key Vault keys, including:
    • Encrypt and Decrypt data with the CryptographyClient
  • sample5_sign_verify.md - Example code for working with Azure Key Vault keys, including:
    • Sign a precalculated digest and verify the signature with Sign and Verify
    • Sign raw data and verify the signature with SignData and VerifyData
  • sample6_wrap_unwrap.md - Example code for working with Azure Key Vault keys, including:
    • Wrap and Unwrap a symmetric key
  • sample7_key_rotation.md - Example code for working with Azure Key Vault keys, including:
    • Define Rotation policy
    • Apply rotation policy
    • Rotate key

Additional Documentation

Contributing

See the CONTRIBUTING.md for details on building, testing, and contributing to these libraries.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit the Contributor License Agreement.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact openc.nosp@m.ode@.nosp@m.micro.nosp@m.soft.nosp@m..com with any additional questions or comments.