add function for getting length of unpublished fallback keys

and fix a typo
This commit is contained in:
Hubert Chathi 2021-11-19 17:48:05 -05:00
parent 3b6ff327c0
commit 4127a84b3d
4 changed files with 33 additions and 2 deletions

View file

@ -139,7 +139,7 @@ struct Account {
std::uint8_t const * random, std::size_t random_length std::uint8_t const * random, std::size_t random_length
); );
/** Number of bytes needed to output the one time keys for this account */ /** Number of bytes needed to output the fallback keys for this account */
std::size_t get_fallback_key_json_length() const; std::size_t get_fallback_key_json_length() const;
/** Deprecated: use get_unpublished_fallback_key_json instead */ /** Deprecated: use get_unpublished_fallback_key_json instead */
@ -147,6 +147,10 @@ struct Account {
std::uint8_t * fallback_json, std::size_t fallback_json_length std::uint8_t * fallback_json, std::size_t fallback_json_length
); );
/** Number of bytes needed to output the unpublished fallback keys for this
* account */
std::size_t get_unpublished_fallback_key_json_length() const;
/** Output the fallback key as JSON: /** Output the fallback key as JSON:
* *
* {"curve25519": * {"curve25519":

View file

@ -297,6 +297,12 @@ OLM_EXPORT size_t olm_account_fallback_key(
void * fallback_key, size_t fallback_key_size void * fallback_key, size_t fallback_key_size
); );
/** The number of bytes needed to hold the unpublished fallback key as returned
* by olm_account_unpublished fallback_key. */
OLM_EXPORT size_t olm_account_unpublished_fallback_key_length(
OlmAccount const * account
);
/** Returns the fallback key (if present, and if unpublished) into the /** Returns the fallback key (if present, and if unpublished) into the
* fallback_key buffer */ * fallback_key buffer */
OLM_EXPORT size_t olm_account_unpublished_fallback_key( OLM_EXPORT size_t olm_account_unpublished_fallback_key(

View file

@ -356,11 +356,25 @@ std::size_t olm::Account::get_fallback_key_json(
return pos - fallback_json; return pos - fallback_json;
} }
std::size_t olm::Account::get_unpublished_fallback_key_json_length(
) const {
std::size_t length = 4 + sizeof(KEY_JSON_CURVE25519) - 1; /* {"curve25519":{}} */
const OneTimeKey & key = current_fallback_key;
if (num_fallback_keys >= 1 && !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_unpublished_fallback_key_json( std::size_t olm::Account::get_unpublished_fallback_key_json(
std::uint8_t * fallback_json, std::size_t fallback_json_length std::uint8_t * fallback_json, std::size_t fallback_json_length
) { ) {
std::uint8_t * pos = fallback_json; std::uint8_t * pos = fallback_json;
if (fallback_json_length < get_fallback_key_json_length()) { if (fallback_json_length < get_unpublished_fallback_key_json_length()) {
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1); return std::size_t(-1);
} }

View file

@ -495,6 +495,13 @@ size_t olm_account_fallback_key(
} }
size_t olm_account_unpublished_fallback_key_length(
OlmAccount const * account
) {
return from_c(account)->get_unpublished_fallback_key_json_length();
}
size_t olm_account_unpublished_fallback_key( size_t olm_account_unpublished_fallback_key(
OlmAccount * account, OlmAccount * account,
void * fallback_key_json, size_t fallback_key_json_length void * fallback_key_json, size_t fallback_key_json_length