Configuring HSM for Key Storage
Overview
Hardware Security Modules (HSMs) are tamper-resistant physical devices that safeguard cryptographic keys and perform cryptographic operations in a hardened environment. Keys stored in an HSM never leave the device boundary, providing the highest level of key protection. This skill covers configuring HSMs using the PKCS#11 standard interface, including key generation, signing, encryption, and key management using both physical HSMs and SoftHSM2 for development.
When to Use
- When deploying or configuring configuring hsm for key storage capabilities in your environment
- When establishing security controls aligned to compliance requirements
- When building or improving security architecture for this domain
- When conducting security assessments that require this implementation
Common Misconfigurations & Verification
- Keys generated with
CKA_EXTRACTABLE=True (or CKA_SENSITIVE=False): this defeats the entire purpose of an HSM since the private key can be exported in the clear. Set CKA_EXTRACTABLE=False, CKA_SENSITIVE=True, CKA_TOKEN=True and verify by attempting C_GetAttributeValue / pkcs11-tool --read-object on the private key — it MUST fail.
- Key generated in software then imported (wrapped/unwrapped) into the HSM: the plaintext key existed outside the boundary. Always use
C_GenerateKeyPair on-device; confirm with CKA_LOCAL=True.
- Single SO/user PIN shared, no key ceremony, weak/default PIN: use distinct SO and user PINs, enforce a quorum (M-of-N) for CA root keys, and enable login retry lockout.
- No per-application slot/partition isolation: separate tokens per app so a compromise of one PIN cannot use another app's keys.
- Audit logging disabled: every
C_Sign/C_Decrypt/key-management op should be logged.
- Verify non-extractability concretely: run
pkcs11-tool --list-objects --type privkey and confirm Access: sensitive, always sensitive, never extractable. A sign/verify round-trip must succeed while a key-export attempt is rejected by the token.
Prerequisites
- Familiarity with cryptography concepts and tools
- Access to a test or lab environment for safe execution
- Python 3.8+ with required dependencies installed
- Appropriate authorization for any testing activities
Objectives
- Configure SoftHSM2 as a development PKCS#11 provider
- Generate and manage keys inside the HSM via PKCS#11
- Perform cryptographic operations (sign, verify, encrypt, decrypt) using HSM-resident keys
- Implement HSM-backed certificate authority operations
- Configure key access policies and user authentication
- Interface with cloud HSM services (AWS CloudHSM, Azure)
Key Concepts
HSM Compliance Levels
| FIPS Level |
Protection |
Use Case |
| FIPS 140-2 Level 1 |
Software only |
Development |
| FIPS 140-2 Level 2 |
Tamper-evident, role-based auth |
General production |
| FIPS 140-2 Level 3 |
Tamper-resistant, identity-based auth |
Financial, government |
| FIPS 140-2 Level 4 |
Physical tamper response |
Military, classified |
PKCS#11 Architecture
Application --> PKCS#11 API --> HSM Provider --> Hardware HSM
|
(SoftHSM2 for dev)
Key Objects in PKCS#11
| Object Type |
Description |
Operations |
| CKO_SECRET_KEY |
Symmetric keys (AES) |
Encrypt, Decrypt, Wrap |
| CKO_PUBLIC_KEY |
Public keys (RSA, EC) |
Verify, Encrypt, Wrap |
| CKO_PRIVATE_KEY |
Private keys (RSA, EC) |
Sign, Decrypt, Unwrap |
| CKO_CERTIFICATE |
X.509 certificates |
Storage, retrieval |
Security Considerations
- Never export private keys from HSM (use CKA_EXTRACTABLE=False)
- Use separate slots/partitions for different applications
- Implement multi-person key ceremony for CA root keys
- Enable audit logging for all HSM operations
- Implement HSM backup and disaster recovery
- Use strong PINs and enable SO (Security Officer) PIN
Validation Criteria
1---2name: configuring-hsm-for-key-storage3description: Hardware Security Modules (HSMs) are tamper-resistant physical devices that safeguard cryptographic keys and perform cryptographic operations in a hardened environment. Keys stored in an HSM never lea4license: Apache-2.05---6# Configuring HSM for Key Storage78## Overview910Hardware Security Modules (HSMs) are tamper-resistant physical devices that safeguard cryptographic keys and perform cryptographic operations in a hardened environment. Keys stored in an HSM never leave the device boundary, providing the highest level of key protection. This skill covers configuring HSMs using the PKCS#11 standard interface, including key generation, signing, encryption, and key management using both physical HSMs and SoftHSM2 for development.111213## When to Use1415- When deploying or configuring configuring hsm for key storage capabilities in your environment16- When establishing security controls aligned to compliance requirements17- When building or improving security architecture for this domain18- When conducting security assessments that require this implementation1920## Common Misconfigurations & Verification2122- **Keys generated with `CKA_EXTRACTABLE=True` (or `CKA_SENSITIVE=False`):** this defeats the entire purpose of an HSM since the private key can be exported in the clear. Set `CKA_EXTRACTABLE=False`, `CKA_SENSITIVE=True`, `CKA_TOKEN=True` and verify by attempting `C_GetAttributeValue` / `pkcs11-tool --read-object` on the private key — it MUST fail.23- **Key generated in software then imported (wrapped/unwrapped) into the HSM:** the plaintext key existed outside the boundary. Always use `C_GenerateKeyPair` on-device; confirm with `CKA_LOCAL=True`.24- **Single SO/user PIN shared, no key ceremony, weak/default PIN:** use distinct SO and user PINs, enforce a quorum (M-of-N) for CA root keys, and enable login retry lockout.25- **No per-application slot/partition isolation:** separate tokens per app so a compromise of one PIN cannot use another app's keys.26- **Audit logging disabled:** every `C_Sign`/`C_Decrypt`/key-management op should be logged.27- **Verify non-extractability concretely:** run `pkcs11-tool --list-objects --type privkey` and confirm `Access: sensitive, always sensitive, never extractable`. A sign/verify round-trip must succeed while a key-export attempt is **rejected** by the token.2829## Prerequisites3031- Familiarity with cryptography concepts and tools32- Access to a test or lab environment for safe execution33- Python 3.8+ with required dependencies installed34- Appropriate authorization for any testing activities3536## Objectives3738- Configure SoftHSM2 as a development PKCS#11 provider39- Generate and manage keys inside the HSM via PKCS#1140- Perform cryptographic operations (sign, verify, encrypt, decrypt) using HSM-resident keys41- Implement HSM-backed certificate authority operations42- Configure key access policies and user authentication43- Interface with cloud HSM services (AWS CloudHSM, Azure)4445## Key Concepts4647### HSM Compliance Levels4849| FIPS Level | Protection | Use Case |50|-----------|-----------|----------|51| FIPS 140-2 Level 1 | Software only | Development |52| FIPS 140-2 Level 2 | Tamper-evident, role-based auth | General production |53| FIPS 140-2 Level 3 | Tamper-resistant, identity-based auth | Financial, government |54| FIPS 140-2 Level 4 | Physical tamper response | Military, classified |5556### PKCS#11 Architecture5758```59Application --> PKCS#11 API --> HSM Provider --> Hardware HSM60 |61 (SoftHSM2 for dev)62```6364### Key Objects in PKCS#116566| Object Type | Description | Operations |67|-------------|-------------|-----------|68| CKO_SECRET_KEY | Symmetric keys (AES) | Encrypt, Decrypt, Wrap |69| CKO_PUBLIC_KEY | Public keys (RSA, EC) | Verify, Encrypt, Wrap |70| CKO_PRIVATE_KEY | Private keys (RSA, EC) | Sign, Decrypt, Unwrap |71| CKO_CERTIFICATE | X.509 certificates | Storage, retrieval |7273## Security Considerations7475- Never export private keys from HSM (use CKA_EXTRACTABLE=False)76- Use separate slots/partitions for different applications77- Implement multi-person key ceremony for CA root keys78- Enable audit logging for all HSM operations79- Implement HSM backup and disaster recovery80- Use strong PINs and enable SO (Security Officer) PIN8182## Validation Criteria8384- [ ] SoftHSM2 initializes with token and user PIN85- [ ] AES key generates inside HSM86- [ ] RSA key pair generates inside HSM87- [ ] Encryption/decryption uses HSM-resident keys88- [ ] Signing/verification uses HSM-resident keys89- [ ] Keys cannot be exported (non-extractable)90- [ ] Key listing shows all HSM-stored objects