Debian Codegraph @

dirsrvtests/tests/suites/password/pwd_algo_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2019 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 from lib389.tasks import *
11 from lib389.utils import *
12 from test389.topologies import topology_st
13 from lib389._constants import DEFAULT_SUFFIX, HOST_STANDALONE, PORT_STANDALONE
14 from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
15 from lib389.paths import Paths
16
17 default_paths = Paths()
18
19 pytestmark = pytest.mark.tier1
20
21 DEBUGGING = os.getenv('DEBUGGING', False)
22 USER_DN = 'uid=user,ou=People,%s' % DEFAULT_SUFFIX
23
24 logging.getLogger(__name__).setLevel(logging.INFO)
25 log = logging.getLogger(__name__)
26
27
28 def _test_bind(user, password):
29 result = True
30 try:
31 userconn = user.bind(password)
32 userconn.unbind_s()
33 except ldap.INVALID_CREDENTIALS:
34 result = False
35 return result
36
37
38 def _test_algo(inst, algo_name):
39 inst.config.set('passwordStorageScheme', algo_name)
40
41 users = UserAccounts(inst, DEFAULT_SUFFIX)
42 user_props = TEST_USER_PROPERTIES.copy()
43 user_props.update({'uid': 'user', 'cn': 'buser', 'userpassword': 'Secret123'})
44 user = users.create(properties=user_props)
45
46 # Make sure when we read the userPassword field, it is the correct ALGO
47 pw_field = user.get_attr_val_utf8('userPassword')
48
49 if algo_name != 'CLEAR' and algo_name != 'DEFAULT':
50 assert (algo_name[:5].lower() in pw_field.lower())
51 # Now make sure a bind works
52 assert (_test_bind(user, 'Secret123'))
53 # Bind with a wrong shorter password, should fail
54 assert (not _test_bind(user, 'Wrong'))
55 # Bind with a wrong longer password, should fail
56 assert (not _test_bind(user, 'This is even more wrong'))
57 # Bind with a wrong exact length password.
58 assert (not _test_bind(user, 'Alsowrong'))
59 # Bind with a subset password, should fail
60 assert (not _test_bind(user, 'Secret'))
61 if not algo_name.startswith('CRYPT'):
62 # Bind with a subset password that is 1 char shorter, to detect off by 1 in clear
63 assert (not _test_bind(user, 'Secret12'))
64 # Bind with a superset password, should fail
65 assert (not _test_bind(user, 'Secret123456'))
66
67 # Delete the user
68 user.delete()
69
70
71 def _test_bind_for_pbkdf2_algo(inst, password):
72 result = True
73 userconn = ldap.initialize("ldap://%s:%s" % (HOST_STANDALONE, PORT_STANDALONE))
74 try:
75 userconn.simple_bind_s(USER_DN, password)
76 userconn.unbind_s()
77 except ldap.INVALID_CREDENTIALS:
78 result = False
79 return result
80
81
82 def _test_algo_for_pbkdf2(inst, algo_name):
83 inst.config.set('passwordStorageScheme', algo_name)
84
85 if DEBUGGING:
86 print('Testing %s' % algo_name)
87
88 # Create the user with a password
89 users = UserAccounts(inst, DEFAULT_SUFFIX)
90 user_props = TEST_USER_PROPERTIES.copy()
91 user_props.update({'uid': 'user', 'cn': 'buser', 'userpassword': 'Secret123'})
92 user = users.create(properties=user_props)
93
94 # Make sure when we read the userPassword field, it is the correct ALGO
95 pw_field = user.get_attr_val_utf8_l('userPassword')
96
97 if DEBUGGING:
98 print(pw_field)
99
100 if algo_name != 'CLEAR':
101 lalgo_name = algo_name.lower()
102 assert (pw_field.startswith('{' + lalgo_name + '}'))
103
104 # Now make sure a bind works
105 assert (_test_bind_for_pbkdf2_algo(inst, 'Secret123'))
106 # Bind with a wrong shorter password, should fail
107 assert (not _test_bind_for_pbkdf2_algo(inst, 'Wrong'))
108 # Bind with a wrong longer password, should fail
109 assert (not _test_bind_for_pbkdf2_algo(inst, 'This is even more wrong'))
110 # Bind with a password that has the algo in the name
111 assert (not _test_bind_for_pbkdf2_algo(inst, '{%s}SomeValues....' % algo_name))
112 # Bind with a wrong exact length password.
113 assert (not _test_bind_for_pbkdf2_algo(inst, 'Alsowrong'))
114 # Bind with a subset password, should fail
115 assert (not _test_bind_for_pbkdf2_algo(inst, 'Secret'))
116 if algo_name != 'CRYPT':
117 # Bind with a subset password that is 1 char shorter, to detect off by 1 in clear
118 assert (not _test_bind_for_pbkdf2_algo(inst, 'Secret12'))
119 # Bind with a superset password, should fail
120 assert (not _test_bind_for_pbkdf2_algo(inst, 'Secret123456'))
121
122 # Delete the user
123 inst.delete_s(USER_DN)
124
125
126 ALGO_SET = ('CLEAR', 'CRYPT', 'CRYPT-MD5', 'CRYPT-SHA256', 'CRYPT-SHA512',
127 'MD5', 'SHA', 'SHA256', 'SHA384', 'SHA512', 'SMD5', 'SSHA',
128 'SSHA256', 'SSHA384', 'SSHA512', 'PBKDF2_SHA256', 'DEFAULT',
129 'GOST_YESCRYPT',
130 )
131
132 if default_paths.rust_enabled and ds_is_newer('1.4.3.0'):
133 ALGO_SET = ('CLEAR', 'CRYPT', 'CRYPT-MD5', 'CRYPT-SHA256', 'CRYPT-SHA512',
134 'MD5', 'SHA', 'SHA256', 'SHA384', 'SHA512', 'SMD5', 'SSHA',
135 'SSHA256', 'SSHA384', 'SSHA512', 'PBKDF2_SHA256', 'DEFAULT',
136 'PBKDF2-SHA1', 'PBKDF2-SHA256', 'PBKDF2-SHA512', 'GOST_YESCRYPT',
137 )
138
139 @pytest.mark.parametrize("algo", ALGO_SET)
140 def test_pwd_algo_test(topology_st, algo):
141 """Assert that all of our password algorithms correctly PASS and FAIL varying
142 password conditions.
143
144 :id: fbb308a8-8374-4abd-b786-1f88e56f7650
145 :parametrized: yes
146 """
147 if algo == 'DEFAULT':
148 if ds_is_older('1.4.0'):
149 pytest.skip("Not implemented")
150 _test_algo(topology_st.standalone, algo)
151 log.info('Test %s PASSED' % algo)
152
153
154 def test_pbkdf2_algo(topology_st):
155 """Changing password storage scheme to PBKDF2_SHA256
156 and trying to bind with different password combination
157
158 :id: 112e265b-f468-4758-b8fa-ed8742de0182
159 :setup: Standalone instance
160 :steps:
161 1. Change password storage scheme to PBKDF2_SHA256
162 2. Add a test user entry
163 3. Bind with correct password
164 4. Bind with incorrect password combination(brute-force)
165 :expectedresults:
166 1. Operation should be successful
167 2. Operation should be successful
168 3. Bind should be successful
169 4. Should not allow to bind with incorrect password
170 """
171 if DEBUGGING:
172 # Add debugging steps(if any)...
173 log.info("ATTACH NOW")
174 time.sleep(30)
175
176 # Merge this to the password suite in the future
177
178 for algo in ('PBKDF2_SHA256',):
179 for i in range(0, 10):
180 _test_algo_for_pbkdf2(topology_st.standalone, algo)
181
182 log.info('Test PASSED')
183
184
185 if __name__ == '__main__':
186 # Run isolated
187 # -s for DEBUG mode
188 CURRENT_FILE = os.path.realpath(__file__)
189 pytest.main("-s %s" % CURRENT_FILE)