Boost logo

Boost Users :

From: Jens Kallup (paule32.jk_at_[hidden])
Date: 2023-12-23 19:43:50


Hello,
I tried use the boost::asio to create a SSL-Server, but I fail.
The Code I used is attached to this Text.
acceptor_ and context_ give me true result, but the start_accept()
methode give me a Handshake-Error: 10009 (invalid descriptor handle).
How can I avoid this, and how can I fix the Code in the right point ?

Thanks for reading
paule32

// -----------------------------------------------------------------
// File:   BoostServer.cc
// Author: (c) 2023 Jens Kallup - paule32
// All rights reserved
//
// only for education, and non-profit usage !
// -----------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>

# include <iostream>
# include <string>
# include <functional>
# include <thread>
# include <memory>

# include <boost/asio.hpp>
# include <boost/asio/ssl.hpp>

using namespace boost::asio;
using namespace std;

// -----------------------------------------------------------------
// @brief namespace name for Version 1.0.0 of our project ...
// -----------------------------------------------------------------
namespace dBaseRelease
{
std::string ApplicationExeName;

// -----------------------------------------------------------------
// @brief This member handle the exception message text that is use
//         to "try catch" exception's during the runtime of the
//         application.
//
// @param  std::string& message  -  The e.what() message.
// @return nothing
//
// @since  dBaseRelease
// @author paule32
// -----------------------------------------------------------------
void handle_exception(const std::string& message) {
 Â Â Â  std::string error_message;
 Â Â Â  for (int len = 0; len < message.length(); ++len) {
 Â Â Â Â Â Â Â  if (message[len] == '[') break;
 Â Â Â Â Â Â Â  error_message.push_back(message[len]);
 Â Â Â  }
 Â Â Â  std::cerr << "Exception "
 Â Â Â  << error_message
 Â Â Â  << std::endl;
}

class SSLServer: public std::enable_shared_from_this<SSLServer> {
public:
 Â Â Â  SSLServer(io_service& service, uint16_t port):
 Â Â Â Â Â Â Â  acceptor_(service, ip::tcp::endpoint(ip::tcp::v4(), port)),
 Â Â Â Â Â Â Â  context_ (ssl::context::tlsv12) {
 Â Â Â Â Â Â Â  context_.set_options(
 Â Â Â Â Â Â Â Â Â Â Â  ssl::context::default_workarounds |
 Â Â Â Â Â Â Â Â Â Â Â  ssl::context::sslv2 |
 Â Â Â Â Â Â Â Â Â Â Â  ssl::context::sslv3 |
 Â Â Â Â Â Â Â Â Â Â Â  ssl::context::tlsv12);

 Â Â Â Â Â Â Â  if (isAcceptorInitialized()
 Â Â Â Â Â Â Â  &&  isContextInitialized ()) {
 Â Â Â Â Â Â Â Â Â Â Â  std::cout << "acceptor ok" << std::endl;
 Â Â Â Â Â Â Â Â Â Â Â  std::cout << "context  ok" << std::endl;

 Â Â Â Â Â Â Â Â Â Â Â  start_accept();
 Â Â Â Â Â Â Â  }   else {
 Â Â Â Â Â Â Â Â Â Â Â  std::cout << "server init error" << std::endl;
 Â Â Â Â Â Â Â  }
 Â Â Â  }
private:
 Â Â Â  void start_accept() {
 Â Â Â Â Â Â Â  ssl_socket_ = std::make_shared<ssl::stream<ip::tcp::socket>
>(acceptor_.get_executor(), context_);

 Â Â Â Â Â Â Â  // ----------------------------------
 Â Â Â Â Â Â Â  // Starte den asynchronen Handshake
 Â Â Â Â Â Â Â  // ----------------------------------
 Â Â Â Â Â Â Â  ssl_socket_->async_handshake(ssl::stream_base::server,
 Â Â Â Â Â Â Â  [this](const boost::system::error_code& error) {
 Â Â Â Â Â Â Â Â Â Â Â  if (!error) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Wenn der Handshake erfolgreich ist,
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // akzeptiere die Verbindung
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  if (ssl_socket_ && ssl_socket_->lowest_layer().is_open()) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cout << "SSL-Handshake erfolgreich und verbunden."
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;
acceptor_.async_accept(ssl_socket_->lowest_layer(),
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  [this](const boost::system::error_code& accept_error) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  if (!accept_error) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  handle_client();
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  } else {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cerr
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << "Accept error: "
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << accept_error.message()
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  });
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }   else {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cerr << "SSL-Handshake erfolgreich, aber
Verbindung nicht geöffnet."
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
 Â Â Â Â Â Â Â Â Â Â Â  }   else {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cerr
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << "Handshake error: "
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << error.message()
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << " ("
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << error.value()
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << ")"
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;
 Â Â Â Â Â Â Â Â Â Â Â  }
 Â Â Â Â Â Â Â  });
 Â Â Â  }
 Â Â Â  void handle_client() {
 Â Â Â Â Â Â Â  // ------------------------------------
 Â Â Â Â Â Â Â  // Lese/Schreibe Daten mit ssl_socket_
 Â Â Â Â Â Â Â  // ------------------------------------
 Â Â Â Â Â Â Â  boost::asio::async_read_until(*ssl_socket_, buffer_, '\n',
 Â Â Â Â Â Â Â  [this](const boost::system::error_code& read_error, std::size_t
bytes_transferred) {
 Â Â Â Â Â Â Â Â Â Â Â  if (!read_error) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::istream is(&buffer_);
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::string received_data;
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::getline(is, received_data);

 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cout << "Received data from client: "
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << received_data
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;

 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Hier kannst du auf die empfangenen Daten
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // reagieren und die Antwort vorbereiten
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::string response = "Hello from the server!\n";

 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Sende die Antwort an den Client
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  boost::asio::async_write(*ssl_socket_,
boost::asio::buffer(response),
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  [this](const boost::system::error_code& write_error,
std::size_t) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  if (!write_error) {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Schließe die Verbindung
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  ssl_socket_->lowest_layer().close();

 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // Starte die nächste Akzeptanz
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  // -----------------------------
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  start_accept();
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  } else {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cerr
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << "Write error: "
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << write_error.message()
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  });
 Â Â Â Â Â Â Â Â Â Â Â  }   else {
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  std::cerr
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << "Read error: "
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << read_error.message()
 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  << std::endl;
 Â Â Â Â Â Â Â Â Â Â Â  }
 Â Â Â Â Â Â Â  });
 Â Â Â  }

 Â Â Â  // ------------------------------------------------
 Â Â Â  // Ãœberprüfung, ob der Acceptor initialisiert wurde
 Â Â Â  // ------------------------------------------------
 Â Â Â  bool isAcceptorInitialized() const {
 Â Â Â Â Â Â Â  return acceptor_.is_open();
 Â Â Â  }

 Â Â Â  // ------------------------------------------------
 Â Â Â  // Ãœberprüfung, ob der Context initialisiert wurde
 Â Â Â  // ------------------------------------------------
 Â Â Â  bool isContextInitialized() {
 Â Â Â Â Â Â Â  try {
 Â Â Â Â Â Â Â Â Â Â Â  // ----------------------------------
 Â Â Â Â Â Â Â Â Â Â Â  // load certificate, and private key
 Â Â Â Â Â Â Â Â Â Â Â  // ----------------------------------
 Â Â Â Â Â Â Â Â Â Â Â  context_.use_certificate_chain_file("server.crt.pem");
 Â Â Â Â Â Â Â Â Â Â Â  context_.use_private_key_file("server.key.pem",
ssl::context::pem);
 Â Â Â Â Â Â Â Â Â Â Â  return true;
 Â Â Â Â Â Â Â  }
 Â Â Â Â Â Â Â  catch (const boost::system::system_error& e) {
 Â Â Â Â Â Â Â Â Â Â Â  handle_exception(e.what());
 Â Â Â Â Â Â Â Â Â Â Â  return false;
 Â Â Â Â Â Â Â  }
 Â Â Â  }
