Signing a certificate using cpprestsdk & boost
I am trying to bind a certificate by [cpprestsdk] [1], so far with no success.
I saw inside the http_client_config object that can be called by the set_ssl_context_callback method and inside this method, bind it to a custom certificate verification method - set_verify_callback.
When I debug my code, the * set_verify_callback * method is called after the request is sent, but my custom validation method is never called.
I have added some sample code that demonstrates the above behavior.
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;
}
source to share
I found a workaround for using this certificate-pinned library.
This workaround requires overriding the method in the library .
In the file https://github.com/Microsoft/cpprestsdk/blob/master/Release/src/http/client/http_client_asio.cpp
override method
bool handle_cert_verification (bool preverified, boost :: asio :: ssl :: verify_context & Ctx confirmation)
then recompile the library and replace it in your project.
I presented the owners with a solution for my main branch that calls set_verify_callback instead of using my own implementation.
source to share