| 1 |
package olm |
| 2 |
|
| 3 |
// #cgo LDFLAGS: -lolm -lstdc++ |
| 4 |
// #include <olm/olm.h> |
| 5 |
import "C" |
| 6 |
|
| 7 |
import ( |
| 8 |
"unsafe" |
| 9 |
|
| 10 |
"maunium.net/go/mautrix/id" |
| 11 |
) |
| 12 |
|
| 13 |
// InboundGroupSession stores an inbound encrypted messaging session for a |
| 14 |
// group. |
| 15 |
type InboundGroupSession struct { |
| 16 |
int *C.OlmInboundGroupSession |
| 17 |
mem []byte |
| 18 |
} |
| 19 |
|
| 20 |
// InboundGroupSessionFromPickled loads an InboundGroupSession from a pickled |
| 21 |
// base64 string. Decrypts the InboundGroupSession using the supplied key. |
| 22 |
// Returns error on failure. If the key doesn't match the one used to encrypt |
| 23 |
// the InboundGroupSession then the error will be "BAD_SESSION_KEY". If the |
| 24 |
// base64 couldn't be decoded then the error will be "INVALID_BASE64". |
| 25 |
func InboundGroupSessionFromPickled(pickled, key []byte) (*InboundGroupSession, error) { |
| 26 |
if len(pickled) == 0 { |
| 27 |
return nil, EmptyInput |
| 28 |
} |
| 29 |
lenKey := len(key) |
| 30 |
if lenKey == 0 { |
| 31 |
key = []byte(" ") |
| 32 |
} |
| 33 |
s := NewBlankInboundGroupSession() |
| 34 |
return s, s.Unpickle(pickled, key) |
| 35 |
} |
| 36 |
|
| 37 |
// NewInboundGroupSession creates a new inbound group session from a key |
| 38 |
// exported from OutboundGroupSession.Key(). Returns error on failure. |
| 39 |
// If the sessionKey is not valid base64 the error will be |
| 40 |
// "OLM_INVALID_BASE64". If the session_key is invalid the error will be |
| 41 |
// "OLM_BAD_SESSION_KEY". |
| 42 |
func NewInboundGroupSession(sessionKey []byte) (*InboundGroupSession, error) { |
| 43 |
if len(sessionKey) == 0 { |
| 44 |
return nil, EmptyInput |
| 45 |
} |
| 46 |
s := NewBlankInboundGroupSession() |
| 47 |
r := C.olm_init_inbound_group_session( |
| 48 |
(*C.OlmInboundGroupSession)(s.int), |
| 49 |
(*C.uint8_t)(&sessionKey[0]), |
| 50 |
C.size_t(len(sessionKey))) |
| 51 |
if r == errorVal() { |
| 52 |
return nil, s.lastError() |
| 53 |
} |
| 54 |
return s, nil |
| 55 |
} |
| 56 |
|
| 57 |
// InboundGroupSessionImport imports an inbound group session from a previous |
| 58 |
// export. Returns error on failure. If the sessionKey is not valid base64 |
| 59 |
// the error will be "OLM_INVALID_BASE64". If the session_key is invalid the |
| 60 |
// error will be "OLM_BAD_SESSION_KEY". |
| 61 |
func InboundGroupSessionImport(sessionKey []byte) (*InboundGroupSession, error) { |
| 62 |
if len(sessionKey) == 0 { |
| 63 |
return nil, EmptyInput |
| 64 |
} |
| 65 |
s := NewBlankInboundGroupSession() |
| 66 |
r := C.olm_import_inbound_group_session( |
| 67 |
(*C.OlmInboundGroupSession)(s.int), |
| 68 |
(*C.uint8_t)(&sessionKey[0]), |
| 69 |
C.size_t(len(sessionKey))) |
| 70 |
if r == errorVal() { |
| 71 |
return nil, s.lastError() |
| 72 |
} |
| 73 |
return s, nil |
| 74 |
} |
| 75 |
|
| 76 |
// inboundGroupSessionSize is the size of an inbound group session object in |
| 77 |
// bytes. |
| 78 |
func inboundGroupSessionSize() uint { |
| 79 |
return uint(C.olm_inbound_group_session_size()) |
| 80 |
} |
| 81 |
|
| 82 |
// newInboundGroupSession initialises an empty InboundGroupSession. |
| 83 |
func NewBlankInboundGroupSession() *InboundGroupSession { |
| 84 |
memory := make([]byte, inboundGroupSessionSize()) |
| 85 |
return &InboundGroupSession{ |
| 86 |
int: C.olm_inbound_group_session(unsafe.Pointer(&memory[0])), |
| 87 |
mem: memory, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// lastError returns an error describing the most recent error to happen to an |
| 92 |
// inbound group session. |
| 93 |
func (s *InboundGroupSession) lastError() error { |
| 94 |
return convertError(C.GoString(C.olm_inbound_group_session_last_error((*C.OlmInboundGroupSession)(s.int)))) |
| 95 |
} |
| 96 |
|
| 97 |
// Clear clears the memory used to back this InboundGroupSession. |
| 98 |
func (s *InboundGroupSession) Clear() error { |
| 99 |
r := C.olm_clear_inbound_group_session((*C.OlmInboundGroupSession)(s.int)) |
| 100 |
if r == errorVal() { |
| 101 |
return s.lastError() |
| 102 |
} |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
// pickleLen returns the number of bytes needed to store an inbound group |
| 107 |
// session. |
| 108 |
func (s *InboundGroupSession) pickleLen() uint { |
| 109 |
return uint(C.olm_pickle_inbound_group_session_length((*C.OlmInboundGroupSession)(s.int))) |
| 110 |
} |
| 111 |
|
| 112 |
// Pickle returns an InboundGroupSession as a base64 string. Encrypts the |
| 113 |
// InboundGroupSession using the supplied key. |
| 114 |
func (s *InboundGroupSession) Pickle(key []byte) []byte { |
| 115 |
if len(key) == 0 { |
| 116 |
panic(NoKeyProvided) |
| 117 |
} |
| 118 |
pickled := make([]byte, s.pickleLen()) |
| 119 |
r := C.olm_pickle_inbound_group_session( |
| 120 |
(*C.OlmInboundGroupSession)(s.int), |
| 121 |
unsafe.Pointer(&key[0]), |
| 122 |
C.size_t(len(key)), |
| 123 |
unsafe.Pointer(&pickled[0]), |
| 124 |
C.size_t(len(pickled))) |
| 125 |
if r == errorVal() { |
| 126 |
panic(s.lastError()) |
| 127 |
} |
| 128 |
return pickled[:r] |
| 129 |
} |
| 130 |
|
| 131 |
func (s *InboundGroupSession) Unpickle(pickled, key []byte) error { |
| 132 |
if len(key) == 0 { |
| 133 |
return NoKeyProvided |
| 134 |
} |
| 135 |
r := C.olm_unpickle_inbound_group_session( |
| 136 |
(*C.OlmInboundGroupSession)(s.int), |
| 137 |
unsafe.Pointer(&key[0]), |
| 138 |
C.size_t(len(key)), |
| 139 |
unsafe.Pointer(&pickled[0]), |
| 140 |
C.size_t(len(pickled))) |
| 141 |
if r == errorVal() { |
| 142 |
return s.lastError() |
| 143 |
} |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func (s *InboundGroupSession) GobEncode() ([]byte, error) { |
| 148 |
pickled := s.Pickle(pickleKey) |
| 149 |
length := unpaddedBase64.DecodedLen(len(pickled)) |
| 150 |
rawPickled := make([]byte, length) |
| 151 |
_, err := unpaddedBase64.Decode(rawPickled, pickled) |
| 152 |
return rawPickled, err |
| 153 |
} |
| 154 |
|
| 155 |
func (s *InboundGroupSession) GobDecode(rawPickled []byte) error { |
| 156 |
if s == nil || s.int == nil { |
| 157 |
*s = *NewBlankInboundGroupSession() |
| 158 |
} |
| 159 |
length := unpaddedBase64.EncodedLen(len(rawPickled)) |
| 160 |
pickled := make([]byte, length) |
| 161 |
unpaddedBase64.Encode(pickled, rawPickled) |
| 162 |
return s.Unpickle(pickled, pickleKey) |
| 163 |
} |
| 164 |
|
| 165 |
func (s *InboundGroupSession) MarshalJSON() ([]byte, error) { |
| 166 |
pickled := s.Pickle(pickleKey) |
| 167 |
quotes := make([]byte, len(pickled)+2) |
| 168 |
quotes[0] = '"' |
| 169 |
quotes[len(quotes)-1] = '"' |
| 170 |
copy(quotes[1:len(quotes)-1], pickled) |
| 171 |
return quotes, nil |
| 172 |
} |
| 173 |
|
| 174 |
func (s *InboundGroupSession) UnmarshalJSON(data []byte) error { |
| 175 |
if len(data) == 0 || data[0] != '"' || data[len(data)-1] != '"' { |
| 176 |
return InputNotJSONString |
| 177 |
} |
| 178 |
if s == nil || s.int == nil { |
| 179 |
*s = *NewBlankInboundGroupSession() |
| 180 |
} |
| 181 |
return s.Unpickle(data[1:len(data)-1], pickleKey) |
| 182 |
} |
| 183 |
|
| 184 |
func clone(original []byte) []byte { |
| 185 |
clone := make([]byte, len(original)) |
| 186 |
copy(clone, original) |
| 187 |
return clone |
| 188 |
} |
| 189 |
|
| 190 |
// decryptMaxPlaintextLen returns the maximum number of bytes of plain-text a |
| 191 |
// given message could decode to. The actual size could be different due to |
| 192 |
// padding. Returns error on failure. If the message base64 couldn't be |
| 193 |
// decoded then the error will be "INVALID_BASE64". If the message is for an |
| 194 |
// unsupported version of the protocol then the error will be |
| 195 |
// "BAD_MESSAGE_VERSION". If the message couldn't be decoded then the error |
| 196 |
// will be "BAD_MESSAGE_FORMAT". |
| 197 |
func (s *InboundGroupSession) decryptMaxPlaintextLen(message []byte) (uint, error) { |
| 198 |
if len(message) == 0 { |
| 199 |
return 0, EmptyInput |
| 200 |
} |
| 201 |
// olm_group_decrypt_max_plaintext_length destroys the input, so we have to clone it |
| 202 |
message = clone(message) |
| 203 |
r := C.olm_group_decrypt_max_plaintext_length( |
| 204 |
(*C.OlmInboundGroupSession)(s.int), |
| 205 |
(*C.uint8_t)(&message[0]), |
| 206 |
C.size_t(len(message))) |
| 207 |
if r == errorVal() { |
| 208 |
return 0, s.lastError() |
| 209 |
} |
| 210 |
return uint(r), nil |
| 211 |
} |
| 212 |
|
| 213 |
// Decrypt decrypts a message using the InboundGroupSession. Returns the the |
| 214 |
// plain-text and message index on success. Returns error on failure. If the |
| 215 |
// base64 couldn't be decoded then the error will be "INVALID_BASE64". If the |
| 216 |
// message is for an unsupported version of the protocol then the error will be |
| 217 |
// "BAD_MESSAGE_VERSION". If the message couldn't be decoded then the error |
| 218 |
// will be BAD_MESSAGE_FORMAT". If the MAC on the message was invalid then the |
| 219 |
// error will be "BAD_MESSAGE_MAC". If we do not have a session key |
| 220 |
// corresponding to the message's index (ie, it was sent before the session key |
| 221 |
// was shared with us) the error will be "OLM_UNKNOWN_MESSAGE_INDEX". |
| 222 |
func (s *InboundGroupSession) Decrypt(message []byte) ([]byte, uint, error) { |
| 223 |
if len(message) == 0 { |
| 224 |
return nil, 0, EmptyInput |
| 225 |
} |
| 226 |
decryptMaxPlaintextLen, err := s.decryptMaxPlaintextLen(message) |
| 227 |
if err != nil { |
| 228 |
return nil, 0, err |
| 229 |
} |
| 230 |
plaintext := make([]byte, decryptMaxPlaintextLen) |
| 231 |
var messageIndex uint32 |
| 232 |
r := C.olm_group_decrypt( |
| 233 |
(*C.OlmInboundGroupSession)(s.int), |
| 234 |
(*C.uint8_t)(&message[0]), |
| 235 |
C.size_t(len(message)), |
| 236 |
(*C.uint8_t)(&plaintext[0]), |
| 237 |
C.size_t(len(plaintext)), |
| 238 |
(*C.uint32_t)(&messageIndex)) |
| 239 |
if r == errorVal() { |
| 240 |
return nil, 0, s.lastError() |
| 241 |
} |
| 242 |
return plaintext[:r], uint(messageIndex), nil |
| 243 |
} |
| 244 |
|
| 245 |
// sessionIdLen returns the number of bytes needed to store a session ID. |
| 246 |
func (s *InboundGroupSession) sessionIdLen() uint { |
| 247 |
return uint(C.olm_inbound_group_session_id_length((*C.OlmInboundGroupSession)(s.int))) |
| 248 |
} |
| 249 |
|
| 250 |
// ID returns a base64-encoded identifier for this session. |
| 251 |
func (s *InboundGroupSession) ID() id.SessionID { |
| 252 |
sessionID := make([]byte, s.sessionIdLen()) |
| 253 |
r := C.olm_inbound_group_session_id( |
| 254 |
(*C.OlmInboundGroupSession)(s.int), |
| 255 |
(*C.uint8_t)(&sessionID[0]), |
| 256 |
C.size_t(len(sessionID))) |
| 257 |
if r == errorVal() { |
| 258 |
panic(s.lastError()) |
| 259 |
} |
| 260 |
return id.SessionID(sessionID[:r]) |
| 261 |
} |
| 262 |
|
| 263 |
// FirstKnownIndex returns the first message index we know how to decrypt. |
| 264 |
func (s *InboundGroupSession) FirstKnownIndex() uint32 { |
| 265 |
return uint32(C.olm_inbound_group_session_first_known_index((*C.OlmInboundGroupSession)(s.int))) |
| 266 |
} |
| 267 |
|
| 268 |
// IsVerified check if the session has been verified as a valid session. (A |
| 269 |
// session is verified either because the original session share was signed, or |
| 270 |
// because we have subsequently successfully decrypted a message.) |
| 271 |
func (s *InboundGroupSession) IsVerified() uint { |
| 272 |
return uint(C.olm_inbound_group_session_is_verified((*C.OlmInboundGroupSession)(s.int))) |
| 273 |
} |
| 274 |
|
| 275 |
// exportLen returns the number of bytes needed to export an inbound group |
| 276 |
// session. |
| 277 |
func (s *InboundGroupSession) exportLen() uint { |
| 278 |
return uint(C.olm_export_inbound_group_session_length((*C.OlmInboundGroupSession)(s.int))) |
| 279 |
} |
| 280 |
|
| 281 |
// Export returns the base64-encoded ratchet key for this session, at the given |
| 282 |
// index, in a format which can be used by |
| 283 |
// InboundGroupSession.InboundGroupSessionImport(). Encrypts the |
| 284 |
// InboundGroupSession using the supplied key. Returns error on failure. |
| 285 |
// if we do not have a session key corresponding to the given index (ie, it was |
| 286 |
// sent before the session key was shared with us) the error will be |
| 287 |
// "OLM_UNKNOWN_MESSAGE_INDEX". |
| 288 |
func (s *InboundGroupSession) Export(messageIndex uint32) (string, error) { |
| 289 |
key := make([]byte, s.exportLen()) |
| 290 |
r := C.olm_export_inbound_group_session( |
| 291 |
(*C.OlmInboundGroupSession)(s.int), |
| 292 |
(*C.uint8_t)(&key[0]), |
| 293 |
C.size_t(len(key)), |
| 294 |
C.uint32_t(messageIndex)) |
| 295 |
if r == errorVal() { |
| 296 |
return "", s.lastError() |
| 297 |
} |
| 298 |
return string(key[:r]), nil |
| 299 |
} |