我正在将TLS 1.3与OpenSSL 1.1.1一起使用。我想让服务器在握手期间/之后通过将其真实主机/ ip与在其x509自签名证书CN字段中找到的主机/ ip进行比较来验证连接的客户端。
在SSL_CTX
的配置过程中,我让对等方像这样验证自己。
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
TCP连接成功后,我在服务器端执行以下操作:
// Without this, client verification does not fail.
if (!SSL_set1_host(ssl, "SomeHostName")) { // This hostname is the same as the one found in certificates CN field
// Handle error...
}
if (SSL_set_fd(ssl, fd) != 1) {
// Handle error...
}
if ((retAccept = SSL_accept(ssl)) != 1) {
// Handle error...
}
printf("%ld\n", SSL_get_verify_result(ssl)); // '0' (X509_V_OK)
X509* peerCert = SSL_get_peer_certificate(ssl); // This contains "SomeHostName" as it should.
printf("\n\n%s\n\n", SSL_get0_peername(ssl)); // '(null)'
我的回调函数(与here相同)在SSL_accept期间打印出以下内容:
SSL_accept:before SSL initialization
SSL_accept:before SSL initialization
SSL_accept:SSLv3/TLS read client hello
SSL_accept:SSLv3/TLS write server hello
SSL_accept:SSLv3/TLS write change cipher spec
SSL_accept:TLSv1.3 write encrypted extensions
SSL_accept:SSLv3/TLS write certificate request
SSL_accept:SSLv3/TLS write certificate
SSL_accept:TLSv1.3 write server certificate verify
SSL_accept:SSLv3/TLS write finished
SSL_accept:TLSv1.3 early data
SSL_accept:TLSv1.3 early data
SSL3 alert write:fatal:bad certificate
SSL_accept:error in error
我还尝试启用/禁用与主机验证相关的某些标志,如下所示:
SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_WILDCARDS);
为什么主机验证失败?