Azure Key Management: Securing Your Secrets and Certificates
Tags: azure
Published: 2023-07-23 17:43:44

## Introduction

Data security has grown to become one of the . Organizations need robust methods to protect sensitive information, such as cryptographic keys, certificates, and application secrets. Microsoft Azure provides a powerful and secure solution to manage these critical components through Azure Key Vault. In this blog post, we will explore Azure Key Management, covering keys, certificates, secrets, and the concept of Hardware Security Modules (HSM).

## Table of Contents

1. [What is Azure Key Vault?](#what-is-azure-key-vault)
2. [Key Management in Azure Key Vault](#key-management-in-azure-key-vault)
   - [Creating and Managing Keys](#creating-and-managing-keys)
   - [Key Types: RSA, ECC, and Oct](#key-types-rsa-ecc-and-oct)
   - [Key Versioning and Rotation](#key-versioning-and-rotation)
   - [Key Usage Scenarios: Encryption and Signing](#key-usage-scenarios-encryption-and-signing)
3. [Managing Certificates in Azure Key Vault](#managing-certificates-in-azure-key-vault)
   - [Uploading Certificates](#uploading-certificates)
   - [Importing Certificates](#importing-certificates)
   - [Certificate Lifecycle Management](#certificate-lifecycle-management)
   - [Certificate Usage Scenarios: SSL/TLS and Code Signing](#certificate-usage-scenarios-ssl-tls-and-code-signing)
4. [Storing Secrets in Azure Key Vault](#storing-secrets-in-azure-key-vault)
   - [Creating and Managing Secrets](#creating-and-managing-secrets)
   - [Secrets Expiration and Versioning](#secrets-expiration-and-versioning)
   - [Secret Usage Scenarios: Connection Strings and API Keys](#secret-usage-scenarios-connection-strings-and-api-keys)
5. [Hardware Security Modules (HSM) in Azure Key Vault](#hardware-security-modules-hsm-in-azure-key-vault)
   - [HSM Overview and Benefits](#hsm-overview-and-benefits)
   - [Enabling HSM-backed Keys and Certificates](#enabling-hsm-backed-keys-and-certificates)
   - [Hardware Security and Compliance](#hardware-security-and-compliance)
6. [References](#references)

## What is Azure Key Vault?

Azure Key Vault is a cloud-based service provided by Microsoft Azure that allows users to securely store and manage cryptographic keys, certificates, and application secrets used by cloud applications and services. 

This provides a highly available and scalable solution, that enables developers to operate under security best practices (and in some cases achieve compliance) without managing the underlying infrastructure.

Azure Key Vault offers various features to protect sensitive information, including role-based access control, auditing, and soft-delete as well as other mechanisms for deletion protection. 



## Key Management in Azure Key Vault

### Creating and Managing Keys

To create a key in Azure Key Vault, you can use the Azure Portal, Azure CLI, or Azure SDKs. Let's use Azure CLI to create a new RSA key:

```bash
# Create an RSA key in Azure Key Vault
az keyvault key create --vault-name my-key-vault --name my-rsa-key --kty RSA
```

The following command creates a key with the name `my-rsa-key`, with a key type of `rsa` in the vault called `my-key-vault`

The power of key vault comes from the ability to you perform multiple operations on your keys.

To view the created key (the public component of it), you can use the following command:

```bash
# Create an RSA key in Azure Key Vault
az keyvault key show --vault-name my-key-vault --name my-rsa-key
```

Sounds simple, doesn't it? Let's explore the different key types that is offered in Azure

### Key Types: RSA, ECC, and Oct

Azure Key Vault supports various key types, including RSA (Rivest-Shamir-Adleman), ECC (Elliptic Curve Cryptography), and symmetric keys (Oct). You can choose the appropriate key type based on your security and performance requirements. It is also worth mentioning that encryption/decryption and wrapping/unwrapping are not available under EC types as of time of this post.

### Key Versioning and Rotation

Key Vault automatically maintains different versions of keys to support key rotation. This ensures that your applications can seamlessly use new keys as they are rolled out, without any interruptions. Azure also provide automatic rotation policies that can make this more streamline.

### Key Usage Scenarios: Encryption and Signing

Keys stored in Azure Key Vault can be used for encryption and signing operations. For example, you can use RSA keys to encrypt data at rest or sign digital certificates for code signing.



## Managing Certificates in Azure Key Vault

### Uploading Certificates

Azure Key Vault allows you to upload X.509 certificates that your applications use for various purposes. Let's use Azure CLI to upload a certificate:

```bash
# Upload a certificate to Azure Key Vault
az keyvault certificate import --vault-name my-key-vault --name my-ssl-cert --file my_ssl_certificate.pfx --password <certificate_password>
```

### Importing Certificates

You can also import existing certificates to Azure Key Vault. Importing certificates allows you to leverage existing private keys securely stored within Key Vault.

### Certificate Lifecycle Management

Azure Key Vault offers features for certificate lifecycle management, including renewal, expiration, and revocation. This ensures that your certificates are always up-to-date and secure.

### Certificate Usage Scenarios: SSL/TLS and Code Signing

Certificates stored in Azure Key Vault can be used for securing SSL/TLS communications and code signing. They provide the necessary cryptographic validation to ensure secure data transmission and trust in software applications.

## Storing Secrets in Azure Key Vault

### Creating and Managing Secrets

In addition to keys and certificates, Azure Key Vault allows you to store and manage secrets. Secrets can be connection strings, API keys, passwords, or any other sensitive information your applications require.

```bash
# Create a secret in Azure Key Vault
az keyvault secret set --vault-name my-key-vault --name my-db-connection-string --value "<your_connection_string>"
```

### Secrets Expiration and Versioning

Just like keys and certificates, secrets support expiration and versioning. This ensures that secrets are periodically rotated and easily retrievable by applications.

### Secret Usage Scenarios: Connection Strings and API Keys

Secrets stored in Azure Key Vault can be used by applications to retrieve sensitive configuration data, such as database connection strings or API keys, without exposing them in source code or configuration files.

## Hardware Security Modules (HSM) in Azure Key Vault

### HSM Overview and Benefits

Azure Key Vault offers an HSM-backed option for keys and certificates. HSMs provide a dedicated and tamper-resistant hardware environment for cryptographic operations, providing an additional layer of security.

### Enabling HSM-backed Keys and Certificates

To enable HSM-backed keys and certificates in Azure Key Vault, you need to specify the `--hsm` parameter when creating them:

```bash
# Create an HSM-backed RSA key in Azure Key Vault
az keyvault key create --vault-name my-key-vault --name my-hsm-rsa-key --kty RSA-HSM
```

### Hardware Security and Compliance

Using HSM-backed keys ensures that cryptographic operations are performed within a secure hardware environment, meeting regulatory compliance requirements for data protection.

It is also worth mentioning that some types of encryption are only available when using a HSM

## References

[Encrypt and decrypt blobs using Azure Key Vault - Azure Storage | Microsoft Learn](https://learn.microsoft.com/en-us/azure/storage/blobs/storage-encrypt-decrypt-blobs-key-vault?tabs=roles-azure-portal%2Cpackages-dotnetcli)

[Key types, algorithms, and operations - Azure Key Vault | Microsoft Learn](https://learn.microsoft.com/en-us/azure/key-vault/keys/about-keys-details)

[az keyvault key | Microsoft Learn](https://learn.microsoft.com/en-us/cli/azure/keyvault/key?view=azure-cli-latest)

[Manage Azure Key Vault using CLI - Azure Key Vault | Microsoft Learn](https://learn.microsoft.com/en-us/azure/key-vault/general/manage-with-cli2)

[Azure Managed HSM Overview - Azure Managed HSM | Microsoft Learn](https://learn.microsoft.com/en-us/azure/key-vault/managed-hsm/overview)

[About keys - Azure Key Vault | Microsoft Learn](https://learn.microsoft.com/en-us/azure/key-vault/keys/about-keys)

[About Azure Key Vault secrets - Azure Key Vault | Microsoft Learn](https://learn.microsoft.com/en-us/azure/key-vault/secrets/about-secrets)

[About Azure Key Vault certificates | Microsoft Learn](https://learn.microsoft.com/en-us/azure/key-vault/certificates/about-certificates)