package faketls
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"io"
"github.com/go-faster/errors"
)
func readServerHello (r io .Reader , clientRandom [32 ]byte , secret []byte ) error {
packetBuf := bytes .NewBuffer (nil )
r = io .TeeReader (r , packetBuf )
handshake , err := readRecord (r )
if err != nil {
return errors .Wrap (err , "handshake record" )
}
if handshake .Type != RecordTypeHandshake {
return errors .Wrap (err , "unexpected record type" )
}
changeCipher , err := readRecord (r )
if err != nil {
return errors .Wrap (err , "change cipher record" )
}
if changeCipher .Type != RecordTypeChangeCipherSpec {
return errors .Wrap (err , "unexpected record type" )
}
cert , err := readRecord (r )
if err != nil {
return errors .Wrap (err , "cert record" )
}
if cert .Type != RecordTypeApplication {
return errors .Wrap (err , "unexpected record type" )
}
const serverRandomOffset = 11
packet := packetBuf .Bytes ()
var originalDigest [32 ]byte
copy (originalDigest [:], packet [serverRandomOffset :serverRandomOffset +32 ])
var zeros [32 ]byte
copy (packet [serverRandomOffset :serverRandomOffset +32 ], zeros [:])
mac := hmac .New (sha256 .New , secret )
if _ , err := mac .Write (clientRandom [:]); err != nil {
return errors .Wrap (err , "hmac write" )
}
if _ , err := mac .Write (packet ); err != nil {
return errors .Wrap (err , "hmac write" )
}
if !bytes .Equal (mac .Sum (nil ), originalDigest [:]) {
return errors .New ("hmac digest mismatch" )
}
return nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .