From 01ec46a97c76f6021cc593e48a613cd879775001 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 8 Aug 2019 11:39:38 -0700 Subject: [PATCH] Upgrading to Tapir Identity --- .gitignore | 2 ++ app/app.go | 6 +++--- app/appService.go | 6 +++--- go.mod | 2 +- go.sum | 5 +++-- protocol/connections/engine.go | 15 +++++---------- protocol/connections/peerapp.go | 18 +++++++++--------- .../connections/peerserverconnection_test.go | 16 ++++++++-------- 8 files changed, 34 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 0e2ac2f..e5b23c2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ server/app/messages .reviewboardrc /vendor/ /testing/tor/ +/storage/testing/ +/testing/storage/ diff --git a/app/app.go b/app/app.go index 5a9c5df..cc8aea9 100644 --- a/app/app.go +++ b/app/app.go @@ -7,9 +7,9 @@ import ( "cwtch.im/cwtch/peer" "cwtch.im/cwtch/protocol/connections" "cwtch.im/cwtch/storage" + "cwtch.im/tapir/primitives" "fmt" "git.openprivacy.ca/openprivacy/libricochet-go/connectivity" - "git.openprivacy.ca/openprivacy/libricochet-go/identity" "git.openprivacy.ca/openprivacy/libricochet-go/log" "io/ioutil" "os" @@ -112,7 +112,7 @@ func (app *application) CreatePeer(name string, password string) { blockedPeers := profile.BlockedPeers() // TODO: Would be nice if ProtocolEngine did not need to explicitly be given the Private Key. - identity := identity.InitializeV3(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) + identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, app.acn, app.eventBuses[profile.Onion], blockedPeers) app.peers[profile.Onion] = peer @@ -167,7 +167,7 @@ func (app *application) LoadProfiles(password string) { peer.Init(app.eventBuses[profile.Onion]) blockedPeers := profile.BlockedPeers() - identity := identity.InitializeV3(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) + identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, app.acn, app.eventBuses[profile.Onion], blockedPeers) app.mutex.Lock() app.peers[profile.Onion] = peer diff --git a/app/appService.go b/app/appService.go index 57853aa..4041527 100644 --- a/app/appService.go +++ b/app/appService.go @@ -6,8 +6,8 @@ import ( "cwtch.im/cwtch/model" "cwtch.im/cwtch/protocol/connections" "cwtch.im/cwtch/storage" + "cwtch.im/tapir/primitives" "git.openprivacy.ca/openprivacy/libricochet-go/connectivity" - "git.openprivacy.ca/openprivacy/libricochet-go/identity" "git.openprivacy.ca/openprivacy/libricochet-go/log" "path" "strconv" @@ -95,7 +95,7 @@ func (as *applicationService) createPeer(name, password string) { blockedPeers := profile.BlockedPeers() // TODO: Would be nice if ProtocolEngine did not need to explicitly be given the Private Key. - identity := identity.InitializeV3(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) + identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], blockedPeers) as.storage[profile.Onion] = profileStore @@ -111,7 +111,7 @@ func (as *applicationService) loadProfiles(password string) { as.eventBuses[profile.Onion] = event.IPCEventManagerFrom(as.bridge, profile.Onion, as.eventBuses[profile.Onion]) blockedPeers := profile.BlockedPeers() - identity := identity.InitializeV3(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) + identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], blockedPeers) as.mutex.Lock() as.storage[profile.Onion] = profileStore diff --git a/go.mod b/go.mod index 57bf5a1..4e61676 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module cwtch.im/cwtch require ( - cwtch.im/tapir v0.1.6 + cwtch.im/tapir v0.1.9 git.openprivacy.ca/openprivacy/libricochet-go v1.0.5 github.com/c-bata/go-prompt v0.2.3 github.com/golang/protobuf v1.3.2 diff --git a/go.sum b/go.sum index b45166f..089cc00 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,8 @@ -cwtch.im/tapir v0.1.6 h1:5wd0z8TOUftEBIlCosLechh5KSAo9HfiQNcqknSzRWA= -cwtch.im/tapir v0.1.6/go.mod h1:EuRYdVrwijeaGBQ4OijDDRHf7R2MDSypqHkSl5DxI34= +cwtch.im/tapir v0.1.9 h1:TXIKN/8q2cNMlwGmu8c8i3Vq2+x61I8G9638LkBicjk= +cwtch.im/tapir v0.1.9/go.mod h1:EuRYdVrwijeaGBQ4OijDDRHf7R2MDSypqHkSl5DxI34= git.openprivacy.ca/openprivacy/libricochet-go v1.0.4 h1:GWLMJ5jBSIC/gFXzdbbeVz7fIAn2FTgW8+wBci6/3Ek= git.openprivacy.ca/openprivacy/libricochet-go v1.0.4/go.mod h1:yMSG1gBaP4f1U+RMZXN85d29D39OK5s8aTpyVRoH5FY= +git.openprivacy.ca/openprivacy/libricochet-go v1.0.5 h1:WAq54xI2xfRCtc3+Tw20MOVvOPmWmO6u0tSrCSt65G8= git.openprivacy.ca/openprivacy/libricochet-go v1.0.5/go.mod h1:yMSG1gBaP4f1U+RMZXN85d29D39OK5s8aTpyVRoH5FY= github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7ISrnJIXKzwaspym5BTKGx93EI= github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0= diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 7d454c7..7787685 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -6,9 +6,9 @@ import ( "cwtch.im/tapir" "cwtch.im/tapir/applications" "cwtch.im/tapir/networks/tor" + "cwtch.im/tapir/primitives" "errors" "git.openprivacy.ca/openprivacy/libricochet-go/connectivity" - "git.openprivacy.ca/openprivacy/libricochet-go/identity" "git.openprivacy.ca/openprivacy/libricochet-go/log" "github.com/golang/protobuf/proto" "golang.org/x/crypto/ed25519" @@ -21,7 +21,7 @@ type engine struct { connectionsManager *Manager // Engine Attributes - identity identity.Identity + identity primitives.Identity acn connectivity.ACN // Engine State @@ -45,14 +45,13 @@ type engine struct { // Engine (ProtocolEngine) encapsulates the logic necessary to make and receive Cwtch connections. // Note: ProtocolEngine doesn't have access to any information necessary to encrypt or decrypt GroupMessages type Engine interface { - Identity() identity.Identity ACN() connectivity.ACN EventManager() event.Manager Shutdown() } // NewProtocolEngine initializes a new engine that runs Cwtch using the given parameters -func NewProtocolEngine(identity identity.Identity, privateKey ed25519.PrivateKey, acn connectivity.ACN, eventManager event.Manager, blockedPeers []string) Engine { +func NewProtocolEngine(identity primitives.Identity, privateKey ed25519.PrivateKey, acn connectivity.ACN, eventManager event.Manager, blockedPeers []string) Engine { engine := new(engine) engine.identity = identity engine.privateKey = privateKey @@ -65,7 +64,7 @@ func NewProtocolEngine(identity identity.Identity, privateKey ed25519.PrivateKey // Init the Server running the Simple App. engine.service = new(tor.BaseOnionService) - engine.service.Init(acn, privateKey, identity) + engine.service.Init(acn, privateKey, &identity) engine.eventManager = eventManager @@ -90,10 +89,6 @@ func (e *engine) ACN() connectivity.ACN { return e.acn } -func (e *engine) Identity() identity.Identity { - return e.identity -} - func (e *engine) EventManager() event.Manager { return e.eventManager } @@ -257,7 +252,7 @@ func (e *engine) peerDisconnected(onion string) { func (e *engine) sendMessageToPeer(eventID string, onion string, context string, message []byte) error { conn, err := e.service.GetConnection(onion) if err == nil { - peerApp, ok := conn.App.(*PeerApp) + peerApp, ok := (conn.App()).(*PeerApp) if ok { peerApp.SendMessage(PeerMessage{eventID, context, message}) return nil diff --git a/protocol/connections/peerapp.go b/protocol/connections/peerapp.go index 086e5bb..b9e0800 100644 --- a/protocol/connections/peerapp.go +++ b/protocol/connections/peerapp.go @@ -11,7 +11,7 @@ import ( // PeerApp encapsulates the behaviour of a Cwtch Peer type PeerApp struct { applications.AuthApp - connection *tapir.Connection + connection tapir.Connection MessageHandler func(string, string, []byte) IsBlocked func(string) bool OnAcknowledgement func(string, string) @@ -40,7 +40,7 @@ func (pa PeerApp) NewInstance() tapir.Application { } // Init is run when the connection is first started. -func (pa *PeerApp) Init(connection *tapir.Connection) { +func (pa *PeerApp) Init(connection tapir.Connection) { // First run the Authentication App pa.AuthApp.Init(connection) @@ -49,15 +49,15 @@ func (pa *PeerApp) Init(connection *tapir.Connection) { pa.connection = connection - if pa.IsBlocked(connection.Hostname) { + if pa.IsBlocked(connection.Hostname()) { pa.connection.Close() - pa.OnClose(connection.Hostname) + pa.OnClose(connection.Hostname()) } else { - pa.OnAuth(connection.Hostname) + pa.OnAuth(connection.Hostname()) go pa.listen() } } else { - pa.OnClose(connection.Hostname) + pa.OnClose(connection.Hostname()) } } @@ -66,16 +66,16 @@ func (pa PeerApp) listen() { message := pa.connection.Expect() if len(message) == 0 { log.Errorf("0 byte read, socket has likely failed. Closing the listen goroutine") - pa.OnClose(pa.connection.Hostname) + pa.OnClose(pa.connection.Hostname()) return } var peerMessage PeerMessage err := json.Unmarshal(message, &peerMessage) if err == nil { if peerMessage.Context == event.ContextAck { - pa.OnAcknowledgement(pa.connection.Hostname, peerMessage.ID) + pa.OnAcknowledgement(pa.connection.Hostname(), peerMessage.ID) } else { - pa.MessageHandler(pa.connection.Hostname, peerMessage.Context, peerMessage.Data) + pa.MessageHandler(pa.connection.Hostname(), peerMessage.Context, peerMessage.Data) // Acknowledge the message // TODO Should this be in the ui? diff --git a/protocol/connections/peerserverconnection_test.go b/protocol/connections/peerserverconnection_test.go index c15cd20..ce89efc 100644 --- a/protocol/connections/peerserverconnection_test.go +++ b/protocol/connections/peerserverconnection_test.go @@ -1,16 +1,16 @@ package connections import ( - "crypto/rand" "cwtch.im/cwtch/event" "cwtch.im/cwtch/protocol" "cwtch.im/cwtch/server/fetch" "cwtch.im/cwtch/server/send" + "cwtch.im/tapir/primitives" "git.openprivacy.ca/openprivacy/libricochet-go" "git.openprivacy.ca/openprivacy/libricochet-go/channels" "git.openprivacy.ca/openprivacy/libricochet-go/connection" "git.openprivacy.ca/openprivacy/libricochet-go/connectivity" - "git.openprivacy.ca/openprivacy/libricochet-go/identity" + identityOld "git.openprivacy.ca/openprivacy/libricochet-go/identity" "golang.org/x/crypto/ed25519" "net" "testing" @@ -34,7 +34,7 @@ func (ts *TestServer) HandleFetchRequest() []*protocol.GroupMessage { return []*protocol.GroupMessage{{Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}, {Ciphertext: []byte("hello"), Signature: []byte{}, Spamguard: []byte{}}} } -func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity, listenChan chan bool) { +func runtestserver(t *testing.T, ts *TestServer, priv ed25519.PrivateKey, identity primitives.Identity, listenChan chan bool) { ln, _ := net.Listen("tcp", "127.0.0.1:5451") listenChan <- true conn, _ := ln.Accept() @@ -44,7 +44,9 @@ func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity, lis if err != nil { t.Errorf("Negotiate Version Error: %v", err) } - err = connection.HandleInboundConnection(rc).ProcessAuthAsV3Server(identity, ServerAuthValid) + // TODO switch from old identity to new tapir identity. + pub := identity.PublicKey() + err = connection.HandleInboundConnection(rc).ProcessAuthAsV3Server(identityOld.InitializeV3("", &priv, &pub), ServerAuthValid) if err != nil { t.Errorf("ServerAuth Error: %v", err) } @@ -65,15 +67,13 @@ func runtestserver(t *testing.T, ts *TestServer, identity identity.Identity, lis } func TestPeerServerConnection(t *testing.T) { - pub, priv, _ := ed25519.GenerateKey(rand.Reader) - - identity := identity.InitializeV3("", &priv, &pub) + identity, priv := primitives.InitializeEphemeralIdentity() t.Logf("Launching Server....\n") ts := new(TestServer) ts.Init() ts.Received = make(chan bool) listenChan := make(chan bool) - go runtestserver(t, ts, identity, listenChan) + go runtestserver(t, ts, priv, identity, listenChan) <-listenChan onionAddr := identity.Hostname()