Skip to content

Replace the CredentialBundle reference in ManagedGroup with a reference to a simple KeyStore - #334

Closed
kkohbrok wants to merge 50 commits into
mainfrom
konrad/simple_keystore
Closed

kkohbrok wants to merge 50 commits into
mainfrom
konrad/simple_keystore

Conversation

@kkohbrok

@kkohbrok kkohbrok commented Feb 4, 2021 •

Copy link
Copy Markdown
Member

This PR introduces a simple key store implementation to OpenMLS and replaces the reference to a CredentialBundle in a ManagedGroup with a reference to a KeyStore.

The KeyStore is currently a very simple design that stores CredentialBundles and KeyPackageBundles. It provides functions to generate fresh instances of both, returning the Credential or KeyPackageBundle respectively, as well as the retrieval of CredentialBundles given the Credential's SignaturePublicKey and of KeyPackageBundles given the KeyPackage's hash.

In ManagedGroup, the CredentialBundle reference is only replaces in a very shallow way, i.e. as soon as a function requires the CredentialBundle, the group looks up its own credential in the public tree via its own_node_index and uses the SignaturePublicKey of that credential to look up the CredentialBundle in the KeyStore. In the process, I also removed the derivation of PartialEq for ManagedGroup. We can still to something very similar by comparing public tree and exporter key. Alternatively, we could implement PartialEq manually.

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 the KeyStore would take write locks, however, the getter for CredentialBundles has to return an RwLock (or at least that's the only way I could make it work, the Problem is the same as with the original double-RefCell approach). This means that outside functions could potentially change CredentialBundles, which is bad from a poison-control standpoint, but also more generally from a design standpoint. @franziskuskiefer any idea how to work around this?

@kkohbrok kkohbrok self-assigned this Feb 4, 2021
@github-actions github-actions Bot added the size/m label Feb 4, 2021
@github-actions github-actions Bot added size/l and removed size/m labels Feb 4, 2021
@kkohbrok
kkohbrok marked this pull request as ready for review February 4, 2021 14:42

@raphaelrobert raphaelrobert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated
pub(crate) fn _key_package_bundle(
&self,
kp_hash: &[u8],
) -> Result<&KeyPackageBundle, KeyStoreError> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Comment thread src/key_store/mod.rs Outdated
/// 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(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Going forward it might be enough to expose the .sign() function instead of the whole bundle, but no objections for now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated

/// Generate a fresh `CredentialBundle` with the given parameters and store
/// it in the `KeyStore`. Returns the corresponding `Credential`.
pub fn fresh_credential(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For both functions I would use generate_ as a prefix instead of fresh_.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Comment thread src/key_store/mod.rs
signature_scheme: SignatureScheme,
) -> Result<Credential, KeyStoreError> {
let cb = CredentialBundle::new(identity, credential_type, signature_scheme)?;
let credential = cb.credential().clone();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated
pub struct KeyStore {
// Map from signature public keys to credential bundles
credentials: HashMap<SignaturePublicKey, CredentialBundle>,
key_packages: HashMap<Vec<u8>, KeyPackageBundle>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do the HashMaps really need a value for the key or could this work with a reference as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure the lifetime issues you describe would occur. I think HashMap will not take ownership here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should we pair up for this? I've tried and I keep running into issues.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 kkohbrok left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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_?

Comment thread src/key_store/mod.rs Outdated
/// 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(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated
pub struct KeyStore {
// Map from signature public keys to credential bundles
credentials: HashMap<SignaturePublicKey, CredentialBundle>,
key_packages: HashMap<Vec<u8>, KeyPackageBundle>,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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?

Comment thread src/key_store/mod.rs
signature_scheme: SignatureScheme,
) -> Result<Credential, KeyStoreError> {
let cb = CredentialBundle::new(identity, credential_type, signature_scheme)?;
let credential = cb.credential().clone();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated
pub(crate) fn _key_package_bundle(
&self,
kp_hash: &[u8],
) -> Result<&KeyPackageBundle, KeyStoreError> {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Comment thread src/key_store/mod.rs Outdated

/// Generate a fresh `CredentialBundle` with the given parameters and store
/// it in the `KeyStore`. Returns the corresponding `Credential`.
pub fn fresh_credential(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

@franziskuskiefer franziskuskiefer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for starting this! A couple thoughts.

Comment thread src/ciphersuite/mod.rs Outdated

impl Eq for SignaturePublicKey {
fn assert_receiver_is_total_eq(&self) {}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This shoudl be onlye impl Eq for SignaturePublicKey {}. But really all of this can be derived, including PartialEq above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated
pub use errors::KeyStoreError;

#[derive(Debug, Default)]
/// The `KeyStore` contains private key material of `Credentials` and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why only private keys?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed the documentation to reflect the fact that the whole KPB/CB is stored, not only the private key material.

Comment thread src/key_store/mod.rs Resolved
Comment thread src/key_store/mod.rs Outdated
/// 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(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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).

Comment thread src/key_store/mod.rs Outdated
pub fn get_credential_bundle(
&self,
cred_pk: &SignaturePublicKey,
) -> Result<&CredentialBundle, KeyStoreError> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Resolved
Comment thread tests/test_managed_group.rs Outdated
ciphersuite.signature_scheme(),
)
.unwrap();
let mut key_store = KeyStore::default();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

duplicated code?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

// CredentialBundle used to sign messages
credential_bundle: &'a CredentialBundle,
// Reference to the KeyStore to obtain private key material.
key_store: &'a KeyStore,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a sentence for clarification in the KeyStore docs.

@kkohbrok kkohbrok left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a sentence for clarification in the KeyStore docs.

Comment thread src/key_store/mod.rs Resolved
Comment thread src/key_store/mod.rs Outdated
pub struct KeyStore {
// Map from signature public keys to credential bundles
credentials: HashMap<SignaturePublicKey, CredentialBundle>,
key_packages: HashMap<Vec<u8>, KeyPackageBundle>,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should we pair up for this? I've tried and I keep running into issues.

@kkohbrok

kkohbrok commented Feb 16, 2021 •

Copy link
Copy Markdown
Member Author

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 clone instances.

I've also changed the ManagedGroup interface slightly, as it's not necessary anymore to pass in a KeyPackageBundle when creating a new group from Welcome. Instead, the hash in the Welcome is used to retrieve the matching KPB from the Key Store.

@kkohbrok
kkohbrok marked this pull request as draft February 23, 2021 13:42
@kkohbrok

Copy link
Copy Markdown
Member Author

I've updated the KeyStore to use RwLock instead of RefCell, which creates the issue of poison-management (see PR description). We can go with this approach for now, but it's not great. I'd be grateful for ideas on how to improve this design.

@kkohbrok
kkohbrok marked this pull request as ready for review February 25, 2021 12:31
Comment thread src/key_store/mod.rs Outdated
/// `KeyPackage` instances.
pub struct KeyStore {
// Map from signature public keys to credential bundles
credential_bundles: RwLock<HashMap<SignaturePublicKey, RwLock<CredentialBundle>>>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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!

Comment thread src/key_store/mod.rs Outdated
//! .generate_credential(
//! "Alice".into(),
//! CredentialType::Basic,
//! SignatureScheme::ED25519,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be nice if we could use the ciphersuite_name here as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can and I changed it so that we do.

Comment thread src/key_store/mod.rs
//! 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.
//!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've extended the documentation to hopefully provide some clarity on this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add explanations for generate_key_package and generate_credential?

Comment thread src/key_store/mod.rs Outdated
Comment on lines +11 to +13
//! `CredentialBundle` instances are only accessible via reference, so that if a
//! `KeyStore` is used in multiple groups, those groups can also share a
//! `CredentialBundle`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've updated #337 to document the plans and also added a short "Future work" section to the end of the module documentation.

Comment thread src/key_store/mod.rs Outdated
Comment on lines +19 to +20
//! `take_key_package_bundle`. This is because each `ManagedGroup` owns the
//! `KeyPackageBundle` in its leaf, so upon creation of the group, it needs to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've updated #337 to document this plan.

Comment thread src/key_store/mod.rs
//! 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.
//!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add explanations for generate_key_package and generate_credential?

@franziskuskiefer franziskuskiefer linked an issue Feb 26, 2021 that may be closed by this pull request
3 of 8 tasks

@franziskuskiefer franziskuskiefer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we're getting there 🙂

Comment thread src/key_store/mod.rs Resolved
Comment thread src/key_store/mod.rs Outdated
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs
/// The `KeyStore` contains `CredentialBundle`s and `KeyPackageBundle`s and
/// makes them available via information in the corresponding `Credential` and
/// `KeyPackage` instances.
pub struct KeyStore {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/key_store/mod.rs Outdated
signature_scheme: SignatureScheme,
) -> Result<Credential, KeyStoreError> {
let cb = CredentialBundle::new(identity, credential_type, signature_scheme)?;
let signature_key = cb.credential().signature_key().clone();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why all this cloning? Appears to be a little excessive.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@raphaelrobert

Copy link
Copy Markdown
Member

@kkohbrok should we close this in favour of #385?

@kkohbrok

kkohbrok commented Apr 8, 2021

Copy link
Copy Markdown
Member Author

@kkohbrok should we close this in favour of #385?

Yes, I meant to do that. Thanks for the reminder!

@kkohbrok kkohbrok closed this Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Store key material in a Key Store

3 participants