@@ -20,7 +20,10 @@ import (
2020 "fmt"
2121 "time"
2222
23- "maunium.net/go/mautrix/crypto/olm"
23+ "go.mau.fi/util/exerrors"
24+
25+ "maunium.net/go/mautrix/crypto/goolm/session"
26+ "maunium.net/go/mautrix/crypto/libolm"
2427 "maunium.net/go/mautrix/id"
2528)
2629
@@ -92,38 +95,42 @@ func decryptKeyExport(passphrase string, exportData []byte) ([]ExportedSession,
9295 return sessionsJSON , nil
9396}
9497
95- func (mach * OlmMachine ) importExportedRoomKey (ctx context.Context , session ExportedSession ) (bool , error ) {
96- if session .Algorithm != id .AlgorithmMegolmV1 {
98+ func (mach * OlmMachine ) importExportedRoomKey (ctx context.Context , sess ExportedSession ) (bool , error ) {
99+ if sess .Algorithm != id .AlgorithmMegolmV1 {
97100 return false , ErrInvalidExportedAlgorithm
98101 }
99102
100- igsInternal , err := olm .InboundGroupSessionImport ([]byte (session .SessionKey ))
103+ igsInternal , err := libolm .InboundGroupSessionImport ([]byte (sess .SessionKey ))
101104 if err != nil {
102105 return false , fmt .Errorf ("failed to import session: %w" , err )
103- } else if igsInternal .ID () != session .SessionID {
106+ } else if igsInternal .ID () != sess .SessionID {
104107 return false , ErrMismatchingExportedSessionID
105108 }
106109 igs := & InboundGroupSession {
107- Internal : igsInternal ,
108- SigningKey : session .SenderClaimedKeys .Ed25519 ,
109- SenderKey : session .SenderKey ,
110- RoomID : session .RoomID ,
110+ InternalLibolm : igsInternal ,
111+ InternalGoolm : exerrors .Must (session .NewMegolmInboundSessionFromExport ([]byte (sess .SessionKey ))),
112+ SigningKey : sess .SenderClaimedKeys .Ed25519 ,
113+ SenderKey : sess .SenderKey ,
114+ RoomID : sess .RoomID ,
111115 // TODO should we add something here to mark the signing key as unverified like key requests do?
112- ForwardingChains : session .ForwardingChains ,
116+ ForwardingChains : sess .ForwardingChains ,
113117
114118 ReceivedAt : time .Now ().UTC (),
115119 }
116120 existingIGS , _ := mach .CryptoStore .GetGroupSession (ctx , igs .RoomID , igs .ID ())
117- firstKnownIndex := igs .Internal .FirstKnownIndex ()
118- if existingIGS != nil && existingIGS .Internal .FirstKnownIndex () <= firstKnownIndex {
121+ firstKnownIndex := igs .InternalLibolm .FirstKnownIndex ()
122+ if firstKnownIndex != igs .InternalGoolm .FirstKnownIndex () {
123+ panic ("indexes different" )
124+ }
125+ if existingIGS != nil && existingIGS .InternalLibolm .FirstKnownIndex () <= firstKnownIndex {
119126 // We already have an equivalent or better session in the store, so don't override it.
120127 return false , nil
121128 }
122129 err = mach .CryptoStore .PutGroupSession (ctx , igs )
123130 if err != nil {
124131 return false , fmt .Errorf ("failed to store imported session: %w" , err )
125132 }
126- mach .markSessionReceived (ctx , session .RoomID , igs .ID (), firstKnownIndex )
133+ mach .markSessionReceived (ctx , sess .RoomID , igs .ID (), firstKnownIndex )
127134 return true , nil
128135}
129136
0 commit comments