You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cloak/internal/server/auth.go

104 lines
2.9 KiB
Go

6 years ago
package server
import (
"bytes"
"encoding/binary"
5 years ago
"errors"
"fmt"
"github.com/cbeuw/Cloak/internal/util"
"time"
5 years ago
log "github.com/sirupsen/logrus"
6 years ago
)
type ClientInfo struct {
UID []byte
SessionId uint32
ProxyMethod string
EncryptionMethod byte
Unordered bool
Transport Transport
}
4 years ago
type authFragments struct {
sharedSecret [32]byte
randPubKey [32]byte
ciphertextWithTag [64]byte
}
const (
UNORDERED_FLAG = 0x01 // 0000 0001
)
5 years ago
var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window")
5 years ago
var ErrUnreconisedProtocol = errors.New("unreconised protocol")
5 years ago
// decryptClientInfo checks if a the authFragments are valid. It doesn't check if the UID is authorised
4 years ago
func decryptClientInfo(fragments authFragments, serverTime time.Time) (info ClientInfo, err error) {
5 years ago
var plaintext []byte
4 years ago
plaintext, err = util.AESGCMDecrypt(fragments.randPubKey[0:12], fragments.sharedSecret[:], fragments.ciphertextWithTag[:])
5 years ago
if err != nil {
return
}
5 years ago
info = ClientInfo{
UID: plaintext[0:16],
SessionId: 0,
ProxyMethod: string(bytes.Trim(plaintext[16:28], "\x00")),
EncryptionMethod: plaintext[28],
Unordered: plaintext[41]&UNORDERED_FLAG != 0,
}
5 years ago
timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37]))
clientTime := time.Unix(timestamp, 0)
if !(clientTime.After(serverTime.Truncate(TIMESTAMP_TOLERANCE)) && clientTime.Before(serverTime.Add(TIMESTAMP_TOLERANCE))) {
5 years ago
err = fmt.Errorf("%v: received timestamp %v", ErrTimestampOutOfWindow, timestamp)
return
6 years ago
}
info.SessionId = binary.BigEndian.Uint32(plaintext[37:41])
return
6 years ago
}
5 years ago
var ErrReplay = errors.New("duplicate random")
var ErrBadProxyMethod = errors.New("invalid proxy method")
// AuthFirstPacket checks if the first packet of data is ClientHello or HTTP GET, and checks if it was from a Cloak client
5 years ago
// 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
func AuthFirstPacket(firstPacket []byte, sta *State) (info ClientInfo, finisher Responder, err error) {
var transport Transport
5 years ago
switch firstPacket[0] {
case 0x47:
transport = &WebSocket{}
5 years ago
case 0x16:
transport = &TLS{}
5 years ago
default:
err = ErrUnreconisedProtocol
return
}
fragments, finisher, err := transport.processFirstPacket(firstPacket, sta.staticPv)
if err != nil {
return
}
4 years ago
if sta.registerRandom(fragments.randPubKey) {
err = ErrReplay
return
}
4 years ago
info, err = decryptClientInfo(fragments, sta.WorldState.Now())
5 years ago
if err != nil {
log.Debug(err)
err = fmt.Errorf("transport %v in correct format but not Cloak: %v", transport, err)
5 years ago
return
}
if _, ok := sta.ProxyBook[info.ProxyMethod]; !ok {
err = ErrBadProxyMethod
return
}
info.Transport = transport
5 years ago
return
}