| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package crypto |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"time" |
| 12 |
|
| 13 |
"maunium.net/go/mautrix/crypto/olm" |
| 14 |
"maunium.net/go/mautrix/event" |
| 15 |
|
| 16 |
"maunium.net/go/mautrix/id" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
SessionNotShared = errors.New("session has not been shared") |
| 21 |
SessionExpired = errors.New("session has expired") |
| 22 |
) |
| 23 |
|
| 24 |
// OlmSessionList is a list of OlmSessions. |
| 25 |
// It implements sort.Interface so that the session with recent successful decryptions comes first. |
| 26 |
type OlmSessionList []*OlmSession |
| 27 |
|
| 28 |
func (o OlmSessionList) Len() int { |
| 29 |
return len(o) |
| 30 |
} |
| 31 |
|
| 32 |
func (o OlmSessionList) Less(i, j int) bool { |
| 33 |
return o[i].LastDecryptedTime.After(o[j].LastEncryptedTime) |
| 34 |
} |
| 35 |
|
| 36 |
func (o OlmSessionList) Swap(i, j int) { |
| 37 |
o[i], o[j] = o[j], o[i] |
| 38 |
} |
| 39 |
|
| 40 |
type OlmSession struct { |
| 41 |
Internal olm.Session |
| 42 |
ExpirationMixin |
| 43 |
id id.SessionID |
| 44 |
} |
| 45 |
|
| 46 |
func (session *OlmSession) ID() id.SessionID { |
| 47 |
if session.id == "" { |
| 48 |
session.id = session.Internal.ID() |
| 49 |
} |
| 50 |
return session.id |
| 51 |
} |
| 52 |
|
| 53 |
func (session *OlmSession) Describe() string { |
| 54 |
return session.Internal.Describe() |
| 55 |
} |
| 56 |
|
| 57 |
func wrapSession(session *olm.Session) *OlmSession { |
| 58 |
return &OlmSession{ |
| 59 |
Internal: *session, |
| 60 |
ExpirationMixin: ExpirationMixin{ |
| 61 |
TimeMixin: TimeMixin{ |
| 62 |
CreationTime: time.Now(), |
| 63 |
LastEncryptedTime: time.Now(), |
| 64 |
LastDecryptedTime: time.Now(), |
| 65 |
}, |
| 66 |
}, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func (account *OlmAccount) NewInboundSessionFrom(senderKey id.Curve25519, ciphertext string) (*OlmSession, error) { |
| 71 |
session, err := account.Internal.NewInboundSessionFrom(senderKey, ciphertext) |
| 72 |
if err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
_ = account.Internal.RemoveOneTimeKeys(session) |
| 76 |
return wrapSession(session), nil |
| 77 |
} |
| 78 |
|
| 79 |
func (session *OlmSession) Encrypt(plaintext []byte) (id.OlmMsgType, []byte) { |
| 80 |
session.LastEncryptedTime = time.Now() |
| 81 |
return session.Internal.Encrypt(plaintext) |
| 82 |
} |
| 83 |
|
| 84 |
func (session *OlmSession) Decrypt(ciphertext string, msgType id.OlmMsgType) ([]byte, error) { |
| 85 |
msg, err := session.Internal.Decrypt(ciphertext, msgType) |
| 86 |
if err == nil { |
| 87 |
session.LastDecryptedTime = time.Now() |
| 88 |
} |
| 89 |
return msg, err |
| 90 |
} |
| 91 |
|
| 92 |
type InboundGroupSession struct { |
| 93 |
Internal olm.InboundGroupSession |
| 94 |
|
| 95 |
SigningKey id.Ed25519 |
| 96 |
SenderKey id.Curve25519 |
| 97 |
RoomID id.RoomID |
| 98 |
|
| 99 |
ForwardingChains []string |
| 100 |
|
| 101 |
id id.SessionID |
| 102 |
} |
| 103 |
|
| 104 |
func NewInboundGroupSession(senderKey id.SenderKey, signingKey id.Ed25519, roomID id.RoomID, sessionKey string) (*InboundGroupSession, error) { |
| 105 |
igs, err := olm.NewInboundGroupSession([]byte(sessionKey)) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
return &InboundGroupSession{ |
| 110 |
Internal: *igs, |
| 111 |
SigningKey: signingKey, |
| 112 |
SenderKey: senderKey, |
| 113 |
RoomID: roomID, |
| 114 |
ForwardingChains: nil, |
| 115 |
}, nil |
| 116 |
} |
| 117 |
|
| 118 |
func (igs *InboundGroupSession) ID() id.SessionID { |
| 119 |
if igs.id == "" { |
| 120 |
igs.id = igs.Internal.ID() |
| 121 |
} |
| 122 |
return igs.id |
| 123 |
} |
| 124 |
|
| 125 |
type OGSState int |
| 126 |
|
| 127 |
const ( |
| 128 |
OGSNotShared OGSState = iota |
| 129 |
OGSAlreadyShared |
| 130 |
OGSIgnored |
| 131 |
) |
| 132 |
|
| 133 |
type UserDevice struct { |
| 134 |
UserID id.UserID |
| 135 |
DeviceID id.DeviceID |
| 136 |
} |
| 137 |
|
| 138 |
type OutboundGroupSession struct { |
| 139 |
Internal olm.OutboundGroupSession |
| 140 |
|
| 141 |
ExpirationMixin |
| 142 |
MaxMessages int |
| 143 |
MessageCount int |
| 144 |
|
| 145 |
Users map[UserDevice]OGSState |
| 146 |
RoomID id.RoomID |
| 147 |
Shared bool |
| 148 |
|
| 149 |
id id.SessionID |
| 150 |
content *event.RoomKeyEventContent |
| 151 |
} |
| 152 |
|
| 153 |
func NewOutboundGroupSession(roomID id.RoomID, encryptionContent *event.EncryptionEventContent) *OutboundGroupSession { |
| 154 |
ogs := &OutboundGroupSession{ |
| 155 |
Internal: *olm.NewOutboundGroupSession(), |
| 156 |
ExpirationMixin: ExpirationMixin{ |
| 157 |
TimeMixin: TimeMixin{ |
| 158 |
CreationTime: time.Now(), |
| 159 |
LastEncryptedTime: time.Now(), |
| 160 |
}, |
| 161 |
MaxAge: 7 * 24 * time.Hour, |
| 162 |
}, |
| 163 |
MaxMessages: 100, |
| 164 |
Shared: false, |
| 165 |
Users: make(map[UserDevice]OGSState), |
| 166 |
RoomID: roomID, |
| 167 |
} |
| 168 |
if encryptionContent != nil { |
| 169 |
if encryptionContent.RotationPeriodMillis != 0 { |
| 170 |
ogs.MaxAge = time.Duration(encryptionContent.RotationPeriodMillis) * time.Millisecond |
| 171 |
} |
| 172 |
if encryptionContent.RotationPeriodMessages != 0 { |
| 173 |
ogs.MaxMessages = encryptionContent.RotationPeriodMessages |
| 174 |
} |
| 175 |
} |
| 176 |
return ogs |
| 177 |
} |
| 178 |
|
| 179 |
func (ogs *OutboundGroupSession) ShareContent() event.Content { |
| 180 |
if ogs.content == nil { |
| 181 |
ogs.content = &event.RoomKeyEventContent{ |
| 182 |
Algorithm: id.AlgorithmMegolmV1, |
| 183 |
RoomID: ogs.RoomID, |
| 184 |
SessionID: ogs.ID(), |
| 185 |
SessionKey: ogs.Internal.Key(), |
| 186 |
} |
| 187 |
} |
| 188 |
return event.Content{Parsed: ogs.content} |
| 189 |
} |
| 190 |
|
| 191 |
func (ogs *OutboundGroupSession) ID() id.SessionID { |
| 192 |
if ogs.id == "" { |
| 193 |
ogs.id = ogs.Internal.ID() |
| 194 |
} |
| 195 |
return ogs.id |
| 196 |
} |
| 197 |
|
| 198 |
func (ogs *OutboundGroupSession) Expired() bool { |
| 199 |
return ogs.MessageCount >= ogs.MaxMessages || ogs.ExpirationMixin.Expired() |
| 200 |
} |
| 201 |
|
| 202 |
func (ogs *OutboundGroupSession) Encrypt(plaintext []byte) ([]byte, error) { |
| 203 |
if !ogs.Shared { |
| 204 |
return nil, SessionNotShared |
| 205 |
} else if ogs.Expired() { |
| 206 |
return nil, SessionExpired |
| 207 |
} |
| 208 |
ogs.MessageCount++ |
| 209 |
ogs.LastEncryptedTime = time.Now() |
| 210 |
return ogs.Internal.Encrypt(plaintext), nil |
| 211 |
} |
| 212 |
|
| 213 |
type TimeMixin struct { |
| 214 |
CreationTime time.Time |
| 215 |
LastEncryptedTime time.Time |
| 216 |
LastDecryptedTime time.Time |
| 217 |
} |
| 218 |
|
| 219 |
type ExpirationMixin struct { |
| 220 |
TimeMixin |
| 221 |
MaxAge time.Duration |
| 222 |
} |
| 223 |
|
| 224 |
func (exp *ExpirationMixin) Expired() bool { |
| 225 |
if exp.MaxAge == 0 { |
| 226 |
return false |
| 227 |
} |
| 228 |
return exp.CreationTime.Add(exp.MaxAge).Before(time.Now()) |
| 229 |
} |