File: t_pkinit.py

package info (click to toggle)
krb5 1.12.1+dfsg-19+deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 67,680 kB
  • ctags: 34,155
  • sloc: ansic: 309,213; cpp: 15,753; exp: 13,257; makefile: 7,652; python: 7,228; sh: 6,457; perl: 3,514; asm: 1,544; yacc: 1,187; awk: 396; xml: 368; csh: 147; lisp: 104; sed: 43
file content (313 lines) | stat: -rw-r--r-- 11,971 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python
from k5test import *

# Skip this test if pkinit wasn't built.
if not os.path.exists(os.path.join(plugins, 'preauth', 'pkinit.so')):
    success('Warning: not testing pkinit because it is not built')
    exit(0)

# Check if soft-pkcs11.so is available.
try:
    import ctypes
    lib = ctypes.LibraryLoader(ctypes.CDLL).LoadLibrary('soft-pkcs11.so')
    del lib
    have_soft_pkcs11 = True
except:
    have_soft_pkcs11 = False

# Construct a krb5.conf fragment configuring pkinit.
certs = os.path.join(srctop, 'tests', 'dejagnu', 'pkinit-certs')
ca_pem = os.path.join(certs, 'ca.pem')
kdc_pem = os.path.join(certs, 'kdc.pem')
user_pem = os.path.join(certs, 'user.pem')
privkey_pem = os.path.join(certs, 'privkey.pem')
privkey_enc_pem = os.path.join(certs, 'privkey-enc.pem')
user_p12 = os.path.join(certs, 'user.p12')
user_enc_p12 = os.path.join(certs, 'user-enc.p12')
path = os.path.join(os.getcwd(), 'testdir', 'tmp-pkinit-certs')
path_enc = os.path.join(os.getcwd(), 'testdir', 'tmp-pkinit-certs-enc')

pkinit_krb5_conf = {'realms': {'$realm': {
            'pkinit_anchors': 'FILE:%s' % ca_pem}}}
pkinit_kdc_conf = {'realms': {'$realm': {
            'default_principal_flags': '+preauth',
            'pkinit_eku_checking': 'none',
            'pkinit_identity': 'FILE:%s,%s' % (kdc_pem, privkey_pem)}}}
restrictive_kdc_conf = {'realms': {'$realm': {
            'restrict_anonymous_to_tgt': 'true' }}}

file_identity = 'FILE:%s,%s' % (user_pem, privkey_pem)
file_enc_identity = 'FILE:%s,%s' % (user_pem, privkey_enc_pem)
dir_identity = 'DIR:%s' % path
dir_enc_identity = 'DIR:%s' % path_enc
dir_file_identity = 'FILE:%s,%s' % (os.path.join(path, 'user.crt'),
                                    os.path.join(path, 'user.key'))
dir_file_enc_identity = 'FILE:%s,%s' % (os.path.join(path_enc, 'user.crt'),
                                        os.path.join(path_enc, 'user.key'))
p12_identity = 'PKCS12:%s' % user_p12
p12_enc_identity = 'PKCS12:%s' % user_enc_p12
p11_identity = 'PKCS11:soft-pkcs11.so'
p11_token_identity = ('PKCS11:module_name=soft-pkcs11.so:'
                      'slotid=1:token=SoftToken (token)')

realm = K5Realm(krb5_conf=pkinit_krb5_conf, kdc_conf=pkinit_kdc_conf,
                get_creds=False)

# Sanity check - password-based preauth should still work.
realm.run(['./responder',
           '-r', 'password=%s' % password('user'),
           'user@%s' % realm.realm])
realm.kinit('user@%s' % realm.realm,
            password=password('user'))
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# Test anonymous PKINIT.
out = realm.kinit('@%s' % realm.realm, flags=['-n'], expected_code=1)
if 'not found in Kerberos database' not in out:
    fail('Wrong error for anonymous PKINIT without anonymous enabled')
realm.addprinc('WELLKNOWN/ANONYMOUS')
realm.kinit('@%s' % realm.realm, flags=['-n'])
realm.klist('WELLKNOWN/ANONYMOUS@WELLKNOWN:ANONYMOUS')
realm.run([kvno, realm.host_princ])

