1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
#ifndef MATRIX__H
#define MATRIX__H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <olm/olm.h>
#define USER_ID_SIZE 64
#define ROOM_ID_SIZE 128
#define SERVER_SIZE 20
#define ACCESS_TOKEN_SIZE 40
#define DEVICE_ID_SIZE 20
#define EXPIRE_MS_SIZE 20
#define REFRESH_TOKEN_SIZE 20
#define MAX_URL_LEN 1024
#define OLM_IDENTITY_KEYS_JSON_SIZE 128
#define DEVICE_KEY_SIZE 44
#define SIGNING_KEY_SIZE 44
#define ONETIME_KEY_SIZE 44
#define MASTER_KEY_SIZE 44
#define KEY_SHARE_EVENT_LEN 1024
#define OLM_ACCOUNT_MEMORY_SIZE 7528
#define OLM_ACCOUNT_RANDOM_SIZE (32+32)
#define OLM_SESSION_MEMORY_SIZE 3352
#define OLM_ENCRYPT_RANDOM_SIZE 32
#define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)
#define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)
#define OLM_KEY_ID_SIZE 32
#define OLM_SIGNATURE_SIZE 128
#define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232
#define MEGOLM_SESSION_ID_SIZE 44
#define MEGOLM_SESSION_KEY_SIZE 306
#define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)
#define JSON_ONETIME_KEY_SIZE 128
#define JSON_ONETIME_KEY_SIGNED_SIZE 256
#define JSON_SIGNATURE_SIZE 256
#define NUM_MEGOLM_SESSIONS 2
#define NUM_OLM_SESSIONS 2
#define NUM_DEVICES 10
// HTTP
typedef struct MatrixHttpConnection MatrixHttpConnection;
bool
MatrixHttpInit(
MatrixHttpConnection ** hc,
const char * host);
bool
MatrixHttpDeinit(
MatrixHttpConnection ** hc);
bool
MatrixHttpSetAccessToken(
MatrixHttpConnection * hc,
const char * accessToken);
bool
MatrixHttpGet(
MatrixHttpConnection * hc,
const char * url,
char * outResponseBuffer, int outResponseCap,
bool authenticated);
bool
MatrixHttpPost(
MatrixHttpConnection * hc,
const char * url,
const char * requestBuffer,
char * outResponseBuffer, int outResponseCap,
bool authenticated);
bool
MatrixHttpPut(
MatrixHttpConnection * hc,
const char * url,
const char * requestBuffer,
char * outResponseBuffer, int outResponseCap,
bool authenticated);
// Matrix Device
typedef struct MatrixDevice {
char deviceId[DEVICE_ID_SIZE];
char deviceKey[DEVICE_KEY_SIZE];
char signingKey[SIGNING_KEY_SIZE];
} MatrixDevice;
// Matrix Olm Account
typedef struct MatrixOlmAccount {
OlmAccount * account;
char memory[OLM_ACCOUNT_MEMORY_SIZE];
} MatrixOlmAccount;
bool
MatrixOlmAccountInit(
MatrixOlmAccount * account);
bool
MatrixOlmAccountUnpickle(
MatrixOlmAccount * account,
void * pickled, int pickledLen,
const void * key, int keyLen);
bool
MatrixOlmAccountGetDeviceKey(
MatrixOlmAccount * account,
char * key, int keyCap);
bool
MatrixOlmAccountGetSigningKey(
MatrixOlmAccount * account,
char * key, int keyCap);
// Matrix Olm Session
typedef struct MatrixOlmSession {
const char * deviceId; // TODO: char[]
int type;
OlmSession * session;
char memory[OLM_SESSION_MEMORY_SIZE];
} MatrixOlmSession;
bool
MatrixOlmSessionUnpickle(
MatrixOlmSession * session,
const char * deviceId,
void * pickled, int pickledLen,
const void * key, int keyLen);
// create an olm sesseion from a type 0 message
bool
MatrixOlmSessionFrom(
MatrixOlmSession * session,
OlmAccount * olmAccount,
const char * deviceId,
const char * deviceKey,
const char * encrypted);
// create a new olm session from a claimed onetime key
bool
MatrixOlmSessionTo(
MatrixOlmSession * session,
OlmAccount * olmAccount,
const char * deviceId,
const char * deviceKey,
const char * deviceOnetimeKey);
bool
MatrixOlmSessionEncrypt(
MatrixOlmSession * session,
const char * plaintext,
char * outBuffer, int outBufferCap);
bool
MatrixOlmSessionDecrypt(
MatrixOlmSession * session,
size_t messageType,
char * encrypted,
char * outBuffer, int outBufferCap);
// Matrix Megolm Session
typedef struct MatrixMegolmInSession {
char roomId[ROOM_ID_SIZE];
char id[MEGOLM_SESSION_ID_SIZE];
char key[MEGOLM_SESSION_KEY_SIZE];
OlmInboundGroupSession * session;
char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];
} MatrixMegolmInSession;
bool
MatrixMegolmInSessionInit(
MatrixMegolmInSession * session,
const char * roomId,
const char * sessionId,
const char * sessionKey, int sessionKeyLen);
bool
MatrixMegolmInSessionDecrypt(
MatrixMegolmInSession * session,
const char * encrypted, int encryptedLen,
char * outDecrypted, int outDecryptedCap);
typedef struct MatrixMegolmOutSession {
char roomId[ROOM_ID_SIZE];
char id[MEGOLM_SESSION_ID_SIZE];
char key[MEGOLM_SESSION_KEY_SIZE];
OlmOutboundGroupSession * session;
char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];
} MatrixMegolmOutSession;
bool
MatrixMegolmOutSessionInit(
MatrixMegolmOutSession * session,
const char * roomId);
bool
MatrixMegolmOutSessionEncrypt(
MatrixMegolmOutSession * session,
const char * plaintext,
char * outBuffer, int outBufferCap);
// Matrix Client
typedef struct MatrixClient {
MatrixOlmAccount olmAccount;
MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];
int numMegolmInSessions;
MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];
int numMegolmOutSessions;
MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];
int numOlmSessions;
MatrixDevice devices[NUM_DEVICES];
int numDevices;
char userId[USER_ID_SIZE];
char accessToken[ACCESS_TOKEN_SIZE];
char deviceId[DEVICE_ID_SIZE];
char expireMs[EXPIRE_MS_SIZE];
char refreshToken[REFRESH_TOKEN_SIZE];
char masterKey[MASTER_KEY_SIZE];
bool verified;
MatrixHttpConnection * hc;
} MatrixClient;
bool
MatrixClientInit(
MatrixClient * client);
bool
MatrixClientSetAccessToken(
MatrixClient * client,
const char * accessToken);
bool
MatrixClientSetDeviceId(
MatrixClient * client,
const char * deviceId);
bool
MatrixClientSetUserId(
MatrixClient * client,
const char * userId);
bool
MatrixClientGenerateOnetimeKeys(
MatrixClient * client,
int numberOfKeys);
bool
MatrixClientUploadOnetimeKeys(
MatrixClient * client);
bool
MatrixClientUploadDeviceKeys(
MatrixClient * client);
bool
MatrixClientClaimOnetimeKey(
MatrixClient * client,
const char * userId,
const char * deviceId,
char * outOnetimeKey, int outOnetimeKeyCap);
bool
MatrixClientLoginPassword(
MatrixClient * client,
const char * username,
const char * password,
const char * displayName);
bool
MatrixClientSendEvent(
MatrixClient * client,
const char * roomId,
const char * msgType,
const char * msgBody);
bool
MatrixClientSendEventEncrypted(
MatrixClient * client,
const char * roomId,
const char * msgType,
const char * msgBody);
bool
MatrixClientSync(
MatrixClient * client,
char * outSyncBuffer, int outSyncCap,
char * nextBatch, int nextBatchCap);
bool
MatrixClientGetRoomEvent(
MatrixClient * client,
const char * roomId,
const char * eventId,
char * outEvent, int outEventCap);
bool
MatrixClientShareMegolmOutSession(
MatrixClient * client,
const char * userId,
const char * deviceId,
MatrixMegolmOutSession * session);
// try to lookup outgoing megolm session, return true if found
bool
MatrixClientGetMegolmOutSession(
MatrixClient * client,
const char * roomId,
MatrixMegolmOutSession ** outSession);
// create a new outgoing megolm session and store it locally
bool
MatrixClientNewMegolmOutSession(
MatrixClient * client,
const char * roomId,
MatrixMegolmOutSession ** outSession);
// try to lookup incoming megolm session, return true if found
bool
MatrixClientGetMegolmInSession(
MatrixClient * client,
const char * roomId, int roomIdLen,
const char * sessionId, int sessionIdLen,
MatrixMegolmInSession ** outSession);
// create a new incoming megolm session and store it locally
bool
MatrixClientNewMegolmInSession(
MatrixClient * client,
const char * roomId,
const char * sessionId,
const char * sessionKey,
MatrixMegolmInSession ** outSession);
// send a m.room_key_request to the device identified by userId/devideId
bool
MatrixClientRequestMegolmInSession(
MatrixClient * client,
const char * roomId,
const char * sessionId,
const char * senderKey,
const char * userId,
const char * deviceId);
// try to lookup olm session, return true if found
bool
MatrixClientGetOlmSession(
MatrixClient * client,
const char * userId,
const char * deviceId,
MatrixOlmSession ** outSession);
// create a new olm session from a type 0 message and store it locally
bool
MatrixClientNewOlmSessionIn(
MatrixClient * client,
const char * userId,
const char * deviceId,
const char * encrypted,
MatrixOlmSession ** outSession);
// create a new olm session with device userId/deviceId and store it locally
// this automatically claims the onetime key
bool
MatrixClientNewOlmSessionOut(
MatrixClient * client,
const char * userId,
const char * deviceId,
MatrixOlmSession ** outSession);
bool
MatrixClientSendToDevice(
MatrixClient * client,
const char * userId,
const char * deviceId,
const char * message,
const char * msgType);
bool
MatrixClientSendToDeviceEncrypted(
MatrixClient * client,
const char * userId,
const char * deviceId,
const char * message,
const char * msgType);
bool
MatrixClientSendDummy(
MatrixClient * client,
const char * userId,
const char * deviceId);
// lookup device key locally and if not present get it from server
bool
MatrixClientRequestDeviceKey(
MatrixClient * client,
const char * deviceId,
char * outDeviceKey, int outDeviceKeyCap);
// lookup signing key locally and if not present get it from server
bool
MatrixClientRequestSigningKey(
MatrixClient * client,
const char * deviceId,
char * outSigningKey, int outSigningKeyCap);
// lookup the master key for this user and if not present get it from server
bool
MatrixClientRequestMasterKey(
MatrixClient * client,
char * outMasterKey, int outMasterKeyCap);
// call keys/query and store retrieved information
// this is called by the other Request* functions
bool
MatrixClientRequestDeviceKeys(
MatrixClient * client);
// delete this device on the server
bool
MatrixClientDeleteDevice(
MatrixClient * client);
// util
void
Randomize(uint8_t * random, int randomLen);
bool
JsonEscape(
const char * sIn, int sInLen,
char * sOut, int sOutCap);
bool
JsonCanonicalize(
const char * sIn, int sInLen,
char * sOut, int sOutCap);
bool
JsonSign(
MatrixClient * client,
const char * sIn, int sInLen,
char * sOut, int sOutCap);
#endif
|