Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在不关闭连接的情况下在循环中增强asio加密_C++_Ssl_Boost_Boost Asio - Fatal编程技术网

C++ 在不关闭连接的情况下在循环中增强asio加密

C++ 在不关闭连接的情况下在循环中增强asio加密,c++,ssl,boost,boost-asio,C++,Ssl,Boost,Boost Asio,我试图扩展boost asio库中的示例。 到目前为止,我所取得的成功 1) 运行echo客户端和服务器 2) 扩展echo服务器以读取邮件,直到标准输入文件结束 3) 运行ssl客户端服务器示例 我需要的帮助是: 我希望能够将消息发送/接收放在客户端的循环中。我不想绕圈子 client c(io_service, ctx, iterator); io_service.run(); 因为这将验证每条消息的证书。我试着在handle_握手和其他功能上打个圈,但都不起作用。直到函数结束时才

我试图扩展boost asio库中的示例。 到目前为止,我所取得的成功

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

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

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

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

client c(io_service, ctx, iterator);
io_service.run();
因为这将验证每条消息的证书。我试着在handle_握手和其他功能上打个圈,但都不起作用。直到函数结束时才会发送。我永远不会得到回应


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

我必须从阻塞客户端开始,并使用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;
}
以下引用。

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

//生成私钥

openssl genrsa -des3 -out server.key 1024
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
//生成证书签名请求

openssl req -new -key server.key -out server.csr
//使用私钥签署证书

openssl genrsa -des3 -out server.key 1024
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
//删除密码要求(例如需要)

//生成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");

那一切都会成功的

@TechnikEmpire。我回答了我自己的问题。是的,有一个while循环。没有线程。有很多阻塞和取消阻塞的例子,没有来自ssl的阻塞例子。正如你所知,这节省了我很多时间。请保持好习惯。