Don't pass a key id when creating a new outbound session

This commit is contained in:
Mark Haines 2015-07-08 16:00:08 +01:00
parent 532dc0d4e7
commit 0e988237f6
9 changed files with 29 additions and 35 deletions

View file

@ -112,13 +112,14 @@ struct Account {
std::uint8_t * one_time_json, std::size_t one_time_json_length std::uint8_t * one_time_json, std::size_t one_time_json_length
); );
/** Lookup a one_time key with the given key-id */ /** Lookup a one time key with the given public key */
OneTimeKey const * lookup_key( OneTimeKey const * lookup_key(
Curve25519PublicKey const & public_key Curve25519PublicKey const & public_key
); );
/** Remove a one time key with the given public key */
std::size_t remove_key( std::size_t remove_key(
std::uint32_t id Curve25519PublicKey const & public_key
); );
}; };

View file

@ -38,7 +38,6 @@ struct Session {
Curve25519PublicKey alice_identity_key; Curve25519PublicKey alice_identity_key;
Curve25519PublicKey alice_base_key; Curve25519PublicKey alice_base_key;
Curve25519PublicKey bob_one_time_key; Curve25519PublicKey bob_one_time_key;
std::uint32_t bob_one_time_key_id;
std::size_t new_outbound_session_random_length(); std::size_t new_outbound_session_random_length();

View file

@ -59,6 +59,7 @@ compile_args += source_files
compile_args += ("--pre-js", pre_js) compile_args += ("--pre-js", pre_js)
compile_args += ("--post-js", post_js) compile_args += ("--post-js", post_js)
compile_args += ("-s", "EXPORTED_FUNCTIONS=@" + exported_functions) compile_args += ("-s", "EXPORTED_FUNCTIONS=@" + exported_functions)
compile_args += sys.argv[1:]
library = "build/olm.js" library = "build/olm.js"

View file

@ -30,16 +30,20 @@ document.addEventListener("DOMContentLoaded", function (event) {
tasks.push(["bob", "Creating account", function() { bob.create() }]); tasks.push(["bob", "Creating account", function() { bob.create() }]);
tasks.push(["alice", "Create outbound session", function() { tasks.push(["alice", "Create outbound session", function() {
var bobs_id_keys = JSON.parse(bob.identity_keys("bob", "bob_device", 0, 0)); var bobs_id_keys = JSON.parse(bob.identity_keys("bob", "bob_device", 0, 0));
var bobs_curve25519_key; var bobs_id_key;
for (key in bobs_id_keys.keys) { for (key in bobs_id_keys.keys) {
if (key.startsWith("curve25519:")) { if (key.startsWith("curve25519:")) {
bobs_curve25519_key = bobs_id_keys.keys[key]; bobs_id_key = bobs_id_keys.keys[key];
} }
} }
var bobs_keys_2 = JSON.parse(bob.one_time_keys())[1]; var bobs_ot_keys = JSON.parse(bob.one_time_keys());
a_session.create_outbound( var bobs_ot_key;
alice, bobs_curve25519_key, bobs_keys_2[0], bobs_keys_2[1] for (key in bobs_ot_keys) {
); if (key.startsWith("curve25519:")) {
bobs_ot_key = bobs_ot_keys[key];
}
}
a_session.create_outbound(alice, bobs_id_key, bobs_ot_key);
}]); }]);
tasks.push(["alice", "Encrypt first message", function() { tasks.push(["alice", "Encrypt first message", function() {
message_1 = a_session.encrypt(""); message_1 = a_session.encrypt("");
@ -96,7 +100,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
window.setTimeout(function() { window.setTimeout(function() {
task[2](); task[2]();
p.done(); p.done();
window.setTimeout(do_tasks, 0, next); window.setTimeout(do_tasks, 50, next);
}, 0) }, 0)
} else { } else {
next(); next();

View file

@ -172,7 +172,7 @@ Session.prototype['unpickle'] = restore_stack(function(key, pickle) {
}); });
Session.prototype['create_outbound'] = restore_stack(function( Session.prototype['create_outbound'] = restore_stack(function(
account, their_identity_key, their_one_time_key_id, their_one_time_key account, their_identity_key, their_one_time_key
) { ) {
var random_length = session_method( var random_length = session_method(
Module['_olm_create_outbound_session_random_length'] Module['_olm_create_outbound_session_random_length']
@ -185,7 +185,6 @@ Session.prototype['create_outbound'] = restore_stack(function(
session_method(Module['_olm_create_outbound_session'])( session_method(Module['_olm_create_outbound_session'])(
this.ptr, account.ptr, this.ptr, account.ptr,
identity_key_buffer, identity_key_array.length, identity_key_buffer, identity_key_array.length,
their_one_time_key_id,
one_time_key_buffer, one_time_key_array.length, one_time_key_buffer, one_time_key_array.length,
random, random_length random, random_length
); );

20
olm.py
View file

@ -1,8 +1,11 @@
#! /usr/bin/python #! /usr/bin/python
from ctypes import * from ctypes import *
import json import json
import os
lib = cdll.LoadLibrary("build/libolm.so") lib = cdll.LoadLibrary(os.path.join(
os.path.dirname(__file__), "build", "libolm.so")
)
lib.olm_error.argtypes = [] lib.olm_error.argtypes = []
@ -149,7 +152,6 @@ session_function(
lib.olm_create_outbound_session, lib.olm_create_outbound_session,
c_void_p, # Account c_void_p, # Account
c_void_p, c_size_t, # Identity Key c_void_p, c_size_t, # Identity Key
c_uint, # One Time Key Id
c_void_p, c_size_t, # One Time Key c_void_p, c_size_t, # One Time Key
c_void_p, c_size_t, # Random c_void_p, c_size_t, # Random
) )
@ -201,8 +203,7 @@ class Session(object):
self.ptr, key_buffer, len(key), pickle_buffer, len(pickle) self.ptr, key_buffer, len(key), pickle_buffer, len(pickle)
) )
def create_outbound(self, account, identity_key, one_time_key_id, def create_outbound(self, account, identity_key, one_time_key):
one_time_key):
r_length = lib.olm_create_outbound_session_random_length(self.ptr) r_length = lib.olm_create_outbound_session_random_length(self.ptr)
random = read_random(r_length) random = read_random(r_length)
random_buffer = create_string_buffer(random) random_buffer = create_string_buffer(random)
@ -212,7 +213,6 @@ class Session(object):
self.ptr, self.ptr,
account.ptr, account.ptr,
identity_key_buffer, len(identity_key), identity_key_buffer, len(identity_key),
one_time_key_id,
one_time_key_buffer, len(one_time_key), one_time_key_buffer, len(one_time_key),
random_buffer, r_length random_buffer, r_length
) )
@ -325,11 +325,6 @@ if __name__ == '__main__':
outbound.add_argument("account_file", help="Local account file") outbound.add_argument("account_file", help="Local account file")
outbound.add_argument("session_file", help="Local session file") outbound.add_argument("session_file", help="Local session file")
outbound.add_argument("identity_key", help="Remote identity key") outbound.add_argument("identity_key", help="Remote identity key")
outbound.add_argument("signed_key_id", help="Remote signed key id",
type=int)
outbound.add_argument("signed_key", help="Remote signed key")
outbound.add_argument("one_time_key_id", help="Remote one time key id",
type=int)
outbound.add_argument("one_time_key", help="Remote one time key") outbound.add_argument("one_time_key", help="Remote one time key")
def do_outbound(args): def do_outbound(args):
@ -343,8 +338,7 @@ if __name__ == '__main__':
account.unpickle(args.key, f.read()) account.unpickle(args.key, f.read())
session = Session() session = Session()
session.create_outbound( session.create_outbound(
account, args.identity_key, args.signed_key_id, args.signed_key, account, args.identity_key, args.one_time_key
args.one_time_key_id, args.one_time_key
) )
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))
@ -416,8 +410,8 @@ if __name__ == '__main__':
decrypt = commands.add_parser("decrypt", help="Decrypt a message") decrypt = commands.add_parser("decrypt", help="Decrypt a message")
decrypt.add_argument("session_file", help="Local session file") decrypt.add_argument("session_file", help="Local session file")
decrypt.add_argument("plaintext_file", help="Plaintext", default="-")
decrypt.add_argument("message_file", help="Message", default="-") decrypt.add_argument("message_file", help="Message", default="-")
decrypt.add_argument("plaintext_file", help="Plaintext", default="-")
def do_decrypt(args): def do_decrypt(args):
session = Session() session = Session()

View file

@ -29,11 +29,12 @@ olm::OneTimeKey const * olm::Account::lookup_key(
} }
std::size_t olm::Account::remove_key( std::size_t olm::Account::remove_key(
std::uint32_t id olm::Curve25519PublicKey const & public_key
) { ) {
OneTimeKey * i; OneTimeKey * i;
for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) { for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) {
if (i->id == id) { if (0 == memcmp(i->key.public_key, public_key.public_key, 32)) {
std::uint32_t id = i->id;
one_time_keys.erase(i); one_time_keys.erase(i);
return id; return id;
} }
@ -42,7 +43,7 @@ std::size_t olm::Account::remove_key(
} }
std::size_t olm::Account::new_account_random_length() { std::size_t olm::Account::new_account_random_length() {
return 103 * 32; return 12 * 32;
} }
std::size_t olm::Account::new_account( std::size_t olm::Account::new_account(

View file

@ -447,7 +447,7 @@ size_t olm_remove_one_time_keys(
OlmSession * session OlmSession * session
) { ) {
size_t result = from_c(account)->remove_key( size_t result = from_c(account)->remove_key(
from_c(session)->bob_one_time_key_id from_c(session)->bob_one_time_key
); );
if (result == std::size_t(-1)) { if (result == std::size_t(-1)) {
from_c(account)->last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; from_c(account)->last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID;

View file

@ -45,8 +45,7 @@ static const olm::KdfInfo OLM_KDF_INFO = {
olm::Session::Session( olm::Session::Session(
) : ratchet(OLM_KDF_INFO, OLM_CIPHER), ) : ratchet(OLM_KDF_INFO, OLM_CIPHER),
last_error(olm::ErrorCode::SUCCESS), last_error(olm::ErrorCode::SUCCESS),
received_message(false), received_message(false) {
bob_one_time_key_id(0) {
} }
@ -157,7 +156,6 @@ std::size_t olm::Session::new_inbound_session(
last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID;
return std::size_t(-1); return std::size_t(-1);
} }
bob_one_time_key_id = our_one_time_key->id;
std::uint8_t shared_secret[96]; std::uint8_t shared_secret[96];
@ -364,7 +362,6 @@ std::size_t olm::pickle_length(
length += olm::pickle_length(value.alice_identity_key); length += olm::pickle_length(value.alice_identity_key);
length += olm::pickle_length(value.alice_base_key); length += olm::pickle_length(value.alice_base_key);
length += olm::pickle_length(value.bob_one_time_key); length += olm::pickle_length(value.bob_one_time_key);
length += olm::pickle_length(value.bob_one_time_key_id);
length += olm::pickle_length(value.ratchet); length += olm::pickle_length(value.ratchet);
return length; return length;
} }
@ -378,7 +375,6 @@ std::uint8_t * olm::pickle(
pos = olm::pickle(pos, value.alice_identity_key); pos = olm::pickle(pos, value.alice_identity_key);
pos = olm::pickle(pos, value.alice_base_key); pos = olm::pickle(pos, value.alice_base_key);
pos = olm::pickle(pos, value.bob_one_time_key); pos = olm::pickle(pos, value.bob_one_time_key);
pos = olm::pickle(pos, value.bob_one_time_key_id);
pos = olm::pickle(pos, value.ratchet); pos = olm::pickle(pos, value.ratchet);
return pos; return pos;
} }
@ -392,7 +388,6 @@ std::uint8_t const * olm::unpickle(
pos = olm::unpickle(pos, end, value.alice_identity_key); pos = olm::unpickle(pos, end, value.alice_identity_key);
pos = olm::unpickle(pos, end, value.alice_base_key); pos = olm::unpickle(pos, end, value.alice_base_key);
pos = olm::unpickle(pos, end, value.bob_one_time_key); pos = olm::unpickle(pos, end, value.bob_one_time_key);
pos = olm::unpickle(pos, end, value.bob_one_time_key_id);
pos = olm::unpickle(pos, end, value.ratchet); pos = olm::unpickle(pos, end, value.ratchet);
return pos; return pos;
} }