/Src/Dependencies/Boost/libs/asio/example/ssl/client.cpp

http://hadesmem.googlecode.com/ · C++ · 156 lines · 120 code · 20 blank · 16 comment · 6 complexity · b6827a2b14eec9467df5ffb3f3939646 MD5 · raw file

  1. //
  2. // client.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <boost/bind.hpp>
  13. #include <boost/asio.hpp>
  14. #include <boost/asio/ssl.hpp>
  15. enum { max_length = 1024 };
  16. class client
  17. {
  18. public:
  19. client(boost::asio::io_service& io_service,
  20. boost::asio::ssl::context& context,
  21. boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
  22. : socket_(io_service, context)
  23. {
  24. socket_.set_verify_mode(boost::asio::ssl::verify_peer);
  25. socket_.set_verify_callback(
  26. boost::bind(&client::verify_certificate, this, _1, _2));
  27. boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator,
  28. boost::bind(&client::handle_connect, this,
  29. boost::asio::placeholders::error));
  30. }
  31. bool verify_certificate(bool preverified,
  32. boost::asio::ssl::verify_context& ctx)
  33. {
  34. // The verify callback can be used to check whether the certificate that is
  35. // being presented is valid for the peer. For example, RFC 2818 describes
  36. // the steps involved in doing this for HTTPS. Consult the OpenSSL
  37. // documentation for more details. Note that the callback is called once
  38. // for each certificate in the certificate chain, starting from the root
  39. // certificate authority.
  40. // In this example we will simply print the certificate's subject name.
  41. char subject_name[256];
  42. X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
  43. X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
  44. std::cout << "Verifying " << subject_name << "\n";
  45. return preverified;
  46. }
  47. void handle_connect(const boost::system::error_code& error)
  48. {
  49. if (!error)
  50. {
  51. socket_.async_handshake(boost::asio::ssl::stream_base::client,
  52. boost::bind(&client::handle_handshake, this,
  53. boost::asio::placeholders::error));
  54. }
  55. else
  56. {
  57. std::cout << "Connect failed: " << error.message() << "\n";
  58. }
  59. }
  60. void handle_handshake(const boost::system::error_code& error)
  61. {
  62. if (!error)
  63. {
  64. std::cout << "Enter message: ";
  65. std::cin.getline(request_, max_length);
  66. size_t request_length = strlen(request_);
  67. boost::asio::async_write(socket_,
  68. boost::asio::buffer(request_, request_length),
  69. boost::bind(&client::handle_write, this,
  70. boost::asio::placeholders::error,
  71. boost::asio::placeholders::bytes_transferred));
  72. }
  73. else
  74. {
  75. std::cout << "Handshake failed: " << error.message() << "\n";
  76. }
  77. }
  78. void handle_write(const boost::system::error_code& error,
  79. size_t bytes_transferred)
  80. {
  81. if (!error)
  82. {
  83. boost::asio::async_read(socket_,
  84. boost::asio::buffer(reply_, bytes_transferred),
  85. boost::bind(&client::handle_read, this,
  86. boost::asio::placeholders::error,
  87. boost::asio::placeholders::bytes_transferred));
  88. }
  89. else
  90. {
  91. std::cout << "Write failed: " << error.message() << "\n";
  92. }
  93. }
  94. void handle_read(const boost::system::error_code& error,
  95. size_t bytes_transferred)
  96. {
  97. if (!error)
  98. {
  99. std::cout << "Reply: ";
  100. std::cout.write(reply_, bytes_transferred);
  101. std::cout << "\n";
  102. }
  103. else
  104. {
  105. std::cout << "Read failed: " << error.message() << "\n";
  106. }
  107. }
  108. private:
  109. boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
  110. char request_[max_length];
  111. char reply_[max_length];
  112. };
  113. int main(int argc, char* argv[])
  114. {
  115. try
  116. {
  117. if (argc != 3)
  118. {
  119. std::cerr << "Usage: client <host> <port>\n";
  120. return 1;
  121. }
  122. boost::asio::io_service io_service;
  123. boost::asio::ip::tcp::resolver resolver(io_service);
  124. boost::asio::ip::tcp::resolver::query query(argv[1], argv[2]);
  125. boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
  126. boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
  127. ctx.load_verify_file("ca.pem");
  128. client c(io_service, ctx, iterator);
  129. io_service.run();
  130. }
  131. catch (std::exception& e)
  132. {
  133. std::cerr << "Exception: " << e.what() << "\n";
  134. }
  135. return 0;
  136. }