diff --git a/src/client.cpp b/src/client.cpp index bb8a7e324953a1e48fcfece994ca93ef11b6f3ec..a697780a1c346a3a77c89e254cbbf999f3f183ec 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -144,10 +144,14 @@ Client::remove_session(std::shared_ptr<Session> s) ec.assign(0, ec.category()); } - // Ignoring short_read error. - if ((ec.category() == boost::asio::error::get_ssl_category()) && - (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ)) +// SSL_R_SHORT_READ is removed in openssl-1.1 +#if defined SSL_R_SHORT_READ + if (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ) return; +#else + if (ERR_GET_REASON(ec.value()) == boost::asio::ssl::error::stream_truncated) + return; +#endif if (ec) // TODO: propagate the error. diff --git a/src/crypto.cpp b/src/crypto.cpp index a111fb508b58ede65cfbffd2e6a1c1924db94abe..53a0220b2855402f7fd6c1c2a116080a27d585e6 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -50,13 +50,9 @@ OlmClient::identity_keys() std::string OlmClient::sign_message(const std::string &msg) { - // Message buffer - 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()); + account_.get(), msg.data(), msg.size(), signature_buf->data(), signature_buf->size()); return std::string(signature_buf->begin(), signature_buf->end()); } @@ -185,30 +181,17 @@ OlmClient::init_outbound_group_session() } std::unique_ptr<BinaryBuf> -mtx::client::crypto::to_buffer(const std::string &data) -{ - auto str_pointer = reinterpret_cast<const uint8_t *>(&data[0]); - const auto nbytes = data.size(); - - auto buf = create_buffer(nbytes); - memcpy(buf->data(), str_pointer, buf->size()); - - return buf; -} - -std::unique_ptr<BinaryBuf> -mtx::client::crypto::decode_base64(const std::string &data) +mtx::client::crypto::decode_base64(const std::string &msg) { - const auto nbytes = data.size(); - const int output_nbytes = olm::decode_base64_length(nbytes); + const int output_nbytes = olm::decode_base64_length(msg.size()); if (output_nbytes == -1) throw std::runtime_error("invalid base64 input length"); auto output_buf = create_buffer(output_nbytes); - auto input_buf = to_buffer(data); - olm::decode_base64(input_buf->data(), nbytes, output_buf->data()); + olm::decode_base64( + reinterpret_cast<const uint8_t *>(msg.data()), msg.size(), output_buf->data()); return output_buf; } @@ -227,12 +210,6 @@ mtx::client::crypto::encode_base64(const uint8_t *data, std::size_t len) return std::string(output_buf->begin(), output_buf->end()); } -std::unique_ptr<BinaryBuf> -mtx::client::crypto::to_buffer(const nlohmann::json &obj) -{ - return to_buffer(obj.dump()); -} - std::string mtx::client::crypto::session_id(OlmOutboundGroupSession *s) { diff --git a/src/crypto.hpp b/src/crypto.hpp index 66b66fba0e245fb5d91e60b90ec8227bcb2fd636..a935eb6cde545b6fe8c1e22414c68c59b1c3daa6 100644 --- a/src/crypto.hpp +++ b/src/crypto.hpp @@ -234,14 +234,6 @@ encode_base64(const uint8_t *data, std::size_t len); std::unique_ptr<BinaryBuf> decode_base64(const std::string &data); -//! Convert the given json struct to an uint8_t buffer. -std::unique_ptr<BinaryBuf> -to_buffer(const nlohmann::json &obj); - -//! Convert the given string to an uint8_t buffer. -std::unique_ptr<BinaryBuf> -to_buffer(const std::string &data); - //! Retrieve the session id. std::string session_id(OlmOutboundGroupSession *s); diff --git a/tests/e2ee.cpp b/tests/e2ee.cpp index 0dbf58ecba75cd810c6ca71512846b24c5081355..265c7734e6748a87ed69ef99eeaf30577007868c 100644 --- a/tests/e2ee.cpp +++ b/tests/e2ee.cpp @@ -357,20 +357,20 @@ TEST(Encryption, ClaimKeys) alice_olm->create_new_utility(); auto msg = json{{"key", contents.at("key").get<std::string>()}}.dump(); - auto identity_keys = to_buffer(bob_ed25519); - auto signature = to_buffer(contents.at("signatures") - .at(user_id) - .at("ed25519:" + device_id) - .get<std::string>()); + auto signature = contents.at("signatures") + .at(user_id) + .at("ed25519:" + device_id) + .get<std::string>(); // Verify signature. - auto ret = olm_ed25519_verify(alice_olm->utility(), - identity_keys->data(), - identity_keys->size(), - msg.data(), - msg.size(), - signature->data(), - signature->size()); + auto ret = olm_ed25519_verify( + alice_olm->utility(), + reinterpret_cast<const uint8_t *>(bob_ed25519.data()), + bob_ed25519.size(), + msg.data(), + msg.size(), + reinterpret_cast<uint8_t *>(&signature[0]), + signature.size()); EXPECT_EQ(std::string(olm_utility_last_error(alice_olm->utility())), "SUCCESS"); diff --git a/tests/utils.cpp b/tests/utils.cpp index a850f5bd9e84c8105bdbca9adf86151842c92733..63879e6e2bdbc168fafa0bd240e32e09208902da 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -49,14 +49,6 @@ TEST(Utilities, CanonicalJSON) EXPECT_EQ(data3.dump(), "{\"a\":null}"); } -TEST(Utilities, JsonToBuffer) -{ - auto msg = json({{"key", "text"}}); - auto buf = to_buffer(msg); - - EXPECT_EQ(std::string(buf->begin(), buf->end()), msg.dump()); -} - TEST(Utilities, VerifySignedOneTimeKey) { auto alice = make_shared<OlmClient>(); @@ -71,15 +63,16 @@ TEST(Utilities, VerifySignedOneTimeKey) auto first_key = keys.curve25519.begin()->second; auto msg = json({{"key", first_key}}).dump(); - auto sig_buf = to_buffer(alice->sign_message(msg)); + auto sig = alice->sign_message(msg); - auto res = olm_ed25519_verify(alice->utility(), - 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()); + auto res = olm_ed25519_verify( + alice->utility(), + reinterpret_cast<const uint8_t *>(alice->identity_keys().ed25519.data()), + alice->identity_keys().ed25519.size(), + reinterpret_cast<const uint8_t *>(msg.data()), + msg.size(), + reinterpret_cast<uint8_t *>(&sig[0]), + sig.size()); EXPECT_EQ(std::string(olm_utility_last_error(alice->utility())), "SUCCESS"); EXPECT_EQ(res, 0); @@ -101,15 +94,16 @@ TEST(Utilities, VerifySignedIdentityKeys) {"ed25519:some_device", keys["ed25519"]}}}}) .dump(); - auto sig_buf = to_buffer(alice->sign_message(msg)); + auto sig = alice->sign_message(msg); - auto res = olm_ed25519_verify(alice->utility(), - 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()); + auto res = olm_ed25519_verify( + alice->utility(), + reinterpret_cast<const uint8_t *>(alice->identity_keys().ed25519.data()), + alice->identity_keys().ed25519.size(), + reinterpret_cast<const uint8_t *>(msg.data()), + msg.size(), + reinterpret_cast<uint8_t *>(&sig[0]), + sig.size()); EXPECT_EQ(std::string(olm_utility_last_error(alice->utility())), "SUCCESS"); EXPECT_EQ(res, 0);