Skip to content

Commit 60ce5e2

Browse files
committed
Wire native Kerberos GSSAPI into the LDAP SASL bind and remove gokrb5
Phase 7 (part 3) of the native Kerberos v5 work: replace the gokrb5-backed LDAP GSSAPI bind with the native, standard-library-only stack, and drop the external dependency entirely. - network/ldap/kerberos_gssapi.go: a native implementation of go-ldap's ldap.GSSAPIClient interface. It acquires a TGT (password or NT hash), gets an ldap/<host> service ticket, and drives the RFC 4752 SASL GSSAPI handshake via the gssapi package: KRB_AP_REQ (mutual), KRB_AP_REP verification, and the security-layer negotiation (GSS Unwrap of the server offer, GSS Wrap of the "no security layer" response). Server KRB-ERRORs are surfaced. - network/ldap/session.go: use the native client; drop the gokrb5 and go-ldap/gssapi imports. - Remove KerberosInit (the last gokrb5 user) and delete kerberos.go; run go mod tidy to drop github.com/jcmturner/gokrb5 from go.mod/go.sum. - Fix a real RFC 4121 §4.2.3 bug in gssapi Wrap, found via live testing against a Windows Server 2016 DC: for a non-confidential Wrap token the EC field must encode the checksum length (not 0), and the {data|checksum} body is right-rotated by RRC. AD rejected the previous EC=0 tokens with SEC_E_INVALID_TOKEN. Unwrap now uses EC to locate the checksum. Added rotateRight; updated tests with initiator<->acceptor integrity round-trips. - Vendor RFC 4121 and RFC 4752 under network/ldap/docs. Live-validated end-to-end against the 2016 DC: native GSSAPI LDAP bind succeeds (no gokrb5) and authenticated LDAP queries return the domain naming contexts and user accounts. go build ./..., go vet, and all kerberos/ldap tests pass.
1 parent 8098331 commit 60ce5e2

9 files changed

Lines changed: 1899 additions & 166 deletions

go.mod

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,13 @@ require (
77
github.com/TheManticoreProject/winacl v1.3.1
88
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667
99
github.com/go-ldap/ldap/v3 v3.4.12
10-
github.com/jcmturner/gokrb5/v8 v8.4.4
1110
golang.org/x/crypto v0.43.0
1211
golang.org/x/term v0.36.0
1312
)
1413

1514
require (
1615
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
17-
github.com/alexbrainman/sspi v0.0.0-20250919150558-7d374ff0d59e // indirect
1816
github.com/google/uuid v1.6.0 // indirect
19-
github.com/hashicorp/go-uuid v1.0.3 // indirect
20-
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
21-
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
22-
github.com/jcmturner/gofork v1.7.6 // indirect
23-
github.com/jcmturner/goidentity/v6 v6.0.1 // indirect
24-
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
2517
golang.org/x/net v0.46.0 // indirect
2618
golang.org/x/sys v0.37.0 // indirect
2719
)

go.sum

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ github.com/TheManticoreProject/winacl v1.3.1 h1:LR8e914481tiGx/bvchieCq3rYQccoiB
66
github.com/TheManticoreProject/winacl v1.3.1/go.mod h1:Xji/qLzY8AkdBNXRgOS7g+UBEuqMQBBZllr+rl7Hzko=
77
github.com/alexbrainman/sspi v0.0.0-20250919150558-7d374ff0d59e h1:4dAU9FXIyQktpoUAgOJK3OTFc/xug0PCXYCqU0FgDKI=
88
github.com/alexbrainman/sspi v0.0.0-20250919150558-7d374ff0d59e/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
9-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
109
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1110
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1211
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 h1:BP4M0CvQ4S3TGls2FvczZtj5Re/2ZzkV9VwqPHH/3Bo=
@@ -15,11 +14,6 @@ github.com/go-ldap/ldap/v3 v3.4.12 h1:1b81mv7MagXZ7+1r7cLTWmyuTqVqdwbtJSjC0DAp9s
1514
github.com/go-ldap/ldap/v3 v3.4.12/go.mod h1:+SPAGcTtOfmGsCb3h1RFiq4xpp4N636G75OEace8lNo=
1615
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1716
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
18-
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
19-
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
20-
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
21-
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
22-
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
2317
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
2418
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
2519
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
@@ -36,54 +30,15 @@ github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZ
3630
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
3731
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3832
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
39-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
40-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
41-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
42-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
43-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
44-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4533
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
4634
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
47-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
48-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
49-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
50-
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
5135
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
5236
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
53-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
54-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
55-
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
56-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
57-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
58-
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
59-
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
6037
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
6138
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
62-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
63-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
64-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
65-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
66-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
67-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7039
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
7140
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
72-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
73-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
74-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
7541
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
7642
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
77-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
78-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
79-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
80-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
81-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
82-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
83-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
84-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
85-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
86-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
87-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8843
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
8944
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

