2015-02-26 22:30:34 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/hmac"
|
|
|
|
"errors"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-02-27 02:09:53 +00:00
|
|
|
// this is used in place of actual frame header data.
|
|
|
|
// TODO: replace this when Msg contains the protocol type code.
|
2015-02-26 22:30:34 +00:00
|
|
|
zeroHeader = []byte{0xC2, 0x80, 0x80}
|
2015-02-27 02:09:53 +00:00
|
|
|
|
|
|
|
// sixteen zero bytes
|
|
|
|
zero16 = make([]byte, 16)
|
2015-02-26 22:30:34 +00:00
|
|
|
)
|
|
|
|
|
2015-03-04 15:27:37 +00: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-26 22:30:34 +00:00
|
|
|
type rlpxFrameRW struct {
|
|
|
|
conn io.ReadWriter
|
2015-02-27 02:09:53 +00:00
|
|
|
enc cipher.Stream
|
|
|
|
dec cipher.Stream
|
2015-02-26 22:30:34 +00:00
|
|
|
|
|
|
|
macCipher cipher.Block
|
|
|
|
egressMAC hash.Hash
|
|
|
|
ingressMAC hash.Hash
|
|
|
|
}
|
|
|
|
|
2015-02-27 02:09:53 +00:00
|
|
|
func newRlpxFrameRW(conn io.ReadWriter, s secrets) *rlpxFrameRW {
|
|
|
|
macc, err := aes.NewCipher(s.MAC)
|
|
|
|
if err != nil {
|
|
|
|
panic("invalid MAC secret: " + err.Error())
|
|
|
|
}
|
|
|
|
encc, err := aes.NewCipher(s.AES)
|
2015-02-26 22:30:34 +00:00
|
|
|
if err != nil {
|
2015-02-27 02:09:53 +00: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-26 22:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
|
|
|
|
ptype, _ := rlp.EncodeToBytes(msg.Code)
|
|
|
|
|
|
|
|
// write header
|
|
|
|
headbuf := make([]byte, 32)
|
|
|
|
fsize := uint32(len(ptype)) + msg.Size
|
|
|
|
putInt24(fsize, headbuf) // TODO: check overflow
|
|
|
|
copy(headbuf[3:], zeroHeader)
|
2015-02-27 02:09:53 +00:00
|
|
|
rw.enc.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now encrypted
|
2015-02-27 13:08:57 +00:00
|
|
|
|
|
|
|
// write header MAC
|
|
|
|
copy(headbuf[16:], updateMAC(rw.egressMAC, rw.macCipher, headbuf[:16]))
|
2015-02-26 22:30:34 +00:00
|
|
|
if _, err := rw.conn.Write(headbuf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-27 13:08:57 +00:00
|
|
|
// write encrypted frame, updating the egress MAC hash with
|
|
|
|
// the data written to conn.
|
2015-02-27 02:09:53 +00:00
|
|
|
tee := cipher.StreamWriter{S: rw.enc, W: io.MultiWriter(rw.conn, rw.egressMAC)}
|
2015-02-26 22:30:34 +00: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 13:08:57 +00:00
|
|
|
// write frame MAC. egress MAC hash is up to date because
|
2015-02-26 22:30:34 +00:00
|
|
|
// frame content was written to it as well.
|
2015-02-27 13:08:57 +00:00
|
|
|
fmacseed := rw.egressMAC.Sum(nil)
|
|
|
|
mac := updateMAC(rw.egressMAC, rw.macCipher, fmacseed)
|
2015-02-27 02:09:53 +00:00
|
|
|
_, err := rw.conn.Write(mac)
|
2015-02-26 22:30:34 +00: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 02:09:53 +00:00
|
|
|
// verify header mac
|
2015-02-27 13:08:57 +00:00
|
|
|
shouldMAC := updateMAC(rw.ingressMAC, rw.macCipher, headbuf[:16])
|
|
|
|
if !hmac.Equal(shouldMAC, headbuf[16:]) {
|
2015-02-26 22:30:34 +00:00
|
|
|
return msg, errors.New("bad header MAC")
|
|
|
|
}
|
2015-02-27 02:09:53 +00:00
|
|
|
rw.dec.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now decrypted
|
|
|
|
fsize := readInt24(headbuf)
|
|
|
|
// ignore protocol type for now
|
2015-02-26 22:30:34 +00:00
|
|
|
|
|
|
|
// read the frame content
|
2015-02-27 02:09:53 +00: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-26 22:30:34 +00:00
|
|
|
if _, err := io.ReadFull(rw.conn, framebuf); err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
2015-02-27 02:09:53 +00:00
|
|
|
|
2015-02-26 22:30:34 +00:00
|
|
|
// read and validate frame MAC. we can re-use headbuf for that.
|
2015-02-27 02:09:53 +00:00
|
|
|
rw.ingressMAC.Write(framebuf)
|
2015-02-27 13:08:57 +00:00
|
|
|
fmacseed := rw.ingressMAC.Sum(nil)
|
|
|
|
if _, err := io.ReadFull(rw.conn, headbuf[:16]); err != nil {
|
2015-02-26 22:30:34 +00:00
|
|
|
return msg, err
|
|
|
|
}
|
2015-02-27 13:08:57 +00:00
|
|
|
shouldMAC = updateMAC(rw.ingressMAC, rw.macCipher, fmacseed)
|
|
|
|
if !hmac.Equal(shouldMAC, headbuf[:16]) {
|
2015-02-26 22:30:34 +00:00
|
|
|
return msg, errors.New("bad frame MAC")
|
|
|
|
}
|
|
|
|
|
2015-02-27 02:09:53 +00:00
|
|
|
// decrypt frame content
|
|
|
|
rw.dec.XORKeyStream(framebuf, framebuf)
|
|
|
|
|
2015-02-26 22:30:34 +00:00
|
|
|
// decode message code
|
2015-02-27 02:09:53 +00:00
|
|
|
content := bytes.NewReader(framebuf[:fsize])
|
2015-02-26 22:30:34 +00:00
|
|
|
if err := rlp.Decode(content, &msg.Code); err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
msg.Size = uint32(content.Len())
|
|
|
|
msg.Payload = content
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2015-02-27 13:08:57 +00: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-26 22:30:34 +00:00
|
|
|
aesbuf := make([]byte, aes.BlockSize)
|
|
|
|
block.Encrypt(aesbuf, mac.Sum(nil))
|
|
|
|
for i := range aesbuf {
|
2015-02-27 13:08:57 +00:00
|
|
|
aesbuf[i] ^= seed[i]
|
2015-02-26 22:30:34 +00:00
|
|
|
}
|
|
|
|
mac.Write(aesbuf)
|
2015-02-27 13:08:57 +00:00
|
|
|
return mac.Sum(nil)[:16]
|
2015-02-26 22:30:34 +00: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)
|
|
|
|
}
|