Make e2ee optional

This commit is contained in:
Tulir Asokan 2020-05-05 20:38:58 +03:00
parent 9b2b164108
commit 0988b0590c
6 changed files with 109 additions and 38 deletions

2
go.mod
View File

@ -21,7 +21,7 @@ require (
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.8
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505163640-61b3fde37bf4
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505170605-f82efbca9632
maunium.net/go/mauview v0.1.0
maunium.net/go/tcell v0.1.0
)

2
go.sum
View File

@ -98,6 +98,8 @@ maunium.net/go/mautrix v0.2.0-beta.4.0.20200505161530-e9e56523da93 h1:kaf3m/X4Qc
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505161530-e9e56523da93/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505163640-61b3fde37bf4 h1:iGxePGtVVBYSni/GQpQHWMiISIFndJK3+GKajZJCSXo=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505163640-61b3fde37bf4/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505170605-f82efbca9632 h1:oPTIlRE5S6QMmumnxV/fZREhwMKJcbMEj67J143EY+U=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200505170605-f82efbca9632/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mauview v0.1.0 h1:x2WdkKI2zdriJuPAB0CKlwmnHGE7W9xfM5z6RgG+IIg=
maunium.net/go/mauview v0.1.0/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U=
maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM=

61
matrix/crypto.go Normal file
View File

@ -0,0 +1,61 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// +build cgo
package matrix
import (
"path/filepath"
"maunium.net/go/gomuks/debug"
"maunium.net/go/mautrix/crypto"
)
type cryptoLogger struct{}
func (c cryptoLogger) Error(message string, args ...interface{}) {
debug.Printf("[Crypto/Error] "+message, args...)
}
func (c cryptoLogger) Warn(message string, args ...interface{}) {
debug.Printf("[Crypto/Warn] "+message, args...)
}
func (c cryptoLogger) Debug(message string, args ...interface{}) {
debug.Printf("[Crypto/Debug] "+message, args...)
}
func (c cryptoLogger) Trace(message string, args ...interface{}) {
debug.Printf("[Crypto/Trace] "+message, args...)
}
func isBadEncryptError(err error) bool {
return err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession
}
func (c *Container) initCrypto() error {
cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.DataDir, "crypto.gob"))
if err != nil {
return err
}
c.crypto = crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore, c.config.Rooms)
err = c.crypto.Load()
if err != nil {
return err
}
return nil
}

View File

@ -36,7 +36,6 @@ import (
"github.com/pkg/errors"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/crypto"
"maunium.net/go/mautrix/crypto/attachment"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
@ -56,7 +55,7 @@ import (
// It is used for all Matrix calls from the UI and Matrix event handlers.
type Container struct {
client *mautrix.Client
crypto *crypto.OlmMachine
crypto CryptoInterface
syncer *GomuksSyncer
gmx ifc.Gomuks
ui ifc.GomuksUI
@ -90,22 +89,14 @@ func (log mxLogger) Debugfln(message string, args ...interface{}) {
debug.Printf("[Matrix] "+message, args...)
}
type cryptoLogger struct{}
func (c cryptoLogger) Error(message string, args ...interface{}) {
debug.Printf("[Crypto/Error] "+message, args...)
}
func (c cryptoLogger) Warn(message string, args ...interface{}) {
debug.Printf("[Crypto/Warn] "+message, args...)
}
func (c cryptoLogger) Debug(message string, args ...interface{}) {
debug.Printf("[Crypto/Debug] "+message, args...)
}
func (c cryptoLogger) Trace(message string, args ...interface{}) {
debug.Printf("[Crypto/Trace] "+message, args...)
type CryptoInterface interface {
Load() error
FlushStore() error
ProcessSyncResponse(resp *mautrix.RespSync, since string)
HandleMemberEvent(*event.Event)
DecryptMegolmEvent(*event.Event) (*event.Event, error)
EncryptMegolmEvent(id.RoomID, event.Type, event.Content) (*event.EncryptedEventContent, error)
ShareGroupSession(id.RoomID, []id.UserID) error
}
// InitClient initializes the mautrix client and connects to the homeserver specified in the config.
@ -135,12 +126,7 @@ func (c *Container) InitClient() error {
c.client.Logger = mxLogger{}
c.client.DeviceID = c.config.DeviceID
cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.DataDir, "crypto.gob"))
if err != nil {
return err
}
c.crypto = crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore, c.config.Rooms)
err = c.crypto.Load()
err = c.initCrypto()
if err != nil {
return err
}
@ -300,12 +286,14 @@ func (c *Container) Stop() {
debug.Print("Error closing history manager:", err)
}
c.history = nil
if c.crypto != nil {
debug.Print("Flushing crypto store")
err = c.crypto.CryptoStore.Flush()
err = c.crypto.FlushStore()
if err != nil {
debug.Print("Error flushing crypto store:", err)
}
}
}
}
// UpdatePushRules fetches the push notification rules from the server and stores them in the current Session object.
@ -355,12 +343,16 @@ func (c *Container) OnLogin() {
debug.Print("Initializing syncer")
c.syncer = NewGomuksSyncer(c.config.Rooms)
if c.crypto != nil {
c.syncer.OnSync(c.crypto.ProcessSyncResponse)
c.syncer.OnEventType(event.StateMember, func(source EventSource, evt *event.Event) {
c.crypto.HandleMemberEvent(evt)
})
c.syncer.OnEventType(event.EventMessage, c.HandleMessage)
c.syncer.OnEventType(event.EventEncrypted, c.HandleEncrypted)
} else {
c.syncer.OnEventType(event.EventEncrypted, c.HandleMessage)
}
c.syncer.OnEventType(event.EventMessage, c.HandleMessage)
c.syncer.OnEventType(event.EventSticker, c.HandleMessage)
c.syncer.OnEventType(event.EventReaction, c.HandleMessage)
c.syncer.OnEventType(event.EventRedaction, c.HandleRedaction)
@ -564,6 +556,8 @@ func (c *Container) HandleEncrypted(source EventSource, mxEvent *event.Event) {
evt, err := c.crypto.DecryptMegolmEvent(mxEvent)
if err != nil {
debug.Print("Failed to decrypt event:", err)
// TODO add decryption failed message instead of passing through directly
c.HandleMessage(source, mxEvent)
return
}
c.HandleMessage(source, evt)
@ -883,10 +877,10 @@ func (c *Container) SendEvent(evt *muksevt.Event) (id.EventID, error) {
_, _ = c.client.UserTyping(evt.RoomID, false, 0)
c.typing = 0
room := c.GetRoom(evt.RoomID)
if room != nil && room.Encrypted && evt.Type != event.EventReaction {
if room != nil && room.Encrypted && c.crypto != nil && evt.Type != event.EventReaction {
encrypted, err := c.crypto.EncryptMegolmEvent(evt.RoomID, evt.Type, evt.Content)
if err != nil {
if err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession {
if isBadEncryptError(err) {
return "", err
}
debug.Print("Got", err, "while trying to encrypt message, sharing group session and trying again...")
@ -1005,10 +999,11 @@ func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*muksevt.Event, e
debug.Printf("Failed to unmarshal content of event %s (type %s) by %s in %s: %v\n%s", evt.ID, evt.Type.Repr(), evt.Sender, evt.RoomID, err, string(evt.Content.VeryRaw))
}
if evt.Type == event.EventEncrypted {
if c.crypto != nil && evt.Type == event.EventEncrypted {
decrypted, err := c.crypto.DecryptMegolmEvent(evt)
if err != nil {
debug.Print("Failed to decrypt event:", err)
// TODO add decryption failed message instead of passing through directly
} else {
resp.Chunk[i] = decrypted
}

13
matrix/nocrypto.go Normal file
View File

@ -0,0 +1,13 @@
// This contains no-op stubs of the methods in crypto.go for non-cgo builds with crypto disabled.
// +build !cgo
package matrix
func isBadEncryptError(err error) bool {
return false
}
func (c *Container) initCrypto() error {
return nil
}

View File

@ -82,7 +82,7 @@ func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt
}
return ParseMessage(matrix, room, evt, displayname)
case *event.EncryptedEventContent:
return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("Encrypted messages are not yet supported", tcell.StyleDefault.Italic(true)))
return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("Decryption failed or gomuks not built with encryption support", tcell.StyleDefault.Italic(true)))
case *event.TopicEventContent, *event.RoomNameEventContent, *event.CanonicalAliasEventContent:
return ParseStateEvent(evt, displayname)
case *event.MemberEventContent: