2015-06-22 18:50:56 +02:00
|
|
|
/* Copyright 2015 OpenMarket Ltd
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "olm/pickle.hh"
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::LocalKey const * olm::Account::lookup_key(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint32_t id
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
for (olm::LocalKey const & key : one_time_keys) {
|
2015-06-12 17:15:37 +02:00
|
|
|
if (key.id == id) return &key;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t olm::Account::remove_key(
|
2015-06-22 12:02:42 +02:00
|
|
|
std::uint32_t id
|
|
|
|
) {
|
|
|
|
LocalKey * i;
|
|
|
|
for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) {
|
|
|
|
if (i->id == id) {
|
|
|
|
one_time_keys.erase(i);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-06-15 18:47:22 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t olm::Account::new_account_random_length() {
|
2015-06-15 18:47:22 +02:00
|
|
|
return 103 * 32;
|
|
|
|
}
|
|
|
|
|
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()) {
|
2015-06-27 01:15:23 +02:00
|
|
|
last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
|
2015-06-22 12:02:42 +02:00
|
|
|
return std::size_t(-1);
|
2015-06-15 18:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned id = 0;
|
|
|
|
|
|
|
|
identity_key.id = ++id;
|
2015-07-07 10:03:12 +02:00
|
|
|
olm::curve25519_generate_key(random, identity_key.key);
|
2015-06-15 18:47:22 +02:00
|
|
|
random += 32;
|
|
|
|
|
|
|
|
random += 32;
|
|
|
|
|
2015-06-23 18:49:04 +02:00
|
|
|
for (unsigned i = 0; i < 10; ++i) {
|
2015-06-15 18:47:22 +02:00
|
|
|
LocalKey & key = *one_time_keys.insert(one_time_keys.end());
|
|
|
|
key.id = ++id;
|
2015-07-07 10:03:12 +02:00
|
|
|
olm::curve25519_generate_key(random, key.key);
|
2015-06-15 18:47:22 +02:00
|
|
|
random += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
namespace olm {
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
static std::size_t pickle_length(
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::LocalKey const & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
return olm::pickle_length(value.id) + olm::pickle_length(value.key);
|
2015-06-12 17:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::LocalKey const & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
pos = olm::pickle(pos, value.id);
|
|
|
|
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-06-27 01:15:23 +02:00
|
|
|
olm::LocalKey & value
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
pos = olm::unpickle(pos, end, value.id);
|
|
|
|
pos = olm::unpickle(pos, end, value.key);
|
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-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-06-27 01:15:23 +02:00
|
|
|
length += olm::pickle_length(value.identity_key);
|
|
|
|
length += olm::pickle_length(value.one_time_keys);
|
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-06-27 01:15:23 +02:00
|
|
|
pos = olm::pickle(pos, value.identity_key);
|
|
|
|
pos = olm::pickle(pos, value.one_time_keys);
|
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-06-27 01:15:23 +02:00
|
|
|
pos = olm::unpickle(pos, end, value.identity_key);
|
|
|
|
pos = olm::unpickle(pos, end, value.one_time_keys);
|
2015-06-12 17:15:37 +02:00
|
|
|
return pos;
|
|
|
|
}
|