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

241 lines
5.2 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
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,
2016-12-06 18:31:15 -05:00
}
}
2017-12-03 07:23:24 -05:00
func (r *AuthenticationReader) readSize() (int, error) {
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)
return int(size), err
2017-04-27 07:31:09 -04:00
}
2017-12-03 07:23:24 -05:00
func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
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
2017-12-03 07:23:24 -05:00
if size == r.auth.Overhead() {
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
2017-12-03 07:23:24 -05:00
var b *buf.Buffer
if size <= buf.Size {
b = buf.New()
} else {
b = buf.NewLocal(size)
2016-12-07 09:00:04 -05:00
}
2017-12-03 07:23:24 -05:00
if err := b.Reset(buf.ReadFullFrom(r.reader, size)); err != nil {
b.Release()
2017-04-27 07:31:09 -04:00
return nil, err
2016-12-06 18:31:15 -05:00
}
2017-12-03 07:23:24 -05: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
}
2017-12-03 07:23:24 -05:00
b.Slice(0, len(rb))
return buf.NewMultiBufferValue(b), 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()
2017-11-09 16:33:15 -05:00
mb2Write := buf.NewMultiBufferCap(len(mb) * 2)
2017-05-01 18:28:16 -04:00
for {
b := mb.SplitFirst()
if b == nil {
b = buf.New()
}
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
if mb.IsEmpty() {
break
}
}
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)
}