Implement importing group session data

olm_import_inbound_group_session, which reads the format written by
olm_export_inbound_group_session to initialise a group session.
This commit is contained in:
Richard van der Hoff 2017-01-06 17:40:39 +00:00
parent 5fbeb3e29b
commit a2f0c93a93
7 changed files with 105 additions and 10 deletions

View file

@ -85,7 +85,8 @@ size_t olm_unpickle_inbound_group_session(
/** /**
* Start a new inbound group session, based on the parameters supplied. * Start a new inbound group session, from a key exported from
* olm_outbound_group_session_key
* *
* Returns olm_error() on failure. On failure last_error will be set with an * Returns olm_error() on failure. On failure last_error will be set with an
* error code. The last_error will be: * error code. The last_error will be:
@ -99,6 +100,23 @@ size_t olm_init_inbound_group_session(
uint8_t const * session_key, size_t session_key_length uint8_t const * session_key, size_t session_key_length
); );
/**
* Import an inbound group session, from a previous export.
*
* Returns olm_error() on failure. On failure last_error will be set with an
* error code. The last_error will be:
*
* * OLM_INVALID_BASE64 if the session_key is not valid base64
* * OLM_BAD_SESSION_KEY if the session_key is invalid
*/
size_t olm_import_inbound_group_session(
OlmInboundGroupSession *session,
/* base64-encoded keys; note that it will be overwritten with the base64-decoded
data. */
uint8_t const * session_key, size_t session_key_length
);
/** /**
* Get an upper bound on the number of bytes of plain-text the decrypt method * Get an upper bound on the number of bytes of plain-text the decrypt method
* will write for a given input message length. The actual size could be * will write for a given input message length. The actual size could be

View file

@ -61,6 +61,15 @@ InboundGroupSession.prototype['create'] = restore_stack(function(session_key) {
); );
}); });
InboundGroupSession.prototype['import_session'] = restore_stack(function(session_key) {
var key_array = array_from_string(session_key);
var key_buffer = stack(key_array);
inbound_group_session_method(Module['_olm_import_inbound_group_session'])(
this.ptr, key_buffer, key_array.length
);
});
InboundGroupSession.prototype['decrypt'] = restore_stack(function( InboundGroupSession.prototype['decrypt'] = restore_stack(function(
message message
) { ) {

1
python/.gitignore vendored
View file

@ -2,3 +2,4 @@
/*.account /*.account
/*.session /*.session
/*.group_session /*.group_session
/group_message

View file

@ -268,6 +268,19 @@ def build_arg_parser():
default=sys.stdin) default=sys.stdin)
inbound_group.set_defaults(func=do_inbound_group) inbound_group.set_defaults(func=do_inbound_group)
import_inbound_group = commands.add_parser(
"import_inbound_group",
help="Create an inbound group session based an exported inbound group"
)
import_inbound_group.add_argument("session_file", help="Local inbound group session file")
import_inbound_group.add_argument(
"export_file",
help="File to read credentials from (default stdin)",
type=argparse.FileType('r'), nargs='?',
default=sys.stdin,
)
import_inbound_group.set_defaults(func=do_import_inbound_group)
group_decrypt = commands.add_parser("group_decrypt", help="Decrypt a group message") group_decrypt = commands.add_parser("group_decrypt", help="Decrypt a group message")
group_decrypt.add_argument("session_file", help="Local inbound group session file") group_decrypt.add_argument("session_file", help="Local inbound group session file")
group_decrypt.add_argument("message_file", help="Message file (default stdin)", group_decrypt.add_argument("message_file", help="Message file (default stdin)",
@ -345,6 +358,19 @@ def do_inbound_group(args):
with open(args.session_file, "wb") as f: with open(args.session_file, "wb") as f:
f.write(session.pickle(args.key)) f.write(session.pickle(args.key))
def do_import_inbound_group(args):
if os.path.exists(args.session_file):
sys.stderr.write("Session %r file already exists\n" % (
args.session_file,
))
sys.exit(1)
data = args.export_file.read().translate(None, "\r\n")
session = InboundGroupSession()
session.import_session(data)
with open(args.session_file, "wb") as f:
f.write(session.pickle(args.key))
def do_group_decrypt(args): def do_group_decrypt(args):
session = InboundGroupSession() session = InboundGroupSession()
session.unpickle(args.key, read_base64_file(args.session_file)) session.unpickle(args.key, read_base64_file(args.session_file))

View file

@ -36,6 +36,10 @@ inbound_group_session_function(
lib.olm_init_inbound_group_session, c_void_p, c_size_t lib.olm_init_inbound_group_session, c_void_p, c_size_t
) )
inbound_group_session_function(
lib.olm_import_inbound_group_session, c_void_p, c_size_t
)
inbound_group_session_function( inbound_group_session_function(
lib.olm_group_decrypt_max_plaintext_length, c_void_p, c_size_t lib.olm_group_decrypt_max_plaintext_length, c_void_p, c_size_t
) )
@ -83,6 +87,12 @@ class InboundGroupSession(object):
self.ptr, key_buffer, len(session_key) self.ptr, key_buffer, len(session_key)
) )
def import_session(self, session_key):
key_buffer = create_string_buffer(session_key)
lib.olm_import_inbound_group_session(
self.ptr, key_buffer, len(session_key)
)
def decrypt(self, message): def decrypt(self, message):
message_buffer = create_string_buffer(message) message_buffer = create_string_buffer(message)
max_plaintext_length = lib.olm_group_decrypt_max_plaintext_length( max_plaintext_length = lib.olm_group_decrypt_max_plaintext_length(

View file

@ -10,10 +10,11 @@ ALICE_GROUP_SESSION=alice.group_session
BOB_ACCOUNT=bob.account BOB_ACCOUNT=bob.account
BOB_SESSION=bob.session BOB_SESSION=bob.session
BOB_GROUP_SESSION=bob.group_session BOB_GROUP_SESSION=bob.group_session
CHARLIE_GROUP_SESSION=charlie.group_session
rm $ALICE_ACCOUNT $BOB_ACCOUNT rm -f $ALICE_ACCOUNT $BOB_ACCOUNT
rm $ALICE_SESSION $BOB_SESSION rm -f $ALICE_SESSION $BOB_SESSION
rm $ALICE_GROUP_SESSION $BOB_GROUP_SESSION rm -f $ALICE_GROUP_SESSION $BOB_GROUP_SESSION $CHARLIE_GROUP_SESSION
$OLM create_account $ALICE_ACCOUNT $OLM create_account $ALICE_ACCOUNT
$OLM create_account $BOB_ACCOUNT $OLM create_account $BOB_ACCOUNT
@ -31,4 +32,8 @@ echo "Hello world" | $OLM encrypt $ALICE_SESSION - - | $OLM inbound $BOB_ACCOUNT
$OLM outbound_group $ALICE_GROUP_SESSION $OLM outbound_group $ALICE_GROUP_SESSION
$OLM group_credentials $ALICE_GROUP_SESSION | $OLM inbound_group $BOB_GROUP_SESSION $OLM group_credentials $ALICE_GROUP_SESSION | $OLM inbound_group $BOB_GROUP_SESSION
echo "Hello group" | $OLM group_encrypt $ALICE_GROUP_SESSION - - | $OLM group_decrypt $BOB_GROUP_SESSION echo "Hello group" | $OLM group_encrypt $ALICE_GROUP_SESSION - group_message
$OLM group_decrypt $BOB_GROUP_SESSION group_message
$OLM export_inbound_group $BOB_GROUP_SESSION | $OLM import_inbound_group $CHARLIE_GROUP_SESSION
$OLM group_decrypt $CHARLIE_GROUP_SESSION group_message

View file

@ -79,15 +79,17 @@ size_t olm_clear_inbound_group_session(
(1 + 4 + MEGOLM_RATCHET_LENGTH + ED25519_PUBLIC_KEY_LENGTH\ (1 + 4 + MEGOLM_RATCHET_LENGTH + ED25519_PUBLIC_KEY_LENGTH\
+ ED25519_SIGNATURE_LENGTH) + ED25519_SIGNATURE_LENGTH)
/** init the session keys from the un-base64-ed session keys */
static size_t _init_group_session_keys( static size_t _init_group_session_keys(
OlmInboundGroupSession *session, OlmInboundGroupSession *session,
const uint8_t *key_buf const uint8_t *key_buf,
int export_format
) { ) {
const uint8_t expected_version =
(export_format ? SESSION_EXPORT_VERSION : SESSION_KEY_VERSION);
const uint8_t *ptr = key_buf; const uint8_t *ptr = key_buf;
size_t version = *ptr++; size_t version = *ptr++;
if (version != SESSION_KEY_VERSION) { if (version != expected_version) {
session->last_error = OLM_BAD_SESSION_KEY; session->last_error = OLM_BAD_SESSION_KEY;
return (size_t)-1; return (size_t)-1;
} }
@ -107,7 +109,7 @@ static size_t _init_group_session_keys(
); );
ptr += ED25519_PUBLIC_KEY_LENGTH; ptr += ED25519_PUBLIC_KEY_LENGTH;
if (!_olm_crypto_ed25519_verify( if (!export_format && !_olm_crypto_ed25519_verify(
&session->signing_key, key_buf, ptr - key_buf, ptr &session->signing_key, key_buf, ptr - key_buf, ptr
)) { )) {
session->last_error = OLM_BAD_SIGNATURE; session->last_error = OLM_BAD_SIGNATURE;
@ -135,11 +137,35 @@ size_t olm_init_inbound_group_session(
} }
_olm_decode_base64(session_key, session_key_length, key_buf); _olm_decode_base64(session_key, session_key_length, key_buf);
result = _init_group_session_keys(session, key_buf); result = _init_group_session_keys(session, key_buf, 0);
_olm_unset(key_buf, SESSION_KEY_RAW_LENGTH); _olm_unset(key_buf, SESSION_KEY_RAW_LENGTH);
return result; return result;
} }
size_t olm_import_inbound_group_session(
OlmInboundGroupSession *session,
const uint8_t * session_key, size_t session_key_length
) {
uint8_t key_buf[SESSION_EXPORT_RAW_LENGTH];
size_t raw_length = _olm_decode_base64_length(session_key_length);
size_t result;
if (raw_length == (size_t)-1) {
session->last_error = OLM_INVALID_BASE64;
return (size_t)-1;
}
if (raw_length != SESSION_EXPORT_RAW_LENGTH) {
session->last_error = OLM_BAD_SESSION_KEY;
return (size_t)-1;
}
_olm_decode_base64(session_key, session_key_length, key_buf);
result = _init_group_session_keys(session, key_buf, 1);
_olm_unset(key_buf, SESSION_EXPORT_RAW_LENGTH);
return result;
}
static size_t raw_pickle_length( static size_t raw_pickle_length(
const OlmInboundGroupSession *session const OlmInboundGroupSession *session
) { ) {