2015-01-18 03:46:08 -06:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
2015-02-18 18:52:03 -06:00
|
|
|
"errors"
|
2015-01-18 03:46:08 -06:00
|
|
|
"fmt"
|
2015-02-26 20:09:53 -06:00
|
|
|
"hash"
|
2015-01-19 17:42:13 -06:00
|
|
|
"io"
|
2015-02-18 18:52:03 -06:00
|
|
|
"net"
|
2015-01-18 03:46:08 -06:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-02-13 17:25:47 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
2015-01-26 08:50:12 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2015-02-26 20:09:53 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
2015-02-04 20:07:58 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2015-02-18 18:52:03 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2015-01-18 03:46:08 -06:00
|
|
|
)
|
|
|
|
|
2015-01-26 08:50:12 -06:00
|
|
|
const (
|
2015-02-04 20:07:58 -06:00
|
|
|
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)
|
2015-01-18 03:46:08 -06:00
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
authMsgLen = sigLen + shaLen + pubLen + shaLen + 1
|
|
|
|
authRespLen = pubLen + shaLen + 1
|
|
|
|
|
|
|
|
eciesBytes = 65 + 16 + 32
|
|
|
|
iHSLen = authMsgLen + eciesBytes // size of the final ECIES payload sent as initiator's handshake
|
|
|
|
rHSLen = authRespLen + eciesBytes // size of the final ECIES payload sent as receiver's handshake
|
|
|
|
)
|
2015-01-18 03:46:08 -06:00
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
type conn struct {
|
|
|
|
*frameRW
|
|
|
|
*protoHandshake
|
|
|
|
}
|
2015-01-21 08:45:53 -06:00
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
func newConn(fd net.Conn, hs *protoHandshake) *conn {
|
|
|
|
return &conn{newFrameRW(fd, msgWriteTimeout), hs}
|
2015-01-21 08:45:53 -06:00
|
|
|
}
|
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
// encHandshake contains the state of the encryption handshake.
|
2015-02-18 18:52:03 -06:00
|
|
|
type encHandshake struct {
|
2015-02-26 20:09:53 -06:00
|
|
|
remoteID discover.NodeID
|
|
|
|
initiator bool
|
|
|
|
initNonce, respNonce []byte
|
|
|
|
dhSharedSecret []byte
|
|
|
|
randomPrivKey *ecdsa.PrivateKey
|
|
|
|
remoteRandomPub *ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// secrets represents the connection secrets
|
|
|
|
// which are negotiated during the encryption handshake.
|
|
|
|
type secrets struct {
|
|
|
|
RemoteID discover.NodeID
|
|
|
|
AES, MAC []byte
|
|
|
|
EgressMAC, IngressMAC hash.Hash
|
|
|
|
Token []byte
|
2015-02-18 18:52:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// protoHandshake is the RLP structure of the protocol handshake.
|
|
|
|
type protoHandshake struct {
|
|
|
|
Version uint64
|
|
|
|
Name string
|
|
|
|
Caps []Cap
|
|
|
|
ListenPort uint64
|
|
|
|
ID discover.NodeID
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:09:53 -06: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 {
|
|
|
|
sharedSecret := crypto.Sha3(h.dhSharedSecret, crypto.Sha3(h.respNonce, h.initNonce))
|
|
|
|
aesSecret := crypto.Sha3(h.dhSharedSecret, sharedSecret)
|
|
|
|
s := secrets{
|
|
|
|
RemoteID: h.remoteID,
|
|
|
|
AES: aesSecret,
|
|
|
|
MAC: crypto.Sha3(h.dhSharedSecret, aesSecret),
|
|
|
|
Token: crypto.Sha3(sharedSecret),
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup sha3 instances for the MACs
|
|
|
|
mac1 := sha3.NewKeccak256()
|
|
|
|
mac1.Write(xor(s.MAC, h.respNonce))
|
|
|
|
mac1.Write(auth)
|
|
|
|
mac2 := sha3.NewKeccak256()
|
|
|
|
mac2.Write(xor(s.MAC, h.initNonce))
|
|
|
|
mac2.Write(authResp)
|
|
|
|
if h.initiator {
|
|
|
|
s.EgressMAC, s.IngressMAC = mac1, mac2
|
|
|
|
} else {
|
|
|
|
s.EgressMAC, s.IngressMAC = mac2, mac1
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
// setupConn starts a protocol session on the given connection.
|
|
|
|
// It runs the encryption handshake and the protocol handshake.
|
|
|
|
// If dial is non-nil, the connection the local node is the initiator.
|
|
|
|
func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node) (*conn, error) {
|
2015-02-04 20:07:58 -06:00
|
|
|
if dial == nil {
|
2015-02-18 18:52:03 -06:00
|
|
|
return setupInboundConn(fd, prv, our)
|
2015-02-04 20:07:58 -06:00
|
|
|
} else {
|
2015-02-18 18:52:03 -06:00
|
|
|
return setupOutboundConn(fd, prv, our, dial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake) (*conn, error) {
|
2015-02-26 20:09:53 -06:00
|
|
|
secrets, err := inboundEncHandshake(fd, prv, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("encryption handshake failed: %v", err)
|
|
|
|
}
|
2015-02-18 18:52:03 -06:00
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
// Run the protocol handshake using authenticated messages.
|
|
|
|
// TODO: move buffering setup here (out of newFrameRW)
|
|
|
|
phsrw := newRlpxFrameRW(fd, secrets)
|
|
|
|
rhs, err := readProtocolHandshake(phsrw, our)
|
2015-02-18 18:52:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
if err := writeProtocolHandshake(phsrw, our); err != nil {
|
2015-02-18 18:52:03 -06:00
|
|
|
return nil, fmt.Errorf("protocol write error: %v", err)
|
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
|
|
|
|
rw := newFrameRW(fd, msgWriteTimeout)
|
2015-02-18 18:52:03 -06:00
|
|
|
return &conn{rw, rhs}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node) (*conn, error) {
|
2015-02-26 20:09:53 -06:00
|
|
|
secrets, err := outboundEncHandshake(fd, prv, dial.ID[:], nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("encryption handshake failed: %v", err)
|
|
|
|
}
|
2015-02-18 18:52:03 -06:00
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
// Run the protocol handshake using authenticated messages.
|
|
|
|
// TODO: move buffering setup here (out of newFrameRW)
|
|
|
|
phsrw := newRlpxFrameRW(fd, secrets)
|
|
|
|
if err := writeProtocolHandshake(phsrw, our); err != nil {
|
2015-02-18 18:52:03 -06:00
|
|
|
return nil, fmt.Errorf("protocol write error: %v", err)
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
rhs, err := readProtocolHandshake(phsrw, our)
|
2015-02-18 18:52:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("protocol handshake read error: %v", err)
|
|
|
|
}
|
|
|
|
if rhs.ID != dial.ID {
|
|
|
|
return nil, errors.New("dialed node id mismatch")
|
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
|
|
|
|
rw := newFrameRW(fd, msgWriteTimeout)
|
2015-02-18 18:52:03 -06:00
|
|
|
return &conn{rw, rhs}, nil
|
2015-01-28 21:16:10 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// outboundEncHandshake negotiates a session token on conn.
|
|
|
|
// it should be called on the dialing side of the connection.
|
|
|
|
//
|
|
|
|
// privateKey is the local client's private key
|
|
|
|
// remotePublicKey is the remote peer's node ID
|
|
|
|
// sessionToken is the token from a previous session with this node.
|
2015-02-26 20:09:53 -06:00
|
|
|
func outboundEncHandshake(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, remotePublicKey []byte, sessionToken []byte) (s secrets, err error) {
|
2015-02-04 20:07:58 -06:00
|
|
|
auth, initNonce, randomPrivKey, err := authMsg(prvKey, remotePublicKey, sessionToken)
|
2015-01-28 21:16:10 -06:00
|
|
|
if err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-01-28 21:16:10 -06:00
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
if _, err = conn.Write(auth); err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-01-20 10:47:46 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
response := make([]byte, rHSLen)
|
|
|
|
if _, err = io.ReadFull(conn, response); err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
|
|
|
recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prvKey)
|
|
|
|
if err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-01-20 10:47:46 -06:00
|
|
|
}
|
2015-01-21 04:22:07 -06:00
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
h := &encHandshake{
|
|
|
|
initiator: true,
|
|
|
|
initNonce: initNonce,
|
|
|
|
respNonce: recNonce,
|
|
|
|
randomPrivKey: randomPrivKey,
|
|
|
|
remoteRandomPub: remoteRandomPubKey,
|
|
|
|
}
|
|
|
|
copy(h.remoteID[:], remotePublicKey)
|
|
|
|
return h.secrets(auth, response), nil
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
2015-01-21 04:22:07 -06:00
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// authMsg creates the initiator handshake.
|
2015-02-26 20:09:53 -06:00
|
|
|
// TODO: change all the names
|
2015-02-04 20:07:58 -06:00
|
|
|
func authMsg(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (
|
|
|
|
auth, initNonce []byte,
|
|
|
|
randomPrvKey *ecdsa.PrivateKey,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
remotePubKey, err := importPublicKey(remotePubKeyS)
|
|
|
|
if err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 17:53:45 -06:00
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
var tokenFlag byte
|
2015-01-18 03:46:08 -06:00
|
|
|
if sessionToken == nil {
|
2015-01-18 17:53:45 -06:00
|
|
|
// no session token found means we need to generate shared secret.
|
|
|
|
// ecies shared secret is used as initial session token for new peers
|
|
|
|
// generate shared key from prv and remote pubkey
|
2015-01-26 10:16:23 -06:00
|
|
|
if sessionToken, err = ecies.ImportECDSA(prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
|
2015-01-18 17:53:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// for known peers, we use stored token from the previous session
|
|
|
|
tokenFlag = 0x01
|
2015-01-18 03:46:08 -06:00
|
|
|
}
|
2015-01-18 17:53:45 -06:00
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
//E(remote-pubk, S(ecdhe-random, sha3(ecdh-shared-secret^nonce)) || H(ecdhe-random-pubk) || pubk || nonce || 0x0)
|
|
|
|
// E(remote-pubk, S(ecdhe-random, sha3(token^nonce)) || H(ecdhe-random-pubk) || pubk || nonce || 0x1)
|
2015-01-18 17:53:45 -06:00
|
|
|
// allocate msgLen long message,
|
2015-02-04 20:07:58 -06:00
|
|
|
var msg []byte = make([]byte, authMsgLen)
|
|
|
|
initNonce = msg[authMsgLen-shaLen-1 : authMsgLen-1]
|
|
|
|
if _, err = rand.Read(initNonce); err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// create known message
|
2015-01-18 17:53:45 -06:00
|
|
|
// ecdh-shared-secret^nonce for new peers
|
|
|
|
// token^nonce for old peers
|
2015-02-04 20:07:58 -06:00
|
|
|
var sharedSecret = xor(sessionToken, initNonce)
|
2015-01-18 03:46:08 -06:00
|
|
|
|
|
|
|
// generate random keypair to use for signing
|
2015-02-04 20:07:58 -06:00
|
|
|
if randomPrvKey, err = crypto.GenerateKey(); err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 17:53:45 -06:00
|
|
|
// sign shared secret (message known to both parties): shared-secret
|
2015-01-18 03:46:08 -06:00
|
|
|
var signature []byte
|
2015-01-18 17:53:45 -06:00
|
|
|
// signature = sign(ecdhe-random, shared-secret)
|
2015-01-18 03:46:08 -06:00
|
|
|
// uses secp256k1.Sign
|
2015-01-18 22:53:48 -06:00
|
|
|
if signature, err = crypto.Sign(sharedSecret, randomPrvKey); err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-18 17:53:45 -06:00
|
|
|
// message
|
|
|
|
// signed-shared-secret || H(ecdhe-random-pubk) || pubk || nonce || 0x0
|
|
|
|
copy(msg, signature) // copy signed-shared-secret
|
|
|
|
// H(ecdhe-random-pubk)
|
2015-01-20 10:47:46 -06:00
|
|
|
var randomPubKey64 []byte
|
2015-02-04 20:07:58 -06:00
|
|
|
if randomPubKey64, err = exportPublicKey(&randomPrvKey.PublicKey); err != nil {
|
2015-01-20 10:47:46 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-26 10:16:23 -06:00
|
|
|
var pubKey64 []byte
|
2015-02-04 20:07:58 -06:00
|
|
|
if pubKey64, err = exportPublicKey(&prvKey.PublicKey); err != nil {
|
2015-01-26 10:16:23 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-26 08:50:12 -06:00
|
|
|
copy(msg[sigLen:sigLen+shaLen], crypto.Sha3(randomPubKey64))
|
2015-01-18 17:53:45 -06:00
|
|
|
// pubkey copied to the correct segment.
|
2015-01-26 10:16:23 -06:00
|
|
|
copy(msg[sigLen+shaLen:sigLen+shaLen+pubLen], pubKey64)
|
2015-01-18 17:53:45 -06:00
|
|
|
// nonce is already in the slice
|
|
|
|
// stick tokenFlag byte to the end
|
2015-02-04 20:07:58 -06:00
|
|
|
msg[authMsgLen-1] = tokenFlag
|
2015-01-18 17:53:45 -06:00
|
|
|
|
|
|
|
// encrypt using remote-pubk
|
2015-01-18 03:46:08 -06:00
|
|
|
// auth = eciesEncrypt(remote-pubk, msg)
|
|
|
|
if auth, err = crypto.Encrypt(remotePubKey, msg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// completeHandshake is called when the initiator receives an
|
|
|
|
// authentication response (aka receiver handshake). It completes the
|
|
|
|
// handshake by reading off parameters the remote peer provides needed
|
|
|
|
// to set up the secure session.
|
|
|
|
func completeHandshake(auth []byte, prvKey *ecdsa.PrivateKey) (
|
|
|
|
respNonce []byte,
|
|
|
|
remoteRandomPubKey *ecdsa.PublicKey,
|
|
|
|
tokenFlag bool,
|
|
|
|
err error,
|
|
|
|
) {
|
2015-01-18 03:46:08 -06:00
|
|
|
var msg []byte
|
2015-02-04 20:07:58 -06:00
|
|
|
// they prove that msg is meant for me,
|
|
|
|
// I prove I possess private key if i can read it
|
|
|
|
if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
|
2015-01-19 17:42:13 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
respNonce = msg[pubLen : pubLen+shaLen]
|
|
|
|
var remoteRandomPubKeyS = msg[:pubLen]
|
|
|
|
if remoteRandomPubKey, err = importPublicKey(remoteRandomPubKeyS); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if msg[authRespLen-1] == 0x01 {
|
|
|
|
tokenFlag = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// inboundEncHandshake negotiates a session token on conn.
|
|
|
|
// it should be called on the listening side of the connection.
|
|
|
|
//
|
|
|
|
// privateKey is the local client's private key
|
|
|
|
// sessionToken is the token from a previous session with this node.
|
2015-02-26 20:09:53 -06:00
|
|
|
func inboundEncHandshake(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, sessionToken []byte) (s secrets, err error) {
|
2015-02-04 20:07:58 -06:00
|
|
|
// we are listening connection. we are responders in the
|
|
|
|
// handshake. Extract info from the authentication. The initiator
|
|
|
|
// starts by sending us a handshake that we need to respond to. so
|
|
|
|
// we read auth message first, then respond.
|
|
|
|
auth := make([]byte, iHSLen)
|
|
|
|
if _, err := io.ReadFull(conn, auth); err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
|
|
|
response, recNonce, initNonce, remotePubKey, randomPrivKey, remoteRandomPubKey, err := authResp(auth, sessionToken, prvKey)
|
|
|
|
if err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
|
|
|
if _, err = conn.Write(response); err != nil {
|
2015-02-26 20:09:53 -06:00
|
|
|
return s, err
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
|
|
|
|
h := &encHandshake{
|
|
|
|
initiator: false,
|
|
|
|
initNonce: initNonce,
|
|
|
|
respNonce: recNonce,
|
|
|
|
randomPrivKey: randomPrivKey,
|
|
|
|
remoteRandomPub: remoteRandomPubKey,
|
|
|
|
}
|
|
|
|
copy(h.remoteID[:], remotePubKey)
|
|
|
|
return h.secrets(auth, response), err
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// authResp is called by peer if it accepted (but not
|
|
|
|
// initiated) the connection from the remote. It is passed the initiator
|
|
|
|
// handshake received and the session token belonging to the
|
|
|
|
// remote initiator.
|
|
|
|
//
|
|
|
|
// The first return value is the authentication response (aka receiver
|
|
|
|
// handshake) that is to be sent to the remote initiator.
|
|
|
|
func authResp(auth, sessionToken []byte, prvKey *ecdsa.PrivateKey) (
|
|
|
|
authResp, respNonce, initNonce, remotePubKeyS []byte,
|
|
|
|
randomPrivKey *ecdsa.PrivateKey,
|
|
|
|
remoteRandomPubKey *ecdsa.PublicKey,
|
|
|
|
err error,
|
|
|
|
) {
|
2015-01-18 03:46:08 -06:00
|
|
|
// they prove that msg is meant for me,
|
|
|
|
// I prove I possess private key if i can read it
|
2015-02-04 20:07:58 -06:00
|
|
|
msg, err := crypto.Decrypt(prvKey, auth)
|
|
|
|
if err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
remotePubKeyS = msg[sigLen+shaLen : sigLen+shaLen+pubLen]
|
|
|
|
remotePubKey, _ := importPublicKey(remotePubKeyS)
|
|
|
|
|
2015-01-18 18:55:24 -06:00
|
|
|
var tokenFlag byte
|
|
|
|
if sessionToken == nil {
|
|
|
|
// no session token found means we need to generate shared secret.
|
|
|
|
// ecies shared secret is used as initial session token for new peers
|
|
|
|
// generate shared key from prv and remote pubkey
|
2015-01-26 10:16:23 -06:00
|
|
|
if sessionToken, err = ecies.ImportECDSA(prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
|
2015-01-18 18:55:24 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// tokenFlag = 0x00 // redundant
|
|
|
|
} else {
|
|
|
|
// for known peers, we use stored token from the previous session
|
|
|
|
tokenFlag = 0x01
|
|
|
|
}
|
|
|
|
|
|
|
|
// the initiator nonce is read off the end of the message
|
2015-02-04 20:07:58 -06:00
|
|
|
initNonce = msg[authMsgLen-shaLen-1 : authMsgLen-1]
|
|
|
|
// I prove that i own prv key (to derive shared secret, and read
|
|
|
|
// nonce off encrypted msg) and that I own shared secret they
|
|
|
|
// prove they own the private key belonging to ecdhe-random-pubk
|
|
|
|
// we can now reconstruct the signed message and recover the peers
|
|
|
|
// pubkey
|
|
|
|
var signedMsg = xor(sessionToken, initNonce)
|
2015-01-20 10:47:46 -06:00
|
|
|
var remoteRandomPubKeyS []byte
|
|
|
|
if remoteRandomPubKeyS, err = secp256k1.RecoverPubkey(signedMsg, msg[:sigLen]); err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 18:55:24 -06:00
|
|
|
// convert to ECDSA standard
|
2015-02-04 20:07:58 -06:00
|
|
|
if remoteRandomPubKey, err = importPublicKey(remoteRandomPubKeyS); err != nil {
|
2015-01-18 03:46:08 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 17:53:45 -06:00
|
|
|
|
2015-01-18 18:55:24 -06:00
|
|
|
// now we find ourselves a long task too, fill it random
|
2015-02-04 20:07:58 -06:00
|
|
|
var resp = make([]byte, authRespLen)
|
2015-01-26 08:50:12 -06:00
|
|
|
// generate shaLen long nonce
|
|
|
|
respNonce = resp[pubLen : pubLen+shaLen]
|
2015-02-04 20:07:58 -06:00
|
|
|
if _, err = rand.Read(respNonce); err != nil {
|
2015-01-18 17:53:45 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 18:55:24 -06:00
|
|
|
// generate random keypair for session
|
2015-02-04 20:07:58 -06:00
|
|
|
if randomPrivKey, err = crypto.GenerateKey(); err != nil {
|
2015-01-18 17:53:45 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 18:55:24 -06:00
|
|
|
// responder auth message
|
2015-01-18 17:53:45 -06:00
|
|
|
// E(remote-pubk, ecdhe-random-pubk || nonce || 0x0)
|
2015-01-20 10:47:46 -06:00
|
|
|
var randomPubKeyS []byte
|
2015-02-04 20:07:58 -06:00
|
|
|
if randomPubKeyS, err = exportPublicKey(&randomPrivKey.PublicKey); err != nil {
|
2015-01-20 10:47:46 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
copy(resp[:pubLen], randomPubKeyS)
|
2015-01-18 17:53:45 -06:00
|
|
|
// nonce is already in the slice
|
2015-02-04 20:07:58 -06:00
|
|
|
resp[authRespLen-1] = tokenFlag
|
2015-01-18 17:53:45 -06:00
|
|
|
|
|
|
|
// encrypt using remote-pubk
|
|
|
|
// auth = eciesEncrypt(remote-pubk, msg)
|
|
|
|
// why not encrypt with ecdhe-random-remote
|
|
|
|
if authResp, err = crypto.Encrypt(remotePubKey, resp); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// importPublicKey unmarshals 512 bit public keys.
|
|
|
|
func importPublicKey(pubKey []byte) (pubKeyEC *ecdsa.PublicKey, err error) {
|
|
|
|
var pubKey65 []byte
|
|
|
|
switch len(pubKey) {
|
|
|
|
case 64:
|
|
|
|
// add 'uncompressed key' flag
|
|
|
|
pubKey65 = append([]byte{0x04}, pubKey...)
|
|
|
|
case 65:
|
|
|
|
pubKey65 = pubKey
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid public key length %v (expect 64/65)", len(pubKey))
|
2015-01-18 17:53:45 -06:00
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
return crypto.ToECDSAPub(pubKey65), nil
|
2015-01-18 17:53:45 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
func exportPublicKey(pubKeyEC *ecdsa.PublicKey) (pubKey []byte, err error) {
|
|
|
|
if pubKeyEC == nil {
|
|
|
|
return nil, fmt.Errorf("no ECDSA public key given")
|
2015-01-18 03:46:08 -06:00
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
return crypto.FromECDSAPub(pubKeyEC)[1:], nil
|
2015-01-18 03:46:08 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
func xor(one, other []byte) (xor []byte) {
|
2015-01-18 17:53:45 -06:00
|
|
|
xor = make([]byte, len(one))
|
2015-01-18 03:46:08 -06:00
|
|
|
for i := 0; i < len(one); i++ {
|
|
|
|
xor[i] = one[i] ^ other[i]
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
return xor
|
2015-01-18 03:46:08 -06:00
|
|
|
}
|
2015-02-18 18:52:03 -06:00
|
|
|
|
|
|
|
func writeProtocolHandshake(w MsgWriter, our *protoHandshake) error {
|
|
|
|
return EncodeMsg(w, handshakeMsg, our.Version, our.Name, our.Caps, our.ListenPort, our.ID[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func readProtocolHandshake(r MsgReader, our *protoHandshake) (*protoHandshake, error) {
|
|
|
|
// read and handle remote handshake
|
|
|
|
msg, err := r.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if msg.Code == discMsg {
|
|
|
|
// disconnect before protocol handshake is valid according to the
|
|
|
|
// spec and we send it ourself if Server.addPeer fails.
|
|
|
|
var reason DiscReason
|
|
|
|
rlp.Decode(msg.Payload, &reason)
|
|
|
|
return nil, discRequestedError(reason)
|
|
|
|
}
|
|
|
|
if msg.Code != handshakeMsg {
|
|
|
|
return nil, fmt.Errorf("expected handshake, got %x", msg.Code)
|
|
|
|
}
|
|
|
|
if msg.Size > baseProtocolMaxMsgSize {
|
|
|
|
return nil, fmt.Errorf("message too big (%d > %d)", msg.Size, baseProtocolMaxMsgSize)
|
|
|
|
}
|
|
|
|
var hs protoHandshake
|
|
|
|
if err := msg.Decode(&hs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// validate handshake info
|
|
|
|
if hs.Version != our.Version {
|
|
|
|
return nil, newPeerError(errP2PVersionMismatch, "required version %d, received %d\n", baseProtocolVersion, hs.Version)
|
|
|
|
}
|
|
|
|
if (hs.ID == discover.NodeID{}) {
|
|
|
|
return nil, newPeerError(errPubkeyInvalid, "missing")
|
|
|
|
}
|
|
|
|
return &hs, nil
|
|
|
|
}
|