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
|
|
|
#ifndef OLM_ACCOUNT_HH_
|
|
|
|
#define OLM_ACCOUNT_HH_
|
2015-06-11 19:03:22 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/list.hh"
|
2016-09-02 16:35:04 +02:00
|
|
|
#include "olm/crypto.h"
|
2016-05-16 13:08:45 +02:00
|
|
|
#include "olm/error.h"
|
2015-06-11 19:03:22 +02:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
namespace olm {
|
2015-06-11 19:03:22 +02:00
|
|
|
|
|
|
|
|
2015-07-07 17:42:03 +02:00
|
|
|
struct IdentityKeys {
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_ed25519_key_pair ed25519_key;
|
|
|
|
_olm_curve25519_key_pair curve25519_key;
|
2015-07-07 17:42:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct OneTimeKey {
|
2015-06-11 19:03:22 +02:00
|
|
|
std::uint32_t id;
|
2015-07-09 17:09:16 +02:00
|
|
|
bool published;
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_key_pair key;
|
2015-06-11 19:03:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static std::size_t const MAX_ONE_TIME_KEYS = 100;
|
|
|
|
|
2015-07-07 10:01:38 +02:00
|
|
|
|
2015-06-11 19:03:22 +02:00
|
|
|
struct Account {
|
2015-07-09 17:09:16 +02:00
|
|
|
Account();
|
2015-07-07 17:42:03 +02:00
|
|
|
IdentityKeys identity_keys;
|
|
|
|
List<OneTimeKey, MAX_ONE_TIME_KEYS> one_time_keys;
|
2021-11-17 20:30:04 +01:00
|
|
|
std::uint8_t num_fallback_keys;
|
2020-08-14 23:29:25 +02:00
|
|
|
OneTimeKey current_fallback_key;
|
|
|
|
OneTimeKey prev_fallback_key;
|
2015-07-09 17:09:16 +02:00
|
|
|
std::uint32_t next_one_time_key_id;
|
2016-05-16 13:08:45 +02:00
|
|
|
OlmErrorCode last_error;
|
2015-06-11 19:03:22 +02:00
|
|
|
|
|
|
|
/** Number of random bytes needed to create a new account */
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t new_account_random_length() const;
|
2015-06-11 19:03:22 +02:00
|
|
|
|
2015-07-09 17:09:16 +02:00
|
|
|
/** Create a new account. Returns std::size_t(-1) on error. If the number of
|
|
|
|
* random bytes is too small then last_error will be NOT_ENOUGH_RANDOM */
|
2015-06-12 15:09:41 +02:00
|
|
|
std::size_t new_account(
|
2015-06-11 19:03:22 +02:00
|
|
|
uint8_t const * random, std::size_t random_length
|
|
|
|
);
|
|
|
|
|
2015-07-07 17:42:03 +02:00
|
|
|
/** Number of bytes needed to output the identity keys for this account */
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t get_identity_json_length() const;
|
2015-07-07 17:42:03 +02:00
|
|
|
|
|
|
|
/** Output the identity keys for this account as JSON in the following
|
2015-07-09 17:09:16 +02:00
|
|
|
* format:
|
2015-07-07 17:42:03 +02:00
|
|
|
*
|
2015-07-10 12:57:53 +02:00
|
|
|
* {"curve25519":"<43 base64 characters>"
|
|
|
|
* ,"ed25519":"<43 base64 characters>"
|
2015-07-09 17:09:16 +02:00
|
|
|
* }
|
|
|
|
*
|
2015-07-08 16:30:34 +02:00
|
|
|
*
|
|
|
|
* Returns the size of the JSON written or std::size_t(-1) on error.
|
|
|
|
* If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL. */
|
2015-07-07 17:42:03 +02:00
|
|
|
std::size_t get_identity_json(
|
2015-07-08 12:16:00 +02:00
|
|
|
std::uint8_t * identity_json, std::size_t identity_json_length
|
2015-07-07 17:42:03 +02:00
|
|
|
);
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
/**
|
|
|
|
* The length of an ed25519 signature in bytes.
|
|
|
|
*/
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t signature_length() const;
|
2015-07-10 12:57:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs a message with the ed25519 key for this account.
|
|
|
|
*/
|
|
|
|
std::size_t sign(
|
|
|
|
std::uint8_t const * message, std::size_t message_length,
|
|
|
|
std::uint8_t * signature, std::size_t signature_length
|
|
|
|
);
|
|
|
|
|
2015-07-08 16:30:34 +02:00
|
|
|
/** Number of bytes needed to output the one time keys for this account */
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t get_one_time_keys_json_length() const;
|
2015-07-08 16:30:34 +02:00
|
|
|
|
2015-07-09 17:09:16 +02:00
|
|
|
/** Output the one time keys that haven't been published yet as JSON:
|
|
|
|
*
|
2015-07-10 12:57:53 +02:00
|
|
|
* {"curve25519":
|
|
|
|
* ["<6 byte key id>":"<43 base64 characters>"
|
|
|
|
* ,"<6 byte key id>":"<43 base64 characters>"
|
2015-07-09 17:09:16 +02:00
|
|
|
* ...
|
2015-07-10 12:57:53 +02:00
|
|
|
* ]
|
2015-07-09 17:09:16 +02:00
|
|
|
* }
|
|
|
|
*
|
2015-07-08 16:30:34 +02:00
|
|
|
* Returns the size of the JSON written or std::size_t(-1) on error.
|
|
|
|
* If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL.
|
|
|
|
*/
|
|
|
|
std::size_t get_one_time_keys_json(
|
|
|
|
std::uint8_t * one_time_json, std::size_t one_time_json_length
|
|
|
|
);
|
|
|
|
|
2021-11-23 22:13:42 +01:00
|
|
|
/** Mark the current list of one_time_keys and the current fallback key as
|
|
|
|
* being published. The current one time keys will no longer be returned by
|
|
|
|
* get_one_time_keys_json() and the current fallback key will no longer be
|
|
|
|
* returned by get_unpublished_fallback_key_json(). */
|
2015-07-09 17:09:16 +02:00
|
|
|
std::size_t mark_keys_as_published();
|
|
|
|
|
|
|
|
/** The largest number of one time keys this account can store. */
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t max_number_of_one_time_keys() const;
|
2015-07-09 17:09:16 +02:00
|
|
|
|
2015-07-09 19:35:54 +02:00
|
|
|
/** The number of random bytes needed to generate a given number of new one
|
|
|
|
* time keys. */
|
2015-07-09 17:09:16 +02:00
|
|
|
std::size_t generate_one_time_keys_random_length(
|
|
|
|
std::size_t number_of_keys
|
2021-06-17 05:22:25 +02:00
|
|
|
) const;
|
2015-07-09 17:09:16 +02:00
|
|
|
|
|
|
|
/** Generates a number of new one time keys. If the total number of keys
|
|
|
|
* stored by this account exceeds max_number_of_one_time_keys() then the
|
|
|
|
* old keys are discarded. Returns std::size_t(-1) on error. If the number
|
|
|
|
* of random bytes is too small then last_error will be NOT_ENOUGH_RANDOM */
|
|
|
|
std::size_t generate_one_time_keys(
|
|
|
|
std::size_t number_of_keys,
|
|
|
|
std::uint8_t const * random, std::size_t random_length
|
|
|
|
);
|
|
|
|
|
2020-08-14 23:29:25 +02:00
|
|
|
/** The number of random bytes needed to generate a fallback key. */
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t generate_fallback_key_random_length() const;
|
2020-08-14 23:29:25 +02:00
|
|
|
|
|
|
|
/** Generates a new fallback key. Returns std::size_t(-1) on error. If the
|
|
|
|
* number of random bytes is too small then last_error will be
|
|
|
|
* NOT_ENOUGH_RANDOM */
|
|
|
|
std::size_t generate_fallback_key(
|
|
|
|
std::uint8_t const * random, std::size_t random_length
|
|
|
|
);
|
|
|
|
|
2021-11-19 23:48:05 +01:00
|
|
|
/** Number of bytes needed to output the fallback keys for this account */
|
2021-06-17 05:22:25 +02:00
|
|
|
std::size_t get_fallback_key_json_length() const;
|
2020-08-14 23:29:25 +02:00
|
|
|
|
2021-11-17 20:30:04 +01:00
|
|
|
/** Deprecated: use get_unpublished_fallback_key_json instead */
|
|
|
|
std::size_t get_fallback_key_json(
|
|
|
|
std::uint8_t * fallback_json, std::size_t fallback_json_length
|
|
|
|
);
|
|
|
|
|
2021-11-19 23:48:05 +01:00
|
|
|
/** Number of bytes needed to output the unpublished fallback keys for this
|
|
|
|
* account */
|
|
|
|
std::size_t get_unpublished_fallback_key_json_length() const;
|
|
|
|
|
2020-08-14 23:29:25 +02:00
|
|
|
/** Output the fallback key as JSON:
|
|
|
|
*
|
|
|
|
* {"curve25519":
|
|
|
|
* ["<6 byte key id>":"<43 base64 characters>"
|
|
|
|
* ,"<6 byte key id>":"<43 base64 characters>"
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*
|
2021-11-17 20:30:04 +01:00
|
|
|
* if there is a fallback key and it has not been published yet.
|
|
|
|
*
|
2020-08-14 23:29:25 +02:00
|
|
|
* Returns the size of the JSON written or std::size_t(-1) on error.
|
|
|
|
* If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL.
|
|
|
|
*/
|
2021-11-17 20:30:04 +01:00
|
|
|
std::size_t get_unpublished_fallback_key_json(
|
2020-08-14 23:29:25 +02:00
|
|
|
std::uint8_t * fallback_json, std::size_t fallback_json_length
|
|
|
|
);
|
|
|
|
|
2021-11-20 03:43:27 +01:00
|
|
|
/** Forget about the old fallback key */
|
|
|
|
void forget_old_fallback_key();
|
|
|
|
|
2015-07-08 17:00:08 +02:00
|
|
|
/** Lookup a one time key with the given public key */
|
2015-07-07 17:42:03 +02:00
|
|
|
OneTimeKey const * lookup_key(
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key const & public_key
|
2015-06-12 15:09:41 +02:00
|
|
|
);
|
2015-06-22 12:02:42 +02:00
|
|
|
|
2015-07-08 17:00:08 +02:00
|
|
|
/** Remove a one time key with the given public key */
|
2015-06-22 12:02:42 +02:00
|
|
|
std::size_t 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-06-12 17:15:37 +02:00
|
|
|
};
|
2015-06-12 15:09:41 +02:00
|
|
|
|
2015-06-11 19:03:22 +02:00
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
std::size_t pickle_length(
|
|
|
|
Account const & value
|
|
|
|
);
|
2015-06-11 19:03:22 +02:00
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
|
|
|
Account const & value
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::uint8_t const * unpickle(
|
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
|
|
|
Account & value
|
|
|
|
);
|
2015-06-11 19:03:22 +02:00
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
} // namespace olm
|
2015-06-11 19:03:22 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
#endif /* OLM_ACCOUNT_HH_ */
|