This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ticket_test.go
477 lines (409 loc) · 12.4 KB
/
ticket_test.go
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
package kerb
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"github.com/jmckaskill/asn1"
"io"
"reflect"
"testing"
"time"
)
var _ = fmt.Println
type req struct {
realm string
key key
req *kdcRequest
app *appRequest
auth *authenticator
time time.Time
out []byte
udp []byte
}
type conn struct {
req *req
proto string
buf *bytes.Buffer
}
func domd5(d []byte) []byte {
h := md5.New()
h.Write(d)
return h.Sum(nil)
}
func (c *conn) Write(d []byte) (n int, err error) {
//defer recoverMust(&err)
if c.proto == "tcp" {
must(len(d) >= 4)
sz := int(binary.BigEndian.Uint32(d))
must(sz == len(d)+4)
d = d[4:]
}
r := c.req
c.req = nil
req := kdcRequest{}
if r.app != nil {
// TGS request
mustUnmarshal(d, &req, tgsRequestParam)
app := appRequest{}
must(len(req.Preauth) == 1 && req.Preauth[0].Type == paTgsRequest)
mustUnmarshal(req.Preauth[0].Data, &app, appRequestParam)
req.Preauth = nil
auth := authenticator{}
adata := mustDecrypt(r.key, nil, app.Auth.Algo, paTgsRequestKey, app.Auth.Data)
mustUnmarshal(adata, &auth, authenticatorParam)
must(auth.Checksum.Algo == signMd5)
must(bytes.Equal(auth.Checksum.Data, domd5(req.Body.FullBytes)))
app.Auth = encryptedData{}
auth.Checksum = checksumData{}
//fmt.Printf("%#v\n\n%#v\n\n", app, *r.app)
must(reflect.DeepEqual(app, *r.app))
//fmt.Printf("%#v\n\n%#v\n\n", auth, *r.auth)
must(reflect.DeepEqual(auth, *r.auth))
} else if !r.time.IsZero() {
// AS request
mustUnmarshal(d, &req, asRequestParam)
ets := encryptedData{}
ts := encryptedTimestamp{}
must(len(req.Preauth) == 1 && req.Preauth[0].Type == paEncryptedTimestamp)
mustUnmarshal(req.Preauth[0].Data, &ets, "")
tsdata := mustDecrypt(r.key, nil, ets.Algo, paEncryptedTimestampKey, ets.Data)
mustUnmarshal(tsdata, &ts, "")
req.Preauth = nil
must(time.Unix(ts.Time.Unix(), int64(ts.Microseconds)*1000).Equal(r.time))
} else {
// AS preauth request
mustUnmarshal(d, &req, asRequestParam)
must(len(req.Preauth) == 0)
}
var b1, b2 kdcRequestBody
mustUnmarshal(req.Body.FullBytes, &b1, "")
mustUnmarshal(r.req.Body.FullBytes, &b2, "")
//fmt.Printf("%#v\n\n%#v\n\n", req, *r.req)
//fmt.Printf("%#v\n\n%#v\n\n", b1, b2)
must(reflect.DeepEqual(req, *r.req))
if c.proto == "udp" && r.udp != nil {
c.buf = bytes.NewBuffer(r.udp)
} else {
c.buf = bytes.NewBuffer(r.out)
}
return len(d), nil
}
func (c *conn) Read(d []byte) (int, error) {
return c.buf.Read(d)
}
func (c *conn) Close() error {
return nil
}
var reqs []*req
var now = time.Now()
var randsrc io.Reader
type randreader struct{}
func (randreader) Read(b []byte) (int, error) {
return randsrc.Read(b)
}
var kcfg = &CredConfig{
Now: func() time.Time {
return now
},
Dial: func(proto, realm string) (io.ReadWriteCloser, error) {
if len(reqs) == 0 {
return nil, errors.New("unexpected request")
}
if reqs[0].realm != realm {
return nil, errors.New("wrong realm")
}
req := reqs[0]
reqs = reqs[1:]
return &conn{req, proto, nil}, nil
},
Rand: randreader{},
}
func addrand(u uint32) {
binary.Write(randsrc.(io.Writer), binary.BigEndian, u)
}
func mkerror(code int, service, realm string, edata []byte) []byte {
now := time.Now()
msg := errorMessage{
ProtoVersion: kerberosVersion,
MsgType: errorType,
ServerTime: now,
ServerMicroseconds: int(now.Nanosecond() / 1000),
Service: splitPrincipal(service),
ServiceRealm: realm,
ErrorCode: code,
ErrorData: edata,
}
return mustMarshal(msg, errorParam)
}
func mkasreq(client, realm string, nonce uint32) *kdcRequest {
return mkreq(client, "krbtgt/"+realm, realm, nonce, defaultLoginFlags, asRequestParam, asRequestType)
}
func mktgsreq(client, service, srealm string, nonce uint32) *kdcRequest {
return mkreq(client, service, srealm, nonce, DefaultTicketConfig.Flags, tgsRequestParam, tgsRequestType)
}
func mkreq(client, service, realm string, nonce uint32, flags int, param string, typ int) *kdcRequest {
body := kdcRequestBody{
Flags: flagsToBitString(flags),
Client: splitPrincipal(client),
ServiceRealm: realm,
Service: splitPrincipal(service),
Till: notill,
Nonce: nonce >> 1,
Algorithms: supportedAlgorithms,
}
braw := asn1.RawValue{}
mustUnmarshal(mustMarshal(body, param), &braw, param)
return &kdcRequest{
ProtoVersion: kerberosVersion,
MsgType: typ,
Body: braw,
}
}
func mktgsapp(tgs *Ticket) *appRequest {
rtkt := asn1.RawValue{}
mustUnmarshal(tgs.ticket, &rtkt, "")
return &appRequest{
ProtoVersion: kerberosVersion,
MsgType: appRequestType,
Flags: flagsToBitString(0),
Ticket: rtkt,
}
}
func mkauth(client, crealm string, seqnum uint32) *authenticator {
return &authenticator{
ProtoVersion: kerberosVersion,
Client: splitPrincipal(client),
ClientRealm: crealm,
Microseconds: int(now.Nanosecond() / 1000),
Time: time.Unix(now.Unix(), 0).UTC(),
SequenceNumber: seqnum,
}
}
func mkasrep(tkt *Ticket, nonce uint32, ckey key) []byte {
rep, erep := mkrep(tkt, nonce)
erepdata := mustMarshal(*erep, encAsReplyParam)
rep.MsgType = asReplyType
rep.Encrypted = encryptedData{
Algo: ckey.EncryptAlgo(asReplyClientKey),
Data: ckey.Encrypt(nil, asReplyClientKey, erepdata),
}
return mustMarshal(*rep, asReplyParam)
}
func mktgsrep(tkt *Ticket, nonce uint32, ckey key) []byte {
rep, erep := mkrep(tkt, nonce)
erepdata := mustMarshal(*erep, encTgsReplyParam)
rep.MsgType = tgsReplyType
rep.Encrypted = encryptedData{
Algo: ckey.EncryptAlgo(tgsReplySessionKey),
Data: ckey.Encrypt(nil, tgsReplySessionKey, erepdata),
}
return mustMarshal(*rep, tgsReplyParam)
}
func mkrep(tkt *Ticket, nonce uint32) (*kdcReply, *encryptedKdcReply) {
erep := encryptedKdcReply{
Key: encryptionKey{
Algo: tkt.key.EncryptAlgo(ticketKey),
Key: tkt.key.Key(),
},
Nonce: nonce >> 1,
Flags: flagsToBitString(tkt.flags),
AuthTime: tkt.authTime,
From: tkt.startTime,
Till: tkt.till,
RenewTill: tkt.renewTill,
Service: tkt.service,
ServiceRealm: tkt.srealm,
}
rep := kdcReply{
ProtoVersion: kerberosVersion,
Client: tkt.client,
ClientRealm: tkt.crealm,
Ticket: asn1.RawValue{FullBytes: tkt.ticket},
}
return &rep, &erep
}
// mkcred creates a credential in the given realm.
//
// krbtgt/realm@realm is a login tgs, krbtgt/realm1@realm2 is a cross TGS for
// granting tickets for services in realm1 to clients in realm2.
func mkcred(serv, realm string) *Credential {
key := mustGenerateKey(cryptRc4Hmac, rand.Reader)
return newCredential(splitPrincipal(serv), realm, key, 0, kcfg)
}
var saltTests = []struct {
algo int
salt string
et2 []eTypeInfo2
et []eTypeInfo
pasalt []byte
}{
{cryptRc4Hmac, "", []eTypeInfo2{{cryptRc4Hmac, ""}}, nil, nil},
{cryptDesCbcMd5, "", []eTypeInfo2{{cryptDesCbcMd5, ""}}, nil, nil},
{cryptDesCbcMd5, "salt", []eTypeInfo2{{cryptDesCbcMd5, "salt"}}, nil, nil},
{cryptDesCbcMd5, "salt", nil, []eTypeInfo{{cryptDesCbcMd5, []byte("salt")}}, nil},
{cryptRc4Hmac, "", []eTypeInfo2{{cryptRc4Hmac, ""}}, nil, []byte("salt")},
{cryptDesCbcMd5, "salt", nil, nil, []byte("salt")},
}
func TestPreauth(t *testing.T) {
for i, d := range saltTests {
ckey := mustLoadStringKey(d.algo, "password", d.salt)
var pa []preauth
if d.et2 != nil {
d := mustMarshal(d.et2, "")
pa = append(pa, preauth{paETypeInfo2, d})
}
if d.et != nil {
d := mustMarshal(d.et, "")
pa = append(pa, preauth{paETypeInfo, d})
}
if d.pasalt != nil {
pa = append(pa, preauth{paPasswordSalt, d.pasalt})
}
edata := mustMarshal(pa, "")
randsrc = new(bytes.Buffer)
addrand(0x12345678) // nonce
addrand(123456) // microseconds
reqs = []*req{
{
realm: "EXAMPLE.COM",
req: mkasreq("foo", "EXAMPLE.COM", 0x12345678),
out: mkerror(KDC_ERR_PREAUTH_REQUIRED, "krbtgt/EXAMPLE.COM", "EXAMPLE.COM", edata),
},
}
c, err := NewCredential("foo", "EXAMPLE.COM", "password", kcfg)
if err != nil {
t.Errorf("Test %d failed with %v\n", i, err)
} else if !reflect.DeepEqual(ckey, c.key) {
t.Errorf("Test %d failed - key mismatch\n", i)
} else if len(reqs) > 0 {
t.Errorf("Test %d failed - requests left\n", i)
}
}
}
func TestGetTicket(t *testing.T) {
randsrc = rand.Reader
tgs := mkcred("krbtgt/EXAMPLE.COM", "EXAMPLE.COM")
serv := mkcred("http/www.example.com", "EXAMPLE.COM")
cred := mkcred("foo", "EXAMPLE.COM")
tgt := tgs.mustGenerateTicket("foo", "EXAMPLE.COM", nil)
tkt := serv.mustGenerateTicket("foo", "EXAMPLE.COM", nil)
randsrc = new(bytes.Buffer)
addrand(0x12345678) // AS_REQ nonce
addrand(123456) // AS_REQ microseconds
addrand(0x23456789) // TGS_REQ nonce
addrand(0x34567890) // TGS_REQ seqnum
reqs = []*req{
{
realm: "EXAMPLE.COM",
key: cred.key,
req: mkasreq("foo", "EXAMPLE.COM", 0x12345678),
time: time.Unix(now.Unix(), 123456000),
out: mkasrep(tgt, 0x12345678, cred.key),
},
{
realm: "EXAMPLE.COM",
key: tgt.key,
req: mktgsreq("foo", "http/www.example.com", "EXAMPLE.COM", 0x23456789),
app: mktgsapp(tgt),
auth: mkauth("foo", "EXAMPLE.COM", 0x34567890),
out: mktgsrep(tkt, 0x23456789, tgt.key),
},
}
tkt2, err := cred.GetTicket("http/www.example.com", nil)
if err != nil {
t.Fatal(err)
} else if len(reqs) > 0 {
t.Fatal("requests left")
} else if !reflect.DeepEqual(tkt, tkt2) {
t.Fatalf("ticket mismatch\ngot: %+v\nexp: %+v\n", tkt2, tkt)
}
}
func TestGetTicket2(t *testing.T) {
randsrc = rand.Reader
serv := mkcred("http/www.example.com", "E.EXAMPLE.COM")
cred := mkcred("foo", "A.EXAMPLE.COM")
tgsa := mkcred("krbtgt/A.EXAMPLE.COM", "A.EXAMPLE.COM")
tgsb := mkcred("krbtgt/B.EXAMPLE.COM", "A.EXAMPLE.COM")
tgsc := mkcred("krbtgt/C.EXAMPLE.COM", "B.EXAMPLE.COM")
tgsd := mkcred("krbtgt/D.EXAMPLE.COM", "C.EXAMPLE.COM")
tgse := mkcred("krbtgt/E.EXAMPLE.COM", "D.EXAMPLE.COM")
tgta := tgsa.mustGenerateTicket("foo", "A.EXAMPLE.COM", nil)
tgtb := tgsb.mustGenerateTicket("foo", "A.EXAMPLE.COM", nil)
tgtc := tgsc.mustGenerateTicket("foo", "A.EXAMPLE.COM", nil)
tgtd := tgsd.mustGenerateTicket("foo", "A.EXAMPLE.COM", nil)
tgte := tgse.mustGenerateTicket("foo", "A.EXAMPLE.COM", nil)
tkt := serv.mustGenerateTicket("foo", "A.EXAMPLE.COM", nil)
randsrc = new(bytes.Buffer)
addrand(0x12345678) // AS_REQ nonce
addrand(123456) // AS_REQ microseconds
addrand(0x23456789) // A nonce
addrand(0x3456789A) // A seqnum
addrand(0x456789AB) // B nonce
addrand(0x56789ABC) // B seqnum
addrand(0x6789ABCD) // C nonce
addrand(0x789ABCDE) // C seqnum
addrand(0x89ABCDEF) // D nonce
addrand(0x9ABCDEF0) // D seqnum
addrand(0xABCDEF01) // E nonce
addrand(0xBCDEF012) // E seqnum
reqs = []*req{
{
realm: "A.EXAMPLE.COM",
key: cred.key,
req: mkasreq("foo", "A.EXAMPLE.COM", 0x12345678),
time: time.Unix(now.Unix(), 123456000),
out: mkasrep(tgta, 0x12345678, cred.key),
},
{
realm: "A.EXAMPLE.COM",
key: tgta.key,
req: mktgsreq("foo", "http/www.example.com", "A.EXAMPLE.COM", 0x23456789),
app: mktgsapp(tgta),
auth: mkauth("foo", "A.EXAMPLE.COM", 0x3456789A),
out: mktgsrep(tgtb, 0x23456789, tgta.key),
},
{
realm: "B.EXAMPLE.COM",
key: tgtb.key,
req: mktgsreq("foo", "http/www.example.com", "B.EXAMPLE.COM", 0x456789AB),
app: mktgsapp(tgtb),
auth: mkauth("foo", "A.EXAMPLE.COM", 0x56789ABC),
out: mktgsrep(tgtc, 0x456789AB, tgtb.key),
},
{
realm: "C.EXAMPLE.COM",
key: tgtc.key,
req: mktgsreq("foo", "http/www.example.com", "C.EXAMPLE.COM", 0x6789ABCD),
app: mktgsapp(tgtc),
auth: mkauth("foo", "A.EXAMPLE.COM", 0x789ABCDE),
out: mktgsrep(tgtd, 0x6789ABCD, tgtc.key),
},
{
realm: "D.EXAMPLE.COM",
key: tgtd.key,
req: mktgsreq("foo", "http/www.example.com", "D.EXAMPLE.COM", 0x89ABCDEF),
app: mktgsapp(tgtd),
auth: mkauth("foo", "A.EXAMPLE.COM", 0x9ABCDEF0),
out: mktgsrep(tgte, 0x89ABCDEF, tgtd.key),
},
{
realm: "E.EXAMPLE.COM",
key: tgte.key,
req: mktgsreq("foo", "http/www.example.com", "E.EXAMPLE.COM", 0xABCDEF01),
app: mktgsapp(tgte),
auth: mkauth("foo", "A.EXAMPLE.COM", 0xBCDEF012),
out: mktgsrep(tkt, 0xABCDEF01, tgte.key),
},
}
tkt2, err := cred.GetTicket("http/www.example.com", nil)
if err != nil {
t.Fatal(err)
} else if len(reqs) > 0 {
t.Fatal("requests left")
} else if !reflect.DeepEqual(tkt, tkt2) {
t.Fatalf("ticket mismatch\ngot: %+v\nexp: %+v\n", tkt2, tkt)
}
}