title: What is boost::asio::ssl::context::load_verify_file and how to work with it?
body: <p>Theare is a wary small amount of boost::asio::<a href="http://en.wikipedia.org/wiki/Transport_Layer_Security" rel="nofollow noreferrer">ssl</a> small C++ educational codes base online. Even less on <a href="http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/ssl__context/load_verify_file.html" rel="nofollow noreferrer">boost::asio::ssl::context::load_verify_file</a> So I found one from <a href="https://stackoverflow.com/questions/7046370/https-request-with-boost-asio-and-openssl">here</a> code with minimal modifications - compiles and runs with boost 1.47.0:</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;boost/asio/ssl.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;istream&gt;
  #include &lt;ostream&gt;
  #include &lt;string&gt;
  
  
  class client
  {
  public:
      client(boost::asio::io_service&amp; io_service, boost::asio::ssl::context&amp; context, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
          : socket_(io_service, context)
      {
          socket_.set_verify_mode(boost::asio::ssl::context::verify_none);
          socket_.set_verify_callback(boost::bind(&amp;client::verify_certificate, this, _1, _2));
  
          boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator, boost::bind(&amp;client::handle_connect, this, boost::asio::placeholders::error));
      }
  
      bool verify_certificate(bool preverified, boost::asio::ssl::verify_context&amp; ctx)
      {
          char subject_name[256];
          X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
          X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
          std::cout &lt;&lt; "Verifying:\n" &lt;&lt; subject_name &lt;&lt; std::endl;
  
          return preverified;
      }
  
      void handle_connect(const boost::system::error_code&amp; error)
      {
          if(!error){
              std::cout &lt;&lt; "Connection OK!" &lt;&lt; std::endl;
              socket_.async_handshake(boost::asio::ssl::stream_base::client, boost::bind(&amp;client::handle_handshake, this, boost::asio::placeholders::error));
          }else{
              std::cout &lt;&lt; "Connect failed: " &lt;&lt; error.message() &lt;&lt; std::endl;
          }
      }
  
      void handle_handshake(const boost::system::error_code&amp; error)
      {
          if(!error){
              std::cout &lt;&lt; "Sending request: " &lt;&lt; std::endl;
  
              std::stringstream request_;
  
              request_ &lt;&lt; "GET /api/0/data/ticker.php HTTP/1.1\r\n";
              request_ &lt;&lt; "Host: mtgox.com\r\n";
              request_ &lt;&lt; "Accept-Encoding: *\r\n";
              request_ &lt;&lt; "\r\n";
  
              std::cout &lt;&lt; request_.str() &lt;&lt; std::endl;
  
              boost::asio::async_write(socket_, boost::asio::buffer(request_.str()), boost::bind(&amp;client::handle_write, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
          }else{
              std::cout &lt;&lt; "Handshake failed: " &lt;&lt; error.message() &lt;&lt; std::endl;
          }
      }
  
      void handle_write(const boost::system::error_code&amp; error, size_t bytes_transferred)
      {
          if (!error){
              std::cout &lt;&lt; "Sending request OK!" &lt;&lt; std::endl;
              boost::asio::async_read(socket_, boost::asio::buffer(reply_, bytes_transferred), boost::bind(&amp;client::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
          }else{
              std::cout &lt;&lt; "Write failed: " &lt;&lt; error.message() &lt;&lt; std::endl;
          }
      }
  
      void handle_read(const boost::system::error_code&amp; error, size_t bytes_transferred)
      {
          if (!error){
              std::cout &lt;&lt; "Reply: ";
              std::cout.write(reply_, bytes_transferred);
              std::cout &lt;&lt; "\n";
          }else{
              std::cout &lt;&lt; "Read failed: " &lt;&lt; error.message() &lt;&lt; std::endl;
          }
      }
  
  private:
      boost::asio::ssl::stream&lt;boost::asio::ip::tcp::socket&gt; socket_;
      char reply_[0x1 &lt;&lt; 16];
  };
  
  int main(int argc, char* argv[])
  {
      try{
          boost::asio::io_service io_service;
  
          boost::asio::ip::tcp::resolver resolver(io_service);
          boost::asio::ip::tcp::resolver::query query("mtgox.com", "443");
          boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
  
          boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
          //context.load_verify_file("key.pem"); // uncomment this line
  
          client c(io_service, context, iterator);
  
          io_service.run();
      }catch (std::exception&amp; e){
          std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
      }
  
      std::cin.get();
      return 0;
  }
  </code></pre>
  
  <p>Here is one really curious line: <code>context.load_verify_file("key.pem");</code> So I'we got some questions about it:</p>
  
  <ol>
  <li>What does it mean for me as http client?</li>
  <li>Is it sent to server?</li>
  <li>How to create such file?</li>
  <li>What is it for anyway?</li>
  <li>Code compiles and works with out it. Is it OK? Is our connection to server safe with out that <code>key.pem</code>?</li>
  <li>I want to use google or other big ssl host. What shall I do?</li>
  </ol>