Debian Codegraph @

matrix/crypto.go

raw symbols SCIP
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 //go:build cgo
18
19 package matrix
20
21 import (
22 "database/sql"
23 "fmt"
24 "os"
25 "path/filepath"
26
27 _ "github.com/mattn/go-sqlite3"
28
29 "maunium.net/go/mautrix/crypto"
30
31 "maunium.net/go/gomuks/debug"
32 )
33
34 type cryptoLogger struct {
35 prefix string
36 }
37
38 func (c cryptoLogger) Error(message string, args ...interface{}) {
39 debug.Printf(fmt.Sprintf("[%s/Error] %s", c.prefix, message), args...)
40 }
41
42 func (c cryptoLogger) Warn(message string, args ...interface{}) {
43 debug.Printf(fmt.Sprintf("[%s/Warn] %s", c.prefix, message), args...)
44 }
45
46 func (c cryptoLogger) Debug(message string, args ...interface{}) {
47 debug.Printf(fmt.Sprintf("[%s/Debug] %s", c.prefix, message), args...)
48 }
49
50 func (c cryptoLogger) Trace(message string, args ...interface{}) {
51 debug.Printf(fmt.Sprintf("[%s/Trace] %s", c.prefix, message), args...)
52 }
53
54 func isBadEncryptError(err error) bool {
55 return err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession
56 }
57
58 func (c *Container) initCrypto() error {
59 var cryptoStore crypto.Store
60 var err error
61 legacyStorePath := filepath.Join(c.config.DataDir, "crypto.gob")
62 if _, err = os.Stat(legacyStorePath); err == nil {
63 debug.Printf("Using legacy crypto store as %s exists", legacyStorePath)
64 cryptoStore, err = crypto.NewGobStore(legacyStorePath)
65 if err != nil {
66 return fmt.Errorf("file open: %w", err)
67 }
68 } else {
69 debug.Printf("Using SQLite crypto store")
70 newStorePath := filepath.Join(c.config.DataDir, "crypto.db")
71 db, err := sql.Open("sqlite3", newStorePath)
72 if err != nil {
73 return fmt.Errorf("sql open: %w", err)
74 }
75 accID := fmt.Sprintf("%s/%s", c.config.UserID.String(), c.config.DeviceID)
76 sqlStore := crypto.NewSQLCryptoStore(db, "sqlite3", accID, c.config.DeviceID, []byte("fi.mau.gomuks"), cryptoLogger{"Crypto/DB"})
77 err = sqlStore.CreateTables()
78 if err != nil {
79 return fmt.Errorf("create table: %w", err)
80 }
81 cryptoStore = sqlStore
82 }
83 crypt := crypto.NewOlmMachine(c.client, cryptoLogger{"Crypto"}, cryptoStore, c.config.Rooms)
84 crypt.AllowUnverifiedDevices = !c.config.SendToVerifiedOnly
85 c.crypto = crypt
86 err = c.crypto.Load()
87 if err != nil {
88 return fmt.Errorf("failed to create olm machine: %w", err)
89 }
90 return nil
91 }
92
93 func (c *Container) cryptoOnLogin() {
94 sqlStore, ok := c.crypto.(*crypto.OlmMachine).CryptoStore.(*crypto.SQLCryptoStore)
95 if !ok {
96 return
97 }
98 sqlStore.DeviceID = c.config.DeviceID
99 sqlStore.AccountID = fmt.Sprintf("%s/%s", c.config.UserID.String(), c.config.DeviceID)
100 }