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

State properties #185

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 146 additions & 1 deletion cmd/interop/src/mls_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,25 @@ MLSClientImpl::JoinGroup(ServerContext* /* context */,
return catch_wrap([=]() { return join_group(request, response); });
}

Status
MLSClientImpl::ExternalJoin(ServerContext* /* context */,
const ExternalJoinRequest* request,
ExternalJoinResponse* response)
{
return catch_wrap([=]() { return external_join(request, response); });
}

// Access information from a group state
Status
MLSClientImpl::PublicGroupState(ServerContext* /* context */,
const PublicGroupStateRequest* request,
PublicGroupStateResponse* response)
{
return state_wrap(request, [=](auto& state) {
return public_group_state(state, request, response);
});
}

Status
MLSClientImpl::StateAuth(ServerContext* /* context */,
const StateAuthRequest* request,
Expand All @@ -124,6 +142,33 @@ MLSClientImpl::StateAuth(ServerContext* /* context */,
request, [=](auto& state) { return state_auth(state, request, response); });
}

Status
MLSClientImpl::Export(ServerContext* /* context */,
const ExportRequest* request,
ExportResponse* response)
{
return state_wrap(
request, [=](auto& state) { return do_export(state, request, response); });
}

Status
MLSClientImpl::Protect(ServerContext* /* context */,
const ProtectRequest* request,
ProtectResponse* response)
{
return state_wrap(
request, [=](auto& state) { return protect(state, request, response); });
}

Status
MLSClientImpl::Unprotect(ServerContext* /* context */,
const UnprotectRequest* request,
UnprotectResponse* response)
{
return state_wrap(
request, [=](auto& state) { return unprotect(state, request, response); });
}

// Operations using a group state
Status
MLSClientImpl::AddProposal(ServerContext* /* context */,
Expand Down Expand Up @@ -154,6 +199,16 @@ MLSClientImpl::HandleCommit(ServerContext* /* context */,
});
}

Status
MLSClientImpl::HandleExternalCommit(ServerContext* /* context */,
const HandleExternalCommitRequest* request,
HandleExternalCommitResponse* response)
{
return state_wrap(request, [=](auto& state) {
return handle_external_commit(state, request, response);
});
}

