2020-05-09 01:03:59 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// 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/>.
|
|
|
|
|
2020-08-02 12:55:02 +02:00
|
|
|
// +build cgo,!nocrypto
|
2020-05-09 01:03:59 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-05-12 21:25:55 +02:00
|
|
|
"fmt"
|
2020-10-06 21:22:04 +02:00
|
|
|
"runtime/debug"
|
2020-05-09 01:03:59 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/maulogger/v2"
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/crypto"
|
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2020-08-02 12:55:02 +02:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2020-05-09 01:03:59 +02:00
|
|
|
)
|
|
|
|
|
2020-09-24 14:25:36 +02:00
|
|
|
var NoSessionFound = crypto.NoSessionFound
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
var levelTrace = maulogger.Level{
|
|
|
|
Name: "Trace",
|
|
|
|
Severity: -10,
|
|
|
|
Color: -1,
|
|
|
|
}
|
|
|
|
|
|
|
|
type CryptoHelper struct {
|
2020-05-09 19:07:21 +02:00
|
|
|
bridge *Bridge
|
|
|
|
client *mautrix.Client
|
|
|
|
mach *crypto.OlmMachine
|
|
|
|
store *database.SQLCryptoStore
|
|
|
|
log maulogger.Logger
|
|
|
|
baseLog maulogger.Logger
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 19:25:48 +02:00
|
|
|
func NewCryptoHelper(bridge *Bridge) Crypto {
|
2020-05-09 01:03:59 +02:00
|
|
|
if !bridge.Config.Bridge.Encryption.Allow {
|
|
|
|
bridge.Log.Debugln("Bridge built with end-to-bridge encryption, but disabled in config")
|
|
|
|
return nil
|
|
|
|
} else if bridge.Config.Bridge.LoginSharedSecret == "" {
|
|
|
|
bridge.Log.Warnln("End-to-bridge encryption enabled, but login_shared_secret not set")
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-09 19:07:21 +02:00
|
|
|
baseLog := bridge.Log.Sub("Crypto")
|
|
|
|
return &CryptoHelper{
|
|
|
|
bridge: bridge,
|
|
|
|
log: baseLog.Sub("Helper"),
|
|
|
|
baseLog: baseLog,
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
2020-05-09 01:03:59 +02:00
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
func (helper *CryptoHelper) Init() error {
|
|
|
|
helper.log.Debugln("Initializing end-to-bridge encryption...")
|
2020-07-10 14:56:45 +02:00
|
|
|
|
|
|
|
helper.store = database.NewSQLCryptoStore(helper.bridge.DB, helper.bridge.AS.BotMXID(),
|
|
|
|
fmt.Sprintf("@%s:%s", helper.bridge.Config.Bridge.FormatUsername("%"), helper.bridge.AS.HomeserverDomain))
|
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
var err error
|
|
|
|
helper.client, err = helper.loginBot()
|
2020-05-09 01:03:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
helper.log.Debugln("Logged in as bridge bot with device ID", helper.client.DeviceID)
|
|
|
|
logger := &cryptoLogger{helper.baseLog}
|
|
|
|
stateStore := &cryptoStateStore{helper.bridge}
|
|
|
|
helper.mach = crypto.NewOlmMachine(helper.client, logger, helper.store, stateStore)
|
2020-08-05 13:58:46 +02:00
|
|
|
helper.mach.AllowKeyShare = helper.allowKeyShare
|
2020-05-09 01:03:59 +02:00
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
helper.client.Syncer = &cryptoSyncer{helper.mach}
|
|
|
|
helper.client.Store = &cryptoClientStore{helper.store}
|
2020-05-09 01:03:59 +02:00
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
return helper.mach.Load()
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 13:58:46 +02:00
|
|
|
func (helper *CryptoHelper) allowKeyShare(device *crypto.DeviceIdentity, info event.RequestedKeyInfo) *crypto.KeyShareRejection {
|
|
|
|
cfg := helper.bridge.Config.Bridge.Encryption.KeySharing
|
|
|
|
if !cfg.Allow {
|
|
|
|
return &crypto.KeyShareRejectNoResponse
|
|
|
|
} else if device.Trust == crypto.TrustStateBlacklisted {
|
|
|
|
return &crypto.KeyShareRejectBlacklisted
|
|
|
|
} else if device.Trust == crypto.TrustStateVerified || !cfg.RequireVerification {
|
|
|
|
portal := helper.bridge.GetPortalByMXID(info.RoomID)
|
|
|
|
if portal == nil {
|
|
|
|
helper.log.Debugfln("Rejecting key request for %s from %s/%s: room is not a portal", info.SessionID, device.UserID, device.DeviceID)
|
|
|
|
return &crypto.KeyShareRejection{Code: event.RoomKeyWithheldUnavailable, Reason: "Requested room is not a portal room"}
|
|
|
|
}
|
|
|
|
user := helper.bridge.GetUserByMXID(device.UserID)
|
2020-09-25 20:33:55 +02:00
|
|
|
if !user.Admin && !user.IsInPortal(portal.Key) {
|
2020-08-05 13:58:46 +02:00
|
|
|
helper.log.Debugfln("Rejecting key request for %s from %s/%s: user is not in portal", info.SessionID, device.UserID, device.DeviceID)
|
|
|
|
return &crypto.KeyShareRejection{Code: event.RoomKeyWithheldUnauthorized, Reason: "You're not in that portal"}
|
|
|
|
}
|
|
|
|
helper.log.Debugfln("Accepting key request for %s from %s/%s", info.SessionID, device.UserID, device.DeviceID)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return &crypto.KeyShareRejectUnverified
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
func (helper *CryptoHelper) loginBot() (*mautrix.Client, error) {
|
2020-07-10 14:56:45 +02:00
|
|
|
deviceID := helper.store.FindDeviceID()
|
2020-05-09 19:07:21 +02:00
|
|
|
if len(deviceID) > 0 {
|
|
|
|
helper.log.Debugln("Found existing device ID for bot in database:", deviceID)
|
|
|
|
}
|
2020-07-10 14:56:45 +02:00
|
|
|
client, err := mautrix.NewClient(helper.bridge.AS.HomeserverURL, "", "")
|
|
|
|
if err != nil {
|
2020-10-16 15:52:37 +02:00
|
|
|
return nil, fmt.Errorf("failed to initialize client: %w", err)
|
2020-07-10 14:56:45 +02:00
|
|
|
}
|
2020-10-16 15:52:37 +02:00
|
|
|
client.Logger = helper.baseLog.Sub("Bot")
|
|
|
|
flows, err := client.GetLoginFlows()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get supported login flows: %w", err)
|
|
|
|
}
|
|
|
|
if !flows.HasFlow(mautrix.AuthTypeAppservice) {
|
|
|
|
// TODO after synapse 1.22, turn this into an error
|
|
|
|
helper.log.Warnln("Encryption enabled in config, but homeserver does not advertise appservice login")
|
|
|
|
//return nil, fmt.Errorf("homeserver does not support appservice login")
|
|
|
|
}
|
|
|
|
// We set the API token to the AS token here to authenticate the appservice login
|
|
|
|
// It'll get overridden after the login
|
|
|
|
client.AccessToken = helper.bridge.AS.Registration.AppToken
|
2020-07-10 14:56:45 +02:00
|
|
|
resp, err := client.Login(&mautrix.ReqLogin{
|
2020-10-16 15:52:37 +02:00
|
|
|
Type: mautrix.AuthTypeAppservice,
|
2020-08-22 12:07:55 +02:00
|
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(helper.bridge.AS.BotMXID())},
|
2020-05-09 19:07:21 +02:00
|
|
|
DeviceID: deviceID,
|
2020-05-09 01:03:59 +02:00
|
|
|
InitialDeviceDisplayName: "WhatsApp Bridge",
|
2020-07-10 14:56:45 +02:00
|
|
|
StoreCredentials: true,
|
2020-05-09 01:03:59 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-16 15:52:37 +02:00
|
|
|
return nil, fmt.Errorf("failed to log in as bridge bot: %w", err)
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
2020-07-10 14:56:45 +02:00
|
|
|
if len(deviceID) == 0 {
|
|
|
|
helper.store.DeviceID = resp.DeviceID
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
func (helper *CryptoHelper) Start() {
|
|
|
|
helper.log.Debugln("Starting syncer for receiving to-device messages")
|
|
|
|
err := helper.client.Sync()
|
|
|
|
if err != nil {
|
|
|
|
helper.log.Errorln("Fatal error syncing:", err)
|
2020-10-06 20:39:28 +02:00
|
|
|
} else {
|
|
|
|
helper.log.Infoln("Bridge bot to-device syncer stopped without error")
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (helper *CryptoHelper) Stop() {
|
2020-10-06 20:39:28 +02:00
|
|
|
helper.log.Debugln("CryptoHelper.Stop() called, stopping bridge bot sync")
|
2020-05-09 19:07:21 +02:00
|
|
|
helper.client.StopSync()
|
|
|
|
}
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
func (helper *CryptoHelper) Decrypt(evt *event.Event) (*event.Event, error) {
|
|
|
|
return helper.mach.DecryptMegolmEvent(evt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (helper *CryptoHelper) Encrypt(roomID id.RoomID, evtType event.Type, content event.Content) (*event.EncryptedEventContent, error) {
|
2020-06-18 11:57:55 +02:00
|
|
|
encrypted, err := helper.mach.EncryptMegolmEvent(roomID, evtType, &content)
|
2020-05-09 01:03:59 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
helper.log.Debugfln("Got %v while encrypting event for %s, sharing group session and trying again...", err, roomID)
|
2020-05-09 19:07:21 +02:00
|
|
|
users, err := helper.store.GetRoomMembers(roomID)
|
2020-05-09 01:03:59 +02:00
|
|
|
if err != nil {
|
2020-10-05 21:38:34 +02:00
|
|
|
return nil, fmt.Errorf("failed to get room member list: %w", err)
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
|
|
|
err = helper.mach.ShareGroupSession(roomID, users)
|
|
|
|
if err != nil {
|
2020-10-05 21:38:34 +02:00
|
|
|
return nil, fmt.Errorf("failed to share group session: %w", err)
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
2020-06-18 11:57:55 +02:00
|
|
|
encrypted, err = helper.mach.EncryptMegolmEvent(roomID, evtType, &content)
|
2020-05-09 01:03:59 +02:00
|
|
|
if err != nil {
|
2020-10-05 21:38:34 +02:00
|
|
|
return nil, fmt.Errorf("failed to encrypt event after re-sharing group session: %w", err)
|
2020-05-09 01:03:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return encrypted, nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 14:25:36 +02:00
|
|
|
func (helper *CryptoHelper) WaitForSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID, timeout time.Duration) bool {
|
|
|
|
return helper.mach.WaitForSession(roomID, senderKey, sessionID, timeout)
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:32:15 +02:00
|
|
|
func (helper *CryptoHelper) ResetSession(roomID id.RoomID) {
|
|
|
|
err := helper.mach.CryptoStore.RemoveOutboundGroupSession(roomID)
|
|
|
|
if err != nil {
|
|
|
|
helper.log.Debugfln("Error manually removing outbound group session in %s: %v", roomID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) {
|
|
|
|
helper.mach.HandleMemberEvent(evt)
|
|
|
|
}
|
|
|
|
|
|
|
|
type cryptoSyncer struct {
|
|
|
|
*crypto.OlmMachine
|
|
|
|
}
|
|
|
|
|
|
|
|
func (syncer *cryptoSyncer) ProcessResponse(resp *mautrix.RespSync, since string) error {
|
2020-10-06 21:22:04 +02:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
syncer.Log.Error("Processing sync response (%s) panicked: %v\n%s", since, err, debug.Stack())
|
|
|
|
}
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
syncer.Log.Trace("Starting sync response handling (%s)", since)
|
|
|
|
syncer.ProcessSyncResponse(resp, since)
|
|
|
|
syncer.Log.Trace("Successfully handled sync response (%s)", since)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
syncer.Log.Warn("Handling sync response (%s) is taking unusually long", since)
|
|
|
|
}
|
2020-05-09 01:03:59 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (syncer *cryptoSyncer) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) {
|
|
|
|
syncer.Log.Error("Error /syncing, waiting 10 seconds: %v", err)
|
|
|
|
return 10 * time.Second, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (syncer *cryptoSyncer) GetFilterJSON(_ id.UserID) *mautrix.Filter {
|
|
|
|
everything := []event.Type{{Type: "*"}}
|
|
|
|
return &mautrix.Filter{
|
|
|
|
Presence: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
AccountData: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
Room: mautrix.RoomFilter{
|
|
|
|
IncludeLeave: false,
|
|
|
|
Ephemeral: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
AccountData: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
State: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
Timeline: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cryptoLogger struct {
|
|
|
|
int maulogger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoLogger) Error(message string, args ...interface{}) {
|
|
|
|
c.int.Errorfln(message, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoLogger) Warn(message string, args ...interface{}) {
|
|
|
|
c.int.Warnfln(message, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoLogger) Debug(message string, args ...interface{}) {
|
|
|
|
c.int.Debugfln(message, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoLogger) Trace(message string, args ...interface{}) {
|
|
|
|
c.int.Logfln(levelTrace, message, args...)
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
type cryptoClientStore struct {
|
|
|
|
int *database.SQLCryptoStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cryptoClientStore) SaveFilterID(_ id.UserID, _ string) {}
|
|
|
|
func (c cryptoClientStore) LoadFilterID(_ id.UserID) string { return "" }
|
|
|
|
func (c cryptoClientStore) SaveRoom(_ *mautrix.Room) {}
|
|
|
|
func (c cryptoClientStore) LoadRoom(_ id.RoomID) *mautrix.Room { return nil }
|
|
|
|
|
|
|
|
func (c cryptoClientStore) SaveNextBatch(_ id.UserID, nextBatchToken string) {
|
|
|
|
c.int.PutNextBatch(nextBatchToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cryptoClientStore) LoadNextBatch(_ id.UserID) string {
|
|
|
|
return c.int.GetNextBatch()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ mautrix.Storer = (*cryptoClientStore)(nil)
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
type cryptoStateStore struct {
|
|
|
|
bridge *Bridge
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:56:45 +02:00
|
|
|
var _ crypto.StateStore = (*cryptoStateStore)(nil)
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
func (c *cryptoStateStore) IsEncrypted(id id.RoomID) bool {
|
|
|
|
portal := c.bridge.GetPortalByMXID(id)
|
|
|
|
if portal != nil {
|
|
|
|
return portal.Encrypted
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptoStateStore) FindSharedRooms(id id.UserID) []id.RoomID {
|
|
|
|
return c.bridge.StateStore.FindSharedRooms(id)
|
|
|
|
}
|
2020-07-10 14:56:45 +02:00
|
|
|
|
|
|
|
func (c *cryptoStateStore) GetEncryptionEvent(id.RoomID) *event.EncryptionEventContent {
|
|
|
|
// TODO implement
|
|
|
|
return nil
|
|
|
|
}
|