在非阻塞模式下,openssl 允许在客户端使用任何证书

In non-blocking mode openssl allows to use any certificate on client side

我使用

将 BIO 更改为非阻塞模式

BIO_set_nbio(m_bio, 1)

BIO_do_connect 不挂起(并使用 BIO_should_retryselect 重试重新连接)。它解决了我连接到错误侦听器的问题,现在立即失败,而不是在 2 小时内超时。

但现在我有一个新问题 - SSL_get_verify_result 总是 returns X509_V_OK。不管它是过期证书还是与服务器证书不同 - 验证总是成功。

我不明白的是非阻塞模式如何以及为什么更改证书的验证。我确认如果客户端证书不相同,则在不切换到非阻塞模式的情况下验证失败。

我尝试用 SSL_CTX_load_verify_locationsSSL_CTX_use_certificate_file 设置客户端证书,但似乎没关系。

正如帕特里克指出的那样,我遇到的问题是由于非阻塞模式下的 SSL 上下文没有服务器证书,因此在建立连接后立即进行验证总是成功的。为了解决这个问题,我在第一次读写数据交换后添加了证书验证(如果它与我希望看到的相同)。

SSL_CTX_new(SSLv23_client_method());
SSL_CTX_load_verify_locations(ctx, certFilePath, NULL);
BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, &ssl);
BIO_set_conn_hostname(bio, hostname);
BIO_set_nbio(bio, 1);
auto err = BIO_do_connect(bio);
// check err and BIO_should_retry to retry as needed

// Problem I had was here - the call always returns X509_V_OK
// in non-blocking mode regardless of what cert I use on 
// my (client) side.
// In IO blocking mode (without BIO_set_nbio call above) validation
// fails as expected for mismatching certs.
SSL_get_verify_result(ssl);

// Exchange some data with server with
// BIO_read/BIO_write

// And now we have server cert in ctx
auto clientCert = SSL_get_certificate(ssl);
auto serverCert = SSL_get_peer_certificate(ssl);
auto certsAreTheSame = X509_cmp(clientCert, serverCert) == 0;