From 532dc0d4e79192a0c7fd1758322f6cae06959859 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Wed, 8 Jul 2015 15:30:34 +0100 Subject: Change the JSON format for one time keys to include what algorithm they are for --- src/account.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++------------ src/base64.cpp | 16 +++++----- src/olm.cpp | 55 ++++------------------------------ src/session.cpp | 1 + 4 files changed, 85 insertions(+), 78 deletions(-) (limited to 'src') diff --git a/src/account.cpp b/src/account.cpp index 297d2f4..a171f5c 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -143,13 +143,17 @@ std::size_t olm::Account::get_identity_json_length( length += sizeof(IDENTITY_JSON_PART_0) - 1; length += device_id_length; length += sizeof(IDENTITY_JSON_PART_1) - 1; - length += 4; + length += olm::encode_base64_length(3); length += sizeof(IDENTITY_JSON_PART_2) - 1; - length += 43; + length += olm::encode_base64_length( + sizeof(identity_keys.curve25519_key.public_key) + ); length += sizeof(IDENTITY_JSON_PART_3) - 1; - length += 4; + length += olm::encode_base64_length(3); length += sizeof(IDENTITY_JSON_PART_4) - 1; - length += 43; + length += olm::encode_base64_length( + sizeof(identity_keys.ed25519_key.public_key) + ); length += sizeof(IDENTITY_JSON_PART_5) - 1; length += user_id_length; length += sizeof(IDENTITY_JSON_PART_6) - 1; @@ -161,9 +165,9 @@ std::size_t olm::Account::get_identity_json_length( length += sizeof(IDENTITY_JSON_PART_9) - 1; length += device_id_length; length += sizeof(IDENTITY_JSON_PART_A) - 1; - length += 4; + length += olm::encode_base64_length(3); length += sizeof(IDENTITY_JSON_PART_B) - 1; - length += 86; + length += olm::encode_base64_length(64); length += sizeof(IDENTITY_JSON_PART_C) - 1; return length; } @@ -176,7 +180,6 @@ std::size_t olm::Account::get_identity_json( std::uint64_t valid_until_ts, std::uint8_t * identity_json, std::size_t identity_json_length ) { - std::uint8_t * pos = identity_json; std::uint8_t signature[64]; size_t expected_length = get_identity_json_length( @@ -191,17 +194,13 @@ std::size_t olm::Account::get_identity_json( pos = write_string(pos, IDENTITY_JSON_PART_0); pos = write_string(pos, device_id, device_id_length); pos = write_string(pos, IDENTITY_JSON_PART_1); - encode_base64(identity_keys.curve25519_key.public_key, 3, pos); - pos += 4; + pos = encode_base64(identity_keys.curve25519_key.public_key, 3, pos); pos = write_string(pos, IDENTITY_JSON_PART_2); - encode_base64(identity_keys.curve25519_key.public_key, 32, pos); - pos += 43; + pos = encode_base64(identity_keys.curve25519_key.public_key, 32, pos); pos = write_string(pos, IDENTITY_JSON_PART_3); - encode_base64(identity_keys.ed25519_key.public_key, 3, pos); - pos += 4; + pos = encode_base64(identity_keys.ed25519_key.public_key, 3, pos); pos = write_string(pos, IDENTITY_JSON_PART_4); - encode_base64(identity_keys.ed25519_key.public_key, 32, pos); - pos += 43; + pos = encode_base64(identity_keys.ed25519_key.public_key, 32, pos); pos = write_string(pos, IDENTITY_JSON_PART_5); pos = write_string(pos, user_id, user_id_length); pos = write_string(pos, IDENTITY_JSON_PART_6); @@ -221,15 +220,69 @@ std::size_t olm::Account::get_identity_json( pos = write_string(pos, IDENTITY_JSON_PART_9); pos = write_string(pos, device_id, device_id_length); pos = write_string(pos, IDENTITY_JSON_PART_A); - encode_base64(identity_keys.ed25519_key.public_key, 3, pos); - pos += 4; + pos = encode_base64(identity_keys.ed25519_key.public_key, 3, pos); pos = write_string(pos, IDENTITY_JSON_PART_B); - encode_base64(signature, 64, pos); - pos += 86; + pos = encode_base64(signature, 64, pos); pos = write_string(pos, IDENTITY_JSON_PART_C); return pos - identity_json; } +namespace { +uint8_t ONE_TIME_KEY_JSON_ALG[] = "curve25519"; +} + +std::size_t olm::Account::get_one_time_keys_json_length( +) { + std::size_t length = 0; + for (auto const & key : one_time_keys) { + length += 2; /* {" */ + length += sizeof(ONE_TIME_KEY_JSON_ALG) - 1; + length += 1; /* : */ + length += olm::encode_base64_length(olm::pickle_length(key.id)); + length += 3; /* ":" */ + length += olm::encode_base64_length(sizeof(key.key.public_key)); + length += 1; /* " */ + } + if (length) { + return length + 1; /* } */ + } else { + return 2; /* {} */ + } +} + + +std::size_t olm::Account::get_one_time_keys_json( + std::uint8_t * one_time_json, std::size_t one_time_json_length +) { + std::uint8_t * pos = one_time_json; + if (one_time_json_length < get_one_time_keys_json_length()) { + last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + return std::size_t(-1); + } + std::uint8_t sep = '{'; + for (auto const & key : one_time_keys) { + *(pos++) = sep; + *(pos++) = '\"'; + pos = write_string(pos, ONE_TIME_KEY_JSON_ALG); + *(pos++) = ':'; + std::uint8_t key_id[olm::pickle_length(key.id)]; + olm::pickle(key_id, key.id); + pos = olm::encode_base64(key_id, sizeof(key_id), pos); + *(pos++) = '\"'; *(pos++) = ':'; *(pos++) = '\"'; + pos = olm::encode_base64( + key.key.public_key, sizeof(key.key.public_key), pos + ); + *(pos++) = '\"'; + sep = ','; + } + if (sep != ',') { + *(pos++) = sep; + } + *(pos++) = '}'; + return pos - one_time_json; +} + + namespace olm { static std::size_t pickle_length( diff --git a/src/base64.cpp b/src/base64.cpp index a8631a1..bf8492e 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -45,14 +45,7 @@ static const std::uint8_t DECODE_BASE64[128] = { } // namespace -std::size_t olm::encode_base64_length( - std::size_t input_length -) { - return 4 * ((input_length + 2) / 3) + (input_length + 2) % 3 - 2; -} - - -void olm::encode_base64( +std::uint8_t * olm::encode_base64( std::uint8_t const * input, std::size_t input_length, std::uint8_t * output ) { @@ -70,6 +63,7 @@ void olm::encode_base64( output += 4; } unsigned remainder = input + input_length - pos; + std::uint8_t * result = output; if (remainder) { unsigned value = pos[0]; if (remainder == 2) { @@ -77,13 +71,16 @@ void olm::encode_base64( value <<= 2; output[2] = ENCODE_BASE64[value & 0x3F]; value >>= 6; + result += 3; } else { value <<= 4; + result += 2; } output[1] = ENCODE_BASE64[value & 0x3F]; value >>= 6; output[0] = ENCODE_BASE64[value]; } + return result; } @@ -98,7 +95,7 @@ std::size_t olm::decode_base64_length( } -void olm::decode_base64( +std::uint8_t const * olm::decode_base64( std::uint8_t const * input, std::size_t input_length, std::uint8_t * output ) { @@ -129,4 +126,5 @@ void olm::decode_base64( } output[0] = value; } + return input + input_length; } diff --git a/src/olm.cpp b/src/olm.cpp index 8106917..ede9c26 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -320,38 +320,6 @@ size_t olm_create_account( return from_c(account)->new_account(from_c(random), random_length); } -namespace { - -static const std::size_t OUTPUT_KEY_LENGTH = 2 + 10 + 2 + - olm::encode_base64_length(32) + 3; - -void output_one_time_key( - olm::OneTimeKey const & key, - std::uint8_t sep, - std::uint8_t * output -) { - output[0] = sep; - output[1] = '['; - std::memset(output + 2, ' ', 10); - uint32_t value = key.id; - uint8_t * number = output + 11; - *number = '0' + value % 10; - value /= 10; - while (value) { - *(--number) = '0' + value % 10; - value /= 10; - } - output[12] = ','; - output[13] = '"'; - olm::encode_base64(key.key.public_key, 32, output + 14); - output[OUTPUT_KEY_LENGTH - 3] = '"'; - output[OUTPUT_KEY_LENGTH - 2] = ']'; - output[OUTPUT_KEY_LENGTH - 1] = '\n'; -} - -} // namespace - - size_t olm_account_identity_keys_length( OlmAccount * account, size_t user_id_length, @@ -388,30 +356,17 @@ size_t olm_account_identity_keys( size_t olm_account_one_time_keys_length( OlmAccount * account ) { - size_t count = from_c(account)->one_time_keys.size(); - return OUTPUT_KEY_LENGTH * count + 1; + return from_c(account)->get_one_time_keys_json_length(); } size_t olm_account_one_time_keys( OlmAccount * account, - void * identity_keys, size_t identity_key_length + void * one_time_keys_json, size_t one_time_key_json_length ) { - std::size_t length = olm_account_one_time_keys_length(account); - if (identity_key_length < length) { - from_c(account)->last_error = - olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; - return size_t(-1); - } - std::uint8_t * output = from_c(identity_keys); - std::uint8_t sep = '['; - for (auto const & key : from_c(account)->one_time_keys) { - output_one_time_key(key, sep, output); - output += OUTPUT_KEY_LENGTH; - sep = ','; - } - output[0] = ']'; - return length; + return from_c(account)->get_one_time_keys_json( + from_c(one_time_keys_json), one_time_key_json_length + ); } diff --git a/src/session.cpp b/src/session.cpp index a56725d..f3b7637 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -157,6 +157,7 @@ std::size_t olm::Session::new_inbound_session( last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; return std::size_t(-1); } + bob_one_time_key_id = our_one_time_key->id; std::uint8_t shared_secret[96]; -- cgit v1.2.3