forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
558 lines (470 loc) · 14.8 KB
/
transport.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package kex2
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"errors"
"io"
"net"
"sync"
"time"
"github.com/ugorji/go/codec"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/net/context"
)
// DeviceID is a 16-byte identifier that each side of key exchange has. It's
// used primarily to tell sender from receiver.
type DeviceID [16]byte
// SessionID is a 32-byte session identifier that's derived from the shared
// session secret. It's used to route messages on the server side.
type SessionID [32]byte
// SecretLen is the number of bytes in the secret.
const SecretLen = 32
// Secret is the 32-byte shared secret identifier
type Secret [SecretLen]byte
// Seqno increments on every message sent from a Kex sender.
type Seqno uint32
// Eq returns true if the two device IDs are equal
func (d DeviceID) Eq(d2 DeviceID) bool {
return hmac.Equal(d[:], d2[:])
}
// Eq returns true if the two session IDs are equal
func (s SessionID) Eq(s2 SessionID) bool {
return hmac.Equal(s[:], s2[:])
}
// MessageRouter is a stateful message router that will be implemented by
// JSON/REST calls to the Keybase API server.
type MessageRouter interface {
// Post a message. Message will always be non-nil and non-empty.
// Even for an EOF, the empty buffer is encrypted via SecretBox,
// so the buffer posted to the server will have data.
Post(I SessionID, sender DeviceID, seqno Seqno, msg []byte) error
// Get messages on the channel. Only poll for `poll` milliseconds. If the timeout
// elapses without any data ready, then just return an empty result, with nil error.
// Several messages can be returned at once, which should be processed in serial.
// They are guaranteed to be in order; otherwise, there was an issue.
// Get() should only return a non-nil error if there was an HTTPS or TCP-level error.
// Application-level errors like EOF or no data ready are handled by modulating
// the `msgs` result.
Get(I SessionID, receiver DeviceID, seqno Seqno, poll time.Duration) (msg [][]byte, err error)
}
// Conn is a struct that obeys the net.Conn interface. It establishes a session abstraction
// over a message channel bounced off the Keybase API server, applying the appropriate
// e2e encryption/MAC'ing.
type Conn struct {
router MessageRouter
secret Secret
sessionID SessionID
deviceID DeviceID
// Protects the read path. There should only be one reader outstanding at once.
readMutex sync.Mutex
readSeqno Seqno
readDeadline time.Time
readTimeout time.Duration
bufferedMsgs [][]byte
// Protects the write path. There should only be one writer oustanding at once.
writeMutex sync.Mutex
writeSeqno Seqno
// Protects the setting of error states. Only one thread should be setting or
// accessing these errors at a time.
errMutex sync.Mutex
readErr error
writeErr error
ctx context.Context
}
const sessionIDText = "Kex v2 Session ID"
// NewConn establishes a Kex session based on the given secret. Will work for
// both ends of the connection, regardless of which order the two started
// their conntection. Will communicate with the other end via the given message router.
// You can specify an optional timeout to cancel any reads longer than that timeout.
func NewConn(ctx context.Context, r MessageRouter, s Secret, d DeviceID, readTimeout time.Duration) (con net.Conn, err error) {
mac := hmac.New(sha256.New, []byte(s[:]))
mac.Write([]byte(sessionIDText))
tmp := mac.Sum(nil)
var sessionID SessionID
copy(sessionID[:], tmp)
ret := &Conn{
router: r,
secret: s,
sessionID: sessionID,
deviceID: d,
readSeqno: 0,
readTimeout: readTimeout,
writeSeqno: 0,
ctx: ctx,
}
return ret, nil
}
// TimedoutError is for operations that timed out; for instance, if no read
// data was available before the deadline.
type timedoutError struct{}
// Error returns the string representation of this error
func (t timedoutError) Error() string { return "operation timed out" }
// Temporary returns if the error is retryable
func (t timedoutError) Temporary() bool { return true }
// Timeout returns if this error is a timeout
func (t timedoutError) Timeout() bool { return true }
// ErrTimedOut is the signleton error we use if the operation timedout.
var ErrTimedOut net.Error = timedoutError{}
// ErrUnimplemented indicates the given method isn't implemented
var ErrUnimplemented = errors.New("unimplemented")
// ErrBadMetadata indicates that the metadata outside the encrypted message
// didn't match what was inside.
var ErrBadMetadata = errors.New("bad metadata")
// ErrBadDecryption indicates that a ciphertext failed to decrypt or MAC properly
var ErrDecryption = errors.New("decryption failed")
// ErrNotEnoughRandomness indicates that encryption failed due to insufficient
// randomness
var ErrNotEnoughRandomness = errors.New("not enough random data")
// ErrBadPacketSequence indicates that packets arrived out of order from the
// server (which they shouldn't).
var ErrBadPacketSequence = errors.New("packets arrived out-of-order")
// ErrWrongSession indicatest that the given session didn't match the
// clients expectations
var ErrWrongSession = errors.New("got message for wrong Session ID")
// ErrSelfReceive indicates that the client received a message sent by
// itself, which should never happen
var ErrSelfRecieve = errors.New("got message back that we sent")
// ErrAgain indicates that no data was available to read, but the
// reader was in non-blocking mode, so to try again later.
var ErrAgain = errors.New("no data were ready to read")
// ErrBadSecret indicates that the secret received was invalid.
var ErrBadSecret = errors.New("bad secret")
// ErrHelloTimeout indicates that the Hello() part of the
// protocol timed out. Most likely due to an incorrect
// secret phrase from the user.
var ErrHelloTimeout = errors.New("hello timeout")
func (c *Conn) setReadError(e error) error {
c.errMutex.Lock()
c.readErr = e
c.errMutex.Unlock()
return e
}
func (c *Conn) setWriteError(e error) error {
c.errMutex.Lock()
c.writeErr = e
c.errMutex.Unlock()
return e
}
func (c *Conn) getErrorForWrite() error {
var err error
c.errMutex.Lock()
if c.readErr != nil && c.readErr != io.EOF {
err = c.readErr
} else if c.writeErr != nil {
err = c.writeErr
}
c.errMutex.Unlock()
return err
}
func (c *Conn) getErrorForRead() error {
var err error
c.errMutex.Lock()
if c.readErr != nil {
err = c.readErr
} else if c.writeErr != nil && c.writeErr != io.EOF {
err = c.writeErr
}
c.errMutex.Unlock()
return err
}
type outerMsg struct {
_struct bool `codec:",toarray"`
SenderID DeviceID `codec:"senderID"`
SessionID SessionID `codec:"sessionID"`
Seqno Seqno `codec:"seqno"`
Nonce [24]byte `codec:"nonce"`
Payload []byte `codec:"payload"`
}
type innerMsg struct {
_struct bool `codec:",toarray"`
SenderID DeviceID `codec:"senderID"`
SessionID SessionID `codec:"sessionID"`
Seqno Seqno `codec:"seqno"`
Payload []byte `codec:"payload"`
}
func (c *Conn) decryptIncomingMessage(msg []byte) (int, error) {
var err error
mh := codec.MsgpackHandle{WriteExt: true}
dec := codec.NewDecoderBytes(msg, &mh)
var om outerMsg
err = dec.Decode(&om)
if err != nil {
return 0, err
}
var plaintext []byte
var ok bool
plaintext, ok = secretbox.Open(plaintext, om.Payload, &om.Nonce, (*[32]byte)(&c.secret))
if !ok {
return 0, ErrDecryption
}
dec = codec.NewDecoderBytes(plaintext, &mh)
var im innerMsg
err = dec.Decode(&im)
if err != nil {
return 0, err
}
if !om.SenderID.Eq(im.SenderID) || !om.SessionID.Eq(im.SessionID) || om.Seqno != im.Seqno {
return 0, ErrBadMetadata
}
if !im.SessionID.Eq(c.sessionID) {
return 0, ErrWrongSession
}
if im.SenderID.Eq(c.deviceID) {
return 0, ErrSelfRecieve
}
if im.Seqno != c.readSeqno+1 {
return 0, ErrBadPacketSequence
}
c.readSeqno = im.Seqno
c.bufferedMsgs = append(c.bufferedMsgs, im.Payload)
return len(im.Payload), nil
}
func (c *Conn) decryptIncomingMessages(msgs [][]byte) (int, error) {
var ret int
for _, msg := range msgs {
n, e := c.decryptIncomingMessage(msg)
if e != nil {
return ret, e
}
ret += n
}
return ret, nil
}
func (c *Conn) readBufferedMsgsIntoBytes(out []byte) (int, error) {
p := 0
// If no buffered messages, then return that we didn't pull any
// new data from the server.
if len(c.bufferedMsgs) == 0 {
return 0, nil
}
// Any empty buffer signals an EOF condition
if len(c.bufferedMsgs[0]) == 0 {
return 0, io.EOF
}
for p < len(out) {
rem := len(out) - p
if len(c.bufferedMsgs) > 0 {
front := c.bufferedMsgs[0]
n := len(front)
// An empty buffer signifies that the other side wanted
// and EOF condition. However, we shouldn't return an EOF
// if we've read anything, this time through.
if n == 0 {
var err error
if p == 0 {
err = io.EOF
}
return p, err
}
if rem < n {
n = rem
copy(out[p:(p+n)], front[0:n])
front = front[n:]
if len(front) == 0 {
// Be careful not to recycle an empty buffer into the
// list of buffered messages, since that has special
// significance (see above).
c.bufferedMsgs = c.bufferedMsgs[1:]
} else {
c.bufferedMsgs[0] = front
}
} else {
copy(out[p:(p+n)], front[:])
c.bufferedMsgs = c.bufferedMsgs[1:]
}
p += n
} else {
break
}
}
return p, nil
}
func (c *Conn) pollLoop(poll time.Duration) (msgs [][]byte, err error) {
var totalWaitTime time.Duration
start := time.Now()
for {
newPoll := poll - totalWaitTime
msgs, err = c.router.Get(c.sessionID, c.deviceID, c.readSeqno+1, newPoll)
totalWaitTime = time.Since(start)
if err != nil || len(msgs) > 0 || totalWaitTime >= poll {
return
}
select {
case <-c.ctx.Done():
return nil, ErrCanceled
default:
}
}
}
// Read data from the connection, returning plaintext data if all
// cryptographic checks passed. Obeys the `net.Conn` interface.
// Returns the number of bytes read into the output buffer.
func (c *Conn) Read(out []byte) (n int, err error) {
c.readMutex.Lock()
defer c.readMutex.Unlock()
// The first error kills the whole stream
if err = c.getErrorForRead(); err != nil {
return 0, err
}
// First see if there's anything buffered, and read that
// out now.
if n, err = c.readBufferedMsgsIntoBytes(out); err != nil {
return 0, c.setReadError(err)
}
if n > 0 {
return n, nil
}
var poll time.Duration
if !c.readDeadline.IsZero() {
poll = c.readDeadline.Sub(time.Now())
if poll.Nanoseconds() < 0 {
return 0, c.setReadError(ErrTimedOut)
}
} else {
poll = c.readTimeout
}
var msgs [][]byte
msgs, err = c.pollLoop(poll)
if err != nil {
return 0, c.setReadError(err)
}
if _, err = c.decryptIncomingMessages(msgs); err != nil {
return 0, c.setReadError(err)
}
if n, err = c.readBufferedMsgsIntoBytes(out); err != nil {
return 0, c.setReadError(err)
}
if n == 0 {
if poll > 0 {
err = ErrTimedOut
} else {
err = ErrAgain
}
}
return n, err
}
func (c *Conn) encryptOutgoingMessage(seqno Seqno, buf []byte) (ret []byte, err error) {
var nonce [24]byte
var n int
if n, err = rand.Read(nonce[:]); err != nil {
return nil, err
} else if n != 24 {
return nil, ErrNotEnoughRandomness
}
im := innerMsg{
SenderID: c.deviceID,
SessionID: c.sessionID,
Seqno: seqno,
Payload: buf,
}
mh := codec.MsgpackHandle{WriteExt: true}
var imPacked []byte
enc := codec.NewEncoderBytes(&imPacked, &mh)
if err = enc.Encode(im); err != nil {
return nil, err
}
ciphertext := secretbox.Seal(nil, imPacked, &nonce, (*[32]byte)(&c.secret))
om := outerMsg{
SenderID: c.deviceID,
SessionID: c.sessionID,
Seqno: seqno,
Nonce: nonce,
Payload: ciphertext,
}
enc = codec.NewEncoderBytes(&ret, &mh)
if err = enc.Encode(om); err != nil {
return nil, err
}
return ret, nil
}
func (c *Conn) nextWriteSeqno() Seqno {
c.writeSeqno++
return c.writeSeqno
}
// Write data to the connection, encrypting and MAC'ing along the way.
// Obeys the `net.Conn` interface
func (c *Conn) Write(buf []byte) (n int, err error) {
c.writeMutex.Lock()
defer c.writeMutex.Unlock()
// Our protocol specifes that writing an empty buffer means "close"
// the connection. We don't want callers of `Write` to do this by
// accident, we want them to call `Close()` explicitly. So short-circuit
// the write operation here for empty buffers.
if len(buf) == 0 {
return 0, nil
}
return c.writeWithLock(buf)
}
func (c *Conn) writeWithLock(buf []byte) (n int, err error) {
var ctext []byte
// The first error kills the whole stream
if err = c.getErrorForWrite(); err != nil {
return 0, err
}
seqno := c.nextWriteSeqno()
ctext, err = c.encryptOutgoingMessage(seqno, buf)
if err != nil {
return 0, c.setWriteError(err)
}
if err = c.router.Post(c.sessionID, c.deviceID, seqno, ctext); err != nil {
return 0, c.setWriteError(err)
}
return len(ctext), nil
}
// Close the connection to the server, sending an empty buffer via POST
// through the `MessageRouter`. Fulfills the `net.Conn` interface
func (c *Conn) Close() error {
c.writeMutex.Lock()
defer c.writeMutex.Unlock()
// Write an empty buffer to signal EOF
if _, err := c.writeWithLock([]byte{}); err != nil {
return err
}
// All subsequent writes should fail.
c.setWriteError(io.EOF)
return nil
}
// LocalAddr returns the local network address, fulfilling the `net.Conn interface`
func (c *Conn) LocalAddr() (addr net.Addr) {
return
}
// RemoteAddr returns the remote network address, fulfilling the `net.Conn interface`
func (c *Conn) RemoteAddr() (addr net.Addr) {
return
}
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future I/O, not just
// the immediately following call to Read or Write.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
func (c *Conn) SetDeadline(t time.Time) error {
return c.SetReadDeadline(t)
}
// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
func (c *Conn) SetReadDeadline(t time.Time) error {
c.readMutex.Lock()
c.readDeadline = t
c.readMutex.Unlock()
return nil
}
// SetWriteDeadline sets the deadline for future Write calls.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
// We're not implementing this feature for now, so make it an error
// if we try to do so.
func (c *Conn) SetWriteDeadline(t time.Time) error {
return ErrUnimplemented
}