我试图执行证书锁定在[cpprestsdk] [1],迄今没有成功。证书锁定使用cpprestsdk&boost
我在里面http_client_config对象所看到的,我们可以调用方法set_ssl_context_callback和方法内,将其链接到自定义证书验证方法 - set_verify_callback。
当我调试我的代码时,方法* set_verify_callback *在请求发送后调用,但我的自定义验证方法从不调用。
我在下面添加了一个示例代码,演示了上述行为。
bool verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx)
{
// this code is never invoked
char subject_name[256];
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
std::cout << "Verifying:\n" << subject_name << std::endl;
return preverified;
}
pplx::task<void> sendRequest()
{
http_client_config config;
config.set_validate_certificates(true);
config.set_ssl_context_callback([](boost::asio::ssl::context& ctx)
{
// this code is invoked right after calling client.request
ctx.set_verify_mode(boost::asio::ssl::context::verify_peer);
ctx.set_verify_callback(&verify_certificate);
});
http_client client("https://google.com", config);
http_request request(methods::GET);
return client.request(request).then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::create_task([] { return json::value(); });
}).then([](json::value jsonValue)
{
});
}
int main(int argc, const char * argv[])
{
try
{
sendRequest().wait();
}
catch (std::exception& e)
{
std::wostringstream ss;
ss << e.what() << std::endl;
std::wcout << ss.str();
// Return an empty task.
}
return 0;
}
OWASP在[Certificate and Public Key Pinning](https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning)上有OpenSSL的复制/粘贴示例。它真正的固定;而不是来自[RFC 7469](https://tools.ietf.org/html/rfc7469)的网页和浏览器的演绎。 – jww
@jww - 谢谢,但它不相关,因为我面临的问题是不会调用验证的回调。 另外,当我尝试使用native_handle访问SSL *对象时,使用SSL_get_peer_certificate(ssl)的证书为空; (我使用https://google.com测试它) – IdoT
顺便说一句,通过在没有cpprest的情况下使用boost来调用回调。 – IdoT