aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/handshake.go
blob: 17f572deab6813cadde36a1d266281dc8a8fec2b (plain) (blame)
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
package p2p

import (
    "crypto/ecdsa"
    "crypto/rand"
    "errors"
    "fmt"
    "hash"
    "io"
    "net"

    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/crypto/ecies"
    "github.com/ethereum/go-ethereum/crypto/secp256k1"
    "github.com/ethereum/go-ethereum/crypto/sha3"
    "github.com/ethereum/go-ethereum/p2p/discover"
    "github.com/ethereum/go-ethereum/rlp"
)

const (
    sskLen = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
    sigLen = 65 // elliptic S256
    pubLen = 64 // 512 bit pubkey in uncompressed representation without format byte
    shaLen = 32 // hash length (for nonce etc)

    authMsgLen  = sigLen + shaLen + pubLen + shaLen + 1
    authRespLen = pubLen + shaLen + 1

    eciesBytes = 65 + 16 + 32
    iHSLen     = authMsgLen + eciesBytes  // size of the final ECIES payload sent as initiator's handshake
    rHSLen     = authRespLen + eciesBytes // size of the final ECIES payload sent as receiver's handshake
)

type conn struct {
    *frameRW
    *protoHandshake
}

func newConn(fd net.Conn, hs *protoHandshake) *conn {
    return &conn{newFrameRW(fd, msgWriteTimeout), hs}
}

// encHandshake contains the state of the encryption handshake.
type encHandshake struct {
    remoteID             discover.NodeID
    initiator            bool
    initNonce, respNonce []byte
    dhSharedSecret       []byte
    randomPrivKey        *ecdsa.PrivateKey
    remoteRandomPub      *ecdsa.PublicKey
}

// secrets represents the connection secrets
// which are negotiated during the encryption handshake.
type secrets struct {
    RemoteID              discover.NodeID
    AES, MAC              []byte
    EgressMAC, IngressMAC hash.Hash
    Token                 []byte
}

// protoHandshake is the RLP structure of the protocol handshake.
type protoHandshake struct {
    Version    uint64
    Name       string
    Caps       []Cap
    ListenPort uint64
    ID         discover.NodeID
}

// secrets is called after the handshake is completed.
// It extracts the connection secrets from the handshake values.
func (h *encHandshake) secrets(auth, authResp []byte) secrets {
    sharedSecret := crypto.Sha3(h.dhSharedSecret, crypto.Sha3(h.respNonce, h.initNonce))
    aesSecret := crypto.Sha3(h.dhSharedSecret, sharedSecret)
    s := secrets{
        RemoteID: h.remoteID,
        AES:      aesSecret,
        MAC:      crypto.Sha3(h.dhSharedSecret, aesSecret),
        Token:    crypto.Sha3(sharedSecret),
    }

    // setup sha3 instances for the MACs
    mac1 := sha3.NewKeccak256()
    mac1.Write(xor(s.MAC, h.respNonce))
    mac1.Write(auth)
    mac2 := sha3.NewKeccak256()
    mac2.Write(xor(s.MAC, h.initNonce))
    mac2.Write(authResp)
    if h.initiator {
        s.EgressMAC, s.IngressMAC = mac1, mac2
    } else {
        s.EgressMAC, s.IngressMAC = mac2, mac1
    }

    return s
}

// setupConn starts a protocol session on the given connection.
// It runs the encryption handshake and the protocol handshake.
// If dial is non-nil, the connection the local node is the initiator.
func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node) (*conn, error) {
    if dial == nil {
        return setupInboundConn(fd, prv, our)
    } else {
        return setupOutboundConn(fd, prv, our, dial)
    }
}

func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake) (*conn, error) {
    secrets, err := inboundEncHandshake(fd, prv, nil)
    if err != nil {
        return nil, fmt.Errorf("encryption handshake failed: %v", err)
    }

    // Run the protocol handshake using authenticated messages.
    // TODO: move buffering setup here (out of newFrameRW)
    phsrw := newRlpxFrameRW(fd, secrets)
    rhs, err := readProtocolHandshake(phsrw, our)
    if err != nil {
        return nil, err
    }
    if err := writeProtocolHandshake(phsrw, our); err != nil {
        return nil, fmt.Errorf("protocol write error: %v", err)
    }

    rw := newFrameRW(fd, msgWriteTimeout)
    return &conn{rw, rhs}, nil
}

