2017-05-02 23:33:51 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2018-09-22 20:12:08 +00:00
|
|
|
"crypto/rand"
|
2017-05-02 23:33:51 +00:00
|
|
|
"crypto/rsa"
|
2018-06-08 21:54:31 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
2018-09-22 20:12:08 +00:00
|
|
|
"golang.org/x/crypto/ed25519"
|
2017-05-02 23:33:51 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server
|
|
|
|
func ServerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
2018-09-22 20:12:08 +00:00
|
|
|
// Server
|
|
|
|
func ServerAuthValid3DH(hostname string, publicKey ed25519.PublicKey) (allowed, known bool) {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessAuthAs3DHServer(t *testing.T) {
|
|
|
|
|
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
|
|
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cconn, _ := net.Dial("tcp", ln.Addr().String())
|
|
|
|
|
|
|
|
cpub, cpriv, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
|
hostname := utils.GetTorV3Hostname(pub)
|
|
|
|
orc := NewOutboundConnection(cconn, hostname)
|
|
|
|
|
|
|
|
known, err := HandleOutboundConnection(orc).ProcessAuthAsV3Client(identity.InitializeV3("", &cpriv, &cpub))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error while testing ProcessAuthAsClient (in ProcessAuthAsServer) %v", err)
|
|
|
|
return
|
|
|
|
} else if !known {
|
|
|
|
t.Errorf("Client should have been known to the server, instead known was: %v", known)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, _ := ln.Accept()
|
|
|
|
|
|
|
|
rc := NewInboundConnection(conn)
|
|
|
|
err := HandleInboundConnection(rc).ProcessAuthAsV3Server(identity.InitializeV3("", &priv, &pub), ServerAuthValid3DH)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error while testing ProcessAuthAsServer: %v", err)
|
|
|
|
}
|
2019-01-11 20:28:50 +00:00
|
|
|
|
2019-11-08 00:11:14 +00:00
|
|
|
// Wait for server to finish
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2019-01-11 20:28:50 +00:00
|
|
|
// Test Close
|
|
|
|
rc.Close()
|
2018-09-22 20:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessAuthAsV3ServerFail(t *testing.T) {
|
|
|
|
|
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
|
|
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cconn, _ := net.Dial("tcp", ln.Addr().String())
|
|
|
|
|
|
|
|
cpub, cpriv, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
|
// Setting the RemoteHostname to the client pub key approximates a server sending the wrong public key.
|
|
|
|
hostname := utils.GetTorV3Hostname(cpub)
|
|
|
|
orc := NewOutboundConnection(cconn, hostname)
|
|
|
|
|
|
|
|
HandleOutboundConnection(orc).ProcessAuthAsV3Client(identity.InitializeV3("", &cpriv, &cpub))
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, _ := ln.Accept()
|
|
|
|
|
|
|
|
rc := NewInboundConnection(conn)
|
|
|
|
err := HandleInboundConnection(rc).ProcessAuthAsV3Server(identity.InitializeV3("", &priv, &pub), ServerAuthValid3DH)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Error while testing ProcessAuthAsServer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessAuthAsV3ClientFail(t *testing.T) {
|
|
|
|
|
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
|
|
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cconn, _ := net.Dial("tcp", ln.Addr().String())
|
|
|
|
|
|
|
|
// Giving the client inconsistent keypair to make EDH fail
|
|
|
|
cpub, _, _ := ed25519.GenerateKey(rand.Reader)
|
2018-10-09 19:55:42 +00:00
|
|
|
_, cpriv, _ := ed25519.GenerateKey(rand.Reader)
|
2018-09-22 20:12:08 +00:00
|
|
|
|
|
|
|
hostname := utils.GetTorV3Hostname(pub)
|
|
|
|
orc := NewOutboundConnection(cconn, hostname)
|
|
|
|
|
|
|
|
HandleOutboundConnection(orc).ProcessAuthAsV3Client(identity.InitializeV3("", &cpriv, &cpub))
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, _ := ln.Accept()
|
|
|
|
|
|
|
|
rc := NewInboundConnection(conn)
|
|
|
|
err := HandleInboundConnection(rc).ProcessAuthAsV3Server(identity.InitializeV3("", &priv, &pub), ServerAuthValid3DH)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Error while testing ProcessAuthAsServer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:33:51 +00:00
|
|
|
func TestProcessAuthTimeout(t *testing.T) {
|
|
|
|
|
|
|
|
ln, _ := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
net.Dial("tcp", ln.Addr().String())
|
2018-11-30 21:04:38 +00:00
|
|
|
time.Sleep(17 * time.Second)
|
2017-05-02 23:33:51 +00:00
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, _ := ln.Accept()
|
2019-01-23 19:57:43 +00:00
|
|
|
|
2019-01-23 19:55:42 +00:00
|
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
2017-05-02 23:33:51 +00:00
|
|
|
rc := NewInboundConnection(conn)
|
2019-01-23 19:55:42 +00:00
|
|
|
err := HandleInboundConnection(rc).ProcessAuthAsV3Server(identity.InitializeV3("", &priv, &pub), ServerAuthValid3DH)
|
2017-05-02 23:33:51 +00:00
|
|
|
if err != utils.ActionTimedOutError {
|
2019-01-11 20:28:50 +00:00
|
|
|
t.Errorf("Error while testing TestProcessAuthTimeout - Should have timed out after 15 seconds, instead ERR was %v", err)
|
2017-05-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
}
|