Skip to content

Commit a1cd0dc

Browse files
committed
Added a couple of tests on using certificates with ldap (due to gnu-tls or openssl possible usage)
1 parent bd7faf7 commit a1cd0dc

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

server/src/uds/core/util/ldaputil.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ def connection(
129129
l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) # type: ignore
130130
# Disable TLS1 and TLS1.1
131131
# 0x304 = TLS1.3, 0x303 = TLS1.2, 0x302 = TLS1.1, 0x301 = TLS1.0, but use ldap module constants
132-
tls_version = {
133-
'1.2': ldap.OPT_X_TLS_PROTOCOL_TLS1_2, # type: ignore
134-
'1.3': getattr(ldap, 'OPT_X_TLS_PROTOCOL_TLS1_3', ldap.OPT_X_TLS_PROTOCOL_TLS1_2), # type: ignore
135-
}.get(getattr(settings, 'SECURE_MIN_TLS_VERSION', '1.2'), ldap.OPT_X_TLS_PROTOCOL_TLS1_2) # type: ignore
136-
137-
l.set_option(ldap.OPT_X_TLS_PROTOCOL_MIN, tls_version) # type: ignore
132+
if hasattr(ldap, 'OPT_X_TLS_PROTOCOL_TLS1_3'):
133+
tls_version = {
134+
'1.2': ldap.OPT_X_TLS_PROTOCOL_TLS1_2, # type: ignore
135+
'1.3': getattr(ldap, 'OPT_X_TLS_PROTOCOL_TLS1_3', ldap.OPT_X_TLS_PROTOCOL_TLS1_2), # type: ignore
136+
}.get(getattr(settings, 'SECURE_MIN_TLS_VERSION', '1.2'), ldap.OPT_X_TLS_PROTOCOL_TLS1_2) # type: ignore
137+
138+
l.set_option(ldap.OPT_X_TLS_PROTOCOL_MIN, tls_version) # type: ignore
138139
# Cipher suites are from GNU TLS, not OpenSSL
139140
# https://gnutls.org/manual/html_node/Priority-Strings.html for more info
140141
# i.e.:
@@ -143,8 +144,14 @@ def connection(
143144
# * PFS
144145
# * SECURE256
145146
#
146-
l.set_option(ldap.OPT_X_TLS_CIPHER_SUITE, cipher_suite) # type: ignore
147-
l.set_option(ldap.OPT_X_TLS_NEWCTX, 0) # type: ignore
147+
# Note: Your distro could have compiled libldap with OpenSSL, so this will not work
148+
# You can simply use OpenSSL cipher suites, but you will need to test them
149+
try:
150+
l.set_option(ldap.OPT_X_TLS_CIPHER_SUITE, cipher_suite) # type: ignore
151+
l.set_option(ldap.OPT_X_TLS_NEWCTX, 0) # type: ignore
152+
except Exception:
153+
logger.info('Cipher suite %s not supported by libldap', cipher_suite)
154+
148155

149156
l.simple_bind_s(who=username, cred=password)
150157
except ldap.SERVER_DOWN as e: # type: ignore

0 commit comments

Comments
 (0)