func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node) (*conn, error) {
    secrets, err := outboundEncHandshake(fd, prv, dial.ID[:], nil)
    if err != nil {
        return nil, fmt.Errorf("encryption handshake failed: %v", err)
    }

    // Run the protocol handshake using authenticated messages.
    // TODO: move buffering setup here (out of newFrameRW)
    phsrw := newRlpxFrameRW(fd, secrets)
    if err := writeProtocolHandshake(phsrw, our); err != nil {
        return nil, fmt.Errorf("protocol write error: %v", err)
    }
    rhs, err := readProtocolHandshake(phsrw, our)
    if err != nil {
        return nil, fmt.Errorf("protocol handshake read error: %v", err)
    }
    if rhs.ID != dial.ID {
        return nil, errors.New("dialed node id mismatch")
    }

    rw := newFrameRW(fd, msgWriteTimeout)
    return &conn{rw, rhs}, nil
}

// outboundEncHandshake negotiates a session token on conn.
// it should be called on the dialing side of the connection.
//
// privateKey is the local client's private key
// remotePublicKey is the remote peer's node ID
// sessionToken is the token from a previous session with this node.
func outboundEncHandshake(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, remotePublicKey []byte, sessionToken []byte) (s secrets, err error) {
    auth, initNonce, randomPrivKey, err := authMsg(prvKey, remotePublicKey, sessionToken)
    if err != nil {
        return s, err
    }
    if _, err = conn.Write(auth); err != nil {
        return s, err
    }

    response := make([]byte, rHSLen)
    if _, err = io.ReadFull(conn, response); err != nil {
        return s, err
    }
    recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prvKey)
    if err != nil {
        return s, err
    }

    h := &encHandshake{
        initiator:       true,
        initNonce:       initNonce,
        respNonce:       recNonce,
        randomPrivKey:   randomPrivKey,
        remoteRandomPub: remoteRandomPubKey,
    }
    copy(h.remoteID[:], remotePublicKey)
    return h.secrets(auth, response), nil
}

// authMsg creates the initiator handshake.
// TODO: change all the names
func authMsg(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (
    auth, initNonce []byte,
    randomPrvKey *ecdsa.PrivateKey,
    err error,
) {
    remotePubKey, err := importPublicKey(remotePubKeyS)
    if err != nil {
        return
    }

    var tokenFlag byte
    if sessionToken == nil {
        // no session token found means we need to generate shared secret.
        // ecies shared secret is used as initial session token for new peers
        // generate shared key from prv and remote pubkey
        if sessionToken, err = ecies.ImportECDSA(prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
            return
        }
    } else {
        // for known peers, we use stored token from the previous session
        tokenFlag = 0x01
    }

    //E(remote-pubk, S(ecdhe-random, sha3(ecdh-shared-secret^nonce)) || H(ecdhe-random-pubk) || pubk || nonce || 0x0)
    // E(remote-pubk, S(ecdhe-random, sha3(token^nonce)) || H(ecdhe-random-pubk) || pubk || nonce || 0x1)
    // allocate msgLen long message,
    var msg []byte = make([]byte, authMsgLen)
    initNonce = msg[authMsgLen-shaLen-1 : authMsgLen-1]
    if _, err = rand.Read(initNonce); err != nil {
        return
    }
    // create known message
    // ecdh-shared-secret^nonce for new peers
    // token^nonce for old peers
    var sharedSecret = xor(sessionToken, initNonce)

    // generate random keypair to use for signing
    if randomPrvKey, err = crypto.GenerateKey(); err != nil {
        return
    }
    // sign shared secret (message known to both parties): shared-secret
    var signature []byte
    // signature = sign(ecdhe-random, shared-secret)
    // uses secp256k1.Sign
    if signature, err = crypto.Sign(sharedSecret, randomPrvKey); err != nil {
        return
    }

    // message
    // signed-shared-secret || H(ecdhe-random-pubk) || pubk || nonce || 0x0
    copy(msg, signature) // copy signed-shared-secret
    // H(ecdhe-random-pubk)
    var randomPubKey64 []byte
    if randomPubKey64, err = exportPublicKey(&randomPrvKey.PublicKey); err != nil {
        return
    }
    var pubKey64 []byte
    if pubKey64, err = exportPublicKey(&prvKey.PublicKey); err != nil {
        return
    }
    copy(msg[sigLen:sigLen+shaLen], crypto.Sha3(randomPubKey64))
    // pubkey copied to the correct segment.
    copy(msg[sigLen+shaLen:sigLen+shaLen+pubLen], pubKey64)
    // nonce is already in the slice
    // stick tokenFlag byte to the end
    msg[authMsgLen-1] = tokenFlag

    // encrypt using remote-pubk
    // auth = eciesEncrypt(remote-pubk, msg)
    if auth, err = crypto.Encrypt(remotePubKey, msg); err != nil {
        return
    }
    return
}

// completeHandshake is called when the initiator receives an
// authentication response (aka receiver handshake). It completes the
// handshake by reading off parameters the remote peer provides needed
// to set up the secure session.
func completeHandshake(auth []byte, prvKey *ecdsa.PrivateKey) (
    respNonce []byte,
    remoteRandomPubKey *ecdsa.PublicKey,
    tokenFlag bool,
    err error,
) {
    var msg []byte
    // they prove that msg is meant for me,
    // I prove I possess private key if i can read it
    if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
        return
    }

    respNonce = msg[pubLen : pubLen+shaLen]
    var remoteRandomPubKeyS = msg[:pubLen]
    if remoteRandomPubKey, err = importPublicKey(remoteRandomPubKeyS); err != nil {
        return
    }
    if msg[authRespLen-1] == 0x01 {
        tokenFlag = true
    }
    return
}

