From 7e73a9448bc398beb25bc19e488624ac17151c71 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sat, 14 Nov 2026 23:09:45 +0800 Subject: [PATCH] fix: ShadowTLS v2 not work with X25519MLKEM768 The v2 server authenticates the client only if its HMAC covers all the data relayed from the handshake server, or all but the last write. The client hashed only the bytes the TLS stack asked for, which end at the server Finished record, so session tickets sent in the same flight were left out whenever the stack's last read did not happen to pick them up. With an X25519MLKEM768 ServerHello this happened on every connection to a Go crypto/tls handshake server, and the chrome fingerprint always fell back. Read the socket through a 16 KiB buffer and hash everything each read returns, as the original implementation does. --- transport/shadowtls/client.go | 5 ----- transport/shadowtls/server_test.go | 1 + transport/shadowtls/v2.go | 14 +++++++------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/transport/shadowtls/client.go b/transport/shadowtls/client.go index 43d8b116..760f699e 100644 --- a/transport/shadowtls/client.go +++ b/transport/shadowtls/client.go @@ -190,11 +190,6 @@ func uTLSHandshakeFunc(config *tls.Config, clientFingerprint string, version int return err } } - if version == 2 { // ShadowTLS v2 not work with X25519MLKEM768 - if err := tlsC.BuildRemovedX25519MLKEM768HandshakeState(tlsConn); err != nil { - return err - } - } return tlsConn.HandshakeContext(ctx) } return tlsC.Client(conn, tlsConfig).HandshakeContext(ctx) diff --git a/transport/shadowtls/server_test.go b/transport/shadowtls/server_test.go index 28c1471d..3bb76b64 100644 --- a/transport/shadowtls/server_test.go +++ b/transport/shadowtls/server_test.go @@ -32,6 +32,7 @@ func TestServer(t *testing.T) { }{ {name: "v1", version: 1}, {name: "v2", version: 2}, + {name: "v2-utls", version: 2, fingerprint: "chrome"}, {name: "v2-utls-websocket", version: 2, fingerprint: "chrome", alpn: WsALPN}, {name: "v3", version: 3}, {name: "v3-utls", version: 3, fingerprint: "chrome"}, diff --git a/transport/shadowtls/v2.go b/transport/shadowtls/v2.go index 7b8fd3cc..e7417ae3 100644 --- a/transport/shadowtls/v2.go +++ b/transport/shadowtls/v2.go @@ -1,6 +1,7 @@ package shadowtls import ( + "bufio" "bytes" "crypto/hmac" "crypto/sha1" @@ -17,19 +18,18 @@ import ( type hashReadConn struct { net.Conn - hmac hash.Hash + reader *bufio.Reader + hmac hash.Hash } func newHashReadConn(conn net.Conn, password string) *hashReadConn { - return &hashReadConn{Conn: conn, hmac: hmac.New(sha1.New, []byte(password))} + readHMAC := hmac.New(sha1.New, []byte(password)) + reader := bufio.NewReaderSize(io.TeeReader(conn, readHMAC), 16*1024) + return &hashReadConn{Conn: conn, reader: reader, hmac: readHMAC} } func (c *hashReadConn) Read(p []byte) (int, error) { - n, err := c.Conn.Read(p) - if n > 0 { - _, _ = c.hmac.Write(p[:n]) - } - return n, err + return c.reader.Read(p) } func (c *hashReadConn) Sum() []byte {