|
| 1 | +# Message Encryption |
| 2 | + |
| 3 | +This document describes the message encryption 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-256-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 | +## Configuration |
| 10 | + |
| 11 | +### Configuration File |
| 12 | + |
| 13 | +Add encryption settings to your `tinode.conf` file: |
| 14 | + |
| 15 | +```json |
| 16 | +{ |
| 17 | + "store_config": { |
| 18 | + "encryption": { |
| 19 | + "enabled": true, |
| 20 | + "key": "base64-encoded-32-byte-key-here" |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### Command Line Flags |
| 27 | + |
| 28 | +You can also enable encryption via command line flags: |
| 29 | + |
| 30 | +```bash |
| 31 | +./tinode-server --encryption_enabled --encryption_key "base64-encoded-32-byte-key-here" |
| 32 | +``` |
| 33 | + |
| 34 | +## Key Management |
| 35 | + |
| 36 | +### Generating an Encryption Key |
| 37 | + |
| 38 | +Generate a 32-byte (256-bit) random key using the built-in keygen tool: |
| 39 | + |
| 40 | +```bash |
| 41 | +# Generate 32-byte encryption key |
| 42 | +cd keygen |
| 43 | +./keygen -encryption |
| 44 | + |
| 45 | +# Generate custom size key |
| 46 | +./keygen -encryption -keysize 32 |
| 47 | + |
| 48 | +# Save key to file |
| 49 | +./keygen -encryption -output encryption.key |
| 50 | +``` |
| 51 | + |
| 52 | +Alternatively, you can use OpenSSL: |
| 53 | + |
| 54 | +```bash |
| 55 | +# Generate 32 random bytes and encode in base64 |
| 56 | +openssl rand -base64 32 |
| 57 | +``` |
| 58 | + |
| 59 | +### Key Storage |
| 60 | + |
| 61 | +Store your encryption key securely: |
| 62 | +- Never commit encryption keys to version control |
| 63 | +- Use environment variables or secure key management systems |
| 64 | +- Consider using hardware security modules (HSMs) for production environments |
| 65 | + |
| 66 | +## Migration |
| 67 | + |
| 68 | +### From Unencrypted to Encrypted |
| 69 | + |
| 70 | +To encrypt existing unencrypted messages, use the migration tool: |
| 71 | + |
| 72 | +```bash |
| 73 | +# First, do a dry run to see what would be encrypted |
| 74 | +go run server/tools/encrypt_messages.go \ |
| 75 | + --config tinode.conf \ |
| 76 | + --key_string "your-base64-encoded-key" \ |
| 77 | + --topic "your-topic-name" \ |
| 78 | + --dry_run |
| 79 | + |
| 80 | +# Then run the actual encryption |
| 81 | +go run server/tools/encrypt_messages.go \ |
| 82 | + --config tinode.conf \ |
| 83 | + --key_string "your-base64-encoded-key" \ |
| 84 | + --topic "your-topic-name" |
| 85 | +``` |
| 86 | + |
| 87 | +### From Encrypted to Unencrypted |
| 88 | + |
| 89 | +To decrypt encrypted messages (use with caution): |
| 90 | + |
| 91 | +```bash |
| 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 | + --reverse |
| 97 | +``` |
| 98 | + |
| 99 | +## Security Considerations |
| 100 | + |
| 101 | +### What is Encrypted |
| 102 | + |
| 103 | +- **Message content**: The actual text/content of messages |
| 104 | +- **Metadata**: Message headers, timestamps, sender info, etc. remain unencrypted |
| 105 | + |
| 106 | +### What is NOT Encrypted |
| 107 | + |
| 108 | +- Message metadata (sender, timestamp, sequence ID, etc.) |
| 109 | +- Topic information |
| 110 | +- User information |
| 111 | +- File attachments (planned for future versions) |
| 112 | + |
| 113 | +### Limitations |
| 114 | + |
| 115 | +- Database administrators can still see message metadata |
| 116 | +- The encryption key must be stored securely |
| 117 | +- If the key is lost, encrypted messages cannot be recovered |
| 118 | +- Encryption adds computational overhead |
| 119 | + |
| 120 | +## Implementation Details |
| 121 | + |
| 122 | +### Encryption Algorithm |
| 123 | + |
| 124 | +- **Cipher**: AES-256 |
| 125 | +- **Mode**: GCM (Galois/Counter Mode) |
| 126 | +- **Key size**: 256 bits (32 bytes) |
| 127 | +- **Nonce**: Random 12-byte nonce for each message |
| 128 | + |
| 129 | +### Storage Format |
| 130 | + |
| 131 | +Encrypted content is stored as a JSON object: |
| 132 | + |
| 133 | +```json |
| 134 | +{ |
| 135 | + "data": "base64-encoded-encrypted-data", |
| 136 | + "nonce": "base64-encoded-nonce", |
| 137 | + "encrypted": true |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### Performance Impact |
| 142 | + |
| 143 | +- **Encryption**: ~1-5ms per message (depending on content size) |
| 144 | +- **Decryption**: ~1-5ms per message (depending on content size) |
| 145 | +- **Storage overhead**: ~33% increase in content field size |
| 146 | + |
| 147 | +## Troubleshooting |
| 148 | + |
| 149 | +### Common Issues |
| 150 | + |
| 151 | +1. **"encryption key is required when encryption is enabled"** |
| 152 | + - Ensure you've provided a valid base64-encoded 32-byte key |
| 153 | + |
| 154 | +2. **"failed to decode base64 encryption key"** |
| 155 | + - Verify your key is properly base64-encoded |
| 156 | + - Ensure the key is exactly 32 bytes when decoded |
| 157 | + |
| 158 | +3. **"failed to create AES cipher"** |
| 159 | + - This usually indicates a system-level issue with crypto libraries |
| 160 | + |
| 161 | +### Logs |
| 162 | + |
| 163 | +Encryption-related errors and warnings are logged with the prefix: |
| 164 | +- `topic[topic-name]: failed to encrypt message content (seq: X) - err: ...` |
| 165 | +- `topic[topic-name]: failed to decrypt message content (seq: X) - err: ...` |
| 166 | + |
| 167 | +## Future Enhancements |
| 168 | + |
| 169 | +- File attachment encryption |
| 170 | +- Key rotation support |
| 171 | +- Hardware security module (HSM) integration |
| 172 | +- Per-topic encryption settings |
| 173 | +- End-to-end encryption support |
| 174 | + |
| 175 | +## API Changes |
| 176 | + |
| 177 | +No changes to the client API are required. Messages are automatically encrypted/decrypted transparently. |
| 178 | + |
| 179 | +## Testing |
| 180 | + |
| 181 | +To test encryption functionality: |
| 182 | + |
| 183 | +1. Start the server with encryption enabled |
| 184 | +2. Send messages through the normal API |
| 185 | +3. Verify messages are encrypted in the database |
| 186 | +4. Verify messages are decrypted when retrieved |
| 187 | +5. Check that encryption/decryption errors are properly logged |
0 commit comments