-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathextend.go
252 lines (203 loc) · 6.73 KB
/
extend.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
// Copyright 2015 The GoTor Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
)
func (c *OnionConnection) handleRelayExtend(circ *Circuit, cell *RelayCell) ActionableError {
data := cell.Data()
Log(LOG_CIRC, "Got extend!")
if circ.nextHop != nil {
return CloseCircuit(errors.New("We already have a next hop."), DESTROY_REASON_PROTOCOL)
}
if circ.extendState != nil {
return CloseCircuit(errors.New("Refusing attempt to extend a circuit twice"), DESTROY_REASON_PROTOCOL)
}
if len(data) != 212 {
return CloseCircuit(errors.New("malformed EXTEND cell"), DESTROY_REASON_PROTOCOL)
}
// Check that we're not connecting back to the source
if c.theyAuthenticated {
sameFP := true
for i := 0; i < 20; i++ {
if c.theirFingerprint[i] != data[192+i] {
sameFP = false
break
}
}
if sameFP {
return CloseCircuit(errors.New("not extending to the source"), DESTROY_REASON_PROTOCOL)
}
}
circReq := &CircuitRequest{}
circReq.connHint.AddAddress(data[0:6])
circReq.connHint.AddFingerprint(data[192:212])
circReq.handshakeData = make([]byte, 186)
copy(circReq.handshakeData, data[6:192])
circReq.handshakeType = uint16(HANDSHAKE_TAP)
circReq.successQueue = c.circuitReadQueue
circReq.newHandshake = false
circReq.localID = circ.id
circReq.handshakeState = &CircuitHandshakeState{}
circ.extendState = circReq.handshakeState
if err := c.parentOR.RequestCircuit(circReq); err != nil {
return CloseCircuit(err, DESTROY_REASON_INTERNAL)
}
return nil
}
func (c *OnionConnection) handleRelayExtend2(circ *Circuit, cell *RelayCell) ActionableError {
Log(LOG_CIRC, "got extend")
data := cell.Data()
nspec := int(data[0])
if 1+(nspec*2)+4 > len(data) {
return CloseCircuit(errors.New("malformed EXTEND cell"), DESTROY_REASON_PROTOCOL)
}
if circ.nextHop != nil {
return CloseCircuit(errors.New("We already have a next hop."), DESTROY_REASON_PROTOCOL)
}
if circ.extendState != nil {
return CloseCircuit(errors.New("Refusing attempt to extend a circuit twice"), DESTROY_REASON_PROTOCOL)
}
circReq := &CircuitRequest{}
circReq.newHandshake = true
readPos := 1
for i := 0; i < nspec; i++ {
lstype := data[readPos]
lslen := int(data[readPos+1])
readPos += 2
if readPos+lslen > len(data)-4 {
return CloseCircuit(errors.New("malformed EXTEND cell"), DESTROY_REASON_PROTOCOL)
}
lsdata := data[readPos : readPos+lslen]
readPos += lslen
if lstype == 0 || lstype == 1 {
if err := circReq.connHint.AddAddress(lsdata); err != nil {
return CloseCircuit(err, DESTROY_REASON_PROTOCOL)
}
} else if lstype == 2 {
if err := circReq.connHint.AddFingerprint(lsdata); err != nil {
return CloseCircuit(err, DESTROY_REASON_PROTOCOL)
}
// Check that we're not connecting back to the source
if c.theyAuthenticated {
sameFP := true
for i := 0; i < 20; i++ {
if c.theirFingerprint[i] != lsdata[i] {
sameFP = false
break
}
}
if sameFP {
return CloseCircuit(errors.New("not extending to the source"), DESTROY_REASON_PROTOCOL)
}
}
} else {
Log(LOG_INFO, "ignoring unknown link specifier type %d", lstype)
}
}
htype := BigEndian.Uint16(data[readPos : readPos+2])
hlen := int(BigEndian.Uint16(data[readPos+2 : readPos+4]))
readPos += 4
if len(data) < readPos+hlen {
return CloseCircuit(errors.New("malformed EXTEND cell"), DESTROY_REASON_PROTOCOL)
}
if nspec < 2 {
return CloseCircuit(errors.New("EXTEND cell is super small.."), DESTROY_REASON_PROTOCOL)
}
circReq.handshakeData = make([]byte, hlen) // XXX use a cellbuf
copy(circReq.handshakeData, data[readPos:readPos+hlen])
circReq.handshakeType = htype
circReq.successQueue = c.circuitReadQueue
circReq.localID = circ.id
circReq.handshakeState = &CircuitHandshakeState{}
circ.extendState = circReq.handshakeState
if err := c.parentOR.RequestCircuit(circReq); err != nil {
return CloseCircuit(err, DESTROY_REASON_INTERNAL)
}
return nil
}
func (c *OnionConnection) handleCreated(cell Cell, newHandshake bool) ActionableError {
circ, ok := c.relayCircuits[cell.CircID()]
if !ok {
return RefuseCircuit(errors.New(cell.Command().String()+": no such circuit?"), DESTROY_REASON_PROTOCOL)
}
Log(LOG_CIRC, "got a created: %d", cell.CircID())
data := cell.Data()
hlen := 148
pos := 0
if newHandshake {
hlen = int(BigEndian.Uint16(data[0:2]))
pos = 2
}
if hlen+pos > len(data) {
return CloseCircuit(errors.New(cell.Command().String()+" cell badly formed"), DESTROY_REASON_PROTOCOL)
}
hdata := make([]byte, hlen) // XXX use a cellbuf
copy(hdata, data[pos:pos+hlen])
// Relay the good news
circ.previousHop <- &CircuitCreated{
id: circ.theirID,
handshakeData: hdata,
newHandshake: newHandshake,
}
return nil
}
func (data *CircuitCreated) Handle(c *OnionConnection, circ *Circuit) ActionableError {
if circ.nextHop != nil {
panic("We managed to create two circuits?")
}
if circ.extendState == nil {
panic("we didn't expect to extend") // XXX this could maybe be triggered by a client?
}
extendState := circ.extendState
circ.nextHop = extendState.nextHop
circ.nextHopID = extendState.nextHopID
circ.extendState = nil
if data.newHandshake {
cell := GetCellBuf(false)
defer ReturnCellBuf(cell) // XXX such a waste
BigEndian.PutUint16(cell[0:2], uint16(len(data.handshakeData)))
copy(cell[2:], data.handshakeData)
// circuit streamid direction command data
return c.sendRelayCell(circ, 0, BackwardDirection, RELAY_EXTENDED2, cell[0:2+len(data.handshakeData)])
} else {
// circuit streamid direction command data
return c.sendRelayCell(circ, 0, BackwardDirection, RELAY_EXTENDED, data.handshakeData)
}
}
func (req *CircuitRequest) Handle(c *OnionConnection, notreallyanthingatall *Circuit) ActionableError {
newID := c.NewCircID()
req.handshakeState.lock.Lock()
aborted := req.handshakeState.aborted
if !aborted {
req.handshakeState.nextHop = c.circuitReadQueue
req.handshakeState.nextHopID = newID
}
req.handshakeState.lock.Unlock()
if aborted {
Log(LOG_INFO, "Aborting CREATE - origin is gone")
return nil
}
cmd := CMD_CREATE2
if !req.newHandshake {
cmd = CMD_CREATE
}
writeCell := NewCell(c.negotiatedVersion, newID, cmd, nil)
data := writeCell.Data()
if req.newHandshake {
BigEndian.PutUint16(data[0:2], uint16(req.handshakeType))
BigEndian.PutUint16(data[2:4], uint16(len(req.handshakeData)))
copy(data[4:], req.handshakeData)
} else {
copy(data, req.handshakeData)
}
// XXX if they send data before the created2, it'll nicely work
c.relayCircuits[writeCell.CircID()] = &RelayCircuit{
id: writeCell.CircID(),
theirID: req.localID,
previousHop: req.successQueue,
}
c.writeQueue <- writeCell.Bytes()
return nil
}