1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/crypto/auth.go

272 lines
5.7 KiB
Go
Raw Normal View History

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
)
2016-12-07 07:00:32 -05:00
type BytesGenerator interface {
Next() []byte
}
type NoOpBytesGenerator struct {
buffer [1]byte
}
func (v NoOpBytesGenerator) Next() []byte {
return v.buffer[:0]
}
type StaticBytesGenerator struct {
Content []byte
}
func (v StaticBytesGenerator) Next() []byte {
return v.Content
}
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) {
iv := v.NonceGenerator.Next()
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
}
additionalData := v.AdditionalDataGenerator.Next()
return v.AEAD.Open(dst, iv, cipherText, additionalData)
}
func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) {
iv := v.NonceGenerator.Next()
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
}
additionalData := v.AdditionalDataGenerator.Next()
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
buffer *buf.Buffer
reader io.Reader
sizeParser ChunkSizeDecoder
size int
transferType protocol.TransferType
2016-12-06 18:31:15 -05:00
}
2017-01-27 07:42:31 -05:00
const (
readerBufferSize = 32 * 1024
)
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,
buffer: buf.NewLocal(readerBufferSize),
reader: reader,
sizeParser: sizeParser,
size: -1,
transferType: transferType,
2016-12-06 18:31:15 -05:00
}
}
2017-04-27 07:31:09 -04:00
func (r *AuthenticationReader) readSize() error {
if r.size >= 0 {
return nil
}
sizeBytes := r.sizeParser.SizeBytes()
if r.buffer.Len() < sizeBytes {
r.buffer.Reset(buf.ReadFrom(r.buffer))
delta := sizeBytes - r.buffer.Len()
if err := r.buffer.AppendSupplier(buf.ReadAtLeastFrom(r.reader, delta)); err != nil {
return err
}
2016-12-07 11:32:40 -05:00
}
2017-04-27 07:31:09 -04:00
size, err := r.sizeParser.Decode(r.buffer.BytesTo(sizeBytes))
2016-12-06 18:31:15 -05:00
if err != nil {
return err
}
2017-04-27 07:31:09 -04:00
r.size = int(size)
r.buffer.SliceFrom(sizeBytes)
return nil
}
func (r *AuthenticationReader) readChunk(waitForData bool) ([]byte, error) {
if err := r.readSize(); err != nil {
return nil, err
}
if r.size > readerBufferSize-r.sizeParser.SizeBytes() {
return nil, newError("size too large ", r.size).AtWarning()
2017-02-06 07:31:36 -05:00
}
2017-04-27 07:31:09 -04:00
if r.size == r.auth.Overhead() {
return nil, io.EOF
2016-12-07 09:00:04 -05:00
}
2016-12-07 07:00:32 -05:00
2017-04-27 07:31:09 -04:00
if r.buffer.Len() < r.size {
if !waitForData {
return nil, io.ErrNoProgress
}
2017-05-23 18:54:30 -04:00
if r.buffer.IsEmpty() {
r.buffer.Clear()
} else {
common.Must(r.buffer.Reset(buf.ReadFrom(r.buffer)))
}
2017-04-27 07:31:09 -04:00
delta := r.size - r.buffer.Len()
if err := r.buffer.AppendSupplier(buf.ReadAtLeastFrom(r.reader, delta)); err != nil {
return nil, err
}
2016-12-07 09:00:04 -05:00
}
2016-12-07 07:00:32 -05:00
2017-04-27 07:31:09 -04:00
b, err := r.auth.Open(r.buffer.BytesTo(0), r.buffer.BytesTo(r.size))
2016-12-07 09:00:04 -05:00
if err != nil {
2017-04-27 07:31:09 -04:00
return nil, err
2016-12-06 18:31:15 -05:00
}
2017-04-27 07:31:09 -04:00
r.buffer.SliceFrom(r.size)
r.size = -1
return b, nil
2016-12-06 18:31:15 -05:00
}
2017-04-23 07:30:08 -04:00
func (r *AuthenticationReader) Read() (buf.MultiBuffer, error) {
2017-04-27 07:31:09 -04:00
b, err := r.readChunk(true)
if err != nil {
return nil, err
2017-04-15 16:22:29 -04:00
}
mb := buf.NewMultiBuffer()
2017-05-02 16:23:07 -04:00
if r.transferType == protocol.TransferTypeStream {
2017-05-01 18:28:16 -04:00
mb.Write(b)
} else {
var bb *buf.Buffer
if len(b) < buf.Size {
bb = buf.New()
} else {
bb = buf.NewLocal(len(b))
2017-04-27 07:31:09 -04:00
}
2017-05-01 18:28:16 -04:00
bb.Append(b)
mb.Append(bb)
2017-04-27 07:31:09 -04:00
}
for r.buffer.Len() >= r.sizeParser.SizeBytes() {
b, err := r.readChunk(false)
if err != nil {
break
}
2017-05-02 16:23:07 -04:00
if r.transferType == protocol.TransferTypeStream {
2017-05-01 18:28:16 -04:00
mb.Write(b)
} else {
var bb *buf.Buffer
if len(b) < buf.Size {
bb = buf.New()
} else {
bb = buf.NewLocal(len(b))
}
bb.Append(b)
mb.Append(bb)
}
2017-04-15 16:22:29 -04:00
}
2017-04-27 07:31:09 -04:00
2017-04-15 16:22:29 -04:00
return mb, nil
}
2016-12-06 18:31:15 -05:00
type AuthenticationWriter struct {
2017-05-02 16:23:07 -04:00
auth Authenticator
2017-05-23 18:54:30 -04:00
buffer []byte
2017-05-02 16:23:07 -04:00
payload []byte
2017-05-23 18:54:30 -04:00
writer *buf.BufferedWriter
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 {
2017-05-23 18:54:30 -04:00
const payloadSize = 1024
2016-12-06 18:31:15 -05:00
return &AuthenticationWriter{
2017-05-02 16:23:07 -04:00
auth: auth,
2017-05-23 18:54:30 -04:00
buffer: make([]byte, payloadSize+sizeParser.SizeBytes()+auth.Overhead()),
payload: make([]byte, payloadSize),
writer: buf.NewBufferedWriterSize(writer, readerBufferSize),
2017-05-02 16:23:07 -04:00
sizeParser: sizeParser,
transferType: transferType,
2016-12-06 18:31:15 -05:00
}
}
2017-05-23 18:54:30 -04:00
func (w *AuthenticationWriter) append(b []byte) error {
2017-04-27 07:31:09 -04:00
encryptedSize := len(b) + w.auth.Overhead()
2017-05-23 18:54:30 -04:00
buffer := w.sizeParser.Encode(uint16(encryptedSize), w.buffer[:0])
2017-04-27 07:31:09 -04:00
2017-05-23 18:54:30 -04:00
buffer, err := w.auth.Seal(buffer, b)
if err != nil {
return err
}
2017-04-27 07:31:09 -04:00
2017-05-23 18:54:30 -04:00
if _, err := w.writer.Write(buffer); err != nil {
return err
}
2016-12-07 16:52:56 -05:00
2017-05-23 18:54:30 -04:00
return 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()
2017-04-15 15:07:23 -04:00
for {
2017-04-27 07:31:09 -04:00
n, _ := mb.Read(w.payload)
2017-05-23 18:54:30 -04:00
if err := w.append(w.payload[:n]); err != nil {
return err
2017-04-15 15:07:23 -04:00
}
if mb.IsEmpty() {
break
}
}
2017-04-27 07:31:09 -04:00
2017-05-23 18:54:30 -04:00
return w.writer.Flush()
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()
for {
b := mb.SplitFirst()
if b == nil {
b = buf.New()
}
2017-05-23 18:54:30 -04:00
if err := w.append(b.Bytes()); err != nil {
b.Release()
return err
2017-05-01 18:28:16 -04:00
}
b.Release()
if mb.IsEmpty() {
break
}
}
2017-05-23 18:54:30 -04:00
return w.writer.Flush()
2017-05-01 18:28:16 -04:00
}
func (w *AuthenticationWriter) Write(mb buf.MultiBuffer) error {
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)
}