aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2017-01-06 12:55:05 +0000
committerRichard van der Hoff <richard@matrix.org>2017-01-06 16:41:56 +0000
commit5fbeb3e29b6440a799d9320e871a1d4d509130b8 (patch)
tree63fd11381cfcd3b3d5b76e288657a3a453b21471 /python
parentbd6ab72ca40e0484be2a39734ba135437e820d63 (diff)
Enable exporting inbound group session keys
A pair of functions which allow you to export the megolm keys for an inbound group session, so that an application can save/restore them.
Diffstat (limited to 'python')
-rwxr-xr-xpython/olm/__main__.py30
-rw-r--r--python/olm/inbound_group_session.py19
2 files changed, 48 insertions, 1 deletions
diff --git a/python/olm/__main__.py b/python/olm/__main__.py
index eb76301..f195591 100755
--- a/python/olm/__main__.py
+++ b/python/olm/__main__.py
@@ -277,6 +277,27 @@ def build_arg_parser():
type=argparse.FileType('wb'), nargs='?',
default=sys.stdout)
group_decrypt.set_defaults(func=do_group_decrypt)
+
+
+ export_inbound_group = commands.add_parser(
+ "export_inbound_group",
+ help="Export the keys for an inbound group session",
+ )
+ export_inbound_group.add_argument(
+ "session_file", help="Local inbound group session file",
+ )
+ export_inbound_group.add_argument(
+ "export_file", help="File to export to (default stdout)",
+ type=argparse.FileType('w'), nargs='?',
+ default=sys.stdout,
+ )
+ export_inbound_group.add_argument(
+ "--message_index",
+ help="Index to export session at. Defaults to the earliest known index",
+ type=int,
+ )
+ export_inbound_group.set_defaults(func=do_export_inbound_group)
+
return parser
def do_outbound_group(args):
@@ -333,6 +354,15 @@ def do_group_decrypt(args):
f.write(session.pickle(args.key))
args.plaintext_file.write(plaintext)
+def do_export_inbound_group(args):
+ session = InboundGroupSession()
+ session.unpickle(args.key, read_base64_file(args.session_file))
+ index = args.message_index
+ if index is None:
+ # default to first known index
+ index = session.first_known_index()
+ args.export_file.write(session.export_session(index))
+
if __name__ == '__main__':
parser = build_arg_parser()
args = parser.parse_args()
diff --git a/python/olm/inbound_group_session.py b/python/olm/inbound_group_session.py
index 27a569c..67906b2 100644
--- a/python/olm/inbound_group_session.py
+++ b/python/olm/inbound_group_session.py
@@ -49,6 +49,13 @@ inbound_group_session_function(
inbound_group_session_function(lib.olm_inbound_group_session_id_length)
inbound_group_session_function(lib.olm_inbound_group_session_id, c_void_p, c_size_t)
+lib.olm_inbound_group_session_first_known_index.argtypes = (c_void_p,)
+lib.olm_inbound_group_session_first_known_index.restypes = c_uint32
+
+inbound_group_session_function(lib.olm_export_inbound_group_session_length)
+inbound_group_session_function(lib.olm_export_inbound_group_session, c_void_p, c_size_t, c_uint32)
+
+
class InboundGroupSession(object):
def __init__(self):
self.buf = create_string_buffer(lib.olm_inbound_group_session_size())
@@ -95,5 +102,15 @@ class InboundGroupSession(object):
def session_id(self):
id_length = lib.olm_inbound_group_session_id_length(self.ptr)
id_buffer = create_string_buffer(id_length)
- lib.olm_inbound_group_session_id(self.ptr, id_buffer, id_length);
+ lib.olm_inbound_group_session_id(self.ptr, id_buffer, id_length)
return id_buffer.raw
+
+ def first_known_index(self):
+ return lib.olm_inbound_group_session_first_known_index(self.ptr)
+
+ def export_session(self, message_index):
+ length = lib.olm_export_inbound_group_session_length(self.ptr)
+ buffer = create_string_buffer(length)
+ lib.olm_export_inbound_group_session(self.ptr, buffer, length,
+ message_index)
+ return buffer.raw