Cloak/internal/client/state.go

185 lines
4.5 KiB
Go
Raw Normal View History

2018-10-07 17:09:45 +00:00
package client
import (
2018-10-14 19:32:54 +00:00
"crypto"
"crypto/rand"
2018-10-07 17:09:45 +00:00
"encoding/base64"
"encoding/binary"
2018-10-07 17:09:45 +00:00
"encoding/json"
"errors"
"io/ioutil"
"strings"
2018-10-14 19:32:54 +00:00
"sync"
2018-10-07 17:09:45 +00:00
"time"
2018-10-14 19:32:54 +00:00
"github.com/cbeuw/Cloak/internal/ecdh"
mux "github.com/cbeuw/Cloak/internal/multiplex"
2018-10-07 17:09:45 +00:00
)
type rawConfig struct {
2019-06-09 11:05:41 +00:00
ServerName string
ProxyMethod string
EncryptionMethod string
UID string
PublicKey string
TicketTimeHint int
BrowserSig string
NumConn int
2018-10-07 17:09:45 +00:00
}
type tthIntervalKeys struct {
interval int64
ephPv crypto.PrivateKey
ephPub crypto.PublicKey
intervalKey []byte
seed int64
}
2018-10-07 17:09:45 +00:00
// State stores global variables
type State struct {
LocalHost string
LocalPort string
RemoteHost string
RemotePort string
2018-10-14 19:32:54 +00:00
Now func() time.Time
SessionID uint32
UID []byte
staticPub crypto.PublicKey
intervalDataM sync.Mutex
intervalData *tthIntervalKeys
2018-10-14 19:32:54 +00:00
2019-06-09 11:05:41 +00:00
ProxyMethod string
EncryptionMethod byte
Cipher mux.Crypto
2019-06-09 11:05:41 +00:00
TicketTimeHint int
ServerName string
BrowserSig string
NumConn int
2018-10-07 17:09:45 +00:00
}
2019-01-22 21:51:57 +00:00
func InitState(localHost, localPort, remoteHost, remotePort string, nowFunc func() time.Time) *State {
2018-10-14 19:32:54 +00:00
ret := &State{
LocalHost: localHost,
LocalPort: localPort,
RemoteHost: remoteHost,
RemotePort: remotePort,
Now: nowFunc,
intervalData: &tthIntervalKeys{},
2018-10-14 19:32:54 +00:00
}
return ret
}
func (sta *State) UpdateIntervalKeys() {
sta.intervalDataM.Lock()
currentInterval := sta.Now().Unix() / int64(sta.TicketTimeHint)
if currentInterval == sta.intervalData.interval {
sta.intervalDataM.Unlock()
return
}
sta.intervalData.interval = currentInterval
ephPv, ephPub, _ := ecdh.GenerateKey(rand.Reader)
intervalKey := ecdh.GenerateSharedSecret(ephPv, sta.staticPub)
seed := int64(binary.BigEndian.Uint64(ephPv.(*[32]byte)[0:8]))
sta.intervalData.ephPv, sta.intervalData.ephPub, sta.intervalData.intervalKey, sta.intervalData.seed = ephPv, ephPub, intervalKey, seed
sta.intervalDataM.Unlock()
}
func (sta *State) GetIntervalKeys() (crypto.PublicKey, []byte, int64) {
sta.intervalDataM.Lock()
defer sta.intervalDataM.Unlock()
return sta.intervalData.ephPub, sta.intervalData.intervalKey, sta.intervalData.seed
}
2019-01-22 21:51:57 +00:00
2018-10-07 17:09:45 +00:00
// semi-colon separated value. This is for Android plugin options
func ssvToJson(ssv string) (ret []byte) {
unescape := func(s string) string {
2018-12-17 22:12:38 +00:00
r := strings.Replace(s, `\\`, `\`, -1)
r = strings.Replace(r, `\=`, `=`, -1)
r = strings.Replace(r, `\;`, `;`, -1)
2018-10-07 17:09:45 +00:00
return r
}
lines := strings.Split(unescape(ssv), ";")
ret = []byte("{")
for _, ln := range lines {
if ln == "" {
break
}
sp := strings.SplitN(ln, "=", 2)
key := sp[0]
value := sp[1]
// JSON doesn't like quotation marks around int
// Yes this is extremely ugly but it's still better than writing a tokeniser
if key == "TicketTimeHint" || key == "NumConn" {
2018-12-17 22:12:38 +00:00
ret = append(ret, []byte(`"`+key+`":`+value+`,`)...)
2018-10-07 17:09:45 +00:00
} else {
2018-12-17 22:12:38 +00:00
ret = append(ret, []byte(`"`+key+`":"`+value+`",`)...)
2018-10-07 17:09:45 +00:00
}
}
ret = ret[:len(ret)-1] // remove the last comma
ret = append(ret, '}')
return ret
}
// ParseConfig parses the config (either a path to json or Android config) into a State variable
func (sta *State) ParseConfig(conf string) (err error) {
var content []byte
if strings.Contains(conf, ";") && strings.Contains(conf, "=") {
content = ssvToJson(conf)
} else {
content, err = ioutil.ReadFile(conf)
if err != nil {
return err
}
}
var preParse rawConfig
err = json.Unmarshal(content, &preParse)
if err != nil {
return err
}
2019-06-09 11:05:41 +00:00
switch preParse.EncryptionMethod {
case "plain":
sta.EncryptionMethod = 0x00
sta.Cipher = &mux.Plain{}
case "aes-gcm":
2019-06-09 11:05:41 +00:00
sta.EncryptionMethod = 0x01
sta.Cipher, err = mux.MakeAESGCMCipher(sta.UID)
if err != nil {
return err
}
2019-06-14 10:26:26 +00:00
case "chacha20-poly1305":
sta.EncryptionMethod = 0x02
sta.Cipher, err = mux.MakeCPCipher(sta.UID)
if err != nil {
return err
}
2019-06-09 11:05:41 +00:00
default:
return errors.New("Unknown encryption method")
}
sta.ProxyMethod = preParse.ProxyMethod
2018-10-07 17:09:45 +00:00
sta.ServerName = preParse.ServerName
sta.TicketTimeHint = preParse.TicketTimeHint
sta.BrowserSig = preParse.BrowserSig
2018-10-09 20:53:55 +00:00
sta.NumConn = preParse.NumConn
2019-06-09 11:05:41 +00:00
2018-12-03 20:30:06 +00:00
uid, err := base64.StdEncoding.DecodeString(preParse.UID)
2018-10-07 17:09:45 +00:00
if err != nil {
2018-12-03 20:30:06 +00:00
return errors.New("Failed to parse UID: " + err.Error())
2018-10-07 17:09:45 +00:00
}
2018-11-07 21:16:13 +00:00
sta.UID = uid
2018-10-07 17:09:45 +00:00
2018-12-03 20:30:06 +00:00
pubBytes, err := base64.StdEncoding.DecodeString(preParse.PublicKey)
2018-10-07 17:09:45 +00:00
if err != nil {
2018-12-03 20:30:06 +00:00
return errors.New("Failed to parse Public key: " + err.Error())
2018-10-07 17:09:45 +00:00
}
pub, ok := ecdh.Unmarshal(pubBytes)
2018-12-03 20:30:06 +00:00
if !ok {
return errors.New("Failed to unmarshal Public key")
}
sta.staticPub = pub
return nil
2018-10-14 19:32:54 +00:00
}