简体   繁体   English

验证LDAPS连接的自签名证书

[英]Authenticating a self-signed certificate for LDAPS connection

I want to make a secure ldap connection(ldaps) from a Linux(Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux) client to a Windows 2012 server, to change user passwords in active directory, through php. 我想从Linux(Linux 3.2.0-4-amd64#1 SMP Debian 3.2.51-1 x86_64 GNU / Linux)客户端到Windows 2012服务器建立安全的ldap连接(ldaps),以更改活动中的用户密码目录,通过PHP。 For that, I've created a self-signed certificate(using Windows Server Manager) on the server, but when I try to connect, I get the following error(by turning debugging option on: ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); ): 为此,我在服务器上创建了一个自签名证书(使用Windows Server Manager),但是当我尝试连接时,我收到以下错误(通过打开调试选项: ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); ):

ldap_create                                                                 
ldap_url_parse_ext(ldaps://xxx.xxx.xxx.xxx)                                 
ldap_bind_s                                                                 
ldap_simple_bind_s                                                          
ldap_sasl_bind_s                                                            
ldap_sasl_bind                                                              
ldap_send_initial_request                                                   
ldap_new_connection 1 1 0                                                   
ldap_int_open_connection                                                    
ldap_connect_to_host: TCP xxx.xxx.xxx.xxx:636                               
ldap_new_socket: 3                                                          
ldap_prepare_socket: 3                                                      
ldap_connect_to_host: Trying xxx.xxx.xxx.xxx:636                            
ldap_pvt_connect: fd: 3 tm: -1 async: 0                                     
TLS: peer cert untrusted or revoked (0x42)                                  
TLS: can't connect: (unknown error code).                                   
ldap_err2string                                                             
PHP Warning:  ldap_bind(): Unable to bind to server 

It seems the client is not able to trust the certificate since it's self-signed. 似乎客户端无法信任证书,因为它是自签名的。

What steps should I take to make a secure connection? 我应采取哪些步骤来建立安全连接? The client side certificates are stored in /etc/ssl/certs/ca-certificates.crt 客户端证书存储在/etc/ssl/certs/ca-certificates.crt

You have to explicitly tell the LDAP client to ignore untrusted certificates. 您必须明确告诉LDAP客户端忽略不受信任的证书。 You can do so by adding the following to your ldap.conf file: 您可以通过将以下内容添加到ldap.conf文件中来执行此操作:

TLS_REQCERT never

This solution is not the preferred one though. 但是,这种解决方案并不是首选。 You should add the required CA root to your client and ensure that the certificate is correctly generated with the server's name in it (and if my memory serves me right the complete CA chain) otherwise nothing would stop someone to perform a MITM attack. 您应该将所需的CA root添加到您的客户端,并确保证书是正确生成的,其中包含服务器的名称(如果我的内存为我提供完整的CA链),否则不会阻止某人执行MITM攻击。

Your LDAP server is using a self-signed certificate so, in order to trust that, the LDAP client needs the certificate for the CA that created that cert. 您的LDAP服务器正在使用自签名证书,因此,为了信任这一点,LDAP客户端需要为创建该证书的CA提供证书。

  1. Put your CA's certificate file in /etc/ldap/certs/myca.pem (you may have to mkdir the certs directory). 将您的CA证书文件放在/etc/ldap/certs/myca.pem (您可能需要mkdir certs目录)。
  2. Add a new line with TLS_CACERT /etc/ldap/certs/myca.pem to /etc/ldap/ldap.conf . 使用TLS_CACERT /etc/ldap/certs/myca.pem/etc/ldap/ldap.conf添加一个新行。 (You may see a similar line with "/etc/ssl/certs/ca-certificates.crt".) (您可能会看到与“/etc/ssl/certs/ca-certificates.crt”类似的行。)

     $ php -a Interactive mode enabled php > ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); ... ldap_init: using /etc/ldap/ldap.conf ... php > $conn = ldap_connect("your_ldap_server"); php > ldap_start_tls($conn); 

Without the configuration change, you'd see PHP Warning: ldap_start_tls(): Unable to start TLS: Connect error in php shell code on line 1 . 如果没有配置更改,您将看到PHP Warning: ldap_start_tls(): Unable to start TLS: Connect error in php shell code on line 1 The comments for the function documentation provide further reading but everyone seems to go straight at setting "TLS_REQCERT never". 功能文档的注释提供了进一步阅读,但似乎每个人都直接设置“TLS_REQCERT never”。 Working != secure. 工作!=安全。 demand is the default setting and I'd leave it that way (or explicitly set that). demand是默认设置,我会这样离开(或明确设置)。 The documentation on TLS_REQCERT is here . TLS_REQCERT上的文档就在这里 (It also appears that if you do set "never" but follow with a later "TLS_CACERT" line, it ignores the never. Ugh.) (似乎如果你设置“never”但后面跟着“TLS_CACERT”行,它会忽略never。呃。)

Note: I know you used "ldaps://" and ldap_bind(), but try the preferred ldap_start_tls(). 注意:我知道您使用了“ldaps://”和ldap_bind(),但尝试使用首选的 ldap_start_tls()。

STARTTLS is an alternative approach that is now the preferred method of encrypting an LDAP connection. STARTTLS是一种替代方法,现在是加密LDAP连接的首选方法。

For those that stumble across this, PHP7.1 now allows you to set the CA file and CA directory via ldap_set_option() rather than having to update a server configuration file. 对于那些偶然发现的人,PHP7.1现在允许您通过ldap_set_option()设置CA文件和CA目录,而不必更新服务器配置文件。

See the documentation for ldap_set_option here, paying attention to the LDAP_OPT_X_TLS_ options: https://secure.php.net/manual/en/function.ldap-set-option.php 请参阅此处的ldap_set_option文档,注意LDAP_OPT_X_TLS_选项: httpsLDAP_OPT_X_TLS_

One caveat: It seems there may be a bug where LDAP_OPT_X_TLS_CACERTFILE is only accepted when LDAP_OPT_X_TLS_CACERTDIR is also set, see https://bugs.php.net/bug.php?id=73558 . 一个警告:似乎可能存在仅在设置LDAP_OPT_X_TLS_CACERTFILE时才接受LDAP_OPT_X_TLS_CACERTDIR的错误,请参阅https://bugs.php.net/bug.php?id=73558 I haven't verified this yet. 我还没有验证这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM