Skip to content

Commit

Permalink
Stop using custom conversion function
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeuw committed Jan 13, 2019
1 parent 41da410 commit 735dc64
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
11 changes: 7 additions & 4 deletions internal/server/TLS.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type ClientHello struct {
extensions map[[2]byte][]byte
}

var u16 = binary.BigEndian.Uint16
var u32 = binary.BigEndian.Uint32

func parseExtensions(input []byte) (ret map[[2]byte][]byte, err error) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -37,7 +40,7 @@ func parseExtensions(input []byte) (ret map[[2]byte][]byte, err error) {
var typ [2]byte
copy(typ[:], input[pointer:pointer+2])
pointer += 2
length := util.BtoInt(input[pointer : pointer+2])
length := int(u16(input[pointer : pointer+2]))
pointer += 2
data := input[pointer : pointer+length]
pointer += length
Expand Down Expand Up @@ -81,7 +84,7 @@ func ParseClientHello(data []byte) (ret *ClientHello, err error) {
}
pointer += 1
// Length
length := util.BtoInt(data[pointer : pointer+3])
length := int(u32(append([]byte{0x00}, data[pointer:pointer+3]...)))
pointer += 3
if length != len(data[pointer:]) {
return ret, errors.New("Hello length doesn't match")
Expand All @@ -98,7 +101,7 @@ func ParseClientHello(data []byte) (ret *ClientHello, err error) {
sessionId := data[pointer : pointer+sessionIdLen]
pointer += sessionIdLen
// Cipher Suites
cipherSuitesLen := util.BtoInt(data[pointer : pointer+2])
cipherSuitesLen := int(u16(data[pointer : pointer+2]))
pointer += 2
cipherSuites := data[pointer : pointer+cipherSuitesLen]
pointer += cipherSuitesLen
Expand All @@ -108,7 +111,7 @@ func ParseClientHello(data []byte) (ret *ClientHello, err error) {
compressionMethods := data[pointer : pointer+compressionMethodsLen]
pointer += compressionMethodsLen
// Extensions
extensionsLen := util.BtoInt(data[pointer : pointer+2])
extensionsLen := int(u16(data[pointer : pointer+2]))
pointer += 2
extensions, err := parseExtensions(data[pointer:])
ret = &ClientHello{
Expand Down
15 changes: 1 addition & 14 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ func AESDecrypt(iv []byte, key []byte, ciphertext []byte) []byte {
return ret
}

// BtoInt converts a byte slice into int in Big Endian order
// Uint methods from binary package can be used, but they are messy
func BtoInt(b []byte) int {
var mult uint = 1
var sum uint
length := uint(len(b))
var i uint
for i = 0; i < length; i++ {
sum += uint(b[i]) * (mult << ((length - i - 1) * 8))
}
return int(sum)
}

// PsudoRandBytes returns a byte slice filled with psudorandom bytes generated by the seed
func PsudoRandBytes(length int, seed int64) []byte {
r := prand.New(prand.NewSource(seed))
Expand All @@ -60,7 +47,7 @@ func ReadTLS(conn net.Conn, buffer []byte) (n int, err error) {
return
}

dataLength := BtoInt(buffer[3:5])
dataLength := int(binary.BigEndian.Uint16(buffer[3:5]))
if dataLength > len(buffer) {
err = errors.New("Reading TLS message: message size greater than buffer. message size: " + strconv.Itoa(dataLength))
return
Expand Down

0 comments on commit 735dc64

Please sign in to comment.