13 #define USER_ID_SIZE 64
\r
14 #define ROOM_ID_SIZE 128
\r
15 #define SERVER_SIZE 20
\r
16 #define ACCESS_TOKEN_SIZE 40
\r
17 #define DEVICE_ID_SIZE 20
\r
18 #define EXPIRE_MS_SIZE 20
\r
19 #define REFRESH_TOKEN_SIZE 20
\r
20 #define MAX_URL_LEN 1024
\r
22 #define OLM_IDENTITY_KEYS_JSON_SIZE 128
\r
23 #define DEVICE_KEY_SIZE 44
\r
24 #define SIGNING_KEY_SIZE 44
\r
25 #define ONETIME_KEY_SIZE 44
\r
26 #define MASTER_KEY_SIZE 44
\r
28 #define KEY_SHARE_EVENT_LEN 1024
\r
30 #define OLM_ACCOUNT_MEMORY_SIZE 7528
\r
31 #define OLM_ACCOUNT_RANDOM_SIZE (32+32)
\r
33 #define OLM_SESSION_MEMORY_SIZE 3352
\r
34 #define OLM_ENCRYPT_RANDOM_SIZE 32
\r
35 #define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)
\r
37 #define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)
\r
38 #define OLM_KEY_ID_SIZE 32
\r
40 #define OLM_SIGNATURE_SIZE 128
\r
42 #define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232
\r
43 #define MEGOLM_SESSION_ID_SIZE 44
\r
44 #define MEGOLM_SESSION_KEY_SIZE 306
\r
45 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)
\r
47 #define JSON_ONETIME_KEY_SIZE 128
\r
48 #define JSON_ONETIME_KEY_SIGNED_SIZE 256
\r
49 #define JSON_SIGNATURE_SIZE 256
\r
51 #define NUM_MEGOLM_SESSIONS 2
\r
52 #define NUM_OLM_SESSIONS 2
\r
53 #define NUM_DEVICES 10
\r
57 typedef struct MatrixHttpConnection MatrixHttpConnection;
\r
61 MatrixHttpConnection ** hc,
\r
66 MatrixHttpConnection ** hc);
\r
69 MatrixHttpSetAccessToken(
\r
70 MatrixHttpConnection * hc,
\r
71 const char * accessToken);
\r
75 MatrixHttpConnection * hc,
\r
77 char * outResponseBuffer, int outResponseCap,
\r
78 bool authenticated);
\r
82 MatrixHttpConnection * hc,
\r
84 const char * requestBuffer,
\r
85 char * outResponseBuffer, int outResponseCap,
\r
86 bool authenticated);
\r
90 MatrixHttpConnection * hc,
\r
92 const char * requestBuffer,
\r
93 char * outResponseBuffer, int outResponseCap,
\r
94 bool authenticated);
\r
100 typedef struct MatrixDevice {
\r
101 char deviceId[DEVICE_ID_SIZE];
\r
102 char deviceKey[DEVICE_KEY_SIZE];
\r
103 char signingKey[SIGNING_KEY_SIZE];
\r
107 // Matrix Olm Account
\r
109 typedef struct MatrixOlmAccount {
\r
110 OlmAccount * account;
\r
111 char memory[OLM_ACCOUNT_MEMORY_SIZE];
\r
112 } MatrixOlmAccount;
\r
115 MatrixOlmAccountInit(
\r
116 MatrixOlmAccount * account);
\r
119 MatrixOlmAccountUnpickle(
\r
120 MatrixOlmAccount * account,
\r
121 void * pickled, int pickledLen,
\r
122 const void * key, int keyLen);
\r
125 MatrixOlmAccountGetDeviceKey(
\r
126 MatrixOlmAccount * account,
\r
127 char * key, int keyCap);
\r
130 MatrixOlmAccountGetSigningKey(
\r
131 MatrixOlmAccount * account,
\r
132 char * key, int keyCap);
\r
135 // Matrix Olm Session
\r
137 typedef struct MatrixOlmSession {
\r
138 const char * deviceId; // TODO: char[]
\r
141 OlmSession * session;
\r
142 char memory[OLM_SESSION_MEMORY_SIZE];
\r
143 } MatrixOlmSession;
\r
146 MatrixOlmSessionUnpickle(
\r
147 MatrixOlmSession * session,
\r
148 const char * deviceId,
\r
149 void * pickled, int pickledLen,
\r
150 const void * key, int keyLen);
\r
152 // create an olm sesseion from a type 0 message
\r
154 MatrixOlmSessionFrom(
\r
155 MatrixOlmSession * session,
\r
156 OlmAccount * olmAccount,
\r
157 const char * deviceId,
\r
158 const char * deviceKey,
\r
159 const char * encrypted);
\r
161 // create a new olm session from a claimed onetime key
\r
163 MatrixOlmSessionTo(
\r
164 MatrixOlmSession * session,
\r
165 OlmAccount * olmAccount,
\r
166 const char * deviceId,
\r
167 const char * deviceKey,
\r
168 const char * deviceOnetimeKey);
\r
171 MatrixOlmSessionEncrypt(
\r
172 MatrixOlmSession * session,
\r
173 const char * plaintext,
\r
174 char * outBuffer, int outBufferCap);
\r
177 MatrixOlmSessionDecrypt(
\r
178 MatrixOlmSession * session,
\r
179 size_t messageType,
\r
181 char * outBuffer, int outBufferCap);
\r
184 // Matrix Megolm Session
\r
186 typedef struct MatrixMegolmInSession {
\r
187 char roomId[ROOM_ID_SIZE];
\r
188 char id[MEGOLM_SESSION_ID_SIZE];
\r
189 char key[MEGOLM_SESSION_KEY_SIZE];
\r
191 OlmInboundGroupSession * session;
\r
192 char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];
\r
194 } MatrixMegolmInSession;
\r
197 MatrixMegolmInSessionInit(
\r
198 MatrixMegolmInSession * session,
\r
199 const char * roomId,
\r
200 const char * sessionId,
\r
201 const char * sessionKey, int sessionKeyLen);
\r
204 MatrixMegolmInSessionDecrypt(
\r
205 MatrixMegolmInSession * session,
\r
206 const char * encrypted, int encryptedLen,
\r
207 char * outDecrypted, int outDecryptedCap);
\r
209 typedef struct MatrixMegolmOutSession {
\r
210 char roomId[ROOM_ID_SIZE];
\r
211 char id[MEGOLM_SESSION_ID_SIZE];
\r
212 char key[MEGOLM_SESSION_KEY_SIZE];
\r
214 OlmOutboundGroupSession * session;
\r
215 char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];
\r
216 } MatrixMegolmOutSession;
\r
219 MatrixMegolmOutSessionInit(
\r
220 MatrixMegolmOutSession * session,
\r
221 const char * roomId);
\r
224 MatrixMegolmOutSessionEncrypt(
\r
225 MatrixMegolmOutSession * session,
\r
226 const char * plaintext,
\r
227 char * outBuffer, int outBufferCap);
\r
232 typedef struct MatrixClient {
\r
233 MatrixOlmAccount olmAccount;
\r
235 MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];
\r
236 int numMegolmInSessions;
\r
237 MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];
\r
238 int numMegolmOutSessions;
\r
239 MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];
\r
240 int numOlmSessions;
\r
242 MatrixDevice devices[NUM_DEVICES];
\r
245 char userId[USER_ID_SIZE];
\r
246 char accessToken[ACCESS_TOKEN_SIZE];
\r
247 char deviceId[DEVICE_ID_SIZE];
\r
248 char expireMs[EXPIRE_MS_SIZE];
\r
249 char refreshToken[REFRESH_TOKEN_SIZE];
\r
250 char masterKey[MASTER_KEY_SIZE];
\r
254 MatrixHttpConnection * hc;
\r
259 MatrixClient * client);
\r
262 MatrixClientSetAccessToken(
\r
263 MatrixClient * client,
\r
264 const char * accessToken);
\r
267 MatrixClientSetDeviceId(
\r
268 MatrixClient * client,
\r
269 const char * deviceId);
\r
272 MatrixClientSetUserId(
\r
273 MatrixClient * client,
\r
274 const char * userId);
\r
277 MatrixClientGenerateOnetimeKeys(
\r
278 MatrixClient * client,
\r
282 MatrixClientUploadOnetimeKeys(
\r
283 MatrixClient * client);
\r
286 MatrixClientUploadDeviceKeys(
\r
287 MatrixClient * client);
\r
290 MatrixClientClaimOnetimeKey(
\r
291 MatrixClient * client,
\r
292 const char * userId,
\r
293 const char * deviceId,
\r
294 char * outOnetimeKey, int outOnetimeKeyCap);
\r
297 MatrixClientLoginPassword(
\r
298 MatrixClient * client,
\r
299 const char * username,
\r
300 const char * password,
\r
301 const char * displayName);
\r
304 MatrixClientSendEvent(
\r
305 MatrixClient * client,
\r
306 const char * roomId,
\r
307 const char * msgType,
\r
308 const char * msgBody);
\r
311 MatrixClientSendEventEncrypted(
\r
312 MatrixClient * client,
\r
313 const char * roomId,
\r
314 const char * msgType,
\r
315 const char * msgBody);
\r
319 MatrixClient * client,
\r
320 char * outSyncBuffer, int outSyncCap,
\r
321 char * nextBatch, int nextBatchCap);
\r
324 MatrixClientGetRoomEvent(
\r
325 MatrixClient * client,
\r
326 const char * roomId,
\r
327 const char * eventId,
\r
328 char * outEvent, int outEventCap);
\r
331 MatrixClientShareMegolmOutSession(
\r
332 MatrixClient * client,
\r
333 const char * userId,
\r
334 const char * deviceId,
\r
335 MatrixMegolmOutSession * session);
\r
337 // try to lookup outgoing megolm session, return true if found
\r
339 MatrixClientGetMegolmOutSession(
\r
340 MatrixClient * client,
\r
341 const char * roomId,
\r
342 MatrixMegolmOutSession ** outSession);
\r
344 // create a new outgoing megolm session and store it locally
\r
346 MatrixClientNewMegolmOutSession(
\r
347 MatrixClient * client,
\r
348 const char * roomId,
\r
349 MatrixMegolmOutSession ** outSession);
\r
351 // try to lookup incoming megolm session, return true if found
\r
353 MatrixClientGetMegolmInSession(
\r
354 MatrixClient * client,
\r
355 const char * roomId, int roomIdLen,
\r
356 const char * sessionId, int sessionIdLen,
\r
357 MatrixMegolmInSession ** outSession);
\r
359 // create a new incoming megolm session and store it locally
\r
361 MatrixClientNewMegolmInSession(
\r
362 MatrixClient * client,
\r
363 const char * roomId,
\r
364 const char * sessionId,
\r
365 const char * sessionKey,
\r
366 MatrixMegolmInSession ** outSession);
\r
368 // send a m.room_key_request to the device identified by userId/devideId
\r
370 MatrixClientRequestMegolmInSession(
\r
371 MatrixClient * client,
\r
372 const char * roomId,
\r
373 const char * sessionId,
\r
374 const char * senderKey,
\r
375 const char * userId,
\r
376 const char * deviceId);
\r
378 // try to lookup olm session, return true if found
\r
380 MatrixClientGetOlmSession(
\r
381 MatrixClient * client,
\r
382 const char * userId,
\r
383 const char * deviceId,
\r
384 MatrixOlmSession ** outSession);
\r
386 // create a new olm session from a type 0 message and store it locally
\r
388 MatrixClientNewOlmSessionIn(
\r
389 MatrixClient * client,
\r
390 const char * userId,
\r
391 const char * deviceId,
\r
392 const char * encrypted,
\r
393 MatrixOlmSession ** outSession);
\r
395 // create a new olm session with device userId/deviceId and store it locally
\r
396 // this automatically claims the onetime key
\r
398 MatrixClientNewOlmSessionOut(
\r
399 MatrixClient * client,
\r
400 const char * userId,
\r
401 const char * deviceId,
\r
402 MatrixOlmSession ** outSession);
\r
405 MatrixClientSendToDevice(
\r
406 MatrixClient * client,
\r
407 const char * userId,
\r
408 const char * deviceId,
\r
409 const char * message,
\r
410 const char * msgType);
\r
413 MatrixClientSendToDeviceEncrypted(
\r
414 MatrixClient * client,
\r
415 const char * userId,
\r
416 const char * deviceId,
\r
417 const char * message,
\r
418 const char * msgType);
\r
421 MatrixClientSendDummy(
\r
422 MatrixClient * client,
\r
423 const char * userId,
\r
424 const char * deviceId);
\r
426 // lookup device key locally and if not present get it from server
\r
428 MatrixClientRequestDeviceKey(
\r
429 MatrixClient * client,
\r
430 const char * deviceId,
\r
431 char * outDeviceKey, int outDeviceKeyCap);
\r
433 // lookup signing key locally and if not present get it from server
\r
435 MatrixClientRequestSigningKey(
\r
436 MatrixClient * client,
\r
437 const char * deviceId,
\r
438 char * outSigningKey, int outSigningKeyCap);
\r
440 // lookup the master key for this user and if not present get it from server
\r
442 MatrixClientRequestMasterKey(
\r
443 MatrixClient * client,
\r
444 char * outMasterKey, int outMasterKeyCap);
\r
446 // call keys/query and store retrieved information
\r
447 // this is called by the other Request* functions
\r
449 MatrixClientRequestDeviceKeys(
\r
450 MatrixClient * client);
\r
452 // delete this device on the server
\r
454 MatrixClientDeleteDevice(
\r
455 MatrixClient * client);
\r
461 Randomize(uint8_t * random, int randomLen);
\r
465 const char * sIn, int sInLen,
\r
466 char * sOut, int sOutCap);
\r
470 const char * sIn, int sInLen,
\r
471 char * sOut, int sOutCap);
\r
475 MatrixClient * client,
\r
476 const char * sIn, int sInLen,
\r
477 char * sOut, int sOutCap);
\r