1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00
v2fly/common/crypto/auth.go

196 lines
4.0 KiB
Go
Raw Normal View History

2016-12-06 23:31:15 +00:00
package crypto
import (
"crypto/cipher"
"errors"
"io"
2016-12-07 12:00:32 +00:00
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-12-06 23:31:15 +00:00
"v2ray.com/core/common/serial"
)
var (
ErrAuthenticationFailed = errors.New("Authentication failed.")
2016-12-07 12:00:32 +00:00
errInsufficientBuffer = errors.New("Insufficient buffer.")
errInvalidNonce = errors.New("Invalid nonce.")
2016-12-06 23:31:15 +00:00
)
2016-12-07 12:00:32 +00: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() {
return nil, errInvalidNonce
}
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() {
return nil, errInvalidNonce
}
additionalData := v.AdditionalDataGenerator.Next()
return v.AEAD.Seal(dst, iv, plainText, additionalData), nil
}
2016-12-06 23:31:15 +00:00
type AuthenticationReader struct {
2016-12-07 12:00:32 +00:00
auth Authenticator
2016-12-09 10:35:27 +00:00
buffer *buf.Buffer
2016-12-07 12:00:32 +00:00
reader io.Reader
2016-12-06 23:31:15 +00:00
2016-12-07 12:00:32 +00:00
chunk []byte
aggressive bool
2016-12-06 23:31:15 +00:00
}
2016-12-07 12:00:32 +00:00
func NewAuthenticationReader(auth Authenticator, reader io.Reader, aggressive bool) *AuthenticationReader {
2016-12-06 23:31:15 +00:00
return &AuthenticationReader{
2016-12-07 12:00:32 +00:00
auth: auth,
2016-12-09 11:08:25 +00:00
buffer: buf.NewLocal(32 * 1024),
2016-12-07 12:00:32 +00:00
reader: reader,
aggressive: aggressive,
2016-12-06 23:31:15 +00:00
}
}
func (v *AuthenticationReader) NextChunk() error {
if v.buffer.Len() < 2 {
return errInsufficientBuffer
}
2016-12-06 23:31:15 +00:00
size := int(serial.BytesToUint16(v.buffer.BytesTo(2)))
if size > v.buffer.Len()-2 {
return errInsufficientBuffer
}
2016-12-07 12:00:32 +00:00
if size == v.auth.Overhead() {
return io.EOF
}
2016-12-07 16:32:40 +00:00
if size < v.auth.Overhead() {
return errors.New("AuthenticationReader: invalid packet size.")
}
2016-12-06 23:31:15 +00:00
cipherChunk := v.buffer.BytesRange(2, size+2)
2016-12-07 21:52:56 +00:00
plainChunk, err := v.auth.Open(cipherChunk[:0], cipherChunk)
2016-12-06 23:31:15 +00:00
if err != nil {
return err
}
v.chunk = plainChunk
2016-12-06 23:52:36 +00:00
v.buffer.SliceFrom(size + 2)
2016-12-06 23:31:15 +00:00
return nil
}
func (v *AuthenticationReader) CopyChunk(b []byte) int {
2016-12-07 12:00:32 +00:00
if len(v.chunk) == 0 {
return 0
}
2016-12-06 23:31:15 +00:00
nBytes := copy(b, v.chunk)
if nBytes == len(v.chunk) {
v.chunk = nil
} else {
v.chunk = v.chunk[nBytes:]
}
return nBytes
}
2016-12-07 14:00:04 +00:00
func (v *AuthenticationReader) EnsureChunk() error {
2016-12-06 23:31:15 +00:00
for {
2016-12-07 12:00:32 +00:00
err := v.NextChunk()
2016-12-07 14:00:04 +00:00
if err == nil {
return nil
}
2016-12-07 12:00:32 +00:00
if err == errInsufficientBuffer {
2016-12-07 21:52:56 +00:00
if v.buffer.IsEmpty() {
v.buffer.Clear()
} else {
2016-12-07 14:00:04 +00:00
leftover := v.buffer.Bytes()
2016-12-09 11:08:25 +00:00
v.buffer.Reset(func(b []byte) (int, error) {
return copy(b, leftover), nil
2016-12-07 14:00:04 +00:00
})
2016-12-07 12:00:32 +00:00
}
2016-12-17 06:40:28 +00:00
err = v.buffer.AppendSupplier(buf.ReadFrom(v.reader))
if err == nil {
2016-12-12 16:42:03 +00:00
continue
}
2016-12-06 23:31:15 +00:00
}
2016-12-07 14:00:04 +00:00
return err
}
}
2016-12-07 12:00:32 +00:00
2016-12-07 14:00:04 +00:00
func (v *AuthenticationReader) Read(b []byte) (int, error) {
if len(v.chunk) > 0 {
2016-12-07 12:00:32 +00:00
nBytes := v.CopyChunk(b)
2016-12-07 14:00:04 +00:00
return nBytes, nil
}
2016-12-07 12:00:32 +00:00
2016-12-07 14:00:04 +00:00
err := v.EnsureChunk()
if err != nil {
return 0, err
2016-12-06 23:31:15 +00:00
}
2016-12-07 14:00:04 +00:00
2016-12-12 20:44:16 +00:00
totalBytes := v.CopyChunk(b)
2016-12-13 07:30:24 +00:00
for v.aggressive && totalBytes < len(b) {
2016-12-12 20:44:16 +00:00
if err := v.NextChunk(); err != nil {
break
}
totalBytes += v.CopyChunk(b[totalBytes:])
}
return totalBytes, nil
2016-12-06 23:31:15 +00:00
}
type AuthenticationWriter struct {
2016-12-21 14:37:16 +00:00
auth Authenticator
buffer []byte
writer io.Writer
2016-12-06 23:31:15 +00:00
}
2016-12-07 12:00:32 +00:00
func NewAuthenticationWriter(auth Authenticator, writer io.Writer) *AuthenticationWriter {
2016-12-06 23:31:15 +00:00
return &AuthenticationWriter{
2016-12-07 12:00:32 +00:00
auth: auth,
buffer: make([]byte, 32*1024),
writer: writer,
2016-12-06 23:31:15 +00:00
}
}
func (v *AuthenticationWriter) Write(b []byte) (int, error) {
2016-12-07 21:52:56 +00:00
cipherChunk, err := v.auth.Seal(v.buffer[2:2], b)
2016-12-07 12:00:32 +00:00
if err != nil {
return 0, err
}
2016-12-07 21:52:56 +00:00
2016-12-07 16:32:40 +00:00
serial.Uint16ToBytes(uint16(len(cipherChunk)), v.buffer[:0])
2016-12-07 12:00:32 +00:00
_, err = v.writer.Write(v.buffer[:2+len(cipherChunk)])
2016-12-06 23:31:15 +00:00
return len(b), err
}