Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / tests / t_mkey.py
1 #!/usr/bin/python
2 from k5test import *
3 import random
4 import re
5 import struct
6
7 # Convenience constants for use as expected enctypes.  defetype is the
8 # default enctype for master keys.
9 aes256 = 'aes256-cts-hmac-sha1-96'
10 aes128 = 'aes128-cts-hmac-sha1-96'
11 des3 = 'des3-cbc-sha1'
12 defetype = aes256
13
14 realm = K5Realm(create_host=False, start_kadmind=True)
15 realm.prep_kadmin()
16 stash_file = os.path.join(realm.testdir, 'stash')
17
18 # Count the number of principals in the realm.
19 nprincs = len(realm.run([kadminl, 'listprincs']).splitlines())
20
21 # List the currently active mkeys and compare against expected
22 # results.  Each argument must be a sequence of four elements: an
23 # expected kvno, an expected enctype, whether the key is expected to
24 # have an activation time, and whether the key is expected to be
25 # currently active.
26 list_mkeys_re = re.compile(r'^KVNO: (\d+), Enctype: (\S+), '
27                            '(Active on: [^\*]+|No activate time set)( \*)?$')
28 def check_mkey_list(*expected):
29     # Split the output of kdb5_util list_mkeys into lines and ignore the first.
30     outlines = realm.run([kdb5_util, 'list_mkeys']).splitlines()[1:]
31     if len(outlines) != len(expected):
32         fail('Unexpected number of list_mkeys output lines')
33     for line, ex in zip(outlines, expected):
34         m = list_mkeys_re.match(line)
35         if not m:
36             fail('Unrecognized list_mkeys output line')
37         kvno, enctype, act_time, active = m.groups()
38         exp_kvno, exp_enctype, exp_act_time_present, exp_active = ex
39         if kvno != str(exp_kvno):
40             fail('Unexpected master key version')
41         if enctype != exp_enctype:
42             fail('Unexpected master key enctype')
43         if act_time.startswith('Active on: ') != exp_act_time_present:
44             fail('Unexpected presence or absence of mkey activation time')
45         if (active == ' *') != exp_active:
46             fail('Master key unexpectedly active or inactive')
47
48
49 # Get the K/M principal.  Verify that it has the expected mkvno.  Each
50 # remaining argment must be a sequence of two elements: an expected
51 # key version and an expected enctype.
52 keyline_re = re.compile(r'^Key: vno (\d+), (\S+)$')
53 def check_master_dbent(expected_mkvno, *expected_keys):
54     outlines = realm.run([kadminl, 'getprinc', 'K/M']).splitlines()
55     mkeyline = [l for l in outlines if l.startswith('MKey: vno ')]
56     if len(mkeyline) != 1 or mkeyline[0] != ('MKey: vno %d' % expected_mkvno):
57         fail('Unexpected mkvno in K/M DB entry')
58     keylines = [l for l in outlines if l.startswith('Key: vno ')]
59     if len(keylines) != len(expected_keys):
60         fail('Unexpected number of key lines in K/M DB entry')
61     for line, ex in zip(keylines, expected_keys):
62         m = keyline_re.match(line)
63         if not m:
64             fail('Unrecognized key line in K/M DB entry')
65         kvno, enctype = m.groups()
66         exp_kvno, exp_enctype = ex
67         if kvno != str(exp_kvno):
68             fail('Unexpected key version in K/M DB entry')
69         if enctype != exp_enctype:
70             fail('Unexpected enctype in K/M DB entry')
71
72
73 # Check the stash file.  Each argument must be a sequence of two
74 # elements: an expected key version and an expected enctype.
75 klist_re = re.compile(r'^\s*(\d+) K/M@KRBTEST.COM \((\S+)\)')
76 def check_stash(*expected):
77     # Split the output of klist -e -k into lines and ignore the first three.
78     outlines = realm.run([klist, '-e', '-k', stash_file]).splitlines()[3:]
79     if len(outlines) != len(expected):
80         fail('Unexpected number of lines in stash file klist')
81     for line, ex in zip(outlines, expected):
82         m = klist_re.match(line)
83         if not m:
84             fail('Unrecognized stash file klist line')
85         kvno, enctype = m.groups()
86         exp_kvno, exp_enctype = ex
87         if kvno != str(exp_kvno):
88             fail('Unexpected stash file klist kvno')
89         if enctype != exp_enctype:
90             fail('Unexpected stash file klist enctype')
91
92
93 # Verify that the user principal has the expected mkvno.
94 def check_mkvno(princ, expected_mkvno):
95     out = realm.run([kadminl, 'getprinc', princ])
96     if ('MKey: vno %d\n' % expected_mkvno) not in out:
97         fail('Unexpected mkvno in user DB entry')
98
99
100 # Change the password using either kadmin.local or kadmin, then check
101 # the mkvno of the principal against expected_mkvno and verify that
102 # the running KDC can access the new key.
103 def change_password_check_mkvno(local, princ, password, expected_mkvno):
104     cmd = ['cpw', '-pw', password, princ]
105     if local:
106         realm.run([kadminl] + cmd)
107     else:
108         realm.run_kadmin(cmd)
109     check_mkvno(princ, expected_mkvno)
110     realm.kinit(princ, password)
111
112
113 # Add a master key with the specified options and a random password.
114 def add_mkey(options):
115     pw = ''.join(random.choice(string.ascii_uppercase) for x in range(5))
116     realm.run([kdb5_util, 'add_mkey'] + options, input=(pw + '\n' + pw + '\n'))
117
118
119 # Run kdb5_util update_princ_encryption (with the dry-run option if
120 # specified) and verify the output against the expected mkvno, number
121 # of updated principals, and number of already-current principals.
122 mkvno_re = {False: re.compile(r'^Principals whose keys are being re-encrypted '
123                               'to master key vno (\d+) if necessary:$'),
124             True: re.compile(r'^Principals whose keys WOULD BE re-encrypted '
125                              'to master key vno (\d+):$')}
126 count_re = {False: re.compile(r'^(\d+) principals processed: (\d+) updated, '
127                               '(\d+) already current$'),
128             True: re.compile(r'^(\d+) principals processed: (\d+) would be '
129                              'updated, (\d+) already current$')}
130 def update_princ_encryption(dry_run, expected_mkvno, expected_updated,
131                             expected_current):
132     opts = ['-f', '-v']
133     if dry_run:
134         opts += ['-n']
135     out = realm.run([kdb5_util, 'update_princ_encryption'] + opts)
136     lines = out.splitlines()
137     # Parse the first line to get the target mkvno.
138     m = mkvno_re[dry_run].match(lines[0])
139     if not m:
140         fail('Unexpected first line of update_princ_encryption output')
141     if m.group(1) != str(expected_mkvno):
142         fail('Unexpected master key version in update_princ_encryption output')
143     # Parse the last line to get the principal counts.
144     m = count_re[dry_run].match(lines[-1])
145     if not m:
146         fail('Unexpected last line of update_princ_encryption output')
147     total, updated, current = m.groups()
148     if (total != str(expected_updated + expected_current) or
149         updated != str(expected_updated) or current != str(expected_current)):
150         fail('Unexpected counts from update_princ_encryption')
151
152
153 # Check the initial state of the realm.
154 check_mkey_list((1, defetype, True, True))
155 check_master_dbent(1, (1, defetype))
156 check_stash((1, defetype))
157 check_mkvno(realm.user_princ, 1)
158
159 # Check that stash will fail if a temp stash file is already present.
160 collisionfile = os.path.join(realm.testdir, 'stash_tmp')
161 f = open(collisionfile, 'w')
162 f.close()
163 output = realm.run([kdb5_util, 'stash'], expected_code=1)
164 if 'Temporary stash file already exists' not in output:
165     fail('Did not detect temp stash file collision')
166 os.unlink(collisionfile)
167
168 # Add a new master key with no options.  Verify that:
169 # 1. The new key appears in list_mkeys but has no activation time and
170 #    is not active.
171 # 2. The new key appears in the K/M DB entry and is the current key to
172 #    encrypt that entry.
173 # 3. The stash file is not modified (since we did not pass -s).
174 # 4. The old key is used for password changes.
175 add_mkey([])
176 check_mkey_list((2, defetype, False, False), (1, defetype, True, True))
177 check_master_dbent(2, (2, defetype), (1, defetype))
178 change_password_check_mkvno(True, realm.user_princ, 'abcd', 1)
179 change_password_check_mkvno(False, realm.user_princ, 'user', 1)
180
181 # Verify that use_mkey won't make all master keys inactive.
182 out = realm.run([kdb5_util, 'use_mkey', '1', 'now+1day'], expected_code=1)
183 if 'there must be one master key currently active' not in out:
184     fail('Unexpected error from use_mkey making all mkeys inactive')
185 check_mkey_list((2, defetype, False, False), (1, defetype, True, True))
186
187 # Make the new master key active.  Verify that:
188 # 1. The new key has an activation time in list_mkeys and is active.
189 # 2. The new key is used for password changes.
190 # 3. The running KDC can access the new key.
191 realm.run([kdb5_util, 'use_mkey', '2', 'now-1day'])
192 check_mkey_list((2, defetype, True, True), (1, defetype, True, False))
193 change_password_check_mkvno(True, realm.user_princ, 'abcd', 2)
194 change_password_check_mkvno(False, realm.user_princ, 'user', 2)
195
196 # Check purge_mkeys behavior with both master keys still in use.
197 out = realm.run([kdb5_util, 'purge_mkeys', '-f', '-v'])
198 if 'All keys in use, nothing purged.' not in out:
199     fail('Unexpected output from purge_mkeys with both mkeys in use')
200
201 # Do an update_princ_encryption dry run and for real.  Verify that:
202 # 1. The target master key is 2 (the active mkvno).
203 # 2. nprincs - 2 principals were updated and one principal was
204 #    skipped (K/M is not included in the output and user was updated
205 #    above).
206 # 3. The dry run doesn't change user/admin's mkvno but the real update
207 #    does.
208 # 4. The old stashed master key is sufficient to access the DB (via
209 #    MKEY_AUX tl-data which keeps the current master key encrypted in
210 #    each of the old master keys).
211 update_princ_encryption(True, 2, nprincs - 2, 1)
212 check_mkvno(realm.admin_princ, 1)
213 update_princ_encryption(False, 2, nprincs - 2, 1)
214 check_mkvno(realm.admin_princ, 2)
215 realm.stop_kdc()
216 realm.start_kdc()
217 realm.kinit(realm.user_princ, 'user')
218
219 # Update all principals back to mkvno 1 and to mkvno 2 again, to
220 # verify that update_princ_encryption targets the active master key.
221 realm.run([kdb5_util, 'use_mkey', '2', 'now+1day'])
222 update_princ_encryption(False, 1, nprincs - 1, 0)
223 check_mkvno(realm.user_princ, 1)
224 realm.run([kdb5_util, 'use_mkey', '2', 'now-1day'])
225 update_princ_encryption(False, 2, nprincs - 1, 0)
226 check_mkvno(realm.user_princ, 2)
227
228 # Test the safety check for purging with an outdated stash file.
229 out = realm.run([kdb5_util, 'purge_mkeys', '-f'], expected_code=1)
230 if 'stash file needs updating' not in out:
231     fail('Unexpected error from purge_mkeys safety check')
232
233 # Update the master stash file and check it.  Save a copy of the old
234 # one for a later test.
235 shutil.copy(stash_file, stash_file + '.old')
236 realm.run([kdb5_util, 'stash'])
237 check_stash((2, defetype), (1, defetype))
238
239 # Do a purge_mkeys dry run and for real.  Verify that:
240 # 1. Master key 1 is purged.
241 # 2. The dry run doesn't remove mkvno 1 but the real one does.
242 # 3. The old stash file is no longer sufficient to access the DB.
243 # 4. If the stash file is updated, it no longer contains mkvno 1.
244 # 5. use_mkey now gives an error if we refer to mkvno 1.
245 # 6. A second purge_mkeys gives the right message.
246 out = realm.run([kdb5_util, 'purge_mkeys', '-v', '-n', '-f'])
247 if 'KVNO: 1' not in out or '1 key(s) would be purged' not in out:
248     fail('Unexpected output from purge_mkeys dry-run')
249 check_mkey_list((2, defetype, True, True), (1, defetype, True, False))
250 check_master_dbent(2, (2, defetype), (1, defetype))
251 out = realm.run([kdb5_util, 'purge_mkeys', '-v', '-f'])
252 check_mkey_list((2, defetype, True, True))
253 check_master_dbent(2, (2, defetype))
254 os.rename(stash_file, stash_file + '.save')
255 os.rename(stash_file + '.old', stash_file)
256 out = realm.run([kadminl, 'getprinc', 'user'], expected_code=1)
257 if 'Unable to decrypt latest master key' not in out:
258     fail('Unexpected error from kadmin.local with old stash file')
259 os.rename(stash_file + '.save', stash_file)
260 realm.run([kdb5_util, 'stash'])
261 check_stash((2, defetype))
262 out = realm.run([kdb5_util, 'use_mkey', '1'], expected_code=1)
263 if '1 is an invalid KVNO value' not in out:
264     fail('Unexpected error from use_mkey with invalid kvno')
265 out = realm.run([kdb5_util, 'purge_mkeys', '-f', '-v'])
266 if 'There is only one master key which can not be purged.' not in out:
267     fail('Unexpected output from purge_mkeys with one mkey')
268
269 # Add a third master key with a specified enctype.  Verify that:
270 # 1. The new master key receives the correct number.
271 # 2. The enctype argument is respected.
272 # 3. The new master key is stashed (by itself, at the moment).
273 # 4. We can roll over to the new master key and use it.
274 add_mkey(['-s', '-e', aes128])
275 check_mkey_list((3, aes128, False, False), (2, defetype, True, True))
276 check_master_dbent(3, (3, aes128), (2, defetype))
277 check_stash((3, aes128))
278 realm.run([kdb5_util, 'use_mkey', '3', 'now-1day'])
279 update_princ_encryption(False, 3, nprincs - 1, 0)
280 check_mkey_list((3, aes128, True, True), (2, defetype, True, False))
281 check_mkvno(realm.user_princ, 3)
282
283 # Regression test for #7994 (randkey does not update principal mkvno)
284 # and #7995 (-keepold does not re-encrypt old keys).
285 add_mkey(['-s'])
286 realm.run([kdb5_util, 'use_mkey', '4', 'now-1day'])
287 realm.run([kadminl, 'cpw', '-randkey', '-keepold', realm.user_princ])
288 # With #7994 unfixed, mkvno of user will still be 3.
289 check_mkvno(realm.user_princ, 4)
290 # With #7995 unfixed, old keys are still encrypted with mkvno 3.
291 update_princ_encryption(False, 4, nprincs - 2, 1)
292 realm.run([kdb5_util, 'purge_mkeys', '-f'])
293 out = realm.run([kadminl, 'xst', '-norandkey', realm.user_princ])
294 if 'Decrypt integrity check failed' in out or 'added to keytab' not in out:
295     fail('Preserved old key data not updated to new master key')
296
297 realm.stop()
298
299 # Load a dump file created with krb5 1.6, before the master key
300 # rollover changes were introduced.  Write out an old-format stash
301 # file consistent with the dump's master password ("footes").  The K/M
302 # entry in this database will not have actkvno tl-data because it was
303 # created prior to master key rollover support.  Verify that:
304 # 1. We can access the database using the old-format stash file.
305 # 2. list_mkeys displays the same list as for a post-1.7 KDB.
306 dumpfile = os.path.join(srctop, 'tests', 'dumpfiles', 'dump.16')
307 os.remove(stash_file)
308 f = open(stash_file, 'w')
309 f.write(struct.pack('=HL24s', 16, 24,
310                     '\xF8\x3E\xFB\xBA\x6D\x80\xD9\x54\xE5\x5D\xF2\xE0'
311                     '\x94\xAD\x6D\x86\xB5\x16\x37\xEC\x7C\x8A\xBC\x86'))
312 f.close()
313 realm.run([kdb5_util, 'load', dumpfile])
314 nprincs = len(realm.run([kadminl, 'listprincs']).splitlines())
315 check_mkvno('K/M', 1)
316 check_mkey_list((1, des3, True, True))
317
318 # Create a new master key and verify that, without actkvkno tl-data:
319 # 1. list_mkeys displays the same as for a post-1.7 KDB.
320 # 2. update_princ_encryption still targets mkvno 1.
321 # 3. libkadm5 still uses mkvno 1 for key changes.
322 # 4. use_mkey creates the same list as for a post-1.7 KDB.
323 add_mkey([])
324 check_mkey_list((2, defetype, False, False), (1, des3, True, True))
325 update_princ_encryption(False, 1, 0, nprincs - 1)
326 realm.run([kadminl, 'addprinc', '-randkey', realm.user_princ])
327 check_mkvno(realm.user_princ, 1)
328 realm.run([kdb5_util, 'use_mkey', '2', 'now-1day'])
329 check_mkey_list((2, defetype, True, True), (1, des3, True, False))
330
331 # Regression test for #8395.  Purge the master key and verify that a
332 # master key fetch does not segfault.
333 realm.run([kadminl, 'purgekeys', '-all', 'K/M'])
334 out = realm.run([kadminl, 'getprinc', realm.user_princ], expected_code=1)
335 if 'Cannot find master key record in database' not in out:
336     fail('Unexpected output from failed master key fetch')
337
338 success('Master key rollover tests')