-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add NewHeader & NewMessage functions
- Loading branch information
Showing
7 changed files
with
339 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package message | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// Flags | ||
const ( | ||
ResponseBitCheck = 0x20 | ||
VersionBitCheck = 0x10 | ||
InitiatorBitCheck = 0x08 | ||
) | ||
|
||
const IKE_HEADER_LEN int = 28 | ||
|
||
type IKEHeader struct { | ||
InitiatorSPI uint64 | ||
ResponderSPI uint64 | ||
MajorVersion uint8 | ||
MinorVersion uint8 | ||
ExchangeType uint8 | ||
Flags uint8 | ||
MessageID uint32 | ||
NextPayload uint8 | ||
PayloadBytes []byte | ||
} | ||
|
||
func NewHeader( | ||
iSPI, rSPI uint64, exchgType uint8, | ||
response, initiator bool, mId uint32, | ||
nextPayload uint8, payloadBytes []byte, | ||
) *IKEHeader { | ||
h := &IKEHeader{ | ||
InitiatorSPI: iSPI, | ||
ResponderSPI: rSPI, | ||
ExchangeType: exchgType, | ||
MajorVersion: 2, | ||
MinorVersion: 0, | ||
MessageID: mId, | ||
NextPayload: nextPayload, | ||
PayloadBytes: payloadBytes, | ||
} | ||
if response { | ||
h.Flags |= ResponseBitCheck | ||
} | ||
if initiator { | ||
h.Flags |= InitiatorBitCheck | ||
} | ||
return h | ||
} | ||
|
||
func (h *IKEHeader) Marshal() ([]byte, error) { | ||
b := make([]byte, IKE_HEADER_LEN) | ||
|
||
binary.BigEndian.PutUint64(b[0:8], h.InitiatorSPI) | ||
binary.BigEndian.PutUint64(b[8:16], h.ResponderSPI) | ||
b[16] = h.NextPayload | ||
b[17] = (h.MajorVersion << 4) | (h.MinorVersion & 0x0F) | ||
b[18] = h.ExchangeType | ||
b[19] = h.Flags | ||
binary.BigEndian.PutUint32(b[20:24], h.MessageID) | ||
|
||
totalLen := IKE_HEADER_LEN + len(h.PayloadBytes) | ||
if totalLen > 0xFFFFFFFF { | ||
return nil, errors.Errorf("length exceeds uint32 limit: %d", totalLen) | ||
} | ||
|
||
binary.BigEndian.PutUint32(b[24:IKE_HEADER_LEN], uint32(totalLen)) | ||
if len(h.PayloadBytes) > 0 { | ||
b = append(b, h.PayloadBytes...) | ||
} | ||
return b, nil | ||
} | ||
|
||
func (h *IKEHeader) IsResponse() bool { | ||
return (h.Flags & ResponseBitCheck) != 0 | ||
} | ||
|
||
func (h *IKEHeader) IsInitiator() bool { | ||
return (h.Flags & InitiatorBitCheck) != 0 | ||
} | ||
|
||
func ParseHeader(b []byte) (*IKEHeader, error) { | ||
// IKE message packet format this implementation referenced is | ||
// defined in RFC 7296, Section 3.1 | ||
// bounds checking | ||
if len(b) < IKE_HEADER_LEN { | ||
return nil, errors.Errorf("ParseHeader(): Received broken IKE header") | ||
} | ||
|
||
totalLen := binary.BigEndian.Uint32(b[24:IKE_HEADER_LEN]) | ||
if totalLen < uint32(IKE_HEADER_LEN) { | ||
return nil, errors.Errorf("ParseHeader(): Illegal IKE message length %d < header length %d", | ||
totalLen, IKE_HEADER_LEN) | ||
} | ||
|
||
h := &IKEHeader{ | ||
InitiatorSPI: binary.BigEndian.Uint64(b[:8]), | ||
ResponderSPI: binary.BigEndian.Uint64(b[8:16]), | ||
NextPayload: b[16], | ||
MajorVersion: b[17] >> 4, | ||
MinorVersion: b[17] & 0x0F, | ||
ExchangeType: b[18], | ||
Flags: b[19], | ||
MessageID: binary.BigEndian.Uint32(b[20:24]), | ||
PayloadBytes: b[IKE_HEADER_LEN:], | ||
} | ||
|
||
return h, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package message | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewAndMarshalIKEHeader(t *testing.T) { | ||
ikeHdr := NewHeader( | ||
0x000000000006f708, 0xc9e2e31f8b64053d, IKE_AUTH, | ||
false, true, 0x03, uint8(NoNext), nil, | ||
) | ||
require.Equal(t, &IKEHeader{ | ||
InitiatorSPI: 0x000000000006f708, | ||
ResponderSPI: 0xc9e2e31f8b64053d, | ||
MajorVersion: 2, | ||
MinorVersion: 0, | ||
ExchangeType: IKE_AUTH, | ||
Flags: InitiatorBitCheck, | ||
MessageID: 0x03, | ||
NextPayload: uint8(NoNext), | ||
}, ikeHdr) | ||
require.True(t, ikeHdr.IsInitiator()) | ||
require.False(t, ikeHdr.IsResponse()) | ||
|
||
b, err := ikeHdr.Marshal() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte{ | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xf7, 0x08, | ||
0xc9, 0xe2, 0xe3, 0x1f, 0x8b, 0x64, 0x05, 0x3d, | ||
0x00, 0x20, 0x23, 0x08, 0x00, 0x00, 0x00, 0x03, | ||
0x00, 0x00, 0x00, 0x1c, | ||
}, b) | ||
} | ||
|
||
func TestParseIKEHeader(t *testing.T) { | ||
ikeHdr, err := ParseHeader( | ||
[]byte{ | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xf7, 0x08, | ||
0xc9, 0xe2, 0xe3, 0x1f, 0x8b, 0x64, 0x05, 0x3d, | ||
0x00, 0x20, 0x23, 0x08, 0x00, 0x00, 0x00, 0x03, | ||
0x00, 0x00, 0x00, 0x1c, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, &IKEHeader{ | ||
InitiatorSPI: 0x000000000006f708, | ||
ResponderSPI: 0xc9e2e31f8b64053d, | ||
MajorVersion: 2, | ||
MinorVersion: 0, | ||
ExchangeType: IKE_AUTH, | ||
Flags: InitiatorBitCheck, | ||
MessageID: 0x03, | ||
NextPayload: uint8(NoNext), | ||
PayloadBytes: []byte{}, | ||
}, ikeHdr) | ||
require.True(t, ikeHdr.IsInitiator()) | ||
require.False(t, ikeHdr.IsResponse()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.