diff --git a/crypto/decryptolm.go b/crypto/decryptolm.go index f90d0866..9cd5c336 100644 --- a/crypto/decryptolm.go +++ b/crypto/decryptolm.go @@ -132,7 +132,7 @@ func (mach *OlmMachine) tryDecryptOlmCiphertext(sender id.UserID, senderKey id.S go mach.unwedgeDevice(sender, senderKey) return nil, fmt.Errorf("failed to create new session from prekey message: %w", err) } - mach.Log.Debug("Created inbound olm session %s for %s/%s", session.ID(), sender, senderKey) + mach.Log.Debug("Created inbound olm session %s for %s/%s: %s", session.ID(), sender, senderKey, session.Describe()) plaintext, err = session.Decrypt(ciphertext, olmType) if err != nil { @@ -151,6 +151,7 @@ func (mach *OlmMachine) tryDecryptOlmCiphertextWithExistingSession(senderKey id. if err != nil { return nil, fmt.Errorf("failed to get session for %s: %w", senderKey, err) } + for _, session := range sessions { if olmType == id.OlmMsgTypePreKey { matches, err := session.Internal.MatchesInboundSession(ciphertext) @@ -160,6 +161,7 @@ func (mach *OlmMachine) tryDecryptOlmCiphertextWithExistingSession(senderKey id. continue } } + mach.Log.Trace("Trying to decrypt olm message from %s with session %s: %s", senderKey, session.ID(), session.Describe()) plaintext, err := session.Decrypt(ciphertext, olmType) if err != nil { if olmType == id.OlmMsgTypePreKey { @@ -170,6 +172,7 @@ func (mach *OlmMachine) tryDecryptOlmCiphertextWithExistingSession(senderKey id. if err != nil { mach.Log.Warn("Failed to update olm session in crypto store after decrypting: %v", err) } + mach.Log.Trace("Decrypted olm message from %s with session %s", senderKey, session.ID()) return plaintext, nil } } diff --git a/crypto/encryptolm.go b/crypto/encryptolm.go index 5b39d6c1..5591848c 100644 --- a/crypto/encryptolm.go +++ b/crypto/encryptolm.go @@ -30,6 +30,7 @@ func (mach *OlmMachine) encryptOlmEvent(session *OlmSession, recipient *DeviceId if err != nil { panic(err) } + mach.Log.Trace("Encrypting olm message for %s with session %s: %s", recipient.IdentityKey, session.ID(), session.Describe()) msgType, ciphertext := session.Encrypt(plaintext) err = mach.CryptoStore.UpdateSession(recipient.IdentityKey, session) if err != nil { diff --git a/crypto/olm/session.go b/crypto/olm/session.go index e7062c48..662de19b 100644 --- a/crypto/olm/session.go +++ b/crypto/olm/session.go @@ -2,6 +2,7 @@ package olm // #cgo LDFLAGS: -lolm -lstdc++ // #include +// #include import "C" import ( @@ -328,3 +329,17 @@ func (s *Session) Decrypt(message string, msgType id.OlmMsgType) ([]byte, error) } return plaintext[:r], nil } + +// https://gitlab.matrix.org/matrix-org/olm/-/blob/3.2.8/include/olm/olm.h#L392-393 +const maxDescribeSize = 600 + +// Describe generates a string describing the internal state of an olm session for debugging and logging purposes. +func (s *Session) Describe() string { + desc := (*C.char)(C.malloc(C.size_t(maxDescribeSize))) + defer C.free(unsafe.Pointer(desc)) + C.olm_session_describe( + (*C.OlmSession)(s.int), + desc, + C.size_t(maxDescribeSize)) + return C.GoString(desc) +} diff --git a/crypto/sessions.go b/crypto/sessions.go index 1cfc8296..baad54c1 100644 --- a/crypto/sessions.go +++ b/crypto/sessions.go @@ -51,6 +51,10 @@ func (session *OlmSession) ID() id.SessionID { return session.id } +func (session *OlmSession) Describe() string { + return session.Internal.Describe() +} + func wrapSession(session *olm.Session) *OlmSession { return &OlmSession{ Internal: *session,