Can correctly accept faketls messages

This commit is contained in:
9seconds
2021-03-25 16:22:00 +03:00
parent a3c64c1d1e
commit 4a2d1df384
7 changed files with 305 additions and 22 deletions
@@ -0,0 +1,67 @@
package clienthello
import (
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"fmt"
"time"
"github.com/9seconds/mtg/v2/mtglib/internal/faketls/record"
)
type ClientHello struct {
Time time.Time
Digest [RandomLen]byte
SessionID []byte
}
func ParseHandshake(secret, handshake []byte) (ClientHello, error) {
hello := ClientHello{}
if len(handshake) < MinLen {
return hello, fmt.Errorf("lengh of handshake is too small: %d", len(handshake))
}
if handshake[0] != HandshakeTypeClient {
return hello, fmt.Errorf("unknown handshake type %#x", handshake[0])
}
copy(hello.Digest[:], handshake[RandomOffset:])
for i := RandomOffset; i < RandomOffset+RandomLen; i++ {
handshake[i] = 0
}
rec := record.AcquireRecord()
defer record.ReleaseRecord(rec)
rec.Type = record.TypeHandshake
rec.Version = record.Version10
rec.Payload.Write(handshake)
// mac is calculated for the whole record, not only
// for the payload part
mac := hmac.New(sha256.New, secret)
rec.Dump(mac)
computedDigest := mac.Sum(nil)
for i := 0; i < RandomLen; i++ {
computedDigest[i] ^= hello.Digest[i]
}
for i := 0; i < RandomLen-4; i++ {
if computedDigest[i] != 0 {
return hello, ErrBadDigest
}
}
timestamp := int64(binary.LittleEndian.Uint32(computedDigest[RandomLen-4:]))
hello.Time = time.Unix(timestamp, 0)
hello.SessionID = make([]byte, handshake[SessionIDOffset])
copy(hello.SessionID, handshake[SessionIDOffset+1:])
return hello, nil
}
@@ -0,0 +1,17 @@
package clienthello
import "errors"
const (
RandomLen = 32
RandomOffset = 6
SessionIDOffset = RandomOffset + RandomLen
MinLen = SessionIDOffset + 1
HandshakeTypeClient = 0x01
)
var (
ErrBadDigest = errors.New("bad digest")
ErrAntiReplayAttack = errors.New("antireplay attack was detected")
)