diff --git a/CMakeLists.txt b/CMakeLists.txt index f450913675b3959fa8e0098221bb1724b8369047..7e82ba721a31362d4134d90a1f66e81a0cd724f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") endif() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always" ) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always -fsized-deallocation" ) endif() endif() diff --git a/src/crypto.cpp b/src/crypto.cpp index 270695dafb5ed43fd029cb6569bf7cb016ec73df..a111fb508b58ede65cfbffd2e6a1c1924db94abe 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -47,18 +47,18 @@ OlmClient::identity_keys() return json::parse(std::string(tmp_buf->begin(), tmp_buf->end())); } -std::unique_ptr<BinaryBuf> +std::string OlmClient::sign_message(const std::string &msg) { // Message buffer - auto buf = str_to_buffer(msg); + auto buf = to_buffer(msg); // Signature buffer auto signature_buf = create_buffer(olm_account_signature_length(account_.get())); olm_account_sign( account_.get(), buf->data(), buf->size(), signature_buf->data(), signature_buf->size()); - return signature_buf; + return std::string(signature_buf->begin(), signature_buf->end()); } std::string @@ -75,8 +75,7 @@ OlmClient::sign_identity_keys() {"ed25519:" + device_id_, keys.ed25519}, }}}; - return encode_base64(sign_message(body.dump())->data(), - olm_account_signature_length(account_.get())); + return sign_message(body.dump()); } std::size_t @@ -113,10 +112,7 @@ std::string OlmClient::sign_one_time_key(const std::string &key) { json j{{"key", key}}; - auto str_json = j.dump(); - auto signature = sign_message(j.dump()); - - return encode_base64(signature->data(), signature->size()); + return sign_message(j.dump()); } std::map<std::string, json> @@ -189,7 +185,7 @@ OlmClient::init_outbound_group_session() } std::unique_ptr<BinaryBuf> -mtx::client::crypto::str_to_buffer(const std::string &data) +mtx::client::crypto::to_buffer(const std::string &data) { auto str_pointer = reinterpret_cast<const uint8_t *>(&data[0]); const auto nbytes = data.size(); @@ -210,7 +206,7 @@ mtx::client::crypto::decode_base64(const std::string &data) throw std::runtime_error("invalid base64 input length"); auto output_buf = create_buffer(output_nbytes); - auto input_buf = str_to_buffer(data); + auto input_buf = to_buffer(data); olm::decode_base64(input_buf->data(), nbytes, output_buf->data()); @@ -232,9 +228,9 @@ mtx::client::crypto::encode_base64(const uint8_t *data, std::size_t len) } std::unique_ptr<BinaryBuf> -mtx::client::crypto::json_to_buffer(const nlohmann::json &obj) +mtx::client::crypto::to_buffer(const nlohmann::json &obj) { - return str_to_buffer(obj.dump()); + return to_buffer(obj.dump()); } std::string @@ -243,7 +239,7 @@ mtx::client::crypto::session_id(OlmOutboundGroupSession *s) auto tmp = create_buffer(olm_outbound_group_session_id_length(s)); olm_outbound_group_session_id(s, tmp->data(), tmp->size()); - return encode_base64(tmp->data(), tmp->size()); + return std::string(tmp->begin(), tmp->end()); } std::string @@ -252,5 +248,5 @@ mtx::client::crypto::session_key(OlmOutboundGroupSession *s) auto tmp = create_buffer(olm_outbound_group_session_key_length(s)); olm_outbound_group_session_key(s, tmp->data(), tmp->size()); - return encode_base64(tmp->data(), tmp->size()); + return std::string(tmp->begin(), tmp->end()); } diff --git a/src/crypto.hpp b/src/crypto.hpp index d5b9a59034aab815cc65bb322385ec7d4598d726..66b66fba0e245fb5d91e60b90ec8227bcb2fd636 100644 --- a/src/crypto.hpp +++ b/src/crypto.hpp @@ -180,7 +180,7 @@ public: void set_user_id(std::string user_id) { user_id_ = std::move(user_id); } //! Sign the given message. - std::unique_ptr<BinaryBuf> sign_message(const std::string &msg); + Base64String sign_message(const std::string &msg); template<class T> std::unique_ptr<T, OlmDeleter> create_olm_object() @@ -236,11 +236,11 @@ decode_base64(const std::string &data); //! Convert the given json struct to an uint8_t buffer. std::unique_ptr<BinaryBuf> -json_to_buffer(const nlohmann::json &obj); +to_buffer(const nlohmann::json &obj); //! Convert the given string to an uint8_t buffer. std::unique_ptr<BinaryBuf> -str_to_buffer(const std::string &data); +to_buffer(const std::string &data); //! Retrieve the session id. std::string diff --git a/tests/utils.cpp b/tests/utils.cpp index bfef609fef67cefd60458a20815a0c22a24f05fd..705e88e439007c3c8e8886e010081056822b7a92 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -14,7 +14,7 @@ using namespace std; TEST(Utilities, JsonToBuffer) { auto msg = json({{"key", "text"}}); - auto buf = json_to_buffer(msg); + auto buf = to_buffer(msg); EXPECT_EQ(std::string(buf->begin(), buf->end()), msg.dump()); } @@ -33,13 +33,13 @@ TEST(Utilities, VerifySignedOneTimeKey) auto first_key = keys.curve25519.begin()->second; auto msg = json({{"key", first_key}}).dump(); - auto sig_buf = alice->sign_message(msg); + auto sig_buf = to_buffer(alice->sign_message(msg)); auto res = olm_ed25519_verify(alice->utility(), - str_to_buffer(alice->identity_keys().ed25519)->data(), - str_to_buffer(alice->identity_keys().ed25519)->size(), - str_to_buffer(msg)->data(), - str_to_buffer(msg)->size(), + to_buffer(alice->identity_keys().ed25519)->data(), + to_buffer(alice->identity_keys().ed25519)->size(), + to_buffer(msg)->data(), + to_buffer(msg)->size(), sig_buf->data(), sig_buf->size()); @@ -63,13 +63,13 @@ TEST(Utilities, VerifySignedIdentityKeys) {"ed25519:some_device", keys["ed25519"]}}}}) .dump(); - auto sig_buf = alice->sign_message(msg); + auto sig_buf = to_buffer(alice->sign_message(msg)); auto res = olm_ed25519_verify(alice->utility(), - str_to_buffer(alice->identity_keys().ed25519)->data(), - str_to_buffer(alice->identity_keys().ed25519)->size(), - str_to_buffer(msg)->data(), - str_to_buffer(msg)->size(), + to_buffer(alice->identity_keys().ed25519)->data(), + to_buffer(alice->identity_keys().ed25519)->size(), + to_buffer(msg)->data(), + to_buffer(msg)->size(), sig_buf->data(), sig_buf->size());