From 4127a84b3dccc40ed9d4f5ea7945ca4534ec1314 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Fri, 19 Nov 2021 17:48:05 -0500 Subject: [PATCH] add function for getting length of unpublished fallback keys and fix a typo --- include/olm/account.hh | 6 +++++- include/olm/olm.h | 6 ++++++ src/account.cpp | 16 +++++++++++++++- src/olm.cpp | 7 +++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/olm/account.hh b/include/olm/account.hh index 7002f51..88302ba 100644 --- a/include/olm/account.hh +++ b/include/olm/account.hh @@ -139,7 +139,7 @@ struct Account { 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; /** 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 ); + /** 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: * * {"curve25519": diff --git a/include/olm/olm.h b/include/olm/olm.h index 0e98e77..cdb35a2 100644 --- a/include/olm/olm.h +++ b/include/olm/olm.h @@ -297,6 +297,12 @@ OLM_EXPORT size_t olm_account_fallback_key( 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 * fallback_key buffer */ OLM_EXPORT size_t olm_account_unpublished_fallback_key( diff --git a/src/account.cpp b/src/account.cpp index cebd89f..80768ae 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -356,11 +356,25 @@ std::size_t olm::Account::get_fallback_key_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::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()) { + if (fallback_json_length < get_unpublished_fallback_key_json_length()) { last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } diff --git a/src/olm.cpp b/src/olm.cpp index 2f3e777..f7fbc42 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -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( OlmAccount * account, void * fallback_key_json, size_t fallback_key_json_length