-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathclient.go
More file actions
275 lines (224 loc) · 7.16 KB
/
Copy pathclient.go
File metadata and controls
275 lines (224 loc) · 7.16 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
package gssapi
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/jcmturner/gokrb5/v8/types"
"github.com/jcmturner/gokrb5/v8/gssapi"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/jcmturner/gokrb5/v8/crypto"
"github.com/jcmturner/gokrb5/v8/iana/keyusage"
"github.com/jcmturner/gokrb5/v8/messages"
"github.com/jcmturner/gokrb5/v8/credentials"
)
// Client implements ldap.GSSAPIClient interface.
type Client struct {
*client.Client
ekey types.EncryptionKey
Subkey types.EncryptionKey
}
// NewClientWithKeytab creates a new client from a keytab credential.
// Set the realm to empty string to use the default realm from config.
func NewClientWithKeytab(username, realm, keytabPath, krb5confPath string, settings ...func(*client.Settings)) (*Client, error) {
krb5conf, err := config.Load(krb5confPath)
if err != nil {
return nil, err
}
keytab, err := keytab.Load(keytabPath)
if err != nil {
return nil, err
}
client := client.NewWithKeytab(username, realm, keytab, krb5conf, settings...)
return &Client{
Client: client,
}, nil
}
// NewClientWithPassword creates a new client from a password credential.
// Set the realm to empty string to use the default realm from config.
func NewClientWithPassword(username, realm, password string, krb5confPath string, settings ...func(*client.Settings)) (*Client, error) {
krb5conf, err := config.Load(krb5confPath)
if err != nil {
return nil, err
}
client := client.NewWithPassword(username, realm, password, krb5conf, settings...)
return &Client{
Client: client,
}, nil
}
// NewClientFromCCache creates a new client from a populated client cache.
func NewClientFromCCache(ccachePath, krb5confPath string, settings ...func(*client.Settings)) (*Client, error) {
krb5conf, err := config.Load(krb5confPath)
if err != nil {
return nil, err
}
ccache, err := credentials.LoadCCache(ccachePath)
if err != nil {
return nil, err
}
client, err := client.NewFromCCache(ccache, krb5conf, settings...)
if err != nil {
return nil, err
}
return &Client{
Client: client,
}, nil
}
// Close deletes any established secure context and closes the client.
func (client *Client) Close() error {
client.Destroy()
return nil
}
// DeleteSecContext destroys any established secure context.
func (client *Client) DeleteSecContext() error {
client.ekey = types.EncryptionKey{}
client.Subkey = types.EncryptionKey{}
return nil
}
// InitSecContext initiates the establishment of a security context for
// GSS-API between the client and server.
// See RFC 4752 section 3.1.
func (client *Client) InitSecContext(target string, input []byte) ([]byte, bool, error) {
return client.InitSecContextWithOptions(target, input, []int{})
}
// InitSecContextWithOptions initiates the establishment of a security context for
// GSS-API between the client and server.
// See RFC 4752 section 3.1.
func (client *Client) InitSecContextWithOptions(target string, input []byte, APOptions []int) ([]byte, bool, error) {
gssapiFlags := []int{gssapi.ContextFlagInteg, gssapi.ContextFlagConf, gssapi.ContextFlagMutual}
switch input {
case nil:
tkt, ekey, err := client.GetServiceTicket(target)
if err != nil {
return nil, false, err
}
client.ekey = ekey
token, err := spnego.NewKRB5TokenAPREQ(client.Client, tkt, ekey, gssapiFlags, APOptions)
if err != nil {
return nil, false, err
}
output, err := token.Marshal()
if err != nil {
return nil, false, err
}
return output, true, nil
default:
var token spnego.KRB5Token
err := token.Unmarshal(input)
if err != nil {
return nil, false, err
}
var completed bool
if token.IsAPRep() {
completed = true
encpart, err := crypto.DecryptEncPart(token.APRep.EncPart, client.ekey, keyusage.AP_REP_ENCPART)
if err != nil {
return nil, false, err
}
part := &messages.EncAPRepPart{}
if err = part.Unmarshal(encpart); err != nil {
return nil, false, err
}
client.Subkey = part.Subkey
}
if token.IsKRBError() {
return nil, !false, token.KRBError
}
return make([]byte, 0), !completed, nil
}
}
// NegotiateSaslAuth performs the last step of the SASL handshake.
// See RFC 4752 section 3.1.
func (client *Client) NegotiateSaslAuth(input []byte, authzid string) ([]byte, error) {
token := &gssapi.WrapToken{}
err := UnmarshalWrapToken(token, input, true)
if err != nil {
return nil, err
}
if (token.Flags & 0b1) == 0 {
return nil, fmt.Errorf("got a Wrapped token that's not from the server")
}
key := client.ekey
if (token.Flags & 0b100) != 0 {
key = client.Subkey
}
_, err = token.Verify(key, keyusage.GSSAPI_ACCEPTOR_SEAL)
if err != nil {
return nil, err
}
pl := token.Payload
if len(pl) != 4 {
return nil, fmt.Errorf("server send bad final token for SASL GSSAPI Handshake")
}
// We never want a security layer
b := [4]byte{0, 0, 0, 0}
payload := append(b[:], []byte(authzid)...)
encType, err := crypto.GetEtype(key.KeyType)
if err != nil {
return nil, err
}
token = &gssapi.WrapToken{
Flags: 0b100,
EC: uint16(encType.GetHMACBitLength() / 8),
RRC: 0,
SndSeqNum: 1,
Payload: payload,
}
if err := token.SetCheckSum(key, keyusage.GSSAPI_INITIATOR_SEAL); err != nil {
return nil, err
}
output, err := token.Marshal()
if err != nil {
return nil, err
}
return output, nil
}
func getGssWrapTokenId() *[2]byte {
return &[2]byte{0x05, 0x04}
}
func UnmarshalWrapToken(wt *gssapi.WrapToken, b []byte, expectFromAcceptor bool) error {
// Check if we can read a whole header
if len(b) < 16 {
return errors.New("bytes shorter than header length")
}
// Is the Token ID correct?
if !bytes.Equal(getGssWrapTokenId()[:], b[0:2]) {
return fmt.Errorf("wrong Token ID. Expected %s, was %s",
hex.EncodeToString(getGssWrapTokenId()[:]),
hex.EncodeToString(b[0:2]))
}
// Check the acceptor flag
flags := b[2]
isFromAcceptor := flags&0x01 == 1
if isFromAcceptor && !expectFromAcceptor {
return errors.New("unexpected acceptor flag is set: not expecting a token from the acceptor")
}
if !isFromAcceptor && expectFromAcceptor {
return errors.New("expected acceptor flag is not set: expecting a token from the acceptor, not the initiator")
}
// Check the filler byte
if b[3] != gssapi.FillerByte {
return fmt.Errorf("unexpected filler byte: expecting 0xFF, was %s ", hex.EncodeToString(b[3:4]))
}
checksumL := binary.BigEndian.Uint16(b[4:6])
// Sanity check on the checksum length
if int(checksumL) > len(b)-gssapi.HdrLen {
return fmt.Errorf("inconsistent checksum length: %d bytes to parse, checksum length is %d", len(b), checksumL)
}
// Compute the offset in int. checksumL is a uint16 read from the wire, so
// 16 + checksumL overflows the uint16 range once checksumL exceeds 65519,
// wrapping to a small value and turning the slices below into out-of-range
// accesses that panic the bind goroutine.
payloadStart := gssapi.HdrLen + int(checksumL)
wt.Flags = flags
wt.EC = checksumL
wt.RRC = binary.BigEndian.Uint16(b[6:8])
wt.SndSeqNum = binary.BigEndian.Uint64(b[8:16])
wt.CheckSum = b[16:payloadStart]
wt.Payload = b[payloadStart:]
return nil
}