Improve homekit secure conn buffers

This commit is contained in:
Alex X
2025-10-21 12:27:38 +03:00
parent 994e0dc526
commit ff18283d11
3 changed files with 12 additions and 13 deletions
+3 -1
View File
@@ -216,8 +216,10 @@ func (c *Client) Dial() (err error) {
return newResponseError(cipherM3, plainM4) return newResponseError(cipherM3, plainM4)
} }
rw := bufio.NewReadWriter(c.reader, bufio.NewWriter(c.Conn))
// like tls.Client wrapper over net.Conn // like tls.Client wrapper over net.Conn
if c.Conn, err = secure.Client(c.Conn, sessionShared, true); err != nil { if c.Conn, err = secure.Client(c.Conn, rw, sessionShared, true); err != nil {
return return
} }
// new reader for new conn // new reader for new conn
+8 -11
View File
@@ -14,9 +14,7 @@ import (
type Conn struct { type Conn struct {
conn net.Conn conn net.Conn
rw *bufio.ReadWriter
rd *bufio.Reader
wr *bufio.Writer
encryptKey []byte encryptKey []byte
decryptKey []byte decryptKey []byte
@@ -26,7 +24,7 @@ type Conn struct {
SharedKey []byte SharedKey []byte
} }
func Client(conn net.Conn, sharedKey []byte, isClient bool) (net.Conn, error) { func Client(conn net.Conn, rw *bufio.ReadWriter, sharedKey []byte, isClient bool) (*Conn, error) {
key1, err := hkdf.Sha512(sharedKey, "Control-Salt", "Control-Read-Encryption-Key") key1, err := hkdf.Sha512(sharedKey, "Control-Salt", "Control-Read-Encryption-Key")
if err != nil { if err != nil {
return nil, err return nil, err
@@ -39,8 +37,7 @@ func Client(conn net.Conn, sharedKey []byte, isClient bool) (net.Conn, error) {
c := &Conn{ c := &Conn{
conn: conn, conn: conn,
rd: bufio.NewReaderSize(conn, 32*1024), rw: rw,
wr: bufio.NewWriterSize(conn, 32*1024),
SharedKey: sharedKey, SharedKey: sharedKey,
} }
@@ -69,14 +66,14 @@ func (c *Conn) Read(b []byte) (n int, err error) {
} }
verify := make([]byte, 2) // verify = plain message size verify := make([]byte, 2) // verify = plain message size
if _, err = io.ReadFull(c.rd, verify); err != nil { if _, err = io.ReadFull(c.rw, verify); err != nil {
return return
} }
n = int(binary.LittleEndian.Uint16(verify)) n = int(binary.LittleEndian.Uint16(verify))
ciphertext := make([]byte, n+Overhead) ciphertext := make([]byte, n+Overhead)
if _, err = io.ReadFull(c.rd, ciphertext); err != nil { if _, err = io.ReadFull(c.rw, ciphertext); err != nil {
return return
} }
@@ -100,7 +97,7 @@ func (c *Conn) Write(b []byte) (n int, err error) {
} }
binary.LittleEndian.PutUint16(verify, uint16(size)) binary.LittleEndian.PutUint16(verify, uint16(size))
if _, err = c.wr.Write(verify); err != nil { if _, err = c.rw.Write(verify); err != nil {
return return
} }
@@ -112,7 +109,7 @@ func (c *Conn) Write(b []byte) (n int, err error) {
return return
} }
if _, err = c.wr.Write(buf[:size+Overhead]); err != nil { if _, err = c.rw.Write(buf[:size+Overhead]); err != nil {
return return
} }
@@ -120,7 +117,7 @@ func (c *Conn) Write(b []byte) (n int, err error) {
n += size n += size
} }
err = c.wr.Flush() err = c.rw.Flush()
return return
} }
+1 -1
View File
@@ -166,7 +166,7 @@ func (s *Server) PairVerify(req *http.Request, rw *bufio.ReadWriter, conn net.Co
return err return err
} }
if conn, err = secure.Client(conn, sessionShared, false); err != nil { if conn, err = secure.Client(conn, rw, sessionShared, false); err != nil {
return err return err
} }