feat(chat): Implement client-side end-to-end encryption with key management

- Add chat keys API endpoint for storing and retrieving encrypted RSA public/private key pairs
- Create client-side crypto utilities for E2E encryption/decryption with hybrid encryption support
- Implement useChatEncryption hook for managing encryption state and key generation in chat UI
- Add chatPublicKey and chatPrivateKeyEncrypted fields to user schema for key storage
- Update chat messages API to return encrypted content with sender's public key for decryption
- Modify send message endpoint to accept pre-encrypted content from client while maintaining legacy server-side encryption
- Update chat page UI to integrate client-side encryption workflow
- Ensure private keys are encrypted client-side with user password before transmission to server
- Server cannot decrypt message content in E2E mode, providing true end-to-end encryption
This commit is contained in:
Christomatt
2026-01-27 03:38:54 +01:00
parent fb2c80b0e3
commit f0253581d2
11 changed files with 969 additions and 306 deletions
+16 -3
View File
@@ -71,10 +71,23 @@ export async function GET(request: NextRequest) {
const messagesWithDecryption = messages.map((msg) => {
const isSentByMe = msg.senderHandle === session.user.handle;
// Return the appropriate encrypted content:
// - For sent messages: use senderEncryptedContent (encrypted with sender's key)
// - For received messages: use encryptedContent (encrypted with recipient's key)
const encryptedForViewer = isSentByMe
? (msg.senderEncryptedContent || msg.encryptedContent)
: msg.encryptedContent;
return {
...msg,
// Only include encrypted content - client will decrypt
content: undefined, // Remove plaintext
id: msg.id,
senderHandle: msg.senderHandle,
senderDisplayName: msg.senderDisplayName,
senderAvatarUrl: msg.senderAvatarUrl,
senderPublicKey: msg.senderChatPublicKey, // For E2E decryption
encryptedContent: encryptedForViewer,
deliveredAt: msg.deliveredAt,
readAt: msg.readAt,
createdAt: msg.createdAt,
isSentByMe,
};
});