【问题标题】:Loading CA certificate from memory从内存中加载 CA 证书
【发布时间】:2014-03-13 07:04:12
【问题描述】:

我正在尝试从内存而不是文件加载 CA 证书。但是我在连接时不断收到握手错误。文件加载完美,内存加载失败。我错过了什么?

std::ifstream file("message_server_ca.crt");
std::vector<char> fileContents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
boost::asio::const_buffer buffer(&fileContents.at(0),fileContents.size());

bool useFile = false;  // switch between file and memory loading.
boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23);
ctx.set_verify_mode(boost::asio::ssl::context::verify_peer);

if(useFile)
{
    // This works perfectly!
    ctx.load_verify_file("message_server_ca.crt");
}
else
{
    // This fails the handshake (asio.ssl:336134278)
    ctx.use_certificate(buffer,boost::asio::ssl::context_base::pem);
}

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

【问题讨论】:

  • 您正在使用两种不同的功能。 load_verify_fileuse_certificate。应该是 use_certificateuse_certificate_file
  • use_certificate_file 也不起作用。所以我使用了有效的 load_verify_file。
  • 我在其他线程中看到了有关 use_private_key_file 方法的信息。但这是一个 SSL 客户端,我在使用 load_verify_file 方法时没有指定私钥。
  • 使用use_certificate的错误码版本,找到错误描述。
  • 这可以使用纯 openssl api 来完成,但我从未使用过 boost 包装器。如果您使用带有自签名 ca 证书的SSL_CTX_use_certificate(),那么您可能会收到X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 错误。当我在我的 ca 包中使用自签名证书(或多个证书)时,我创建了我的 X509 对象(可以从内存中执行此操作)并使用以下函数调用组合之一:1.) X509_STORE_new() X509_STORE_add_cert() SSL_CTX_set_cert_store() 2.) SSL_CTX_get_cert_store() X509_STORE_add_cert()。尝试在 boost 实现的源代码中搜索这些。

标签: c++ boost openssl boost-asio


【解决方案1】:

看来你想要add_certificate_authority():

此功能用于添加一个受信任的证书颁发机构来自 内存缓冲区。

use_certificate()use_certificate_file() 用于握手中提供的服务器或客户端证书,即不是用于测试这些证书的 CA。

这些函数(load_verify_file()add_certificate_authority())的命名不一致。我猜是因为内存缓冲区版本是最近才添加的。

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2021-09-03
    • 2015-12-27
    • 2021-01-17
    • 2020-03-02
    • 2015-07-02
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多