1 # Tests for Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
3 # Copyright (C) Catalyst IT Ltd. 2017
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
22 These tests need to be run in an environment in which
23 io->ac->gpg_key_ids == NULL, so that the gpg supplemental credentials
24 are not generated. And also need to be in an environment with a
25 functional level less than 2008 to ensure the kerberos newer keys are not
29 from samba.tests.password_hash import (
34 from samba.ndr import ndr_unpack
35 from samba.dcerpc import drsblobs
39 class PassWordHashFl2003Tests(PassWordHashTests):
42 super(PassWordHashFl2003Tests, self).setUp()
44 def test_default_supplementalCredentials(self):
45 self.add_user(options=[("password hash userPassword schemes", "")])
47 sc = self.get_supplemental_creds()
49 # Check that we got all the expected supplemental credentials
50 # And they are in the expected order.
51 size = len(sc.sub.packages)
52 self.assertEquals(3, size)
54 (pos, package) = get_package(sc, "Primary:Kerberos")
55 self.assertEquals(1, pos)
56 self.assertEquals("Primary:Kerberos", package.name)
58 (pos, package) = get_package(sc, "Packages")
59 self.assertEquals(2, pos)
60 self.assertEquals("Packages", package.name)
62 # Check that the Package names are correct.
64 pb = ndr_unpack(drsblobs.package_PackagesBlob,
65 binascii.a2b_hex(package.data))
66 expected = ["Kerberos", "WDigest"]
67 self.assertEquals(expected, pb.names)
69 (pos, package) = get_package(sc, "Primary:WDigest")
70 self.assertEquals(3, pos)
71 self.assertEquals("Primary:WDigest", package.name)
73 # Check that the WDigest values are correct.
75 digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
76 binascii.a2b_hex(package.data))
77 self.check_wdigests(digests)
80 def test_userPassword_sha256(self):
81 self.add_user(options=[("password hash userPassword schemes",
84 sc = self.get_supplemental_creds()
86 # Check that we got all the expected supplemental credentials
87 # And they are in the expected order.
88 size = len(sc.sub.packages)
89 self.assertEquals(4, size)
91 (pos, package) = get_package(sc, "Primary:Kerberos")
92 self.assertEquals(1, pos)
93 self.assertEquals("Primary:Kerberos", package.name)
95 (pos, wd_package) = get_package(sc, "Primary:WDigest")
96 self.assertEquals(2, pos)
97 self.assertEquals("Primary:WDigest", wd_package.name)
99 (pos, package) = get_package(sc, "Packages")
100 self.assertEquals(3, pos)
101 self.assertEquals("Packages", package.name)
103 (pos, up_package) = get_package(sc, "Primary:userPassword")
104 self.assertEquals(4, pos)
105 self.assertEquals("Primary:userPassword", up_package.name)
107 # Check that the WDigest values are correct.
109 digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
110 binascii.a2b_hex(wd_package.data))
111 self.check_wdigests(digests)
113 # Check that the userPassword hashes are computed correctly
115 up = ndr_unpack(drsblobs.package_PrimaryUserPasswordBlob,
116 binascii.a2b_hex(up_package.data))
118 self.checkUserPassword(up, [("{CRYPT}", "5", None)])
119 self.checkNtHash(USER_PASS, up.current_nt_hash.hash)
121 def test_supplementalCredentials_cleartext(self):
122 self.add_user(clear_text=True,
123 options=[("password hash userPassword schemes", "")])
125 sc = self.get_supplemental_creds()
127 # Check that we got all the expected supplemental credentials
128 # And they are in the expected order.
129 size = len(sc.sub.packages)
130 self.assertEquals(4, size)
132 (pos, package) = get_package(sc, "Primary:Kerberos")
133 self.assertEquals(1, pos)
134 self.assertEquals("Primary:Kerberos", package.name)
136 (pos, wd_package) = get_package(sc, "Primary:WDigest")
137 self.assertEquals(2, pos)
138 self.assertEquals("Primary:WDigest", wd_package.name)
140 (pos, package) = get_package(sc, "Packages")
141 self.assertEquals(3, pos)
142 self.assertEquals("Packages", package.name)
144 (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
145 self.assertEquals(4, pos)
146 self.assertEquals("Primary:CLEARTEXT", ct_package.name)
148 # Check that the WDigest values are correct.
150 digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
151 binascii.a2b_hex(wd_package.data))
152 self.check_wdigests(digests)
154 # Check the clear text value is correct.
155 ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
156 binascii.a2b_hex(ct_package.data))
157 self.assertEquals(USER_PASS.encode('utf-16-le'), ct.cleartext)
159 def test_userPassword_cleartext_sha512(self):
160 self.add_user(clear_text=True,
161 options=[("password hash userPassword schemes",
162 "CryptSHA512:rounds=10000")])
164 sc = self.get_supplemental_creds()
166 # Check that we got all the expected supplemental credentials
167 # And they are in the expected order.
168 size = len(sc.sub.packages)
169 self.assertEquals(5, size)
171 (pos, package) = get_package(sc, "Primary:Kerberos")
172 self.assertEquals(1, pos)
173 self.assertEquals("Primary:Kerberos", package.name)
175 (pos, wd_package) = get_package(sc, "Primary:WDigest")
176 self.assertEquals(2, pos)
177 self.assertEquals("Primary:WDigest", wd_package.name)
179 (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
180 self.assertEquals(3, pos)
181 self.assertEquals("Primary:CLEARTEXT", ct_package.name)
183 (pos, package) = get_package(sc, "Packages")
184 self.assertEquals(4, pos)
185 self.assertEquals("Packages", package.name)
187 (pos, up_package) = get_package(sc, "Primary:userPassword")
188 self.assertEquals(5, pos)
189 self.assertEquals("Primary:userPassword", up_package.name)
191 # Check that the WDigest values are correct.
193 digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
194 binascii.a2b_hex(wd_package.data))
195 self.check_wdigests(digests)
197 # Check the clear text value is correct.
198 ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
199 binascii.a2b_hex(ct_package.data))
200 self.assertEquals(USER_PASS.encode('utf-16-le'), ct.cleartext)
202 # Check that the userPassword hashes are computed correctly
204 up = ndr_unpack(drsblobs.package_PrimaryUserPasswordBlob,
205 binascii.a2b_hex(up_package.data))
206 self.checkUserPassword(up, [("{CRYPT}", "6", 10000)])
207 self.checkNtHash(USER_PASS, up.current_nt_hash.hash)