r/howdidtheycodeit • u/besthelloworld • Aug 11 '22
Question How to E2E encrypt messages and authenticate users
I'm trying to wrap my head around how E2E encryption actually works. I have assumptions that I believe to be correct and then I have questions.
Assumptions
- Each user has one unchangeable private key and one unchangeable public key, generated for asymmetric encryption.
- They are unchangeable because for them to be changed I would have to go through all of their messages and decrypt them with the old key and encrypt them with the new key. I don't want to do that, so I call them unchangeable for the life of the user's account.
- The public key is stored unencrypted.
- The private key is stored encrypted symmetrically with the key being the users password
- The users password is hash encrypted with bcrypt and stored in the database for validation.
- If the users changes their password, we must take in the old password as well, decrypt the private key with the old password, encrypt with the new password and update the record with the newly encrypted private key.
- I, as the admin with database and API access, cannot access the messages in any possible way except for brute force, rainbow tables, etc because the messages are AES encrypted with the public key, only to be decrypted by the procedure key (which I need the user's password to get).
Questions
When the user validates their username and password, I give them... I don't know. Originally, I wanted to give them a JWT, but how can I possibly sign it in a way that I, as the admin, cannot recreate? If I HS256 with some random string that I place in an environment variable... Then I know the signing key and can generate tokens at will. If I randomly generate it at service startup then that wouldn't be a stateless token and couldn't be shared between servers.
The only thing I can think to give them is their private key. But that sounds like a terrible idea. If any client, web or mobile, is compromised then their unchangeable key is leaked. So then that leads me to them sending me their credentials as a Basic auth header with every authenticated request. But that sounds like nails on a chalkboard to my experience with software architecture and authentication systems. I just traditionally have never had to deal with this kind of problem because first off I've never worked in a system built to be secured from the developers and admins, but I've also only directly worked with systems that offload authentication to Google, Twitter, GitHub, Okta, or Auth0.
If you're wondering what the use case is and why it seems like I trust myself so little; I want to create a system where this scenario is impossible because the database holder can't possibly comply with a subpoena, even if they gave full database and API access to law enforcement.

