forked from wheelcomplex/kmg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vsa.go
348 lines (318 loc) · 8.67 KB
/
vsa.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
package kmgRadius
import (
"crypto"
_ "crypto/md5"
"crypto/rand"
"encoding/binary"
"fmt"
"reflect"
)
//Vendor Specific Attributes
type VSA interface {
GetType() VendorType
String() string
Encode() (b []byte, err error)
}
// the data is the data from avp
func vsaDecode(p *Packet, data []byte) (vsa VSA, err error) {
if len(data) < 4 {
return nil, fmt.Errorf("[vsaDecode] len(data)[%d]<4", len(data))
}
vendorId := VendorId(binary.BigEndian.Uint32(data[:4]))
switch vendorId {
case VendorIdMicrosoft:
if len(data) < 5 {
return nil, fmt.Errorf("[vsaDecode] len(data)[%d]<5", len(data))
}
vendorType := VendorType(data[4])
decoder := vendorType.decoder()
return decoder(p, vendorType, data[6:])
default:
return nil, fmt.Errorf("[vsaDecode] not implement vendorid:%d", vendorId)
}
}
type VendorId uint32
const (
VendorIdMicrosoft = 311
)
func (a VendorId) String() string {
switch a {
case VendorIdMicrosoft:
return "Microsoft"
default:
return fmt.Sprintf("unknow VendorId:%d", a)
}
}
type VendorType uint8 //微软的?
const (
VendorTypeMSMPPEEncryptionPolicy VendorType = 7
VendorTypeMSMPPEEncryptionTypes VendorType = 8
VendorTypeMSMPPESendKey VendorType = 16
VendorTypeMSMPPERecvKey VendorType = 17
)
func (a VendorType) String() string {
switch a {
case VendorTypeMSMPPEEncryptionPolicy:
return "MSMPPEEncryptionPolicy"
case VendorTypeMSMPPEEncryptionTypes:
return "MSMPPEEncryptionTypes"
case VendorTypeMSMPPESendKey:
return "MSMPPESendKey"
case VendorTypeMSMPPERecvKey:
return "MSMPPERecvKey"
default:
return fmt.Sprintf("unknow VendorType:%d", a)
}
}
func (a VendorType) decoder() func(p *Packet, typ VendorType, data []byte) (avp VSA, err error) {
switch a {
case VendorTypeMSMPPEEncryptionPolicy:
return vsaUint32Enum(MSMPPEEncryptionPolicy(0))
case VendorTypeMSMPPEEncryptionTypes:
return vsaUint32Enum(MSMPPEEncryptionTypes(0))
case VendorTypeMSMPPESendKey, VendorTypeMSMPPERecvKey:
return vsaSendOrRecvKey
default:
return vsaBinary
}
}
func (a VendorType) VendorId() VendorId {
return VendorIdMicrosoft
}
func vsaEncodeWithByteSlice(typ VendorType, data []byte) (b []byte, err error) {
if len(data) > 255-2 {
return nil, fmt.Errorf("[encodeWithByteSlice] data length %d overflow(should less than 253)", len(data))
}
length := len(data) + 6
b = make([]byte, length)
binary.BigEndian.PutUint32(b[:4], uint32(typ.VendorId()))
b[4] = byte(typ)
b[5] = byte(len(data) + 2)
copy(b[6:], data)
return b, nil
}
func vsaBinary(p *Packet, typ VendorType, data []byte) (avp VSA, err error) {
return &BinaryVSA{
Type: typ,
Value: data,
}, nil
}
type BinaryVSA struct {
Type VendorType
Value []byte
}
func (a *BinaryVSA) GetType() VendorType {
return a.Type
}
func (a *BinaryVSA) String() string {
return fmt.Sprintf("Type: %s Value: %#v", a.Type, a.Value)
}
func (a *BinaryVSA) Encode() (b []byte, err error) {
if len(a.Value) > 253 {
return nil, fmt.Errorf("[BinaryAVP.Encode] len(a.Value)[%d]>253", len(a.Value))
}
return vsaEncodeWithByteSlice(a.Type, a.Value)
}
func (a *BinaryVSA) GetValue() interface{} {
return a.Value
}
// t should from a uint32 type like AcctStatusTypeEnum
func vsaUint32Enum(t Stringer) func(p *Packet, typ VendorType, data []byte) (avp VSA, err error) {
return func(p *Packet, typ VendorType, data []byte) (avp VSA, err error) {
if len(data) != 4 {
return nil, fmt.Errorf("[vsaUint32Enum] len(data)[%d]!=4", len(data))
}
value := reflect.New(reflect.TypeOf(t)).Elem()
value.SetUint(uint64(binary.BigEndian.Uint32(data)))
valueI := value.Interface().(Stringer)
return &Uint32EnumVSA{
Type: typ,
Value: valueI,
}, nil
}
}
type Uint32EnumVSA struct {
Type VendorType
Value Stringer // value should derive from a uint32 type like AcctStatusTypeEnum
}
func (a *Uint32EnumVSA) GetType() VendorType {
return a.Type
}
func (a *Uint32EnumVSA) String() string {
return fmt.Sprintf("Type: %s Value: %s", a.GetType(), a.Value)
}
func (a *Uint32EnumVSA) Encode() (b []byte, err error) {
b = make([]byte, 4)
value := reflect.ValueOf(a.Value)
out := value.Uint()
if out >= (1 << 32) {
panic("[Uint32EnumAVP.Encode] enum number overflow")
}
binary.BigEndian.PutUint32(b, uint32(out))
return vsaEncodeWithByteSlice(a.Type, b)
}
func (a *Uint32EnumVSA) Copy() VSA {
return &Uint32EnumVSA{
Type: a.Type,
Value: a.Value,
}
}
func (a *Uint32EnumVSA) GetValue() interface{} {
return a.Value
}
func vsaSendOrRecvKey(p *Packet, typ VendorType, data []byte) (avp VSA, err error) {
key, salt, err := msMPPEKeyDecode(p, data)
if err != nil {
return nil, err
}
return &MSMPPESendOrRecvKeyVSA{
packet: p,
Type: typ,
Salt: salt,
Key: key,
}, nil
}
//send or recv key
type MSMPPESendOrRecvKeyVSA struct {
packet *Packet
Type VendorType
Salt [2]byte //最高位要是1
Key []byte
}
func (a *MSMPPESendOrRecvKeyVSA) SetPacket(p *Packet) {
a.packet = p
}
func (a *MSMPPESendOrRecvKeyVSA) GetType() VendorType {
return a.Type
}
func (a *MSMPPESendOrRecvKeyVSA) String() string {
return fmt.Sprintf("Type: %s Salt: %#v Key: %#v", a.GetType(), a.Salt, a.Key)
}
func (a *MSMPPESendOrRecvKeyVSA) Encode() (b []byte, err error) {
b, err = msMPPEKeyEncode(a.packet, a.Salt, a.Key)
if err != nil {
return nil, err
}
return vsaEncodeWithByteSlice(a.Type, b)
}
func (a *MSMPPESendOrRecvKeyVSA) GetValue() interface{} {
return a.Key
}
//随机一个新的 MSMPPESendOrRecvKeyVSA 的对象
func NewMSMPPESendOrRecvKeyVSA(p *Packet, typ VendorType, key []byte) *MSMPPESendOrRecvKeyVSA {
salt := [2]byte{}
_, err := rand.Read(salt[:])
if err != nil {
panic(err)
}
salt[0] = salt[0] | 0x80 //最高位要是1
vsa := &MSMPPESendOrRecvKeyVSA{
packet: p,
Type: typ,
Salt: salt,
Key: key,
}
return vsa
}
type MSMPPEEncryptionPolicy uint32
const (
MSMPPEEncryptionPolicyEncryptionAllowed MSMPPEEncryptionPolicy = 1
MSMPPEEncryptionPolicyEncryptionRequired MSMPPEEncryptionPolicy = 2
)
func (a MSMPPEEncryptionPolicy) String() string {
switch a {
case MSMPPEEncryptionPolicyEncryptionAllowed:
return "Allowed"
case MSMPPEEncryptionPolicyEncryptionRequired:
return "Required"
default:
return fmt.Sprintf("unknow MSMPPEEncryptionPolicy:%d", a)
}
}
type MSMPPEEncryptionTypes uint32
const (
MSMPPEEncryptionTypesRC4Bit40 MSMPPEEncryptionTypes = 2
MSMPPEEncryptionTypesRC4Bit128 MSMPPEEncryptionTypes = 4
MSMPPEEncryptionTypesRC4Bit40Or128 MSMPPEEncryptionTypes = 6
)
func (a MSMPPEEncryptionTypes) String() string {
switch a {
case MSMPPEEncryptionTypesRC4Bit40:
return "RC4Bit40"
case MSMPPEEncryptionTypesRC4Bit128:
return "RC4Bit128"
case MSMPPEEncryptionTypesRC4Bit40Or128:
return "RC4Bit40Or128"
default:
return fmt.Sprintf("unknow MSMPPEEncryptionTypes:%d", a)
}
}
//内有随机
func msMPPEKeyEncode(p *Packet, salt [2]byte, inData []byte) (out []byte, err error) {
paddingSize := 16 - (len(inData)+1)%16
if paddingSize == 16 {
paddingSize = 0
}
paddingedData := make([]byte, len(inData)+1+paddingSize)
if len(inData) > 255 {
return nil, fmt.Errorf("[msMPPEKeyEncode] length overflow len(inData)[%d]>255", len(inData))
}
paddingedData[0] = byte(len(inData))
copy(paddingedData[1:], inData)
out = make([]byte, len(paddingedData)+2)
copy(out[:2], salt[:])
h := crypto.MD5.New()
h.Write(p.Secret)
h.Write(p.Authenticator[:])
h.Write(out[:2])
b := h.Sum(nil)
blockSize := len(paddingedData) / 16
for i := 0; i < blockSize; i++ {
thisC := out[2+i*16 : 2+i*16+16]
thisP := paddingedData[i*16 : i*16+16]
xor(b, thisP, thisC)
//最后一次不用hash了
h := crypto.MD5.New()
h.Write(p.Secret)
h.Write(thisC)
b = h.Sum(nil)
}
return out, nil
}
func msMPPEKeyDecode(p *Packet, inData []byte) (out []byte, salt [2]byte, err error) {
if len(inData) < 18 {
return nil, salt, fmt.Errorf("[msMPPEKeyDecode] len(inData)[%d]<18", len(inData))
}
if (len(inData)-2)%16 != 0 {
return nil, salt, fmt.Errorf("[msMPPEKeyDecode] (len(inData)[%d]-2)%%16!=0", len(inData))
}
salt = [2]byte{}
copy(salt[:], inData[:2])
h := crypto.MD5.New()
h.Write(p.Secret)
h.Write(p.Authenticator[:])
h.Write(salt[:])
b := h.Sum(nil)
blockSize := (len(inData) - 2) / 16
out = make([]byte, len(inData)-2)
for i := 0; i < blockSize; i++ {
thisC := inData[2+i*16 : 2+i*16+16]
thisP := out[i*16 : i*16+16]
xor(b, thisC, thisP)
//最后一次不用hash了
h := crypto.MD5.New()
h.Write(p.Secret)
h.Write(thisC)
b = h.Sum(nil)
}
//fmt.Printf("%#v\n%#v\n%#v\n", salt, inData, out)
if int(out[0]) > len(inData)-3 {
return nil, salt, fmt.Errorf("[msMPPEKeyDecode] out[0][%d]>len(inData)[%d]-3", out[0], len(inData))
}
return out[1 : out[0]+1], salt, nil
}
func xor(inA []byte, inB []byte, out []byte) {
for i := 0; i < len(inA); i++ {
out[i] = inA[i] ^ inB[i]
}
}