| 1 |
package olm |
| 2 |
|
| 3 |
// #cgo LDFLAGS: -lolm -lstdc++ |
| 4 |
// #include <olm/olm.h> |
| 5 |
// #include <stdlib.h> |
| 6 |
// #include <stdio.h> |
| 7 |
// void olm_session_describe(OlmSession * session, char *buf, size_t buflen) __attribute__((weak)); |
| 8 |
// void meowlm_session_describe(OlmSession * session, char *buf, size_t buflen) { |
| 9 |
// if (olm_session_describe) { |
| 10 |
// olm_session_describe(session, buf, buflen); |
| 11 |
// } else { |
| 12 |
// sprintf(buf, "olm_session_describe not supported"); |
| 13 |
// } |
| 14 |
// } |
| 15 |
import "C" |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/rand" |
| 19 |
"unsafe" |
| 20 |
|
| 21 |
"maunium.net/go/mautrix/id" |
| 22 |
) |
| 23 |
|
| 24 |
// Session stores an end to end encrypted messaging session. |
| 25 |
type Session struct { |
| 26 |
int *C.OlmSession |
| 27 |
mem []byte |
| 28 |
} |
| 29 |
|
| 30 |
// sessionSize is the size of a session object in bytes. |
| 31 |
func sessionSize() uint { |
| 32 |
return uint(C.olm_session_size()) |
| 33 |
} |
| 34 |
|
| 35 |
// SessionFromPickled loads a Session from a pickled base64 string. Decrypts |
| 36 |
// the Session using the supplied key. Returns error on failure. If the key |
| 37 |
// doesn't match the one used to encrypt the Session then the error will be |
| 38 |
// "BAD_SESSION_KEY". If the base64 couldn't be decoded then the error will be |
| 39 |
// "INVALID_BASE64". |
| 40 |
func SessionFromPickled(pickled, key []byte) (*Session, error) { |
| 41 |
if len(pickled) == 0 { |
| 42 |
return nil, EmptyInput |
| 43 |
} |
| 44 |
s := NewBlankSession() |
| 45 |
return s, s.Unpickle(pickled, key) |
| 46 |
} |
| 47 |
|
| 48 |
func NewBlankSession() *Session { |
| 49 |
memory := make([]byte, sessionSize()) |
| 50 |
return &Session{ |
| 51 |
int: C.olm_session(unsafe.Pointer(&memory[0])), |
| 52 |
mem: memory, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// lastError returns an error describing the most recent error to happen to a |
| 57 |
// session. |
| 58 |
func (s *Session) lastError() error { |
| 59 |
return convertError(C.GoString(C.olm_session_last_error((*C.OlmSession)(s.int)))) |
| 60 |
} |
| 61 |
|
| 62 |
// Clear clears the memory used to back this Session. |
| 63 |
func (s *Session) Clear() error { |
| 64 |
r := C.olm_clear_session((*C.OlmSession)(s.int)) |
| 65 |
if r == errorVal() { |
| 66 |
return s.lastError() |
| 67 |
} |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
// pickleLen returns the number of bytes needed to store a session. |
| 72 |
func (s *Session) pickleLen() uint { |
| 73 |
return uint(C.olm_pickle_session_length((*C.OlmSession)(s.int))) |
| 74 |
} |
| 75 |
|
| 76 |
// createOutboundRandomLen returns the number of random bytes needed to create |
| 77 |
// an outbound session. |
| 78 |
func (s *Session) createOutboundRandomLen() uint { |
| 79 |
return uint(C.olm_create_outbound_session_random_length((*C.OlmSession)(s.int))) |
| 80 |
} |
| 81 |
|
| 82 |
// idLen returns the length of the buffer needed to return the id for this |
| 83 |
// session. |
| 84 |
func (s *Session) idLen() uint { |
| 85 |
return uint(C.olm_session_id_length((*C.OlmSession)(s.int))) |
| 86 |
} |
| 87 |
|
| 88 |
// encryptRandomLen returns the number of random bytes needed to encrypt the |
| 89 |
// next message. |
| 90 |
func (s *Session) encryptRandomLen() uint { |
| 91 |
return uint(C.olm_encrypt_random_length((*C.OlmSession)(s.int))) |
| 92 |
} |
| 93 |
|
| 94 |
// encryptMsgLen returns the size of the next message in bytes for the given |
| 95 |
// number of plain-text bytes. |
| 96 |
func (s *Session) encryptMsgLen(plainTextLen int) uint { |
| 97 |
return uint(C.olm_encrypt_message_length((*C.OlmSession)(s.int), C.size_t(plainTextLen))) |
| 98 |
} |
| 99 |
|
| 100 |
// decryptMaxPlaintextLen returns the maximum number of bytes of plain-text a |
| 101 |
// given message could decode to. The actual size could be different due to |
| 102 |
// padding. Returns error on failure. If the message base64 couldn't be |
| 103 |
// decoded then the error will be "INVALID_BASE64". If the message is for an |
| 104 |
// unsupported version of the protocol then the error will be |
| 105 |
// "BAD_MESSAGE_VERSION". If the message couldn't be decoded then the error |
| 106 |
// will be "BAD_MESSAGE_FORMAT". |
| 107 |
func (s *Session) decryptMaxPlaintextLen(message string, msgType id.OlmMsgType) (uint, error) { |
| 108 |
if len(message) == 0 { |
| 109 |
return 0, EmptyInput |
| 110 |
} |
| 111 |
r := C.olm_decrypt_max_plaintext_length( |
| 112 |
(*C.OlmSession)(s.int), |
| 113 |
C.size_t(msgType), |
| 114 |
unsafe.Pointer(C.CString(message)), |
| 115 |
C.size_t(len(message))) |
| 116 |
if r == errorVal() { |
| 117 |
return 0, s.lastError() |
| 118 |
} |
| 119 |
return uint(r), nil |
| 120 |
} |
| 121 |
|
| 122 |
// Pickle returns a Session as a base64 string. Encrypts the Session using the |
| 123 |
// supplied key. |
| 124 |
func (s *Session) Pickle(key []byte) []byte { |
| 125 |
if len(key) == 0 { |
| 126 |
panic(NoKeyProvided) |
| 127 |
} |
| 128 |
pickled := make([]byte, s.pickleLen()) |
| 129 |
r := C.olm_pickle_session( |
| 130 |
(*C.OlmSession)(s.int), |
| 131 |
unsafe.Pointer(&key[0]), |
| 132 |
C.size_t(len(key)), |
| 133 |
unsafe.Pointer(&pickled[0]), |
| 134 |
C.size_t(len(pickled))) |
| 135 |
if r == errorVal() { |
| 136 |
panic(s.lastError()) |
| 137 |
} |
| 138 |
return pickled[:r] |
| 139 |
} |
| 140 |
|
| 141 |
func (s *Session) Unpickle(pickled, key []byte) error { |
| 142 |
if len(key) == 0 { |
| 143 |
return NoKeyProvided |
| 144 |
} |
| 145 |
r := C.olm_unpickle_session( |
| 146 |
(*C.OlmSession)(s.int), |
| 147 |
unsafe.Pointer(&key[0]), |
| 148 |
C.size_t(len(key)), |
| 149 |
unsafe.Pointer(&pickled[0]), |
| 150 |
C.size_t(len(pickled))) |
| 151 |
if r == errorVal() { |
| 152 |
return s.lastError() |
| 153 |
} |
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
func (s *Session) GobEncode() ([]byte, error) { |
| 158 |
pickled := s.Pickle(pickleKey) |
| 159 |
length := unpaddedBase64.DecodedLen(len(pickled)) |
| 160 |
rawPickled := make([]byte, length) |
| 161 |
_, err := unpaddedBase64.Decode(rawPickled, pickled) |
| 162 |
return rawPickled, err |
| 163 |
} |
| 164 |
|
| 165 |
func (s *Session) GobDecode(rawPickled []byte) error { |
| 166 |
if s == nil || s.int == nil { |
| 167 |
*s = *NewBlankSession() |
| 168 |
} |
| 169 |
length := unpaddedBase64.EncodedLen(len(rawPickled)) |
| 170 |
pickled := make([]byte, length) |
| 171 |
unpaddedBase64.Encode(pickled, rawPickled) |
| 172 |
return s.Unpickle(pickled, pickleKey) |
| 173 |
} |
| 174 |
|
| 175 |
func (s *Session) MarshalJSON() ([]byte, error) { |
| 176 |
pickled := s.Pickle(pickleKey) |
| 177 |
quotes := make([]byte, len(pickled)+2) |
| 178 |
quotes[0] = '"' |
| 179 |
quotes[len(quotes)-1] = '"' |
| 180 |
copy(quotes[1:len(quotes)-1], pickled) |
| 181 |
return quotes, nil |
| 182 |
} |
| 183 |
|
| 184 |
func (s *Session) UnmarshalJSON(data []byte) error { |
| 185 |
if len(data) == 0 || len(data) == 0 || data[0] != '"' || data[len(data)-1] != '"' { |
| 186 |
return InputNotJSONString |
| 187 |
} |
| 188 |
if s == nil || s.int == nil { |
| 189 |
*s = *NewBlankSession() |
| 190 |
} |
| 191 |
return s.Unpickle(data[1:len(data)-1], pickleKey) |
| 192 |
} |
| 193 |
|
| 194 |
// Id returns an identifier for this Session. Will be the same for both ends |
| 195 |
// of the conversation. |
| 196 |
func (s *Session) ID() id.SessionID { |
| 197 |
sessionID := make([]byte, s.idLen()) |
| 198 |
r := C.olm_session_id( |
| 199 |
(*C.OlmSession)(s.int), |
| 200 |
unsafe.Pointer(&sessionID[0]), |
| 201 |
C.size_t(len(sessionID))) |
| 202 |
if r == errorVal() { |
| 203 |
panic(s.lastError()) |
| 204 |
} |
| 205 |
return id.SessionID(sessionID) |
| 206 |
} |
| 207 |
|
| 208 |
// HasReceivedMessage returns true if this session has received any message. |
| 209 |
func (s *Session) HasReceivedMessage() bool { |
| 210 |
switch C.olm_session_has_received_message((*C.OlmSession)(s.int)) { |
| 211 |
case 0: |
| 212 |
return false |
| 213 |
default: |
| 214 |
return true |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// MatchesInboundSession checks if the PRE_KEY message is for this in-bound |
| 219 |
// Session. This can happen if multiple messages are sent to this Account |
| 220 |
// before this Account sends a message in reply. Returns true if the session |
| 221 |
// matches. Returns false if the session does not match. Returns error on |
| 222 |
// failure. If the base64 couldn't be decoded then the error will be |
| 223 |
// "INVALID_BASE64". If the message was for an unsupported protocol version |
| 224 |
// then the error will be "BAD_MESSAGE_VERSION". If the message couldn't be |
| 225 |
// decoded then then the error will be "BAD_MESSAGE_FORMAT". |
| 226 |
func (s *Session) MatchesInboundSession(oneTimeKeyMsg string) (bool, error) { |
| 227 |
if len(oneTimeKeyMsg) == 0 { |
| 228 |
return false, EmptyInput |
| 229 |
} |
| 230 |
r := C.olm_matches_inbound_session( |
| 231 |
(*C.OlmSession)(s.int), |
| 232 |
unsafe.Pointer(&([]byte(oneTimeKeyMsg))[0]), |
| 233 |
C.size_t(len(oneTimeKeyMsg))) |
| 234 |
if r == 1 { |
| 235 |
return true, nil |
| 236 |
} else if r == 0 { |
| 237 |
return false, nil |
| 238 |
} else { // if r == errorVal() |
| 239 |
return false, s.lastError() |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// MatchesInboundSessionFrom checks if the PRE_KEY message is for this in-bound |
| 244 |
// Session. This can happen if multiple messages are sent to this Account |
| 245 |
// before this Account sends a message in reply. Returns true if the session |
| 246 |
// matches. Returns false if the session does not match. Returns error on |
| 247 |
// failure. If the base64 couldn't be decoded then the error will be |
| 248 |
// "INVALID_BASE64". If the message was for an unsupported protocol version |
| 249 |
// then the error will be "BAD_MESSAGE_VERSION". If the message couldn't be |
| 250 |
// decoded then then the error will be "BAD_MESSAGE_FORMAT". |
| 251 |
func (s *Session) MatchesInboundSessionFrom(theirIdentityKey, oneTimeKeyMsg string) (bool, error) { |
| 252 |
if len(theirIdentityKey) == 0 || len(oneTimeKeyMsg) == 0 { |
| 253 |
return false, EmptyInput |
| 254 |
} |
| 255 |
r := C.olm_matches_inbound_session_from( |
| 256 |
(*C.OlmSession)(s.int), |
| 257 |
unsafe.Pointer(&([]byte(theirIdentityKey))[0]), |
| 258 |
C.size_t(len(theirIdentityKey)), |
| 259 |
unsafe.Pointer(&([]byte(oneTimeKeyMsg))[0]), |
| 260 |
C.size_t(len(oneTimeKeyMsg))) |
| 261 |
if r == 1 { |
| 262 |
return true, nil |
| 263 |
} else if r == 0 { |
| 264 |
return false, nil |
| 265 |
} else { // if r == errorVal() |
| 266 |
return false, s.lastError() |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// EncryptMsgType returns the type of the next message that Encrypt will |
| 271 |
// return. Returns MsgTypePreKey if the message will be a PRE_KEY message. |
| 272 |
// Returns MsgTypeMsg if the message will be a normal message. Returns error |
| 273 |
// on failure. |
| 274 |
func (s *Session) EncryptMsgType() id.OlmMsgType { |
| 275 |
switch C.olm_encrypt_message_type((*C.OlmSession)(s.int)) { |
| 276 |
case C.size_t(id.OlmMsgTypePreKey): |
| 277 |
return id.OlmMsgTypePreKey |
| 278 |
case C.size_t(id.OlmMsgTypeMsg): |
| 279 |
return id.OlmMsgTypeMsg |
| 280 |
default: |
| 281 |
panic("olm_encrypt_message_type returned invalid result") |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
// Encrypt encrypts a message using the Session. Returns the encrypted message |
| 286 |
// as base64. |
| 287 |
func (s *Session) Encrypt(plaintext []byte) (id.OlmMsgType, []byte) { |
| 288 |
if len(plaintext) == 0 { |
| 289 |
panic(EmptyInput) |
| 290 |
} |
| 291 |
// Make the slice be at least length 1 |
| 292 |
random := make([]byte, s.encryptRandomLen()+1) |
| 293 |
_, err := rand.Read(random) |
| 294 |
if err != nil { |
| 295 |
panic(NotEnoughGoRandom) |
| 296 |
} |
| 297 |
messageType := s.EncryptMsgType() |
| 298 |
message := make([]byte, s.encryptMsgLen(len(plaintext))) |
| 299 |
r := C.olm_encrypt( |
| 300 |
(*C.OlmSession)(s.int), |
| 301 |
unsafe.Pointer(&plaintext[0]), |
| 302 |
C.size_t(len(plaintext)), |
| 303 |
unsafe.Pointer(&random[0]), |
| 304 |
C.size_t(len(random)), |
| 305 |
unsafe.Pointer(&message[0]), |
| 306 |
C.size_t(len(message))) |
| 307 |
if r == errorVal() { |
| 308 |
panic(s.lastError()) |
| 309 |
} |
| 310 |
return messageType, message[:r] |
| 311 |
} |
| 312 |
|
| 313 |
// Decrypt decrypts a message using the Session. Returns the the plain-text on |
| 314 |
// success. Returns error on failure. If the base64 couldn't be decoded then |
| 315 |
// the error will be "INVALID_BASE64". If the message is for an unsupported |
| 316 |
// version of the protocol then the error will be "BAD_MESSAGE_VERSION". If |
| 317 |
// the message couldn't be decoded then the error will be BAD_MESSAGE_FORMAT". |
| 318 |
// If the MAC on the message was invalid then the error will be |
| 319 |
// "BAD_MESSAGE_MAC". |
| 320 |
func (s *Session) Decrypt(message string, msgType id.OlmMsgType) ([]byte, error) { |
| 321 |
if len(message) == 0 { |
| 322 |
return nil, EmptyInput |
| 323 |
} |
| 324 |
decryptMaxPlaintextLen, err := s.decryptMaxPlaintextLen(message, msgType) |
| 325 |
if err != nil { |
| 326 |
return nil, err |
| 327 |
} |
| 328 |
plaintext := make([]byte, decryptMaxPlaintextLen) |
| 329 |
r := C.olm_decrypt( |
| 330 |
(*C.OlmSession)(s.int), |
| 331 |
C.size_t(msgType), |
| 332 |
unsafe.Pointer(&([]byte(message))[0]), |
| 333 |
C.size_t(len(message)), |
| 334 |
unsafe.Pointer(&plaintext[0]), |
| 335 |
C.size_t(len(plaintext))) |
| 336 |
if r == errorVal() { |
| 337 |
return nil, s.lastError() |
| 338 |
} |
| 339 |
return plaintext[:r], nil |
| 340 |
} |
| 341 |
|
| 342 |
// https://gitlab.matrix.org/matrix-org/olm/-/blob/3.2.8/include/olm/olm.h#L392-393 |
| 343 |
const maxDescribeSize = 600 |
| 344 |
|
| 345 |
// Describe generates a string describing the internal state of an olm session for debugging and logging purposes. |
| 346 |
func (s *Session) Describe() string { |
| 347 |
desc := (*C.char)(C.malloc(C.size_t(maxDescribeSize))) |
| 348 |
defer C.free(unsafe.Pointer(desc)) |
| 349 |
C.meowlm_session_describe( |
| 350 |
(*C.OlmSession)(s.int), |
| 351 |
desc, |
| 352 |
C.size_t(maxDescribeSize)) |
| 353 |
return C.GoString(desc) |
| 354 |
} |