From b3c80a76ddbd871c9ef64762d4e532c3a55b903f Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Tue, 30 Sep 2014 14:34:34 -0700 Subject: [PATCH 1/4] Make errors human readable Change error messages from numeric codes to human readable strings. This makes debugging failures much easier. --- src/ffi.rs | 6 ++++++ src/ssl/error.rs | 19 ++++++++++--------- src/ssl/mod.rs | 1 + 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 2118391b..42d33c87 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -272,6 +272,10 @@ extern "C" { pub fn ERR_get_error() -> c_ulong; + pub fn ERR_lib_error_string(err: c_ulong) -> *const c_char; + pub fn ERR_func_error_string(err: c_ulong) -> *const c_char; + pub fn ERR_reason_error_string(err: c_ulong) -> *const c_char; + pub fn EVP_md5() -> *const EVP_MD; pub fn EVP_ripemd160() -> *const EVP_MD; pub fn EVP_sha1() -> *const EVP_MD; @@ -345,6 +349,8 @@ extern "C" { pub fn SSL_library_init() -> c_int; + pub fn SSL_load_error_strings(); + #[cfg(feature = "sslv2")] pub fn SSLv2_method() -> *const SSL_METHOD; pub fn SSLv3_method() -> *const SSL_METHOD; diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 452f8aad..b7688595 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -1,5 +1,6 @@ use libc::c_ulong; use std::io::IoError; +use std::c_str::CString; use ffi; @@ -20,24 +21,24 @@ pub enum OpensslError { /// An unknown error UnknownError { /// The library reporting the error - library: u8, + library: CString, /// The function reporting the error - function: u16, + function: CString, /// The reason for the error - reason: u16 + reason: CString } } -fn get_lib(err: c_ulong) -> u8 { - ((err >> 24) & 0xff) as u8 +fn get_lib(err: c_ulong) -> CString { + unsafe { CString::new(ffi::ERR_lib_error_string(err), false) } } -fn get_func(err: c_ulong) -> u16 { - ((err >> 12) & 0xfff) as u16 +fn get_func(err: c_ulong) -> CString { + unsafe { CString::new(ffi::ERR_func_error_string(err), false) } } -fn get_reason(err: c_ulong) -> u16 { - (err & 0xfff) as u16 +fn get_reason(err: c_ulong) -> CString { + unsafe { CString::new(ffi::ERR_reason_error_string(err), false) } } impl SslError { diff --git a/src/ssl/mod.rs b/src/ssl/mod.rs index 379a98cf..b5027e8f 100644 --- a/src/ssl/mod.rs +++ b/src/ssl/mod.rs @@ -24,6 +24,7 @@ fn init() { unsafe { INIT.doit(|| { ffi::SSL_library_init(); + ffi::SSL_load_error_strings(); // maybe add err_load_crypto_strings? let verify_idx = ffi::SSL_CTX_get_ex_new_index(0, ptr::null(), None, None, None); assert!(verify_idx >= 0); From 02c124a1fecee45f06e08314554502fce7a8190a Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Thu, 2 Oct 2014 02:05:49 -0700 Subject: [PATCH 2/4] Address CR comments and add a test --- src/ssl/error.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/ssl/error.rs b/src/ssl/error.rs index b7688595..a1e8eadc 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -21,24 +21,36 @@ pub enum OpensslError { /// An unknown error UnknownError { /// The library reporting the error - library: CString, + library: String, /// The function reporting the error - function: CString, + function: String, /// The reason for the error - reason: CString + reason: String } } -fn get_lib(err: c_ulong) -> CString { - unsafe { CString::new(ffi::ERR_lib_error_string(err), false) } +fn get_lib(err: c_ulong) -> String { + unsafe { CString::new(ffi::ERR_lib_error_string(err), false) }.to_string() } -fn get_func(err: c_ulong) -> CString { - unsafe { CString::new(ffi::ERR_func_error_string(err), false) } +fn get_func(err: c_ulong) -> String { + unsafe { CString::new(ffi::ERR_func_error_string(err), false).to_string() } } -fn get_reason(err: c_ulong) -> CString { - unsafe { CString::new(ffi::ERR_reason_error_string(err), false) } +fn get_reason(err: c_ulong) -> String { + unsafe { CString::new(ffi::ERR_reason_error_string(err), false).to_string() } +} + +#[test] +fn test_uknown_error_should_have_correct_messages() { + let err = 336032784; + let library = get_lib(err); + let function = get_func(err); + let reason = get_reason(err); + + assert_eq!(library.as_slice(),"20"); + assert_eq!(function.as_slice(), "119"); + assert_eq!(reason.as_slice(), "1040"); } impl SslError { From 5713c42df72da232b21dd5c55b91a527227174bb Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Thu, 2 Oct 2014 02:11:35 -0700 Subject: [PATCH 3/4] Fix error messages --- src/ssl/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ssl/error.rs b/src/ssl/error.rs index a1e8eadc..0c1af90a 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -48,9 +48,9 @@ fn test_uknown_error_should_have_correct_messages() { let function = get_func(err); let reason = get_reason(err); - assert_eq!(library.as_slice(),"20"); - assert_eq!(function.as_slice(), "119"); - assert_eq!(reason.as_slice(), "1040"); + assert_eq!(library.as_slice(),"SSL routines"); + assert_eq!(function.as_slice(), "SSL23_GET_SERVER_HELLO"); + assert_eq!(reason.as_slice(), "sslv3 alert handshake failure"); } impl SslError { From a8cadc46ac9f125e74b729fdbd1cb75897cab575 Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Sun, 5 Oct 2014 02:50:33 -0700 Subject: [PATCH 4/4] Load crypto error strings --- src/ffi.rs | 3 ++- src/ssl/mod.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 42d33c87..9ceb7f31 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -276,6 +276,8 @@ extern "C" { pub fn ERR_func_error_string(err: c_ulong) -> *const c_char; pub fn ERR_reason_error_string(err: c_ulong) -> *const c_char; + pub fn ERR_load_crypto_strings(); + pub fn EVP_md5() -> *const EVP_MD; pub fn EVP_ripemd160() -> *const EVP_MD; pub fn EVP_sha1() -> *const EVP_MD; @@ -427,4 +429,3 @@ extern "C" { pub fn i2d_RSAPrivateKey(k: *mut RSA, buf: *const *mut u8) -> c_int; pub fn d2i_RSAPrivateKey(k: *const *mut RSA, buf: *const *const u8, len: c_uint) -> *mut RSA; } - diff --git a/src/ssl/mod.rs b/src/ssl/mod.rs index b5027e8f..c7c024ed 100644 --- a/src/ssl/mod.rs +++ b/src/ssl/mod.rs @@ -24,7 +24,8 @@ fn init() { unsafe { INIT.doit(|| { ffi::SSL_library_init(); - ffi::SSL_load_error_strings(); // maybe add err_load_crypto_strings? + ffi::SSL_load_error_strings(); + ffi::ERR_load_crypto_strings(); let verify_idx = ffi::SSL_CTX_get_ex_new_index(0, ptr::null(), None, None, None); assert!(verify_idx >= 0);