Overview
The Key Management Service uses RSA-2048 asymmetric encryption:- Public keys are distributed to clients for encrypting sensitive data
- Private keys remain on the server for decryption
- Keys are device-specific and have automatic expiration
Key Generation Flow
Server generates RSA key pair
The server generates a 2048-bit RSA key pair using Node.js crypto module.
Store private key securely
Private key is stored in the database with:
- Unique key ID
- Device association
- Expiration timestamp (30 days default)
- Creation timestamp
Generating Client Encryption Keys
GraphQL Mutation
Input Type
Response Type
Server-Side Implementation
RSA Key Pair Generation
The service uses Node.js built-in crypto module to generate 2048-bit RSA keys:Key Reuse Strategy
Before generating a new key, the service checks for existing valid keys:Rate Limiting
The service enforces rate limits to prevent abuse:- Limit: 5 key generations per device
- Window: 24 hours
- Reset: Counter resets after the time window
Key Rotation
Automatic Expiration
Keys automatically expire after a configured interval (default: 30 days):Manual Key Rotation
You can manually rotate keys for a device:Server-Side Rotation Implementation
- Marks all existing keys as deprecated
- Generates a new key pair
- Returns the new public key to the client
Key Validation
Validate whether a key ID is still valid:Configuration
Key generation behavior can be configured via environment variables:Key Storage Schema
Keys are stored in the database with the following structure:Complete Example
Best Practices
Reuse Valid Keys
The service automatically reuses valid keys for a device. Don’t generate new keys unnecessarily.
Store Keys Securely
Store the keyId and publicKey securely on the client (e.g., encrypted local storage).
Handle Expiration
Implement retry logic for expired keys. Generate a new key when decryption fails with “key expired” error.
Monitor Rate Limits
Avoid hitting rate limits by caching keys and only generating when necessary.
Troubleshooting
Key Not Found Error
If you receive “Encryption key not found”, the key may have been:- Deleted from the database
- Never generated
- Expired and cleaned up
generateClientEncryptionKey.
Key Expired Error
Keys expire after 30 days (default). When a key expires: Solution: CallrotateClientEncryptionKey to get a new key.
Rate Limit Exceeded
If you hit the rate limit (5 keys per 24 hours per device): Solution: Wait for the rate limit window to reset or reuse the existing valid key.Next Steps
Encryption & Decryption
Learn how to encrypt and decrypt data using generated keys
Authentication
Implement secure authentication with encrypted credentials