// inboundEncHandshake negotiates a session token on conn.
// it should be called on the listening side of the connection.
//
// privateKey is the local client's private key
// sessionToken is the token from a previous session with this node.
func inboundEncHandshake(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, sessionToken []byte) (s secrets, err error) {
    // we are listening connection. we are responders in the
    // handshake. Extract info from the authentication. The initiator
    // starts by sending us a handshake that we need to respond to. so
    // we read auth message first, then respond.
    auth := make([]byte, iHSLen)
    if _, err := io.ReadFull(conn, auth); err != nil {
        return s, err
    }
    response, recNonce, initNonce, remotePubKey, randomPrivKey, remoteRandomPubKey, err := authResp(auth, sessionToken, prvKey)
    if err != nil {
        return s, err
    }
    if _, err = conn.Write(response); err != nil {
        return s, err
    }

    h := &encHandshake{
        initiator:       false,
        initNonce:       initNonce,
        respNonce:       recNonce,
        randomPrivKey:   randomPrivKey,
        remoteRandomPub: remoteRandomPubKey,
    }
    copy(h.remoteID[:], remotePubKey)
    return h.secrets(auth, response), err
}

// authResp is called by peer if it accepted (but not
// initiated) the connection from the remote. It is passed the initiator
// handshake received and the session token belonging to the
// remote initiator.
//
// The first return value is the authentication response (aka receiver
// handshake) that is to be sent to the remote initiator.
func authResp(auth, sessionToken []byte, prvKey *ecdsa.PrivateKey) (
    authResp, respNonce, initNonce, remotePubKeyS []byte,
    randomPrivKey *ecdsa.PrivateKey,
    remoteRandomPubKey *ecdsa.PublicKey,
    err error,
) {
    // they prove that msg is meant for me,
    // I prove I possess private key if i can read it
    msg, err := crypto.Decrypt(prvKey, auth)
    if err != nil {
        return
    }

    remotePubKeyS = msg[sigLen+shaLen : sigLen+shaLen+pubLen]
    remotePubKey, _ := importPublicKey(remotePubKeyS)

    var tokenFlag byte
    if sessionToken == nil {
        // no session token found means we need to generate shared secret.
        // ecies shared secret is used as initial session token for new peers
        // generate shared key from prv and remote pubkey
        if sessionToken, err = ecies.ImportECDSA(prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
            return
        }
        // tokenFlag = 0x00 // redundant
    } else {
        // for known peers, we use stored token from the previous session
        tokenFlag = 0x01
    }

    // the initiator nonce is read off the end of the message
    initNonce = msg[authMsgLen-shaLen-1 : authMsgLen-1]
    // I prove that i own prv key (to derive shared secret, and read
    // nonce off encrypted msg) and that I own shared secret they
    // prove they own the private key belonging to ecdhe-random-pubk
    // we can now reconstruct the signed message and recover the peers
    // pubkey
    var signedMsg = xor(sessionToken, initNonce)
    var remoteRandomPubKeyS []byte
    if remoteRandomPubKeyS, err = secp256k1.RecoverPubkey(signedMsg, msg[:sigLen]); err != nil {
        return
    }
    // convert to ECDSA standard
    if remoteRandomPubKey, err = importPublicKey(remoteRandomPubKeyS); err != nil {
        return
    }

    // now we find ourselves a long task too, fill it random
    var resp = make([]byte, authRespLen)
    // generate shaLen long nonce
    respNonce = resp[pubLen : pubLen+shaLen]
    if _, err = rand.Read(respNonce); err != nil {
        return
    }
    // generate random keypair for session
    if randomPrivKey, err = crypto.GenerateKey(); err != nil {
        return
    }
    // responder auth message
    // E(remote-pubk, ecdhe-random-pubk || nonce || 0x0)
    var randomPubKeyS []byte
    if randomPubKeyS, err = exportPublicKey(&randomPrivKey.PublicKey); err != nil {
        return
    }
    copy(resp[:pubLen], randomPubKeyS)
    // nonce is already in the slice
    resp[authRespLen-1] = tokenFlag

    // encrypt using remote-pubk
    // auth = eciesEncrypt(remote-pubk, msg)
    // why not encrypt with ecdhe-random-remote
    if authResp, err = crypto.Encrypt(remotePubKey, resp); err != nil {
        return
    }
    return
}

