2017-06-12 104 views
3

我试图执行证书锁定在[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; 
} 
+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

+0

@jww - 谢谢,但它不相关,因为我面临的问题是不会调用验证的回调。 另外,当我尝试使用native_handle访问SSL *对象时,使用SSL_get_peer_certificate(ssl)的证书为空; (我使用https://google.com测试它) – IdoT

+0

顺便说一句,通过在没有cpprest的情况下使用boost来调用回调。 – IdoT

回答

0

我发现了一个解决方法,使用此库与证书锁定。

此替代方法需要重写库中的方法。

在文件https://github.com/Microsoft/cpprestsdk/blob/master/Release/src/http/client/http_client_asio.cpp

覆盖的方法

布尔handle_cert_verification(布尔预验证, 升压:: ASIO :: SSL :: verify_context & verifyCtx)

然后重新编译库并将其替换到您的项目中。

我已经提交所有者的主分支的解决方案,这是调用set_verify_callback而不是使用自己的实现。