2024-08-01 19:17:05 +00:00
|
|
|
//go:build e2ee
|
2021-07-16 14:41:08 +00:00
|
|
|
// +build e2ee
|
|
|
|
|
2021-07-16 13:33:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-10 18:30:14 +00:00
|
|
|
"context"
|
2021-07-16 13:33:51 +00:00
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"maunium.net/go/mautrix/crypto/cryptohelper"
|
2021-07-16 13:33:51 +00:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
2021-07-16 16:11:17 +00:00
|
|
|
type Crypto struct {
|
2024-08-10 18:30:14 +00:00
|
|
|
helper *cryptohelper.CryptoHelper
|
2021-07-16 16:11:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 16:55:12 +00:00
|
|
|
func BuildTagsE2EE() {
|
|
|
|
buildTags = append(buildTags, "e2ee")
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:41:08 +00:00
|
|
|
func MatrixE2EE() bool { return true }
|
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
func InitMatrixCrypto(d *MatrixDaemon) error {
|
2021-07-16 13:33:51 +00:00
|
|
|
d.Encryption = d.app.config.Section("matrix").Key("encryption").MustBool(false)
|
|
|
|
if !d.Encryption {
|
2024-08-10 18:30:14 +00:00
|
|
|
// return fmt.Errorf("encryption disabled")
|
|
|
|
return nil
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
|
2021-07-16 13:33:51 +00:00
|
|
|
dbPath := d.app.config.Section("files").Key("matrix_sql").String()
|
2024-08-10 18:30:14 +00:00
|
|
|
var err error
|
|
|
|
d.crypto = &Crypto{}
|
|
|
|
d.crypto.helper, err = cryptohelper.NewCryptoHelper(d.bot, []byte("jfa-go"), dbPath)
|
2021-07-16 13:33:51 +00:00
|
|
|
if err != nil {
|
2024-08-10 18:30:14 +00:00
|
|
|
return err
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
|
|
|
|
err = d.crypto.helper.Init(context.TODO())
|
2021-07-16 13:33:51 +00:00
|
|
|
if err != nil {
|
2024-08-10 18:30:14 +00:00
|
|
|
return err
|
2021-07-16 16:11:17 +00:00
|
|
|
}
|
2021-07-16 13:33:51 +00:00
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
d.bot.Crypto = d.crypto.helper
|
2021-07-16 13:33:51 +00:00
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
d.Encryption = true
|
|
|
|
return nil
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
func EncryptRoom(d *MatrixDaemon, roomID id.RoomID) error {
|
2021-07-16 13:33:51 +00:00
|
|
|
if !d.Encryption {
|
2024-08-10 18:30:14 +00:00
|
|
|
return nil
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
_, err := d.bot.SendStateEvent(context.TODO(), roomID, event.StateEncryption, "", event.EncryptionEventContent{
|
2021-07-16 13:33:51 +00:00
|
|
|
Algorithm: id.AlgorithmMegolmV1,
|
|
|
|
RotationPeriodMillis: 7 * 24 * 60 * 60 * 1000,
|
|
|
|
RotationPeriodMessages: 100,
|
|
|
|
})
|
2024-08-10 18:30:14 +00:00
|
|
|
return err
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|