Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pickling logic to pk_encryption #173

Closed
wants to merge 15 commits into from

Conversation

devonh
Copy link
Contributor

@devonh devonh commented Aug 23, 2024

This is an attempt to add pickling to the new pk_encryption work. #171
I am not very familiar with this codebase, but I took my best attempt at implementing the functionality based on how the legacy libolm Account pickling works.

The tests are passing against the olm_rs impl which gives me some confidence in it.

@poljar poljar force-pushed the poljar/pk-dekurcina branch 2 times, most recently from 6f97915 to c231c72 Compare September 2, 2024 14:15
This patch introduces support for the libolm PkEncryption/PkDecryption
concepts, ensuring bug-for-bug compatibility with libolm. Notably, the
libolm implementation has a known flaw that leaves ciphertext
unauthenticated, as documented in the Matrix spec [1]. To address this,
the feature is gated behind a feature flag to better inform users of
this issue.

[1]: https://spec.matrix.org/v1.11/client-server-api/#backup-algorithm-mmegolm_backupv1curve25519-aes-sha2

Changelog: Add support for the libolm PkEncryption feature. This allows
Matrix clients to implement the [m.megolm_backup.v1.curve25519-aes-sha2](https://spec.matrix.org/v1.11/client-server-api/#backup-algorithm-mmegolm_backupv1curve25519-aes-sha2)
room key backup algorithm. Please note that this algorithm contains a
critical flaw and should only be used for compatibility reasons.
Copy link
Collaborator

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some nits, and a question if one of the methods is even needed.

/// PicklingMode::Encrypted { key: [0u8; 32].to_vec() },
/// ).expect("We should be able to unpickle our exported PkDecryption");
/// ```
pub fn to_libolm_pickle(&self, pickle_key: &[u8]) -> Result<String, crate::LibolmPickleError> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, does anybody use that? We just need the seed for a Curve25519, AFAIK even in libolm days, Element Web didn't use this to store the key.

Persisting the public key is unnecessary, and then there's the whole key re-use trap this might propagate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently how Matrix Content Scanner saves it's keys. I have no objections to saving them in a different format going forward. I kept this in out of convenience to minimize changes in the content scanner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go ahead and remove the to_libolm_pickle function here 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure if removing this entirely will work. The Matrix Content Scanner backend relies on saving it's key locally in pickled form.

I could change this to be pickle and create a pickled PkDecryption which only contains the secret and omits the public key. Either way we will need something that allows us to create a new key and save it somewhere.

@poljar Is there a better way to go about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those would allow us to save the key yes. We would lose the additional encryption we get with the current approach though. (Since the libolm pickle is encrypted with an additional key)

I'm not sure if the encrypted pickle adds much, since the key for it is stored in the Content Scanner's config file.

Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you don't have encryption for storage. Let me sleep on that since it's quite late where I'm at.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuck it, let's keep it then. Somebody else might depend on this as well and then I'll have this conversation again if we don't keep it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the scanner itself it seems fine to me to just ignore the pickled key and create a new one, it would just break on update for a very small number of reqs that fetched the public key before the update, and it will just work on retry I believe (new public key will be fetch and used).

Copy link
Contributor

@MatMaul MatMaul Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the encrypted pickle adds much, since the key for it is stored in the Content Scanner's config file.

Agreed, it seems fairly useless to me.

But poljar point applies, now that it's done here we may as well merge it if others need it, it's not a lot of code.

Do we need to plug the pickle logic in the content scanner or just remove it altogether (and generate a new key on first start) is another story :)

src/pk_encryption.rs Outdated Show resolved Hide resolved
struct PkDecryptionPickle {
version: u32,
public_curve25519_key: [u8; 32],
private_curve25519_key: Box<[u8; 32]>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still found a small nit in the docs, though after that I think this is fine. We still need to wait for #171 to land though.

src/pk_encryption.rs Outdated Show resolved Hide resolved
Co-authored-by: Damir Jelić <poljar@termina.org.uk>
@poljar poljar force-pushed the poljar/pk-dekurcina branch 3 times, most recently from 805e657 to 484317b Compare September 11, 2024 09:34
@poljar poljar deleted the branch matrix-org:poljar/pk-dekurcina September 11, 2024 09:39
@poljar poljar closed this Sep 11, 2024
@poljar
Copy link
Collaborator

poljar commented Sep 11, 2024

Oh my, this got auto-closed because the base branch got auto-deleted. Could you reopen it with a different base?

@devonh
Copy link
Contributor Author

devonh commented Sep 11, 2024

Oh my, this got auto-closed because the base branch got auto-deleted. Could you reopen it with a different base?

haha yeah no problem. I'll open it against main now

@poljar
Copy link
Collaborator

poljar commented Sep 11, 2024

Oh my, this got auto-closed because the base branch got auto-deleted. Could you reopen it with a different base?

haha yeah no problem. I'll open it against main now

Thanks.

@devonh
Copy link
Contributor Author

devonh commented Sep 11, 2024

Oh my, this got auto-closed because the base branch got auto-deleted. Could you reopen it with a different base?

haha yeah no problem. I'll open it against main now

Thanks.

Hmm, it seems like I might not have enough permissions to be able to reopen a PR.
Are you able to reopen it?

Otherwise I have to create a whole new PR.

@poljar
Copy link
Collaborator

poljar commented Sep 11, 2024

I am not able to reopen it either 😅. I can't reopen it because the base branch was deleted and I can't change the base branch...

@devonh
Copy link
Contributor Author

devonh commented Sep 11, 2024

Haha what a pickle :P
I can just create a new PR then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants