Reputation: 2871
I'm trying to implement certificate pinning over [cpprestsdk][1], so far without success.
I have seen in inside http_client_config object we can call method set_ssl_context_callback and inside that method, link it to custom certificate validation method - set_verify_callback.
When i debugged my code, the method * set_verify_callback* is called after the request is sent, yet my custom validation method is never called.
I have added a sample code below which demonstrate the behaviour described above.
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;
}
Upvotes: 3
Views: 2389
Reputation: 2871
I've found a workaround for using this library with certificate pinning.
This workaround requires to override a method in the library.
In the file https://github.com/Microsoft/cpprestsdk/blob/master/Release/src/http/client/http_client_asio.cpp
override the method
bool handle_cert_verification(bool preverified, boost::asio::ssl::verify_context &verifyCtx)
then recompile the library and replace it in your project.
I have submitted the owners the solution for their main branch, which is calling the set_verify_callback instead of using their own implementation.
Upvotes: 1