# Test with anonymous restricted; FAST should work but kvno should fail.
r_env = realm.special_env('restrict', True, kdc_conf=restrictive_kdc_conf)
realm.stop_kdc()
realm.start_kdc(env=r_env)
realm.kinit('@%s' % realm.realm, flags=['-n'])
realm.kinit('@%s' % realm.realm, flags=['-n', '-T', realm.ccache])
out = realm.run([kvno, realm.host_princ], expected_code=1)
if 'KDC policy rejects request' not in out:
    fail('Wrong error for restricted anonymous PKINIT')

# Regression test for #8458: S4U2Self requests crash the KDC if
# anonymous is restricted.
realm.kinit(realm.host_princ, flags=['-k'])
realm.run([kvno, '-U', 'user', realm.host_princ])

# Go back to a normal KDC and disable anonymous PKINIT.
realm.stop_kdc()
realm.start_kdc()
realm.run_kadminl('delprinc -force WELLKNOWN/ANONYMOUS')

# Run the basic test - PKINIT with FILE: identity, with no password on the key.
realm.run(['./responder',
           '-x',
           'pkinit=',
           '-X',
           'X509_user_identity=%s' % file_identity,
           'user@%s' % realm.realm])
realm.kinit('user@%s' % realm.realm,
            flags=['-X', 'X509_user_identity=%s' % file_identity])
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# Run the basic test - PKINIT with FILE: identity, with a password on the key,
# supplied by the prompter.
# Expect failure if the responder does nothing, and we have no prompter.
realm.run(['./responder',
          '-x',
          'pkinit={"%s": 0}' % file_enc_identity,
          '-X',
          'X509_user_identity=%s' % file_enc_identity,
          'user@%s' % realm.realm],
          expected_code=2)
realm.kinit('user@%s' % realm.realm,
            flags=['-X', 'X509_user_identity=%s' % file_enc_identity],
            password='encrypted')
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# Run the basic test - PKINIT with FILE: identity, with a password on the key,
# supplied by the responder.
# Supply the response in raw form.
realm.run(['./responder',
           '-x',
           'pkinit={"%s": 0}' % file_enc_identity,
           '-r',
           'pkinit={"%s": "encrypted"}' % file_enc_identity,
           '-X',
           'X509_user_identity=%s' % file_enc_identity,
           'user@%s' % realm.realm])
# Supply the response through the convenience API.
realm.run(['./responder',
           '-X',
           'X509_user_identity=%s' % file_enc_identity,
           '-p',
           '%s=%s' % (file_enc_identity, 'encrypted'),
           'user@%s' % realm.realm])
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# PKINIT with DIR: identity, with no password on the key.
os.mkdir(path)
os.mkdir(path_enc)
shutil.copy(privkey_pem, os.path.join(path, 'user.key'))
shutil.copy(privkey_enc_pem, os.path.join(path_enc, 'user.key'))
shutil.copy(user_pem, os.path.join(path, 'user.crt'))
shutil.copy(user_pem, os.path.join(path_enc, 'user.crt'))
realm.run(['./responder',
           '-x',
           'pkinit=',
           '-X',
           'X509_user_identity=%s' % dir_identity,
           'user@%s' % realm.realm])
realm.kinit('user@%s' % realm.realm,
            flags=['-X', 'X509_user_identity=%s' % dir_identity])
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# PKINIT with DIR: identity, with a password on the key, supplied by the
# prompter.
# Expect failure if the responder does nothing, and we have no prompter.
realm.run(['./responder',
           '-x',
           'pkinit={"%s": 0}' %
           dir_file_enc_identity,
           '-X',
           'X509_user_identity=%s' % dir_enc_identity,
           'user@%s' % realm.realm],
           expected_code=2)
realm.kinit('user@%s' % realm.realm,
            flags=['-X', 'X509_user_identity=%s' % dir_enc_identity],
            password='encrypted')
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# PKINIT with DIR: identity, with a password on the key, supplied by the
# responder.
# Supply the response in raw form.
realm.run(['./responder',
           '-x',
           'pkinit={"%s": 0}' %
           dir_file_enc_identity,
           '-r',
           'pkinit={"%s": "encrypted"}' % dir_file_enc_identity,
           '-X',
           'X509_user_identity=%s' % dir_enc_identity,
           'user@%s' % realm.realm])
# Supply the response through the convenience API.
realm.run(['./responder',
           '-X',
           'X509_user_identity=%s' % dir_enc_identity,
           '-p',
           '%s=%s' % (dir_file_enc_identity, 'encrypted'),
           'user@%s' % realm.realm])
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# PKINIT with PKCS12: identity, with no password on the bundle.
realm.run(['./responder',
           '-x',
           'pkinit=',
           '-X',
           'X509_user_identity=%s' % p12_identity,
           'user@%s' % realm.realm])
