package faketls

import (
	
	
	
	

	
)

// readServerHello reads faketls ServerHello.
func ( io.Reader,  [32]byte,  []byte) error {
	 := bytes.NewBuffer(nil)
	 = io.TeeReader(, )

	,  := readRecord()
	if  != nil {
		return errors.Wrap(, "handshake record")
	}
	if .Type != RecordTypeHandshake {
		return errors.Wrap(, "unexpected record type")
	}

	,  := readRecord()
	if  != nil {
		return errors.Wrap(, "change cipher record")
	}
	if .Type != RecordTypeChangeCipherSpec {
		return errors.Wrap(, "unexpected record type")
	}

	,  := readRecord()
	if  != nil {
		return errors.Wrap(, "cert record")
	}
	if .Type != RecordTypeApplication {
		return errors.Wrap(, "unexpected record type")
	}

	// `$record_header = type 1 byte + version 2 bytes + payload_length 2 bytes = 5 bytes`
	// `$server_hello_header = type 1 bytes + version 2 bytes + length 3 bytes = 6 bytes`
	// `$offset = $record_header + $server_hello_header = 11 bytes`
	const  = 11
	 := .Bytes()
	// Copy original digest.
	var  [32]byte
	copy([:], [:+32])
	// Fill original digest by zeros.
	var  [32]byte
	copy([:+32], [:])

	 := hmac.New(sha256.New, )
	if ,  := .Write([:]);  != nil {
		return errors.Wrap(, "hmac write")
	}
	if ,  := .Write();  != nil {
		return errors.Wrap(, "hmac write")
	}
	if !bytes.Equal(.Sum(nil), [:]) {
		return errors.New("hmac digest mismatch")
	}

	return nil
}