如何使用OpenSSL 1.1.0验证主机名
How can I validate hostnames with OpenSSL 1.1.0?
我正在尝试使用openSSL实现示例主机名验证。我组合的示例C/C 代码是:
// please note I'm connecting to https://openssl.org itself
// enable SNI
if(!SSL_set_tlsext_host_name(ssl, "www.openssl.org")) throw;
if(!SSL_connect(ssl)) throw;
// connection is fine, I can get the homepage via HTTP
X509 *cert = SSL_get_peer_certificate(ssl);
if(cert) {
if(!X509_VERIFY_PARAM_set1_host(SSL_get0_param(ssl), "google.com", 0)) throw;
SSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
const long cert_res = SSL_get_verify_result(ssl);
if(cert_res == X509_V_OK) {
printf("Certificate verified!n");
}
X509_free(cert);
}
根据上面的代码,我成功地连接到 openssl.org 域;然后,我将名称设置为 google.com 以测试失败,但是代码仍然成功。
我在做什么错?如何使用OpenSSL API实现彻底验证主机名的验证?我不想重新实施(最有可能使用错误/错误(,库中已经实现了什么...
我正在使用ubuntu 16.04,this libssl 版本: /lib/x86_64-linux-gnu/libssl.so.1.0.0
。
jww
所建议的,一个人只需要设置(至少(
if(!X509_VERIFY_PARAM_set1_host(SSL_get0_param(ssl), "google.com", 0)) throw;
在执行连接本身之前:
if(!SSL_connect(ssl)) throw;
在这种情况下,通过添加以下代码:
来确保OpenSL在连接时间自动实现支票SSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
在调用SSL_connect
之前,或遵循与以前相同的路径,如果要在更多详细信息中处理问题,请通过SSL_get_verify_result
返回X509_V_ERR_HOSTNAME_MISMATCH
:
// please note I'm connecting to https://openssl.org itself
// enable SNI
if(!SSL_set_tlsext_host_name(ssl, "www.openssl.org")) throw;
// enable name/domain verification
if(!X509_VERIFY_PARAM_set1_host(SSL_get0_param(ssl), "google.com", 0)) throw;
if(!SSL_connect(ssl)) throw;
// get certificate
X509 *cert = SSL_get_peer_certificate(ssl);
if(cert) {
const long cert_res = SSL_get_verify_result(ssl);
// in case of name/domain mismatch cert_res will
// be set as 62 --> X509_V_ERR_HOSTNAME_MISMATCH
if(cert_res != X509_V_OK) throw; // certificate has been tampered with
X509_free(cert);
} else throw; // we couldn't get certificate
相关文章:
- 正在尝试了解输入验证循环
- 如何在C++中检查2D数组中负值的输入验证
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- LibGit2 SSH身份验证失败
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- constexpr上下文中std::initializer_list的验证
- 正在验证c++中用户的整数输入
- 加密++验证大文件签名
- C++卡验证问题
- 验证指针链
- 此代码验证公式是什么意思?
- cin 的十进制输入验证?
- 如何停止 CLR 主机?
- 简单的 HTML 验证器
- 模拟的 HTTP 身份验证仅在本地主机上工作
- 如何使用OpenSSL 1.1.0验证主机名
- Boost、asio、https 和主机/证书验证
- 使用SSL通配符证书验证主机
- 在连接(ping)之前验证IP地址(主机)是否存在