mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
670 lines
19 KiB
670 lines
19 KiB
10 years ago
|
// Copyright 2015 The go-ethereum Authors
|
||
9 years ago
|
// This file is part of the go-ethereum library.
|
||
10 years ago
|
//
|
||
9 years ago
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
10 years ago
|
// 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.
|
||
|
//
|
||
9 years ago
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
10 years ago
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
9 years ago
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
9 years ago
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
10 years ago
|
|
||
4 years ago
|
// Package rlpx implements the RLPx transport protocol.
|
||
|
package rlpx
|
||
10 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/aes"
|
||
|
"crypto/cipher"
|
||
10 years ago
|
"crypto/ecdsa"
|
||
|
"crypto/elliptic"
|
||
10 years ago
|
"crypto/hmac"
|
||
10 years ago
|
"crypto/rand"
|
||
9 years ago
|
"encoding/binary"
|
||
10 years ago
|
"errors"
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"hash"
|
||
|
"io"
|
||
9 years ago
|
mrand "math/rand"
|
||
10 years ago
|
"net"
|
||
|
"time"
|
||
10 years ago
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/crypto"
|
||
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/rlp"
|
||
7 years ago
|
"github.com/golang/snappy"
|
||
6 years ago
|
"golang.org/x/crypto/sha3"
|
||
10 years ago
|
)
|
||
|
|
||
4 years ago
|
// Conn is an RLPx network connection. It wraps a low-level network connection. The
|
||
|
// underlying connection should not be used for other activity when it is wrapped by Conn.
|
||
|
//
|
||
|
// Before sending messages, a handshake must be performed by calling the Handshake method.
|
||
|
// This type is not generally safe for concurrent use, but reading and writing of messages
|
||
|
// may happen concurrently after the handshake.
|
||
|
type Conn struct {
|
||
|
dialDest *ecdsa.PublicKey
|
||
|
conn net.Conn
|
||
|
handshake *handshakeState
|
||
|
snappy bool
|
||
|
}
|
||
10 years ago
|
|
||
4 years ago
|
type handshakeState struct {
|
||
|
enc cipher.Stream
|
||
|
dec cipher.Stream
|
||
10 years ago
|
|
||
4 years ago
|
macCipher cipher.Block
|
||
|
egressMAC hash.Hash
|
||
|
ingressMAC hash.Hash
|
||
|
}
|
||
7 years ago
|
|
||
4 years ago
|
// NewConn wraps the given network connection. If dialDest is non-nil, the connection
|
||
|
// behaves as the initiator during the handshake.
|
||
|
func NewConn(conn net.Conn, dialDest *ecdsa.PublicKey) *Conn {
|
||
|
return &Conn{
|
||
|
dialDest: dialDest,
|
||
|
conn: conn,
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
4 years ago
|
// SetSnappy enables or disables snappy compression of messages. This is usually called
|
||
|
// after the devp2p Hello message exchange when the negotiated version indicates that
|
||
|
// compression is available on both ends of the connection.
|
||
|
func (c *Conn) SetSnappy(snappy bool) {
|
||
|
c.snappy = snappy
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// SetReadDeadline sets the deadline for all future read operations.
|
||
|
func (c *Conn) SetReadDeadline(time time.Time) error {
|
||
|
return c.conn.SetReadDeadline(time)
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// SetWriteDeadline sets the deadline for all future write operations.
|
||
|
func (c *Conn) SetWriteDeadline(time time.Time) error {
|
||
|
return c.conn.SetWriteDeadline(time)
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// SetDeadline sets the deadline for all future read and write operations.
|
||
|
func (c *Conn) SetDeadline(time time.Time) error {
|
||
|
return c.conn.SetDeadline(time)
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// Read reads a message from the connection.
|
||
|
func (c *Conn) Read() (code uint64, data []byte, wireSize int, err error) {
|
||
|
if c.handshake == nil {
|
||
|
panic("can't ReadMsg before handshake")
|
||
|
}
|
||
|
|
||
|
frame, err := c.handshake.readFrame(c.conn)
|
||
|
if err != nil {
|
||
|
return 0, nil, 0, err
|
||
|
}
|
||
|
code, data, err = rlp.SplitUint64(frame)
|
||
|
if err != nil {
|
||
|
return 0, nil, 0, fmt.Errorf("invalid message code: %v", err)
|
||
|
}
|
||
|
wireSize = len(data)
|
||
|
|
||
|
// If snappy is enabled, verify and decompress message.
|
||
|
if c.snappy {
|
||
|
var actualSize int
|
||
|
actualSize, err = snappy.DecodedLen(data)
|
||
|
if err != nil {
|
||
|
return code, nil, 0, err
|
||
|
}
|
||
|
if actualSize > maxUint24 {
|
||
|
return code, nil, 0, errPlainMessageTooLarge
|
||
10 years ago
|
}
|
||
4 years ago
|
data, err = snappy.Decode(nil, data)
|
||
10 years ago
|
}
|
||
4 years ago
|
return code, data, wireSize, err
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
func (h *handshakeState) readFrame(conn io.Reader) ([]byte, error) {
|
||
|
// read the header
|
||
|
headbuf := make([]byte, 32)
|
||
|
if _, err := io.ReadFull(conn, headbuf); err != nil {
|
||
10 years ago
|
return nil, err
|
||
|
}
|
||
4 years ago
|
|
||
|
// verify header mac
|
||
|
shouldMAC := updateMAC(h.ingressMAC, h.macCipher, headbuf[:16])
|
||
|
if !hmac.Equal(shouldMAC, headbuf[16:]) {
|
||
|
return nil, errors.New("bad header MAC")
|
||
10 years ago
|
}
|
||
4 years ago
|
h.dec.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now decrypted
|
||
|
fsize := readInt24(headbuf)
|
||
|
// ignore protocol type for now
|
||
7 years ago
|
|
||
4 years ago
|
// read the frame content
|
||
|
var rsize = fsize // frame size rounded up to 16 byte boundary
|
||
|
if padding := fsize % 16; padding > 0 {
|
||
|
rsize += 16 - padding
|
||
|
}
|
||
|
framebuf := make([]byte, rsize)
|
||
|
if _, err := io.ReadFull(conn, framebuf); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
10 years ago
|
|
||
4 years ago
|
// read and validate frame MAC. we can re-use headbuf for that.
|
||
|
h.ingressMAC.Write(framebuf)
|
||
|
fmacseed := h.ingressMAC.Sum(nil)
|
||
|
if _, err := io.ReadFull(conn, headbuf[:16]); err != nil {
|
||
10 years ago
|
return nil, err
|
||
|
}
|
||
4 years ago
|
shouldMAC = updateMAC(h.ingressMAC, h.macCipher, fmacseed)
|
||
|
if !hmac.Equal(shouldMAC, headbuf[:16]) {
|
||
|
return nil, errors.New("bad frame MAC")
|
||
|
}
|
||
|
|
||
|
// decrypt frame content
|
||
|
h.dec.XORKeyStream(framebuf, framebuf)
|
||
|
return framebuf[:fsize], nil
|
||
|
}
|
||
|
|
||
|
// Write writes a message to the connection.
|
||
|
//
|
||
|
// Write returns the written size of the message data. This may be less than or equal to
|
||
|
// len(data) depending on whether snappy compression is enabled.
|
||
|
func (c *Conn) Write(code uint64, data []byte) (uint32, error) {
|
||
|
if c.handshake == nil {
|
||
|
panic("can't WriteMsg before handshake")
|
||
10 years ago
|
}
|
||
4 years ago
|
if len(data) > maxUint24 {
|
||
|
return 0, errPlainMessageTooLarge
|
||
10 years ago
|
}
|
||
4 years ago
|
if c.snappy {
|
||
|
data = snappy.Encode(nil, data)
|
||
10 years ago
|
}
|
||
4 years ago
|
|
||
|
wireSize := uint32(len(data))
|
||
|
err := c.handshake.writeFrame(c.conn, code, data)
|
||
|
return wireSize, err
|
||
|
}
|
||
|
|
||
|
func (h *handshakeState) writeFrame(conn io.Writer, code uint64, data []byte) error {
|
||
|
ptype, _ := rlp.EncodeToBytes(code)
|
||
|
|
||
|
// write header
|
||
|
headbuf := make([]byte, 32)
|
||
|
fsize := len(ptype) + len(data)
|
||
|
if fsize > maxUint24 {
|
||
|
return errPlainMessageTooLarge
|
||
|
}
|
||
|
putInt24(uint32(fsize), headbuf)
|
||
|
copy(headbuf[3:], zeroHeader)
|
||
|
h.enc.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now encrypted
|
||
|
|
||
|
// write header MAC
|
||
|
copy(headbuf[16:], updateMAC(h.egressMAC, h.macCipher, headbuf[:16]))
|
||
|
if _, err := conn.Write(headbuf); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// write encrypted frame, updating the egress MAC hash with
|
||
|
// the data written to conn.
|
||
|
tee := cipher.StreamWriter{S: h.enc, W: io.MultiWriter(conn, h.egressMAC)}
|
||
|
if _, err := tee.Write(ptype); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if _, err := tee.Write(data); err != nil {
|
||
|
return err
|
||
10 years ago
|
}
|
||
4 years ago
|
if padding := fsize % 16; padding > 0 {
|
||
|
if _, err := tee.Write(zero16[:16-padding]); err != nil {
|
||
|
return err
|
||
|
}
|
||
10 years ago
|
}
|
||
4 years ago
|
|
||
|
// write frame MAC. egress MAC hash is up to date because
|
||
|
// frame content was written to it as well.
|
||
|
fmacseed := h.egressMAC.Sum(nil)
|
||
|
mac := updateMAC(h.egressMAC, h.macCipher, fmacseed)
|
||
|
_, err := conn.Write(mac)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func readInt24(b []byte) uint32 {
|
||
|
return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
func putInt24(v uint32, b []byte) {
|
||
|
b[0] = byte(v >> 16)
|
||
|
b[1] = byte(v >> 8)
|
||
|
b[2] = byte(v)
|
||
|
}
|
||
|
|
||
|
// 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 {
|
||
|
aesbuf := make([]byte, aes.BlockSize)
|
||
|
block.Encrypt(aesbuf, mac.Sum(nil))
|
||
|
for i := range aesbuf {
|
||
|
aesbuf[i] ^= seed[i]
|
||
|
}
|
||
|
mac.Write(aesbuf)
|
||
|
return mac.Sum(nil)[:16]
|
||
|
}
|
||
|
|
||
|
// Handshake performs the handshake. This must be called before any data is written
|
||
|
// or read from the connection.
|
||
|
func (c *Conn) Handshake(prv *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
|
||
10 years ago
|
var (
|
||
4 years ago
|
sec Secrets
|
||
10 years ago
|
err error
|
||
|
)
|
||
4 years ago
|
if c.dialDest != nil {
|
||
|
sec, err = initiatorEncHandshake(c.conn, prv, c.dialDest)
|
||
10 years ago
|
} else {
|
||
4 years ago
|
sec, err = receiverEncHandshake(c.conn, prv)
|
||
10 years ago
|
}
|
||
|
if err != nil {
|
||
6 years ago
|
return nil, err
|
||
10 years ago
|
}
|
||
4 years ago
|
c.InitWithSecrets(sec)
|
||
|
return sec.remote, err
|
||
|
}
|
||
|
|
||
|
// InitWithSecrets injects connection secrets as if a handshake had
|
||
|
// been performed. This cannot be called after the handshake.
|
||
|
func (c *Conn) InitWithSecrets(sec Secrets) {
|
||
|
if c.handshake != nil {
|
||
|
panic("can't handshake twice")
|
||
|
}
|
||
|
macc, err := aes.NewCipher(sec.MAC)
|
||
|
if err != nil {
|
||
|
panic("invalid MAC secret: " + err.Error())
|
||
|
}
|
||
|
encc, err := aes.NewCipher(sec.AES)
|
||
|
if err != nil {
|
||
|
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())
|
||
|
c.handshake = &handshakeState{
|
||
|
enc: cipher.NewCTR(encc, iv),
|
||
|
dec: cipher.NewCTR(encc, iv),
|
||
|
macCipher: macc,
|
||
|
egressMAC: sec.EgressMAC,
|
||
|
ingressMAC: sec.IngressMAC,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Close closes the underlying network connection.
|
||
|
func (c *Conn) Close() error {
|
||
|
return c.conn.Close()
|
||
|
}
|
||
|
|
||
|
// Constants for the handshake.
|
||
|
const (
|
||
|
maxUint24 = int(^uint32(0) >> 8)
|
||
|
|
||
|
sskLen = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
|
||
|
sigLen = crypto.SignatureLength // 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
|
||
|
|
||
|
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
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
// this is used in place of actual frame header data.
|
||
|
// TODO: replace this when Msg contains the protocol type code.
|
||
|
zeroHeader = []byte{0xC2, 0x80, 0x80}
|
||
|
// sixteen zero bytes
|
||
|
zero16 = make([]byte, 16)
|
||
|
|
||
|
// errPlainMessageTooLarge is returned if a decompressed message length exceeds
|
||
|
// the allowed 24 bits (i.e. length >= 16MB).
|
||
|
errPlainMessageTooLarge = errors.New("message length >= 16MB")
|
||
|
)
|
||
|
|
||
|
// Secrets represents the connection secrets which are negotiated during the handshake.
|
||
|
type Secrets struct {
|
||
|
AES, MAC []byte
|
||
|
EgressMAC, IngressMAC hash.Hash
|
||
|
remote *ecdsa.PublicKey
|
||
10 years ago
|
}
|
||
|
|
||
|
// encHandshake contains the state of the encryption handshake.
|
||
|
type encHandshake struct {
|
||
6 years ago
|
initiator bool
|
||
|
remote *ecies.PublicKey // remote-pubk
|
||
10 years ago
|
initNonce, respNonce []byte // nonce
|
||
|
randomPrivKey *ecies.PrivateKey // ecdhe-random
|
||
|
remoteRandomPub *ecies.PublicKey // ecdhe-random-pubk
|
||
|
}
|
||
|
|
||
9 years ago
|
// 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"`
|
||
|
}
|
||
|
|
||
4 years ago
|
// 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.
|
||
|
func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s Secrets, err error) {
|
||
|
authMsg := new(authMsgV4)
|
||
|
authPacket, err := readHandshakeMsg(authMsg, encAuthMsgLen, prv, conn)
|
||
|
if err != nil {
|
||
|
return s, err
|
||
|
}
|
||
|
h := new(encHandshake)
|
||
|
if err := h.handleAuthMsg(authMsg, prv); err != nil {
|
||
|
return s, err
|
||
|
}
|
||
|
|
||
|
authRespMsg, err := h.makeAuthResp()
|
||
|
if err != nil {
|
||
|
return s, err
|
||
|
}
|
||
|
var authRespPacket []byte
|
||
|
if authMsg.gotPlain {
|
||
|
authRespPacket, err = authRespMsg.sealPlain(h)
|
||
|
} else {
|
||
|
authRespPacket, err = sealEIP8(authRespMsg, h)
|
||
|
}
|
||
|
if err != nil {
|
||
|
return s, err
|
||
|
}
|
||
|
if _, err = conn.Write(authRespPacket); err != nil {
|
||
|
return s, err
|
||
|
}
|
||
|
return h.secrets(authPacket, authRespPacket)
|
||
|
}
|
||
|
|
||
|
func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) error {
|
||
|
// Import the remote identity.
|
||
|
rpub, err := importPublicKey(msg.InitiatorPubkey[:])
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
h.initNonce = msg.Nonce[:]
|
||
|
h.remote = rpub
|
||
|
|
||
|
// Generate random keypair for ECDH.
|
||
|
// If a private key is already set, use it instead of generating one (for testing).
|
||
|
if h.randomPrivKey == nil {
|
||
|
h.randomPrivKey, err = ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Check the signature.
|
||
|
token, err := h.staticSharedSecret(prv)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
signedMsg := xor(token, h.initNonce)
|
||
|
remoteRandomPub, err := crypto.Ecrecover(signedMsg, msg.Signature[:])
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
h.remoteRandomPub, _ = importPublicKey(remoteRandomPub)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// secrets is called after the handshake is completed.
|
||
|
// It extracts the connection secrets from the handshake values.
|
||
4 years ago
|
func (h *encHandshake) secrets(auth, authResp []byte) (Secrets, error) {
|
||
10 years ago
|
ecdheSecret, err := h.randomPrivKey.GenerateShared(h.remoteRandomPub, sskLen, sskLen)
|
||
|
if err != nil {
|
||
4 years ago
|
return Secrets{}, err
|
||
10 years ago
|
}
|
||
|
|
||
|
// derive base secrets from ephemeral key agreement
|
||
9 years ago
|
sharedSecret := crypto.Keccak256(ecdheSecret, crypto.Keccak256(h.respNonce, h.initNonce))
|
||
|
aesSecret := crypto.Keccak256(ecdheSecret, sharedSecret)
|
||
4 years ago
|
s := Secrets{
|
||
|
remote: h.remote.ExportECDSA(),
|
||
6 years ago
|
AES: aesSecret,
|
||
|
MAC: crypto.Keccak256(ecdheSecret, aesSecret),
|
||
10 years ago
|
}
|
||
|
|
||
|
// setup sha3 instances for the MACs
|
||
6 years ago
|
mac1 := sha3.NewLegacyKeccak256()
|
||
10 years ago
|
mac1.Write(xor(s.MAC, h.respNonce))
|
||
|
mac1.Write(auth)
|
||
6 years ago
|
mac2 := sha3.NewLegacyKeccak256()
|
||
10 years ago
|
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
|
||
|
}
|
||
|
|
||
9 years ago
|
// 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) {
|
||
6 years ago
|
return ecies.ImportECDSA(prv).GenerateShared(h.remote, sskLen, sskLen)
|
||
10 years ago
|
}
|
||
|
|
||
|
// 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.
|
||
4 years ago
|
func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ecdsa.PublicKey) (s Secrets, err error) {
|
||
6 years ago
|
h := &encHandshake{initiator: true, remote: ecies.ImportECDSAPublic(remote)}
|
||
6 years ago
|
authMsg, err := h.makeAuthMsg(prv)
|
||
10 years ago
|
if err != nil {
|
||
|
return s, err
|
||
|
}
|
||
9 years ago
|
authPacket, err := sealEIP8(authMsg, h)
|
||
10 years ago
|
if err != nil {
|
||
|
return s, err
|
||
|
}
|
||
4 years ago
|
|
||
9 years ago
|
if _, err = conn.Write(authPacket); err != nil {
|
||
10 years ago
|
return s, err
|
||
|
}
|
||
|
|
||
9 years ago
|
authRespMsg := new(authRespV4)
|
||
|
authRespPacket, err := readHandshakeMsg(authRespMsg, encAuthRespLen, prv, conn)
|
||
|
if err != nil {
|
||
10 years ago
|
return s, err
|
||
|
}
|
||
9 years ago
|
if err := h.handleAuthResp(authRespMsg); err != nil {
|
||
10 years ago
|
return s, err
|
||
|
}
|
||
9 years ago
|
return h.secrets(authPacket, authRespPacket)
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// makeAuthMsg creates the initiator handshake message.
|
||
6 years ago
|
func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) {
|
||
9 years ago
|
// Generate random initiator nonce.
|
||
|
h.initNonce = make([]byte, shaLen)
|
||
6 years ago
|
_, err := rand.Read(h.initNonce)
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, err
|
||
|
}
|
||
9 years ago
|
// Generate random keypair to for ECDH.
|
||
8 years ago
|
h.randomPrivKey, err = ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
|
||
10 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
9 years ago
|
// Sign known message: static-shared-secret ^ nonce
|
||
6 years ago
|
token, err := h.staticSharedSecret(prv)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
10 years ago
|
}
|
||
|
signed := xor(token, h.initNonce)
|
||
|
signature, err := crypto.Sign(signed, h.randomPrivKey.ExportECDSA())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
9 years ago
|
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
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) {
|
||
|
h.respNonce = msg.Nonce[:]
|
||
|
h.remoteRandomPub, err = importPublicKey(msg.RandomPubkey[:])
|
||
|
return err
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) {
|
||
|
// Generate random nonce.
|
||
|
h.respNonce = make([]byte, shaLen)
|
||
|
if _, err = rand.Read(h.respNonce); err != nil {
|
||
10 years ago
|
return nil, err
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
msg = new(authRespV4)
|
||
|
copy(msg.Nonce[:], h.respNonce)
|
||
|
copy(msg.RandomPubkey[:], exportPubkey(&h.randomPrivKey.PublicKey))
|
||
|
msg.Version = 4
|
||
|
return msg, nil
|
||
|
}
|
||
|
|
||
|
func (msg *authMsgV4) decodePlain(input []byte) {
|
||
|
n := copy(msg.Signature[:], input)
|
||
|
n += shaLen // skip sha3(initiator-ephemeral-pubk)
|
||
|
n += copy(msg.InitiatorPubkey[:], input[n:])
|
||
8 years ago
|
copy(msg.Nonce[:], input[n:])
|
||
9 years ago
|
msg.Version = 4
|
||
|
msg.gotPlain = true
|
||
|
}
|
||
|
|
||
|
func (msg *authRespV4) sealPlain(hs *encHandshake) ([]byte, error) {
|
||
|
buf := make([]byte, authRespLen)
|
||
|
n := copy(buf, msg.RandomPubkey[:])
|
||
8 years ago
|
copy(buf[n:], msg.Nonce[:])
|
||
6 years ago
|
return ecies.Encrypt(rand.Reader, hs.remote, buf, nil, nil)
|
||
9 years ago
|
}
|
||
|
|
||
|
func (msg *authRespV4) decodePlain(input []byte) {
|
||
|
n := copy(msg.RandomPubkey[:], input)
|
||
8 years ago
|
copy(msg.Nonce[:], input[n:])
|
||
9 years ago
|
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
|
||
10 years ago
|
}
|
||
9 years ago
|
// 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))
|
||
10 years ago
|
|
||
6 years ago
|
enc, err := ecies.Encrypt(rand.Reader, h.remote, buf.Bytes(), nil, prefix)
|
||
9 years ago
|
return append(prefix, enc...), err
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
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)
|
||
7 years ago
|
if dec, err := key.Decrypt(buf, nil, nil); err == nil {
|
||
9 years ago
|
msg.decodePlain(dec)
|
||
|
return buf, nil
|
||
10 years ago
|
}
|
||
9 years ago
|
// 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
|
||
|
}
|
||
7 years ago
|
dec, err := key.Decrypt(buf[2:], nil, prefix)
|
||
9 years ago
|
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)
|
||
10 years ago
|
}
|
||
|
|
||
|
// 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
|
||
7 years ago
|
pub, err := crypto.UnmarshalPubkey(pubKey65)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
9 years ago
|
}
|
||
|
return ecies.ImportECDSAPublic(pub), nil
|
||
10 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|