Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

in your php code. In *nix you need to edit your /etc/ldap.conf to contain

TLS_REQCERT never

Another thing to be aware of is that it requires version 3 (version 2 is php default):

//$hostnameSSL example would be "ldaps://just.example.com:636" , just make sure it has ldaps://
$con = ldap_connect($hostnameSSL);
ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);

To get a better idea of what's going on, you can enable debug logging by:

ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);

This can be done before the ldap_connect takes place.

Doing just this didn't work for me. What I had to do (following the thread at: mediawiki.org/wiki/Thread:Extension_talk:LDAP_Authentication/…), was to set $wgLDAPEncryptionType = array('YOUR_DOMAIN' => 'clear'); in the LocalSettings.php. That got it up and working immediately. – miCRoSCoPiC_eaRthLinG Nov 29, 2012 at 10:39 Please not that by disabling certificate verification you open up a security hole by allowing man in the middle attack. You are encrypting the transmission without verifying the destination! – svandragt Dec 2, 2014 at 10:11 @svandragt I don't even care at this point. If it will return me something that is not an error I will be eternally grateful. On a windows system running PHP 5.3, the answer above does not work – Kellen Stuart Apr 17, 2017 at 17:56 i agree on fixing the ssl issue; however, +1 for ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); since without that, fixing the issue wasn't going to work. all references were wrong about where my win+cygwin+php ldap.conf was being looked for (/etc/openldap/ldap.conf is where it ended up being looked for). – WEBjuju Feb 15, 2019 at 23:05

The specific scenario presented in the question--with an expired certificate that can't be changed--does appear to require disabling certificate validation on the LDAP client.

However, I suspect a lot of people, like me, reach this page for other root causes of receiving opaque LDAP TLS errors, where disabling validation of TLS certificates is not an appropriate answer.

In my case--using the LDAP Authentication extension for Mediawiki on an Ubuntu 18.04 LTS server, and authenticating against Active Directory on a Windows Server 2012 server--authentication stopped working in January/February 2020. The server certificate and the CA certificate were still both valid, and openssl s_client -verify 2 -connect <AD server>:636 from the Mediawiki server passed just fine.

Eventually I noticed that the signature algorithm in the SSL certificate served by AD/LDAP was SHA1, which I remembered recently suffered from the first known chosen-prefix collision exploit. This led me to investigate the changelog for packages that had recently been updated on the system, which turned up "Mark SHA1 as insecure for certificate signing" in the gnutls28 changelog circa January 8th, 2020. (The chain of dependencies from the php-ldap package in Ubuntu 18.04 goes to php7.2-ldap -> libldap-2.4-2 -> libgnutls30, whose source package is gnutls28.)

I followed some instructions to update the Windows CA to use SHA256 and then selectively followed instructions to renew the AD/LDAP cert, installed the new CA cert on my Mediawiki server, and the problem was solved! Briefly, these steps included:

  • In an Admin PowerShell on the AD server, run certutil -setreg ca\csp\CNGHashAlgorithm SHA256
  • In the Certification Authority MMC, right click on the CA -> All Tasks -> Renew CA Certificate
  • In a blank MMC, add snap-in for Certificates; select Local Computer
  • Under Personal -> Certificates, find the current entry used by LDAPS (Kerberos Authentication template type) -> All Tasks -> Advanced Options -> Renew This Certificate with the Same Key
  • In the same window, open the new CA certificate -> Details -> Copy to file -> no private key -> base64-encoded X.509
  • Copy the resulting file to /usr/share/ca-certificates/ on the Mediawiki server, then run sudo dpkg-reconfigure ca-certificates and select the new CA cert for inclusion.
  • P.S. For SEO purposes, depending on the mode I was using, error messages included:

  • ldap_start_tls(): Unable to start TLS: Connect error in /var/www/mediawiki/extensions/LdapAuthentication/LdapAuthenticationPlugin.php in the HTTP error log
  • ldap_start_tls(): Unable to start TLS: Can't contact LDAP server in [...]
  • Failed to start TLS. in the Mediawiki debug log (when using wgLDAPEncryptionType = ssl, i.e. encrypted LDAP port, 636)
  • Failed to bind as CN=foobar,CN=Users,DC=myOrgName,DC=local in the Mediwiki debug log (when using wgLDAPEncryptionType = tls, i.e. STARTTLS on the unencrypted LDAP port, 389)
  • @KolobCanyon Yep; I didn't either. Just making the folders and ldap.conf file with TLS_REQCERT never worked for me. If you're concerned about having OpenLDAP, try echo phpinfo(); and check the output for your LDAP version. – Nelson Frew Jun 29, 2020 at 1:31

    Create the directory /etc/ldap/cacerts and copy the cacert to /etc/ldap/cacerts/cacert.asc

    Restart apache.

  • In redhat based systems:

    Install the package: openldap-clients and in the file /etc/openldap/ldap.conf edit the line:

    TLS_CACERT /etc/openldap/cacerts/cacert.asc
    

    Create the directory /etc/openldap/cacerts and copy the cacert to /etc/openldap/cacerts/cacert.asc

    Restart httpd

  • This looks like the correct answer (copying the specific cert I want to trust instead of ignoring certificate verification). Unfortunately, I can't verify it anymore as we obsoleted that particular setup. – user323094 Oct 12, 2015 at 10:22

    I was able to get this working properly with openldap on Amazon Linux (Elastic Beanstalk PHP 7.0) with MacOS Server 5 LDAP, with TLS set to demand.

    in /etc/openldap/ldap.conf:

    TLS_REQCERT demand

    TLS_CACERT /etc/openldap/certs/yourcacert.pem

    (note that if you are not using openldap, the path will be /etc/ldap/certs/yourcacert.pem). This setup did not work until I placed the certificate inside the certs folder; it did not work from any other path.

    The certificate to be placed in that path is NOT the TLS certificate of the server. It is the CA (Certificate Authority) certificate of the authority whom issued the server/domain specific TLS certificate. Only the CA certificate placed in that path will allow TLS to work before attempting an LDAP bind in php. Get the CA certificate from your server or download it from the authority's site, they are freely available.

    To test if LDAP bind is even working without TLS, set TLS_REQCERT never temporarily (may need to comment # out TLS_CACERT). If you get "Can't connect to LDAP" it is not a TLS error; it simply cannot connect to the server and you likely need to open port 389 (not 636 for TLS).

    Remember to restart your Apache server every time you make a change to the config file or certificate.

    Some additional help for others, the certificate solution here solved my ldapsearch command line issue, but still PHP complained **Can't contact LDAP server**

    Turned out to be SELinux on RHEL7 ( CentOS7 ) blocks HTTPD from using LDAP ports 389 and 636 by default, you can unblock with:

    setsebool -P httpd_can_network_connect 1
    

    Check your SELinux audit log file for things being blocked.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.