realm.kinit('user@%s' % realm.realm,
            flags=['-X', 'X509_user_identity=%s' % p12_identity])
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# PKINIT with PKCS12: identity, with a password on the bundle, supplied by the
# prompter.
# Expect failure if the responder does nothing, and we have no prompter.
realm.run(['./responder',
           '-x',
           'pkinit={"%s": 0}' % p12_enc_identity,
           '-X',
           'X509_user_identity=%s' % p12_enc_identity,
           'user@%s' % realm.realm],
           expected_code=2)
realm.kinit('user@%s' % realm.realm,
            flags=['-X', 'X509_user_identity=%s' % p12_enc_identity],
            password='encrypted')
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

# PKINIT with PKCS12: identity, with a password on the bundle, supplied by the
# responder.
# Supply the response in raw form.
realm.run(['./responder',
           '-x',
           'pkinit={"%s": 0}' % p12_enc_identity,
           '-r',
           'pkinit={"%s": "encrypted"}' % p12_enc_identity,
           '-X',
           'X509_user_identity=%s' % p12_enc_identity,
           'user@%s' % realm.realm])
# Supply the response through the convenience API.
realm.run(['./responder',
           '-X',
           'X509_user_identity=%s' % p12_enc_identity,
           '-p',
           '%s=%s' % (p12_enc_identity, 'encrypted'),
           'user@%s' % realm.realm])
realm.klist('user@%s' % realm.realm)
realm.run([kvno, realm.host_princ])

if have_soft_pkcs11:
    softpkcs11rc = os.path.join(os.getcwd(), 'testdir', 'soft-pkcs11.rc')
    realm.env['SOFTPKCS11RC'] = softpkcs11rc

    # PKINIT with PKCS11: identity, with no need for a PIN.
    conf = open(softpkcs11rc, 'w')
    conf.write("%s\t%s\t%s\t%s\n" % ('user', 'user token', user_pem,
                                     privkey_pem))
    conf.close()
    # Expect to succeed without having to supply any more information.
    realm.run(['./responder',
               '-x',
               'pkinit=',
               '-X',
               'X509_user_identity=%s' % p11_identity,
               'user@%s' % realm.realm])
    realm.kinit('user@%s' % realm.realm,
                flags=['-X', 'X509_user_identity=%s' % p11_identity])
    realm.klist('user@%s' % realm.realm)
    realm.run([kvno, realm.host_princ])

    # PKINIT with PKCS11: identity, with a PIN supplied by the prompter.
    os.remove(softpkcs11rc)
    conf = open(softpkcs11rc, 'w')
    conf.write("%s\t%s\t%s\t%s\n" % ('user', 'user token', user_pem,
                                     privkey_enc_pem))
    conf.close()
    # Expect failure if the responder does nothing, and there's no prompter
    realm.run(['./responder',
               '-x',
               'pkinit={"%s": 0}' % p11_token_identity,
               '-X',
               'X509_user_identity=%s' % p11_identity,
               'user@%s' % realm.realm],
               expected_code=2)
    realm.kinit('user@%s' % realm.realm,
                flags=['-X', 'X509_user_identity=%s' % p11_identity],
                password='encrypted')
    realm.klist('user@%s' % realm.realm)
    realm.run([kvno, realm.host_princ])

    # PKINIT with PKCS11: identity, with a PIN supplied by the responder.
    # Supply the response in raw form.
    realm.run(['./responder',
               '-x',
               'pkinit={"%s": 0}' % p11_token_identity,
               '-r',
               'pkinit={"%s": "encrypted"}' %
               p11_token_identity,
               '-X',
               'X509_user_identity=%s' % p11_identity,
               'user@%s' % realm.realm])
    # Supply the response through the convenience API.
    realm.run(['./responder',
               '-X',
               'X509_user_identity=%s' % p11_identity,
               '-p',
               '%s=%s' % (p11_token_identity, 'encrypted'),
               'user@%s' % realm.realm])
    realm.klist('user@%s' % realm.realm)
    realm.run([kvno, realm.host_princ])
else:
    output('soft-pkcs11.so not found: skipping tests with PKCS11 identities\n')

success('Authenticated PKINIT')