c++ - 使用 OpenSSL 的相互认证总是成功的

标签 c++ ssl openssl mutual-authentication

我正在使用 openssl 和 zmq 编写服务器和客户端。 我的客户端和服务器需要相互验证。 但是在服务器上设置 SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_FAIL_IF_NO_PEER_CERT,NULL) 之后,无论客户端是否发送证书,握手总是成功。 此外,SSL_get_peer_certificate(tls->get_ssl_()) 返回 null,SSL_get_verify_result(tls->get_ssl_()) 返回 0,这意味着 X509_V_OK .

我现在真的很迷茫,很绝望。有什么建议或更正吗?

这是我的部分代码:

OpenSSL_add_all_algorithms();
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();

const SSL_METHOD *meth;
SSL_CTX *ssl_ctx;

     //**************************part of client************************
  {
    meth = SSLv23_client_method();
    ssl_ctx = SSL_CTX_new(meth);   


    SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_PEER,NULL);

    int rc1 = SSL_CTX_load_verify_locations(ssl_ctx, ".\\demoCA\\private\\server_chain.pem",".\\demoCA\\private\\");///   
     SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx,"pw");

     std::string cert_chain(".\\demoCA\\private\\client_chain.pem");
     std::string cert(".\\demoCA\\private\\client_crt.pem");
     std::string key(".\\demoCA\\private\\client_key.pem");

     int code = SSL_CTX_use_certificate_chain_file(ssl_ctx,cert_chain.c_str());

     if (code != 1)
    {
         std::cout<<"error1\n";
        //throw TLSException("failed to read credentials.");
     }
    code = SSL_CTX_use_PrivateKey_file(ssl_ctx,key.c_str(),SSL_FILETYPE_PEM);   
    i f (code != 1)
    {
        std::cout<<"error2\n";
        //throw TLSException("failed to read credentials.");
    }
    if(!SSL_CTX_check_private_key(ssl_ctx))
    {
        std::cout<<"key wrong";
        system("pause");
        exit(0);
    }
   }

//*****************part of server****************************
{
    meth = SSLv23_server_method();
    ssl_ctx = SSL_CTX_new(meth);

    SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_FAIL_IF_NO_PEER_CERT,NULL)   
    SSL_CTX_set_client_CA_list(ssl_ctx,SSL_load_client_CA_file(".\\demoCA\\private\\client_chain.pem"));//

    SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx,"pw");

    std::string cert_chain(".\\demoCA\\private\\server_chain.pem");
    std::string cert(".\\demoCA\\private\\server_crt.pem");
    std::string key(".\\demoCA\\private\\server_key.pem");

    int rc = SSL_CTX_use_certificate_file(ssl_ctx,cert.c_str(),SSL_FILETYPE_PEM);

    if (rc!=1)
    {
        //throw TLSException("failed to read credentials.");
        std::cout<<"error1\n";
    }

    rc = SSL_CTX_use_PrivateKey_file(ssl_ctx,key.c_str(),SSL_FILETYPE_PEM);

    if (rc!=1)
    {
        //throw TLSException("failed to read credentials.");   
        std::cout<<"error2\n";
    }

    int rcode = SSL_CTX_check_private_key(ssl_ctx);
    if(rcode!=1)
    {
        std::cout<<"key wrong";
        system("pause");
        //exit(0);
    }
}

最佳答案

来自documentation of SSL_CTX_set_verify :

SSL_VERIFY_FAIL_IF_NO_PEER_CERT

Server mode: if the client did not return a certificate, the TLS/SSL handshake is immediately terminated with a "handshake failure" alert. This flag must be used together with SSL_VERIFY_PEER.

您没有按照文档中的描述将它与 SSL_VERIFY_PEER 一起使用,因此它没有任何效果。

关于c++ - 使用 OpenSSL 的相互认证总是成功的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28392273/

相关文章:

c++ - 完美转运不取货方式

c# - 如何在 C# 中使用 SSL 连接到 LDAP Novell?

wordpress - 子域的 .htaccess 规则强制在所有页面上使用 https

python - 为什么 pyOpenSSL 有 OpenSSL DLL 的单独副本?

openssl - 为什么验证比签名快得多?

ssl - 从 openssl s_server 查看 pem 格式的客户端证书

c++ - 如何访问 std::auto_ptr 指向的对象

C++ lambda,没有看到函数和参数?

c++ - Boost序列化多个对象

postgresql - 无法在 Kong 0.10.x 和数据存储 postgresql-9.6 之间建立 SSL/TLS 连接