如何使用 OpenSSL 1.1.0 验证主机名?

How can I validate hostnames with OpenSSL 1.1.0?

我正在尝试实现示例 hostname validation with 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 实现对主机名的 彻底 验证?我不想重新实现(最有可能使用 bugs/wrongly)库中已经实现的内容...

我正在使用 Ubuntu 16.04 和这个 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;

在这种情况下,一种情况是通过添加以下代码确保 OpenSSL 在连接时自动执行检查:

SSL_set_verify(ssl, SSL_VERIFY_PEER, 0);

在调用 SSL_connect 之前,或者按照与之前相同的路径并通过 SSL_get_verify_result return 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