private:
 Â Â Â  ip::tcp::acceptor acceptor_;
 Â Â Â  ssl::context context_;
 Â Â Â  std::shared_ptr<ssl::stream<ip::tcp::socket>> ssl_socket_;
 Â Â Â  boost::asio::streambuf buffer_;
};
}   // namespace: dBaseRelease

int main(int argc, char **argv) {
 Â Â Â  using namespace dBaseRelease;

 Â Â Â  try {
 Â Â Â Â Â Â Â  ApplicationExeName = argv[0];
 Â Â Â Â Â Â Â  io_service service;

 Â Â Â Â Â Â Â  std::shared_ptr<SSLServer> server =
std::make_shared<SSLServer>(service, 12345);
 Â Â Â Â Â Â Â  service.run();
 Â Â Â  }
 Â Â Â  catch (const std::exception& e) {
 Â Â Â Â Â Â Â  std::cerr << "Exception: "
 Â Â Â Â Â Â Â  << e.what()
 Â Â Â Â Â Â Â  << std::endl;

 Â Â Â Â Â Â Â  return EXIT_FAILURE;
 Â Â Â  }   return EXIT_SUCCESS;
}

-- 
Diese E-Mail wurde von Avast-Antivirussoftware auf Viren geprüft.
www.avast.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net