network/kerberos/v5/gssapi/permessage.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ func rotateLeft(b []byte, n int) []byte {
9090
return out
9191
}
9292

93+
// rotateRight rotates b right by n octets (RFC 4121 §4.2.5): the last n octets
94+
// move to the front.
95+
func rotateRight(b []byte, n int) []byte {
96+
if len(b) == 0 {
97+
return b
98+
}
99+
n %= len(b)
100+
out := make([]byte, len(b))
101+
copy(out, b[len(b)-n:])
102+
copy(out[n:], b[:len(b)-n])
103+
return out
104+
}
105+
93106
// micHeader builds the 16-byte MIC token header.
94107
func micHeader(flags byte, seq uint64) []byte {
95108
h := make([]byte, 16)
@@ -177,14 +190,20 @@ func (ctx *SecContext) Wrap(data []byte, seal bool) ([]byte, error) {
177190
if !ok {
178191
return nil, fmt.Errorf("gssapi: no checksum for etype %d", etype)
179192
}
193+
// RFC 4121 §4.2.4: the checksum is computed over data | header with EC and
194+
// RRC zeroed. hdr already has EC=RRC=0 here.
180195
sum, err := kerbcrypto.GetChecksum(ct, key, kgUsageInitiatorSeal, append(append([]byte{}, data...), hdr...))
181196
if err != nil {
182197
return nil, err
183198
}
184-
out := append([]byte{}, hdr...)
185-
out = append(out, data...)
186-
out = append(out, sum...)
187-
return out, nil
199+
// §4.2.3: for a non-confidential Wrap token, EC encodes the checksum length.
200+
// Set EC and RRC to that length and right-rotate {data|checksum} by RRC so
201+
// the token is {header | checksum | data}, matching what AD emits/expects.
202+
ec := len(sum)
203+
binary.BigEndian.PutUint16(hdr[4:6], uint16(ec))
204+
binary.BigEndian.PutUint16(hdr[6:8], uint16(ec))
205+
body := rotateRight(append(append([]byte{}, data...), sum...), ec)
206+
return append(hdr, body...), nil
188207
}
189208

190209
// Unwrap decodes a Wrap token received from the acceptor, returning the
@@ -224,12 +243,17 @@ func (ctx *SecContext) Unwrap(token []byte) (data []byte, sealed bool, err error
224243
if !ok {
225244
return nil, false, fmt.Errorf("gssapi: no checksum for etype %d", etype)
226245
}
227-
ml := micLen(etype)
228-
if len(body) < ml {
246+
// For a non-confidential Wrap token, EC is the checksum length (RFC 4121
247+
// §4.2.3); fall back to the etype's checksum length if EC is absent.
248+
cksumLen := ec
249+
if cksumLen == 0 {
250+
cksumLen = micLen(etype)
251+
}
252+
if len(body) < cksumLen {
229253
return nil, false, fmt.Errorf("gssapi: Wrap token too short for checksum")
230254
}
231-
payload := body[:len(body)-ml]
232-
got := body[len(body)-ml:]
255+
payload := body[:len(body)-cksumLen]
256+
got := body[len(body)-cksumLen:]
233257
// The checksum is over data | header with EC and RRC zeroed.
234258
zhdr := append([]byte{}, hdr...)
235259
zhdr[4], zhdr[5], zhdr[6], zhdr[7] = 0, 0, 0, 0

network/kerberos/v5/gssapi/permessage_test.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestUnwrapSealFromAcceptor(t *testing.T) {
135135
}
136136
}
137137

138-
func TestWrapIntegrityOnlyRoundtrip(t *testing.T) {
138+
func TestWrapIntegrityOnlyStructure(t *testing.T) {
139139
ctx := newTestContext(300)
140140
data := []byte("signed-not-sealed")
141141

@@ -150,17 +150,51 @@ func TestWrapIntegrityOnlyRoundtrip(t *testing.T) {
150150
if !bytes.Contains(tok, data) {
151151
t.Error("integrity-only Wrap should carry plaintext")
152152
}
153-
154-
// Acceptor verifies: recompute over payload | header(EC/RRC zeroed).
155-
ct, _ := kerbcrypto.ChecksumTypeForEType(ctx.SessionEType)
153+
// RFC 4121 §4.2.3: EC must encode the checksum length (not 0) for a
154+
// non-confidential Wrap token, and RRC is set to rotate it.
156155
ml := micLen(ctx.SessionEType)
157-
payload := tok[16 : len(tok)-ml]
158-
want, _ := kerbcrypto.GetChecksum(ct, ctx.SessionKey, kgUsageInitiatorSeal, append(append([]byte{}, payload...), tok[:16]...))
159-
if !bytes.Equal(tok[len(tok)-ml:], want) {
160-
t.Error("integrity-only Wrap checksum does not verify")
156+
if ec := int(binary.BigEndian.Uint16(tok[4:6])); ec != ml {
157+
t.Errorf("EC = %d, want checksum length %d", ec, ml)
158+
}
159+
if rrc := int(binary.BigEndian.Uint16(tok[6:8])); rrc != ml {
160+
t.Errorf("RRC = %d, want %d", rrc, ml)
161+
}
162+
}
163+
164+
// acceptorWrapIntegrity simulates the acceptor emitting an integrity-only Wrap
165+
// token (SentByAcceptor, EC=RRC=checksum length, right-rotated) as AD does.
166+
func acceptorWrapIntegrity(t *testing.T, key []byte, etype int, seq uint64, data []byte) []byte {
167+
t.Helper()
168+
ct, _ := kerbcrypto.ChecksumTypeForEType(etype)
169+
ml := micLen(etype)
170+
hdr := wrapHeader(flagSentByAcceptor, seq) // EC=RRC=0 for checksum
171+
sum, err := kerbcrypto.GetChecksum(ct, key, kgUsageAcceptorSeal, append(append([]byte{}, data...), hdr...))
172+
if err != nil {
173+
t.Fatal(err)
161174
}
162-
if !bytes.Equal(payload, data) {
163-
t.Error("integrity-only Wrap payload mismatch")
175+
binary.BigEndian.PutUint16(hdr[4:6], uint16(ml))
176+
binary.BigEndian.PutUint16(hdr[6:8], uint16(ml))
177+
body := make([]byte, 0, len(data)+ml)
178+
body = append(append(body, data...), sum...)
179+
// right-rotate by ml
180+
rot := append(append([]byte{}, body[len(body)-ml:]...), body[:len(body)-ml]...)
181+
return append(hdr, rot...)
182+
}
183+
184+
func TestUnwrapIntegrityFromAcceptor(t *testing.T) {
185+
ctx := newTestContext(1)
186+
data := []byte("server-signed-layer-offer")
187+
tok := acceptorWrapIntegrity(t, ctx.SessionKey, ctx.SessionEType, 3, data)
188+
189+
got, sealed, err := ctx.Unwrap(tok)
190+
if err != nil {
191+
t.Fatalf("Unwrap integrity: %v", err)
192+
}
193+
if sealed {
194+
t.Error("token should be reported as not sealed")
195+
}
196+
if !bytes.Equal(got, data) {
197+
t.Errorf("Unwrap integrity data = %q, want %q", got, data)
164198
}
165199
}
166200

network/kerberos/v5/kerberos.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)