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

345 lines
7.6 KiB
Go
Raw Normal View History

2016-12-06 18:31:15 -05:00
package crypto
import (
"crypto/cipher"
"io"
"math/rand"
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
sizeBytes []byte
2017-05-02 16:23:07 -04:00
transferType protocol.TransferType
2018-07-07 09:42:24 -04:00
padding PaddingLengthGenerator
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 {
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,
sizeBytes: make([]byte, sizeParser.SizeBytes()),
2016-12-06 18:31:15 -05: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
}
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
}
if _, err := io.ReadFull(r.reader, r.sizeBytes); err != nil {
2018-07-07 09:42:24 -04:00
return 0, 0, err
}
var padding uint16
2018-07-07 09:42:24 -04:00
if r.padding != nil {
padding = r.padding.NextPaddingLen()
2016-12-06 18:31:15 -05:00
}
size, err := r.sizeParser.Decode(r.sizeBytes)
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()
2018-11-02 10:01:33 -04:00
if _, err := b.ReadFullFrom(r.reader, size); err != nil {
2018-08-16 06:05:33 -04:00
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
}
if r.done {
2018-08-16 06:05:33 -04:00
return io.EOF
}
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
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
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
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
}
2018-11-17 03:12:20 -05:00
*mb = append(*mb, b)
2018-08-16 06:05:33 -04:00
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-11-18 13:36:36 -05:00
*mb = buf.MergeBytes(*mb, rb)
2018-08-16 06:05:33 -04:00
return nil
2018-04-01 17:31:53 -04:00
}
func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
const readSize = 16
2018-11-16 05:29:16 -05:00
mb := make(buf.MultiBuffer, 0, readSize)
2018-08-16 06:05:33 -04:00
if err := r.readInternal(false, &mb); err != nil {
2018-11-17 16:45:07 -05:00
buf.ReleaseMulti(mb)
2018-08-16 06:05:33 -04:00
return nil, err
}
2018-04-01 17:31:53 -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 {
2018-11-17 16:45:07 -05:00
buf.ReleaseMulti(mb)
2018-04-01 17:31:53 -04:00
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 {
w := &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
}
if padding != nil {
w.padding = padding
}
return w
2016-12-06 18:31:15 -05:00
}
2018-11-15 15:14:53 -05:00
func (w *AuthenticationWriter) seal(b []byte) (*buf.Buffer, error) {
encryptedSize := int32(len(b) + w.auth.Overhead())
2018-11-02 16:34:04 -04:00
var paddingSize int32
2018-07-07 09:42:24 -04:00
if w.padding != nil {
2018-11-02 16:34:04 -04:00
paddingSize = int32(w.padding.NextPaddingLen())
2018-07-07 09:42:24 -04:00
}
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()
2018-11-02 16:34:04 -04:00
w.sizeParser.Encode(uint16(encryptedSize+paddingSize), eb.Extend(w.sizeParser.SizeBytes()))
2018-11-15 15:14:53 -05:00
if _, err := w.auth.Seal(eb.Extend(encryptedSize)[:0], b); err != nil {
2017-11-09 16:33:15 -05:00
eb.Release()
return nil, err
2017-05-23 18:54:30 -04:00
}
2018-07-07 09:42:24 -04:00
if paddingSize > 0 {
// With size of the chunk and padding length encrypted, the content of padding doesn't matter much.
2018-11-11 10:34:58 -05:00
paddingBytes := eb.Extend(paddingSize)
common.Must2(rand.Read(paddingBytes))
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 {
2018-11-17 16:45:07 -05:00
defer buf.ReleaseMulti(mb)
2017-04-15 16:29:38 -04:00
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-11-16 05:29:16 -05:00
mb2Write := make(buf.MultiBuffer, 0, len(mb)+10)
2017-11-09 16:33:15 -05:00
2018-11-18 13:36:36 -05:00
temp := buf.New()
defer temp.Release()
rawBytes := temp.Extend(payloadSize)
2017-04-15 15:07:23 -04:00
for {
nb, nBytes := buf.SplitBytes(mb, rawBytes)
2018-11-18 13:36:36 -05:00
mb = nb
eb, err := w.seal(rawBytes[:nBytes])
2017-11-09 16:33:15 -05:00
if err != nil {
2018-11-17 16:45:07 -05:00
buf.ReleaseMulti(mb2Write)
2017-05-23 18:54:30 -04:00
return err
2017-04-15 15:07:23 -04:00
}
2018-11-17 03:12:20 -05:00
mb2Write = append(mb2Write, 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 {
2018-11-17 16:45:07 -05:00
defer buf.ReleaseMulti(mb)
2017-05-01 18:28:16 -04:00
2018-11-16 05:29:16 -05:00
mb2Write := make(buf.MultiBuffer, 0, 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
2018-11-15 15:14:53 -05:00
eb, err := w.seal(b.Bytes())
2017-11-09 16:33:15 -05:00
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
2018-11-17 03:12:20 -05:00
mb2Write = append(mb2Write, 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
}
// 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() {
2018-11-15 15:14:53 -05:00
eb, err := w.seal([]byte{})
2018-09-01 15:19:49 -04:00
common.Must(err)
2018-11-16 05:08:12 -05:00
return w.writer.WriteMultiBuffer(buf.MultiBuffer{eb})
2018-04-09 10:48:25 -04:00
}
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)
}