【问题标题】:cpprestsdk Websocket TLS Handshake failed errorcpprestsdk Websocket TLS握手失败错误
【发布时间】:2021-11-13 08:38:48
【问题描述】:
我是 cpprestsdk 的新手,我需要一个 websocket 客户端。到目前为止,我能够让 websocket 客户端通过 wss 服务器连接,并将 validate_certificates 设置为 false。当我启用它时,我收到“TLS 握手失败”错误。在检查来自服务器的日志时,我看到客户端正在发送初始握手消息,当服务器响应时它会发送失败消息。
当我将证书验证设置为 false 时,客户端会发送一条成功消息。
如何配置 websocket 客户端以验证自签名证书以及 CA 颁发的证书?
【问题讨论】:
标签:
websocket
cpprest-sdk
【解决方案1】:
看起来在 Windows 机器上,SSL 上下文无法从根 CA 存储加载证书。通过加载证书并将其分配给 ssl 上下文来修复它。
websocket_client_config config;
config.set_ssl_context_callback([this]
(boost::asio::ssl::context& ctx) {
// attach X509_STORE to boost ssl context
HCERTSTORE hStore = CertOpenSystemStore(0, "ROOT");
if (hStore == NULL) {
return;
}
m_RootCACertificateStore = X509_STORE_new();
PCCERT_CONTEXT pContext = NULL;
while ((pContext = CertEnumCertificatesInStore(hStore,
pContext)) != NULL) {
// convert from DER to internal format
X509* x509 = d2i_X509(NULL,
(const unsigned char**)&pContext->pbCertEncoded,
pContext->cbCertEncoded);
if (x509 != NULL) {
X509_STORE_add_cert(m_RootCACertificateStore, x509);
X509_free(x509);
}
}
CertFreeCertificateContext(pContext);
CertCloseStore(hStore, 0);
SSL_CTX_set1_cert_store(
ctx.native_handle(),m_RootCACertificateStore );
});`