Repository navigation
Conversation
raphaelrobert
left a comment
There was a problem hiding this comment.
Thanks for the PR! Looks good in general.
My take on the question of what the key store should store: It should only store the "init keys" used for new groups. The key packages that are generated for updates in a group should not leave the group (there is no use for them outside of it). The ManagedGroup also knows best how to handle their lifecycle.
| pub(crate) fn _key_package_bundle( | ||
| &self, | ||
| kp_hash: &[u8], | ||
| ) -> Result<&KeyPackageBundle, KeyStoreError> { |
There was a problem hiding this comment.
I'm thinking this should return the value and not just a reference, otherwise we might keep around key package bundles even though they are no longer needed (which is bad for FS).
| /// refactored to use the key store for KeyPackageBundles and thus in tests | ||
| /// we need to access the credential bundle to create KeyPackageBundles | ||
| /// ad-hoc. | ||
| pub fn credential_bundle( |
There was a problem hiding this comment.
Going forward it might be enough to expose the .sign() function instead of the whole bundle, but no objections for now.
There was a problem hiding this comment.
We're essentially doing that already. Functionally, we're already doing that, since the only thing you can do with a CredentialBundle besides looking at the Credential is using sign.
|
|
||
| /// Generate a fresh `CredentialBundle` with the given parameters and store | ||
| /// it in the `KeyStore`. Returns the corresponding `Credential`. | ||
| pub fn fresh_credential( |
There was a problem hiding this comment.
For both functions I would use generate_ as a prefix instead of fresh_.
| signature_scheme: SignatureScheme, | ||
| ) -> Result<Credential, KeyStoreError> { | ||
| let cb = CredentialBundle::new(identity, credential_type, signature_scheme)?; | ||
| let credential = cb.credential().clone(); |
There was a problem hiding this comment.
Do we really need to clone here and return the value (same for key packages)? Most likely we will just serialise it shortly after, which works with a reference as well.
There was a problem hiding this comment.
Done! Note, that that's not the case for most of the tests, where I now have to clone, but you're right in that it should save us a clone in most non-test cases.
| pub struct KeyStore { | ||
| // Map from signature public keys to credential bundles | ||
| credentials: HashMap<SignaturePublicKey, CredentialBundle>, | ||
| key_packages: HashMap<Vec<u8>, KeyPackageBundle>, |
There was a problem hiding this comment.
Do the HashMaps really need a value for the key or could this work with a reference as well?
There was a problem hiding this comment.
I could use a reference, but which one can I use without getting into trouble lifetime-wise? As far as I can see, I can't use a reference to the value I'm storing, because I need the reference at the moment I'm storing the value. This doesn't work, because I'm moving the value in the process, making the reference invalid. Was that what you had in mind?
There was a problem hiding this comment.
No, I just meant for the key. The value has to be a value of course, but the key doesn't get stored in a HashMap it just gets hashed and it shouldn't be a lifetime issue.
There was a problem hiding this comment.
I see what you mean, but I'm not sure it's worth it. This would mean that the hashes of the key packages we store have to live as long as the KeyStore object. When we generate a KPB, the hash of the key package has to be computed locally. It's not part of the key package and will then go out of scope, which causes a lifetime issue. It might be possible, but I honestly just don't really see a good way to do it.
There was a problem hiding this comment.
I'm not sure the lifetime issues you describe would occur. I think HashMap will not take ownership here.
There was a problem hiding this comment.
Should we pair up for this? I've tried and I keep running into issues.
There was a problem hiding this comment.
It appears that the HashMap really needs ownership of the key that you pass in insert. And if you pass a reference, it needs to stay alive at least as long as the HasMap itself. This seems to have been a design decision. There might be a data structure that doesn't do this, but I'm not sure it would be worth using something non-standard here.
See also: rust-lang/rust#44271
In particular: - rename get and insert functions - return a KPB instead of a KPB reference when getting KPBs from the store - return references to KPs and Credentials when storing KPBs or CBs
kkohbrok
left a comment
There was a problem hiding this comment.
Thanks for the review! I've implemented the requested changes and also prefixed the names of the "getter" functions wit get_. It's a bit unintuitive that the KeyPackageBundle getter removes the KPB from the store while the CredentialBundle getter doesn't. I'd like to reflect that in the names, but I'm not sure what's the convention here. Maybe fetch_ or remove_ as prefix instead of get_?
| /// refactored to use the key store for KeyPackageBundles and thus in tests | ||
| /// we need to access the credential bundle to create KeyPackageBundles | ||
| /// ad-hoc. | ||
| pub fn credential_bundle( |
There was a problem hiding this comment.
We're essentially doing that already. Functionally, we're already doing that, since the only thing you can do with a CredentialBundle besides looking at the Credential is using sign.
| pub struct KeyStore { | ||
| // Map from signature public keys to credential bundles | ||
| credentials: HashMap<SignaturePublicKey, CredentialBundle>, | ||
| key_packages: HashMap<Vec<u8>, KeyPackageBundle>, |
There was a problem hiding this comment.
I could use a reference, but which one can I use without getting into trouble lifetime-wise? As far as I can see, I can't use a reference to the value I'm storing, because I need the reference at the moment I'm storing the value. This doesn't work, because I'm moving the value in the process, making the reference invalid. Was that what you had in mind?
| signature_scheme: SignatureScheme, | ||
| ) -> Result<Credential, KeyStoreError> { | ||
| let cb = CredentialBundle::new(identity, credential_type, signature_scheme)?; | ||
| let credential = cb.credential().clone(); |
There was a problem hiding this comment.
Done! Note, that that's not the case for most of the tests, where I now have to clone, but you're right in that it should save us a clone in most non-test cases.
| pub(crate) fn _key_package_bundle( | ||
| &self, | ||
| kp_hash: &[u8], | ||
| ) -> Result<&KeyPackageBundle, KeyStoreError> { |
|
|
||
| /// Generate a fresh `CredentialBundle` with the given parameters and store | ||
| /// it in the `KeyStore`. Returns the corresponding `Credential`. | ||
| pub fn fresh_credential( |
franziskuskiefer
left a comment
There was a problem hiding this comment.
Thanks for starting this! A couple thoughts.
|
|
||
| impl Eq for SignaturePublicKey { | ||
| fn assert_receiver_is_total_eq(&self) {} | ||
| } |
There was a problem hiding this comment.
This shoudl be onlye impl Eq for SignaturePublicKey {}. But really all of this can be derived, including PartialEq above.
There was a problem hiding this comment.
All of the traits are now derived. Note, that this means that for two SignaturePublicKey instances to be equal, both the public key and their SignatureScheme have to be equal.
| pub use errors::KeyStoreError; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| /// The `KeyStore` contains private key material of `Credentials` and |
There was a problem hiding this comment.
Fixed the documentation to reflect the fact that the whole KPB/CB is stored, not only the private key material.
| /// corresponding to the given `KeyPackage` hash. TODO: This is not in use | ||
| /// yet, because the groups are not yet refactored to use the key store for | ||
| /// KeyPackageBundles. | ||
| pub(crate) fn _get_key_package_bundle( |
There was a problem hiding this comment.
What's the rationale behind removing a KeyPackageBundle immediately when reading it? This requires to either keep track of removed key packages or perform a write (deletion) on the permanent storage on every read (which might not be desirable).
And again is the question who owns the key package bundle after this.
There was a problem hiding this comment.
This came from #334 (comment). The idea is that the key package is used in the creation of a group, after which it lives on in the group state. This will change eventually with the evolution of the key store (see above or #337).
| pub fn get_credential_bundle( | ||
| &self, | ||
| cred_pk: &SignaturePublicKey, | ||
| ) -> Result<&CredentialBundle, KeyStoreError> { |
There was a problem hiding this comment.
So in this function the return value is only a reference to the value in the key store. This is inconsistent. The questions as above where the ownership is transferred apply here.
There was a problem hiding this comment.
Right. The reason is that we expect CredentialBundles to be used across groups, while KeyPackageBundles are currently consumed when creating a new group. I expect this to change in the future, when KeyPackageBundles don't live in their groups anymore. Until then, this at least enables the creation of multiple groups with the same CredentialBundle.
| ciphersuite.signature_scheme(), | ||
| ) | ||
| .unwrap(); | ||
| let mut key_store = KeyStore::default(); |
There was a problem hiding this comment.
Not using Cell or something in the KeyStore of course requires you to have a mutable store. It's not used a lot but it's of course something that later needs refactoring. (Might be something to change from the beginning to safe us some refactoring.)
There was a problem hiding this comment.
Right. I'm in the process of finding a good solution for this. The goal of the next PR is to allow access via non-mutable references, such that we can actually use the KeyPackageBundle storage functionality.
| sender: &Credential, | ||
| added_member: &Credential, | ||
| ) { | ||
| let own_identity = match managed_group.credential() { |
| // CredentialBundle used to sign messages | ||
| credential_bundle: &'a CredentialBundle, | ||
| // Reference to the KeyStore to obtain private key material. | ||
| key_store: &'a KeyStore, |
There was a problem hiding this comment.
This implies that the KeyStore is owned by the client (however that looks like) and there's only one key store? This should also be clarified in the KeyStore module docs.
There was a problem hiding this comment.
Added a sentence for clarification in the KeyStore docs.
kkohbrok
left a comment
There was a problem hiding this comment.
Thanks for the review! I've tried to answer/address all the comments. I've also added a few simple tests for generation/loading of KPBs and CBs.
| // CredentialBundle used to sign messages | ||
| credential_bundle: &'a CredentialBundle, | ||
| // Reference to the KeyStore to obtain private key material. | ||
| key_store: &'a KeyStore, |
There was a problem hiding this comment.
Added a sentence for clarification in the KeyStore docs.
| pub struct KeyStore { | ||
| // Map from signature public keys to credential bundles | ||
| credentials: HashMap<SignaturePublicKey, CredentialBundle>, | ||
| key_packages: HashMap<Vec<u8>, KeyPackageBundle>, |
There was a problem hiding this comment.
Should we pair up for this? I've tried and I keep running into issues.
|
Updating the Key Store to use RefCells was not that much refactoring after all an it's now included in this PR. As a consequence, I could simplify some tests (including the managed group testing framework) and get rid of a number of I've also changed the |
|
I've updated the KeyStore to use |
| /// `KeyPackage` instances. | ||
| pub struct KeyStore { | ||
| // Map from signature public keys to credential bundles | ||
| credential_bundles: RwLock<HashMap<SignaturePublicKey, RwLock<CredentialBundle>>>, |
There was a problem hiding this comment.
So something that gives you a lot of trouble is the value in the hash map here. Why do you need to have an RwLock on it?
There was a problem hiding this comment.
Excellent question! It used to be a RefCell, because at some point, I think I convinced myself that we needed another layer of interior mutability here. But on further inspection, I think it might not actually be needed. I've removed it and it turns out it's much nicer without it 🙂
Thanks for pointing this out!
| //! .generate_credential( | ||
| //! "Alice".into(), | ||
| //! CredentialType::Basic, | ||
| //! SignatureScheme::ED25519, |
There was a problem hiding this comment.
Would be nice if we could use the ciphersuite_name here as well.
There was a problem hiding this comment.
We can and I changed it so that we do.
| //! A `KeyStore` is meant to be used across multiple `ManagedGroup` instances to | ||
| //! allow sharing the same `CredentialBundle`. If this is not desired, multiple | ||
| //! `KeyStore` instances can be used across groups. | ||
| //! |
There was a problem hiding this comment.
So what I'm missing here is the high level idea of which parts are stored, what's returned as a reference, what's returned as a copy (and why each time).
There was a problem hiding this comment.
I've extended the documentation to hopefully provide some clarity on this.
There was a problem hiding this comment.
Can you also add explanations for generate_key_package and generate_credential?
| //! `CredentialBundle` instances are only accessible via reference, so that if a | ||
| //! `KeyStore` is used in multiple groups, those groups can also share a | ||
| //! `CredentialBundle`. |
There was a problem hiding this comment.
While this generally makes sense it introduces threading issues now when we consider the key store to be used across multiple threads in parallel. So my question would be if it could be easier to hand out copies to avoid locking the other groups with the same credential bundle. This of course leads to the question what happens when the credential bundle changes/gets deleted. This would have to be handled explicitly through key store owner (the client). But I think the client has to become active in this case anyway.
There was a problem hiding this comment.
Generally, groups will only ever read from a CredentialBundle, so RwLock should allow parallel access to the same credential without issues. This leads to your question regarding updating/deleting of a credential. I don't think Credentials or CredentialBundles should ever be updated, but only deleted. On an MLS level, the credential is part of the KeyPackage and can thus only be "changed" (i.e. replaced by a fresh one) by doing an Update. As the API stands right now, this requires client activity to intiate it.
So the lifecycle of a CredentialBundle would look as follows:
- it gets created
- the client decides to use it in one or more groups by creating KeyPackageBundles with that CredentialBundle and updating the groups with these KeyPackageBundles.
- potentially, the client publishes some KeyPackageBundles created with the CredentialBundle and thus potentially joins new groups with them
- if the client wants to at some point delete the CredentialBundle, it first has to create a new one and replace KeyPackages that use them. This includes KeyPackages the client previously published, but also the ones that are in use in various groups. If the client wants to be totally safe, it has to wait until all KeyPackages that include the CredentialBundle have expired (as others might have downloaded them from the server before the update and still plan to use them).
This raises the question of who keeps track of how/where a CredentialBundle is used and when KeyPackageBundles that use it expire. If at some point the KeyStore is responsible for generation of all of the KeyPackageBundles (not just the "init" ones as is now the case), it could keep track of expiration and use. But that's not the case just yet.
There was a problem hiding this comment.
I agree that there should be no modification but only delete and create.
Generally, groups will only ever read from a CredentialBundle, so RwLock should allow parallel access to the same credential without issues.
Right, but my understanding of the locking you do here is that the entire hashmap gets locked. This way any write operation to the hashmap (in future this might also include any read from something that's permanently stored and then cached in the hashmap) would block a read. Or do I misunderstand the design?
This raises the question of who keeps track of how/where a CredentialBundle is used and when KeyPackageBundles that use it expire.
Right, I think this is the main question. I currently expect this to be the client or the key store. We don't have to decide right now but we should make sure the API here is good for any route we might want to take or can be easily upgraded. And we should document all this here.
There was a problem hiding this comment.
I've updated #337 to document the plans and also added a short "Future work" section to the end of the module documentation.
| //! `take_key_package_bundle`. This is because each `ManagedGroup` owns the | ||
| //! `KeyPackageBundle` in its leaf, so upon creation of the group, it needs to |
There was a problem hiding this comment.
So this is what we do right now. But I'm not convinced yet that it's something we should be doing. This is why I'd like us to have an upgrade path from here and also describe it. I would expect us to deprecate the take_ function here and instead introduce a get_ function that is used when starting the client such that the ManagedGroup doesn't need to persist anything here.
There was a problem hiding this comment.
You're right. That was one of the questions that were left after our last discussion: Who should persist KeyPackages that are allocated to an individual group? Should it be the group (probably instructing the KeyStore to do it) or should it be the KeyStore directly?
Having written the answer to your other question, I now believe that it should be the KeyStore that holds (and persists) all the KeyPackageBundles and not the groups. This would allow it to keep track of expirations and active use of KeyPackageBundles and thus prevent the deletion of CredentialBundles if they are potentially still in use somewhere.
There was a problem hiding this comment.
I now believe that it should be the KeyStore that holds (and persists) all the KeyPackageBundles and not the groups
Yep, that's where I think we have to end up. But as I said I think we have this upgrade path. But we should also document where we (probably) want to go here.
| //! A `KeyStore` is meant to be used across multiple `ManagedGroup` instances to | ||
| //! allow sharing the same `CredentialBundle`. If this is not desired, multiple | ||
| //! `KeyStore` instances can be used across groups. | ||
| //! |
There was a problem hiding this comment.
Can you also add explanations for generate_key_package and generate_credential?
franziskuskiefer
left a comment
There was a problem hiding this comment.
I think we're getting there 🙂
| pub fn take_key_package_bundle(&self, kp_hash: &[u8]) -> Option<KeyPackageBundle> { | ||
| // We unwrap here, because the two functions claiming write locks (this | ||
| // one and `generate_key_package`) only hold the lock very briefly and | ||
| // should not panic during that period. |
There was a problem hiding this comment.
Hm, "should not panic" is very optimistic... We should handle all the potential locks in these functions somehow. Or add it as a todo. But this has huge potential to create bugs that are impossible to debug.
There was a problem hiding this comment.
Yes, I wasn't sure what to write here and I agree that "should not panic" is very optimistic. The way I understand poisoning is that something is considered "poisoned" if a piece of code panics while holding a write lock. This means that as long as we thoroughly check what is going on while we hold a write lock, we should be ok. It's a fine line, but I would argue that it's doable, since the only thing we (currently) do is taking the write lock, inserting or removing a value and then immediately dropping it again. Of course, this is dangerous, because adding new code could break this pattern. That being said, I still believe that if we're careful here, unwrapping should be fine.
| /// The `KeyStore` contains `CredentialBundle`s and `KeyPackageBundle`s and | ||
| /// makes them available via information in the corresponding `Credential` and | ||
| /// `KeyPackage` instances. | ||
| pub struct KeyStore { |
There was a problem hiding this comment.
Looks like we currently don't have a way to actually delete anything here? I think we can go without in the first version but should document it. Because this will also be necessary for testing of all the async things it would be nice to have it earlier. But I'm fine without in the first version.
There was a problem hiding this comment.
Yes, I've not added that yet. I thought I'd add it once we have a way to keep track of Credential usage, to make sure that we can safely delete CredentialBundle instances. Not sure when that is going to happen, though.
| signature_scheme: SignatureScheme, | ||
| ) -> Result<Credential, KeyStoreError> { | ||
| let cb = CredentialBundle::new(identity, credential_type, signature_scheme)?; | ||
| let signature_key = cb.credential().signature_key().clone(); |
There was a problem hiding this comment.
Why all this cloning? Appears to be a little excessive.
There was a problem hiding this comment.
I believe we have to clone here at least once, because right afterwards, the CredentialBundle is consumed by insert. I've refactored a bit to get rid of a clone. Both generate_ functions currently return a full Credential/KeyPackage instance. We could also try returning a reference, but that would require another Guard struct and it would keep a read lock on the corresponding HashMap. What do you think? I think a clone is ok here.
Co-authored-by: Franziskus Kiefer <[email protected]>
…nto konrad/simple_keystore
This PR introduces a simple key store implementation to OpenMLS and replaces the reference to a
CredentialBundlein aManagedGroupwith a reference to aKeyStore.The
KeyStoreis currently a very simple design that storesCredentialBundles andKeyPackageBundles. It provides functions to generate fresh instances of both, returning theCredentialorKeyPackageBundlerespectively, as well as the retrieval ofCredentialBundles given theCredential'sSignaturePublicKeyand ofKeyPackageBundles given theKeyPackage's hash.In
ManagedGroup, theCredentialBundlereference is only replaces in a very shallow way, i.e. as soon as a function requires theCredentialBundle, the group looks up its own credential in the public tree via itsown_node_indexand uses theSignaturePublicKeyof that credential to look up theCredentialBundlein theKeyStore. In the process, I also removed the derivation ofPartialEqforManagedGroup. We can still to something very similar by comparing public tree and exporter key. Alternatively, we could implementPartialEqmanually.Since the key package (and thus the credential) in the own leaf in the public tree gets blanked or replaced when oneself is removed,
self.credential()now can return an error, which gets triggered, if it is used after one was removed from the group. This caused changes primarily in callback functions and event listeners in test functions.One thing I didn't quite know what to do with is the
PoisonErrors. Ideally, only theKeyStorewould take write locks, however, the getter forCredentialBundles has to return anRwLock(or at least that's the only way I could make it work, the Problem is the same as with the original double-RefCellapproach). This means that outside functions could potentially changeCredentialBundles, which is bad from a poison-control standpoint, but also more generally from a design standpoint. @franziskuskiefer any idea how to work around this?