Loading CA certificate from memory

ぃ、小莉子 提交于 2021-02-08 02:00:46

问题


I am trying to load CA certificate from memory instead of file. But I keep getting handshake error while connecting. The file loading works perfectly, memory loading fails. What am I missing?

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();

回答1:


It appears that you want add_certificate_authority():

This function is used to add one trusted certification authority from a memory buffer.

use_certificate() and use_certificate_file() are for the server or client certificate presented in the handshake, i.e. not the CA used to test those certificates.

These functions (load_verify_file() and add_certificate_authority()) are not consistently named. I guess it is because the memory buffer versions were added relatively recently.



来源:https://stackoverflow.com/questions/22371410/loading-ca-certificate-from-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!