Ensure OpenSSL error queue is cleared before each operation.
diff --git a/asio/include/asio/ssl/detail/impl/engine.ipp b/asio/include/asio/ssl/detail/impl/engine.ipp
index 3f05a2b..bf388dd 100644
--- a/asio/include/asio/ssl/detail/impl/engine.ipp
+++ b/asio/include/asio/ssl/detail/impl/engine.ipp
@@ -231,6 +231,7 @@
     std::size_t* bytes_transferred)
 {
   std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
+  ::ERR_clear_error();
   int result = (this->*op)(data, length);
   int ssl_error = ::SSL_get_error(ssl_, result);
   int sys_error = static_cast<int>(::ERR_get_error());
diff --git a/asio/include/asio/ssl/impl/context.ipp b/asio/include/asio/ssl/impl/context.ipp
index 98c9775..ee12e55 100644
--- a/asio/include/asio/ssl/impl/context.ipp
+++ b/asio/include/asio/ssl/impl/context.ipp
@@ -62,6 +62,8 @@
 context::context(context::method m)
   : handle_(0)
 {
+  ::ERR_clear_error();
+
   switch (m)
   {
 #if defined(OPENSSL_NO_SSL2)
@@ -311,6 +313,8 @@
 asio::error_code context::load_verify_file(
     const std::string& filename, asio::error_code& ec)
 {
+  ::ERR_clear_error();
+
   if (::SSL_CTX_load_verify_locations(handle_, filename.c_str(), 0) != 1)
   {
     ec = asio::error_code(
@@ -368,6 +372,8 @@
 asio::error_code context::set_default_verify_paths(
     asio::error_code& ec)
 {
+  ::ERR_clear_error();
+
   if (::SSL_CTX_set_default_verify_paths(handle_) != 1)
   {
     ec = asio::error_code(
@@ -390,6 +396,8 @@
 asio::error_code context::add_verify_path(
     const std::string& path, asio::error_code& ec)
 {
+  ::ERR_clear_error();
+
   if (::SSL_CTX_load_verify_locations(handle_, 0, path.c_str()) != 1)
   {
     ec = asio::error_code(
@@ -482,6 +490,8 @@
     }
   }
 
+  ::ERR_clear_error();
+
   if (::SSL_CTX_use_certificate_file(handle_, filename.c_str(), file_type) != 1)
   {
     ec = asio::error_code(
@@ -574,6 +584,8 @@
 asio::error_code context::use_certificate_chain_file(
     const std::string& filename, asio::error_code& ec)
 {
+  ::ERR_clear_error();
+
   if (::SSL_CTX_use_certificate_chain_file(handle_, filename.c_str()) != 1)
   {
     ec = asio::error_code(
@@ -716,6 +728,8 @@
     }
   }
 
+  ::ERR_clear_error();
+
   if (::SSL_CTX_use_PrivateKey_file(handle_, filename.c_str(), file_type) != 1)
   {
     ec = asio::error_code(
@@ -756,6 +770,8 @@
     }
   }
 
+  ::ERR_clear_error();
+
   if (::SSL_CTX_use_RSAPrivateKey_file(
         handle_, filename.c_str(), file_type) != 1)
   {
@@ -779,6 +795,8 @@
 asio::error_code context::use_tmp_dh(
     const const_buffer& dh, asio::error_code& ec)
 {
+  ::ERR_clear_error();
+
   bio_cleanup bio = { make_buffer_bio(dh) };
   if (bio.p)
   {
@@ -801,6 +819,8 @@
 asio::error_code context::use_tmp_dh_file(
     const std::string& filename, asio::error_code& ec)
 {
+  ::ERR_clear_error();
+
   bio_cleanup bio = { ::BIO_new_file(filename.c_str(), "r") };
   if (bio.p)
   {