相互身份验证总是成功与OpenSSL

问题描述:

我正在使用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

我真的很困惑,现在绝望。任何建议或更正?

这是我的代码部分:

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); 
    } 
} 
+0

另请参阅OpenSSL wiki上的[库初始化](https://wiki.openssl.org/index.php/Library_Initialization)。您不需要进行四次启动电话。 – jww 2015-02-08 09:42:22

+0

你的意思是? OpenSSL_add_all_algorithms(); SSL_library_init(); SSL_load_error_strings(); ERR_load_BIO_strings(); – 601492584 2015-02-08 09:50:38

documentation of SSL_CTX_set_verify

SSL_VERIFY_FAIL_IF_NO_PEER_CERT

服务器模式:如果客户未返回证书,TLS/SSL握手立即以“握手失败”警报终止。 该标志必须与SSL_VERIFY_PEER一起使用。

您没有将它与文档中所述的SSL_VERIFY_PEER一起使用,因此它不起作用。

+0

上帝!非常感谢 !!!我注意到了那个音符,但是我错误地指出了,我认为这意味着在客户端上使用SSL_VERIFY_PEER! – 601492584 2015-02-08 09:29:37

+0

非常感谢,阻止我浪费更多时间在这个愚蠢的错误上。 – 601492584 2015-02-08 09:30:42

+0

嘿...... ..我在这里有另一个问题,http://stackoverflow.com/questions/29211061/using-openssl-with-its-unblocked-bio-ssl-read-return-ssl-error-syscall-和-ssl。你可以看看吗?什么可以称为这种问题,任何想法? – 601492584 2015-03-24 07:52:02