2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-02-27 00:30:34 +02:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2015-05-16 01:38:28 +03:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2015-02-27 00:30:34 +02:00
|
|
|
"crypto/hmac"
|
2015-05-16 01:38:28 +03:00
|
|
|
"crypto/rand"
|
2015-12-23 02:48:55 +02:00
|
|
|
"encoding/binary"
|
2015-02-27 00:30:34 +02:00
|
|
|
"errors"
|
2015-05-16 01:38:28 +03:00
|
|
|
"fmt"
|
2015-02-27 00:30:34 +02:00
|
|
|
"hash"
|
|
|
|
"io"
|
2017-09-26 16:54:49 +03:00
|
|
|
"io/ioutil"
|
2015-12-23 02:48:55 +02:00
|
|
|
mrand "math/rand"
|
2015-05-16 01:38:28 +03:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-02-27 00:30:34 +02:00
|
|
|
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/bitutil"
|
2015-05-16 01:38:28 +03:00
|
|
|
"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"
|
2015-02-27 00:30:34 +02:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2017-09-26 16:54:49 +03:00
|
|
|
"github.com/golang/snappy"
|
2015-02-27 00:30:34 +02:00
|
|
|
)
|
|
|
|
|
2015-05-16 01:38:28 +03:00
|
|
|
const (
|
|
|
|
maxUint24 = ^uint32(0) >> 8
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
eciesOverhead = 65 /* pubkey */ + 16 /* IV */ + 32 /* MAC */
|
|
|
|
|
|
|
|
encAuthMsgLen = authMsgLen + eciesOverhead // size of encrypted pre-EIP-8 initiator handshake
|
|
|
|
encAuthRespLen = authRespLen + eciesOverhead // size of encrypted pre-EIP-8 handshake reply
|
2015-05-16 01:38:28 +03:00
|
|
|
|
|
|
|
// total timeout for encryption handshake and protocol
|
|
|
|
// handshake in both directions.
|
|
|
|
handshakeTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
// This is the timeout for sending the disconnect reason.
|
|
|
|
// This is shorter than the usual timeout because we don't want
|
|
|
|
// to wait if the connection is known to be bad anyway.
|
|
|
|
discWriteTimeout = 1 * time.Second
|
|
|
|
)
|
|
|
|
|
2017-09-26 16:54:49 +03:00
|
|
|
// errPlainMessageTooLarge is returned if a decompressed message length exceeds
|
|
|
|
// the allowed 24 bits (i.e. length >= 16MB).
|
|
|
|
var errPlainMessageTooLarge = errors.New("message length >= 16MB")
|
|
|
|
|
2015-05-16 01:38:28 +03:00
|
|
|
// rlpx is the transport protocol used by actual (non-test) connections.
|
|
|
|
// It wraps the frame encoder with locks and read/write deadlines.
|
|
|
|
type rlpx struct {
|
|
|
|
fd net.Conn
|
|
|
|
|
|
|
|
rmu, wmu sync.Mutex
|
|
|
|
rw *rlpxFrameRW
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRLPX(fd net.Conn) transport {
|
|
|
|
fd.SetDeadline(time.Now().Add(handshakeTimeout))
|
|
|
|
return &rlpx{fd: fd}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rlpx) ReadMsg() (Msg, error) {
|
|
|
|
t.rmu.Lock()
|
|
|
|
defer t.rmu.Unlock()
|
|
|
|
t.fd.SetReadDeadline(time.Now().Add(frameReadTimeout))
|
|
|
|
return t.rw.ReadMsg()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rlpx) WriteMsg(msg Msg) error {
|
|
|
|
t.wmu.Lock()
|
|
|
|
defer t.wmu.Unlock()
|
|
|
|
t.fd.SetWriteDeadline(time.Now().Add(frameWriteTimeout))
|
|
|
|
return t.rw.WriteMsg(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rlpx) close(err error) {
|
|
|
|
t.wmu.Lock()
|
|
|
|
defer t.wmu.Unlock()
|
|
|
|
// Tell the remote end why we're disconnecting if possible.
|
|
|
|
if t.rw != nil {
|
|
|
|
if r, ok := err.(DiscReason); ok && r != DiscNetworkError {
|
2018-02-22 13:41:06 +03:00
|
|
|
// rlpx tries to send DiscReason to disconnected peer
|
|
|
|
// if the connection is net.Pipe (in-memory simulation)
|
|
|
|
// it hangs forever, since net.Pipe does not implement
|
|
|
|
// a write deadline. Because of this only try to send
|
|
|
|
// the disconnect reason message if there is no error.
|
|
|
|
if err := t.fd.SetWriteDeadline(time.Now().Add(discWriteTimeout)); err == nil {
|
|
|
|
SendItems(t.rw, discMsg, r)
|
|
|
|
}
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t.fd.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *rlpx) doProtoHandshake(our *protoHandshake) (their *protoHandshake, err error) {
|
|
|
|
// Writing our handshake happens concurrently, we prefer
|
|
|
|
// returning the handshake read error. If the remote side
|
|
|
|
// disconnects us early with a valid reason, we should return it
|
|
|
|
// as the error so it can be tracked elsewhere.
|
|
|
|
werr := make(chan error, 1)
|
|
|
|
go func() { werr <- Send(t.rw, handshakeMsg, our) }()
|
|
|
|
if their, err = readProtocolHandshake(t.rw, our); err != nil {
|
2015-06-09 16:05:40 +03:00
|
|
|
<-werr // make sure the write terminates too
|
2015-05-16 01:38:28 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := <-werr; err != nil {
|
|
|
|
return nil, fmt.Errorf("write error: %v", err)
|
|
|
|
}
|
2017-09-26 16:54:49 +03:00
|
|
|
// If the protocol version supports Snappy encoding, upgrade immediately
|
|
|
|
t.rw.snappy = their.Version >= snappyProtocolVersion
|
|
|
|
|
2015-05-16 01:38:28 +03:00
|
|
|
return their, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readProtocolHandshake(rw MsgReader, our *protoHandshake) (*protoHandshake, error) {
|
|
|
|
msg, err := rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if msg.Size > baseProtocolMaxMsgSize {
|
|
|
|
return nil, fmt.Errorf("message too big")
|
|
|
|
}
|
|
|
|
if msg.Code == discMsg {
|
|
|
|
// Disconnect before protocol handshake is valid according to the
|
|
|
|
// spec and we send it ourself if the posthanshake checks fail.
|
|
|
|
// We can't return the reason directly, though, because it is echoed
|
|
|
|
// back otherwise. Wrap it in a string instead.
|
|
|
|
var reason [1]DiscReason
|
|
|
|
rlp.Decode(msg.Payload, &reason)
|
|
|
|
return nil, reason[0]
|
|
|
|
}
|
|
|
|
if msg.Code != handshakeMsg {
|
|
|
|
return nil, fmt.Errorf("expected handshake, got %x", msg.Code)
|
|
|
|
}
|
|
|
|
var hs protoHandshake
|
|
|
|
if err := msg.Decode(&hs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
if len(hs.ID) != 64 || !bitutil.TestBytes(hs.ID) {
|
2015-05-16 01:38:28 +03:00
|
|
|
return nil, DiscInvalidIdentity
|
|
|
|
}
|
|
|
|
return &hs, nil
|
|
|
|
}
|
|
|
|
|
2018-02-26 19:22:46 +03:00
|
|
|
// doEncHandshake runs the protocol handshake using authenticated
|
|
|
|
// messages. the protocol handshake is the first authenticated message
|
|
|
|
// and also verifies whether the encryption handshake 'worked' and the
|
|
|
|
// remote side actually provided the right public key.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
func (t *rlpx) doEncHandshake(prv *ecdsa.PrivateKey, dial *ecdsa.PublicKey) (*ecdsa.PublicKey, error) {
|
2015-05-16 01:38:28 +03:00
|
|
|
var (
|
|
|
|
sec secrets
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if dial == nil {
|
2018-07-23 18:36:08 +03:00
|
|
|
sec, err = receiverEncHandshake(t.fd, prv)
|
2015-05-16 01:38:28 +03:00
|
|
|
} else {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
sec, err = initiatorEncHandshake(t.fd, prv, dial)
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
return nil, err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
t.wmu.Lock()
|
|
|
|
t.rw = newRLPXFrameRW(t.fd, sec)
|
|
|
|
t.wmu.Unlock()
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
return sec.Remote.ExportECDSA(), nil
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// encHandshake contains the state of the encryption handshake.
|
|
|
|
type encHandshake struct {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
initiator bool
|
|
|
|
remote *ecies.PublicKey // remote-pubk
|
2015-05-16 01:38:28 +03:00
|
|
|
initNonce, respNonce []byte // nonce
|
|
|
|
randomPrivKey *ecies.PrivateKey // ecdhe-random
|
|
|
|
remoteRandomPub *ecies.PublicKey // ecdhe-random-pubk
|
|
|
|
}
|
|
|
|
|
|
|
|
// secrets represents the connection secrets
|
|
|
|
// which are negotiated during the encryption handshake.
|
|
|
|
type secrets struct {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
Remote *ecies.PublicKey
|
2015-05-16 01:38:28 +03:00
|
|
|
AES, MAC []byte
|
|
|
|
EgressMAC, IngressMAC hash.Hash
|
|
|
|
Token []byte
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
// RLPx v4 handshake auth (defined in EIP-8).
|
|
|
|
type authMsgV4 struct {
|
|
|
|
gotPlain bool // whether read packet had plain format.
|
|
|
|
|
|
|
|
Signature [sigLen]byte
|
|
|
|
InitiatorPubkey [pubLen]byte
|
|
|
|
Nonce [shaLen]byte
|
|
|
|
Version uint
|
|
|
|
|
|
|
|
// Ignore additional fields (forward-compatibility)
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLPx v4 handshake response (defined in EIP-8).
|
|
|
|
type authRespV4 struct {
|
|
|
|
RandomPubkey [pubLen]byte
|
|
|
|
Nonce [shaLen]byte
|
|
|
|
Version uint
|
|
|
|
|
|
|
|
// Ignore additional fields (forward-compatibility)
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:38:28 +03:00
|
|
|
// 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, error) {
|
|
|
|
ecdheSecret, err := h.randomPrivKey.GenerateShared(h.remoteRandomPub, sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return secrets{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive base secrets from ephemeral key agreement
|
2016-02-21 20:40:27 +02:00
|
|
|
sharedSecret := crypto.Keccak256(ecdheSecret, crypto.Keccak256(h.respNonce, h.initNonce))
|
|
|
|
aesSecret := crypto.Keccak256(ecdheSecret, sharedSecret)
|
2015-05-16 01:38:28 +03:00
|
|
|
s := secrets{
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
Remote: h.remote,
|
|
|
|
AES: aesSecret,
|
|
|
|
MAC: crypto.Keccak256(ecdheSecret, aesSecret),
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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, nil
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
// staticSharedSecret returns the static shared secret, the result
|
|
|
|
// of key agreement between the local and remote static node key.
|
|
|
|
func (h *encHandshake) staticSharedSecret(prv *ecdsa.PrivateKey) ([]byte, error) {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
return ecies.ImportECDSA(prv).GenerateShared(h.remote, sskLen, sskLen)
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initiatorEncHandshake negotiates a session token on conn.
|
|
|
|
// it should be called on the dialing side of the connection.
|
|
|
|
//
|
|
|
|
// prv is the local client's private key.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ecdsa.PublicKey) (s secrets, err error) {
|
|
|
|
h := &encHandshake{initiator: true, remote: ecies.ImportECDSAPublic(remote)}
|
2018-07-23 18:36:08 +03:00
|
|
|
authMsg, err := h.makeAuthMsg(prv)
|
2015-05-16 01:38:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2016-04-04 00:08:09 +03:00
|
|
|
authPacket, err := sealEIP8(authMsg, h)
|
2015-05-16 01:38:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
if _, err = conn.Write(authPacket); err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
authRespMsg := new(authRespV4)
|
|
|
|
authRespPacket, err := readHandshakeMsg(authRespMsg, encAuthRespLen, prv, conn)
|
|
|
|
if err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return s, err
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
if err := h.handleAuthResp(authRespMsg); err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return s, err
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
return h.secrets(authPacket, authRespPacket)
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
// makeAuthMsg creates the initiator handshake message.
|
2018-07-23 18:36:08 +03:00
|
|
|
func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) {
|
2015-12-23 02:48:55 +02:00
|
|
|
// Generate random initiator nonce.
|
|
|
|
h.initNonce = make([]byte, shaLen)
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
_, err := rand.Read(h.initNonce)
|
|
|
|
if err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
// Generate random keypair to for ECDH.
|
2017-02-18 11:24:12 +03:00
|
|
|
h.randomPrivKey, err = ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
|
2015-05-16 01:38:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
// Sign known message: static-shared-secret ^ nonce
|
2018-07-23 18:36:08 +03:00
|
|
|
token, err := h.staticSharedSecret(prv)
|
2015-12-23 02:48:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
signed := xor(token, h.initNonce)
|
|
|
|
signature, err := crypto.Sign(signed, h.randomPrivKey.ExportECDSA())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
msg := new(authMsgV4)
|
|
|
|
copy(msg.Signature[:], signature)
|
|
|
|
copy(msg.InitiatorPubkey[:], crypto.FromECDSAPub(&prv.PublicKey)[1:])
|
|
|
|
copy(msg.Nonce[:], h.initNonce)
|
|
|
|
msg.Version = 4
|
|
|
|
return msg, nil
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) {
|
|
|
|
h.respNonce = msg.Nonce[:]
|
|
|
|
h.remoteRandomPub, err = importPublicKey(msg.RandomPubkey[:])
|
|
|
|
return err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// receiverEncHandshake negotiates a session token on conn.
|
|
|
|
// it should be called on the listening side of the connection.
|
|
|
|
//
|
|
|
|
// prv is the local client's private key.
|
2018-07-23 18:36:08 +03:00
|
|
|
func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s secrets, err error) {
|
2015-12-23 02:48:55 +02:00
|
|
|
authMsg := new(authMsgV4)
|
|
|
|
authPacket, err := readHandshakeMsg(authMsg, encAuthMsgLen, prv, conn)
|
|
|
|
if err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return s, err
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
h := new(encHandshake)
|
|
|
|
if err := h.handleAuthMsg(authMsg, prv); err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
authRespMsg, err := h.makeAuthResp()
|
2015-05-16 01:38:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
var authRespPacket []byte
|
|
|
|
if authMsg.gotPlain {
|
|
|
|
authRespPacket, err = authRespMsg.sealPlain(h)
|
|
|
|
} else {
|
|
|
|
authRespPacket, err = sealEIP8(authRespMsg, h)
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-12-23 02:48:55 +02:00
|
|
|
return s, err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
if _, err = conn.Write(authRespPacket); err != nil {
|
|
|
|
return s, err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
return h.secrets(authPacket, authRespPacket)
|
|
|
|
}
|
2015-05-16 01:38:28 +03:00
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) error {
|
|
|
|
// Import the remote identity.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
rpub, err := importPublicKey(msg.InitiatorPubkey[:])
|
2015-05-16 01:38:28 +03:00
|
|
|
if err != nil {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
return err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
h.initNonce = msg.Nonce[:]
|
|
|
|
h.remote = rpub
|
2015-05-16 01:38:28 +03:00
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
// Generate random keypair for ECDH.
|
|
|
|
// If a private key is already set, use it instead of generating one (for testing).
|
|
|
|
if h.randomPrivKey == nil {
|
2017-02-18 11:24:12 +03:00
|
|
|
h.randomPrivKey, err = ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
|
2015-12-23 02:48:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
|
|
|
|
// Check the signature.
|
|
|
|
token, err := h.staticSharedSecret(prv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-16 01:38:28 +03:00
|
|
|
signedMsg := xor(token, h.initNonce)
|
2015-12-23 02:48:55 +02:00
|
|
|
remoteRandomPub, err := secp256k1.RecoverPubkey(signedMsg, msg.Signature[:])
|
2015-05-16 01:38:28 +03:00
|
|
|
if err != nil {
|
2015-12-23 02:48:55 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.remoteRandomPub, _ = importPublicKey(remoteRandomPub)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) {
|
|
|
|
// Generate random nonce.
|
|
|
|
h.respNonce = make([]byte, shaLen)
|
|
|
|
if _, err = rand.Read(h.respNonce); err != nil {
|
2015-05-16 01:38:28 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-14 05:21:02 +03:00
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
msg = new(authRespV4)
|
|
|
|
copy(msg.Nonce[:], h.respNonce)
|
|
|
|
copy(msg.RandomPubkey[:], exportPubkey(&h.randomPrivKey.PublicKey))
|
|
|
|
msg.Version = 4
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *authMsgV4) sealPlain(h *encHandshake) ([]byte, error) {
|
|
|
|
buf := make([]byte, authMsgLen)
|
|
|
|
n := copy(buf, msg.Signature[:])
|
2016-02-21 20:40:27 +02:00
|
|
|
n += copy(buf[n:], crypto.Keccak256(exportPubkey(&h.randomPrivKey.PublicKey)))
|
2015-12-23 02:48:55 +02:00
|
|
|
n += copy(buf[n:], msg.InitiatorPubkey[:])
|
|
|
|
n += copy(buf[n:], msg.Nonce[:])
|
|
|
|
buf[n] = 0 // token-flag
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
return ecies.Encrypt(rand.Reader, h.remote, buf, nil, nil)
|
2015-12-23 02:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *authMsgV4) decodePlain(input []byte) {
|
|
|
|
n := copy(msg.Signature[:], input)
|
|
|
|
n += shaLen // skip sha3(initiator-ephemeral-pubk)
|
|
|
|
n += copy(msg.InitiatorPubkey[:], input[n:])
|
2017-01-09 13:16:06 +03:00
|
|
|
copy(msg.Nonce[:], input[n:])
|
2015-12-23 02:48:55 +02:00
|
|
|
msg.Version = 4
|
|
|
|
msg.gotPlain = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *authRespV4) sealPlain(hs *encHandshake) ([]byte, error) {
|
|
|
|
buf := make([]byte, authRespLen)
|
|
|
|
n := copy(buf, msg.RandomPubkey[:])
|
2017-01-09 13:16:06 +03:00
|
|
|
copy(buf[n:], msg.Nonce[:])
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
return ecies.Encrypt(rand.Reader, hs.remote, buf, nil, nil)
|
2015-12-23 02:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *authRespV4) decodePlain(input []byte) {
|
|
|
|
n := copy(msg.RandomPubkey[:], input)
|
2017-01-09 13:16:06 +03:00
|
|
|
copy(msg.Nonce[:], input[n:])
|
2015-12-23 02:48:55 +02:00
|
|
|
msg.Version = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
var padSpace = make([]byte, 300)
|
|
|
|
|
|
|
|
func sealEIP8(msg interface{}, h *encHandshake) ([]byte, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := rlp.Encode(buf, msg); err != nil {
|
|
|
|
return nil, err
|
2015-07-14 05:21:02 +03:00
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
// pad with random amount of data. the amount needs to be at least 100 bytes to make
|
|
|
|
// the message distinguishable from pre-EIP-8 handshakes.
|
|
|
|
pad := padSpace[:mrand.Intn(len(padSpace)-100)+100]
|
|
|
|
buf.Write(pad)
|
|
|
|
prefix := make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(prefix, uint16(buf.Len()+eciesOverhead))
|
2015-07-14 05:21:02 +03:00
|
|
|
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
enc, err := ecies.Encrypt(rand.Reader, h.remote, buf.Bytes(), nil, prefix)
|
2015-12-23 02:48:55 +02:00
|
|
|
return append(prefix, enc...), err
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-23 02:48:55 +02:00
|
|
|
type plainDecoder interface {
|
|
|
|
decodePlain([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readHandshakeMsg(msg plainDecoder, plainSize int, prv *ecdsa.PrivateKey, r io.Reader) ([]byte, error) {
|
|
|
|
buf := make([]byte, plainSize)
|
|
|
|
if _, err := io.ReadFull(r, buf); err != nil {
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
// Attempt decoding pre-EIP-8 "plain" format.
|
|
|
|
key := ecies.ImportECDSA(prv)
|
2018-03-26 13:46:18 +03:00
|
|
|
if dec, err := key.Decrypt(buf, nil, nil); err == nil {
|
2015-12-23 02:48:55 +02:00
|
|
|
msg.decodePlain(dec)
|
|
|
|
return buf, nil
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
2015-12-23 02:48:55 +02:00
|
|
|
// Could be EIP-8 format, try that.
|
|
|
|
prefix := buf[:2]
|
|
|
|
size := binary.BigEndian.Uint16(prefix)
|
|
|
|
if size < uint16(plainSize) {
|
|
|
|
return buf, fmt.Errorf("size underflow, need at least %d bytes", plainSize)
|
|
|
|
}
|
|
|
|
buf = append(buf, make([]byte, size-uint16(plainSize)+2)...)
|
|
|
|
if _, err := io.ReadFull(r, buf[plainSize:]); err != nil {
|
|
|
|
return buf, err
|
|
|
|
}
|
2018-03-26 13:46:18 +03:00
|
|
|
dec, err := key.Decrypt(buf[2:], nil, prefix)
|
2015-12-23 02:48:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
// Can't use rlp.DecodeBytes here because it rejects
|
|
|
|
// trailing data (forward-compatibility).
|
|
|
|
s := rlp.NewStream(bytes.NewReader(dec), 0)
|
|
|
|
return buf, s.Decode(msg)
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// importPublicKey unmarshals 512 bit public keys.
|
|
|
|
func importPublicKey(pubKey []byte) (*ecies.PublicKey, 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))
|
|
|
|
}
|
|
|
|
// TODO: fewer pointless conversions
|
2018-06-12 16:26:08 +03:00
|
|
|
pub, err := crypto.UnmarshalPubkey(pubKey65)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-12-23 02:48:55 +02:00
|
|
|
}
|
|
|
|
return ecies.ImportECDSAPublic(pub), nil
|
2015-05-16 01:38:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func exportPubkey(pub *ecies.PublicKey) []byte {
|
|
|
|
if pub == nil {
|
|
|
|
panic("nil pubkey")
|
|
|
|
}
|
|
|
|
return elliptic.Marshal(pub.Curve, pub.X, pub.Y)[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:30:34 +02:00
|
|
|
var (
|
2015-02-27 04:09:53 +02:00
|
|
|
// this is used in place of actual frame header data.
|
|
|
|
// TODO: replace this when Msg contains the protocol type code.
|
2015-02-27 00:30:34 +02:00
|
|
|
zeroHeader = []byte{0xC2, 0x80, 0x80}
|
2015-02-27 04:09:53 +02:00
|
|
|
// sixteen zero bytes
|
|
|
|
zero16 = make([]byte, 16)
|
2015-02-27 00:30:34 +02:00
|
|
|
)
|
|
|
|
|
2015-03-04 17:27:37 +02:00
|
|
|
// rlpxFrameRW implements a simplified version of RLPx framing.
|
|
|
|
// chunked messages are not supported and all headers are equal to
|
|
|
|
// zeroHeader.
|
|
|
|
//
|
|
|
|
// rlpxFrameRW is not safe for concurrent use from multiple goroutines.
|
2015-02-27 00:30:34 +02:00
|
|
|
type rlpxFrameRW struct {
|
|
|
|
conn io.ReadWriter
|
2015-02-27 04:09:53 +02:00
|
|
|
enc cipher.Stream
|
|
|
|
dec cipher.Stream
|
2015-02-27 00:30:34 +02:00
|
|
|
|
|
|
|
macCipher cipher.Block
|
|
|
|
egressMAC hash.Hash
|
|
|
|
ingressMAC hash.Hash
|
2017-09-26 16:54:49 +03:00
|
|
|
|
|
|
|
snappy bool
|
2015-02-27 00:30:34 +02:00
|
|
|
}
|
|
|
|
|
2015-05-16 01:38:28 +03:00
|
|
|
func newRLPXFrameRW(conn io.ReadWriter, s secrets) *rlpxFrameRW {
|
2015-02-27 04:09:53 +02:00
|
|
|
macc, err := aes.NewCipher(s.MAC)
|
|
|
|
if err != nil {
|
|
|
|
panic("invalid MAC secret: " + err.Error())
|
|
|
|
}
|
|
|
|
encc, err := aes.NewCipher(s.AES)
|
2015-02-27 00:30:34 +02:00
|
|
|
if err != nil {
|
2015-02-27 04:09:53 +02:00
|
|
|
panic("invalid AES secret: " + err.Error())
|
|
|
|
}
|
|
|
|
// we use an all-zeroes IV for AES because the key used
|
|
|
|
// for encryption is ephemeral.
|
|
|
|
iv := make([]byte, encc.BlockSize())
|
|
|
|
return &rlpxFrameRW{
|
|
|
|
conn: conn,
|
|
|
|
enc: cipher.NewCTR(encc, iv),
|
|
|
|
dec: cipher.NewCTR(encc, iv),
|
|
|
|
macCipher: macc,
|
|
|
|
egressMAC: s.EgressMAC,
|
|
|
|
ingressMAC: s.IngressMAC,
|
2015-02-27 00:30:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
|
|
|
|
ptype, _ := rlp.EncodeToBytes(msg.Code)
|
|
|
|
|
2017-09-26 16:54:49 +03:00
|
|
|
// if snappy is enabled, compress message now
|
|
|
|
if rw.snappy {
|
|
|
|
if msg.Size > maxUint24 {
|
|
|
|
return errPlainMessageTooLarge
|
|
|
|
}
|
|
|
|
payload, _ := ioutil.ReadAll(msg.Payload)
|
|
|
|
payload = snappy.Encode(nil, payload)
|
|
|
|
|
|
|
|
msg.Payload = bytes.NewReader(payload)
|
|
|
|
msg.Size = uint32(len(payload))
|
|
|
|
}
|
2015-02-27 00:30:34 +02:00
|
|
|
// write header
|
|
|
|
headbuf := make([]byte, 32)
|
|
|
|
fsize := uint32(len(ptype)) + msg.Size
|
2015-03-04 17:39:04 +02:00
|
|
|
if fsize > maxUint24 {
|
|
|
|
return errors.New("message size overflows uint24")
|
|
|
|
}
|
2015-02-27 00:30:34 +02:00
|
|
|
putInt24(fsize, headbuf) // TODO: check overflow
|
|
|
|
copy(headbuf[3:], zeroHeader)
|
2015-02-27 04:09:53 +02:00
|
|
|
rw.enc.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now encrypted
|
2015-02-27 15:08:57 +02:00
|
|
|
|
|
|
|
// write header MAC
|
|
|
|
copy(headbuf[16:], updateMAC(rw.egressMAC, rw.macCipher, headbuf[:16]))
|
2015-02-27 00:30:34 +02:00
|
|
|
if _, err := rw.conn.Write(headbuf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:08:57 +02:00
|
|
|
// write encrypted frame, updating the egress MAC hash with
|
|
|
|
// the data written to conn.
|
2015-02-27 04:09:53 +02:00
|
|
|
tee := cipher.StreamWriter{S: rw.enc, W: io.MultiWriter(rw.conn, rw.egressMAC)}
|
2015-02-27 00:30:34 +02:00
|
|
|
if _, err := tee.Write(ptype); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tee, msg.Payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if padding := fsize % 16; padding > 0 {
|
|
|
|
if _, err := tee.Write(zero16[:16-padding]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:08:57 +02:00
|
|
|
// write frame MAC. egress MAC hash is up to date because
|
2015-02-27 00:30:34 +02:00
|
|
|
// frame content was written to it as well.
|
2015-02-27 15:08:57 +02:00
|
|
|
fmacseed := rw.egressMAC.Sum(nil)
|
|
|
|
mac := updateMAC(rw.egressMAC, rw.macCipher, fmacseed)
|
2015-02-27 04:09:53 +02:00
|
|
|
_, err := rw.conn.Write(mac)
|
2015-02-27 00:30:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
|
|
|
|
// read the header
|
|
|
|
headbuf := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rw.conn, headbuf); err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
2015-02-27 04:09:53 +02:00
|
|
|
// verify header mac
|
2015-02-27 15:08:57 +02:00
|
|
|
shouldMAC := updateMAC(rw.ingressMAC, rw.macCipher, headbuf[:16])
|
|
|
|
if !hmac.Equal(shouldMAC, headbuf[16:]) {
|
2015-02-27 00:30:34 +02:00
|
|
|
return msg, errors.New("bad header MAC")
|
|
|
|
}
|
2015-02-27 04:09:53 +02:00
|
|
|
rw.dec.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now decrypted
|
|
|
|
fsize := readInt24(headbuf)
|
|
|
|
// ignore protocol type for now
|
2015-02-27 00:30:34 +02:00
|
|
|
|
|
|
|
// read the frame content
|
2015-02-27 04:09:53 +02:00
|
|
|
var rsize = fsize // frame size rounded up to 16 byte boundary
|
|
|
|
if padding := fsize % 16; padding > 0 {
|
|
|
|
rsize += 16 - padding
|
|
|
|
}
|
|
|
|
framebuf := make([]byte, rsize)
|
2015-02-27 00:30:34 +02:00
|
|
|
if _, err := io.ReadFull(rw.conn, framebuf); err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
2015-02-27 04:09:53 +02:00
|
|
|
|
2015-02-27 00:30:34 +02:00
|
|
|
// read and validate frame MAC. we can re-use headbuf for that.
|
2015-02-27 04:09:53 +02:00
|
|
|
rw.ingressMAC.Write(framebuf)
|
2015-02-27 15:08:57 +02:00
|
|
|
fmacseed := rw.ingressMAC.Sum(nil)
|
|
|
|
if _, err := io.ReadFull(rw.conn, headbuf[:16]); err != nil {
|
2015-02-27 00:30:34 +02:00
|
|
|
return msg, err
|
|
|
|
}
|
2015-02-27 15:08:57 +02:00
|
|
|
shouldMAC = updateMAC(rw.ingressMAC, rw.macCipher, fmacseed)
|
|
|
|
if !hmac.Equal(shouldMAC, headbuf[:16]) {
|
2015-02-27 00:30:34 +02:00
|
|
|
return msg, errors.New("bad frame MAC")
|
|
|
|
}
|
|
|
|
|
2015-02-27 04:09:53 +02:00
|
|
|
// decrypt frame content
|
|
|
|
rw.dec.XORKeyStream(framebuf, framebuf)
|
|
|
|
|
2015-02-27 00:30:34 +02:00
|
|
|
// decode message code
|
2015-02-27 04:09:53 +02:00
|
|
|
content := bytes.NewReader(framebuf[:fsize])
|
2015-02-27 00:30:34 +02:00
|
|
|
if err := rlp.Decode(content, &msg.Code); err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
msg.Size = uint32(content.Len())
|
|
|
|
msg.Payload = content
|
2017-09-26 16:54:49 +03:00
|
|
|
|
|
|
|
// if snappy is enabled, verify and decompress message
|
|
|
|
if rw.snappy {
|
|
|
|
payload, err := ioutil.ReadAll(msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
size, err := snappy.DecodedLen(payload)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
if size > int(maxUint24) {
|
|
|
|
return msg, errPlainMessageTooLarge
|
|
|
|
}
|
|
|
|
payload, err = snappy.Decode(nil, payload)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
msg.Size, msg.Payload = uint32(size), bytes.NewReader(payload)
|
|
|
|
}
|
2015-02-27 00:30:34 +02:00
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:08:57 +02:00
|
|
|
// updateMAC reseeds the given hash with encrypted seed.
|
|
|
|
// it returns the first 16 bytes of the hash sum after seeding.
|
|
|
|
func updateMAC(mac hash.Hash, block cipher.Block, seed []byte) []byte {
|
2015-02-27 00:30:34 +02:00
|
|
|
aesbuf := make([]byte, aes.BlockSize)
|
|
|
|
block.Encrypt(aesbuf, mac.Sum(nil))
|
|
|
|
for i := range aesbuf {
|
2015-02-27 15:08:57 +02:00
|
|
|
aesbuf[i] ^= seed[i]
|
2015-02-27 00:30:34 +02:00
|
|
|
}
|
|
|
|
mac.Write(aesbuf)
|
2015-02-27 15:08:57 +02:00
|
|
|
return mac.Sum(nil)[:16]
|
2015-02-27 00:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func readInt24(b []byte) uint32 {
|
|
|
|
return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
|
|
|
|
}
|
|
|
|
|
|
|
|
func putInt24(v uint32, b []byte) {
|
|
|
|
b[0] = byte(v >> 16)
|
|
|
|
b[1] = byte(v >> 8)
|
|
|
|
b[2] = byte(v)
|
|
|
|
}
|