2016-04-26 12:44:32 +02:00
|
|
|
/* Copyright 2015, 2016 OpenMarket Ltd
|
2015-06-22 18:50:56 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/account.hh"
|
2015-07-07 17:42:03 +02:00
|
|
|
#include "olm/base64.hh"
|
2016-09-05 13:59:12 +02:00
|
|
|
#include "olm/pickle.h"
|
2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/pickle.hh"
|
2015-08-19 18:18:09 +02:00
|
|
|
#include "olm/memory.hh"
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2015-07-09 17:09:16 +02:00
|
|
|
olm::Account::Account(
|
|
|
|
) : next_one_time_key_id(0),
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error(OlmErrorCode::OLM_SUCCESS) {
|
2020-08-14 23:29:25 +02:00
|
|
|
// since we don't need to keep track of whether the fallback keys are
|
|
|
|
// published, use the published flag as in indication for whether the keys
|
|
|
|
// were generated
|
|
|
|
current_fallback_key.published = false;
|
|
|
|
prev_fallback_key.published = false;
|
2015-07-09 17:09:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2015-07-07 17:42:03 +02:00
|
|
|
olm::OneTimeKey const * olm::Account::lookup_key(
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key const & public_key
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-07-07 17:42:03 +02:00
|
|
|
for (olm::OneTimeKey const & key : one_time_keys) {
|
2016-09-02 16:13:24 +02:00
|
|
|
if (olm::array_equal(key.key.public_key.public_key, public_key.public_key)) {
|
2015-07-08 15:53:25 +02:00
|
|
|
return &key;
|
|
|
|
}
|
2015-06-12 17:15:37 +02:00
|
|
|
}
|
2020-08-14 23:29:25 +02:00
|
|
|
if (current_fallback_key.published
|
2020-09-16 22:14:23 +02:00
|
|
|
&& olm::array_equal(
|
|
|
|
current_fallback_key.key.public_key.public_key, public_key.public_key
|
|
|
|
)
|
|
|
|
) {
|
2020-08-14 23:29:25 +02:00
|
|
|
return ¤t_fallback_key;
|
|
|
|
}
|
|
|
|
if (prev_fallback_key.published
|
2020-09-16 22:14:23 +02:00
|
|
|
&& olm::array_equal(
|
|
|
|
prev_fallback_key.key.public_key.public_key, public_key.public_key
|
|
|
|
)
|
|
|
|
) {
|
2020-08-14 23:29:25 +02:00
|
|
|
return &prev_fallback_key;
|
|
|
|
}
|
2015-06-12 17:15:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t olm::Account::remove_key(
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key const & public_key
|
2015-06-22 12:02:42 +02:00
|
|
|
) {
|
2015-07-07 17:42:03 +02:00
|
|
|
OneTimeKey * i;
|
2015-06-22 12:02:42 +02:00
|
|
|
for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) {
|
2016-09-02 16:13:24 +02:00
|
|
|
if (olm::array_equal(i->key.public_key.public_key, public_key.public_key)) {
|
2015-07-08 17:00:08 +02:00
|
|
|
std::uint32_t id = i->id;
|
2015-06-22 12:02:42 +02:00
|
|
|
one_time_keys.erase(i);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 23:29:25 +02:00
|
|
|
// check if the key is a fallback key, to avoid returning an error, but
|
|
|
|
// don't actually remove it
|
|
|
|
if (current_fallback_key.published
|
2020-09-16 22:14:23 +02:00
|
|
|
&& olm::array_equal(
|
|
|
|
current_fallback_key.key.public_key.public_key, public_key.public_key
|
|
|
|
)
|
|
|
|
) {
|
2020-08-14 23:29:25 +02:00
|
|
|
return current_fallback_key.id;
|
|
|
|
}
|
|
|
|
if (prev_fallback_key.published
|
2020-09-16 22:14:23 +02:00
|
|
|
&& olm::array_equal(
|
|
|
|
prev_fallback_key.key.public_key.public_key, public_key.public_key
|
|
|
|
)
|
|
|
|
) {
|
2020-08-14 23:29:25 +02:00
|
|
|
return prev_fallback_key.id;
|
|
|
|
}
|
2015-06-22 12:02:42 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-06-15 18:47:22 +02:00
|
|
|
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t olm::Account::new_account_random_length() const {
|
2016-09-02 16:11:14 +02:00
|
|
|
return ED25519_RANDOM_LENGTH + CURVE25519_RANDOM_LENGTH;
|
2015-06-15 18:47:22 +02:00
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t olm::Account::new_account(
|
2015-06-15 18:47:22 +02:00
|
|
|
uint8_t const * random, std::size_t random_length
|
|
|
|
) {
|
|
|
|
if (random_length < new_account_random_length()) {
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
|
2015-06-22 12:02:42 +02:00
|
|
|
return std::size_t(-1);
|
2015-06-15 18:47:22 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_crypto_ed25519_generate_key(random, &identity_keys.ed25519_key);
|
2016-09-02 16:11:14 +02:00
|
|
|
random += ED25519_RANDOM_LENGTH;
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_crypto_curve25519_generate_key(random, &identity_keys.curve25519_key);
|
2015-06-15 18:47:22 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-07 17:42:03 +02:00
|
|
|
namespace {
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
uint8_t KEY_JSON_ED25519[] = "\"ed25519\":";
|
|
|
|
uint8_t KEY_JSON_CURVE25519[] = "\"curve25519\":";
|
2015-07-07 17:42:03 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2015-08-18 18:09:55 +02:00
|
|
|
static std::uint8_t * write_string(
|
2015-07-07 17:42:03 +02:00
|
|
|
std::uint8_t * pos,
|
|
|
|
T const & value
|
|
|
|
) {
|
|
|
|
std::memcpy(pos, value, sizeof(T) - 1);
|
|
|
|
return pos + (sizeof(T) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t olm::Account::get_identity_json_length() const {
|
2015-07-07 17:42:03 +02:00
|
|
|
std::size_t length = 0;
|
2015-07-10 12:57:53 +02:00
|
|
|
length += 1; /* { */
|
|
|
|
length += sizeof(KEY_JSON_CURVE25519) - 1;
|
|
|
|
length += 1; /* " */
|
2015-07-08 16:30:34 +02:00
|
|
|
length += olm::encode_base64_length(
|
|
|
|
sizeof(identity_keys.curve25519_key.public_key)
|
|
|
|
);
|
2015-07-10 12:57:53 +02:00
|
|
|
length += 2; /* ", */
|
|
|
|
length += sizeof(KEY_JSON_ED25519) - 1;
|
|
|
|
length += 1; /* " */
|
2015-07-08 16:30:34 +02:00
|
|
|
length += olm::encode_base64_length(
|
|
|
|
sizeof(identity_keys.ed25519_key.public_key)
|
|
|
|
);
|
2015-07-10 12:57:53 +02:00
|
|
|
length += 2; /* "} */
|
2015-07-07 17:42:03 +02:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t olm::Account::get_identity_json(
|
|
|
|
std::uint8_t * identity_json, std::size_t identity_json_length
|
|
|
|
) {
|
|
|
|
std::uint8_t * pos = identity_json;
|
2015-07-10 12:57:53 +02:00
|
|
|
size_t expected_length = get_identity_json_length();
|
2015-07-07 17:42:03 +02:00
|
|
|
|
|
|
|
if (identity_json_length < expected_length) {
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-07-07 17:42:03 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
*(pos++) = '{';
|
|
|
|
pos = write_string(pos, KEY_JSON_CURVE25519);
|
|
|
|
*(pos++) = '\"';
|
|
|
|
pos = olm::encode_base64(
|
2016-09-02 16:13:24 +02:00
|
|
|
identity_keys.curve25519_key.public_key.public_key,
|
|
|
|
sizeof(identity_keys.curve25519_key.public_key.public_key),
|
2015-07-10 12:57:53 +02:00
|
|
|
pos
|
2015-07-07 17:42:03 +02:00
|
|
|
);
|
2015-07-10 12:57:53 +02:00
|
|
|
*(pos++) = '\"'; *(pos++) = ',';
|
|
|
|
pos = write_string(pos, KEY_JSON_ED25519);
|
|
|
|
*(pos++) = '\"';
|
|
|
|
pos = olm::encode_base64(
|
2016-09-02 16:13:24 +02:00
|
|
|
identity_keys.ed25519_key.public_key.public_key,
|
|
|
|
sizeof(identity_keys.ed25519_key.public_key.public_key),
|
2015-07-10 12:57:53 +02:00
|
|
|
pos
|
|
|
|
);
|
|
|
|
*(pos++) = '\"'; *(pos++) = '}';
|
2015-07-07 17:42:03 +02:00
|
|
|
return pos - identity_json;
|
|
|
|
}
|
2015-06-15 18:47:22 +02:00
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
|
|
|
|
std::size_t olm::Account::signature_length(
|
2021-06-17 05:22:25 +02:00
|
|
|
) const {
|
2016-09-02 16:11:14 +02:00
|
|
|
return ED25519_SIGNATURE_LENGTH;
|
2015-07-08 16:30:34 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
|
|
|
|
std::size_t olm::Account::sign(
|
|
|
|
std::uint8_t const * message, std::size_t message_length,
|
|
|
|
std::uint8_t * signature, std::size_t signature_length
|
|
|
|
) {
|
|
|
|
if (signature_length < this->signature_length()) {
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-07-10 12:57:53 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_crypto_ed25519_sign(
|
|
|
|
&identity_keys.ed25519_key, message, message_length, signature
|
2015-07-10 12:57:53 +02:00
|
|
|
);
|
|
|
|
return this->signature_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-08 16:30:34 +02:00
|
|
|
std::size_t olm::Account::get_one_time_keys_json_length(
|
2021-06-17 05:22:25 +02:00
|
|
|
) const {
|
2015-07-08 16:30:34 +02:00
|
|
|
std::size_t length = 0;
|
2015-08-18 18:09:55 +02:00
|
|
|
bool is_empty = true;
|
2015-07-08 16:30:34 +02:00
|
|
|
for (auto const & key : one_time_keys) {
|
2015-07-09 17:09:16 +02:00
|
|
|
if (key.published) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-18 18:09:55 +02:00
|
|
|
is_empty = false;
|
2015-07-08 16:30:34 +02:00
|
|
|
length += 2; /* {" */
|
2017-09-28 13:38:55 +02:00
|
|
|
length += olm::encode_base64_length(_olm_pickle_uint32_length(key.id));
|
2015-07-08 16:30:34 +02:00
|
|
|
length += 3; /* ":" */
|
|
|
|
length += olm::encode_base64_length(sizeof(key.key.public_key));
|
|
|
|
length += 1; /* " */
|
|
|
|
}
|
2015-08-18 18:09:55 +02:00
|
|
|
if (is_empty) {
|
|
|
|
length += 1; /* { */
|
2015-07-08 16:30:34 +02:00
|
|
|
}
|
2015-07-10 12:57:53 +02:00
|
|
|
length += 3; /* }{} */
|
|
|
|
length += sizeof(KEY_JSON_CURVE25519) - 1;
|
|
|
|
return length;
|
2015-07-08 16:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()) {
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-07-08 16:30:34 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-07-10 12:57:53 +02:00
|
|
|
*(pos++) = '{';
|
|
|
|
pos = write_string(pos, KEY_JSON_CURVE25519);
|
2015-07-08 16:30:34 +02:00
|
|
|
std::uint8_t sep = '{';
|
|
|
|
for (auto const & key : one_time_keys) {
|
2015-07-09 17:09:16 +02:00
|
|
|
if (key.published) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-08 16:30:34 +02:00
|
|
|
*(pos++) = sep;
|
|
|
|
*(pos++) = '\"';
|
2017-09-28 13:38:55 +02:00
|
|
|
std::uint8_t key_id[_olm_pickle_uint32_length(key.id)];
|
|
|
|
_olm_pickle_uint32(key_id, key.id);
|
2015-07-08 16:30:34 +02:00
|
|
|
pos = olm::encode_base64(key_id, sizeof(key_id), pos);
|
|
|
|
*(pos++) = '\"'; *(pos++) = ':'; *(pos++) = '\"';
|
|
|
|
pos = olm::encode_base64(
|
2016-09-02 16:13:24 +02:00
|
|
|
key.key.public_key.public_key, sizeof(key.key.public_key.public_key), pos
|
2015-07-08 16:30:34 +02:00
|
|
|
);
|
|
|
|
*(pos++) = '\"';
|
|
|
|
sep = ',';
|
|
|
|
}
|
|
|
|
if (sep != ',') {
|
2015-08-18 18:09:55 +02:00
|
|
|
/* The list was empty */
|
2015-07-08 16:30:34 +02:00
|
|
|
*(pos++) = sep;
|
|
|
|
}
|
|
|
|
*(pos++) = '}';
|
2015-07-10 12:57:53 +02:00
|
|
|
*(pos++) = '}';
|
2015-07-08 16:30:34 +02:00
|
|
|
return pos - one_time_json;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-09 17:09:16 +02:00
|
|
|
std::size_t olm::Account::mark_keys_as_published(
|
|
|
|
) {
|
|
|
|
std::size_t count = 0;
|
|
|
|
for (auto & key : one_time_keys) {
|
|
|
|
if (!key.published) {
|
|
|
|
key.published = true;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t olm::Account::max_number_of_one_time_keys(
|
2021-06-17 05:22:25 +02:00
|
|
|
) const {
|
2015-07-09 17:09:16 +02:00
|
|
|
return olm::MAX_ONE_TIME_KEYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t olm::Account::generate_one_time_keys_random_length(
|
|
|
|
std::size_t number_of_keys
|
2021-06-17 05:22:25 +02:00
|
|
|
) const {
|
2016-09-02 16:11:14 +02:00
|
|
|
return CURVE25519_RANDOM_LENGTH * number_of_keys;
|
2015-07-09 17:09:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t olm::Account::generate_one_time_keys(
|
|
|
|
std::size_t number_of_keys,
|
|
|
|
std::uint8_t const * random, std::size_t random_length
|
|
|
|
) {
|
|
|
|
if (random_length < generate_one_time_keys_random_length(number_of_keys)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
|
2015-07-09 17:09:16 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < number_of_keys; ++i) {
|
|
|
|
OneTimeKey & key = *one_time_keys.insert(one_time_keys.begin());
|
|
|
|
key.id = ++next_one_time_key_id;
|
|
|
|
key.published = false;
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_crypto_curve25519_generate_key(random, &key.key);
|
2016-09-02 16:11:14 +02:00
|
|
|
random += CURVE25519_RANDOM_LENGTH;
|
2015-07-09 17:09:16 +02:00
|
|
|
}
|
|
|
|
return number_of_keys;
|
|
|
|
}
|
|
|
|
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t olm::Account::generate_fallback_key_random_length() const {
|
2020-08-14 23:29:25 +02:00
|
|
|
return CURVE25519_RANDOM_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t olm::Account::generate_fallback_key(
|
|
|
|
std::uint8_t const * random, std::size_t random_length
|
|
|
|
) {
|
|
|
|
if (random_length < generate_fallback_key_random_length()) {
|
|
|
|
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
prev_fallback_key = current_fallback_key;
|
|
|
|
current_fallback_key.id = ++next_one_time_key_id;
|
|
|
|
current_fallback_key.published = true;
|
|
|
|
_olm_crypto_curve25519_generate_key(random, ¤t_fallback_key.key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t olm::Account::get_fallback_key_json_length(
|
2021-06-17 05:22:25 +02:00
|
|
|
) const {
|
2020-11-24 06:40:28 +01:00
|
|
|
std::size_t length = 4 + sizeof(KEY_JSON_CURVE25519) - 1; /* {"curve25519":{}} */
|
2021-06-17 05:22:25 +02:00
|
|
|
const OneTimeKey & key = current_fallback_key;
|
2020-08-14 23:29:25 +02:00
|
|
|
if (key.published) {
|
|
|
|
length += 1; /* " */
|
|
|
|
length += olm::encode_base64_length(_olm_pickle_uint32_length(key.id));
|
|
|
|
length += 3; /* ":" */
|
|
|
|
length += olm::encode_base64_length(sizeof(key.key.public_key));
|
|
|
|
length += 1; /* " */
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t olm::Account::get_fallback_key_json(
|
|
|
|
std::uint8_t * fallback_json, std::size_t fallback_json_length
|
|
|
|
) {
|
|
|
|
std::uint8_t * pos = fallback_json;
|
|
|
|
if (fallback_json_length < get_fallback_key_json_length()) {
|
|
|
|
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
*(pos++) = '{';
|
|
|
|
pos = write_string(pos, KEY_JSON_CURVE25519);
|
|
|
|
*(pos++) = '{';
|
|
|
|
OneTimeKey & key = current_fallback_key;
|
|
|
|
if (key.published) {
|
|
|
|
*(pos++) = '\"';
|
|
|
|
std::uint8_t key_id[_olm_pickle_uint32_length(key.id)];
|
|
|
|
_olm_pickle_uint32(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.public_key, sizeof(key.key.public_key.public_key), pos
|
|
|
|
);
|
|
|
|
*(pos++) = '\"';
|
|
|
|
}
|
|
|
|
*(pos++) = '}';
|
|
|
|
*(pos++) = '}';
|
|
|
|
return pos - fallback_json;
|
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
namespace olm {
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2015-07-07 17:42:03 +02:00
|
|
|
static std::size_t pickle_length(
|
|
|
|
olm::IdentityKeys const & value
|
|
|
|
) {
|
|
|
|
size_t length = 0;
|
2016-09-05 13:59:12 +02:00
|
|
|
length += _olm_pickle_ed25519_key_pair_length(&value.ed25519_key);
|
2015-07-07 17:42:03 +02:00
|
|
|
length += olm::pickle_length(value.curve25519_key);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
|
|
|
olm::IdentityKeys const & value
|
|
|
|
) {
|
2016-09-05 13:59:12 +02:00
|
|
|
pos = _olm_pickle_ed25519_key_pair(pos, &value.ed25519_key);
|
2015-07-07 17:42:03 +02:00
|
|
|
pos = olm::pickle(pos, value.curve25519_key);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::uint8_t const * unpickle(
|
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
|
|
|
olm::IdentityKeys & value
|
|
|
|
) {
|
2021-07-06 12:36:39 +02:00
|
|
|
pos = _olm_unpickle_ed25519_key_pair(pos, end, &value.ed25519_key); UNPICKLE_OK(pos);
|
|
|
|
pos = olm::unpickle(pos, end, value.curve25519_key); UNPICKLE_OK(pos);
|
2015-07-07 17:42:03 +02:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
static std::size_t pickle_length(
|
2015-07-07 17:42:03 +02:00
|
|
|
olm::OneTimeKey const & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-07-09 17:09:16 +02:00
|
|
|
std::size_t length = 0;
|
|
|
|
length += olm::pickle_length(value.id);
|
|
|
|
length += olm::pickle_length(value.published);
|
|
|
|
length += olm::pickle_length(value.key);
|
|
|
|
return length;
|
2015-06-12 17:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
2015-07-07 17:42:03 +02:00
|
|
|
olm::OneTimeKey const & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
pos = olm::pickle(pos, value.id);
|
2015-07-09 17:09:16 +02:00
|
|
|
pos = olm::pickle(pos, value.published);
|
2015-06-27 01:15:23 +02:00
|
|
|
pos = olm::pickle(pos, value.key);
|
2015-06-12 17:15:37 +02:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::uint8_t const * unpickle(
|
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
2015-07-07 17:42:03 +02:00
|
|
|
olm::OneTimeKey & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2021-07-06 12:36:39 +02:00
|
|
|
pos = olm::unpickle(pos, end, value.id); UNPICKLE_OK(pos);
|
|
|
|
pos = olm::unpickle(pos, end, value.published); UNPICKLE_OK(pos);
|
|
|
|
pos = olm::unpickle(pos, end, value.key); UNPICKLE_OK(pos);
|
2015-06-12 17:15:37 +02:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
} // namespace olm
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2015-07-10 19:00:18 +02:00
|
|
|
namespace {
|
2016-09-01 14:35:23 +02:00
|
|
|
// pickle version 1 used only 32 bytes for the ed25519 private key.
|
|
|
|
// Any keys thus used should be considered compromised.
|
2020-08-14 23:29:25 +02:00
|
|
|
// pickle version 2 does not have fallback keys.
|
|
|
|
static const std::uint32_t ACCOUNT_PICKLE_VERSION = 3;
|
2015-07-10 19:00:18 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t olm::pickle_length(
|
|
|
|
olm::Account const & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
|
|
|
std::size_t length = 0;
|
2015-07-10 19:00:18 +02:00
|
|
|
length += olm::pickle_length(ACCOUNT_PICKLE_VERSION);
|
2015-07-07 17:42:03 +02:00
|
|
|
length += olm::pickle_length(value.identity_keys);
|
2015-06-27 01:15:23 +02:00
|
|
|
length += olm::pickle_length(value.one_time_keys);
|
2020-08-14 23:29:25 +02:00
|
|
|
length += olm::pickle_length(value.current_fallback_key);
|
|
|
|
length += olm::pickle_length(value.prev_fallback_key);
|
2015-07-09 17:09:16 +02:00
|
|
|
length += olm::pickle_length(value.next_one_time_key_id);
|
2015-06-12 17:15:37 +02:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::uint8_t * olm::pickle(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t * pos,
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::Account const & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-07-10 19:00:18 +02:00
|
|
|
pos = olm::pickle(pos, ACCOUNT_PICKLE_VERSION);
|
2015-07-07 17:42:03 +02:00
|
|
|
pos = olm::pickle(pos, value.identity_keys);
|
2015-06-27 01:15:23 +02:00
|
|
|
pos = olm::pickle(pos, value.one_time_keys);
|
2020-08-14 23:29:25 +02:00
|
|
|
pos = olm::pickle(pos, value.current_fallback_key);
|
|
|
|
pos = olm::pickle(pos, value.prev_fallback_key);
|
2015-07-09 17:09:16 +02:00
|
|
|
pos = olm::pickle(pos, value.next_one_time_key_id);
|
2015-06-12 17:15:37 +02:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::uint8_t const * olm::unpickle(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::Account & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-07-10 19:00:18 +02:00
|
|
|
uint32_t pickle_version;
|
2021-07-06 12:36:39 +02:00
|
|
|
|
|
|
|
pos = olm::unpickle(pos, end, pickle_version); UNPICKLE_OK(pos);
|
|
|
|
|
2016-09-01 14:35:23 +02:00
|
|
|
switch (pickle_version) {
|
|
|
|
case ACCOUNT_PICKLE_VERSION:
|
2020-08-14 23:29:25 +02:00
|
|
|
case 2:
|
2016-09-01 14:35:23 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
value.last_error = OlmErrorCode::OLM_BAD_LEGACY_ACCOUNT_PICKLE;
|
2021-07-06 12:36:39 +02:00
|
|
|
return nullptr;
|
2016-09-01 14:35:23 +02:00
|
|
|
default:
|
|
|
|
value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
|
2021-07-06 12:36:39 +02:00
|
|
|
return nullptr;
|
2015-07-10 19:00:18 +02:00
|
|
|
}
|
2021-07-06 12:36:39 +02:00
|
|
|
|
|
|
|
pos = olm::unpickle(pos, end, value.identity_keys); UNPICKLE_OK(pos);
|
|
|
|
pos = olm::unpickle(pos, end, value.one_time_keys); UNPICKLE_OK(pos);
|
|
|
|
|
2020-08-14 23:29:25 +02:00
|
|
|
if (pickle_version == 2) {
|
|
|
|
// version 2 did not have fallback keys
|
|
|
|
value.current_fallback_key.published = false;
|
|
|
|
value.prev_fallback_key.published = false;
|
|
|
|
} else {
|
2021-07-06 12:36:39 +02:00
|
|
|
pos = olm::unpickle(pos, end, value.current_fallback_key); UNPICKLE_OK(pos);
|
|
|
|
pos = olm::unpickle(pos, end, value.prev_fallback_key); UNPICKLE_OK(pos);
|
2020-08-14 23:29:25 +02:00
|
|
|
}
|
2021-07-06 12:36:39 +02:00
|
|
|
|
|
|
|
pos = olm::unpickle(pos, end, value.next_one_time_key_id); UNPICKLE_OK(pos);
|
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
return pos;
|
|
|
|
}
|