2016-10-18 04:41:20 +02:00
|
|
|
package brontide
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-11-08 04:45:00 +01:00
|
|
|
"math"
|
2016-10-18 04:41:20 +02:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2020-04-28 10:06:29 +02:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2018-07-31 09:17:17 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-08-25 06:48:32 +02:00
|
|
|
"github.com/lightningnetwork/lnd/tor"
|
2016-10-18 04:41:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Conn is an implementation of net.Conn which enforces an authenticated key
|
|
|
|
// exchange and message encryption protocol dubbed "Brontide" after initial TCP
|
|
|
|
// connection establishment. In the case of a successful handshake, all
|
|
|
|
// messages sent via the .Write() method are encrypted with an AEAD cipher
|
2017-02-23 20:56:47 +01:00
|
|
|
// along with an encrypted length-prefix. See the Machine struct for
|
2016-10-18 04:41:20 +02:00
|
|
|
// additional details w.r.t to the handshake and encryption scheme.
|
|
|
|
type Conn struct {
|
|
|
|
conn net.Conn
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
noise *Machine
|
2016-10-18 04:41:20 +02:00
|
|
|
|
|
|
|
readBuf bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that Conn meets the net.Conn interface.
|
|
|
|
var _ net.Conn = (*Conn)(nil)
|
|
|
|
|
|
|
|
// Dial attempts to establish an encrypted+authenticated connection with the
|
|
|
|
// remote peer located at address which has remotePub as its long-term static
|
|
|
|
// public key. In the case of a handshake failure, the connection is closed and
|
|
|
|
// a non-nil error is returned.
|
2020-04-28 10:06:29 +02:00
|
|
|
func Dial(local keychain.SingleKeyECDH, netAddr *lnwire.NetAddress,
|
2020-08-25 06:48:32 +02:00
|
|
|
timeout time.Duration, dialer tor.DialFunc) (*Conn, error) {
|
2020-04-28 10:06:29 +02:00
|
|
|
|
2016-10-27 04:04:19 +02:00
|
|
|
ipAddr := netAddr.Address.String()
|
2017-10-20 23:45:23 +02:00
|
|
|
var conn net.Conn
|
|
|
|
var err error
|
2020-08-25 06:48:32 +02:00
|
|
|
conn, err = dialer("tcp", ipAddr, timeout)
|
2016-10-18 04:41:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Conn{
|
|
|
|
conn: conn,
|
2020-04-28 10:06:29 +02:00
|
|
|
noise: NewBrontideMachine(true, local, netAddr.IdentityKey),
|
2016-10-18 04:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate the handshake by sending the first act to the receiver.
|
|
|
|
actOne, err := b.noise.GenActOne()
|
|
|
|
if err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
b.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := conn.Write(actOne[:]); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
b.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:56:31 +02:00
|
|
|
// We'll ensure that we get ActTwo from the remote peer in a timely
|
2018-04-03 00:56:41 +02:00
|
|
|
// manner. If they don't respond within 1s, then we'll kill the
|
2017-10-04 23:56:31 +02:00
|
|
|
// connection.
|
2019-02-16 03:13:52 +01:00
|
|
|
err = conn.SetReadDeadline(time.Now().Add(handshakeReadTimeout))
|
|
|
|
if err != nil {
|
|
|
|
b.conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-04 23:56:31 +02:00
|
|
|
|
2016-10-18 04:41:20 +02:00
|
|
|
// If the first act was successful (we know that address is actually
|
|
|
|
// remotePub), then read the second act after which we'll be able to
|
|
|
|
// send our static public key to the remote peer with strong forward
|
|
|
|
// secrecy.
|
|
|
|
var actTwo [ActTwoSize]byte
|
|
|
|
if _, err := io.ReadFull(conn, actTwo[:]); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
b.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := b.noise.RecvActTwo(actTwo); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
b.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, complete the handshake by sending over our encrypted static
|
|
|
|
// key and execute the final ECDH operation.
|
|
|
|
actThree, err := b.noise.GenActThree()
|
|
|
|
if err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
b.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := conn.Write(actThree[:]); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
b.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:56:31 +02:00
|
|
|
// We'll reset the deadline as it's no longer critical beyond the
|
|
|
|
// initial handshake.
|
2019-02-16 03:13:52 +01:00
|
|
|
err = conn.SetReadDeadline(time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
b.conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-04 23:56:31 +02:00
|
|
|
|
2016-10-18 04:41:20 +02:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-02-22 05:11:19 +01:00
|
|
|
// ReadNextMessage uses the connection in a message-oriented manner, instructing
|
|
|
|
// it to read the next _full_ message with the brontide stream. This function
|
|
|
|
// will block until the read of the header and body succeeds.
|
|
|
|
//
|
|
|
|
// NOTE: This method SHOULD NOT be used in the case that the connection may be
|
|
|
|
// adversarial and induce long delays. If the caller needs to set read deadlines
|
|
|
|
// appropriately, it is preferred that they use the split ReadNextHeader and
|
|
|
|
// ReadNextBody methods so that the deadlines can be set appropriately on each.
|
2017-04-21 00:35:29 +02:00
|
|
|
func (c *Conn) ReadNextMessage() ([]byte, error) {
|
|
|
|
return c.noise.ReadMessage(c.conn)
|
|
|
|
}
|
|
|
|
|
2019-02-22 05:11:19 +01:00
|
|
|
// ReadNextHeader uses the connection to read the next header from the brontide
|
|
|
|
// stream. This function will block until the read of the header succeeds and
|
|
|
|
// return the packet length (including MAC overhead) that is expected from the
|
|
|
|
// subsequent call to ReadNextBody.
|
|
|
|
func (c *Conn) ReadNextHeader() (uint32, error) {
|
|
|
|
return c.noise.ReadHeader(c.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadNextBody uses the connection to read the next message body from the
|
|
|
|
// brontide stream. This function will block until the read of the body succeeds
|
|
|
|
// and return the decrypted payload. The provided buffer MUST be the packet
|
|
|
|
// length returned by the preceding call to ReadNextHeader.
|
|
|
|
func (c *Conn) ReadNextBody(buf []byte) ([]byte, error) {
|
|
|
|
return c.noise.ReadBody(c.conn, buf)
|
|
|
|
}
|
|
|
|
|
2016-10-18 04:41:20 +02:00
|
|
|
// Read reads data from the connection. Read can be made to time out and
|
2018-04-18 04:02:04 +02:00
|
|
|
// return an Error with Timeout() == true after a fixed time limit; see
|
2016-10-18 04:41:20 +02:00
|
|
|
// SetDeadline and SetReadDeadline.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) Read(b []byte) (n int, err error) {
|
2017-04-21 00:35:29 +02:00
|
|
|
// In order to reconcile the differences between the record abstraction
|
|
|
|
// of our AEAD connection, and the stream abstraction of TCP, we
|
|
|
|
// maintain an intermediate read buffer. If this buffer becomes
|
2018-02-07 04:11:11 +01:00
|
|
|
// depleted, then we read the next record, and feed it into the
|
2017-04-21 00:35:29 +02:00
|
|
|
// buffer. Otherwise, we read directly from the buffer.
|
2016-10-18 04:41:20 +02:00
|
|
|
if c.readBuf.Len() == 0 {
|
|
|
|
plaintext, err := c.noise.ReadMessage(c.conn)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.readBuf.Write(plaintext); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.readBuf.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes data to the connection. Write can be made to time out and
|
2018-04-18 04:02:04 +02:00
|
|
|
// return an Error with Timeout() == true after a fixed time limit; see
|
2016-10-18 04:41:20 +02:00
|
|
|
// SetDeadline and SetWriteDeadline.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) Write(b []byte) (n int, err error) {
|
2016-11-08 04:45:00 +01:00
|
|
|
// If the message doesn't require any chunking, then we can go ahead
|
|
|
|
// with a single write.
|
2017-01-08 04:15:58 +01:00
|
|
|
if len(b) <= math.MaxUint16 {
|
2019-04-23 01:03:56 +02:00
|
|
|
err = c.noise.WriteMessage(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.noise.Flush(c.conn)
|
2016-11-08 04:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we need to split the message into fragments, then we'll write
|
2017-01-08 04:15:58 +01:00
|
|
|
// chunks which maximize usage of the available payload.
|
|
|
|
chunkSize := math.MaxUint16
|
2016-11-08 04:45:00 +01:00
|
|
|
|
|
|
|
bytesToWrite := len(b)
|
|
|
|
bytesWritten := 0
|
|
|
|
for bytesWritten < bytesToWrite {
|
|
|
|
// If we're on the last chunk, then truncate the chunk size as
|
|
|
|
// necessary to avoid an out-of-bounds array memory access.
|
|
|
|
if bytesWritten+chunkSize > len(b) {
|
|
|
|
chunkSize = len(b) - bytesWritten
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice off the next chunk to be written based on our running
|
|
|
|
// counter and next chunk size.
|
|
|
|
chunk := b[bytesWritten : bytesWritten+chunkSize]
|
2019-04-23 01:03:56 +02:00
|
|
|
if err := c.noise.WriteMessage(chunk); err != nil {
|
2016-11-08 04:45:00 +01:00
|
|
|
return bytesWritten, err
|
|
|
|
}
|
|
|
|
|
2019-04-23 01:03:56 +02:00
|
|
|
n, err := c.noise.Flush(c.conn)
|
|
|
|
bytesWritten += n
|
|
|
|
if err != nil {
|
|
|
|
return bytesWritten, err
|
|
|
|
}
|
2016-11-08 04:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytesWritten, nil
|
2016-10-18 04:41:20 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 01:04:53 +02:00
|
|
|
// WriteMessage encrypts and buffers the next message p for the connection. The
|
|
|
|
// ciphertext of the message is prepended with an encrypt+auth'd length which
|
|
|
|
// must be used as the AD to the AEAD construction when being decrypted by the
|
|
|
|
// other side.
|
|
|
|
//
|
|
|
|
// NOTE: This DOES NOT write the message to the wire, it should be followed by a
|
|
|
|
// call to Flush to ensure the message is written.
|
|
|
|
func (c *Conn) WriteMessage(b []byte) error {
|
|
|
|
return c.noise.WriteMessage(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush attempts to write a message buffered using WriteMessage to the
|
|
|
|
// underlying connection. If no buffered message exists, this will result in a
|
|
|
|
// NOP. Otherwise, it will continue to write the remaining bytes, picking up
|
|
|
|
// where the byte stream left off in the event of a partial write. The number of
|
|
|
|
// bytes returned reflects the number of plaintext bytes in the payload, and
|
|
|
|
// does not account for the overhead of the header or MACs.
|
|
|
|
//
|
|
|
|
// NOTE: It is safe to call this method again iff a timeout error is returned.
|
|
|
|
func (c *Conn) Flush() (int, error) {
|
|
|
|
return c.noise.Flush(c.conn)
|
|
|
|
}
|
|
|
|
|
2016-10-18 04:41:20 +02:00
|
|
|
// Close closes the connection. Any blocked Read or Write operations will be
|
|
|
|
// unblocked and return errors.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) Close() error {
|
|
|
|
// TODO(roasbeef): reset brontide state?
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
|
|
|
return c.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeadline sets the read and write deadlines associated with the
|
|
|
|
// connection. It is equivalent to calling both SetReadDeadline and
|
|
|
|
// SetWriteDeadline.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) SetDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadDeadline sets the deadline for future Read calls. A zero value for t
|
|
|
|
// means Read will not time out.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteDeadline sets the deadline for future Write calls. Even if write
|
|
|
|
// times out, it may return n > 0, indicating that some of the data was
|
|
|
|
// successfully written. A zero value for t means Write will not time out.
|
|
|
|
//
|
|
|
|
// Part of the net.Conn interface.
|
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemotePub returns the remote peer's static public key.
|
|
|
|
func (c *Conn) RemotePub() *btcec.PublicKey {
|
|
|
|
return c.noise.remoteStatic
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalPub returns the local peer's static public key.
|
|
|
|
func (c *Conn) LocalPub() *btcec.PublicKey {
|
|
|
|
return c.noise.localStatic.PubKey()
|
|
|
|
}
|