title: I have no idea why did Boost Asio SSL handshake always fail?
body: <p>I tried use Boost Asio and Openssl to post some json to a SSL server,my case was it sucks when the code execute handshake with the server,the error was
  "handshake: certificate verify failed"
   and I have no idea even I have Googled and viewed a branch of relative web pages ,I didn't find the answer yet,the SSL client example seems didn't work for me,what wrong I have done?</p>
  
  <p>my code is</p>
  
  <pre><code>#include "root_certificates.hpp"
  #include &lt;boost/beast/core.hpp&gt;
  #include &lt;boost/beast/http.hpp&gt;
  #include &lt;boost/beast/version.hpp&gt;
  #include &lt;boost/asio/connect.hpp&gt;
  #include &lt;boost/asio/ip/tcp.hpp&gt;
  #include &lt;boost/asio/ssl/error.hpp&gt;
  #include &lt;boost/asio/ssl/stream.hpp&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;time.h&gt;
  #include&lt;fstream&gt;
  #include &lt;ctime&gt;
  #include &lt;istream&gt;
  
  int postsslserver()
  {
      try
      {
          auto const host ="mydomain.com";
          auto const port = "https";
          auto const target ="/apps/postpage.html" ;
          retcode = 0;
  
          char mybuffer[80];
  
          setlocale(LC_ALL, "");
  
          pwmd5hashed = "mysecret";
  
          std::string jsondata ="\"Double\":12.0000001,";
  
          int version =11;
  
          // The io_context is required for all I/O
          boost::asio::io_context ioc;
  
          // The SSL context is required, and holds certificates
          ssl::context ctx{ ssl::context::sslv23_client };
  
          //20181021
          ctx.set_default_verify_paths();
  
          // This holds the root certificate used for verification
          //load_root_certificates(ctx);
  
          // Verify the remote server's certificate
          //ctx.set_verify_mode(ssl::verify_peer);
          ctx.set_verify_mode(ssl::verify_peer);
  
          // These objects perform our I/O
          tcp::resolver resolver{ ioc };
          ssl::stream&lt;tcp::socket&gt; stream{ ioc, ctx };
  
          // Set SNI Hostname (many hosts need this to handshake successfully)
          if (!SSL_set_tlsext_host_name(stream.native_handle(), host))
          {
              boost::system::error_code ec{ static_cast&lt;int&gt;(::ERR_get_error()), boost::asio::error::get_ssl_category() };
              throw boost::system::system_error{ ec };
          }
  
          // Look up the domain name
          auto const results = resolver.resolve(host, port);
  
          // Make the connection on the IP address we get from a lookup
          boost::asio::connect(stream.next_layer(), results.begin(), results.end());
  
          // Perform the SSL handshake
          stream.handshake(ssl::stream_base::client);// error always occured this line of code,the error hints was "handshake: certificate verify failed"
  
          // Set up an HTTP POST request message
          http::request&lt;http::string_body&gt; req{ http::verb::post, target, version };
          req.set(http::field::host, host);
          req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
          req.set(http::field::content_type, "application/json");
          req.set(http::field::body, jsondata);
          //req.keep_alive(req.keep_alive());
  
          // Send the HTTP request to the remote host
          http::write(stream, req);
  
          // This buffer is used for reading and must be persisted
          boost::beast::flat_buffer buffer;
  
          // Declare a container to hold the response
          http::response&lt;http::dynamic_body&gt; res;
  
          // Receive the HTTP response
          http::read(stream, buffer, res);
  
          // Write the message to standard out
          std::cout &lt;&lt; res &lt;&lt; std::endl;
  
          // Gracefully close the stream
          boost::system::error_code ec;
          stream.shutdown(ec);
          if (ec == boost::asio::error::eof)
          {
              // Rationale:
              // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
              ec.assign(0, ec.category());
          }
          if (ec)
              throw boost::system::system_error{ ec };
  
          // If we get here then the connection is closed gracefully
      }
      catch (std::exception const&amp; e)
      {
          //std::cerr &lt;&lt; "Error: " &lt;&lt; e.what() &lt;&lt; std::endl;
          write_text_to_log_file(e.what());
          return EXIT_FAILURE;
      }
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>thanks in advance</p>