2018-12-09 23:45:06 +00:00
|
|
|
package multiplex
|
2018-10-07 17:09:45 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-03 21:05:06 +00:00
|
|
|
"crypto/aes"
|
2019-07-31 23:16:33 +00:00
|
|
|
"crypto/cipher"
|
2018-10-07 17:09:45 +00:00
|
|
|
"encoding/binary"
|
2018-12-09 23:45:06 +00:00
|
|
|
"errors"
|
2019-09-01 19:23:45 +00:00
|
|
|
"fmt"
|
2020-04-14 00:53:28 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/common"
|
2019-08-03 21:05:06 +00:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
"golang.org/x/crypto/salsa20"
|
2018-10-07 17:09:45 +00:00
|
|
|
)
|
|
|
|
|
2020-04-13 21:48:28 +00:00
|
|
|
type Obfser func(*Frame, []byte, int) (int, error)
|
2018-12-09 23:45:06 +00:00
|
|
|
type Deobfser func([]byte) (*Frame, error)
|
|
|
|
|
2019-01-06 01:40:27 +00:00
|
|
|
var u32 = binary.BigEndian.Uint32
|
2019-08-27 14:06:28 +00:00
|
|
|
var u64 = binary.BigEndian.Uint64
|
2019-06-14 09:48:59 +00:00
|
|
|
var putU32 = binary.BigEndian.PutUint32
|
2019-08-27 14:06:28 +00:00
|
|
|
var putU64 = binary.BigEndian.PutUint64
|
2019-01-06 01:40:27 +00:00
|
|
|
|
2019-08-27 14:06:28 +00:00
|
|
|
const HEADER_LEN = 14
|
2019-01-13 21:28:57 +00:00
|
|
|
|
2019-08-16 22:44:40 +00:00
|
|
|
const (
|
|
|
|
E_METHOD_PLAIN = iota
|
|
|
|
E_METHOD_AES_GCM
|
|
|
|
E_METHOD_CHACHA20_POLY1305
|
|
|
|
)
|
|
|
|
|
2020-04-08 11:18:20 +00:00
|
|
|
// Obfuscator is responsible for the obfuscation and deobfuscation of frames
|
|
|
|
type Obfuscator struct {
|
|
|
|
// Used in Stream.Write. Add multiplexing headers, encrypt and add TLS header
|
|
|
|
Obfs Obfser
|
|
|
|
// Remove TLS header, decrypt and unmarshall frames
|
2020-04-09 15:37:46 +00:00
|
|
|
Deobfs Deobfser
|
|
|
|
SessionKey [32]byte
|
|
|
|
minOverhead int
|
2020-04-08 11:18:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 19:53:09 +00:00
|
|
|
func MakeObfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Obfser {
|
2020-04-13 21:48:28 +00:00
|
|
|
obfs := func(f *Frame, buf []byte, payloadOffsetInBuf int) (int, error) {
|
2019-08-20 21:43:04 +00:00
|
|
|
// we need the encrypted data to be at least 8 bytes to be used as nonce for salsa20 stream header encryption
|
|
|
|
// this will be the case if the encryption method is an AEAD cipher, however for plain, it's well possible
|
|
|
|
// that the frame payload is smaller than 8 bytes, so we need to add on the difference
|
2020-04-13 21:48:28 +00:00
|
|
|
payloadLen := len(f.Payload)
|
|
|
|
if payloadLen == 0 {
|
|
|
|
return 0, errors.New("payload cannot be empty")
|
|
|
|
}
|
2020-04-12 15:10:48 +00:00
|
|
|
var extraLen int
|
2019-08-04 09:38:49 +00:00
|
|
|
if payloadCipher == nil {
|
2020-04-13 21:48:28 +00:00
|
|
|
if extraLen = 8 - payloadLen; extraLen < 0 {
|
2020-04-12 15:10:48 +00:00
|
|
|
extraLen = 0
|
2019-08-04 09:38:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-12 15:10:48 +00:00
|
|
|
extraLen = payloadCipher.Overhead()
|
|
|
|
if extraLen < 8 {
|
|
|
|
return 0, errors.New("AEAD's Overhead cannot be fewer than 8 bytes")
|
|
|
|
}
|
2019-08-04 09:38:49 +00:00
|
|
|
}
|
2020-04-13 21:48:28 +00:00
|
|
|
|
|
|
|
usefulLen := HEADER_LEN + payloadLen + extraLen
|
|
|
|
if len(buf) < usefulLen {
|
|
|
|
return 0, errors.New("obfs buffer too small")
|
2019-08-04 09:38:49 +00:00
|
|
|
}
|
2019-08-20 21:43:04 +00:00
|
|
|
// we do as much in-place as possible to save allocation
|
2020-04-13 21:48:28 +00:00
|
|
|
payload := buf[HEADER_LEN : HEADER_LEN+payloadLen]
|
|
|
|
if payloadOffsetInBuf != HEADER_LEN {
|
|
|
|
// if payload is not at the correct location in buffer
|
|
|
|
copy(payload, f.Payload)
|
|
|
|
}
|
2019-08-20 21:43:04 +00:00
|
|
|
|
2020-04-13 21:48:28 +00:00
|
|
|
header := buf[:HEADER_LEN]
|
2019-06-14 09:48:59 +00:00
|
|
|
putU32(header[0:4], f.StreamID)
|
2019-08-27 14:06:28 +00:00
|
|
|
putU64(header[4:12], f.Seq)
|
|
|
|
header[12] = f.Closing
|
2020-04-12 15:10:48 +00:00
|
|
|
header[13] = byte(extraLen)
|
2019-06-14 09:48:59 +00:00
|
|
|
|
2019-07-31 23:43:33 +00:00
|
|
|
if payloadCipher == nil {
|
2020-04-12 15:10:48 +00:00
|
|
|
if extraLen != 0 { // read nonce
|
2020-04-13 21:48:28 +00:00
|
|
|
extra := buf[usefulLen-extraLen : usefulLen]
|
2020-04-14 00:53:28 +00:00
|
|
|
common.CryptoRandRead(extra)
|
2019-08-04 09:38:49 +00:00
|
|
|
}
|
2019-07-31 23:43:33 +00:00
|
|
|
} else {
|
2020-04-13 21:48:28 +00:00
|
|
|
payloadCipher.Seal(payload[:0], header[:12], payload, nil)
|
2019-06-09 14:03:28 +00:00
|
|
|
}
|
2019-06-09 11:05:41 +00:00
|
|
|
|
2020-04-13 21:48:28 +00:00
|
|
|
nonce := buf[usefulLen-8 : usefulLen]
|
2019-08-03 21:05:06 +00:00
|
|
|
salsa20.XORKeyStream(header, header, nonce, &salsaKey)
|
2019-06-14 09:48:59 +00:00
|
|
|
|
2019-08-04 09:38:49 +00:00
|
|
|
return usefulLen, nil
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
return obfs
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:53:09 +00:00
|
|
|
func MakeDeobfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Deobfser {
|
|
|
|
// stream header length + minimum data size (i.e. nonce size of salsa20)
|
2020-04-12 15:10:48 +00:00
|
|
|
const minInputLen = HEADER_LEN + 8
|
2018-12-09 23:45:06 +00:00
|
|
|
deobfs := func(in []byte) (*Frame, error) {
|
2020-04-08 14:58:46 +00:00
|
|
|
if len(in) < minInputLen {
|
|
|
|
return nil, fmt.Errorf("input size %v, but it cannot be shorter than %v bytes", len(in), minInputLen)
|
2018-12-09 23:45:06 +00:00
|
|
|
}
|
2019-06-14 09:48:59 +00:00
|
|
|
|
2020-04-08 19:53:09 +00:00
|
|
|
header := in[:HEADER_LEN]
|
|
|
|
pldWithOverHead := in[HEADER_LEN:] // payload + potential overhead
|
2019-06-14 09:48:59 +00:00
|
|
|
|
2020-04-08 19:53:09 +00:00
|
|
|
nonce := in[len(in)-8:]
|
2019-08-03 21:05:06 +00:00
|
|
|
salsa20.XORKeyStream(header, header, nonce, &salsaKey)
|
2019-06-14 09:48:59 +00:00
|
|
|
|
|
|
|
streamID := u32(header[0:4])
|
2019-08-27 14:06:28 +00:00
|
|
|
seq := u64(header[4:12])
|
|
|
|
closing := header[12]
|
|
|
|
extraLen := header[13]
|
2019-06-14 09:48:59 +00:00
|
|
|
|
2019-08-07 16:53:34 +00:00
|
|
|
usefulPayloadLen := len(pldWithOverHead) - int(extraLen)
|
2020-04-12 15:10:48 +00:00
|
|
|
if usefulPayloadLen < 0 || usefulPayloadLen > len(pldWithOverHead) {
|
|
|
|
return nil, errors.New("extra length is negative or extra length is greater than total pldWithOverHead length")
|
2019-08-07 16:22:40 +00:00
|
|
|
}
|
2019-08-07 16:53:34 +00:00
|
|
|
|
|
|
|
var outputPayload []byte
|
2019-06-09 11:05:41 +00:00
|
|
|
|
2019-07-31 23:43:33 +00:00
|
|
|
if payloadCipher == nil {
|
2019-08-07 16:53:34 +00:00
|
|
|
if extraLen == 0 {
|
|
|
|
outputPayload = pldWithOverHead
|
|
|
|
} else {
|
|
|
|
outputPayload = pldWithOverHead[:usefulPayloadLen]
|
|
|
|
}
|
2019-07-31 23:43:33 +00:00
|
|
|
} else {
|
2019-08-27 14:06:28 +00:00
|
|
|
_, err := payloadCipher.Open(pldWithOverHead[:0], header[:12], pldWithOverHead, nil)
|
2019-07-31 23:43:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-07 16:53:34 +00:00
|
|
|
outputPayload = pldWithOverHead[:usefulPayloadLen]
|
2019-07-31 23:43:33 +00:00
|
|
|
}
|
2019-06-14 12:28:14 +00:00
|
|
|
|
2018-12-09 23:45:06 +00:00
|
|
|
ret := &Frame{
|
2018-10-27 22:35:46 +00:00
|
|
|
StreamID: streamID,
|
|
|
|
Seq: seq,
|
|
|
|
Closing: closing,
|
2019-06-14 12:28:14 +00:00
|
|
|
Payload: outputPayload,
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
2018-12-09 23:45:06 +00:00
|
|
|
return ret, nil
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
return deobfs
|
|
|
|
}
|
2019-08-03 21:05:06 +00:00
|
|
|
|
2020-04-10 15:09:05 +00:00
|
|
|
func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfuscator, err error) {
|
|
|
|
obfuscator = Obfuscator{
|
2020-04-09 15:37:46 +00:00
|
|
|
SessionKey: sessionKey,
|
|
|
|
}
|
2019-08-03 21:05:06 +00:00
|
|
|
var payloadCipher cipher.AEAD
|
|
|
|
switch encryptionMethod {
|
2019-08-16 22:44:40 +00:00
|
|
|
case E_METHOD_PLAIN:
|
2019-08-03 21:05:06 +00:00
|
|
|
payloadCipher = nil
|
2020-04-09 15:37:46 +00:00
|
|
|
obfuscator.minOverhead = 0
|
2019-08-16 22:44:40 +00:00
|
|
|
case E_METHOD_AES_GCM:
|
2019-08-03 21:05:06 +00:00
|
|
|
var c cipher.Block
|
2020-04-07 20:15:28 +00:00
|
|
|
c, err = aes.NewCipher(sessionKey[:])
|
2019-08-03 21:05:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
payloadCipher, err = cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-09 15:37:46 +00:00
|
|
|
obfuscator.minOverhead = payloadCipher.Overhead()
|
2019-08-16 22:44:40 +00:00
|
|
|
case E_METHOD_CHACHA20_POLY1305:
|
2020-04-07 20:15:28 +00:00
|
|
|
payloadCipher, err = chacha20poly1305.New(sessionKey[:])
|
2019-08-03 21:05:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-09 15:37:46 +00:00
|
|
|
obfuscator.minOverhead = payloadCipher.Overhead()
|
2019-08-03 21:05:06 +00:00
|
|
|
default:
|
2020-04-10 15:09:05 +00:00
|
|
|
return obfuscator, errors.New("Unknown encryption method")
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 15:37:46 +00:00
|
|
|
obfuscator.Obfs = MakeObfs(sessionKey, payloadCipher)
|
|
|
|
obfuscator.Deobfs = MakeDeobfs(sessionKey, payloadCipher)
|
2019-08-03 21:05:06 +00:00
|
|
|
return
|
|
|
|
}
|