|
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
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:
Then, use in your CMake file:
See the final two steps in the next section for details on creating the Key Vault with the Azure CLI.
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:
Use the Azure CLI snippet below to create/get client secret credentials.
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:
Grant the above mentioned application authorization to perform key operations on the Azure Key Vault:
–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:
<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:
If you are creating a Managed HSM resource, use the following CLI command:
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:
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.
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.
Once you've replaced your-vault-url with the above returned URI, you can create the KeyClient:
Once you've created a KeyVaultKey
in the Azure Key Vault, you can also create the CryptographyClient:
Azure Key Vault supports multiple key types and algorithms, and enables the use of hardware security modules (HSM) for high value keys.
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.
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.
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.
Replaceable HTTP transport adapter | Long-running operations |
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:
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.
GetKey
retrieves a key previously stored in the Azure Key Vault.
UpdateKeyProperties
updates a key previously stored in the Azure Key Vault.
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.
You will need to wait for the long-running operation to complete before trying to purge or recover the key.
This example lists all the keys in the specified Azure Key Vault.
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".
You will notice that additional information is logged, like the client request ID of the operation.
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:
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.