-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessage.go
439 lines (373 loc) · 11.3 KB
/
message.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package secureio
import (
"crypto/aes"
"encoding/binary"
"fmt"
"io"
"sync"
"sync/atomic"
"github.com/xaionaro-go/slice"
"github.com/xaionaro-go/unsafetools"
"golang.org/x/crypto/poly1305"
"lukechampine.com/blake3"
xerrors "github.com/xaionaro-go/errors"
)
const (
maxPossiblePacketSize = 1<<16 - 256
)
const (
ivSize = 24 // XChaCha20
)
var (
payloadSizeLimit uint32
payloadLossySizeLimit uint32
)
func init() {
SetPayloadSizeLimit(uint32(maxPossiblePacketSize - messagesContainerHeadersSize - messageHeadersSize))
SetPayloadLossySizeLimit(uint32(1500 - 48 - messagesContainerHeadersSize - messageHeadersSize)) // 48 == 40 + 8: IPv6 headers + UDP headers
}
// SetPayloadSizeLimit sets the default PayloadSizeLimit is the backend
// is not considered lossy (see IsLossyWriter).
func SetPayloadSizeLimit(newSize uint32) {
newSize &= ^(uint32(aes.BlockSize) - 1)
atomic.StoreUint32(&payloadSizeLimit, newSize)
}
// SetPayloadLossySizeLimit sets the default PayloadSizeLimit is the backend
// is considered lossy (see IsLossyWriter).
func SetPayloadLossySizeLimit(newSize uint32) {
newSize &= ^(uint32(aes.BlockSize) - 1)
atomic.StoreUint32(&payloadLossySizeLimit, newSize)
}
var (
poly1305KeyXORer = []byte("github.com/xaionaro-go/secureio github.com/xaionaro-go/secureio github.com/xaionaro-go/secureio github.com/xaionaro-go/secureio")
)
// MessageType is the identifier of the type of the message.
// It is used to determine how to interpret the message (which Handler to use).
type MessageType uint32
const (
messageTypeUndefined = MessageType(0)
messageTypeKeyExchange = MessageType(^uint32(0) - iota)
messageTypeNegotiation
// MessageTypeReadWrite is the default MessageType for
// the in-band data. It used by default for (*Session).Read and
// (*Session).Write.
MessageTypeReadWrite
messageTypeReserved0
messageTypeReserved1
messageTypeReserved2
messageTypeReserved3
messageTypeReserved4
messageTypeReserved5
messageTypeReserved7
messageTypeReserved8
messageTypeReserved9
messageTypeReserved10
messageTypeReserved11
messageTypeReserved12
messageTypeReserved13
messageTypeReserved14
messageTypeReservedAbove
)
func (t MessageType) isInternal() bool {
switch t {
case messageTypeKeyExchange, messageTypeNegotiation:
return true
}
return false
}
// MessageTypeChannel returns MessageType for multiplexer.
// See (*Session).SetHandlerFuncs.
func MessageTypeChannel(channelID uint32) MessageType {
if channelID+1 >= uint32(messageTypeReservedAbove) {
panic("channelID is too high")
}
return MessageType(channelID) + 1
}
func (t MessageType) String() string {
switch {
case t == messageTypeUndefined:
return `undefined`
case t == messageTypeKeyExchange:
return `key_exchange`
case t == MessageTypeReadWrite:
return fmt.Sprintf(`read_write`)
case t >= MessageTypeChannel(0) && t < messageTypeReservedAbove:
return fmt.Sprintf(`channel%d`, uint8(t-MessageTypeReadWrite))
}
return `unknown` // should not happen
}
type messageLength uint32
type messageHeadersData struct {
Type MessageType
Length messageLength
messageFlags
}
type messageHeaders struct {
messageHeadersData
pool *messageHeadersPool
isBusy bool
}
type messageFlags uint8
const (
messageFlagsIsConfidential = messageFlags(1 << iota)
messageFlagsIsFragmented
)
func (flags messageFlags) IsConfidential() bool {
return flags&messageFlagsIsConfidential != 0
}
func (flags *messageFlags) SetIsConfidential(newValue bool) {
if newValue {
*flags |= messageFlagsIsConfidential
} else {
*flags &= ^messageFlagsIsConfidential
}
}
func (flags messageFlags) IsFragmented() bool {
return flags&messageFlagsIsFragmented != 0
}
func (flags *messageFlags) SetIsFragmented(newValue bool) {
if newValue {
*flags |= messageFlagsIsFragmented
} else {
*flags &= ^messageFlagsIsFragmented
}
}
type packetID [8]byte
func (id *packetID) Value() uint64 {
return binaryOrderType.Uint64(id[:])
}
func (id *packetID) String() string {
if id == nil {
return ""
}
return fmt.Sprint(id.Value())
}
func (id *packetID) SetNextPacketID(sess *Session) {
binaryOrderType.PutUint64((*id)[:], sess.getNextPacketID())
}
func (id *packetID) Read(b []byte) (int, error) {
if len(b) < len(*id) {
return 0, newErrTooShort(uint(len(*id)), uint(len(b)))
}
copy((*id)[:], b)
return len(*id), nil
}
func (id *packetID) Write(b []byte) (int, error) {
if len(b) < len(*id) {
return 0, newErrTooShort(uint(len(*id)), uint(len(b)))
}
copy(b, (*id)[:])
return len(*id), nil
}
type messagesContainerHeadersData struct {
PacketID packetID // Should be the first. It is used to decrypt other values!
ContainerHeadersChecksum [poly1305.TagSize]byte
MessagesChecksum [poly1305.TagSize]byte
Length messageLength
}
type messagesContainerHeaders struct {
messagesContainerHeadersData
pool *messagesContainerHeadersPool
isBusy bool
}
var (
messageHeadersSize = uint(binary.Size(messageHeadersData{}))
messagesContainerHeadersSize = uint(binary.Size(messagesContainerHeadersData{}))
)
func (hdr *messageHeadersData) Reset() {
hdr.Type = messageTypeUndefined
hdr.Length = 0
hdr.messageFlags = 0
}
type messageHeadersPool struct {
storage sync.Pool
}
func newMessageHeadersPool() *messageHeadersPool {
pool := &messageHeadersPool{}
pool.storage.New = func() interface{} {
msg := &messageHeaders{
pool: pool,
}
return msg
}
return pool
}
func (hdr *messageHeadersData) Set(msgType MessageType, payload []byte) {
hdr.Type = msgType
hdr.Length = messageLength(len(payload))
}
func (pool *messageHeadersPool) AcquireMessageHeaders() *messageHeaders {
hdr := pool.storage.Get().(*messageHeaders)
if hdr.isBusy {
panic(`should not happened`)
}
hdr.isBusy = true
return hdr
}
func (pool *messageHeadersPool) Put(hdr *messageHeaders) {
if hdr == nil || !hdr.isBusy {
panic(`should not happened`)
}
hdr.Reset()
hdr.isBusy = false
pool.storage.Put(hdr)
}
func (hdr *messageHeaders) Release() {
hdr.pool.Put(hdr)
}
func (hdr *messageHeadersData) Read(b []byte) (int, error) {
if uint(len(b)) < messageHeadersSize {
return 0, newErrTooShort(messageHeadersSize, uint(len(b)))
}
hdr.Type = MessageType(binaryOrderType.Uint32(b[0:]))
b = b[4:]
hdr.Length = messageLength(binaryOrderType.Uint32(b[0:]))
b = b[4:]
hdr.messageFlags = messageFlags(b[0])
b = b[1:]
return int(messageHeadersSize), nil
}
func (hdr *messageHeadersData) Write(b []byte) (int, error) {
if uint(len(b)) < messageHeadersSize {
return 0, newErrTooShort(messageHeadersSize, uint(len(b)))
}
binaryOrderType.PutUint32(b[0:], uint32(hdr.Type))
b = b[4:]
binaryOrderType.PutUint32(b[0:], uint32(hdr.Length))
b = b[4:]
b[0] = uint8(hdr.messageFlags)
b = b[1:]
return int(messageHeadersSize), nil
}
func (containerHdr *messagesContainerHeadersData) SetNextPacketID(sess *Session) {
containerHdr.PacketID.SetNextPacketID(sess)
}
func (containerHdr *messagesContainerHeadersData) calculatePoly1305Key(cipherKey []byte) (result [32]byte) {
copy(result[:len(containerHdr.PacketID)], containerHdr.PacketID[:])
copy(result[len(containerHdr.PacketID):], cipherKey)
for idx := range result {
result[idx] ^= poly1305KeyXORer[idx]
}
result = blake3.Sum256(result[:])
return result
}
func (containerHdr *messagesContainerHeadersData) CalculateHeadersChecksumTo(cipherKey []byte, dst *[poly1305.TagSize]byte) {
key := containerHdr.calculatePoly1305Key(cipherKey)
poly1305.Sum(
dst,
unsafetools.BytesOf(containerHdr)[len(containerHdr.PacketID)+poly1305.TagSize*2:],
&key,
)
}
func (containerHdr *messagesContainerHeadersData) CalculateMessagesChecksumTo(cipherKey []byte, dst *[poly1305.TagSize]byte, messagesBytes []byte) {
key := containerHdr.calculatePoly1305Key(cipherKey)
poly1305.Sum(
dst,
messagesBytes,
&key,
)
}
func (containerHdr *messagesContainerHeadersData) Read(b []byte) (int, error) {
if uint(len(b)) < messagesContainerHeadersSize {
return 0, newErrTooShort(messagesContainerHeadersSize, uint(len(b)))
}
ivN, err := containerHdr.PacketID.Read(b)
if err != nil {
return 0, wrapError(err)
}
n, err := containerHdr.ReadAfterIV(b[len(containerHdr.PacketID):])
if err != nil {
return 0, err
}
return n + ivN, nil
}
func (containerHdr *messagesContainerHeadersData) ReadAfterIV(b []byte) (int, error) {
copy(containerHdr.ContainerHeadersChecksum[:], b[:poly1305.TagSize])
b = b[poly1305.TagSize:]
copy(containerHdr.MessagesChecksum[:], b[:poly1305.TagSize])
b = b[poly1305.TagSize:]
containerHdr.Length = messageLength(binaryOrderType.Uint32(b))
b = b[4:]
return int(messagesContainerHeadersSize) - len(containerHdr.PacketID), nil
}
func (containerHdr *messagesContainerHeadersData) Write(b []byte) (int, error) {
if uint(len(b)) < messagesContainerHeadersSize {
return 0, newErrTooShort(messagesContainerHeadersSize, uint(len(b)))
}
_, err := containerHdr.PacketID.Write(b)
if err != nil {
return 0, wrapError(err)
}
b = b[len(containerHdr.PacketID):]
copy(b, containerHdr.ContainerHeadersChecksum[:])
b = b[poly1305.TagSize:]
copy(b, containerHdr.MessagesChecksum[:])
b = b[poly1305.TagSize:]
binaryOrderType.PutUint32(b, uint32(containerHdr.Length))
b = b[4:]
return int(messagesContainerHeadersSize), nil
}
var messagesContainerHeadersSizeBufPool = sync.Pool{New: func() interface{} {
return make([]byte, messagesContainerHeadersSize)
}}
func (containerHdr *messagesContainerHeadersData) WriteTo(w io.Writer) (int64, error) {
buf := messagesContainerHeadersSizeBufPool.Get().([]byte)
defer messagesContainerHeadersSizeBufPool.Put(buf)
n0, err := containerHdr.Write(buf)
if err != nil {
return int64(n0), xerrors.Errorf(
"unable to write the headers' data to a buffer: %w", err,
)
}
n1, err := w.Write(buf)
if err != nil {
return int64(n1), xerrors.Errorf(
"unable to write headers' data from a buffer to a writer: %w", err,
)
}
return int64(n0 + n1), nil
}
type messagesContainerHeadersPool struct {
storage sync.Pool
}
func newMessagesContainerHeadersPool() *messagesContainerHeadersPool {
pool := &messagesContainerHeadersPool{}
pool.storage.New = func() interface{} {
containerHdr := &messagesContainerHeaders{
pool: pool,
}
return containerHdr
}
return pool
}
func (pool *messagesContainerHeadersPool) AcquireMessagesContainerHeaders(sess *Session) *messagesContainerHeaders {
containerHdr := pool.storage.Get().(*messagesContainerHeaders)
if containerHdr.isBusy {
panic(`should not happened`)
}
containerHdr.isBusy = true
containerHdr.SetNextPacketID(sess)
return containerHdr
}
func (pool *messagesContainerHeadersPool) Put(containerHdr *messagesContainerHeaders) {
if containerHdr == nil || !containerHdr.isBusy {
panic(`should not happened`)
}
containerHdr.Reset()
containerHdr.isBusy = false
pool.storage.Put(containerHdr)
}
func (containerHdr *messagesContainerHeadersData) Set(cipherKey []byte, messagesBytes []byte) error {
containerHdr.Length = messageLength(len(messagesBytes))
containerHdr.CalculateHeadersChecksumTo(cipherKey, &containerHdr.ContainerHeadersChecksum)
containerHdr.CalculateMessagesChecksumTo(cipherKey, &containerHdr.MessagesChecksum, messagesBytes)
return nil
}
func (containerHdr *messagesContainerHeadersData) Reset() {
containerHdr.Length = 0
slice.SetZeros(containerHdr.ContainerHeadersChecksum[:])
slice.SetZeros(containerHdr.MessagesChecksum[:])
}
func (containerHdr *messagesContainerHeaders) Release() {
containerHdr.pool.Put(containerHdr)
}