2016-07-23 07:17:51 -04:00
|
|
|
package encoding
|
2016-02-25 15:50:38 -05:00
|
|
|
|
|
|
|
import (
|
2020-06-06 05:11:30 -04:00
|
|
|
"bytes"
|
2020-06-08 04:50:50 -04:00
|
|
|
"context"
|
2020-06-06 05:11:30 -04:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2016-02-25 15:50:38 -05:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
2020-06-06 05:11:30 -04:00
|
|
|
"crypto/sha256"
|
2018-11-02 13:20:02 -04:00
|
|
|
"encoding/binary"
|
|
|
|
"hash"
|
2016-02-25 15:50:38 -05:00
|
|
|
"hash/fnv"
|
|
|
|
"io"
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/bitmask"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/crypto"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/dice"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/drain"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/vmess"
|
|
|
|
vmessaead "github.com/v2fly/v2ray-core/v5/proxy/vmess/aead"
|
2016-02-25 15:50:38 -05:00
|
|
|
)
|
|
|
|
|
2018-11-02 13:20:02 -04:00
|
|
|
func hashTimestamp(h hash.Hash, t protocol.Timestamp) []byte {
|
2018-11-13 17:19:58 -05:00
|
|
|
common.Must2(serial.WriteUint64(h, uint64(t)))
|
|
|
|
common.Must2(serial.WriteUint64(h, uint64(t)))
|
|
|
|
common.Must2(serial.WriteUint64(h, uint64(t)))
|
|
|
|
common.Must2(serial.WriteUint64(h, uint64(t)))
|
2018-11-02 13:20:02 -04:00
|
|
|
return h.Sum(nil)
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-13 03:32:29 -05:00
|
|
|
// ClientSession stores connection session info for VMess client.
|
2016-02-25 15:50:38 -05:00
|
|
|
type ClientSession struct {
|
2020-09-20 21:10:56 -04:00
|
|
|
isAEAD bool
|
2016-02-25 15:50:38 -05:00
|
|
|
idHash protocol.IDHash
|
2018-04-14 09:05:49 -04:00
|
|
|
requestBodyKey [16]byte
|
|
|
|
requestBodyIV [16]byte
|
|
|
|
responseBodyKey [16]byte
|
|
|
|
responseBodyIV [16]byte
|
|
|
|
responseReader io.Reader
|
|
|
|
responseHeader byte
|
2021-04-30 07:08:15 -04:00
|
|
|
|
|
|
|
readDrainer drain.Drainer
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-13 03:32:29 -05:00
|
|
|
// NewClientSession creates a new ClientSession.
|
2021-04-30 07:08:15 -04:00
|
|
|
func NewClientSession(ctx context.Context, isAEAD bool, idHash protocol.IDHash, behaviorSeed int64) *ClientSession {
|
2020-09-20 21:10:56 -04:00
|
|
|
session := &ClientSession{
|
|
|
|
isAEAD: isAEAD,
|
|
|
|
idHash: idHash,
|
2020-09-14 05:49:59 -04:00
|
|
|
}
|
|
|
|
|
2020-09-20 21:10:56 -04:00
|
|
|
randomBytes := make([]byte, 33) // 16 + 16 + 1
|
|
|
|
common.Must2(rand.Read(randomBytes))
|
2018-04-14 09:05:49 -04:00
|
|
|
copy(session.requestBodyKey[:], randomBytes[:16])
|
|
|
|
copy(session.requestBodyIV[:], randomBytes[16:32])
|
2016-02-25 15:50:38 -05:00
|
|
|
session.responseHeader = randomBytes[32]
|
2020-09-20 21:10:56 -04:00
|
|
|
|
|
|
|
if !session.isAEAD {
|
2020-06-06 05:11:30 -04:00
|
|
|
session.responseBodyKey = md5.Sum(session.requestBodyKey[:])
|
|
|
|
session.responseBodyIV = md5.Sum(session.requestBodyIV[:])
|
|
|
|
} else {
|
|
|
|
BodyKey := sha256.Sum256(session.requestBodyKey[:])
|
|
|
|
copy(session.responseBodyKey[:], BodyKey[:16])
|
2020-09-12 04:27:40 -04:00
|
|
|
BodyIV := sha256.Sum256(session.requestBodyIV[:])
|
2020-06-06 05:11:30 -04:00
|
|
|
copy(session.responseBodyIV[:], BodyIV[:16])
|
|
|
|
}
|
2021-04-30 07:08:15 -04:00
|
|
|
{
|
|
|
|
var err error
|
|
|
|
session.readDrainer, err = drain.NewBehaviorSeedLimitedDrainer(behaviorSeed, 18, 3266, 64)
|
|
|
|
if err != nil {
|
|
|
|
newError("unable to initialize drainer").Base(err).WriteToLog()
|
|
|
|
session.readDrainer = drain.NewNopDrainer()
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2016-02-25 15:50:38 -05:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2017-10-22 14:17:06 -04:00
|
|
|
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
|
2016-02-25 15:50:38 -05:00
|
|
|
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
2018-10-18 03:25:58 -04:00
|
|
|
account := header.User.Account.(*vmess.MemoryAccount)
|
2020-09-20 21:10:56 -04:00
|
|
|
if !c.isAEAD {
|
2020-06-06 05:11:30 -04:00
|
|
|
idHash := c.idHash(account.AnyValidID().Bytes())
|
|
|
|
common.Must2(serial.WriteUint64(idHash, uint64(timestamp)))
|
|
|
|
common.Must2(writer.Write(idHash.Sum(nil)))
|
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
2018-11-14 16:55:20 -05:00
|
|
|
common.Must(buffer.WriteByte(Version))
|
2018-07-30 16:45:06 -04:00
|
|
|
common.Must2(buffer.Write(c.requestBodyIV[:]))
|
|
|
|
common.Must2(buffer.Write(c.requestBodyKey[:]))
|
2018-11-14 16:55:20 -05:00
|
|
|
common.Must(buffer.WriteByte(c.responseHeader))
|
|
|
|
common.Must(buffer.WriteByte(byte(header.Option)))
|
2017-10-27 15:11:45 -04:00
|
|
|
|
2020-11-17 15:23:30 -05:00
|
|
|
paddingLen := dice.Roll(16)
|
|
|
|
security := byte(paddingLen<<4) | byte(header.Security)
|
2018-11-14 16:55:20 -05:00
|
|
|
common.Must2(buffer.Write([]byte{security, byte(0), byte(header.Command)}))
|
2017-05-17 15:13:01 -04:00
|
|
|
|
|
|
|
if header.Command != protocol.RequestCommandMux {
|
2018-02-23 17:42:01 -05:00
|
|
|
if err := addrParser.WriteAddressPort(buffer, header.Address, header.Port); err != nil {
|
|
|
|
return newError("failed to writer address and port").Base(err)
|
2017-05-17 15:13:01 -04:00
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:23:30 -05:00
|
|
|
if paddingLen > 0 {
|
|
|
|
common.Must2(buffer.ReadFullFrom(rand.Reader, int32(paddingLen)))
|
2016-12-09 19:05:55 -05:00
|
|
|
}
|
2016-12-07 11:32:40 -05:00
|
|
|
|
2018-07-30 16:54:24 -04:00
|
|
|
{
|
|
|
|
fnv1a := fnv.New32a()
|
|
|
|
common.Must2(fnv1a.Write(buffer.Bytes()))
|
2018-11-02 16:34:04 -04:00
|
|
|
hashBytes := buffer.Extend(int32(fnv1a.Size()))
|
|
|
|
fnv1a.Sum(hashBytes[:0])
|
2018-07-30 16:54:24 -04:00
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
|
2020-09-20 21:10:56 -04:00
|
|
|
if !c.isAEAD {
|
2020-06-06 05:11:30 -04:00
|
|
|
iv := hashTimestamp(md5.New(), timestamp)
|
2020-10-11 07:22:46 -04:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv)
|
2020-06-06 05:11:30 -04:00
|
|
|
aesStream.XORKeyStream(buffer.Bytes(), buffer.Bytes())
|
|
|
|
common.Must2(writer.Write(buffer.Bytes()))
|
|
|
|
} else {
|
|
|
|
var fixedLengthCmdKey [16]byte
|
|
|
|
copy(fixedLengthCmdKey[:], account.ID.CmdKey())
|
|
|
|
vmessout := vmessaead.SealVMessAEADHeader(fixedLengthCmdKey, buffer.Bytes())
|
|
|
|
common.Must2(io.Copy(writer, bytes.NewReader(vmessout)))
|
|
|
|
}
|
|
|
|
|
2017-10-22 14:17:06 -04:00
|
|
|
return nil
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:27:49 -04:00
|
|
|
func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, writer io.Writer) buf.Writer {
|
2017-04-23 07:30:08 -04:00
|
|
|
var sizeParser crypto.ChunkSizeEncoder = crypto.PlainChunkSizeParser{}
|
2017-02-14 08:16:43 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
2018-04-14 09:05:49 -04:00
|
|
|
sizeParser = NewShakeSizeParser(c.requestBodyIV[:])
|
2017-02-14 08:16:43 -05:00
|
|
|
}
|
2018-11-13 03:51:55 -05:00
|
|
|
var padding crypto.PaddingLengthGenerator
|
2018-07-07 09:42:24 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionGlobalPadding) {
|
|
|
|
padding = sizeParser.(crypto.PaddingLengthGenerator)
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:13:02 -05:00
|
|
|
switch request.Security {
|
|
|
|
case protocol.SecurityType_NONE:
|
2016-12-07 11:32:40 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2018-02-09 16:29:30 -05:00
|
|
|
if request.Command.TransferType() == protocol.TransferTypeStream {
|
2017-05-01 18:28:16 -04:00
|
|
|
return crypto.NewChunkStreamWriter(sizeParser, writer)
|
|
|
|
}
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(NoOpAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, protocol.TransferTypePacket, padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
|
|
|
return buf.NewWriter(writer)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_LEGACY:
|
2018-04-14 09:05:49 -04:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(c.requestBodyKey[:], c.requestBodyIV[:])
|
2016-12-07 11:32:40 -05:00
|
|
|
cryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, cryptionWriter, request.Command.TransferType(), padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
2018-07-31 10:05:57 -04:00
|
|
|
return &buf.SequentialWriter{Writer: cryptionWriter}
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_AES128_GCM:
|
2018-09-12 09:43:36 -04:00
|
|
|
aead := crypto.NewAesGcm(c.requestBodyKey[:])
|
2016-12-07 11:32:40 -05:00
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2021-04-28 18:29:42 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionAuthenticatedLength) {
|
|
|
|
AuthenticatedLengthKey := vmessaead.KDF16(c.requestBodyKey[:], "auth_len")
|
|
|
|
AuthenticatedLengthKeyAEAD := crypto.NewAesGcm(AuthenticatedLengthKey)
|
|
|
|
|
|
|
|
lengthAuth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: AuthenticatedLengthKeyAEAD,
|
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
}
|
|
|
|
sizeParser = NewAEADSizeParser(lengthAuth)
|
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_CHACHA20_POLY1305:
|
2018-09-12 09:43:36 -04:00
|
|
|
aead, err := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.requestBodyKey[:]))
|
|
|
|
common.Must(err)
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2021-04-28 18:29:42 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionAuthenticatedLength) {
|
|
|
|
AuthenticatedLengthKey := vmessaead.KDF16(c.requestBodyKey[:], "auth_len")
|
|
|
|
AuthenticatedLengthKeyAEAD, err := chacha20poly1305.New(GenerateChacha20Poly1305Key(AuthenticatedLengthKey))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
lengthAuth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: AuthenticatedLengthKeyAEAD,
|
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
}
|
|
|
|
sizeParser = NewAEADSizeParser(lengthAuth)
|
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
default:
|
|
|
|
panic("Unknown security type.")
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2017-07-25 16:21:59 -04:00
|
|
|
func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.ResponseHeader, error) {
|
2020-09-20 21:10:56 -04:00
|
|
|
if !c.isAEAD {
|
2020-06-06 05:11:30 -04:00
|
|
|
aesStream := crypto.NewAesDecryptionStream(c.responseBodyKey[:], c.responseBodyIV[:])
|
|
|
|
c.responseReader = crypto.NewCryptionReader(aesStream, reader)
|
|
|
|
} else {
|
2020-10-11 07:22:46 -04:00
|
|
|
aeadResponseHeaderLengthEncryptionKey := vmessaead.KDF16(c.responseBodyKey[:], vmessaead.KDFSaltConstAEADRespHeaderLenKey)
|
|
|
|
aeadResponseHeaderLengthEncryptionIV := vmessaead.KDF(c.responseBodyIV[:], vmessaead.KDFSaltConstAEADRespHeaderLenIV)[:12]
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2020-06-08 01:21:59 -04:00
|
|
|
aeadResponseHeaderLengthEncryptionKeyAESBlock := common.Must2(aes.NewCipher(aeadResponseHeaderLengthEncryptionKey)).(cipher.Block)
|
|
|
|
aeadResponseHeaderLengthEncryptionAEAD := common.Must2(cipher.NewGCM(aeadResponseHeaderLengthEncryptionKeyAESBlock)).(cipher.AEAD)
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2020-06-08 01:21:59 -04:00
|
|
|
var aeadEncryptedResponseHeaderLength [18]byte
|
|
|
|
var decryptedResponseHeaderLength int
|
|
|
|
var decryptedResponseHeaderLengthBinaryDeserializeBuffer uint16
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2021-04-30 07:08:15 -04:00
|
|
|
if n, err := io.ReadFull(reader, aeadEncryptedResponseHeaderLength[:]); err != nil {
|
|
|
|
c.readDrainer.AcknowledgeReceive(n)
|
|
|
|
return nil, drain.WithError(c.readDrainer, reader, newError("Unable to Read Header Len").Base(err))
|
2021-11-27 04:16:41 -05:00
|
|
|
} else { // nolint: revive
|
2021-04-30 07:08:15 -04:00
|
|
|
c.readDrainer.AcknowledgeReceive(n)
|
2020-06-06 05:11:30 -04:00
|
|
|
}
|
2020-06-08 01:21:59 -04:00
|
|
|
if decryptedResponseHeaderLengthBinaryBuffer, err := aeadResponseHeaderLengthEncryptionAEAD.Open(nil, aeadResponseHeaderLengthEncryptionIV, aeadEncryptedResponseHeaderLength[:], nil); err != nil {
|
2021-04-30 07:08:15 -04:00
|
|
|
return nil, drain.WithError(c.readDrainer, reader, newError("Failed To Decrypt Length").Base(err))
|
2021-11-27 04:16:41 -05:00
|
|
|
} else { // nolint: revive
|
2020-06-08 01:21:59 -04:00
|
|
|
common.Must(binary.Read(bytes.NewReader(decryptedResponseHeaderLengthBinaryBuffer), binary.BigEndian, &decryptedResponseHeaderLengthBinaryDeserializeBuffer))
|
|
|
|
decryptedResponseHeaderLength = int(decryptedResponseHeaderLengthBinaryDeserializeBuffer)
|
2020-06-06 05:11:30 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
aeadResponseHeaderPayloadEncryptionKey := vmessaead.KDF16(c.responseBodyKey[:], vmessaead.KDFSaltConstAEADRespHeaderPayloadKey)
|
|
|
|
aeadResponseHeaderPayloadEncryptionIV := vmessaead.KDF(c.responseBodyIV[:], vmessaead.KDFSaltConstAEADRespHeaderPayloadIV)[:12]
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2020-06-08 01:21:59 -04:00
|
|
|
aeadResponseHeaderPayloadEncryptionKeyAESBlock := common.Must2(aes.NewCipher(aeadResponseHeaderPayloadEncryptionKey)).(cipher.Block)
|
|
|
|
aeadResponseHeaderPayloadEncryptionAEAD := common.Must2(cipher.NewGCM(aeadResponseHeaderPayloadEncryptionKeyAESBlock)).(cipher.AEAD)
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2020-06-08 01:21:59 -04:00
|
|
|
encryptedResponseHeaderBuffer := make([]byte, decryptedResponseHeaderLength+16)
|
2020-06-06 05:11:30 -04:00
|
|
|
|
2021-04-30 07:08:15 -04:00
|
|
|
if n, err := io.ReadFull(reader, encryptedResponseHeaderBuffer); err != nil {
|
|
|
|
c.readDrainer.AcknowledgeReceive(n)
|
|
|
|
return nil, drain.WithError(c.readDrainer, reader, newError("Unable to Read Header Data").Base(err))
|
2021-11-27 04:16:41 -05:00
|
|
|
} else { // nolint: revive
|
2021-04-30 07:08:15 -04:00
|
|
|
c.readDrainer.AcknowledgeReceive(n)
|
2020-06-06 05:11:30 -04:00
|
|
|
}
|
|
|
|
|
2020-06-08 01:21:59 -04:00
|
|
|
if decryptedResponseHeaderBuffer, err := aeadResponseHeaderPayloadEncryptionAEAD.Open(nil, aeadResponseHeaderPayloadEncryptionIV, encryptedResponseHeaderBuffer, nil); err != nil {
|
2021-04-30 07:08:15 -04:00
|
|
|
return nil, drain.WithError(c.readDrainer, reader, newError("Failed To Decrypt Payload").Base(err))
|
2021-11-27 04:16:41 -05:00
|
|
|
} else { // nolint: revive
|
2020-06-08 01:21:59 -04:00
|
|
|
c.responseReader = bytes.NewReader(decryptedResponseHeaderBuffer)
|
2020-06-06 05:11:30 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2018-11-15 15:32:27 -05:00
|
|
|
buffer := buf.StackNew()
|
2017-10-27 15:11:45 -04:00
|
|
|
defer buffer.Release()
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := buffer.ReadFullFrom(c.responseReader, 4); err != nil {
|
2019-08-26 21:29:29 -04:00
|
|
|
return nil, newError("failed to read response header").Base(err).AtWarning()
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
if buffer.Byte(0) != c.responseHeader {
|
|
|
|
return nil, newError("unexpected response header. Expecting ", int(c.responseHeader), " but actually ", int(buffer.Byte(0)))
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:34:25 -04:00
|
|
|
header := &protocol.ResponseHeader{
|
2017-10-27 15:11:45 -04:00
|
|
|
Option: bitmask.Byte(buffer.Byte(1)),
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
if buffer.Byte(2) != 0 {
|
|
|
|
cmdID := buffer.Byte(2)
|
2018-04-01 18:44:47 -04:00
|
|
|
dataLen := int32(buffer.Byte(3))
|
2017-10-27 15:11:45 -04:00
|
|
|
|
2018-11-02 10:01:33 -04:00
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(c.responseReader, dataLen); err != nil {
|
2018-02-22 09:26:00 -05:00
|
|
|
return nil, newError("failed to read response command").Base(err)
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2017-10-27 15:11:45 -04:00
|
|
|
command, err := UnmarshalCommand(cmdID, buffer.Bytes())
|
2016-04-25 18:46:04 -04:00
|
|
|
if err == nil {
|
|
|
|
header.Command = command
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2020-09-20 21:10:56 -04:00
|
|
|
if c.isAEAD {
|
2020-06-06 05:11:30 -04:00
|
|
|
aesStream := crypto.NewAesDecryptionStream(c.responseBodyKey[:], c.responseBodyIV[:])
|
|
|
|
c.responseReader = crypto.NewCryptionReader(aesStream, reader)
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:21:59 -04:00
|
|
|
func (c *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, reader io.Reader) buf.Reader {
|
2017-04-23 07:30:08 -04:00
|
|
|
var sizeParser crypto.ChunkSizeDecoder = crypto.PlainChunkSizeParser{}
|
2017-02-14 08:16:43 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
2018-04-14 09:05:49 -04:00
|
|
|
sizeParser = NewShakeSizeParser(c.responseBodyIV[:])
|
2017-02-14 08:16:43 -05:00
|
|
|
}
|
2018-11-13 03:51:55 -05:00
|
|
|
var padding crypto.PaddingLengthGenerator
|
2018-07-07 09:42:24 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionGlobalPadding) {
|
|
|
|
padding = sizeParser.(crypto.PaddingLengthGenerator)
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:13:02 -05:00
|
|
|
switch request.Security {
|
|
|
|
case protocol.SecurityType_NONE:
|
2016-12-07 11:32:40 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2018-02-09 16:29:30 -05:00
|
|
|
if request.Command.TransferType() == protocol.TransferTypeStream {
|
2017-05-01 18:28:16 -04:00
|
|
|
return crypto.NewChunkStreamReader(sizeParser, reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(NoOpAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
|
|
|
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, protocol.TransferTypePacket, padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
|
|
|
return buf.NewReader(reader)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_LEGACY:
|
2016-12-07 11:32:40 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, c.responseReader, request.Command.TransferType(), padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
2017-07-25 16:21:59 -04:00
|
|
|
return buf.NewReader(c.responseReader)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_AES128_GCM:
|
2018-09-12 09:43:36 -04:00
|
|
|
aead := crypto.NewAesGcm(c.responseBodyKey[:])
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.responseBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2021-04-28 18:29:42 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionAuthenticatedLength) {
|
|
|
|
AuthenticatedLengthKey := vmessaead.KDF16(c.requestBodyKey[:], "auth_len")
|
|
|
|
AuthenticatedLengthKeyAEAD := crypto.NewAesGcm(AuthenticatedLengthKey)
|
|
|
|
|
|
|
|
lengthAuth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: AuthenticatedLengthKeyAEAD,
|
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
}
|
|
|
|
sizeParser = NewAEADSizeParser(lengthAuth)
|
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_CHACHA20_POLY1305:
|
2018-04-14 09:05:49 -04:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.responseBodyKey[:]))
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.responseBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2021-04-28 18:29:42 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionAuthenticatedLength) {
|
|
|
|
AuthenticatedLengthKey := vmessaead.KDF16(c.requestBodyKey[:], "auth_len")
|
|
|
|
AuthenticatedLengthKeyAEAD, err := chacha20poly1305.New(GenerateChacha20Poly1305Key(AuthenticatedLengthKey))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
lengthAuth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: AuthenticatedLengthKeyAEAD,
|
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
}
|
|
|
|
sizeParser = NewAEADSizeParser(lengthAuth)
|
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
default:
|
|
|
|
panic("Unknown security type.")
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 07:10:12 -04:00
|
|
|
func GenerateChunkNonce(nonce []byte, size uint32) crypto.BytesGenerator {
|
|
|
|
c := append([]byte(nil), nonce...)
|
|
|
|
count := uint16(0)
|
|
|
|
return func() []byte {
|
2018-11-02 13:20:02 -04:00
|
|
|
binary.BigEndian.PutUint16(c, count)
|
2018-04-14 07:10:12 -04:00
|
|
|
count++
|
|
|
|
return c[:size]
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|