Authenticating a self-signed certificate for LDAPS connection

时光毁灭记忆、已成空白 提交于 2019-12-03 04:06:44

You have to explicitly tell the LDAP client to ignore untrusted certificates. You can do so by adding the following to your ldap.conf file:

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.

JCotton

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.

  1. Put your CA's certificate file in /etc/ldap/certs/myca.pem (you may have to mkdir the certs directory).
  2. Add a new line with TLS_CACERT /etc/ldap/certs/myca.pem to /etc/ldap/ldap.conf. (You may see a similar line with "/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. The comments for the function documentation provide further reading but everyone seems to go straight at setting "TLS_REQCERT never". Working != secure. demand is the default setting and I'd leave it that way (or explicitly set that). The documentation on TLS_REQCERT is here. (It also appears that if you do set "never" but follow with a later "TLS_CACERT" line, it ignores the never. Ugh.)

Note: I know you used "ldaps://" and ldap_bind(), but try the preferred ldap_start_tls().

STARTTLS is an alternative approach that is now the preferred method of encrypting an LDAP connection.

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.

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

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. I haven't verified this yet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!