63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package peap
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
type AVPType uint16
|
|
|
|
const (
|
|
AVPAckResult AVPType = 3
|
|
)
|
|
|
|
const ExtensionHeaderSize = 4
|
|
|
|
type ExtensionAVP struct {
|
|
Mandatory bool
|
|
Type AVPType // 14-bit field
|
|
Length uint16
|
|
Value []byte
|
|
}
|
|
|
|
var (
|
|
ErrorReservedBitSet = errors.New("PEAP-Extension: Reserved bit is not 0")
|
|
)
|
|
|
|
func (eavp *ExtensionAVP) Decode(raw []byte) error {
|
|
typ := binary.BigEndian.Uint16(raw[:2])
|
|
if typ>>15 == 1 {
|
|
eavp.Mandatory = true
|
|
}
|
|
if typ>>14&1 != 0 {
|
|
return ErrorReservedBitSet
|
|
}
|
|
eavp.Type = AVPType(typ & 0b0011111111111111)
|
|
eavp.Length = binary.BigEndian.Uint16(raw[2:4])
|
|
val := raw[4:]
|
|
if eavp.Length != uint16(len(val)) {
|
|
return fmt.Errorf("PEAP-Extension: Invalid length: %d, should be %d", eavp.Length, len(val))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (eavp ExtensionAVP) Encode() []byte {
|
|
buff := []byte{
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
}
|
|
t := uint16(eavp.Type)
|
|
// Type is a 14-bit number, the highest bit is the mandatory flag
|
|
if eavp.Mandatory {
|
|
t = t | 0b1000000000000000
|
|
}
|
|
// The next bit is reserved and should always be set to 0
|
|
t = t & 0b1011111111111111
|
|
binary.BigEndian.PutUint16(buff[0:], t)
|
|
binary.BigEndian.PutUint16(buff[2:], uint16(len(eavp.Value)))
|
|
return append(buff, eavp.Value...)
|
|
}
|