-
Notifications
You must be signed in to change notification settings - Fork 6
/
stream.go
210 lines (173 loc) · 5.29 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package stream
import (
"fmt"
"io"
"net"
"sync"
"time"
)
// EncryptedStream is an encrypted stream. Data are encrypted before writing to
// underlying stream, and are decrypted after reading from underlying stream.
type EncryptedStream struct {
config *Config
stream io.ReadWriter
encoder *Encoder
decoder *Decoder
lock sync.RWMutex
isClosed bool
readLock sync.Mutex
readLenBuffer []byte
readBuffer []byte
decryptBuffer []byte
decryptBufStart int
decryptBufEnd int
writeLock sync.Mutex
writeLenBuffer []byte
encryptBuffer []byte
}
// NewEncryptedStream creates an EncryptedStream with a given ReadWriter and
// config.
func NewEncryptedStream(stream io.ReadWriter, config *Config) (*EncryptedStream, error) {
config, err := MergeConfig(DefaultConfig(), config)
if err != nil {
return nil, err
}
err = config.Verify()
if err != nil {
return nil, err
}
encoder, err := NewEncoder(config.Cipher, config.Initiator, config.SequentialNonce)
if err != nil {
return nil, err
}
decoder, err := NewDecoder(config.Cipher, config.Initiator, config.SequentialNonce, config.DisableNonceVerification)
if err != nil {
return nil, err
}
es := &EncryptedStream{
config: config,
stream: stream,
encoder: encoder,
decoder: decoder,
readBuffer: make([]byte, config.MaxChunkSize+config.Cipher.MaxOverhead()+config.Cipher.NonceSize()),
encryptBuffer: make([]byte, config.MaxChunkSize+config.Cipher.MaxOverhead()+config.Cipher.NonceSize()),
decryptBuffer: make([]byte, config.MaxChunkSize),
readLenBuffer: make([]byte, 4),
writeLenBuffer: make([]byte, 4),
}
return es, nil
}
// IsClosed returns whether the EncryptedStream is closed.
func (es *EncryptedStream) IsClosed() bool {
es.lock.RLock()
defer es.lock.RUnlock()
return es.isClosed
}
// Read implements net.Conn and io.Reader
func (es *EncryptedStream) Read(b []byte) (int, error) {
if es.IsClosed() {
return 0, io.ErrClosedPipe
}
es.readLock.Lock()
defer es.readLock.Unlock()
if es.decryptBufStart >= es.decryptBufEnd {
n, err := readVarBytes(es.stream, es.readBuffer, es.readLenBuffer)
if err != nil {
return 0, err
}
if n > es.config.MaxChunkSize+es.config.Cipher.MaxOverhead()+es.config.Cipher.NonceSize() {
return 0, fmt.Errorf("received invalid encrypted data size %d", n)
}
es.decryptBuffer = es.decryptBuffer[:cap(es.decryptBuffer)]
es.decryptBuffer, err = es.decoder.Decode(es.decryptBuffer, es.readBuffer[:n])
if err != nil {
return 0, err
}
es.decryptBufStart = 0
es.decryptBufEnd = len(es.decryptBuffer)
}
n := copy(b, es.decryptBuffer[es.decryptBufStart:es.decryptBufEnd])
es.decryptBufStart += n
return n, nil
}
// Write implements net.Conn and io.Writer
func (es *EncryptedStream) Write(b []byte) (int, error) {
if es.IsClosed() {
return 0, io.ErrClosedPipe
}
es.writeLock.Lock()
defer es.writeLock.Unlock()
bytesWrite := 0
var err error
for bytesWrite < len(b) {
n := len(b) - bytesWrite
if n > es.config.MaxChunkSize {
n = es.config.MaxChunkSize
}
es.encryptBuffer = es.encryptBuffer[:cap(es.encryptBuffer)]
es.encryptBuffer, err = es.encoder.Encode(es.encryptBuffer, b[bytesWrite:bytesWrite+n])
if err != nil {
return bytesWrite, err
}
err = writeVarBytes(es.stream, es.encryptBuffer, es.writeLenBuffer)
if err != nil {
return bytesWrite, err
}
bytesWrite += n
}
return bytesWrite, nil
}
// Close implements net.Conn and io.Closer. Will call underlying stream's
// Close() method if it has one.
func (es *EncryptedStream) Close() error {
es.lock.Lock()
defer es.lock.Unlock()
if es.isClosed {
return nil
}
if stream, ok := es.stream.(io.Closer); ok {
return stream.Close()
}
es.isClosed = true
return nil
}
// LocalAddr implements net.Conn. Will call underlying stream's LocalAddr()
// method if it has one, otherwise will return nil.
func (es *EncryptedStream) LocalAddr() net.Addr {
if stream, ok := es.stream.(interface{ LocalAddr() net.Addr }); ok {
return stream.LocalAddr()
}
return nil
}
// RemoteAddr implements net.Conn. Will call underlying stream's RemoteAddr()
// method if it has one, otherwise will return nil.
func (es *EncryptedStream) RemoteAddr() net.Addr {
if stream, ok := es.stream.(interface{ RemoteAddr() net.Addr }); ok {
return stream.RemoteAddr()
}
return nil
}
// SetDeadline implements net.Conn. Will call underlying stream's SetDeadline()
// method if it has one, otherwise will return nil.
func (es *EncryptedStream) SetDeadline(t time.Time) error {
if stream, ok := es.stream.(interface{ SetDeadline(t time.Time) error }); ok {
return stream.SetDeadline(t)
}
return nil
}
// SetReadDeadline implements net.Conn. Will call underlying stream's
// SetReadDeadline() method if it has one, otherwise will return nil.
func (es *EncryptedStream) SetReadDeadline(t time.Time) error {
if stream, ok := es.stream.(interface{ SetReadDeadline(t time.Time) error }); ok {
return stream.SetReadDeadline(t)
}
return nil
}
// SetWriteDeadline implements net.Conn. Will call underlying stream's
// SetWriteDeadline() method if it has one, otherwise will return nil.
func (es *EncryptedStream) SetWriteDeadline(t time.Time) error {
if stream, ok := es.stream.(interface{ SetWriteDeadline(t time.Time) error }); ok {
return stream.SetWriteDeadline(t)
}
return nil
}