// Cached join transactions
uint32_t
MLSClientImpl::store_join(mls::HPKEPrivateKey&& init_priv,
Expand Down Expand Up @@ -345,7 +400,7 @@ MLSClientImpl::create_group(const CreateGroupRequest* request,
mls::KeyPackage(cipher_suite, init_priv.public_key, cred, sig_priv, {});

auto state =
mls::State(group_id, cipher_suite, init_priv, sig_priv, key_package);
mls::State(group_id, cipher_suite, init_priv, sig_priv, key_package, {});
auto state_id = store_state(std::move(state), request->encrypt_handshake());

response->set_state_id(state_id);
Expand Down Expand Up @@ -393,7 +448,41 @@ MLSClientImpl::join_group(const JoinGroupRequest* request,
return Status::OK;
}

Status
MLSClientImpl::external_join(const ExternalJoinRequest* request,
ExternalJoinResponse* response)
{
auto pgs_data = string_to_bytes(request->public_group_state());
auto pgs = tls::get<mls::PublicGroupState>(pgs_data);

auto init_priv = mls::HPKEPrivateKey::generate(pgs.cipher_suite);
auto sig_priv = mls::SignaturePrivateKey::generate(pgs.cipher_suite);
auto cred = mls::Credential::basic({}, sig_priv.public_key);
auto kp =
mls::KeyPackage(pgs.cipher_suite, init_priv.public_key, cred, sig_priv, {});

auto leaf_secret = mls::random_bytes(pgs.cipher_suite.secret_size());
auto [commit, state] =
mls::State::external_join(leaf_secret, sig_priv, kp, pgs);
auto commit_data = tls::marshal(commit);
auto state_id = store_state(std::move(state), request->encrypt_handshake());

response->set_state_id(state_id);
response->set_commit(bytes_to_string(commit_data));
return Status::OK;
}

// Access information from a group state
Status
MLSClientImpl::public_group_state(CachedState& entry,
const PublicGroupStateRequest* /* request */,
PublicGroupStateResponse* response)
{
auto pgs = tls::marshal(entry.state.public_group_state());
response->set_public_group_state(bytes_to_string(pgs));
return Status::OK;
}

Status
MLSClientImpl::state_auth(CachedState& entry,
const StateAuthRequest* /* request */,
Expand All @@ -404,6 +493,43 @@ MLSClientImpl::state_auth(CachedState& entry,
return Status::OK;
}

Status
MLSClientImpl::do_export(CachedState& entry,
const ExportRequest* request,
ExportResponse* response)
{
auto label = request->label();
auto context = string_to_bytes(request->context());
auto size = request->key_length();
auto secret = entry.state.do_export(label, context, size);
response->set_exported_secret(bytes_to_string(secret));
return Status::OK;
}

Status
MLSClientImpl::protect(CachedState& entry,
const ProtectRequest* request,
ProtectResponse* response)
{
auto pt = string_to_bytes(request->application_data());
auto ct = entry.state.protect(pt);
auto ct_data = tls::marshal(ct);
response->set_ciphertext(bytes_to_string(ct_data));
return Status::OK;
}

Status
MLSClientImpl::unprotect(CachedState& entry,
const UnprotectRequest* request,
UnprotectResponse* response)
{
auto ct_data = string_to_bytes(request->ciphertext());
auto ct = tls::get<mls::MLSCiphertext>(ct_data);
auto pt = entry.state.unprotect(ct);
response->set_application_data(bytes_to_string(pt));
return Status::OK;
}

// Operations on a running group
Status
MLSClientImpl::add_proposal(CachedState& entry,
Expand Down Expand Up @@ -506,3 +632,22 @@ MLSClientImpl::handle_commit(CachedState& entry,
response->set_state_id(next_id);
return Status::OK;
}

Status
MLSClientImpl::handle_external_commit(
CachedState& entry,
const HandleExternalCommitRequest* request,
HandleExternalCommitResponse* response)
{
auto commit_data = string_to_bytes(request->commit());
auto commit = tls::get<mls::MLSPlaintext>(commit_data);
auto should_be_next = entry.state.handle(commit);
if (!should_be_next) {
throw std::runtime_error("Commit failed to produce a new state");
}

auto& next = opt::get(should_be_next);
auto next_id = store_state(std::move(next), entry.encrypt_handshake);
response->set_state_id(next_id);
return Status::OK;
}
35 changes: 35 additions & 0 deletions cmd/interop/src/mls_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,26 @@ class MLSClientImpl final : public MLSClient::Service
Status JoinGroup(ServerContext* context,
const JoinGroupRequest* request,
JoinGroupResponse* response) override;
Status ExternalJoin(ServerContext* context,
const ExternalJoinRequest* request,
ExternalJoinResponse* response) override;

// Access information from a group state
Status PublicGroupState(ServerContext* context,
const PublicGroupStateRequest* request,
PublicGroupStateResponse* response) override;
Status StateAuth(ServerContext* context,
const StateAuthRequest* request,
StateAuthResponse* response) override;
Status Export(ServerContext* context,
const ExportRequest* request,
ExportResponse* response) override;
Status Protect(ServerContext* context,
const ProtectRequest* request,
ProtectResponse* response) override;
Status Unprotect(ServerContext* context,
const UnprotectRequest* request,
UnprotectResponse* response) override;

// Operations using a group state
Status AddProposal(ServerContext* context,
Expand All @@ -54,6 +69,9 @@ class MLSClientImpl final : public MLSClient::Service
Status HandleCommit(ServerContext* context,
const HandleCommitRequest* request,
HandleCommitResponse* response) override;
Status HandleExternalCommit(ServerContext* context,
const HandleExternalCommitRequest* request,
HandleExternalCommitResponse* response) override;

private:
// Wrapper for methods that rely on state
Expand Down Expand Up @@ -108,11 +126,25 @@ class MLSClientImpl final : public MLSClient::Service
CreateKeyPackageResponse* response);
Status join_group(const JoinGroupRequest* request,
JoinGroupResponse* response);
Status external_join(const ExternalJoinRequest* request,
ExternalJoinResponse* response);

// Access information from a group state
Status public_group_state(CachedState& entry,
const PublicGroupStateRequest* request,
PublicGroupStateResponse* response);
Status state_auth(CachedState& entry,
const StateAuthRequest* request,
StateAuthResponse* response);
Status do_export(CachedState& entry,
const ExportRequest* request,
ExportResponse* response);
Status protect(CachedState& entry,
const ProtectRequest* request,
ProtectResponse* response);
Status unprotect(CachedState& entry,
const UnprotectRequest* request,
UnprotectResponse* response);

// Operations on a running group
Status add_proposal(CachedState& entry,
Expand All @@ -124,4 +156,7 @@ class MLSClientImpl final : public MLSClient::Service
Status handle_commit(CachedState& entry,
const HandleCommitRequest* request,
HandleCommitResponse* response);
Status handle_external_commit(CachedState& entry,
const HandleExternalCommitRequest* request,
HandleExternalCommitResponse* response);
};