From c489975015094b0868e9c2c300583f71c74df096 Mon Sep 17 00:00:00 2001 From: Miguel Portilla Date: Fri, 20 Feb 2015 13:23:24 -0500 Subject: [PATCH] Display human readable SSL error codes --- beast/asio/Asio.unity.cpp | 2 ++ beast/asio/{ssl.h => error.h} | 12 ++++--- beast/asio/impl/error.cpp | 59 +++++++++++++++++++++++++++++++++ beast/asio/tests/error_test.cpp | 44 ++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) rename beast/asio/{ssl.h => error.h} (81%) create mode 100644 beast/asio/impl/error.cpp create mode 100644 beast/asio/tests/error_test.cpp diff --git a/beast/asio/Asio.unity.cpp b/beast/asio/Asio.unity.cpp index fbc81d76fc..fb6ff5ca27 100644 --- a/beast/asio/Asio.unity.cpp +++ b/beast/asio/Asio.unity.cpp @@ -22,6 +22,8 @@ #endif #include +#include #include #include +#include diff --git a/beast/asio/ssl.h b/beast/asio/error.h similarity index 81% rename from beast/asio/ssl.h rename to beast/asio/error.h index b9cff3fad4..342e0fee3c 100644 --- a/beast/asio/ssl.h +++ b/beast/asio/error.h @@ -17,11 +17,11 @@ */ //============================================================================== -#ifndef BEAST_ASIO_SSL_H_INCLUDED -#define BEAST_ASIO_SSL_H_INCLUDED +#ifndef BEAST_ASIO_ERROR_H_INCLUDED +#define BEAST_ASIO_ERROR_H_INCLUDED +#include #include -#include namespace beast { namespace asio { @@ -32,8 +32,12 @@ bool is_short_read (boost::system::error_code const& ec) { return (ec.category() == boost::asio::error::get_ssl_category()) - && (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ); + && (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ); } + +/** Returns a human readable message if the error code is SSL related. */ +std::string +asio_message(boost::system::error_code const& ec); } } diff --git a/beast/asio/impl/error.cpp b/beast/asio/impl/error.cpp new file mode 100644 index 0000000000..c97b15a15b --- /dev/null +++ b/beast/asio/impl/error.cpp @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include + +namespace beast { +namespace asio { + +// This buffer must be at least 120 bytes, most examples use 256. +// https://www.openssl.org/docs/crypto/ERR_error_string.html +static std::uint32_t const errorBufferSize (256); + +std::string +asio_message (boost::system::error_code const& ec) +{ + std::string error; + + if (ec.category () == boost::asio::error::get_ssl_category ()) + { + error = " (" + + boost::lexical_cast (ERR_GET_LIB (ec.value ())) + + "," + + boost::lexical_cast (ERR_GET_FUNC (ec.value ())) + + "," + + boost::lexical_cast (ERR_GET_REASON (ec.value ())) + + ") "; + + // + char buf[errorBufferSize]; + ::ERR_error_string_n (ec.value (), buf, errorBufferSize); + error += buf; + } + else + { + error = ec.message (); + } + + return error; +} + +} +} diff --git a/beast/asio/tests/error_test.cpp b/beast/asio/tests/error_test.cpp new file mode 100644 index 0000000000..bc64109de7 --- /dev/null +++ b/beast/asio/tests/error_test.cpp @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include + +namespace beast { +namespace asio { + +class error_test : public unit_test::suite +{ +public: + void run () + { + { + boost::system::error_code ec = boost::system::error_code (335544539, + boost::asio::error::get_ssl_category ()); + std::string const s = beast::asio::asio_message (ec); + expect(s == " (20,0,219) error:140000DB:SSL routines:SSL routines:short read"); + } + } +}; + +BEAST_DEFINE_TESTSUITE(error,asio,beast); + +} +}