|
| 1 | +# Message Encryption at Rest |
| 2 | + |
| 3 | +This document describes the message encryption at rest feature in Tinode, which allows encrypting message content stored in the database to prevent unauthorized access to message content using database tools. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The encryption feature uses AES-GCM symmetric encryption to encrypt only the `content` field of messages. The encryption is transparent to clients - messages are automatically encrypted when saved and decrypted when retrieved. |
| 8 | + |
| 9 | +**Supported AES key sizes:** |
| 10 | + |
| 11 | +- AES-128: 16 bytes (128 bits) |
| 12 | +- AES-192: 24 bytes (192 bits) |
| 13 | +- AES-256: 32 bytes (256 bits) |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +### Configuration File |
| 18 | + |
| 19 | +Add encryption settings to your `tinode.conf` file: |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "store_config": { |
| 24 | + "encrypt_at_rest": { |
| 25 | + "key": "base64-encoded-key-here" |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +**Note:** If no key is provided or the key is empty, encryption is disabled. |
| 32 | + |
| 33 | +### Command Line Flags |
| 34 | + |
| 35 | +You can also enable encryption via command line flags (overrides config file): |
| 36 | + |
| 37 | +```bash |
| 38 | +./tinode-server --message_encrypt_at_rest_key "base64-encoded-key-here" |
| 39 | +``` |
| 40 | + |
| 41 | +## Key Management |
| 42 | + |
| 43 | +### Generating an Encryption Key |
| 44 | + |
| 45 | +Generate a random key using the built-in keygen tool: |
| 46 | + |
| 47 | +```bash |
| 48 | +# Generate 32-byte encryption key (AES-256) |
| 49 | +cd keygen |
| 50 | +./keygen -encryption |
| 51 | + |
| 52 | +# Generate 16-byte encryption key (AES-128) |
| 53 | +./keygen -encryption -keysize 16 |
| 54 | + |
| 55 | +# Generate 24-byte encryption key (AES-192) |
| 56 | +./keygen -encryption -keysize 24 |
| 57 | + |
| 58 | +# Save key to file using shell redirection |
| 59 | +./keygen -encryption > encryption.key |
| 60 | +``` |
| 61 | + |
| 62 | +The keygen tool validates that the key size is exactly 16, 24, or 32 bytes. |
| 63 | + |
| 64 | +Alternatively, you can use OpenSSL: |
| 65 | + |
| 66 | +```bash |
| 67 | +# Generate 16 random bytes and encode in base64 (AES-128) |
| 68 | +openssl rand -base64 16 |
| 69 | + |
| 70 | +# Generate 24 random bytes and encode in base64 (AES-192) |
| 71 | +openssl rand -base64 24 |
| 72 | + |
| 73 | +# Generate 32 random bytes and encode in base64 (AES-256) |
| 74 | +openssl rand -base64 32 |
| 75 | +``` |
| 76 | + |
| 77 | +### Key Storage |
| 78 | + |
| 79 | +Store your encryption key securely: |
| 80 | +- Never commit encryption keys to version control |
| 81 | +- Use environment variables or secure key management systems |
| 82 | +- Consider using hardware security modules (HSMs) for production environments |
| 83 | + |
| 84 | +## Migration |
| 85 | + |
| 86 | +### From Unencrypted to Encrypted |
| 87 | + |
| 88 | +To encrypt existing unencrypted messages, use the migration tool: |
| 89 | + |
| 90 | +```bash |
| 91 | +# First, do a dry run to see what would be encrypted |
| 92 | +go run server/tools/encrypt_messages.go \ |
| 93 | + --config tinode.conf \ |
| 94 | + --key_string "your-base64-encoded-key" \ |
| 95 | + --topic "your-topic-name" \ |
| 96 | + --dry_run |
| 97 | + |
| 98 | +# Then run the actual encryption |
| 99 | +go run server/tools/encrypt_messages.go \ |
| 100 | + --config tinode.conf \ |
| 101 | + --key_string "your-base64-encoded-key" \ |
| 102 | + --topic "your-topic-name" |
| 103 | +``` |
| 104 | + |
| 105 | +**Note:** The migration tool now uses the proper store interface and handles all supported AES key sizes. |
| 106 | + |
| 107 | +### From Encrypted to Unencrypted |
| 108 | + |
| 109 | +To decrypt encrypted messages (use with caution): |
| 110 | + |
| 111 | +```bash |
| 112 | +go run server/tools/encrypt_messages.go \ |
| 113 | + --config tinode.conf \ |
| 114 | + --key_string "your-base64-encoded-key" \ |
| 115 | + --topic "your-topic-name" \ |
| 116 | + --reverse |
| 117 | +``` |
| 118 | + |
| 119 | +## Security Considerations |
| 120 | + |
| 121 | +### What is Encrypted |
| 122 | + |
| 123 | +- **Message content**: The actual text/content of messages |
| 124 | +- **Metadata**: Message headers, timestamps, sender info, etc. remain unencrypted |
| 125 | + |
| 126 | +### What is NOT Encrypted |
| 127 | + |
| 128 | +- Message metadata (sender, timestamp, sequence ID, etc.) |
| 129 | +- Topic information |
| 130 | +- User information |
| 131 | +- File attachments (planned for future versions) |
| 132 | + |
| 133 | +### Limitations |
| 134 | + |
| 135 | +- Database administrators can still see message metadata |
| 136 | +- The encryption key must be stored securely |
| 137 | +- If the key is lost, encrypted messages cannot be recovered |
| 138 | +- Encryption adds computational overhead |
| 139 | + |
| 140 | +## Implementation Details |
| 141 | + |
| 142 | +### Encryption Algorithm |
| 143 | + |
| 144 | +- **Cipher**: AES (Advanced Encryption Standard) |
| 145 | +- **Mode**: GCM (Galois/Counter Mode) |
| 146 | +- **Key sizes**: 128, 192, or 256 bits (16, 24, or 32 bytes) |
| 147 | +- **Nonce**: Random 12-byte nonce for each message |
| 148 | + |
| 149 | +### Storage Format |
| 150 | + |
| 151 | +Encrypted content is stored as a JSON object with automatic base64 encoding: |
| 152 | + |
| 153 | +```json |
| 154 | +{ |
| 155 | + "data": "base64-encoded-encrypted-data", |
| 156 | + "nonce": "base64-encoded-nonce", |
| 157 | + "encrypted": true |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +The `data` and `nonce` fields are automatically base64 encoded/decoded during JSON marshaling/unmarshaling. |
| 162 | + |
| 163 | +### Performance Impact |
| 164 | + |
| 165 | +- **Encryption**: ~1-5ms per message (depending on content size) |
| 166 | +- **Decryption**: ~1-5ms per message (depending on content size) |
| 167 | +- **Storage overhead**: ~33% increase in content field size |
| 168 | + |
| 169 | +## Troubleshooting |
| 170 | + |
| 171 | +### Common Issues |
| 172 | + |
| 173 | +1. **"encryption key must be 16, 24, or 32 bytes"** |
| 174 | + - Ensure your key is exactly 16, 24, or 32 bytes when decoded from base64 |
| 175 | + - Use the keygen tool to generate valid keys |
| 176 | + |
| 177 | +2. **"failed to decode base64 encryption key"** |
| 178 | + - Verify your key is properly base64-encoded |
| 179 | + - Ensure there are no extra spaces or newlines in the key |
| 180 | + |
| 181 | +3. **"failed to create AES cipher"** |
| 182 | + - This usually indicates a system-level issue with crypto libraries |
| 183 | + |
| 184 | +### Logs |
| 185 | + |
| 186 | +Encryption-related errors and warnings are logged with the prefix: |
| 187 | +- `topic[topic-name]: failed to encrypt message content (seq: X) - err: ...` |
| 188 | +- `topic[topic-name]: failed to decrypt message content (seq: X) - err: ...` |
| 189 | + |
| 190 | +## Future Enhancements |
| 191 | + |
| 192 | +- File attachment encryption |
| 193 | +- Key rotation support |
| 194 | +- Hardware security module (HSM) integration |
| 195 | +- Per-topic encryption settings |
| 196 | +- End-to-end encryption support |
| 197 | + |
| 198 | +## Migration from Previous Versions |
| 199 | + |
| 200 | +**New configuration:** |
| 201 | + |
| 202 | +```json |
| 203 | +{ |
| 204 | + "store_config": { |
| 205 | + "encrypt_at_rest": { |
| 206 | + "key": "base64-encoded-key" |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +**Key changes:** |
| 213 | +- Support for multiple AES key sizes (16, 24, 32 bytes) instead of just 32 bytes |
| 214 | +- Command line flag renamed from `--encryption_key` to `--message_encrypt_at_rest_key` |
| 215 | + |
| 216 | +### Migration Steps |
| 217 | + |
| 218 | +1. **Update your configuration file** to use the new field names |
| 219 | +2. **Test with a dry run** using the migration tool |
| 220 | +3. **Restart the server** with the new configuration |
| 221 | +4. **Verify encryption is working** by checking logs and database content |
| 222 | + |
| 223 | +## API Changes |
| 224 | + |
| 225 | +No changes to the client API are required. Messages are automatically encrypted/decrypted transparently. |
| 226 | + |
| 227 | +## Testing |
| 228 | + |
| 229 | +To test encryption functionality: |
| 230 | + |
| 231 | +1. Start the server with encryption enabled |
| 232 | +2. Send messages through the normal API |
| 233 | +3. Verify messages are encrypted in the database |
| 234 | +4. Verify messages are decrypted when retrieved |
| 235 | +5. Check that encryption/decryption errors are properly logged |
0 commit comments