-
Notifications
You must be signed in to change notification settings - Fork 15
/
authenticate.go
437 lines (375 loc) · 11.1 KB
/
authenticate.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
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"fmt"
)
//
// tacplus authenticate message
// https://datatracker.ietf.org/doc/html/rfc8907#section-5
//
// AuthenStartLen minumum length of this packet type
const AuthenStartLen = 0x08
// AuthenStartOption is used to inject options when creating new AuthenStart types
type AuthenStartOption func(*AuthenStart)
// SetAuthenStartAction sets indicated authentication action
func SetAuthenStartAction(v AuthenAction) AuthenStartOption {
return func(a *AuthenStart) {
a.Action = v
}
}
// SetAuthenStartPrivLvl sets the indicated authentication priv level
func SetAuthenStartPrivLvl(v PrivLvl) AuthenStartOption {
return func(a *AuthenStart) {
a.PrivLvl = v
}
}
// SetAuthenStartType sets the indicated authentication type
func SetAuthenStartType(v AuthenType) AuthenStartOption {
return func(a *AuthenStart) {
a.Type = v
}
}
// SetAuthenStartService sets the indicated authentication service
func SetAuthenStartService(v AuthenService) AuthenStartOption {
return func(a *AuthenStart) {
a.Service = v
}
}
// SetAuthenStartUser sets the indicated user
func SetAuthenStartUser(v AuthenUser) AuthenStartOption {
return func(a *AuthenStart) {
a.User = v
}
}
// SetAuthenStartPort sets the calling port
func SetAuthenStartPort(v AuthenPort) AuthenStartOption {
return func(a *AuthenStart) {
a.Port = v
}
}
// SetAuthenStartRemAddr sets the remote address
func SetAuthenStartRemAddr(v AuthenRemAddr) AuthenStartOption {
return func(a *AuthenStart) {
a.RemAddr = v
}
}
// SetAuthenStartData sets the authentication data
func SetAuthenStartData(v AuthenData) AuthenStartOption {
return func(a *AuthenStart) {
a.Data = v
}
}
// NewAuthenStart will create a new AuthenStart based on the provided options
func NewAuthenStart(opts ...AuthenStartOption) *AuthenStart {
a := &AuthenStart{}
for _, opt := range opts {
opt(a)
}
return a
}
// AuthenStart https://datatracker.ietf.org/doc/html/rfc8907#section-5.1
type AuthenStart struct {
Action AuthenAction
PrivLvl PrivLvl
Type AuthenType
Service AuthenService
User AuthenUser
Port AuthenPort
RemAddr AuthenRemAddr
Data AuthenData
}
// Validate all fields on this type
func (a *AuthenStart) Validate() error {
// validate
if a.Type == AuthenTypeNotSet {
return fmt.Errorf("bad value for AuthenType; AuthenTypeNotSet not allowed for AuthenStart packets")
}
for _, t := range []Field{a.Action, a.PrivLvl, a.Type, a.Service, a.User, a.Port, a.RemAddr, a.Data} {
if err := t.Validate(a.Type); err != nil {
return err
}
}
return nil
}
// MarshalBinary encodes AuthenStart to tacacs bytes
func (a *AuthenStart) MarshalBinary() ([]byte, error) {
// validate
if err := a.Validate(); err != nil {
return nil, err
}
buf := make([]byte, 0, AuthenStartLen)
buf = append(buf, uint8(a.Action))
buf = append(buf, uint8(a.PrivLvl))
buf = append(buf, uint8(a.Type))
buf = append(buf, uint8(a.Service))
buf = append(buf, uint8(a.User.Len()))
buf = append(buf, uint8(a.Port.Len()))
buf = append(buf, uint8(a.RemAddr.Len()))
buf = append(buf, uint8(a.Data.Len()))
buf = append(buf, a.User...)
buf = append(buf, a.Port...)
buf = append(buf, a.RemAddr...)
buf = append(buf, a.Data...)
return buf, nil
}
// UnmarshalBinary decodes decrypted tacacs bytes to AuthenStart
func (a *AuthenStart) UnmarshalBinary(data []byte) error {
if len(data) < AuthenStartLen {
return fmt.Errorf("authenStart size [%v] is too small for the minimum size [%v]", len(data), AuthenStartLen)
}
a.Action = AuthenAction(data[0])
a.PrivLvl = PrivLvl(data[1])
a.Type = AuthenType(data[2])
a.Service = AuthenService(data[3])
buf := readBuffer(data[4:])
userLen := buf.int()
portLen := buf.int()
remAddrLen := buf.int()
dataLen := buf.int()
a.User = AuthenUser(buf.string(userLen))
a.Port = AuthenPort(buf.string(portLen))
a.RemAddr = AuthenRemAddr(buf.string(remAddrLen))
a.Data = AuthenData(buf.string(dataLen))
// detect secret mismatch
if a.Len() != userLen+portLen+remAddrLen+dataLen {
return NewBadSecretErr("bad secret detected authenstart")
}
// validate
if err := a.Validate(); err != nil {
return err
}
return nil
}
// Len will return the unmarshalled size of the component types
func (a AuthenStart) Len() int {
sum := a.User.Len()
sum += a.Port.Len()
sum += a.RemAddr.Len()
sum += a.Data.Len()
return sum
}
// Fields returns fields from this packet compatible with a structured logger
func (a AuthenStart) Fields() map[string]string {
return map[string]string{
"packet-type": "AuthenStart",
"action": a.Action.String(),
"priv-lvl": a.PrivLvl.String(),
"type": a.Type.String(),
"service": a.Service.String(),
"user": a.User.String(),
"port": a.Port.String(),
"rem-addr": a.RemAddr.String(),
"data": a.Data.String(),
}
}
// AuthenContinueLen minumum length of this packet type
const AuthenContinueLen = 0x05
// AuthenContinueOption is used to inject options when creating new AuthenContinue types
type AuthenContinueOption func(*AuthenContinue)
// SetAuthenContinueFlag sets AuthenContinueFlag
func SetAuthenContinueFlag(v AuthenContinueFlag) AuthenContinueOption {
return func(a *AuthenContinue) {
a.Flags = v
}
}
// SetAuthenContinueUserMessage sets AuthenUserMessage
func SetAuthenContinueUserMessage(v AuthenUserMessage) AuthenContinueOption {
return func(a *AuthenContinue) {
a.UserMessage = v
}
}
// SetAuthenContinueData sets AuthenData
func SetAuthenContinueData(v AuthenData) AuthenContinueOption {
return func(a *AuthenContinue) {
a.Data = v
}
}
// NewAuthenContinue will create a new AuthenContinue based on the provided options
func NewAuthenContinue(opts ...AuthenContinueOption) *AuthenContinue {
a := &AuthenContinue{}
var f AuthenContinueFlag
defaults := []AuthenContinueOption{
SetAuthenContinueFlag(f),
}
opts = append(defaults, opts...)
for _, opt := range opts {
opt(a)
}
return a
}
// AuthenContinue see https://datatracker.ietf.org/doc/html/rfc8907#section-5.3
type AuthenContinue struct {
Flags AuthenContinueFlag
UserMessage AuthenUserMessage
Data AuthenData
}
// Validate all fields on this type
func (a *AuthenContinue) Validate() error {
// validate
for _, t := range []Field{a.UserMessage, a.Data} {
if err := t.Validate(nil); err != nil {
return err
}
}
return nil
}
// MarshalBinary encodes AuthenContinue to tacacs bytes
func (a *AuthenContinue) MarshalBinary() ([]byte, error) {
// validate
if err := a.Validate(); err != nil {
return nil, err
}
buf := make([]byte, 0, AuthenContinueLen)
buf = appendUint16(buf, a.UserMessage.Len())
buf = appendUint16(buf, a.Data.Len())
buf = append(buf, uint8(a.Flags))
buf = append(buf, a.UserMessage...)
buf = append(buf, a.Data...)
return buf, nil
}
// UnmarshalBinary decodes decrypted tacacs bytes to AuthenContinue
func (a *AuthenContinue) UnmarshalBinary(data []byte) error {
if len(data) < AuthenContinueLen {
return fmt.Errorf("authenContinue size [%v] is too small for the minimum size [%v]", len(data), AuthenContinueLen)
}
buf := readBuffer(data)
userMessageLen := buf.uint16()
dataLen := buf.uint16()
a.Flags = AuthenContinueFlag(buf.byte())
a.UserMessage = AuthenUserMessage(buf.string(userMessageLen))
a.Data = AuthenData(buf.string(dataLen))
// detect secret mismatch
if a.Len() != userMessageLen+dataLen {
return NewBadSecretErr("bad secret detected authencontinue")
}
// validate
if err := a.Validate(); err != nil {
return err
}
return nil
}
// Len will return the unmarshalled size of the component types
func (a AuthenContinue) Len() int {
sum := a.UserMessage.Len()
sum += a.Data.Len()
return sum
}
// Fields returns fields from this packet compatible with a structured logger
func (a AuthenContinue) Fields() map[string]string {
return map[string]string{
"packet-type": "AuthenContinue",
"flags": a.Flags.String(),
"user-msg": a.UserMessage.String(),
"data": a.Data.String(),
}
}
// AuthenReplyLen minumum length of this packet type
const AuthenReplyLen = 0x05
// AuthenReplyOption is used to inject options when creating new AuthenReply types
type AuthenReplyOption func(*AuthenReply)
// SetAuthenReplyStatus sets an AuthenStatus
func SetAuthenReplyStatus(v AuthenStatus) AuthenReplyOption {
return func(a *AuthenReply) {
a.Status = v
}
}
// SetAuthenReplyFlag sets an AuthenReplyFlag
func SetAuthenReplyFlag(v AuthenReplyFlag) AuthenReplyOption {
return func(a *AuthenReply) {
a.Flags = v
}
}
// SetAuthenReplyServerMsg sets an AuthenServerMsg
func SetAuthenReplyServerMsg(v string) AuthenReplyOption {
return func(a *AuthenReply) {
a.ServerMsg = AuthenServerMsg(v)
}
}
// SetAuthenReplyData sets an AuthenData
func SetAuthenReplyData(v AuthenData) AuthenReplyOption {
return func(a *AuthenReply) {
a.Data = v
}
}
// NewAuthenReply will create a new AuthenReply based on the provided options
func NewAuthenReply(opts ...AuthenReplyOption) *AuthenReply {
a := &AuthenReply{}
for _, opt := range opts {
opt(a)
}
return a
}
// AuthenReply https://datatracker.ietf.org/doc/html/rfc8907#section-5.2
type AuthenReply struct {
Status AuthenStatus
Flags AuthenReplyFlag
ServerMsg AuthenServerMsg
Data AuthenData
}
// Validate all fields on this type
func (a *AuthenReply) Validate() error {
// validate
for _, t := range []Field{a.Status} {
if err := t.Validate(nil); err != nil {
return err
}
}
return nil
}
// MarshalBinary encodes AuthenReply to tacacs bytes
func (a *AuthenReply) MarshalBinary() ([]byte, error) {
// validate
if err := a.Validate(); err != nil {
return nil, err
}
buf := make([]byte, 0, AuthenReplyLen)
buf = append(buf, uint8(a.Status))
buf = append(buf, uint8(a.Flags))
buf = appendUint16(buf, a.ServerMsg.Len())
buf = appendUint16(buf, a.Data.Len())
buf = append(buf, a.ServerMsg...)
buf = append(buf, a.Data...)
return buf, nil
}
// UnmarshalBinary decodes decrypted tacacs bytes to AuthenReply
func (a *AuthenReply) UnmarshalBinary(data []byte) error {
if len(data) < AuthenReplyLen {
return fmt.Errorf("authenReply size [%v] is too small for the minimum size [%v]", len(data), AuthenReplyLen)
}
a.Status = AuthenStatus(data[0])
a.Flags = AuthenReplyFlag(data[1])
buf := readBuffer(data[2:])
serverMsgLen := buf.uint16()
dataLen := buf.uint16()
a.ServerMsg = AuthenServerMsg(buf.string(serverMsgLen))
a.Data = AuthenData(buf.string(dataLen))
// detect secret mismatch
if a.Len() != serverMsgLen+dataLen {
return NewBadSecretErr("bad secret detected authenreply")
}
// validate
if err := a.Validate(); err != nil {
return err
}
return nil
}
// Len will return the unmarshalled size of the component types
func (a AuthenReply) Len() int {
sum := a.ServerMsg.Len()
sum += a.Data.Len()
return sum
}
// Fields returns fields from this packet compatible with a structured logger
func (a AuthenReply) Fields() map[string]string {
return map[string]string{
"packet-type": "AuthenReply",
"status": a.Status.String(),
"flags": a.Flags.String(),
"server-msg": a.ServerMsg.String(),
"data": a.Data.String(),
}
}