2016-10-18 04:41:20 +02:00
|
|
|
package brontide
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
2017-10-04 23:56:31 +02:00
|
|
|
"time"
|
2016-10-18 04:41:20 +02:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Listener is an implementation of a net.Conn which executes an authenticated
|
2017-08-02 02:17:55 +02:00
|
|
|
// key exchange and message encryption protocol dubbed "Machine" after
|
2017-02-23 20:56:47 +01:00
|
|
|
// initial connection acceptance. See the Machine struct for additional
|
2016-10-18 04:41:20 +02:00
|
|
|
// details w.r.t the handshake and encryption scheme used within the
|
|
|
|
// connection.
|
|
|
|
type Listener struct {
|
|
|
|
localStatic *btcec.PrivateKey
|
|
|
|
|
|
|
|
tcp *net.TCPListener
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that Conn meets the net.Listener interface.
|
|
|
|
var _ net.Listener = (*Listener)(nil)
|
|
|
|
|
|
|
|
// NewListener returns a new net.Listener which enforces the Brontide scheme
|
|
|
|
// during both initial connection establishment and data transfer.
|
|
|
|
func NewListener(localStatic *btcec.PrivateKey, listenAddr string) (*Listener,
|
|
|
|
error) {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", listenAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Listener{
|
|
|
|
localStatic: localStatic,
|
|
|
|
tcp: l,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept waits for and returns the next connection to the listener. All
|
|
|
|
// incoming connections are authenticated via the three act Brontide
|
2017-08-02 02:17:55 +02:00
|
|
|
// key-exchange scheme. This function will fail with a non-nil error in the
|
2017-07-30 02:40:27 +02:00
|
|
|
// case that either the handshake breaks down, or the remote peer doesn't know
|
2016-10-18 04:41:20 +02:00
|
|
|
// our static public key.
|
|
|
|
//
|
|
|
|
// Part of the net.Listener interface.
|
|
|
|
func (l *Listener) Accept() (net.Conn, error) {
|
|
|
|
conn, err := l.tcp.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
brontideConn := &Conn{
|
|
|
|
conn: conn,
|
|
|
|
noise: NewBrontideMachine(false, l.localStatic, nil),
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:56:31 +02:00
|
|
|
// We'll ensure that we get ActOne from the remote peer in a timely
|
|
|
|
// manner. If they don't respond within 15 seconds, then we'll kill the
|
|
|
|
// connection.
|
|
|
|
conn.SetReadDeadline(time.Now().Add(time.Second * 15))
|
|
|
|
|
2016-10-18 04:41:20 +02:00
|
|
|
// Attempt to carry out the first act of the handshake protocol. If the
|
|
|
|
// connecting node doesn't know our long-term static public key, then
|
|
|
|
// this portion will fail with a non-nil error.
|
|
|
|
var actOne [ActOneSize]byte
|
|
|
|
if _, err := io.ReadFull(conn, actOne[:]); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
brontideConn.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := brontideConn.noise.RecvActOne(actOne); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
brontideConn.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, progress the handshake processes by sending over our ephemeral
|
|
|
|
// key for the session along with an authenticating tag.
|
|
|
|
actTwo, err := brontideConn.noise.GenActTwo()
|
|
|
|
if err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
brontideConn.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := conn.Write(actTwo[:]); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
brontideConn.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
|
|
|
|
// manner. If they don't respond within 15 seconds, then we'll kill the
|
|
|
|
// connection.
|
|
|
|
conn.SetReadDeadline(time.Now().Add(time.Second * 15))
|
|
|
|
|
2017-07-30 02:40:27 +02:00
|
|
|
// Finally, finish the handshake processes by reading and decrypting
|
|
|
|
// the connection peer's static public key. If this succeeds then both
|
2016-10-18 04:41:20 +02:00
|
|
|
// sides have mutually authenticated each other.
|
|
|
|
var actThree [ActThreeSize]byte
|
|
|
|
if _, err := io.ReadFull(conn, actThree[:]); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
brontideConn.conn.Close()
|
2016-10-18 04:41:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := brontideConn.noise.RecvActThree(actThree); err != nil {
|
2016-12-01 04:11:49 +01:00
|
|
|
brontideConn.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.
|
|
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
|
2016-10-18 04:41:20 +02:00
|
|
|
return brontideConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the listener. Any blocked Accept operations will be unblocked
|
|
|
|
// and return errors.
|
|
|
|
//
|
|
|
|
// Part of the net.Listener interface.
|
|
|
|
func (l *Listener) Close() error {
|
|
|
|
return l.tcp.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
|
|
|
//
|
|
|
|
// Part of the net.Listener interface.
|
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return l.tcp.Addr()
|
|
|
|
}
|