c – 使用cpprestsdk和boost固定证书
我试图在[cpprestsdk] [1]上实现证书固定,到目前为止还没有成功.
我在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; } 解决方法
我找到了使用此库与证书固定的解决方法.
此解决方法需要覆盖库中的方法. 在文件https://github.com/Microsoft/cpprestsdk/blob/master/Release/src/http/client/http_client_asio.cpp中 覆盖方法
然后重新编译库并在项目中替换它. 我已经向业主提交了他们的主分支的解决方案,它正在调用set_verify_callback而不是使用他们自己的实现. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |