Comment: Add Reality section and short desciption

This commit is contained in:
yuhan6665
2026-09-19 14:44:42 -04:00
parent 2487b4c3d5
commit 120fe2ecb8
8 changed files with 41 additions and 3 deletions
+8
View File
@@ -417,7 +417,15 @@ func cipherAES(key, iv []byte, isRead bool) any {
// macSHA1 returns a SHA-1 based constant time MAC.
func macSHA1(key []byte) hash.Hash {
//////////////////////////////////// [REALITY] SECTION: do not use newConstantTimeHash
//h := sha1.New
// The BoringCrypto SHA1 does not have a constant-time
// checksum function, so don't try to use it.
//if !boring.Enabled {
//h = newConstantTimeHash(h)
//}
return hmac.New(sha1.New, key)
//////////////////////////////////// [REALITY] SECTION END
}
// macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
+6
View File
@@ -70,7 +70,9 @@ const (
recordHeaderLen = 5 // record header length
maxHandshake = 65536 // maximum handshake we support (protocol max is 16 MB)
maxHandshakeCertificateMsg = 262144 // maximum certificate message size (256 KiB)
//////////////////////////////////// [REALITY] SECTION: change maxUselessRecords to match with OpenSSL
maxUselessRecords = 32 // maximum number of consecutive non-advancing records
//////////////////////////////////// [REALITY] SECTION END
)
// TLS record types.
@@ -571,6 +573,7 @@ const (
RenegotiateFreelyAsClient
)
//////////////////////////////////// [REALITY] SECTION: define var
type LimitFallback struct {
AfterBytes uint64
BytesPerSec uint64
@@ -600,6 +603,7 @@ type Config struct {
LimitFallbackUpload LimitFallback
LimitFallbackDownload LimitFallback
//////////////////////////////////// [REALITY] SECTION END
// Rand provides the source of entropy for the connection.
// If Rand is nil, TLS uses the cryptographic random reader in package
@@ -1039,6 +1043,7 @@ func (c *Config) Clone() *Config {
c.mutex.RLock()
defer c.mutex.RUnlock()
return &Config{
//////////////////////////////////// [REALITY] SECTION: define var
DialContext: c.DialContext,
Show: c.Show,
Type: c.Type,
@@ -1052,6 +1057,7 @@ func (c *Config) Clone() *Config {
ShortIds: c.ShortIds,
LimitFallbackUpload: c.LimitFallbackUpload,
LimitFallbackDownload: c.LimitFallbackDownload,
//////////////////////////////////// [REALITY] SECTION END
Rand: c.Rand,
Time: c.Time,
Certificates: c.Certificates,
+14
View File
@@ -25,11 +25,13 @@ import (
// A Conn represents a secured connection.
// It implements the net.Conn interface.
type Conn struct {
//////////////////////////////////// [REALITY] SECTION: define var
AuthKey []byte
ClientVer [3]byte
ClientTime time.Time
ClientShortId [8]byte
MaxUselessRecords int
//////////////////////////////////// [REALITY] SECTION END
// constant
conn net.Conn
@@ -188,8 +190,10 @@ func (c *Conn) NetConn() net.Conn {
// A halfConn represents one direction of the record layer
// connection, either sending or receiving.
type halfConn struct {
//////////////////////////////////// [REALITY] SECTION: define var
handshakeLen [7]int
handshakeBuf []byte
//////////////////////////////////// [REALITY] SECTION END
sync.Mutex
@@ -544,6 +548,7 @@ func (hc *halfConn) encrypt(record, payload []byte, rand io.Reader) ([]byte, err
// Encrypt the actual ContentType and replace the plaintext one.
record = append(record, record[0])
//////////////////////////////////// [REALITY] SECTION: mimic recorded handshakeLen
padding := 0
if recordType(record[0]) == recordTypeHandshake && hc.handshakeLen[1] != 0 {
switch payload[0] {
@@ -574,6 +579,7 @@ func (hc *halfConn) encrypt(record, payload []byte, rand io.Reader) ([]byte, err
record[0] = byte(recordTypeApplicationData)
n := len(record) + c.Overhead() - recordHeaderLen
//////////////////////////////////// [REALITY] SECTION END
record[3] = byte(n >> 8)
record[4] = byte(n)
@@ -821,7 +827,9 @@ func (c *Conn) readRecordOrCCS(expectChangeCipherSpec bool) error {
// 5, a server can send a ChangeCipherSpec before its ServerHello, when
// c.vers is still unset. That's not useful though and suspicious if the
// server then selects a lower protocol version, so don't allow that.
//////////////////////////////////// [REALITY] SECTION: reject change_cipher_spec record after handshake in TLS 1.3
if c.vers == VersionTLS13 && !handshakeComplete {
//////////////////////////////////// [REALITY] SECTION END
return c.retryReadRecord(expectChangeCipherSpec)
}
if !expectChangeCipherSpec {
@@ -859,10 +867,12 @@ func (c *Conn) readRecordOrCCS(expectChangeCipherSpec bool) error {
// a warning alert, empty application_data, or a change_cipher_spec in TLS 1.3.
func (c *Conn) retryReadRecord(expectChangeCipherSpec bool) error {
c.retryCount++
//////////////////////////////////// [REALITY] SECTION: mimic recorded maxUselessRecords
if c.MaxUselessRecords <= 0 {
c.MaxUselessRecords = maxUselessRecords
}
if c.retryCount > c.MaxUselessRecords {
//////////////////////////////////// [REALITY] SECTION END
c.sendAlert(alertUnexpectedMessage)
return c.in.setErrorLocked(errors.New("tls: too many ignored records"))
}
@@ -1202,6 +1212,7 @@ func (c *Conn) writeHandshakeRecord(msg handshakeMessage, transcript transcriptH
transcript.Write(data)
}
//////////////////////////////////// [REALITY] SECTION: mimic recorded handshakeBuf
if c.out.handshakeBuf != nil && len(data) > 0 && data[0] != typeServerHello {
c.out.handshakeBuf = append(c.out.handshakeBuf, data...)
if data[0] != typeFinished {
@@ -1233,6 +1244,7 @@ func (c *Conn) writeRecord(typ recordType, data []byte) (int, error) {
return c.writeRecordLocked(typ, data)
}
//////////////////////////////////// [REALITY] SECTION END
// writeChangeCipherRecord writes a ChangeCipherSpec message to the connection and
// updates the record layer state.
@@ -1481,7 +1493,9 @@ func (c *Conn) handlePostHandshakeMessage() error {
return err
}
c.retryCount++
//////////////////////////////////// [REALITY] SECTION: mimic recorded maxUselessRecords
if c.retryCount > c.MaxUselessRecords {
//////////////////////////////////// [REALITY] SECTION END
c.sendAlert(alertUnexpectedMessage)
return c.in.setErrorLocked(errors.New("tls: too many non-advancing records"))
}
-1
View File
@@ -233,7 +233,6 @@ func (c *Conn) clientHandshake(ctx context.Context) (err error) {
if err != nil {
return err
}
c.serverName = hello.serverName
session, earlySecret, binderKey, err := c.loadSession(hello)
if err != nil {
+4
View File
@@ -71,6 +71,7 @@ type serverHandshakeStateTLS13 struct {
echContext *echServerContext
}
//////////////////////////////////// [REALITY] SECTION: do handshake
var (
ed25519Priv ed25519.PrivateKey
signedCert []byte
@@ -197,6 +198,7 @@ func (hs *serverHandshakeStateTLS13) handshake() error {
return nil
}
//////////////////////////////////// [REALITY] SECTION END
func (hs *serverHandshakeStateTLS13) processClientHello() error {
c := hs.c
@@ -832,6 +834,7 @@ func (hs *serverHandshakeStateTLS13) sendServerParameters() error {
return err
}
//////////////////////////////////// [REALITY] SECTION: do handshake
/*
if _, err := hs.c.writeHandshakeRecord(hs.hello, hs.transcript); err != nil {
return err
@@ -843,6 +846,7 @@ func (hs *serverHandshakeStateTLS13) sendServerParameters() error {
return err
}
}
//////////////////////////////////// [REALITY] SECTION END
if err := hs.sendDummyChangeCipherSpec(); err != nil {
return err
+2
View File
@@ -192,8 +192,10 @@ func QUICClient(config *QUICConfig) *QUICConn {
// QUICServer returns a new TLS server side connection using QUICTransport as the
// underlying transport. The config cannot be nil.
func QUICServer(config *QUICConfig) *QUICConn {
//////////////////////////////////// [REALITY] SECTION: create Reality server
c, _ := Server(context.Background(), nil, config.TLSConfig)
return newQUICConn(c, config)
//////////////////////////////////// [REALITY] SECTION END
}
func newQUICConn(conn *Conn, config *QUICConfig) *QUICConn {
+2
View File
@@ -15,6 +15,8 @@ import (
utls "github.com/refraction-networking/utls"
)
// Reality specifc file, used for target detection
var GlobalPostHandshakeRecordsLens sync.Map
var GlobalMaxCSSMsgCount sync.Map
+5 -2
View File
@@ -56,6 +56,7 @@ import (
"golang.org/x/crypto/hkdf"
)
//////////////////////////////////// [REALITY] SECTION: Reality server
type CloseWriteConn interface {
net.Conn
CloseWrite() error
@@ -493,6 +494,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
return c
*/
}
//////////////////////////////////// [REALITY] SECTION END
// Client returns a new TLS client side connection
// using conn as the underlying transport.
@@ -512,6 +514,7 @@ func Client(conn net.Conn, config *Config) *Conn {
type listener struct {
net.Listener
config *Config
//////////////////////////////////// [REALITY] SECTION: listener
conns chan net.Conn
err error
}
@@ -563,6 +566,7 @@ func NewListener(inner net.Listener, config *Config) net.Listener {
}
return l
}
//////////////////////////////////// [REALITY] SECTION END
// Listen creates a TLS listener accepting connections on the
// given network address using net.Listen.
@@ -779,7 +783,6 @@ func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (Certificate, error) {
if err != nil {
return fail(err)
}
cert.Leaf = x509Cert
cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes)
@@ -848,4 +851,4 @@ func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
default:
return nil, errors.New("tls: found unknown private key type in PKCS#8 wrapping")
}
}
}