1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/crypto/auth.go

285 lines
6.0 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
}
2017-11-25 18:51:54 -05:00
type IncreasingAEADNonceGenerator struct {
nonce []byte
}
func NewIncreasingAEADNonceGenerator() *IncreasingAEADNonceGenerator {
return &IncreasingAEADNonceGenerator{
nonce: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
}
}
func (g *IncreasingAEADNonceGenerator) Next() []byte {
for i := range g.nonce {
g.nonce[i]++
if g.nonce[i] != 0 {
break
}
}
return g.nonce
}
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) {
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
}
2017-11-26 10:55:35 -05:00
var additionalData []byte
if v.AdditionalDataGenerator != nil {
additionalData = v.AdditionalDataGenerator.Next()
}
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) {
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
}
2017-11-26 10:55:35 -05:00
var additionalData []byte
if v.AdditionalDataGenerator != nil {
additionalData = v.AdditionalDataGenerator.Next()
}
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())
_, err := io.ReadFull(r.reader, sizeBytes)
2016-12-06 18:31:15 -05:00
if 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) {
if soft && r.reader.BufferedBytes() < 2 {
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-03-12 17:10:13 -04:00
b := buf.NewSize(uint32(size))
2018-04-01 17:31:53 -04:00
if err := b.Reset(buf.ReadFullFrom(r.reader, int(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-01 17:31:53 -04:00
rb, err := r.auth.Open(b.BytesTo(0), b.BytesTo(int(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
}
2017-12-03 07:23:24 -05:00
b.Slice(0, 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) {
encryptedSize := b.Len() + w.auth.Overhead()
eb := buf.New()
common.Must(eb.Reset(func(bb []byte) (int, error) {
w.sizeParser.Encode(uint16(encryptedSize), bb[:0])
return w.sizeParser.SizeBytes(), nil
}))
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()
2017-11-21 16:03:43 -05:00
payloadSize := buf.Size - w.auth.Overhead() - w.sizeParser.SizeBytes()
2017-11-09 16:33:15 -05:00
mb2Write := buf.NewMultiBufferCap(len(mb) + 10)
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-01 16:41:38 -04:00
if mb.IsEmpty() {
b := buf.New()
defer b.Release()
eb, _ := w.seal(b)
return w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(eb))
}
2018-03-28 16:24:01 -04:00
mb2Write := buf.NewMultiBufferCap(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 {
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)
}