在没有关闭连接的情况下在循环中增强asio加密

时间:2015-11-10 21:37:24

标签: c++ ssl boost boost-asio

我正在尝试扩展boost asio库中的示例。 到目前为止我取得了成功。

1)运行echo客户端和服务器。

http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_client.cpp

http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp

2)扩展echo服务器以读取消息,直到标准输入文件结束。

3)运行ssl客户端服务器示例。

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/ssl/client.cpp

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/ssl/server.cpp

我需要什么帮助: 我希望能够将消息发送/接收放在客户端的循环中。我不想围绕

client c(io_service, ctx, iterator);
io_service.run();

因为这会验证每封邮件的证书。我尝试在handle_handshake和其他函数周围放置一个循环,但它们不起作用。直到函数结束才会发送。我永远不会收到回复。

我愿意避免使用异步IO,但我仍然想要加密。

1 个答案:

答案 0 :(得分:1)

我必须从阻塞客户端开始,并使用https从ssl客户端和ssl示例添加我需要的内容:

这适用于未经修改的ssl服务器。

//
// ssl_blocking_tcp_echo_client.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

  bool verify_certificate(bool preverified,
      boost::asio::ssl::verify_context& ctx)
  {
    // The verify callback can be used to check whether the certificate that is
    // being presented is valid for the peer. For example, RFC 2818 describes
    // the steps involved in doing this for HTTPS. Consult the OpenSSL
    // documentation for more details. Note that the callback is called once
    // for each certificate in the certificate chain, starting from the root
    // certificate authority.

    // In this example we will simply print the certificate's subject name.
    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 << "Verifying " << subject_name << "\n";

    return preverified;
  }

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
      return 1;
    }


    boost::asio::io_service io_service;
    boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
    ctx.load_verify_file("server.crt");
    ctx.set_verify_mode(boost::asio::ssl::verify_peer);
    boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(io_service, ctx);

    tcp::resolver resolver(io_service);
    boost::asio::connect(socket.lowest_layer(), resolver.resolve({argv[1], argv[2]}) );
    socket.set_verify_callback(
        boost::bind(&verify_certificate, _1, _2));
    socket.handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client);

    while(std::cin)
    {
        std::cout << "Enter message: ";
        char request[max_length];
        std::cin.getline(request, max_length);
        size_t request_length = std::strlen(request);
        boost::asio::write(socket, boost::asio::buffer(request, request_length));

        char reply[max_length];
        size_t reply_length = boost::asio::read(socket,
            boost::asio::buffer(reply, request_length));
        std::cout << "Reply is: ";
        std::cout.write(reply, reply_length);
        std::cout << "\n";
    }

  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

在开始之前,我根据Shootfast对this question的回答修改了服务器和客户端。我不得不稍微修改它,因为有些东西太短了。我将此命令更改为2048而不是原来的512。

openssl dhparam -out dh512.pem 2048

引用如下。

好的,对于将来发现此问题的任何人,您需要创建证书并对其进行适当签名。 以下是linux的命令:

//生成私钥

openssl genrsa -des3 -out server.key 1024

//生成证书签名请求

openssl req -new -key server.key -out server.csr

//使用私钥签名证书

openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

//删除密码要求(例如需要)

cp server.key server.key.secure
openssl rsa -in server.key.secure -out server.key

//生成dhparam文件

openssl dhparam -out dh512.pem 2048

完成后,您需要更改server.cpp和client.cpp中的文件名。

server.cpp

context_.use_certificate_chain_file("server.crt"); 
context_.use_private_key_file("server.key", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");

client.cpp

ctx.load_verify_file("server.crt");

然后它应该全部工作!