2016-12-06 18:31:15 -05:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/cipher"
|
|
|
|
"io"
|
2016-12-07 07:00:32 -05:00
|
|
|
|
2017-05-23 18:54:30 -04:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-05-02 16:23:07 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-06 18:31:15 -05:00
|
|
|
)
|
|
|
|
|
2018-04-14 07:10:12 -04:00
|
|
|
type BytesGenerator func() []byte
|
2016-12-07 07:00:32 -05:00
|
|
|
|
2018-04-14 07:10:12 -04:00
|
|
|
func GenerateEmptyBytes() BytesGenerator {
|
|
|
|
var b [1]byte
|
|
|
|
return func() []byte {
|
|
|
|
return b[:0]
|
|
|
|
}
|
2017-11-25 18:51:54 -05:00
|
|
|
}
|
|
|
|
|
2018-04-14 07:10:12 -04:00
|
|
|
func GenerateStaticBytes(content []byte) BytesGenerator {
|
|
|
|
return func() []byte {
|
|
|
|
return content
|
2017-11-25 18:51:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 07:10:12 -04:00
|
|
|
func GenerateIncreasingNonce(nonce []byte) BytesGenerator {
|
|
|
|
c := append([]byte(nil), nonce...)
|
|
|
|
return func() []byte {
|
|
|
|
for i := range c {
|
|
|
|
c[i]++
|
|
|
|
if c[i] != 0 {
|
|
|
|
break
|
|
|
|
}
|
2017-11-25 18:51:54 -05:00
|
|
|
}
|
2018-04-14 07:10:12 -04:00
|
|
|
return c
|
2017-11-25 18:51:54 -05:00
|
|
|
}
|
2018-04-14 07:10:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateInitialAEADNonce() BytesGenerator {
|
|
|
|
return GenerateIncreasingNonce([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
|
2017-11-25 18:51:54 -05:00
|
|
|
}
|
|
|
|
|
2016-12-07 07:00:32 -05:00
|
|
|
type Authenticator interface {
|
|
|
|
NonceSize() int
|
|
|
|
Overhead() int
|
|
|
|
Open(dst, cipherText []byte) ([]byte, error)
|
|
|
|
Seal(dst, plainText []byte) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AEADAuthenticator struct {
|
|
|
|
cipher.AEAD
|
|
|
|
NonceGenerator BytesGenerator
|
|
|
|
AdditionalDataGenerator BytesGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AEADAuthenticator) Open(dst, cipherText []byte) ([]byte, error) {
|
2018-04-14 07:10:12 -04:00
|
|
|
iv := v.NonceGenerator()
|
2016-12-07 07:00:32 -05:00
|
|
|
if len(iv) != v.AEAD.NonceSize() {
|
2017-04-09 09:04:04 -04:00
|
|
|
return nil, newError("invalid AEAD nonce size: ", len(iv))
|
2016-12-07 07:00:32 -05:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:55:35 -05:00
|
|
|
var additionalData []byte
|
|
|
|
if v.AdditionalDataGenerator != nil {
|
2018-04-14 07:10:12 -04:00
|
|
|
additionalData = v.AdditionalDataGenerator()
|
2017-11-26 10:55:35 -05:00
|
|
|
}
|
2016-12-07 07:00:32 -05:00
|
|
|
return v.AEAD.Open(dst, iv, cipherText, additionalData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) {
|
2018-04-14 07:10:12 -04:00
|
|
|
iv := v.NonceGenerator()
|
2016-12-07 07:00:32 -05:00
|
|
|
if len(iv) != v.AEAD.NonceSize() {
|
2017-04-09 09:04:04 -04:00
|
|
|
return nil, newError("invalid AEAD nonce size: ", len(iv))
|
2016-12-07 07:00:32 -05:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:55:35 -05:00
|
|
|
var additionalData []byte
|
|
|
|
if v.AdditionalDataGenerator != nil {
|
2018-04-14 07:10:12 -04:00
|
|
|
additionalData = v.AdditionalDataGenerator()
|
2017-11-26 10:55:35 -05:00
|
|
|
}
|
2016-12-07 07:00:32 -05:00
|
|
|
return v.AEAD.Seal(dst, iv, plainText, additionalData), nil
|
|
|
|
}
|
2016-12-06 18:31:15 -05:00
|
|
|
|
|
|
|
type AuthenticationReader struct {
|
2017-05-02 16:23:07 -04:00
|
|
|
auth Authenticator
|
2017-12-03 07:23:24 -05:00
|
|
|
reader *buf.BufferedReader
|
2017-05-02 16:23:07 -04:00
|
|
|
sizeParser ChunkSizeDecoder
|
|
|
|
transferType protocol.TransferType
|
2018-04-01 17:31:53 -04:00
|
|
|
size int32
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func NewAuthenticationReader(auth Authenticator, sizeParser ChunkSizeDecoder, reader io.Reader, transferType protocol.TransferType) *AuthenticationReader {
|
2016-12-06 18:31:15 -05:00
|
|
|
return &AuthenticationReader{
|
2017-05-02 16:23:07 -04:00
|
|
|
auth: auth,
|
2017-12-03 07:23:24 -05:00
|
|
|
reader: buf.NewBufferedReader(buf.NewReader(reader)),
|
2017-05-02 16:23:07 -04:00
|
|
|
sizeParser: sizeParser,
|
|
|
|
transferType: transferType,
|
2018-04-01 17:31:53 -04:00
|
|
|
size: -1,
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 17:31:53 -04:00
|
|
|
func (r *AuthenticationReader) readSize() (int32, error) {
|
|
|
|
if r.size != -1 {
|
|
|
|
s := r.size
|
|
|
|
r.size = -1
|
|
|
|
return s, nil
|
|
|
|
}
|
2017-12-03 07:23:24 -05:00
|
|
|
sizeBytes := make([]byte, r.sizeParser.SizeBytes())
|
2018-04-11 10:15:29 -04:00
|
|
|
if _, err := io.ReadFull(r.reader, sizeBytes); err != nil {
|
2017-12-03 07:23:24 -05:00
|
|
|
return 0, err
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
2017-12-03 07:23:24 -05:00
|
|
|
size, err := r.sizeParser.Decode(sizeBytes)
|
2018-04-01 17:31:53 -04:00
|
|
|
return int32(size), err
|
2017-04-27 07:31:09 -04:00
|
|
|
}
|
|
|
|
|
2018-04-01 17:31:53 -04:00
|
|
|
var errSoft = newError("waiting for more data")
|
|
|
|
|
|
|
|
func (r *AuthenticationReader) readInternal(soft bool) (*buf.Buffer, error) {
|
2018-04-03 18:29:30 -04:00
|
|
|
if soft && r.reader.BufferedBytes() < r.sizeParser.SizeBytes() {
|
2018-04-01 17:31:53 -04:00
|
|
|
return nil, errSoft
|
|
|
|
}
|
|
|
|
|
2017-12-03 07:23:24 -05:00
|
|
|
size, err := r.readSize()
|
|
|
|
if err != nil {
|
2017-04-27 07:31:09 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-06 07:31:36 -05:00
|
|
|
|
2018-04-01 17:31:53 -04:00
|
|
|
if size == -2 || size == int32(r.auth.Overhead()) {
|
|
|
|
r.size = -2
|
2017-04-27 07:31:09 -04:00
|
|
|
return nil, io.EOF
|
2016-12-07 09:00:04 -05:00
|
|
|
}
|
2016-12-07 07:00:32 -05:00
|
|
|
|
2018-04-01 17:31:53 -04:00
|
|
|
if soft && size > r.reader.BufferedBytes() {
|
|
|
|
r.size = size
|
|
|
|
return nil, errSoft
|
|
|
|
}
|
|
|
|
|
2018-04-02 16:01:55 -04:00
|
|
|
b := buf.NewSize(size)
|
2018-04-01 18:44:47 -04:00
|
|
|
if err := b.Reset(buf.ReadFullFrom(r.reader, size)); err != nil {
|
2017-12-03 07:23:24 -05:00
|
|
|
b.Release()
|
2017-04-27 07:31:09 -04:00
|
|
|
return nil, err
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 14:00:50 -04:00
|
|
|
rb, err := r.auth.Open(b.BytesTo(0), b.BytesTo(size))
|
2017-04-27 07:31:09 -04:00
|
|
|
if err != nil {
|
2017-12-03 07:23:24 -05:00
|
|
|
b.Release()
|
2017-04-27 07:31:09 -04:00
|
|
|
return nil, err
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
2018-04-02 14:00:50 -04:00
|
|
|
b.Slice(0, int32(len(rb)))
|
2018-04-01 17:31:53 -04:00
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
|
|
b, err := r.readInternal(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mb := buf.NewMultiBufferCap(32)
|
|
|
|
mb.Append(b)
|
|
|
|
|
|
|
|
for {
|
|
|
|
b, err := r.readInternal(true)
|
|
|
|
if err == errSoft || err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
mb.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mb.Append(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mb, nil
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 18:31:15 -05:00
|
|
|
type AuthenticationWriter struct {
|
2017-05-02 16:23:07 -04:00
|
|
|
auth Authenticator
|
2017-11-09 16:33:15 -05:00
|
|
|
writer buf.Writer
|
2017-05-02 16:23:07 -04:00
|
|
|
sizeParser ChunkSizeEncoder
|
|
|
|
transferType protocol.TransferType
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func NewAuthenticationWriter(auth Authenticator, sizeParser ChunkSizeEncoder, writer io.Writer, transferType protocol.TransferType) *AuthenticationWriter {
|
2016-12-06 18:31:15 -05:00
|
|
|
return &AuthenticationWriter{
|
2017-05-02 16:23:07 -04:00
|
|
|
auth: auth,
|
2017-11-09 16:33:15 -05:00
|
|
|
writer: buf.NewWriter(writer),
|
2017-05-02 16:23:07 -04:00
|
|
|
sizeParser: sizeParser,
|
|
|
|
transferType: transferType,
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
func (w *AuthenticationWriter) seal(b *buf.Buffer) (*buf.Buffer, error) {
|
2018-04-02 14:00:50 -04:00
|
|
|
encryptedSize := int(b.Len()) + w.auth.Overhead()
|
2017-11-09 16:33:15 -05:00
|
|
|
|
|
|
|
eb := buf.New()
|
|
|
|
common.Must(eb.Reset(func(bb []byte) (int, error) {
|
|
|
|
w.sizeParser.Encode(uint16(encryptedSize), bb[:0])
|
2018-04-03 05:32:03 -04:00
|
|
|
return int(w.sizeParser.SizeBytes()), nil
|
2017-11-09 16:33:15 -05:00
|
|
|
}))
|
|
|
|
if err := eb.AppendSupplier(func(bb []byte) (int, error) {
|
|
|
|
_, err := w.auth.Seal(bb[:0], b.Bytes())
|
|
|
|
return encryptedSize, err
|
|
|
|
}); err != nil {
|
|
|
|
eb.Release()
|
|
|
|
return nil, err
|
2017-05-23 18:54:30 -04:00
|
|
|
}
|
2016-12-07 16:52:56 -05:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
return eb, nil
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
2017-04-15 15:07:23 -04:00
|
|
|
|
2017-05-01 18:28:16 -04:00
|
|
|
func (w *AuthenticationWriter) writeStream(mb buf.MultiBuffer) error {
|
2017-04-15 16:29:38 -04:00
|
|
|
defer mb.Release()
|
|
|
|
|
2018-04-03 05:32:03 -04:00
|
|
|
payloadSize := buf.Size - int32(w.auth.Overhead()) - w.sizeParser.SizeBytes()
|
2018-04-02 14:00:50 -04:00
|
|
|
mb2Write := buf.NewMultiBufferCap(int32(len(mb) + 10))
|
2017-11-09 16:33:15 -05:00
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
for {
|
2017-11-09 16:33:15 -05:00
|
|
|
b := buf.New()
|
|
|
|
common.Must(b.Reset(func(bb []byte) (int, error) {
|
2017-11-21 16:03:43 -05:00
|
|
|
return mb.Read(bb[:payloadSize])
|
2017-11-09 16:33:15 -05:00
|
|
|
}))
|
|
|
|
eb, err := w.seal(b)
|
|
|
|
b.Release()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
mb2Write.Release()
|
2017-05-23 18:54:30 -04:00
|
|
|
return err
|
2017-04-15 15:07:23 -04:00
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
mb2Write.Append(eb)
|
2017-04-15 15:07:23 -04:00
|
|
|
if mb.IsEmpty() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 07:31:09 -04:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
return w.writer.WriteMultiBuffer(mb2Write)
|
2017-04-15 15:07:23 -04:00
|
|
|
}
|
2017-05-01 18:28:16 -04:00
|
|
|
|
|
|
|
func (w *AuthenticationWriter) writePacket(mb buf.MultiBuffer) error {
|
|
|
|
defer mb.Release()
|
|
|
|
|
2018-04-02 14:00:50 -04:00
|
|
|
mb2Write := buf.NewMultiBufferCap(int32(len(mb)) + 1)
|
2017-11-09 16:33:15 -05:00
|
|
|
|
2018-04-01 16:41:38 -04:00
|
|
|
for !mb.IsEmpty() {
|
2017-05-01 18:28:16 -04:00
|
|
|
b := mb.SplitFirst()
|
|
|
|
if b == nil {
|
2018-04-01 16:41:38 -04:00
|
|
|
continue
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
eb, err := w.seal(b)
|
|
|
|
b.Release()
|
|
|
|
if err != nil {
|
|
|
|
mb2Write.Release()
|
2017-05-23 18:54:30 -04:00
|
|
|
return err
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
mb2Write.Append(eb)
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
return w.writer.WriteMultiBuffer(mb2Write)
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
func (w *AuthenticationWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
2018-04-09 10:48:25 -04:00
|
|
|
if mb.IsEmpty() {
|
|
|
|
b := buf.New()
|
|
|
|
defer b.Release()
|
|
|
|
|
|
|
|
eb, _ := w.seal(b)
|
|
|
|
return w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(eb))
|
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
if w.transferType == protocol.TransferTypeStream {
|
2017-05-01 18:28:16 -04:00
|
|
|
return w.writeStream(mb)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.writePacket(mb)
|
|
|
|
}
|