gomuks/matrix/crypto.go

101 lines
3.2 KiB
Go
Raw Normal View History

2020-05-05 19:38:58 +02:00
// 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/>.
2022-04-15 11:53:09 +02:00
//go:build cgo
2020-05-05 19:38:58 +02:00
package matrix
import (
2021-02-08 21:42:40 +01:00
"database/sql"
"fmt"
2021-02-08 21:42:40 +01:00
"os"
2020-05-05 19:38:58 +02:00
"path/filepath"
2021-02-08 21:42:40 +01:00
_ "github.com/mattn/go-sqlite3"
2020-05-05 19:38:58 +02:00
"maunium.net/go/mautrix/crypto"
2020-05-05 20:15:53 +02:00
"maunium.net/go/gomuks/debug"
2020-05-05 19:38:58 +02:00
)
2021-02-08 21:42:40 +01:00
type cryptoLogger struct {
prefix string
}
2020-05-05 19:38:58 +02:00
func (c cryptoLogger) Error(message string, args ...interface{}) {
2021-02-08 21:42:40 +01:00
debug.Printf(fmt.Sprintf("[%s/Error] %s", c.prefix, message), args...)
2020-05-05 19:38:58 +02:00
}
func (c cryptoLogger) Warn(message string, args ...interface{}) {
2021-02-08 21:42:40 +01:00
debug.Printf(fmt.Sprintf("[%s/Warn] %s", c.prefix, message), args...)
2020-05-05 19:38:58 +02:00
}
func (c cryptoLogger) Debug(message string, args ...interface{}) {
2021-02-08 21:42:40 +01:00
debug.Printf(fmt.Sprintf("[%s/Debug] %s", c.prefix, message), args...)
2020-05-05 19:38:58 +02:00
}
func (c cryptoLogger) Trace(message string, args ...interface{}) {
2021-02-08 21:42:40 +01:00
debug.Printf(fmt.Sprintf("[%s/Trace] %s", c.prefix, message), args...)
2020-05-05 19:38:58 +02:00
}
func isBadEncryptError(err error) bool {
return err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession
}
func (c *Container) initCrypto() error {
2021-02-08 21:42:40 +01:00
var cryptoStore crypto.Store
var err error
legacyStorePath := filepath.Join(c.config.DataDir, "crypto.gob")
if _, err = os.Stat(legacyStorePath); err == nil {
debug.Printf("Using legacy crypto store as %s exists", legacyStorePath)
cryptoStore, err = crypto.NewGobStore(legacyStorePath)
if err != nil {
return fmt.Errorf("file open: %w", err)
}
} else {
debug.Printf("Using SQLite crypto store")
newStorePath := filepath.Join(c.config.DataDir, "crypto.db")
db, err := sql.Open("sqlite3", newStorePath)
if err != nil {
return fmt.Errorf("sql open: %w", err)
}
accID := fmt.Sprintf("%s/%s", c.config.UserID.String(), c.config.DeviceID)
sqlStore := crypto.NewSQLCryptoStore(db, "sqlite3", accID, c.config.DeviceID, []byte("fi.mau.gomuks"), cryptoLogger{"Crypto/DB"})
err = sqlStore.CreateTables()
if err != nil {
return fmt.Errorf("create table: %w", err)
}
cryptoStore = sqlStore
2020-05-05 19:38:58 +02:00
}
2021-02-08 21:42:40 +01:00
crypt := crypto.NewOlmMachine(c.client, cryptoLogger{"Crypto"}, cryptoStore, c.config.Rooms)
crypt.AllowUnverifiedDevices = !c.config.SendToVerifiedOnly
c.crypto = crypt
2020-05-05 19:38:58 +02:00
err = c.crypto.Load()
if err != nil {
return fmt.Errorf("failed to create olm machine: %w", err)
2020-05-05 19:38:58 +02:00
}
return nil
}
2021-02-08 21:42:40 +01:00
func (c *Container) cryptoOnLogin() {
sqlStore, ok := c.crypto.(*crypto.OlmMachine).CryptoStore.(*crypto.SQLCryptoStore)
if !ok {
return
}
sqlStore.DeviceID = c.config.DeviceID
sqlStore.AccountID = fmt.Sprintf("%s/%s", c.config.UserID.String(), c.config.DeviceID)
}