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
755cc434
Commit
755cc434
authored
6 years ago
by
Konstantinos Sideris
Browse files
Options
Downloads
Patches
Plain Diff
Add methods for importing/exporting inbound megolm sessions
parent
3328fded
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/mtxclient/crypto/client.hpp
+9
-1
9 additions, 1 deletion
include/mtxclient/crypto/client.hpp
lib/crypto/client.cpp
+30
-0
30 additions, 0 deletions
lib/crypto/client.cpp
tests/e2ee.cpp
+59
-0
59 additions, 0 deletions
tests/e2ee.cpp
with
98 additions
and
1 deletion
include/mtxclient/crypto/client.hpp
+
9
−
1
View file @
755cc434
...
@@ -214,10 +214,18 @@ session_id(OlmSession *s);
...
@@ -214,10 +214,18 @@ session_id(OlmSession *s);
std
::
string
std
::
string
session_id
(
OlmOutboundGroupSession
*
s
);
session_id
(
OlmOutboundGroupSession
*
s
);
//! Retrieve the session key.
//! Retrieve the session key
from an *outbound* megolm session
.
std
::
string
std
::
string
session_key
(
OlmOutboundGroupSession
*
s
);
session_key
(
OlmOutboundGroupSession
*
s
);
//! Retrieve the session key from an *inbound* megolm session.
std
::
string
export_session
(
OlmInboundGroupSession
*
s
);
//! Create an *inbound* megolm session from an exported session key.
InboundGroupSessionPtr
import_session
(
const
std
::
string
&
session_key
);
bool
bool
matches_inbound_session
(
OlmSession
*
session
,
const
std
::
string
&
one_time_key_message
);
matches_inbound_session
(
OlmSession
*
session
,
const
std
::
string
&
one_time_key_message
);
...
...
This diff is collapsed.
Click to expand it.
lib/crypto/client.cpp
+
30
−
0
View file @
755cc434
...
@@ -437,6 +437,36 @@ mtx::crypto::session_key(OlmOutboundGroupSession *s)
...
@@ -437,6 +437,36 @@ mtx::crypto::session_key(OlmOutboundGroupSession *s)
return
std
::
string
(
tmp
.
begin
(),
tmp
.
end
());
return
std
::
string
(
tmp
.
begin
(),
tmp
.
end
());
}
}
std
::
string
mtx
::
crypto
::
export_session
(
OlmInboundGroupSession
*
s
)
{
const
size_t
len
=
olm_export_inbound_group_session_length
(
s
);
const
uint32_t
index
=
olm_inbound_group_session_first_known_index
(
s
);
auto
session_key
=
create_buffer
(
len
);
const
int
rc
=
olm_export_inbound_group_session
(
s
,
session_key
.
data
(),
session_key
.
size
(),
index
);
if
(
rc
==
-
1
)
throw
olm_exception
(
"session_key"
,
s
);
return
std
::
string
(
session_key
.
begin
(),
session_key
.
end
());
}
InboundGroupSessionPtr
mtx
::
crypto
::
import_session
(
const
std
::
string
&
session_key
)
{
auto
session
=
create_olm_object
<
InboundSessionObject
>
();
const
int
rc
=
olm_import_inbound_group_session
(
session
.
get
(),
reinterpret_cast
<
const
uint8_t
*>
(
session_key
.
data
()),
session_key
.
size
());
if
(
rc
==
-
1
)
throw
olm_exception
(
"import_session"
,
session
.
get
());
return
session
;
}
bool
bool
mtx
::
crypto
::
matches_inbound_session
(
OlmSession
*
session
,
const
std
::
string
&
one_time_key_message
)
mtx
::
crypto
::
matches_inbound_session
(
OlmSession
*
session
,
const
std
::
string
&
one_time_key_message
)
{
{
...
...
This diff is collapsed.
Click to expand it.
tests/e2ee.cpp
+
59
−
0
View file @
755cc434
...
@@ -1071,6 +1071,65 @@ TEST(ExportSessions, EncryptDecrypt)
...
@@ -1071,6 +1071,65 @@ TEST(ExportSessions, EncryptDecrypt)
EXPECT_EQ
(
json
(
keys
).
dump
(),
json
(
restored_keys
).
dump
());
EXPECT_EQ
(
json
(
keys
).
dump
(),
json
(
restored_keys
).
dump
());
}
}
TEST
(
ExportSessions
,
InboundMegolmSessions
)
{
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
);
// ==================== SESSION SETUP =================== //
// 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
);
// 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
);
// ==================== SESSION IMPORT/EXPORT =================== //
auto
exported_session_key
=
export_session
(
inbound_megolm_session
.
get
());
auto
restored_inbound_session
=
import_session
(
exported_session_key
);
// Decrypt message again.
auto
restored_ciphertext
=
std
::
string
((
char
*
)
encrypted_secret_message
.
data
(),
encrypted_secret_message
.
size
());
auto
restored_plaintext
=
bob
->
decrypt_group_message
(
restored_inbound_session
.
get
(),
restored_ciphertext
);
auto
restored_output_str
=
std
::
string
((
char
*
)
restored_plaintext
.
data
.
data
(),
restored_plaintext
.
data
.
size
());
ASSERT_EQ
(
restored_output_str
,
secret_message
);
}
TEST
(
Encryption
,
DISABLED_HandleRoomKeyEvent
)
{}
TEST
(
Encryption
,
DISABLED_HandleRoomKeyEvent
)
{}
TEST
(
Encryption
,
DISABLED_HandleRoomKeyRequestEvent
)
{}
TEST
(
Encryption
,
DISABLED_HandleRoomKeyRequestEvent
)
{}
TEST
(
Encryption
,
DISABLED_HandleNewDevices
)
{}
TEST
(
Encryption
,
DISABLED_HandleNewDevices
)
{}
...
...
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