summaryrefslogtreecommitdiff
path: root/python/samba/tests/password_hash_fl2003.py
blob: ab99b3b1823e01d49ba25aab7892a1fbde744617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Tests for Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
#
# Copyright (C) Catalyst IT Ltd. 2017
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Tests for source4/dsdb/samdb/ldb_modules/password_hash.c

These tests need to be run in an environment in which
io->ac->gpg_key_ids == NULL, so that the gpg supplemental credentials
are not generated. And also need to be in an environment with a
functional level less than 2008 to ensure the kerberos newer keys are not
generated
"""

from samba.tests.password_hash import (
    PassWordHashTests,
    get_package,
    USER_PASS
)
from samba.ndr import ndr_unpack
from samba.dcerpc import drsblobs
import binascii


class PassWordHashFl2003Tests(PassWordHashTests):

    def setUp(self):
        super(PassWordHashFl2003Tests, self).setUp()

    def test_default_supplementalCredentials(self):
        self.add_user(options=[("password hash userPassword schemes", "")])

        sc = self.get_supplemental_creds()

        # Check that we got all the expected supplemental credentials
        # And they are in the expected order.
        size = len(sc.sub.packages)
        self.assertEquals(3, size)

        (pos, package) = get_package(sc, "Primary:Kerberos")
        self.assertEquals(1, pos)
        self.assertEquals("Primary:Kerberos", package.name)

        (pos, package) = get_package(sc, "Packages")
        self.assertEquals(2, pos)
        self.assertEquals("Packages", package.name)

        (pos, package) = get_package(sc, "Primary:WDigest")
        self.assertEquals(3, pos)
        self.assertEquals("Primary:WDigest", package.name)

        # Check that the WDigest values are correct.
        #
        digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
                             binascii.a2b_hex(package.data))
        self.check_wdigests(digests)

    def test_userPassword_sha256(self):
        self.add_user(options=[("password hash userPassword schemes",
                                "CryptSHA256")])

        sc = self.get_supplemental_creds()

        # Check that we got all the expected supplemental credentials
        # And they are in the expected order.
        size = len(sc.sub.packages)
        self.assertEquals(4, size)

        (pos, package) = get_package(sc, "Primary:Kerberos")
        self.assertEquals(1, pos)
        self.assertEquals("Primary:Kerberos", package.name)

        (pos, wd_package) = get_package(sc, "Primary:WDigest")
        self.assertEquals(2, pos)
        self.assertEquals("Primary:WDigest", wd_package.name)

        (pos, package) = get_package(sc, "Packages")
        self.assertEquals(3, pos)
        self.assertEquals("Packages", package.name)

        (pos, up_package) = get_package(sc, "Primary:userPassword")
        self.assertEquals(4, pos)
        self.assertEquals("Primary:userPassword", up_package.name)

        # Check that the WDigest values are correct.
        #
        digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
                             binascii.a2b_hex(wd_package.data))
        self.check_wdigests(digests)

        # Check that the userPassword hashes are computed correctly
        #
        up = ndr_unpack(drsblobs.package_PrimaryUserPasswordBlob,
                        binascii.a2b_hex(up_package.data))

        self.checkUserPassword(up, [("{CRYPT}", "5", None)])
        self.checkNtHash(USER_PASS, up.current_nt_hash.hash)

    def test_supplementalCredentials_cleartext(self):
        self.add_user(clear_text=True,
                      options=[("password hash userPassword schemes", "")])

        sc = self.get_supplemental_creds()

        # Check that we got all the expected supplemental credentials
        # And they are in the expected order.
        size = len(sc.sub.packages)
        self.assertEquals(4, size)

        (pos, package) = get_package(sc, "Primary:Kerberos")
        self.assertEquals(1, pos)
        self.assertEquals("Primary:Kerberos", package.name)

        (pos, wd_package) = get_package(sc, "Primary:WDigest")
        self.assertEquals(2, pos)
        self.assertEquals("Primary:WDigest", wd_package.name)

        (pos, package) = get_package(sc, "Packages")
        self.assertEquals(3, pos)
        self.assertEquals("Packages", package.name)

        (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
        self.assertEquals(4, pos)
        self.assertEquals("Primary:CLEARTEXT", ct_package.name)

        # Check that the WDigest values are correct.
        #
        digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
                             binascii.a2b_hex(wd_package.data))
        self.check_wdigests(digests)

        # Check the clear text  value is correct.
        ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
                        binascii.a2b_hex(ct_package.data))
        self.assertEquals(USER_PASS.encode('utf-16-le'), ct.cleartext)

    def test_userPassword_cleartext_sha512(self):
        self.add_user(clear_text=True,
                      options=[("password hash userPassword schemes",
                                "CryptSHA512:rounds=10000")])

        sc = self.get_supplemental_creds()

        # Check that we got all the expected supplemental credentials
        # And they are in the expected order.
        size = len(sc.sub.packages)
        self.assertEquals(5, size)

        (pos, package) = get_package(sc, "Primary:Kerberos")
        self.assertEquals(1, pos)
        self.assertEquals("Primary:Kerberos", package.name)

        (pos, wd_package) = get_package(sc, "Primary:WDigest")
        self.assertEquals(2, pos)
        self.assertEquals("Primary:WDigest", wd_package.name)

        (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
        self.assertEquals(3, pos)
        self.assertEquals("Primary:CLEARTEXT", ct_package.name)

        (pos, package) = get_package(sc, "Packages")
        self.assertEquals(4, pos)
        self.assertEquals("Packages", package.name)

        (pos, up_package) = get_package(sc, "Primary:userPassword")
        self.assertEquals(5, pos)
        self.assertEquals("Primary:userPassword", up_package.name)

        # Check that the WDigest values are correct.
        #
        digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
                             binascii.a2b_hex(wd_package.data))
        self.check_wdigests(digests)

        # Check the clear text  value is correct.
        ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
                        binascii.a2b_hex(ct_package.data))
        self.assertEquals(USER_PASS.encode('utf-16-le'), ct.cleartext)

        # Check that the userPassword hashes are computed correctly
        #
        up = ndr_unpack(drsblobs.package_PrimaryUserPasswordBlob,
                        binascii.a2b_hex(up_package.data))
        self.checkUserPassword(up, [("{CRYPT}", "6", 10000)])
        self.checkNtHash(USER_PASS, up.current_nt_hash.hash)