Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mtxclient
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
mtxclient
Commits
c0a16c60
Commit
c0a16c60
authored
6 years ago
by
Konstantinos Sideris
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for megolm sessions locally
parent
2ce72cb8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
examples/crypto_bot.cpp
+2
-0
2 additions, 0 deletions
examples/crypto_bot.cpp
include/mtxclient/crypto/client.hpp
+3
-0
3 additions, 0 deletions
include/mtxclient/crypto/client.hpp
lib/crypto/client.cpp
+6
-0
6 additions, 0 deletions
lib/crypto/client.cpp
tests/e2ee.cpp
+80
-0
80 additions, 0 deletions
tests/e2ee.cpp
with
91 additions
and
0 deletions
examples/crypto_bot.cpp
+
2
−
0
View file @
c0a16c60
...
...
@@ -475,6 +475,7 @@ keys_uploaded_cb(const mtx::responses::UploadKeys &, RequestErr err)
return
;
}
olm_client
->
mark_keys_as_published
();
console
->
info
(
"keys uploaded"
);
}
...
...
@@ -852,6 +853,7 @@ login_cb(const mtx::responses::Login &, RequestErr err)
return
;
}
olm_client
->
mark_keys_as_published
();
console
->
info
(
"keys uploaded"
);
console
->
debug
(
"starting initial sync"
);
...
...
This diff is collapsed.
Click to expand it.
include/mtxclient/crypto/client.hpp
+
3
−
0
View file @
c0a16c60
...
...
@@ -180,6 +180,9 @@ public:
//! Generate the json structure for the signed one time key.
json
signed_one_time_key_json
(
const
std
::
string
&
key
,
const
std
::
string
&
signature
);
//! Marks the current set of one time keys as being published.
void
mark_keys_as_published
();
//! Prepare request for the /keys/upload endpoint by signing identity & one time keys.
mtx
::
requests
::
UploadKeys
create_upload_keys_request
(
const
OneTimeKeys
&
keys
);
mtx
::
requests
::
UploadKeys
create_upload_keys_request
();
...
...
This diff is collapsed.
Click to expand it.
lib/crypto/client.cpp
+
6
−
0
View file @
c0a16c60
...
...
@@ -142,6 +142,12 @@ OlmClient::signed_one_time_key_json(const std::string &key, const std::string &s
{
"signatures"
,
{{
user_id_
,
{{
"ed25519:"
+
device_id_
,
signature
}}}}}};
}
void
OlmClient
::
mark_keys_as_published
()
{
olm_account_mark_keys_as_published
(
account_
.
get
());
}
mtx
::
requests
::
UploadKeys
OlmClient
::
create_upload_keys_request
()
{
...
...
This diff is collapsed.
Click to expand it.
tests/e2ee.cpp
+
80
−
0
View file @
c0a16c60
...
...
@@ -559,3 +559,83 @@ TEST(Encryption, OlmSessions)
auto
body_str
=
std
::
string
((
char
*
)
decrypted
.
data
(),
decrypted
.
size
());
ASSERT_EQ
(
body_str
,
plaintext
);
}
TEST
(
Encryption
,
MegolmSessions
)
{
auto
alice
=
std
::
make_shared
<
OlmClient
>
();
alice
->
create_new_account
();
alice
->
generate_one_time_keys
(
1
);
auto
bob
=
std
::
make_shared
<
OlmClient
>
();
bob
->
create_new_account
();
bob
->
generate_one_time_keys
(
1
);
// Alice wants to send an encrypted megolm message to Bob.
const
std
::
string
secret_message
=
"Hey, Bob!"
;
// Alice creates an outbound megolm session that will be used by both parties.
auto
outbound_megolm_session
=
alice
->
init_outbound_group_session
();
auto
msg_index
=
olm_outbound_group_session_message_index
(
outbound_megolm_session
.
get
());
ASSERT_EQ
(
msg_index
,
0
);
// Alice extracts the session id & session key so she can share them with Bob.
const
auto
session_id
=
mtx
::
crypto
::
session_id
(
outbound_megolm_session
.
get
());
const
auto
session_key
=
mtx
::
crypto
::
session_key
(
outbound_megolm_session
.
get
());
// Encrypt the message using megolm.
auto
encrypted_secret_message
=
alice
->
encrypt_group_message
(
outbound_megolm_session
.
get
(),
secret_message
);
msg_index
=
olm_outbound_group_session_message_index
(
outbound_megolm_session
.
get
());
ASSERT_EQ
(
msg_index
,
1
);
// First she will create an outbound olm session so she can share the session data.
// Alice will need Bob's curve25519 key and one claimed one time key.
auto
outbound_olm_session
=
alice
->
create_outbound_session
(
bob
->
identity_keys
().
curve25519
,
bob
->
one_time_keys
().
curve25519
.
begin
()
->
second
);
const
auto
msg_type
=
olm_encrypt_message_type
(
outbound_olm_session
.
get
());
// Plaintext version of the session data to be shared.
const
auto
session_data
=
json
{{
"session_id"
,
session_id
},
{
"session_key"
,
session_key
}};
// Alice encrypts the session data using olm.
const
auto
encrypted_session_data
=
alice
->
encrypt_message
(
outbound_olm_session
.
get
(),
session_data
.
dump
());
const
auto
encrypted_session_data_str
=
std
::
string
((
char
*
)
encrypted_session_data
.
data
(),
encrypted_session_data
.
size
());
//
// Alice sends the olm & megolm messages to Bob ...
//
// Bob creates an inbound olm session to receive the session data.
auto
inbound_olm_session
=
bob
->
create_inbound_session
(
encrypted_session_data
);
// and validates that the message was indeed from Alice.
ASSERT_EQ
(
1
,
matches_inbound_session_from
(
inbound_olm_session
.
get
(),
alice
->
identity_keys
().
curve25519
,
encrypted_session_data_str
));
// Bob decrypts the encrypted olm message.
auto
plaintext_session_data
=
bob
->
decrypt_message
(
inbound_olm_session
.
get
(),
msg_type
,
encrypted_session_data_str
);
auto
session_str_data
=
json
::
parse
(
std
::
string
((
char
*
)
plaintext_session_data
.
data
(),
plaintext_session_data
.
size
()));
// Validate that the output matches the input.
ASSERT_EQ
(
session_id
,
session_str_data
.
at
(
"session_id"
).
get
<
std
::
string
>
());
ASSERT_EQ
(
session_key
,
session_str_data
.
at
(
"session_key"
).
get
<
std
::
string
>
());
// Bob will use the session_key to create an inbound megolm session.
// The session_id will be used to map future messages to this session.
auto
inbound_megolm_session
=
bob
->
init_inbound_group_session
(
session_key
);
// Bob can finally decrypt Alice's original message.
auto
ciphertext
=
std
::
string
((
char
*
)
encrypted_secret_message
.
data
(),
encrypted_secret_message
.
size
());
auto
bob_plaintext
=
bob
->
decrypt_group_message
(
inbound_megolm_session
.
get
(),
ciphertext
);
auto
output_str
=
std
::
string
((
char
*
)
bob_plaintext
.
data
.
data
(),
bob_plaintext
.
data
.
size
());
ASSERT_EQ
(
output_str
,
secret_message
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment