aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ffi
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-08-12 19:25:10 -0400
committerJack Lloyd <[email protected]>2018-08-12 19:25:10 -0400
commit2ddb3656d721f22c912e428b6c6a6af9398fbed9 (patch)
tree67adfc988ffeaf68725d01b88d49711a7203212b /src/lib/ffi
parente0a43a200901e9c212573b09730c142b42c5719e (diff)
Better error reporting for FFI
Previously safe_get(x) where x was null would return an error about an exception being thrown, instead of a null pointer error.
Diffstat (limited to 'src/lib/ffi')
-rw-r--r--src/lib/ffi/ffi_kdf.cpp2
-rw-r--r--src/lib/ffi/ffi_mp.cpp2
-rw-r--r--src/lib/ffi/ffi_util.h20
3 files changed, 18 insertions, 6 deletions
diff --git a/src/lib/ffi/ffi_kdf.cpp b/src/lib/ffi/ffi_kdf.cpp
index 3b907e967..479bf2140 100644
--- a/src/lib/ffi/ffi_kdf.cpp
+++ b/src/lib/ffi/ffi_kdf.cpp
@@ -91,7 +91,7 @@ int botan_bcrypt_generate(uint8_t* out, size_t* out_len,
return BOTAN_FFI_ERROR_BAD_FLAG;
if(wf < 4 || wf > 18)
- throw FFI_Error("Bad bcrypt work factor " + std::to_string(wf));
+ throw FFI_Error("Bad bcrypt work factor " + std::to_string(wf), BOTAN_FFI_ERROR_BAD_PARAMETER);
Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
const std::string bcrypt = Botan::generate_bcrypt(pass, rng, static_cast<uint16_t>(wf));
diff --git a/src/lib/ffi/ffi_mp.cpp b/src/lib/ffi/ffi_mp.cpp
index cbd784d7a..17d5d19c7 100644
--- a/src/lib/ffi/ffi_mp.cpp
+++ b/src/lib/ffi/ffi_mp.cpp
@@ -114,7 +114,7 @@ int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t*
else if(digit_base == 16)
base = Botan::BigInt::Hexadecimal;
else
- throw FFI_Error("botan_mp_to_str invalid digit base");
+ throw FFI_Error("botan_mp_to_str invalid digit base", BOTAN_FFI_ERROR_BAD_PARAMETER);
std::vector<uint8_t> hex = Botan::BigInt::encode(bn, base);
hex.push_back(0); // null terminator
diff --git a/src/lib/ffi/ffi_util.h b/src/lib/ffi/ffi_util.h
index 2aa0edd83..623b4bf20 100644
--- a/src/lib/ffi/ffi_util.h
+++ b/src/lib/ffi/ffi_util.h
@@ -18,7 +18,15 @@ namespace Botan_FFI {
class BOTAN_UNSTABLE_API FFI_Error final : public Botan::Exception
{
public:
- explicit FFI_Error(const std::string& what) : Exception("FFI error", what) {}
+ FFI_Error(const std::string& what, int err_code) :
+ Exception("FFI error", what),
+ m_err_code(err_code)
+ {}
+
+ int error_code() const { return m_err_code; }
+
+ private:
+ int m_err_code;
};
template<typename T, uint32_t MAGIC>
@@ -50,15 +58,15 @@ template<typename T, uint32_t M>
T& safe_get(botan_struct<T,M>* p)
{
if(!p)
- throw FFI_Error("Null pointer argument");
+ throw FFI_Error("Null pointer argument", BOTAN_FFI_ERROR_NULL_POINTER);
if(p->magic_ok() == false)
- throw FFI_Error("Bad magic in ffi object");
+ throw FFI_Error("Bad magic in ffi object", BOTAN_FFI_ERROR_INVALID_OBJECT);
T* t = p->unsafe_get();
if(t)
return *t;
else
- throw FFI_Error("Invalid object pointer");
+ throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT);
}
template<typename Thunk>
@@ -72,6 +80,10 @@ int ffi_guard_thunk(const char* func_name, Thunk thunk)
{
return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
}
+ catch(Botan_FFI::FFI_Error& e)
+ {
+ return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
+ }
catch(Botan::Key_Not_Set& e)
{
return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_KEY_NOT_SET);