openssl1.1.1d在boost.asio中的使用

(双向认证,需要和server一样配置,如果需要密码需要set密码的回调)

参考:https://stackoverflow.com/questions/30925952/boost-asio-exception-use-private-key-file-key-values-mismatch

https://stackoverflow.com/questions/27218516/boost-asio-ssl-password-callback-not-called-if-private-key-passed-with-context

1、证书生成

1.1证书生成

a 分别生成ca,server,client的秘钥

#生成证书

1.首先要生成服务器端的私钥(key文件):openssl genrsa -des3 -out server.key 1024

genras表示生成RSA私有密钥文件,-des3表示用DES3加密该文件,1024是我们的key的长度。

2.openssl req -new -key server.key -out server.csr -config openssl.cnf 生成Certificate Signing Request(CSR)

3.对客户端也作同样的命令生成key及csr文件:openssl genrsa -des3 -out client.key 1024

openssl req -new -key client.key -out client.csr -config openssl.cnf

b 生成ca的根证书 

openssl genrsa -des3 -out ca.key 2048

openssl req -new -x509 -key ca.key -out ca.crt -days 3650 -config openssl.cnf

-x509:本option将产生自签名的证书。

c 生成server,client的申请书

d 用ca签名,生成server和client的证书

openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config openssl.cnf

openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key -config openssl.cnf

e 生成dh1024(需要大于dh512,最好用2048)

openssl dhparam -out dh1024.pem 1024

2、boost设置

2.1 服务端配置

//

// server.cpp

// ~~~~~~~~~~

//

// Copyright (c) 2003-2019 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 <stdio.h>

#include <string>

#include <vector>

#include <cstdlib>

#include <functional>

#include <iostream>

#include <boost/asio.hpp>

#include <boost/asio/ssl.hpp>

// an experiment to see if we can use ASIO with LibreSSL.

using  namespace boost;

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

using  boost::asio::ssl::context;

//--------------------------------------------------------------

class server_session {

public:

  server_session(asio::io_context *context, asio::ssl::context *ssl_context)

      : _ssl_stream(*context, *ssl_context) {}

  void do_read() {

    try {

      std::cout << "got a new connection" << std::endl;

      // read message

      char buf[5]; // message will be "hello\n"

      boost::system::error_code ec;

      std::size_t n = _ssl_stream.read_some(asio::buffer(buf, 5));

      std::cout << "read " << n << " bytes: " << buf << std::endl;

      // respond?  do something useful, close connection

    } catch (const std::exception &e) {

      std::cout << "Exception: " << e.what() << std::endl;

    }

  }

  void do_handshake() {

    _ssl_stream.async_handshake(

        asio::ssl::stream_base::server, [this](std::error_code ec) {

          if (ec) {

            std::cout << "handshake error: " << ec.message() << std::endl;

            return;

          }

          do_read();

        });

  }

  asio::ssl::stream<tcp::socket> *stream() { return &_ssl_stream; }

private:

  asio::ssl::stream<asio::ip::tcp::socket> _ssl_stream;

};

//--------------------------------------------------------------

class server {

public:

  server(char *port)

      : _context(), _ssl_context(context::sslv23),

        _endpoint(tcp::v4(), std::atoi(port)), _acceptor(_context, _endpoint) {

    _ssl_context.use_tmp_dh_file("dh1024.pem");

    _ssl_context.set_options(context::default_workarounds | context::no_sslv2 |

                            context::single_dh_use);

    _ssl_context.set_verify_mode(boost::asio::ssl::verify_peer |boost::asio::ssl::verify_fail_if_no_peer_cert); //double

    _ssl_context.set_verify_mode(1); // server side

    _ssl_context.use_certificate_file("server.crt", context::pem);

    _ssl_context.use_private_key_file("server.key", context::pem);

  }

  void run() {

    listen();

    _context.run();

  }

  void listen() {

    std::unique_ptr<server_session> new_session(

        new server_session(&_context, &_ssl_context));

    _sessions.push_back(std::move(new_session));

    _acceptor.async_accept(_sessions.back()->stream()->lowest_layer(),

                          [this](std::error_code ec) {

                            if (!ec) {

                              _sessions.back()->do_handshake();

                            }

                            listen();

                            // todo clean up old sessions

                          });

  }

private:

  asio::io_context _context;

  asio::ssl::context _ssl_context;

  tcp::endpoint _endpoint;

  tcp::acceptor _acceptor;

  std::vector<std::unique_ptr<server_session>> _sessions;

};

//--------------------------------------------------------------

int main(int argc, char *argv[]) {

  try {

    // take port from cmd line

    if (argc != 2) {

      std::cerr << "Usage: ssl_server <port>\n";

      return 1;

    }

    server server{argv[1]};

    server.run();

    return 0;

  } catch (const std::exception &e) {

    std::cerr << "Exception: " << e.what() << "\n";

    return 1;

  }

}

2.1 客户端配置

不需要客户证书和ca即可登录收发数据,只需要

//

// client.cpp

// ~~~~~~~~~~

//

// Copyright (c) 2003-2019 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 <functional>

#include <iostream>

#include <boost/asio.hpp>

#include <boost/asio/ssl.hpp>

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

using std::placeholders::_1;

using std::placeholders::_2;

enum { max_length = 1024 };

class client

{

public:

  client(boost::asio::io_context& io_context,

      boost::asio::ssl::context& context,

      const tcp::resolver::results_type& endpoints)

    : socket_(io_context, context)

  {

    socket_.set_verify_mode(boost::asio::ssl::verify_peer);

    socket_.set_verify_callback(

        std::bind(&client::verify_certificate, this, _1, _2));

    connect(endpoints);

  }

private:

  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;

  }

  void connect(const tcp::resolver::results_type& endpoints)

  {

    boost::asio::async_connect(socket_.lowest_layer(), endpoints,

        [this](const boost::system::error_code& error,

          const tcp::endpoint& /*endpoint*/)

        {

          if (!error)

          {

            handshake();

          }

          else

          {

            std::cout << "Connect failed: " << error.message() << "\n";

          }

        });

  }

  void handshake()

  {

    socket_.async_handshake(boost::asio::ssl::stream_base::client,

        [this](const boost::system::error_code& error)

        {

          if (!error)

          {

            send_request();

          }

          else

          {

            std::cout << "Handshake failed: " << error.message() << "\n";

          }

        });

  }

  void send_request()

  {

    std::cout << "Enter message: ";

    std::cin.getline(request_, max_length);

    size_t request_length = std::strlen(request_);

    boost::asio::async_write(socket_,

        boost::asio::buffer(request_, request_length),

        [this](const boost::system::error_code& error, std::size_t length)

        {

          if (!error)

          {

            receive_response(length);

          }

          else

          {

            std::cout << "Write failed: " << error.message() << "\n";

          }

        });

  }

  void receive_response(std::size_t length)

  {

    boost::asio::async_read(socket_,

        boost::asio::buffer(reply_, length),

        [this](const boost::system::error_code& error, std::size_t length)

        {

          if (!error)

          {

            std::cout << "Reply: ";

            std::cout.write(reply_, length);

            std::cout << "\n";

          }

          else

          {

            std::cout << "Read failed: " << error.message() << "\n";

          }

        });

  }

  boost::asio::ssl::stream<tcp::socket> socket_;

  char request_[max_length];

  char reply_[max_length];

};

std::string passwd_callback(std::size_t max_length,boost::asio::ssl::context::password_purpose){

    return "666666";

};

int main(int argc, char* argv[])

{

  try

  {

    if (argc != 3)

    {

      std::cerr << "Usage: client <host> <port>\n";

      return 1;

    }

    boost::asio::io_context io_context;

    tcp::resolver resolver(io_context);

    auto endpoints = resolver.resolve(argv[1], argv[2]);

    boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);

    ctx.set_password_callback(passwd_callback); //set passwd,clent.key passwd

    ctx.set_verify_mode(boost::asio::ssl::verify_peer);

    ctx.use_certificate_file("client.crt", boost::asio::ssl::context::pem);//load client.crt

    ctx.use_private_key_file("client.key", boost::asio::ssl::context::pem);//load client.key

    ctx.load_verify_file("ca.crt"); //load ca.crt--root crt

    client c(io_context, ctx, endpoints);

    io_context.run();

  }

  catch (std::exception& e)

  {

    std::cerr << "Exception: " << e.what() << "\n";

  }

  return 0;

}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357