forked from dotnet/Kerberos.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKdcHandlerTests.cs
More file actions
499 lines (421 loc) · 18 KB
/
KdcHandlerTests.cs
File metadata and controls
499 lines (421 loc) · 18 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// -----------------------------------------------------------------------
// Licensed to The .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Kerberos.NET;
using Kerberos.NET.Client;
using Kerberos.NET.Configuration;
using Kerberos.NET.Credentials;
using Kerberos.NET.Crypto;
using Kerberos.NET.Entities;
using Kerberos.NET.Entities.Pac;
using Kerberos.NET.Server;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Tests.Kerberos.NET.KdcListenerTestBase;
namespace Tests.Kerberos.NET
{
[TestClass]
public class KdcHandlerTests : BaseTest
{
private const string Realm = "CORP2.IDENTITYINTERVENTION.COM";
private const string Upn = "fake@" + Realm;
private const string Realm2 = "TEST.COM";
private const string Upn2WithoutRealm = "fakeuser";
// private const string Upn2WithoutRealm = "fakeuser@" + Realm2;
[TestMethod]
public void KdcAsReqHandler_Sync()
{
KrbAsRep asRep = RequestTgt(cname: Upn, crealm: Realm, srealm: Realm, out _, out KrbAsReq asReq);
ValidateAsRep(
asRep,
expectedCName: Upn,
expectedCRealm: Realm,
expectedSRealm: Realm,
expectPac: true,
asReq);
}
[TestMethod]
public void KdcTgsReqHandler_Sync()
{
KrbAsRep asRep = RequestTgt(cname: Upn, crealm: Realm, srealm: Realm, out KrbEncryptionKey tgtKey);
Assert.IsNotNull(asRep);
var spn = "host/foo." + Realm;
var tgsReq = KrbTgsReq.CreateTgsReq(
new RequestServiceTicket
{
Realm = Realm,
ServicePrincipalName = spn
},
tgtKey,
asRep,
out KrbEncryptionKey sessionKey
);
var handler = new KdcTgsReqMessageHandler(tgsReq.EncodeApplication(), new KdcServerOptions
{
DefaultRealm = Realm,
IsDebug = true,
RealmLocator = realm => new FakeRealmService(realm)
});
var results = handler.Execute();
var tgsRep = KrbTgsRep.DecodeApplication(results);
var realmService = new FakeRealmService(Realm);
var ticketKey = realmService.Principals.Find(KrbPrincipalName.FromString(spn)).RetrieveLongTermCredential();
ValidateTgsRep(
tgsRep,
subSessionKey: sessionKey.AsKey(),
ticketKey: ticketKey,
expectedCName: Upn,
expectedCRealm: Realm,
expectedSName: spn,
expectedSRealm: Realm,
expectPac: true);
}
[TestMethod]
public void KdcTgsReqHandler_Sync_ReferralTgt()
{
var sourceRealm = Realm2;
var destRealm = Realm;
var cname = new KrbPrincipalName
{
Type = PrincipalNameType.NT_PRINCIPAL,
Name = new[] { Upn2WithoutRealm }
};
KrbAsRep asRep = CreateReferralTgt(
sourceRealm,
destRealm,
cname,
includePac: true,
out KerberosKey tgtKey,
out KerberosKey asRepKey,
out KrbEncryptionKey sessionKey);
ValidateAsRep(
asRep,
expectedCName: Upn2WithoutRealm,
expectedCRealm: sourceRealm,
expectedSRealm: destRealm,
expectPac: true);
// Send a TGS-REQ to get a service ticket in the destination realm
var spn = "host/foo." + Realm;
var tgsReq = KrbTgsReq.CreateTgsReq(
new RequestServiceTicket
{
Realm = Realm,
ServicePrincipalName = spn
},
sessionKey,
asRep,
out KrbEncryptionKey subSessionKey
);
var handler = new KdcTgsReqMessageHandler(tgsReq.EncodeApplication(), new KdcServerOptions
{
DefaultRealm = destRealm,
IsDebug = true,
RealmLocator = realm => new FakeRealmService(realm)
});
var results = handler.Execute();
var tgsRep = KrbTgsRep.DecodeApplication(results);
var destRealmService = new FakeRealmService(destRealm);
var servicePrincipal = destRealmService.Principals.Find(KrbPrincipalName.FromString(spn), destRealm);
var ticketKey = servicePrincipal.RetrieveLongTermCredential();
ValidateTgsRep(
tgsRep,
subSessionKey: subSessionKey.AsKey(),
ticketKey: ticketKey,
expectedCName: Upn2WithoutRealm,
expectedCRealm: sourceRealm,
expectedSName: spn,
expectedSRealm: destRealm,
expectPac: true);
}
private void ValidateAsRep(
KrbAsRep asRep,
string expectedCName,
string expectedCRealm,
string expectedSRealm,
bool expectPac,
KrbAsReq asReq = null)
{
Assert.IsNotNull(asRep);
// RFC 4120 Section 3.1.5 Receipt of KRB_AS_REP Message
// "If the reply message type is KRB_AS_REP, then the client verifies that the cname and crealm fields in
// the cleartext portion of the reply match what it requested."
Assert.AreEqual(MessageType.KRB_AS_REP, asRep.MessageType);
Assert.AreEqual(expectedCRealm, asRep.CRealm);
Assert.AreEqual(expectedCName, asRep.CName.FullyQualifiedName);
// Optionally double check that the AS-REQ also matches
if (asReq != null)
{
Assert.AreEqual(Realm, asReq.Body.Realm);
Assert.AreEqual(Upn, asReq.Body.CName.FullyQualifiedName);
}
// Check that correct TGT was generated
Assert.AreEqual(expectedSRealm, asRep.Ticket.Realm);
Assert.AreEqual($"krbtgt/{expectedSRealm}", asRep.Ticket.SName.FullyQualifiedName);
// Clients can't decrypt TGTs usually, but for the sake of testing let's check what's inside
var realmService = new FakeRealmService(Realm);
var tgtPrincipalName = KrbPrincipalName.WellKnown.Krbtgt(Realm);
var tgtEncPartKey = realmService.Principals.Find(tgtPrincipalName).RetrieveLongTermCredential();
var ticketEncPart = asRep.Ticket.EncryptedPart.Decrypt(
tgtEncPartKey,
KeyUsage.Ticket,
d => KrbEncTicketPart.DecodeApplication(d)
);
Assert.IsNotNull(ticketEncPart);
Assert.AreEqual(expectedCRealm, ticketEncPart.CRealm);
Assert.AreEqual(expectedCName, ticketEncPart.CName.FullyQualifiedName);
// Check PAC fields
bool success = ticketEncPart.TryGetPac(out PrivilegedAttributeCertificate pac);
if (!expectPac)
{
Assert.IsFalse(success);
Assert.IsNull(pac);
}
else
{
Assert.IsTrue(success);
Assert.IsNotNull(pac);
Assert.AreEqual(expectedCName, pac.ClientInformation.Name);
}
}
private void ValidateTgsRep(
KrbTgsRep tgsRep,
KerberosKey subSessionKey,
KerberosKey ticketKey,
string expectedCName,
string expectedCRealm,
string expectedSName,
string expectedSRealm,
bool expectPac)
{
Assert.IsNotNull(tgsRep);
var encKdcRepPart = tgsRep.EncPart.Decrypt(
subSessionKey,
KeyUsage.EncTgsRepPartSubSessionKey,
d => KrbEncTgsRepPart.DecodeApplication(d)
);
Assert.IsNotNull(encKdcRepPart);
Assert.AreEqual(expectedCRealm, tgsRep.CRealm);
Assert.AreEqual(expectedCName, tgsRep.CName.FullyQualifiedName);
Assert.IsNotNull(tgsRep.Ticket);
Assert.AreEqual(expectedSRealm, tgsRep.Ticket.Realm);
Assert.AreEqual(expectedSName, tgsRep.Ticket.SName.FullyQualifiedName);
// Clients can't decrypt service tickets usually, but for the sake of testing let's check what's inside
var ticketEncPart = tgsRep.Ticket.EncryptedPart.Decrypt(
ticketKey,
KeyUsage.Ticket,
d => KrbEncTicketPart.DecodeApplication(d)
);
Assert.IsNotNull(ticketEncPart);
Assert.AreEqual(expectedCRealm, ticketEncPart.CRealm);
Assert.AreEqual(expectedCName, ticketEncPart.CName.FullyQualifiedName);
// Check PAC fields
bool success = ticketEncPart.TryGetPac(out PrivilegedAttributeCertificate pac);
if (!expectPac)
{
Assert.IsFalse(success);
Assert.IsNull(pac);
}
else
{
Assert.IsTrue(success);
Assert.IsNotNull(pac);
Assert.AreEqual(expectedCName, pac.ClientInformation.Name);
}
}
private KrbAsRep CreateReferralTgt(
string sourceRealm,
string destRealm,
KrbPrincipalName cname,
bool includePac,
out KerberosKey tgtKey,
out KerberosKey asRepKey,
out KrbEncryptionKey sessionKey)
{
var sourceRealmService = new FakeRealmService(sourceRealm);
var sname = KrbPrincipalName.WellKnown.Krbtgt(destRealm);
var servicePrincipal = sourceRealmService.Principals.Find(sname, destRealm);
tgtKey = servicePrincipal.RetrieveLongTermCredential();
var clientPrincipal = sourceRealmService.Principals.Find(cname, sourceRealm);
asRepKey = clientPrincipal.RetrieveLongTermCredential();
sessionKey = KrbEncryptionKey.Generate(EncryptionType.AES128_CTS_HMAC_SHA256_128);
DateTimeOffset now = DateTimeOffset.UtcNow;
KrbAuthorizationData[] authorizationData = null;
if (includePac)
{
var pac = clientPrincipal.GeneratePac();
Assert.IsNotNull(pac);
pac.ClientInformation = new PacClientInfo
{
Name = cname.FullyQualifiedName,
ClientId = RpcFileTime.ConvertWithoutMicroseconds(now),
};
authorizationData = new[]
{
new KrbAuthorizationData
{
Type = AuthorizationDataType.AdIfRelevant,
Data = new KrbAuthorizationDataSequence
{
AuthorizationData = new[]
{
new KrbAuthorizationData
{
Type = AuthorizationDataType.AdWin2kPac,
Data = pac.Encode(tgtKey, tgtKey)
}
}
}.Encode()
}
};
}
var encTicketPart = new KrbEncTicketPart()
{
CName = cname,
CRealm = sourceRealm,
Key = sessionKey,
AuthTime = now,
StartTime = now,
EndTime = now.AddHours(1),
RenewTill = now.AddDays(30),
Flags = TicketFlags.PreAuthenticated | TicketFlags.Initial | TicketFlags.Renewable | TicketFlags.Forwardable,
AuthorizationData = authorizationData,
CAddr = new KrbHostAddress[] { },
Transited = new KrbTransitedEncoding()
};
KrbTicket ticket = new KrbTicket()
{
Realm = destRealm,
SName = sname,
EncryptedPart = KrbEncryptedData.Encrypt(
encTicketPart.EncodeApplication(),
tgtKey,
KeyUsage.Ticket
)
};
KrbEncAsRepPart encAsRepPart = new KrbEncAsRepPart
{
AuthTime = encTicketPart.AuthTime,
StartTime = encTicketPart.AuthTime,
EndTime = encTicketPart.EndTime,
RenewTill = encTicketPart.RenewTill,
KeyExpiration = servicePrincipal.Expires,
Realm = destRealm,
SName = sname,
Flags = encTicketPart.Flags,
CAddr = encTicketPart.CAddr,
Key = sessionKey,
Nonce = 1234567890,
LastReq = new[] { new KrbLastReq { Type = 0, Value = now } },
EncryptedPaData = new KrbMethodData
{
MethodData = new[]
{
new KrbPaData
{
Type = PaDataType.PA_SUPPORTED_ETYPES,
Value = servicePrincipal.SupportedEncryptionTypes.AsReadOnlyMemory(littleEndian: true)
}
}
}
};
KrbAsRep asRep = new KrbAsRep
{
CRealm = Realm2,
CName = new KrbPrincipalName
{
Type = PrincipalNameType.NT_PRINCIPAL,
Name = new[] { Upn2WithoutRealm }
},
MessageType = MessageType.KRB_AS_REP,
Ticket = ticket,
EncPart = KrbEncryptedData.Encrypt(
encAsRepPart.EncodeApplication(),
asRepKey,
asRepKey.EncryptionType,
KeyUsage.EncAsRepPart
)
};
return asRep;
}
private KrbAsRep RequestTgt(string cname, string crealm, string srealm, out KrbEncryptionKey sessionKey)
{
return RequestTgt(cname, crealm, srealm, out sessionKey, out _);
}
private KrbAsRep RequestTgt(string cname, string crealm, string srealm, out KrbEncryptionKey sessionKey, out KrbAsReq asReq)
{
var cred = new KerberosPasswordCredential(cname, "P@ssw0rd!", crealm)
{
// cheating by skipping the initial leg of requesting PA-type
Salts = new[]
{
new KeyValuePair<EncryptionType, string>(
EncryptionType.AES256_CTS_HMAC_SHA1_96,
"CORP.IDENTITYINTERVENTION.COMfake@CORP2.IDENTITYINTERVENTION.COM"
)
},
Configuration = Krb5Config.Default()
};
asReq = KrbAsReq.CreateAsReq(
cred,
AuthenticationOptions.AllAuthentication
);
var handler = new KdcAsReqMessageHandler(asReq.EncodeApplication(), new KdcServerOptions
{
DefaultRealm = srealm,
IsDebug = true,
RealmLocator = realm => new FakeRealmService(realm)
});
handler.PreAuthHandlers[PaDataType.PA_ENC_TIMESTAMP] = service => new PaDataTimestampHandler(service);
var results = handler.Execute();
var decoded = KrbAsRep.DecodeApplication(results);
var decrypted = cred.DecryptKdcRep(
decoded,
KeyUsage.EncAsRepPart,
d => KrbEncAsRepPart.DecodeApplication(d)
);
sessionKey = decrypted.Key;
return decoded;
}
[TestMethod]
public void AsReqPreAuth_PkinitCertificateAccessible()
{
using (var credCert = new X509Certificate2(ReadDataFile("testuser.pfx"), "p"))
using (var cred = new TrustedAsymmetricCredential(credCert, "user@domain.com"))
{
var asReq = KrbAsReq.CreateAsReq(cred, AuthenticationOptions.AllAuthentication);
var handler = new KdcAsReqMessageHandler(
asReq.EncodeApplication(),
new KdcServerOptions
{
DefaultRealm = "corp.identityintervention.com",
RealmLocator = realm => new FakeRealmService(realm)
});
handler.PreAuthHandlers[PaDataType.PA_PK_AS_REQ] = service => new PaDataPkAsReqHandler(service)
{
IncludeOption = X509IncludeOption.EndCertOnly
};
var context = new PreAuthenticationContext();
handler.DecodeMessage(context);
handler.ExecutePreValidate(context);
handler.QueryPreValidate(context);
handler.ValidateTicketRequest(context);
handler.QueryPreExecute(context);
handler.ExecuteCore(context);
Assert.AreEqual(PaDataType.PA_PK_AS_REQ, context.ClientAuthority);
Assert.AreEqual(1, context.PreAuthenticationState.Count);
Assert.IsTrue(context.PreAuthenticationState.TryGetValue(PaDataType.PA_PK_AS_REQ, out PaDataState paState));
var state = paState as PkInitState;
Assert.IsNotNull(state);
Assert.IsNotNull(state.ClientCertificate);
Assert.AreEqual(1, state.ClientCertificate.Count);
var clientCert = state.ClientCertificate[0];
Assert.IsFalse(clientCert.HasPrivateKey);
Assert.AreEqual(credCert.Thumbprint, clientCert.Thumbprint);
}
}
}
}