From 3d2627e41ed48463bb9788164accc0fcb9ab193c Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris <sideris.konstantin@gmail.com> Date: Tue, 18 Sep 2018 18:05:18 +0300 Subject: [PATCH] Replace generic runtime_error with sodium_exception --- .ci/install.sh | 1 + .travis.yml | 2 +- include/mtxclient/crypto/client.hpp | 13 +++++++++++++ lib/crypto/client.cpp | 10 +++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.ci/install.sh b/.ci/install.sh index bc3ca71e7..a968e69db 100755 --- a/.ci/install.sh +++ b/.ci/install.sh @@ -3,6 +3,7 @@ set -ex if [ $TRAVIS_OS_NAME == osx ]; then + brew update || true brew upgrade boost cmake brew install libsodium fi diff --git a/.travis.yml b/.travis.yml index ffcb08801..3c2fa9e8b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -61,6 +61,6 @@ after_success: if [ $COVERAGE == ON ]; then docker run -v `pwd`:/build \ --net=host mujx/mtxclient-dev:0.1.0 \ - /bin/bash -c "cmake --build --target test_coverage && ./.ci/coverage.sh" && \ + /bin/bash -c "cmake --build build --target test_coverage && ./.ci/coverage.sh" && \ bash <(curl -s https://codecov.io/bash) -f "!*tests*" || echo "Codecov failed" fi diff --git a/include/mtxclient/crypto/client.hpp b/include/mtxclient/crypto/client.hpp index 7c46b5d2d..740166bec 100644 --- a/include/mtxclient/crypto/client.hpp +++ b/include/mtxclient/crypto/client.hpp @@ -54,6 +54,19 @@ private: std::string msg_; }; +class sodium_exception : public std::exception +{ +public: + sodium_exception(std::string func, const char *msg) + : msg_(func + ": " + std::string(msg)) + {} + + virtual const char *what() const throw() { return msg_.c_str(); } + +private: + std::string msg_; +}; + //! Create a uint8_t buffer which is initialized with random bytes. inline BinaryBuf create_buffer(std::size_t nbytes) diff --git a/lib/crypto/client.cpp b/lib/crypto/client.cpp index 4eaaa6d6d..5af207530 100644 --- a/lib/crypto/client.cpp +++ b/lib/crypto/client.cpp @@ -567,7 +567,7 @@ mtx::crypto::decrypt_exported_sessions(const std::string &data, std::string pass { if (data.size() < crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES + crypto_pwhash_SALTBYTES) - throw std::runtime_error{"decrypt_exported_sessions ciphertext too small"}; + throw sodium_exception{"decrypt_exported_sessions", "ciphertext too small"}; const auto nonce_start = data.begin(); const auto nonce_end = nonce_start + crypto_secretbox_NONCEBYTES; @@ -586,7 +586,7 @@ mtx::crypto::decrypt_exported_sessions(const std::string &data, std::string pass ciphertext.size(), nonce.data(), reinterpret_cast<const unsigned char *>(key.data())) != 0) - throw std::runtime_error{"crypto_secretbox_open_easy: failed to decrypt"}; + throw sodium_exception{"crypto_secretbox_open_easy", "failed to decrypt"}; return json::parse(std::string(decrypted.begin(), decrypted.end())); } @@ -610,7 +610,7 @@ mtx::crypto::base642bin(const std::string &b64) &max_end, sodium_base64_VARIANT_ORIGINAL); if (rc != 0) - throw std::runtime_error{"base642bin failed"}; + throw sodium_exception{"sodium_base642bin", "encoding failed"}; if (bin_len != bin_maxlen) ciphertext.resize(bin_len); @@ -639,7 +639,7 @@ BinaryBuf mtx::crypto::derive_key(const std::string &pass, const BinaryBuf &salt) { if (salt.size() != crypto_pwhash_SALTBYTES) - throw std::runtime_error{"derive_key: invalid buffer size for salt"}; + throw sodium_exception{"derive_key", "invalid buffer size for salt"}; auto key = create_buffer(crypto_secretbox_KEYBYTES); @@ -652,7 +652,7 @@ mtx::crypto::derive_key(const std::string &pass, const BinaryBuf &salt) crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE, crypto_pwhash_ALG_DEFAULT) != 0) { - throw std::runtime_error{"crypto_pwhash: out of memory"}; + throw sodium_exception{"crypto_pwhash", "out of memory"}; } return key; -- GitLab