From a161409577ae49c0ea5fb2e8a9ca98609ef1a254 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Mon, 6 Apr 2020 14:29:38 +0100 Subject: [PATCH] Rename a struct --- internal/server/TLS.go | 18 +++++++++--------- internal/server/auth.go | 16 ++++++++-------- internal/server/transport.go | 2 +- internal/server/websocket.go | 18 +++++++++--------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/internal/server/TLS.go b/internal/server/TLS.go index 1adfd88..d68620d 100644 --- a/internal/server/TLS.go +++ b/internal/server/TLS.go @@ -19,7 +19,7 @@ func (TLS) String() string { return "TLS" } func (TLS) HasRecordLayer() bool { return true } func (TLS) UnitReadFunc() func(net.Conn, []byte) (int, error) { return util.ReadTLS } -func (TLS) handshake(clientHello []byte, privateKey crypto.PrivateKey, originalConn net.Conn) (ai authenticationInfo, finisher func([]byte) (net.Conn, error), err error) { +func (TLS) handshake(clientHello []byte, privateKey crypto.PrivateKey, originalConn net.Conn) (fragments authFragments, finisher func([]byte) (net.Conn, error), err error) { var ch *ClientHello ch, err = parseClientHello(clientHello) if err != nil { @@ -28,15 +28,15 @@ func (TLS) handshake(clientHello []byte, privateKey crypto.PrivateKey, originalC return } - ai, err = unmarshalClientHello(ch, privateKey) + fragments, err = unmarshalClientHello(ch, privateKey) if err != nil { - err = fmt.Errorf("failed to unmarshal ClientHello into authenticationInfo: %v", err) + err = fmt.Errorf("failed to unmarshal ClientHello into authFragments: %v", err) return } finisher = func(sessionKey []byte) (preparedConn net.Conn, err error) { preparedConn = originalConn - reply, err := composeReply(ch, ai.sharedSecret[:], sessionKey) + reply, err := composeReply(ch, fragments.sharedSecret[:], sessionKey) if err != nil { err = fmt.Errorf("failed to compose TLS reply: %v", err) return @@ -53,15 +53,15 @@ func (TLS) handshake(clientHello []byte, privateKey crypto.PrivateKey, originalC return } -func unmarshalClientHello(ch *ClientHello, staticPv crypto.PrivateKey) (ai authenticationInfo, err error) { - copy(ai.randPubKey[:], ch.random) - ephPub, ok := ecdh.Unmarshal(ai.randPubKey[:]) +func unmarshalClientHello(ch *ClientHello, staticPv crypto.PrivateKey) (fragments authFragments, err error) { + copy(fragments.randPubKey[:], ch.random) + ephPub, ok := ecdh.Unmarshal(fragments.randPubKey[:]) if !ok { err = ErrInvalidPubKey return } - copy(ai.sharedSecret[:], ecdh.GenerateSharedSecret(staticPv, ephPub)) + copy(fragments.sharedSecret[:], ecdh.GenerateSharedSecret(staticPv, ephPub)) var keyShare []byte keyShare, err = parseKeyShare(ch.extensions[[2]byte{0x00, 0x33}]) if err != nil { @@ -73,6 +73,6 @@ func unmarshalClientHello(ch *ClientHello, staticPv crypto.PrivateKey) (ai authe err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(ctxTag)) return } - copy(ai.ciphertextWithTag[:], ctxTag) + copy(fragments.ciphertextWithTag[:], ctxTag) return } diff --git a/internal/server/auth.go b/internal/server/auth.go index 39dc515..9eeba4a 100644 --- a/internal/server/auth.go +++ b/internal/server/auth.go @@ -21,7 +21,7 @@ type ClientInfo struct { Transport Transport } -type authenticationInfo struct { +type authFragments struct { sharedSecret [32]byte randPubKey [32]byte ciphertextWithTag [64]byte @@ -34,10 +34,10 @@ const ( var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window") var ErrUnreconisedProtocol = errors.New("unreconised protocol") -// touchStone checks if a the authenticationInfo are valid. It doesn't check if the UID is authorised -func touchStone(ai authenticationInfo, now func() time.Time) (info ClientInfo, err error) { +// touchStone checks if a the authFragments are valid. It doesn't check if the UID is authorised +func touchStone(fragments authFragments, now func() time.Time) (info ClientInfo, err error) { var plaintext []byte - plaintext, err = util.AESGCMDecrypt(ai.randPubKey[0:12], ai.sharedSecret[:], ai.ciphertextWithTag[:]) + plaintext, err = util.AESGCMDecrypt(fragments.randPubKey[0:12], fragments.sharedSecret[:], fragments.ciphertextWithTag[:]) if err != nil { return } @@ -80,19 +80,19 @@ func PrepareConnection(firstPacket []byte, sta *State, conn net.Conn) (info Clie return } - var ai authenticationInfo - ai, finisher, err = transport.handshake(firstPacket, sta.staticPv, conn) + var fragments authFragments + fragments, finisher, err = transport.handshake(firstPacket, sta.staticPv, conn) if err != nil { return } - if sta.registerRandom(ai.randPubKey) { + if sta.registerRandom(fragments.randPubKey) { err = ErrReplay return } - info, err = touchStone(ai, sta.Now) + info, err = touchStone(fragments, sta.Now) if err != nil { log.Debug(err) err = fmt.Errorf("transport %v in correct format but not Cloak: %v", transport, err) diff --git a/internal/server/transport.go b/internal/server/transport.go index baa9872..9fbe898 100644 --- a/internal/server/transport.go +++ b/internal/server/transport.go @@ -9,7 +9,7 @@ import ( type Transport interface { HasRecordLayer() bool UnitReadFunc() func(net.Conn, []byte) (int, error) - handshake(reqPacket []byte, privateKey crypto.PrivateKey, originalConn net.Conn) (authenticationInfo, func([]byte) (net.Conn, error), error) + handshake(reqPacket []byte, privateKey crypto.PrivateKey, originalConn net.Conn) (authFragments, func([]byte) (net.Conn, error), error) } var ErrInvalidPubKey = errors.New("public key has invalid format") diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 5500e65..92ed109 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -19,7 +19,7 @@ func (WebSocket) String() string { return "We func (WebSocket) HasRecordLayer() bool { return false } func (WebSocket) UnitReadFunc() func(net.Conn, []byte) (int, error) { return util.ReadWebSocket } -func (WebSocket) handshake(reqPacket []byte, privateKey crypto.PrivateKey, originalConn net.Conn) (ai authenticationInfo, finisher func([]byte) (net.Conn, error), err error) { +func (WebSocket) handshake(reqPacket []byte, privateKey crypto.PrivateKey, originalConn net.Conn) (fragments authFragments, finisher func([]byte) (net.Conn, error), err error) { var req *http.Request req, err = http.ReadRequest(bufio.NewReader(bytes.NewBuffer(reqPacket))) if err != nil { @@ -29,9 +29,9 @@ func (WebSocket) handshake(reqPacket []byte, privateKey crypto.PrivateKey, origi var hiddenData []byte hiddenData, err = base64.StdEncoding.DecodeString(req.Header.Get("hidden")) - ai, err = unmarshalHidden(hiddenData, privateKey) + fragments, err = unmarshalHidden(hiddenData, privateKey) if err != nil { - err = fmt.Errorf("failed to unmarshal hidden data from WS into authenticationInfo: %v", err) + err = fmt.Errorf("failed to unmarshal hidden data from WS into authFragments: %v", err) return } @@ -47,7 +47,7 @@ func (WebSocket) handshake(reqPacket []byte, privateKey crypto.PrivateKey, origi util.CryptoRandRead(nonce) // reply: [12 bytes nonce][32 bytes encrypted session key][16 bytes authentication tag] - encryptedKey, err := util.AESGCMEncrypt(nonce, ai.sharedSecret[:], sessionKey) // 32 + 16 = 48 bytes + encryptedKey, err := util.AESGCMEncrypt(nonce, fragments.sharedSecret[:], sessionKey) // 32 + 16 = 48 bytes if err != nil { err = fmt.Errorf("failed to encrypt reply: %v", err) return @@ -67,26 +67,26 @@ func (WebSocket) handshake(reqPacket []byte, privateKey crypto.PrivateKey, origi var ErrBadGET = errors.New("non (or malformed) HTTP GET") -func unmarshalHidden(hidden []byte, staticPv crypto.PrivateKey) (ai authenticationInfo, err error) { +func unmarshalHidden(hidden []byte, staticPv crypto.PrivateKey) (fragments authFragments, err error) { if len(hidden) < 96 { err = ErrBadGET return } - copy(ai.randPubKey[:], hidden[0:32]) - ephPub, ok := ecdh.Unmarshal(ai.randPubKey[:]) + copy(fragments.randPubKey[:], hidden[0:32]) + ephPub, ok := ecdh.Unmarshal(fragments.randPubKey[:]) if !ok { err = ErrInvalidPubKey return } - copy(ai.sharedSecret[:], ecdh.GenerateSharedSecret(staticPv, ephPub)) + copy(fragments.sharedSecret[:], ecdh.GenerateSharedSecret(staticPv, ephPub)) if len(hidden[32:]) != 64 { err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(hidden[32:])) return } - copy(ai.ciphertextWithTag[:], hidden[32:]) + copy(fragments.ciphertextWithTag[:], hidden[32:]) return }