// importPublicKey unmarshals 512 bit public keys.
func importPublicKey(pubKey []byte) (pubKeyEC *ecdsa.PublicKey, err error) {
    var pubKey65 []byte
    switch len(pubKey) {
    case 64:
        // add 'uncompressed key' flag
        pubKey65 = append([]byte{0x04}, pubKey...)
    case 65:
        pubKey65 = pubKey
    default:
        return nil, fmt.Errorf("invalid public key length %v (expect 64/65)", len(pubKey))
    }
    return crypto.ToECDSAPub(pubKey65), nil
}

func exportPublicKey(pubKeyEC *ecdsa.PublicKey) (pubKey []byte, err error) {
    if pubKeyEC == nil {
        return nil, fmt.Errorf("no ECDSA public key given")
    }
    return crypto.FromECDSAPub(pubKeyEC)[1:], nil
}

func xor(one, other []byte) (xor []byte) {
    xor = make([]byte, len(one))
    for i := 0; i < len(one); i++ {
        xor[i] = one[i] ^ other[i]
    }
    return xor
}

func writeProtocolHandshake(w MsgWriter, our *protoHandshake) error {
    return EncodeMsg(w, handshakeMsg, our.Version, our.Name, our.Caps, our.ListenPort, our.ID[:])
}

func readProtocolHandshake(r MsgReader, our *protoHandshake) (*protoHandshake, error) {
    // read and handle remote handshake
    msg, err := r.ReadMsg()
    if err != nil {
        return nil, err
    }
    if msg.Code == discMsg {
        // disconnect before protocol handshake is valid according to the
        // spec and we send it ourself if Server.addPeer fails.
        var reason DiscReason
        rlp.Decode(msg.Payload, &reason)
        return nil, discRequestedError(reason)
    }
    if msg.Code != handshakeMsg {
        return nil, fmt.Errorf("expected handshake, got %x", msg.Code)
    }
    if msg.Size > baseProtocolMaxMsgSize {
        return nil, fmt.Errorf("message too big (%d > %d)", msg.Size, baseProtocolMaxMsgSize)
    }
    var hs protoHandshake
    if err := msg.Decode(&hs); err != nil {
        return nil, err
    }
    // validate handshake info
    if hs.Version != our.Version {
        return nil, newPeerError(errP2PVersionMismatch, "required version %d, received %d\n", baseProtocolVersion, hs.Version)
    }
    if (hs.ID == discover.NodeID{}) {
        return nil, newPeerError(errPubkeyInvalid, "missing")
    }
    return &hs, nil
}