Files
mautrix-telegram/pkg/gotd/mtproxy/faketls/server_hello.go
T
Igor Artamonov aab48f0dbe
Go / Lint (old) (push) Failing after 4m43s
Go / Lint (latest) (push) Failing after 4m40s
Go / Lint (old) (pull_request) Failing after 4m40s
Go / Lint (latest) (pull_request) Failing after 4m43s
faketls: include real record type and peek bytes on handshake errors
The previous error path used errors.Wrap(err, "unexpected record type")
inside type-mismatch branches where err was already nil. With
go-faster/errors that produced a wrapError with no cause and no detail,
making the user-visible message "unexpected record type" useless for
diagnostics — there was no way to tell what mtg actually sent.

Switch to errors.Errorf with the actual received byte and a 32-byte
hex peek of the read buffer. Also wrap the read-error path with the
same peek so a partial response is visible.

This is a diagnostic-only change; the parser semantics are unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 10:51:50 +03:00

78 lines
2.3 KiB
Go

package faketls
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"io"
"github.com/go-faster/errors"
)
// peekDump returns up to n bytes from the start of buf as a hex string for diagnostics.
func peekDump(buf []byte, n int) string {
if len(buf) < n {
n = len(buf)
}
return hex.EncodeToString(buf[:n])
}
// readServerHello reads faketls ServerHello.
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.Wrapf(err, "handshake record (peek=%s)", peekDump(packetBuf.Bytes(), 32))
}
if handshake.Type != RecordTypeHandshake {
return errors.Errorf("unexpected handshake record type: got 0x%02x, want 0x%02x (peek=%s)",
byte(handshake.Type), byte(RecordTypeHandshake), peekDump(packetBuf.Bytes(), 32))
}
changeCipher, err := readRecord(r)
if err != nil {
return errors.Wrap(err, "change cipher record")
}
if changeCipher.Type != RecordTypeChangeCipherSpec {
return errors.Errorf("unexpected change cipher record type: got 0x%02x, want 0x%02x",
byte(changeCipher.Type), byte(RecordTypeChangeCipherSpec))
}
cert, err := readRecord(r)
if err != nil {
return errors.Wrap(err, "cert record")
}
if cert.Type != RecordTypeApplication {
return errors.Errorf("unexpected application record type: got 0x%02x, want 0x%02x",
byte(cert.Type), byte(RecordTypeApplication))
}
// `$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 serverRandomOffset = 11
packet := packetBuf.Bytes()
// Copy original digest.
var originalDigest [32]byte
copy(originalDigest[:], packet[serverRandomOffset:serverRandomOffset+32])
// Fill original digest by zeros.
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
}