Cloak/internal/server/auth.go

179 lines
5.0 KiB
Go
Raw Normal View History

2018-10-09 15:07:54 +00:00
package server
import (
2019-09-01 00:33:34 +00:00
"bufio"
2018-10-09 15:07:54 +00:00
"bytes"
2019-09-01 00:33:34 +00:00
"crypto/rand"
"encoding/base64"
2018-10-09 15:07:54 +00:00
"encoding/binary"
2019-08-02 14:45:33 +00:00
"errors"
"fmt"
2018-10-14 19:32:54 +00:00
"github.com/cbeuw/Cloak/internal/util"
2019-09-01 00:33:34 +00:00
"net"
"net/http"
"time"
2019-09-01 00:33:34 +00:00
log "github.com/sirupsen/logrus"
2018-10-09 15:07:54 +00:00
)
2019-08-12 13:21:42 +00:00
type ClientInfo struct {
UID []byte
SessionId uint32
ProxyMethod string
EncryptionMethod byte
2019-08-12 21:43:16 +00:00
Unordered bool
2019-09-01 19:23:45 +00:00
Transport Transport
2019-08-12 13:21:42 +00:00
}
2019-08-31 20:40:50 +00:00
type authenticationInfo struct {
sharedSecret []byte
nonce []byte
ciphertextWithTag []byte
}
2019-08-12 21:43:16 +00:00
const (
UNORDERED_FLAG = 0x01 // 0000 0001
)
2019-08-02 14:45:33 +00:00
var ErrInvalidPubKey = errors.New("public key has invalid format")
var ErrCiphertextLength = errors.New("ciphertext has the wrong length")
var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window")
2019-09-01 00:33:34 +00:00
var ErrUnreconisedProtocol = errors.New("unreconised protocol")
2019-08-02 14:45:33 +00:00
2019-09-01 22:14:28 +00:00
// touchStone checks if a the authenticationInfo are valid. It doesn't check if the UID is authorised
2019-08-31 20:40:50 +00:00
func touchStone(ai authenticationInfo, now func() time.Time) (info ClientInfo, err error) {
2019-08-02 14:45:33 +00:00
var plaintext []byte
2019-08-31 20:40:50 +00:00
plaintext, err = util.AESGCMDecrypt(ai.nonce, ai.sharedSecret, ai.ciphertextWithTag)
2019-08-02 00:01:19 +00:00
if err != nil {
2019-06-09 14:03:28 +00:00
return
}
2019-08-02 00:01:19 +00:00
2019-08-14 10:48:32 +00:00
info = ClientInfo{
2019-08-12 13:21:42 +00:00
UID: plaintext[0:16],
SessionId: 0,
ProxyMethod: string(bytes.Trim(plaintext[16:28], "\x00")),
EncryptionMethod: plaintext[28],
2019-08-12 21:43:16 +00:00
Unordered: plaintext[41]&UNORDERED_FLAG != 0,
2019-08-12 13:21:42 +00:00
}
2019-08-02 00:01:19 +00:00
timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37]))
clientTime := time.Unix(timestamp, 0)
2019-08-16 22:56:46 +00:00
serverTime := now()
if !(clientTime.After(serverTime.Truncate(TIMESTAMP_TOLERANCE)) && clientTime.Before(serverTime.Add(TIMESTAMP_TOLERANCE))) {
2019-08-02 14:45:33 +00:00
err = fmt.Errorf("%v: received timestamp %v", ErrTimestampOutOfWindow, timestamp)
2019-06-09 11:05:41 +00:00
return
2018-10-09 15:07:54 +00:00
}
2019-08-12 13:21:42 +00:00
info.SessionId = binary.BigEndian.Uint32(plaintext[37:41])
2018-11-07 21:16:13 +00:00
return
2018-10-09 15:07:54 +00:00
}
2019-09-01 00:33:34 +00:00
2019-09-01 19:23:45 +00:00
var ErrBadClientHello = errors.New("non (or malformed) ClientHello")
var ErrReplay = errors.New("duplicate random")
var ErrBadProxyMethod = errors.New("invalid proxy method")
2019-09-01 00:33:34 +00:00
// PrepareConnection checks if the first packet of data is ClientHello or HTTP GET, and checks if it was from a Cloak client
// if it is from a Cloak client, it returns the ClientInfo with the decrypted fields. It doesn't check if the user
// is authorised. It also returns a finisher callback function to be called when the caller wishes to proceed with
// the handshake
2019-09-01 19:23:45 +00:00
func PrepareConnection(firstPacket []byte, sta *State, conn net.Conn) (info ClientInfo, finisher func([]byte) (net.Conn, error), err error) {
var transport Transport
2019-09-01 00:33:34 +00:00
var ai authenticationInfo
switch firstPacket[0] {
case 0x47:
2019-09-02 13:04:52 +00:00
transport = WebSocket{}
2019-09-01 00:33:34 +00:00
var req *http.Request
req, err = http.ReadRequest(bufio.NewReader(bytes.NewBuffer(firstPacket)))
if err != nil {
err = fmt.Errorf("failed to parse first HTTP GET: %v", err)
return
}
var hiddenData []byte
hiddenData, err = base64.StdEncoding.DecodeString(req.Header.Get("hidden"))
ai, err = unmarshalHidden(hiddenData, sta.staticPv)
if err != nil {
err = fmt.Errorf("failed to unmarshal hidden data from WS into authenticationInfo: %v", err)
return
}
2019-09-01 19:23:45 +00:00
finisher = func(sessionKey []byte) (preparedConn net.Conn, err error) {
2019-09-01 00:33:34 +00:00
handler := newWsHandshakeHandler()
2019-09-01 19:23:45 +00:00
http.Serve(newWsAcceptor(conn, firstPacket), handler)
2019-09-01 00:33:34 +00:00
<-handler.finished
2019-09-01 19:23:45 +00:00
preparedConn = handler.conn
2019-09-01 00:33:34 +00:00
nonce := make([]byte, 12)
rand.Read(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
if err != nil {
2019-09-01 19:23:45 +00:00
err = fmt.Errorf("failed to encrypt reply: %v", err)
return
2019-09-01 00:33:34 +00:00
}
reply := append(nonce, encryptedKey...)
2019-09-01 19:23:45 +00:00
_, err = preparedConn.Write(reply)
2019-09-01 00:33:34 +00:00
if err != nil {
2019-09-01 19:23:45 +00:00
err = fmt.Errorf("failed to write reply: %v", err)
go preparedConn.Close()
return
2019-09-01 00:33:34 +00:00
}
2019-09-01 19:23:45 +00:00
return
2019-09-01 00:33:34 +00:00
}
case 0x16:
2019-09-02 13:04:52 +00:00
transport = TLS{}
2019-09-01 00:33:34 +00:00
var ch *ClientHello
ch, err = parseClientHello(firstPacket)
if err != nil {
log.Debug(err)
err = ErrBadClientHello
return
}
if sta.registerRandom(ch.random) {
err = ErrReplay
return
}
ai, err = unmarshalClientHello(ch, sta.staticPv)
if err != nil {
err = fmt.Errorf("failed to unmarshal ClientHello into authenticationInfo: %v", err)
return
}
2019-09-01 19:23:45 +00:00
finisher = func(sessionKey []byte) (preparedConn net.Conn, err error) {
preparedConn = conn
2019-09-01 00:33:34 +00:00
reply, err := composeReply(ch, ai.sharedSecret, sessionKey)
if err != nil {
2019-09-01 19:23:45 +00:00
err = fmt.Errorf("failed to compose TLS reply: %v", err)
return
2019-09-01 00:33:34 +00:00
}
2019-09-01 19:23:45 +00:00
_, err = preparedConn.Write(reply)
2019-09-01 00:33:34 +00:00
if err != nil {
2019-09-01 19:23:45 +00:00
err = fmt.Errorf("failed to write TLS reply: %v", err)
go preparedConn.Close()
return
2019-09-01 00:33:34 +00:00
}
2019-09-01 19:23:45 +00:00
return
2019-09-01 00:33:34 +00:00
}
default:
err = ErrUnreconisedProtocol
return
}
info, err = touchStone(ai, sta.Now)
if err != nil {
log.Debug(err)
2019-09-02 13:04:52 +00:00
err = fmt.Errorf("transport %v in correct format but not Cloak: %v", transport, err)
2019-09-01 00:33:34 +00:00
return
}
2019-09-01 19:23:45 +00:00
info.Transport = transport
2019-09-01 00:33:34 +00:00
if _, ok := sta.ProxyBook[info.ProxyMethod]; !ok {
err = ErrBadProxyMethod
return
}
return
}