带有密码的BOOST ASIO负载键
BOOST ASIO load key.pem with passphrase
目前我正在处理:
...
ctx.use_certificate_chain_file("./C/cert.pem");
ctx.use_private_key_file("./C/key.pem", boost::asio::ssl::context::pem);
ctx.load_verify_file("./C/ca.pem");
...
到目前为止,一切正常,但是我真正需要做的就是加载相同的键。处理加密的PEM文件,请记住,我对像Python这样的高级语言更加熟悉,因此C 不是我的Forte
任何帮助将不胜感激,谢谢!
您应该熟悉Python的回调。
首先定义回调函数:
using namespace boost::asio;
// this function is called to obtain password info about an encrypted key
std::string my_password_callback(
std::size_t max_length, // the maximum length for a password
ssl::context::password_purpose purpose ) // for_reading or for_writing
{
std::string password;
// security warning: !! DO NOT hard-code the password here !!
// read it from a SECURE location on your system
return password;
}
然后用set_password_callback()
设置回调:
// set the callback before you load the protected key
ctx.set_password_callback(my_password_callback);
// ...
// this will call my_password_callback if a password is required
ctx.use_private_key_file("key.pem",ssl::context::pem);
如果要使用类方法作为回调,
class server {
std::string password_callback(); //NOTE: no parameters
// ...
};
您可以使用boost::bind()
设置回调:
#include <boost/bind.hpp>
void server::startup() {
ctx_.set_password_callback(
boost::bind(&server::password_callback,this) );
// ...
}
无论哪种情况,boost::system::system_error
异常(基于std::exception
)都将被抛弃如果无法解密密钥,可能是因为密码错误或找不到文件。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 提升 ASIO 无法识别计时器对象
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 缓慢提升ASIO
- 从 Boost ASIO 获取 epoll 描述符 io_service对象
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- C++ Boost::asio串行通信与Arduino无法写入
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 提升 Asio TCP 服务器 处理多个客户端
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 具有内存顺序的原子负载存储
- 提升 ASIO - io_service 不要等待连接到线程
- 执行时使用 boost::asio::d eadline_timer 时出错
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- Boost.Asio/OpenSSL HTTPS GET certificate trouble
- C++ boost::asio::ip::tcp::acceptor 有时不接受连接器?
- boost::asio data owning `ConstBufferSequence`
- 带有密码的BOOST ASIO负载键