Debian Codegraph @

dirsrvtests/tests/suites/tls/cipher_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2022 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9 import pytest
10 import os
11 import ldap
12 import time
13 from lib389.config import Encryption
14 from test389.topologies import topology_st as topo
15 from lib389.utils import ds_is_older
16 from lib389.nss_ssl import NssSsl
17 from lib389.config import Config
18 from lib389._constants import DN_DM, PASSWORD, SECUREPORT_STANDALONE
19 from lib389.dseldif import DSEldif
20 import subprocess
21 import logging
22
23 log = logging.getLogger(__name__)
24
25 pytestmark = pytest.mark.tier1
26
27 LDAPSPORT = str(SECUREPORT_STANDALONE)
28 ENC_DN = 'cn=encryption,cn=config'
29
30 def test_long_cipher_list(topo):
31 """Test a long cipher list, and makre sure it is not truncated
32
33 :id: bc400f54-3966-49c8-b640-abbf4fb2377d
34 :setup: Standalone Instance
35 :steps:
36 1. Set nsSSL3Ciphers to a very long list of ciphers
37 2. Ciphers are applied correctly
38 :expectedresults:
39 1. Success
40 2. Success
41 """
42 ENABLED_CIPHER = "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384::AES-GCM::AEAD::256"
43 DISABLED_CIPHER = "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256::AES-GCM::AEAD::128"
44 CIPHER_LIST = (
45 "-all,-SSL_CK_RC4_128_WITH_MD5,-SSL_CK_RC4_128_EXPORT40_WITH_MD5,-SSL_CK_RC2_128_CBC_WITH_MD5,"
46 "-SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5,-SSL_CK_DES_64_CBC_WITH_MD5,-SSL_CK_DES_192_EDE3_CBC_WITH_MD5,"
47 "-TLS_RSA_WITH_RC4_128_MD5,-TLS_RSA_WITH_RC4_128_SHA,-TLS_RSA_WITH_3DES_EDE_CBC_SHA,"
48 "-TLS_RSA_WITH_DES_CBC_SHA,-SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA,-SSL_RSA_FIPS_WITH_DES_CBC_SHA,"
49 "-TLS_RSA_EXPORT_WITH_RC4_40_MD5,-TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5,-TLS_RSA_WITH_NULL_MD5,"
50 "-TLS_RSA_WITH_NULL_SHA,-TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA,-SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA,"
51 "-SSL_FORTEZZA_DMS_WITH_RC4_128_SHA,-SSL_FORTEZZA_DMS_WITH_NULL_SHA,-TLS_DHE_DSS_WITH_DES_CBC_SHA,"
52 "-TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA,-TLS_DHE_RSA_WITH_DES_CBC_SHA,-TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,"
53 "+TLS_RSA_WITH_AES_128_CBC_SHA,-TLS_DHE_DSS_WITH_AES_128_CBC_SHA,-TLS_DHE_RSA_WITH_AES_128_CBC_SHA,"
54 "+TLS_RSA_WITH_AES_256_CBC_SHA,-TLS_DHE_DSS_WITH_AES_256_CBC_SHA,-TLS_DHE_RSA_WITH_AES_256_CBC_SHA,"
55 "-TLS_RSA_EXPORT1024_WITH_RC4_56_SHA,-TLS_DHE_DSS_WITH_RC4_128_SHA,-TLS_ECDHE_RSA_WITH_RC4_128_SHA,"
56 "-TLS_RSA_WITH_NULL_SHA,-TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA,-SSL_CK_DES_192_EDE3_CBC_WITH_MD5,"
57 "-TLS_RSA_WITH_RC4_128_MD5,-TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,-TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,"
58 "-TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,+TLS_AES_128_GCM_SHA256,+TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"
59 )
60
61 topo.standalone.enable_tls()
62 enc = Encryption(topo.standalone)
63 enc.set('nsSSL3Ciphers', CIPHER_LIST)
64 topo.standalone.restart()
65 enabled_ciphers = enc.get_attr_vals_utf8('nssslenabledciphers')
66 assert ENABLED_CIPHER in enabled_ciphers
67 assert DISABLED_CIPHER not in enabled_ciphers
68
69
70 def connectWithOpenssl(topology_st, expect=True):
71 """
72 Connect with OpenSSL
73 Condition:
74 If expect is True, the handshake should be successful.
75 If expect is False, the handshake should be refused with
76 access log: "Cannot communicate securely with peer:
77 no common encryption algorithm(s)."
78 """
79 log.info(f'Testing connection -- expect to handshake {"successfully" if expect else "failed"}')
80
81 myurl = f'localhost:{LDAPSPORT}'
82 cmdline = ['/usr/bin/openssl', 's_client', '-connect', myurl]
83
84 strcmdline = " ".join(cmdline)
85 log.info(f"Running cmdline: {strcmdline}")
86
87 with subprocess.Popen(cmdline, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
88 for line in proc.stdout:
89 if line == b"":
90 break
91 if b'Cipher is' in line:
92 log.info(f"Found: {line}")
93 if expect:
94 assert b'(NONE)' not in line
95 proc.stdin.close()
96 else:
97 assert b'(NONE)' in line
98 proc.stdin.close()
99
100
101 @pytest.fixture(scope='function')
102 def setup_cipher_test(request, topo):
103 topo.standalone.enable_tls()
104 topo.standalone.restart()
105 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
106
107 def fin():
108 try:
109 topo.standalone.encryption.set('nsSSL3Ciphers', b'default')
110 topo.standalone.restart()
111 except ValueError:
112 dse = DSEldif(topo.standalone)
113 topo.standalone.stop()
114 dse.replace(ENC_DN, 'nsSSL3Ciphers', b'default')
115 topo.standalone.use_ldap_uri()
116 topo.standalone.start()
117 request.addfinalizer(fin)
118
119
120 def test_cipher_policy_allowWeakCipher(topo, setup_cipher_test):
121 """Verify negotiated TLS handshake succeeds when weak ciphers are explicitly allowed
122
123 :id: dcea9436-b26c-44ed-ac0c-3437612bf15a
124 :setup: Standalone Instance
125 :steps:
126 1. Set nsslapd-errorlog-level to 64 and allowWeakCipher to on, then restart
127 2. Run openssl s_client (negotiated cipher) and expect a successful handshake
128 3. Restore nsslapd-errorlog-level and set allowWeakCipher to off
129 :expectedresults:
130 1. Success
131 2. Negotiated cipher is not (NONE)
132 3. Success
133 """
134 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
135 errloglevel = topo.standalone.config.get_attr_val('nsslapd-errorlog-level')
136 try:
137 topo.standalone.config.set('nsslapd-errorlog-level', '64')
138 topo.standalone.encryption.set('allowWeakCipher', 'on')
139 topo.standalone.restart()
140
141 connectWithOpenssl(topo)
142 finally:
143 topo.standalone.config.set('nsslapd-errorlog-level', errloglevel)
144 topo.standalone.encryption.set('allowWeakCipher', 'off')
145
146
147 def test_cipher_policy_delete_allowWeakCipher(topo, setup_cipher_test):
148 """Verify cipher behavior after removing allowWeakCipher so the default policy applies
149
150 :id: ac8bc03d-5c1c-4c3a-9d34-61b116f0e7cd
151 :setup: Standalone Instance
152 :steps:
153 1. Set nsslapd-errorlog-level to 64 and delete allowWeakCipher, then restart
154 2. Run openssl s_client (negotiated cipher) and expect a successful handshake
155 3. Restore nsslapd-errorlog-level and allowWeakCipher to its prior value
156 :expectedresults:
157 1. Success
158 2. Negotiated cipher is not (NONE)
159 3. Success
160 """
161 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
162 errloglevel = topo.standalone.config.get_attr_val('nsslapd-errorlog-level')
163 allowWeakCipher = topo.standalone.encryption.get_attr_val('allowWeakCipher')
164 try:
165 topo.standalone.config.set('nsslapd-errorlog-level', '64')
166 topo.standalone.encryption.delete('allowWeakCipher')
167 topo.standalone.restart()
168
169 connectWithOpenssl(topo)
170 finally:
171 topo.standalone.config.set('nsslapd-errorlog-level', errloglevel)
172 topo.standalone.encryption.set('allowWeakCipher', allowWeakCipher)
173
174
175 def test_cipher_policy_all_ciphers_disabled(topo, setup_cipher_test):
176 """Verify bind is not possible when TLS is configured but all TLS ciphers are disabled
177
178 :id: e38b4e7d-8dda-4ac5-9013-25762715792b
179 :setup: Standalone Instance
180 :steps:
181 1. Set nsSSL3Ciphers to -all
182 2. Attempt to restart the standalone instance
183 :expectedresults:
184 1. Success
185 2. Restart raises ldap.SERVER_DOWN because no ciphers are available
186 """
187 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
188
189 topo.standalone.encryption.set('nsSSL3Ciphers', b'-all')
190
191 # Server fails to bind and restart because TLS is configured, but no ciphers are enabled
192 with pytest.raises(ldap.SERVER_DOWN):
193 topo.standalone.restart()
194
195
196 def test_cipher_policy_delete_ciphers(topo, setup_cipher_test):
197 """Verify TLS handshake fails after deleting nsSSL3Ciphers from the encryption entry
198
199 :id: aa264a5d-655d-4e15-91cb-e288a9ea9a8a
200 :setup: Standalone Instance
201 :steps:
202 1. Delete nsSSL3Ciphers and restart the server
203 2. Run openssl s_client (negotiated cipher) and expect handshake failure
204 :expectedresults:
205 1. Success
206 2. Handshake fails
207 """
208 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
209
210 topo.standalone.encryption.delete('nsSSL3Ciphers')
211 topo.standalone.restart()
212
213 connectWithOpenssl(topo, False)
214
215
216 def test_cipher_policy_default_ciphers(topo, setup_cipher_test):
217 """Verify TLS handshakes when nsSSL3Ciphers is set to the default cipher suite
218
219 :id: f88fd3f6-4b97-4b3b-9d18-5a98591eaeb2
220 :setup: Standalone Instance
221 :steps:
222 1. Set nsSSL3Ciphers to default and restart the server
223 2. Attempt openssl handshake
224 :expectedresults:
225 1. Success
226 2. Success
227 """
228 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
229
230 topo.standalone.encryption.set('nsSSL3Ciphers', b'default')
231 topo.standalone.restart()
232
233 connectWithOpenssl(topo)
234
235
236 def test_cipher_policy_all_with_excluded_cipher(topo, setup_cipher_test):
237 """Verify TLS handshake when TLS_AES_128_GCM_SHA256 is excluded from an otherwise broad cipher list
238
239 :id: 401a9f4f-e496-4120-958b-c43d8cd62275
240 :setup: Standalone Instance
241 :steps:
242 1. Set nsSSL3Ciphers to +all,-TLS_AES_128_GCM_SHA256 and restart
243 2. Run openssl s_client (negotiated cipher) and expect a successful handshake
244 :expectedresults:
245 1. Success
246 2. Handshake succeeds
247 """
248 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
249
250 topo.standalone.encryption.set('nsSSL3Ciphers', b'+all,-TLS_AES_128_GCM_SHA256')
251 topo.standalone.restart()
252
253 connectWithOpenssl(topo)
254
255
256 def test_cipher_policy_default_ciphers_no_weak(topo, setup_cipher_test):
257 """Verify negotiated TLS handshake with default ciphers and allowWeakCipher off
258
259 :id: f5e87043-4660-4db6-9f83-5c0765076bce
260 :setup: Standalone Instance
261 :steps:
262 1. Set nsSSL3Ciphers to default, set allowWeakCipher to off, and restart
263 2. Run openssl s_client (negotiated cipher) and expect a successful handshake
264 :expectedresults:
265 1. Success
266 2. Handshake succeeds
267 """
268 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
269
270 topo.standalone.encryption.set('nsSSL3Ciphers', b'default')
271 topo.standalone.encryption.set('allowWeakCipher', 'off')
272 topo.standalone.restart()
273
274 connectWithOpenssl(topo)
275
276
277 def test_cipher_policy_clear_ciphers(topo, setup_cipher_test):
278 """Verify negotiated TLS handshake when nsSSL3Ciphers is cleared and weak ciphers are allowed
279
280 :id: c6117e7a-0da7-48e8-91b1-c7764acb192f
281 :setup: Standalone Instance
282 :steps:
283 1. Set nsSSL3Ciphers to None, set allowWeakCipher to on, delete nsslapd-errorlog-level, and restart
284 2. Run openssl s_client (negotiated cipher) and expect a successful handshake
285 3. Set allowWeakCipher to off
286 :expectedresults:
287 1. Success
288 2. Handshake succeeds
289 3. Success
290 """
291
292 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
293
294 try:
295 topo.standalone.encryption.set('nsSSL3Ciphers', None)
296 topo.standalone.encryption.set('allowWeakCipher', b'on')
297 topo.standalone.config.delete('nsslapd-errorlog-level')
298 topo.standalone.restart()
299
300 connectWithOpenssl(topo)
301 finally:
302 topo.standalone.encryption.set('allowWeakCipher', b'off')
303
304
305 def test_cipher_policy_unsupported_cipher(topo, setup_cipher_test):
306 """Verify change_ciphers rejects an unsupported cipher and leaves nsSSL3Ciphers unchanged
307
308 :id: ebb44bd2-b962-4847-958a-db955a09b7c1
309 :setup: Standalone Instance
310 :steps:
311 1. Record the current nsSSL3Ciphers value
312 2. Call change_ciphers to add the unsupported fortezza cipher suite
313 3. Read nsSSL3Ciphers again
314 :expectedresults:
315 1. Success
316 2. change_ciphers returns False
317 3. nsSSL3Ciphers is unchanged from the value recorded in step 1
318 """
319 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
320 default_cipher = topo.standalone.encryption.get_attr_val('nsSSL3Ciphers')
321
322 # change_ciphers should return False because 'fortezza' cipher is not supported
323 assert not topo.standalone.encryption.change_ciphers('+', ['fortezza']), "Unsupported cipher 'fortezza' was not rejected"
324
325 # Verify that nsSSL3Ciphers is still set to'default'
326 assert topo.standalone.encryption.get_attr_val('nsSSL3Ciphers') == default_cipher, "nsSSL3Ciphers attribute should not be modified"
327
328
329 if __name__ == '__main__':
330 # Run isolated
331 # -s for DEBUG mode
332 CURRENT_FILE = os.path.realpath(__file__)
333 pytest.main(["-s", CURRENT_FILE])