-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
493 lines (408 loc) · 11.1 KB
/
net.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/asn1"
"encoding/binary"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"math"
"net"
"sync"
"sync/atomic"
"time"
)
var (
baseTLSConfig = &tls.Config{
SessionTicketsDisabled: true,
CurvePreferences: []tls.CurveID{tls.X25519, tls.CurveP256},
Renegotiation: tls.RenegotiateNever,
MinVersion: tls.VersionTLS13,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
},
}
shouldHaveTopic = map[MsgType]bool{
MsgTypeMPC: true,
MsgTypeDiscovery: true,
}
)
type MsgType uint8
const (
MsgTypeNone MsgType = iota
MsgTypeDiscovery
MsgTypeMPC
maxBuffLen = 1024 * 1024 * 20 // 20MB
)
type Logger interface {
DebugEnabled() bool
Debugf(format string, a ...interface{})
Warnf(format string, a ...interface{})
}
type PartyConnectionConfig struct {
AuthFunc func(tlsContext []byte) Handshake
Domain string
Id int
Endpoint string
TlsCAs *x509.CertPool
}
type errReporter func(string, ...interface{})
type outChan chan *outMsg
func (o outChan) enqueue(msg *outMsg, onFailure func(), timeout time.Duration) {
t1 := time.NewTimer(timeout)
defer t1.Stop()
select {
case o <- msg:
case <-t1.C:
onFailure()
}
}
type remoteParty struct {
onStart sync.Once
authenticate func(tlsTopic []byte) Handshake
// config
domain string
id int
reportErr errReporter
endpoint string
// state
tlsConf *tls.Config
conn *tls.Conn
msgs outChan
}
type outMsg struct {
msgType MsgType
data []byte
topic []byte
}
type InMsg struct {
Domain string
From uint16
Data []byte
Topic []byte
Type uint8
}
type Out struct {
Broadcast bool
Data []byte
Topic string
}
type Party struct {
InMessages <-chan InMsg
SendMessage SendMessage
Address string
Logger Logger
Identity []byte
}
type SendMessage func(msgType uint8, topic []byte, msg []byte, to ...uint16)
type SocketRemoteParties map[int]*remoteParty
func (parties SocketRemoteParties) Clone() SocketRemoteParties {
res := make(SocketRemoteParties)
for k, v := range parties {
res[k] = &remoteParty{
msgs: make(outChan, 10),
reportErr: v.reportErr,
endpoint: v.endpoint,
tlsConf: v.tlsConf,
id: v.id,
}
}
return res
}
func (parties SocketRemoteParties) Send(msgType uint8, topic []byte, msg []byte, to ...uint16) {
for _, dst := range to {
p, exists := parties[int(dst)]
if !exists {
panic(fmt.Sprintf("party %d doesn't exist", dst))
}
p.startOnce()
msgToSend := outMsg{
msgType: MsgType(msgType),
topic: topic,
data: msg,
}
onTimeout := func() {
p.reportErr(fmt.Sprintf("timeout sending to %d", dst))
panic("bla")
}
p.msgs.enqueue(&msgToSend, onTimeout, time.Second*10)
}
}
func NewSocketRemoteParty(config PartyConnectionConfig, l Logger) *remoteParty {
tlsConfig := baseTLSConfig.Clone()
tlsConfig.RootCAs = config.TlsCAs
p := &remoteParty{
authenticate: config.AuthFunc,
msgs: make(chan *outMsg, 1000),
reportErr: l.Warnf,
endpoint: config.Endpoint,
tlsConf: tlsConfig,
id: config.Id,
domain: config.Domain,
}
return p
}
func (rp *remoteParty) startOnce() {
rp.onStart.Do(func() {
go rp.sendMessages()
})
}
func ServiceConnections(listener net.Listener, p2id participant2ID, l Logger) (<-chan InMsg, func()) {
var stopFlag uint32
stop := func() {
atomic.StoreUint32(&stopFlag, 1)
listener.Close()
}
inMsgs := make(chan InMsg)
go func() {
for atomic.LoadUint32(&stopFlag) == 0 {
conn, err := listener.Accept()
if err != nil {
return
}
go handleConn(p2id, conn, inMsgs, &stopFlag, l)
}
}()
return inMsgs, stop
}
func Listen(addr string, rawCert []byte, privateKey []byte) net.Listener {
tlsConfig := baseTLSConfig.Clone()
cert, err := tls.X509KeyPair(rawCert, privateKey)
if err != nil {
panic(fmt.Errorf("failed parsing TLS certificate and private key: %v", err))
}
tlsConfig.Certificates = []tls.Certificate{cert}
lsnr, err := tls.Listen("tcp", addr, tlsConfig)
if err != nil {
panic(fmt.Errorf("failed listening on %s: %v", addr, err))
}
return lsnr
}
type participant2ID map[string]uint16
func handleConn(p2id participant2ID, conn net.Conn, inMsgs chan InMsg, stopFlag *uint32, l Logger) {
l.Debugf("Connection from %s", conn.RemoteAddr())
domain, from, authenticationSucceeded := authenticateConnection(p2id, conn, l)
if !authenticationSucceeded {
l.Warnf("Connection from %s failed authenticating", conn.RemoteAddr())
return
}
l.Debugf("Connection from %s authenticated as %d", conn.RemoteAddr(), from)
for atomic.LoadUint32(stopFlag) == 0 {
msgType, topic, data, err := readMsg(conn)
if err != nil {
fmt.Printf("Failed reading message from %s: %v\n", conn.RemoteAddr().String(), err)
return
}
if l.DebugEnabled() {
l.Debugf("Read message for %s of %d bytes from %d", hex.EncodeToString(topic), len(data), from)
}
inMsgs <- InMsg{
Domain: domain,
Type: uint8(msgType),
Topic: topic,
Data: data,
From: from,
}
}
}
func authenticateConnection(p2id participant2ID, conn net.Conn, logger Logger) (string, uint16, bool) {
var h Handshake
if err := h.Read(conn); err != nil {
logger.Warnf("failed authenticating %s: %v", conn.RemoteAddr().String(), err)
return "", 0, false
}
now := time.Now()
createTime := time.Unix(h.Timestamp, 0)
if createTime.Add(time.Second * 30).Before(now) {
logger.Warnf("Authentication message was created on %v and is too late in the past (now it's %v)", createTime, now)
}
binding := extractTLSBinding(conn)
if !bytes.Equal(binding, h.TLSBinding) {
logger.Warnf("TLS binding mismatch\n")
return "", 0, false
}
bl, _ := pem.Decode(h.Identity)
if bl == nil {
logger.Warnf("Identity received is not a PEM (%s)", string(h.Identity))
return "", 0, false
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
logger.Warnf("Identity received (%s) is not a valid x509 certificate: %v", string(h.Identity), err)
return "", 0, false
}
pk := cert.PublicKey.(*ecdsa.PublicKey)
sig := h.Signature
h.Signature = nil
if !ecdsa.VerifyASN1(pk, sha256Digest(h.Bytes()), sig) {
logger.Warnf("Signature mismatch")
return "", 0, false
}
lookupKey := hex.EncodeToString(sha256Digest([]byte(h.Domain), h.Identity))
from, exists := p2id[lookupKey]
if !exists {
logger.Warnf("Node %s doesn't exist", lookupKey)
return "", 0, false
}
logger.Debugf("Node %d authenticated", from)
return h.Domain, uint16(from), true
}
func readMsg(conn net.Conn) (MsgType, []byte, []byte, error) {
// Read message type and length to figure out whether message should have a topic or not
typeAndLengthBuff := make([]byte, 5)
if _, err := io.ReadFull(conn, typeAndLengthBuff); err != nil {
return 0, nil, nil, fmt.Errorf("failed reading length from %s: %v", conn.RemoteAddr().String(), err)
}
msgType := MsgType(typeAndLengthBuff[0])
bufferLength := binary.LittleEndian.Uint32(typeAndLengthBuff[1:])
if int(bufferLength) > maxBuffLen {
return 0, nil, nil, fmt.Errorf("buffer length too big (%d), allowed up to %d", bufferLength, maxBuffLen)
}
var topic []byte
if shouldHaveTopic[msgType] {
topic = make([]byte, 32)
if _, err := io.ReadFull(conn, topic); err != nil {
return 0, nil, nil, fmt.Errorf("failed reading topic from %s: %v", conn.RemoteAddr().String(), err)
}
}
buff := make([]byte, bufferLength)
if _, err := io.ReadFull(conn, buff); err != nil {
return 0, nil, nil, fmt.Errorf("failed reading data from %s: %v", conn.RemoteAddr().String(), err)
}
return msgType, topic, buff, nil
}
func sha256Digest(b ...[]byte) []byte {
hash := sha256.New()
for _, bytes := range b {
hash.Write(bytes)
}
return hash.Sum(nil)
}
func extractTLSBinding(conn net.Conn) []byte {
cs := conn.(*tls.Conn).ConnectionState()
tlsBinding, err := cs.ExportKeyingMaterial("MPC", []byte("MPC"), 32)
if err != nil {
panic("failed extracting TLS topic")
}
return tlsBinding
}
type Handshake struct {
Domain string
TLSBinding []byte
Identity []byte
Timestamp int64
Signature []byte
}
func (h *Handshake) Read(reader io.Reader) error {
lengthBuff := make([]byte, 2)
if _, err := io.ReadFull(reader, lengthBuff); err != nil {
return fmt.Errorf("failed reading size buffer: %v", err)
}
bufferLength := binary.LittleEndian.Uint16(lengthBuff)
if int(bufferLength) > maxBuffLen {
return fmt.Errorf("buffer length too big (%d), allowed up to %d", bufferLength, maxBuffLen)
}
buff := make([]byte, bufferLength)
if _, err := io.ReadFull(reader, buff); err != nil {
return fmt.Errorf("failed reading the buffer: %v", err)
}
if _, err := asn1.Unmarshal(buff, h); err != nil {
return fmt.Errorf("failed unmarshaling error: %v", err)
}
return nil
}
func (h *Handshake) Write(writer io.Writer) error {
data := h.Bytes()
lengthBuff := make([]byte, 2)
binary.LittleEndian.PutUint16(lengthBuff, uint16(len(data)))
_, err := writer.Write(lengthBuff)
if err != nil {
return fmt.Errorf("failed sending handshake message length: %w", err)
}
_, err = writer.Write(data)
if err != nil {
return fmt.Errorf("failed sending handshake data: %w", err)
}
return nil
}
func (h Handshake) Bytes() []byte {
b, err := asn1.Marshal(h)
if err != nil {
panic(err)
}
return b
}
func (rp *remoteParty) sendMessages() {
for {
if !rp.maybeConnect() {
time.Sleep(time.Second)
continue
}
msg := <-rp.msgs
rp.send(msg)
}
}
func (rp *remoteParty) send(msg *outMsg) {
// Ensure topic is either 32 bytes or 0 bytes
ctxSize := len(msg.topic)
if ctxSize != 0 && ctxSize != 32 {
panic("topic should be either empty or 32 bytes")
}
// Prepare the buffer to send the data
buffSize := 1 + 4 + len(msg.topic)
header := make([]byte, buffSize)
dataLen := len(msg.data)
if dataLen > math.MaxUint32 {
panic(fmt.Sprintf("data too large (doesn't fit in 16 bits): %d", dataLen))
}
header[0] = uint8(msg.msgType)
binary.LittleEndian.PutUint32(header[1:], uint32(dataLen))
if ctxSize > 0 {
copy(header[5:], msg.topic)
}
// Write the header to stream
_, err := rp.conn.Write(header)
if err != nil {
rp.reportErr("failed sending header of %d bytes to %s: %v", len(header), rp.endpoint, err)
rp.conn.Close()
rp.conn = nil
return
}
// Write the data to stream
_, err = rp.conn.Write(msg.data)
if err != nil {
rp.reportErr("failed sending data of %d bytes to %s: %v", len(msg.data), rp.endpoint, err)
rp.conn.Close()
rp.conn = nil
return
}
}
func (rp *remoteParty) maybeConnect() bool {
if rp.conn != nil {
return true
}
conn, err := tls.Dial("tcp", rp.endpoint, rp.tlsConf)
if err != nil {
rp.reportErr("failed connecting to %s: %v", rp.endpoint, err)
fmt.Println("failed to connect to :", rp.endpoint)
return false
}
rp.conn = conn
handshake := rp.authenticate(extractTLSBinding(rp.conn))
handshake.Domain = rp.domain
if err := handshake.Write(rp.conn); err != nil {
rp.reportErr("failed sending handshake to %s: %v", rp.endpoint, err)
rp.conn.Close()
rp.conn = nil
return false
}
return true
}