您应该熟悉来自 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),可能是因为密码错误或找不到文件。