Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions lndc/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package lndc

import (
"bytes"
"fmt"
"io"
"log"
"math"
"net"
"log"
"time"
"fmt"

"github.com/mit-dci/lit/btcutil/btcec"
"github.com/mit-dci/lit/lnutil"
Expand All @@ -29,17 +29,31 @@ type Conn struct {

// A compile-time assertion to ensure that Conn meets the net.Conn interface.
var _ net.Conn = (*Conn)(nil)

var Noise_XK bool
// 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.
func Dial(localPriv *btcec.PrivateKey, ipAddr string, remotePKH string,
func Dial(localPriv *btcec.PrivateKey, ipAddr string, remoteAddress string,
dialer func(string, string) (net.Conn, error)) (*Conn, error) {

var remotePKH string
var remotePK [33]byte
if remoteAddress[0:3] == "ln1" { // its a remote PKH
remotePKH = remoteAddress
} else if len(remoteAddress) == 33 { // remotePK
temp := []byte(remoteAddress)
copy(remotePK[:], temp)
}
var conn net.Conn
var err error
var empty [33]byte
if remotePK != empty {
log.Println("Connecting via Noise_XK since we know remotePK")
Noise_XK = true
SetConsts()
}
conn, err = dialer("tcp", ipAddr)
log.Println("ipAddr is", ipAddr)
if err != nil {
return nil, err
}
Expand All @@ -48,9 +62,8 @@ func Dial(localPriv *btcec.PrivateKey, ipAddr string, remotePKH string,
conn: conn,
noise: NewNoiseMachine(true, localPriv),
}

// Initiate the handshake by sending the first act to the receiver.
actOne, err := b.noise.GenActOne()
actOne, err := b.noise.GenActOne(remotePK)
if err != nil {
b.conn.Close()
return nil, err
Expand All @@ -69,22 +82,30 @@ func Dial(localPriv *btcec.PrivateKey, ipAddr string, remotePKH string,
// 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
actTwo := make([]byte, ActTwoSize)
if _, err := io.ReadFull(conn, actTwo[:]); err != nil {
b.conn.Close()
return nil, err
}
s, err := b.noise.RecvActTwo(actTwo)
if err != nil {
b.conn.Close()
return nil, err
if !Noise_XK {
remotePK, err = b.noise.RecvActTwo(actTwo)
if err != nil {
b.conn.Close()
return nil, err
}
} else {
if _, err := b.noise.RecvActTwo(actTwo); err != nil {
b.conn.Close()
return nil, err
}
}

log.Println("Received pubkey", s)
if lnutil.LitAdrFromPubkey(s) != remotePKH {
log.Println("Received pubkey", remotePK)
if lnutil.LitAdrFromPubkey(remotePK) != remotePKH && !Noise_XK {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to make sense to switch these clauses around, so that if Noise_XK is true it will not do the comparison.

// for noise_XK dont check PKH and PK because we'd have already checked this
// the last time we connected to this guy
return nil, fmt.Errorf("Remote PKH doesn't match. Quitting!")
}
log.Printf("Received PKH %s matches", lnutil.LitAdrFromPubkey(s))
log.Printf("Received PKH %s matches", lnutil.LitAdrFromPubkey(remotePK))

// Finally, complete the handshake by sending over our encrypted static
// key and execute the final ECDH operation.
Expand Down
12 changes: 9 additions & 3 deletions lndc/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lndc
import (
"errors"
"io"
"log"
"net"
"time"

Expand Down Expand Up @@ -111,20 +112,25 @@ func (l *Listener) doHandshake(conn net.Conn) {
// 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
actOne := make([]byte, ActOneSize)
log.Println("Handshake Version", HandshakeVersion)
if _, err := io.ReadFull(conn, actOne[:]); err != nil {
lndcConn.conn.Close()
l.rejectConn(err)
return
}
if actOne[0] == 0 { // remote node wants to connect via XK
HandshakeVersion = byte(0)
ActTwoSize = 50
} // no need for else as default covers XX
if err := lndcConn.noise.RecvActOne(actOne); err != nil {
lndcConn.conn.Close()
l.rejectConn(err)
return
}
// Next, progress the handshake processes by sending over our ephemeral
// key for the session along with an authenticating tag.
actTwo, err := lndcConn.noise.GenActTwo()
actTwo, err := lndcConn.noise.GenActTwo(HandshakeVersion)
if err != nil {
lndcConn.conn.Close()
l.rejectConn(err)
Expand All @@ -150,7 +156,7 @@ func (l *Listener) doHandshake(conn net.Conn) {
// Finally, finish the handshake processes by reading and decrypting
// the connection peer's static public key. If this succeeds then both
// sides have mutually authenticated each other.
var actThree [ActThreeSize]byte
actThree := make([]byte, ActThreeSize)
if _, err := io.ReadFull(conn, actThree[:]); err != nil {
lndcConn.conn.Close()
l.rejectConn(err)
Expand Down
Loading