forked from vuvuzela/vuvuzela
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
292 lines (243 loc) · 7.31 KB
/
client.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
// Copyright 2015 David Lazar. All rights reserved.
// Use of this source code is governed by the GNU AGPL
// license that can be found in the LICENSE file.
package vuvuzela
import (
"crypto/ed25519"
"fmt"
"strings"
"sync"
"time"
"github.com/davidlazar/go-crypto/encoding/base32"
"golang.org/x/crypto/nacl/box"
"vuvuzela.io/alpenhorn/config"
"vuvuzela.io/alpenhorn/errors"
"vuvuzela.io/alpenhorn/typesocket"
"vuvuzela.io/crypto/onionbox"
"vuvuzela.io/vuvuzela/convo"
"vuvuzela.io/vuvuzela/coordinator"
"vuvuzela.io/vuvuzela/mixnet"
)
type Client struct {
PersistPath string
CoordinatorLatency time.Duration // Eventually we will measure this.
ConfigClient *config.Client
Handler ConvoHandler
mu sync.Mutex
rounds map[uint32]*roundState
conn typesocket.Conn
latestRound uint32
convoConfig *config.SignedConfig
convoConfigHash string
}
type roundState struct {
Config *convo.ConvoConfig
ConfigParent *config.SignedConfig
mu sync.Mutex
OnionKeys [][]*[32]byte // [msg][mixer]
}
type ConvoHandler interface {
Outgoing(round uint32) []*convo.DeadDropMessage
Replies(round uint32, messages [][]byte)
NewConfig(chain []*config.SignedConfig)
Error(err error)
DebugError(err error)
GlobalAnnouncement(message string)
}
func (c *Client) ConnectConvo() (chan error, error) {
if c.Handler == nil {
return nil, errors.New("no convo handler")
}
c.mu.Lock()
if c.rounds == nil {
c.rounds = make(map[uint32]*roundState)
}
c.mu.Unlock()
// Fetch the current config to get the coordinator's key and address.
convoConfig, err := c.ConfigClient.CurrentConfig("Convo")
if err != nil {
return nil, errors.Wrap(err, "fetching latest convo config")
}
convoInner := convoConfig.Inner.(*convo.ConvoConfig)
wsAddr := fmt.Sprintf("wss://%s/convo/ws", convoInner.Coordinator.Address)
conn, err := typesocket.Dial(wsAddr, convoInner.Coordinator.Key)
if err != nil {
return nil, err
}
c.mu.Lock()
c.conn = conn
c.mu.Unlock()
disconnect := make(chan error, 1)
go func() {
disconnect <- conn.Serve(c.convoMux())
c.mu.Lock()
c.conn = nil
c.mu.Unlock()
}()
return disconnect, nil
}
func (c *Client) CloseConvo() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
conn := c.conn
c.conn = nil
return conn.Close()
}
return nil
}
func (c *Client) LatestRound() (uint32, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn == nil {
return 0, errors.New("not connected to convo service")
}
return c.latestRound, nil
}
func (c *Client) setLatestRound(round uint32) {
c.mu.Lock()
if round > c.latestRound {
c.latestRound = round
}
c.mu.Unlock()
}
func (c *Client) convoMux() typesocket.Mux {
return typesocket.NewMux(map[string]interface{}{
"announcement": c.globalAnnouncement,
"newround": c.newConvoRound,
"reply": c.openReplyOnion,
"error": c.convoRoundError,
})
}
func (c *Client) globalAnnouncement(conn typesocket.Conn, v coordinator.GlobalAnnouncement) {
c.Handler.GlobalAnnouncement(v.Message)
}
func (c *Client) convoRoundError(conn typesocket.Conn, v coordinator.RoundError) {
if strings.Contains(v.Err, "round is closed:") || strings.Contains(v.Err, "round not found") {
// The client now supports retransmission so it's safe to ignore these errors by default.
c.Handler.DebugError(errors.New("error from convo coordinator: round %d: %s", v.Round, v.Err))
} else {
c.Handler.Error(errors.New("error from convo coordinator: round %d: %s", v.Round, v.Err))
}
}
func (c *Client) newConvoRound(conn typesocket.Conn, v coordinator.NewRound) {
c.setLatestRound(v.Round)
if time.Until(v.EndTime) < 20*time.Millisecond {
c.Handler.DebugError(errors.New("newConvoRound %d: skipping round (only %s left)", v.Round, time.Until(v.EndTime)))
return
}
c.mu.Lock()
defer c.mu.Unlock()
st, ok := c.rounds[v.Round]
if ok {
if st.ConfigParent.Hash() != v.ConfigHash {
c.Handler.Error(errors.New("coordinator announced different configs round %d", v.Round))
}
return
}
var conf *config.SignedConfig
if v.ConfigHash == c.convoConfigHash {
// common case
conf = c.convoConfig
} else {
// Fetch the new config.
configs, err := c.ConfigClient.FetchAndVerifyChain(c.convoConfig, v.ConfigHash)
if err != nil {
c.Handler.Error(errors.Wrap(err, "fetching convo config"))
return
}
c.Handler.NewConfig(configs)
newConfig := configs[0]
c.convoConfig = newConfig
c.convoConfigHash = v.ConfigHash
if err := c.persistLocked(); err != nil {
panic("failed to persist state: " + err.Error())
}
conf = newConfig
}
st = &roundState{
Config: conf.Inner.(*convo.ConvoConfig),
ConfigParent: conf,
}
c.rounds[v.Round] = st
// Run the rest of the round in a new goroutine to release the client lock.
go c.runRound(conn, st, v)
return
}
func (c *Client) runRound(conn typesocket.Conn, st *roundState, v coordinator.NewRound) {
round := v.Round
settingsMsg := v.MixSettings.SigningMessage()
for i, mixer := range st.Config.MixServers {
if !ed25519.Verify(mixer.Key, settingsMsg, v.MixSignatures[i]) {
err := errors.New(
"round %d: failed to verify mixnet settings for key %s",
round, base32.EncodeToString(mixer.Key),
)
c.Handler.Error(err)
return
}
}
if time.Until(v.EndTime) < c.CoordinatorLatency {
c.Handler.DebugError(errors.New("runRound %d: skipping round (only %s left)", v.Round, time.Until(v.EndTime)))
return
}
time.Sleep(time.Until(v.EndTime) - c.CoordinatorLatency - 10*time.Millisecond)
outgoing := c.Handler.Outgoing(round)
onionKeys := make([][]*[32]byte, len(outgoing))
onions := make([][]byte, len(outgoing))
for i, deadDropMsg := range outgoing {
msg := deadDropMsg.Marshal()
onions[i], onionKeys[i] = onionbox.Seal(msg, mixnet.ForwardNonce(round), v.MixSettings.OnionKeys)
}
st.mu.Lock()
st.OnionKeys = onionKeys
st.mu.Unlock()
if time.Until(v.EndTime) < 10*time.Millisecond {
c.Handler.DebugError(errors.New("runRound %d: abandoning round (only %s left)", round, time.Until(v.EndTime)))
return
}
conn.Send("onion", coordinator.OnionMsg{
Round: round,
Onions: onions,
})
}
func (c *Client) openReplyOnion(conn typesocket.Conn, v coordinator.OnionMsg) {
c.mu.Lock()
st, ok := c.rounds[v.Round]
c.mu.Unlock()
if !ok {
c.Handler.Error(errors.New("openReplyOnion: round %d not configured", v.Round))
return
}
st.mu.Lock()
onionKeys := st.OnionKeys
st.mu.Unlock()
if onionKeys == nil {
c.Handler.Error(errors.New("openReplyOnion: didn't generate onion keys for round %d", v.Round))
return
}
if len(st.OnionKeys) != len(v.Onions) {
err := errors.New("round %d: expected %d onions, got %d", v.Round, len(st.OnionKeys), len(v.Onions))
c.Handler.Error(err)
return
}
expectedOnionSize := convo.SizeEncryptedMessageBody + len(st.Config.MixServers)*box.Overhead
msgs := make([][]byte, len(v.Onions))
for i, onion := range v.Onions {
if len(onion) != expectedOnionSize {
err := errors.New("convo round %d: received malformed onion: got %d bytes, want %d bytes", v.Round, len(onion), expectedOnionSize)
c.Handler.Error(err)
continue
}
msg, ok := onionbox.Open(onion, mixnet.BackwardNonce(v.Round), st.OnionKeys[i])
if !ok {
err := errors.New("convo round %d: failed to decrypt onion", v.Round)
c.Handler.Error(err)
}
msgs[i] = msg
}
c.Handler.Replies(v.Round, msgs)
c.mu.Lock()
delete(c.rounds, v.Round)
c.mu.Unlock()
}