Skip to content

Commit

Permalink
r6
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiSubira committed Jul 19, 2022
1 parent 39c6505 commit 2c0d915
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 62 deletions.
94 changes: 47 additions & 47 deletions pkg/slayers/extn.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,64 +450,64 @@ func (e *EndToEndExtnSkipper) NextLayerType() gopacket.LayerType {
// PacketAuthSPI is the identifier for the key used for the
// packet authentication option. DRKey values are in the
// range [1, 2^21-1].
type PacketAuthenticatorSPI uint32
type PacketAuthSPI uint32

type PacketAuthenticatorDRKeyType uint8
type PacketAuthDRKeyType uint8

const (
ASHost PacketAuthenticatorDRKeyType = iota
HostHost
PacketAuthASHost PacketAuthDRKeyType = iota
PacketAuthHostHost
)

type PacketAuthenticatorDRKeyDirection uint8
type PacketAuthDRKeyDirection uint8

const (
SenderSide PacketAuthenticatorDRKeyDirection = iota
ReceiverSide
PacketAuthSenderSide PacketAuthDRKeyDirection = iota
PacketAuthReceiverSide
)

type PacketAuthenticatorDRKeyEpochType uint8
type PacketAuthDRKeyEpochType uint8

const (
Later PacketAuthenticatorDRKeyEpochType = iota
Earlier
PacketAuthLater PacketAuthDRKeyEpochType = iota
PacketAuthEarlier
)

func (p PacketAuthenticatorSPI) Type() PacketAuthenticatorDRKeyType {
func (p PacketAuthSPI) Type() PacketAuthDRKeyType {
if p&(1<<18) == 0 {
return ASHost
return PacketAuthASHost
}
return HostHost
return PacketAuthHostHost
}

func (p PacketAuthenticatorSPI) Direction() PacketAuthenticatorDRKeyDirection {
func (p PacketAuthSPI) Direction() PacketAuthDRKeyDirection {
if p&(1<<17) == 0 {
return SenderSide
return PacketAuthSenderSide
}
return ReceiverSide
return PacketAuthReceiverSide
}

func (p PacketAuthenticatorSPI) Epoch() PacketAuthenticatorDRKeyEpochType {
func (p PacketAuthSPI) Epoch() PacketAuthDRKeyEpochType {
if p&(1<<16) == 0 {
return Later
return PacketAuthLater
}
return Earlier
return PacketAuthEarlier
}

func (p PacketAuthenticatorSPI) DRKeyProto() uint16 {
func (p PacketAuthSPI) DRKeyProto() uint16 {
return uint16(p)
}

func (p PacketAuthenticatorSPI) IsDRKey() bool {
func (p PacketAuthSPI) IsDRKey() bool {
return p > 0 && p < (1<<21)
}

func MakePacketAuthSPIDrkey(
func MakePacketAuthSPIDRKey(
proto uint16,
drkeyType PacketAuthenticatorDRKeyType,
dir PacketAuthenticatorDRKeyDirection,
epoch PacketAuthenticatorDRKeyEpochType,
) PacketAuthenticatorSPI {
drkeyType PacketAuthDRKeyType,
dir PacketAuthDRKeyDirection,
epoch PacketAuthDRKeyEpochType,
) PacketAuthSPI {

if proto < 1 {
panic("Invalid proto identifier value")
Expand All @@ -526,7 +526,7 @@ func MakePacketAuthSPIDrkey(
spi |= uint32((epoch & 0x1)) << 16
spi |= uint32(proto)

return PacketAuthenticatorSPI(spi)
return PacketAuthSPI(spi)
}

// PacketAuthAlg is the enumerator for authenticator algorithm types in the
Expand All @@ -538,49 +538,49 @@ const (
PacketAuthSHA1_AES_CBC
)

// PacketAuthenticatorOption wraps an EndToEndOption of OptTypeAuthenticator.
// PacketAuthOption wraps an EndToEndOption of OptTypeAuthenticator.
// This can be used to serialize and parse the internal structure of the packet authenticator
// option.
type PacketAuthenticatorOption struct {
type PacketAuthOption struct {
*EndToEndOption
}

// NewPacketAuthenticatorOption creates a new EndToEndOption of
// NewPacketAuthOption creates a new EndToEndOption of
// OptTypeAuthenticator, initialized with the given SPAO data.
func NewPacketAuthenticatorOption(
spi PacketAuthenticatorSPI,
func NewPacketAuthOption(
spi PacketAuthSPI,
alg PacketAuthAlg,
ts uint32,
sn uint32,
auth []byte,
) PacketAuthenticatorOption {
) PacketAuthOption {

o := PacketAuthenticatorOption{EndToEndOption: new(EndToEndOption)}
o := PacketAuthOption{EndToEndOption: new(EndToEndOption)}
o.Reset(spi, alg, ts, sn, auth)
return o
}

// ParsePacketAuthenticatorOption parses o as a packet authenticator option.
// ParsePacketAuthOption parses o as a packet authenticator option.
// Performs minimal checks to ensure that SPI, algorithm, timestamp, RSV, and
// sequence number are set.
// Checking the size and content of the Authenticator data must be done by the
// caller.
func ParsePacketAuthenticatorOption(o *EndToEndOption) (PacketAuthenticatorOption, error) {
func ParsePacketAuthOption(o *EndToEndOption) (PacketAuthOption, error) {
if o.OptType != OptTypeAuthenticator {
return PacketAuthenticatorOption{},
return PacketAuthOption{},
serrors.New("wrong option type", "expected", OptTypeAuthenticator, "actual", o.OptType)
}
if len(o.OptData) < 12 {
return PacketAuthenticatorOption{},
return PacketAuthOption{},
serrors.New("buffer too short", "expected", 12, "actual", len(o.OptData))
}
return PacketAuthenticatorOption{o}, nil
return PacketAuthOption{o}, nil
}

// Reset reinitializes the underlying EndToEndOption with the SPAO data.
// Reuses the OptData buffer if it is of sufficient capacity.
func (o PacketAuthenticatorOption) Reset(
spi PacketAuthenticatorSPI,
func (o PacketAuthOption) Reset(
spi PacketAuthSPI,
alg PacketAuthAlg,
ts uint32,
sn uint32,
Expand Down Expand Up @@ -620,28 +620,28 @@ func (o PacketAuthenticatorOption) Reset(
}

// SPI returns returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) SPI() PacketAuthenticatorSPI {
return PacketAuthenticatorSPI(binary.BigEndian.Uint32(o.OptData[:4]))
func (o PacketAuthOption) SPI() PacketAuthSPI {
return PacketAuthSPI(binary.BigEndian.Uint32(o.OptData[:4]))
}

// Algorithm returns the algorithm type stored in the data buffer.
func (o PacketAuthenticatorOption) Algorithm() PacketAuthAlg {
func (o PacketAuthOption) Algorithm() PacketAuthAlg {
return PacketAuthAlg(o.OptData[4])
}

// Timestamp returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) Timestamp() uint32 {
func (o PacketAuthOption) Timestamp() uint32 {
return uint32(o.OptData[5])<<16 + uint32(o.OptData[6])<<8 + uint32(o.OptData[7])
}

// SequenceNumber returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) SequenceNumber() uint32 {
func (o PacketAuthOption) SequenceNumber() uint32 {
return uint32(o.OptData[9])<<16 + uint32(o.OptData[10])<<8 + uint32(o.OptData[11])
}

// Authenticator returns slice of the underlying auth buffer.
// Changes to this slice will be reflected on the wire when
// the extension is serialized.
func (o PacketAuthenticatorOption) Authenticator() []byte {
func (o PacketAuthOption) Authenticator() []byte {
return o.OptData[12:]
}
35 changes: 20 additions & 15 deletions pkg/slayers/extn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,11 @@ func prepRawPacketWithExtn(t *testing.T, extns ...slayers.L4ProtocolType) []byte
return buf.Bytes()
}

var spi = slayers.MakePacketAuthSPIDrkey(
var spi = slayers.MakePacketAuthSPIDRKey(
1,
slayers.ASHost,
slayers.ReceiverSide,
slayers.Later)
slayers.PacketAuthASHost,
slayers.PacketAuthReceiverSide,
slayers.PacketAuthLater)
var algo = slayers.PacketAuthSHA1_AES_CBC
var ts = binary.LittleEndian.Uint32([]byte{1, 2, 3, 0})
var sn = binary.LittleEndian.Uint32([]byte{4, 5, 6, 0})
Expand Down Expand Up @@ -584,7 +584,7 @@ var rawE2EOptAuth = append(
func TestOptAuthenticatorSerialize(t *testing.T) {
cases := []struct {
name string
spi slayers.PacketAuthenticatorSPI
spi slayers.PacketAuthSPI
algo slayers.PacketAuthAlg
ts uint32
sn uint32
Expand Down Expand Up @@ -623,14 +623,14 @@ func TestOptAuthenticatorSerialize(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
if c.panics {
assert.Panics(t, func() {
slayers.NewPacketAuthenticatorOption(c.spi, c.algo, c.ts, c.sn, c.optAuth)
slayers.NewPacketAuthOption(c.spi, c.algo, c.ts, c.sn, c.optAuth)
},
"The code did not panic",
)
return
}

spao := slayers.NewPacketAuthenticatorOption(c.spi, c.algo, c.ts, c.sn, c.optAuth)
spao := slayers.NewPacketAuthOption(c.spi, c.algo, c.ts, c.sn, c.optAuth)

e2e := slayers.EndToEndExtn{}
e2e.NextHdr = slayers.L4UDP
Expand All @@ -654,12 +654,12 @@ func TestOptAuthenticatorDeserialize(t *testing.T) {
assert.Equal(t, slayers.L4UDP, e2e.NextHdr, "NextHeader")
optAuth, err := e2e.FindOption(slayers.OptTypeAuthenticator)
require.NoError(t, err, "FindOption")
auth, err := slayers.ParsePacketAuthenticatorOption(optAuth)
require.NoError(t, err, "ParsePacketAuthenticatorOption")
auth, err := slayers.ParsePacketAuthOption(optAuth)
require.NoError(t, err, "ParsePacketAuthOption")
assert.Equal(t, spi, auth.SPI(), "SPI")
assert.Equal(t, slayers.ASHost, auth.SPI().Type())
assert.Equal(t, slayers.ReceiverSide, auth.SPI().Direction())
assert.Equal(t, slayers.Later, auth.SPI().Epoch())
assert.Equal(t, slayers.PacketAuthASHost, auth.SPI().Type())
assert.Equal(t, slayers.PacketAuthReceiverSide, auth.SPI().Direction())
assert.Equal(t, slayers.PacketAuthLater, auth.SPI().Epoch())
assert.Equal(t, true, auth.SPI().IsDRKey())
assert.Equal(t, algo, auth.Algorithm(), "Algorithm Type")
assert.Equal(t, ts, auth.Timestamp(), "Timestamp")
Expand All @@ -668,7 +668,12 @@ func TestOptAuthenticatorDeserialize(t *testing.T) {
}

func TestMakePacketAuthSPIDrkey(t *testing.T) {
spi := slayers.MakePacketAuthSPIDrkey(1, slayers.ASHost, slayers.SenderSide, slayers.Later)
spi := slayers.MakePacketAuthSPIDRKey(
1,
slayers.PacketAuthASHost,
slayers.PacketAuthSenderSide,
slayers.PacketAuthLater,
)
assert.EqualValues(t, binary.BigEndian.Uint32([]byte{0, 0, 0, 1}), spi)
}

Expand All @@ -688,6 +693,6 @@ func TestOptAuthenticatorDeserializeCorrupt(t *testing.T) {
assert.NoError(t, e2e.DecodeFromBytes(b.Bytes(), gopacket.NilDecodeFeedback))
optAuth, err := e2e.FindOption(slayers.OptTypeAuthenticator)
require.NoError(t, err, "FindOption")
_, err = slayers.ParsePacketAuthenticatorOption(optAuth)
require.Error(t, err, "ParsePacketAuthenticatorOption should fail")
_, err = slayers.ParsePacketAuthOption(optAuth)
require.Error(t, err, "ParsePacketAuthOption should fail")
}

0 comments on commit 2c0d915

Please sign in to comment.