forked from libp2p/go-libp2p-noise
-
Notifications
You must be signed in to change notification settings - Fork 3
/
xx_handshake.go
277 lines (219 loc) · 8.21 KB
/
xx_handshake.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
package noise
import (
"context"
"fmt"
proto "github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p-core/peer"
pb "github.com/ChainSafe/go-libp2p-noise/pb"
xx "github.com/ChainSafe/go-libp2p-noise/xx"
)
func (s *secureSession) xx_sendHandshakeMessage(payload []byte, initial_stage bool) error {
var msgbuf xx.MessageBuffer
s.xx_ns, msgbuf = xx.SendMessage(s.xx_ns, payload, nil)
var encMsgBuf []byte
if initial_stage {
encMsgBuf = msgbuf.Encode0()
} else {
encMsgBuf = msgbuf.Encode1()
}
err := s.writeLength(len(encMsgBuf))
if err != nil {
log.Error("xx_sendHandshakeMessage initiator=%v err=%s", s.initiator, err)
return fmt.Errorf("xx_sendHandshakeMessage write length err=%s", err)
}
_, err = s.insecure.Write(encMsgBuf)
if err != nil {
log.Error("xx_sendHandshakeMessage initiator=%v err=%s", s.initiator, err)
return fmt.Errorf("xx_sendHandshakeMessage write to conn err=%s", err)
}
log.Debugf("xx_sendHandshakeMessage initiator=%v msgbuf=%v initial_stage=%v", s.initiator, msgbuf, initial_stage)
return nil
}
func (s *secureSession) xx_recvHandshakeMessage(initial_stage bool) (buf []byte, plaintext []byte, valid bool, err error) {
l, err := s.readLength()
if err != nil {
return nil, nil, false, fmt.Errorf("xx_recvHandshakeMessage read length err=%s", err)
}
buf = make([]byte, l)
_, err = s.insecure.Read(buf)
if err != nil {
return buf, nil, false, fmt.Errorf("xx_recvHandshakeMessage read from conn err=%s", err)
}
var msgbuf *xx.MessageBuffer
if initial_stage {
msgbuf, err = xx.Decode0(buf)
} else {
msgbuf, err = xx.Decode1(buf)
}
if err != nil {
log.Debugf("xx_recvHandshakeMessage initiator=%v decode err=%s", s.initiator, err)
return buf, nil, false, fmt.Errorf("xx_recvHandshakeMessage decode msg err=%s", err)
}
s.xx_ns, plaintext, valid = xx.RecvMessage(s.xx_ns, msgbuf)
if !valid {
log.Error("xx_recvHandshakeMessage initiator=%v err=validation fail", s.initiator)
return buf, nil, false, fmt.Errorf("xx_recvHandshakeMessage validation fail")
}
log.Debugf("xx_recvHandshakeMessage initiator=%v msgbuf=%v initial_stage=%v", s.initiator, msgbuf, initial_stage)
return buf, plaintext, valid, nil
}
// Runs the XX handshake
// XX:
// -> e
// <- e, ee, s, es
// -> s, se
// if fallback = true, initialMsg is used as the message in stage 1 of the initiator and stage 0
// of the responder
func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, payload []byte, initialMsg []byte) (err error) {
kp := xx.NewKeypair(s.noiseKeypair.public_key, s.noiseKeypair.private_key)
log.Debugf("runHandshake_xx initiator=%v fallback=%v pubkey=%x", s.initiator, fallback, kp.PubKey())
// new XX noise session
s.xx_ns = xx.InitSession(s.initiator, s.prologue, kp, [32]byte{})
if s.initiator {
// stage 0 //
if !fallback {
err = s.xx_sendHandshakeMessage(nil, true)
if err != nil {
return fmt.Errorf("runHandshake_xx stage 0 initiator fail: %s", err)
}
} else {
e_ik := s.ik_ns.Ephemeral()
log.Debugf("runHandshake_xx stage=0 initiator=true fallback=true ephemeralkeys=%x", e_ik)
e_xx := xx.NewKeypair(e_ik.PubKey(), e_ik.PrivKey())
// initialize state as if we sent the first message
var msgbuf xx.MessageBuffer
s.xx_ns, msgbuf = xx.SendMessage(s.xx_ns, nil, &e_xx)
log.Debugf("runHandshake_xx stage=0 initiator=true fallback=true msgbuf=%v", msgbuf)
}
// stage 1 //
var plaintext []byte
var valid bool
if !fallback {
// read reply
_, plaintext, valid, err = s.xx_recvHandshakeMessage(false)
if err != nil {
return fmt.Errorf("runHandshake_xx initiator stage 1 fail: %s", err)
}
if !valid {
return fmt.Errorf("runHandshake_xx stage 1 initiator validation fail")
}
} else {
var msgbuf *xx.MessageBuffer
msgbuf, err = xx.Decode1(initialMsg)
log.Debugf("xx_recvHandshakeMessage stage=1 initiator=%v msgbuf=%v", s.initiator, msgbuf)
if err != nil {
log.Debugf("xx_recvHandshakeMessage stage=1 initiator=%v decode_err=%s", s.initiator, err)
return fmt.Errorf("runHandshake_xx decode msg fail: %s", err)
}
s.xx_ns, plaintext, valid = xx.RecvMessage(s.xx_ns, msgbuf)
if !valid {
log.Errorf("xx_recvHandshakeMessage initiator=%v", s.initiator, "error", "validation fail")
return fmt.Errorf("runHandshake_xx validation fail")
}
}
// stage 2 //
err = s.xx_sendHandshakeMessage(payload, false)
if err != nil {
return fmt.Errorf("runHandshake_xx stage=2 initiator=true err=%s", err)
}
// unmarshal payload
nhp := new(pb.NoiseHandshakePayload)
err = proto.Unmarshal(plaintext, nhp)
if err != nil {
return fmt.Errorf("runHandshake_xx stage=2 initiator=true err=cannot unmarshal payload")
}
// set remote libp2p public key
err = s.setRemotePeerInfo(nhp.GetLibp2PKey())
if err != nil {
log.Errorf("runHandshake_xx stage=2 initiator=true set remote peer info err=%s", err)
return fmt.Errorf("runHandshake_xx stage=2 initiator=true read remote libp2p key fail")
}
// assert that remote peer ID matches libp2p public key
pid, err := peer.IDFromPublicKey(s.RemotePublicKey())
if pid != s.remotePeer {
log.Errorf("runHandshake_xx stage=2 initiator=true check remote peer id err: expected %x got %x", s.remotePeer, pid)
} else if err != nil {
log.Errorf("runHandshake_xx stage 2 initiator check remote peer id err %s", err)
}
// verify payload is signed by libp2p key
err = s.verifyPayload(nhp, s.xx_ns.RemoteKey())
if err != nil {
log.Errorf("runHandshake_xx stage=2 initiator=true verify payload err=%s", err)
}
if s.noisePipesSupport {
s.noiseStaticKeyCache[s.remotePeer] = s.xx_ns.RemoteKey()
}
} else {
// stage 0 //
var plaintext []byte
var valid bool
nhp := new(pb.NoiseHandshakePayload)
if !fallback {
// read message
_, plaintext, valid, err = s.xx_recvHandshakeMessage(true)
if err != nil {
return fmt.Errorf("runHandshake_xx stage=0 initiator=false err=%s", err)
}
if !valid {
return fmt.Errorf("runHandshake_xx stage=0 initiator=false err=validation fail")
}
} else {
var msgbuf *xx.MessageBuffer
msgbuf, err = xx.Decode1(initialMsg)
if err != nil {
log.Errorf("runHandshake_xx recv msg err", err)
return err
}
xx_msgbuf := xx.NewMessageBuffer(msgbuf.NE(), nil, nil)
log.Debugf("runHandshake_xx initiator=false msgbuf=%v modified_msgbuf=%v", msgbuf, xx_msgbuf)
s.xx_ns, plaintext, valid = xx.RecvMessage(s.xx_ns, &xx_msgbuf)
if !valid {
log.Errorf("runHandshake_xx initiator=false recv msg err=%s", "validation fail")
return fmt.Errorf("runHandshake_xx validation fail")
}
}
// stage 1 //
err = s.xx_sendHandshakeMessage(payload, false)
if err != nil {
return fmt.Errorf("runHandshake_xx stage=1 initiator=false err=%s", err)
}
// stage 2 //
// read message
_, plaintext, valid, err = s.xx_recvHandshakeMessage(false)
if err != nil {
return fmt.Errorf("runHandshake_xx stage=2 initiator=false err=%s", err)
}
if !valid {
return fmt.Errorf("runHandshake_xx stage=2 initiator=false err=validation fail")
}
log.Debugf("runHandshake_xx stage=2 initiator=false remote key=%x", s.xx_ns.RemoteKey())
// unmarshal payload
err = proto.Unmarshal(plaintext, nhp)
if err != nil {
return fmt.Errorf("runHandshake_xx stage=2 initiator=false err=cannot unmarshal payload")
}
// set remote libp2p public key
err = s.setRemotePeerInfo(nhp.GetLibp2PKey())
if err != nil {
log.Errorf("runHandshake_xx stage=2 initiator=false set remote peer info err=%s", err)
return fmt.Errorf("runHandshake_xx stage=2 initiator=false read remote libp2p key fail")
}
// assert that remote peer ID matches libp2p key
err = s.setRemotePeerID(s.RemotePublicKey())
if err != nil {
log.Errorf("runHandshake_xx stage=2 initiator=false set remote peer id err=%s", err)
}
s.remote.noiseKey = s.xx_ns.RemoteKey()
// verify payload is signed by libp2p key
err = s.verifyPayload(nhp, s.remote.noiseKey)
if err != nil {
log.Errorf("runHandshake_xx stage=2 initiator=false verify payload err=%s", err)
return fmt.Errorf("runHandshake_xx stage=2 initiator=false err=%s", err)
}
if s.noisePipesSupport {
s.noiseStaticKeyCache[s.remotePeer] = s.remote.noiseKey
}
}
log.Debugf("runHandshake_xx done initiator=%v", s.initiator)
return nil
}