ssl 无法使用boost验证我的自签名证书

ezykj2lf  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(179)

我通过自签名证书(用我自己的CA)生成,现在我正试图让一个boost ASIO客户端验证服务器的身份。我用openssl验证了这些,验证似乎有效。
服务器和客户端代码分别为herehere
我只修改了以下部分:

  1. class server
  2. {
  3. public:
  4. server(boost::asio::io_service& io_service, unsigned short port)
  5. : io_service_(io_service),
  6. acceptor_(io_service,
  7. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
  8. context_(boost::asio::ssl::context::tlsv12_server)
  9. {
  10. context_.set_options(
  11. boost::asio::ssl::context::default_workarounds
  12. | boost::asio::ssl::context::no_sslv2
  13. | boost::asio::ssl::context::single_dh_use);
  14. context_.set_password_callback(boost::bind(&server::get_password, this));
  15. // Use the certificate for my website that I had generated context_.use_certificate_file("/home/paul/ca/intermediate/certs/mywebsite.net.cert.pem", boost::asio::ssl::context::pem);
  16. // Not sure if I need this, probably not. I do have an intermediate CA though
  17. //context_.use_certificate_chain_file("/home/paul/ca/intermediate/certs/ca-chain.cert.pem");
  18. // Use website private key context_.use_private_key_file("/home/paul/ca/intermediate/private/mywebsite.net.key.pem", boost::asio::ssl::context::pem);
  19. context_.use_tmp_dh_file("/home/paul/SSLTest/dh512.pem");
  20. start_accept();
  21. }

在客户端:

  1. bool verify_certificate(bool preverified,
  2. boost::asio::ssl::verify_context& ctx)
  3. {
  4. // The verify callback can be used to check whether the certificate that is
  5. // being presented is valid for the peer. For example, RFC 2818 describes
  6. // the steps involved in doing this for HTTPS. Consult the OpenSSL
  7. // documentation for more details. Note that the callback is called once
  8. // for each certificate in the certificate chain, starting from the root
  9. // certificate authority.
  10. // In this example we will simply print the certificate's subject name.
  11. char subject_name[256];
  12. X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
  13. X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
  14. std::cout << "Verifying " << subject_name << "\n";
  15. std::cout << "preverified: " << std::boolalpha << preverified << "\n";
  16. return preverified;
  17. }
  18. ...
  19. int main(int argc, char* argv[])
  20. {
  21. try
  22. {
  23. boost::asio::io_service io_service;
  24. boost::asio::ip::tcp::resolver resolver(io_service);
  25. boost::asio::ip::tcp::resolver::query query("localhost", "3232");
  26. boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
  27. boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client);
  28. std::ifstream ca_file("/home/paul/ca/certs/ca.cert.pem", std::ios::binary | std::ios::ate);
  29. std::vector<char> data;
  30. auto size = ca_file.tellg();
  31. data.resize(size);
  32. ca_file.seekg(0, std::ios::beg);
  33. ca_file.read(data.data(), size);
  34. ca_file.close();
  35. // Have my own CA added to the list of known CAs
  36. ctx.add_certificate_authority(boost::asio::buffer(data, data.size()));
  37. // Not sure if I need something here, the CA should be enough to
  38. // validate the server's certificate prompted (even if signed by the
  39. // intermediate CA)
  40. //ctx.load_verify_file("/home/paul/ca/private/ca.key.pem");
  41. //ctx.load_verify_file("/home/paul/ca/intermediate/private/intermediate.key.pem");
  42. client c(io_service, ctx, iterator);

然而,这是行不通的,客户端返回

  1. Verifying /C=IT/ST=Italy/L=Milan/O=MyCompanyLtd/OU=MyCompanyLtd Auth/CN=mywebsite.net/[email protected]
  2. preverified: false
  3. Handshake failed: certificate verify failed

我知道回调中没有执行验证,但我认为set_verification_callback中设置的回调将在预验证后调用(因此使用preverified参数)。
我哪里错了?

eivnm1vs

eivnm1vs1#

光靠你提供的数据是不行的。没有足够的信息来验证证书。一般来说,这两个人都应该打电话。

  1. ctx.use_certificate_chain_file("path");
  2. ctx.use_private_key_file("path", boost::asio::ssl::context::pem);

相关问题