2016-12-06 18:31:15 -05:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/cipher"
|
2018-07-07 09:42:24 -04:00
|
|
|
"crypto/rand"
|
2016-12-06 18:31:15 -05:00
|
|
|
"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"
|
2018-08-16 06:05:33 -04:00
|
|
|
"v2ray.com/core/common/bytespool"
|
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
|
2018-08-17 10:27:35 -04:00
|
|
|
sizeBytes []byte
|
2017-05-02 16:23:07 -04:00
|
|
|
transferType protocol.TransferType
|
2018-07-07 09:42:24 -04:00
|
|
|
padding PaddingLengthGenerator
|
2018-07-09 16:27:24 -04:00
|
|
|
size uint16
|
|
|
|
paddingLen uint16
|
|
|
|
hasSize bool
|
|
|
|
done bool
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-07 09:42:24 -04:00
|
|
|
func NewAuthenticationReader(auth Authenticator, sizeParser ChunkSizeDecoder, reader io.Reader, transferType protocol.TransferType, paddingLen PaddingLengthGenerator) *AuthenticationReader {
|
2018-08-17 10:27:35 -04:00
|
|
|
r := &AuthenticationReader{
|
2017-05-02 16:23:07 -04:00
|
|
|
auth: auth,
|
|
|
|
sizeParser: sizeParser,
|
|
|
|
transferType: transferType,
|
2018-07-07 09:42:24 -04:00
|
|
|
padding: paddingLen,
|
2018-08-17 10:27:35 -04:00
|
|
|
sizeBytes: make([]byte, sizeParser.SizeBytes()),
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
2018-08-17 10:27:35 -04:00
|
|
|
if breader, ok := reader.(*buf.BufferedReader); ok {
|
|
|
|
r.reader = breader
|
|
|
|
} else {
|
|
|
|
r.reader = &buf.BufferedReader{Reader: buf.NewReader(reader)}
|
|
|
|
}
|
|
|
|
return r
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-09 16:27:24 -04:00
|
|
|
func (r *AuthenticationReader) readSize() (uint16, uint16, error) {
|
|
|
|
if r.hasSize {
|
|
|
|
r.hasSize = false
|
|
|
|
return r.size, r.paddingLen, nil
|
2018-04-01 17:31:53 -04:00
|
|
|
}
|
2018-08-17 10:27:35 -04:00
|
|
|
if _, err := io.ReadFull(r.reader, r.sizeBytes); err != nil {
|
2018-07-07 09:42:24 -04:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
2018-07-09 16:27:24 -04:00
|
|
|
var padding uint16
|
2018-07-07 09:42:24 -04:00
|
|
|
if r.padding != nil {
|
2018-07-09 16:27:24 -04:00
|
|
|
padding = r.padding.NextPaddingLen()
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
2018-08-17 10:27:35 -04:00
|
|
|
size, err := r.sizeParser.Decode(r.sizeBytes)
|
2018-07-09 16:27:24 -04:00
|
|
|
return size, padding, err
|
2017-04-27 07:31:09 -04:00
|
|
|
}
|
|
|
|
|
2018-04-01 17:31:53 -04:00
|
|
|
var errSoft = newError("waiting for more data")
|
|
|
|
|
2018-08-16 06:05:33 -04:00
|
|
|
func (r *AuthenticationReader) readBuffer(size int32, padding int32) (*buf.Buffer, error) {
|
|
|
|
b := buf.New()
|
|
|
|
if err := b.Reset(buf.ReadFullFrom(r.reader, size)); err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
size -= padding
|
|
|
|
rb, err := r.auth.Open(b.BytesTo(0), b.BytesTo(size))
|
|
|
|
if err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.Resize(0, int32(len(rb)))
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AuthenticationReader) readInternal(soft bool, mb *buf.MultiBuffer) error {
|
2018-04-03 18:29:30 -04:00
|
|
|
if soft && r.reader.BufferedBytes() < r.sizeParser.SizeBytes() {
|
2018-08-16 06:05:33 -04:00
|
|
|
return errSoft
|
2018-04-01 17:31:53 -04:00
|
|
|
}
|
|
|
|
|
2018-07-09 16:27:24 -04:00
|
|
|
if r.done {
|
2018-08-16 06:05:33 -04:00
|
|
|
return io.EOF
|
2018-07-09 16:27:24 -04:00
|
|
|
}
|
|
|
|
|
2018-07-07 09:42:24 -04:00
|
|
|
size, padding, err := r.readSize()
|
2017-12-03 07:23:24 -05:00
|
|
|
if err != nil {
|
2018-08-16 06:05:33 -04:00
|
|
|
return err
|
2017-04-27 07:31:09 -04:00
|
|
|
}
|
2017-02-06 07:31:36 -05:00
|
|
|
|
2018-07-09 16:27:24 -04:00
|
|
|
if size == uint16(r.auth.Overhead())+padding {
|
|
|
|
r.done = true
|
2018-08-16 06:05:33 -04:00
|
|
|
return io.EOF
|
2016-12-07 09:00:04 -05:00
|
|
|
}
|
2016-12-07 07:00:32 -05:00
|
|
|
|
2018-07-09 16:27:24 -04:00
|
|
|
if soft && int32(size) > r.reader.BufferedBytes() {
|
2018-04-01 17:31:53 -04:00
|
|
|
r.size = size
|
2018-07-07 09:42:24 -04:00
|
|
|
r.paddingLen = padding
|
2018-07-09 16:27:24 -04:00
|
|
|
r.hasSize = true
|
2018-08-16 06:05:33 -04:00
|
|
|
return errSoft
|
2018-04-01 17:31:53 -04:00
|
|
|
}
|
|
|
|
|
2018-08-16 06:05:33 -04:00
|
|
|
if size <= buf.Size {
|
|
|
|
b, err := r.readBuffer(int32(size), int32(padding))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
mb.Append(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := bytespool.Alloc(int32(size))
|
|
|
|
defer bytespool.Free(payload)
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(r.reader, payload[:size]); err != nil {
|
|
|
|
return err
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-07 09:42:24 -04:00
|
|
|
size -= padding
|
|
|
|
|
2018-08-16 06:05:33 -04:00
|
|
|
rb, err := r.auth.Open(payload[:0], payload[:size])
|
2017-04-27 07:31:09 -04:00
|
|
|
if err != nil {
|
2018-08-16 06:05:33 -04:00
|
|
|
return err
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
2018-04-01 17:31:53 -04:00
|
|
|
|
2018-08-16 06:05:33 -04:00
|
|
|
common.Must2(mb.Write(rb))
|
|
|
|
return nil
|
2018-04-01 17:31:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
2018-07-31 06:37:59 -04:00
|
|
|
const readSize = 16
|
|
|
|
mb := buf.NewMultiBufferCap(readSize)
|
2018-08-16 06:05:33 -04:00
|
|
|
if err := r.readInternal(false, &mb); err != nil {
|
|
|
|
mb.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-01 17:31:53 -04:00
|
|
|
|
2018-07-31 06:37:59 -04:00
|
|
|
for i := 1; i < readSize; i++ {
|
2018-08-16 06:05:33 -04:00
|
|
|
err := r.readInternal(true, &mb)
|
2018-04-01 17:31:53 -04:00
|
|
|
if err == errSoft || err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
mb.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-07-07 09:42:24 -04:00
|
|
|
padding PaddingLengthGenerator
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-07 09:42:24 -04:00
|
|
|
func NewAuthenticationWriter(auth Authenticator, sizeParser ChunkSizeEncoder, writer io.Writer, transferType protocol.TransferType, padding PaddingLengthGenerator) *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,
|
2018-07-07 09:42:24 -04:00
|
|
|
padding: padding,
|
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()
|
2018-07-30 14:08:50 -04:00
|
|
|
var paddingSize int
|
2018-07-07 09:42:24 -04:00
|
|
|
if w.padding != nil {
|
|
|
|
paddingSize = int(w.padding.NextPaddingLen())
|
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
|
2018-09-01 15:19:49 -04:00
|
|
|
totalSize := encryptedSize + paddingSize
|
|
|
|
if totalSize > buf.Size {
|
|
|
|
return nil, newError("size too large: ", totalSize)
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
eb := buf.New()
|
|
|
|
common.Must(eb.Reset(func(bb []byte) (int, error) {
|
2018-07-07 09:42:24 -04:00
|
|
|
w.sizeParser.Encode(uint16(encryptedSize+paddingSize), 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
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
if paddingSize > 0 {
|
2018-07-30 14:08:50 -04:00
|
|
|
common.Must(eb.AppendSupplier(buf.ReadFullFrom(rand.Reader, int32(paddingSize))))
|
2018-07-07 09:42:24 -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-07-08 15:46:13 -04:00
|
|
|
var maxPadding int32
|
|
|
|
if w.padding != nil {
|
|
|
|
maxPadding = int32(w.padding.MaxPaddingLen())
|
|
|
|
}
|
|
|
|
|
|
|
|
payloadSize := buf.Size - int32(w.auth.Overhead()) - w.sizeParser.SizeBytes() - maxPadding
|
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-09-01 15:19:49 -04:00
|
|
|
for _, b := range mb {
|
2018-08-29 10:37:12 -04:00
|
|
|
if b.IsEmpty() {
|
2018-04-01 16:41:38 -04:00
|
|
|
continue
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
2018-09-01 15:19:49 -04:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
eb, err := w.seal(b)
|
|
|
|
if err != nil {
|
2018-09-01 15:19:49 -04:00
|
|
|
continue
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
2018-09-01 15:19:49 -04:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
mb2Write.Append(eb)
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
|
|
|
|
2018-09-01 16:02:45 -04:00
|
|
|
if mb2Write.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
return w.writer.WriteMultiBuffer(mb2Write)
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
|
|
|
|
2018-07-31 06:37:59 -04:00
|
|
|
// WriteMultiBuffer implements buf.Writer.
|
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()
|
|
|
|
|
2018-09-01 15:19:49 -04:00
|
|
|
eb, err := w.seal(b)
|
|
|
|
common.Must(err)
|
2018-04-09 10:48:25 -04:00
|
|
|
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)
|
|
|
|
}
|