Skip to content

Commit fce1195

Browse files
authored
Final changes
#4952 (comment)
1 parent 0199dea commit fce1195

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

proxy/vless/encryption/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ func (i *ClientInstance) Handshake(conn net.Conn) (*CommonConn, error) {
161161
if err != nil {
162162
return nil, err
163163
}
164-
pfsKey := append(mlkem768Key, x25519Key...)
164+
pfsKey := make([]byte, 32+32) // no more capacity
165+
copy(pfsKey, mlkem768Key)
166+
copy(pfsKey[32:], x25519Key)
165167
c.UnitedKey = append(pfsKey, nfsKey...)
166168
c.GCM = NewGCM(pfsPublicKey, c.UnitedKey)
167169
c.PeerGCM = NewGCM(encryptedPfsPublicKey[:1088+32], c.UnitedKey)

proxy/vless/encryption/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (c *CommonConn) Read(b []byte) (int, error) {
8989
if err != nil {
9090
if c.Client != nil && strings.HasPrefix(err.Error(), "invalid header: ") { // client's 0-RTT
9191
c.Client.RWLock.Lock()
92-
if bytes.Equal(c.UnitedKey[:32], c.Client.PfsKey) {
92+
if bytes.HasPrefix(c.UnitedKey, c.Client.PfsKey) {
9393
c.Client.Expire = time.Now() // expired
9494
}
9595
c.Client.RWLock.Unlock()

proxy/vless/encryption/server.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
type ServerSession struct {
2121
Expire time.Time
2222
PfsKey []byte
23-
Replays sync.Map
23+
NfsKeys sync.Map
2424
}
2525

2626
type ServerInstance struct {
@@ -178,7 +178,7 @@ func (i *ServerInstance) Handshake(conn net.Conn) (*CommonConn, error) {
178178
s := i.Sessions[[16]byte(ticket)]
179179
i.RWLock.RUnlock()
180180
if s == nil {
181-
noises := make([]byte, crypto.RandBetween(100, 1000))
181+
noises := make([]byte, crypto.RandBetween(1268, 2268)) // matches 1-RTT's server hello length for "random", though it is not important, just for example
182182
var err error
183183
for err == nil {
184184
rand.Read(noises)
@@ -187,21 +187,21 @@ func (i *ServerInstance) Handshake(conn net.Conn) (*CommonConn, error) {
187187
conn.Write(noises) // make client do new handshake
188188
return nil, errors.New("expired ticket")
189189
}
190-
if _, replay := s.Replays.LoadOrStore([32]byte(nfsKey), true); replay { // prevents bad client also
190+
if _, loaded := s.NfsKeys.LoadOrStore([32]byte(nfsKey), true); loaded { // prevents bad client also
191191
return nil, errors.New("replay detected")
192192
}
193-
c.UnitedKey = append(s.PfsKey, nfsKey...) // the same nfsKey links the upload & download
194-
c.PreWrite = make([]byte, 16) // always trust yourself, not the client
195-
rand.Read(c.PreWrite)
193+
c.UnitedKey = append(s.PfsKey, nfsKey...) // the same nfsKey links the upload & download (prevents server -> client's another request)
194+
c.PreWrite = make([]byte, 16)
195+
rand.Read(c.PreWrite) // always trust yourself, not the client (also prevents being parsed as TLS thus causing false interruption for "native" and "xorpub")
196196
c.GCM = NewGCM(c.PreWrite, c.UnitedKey)
197-
c.PeerGCM = NewGCM(encryptedTicket, c.UnitedKey) // unchangeable ctx, and different ctx length for upload / download
197+
c.PeerGCM = NewGCM(encryptedTicket, c.UnitedKey) // unchangeable ctx (prevents server -> server), and different ctx length for upload / download (prevents client -> client)
198198
if i.XorMode == 2 {
199199
c.Conn = NewXorConn(conn, NewCTR(c.UnitedKey, c.PreWrite), NewCTR(c.UnitedKey, iv), 16, 0) // it doesn't matter if the attacker sends client's iv back to the client
200200
}
201201
return c, nil
202202
}
203203

204-
if length < 1184+32+16 { // client may send more public keys
204+
if length < 1184+32+16 { // client may send more public keys in the future's version
205205
return nil, errors.New("too short length")
206206
}
207207
encryptedPfsPublicKey := make([]byte, length)
@@ -225,7 +225,9 @@ func (i *ServerInstance) Handshake(conn net.Conn) (*CommonConn, error) {
225225
if err != nil {
226226
return nil, err
227227
}
228-
pfsKey := append(mlkem768Key, x25519Key...)
228+
pfsKey := make([]byte, 32+32) // no more capacity
229+
copy(pfsKey, mlkem768Key)
230+
copy(pfsKey[32:], x25519Key)
229231
pfsPublicKey := append(encapsulatedPfsKey, x25519SKey.PublicKey().Bytes()...)
230232
c.UnitedKey = append(pfsKey, nfsKey...)
231233
c.GCM = NewGCM(pfsPublicKey, c.UnitedKey)

0 commit comments

Comments
 (0)