]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.c
working examples for esp
[matrix_esp_thesis] / src / matrix.c
1 #include "matrix.h"\r
2 \r
3 #include <time.h>\r
4 #include <stdio.h>\r
5 #include <mjson.h>\r
6 \r
7 #ifdef ESP_PLATFORM\r
8 #include <esp_random.h>\r
9 #endif\r
10 \r
11 #define STATIC\r
12 \r
13 #define LOGIN_REQUEST_SIZE 1024\r
14 #define LOGIN_RESPONSE_SIZE 1024\r
15 #define LOGIN_URL "/_matrix/client/v3/login"\r
16 \r
17 #define ENCRYPTED_REQUEST_SIZE (1024*5)\r
18 #define ENCRYPTED_EVENT_SIZE (1024*10)\r
19 #define ROOM_SEND_REQUEST_SIZE 256\r
20 #define ROOM_SEND_RESPONSE_SIZE 1024\r
21 #define ROOM_SEND_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"\r
22 \r
23 #define ROOMKEY_REQUEST_SIZE (1024*4)\r
24 \r
25 #define TODEVICE_EVENT_SIZE (1024*5)\r
26 #define TODEVICE_URL "/_matrix/client/v3/sendToDevice/%s/%d"\r
27 \r
28 #define KEYS_QUERY_URL "/_matrix/client/v3/keys/query"\r
29 #define KEYS_QUERY_REQUEST_SIZE 256\r
30 #define KEYS_QUERY_RESPONSE_SIZE (1024*10)\r
31 \r
32 #define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload"\r
33 #define KEYS_UPLOAD_REQUEST_SIZE 1024*4\r
34 #define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048*4\r
35 #define KEYS_UPLOAD_RESPONSE_SIZE 2048\r
36 \r
37 #define KEYS_CLAIM_URL "/_matrix/client/v3/keys/claim"\r
38 #define KEYS_CLAIM_REQUEST_SIZE 1024\r
39 #define KEYS_CLAIM_RESPONSE_SIZE 1024\r
40 \r
41 #define SYNC_TIMEOUT 5000\r
42 \r
43 #define JSON_QUERY_SIZE 128\r
44 #define JSON_MAX_INDICES 100\r
45 #define JSON_MAX_ENTRY_SIZE 1024\r
46 \r
47 #define MAX(a,b) ((a) > (b) ? (a) : (b))\r
48 #define MIN(a,b) ((a) < (b) ? (a) : (b))\r
49 \r
50 void\r
51 Randomize(\r
52     uint8_t * random,\r
53     int randomLen)\r
54 {\r
55     #ifdef ESP_PLATFORM\r
56 \r
57     for (int i = 0; i < randomLen; i++)\r
58     {\r
59         random[i] = esp_random() % 256;\r
60     }\r
61 \r
62     #else\r
63 \r
64     STATIC bool first = true;\r
65     if (first) { srand(time(0)); first = false; }\r
66 \r
67     for (int i = 0; i < randomLen; i++)\r
68     {\r
69         random[i] = rand() % 256;\r
70     }\r
71 \r
72     #endif\r
73 }\r
74 \r
75 bool\r
76 JsonEscape(\r
77     const char * sIn, int sInLen,\r
78     char * sOut, int sOutCap)\r
79 {\r
80     int sOutIndex = 0;\r
81 \r
82     for (int i = 0; i < sInLen; i++)\r
83     {\r
84         if (i >= sOutCap)\r
85             return false;\r
86         \r
87         if (sIn[i] == '.' ||\r
88             sIn[i] == '[' ||\r
89             sIn[i] == ']'\r
90         ) {\r
91             sOut[sOutIndex++] = '\\';\r
92         }\r
93         sOut[sOutIndex++] = sIn[i];\r
94     }\r
95 \r
96     if (sOutIndex < sOutCap)\r
97         sOut[sOutIndex] = '\0';\r
98 \r
99     return true;\r
100 }\r
101 \r
102 bool\r
103 JsonCanonicalize(\r
104     const char * sIn, int sInLen,\r
105     char * sOut, int sOutCap)\r
106 {\r
107     snprintf(sOut, sOutCap, "{}");\r
108 \r
109     int koff, klen, voff, vlen, vtype, off;\r
110 \r
111     struct Key {\r
112         const char * ptr;\r
113         int len;\r
114     };\r
115 \r
116     struct Key keys[JSON_MAX_INDICES];\r
117     int numKeys = 0;\r
118 \r
119     for (off = 0; (off = mjson_next(sIn, sInLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0; ) {\r
120         keys[numKeys].ptr = sIn + koff;\r
121         keys[numKeys].len = klen;\r
122         numKeys++;\r
123     }\r
124 \r
125     for (int i = 0; i < numKeys; i++) {\r
126         for (int j = i; j < numKeys; j++) {\r
127             if (\r
128                 strncmp(\r
129                     keys[i].ptr,\r
130                     keys[j].ptr,\r
131                     MIN(keys[i].len, keys[j].len)\r
132                 ) > 0\r
133             ) {\r
134                 struct Key k = keys[i];\r
135                 keys[i] = keys[j];\r
136                 keys[j] = k;\r
137             }\r
138         }\r
139     }\r
140 \r
141     for (int i = 0; i < numKeys; i++) {\r
142         char jp[JSON_QUERY_SIZE];\r
143         snprintf(jp, JSON_QUERY_SIZE, "$.%.*s", keys[i].len-2, keys[i].ptr+1);\r
144 \r
145         const char * valPtr;\r
146         int valLen;\r
147         mjson_find(sIn, sInLen, jp, &valPtr, &valLen);\r
148         \r
149         STATIC char newEntry[JSON_MAX_ENTRY_SIZE];\r
150         snprintf(newEntry, JSON_MAX_ENTRY_SIZE, "{%.*s:%.*s}", keys[i].len, keys[i].ptr, valLen, valPtr);\r
151 \r
152         char * buffer = strdup(sOut);\r
153 \r
154         struct mjson_fixedbuf fb = { sOut, sOutCap, 0 };\r
155         mjson_merge(buffer, strlen(buffer), newEntry, strlen(newEntry), mjson_print_fixed_buf, &fb);\r
156 \r
157         free(buffer);\r
158     }\r
159 \r
160     // TODO: recursively sort entries\r
161 \r
162     return true;\r
163 }\r
164 \r
165 bool JsonSign(\r
166     MatrixClient * client,\r
167     const char * sIn, int sInLen,\r
168     char * sOut, int sOutCap)\r
169 {\r
170     STATIC char signature[OLM_SIGNATURE_SIZE];\r
171     size_t res =\r
172         olm_account_sign(client->olmAccount.account,\r
173             sIn, sInLen,\r
174             signature, OLM_SIGNATURE_SIZE);\r
175     \r
176     int signatureLen = res;\r
177     \r
178     STATIC char thisSigningKey[SIGNING_KEY_SIZE];\r
179     MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, SIGNING_KEY_SIZE);\r
180 \r
181     STATIC char signatureJson[JSON_SIGNATURE_SIZE];\r
182     int signatureJsonLen =\r
183         mjson_snprintf(signatureJson, JSON_SIGNATURE_SIZE,\r
184             "{"\r
185                 "\"signatures\":{"\r
186                     "\"%s\":{"\r
187                         "\"ed25519:%s\":\"%.*s\""\r
188                     "}"\r
189                 "}"\r
190             "}",\r
191             client->userId,\r
192             //"1",\r
193             client->deviceId,\r
194             signatureLen, signature);\r
195 \r
196     struct mjson_fixedbuf result = { sOut, sOutCap, 0 };\r
197     mjson_merge(\r
198         sIn, sInLen,\r
199         signatureJson, signatureJsonLen,\r
200         mjson_print_fixed_buf,\r
201         &result);\r
202 \r
203     return true;\r
204 }\r
205 \r
206 \r
207 bool\r
208 MatrixOlmAccountInit(\r
209     MatrixOlmAccount * account)\r
210 {\r
211     account->account = olm_account(account->memory);\r
212 \r
213     STATIC uint8_t random[OLM_ACCOUNT_RANDOM_SIZE];\r
214     Randomize(random, OLM_ACCOUNT_RANDOM_SIZE);\r
215 \r
216     size_t res = olm_create_account(\r
217         account->account,\r
218         random,\r
219         OLM_ACCOUNT_RANDOM_SIZE);\r
220 \r
221     return res != olm_error();\r
222 }\r
223 \r
224 bool\r
225 MatrixOlmAccountUnpickle(\r
226     MatrixOlmAccount * account,\r
227     void * pickled, int pickledLen,\r
228     const void * key, int keyLen)\r
229 {\r
230     size_t res;\r
231     res = olm_unpickle_account(account->account,\r
232         key, keyLen,\r
233         pickled, pickledLen);\r
234     if (res == olm_error()) {\r
235         printf("error unpickling olm account:%s\n",\r
236             olm_account_last_error(account->account));\r
237     }\r
238     return res != olm_error();\r
239 }\r
240 \r
241 bool\r
242 MatrixOlmAccountGetDeviceKey(\r
243     MatrixOlmAccount * account,\r
244     char * key, int keyCap)\r
245 {\r
246     STATIC char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
247     size_t res =\r
248         olm_account_identity_keys(account->account,\r
249             deviceKeysJson, OLM_IDENTITY_KEYS_JSON_SIZE);\r
250     mjson_get_string(deviceKeysJson, res,\r
251         "$.curve25519",\r
252         key, keyCap);\r
253     return true;\r
254 }\r
255 \r
256 bool\r
257 MatrixOlmAccountGetSigningKey(\r
258     MatrixOlmAccount * account,\r
259     char * key, int keyCap)\r
260 {\r
261     STATIC char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
262     size_t res =\r
263         olm_account_identity_keys(account->account,\r
264             deviceKeysJson, OLM_IDENTITY_KEYS_JSON_SIZE);\r
265     mjson_get_string(deviceKeysJson, res,\r
266         "$.ed25519",\r
267         key, keyCap);\r
268     return true;\r
269 }\r
270 \r
271 bool\r
272 MatrixOlmSessionFrom(\r
273     MatrixOlmSession * session,\r
274     OlmAccount * olmAccount,\r
275     const char * deviceId,\r
276     const char * deviceKey,\r
277     const char * encrypted)\r
278 {\r
279     memset(session, 0, sizeof(MatrixOlmSession));\r
280 \r
281     session->deviceId = deviceId;\r
282 \r
283     session->session =\r
284         olm_session(session->memory);\r
285     \r
286     char * encryptedCopy = strdup(encrypted);\r
287     \r
288     size_t res =\r
289         olm_create_inbound_session_from(session->session, olmAccount,\r
290             deviceKey, strlen(deviceKey),\r
291             encryptedCopy, strlen(encryptedCopy));\r
292     \r
293     if (res == olm_error()) {\r
294         printf("error olm:%s\n", olm_session_last_error(session->session));\r
295     }\r
296 \r
297     return res != olm_error();\r
298 }\r
299 \r
300 bool\r
301 MatrixOlmSessionTo(\r
302     MatrixOlmSession * session,\r
303     OlmAccount * olmAccount,\r
304     const char * deviceId,\r
305     const char * deviceKey,\r
306     const char * deviceOnetimeKey)\r
307 {\r
308     memset(session, 0, sizeof(MatrixOlmSession));\r
309 \r
310     session->deviceId = deviceId;\r
311 \r
312     session->session =\r
313         olm_session(session->memory);\r
314 \r
315     STATIC uint8_t random[OLM_OUTBOUND_SESSION_RANDOM_SIZE];\r
316     Randomize(random, OLM_OUTBOUND_SESSION_RANDOM_SIZE);\r
317 \r
318     size_t res =\r
319         olm_create_outbound_session(session->session,\r
320             olmAccount,\r
321             deviceKey, strlen(deviceKey),\r
322             deviceOnetimeKey, strlen(deviceOnetimeKey),\r
323             random, OLM_OUTBOUND_SESSION_RANDOM_SIZE);\r
324     \r
325     if (res == olm_error()) {\r
326         printf("error olm:%s\n", olm_session_last_error(session->session));\r
327     }\r
328 \r
329     return res != olm_error();\r
330 }\r
331 \r
332 bool\r
333 MatrixOlmSessionUnpickle(\r
334     MatrixOlmSession * session,\r
335     const char * deviceId,\r
336     void * pickled, int pickledLen,\r
337     const void * key, int keyLen)\r
338 {\r
339     memset(session, 0, sizeof(MatrixOlmSession));\r
340 \r
341     session->deviceId = deviceId;\r
342 \r
343     session->session =\r
344         olm_session(session->memory);\r
345     \r
346     size_t res;\r
347     res = olm_unpickle_session(session->session,\r
348         key, keyLen,\r
349         pickled, pickledLen);\r
350     \r
351     if (res == olm_error()) {\r
352         printf("error unpickling olm session:%s\n", olm_session_last_error(session->session));\r
353     }\r
354 \r
355     return res != olm_error();\r
356 }\r
357 \r
358 bool\r
359 MatrixOlmSessionEncrypt(\r
360     MatrixOlmSession * session,\r
361     const char * plaintext,\r
362     char * outBuffer, int outBufferCap)\r
363 {\r
364     STATIC uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];\r
365     Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);\r
366 \r
367     size_t res = olm_encrypt(session->session,\r
368         plaintext, strlen(plaintext),\r
369         random, OLM_ENCRYPT_RANDOM_SIZE,\r
370         outBuffer, outBufferCap);\r
371 \r
372     return res != olm_error();\r
373 }\r
374 \r
375 bool\r
376 MatrixOlmSessionDecrypt(\r
377     MatrixOlmSession * session,\r
378     size_t messageType,\r
379     char * encrypted,\r
380     char * outBuffer, int outBufferCap)\r
381 {\r
382     STATIC uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];\r
383     Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);\r
384 \r
385     size_t res =\r
386         olm_decrypt(session->session,\r
387             messageType,\r
388             encrypted, strlen(encrypted),\r
389             outBuffer, outBufferCap);\r
390     \r
391     if (res != olm_error() && res < outBufferCap)\r
392         outBuffer[res] = '\0';\r
393 \r
394     return res != olm_error();\r
395 }\r
396 \r
397 bool\r
398 MatrixMegolmInSessionInit(\r
399     MatrixMegolmInSession * session,\r
400     const char * roomId,\r
401     const char * sessionId,\r
402     const char * sessionKey, int sessionKeyLen)\r
403 {\r
404     memset(session, 0, sizeof(MatrixMegolmInSession));\r
405     \r
406     strncpy(session->roomId, roomId, sizeof(session->roomId));\r
407     strncpy(session->id, sessionId, sizeof(session->id));\r
408     strncpy(session->key, sessionKey, sizeof(session->key));\r
409 \r
410     session->session =\r
411         olm_inbound_group_session(session->memory);\r
412 \r
413     size_t res =\r
414         olm_init_inbound_group_session(\r
415         // olm_import_inbound_group_session(\r
416             session->session,\r
417             (const uint8_t *)sessionKey, sessionKeyLen);\r
418     if (res == olm_error()) {\r
419         printf("Error initializing Megolm session: %s\n", olm_inbound_group_session_last_error(session->session));\r
420     }\r
421 \r
422     return res != olm_error();\r
423 }\r
424 \r
425 bool\r
426 MatrixMegolmInSessionDecrypt(\r
427     MatrixMegolmInSession * session,\r
428     const char * encrypted, int encryptedLen,\r
429     char * outDecrypted, int outDecryptedCap)\r
430 {\r
431     // uint8_t buffer[1024];\r
432     // memcpy(buffer, encrypted, encryptedLen);\r
433 \r
434     uint32_t megolmInMessageIndex;\r
435 \r
436     size_t res =\r
437         olm_group_decrypt(session->session,\r
438             (uint8_t *)encrypted, encryptedLen,\r
439             (uint8_t *)outDecrypted, outDecryptedCap,\r
440             &megolmInMessageIndex);\r
441     \r
442     printf("message index: %d\n", (int)megolmInMessageIndex);\r
443     \r
444     if (res == olm_error()) {\r
445         printf("error decrypting megolm message: %s\n", olm_inbound_group_session_last_error(session->session));\r
446     }\r
447     else {\r
448         printf("decrypted len: %d\n", res);\r
449     }\r
450     \r
451     return true;\r
452 }\r
453 \r
454 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session\r
455 bool\r
456 MatrixMegolmOutSessionInit(\r
457     MatrixMegolmOutSession * session,\r
458     const char * roomId)\r
459 {\r
460     memset(session, 0, sizeof(MatrixMegolmOutSession));\r
461 \r
462     STATIC uint8_t random[MEGOLM_INIT_RANDOM_SIZE];\r
463     Randomize(random, MEGOLM_INIT_RANDOM_SIZE);\r
464 \r
465     strncpy(session->roomId, roomId, ROOM_ID_SIZE);\r
466 \r
467     session->session =\r
468         olm_outbound_group_session(session->memory);\r
469 \r
470     olm_init_outbound_group_session(\r
471         session->session,\r
472         random,\r
473         MEGOLM_INIT_RANDOM_SIZE);\r
474 \r
475     olm_outbound_group_session_id(session->session,\r
476         (uint8_t *)session->id,\r
477         MEGOLM_SESSION_ID_SIZE);\r
478         \r
479     olm_outbound_group_session_key(session->session,\r
480         (uint8_t *)session->key,\r
481         MEGOLM_SESSION_KEY_SIZE);\r
482     \r
483     return true;\r
484 }\r
485 \r
486 bool\r
487 MatrixMegolmOutSessionEncrypt(\r
488     MatrixMegolmOutSession * session,\r
489     const char * plaintext,\r
490     char * outBuffer, int outBufferCap)\r
491 {\r
492     size_t res = olm_group_encrypt(session->session,\r
493         (uint8_t *)plaintext, strlen(plaintext),\r
494         (uint8_t *)outBuffer, outBufferCap);\r
495 \r
496     return res != olm_error();\r
497 }\r
498 \r
499 bool\r
500 MatrixMegolmOutSessionSave(\r
501     MatrixMegolmOutSession * session,\r
502     const char * filename,\r
503     const char * key)\r
504 {\r
505     FILE * f = fopen(filename, "w");\r
506 \r
507     size_t roomIdLen = strlen(session->roomId);\r
508     fwrite(&roomIdLen, sizeof(size_t), 1, f);\r
509     fwrite(session->roomId, 1, roomIdLen, f);\r
510 \r
511     size_t pickleBufferLen =\r
512         olm_pickle_outbound_group_session_length(\r
513             session->session);\r
514     void * pickleBuffer = malloc(pickleBufferLen);\r
515 \r
516     olm_pickle_outbound_group_session(\r
517         session->session,\r
518         key, strlen(key),\r
519         pickleBuffer, pickleBufferLen);\r
520     \r
521     fwrite(&pickleBufferLen, sizeof(size_t), 1, f);\r
522     fwrite(pickleBuffer, 1, pickleBufferLen, f);\r
523     free(pickleBuffer);\r
524 \r
525     fclose(f);\r
526 \r
527     return true;\r
528 }\r
529 \r
530 bool\r
531 MatrixMegolmOutSessionLoad(\r
532     MatrixMegolmOutSession * session,\r
533     const char * filename,\r
534     const char * key)\r
535 {\r
536     FILE * f = fopen(filename, "r");\r
537 \r
538     size_t roomIdLen;\r
539     fread(&roomIdLen, sizeof(size_t), 1, f);\r
540     fread(session->roomId, 1, roomIdLen, f);\r
541     for (int i = roomIdLen; i < ROOM_ID_SIZE; i++)\r
542         session->roomId[i] = '\0';\r
543 \r
544     size_t pickleBufferLen;\r
545     fread(&pickleBufferLen, sizeof(size_t), 1, f);\r
546 \r
547     void * pickleBuffer = malloc(pickleBufferLen);\r
548     fread(pickleBuffer, 1, pickleBufferLen, f);\r
549 \r
550     olm_unpickle_outbound_group_session(\r
551         session->session,\r
552         key, strlen(key),\r
553         pickleBuffer, pickleBufferLen);\r
554     \r
555     free(pickleBuffer);\r
556 \r
557     olm_outbound_group_session_id(session->session, (uint8_t *)session->id, MEGOLM_SESSION_ID_SIZE);\r
558     olm_outbound_group_session_key(session->session, (uint8_t *)session->key, MEGOLM_SESSION_KEY_SIZE);\r
559 \r
560     fclose(f);\r
561 \r
562     return true;\r
563 }\r
564 \r
565 \r
566 \r
567 bool\r
568 MatrixClientInit(\r
569     MatrixClient * client)\r
570 {\r
571     memset(client, 0, sizeof(MatrixClient));\r
572 \r
573     // init olm account\r
574     MatrixOlmAccountInit(&client->olmAccount);\r
575 \r
576     return true;\r
577 }\r
578 \r
579 bool\r
580 MatrixClientSave(\r
581     MatrixClient * client,\r
582     const char * filename)\r
583 {\r
584     FILE * f = fopen(filename, "w");\r
585     \r
586     \r
587     char thisDeviceKey[DEVICE_KEY_SIZE];\r
588     MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
589     char thisSigningKey[DEVICE_KEY_SIZE];\r
590     MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
591 \r
592 \r
593     fwrite(thisDeviceKey, 1, DEVICE_KEY_SIZE, f);\r
594     fwrite(thisSigningKey, 1, DEVICE_KEY_SIZE, f);\r
595     fwrite(client->userId, 1, USER_ID_SIZE, f);\r
596     fwrite(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);\r
597     fwrite(client->deviceId, 1, DEVICE_ID_SIZE, f);\r
598     fwrite(client->expireMs, 1, EXPIRE_MS_SIZE, f);\r
599     fwrite(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f);\r
600 \r
601     fwrite(&client->numDevices, sizeof(int), 1, f);\r
602     for (int i = 0; i < client->numDevices; i++) {\r
603         fwrite(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f);\r
604         fwrite(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f);\r
605     }\r
606 \r
607     fclose(f);\r
608     return true;\r
609 }\r
610 \r
611 bool\r
612 MatrixClientLoad(\r
613     MatrixClient * client,\r
614     const char * filename)\r
615 {\r
616     FILE * f = fopen(filename, "r");\r
617     \r
618     \r
619     char thisDeviceKey[DEVICE_KEY_SIZE];\r
620     MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
621     char thisSigningKey[DEVICE_KEY_SIZE];\r
622     MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
623 \r
624 \r
625     fread(thisDeviceKey, 1, DEVICE_KEY_SIZE, f);\r
626     fread(thisSigningKey, 1, DEVICE_KEY_SIZE, f);\r
627     fread(client->userId, 1, USER_ID_SIZE, f);\r
628     fread(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);\r
629     fread(client->deviceId, 1, DEVICE_ID_SIZE, f);\r
630     fread(client->expireMs, 1, EXPIRE_MS_SIZE, f);\r
631     fread(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f);\r
632 \r
633     fread(&client->numDevices, sizeof(int), 1, f);\r
634     for (int i = 0; i < client->numDevices; i++) {\r
635         fread(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f);\r
636         fread(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f);\r
637     }\r
638 \r
639     fclose(f);\r
640     return true;\r
641 }\r
642 \r
643 bool\r
644 MatrixClientSetAccessToken(\r
645     MatrixClient * client,\r
646     const char * accessToken)\r
647 {\r
648     for (int i = 0; i < ACCESS_TOKEN_SIZE-1; i++)\r
649         client->accessToken[i] = accessToken[i];\r
650     client->accessToken[ACCESS_TOKEN_SIZE-1] = '\0';\r
651 \r
652     return true;\r
653 }\r
654 \r
655 bool\r
656 MatrixClientSetDeviceId(\r
657     MatrixClient * client,\r
658     const char * deviceId)\r
659 {\r
660     for (int i = 0; i < DEVICE_ID_SIZE-1; i++)\r
661         client->deviceId[i] = deviceId[i];\r
662     client->deviceId[DEVICE_ID_SIZE-1] = '\0';\r
663 \r
664     return true;\r
665 }\r
666 \r
667 bool\r
668 MatrixClientSetUserId(\r
669     MatrixClient * client,\r
670     const char * userId)\r
671 {\r
672     for (int i = 0; i < USER_ID_SIZE-1; i++)\r
673         client->userId[i] = userId[i];\r
674     client->userId[USER_ID_SIZE-1] = '\0';\r
675 \r
676     return true;\r
677 }\r
678 \r
679 bool\r
680 MatrixClientGenerateOnetimeKeys(\r
681     MatrixClient * client,\r
682     int numberOfKeys)\r
683 {\r
684     STATIC uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];\r
685     Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
686 \r
687     size_t res =\r
688         olm_account_generate_one_time_keys(client->olmAccount.account,\r
689             numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
690 \r
691     return res != olm_error();\r
692 }\r
693 \r
694 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
695 bool\r
696 MatrixClientUploadOnetimeKeys(\r
697     MatrixClient * client)\r
698 {\r
699     STATIC char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
700 \r
701     mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
702         "{");\r
703 \r
704     STATIC char onetimeKeysBuffer[1024];\r
705     olm_account_one_time_keys(client->olmAccount.account,\r
706         onetimeKeysBuffer, 1024);\r
707 \r
708     const char *keys;\r
709     int keysLen;\r
710     mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);\r
711 \r
712     int koff, klen, voff, vlen, vtype, off = 0;\r
713     while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {\r
714         STATIC char keyJson[JSON_ONETIME_KEY_SIZE];\r
715         \r
716         int keyJsonLen =\r
717             snprintf(keyJson, JSON_ONETIME_KEY_SIZE,\r
718                 "{\"key\":\"%.*s\"}",\r
719                 vlen-2, keys + voff+1);\r
720 \r
721         STATIC char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];\r
722 \r
723         JsonSign(client,\r
724             keyJson, keyJsonLen,\r
725             keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
726         \r
727         mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
728             "\"signed_curve25519:%.*s\":%s,",\r
729             klen-2, keys + koff+1,\r
730             keyJsonSigned);\r
731     }\r
732 \r
733     if (requestBuffer[strlen(requestBuffer)-1] == ',')\r
734         requestBuffer[strlen(requestBuffer)-1] = '\0';\r
735 \r
736     mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
737         "}");\r
738         \r
739     // STATIC char onetimeKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
740     // JsonSign(client,\r
741     //     requestBuffer, strlen(requestBuffer),\r
742     //     onetimeKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
743         \r
744     // STATIC char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
745     // snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
746     // "{\"one_time_keys\":%s}", onetimeKeysSignedBuffer);\r
747     STATIC char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
748     snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
749     "{\"one_time_keys\":%s}", requestBuffer);\r
750 \r
751     STATIC char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
752     MatrixHttpPost(client->hc,\r
753         KEYS_UPLOAD_URL,\r
754         finalEvent,\r
755         responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
756         true);\r
757 \r
758     return true;\r
759 }\r
760 \r
761 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
762 bool\r
763 MatrixClientUploadDeviceKey(\r
764     MatrixClient * client)\r
765 {\r
766     char thisDeviceKey[DEVICE_KEY_SIZE];\r
767     MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
768     char thisSigningKey[DEVICE_KEY_SIZE];\r
769     MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
770 \r
771     STATIC char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
772 \r
773     int deviceKeysBufferLen =\r
774         mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
775             "{"\r
776                 "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"\r
777                 "\"device_id\":\"%s\","\r
778                 "\"keys\":{"\r
779                     "\"curve25519:%s\":\"%s\","\r
780                     "\"ed25519:%s\":\"%s\""\r
781                 "},"\r
782                 "\"user_id\":\"%s\""\r
783             "}",\r
784             client->deviceId,\r
785             client->deviceId, thisDeviceKey,\r
786             client->deviceId, thisSigningKey,\r
787             client->userId);\r
788 \r
789     STATIC char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
790     JsonSign(client,\r
791         deviceKeysBuffer, deviceKeysBufferLen,\r
792         deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
793     \r
794     STATIC char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE+30];\r
795     snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE+30,\r
796     "{\"device_keys\":%s}", deviceKeysSignedBuffer);\r
797 \r
798     STATIC char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
799     MatrixHttpPost(client->hc,\r
800         KEYS_UPLOAD_URL,\r
801         finalEvent,\r
802         responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
803         true);\r
804 \r
805     return true;\r
806 }\r
807 \r
808 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim\r
809 bool\r
810 MatrixClientClaimOnetimeKey(\r
811     MatrixClient * client,\r
812     const char * userId,\r
813     const char * deviceId,\r
814     char * outOnetimeKey, int outOnetimeKeyCap)\r
815 {\r
816     STATIC char requestBuffer[KEYS_CLAIM_REQUEST_SIZE];\r
817     mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE,\r
818     "{"\r
819       "\"one_time_keys\":{"\r
820         "\"%s\":{"\r
821           "\"%s\":\"signed_curve25519\""\r
822         "}"\r
823       "},"\r
824       "\"timeout\":10000"\r
825     "}",\r
826     userId,\r
827     deviceId);\r
828 \r
829     STATIC char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE];\r
830     MatrixHttpPost(client->hc,\r
831         KEYS_CLAIM_URL,\r
832         requestBuffer,\r
833         responseBuffer, KEYS_CLAIM_RESPONSE_SIZE,\r
834         true);\r
835     \r
836     STATIC char userIdEscaped[USER_ID_SIZE];\r
837     JsonEscape(userId, strlen(userId),\r
838         userIdEscaped, USER_ID_SIZE);\r
839     \r
840     STATIC char query[JSON_QUERY_SIZE];\r
841     snprintf(query, JSON_QUERY_SIZE,\r
842         "$.one_time_keys.%s.%s",\r
843         userIdEscaped,\r
844         deviceId);\r
845     \r
846     const char * keyObject;\r
847     int keyObjectSize;\r
848     mjson_find(responseBuffer, strlen(responseBuffer),\r
849         query,\r
850         &keyObject, &keyObjectSize);\r
851     \r
852     int koff, klen, voff, vlen, vtype;\r
853     mjson_next(keyObject, keyObjectSize, 0,\r
854         &koff, &klen, &voff, &vlen, &vtype);\r
855     \r
856     mjson_get_string(keyObject + voff, vlen,\r
857         "$.key", outOnetimeKey, outOnetimeKeyCap);\r
858     \r
859     // TODO:verify signature\r
860     \r
861     return true;\r
862 }\r
863 \r
864 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login\r
865 bool\r
866 MatrixClientLoginPassword(\r
867     MatrixClient * client,\r
868     const char * username,\r
869     const char * password,\r
870     const char * displayName)\r
871 {\r
872     STATIC char requestBuffer[LOGIN_REQUEST_SIZE];\r
873 \r
874     mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,\r
875         "{"\r
876             "\"type\":\"m.login.password\","\r
877             "\"identifier\":{"\r
878                 "\"type\":\"m.id.user\","\r
879                 "\"user\":\"%s\""\r
880             "},"\r
881             "\"password\":\"%s\","\r
882             "\"initial_device_display_name\":\"%s\""\r
883         "}",\r
884         username,\r
885         password,\r
886         displayName);\r
887     \r
888     STATIC char responseBuffer[LOGIN_RESPONSE_SIZE];\r
889     bool result =\r
890         MatrixHttpPost(client->hc,\r
891             LOGIN_URL,\r
892             requestBuffer,\r
893             responseBuffer, LOGIN_RESPONSE_SIZE,\r
894             false);\r
895     \r
896     if (!result)\r
897         return false;\r
898     \r
899     int responseLen = strlen(responseBuffer);\r
900 \r
901     mjson_get_string(responseBuffer, responseLen,\r
902         "$.access_token",\r
903         client->accessToken, ACCESS_TOKEN_SIZE);\r
904     mjson_get_string(responseBuffer, responseLen,\r
905         "$.device_id",\r
906         client->deviceId, DEVICE_ID_SIZE);\r
907     mjson_get_string(responseBuffer, responseLen,\r
908         "$.expires_in_ms",\r
909         client->expireMs, EXPIRE_MS_SIZE);\r
910     mjson_get_string(responseBuffer, responseLen,\r
911         "$.refresh_token",\r
912         client->refreshToken, REFRESH_TOKEN_SIZE);\r
913         \r
914     MatrixHttpSetAccessToken(client->hc, client->accessToken);\r
915 \r
916     return true;\r
917 }\r
918 \r
919 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid\r
920 bool\r
921 MatrixClientSendEvent(\r
922     MatrixClient * client,\r
923     const char * roomId,\r
924     const char * msgType,\r
925     const char * msgBody)\r
926 {    \r
927     STATIC char requestUrl[MAX_URL_LEN];\r
928     sprintf(requestUrl,\r
929         ROOM_SEND_URL, roomId, msgType, (int)time(NULL));\r
930 \r
931     STATIC char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
932     bool result =\r
933         MatrixHttpPut(client->hc,\r
934             requestUrl,\r
935             msgBody,\r
936             responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
937             true);\r
938     \r
939     return result;\r
940 }\r
941 \r
942 // https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted\r
943 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event\r
944 bool\r
945 MatrixClientSendEventEncrypted(\r
946     MatrixClient * client,\r
947     const char * roomId,\r
948     const char * msgType,\r
949     const char * msgBody)\r
950 {\r
951     // event json\r
952     STATIC char requestBuffer[ROOM_SEND_REQUEST_SIZE];\r
953     sprintf(requestBuffer,\r
954         "{"\r
955         "\"type\":\"%s\","\r
956         "\"content\":%s,"\r
957         "\"room_id\":\"%s\""\r
958         "}",\r
959         msgType,\r
960         msgBody,\r
961         roomId);\r
962 \r
963     // get megolm session\r
964     MatrixMegolmOutSession * outSession;\r
965     if (! MatrixClientGetMegolmOutSession(client, roomId, &outSession))\r
966         MatrixClientNewMegolmOutSession(client, roomId, &outSession);\r
967         \r
968     // encrypt\r
969     STATIC char encryptedBuffer[ENCRYPTED_REQUEST_SIZE/10];\r
970     MatrixMegolmOutSessionEncrypt(outSession,\r
971         requestBuffer,\r
972         encryptedBuffer, ENCRYPTED_REQUEST_SIZE);\r
973 \r
974     char thisDeviceKey[DEVICE_KEY_SIZE];\r
975     MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
976     \r
977 \r
978     // encrypted event json\r
979     const char * senderKey = thisDeviceKey;\r
980     const char * sessionId = outSession->id;\r
981     const char * deviceId = client->deviceId;\r
982 \r
983     STATIC char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE/10];\r
984     sprintf(encryptedEventBuffer,\r
985         "{"\r
986         "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
987         "\"ciphertext\":\"%s\","\r
988         "\"device_id\":\"%s\","\r
989         "\"sender_key\":\"%s\","\r
990         "\"session_id\":\"%s\""\r
991         "}",\r
992         encryptedBuffer,\r
993         deviceId,\r
994         senderKey,\r
995         sessionId);\r
996 \r
997     // send\r
998     return MatrixClientSendEvent(client,\r
999         roomId,\r
1000         "m.room.encrypted",\r
1001         encryptedEventBuffer);\r
1002 }\r
1003 \r
1004 // https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync\r
1005 bool\r
1006 MatrixClientSync(\r
1007     MatrixClient * client,\r
1008     char * outSyncBuffer, int outSyncCap,\r
1009     const char * nextBatch)\r
1010 {\r
1011     // filter={\"event_fields\":[\"to_device\"]}\r
1012     STATIC char url[MAX_URL_LEN];\r
1013     snprintf(url, MAX_URL_LEN,\r
1014         "/_matrix/client/v3/sync?timeout=%d%s",\r
1015         SYNC_TIMEOUT,\r
1016         strlen(nextBatch) > 0 ? "&since=" : "");\r
1017     \r
1018     int index = strlen(url);\r
1019 \r
1020     for (size_t i = 0; i < strlen(nextBatch); i++) {\r
1021         char c = nextBatch[i];\r
1022 \r
1023         if (c == '~') {\r
1024             url[index++] = '%';\r
1025             url[index++] = '7';\r
1026             url[index++] = 'E';\r
1027         }\r
1028         else {\r
1029             url[index++] = c;\r
1030         }\r
1031     }\r
1032     url[index] = '\0';\r
1033 \r
1034     return\r
1035         MatrixHttpGet(client->hc,\r
1036             url,\r
1037             outSyncBuffer, outSyncCap,\r
1038             true);\r
1039 }\r
1040 \r
1041 // https://spec.matrix.org/v1.7/client-server-api/#get_matrixclientv3roomsroomideventeventid\r
1042 bool\r
1043 MatrixClientGetRoomEvent(\r
1044     MatrixClient * client,\r
1045     const char * roomId,\r
1046     const char * eventId,\r
1047     char * outEvent, int outEventCap)\r
1048 {\r
1049     STATIC char url[MAX_URL_LEN];\r
1050     snprintf(url, MAX_URL_LEN,\r
1051         "/_matrix/client/v3/rooms/%s/event/%s",\r
1052             roomId,\r
1053             eventId);\r
1054 \r
1055     return\r
1056         MatrixHttpGet(client->hc,\r
1057             url,\r
1058             outEvent, outEventCap,\r
1059             true);\r
1060 }\r
1061 \r
1062 bool\r
1063 MatrixClientShareMegolmOutSession(\r
1064     MatrixClient * client,\r
1065     const char * userId,\r
1066     const char * deviceId,\r
1067     MatrixMegolmOutSession * session)\r
1068 {\r
1069     // generate room key event\r
1070     STATIC char eventBuffer[KEY_SHARE_EVENT_LEN];\r
1071     sprintf(eventBuffer,\r
1072         "{"\r
1073             "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
1074             "\"room_id\":\"%s\","\r
1075             "\"session_id\":\"%s\","\r
1076             "\"session_key\":\"%s\""\r
1077         "}",\r
1078         session->roomId,\r
1079         session->id,\r
1080         session->key\r
1081     );\r
1082 \r
1083     // send\r
1084     MatrixClientSendToDeviceEncrypted(client,\r
1085         userId,\r
1086         deviceId,\r
1087         eventBuffer,\r
1088         "m.room_key");\r
1089 \r
1090     return true;\r
1091 }\r
1092 \r
1093 bool\r
1094 MatrixClientShareMegolmOutSessionTest(\r
1095     MatrixClient * client,\r
1096     const char * userId,\r
1097     const char * deviceId,\r
1098     MatrixMegolmOutSession * session)\r
1099 {\r
1100     // generate room key event\r
1101     char eventBuffer[KEY_SHARE_EVENT_LEN];\r
1102     sprintf(eventBuffer,\r
1103         "{"\r
1104             "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
1105             "\"room_id\":\"%s\","\r
1106             "\"session_id\":\"%s\","\r
1107             "\"session_key\":\"%s\""\r
1108         "}",\r
1109         session->roomId,\r
1110         session->id,\r
1111         session->key\r
1112     );\r
1113 \r
1114     // send\r
1115     MatrixClientSendToDevice(client,\r
1116         userId,\r
1117         deviceId,\r
1118         eventBuffer,\r
1119         "m.room_key");\r
1120 \r
1121     return true;\r
1122 }\r
1123 \r
1124 bool\r
1125 MatrixClientGetMegolmOutSession(\r
1126     MatrixClient * client,\r
1127     const char * roomId,\r
1128     MatrixMegolmOutSession ** outSession)\r
1129 {\r
1130     for (int i = 0; i < client->numMegolmOutSessions; i++)\r
1131     {\r
1132         if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)\r
1133         {\r
1134             *outSession = &client->megolmOutSessions[i];\r
1135             return true;\r
1136         }\r
1137     }\r
1138 \r
1139     return false;\r
1140 }\r
1141 \r
1142 bool\r
1143 MatrixClientNewMegolmOutSession(\r
1144     MatrixClient * client,\r
1145     const char * roomId,\r
1146     MatrixMegolmOutSession ** outSession)\r
1147 {\r
1148     if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)\r
1149     {\r
1150         MatrixMegolmOutSession * result =\r
1151             &client->megolmOutSessions[client->numMegolmOutSessions];\r
1152         \r
1153         MatrixMegolmOutSessionInit(result,\r
1154             roomId);\r
1155 \r
1156         *outSession = result;\r
1157 \r
1158         client->numMegolmOutSessions++;\r
1159 \r
1160         return true;\r
1161     }\r
1162 \r
1163     return false;\r
1164 }\r
1165 \r
1166 bool\r
1167 MatrixClientGetMegolmInSession(\r
1168     MatrixClient * client,\r
1169     const char * roomId, int roomIdLen,\r
1170     const char * sessionId, int sessionIdLen,\r
1171     MatrixMegolmInSession ** outSession)\r
1172 {\r
1173     for (int i = 0; i < client->numMegolmInSessions; i++)\r
1174     {\r
1175         if (strncmp(client->megolmInSessions[i].roomId, roomId, roomIdLen) == 0 &&\r
1176             strncmp(client->megolmInSessions[i].id, sessionId, sessionIdLen) == 0)\r
1177         {\r
1178             *outSession = &client->megolmInSessions[i];\r
1179             return true;\r
1180         }\r
1181     }\r
1182 \r
1183     return false;\r
1184 }\r
1185 \r
1186 bool\r
1187 MatrixClientNewMegolmInSession(\r
1188     MatrixClient * client,\r
1189     const char * roomId,\r
1190     const char * sessionId,\r
1191     const char * sessionKey,\r
1192     MatrixMegolmInSession ** outSession)\r
1193 {\r
1194     if (client->numMegolmInSessions < NUM_MEGOLM_SESSIONS)\r
1195     {\r
1196         MatrixMegolmInSession * result =\r
1197             &client->megolmInSessions[client->numMegolmInSessions];\r
1198         \r
1199         MatrixMegolmInSessionInit(result,\r
1200             roomId,\r
1201             sessionId,\r
1202             sessionKey, strlen(sessionKey));\r
1203         \r
1204         *outSession = result;\r
1205 \r
1206         client->numMegolmInSessions++;\r
1207 \r
1208         return true;\r
1209     }\r
1210 \r
1211     return false;\r
1212 }\r
1213 \r
1214 bool\r
1215 MatrixClientRequestMegolmInSession(\r
1216     MatrixClient * client,\r
1217     const char * roomId,\r
1218     const char * sessionId,\r
1219     const char * senderKey,\r
1220     const char * userId,\r
1221     const char * deviceId)\r
1222 {\r
1223     // TODO: cancel requests\r
1224     MatrixClientSendDummy(client, userId, deviceId);\r
1225 \r
1226     STATIC char event[ROOMKEY_REQUEST_SIZE];\r
1227     snprintf(event, ROOMKEY_REQUEST_SIZE,\r
1228         "{"\r
1229             "\"action\":\"request\","\r
1230             "\"body\":{"\r
1231                 "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
1232                 "\"room_id\":\"%s\","\r
1233                 "\"sender_key\":\"%s\","\r
1234                 "\"session_id\":\"%s\""\r
1235             "},"\r
1236             "\"request_id\":\"%lld\","\r
1237             "\"requesting_device_id\":\"%s\""\r
1238         "}",\r
1239         roomId,\r
1240         senderKey,\r
1241         sessionId,\r
1242         time(NULL),\r
1243         client->deviceId);\r
1244 \r
1245     \r
1246     MatrixClientSendToDevice(client,\r
1247         userId,\r
1248         deviceId,\r
1249         event,\r
1250         "m.room_key_request");\r
1251 \r
1252     return true;\r
1253 }\r
1254 \r
1255 bool\r
1256 MatrixClientGetOlmSessionIn(\r
1257     MatrixClient * client,\r
1258     const char * userId,\r
1259     const char * deviceId,\r
1260     MatrixOlmSession ** outSession)\r
1261 {\r
1262     for (int i = 0; i < client->numOlmSessions; i++)\r
1263     {\r
1264         if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)\r
1265         {\r
1266             *outSession = &client->olmSessions[i];\r
1267             return true;\r
1268         }\r
1269     }\r
1270 \r
1271     return false;\r
1272 }\r
1273 \r
1274 bool\r
1275 MatrixClientNewOlmSessionIn(\r
1276     MatrixClient * client,\r
1277     const char * userId,\r
1278     const char * deviceId,\r
1279     const char * encrypted,\r
1280     MatrixOlmSession ** outSession)\r
1281 {\r
1282     if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
1283     {\r
1284         STATIC char deviceKey[DEVICE_KEY_SIZE];\r
1285         MatrixClientRequestDeviceKey(client,\r
1286             deviceId,\r
1287             deviceKey, DEVICE_KEY_SIZE);\r
1288 \r
1289         MatrixOlmSessionFrom(\r
1290             &client->olmSessions[client->numOlmSessions],\r
1291             client->olmAccount.account,\r
1292             deviceId,\r
1293             deviceKey,\r
1294             encrypted);\r
1295 \r
1296         *outSession = &client->olmSessions[client->numOlmSessions];\r
1297         \r
1298         client->numOlmSessions++;\r
1299 \r
1300         return true;\r
1301     }\r
1302 \r
1303     return false;\r
1304 }\r
1305 \r
1306 bool\r
1307 MatrixClientGetOlmSessionOut(\r
1308     MatrixClient * client,\r
1309     const char * userId,\r
1310     const char * deviceId,\r
1311     MatrixOlmSession ** outSession)\r
1312 {\r
1313     for (int i = 0; i < client->numOlmSessions; i++)\r
1314     {\r
1315         if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)\r
1316         {\r
1317             *outSession = &client->olmSessions[i];\r
1318             return true;\r
1319         }\r
1320     }\r
1321 \r
1322     return false;\r
1323 }\r
1324 \r
1325 bool\r
1326 MatrixClientNewOlmSessionOut(\r
1327     MatrixClient * client,\r
1328     const char * userId,\r
1329     const char * deviceId,\r
1330     MatrixOlmSession ** outSession)\r
1331 {\r
1332     if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
1333     {\r
1334         STATIC char deviceKey[DEVICE_KEY_SIZE];\r
1335         MatrixClientRequestDeviceKey(client,\r
1336             deviceId,\r
1337             deviceKey, DEVICE_KEY_SIZE);\r
1338 \r
1339         char onetimeKey[ONETIME_KEY_SIZE];\r
1340         MatrixClientClaimOnetimeKey(client,\r
1341             userId,\r
1342             deviceId,\r
1343             onetimeKey, ONETIME_KEY_SIZE);\r
1344 \r
1345         MatrixOlmSessionTo(\r
1346             &client->olmSessions[client->numOlmSessions],\r
1347             client->olmAccount.account,\r
1348             deviceId,\r
1349             deviceKey,\r
1350             onetimeKey);\r
1351 \r
1352         *outSession = &client->olmSessions[client->numOlmSessions];\r
1353         \r
1354         client->numOlmSessions++;\r
1355 \r
1356         return true;\r
1357     }\r
1358 \r
1359     return false;\r
1360 }\r
1361 \r
1362 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid\r
1363 bool\r
1364 MatrixClientSendToDevice(\r
1365     MatrixClient * client,\r
1366     const char * userId,\r
1367     const char * deviceId,\r
1368     const char * message,\r
1369     const char * msgType)\r
1370 {\r
1371     STATIC char requestUrl[MAX_URL_LEN];\r
1372     sprintf(requestUrl,\r
1373         TODEVICE_URL, msgType, (int)time(NULL));\r
1374 \r
1375     STATIC char eventBuffer[TODEVICE_EVENT_SIZE];\r
1376     snprintf(eventBuffer, TODEVICE_EVENT_SIZE,\r
1377         "{"\r
1378             "\"messages\":{"\r
1379                 "\"%s\":{"\r
1380                     "\"%s\":%s"\r
1381                 "}"\r
1382             "}"\r
1383         "}",\r
1384         userId,\r
1385         deviceId,\r
1386         message);\r
1387 \r
1388     STATIC char responseBuffer[ROOM_SEND_RESPONSE_SIZE];\r
1389     bool result =\r
1390         MatrixHttpPut(client->hc,\r
1391             requestUrl,\r
1392             eventBuffer,\r
1393             responseBuffer, ROOM_SEND_RESPONSE_SIZE,\r
1394             true);\r
1395     \r
1396     printf("%s\n", responseBuffer);\r
1397     \r
1398     return result;\r
1399 }\r
1400 \r
1401 bool\r
1402 MatrixClientSendToDeviceEncrypted(\r
1403     MatrixClient * client,\r
1404     const char * userId,\r
1405     const char * deviceId,\r
1406     const char * message,\r
1407     const char * msgType)\r
1408 {\r
1409     // get olm session\r
1410     MatrixOlmSession * olmSession;\r
1411     if (! MatrixClientGetOlmSessionOut(client, userId, deviceId, &olmSession))\r
1412         MatrixClientNewOlmSessionOut(client, userId, deviceId, &olmSession);\r
1413 \r
1414     // create event json\r
1415     char targetDeviceKey[DEVICE_KEY_SIZE];\r
1416     MatrixClientRequestDeviceKey(client, deviceId, targetDeviceKey, DEVICE_KEY_SIZE);\r
1417     char targetSigningKey[SIGNING_KEY_SIZE];\r
1418     MatrixClientRequestSigningKey(client, deviceId, targetSigningKey, SIGNING_KEY_SIZE);\r
1419     \r
1420     char thisSigningKey[DEVICE_KEY_SIZE];\r
1421     MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
1422 \r
1423     STATIC char eventBuffer[TODEVICE_EVENT_SIZE];\r
1424     sprintf(eventBuffer,\r
1425         "{"\r
1426         "\"type\":\"%s\","\r
1427         "\"content\":%s,"\r
1428         "\"sender\":\"%s\","\r
1429         "\"recipient\":\"%s\","\r
1430         "\"recipient_keys\":{"\r
1431           "\"ed25519\":\"%s\""\r
1432         "},"\r
1433         "\"keys\":{"\r
1434           "\"ed25519\":\"%s\""\r
1435         "}"\r
1436         "}",\r
1437         msgType,\r
1438         message,\r
1439         client->userId,\r
1440         userId, // recipient user id\r
1441         targetSigningKey, // recipient device key\r
1442         thisSigningKey);\r
1443 \r
1444     // encrypt\r
1445     STATIC char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];\r
1446     MatrixOlmSessionEncrypt(olmSession,\r
1447         eventBuffer,\r
1448         encryptedBuffer, ENCRYPTED_REQUEST_SIZE);\r
1449 \r
1450     char thisDeviceKey[DEVICE_KEY_SIZE];\r
1451     MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
1452 \r
1453 \r
1454     STATIC char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];\r
1455     sprintf(encryptedEventBuffer,\r
1456         "{"\r
1457         "\"algorithm\":\"m.olm.v1.curve25519-aes-sha2\","\r
1458         "\"ciphertext\":{"\r
1459           "\"%s\":{"\r
1460             "\"body\":\"%s\","\r
1461             "\"type\":%d"\r
1462           "}"\r
1463         "},"\r
1464         "\"device_id\":\"%s\","\r
1465         "\"sender_key\":\"%s\""\r
1466         "}",\r
1467         targetDeviceKey,\r
1468         encryptedBuffer,\r
1469         olm_session_has_received_message(olmSession->session),\r
1470         client->deviceId,\r
1471         thisDeviceKey);\r
1472 \r
1473     // send\r
1474     return MatrixClientSendToDevice(\r
1475         client,\r
1476         userId,\r
1477         deviceId,\r
1478         encryptedEventBuffer,\r
1479         "m.room.encrypted");\r
1480 }\r
1481 \r
1482 bool\r
1483 MatrixClientSendDummy(\r
1484     MatrixClient * client,\r
1485     const char * userId,\r
1486     const char * deviceId)\r
1487 {\r
1488     return MatrixClientSendToDeviceEncrypted(\r
1489         client,\r
1490         userId,\r
1491         deviceId,\r
1492         "{}",\r
1493         "m.dummy");\r
1494 }\r
1495 \r
1496 bool\r
1497 MatrixClientFindDevice(\r
1498     MatrixClient * client,\r
1499     const char * deviceId,\r
1500     MatrixDevice ** outDevice)\r
1501 {\r
1502     MatrixClientRequestDeviceKeys(client);\r
1503 \r
1504     for (int i = 0; i < client->numDevices; i++)\r
1505     {\r
1506         if (strcmp(client->devices[i].deviceId, deviceId) == 0)\r
1507         {\r
1508             *outDevice = &client->devices[i];\r
1509             return true;\r
1510         }\r
1511     }\r
1512 \r
1513     *outDevice = NULL;\r
1514     return false;\r
1515 }\r
1516 \r
1517 bool\r
1518 MatrixClientRequestDeviceKey(\r
1519     MatrixClient * client,\r
1520     const char * deviceId,\r
1521     char * outDeviceKey, int outDeviceKeyCap)\r
1522 {\r
1523     MatrixDevice * device;\r
1524     \r
1525     if (MatrixClientFindDevice(client, deviceId, &device))\r
1526     {\r
1527         strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);\r
1528         return true;\r
1529     }\r
1530 \r
1531     MatrixClientRequestDeviceKeys(client);\r
1532     \r
1533     if (MatrixClientFindDevice(client, deviceId, &device))\r
1534     {\r
1535         strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);\r
1536         return true;\r
1537     }\r
1538 \r
1539     return false;\r
1540 }\r
1541 \r
1542 bool\r
1543 MatrixClientRequestSigningKey(\r
1544     MatrixClient * client,\r
1545     const char * deviceId,\r
1546     char * outSigningKey, int outSigningKeyCap)\r
1547 {\r
1548     MatrixDevice * device;\r
1549     \r
1550     if (MatrixClientFindDevice(client, deviceId, &device))\r
1551     {\r
1552         strncpy(outSigningKey, device->signingKey, outSigningKeyCap);\r
1553         return true;\r
1554     }\r
1555 \r
1556     MatrixClientRequestDeviceKeys(client);\r
1557     \r
1558     if (MatrixClientFindDevice(client, deviceId, &device))\r
1559     {\r
1560         strncpy(outSigningKey, device->signingKey, outSigningKeyCap);\r
1561         return true;\r
1562     }\r
1563 \r
1564     return false;\r
1565 }\r
1566 \r
1567 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery\r
1568 bool\r
1569 MatrixClientRequestDeviceKeys(\r
1570     MatrixClient * client)\r
1571 {\r
1572     if (client->numDevices >= NUM_DEVICES) {\r
1573         printf("Maximum number of devices reached\n");\r
1574         return false;\r
1575     }\r
1576 \r
1577     STATIC char userIdEscaped[USER_ID_SIZE];\r
1578     JsonEscape(client->userId, strlen(client->userId),\r
1579         userIdEscaped, USER_ID_SIZE);\r
1580 \r
1581     STATIC char request[KEYS_QUERY_REQUEST_SIZE];\r
1582     snprintf(request, KEYS_QUERY_REQUEST_SIZE,\r
1583         "{\"device_keys\":{\"%s\":[]}}", client->userId);\r
1584 \r
1585     STATIC char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];\r
1586     bool requestResult = MatrixHttpPost(client->hc,\r
1587         KEYS_QUERY_URL,\r
1588         request,\r
1589         responseBuffer, KEYS_QUERY_RESPONSE_SIZE,\r
1590         true);\r
1591 \r
1592     if (! requestResult)\r
1593         return false;\r
1594 \r
1595     // query for retrieving device keys for user id\r
1596     STATIC char query[JSON_QUERY_SIZE];\r
1597     snprintf(query, JSON_QUERY_SIZE,\r
1598         "$.device_keys.%s", userIdEscaped);\r
1599     \r
1600     const char * s;\r
1601     int slen;\r
1602     mjson_find(responseBuffer, strlen(responseBuffer),\r
1603         query, &s, &slen);\r
1604     \r
1605     // loop over keys\r
1606     \r
1607     int koff, klen, voff, vlen, vtype, off = 0;\r
1608     for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,\r
1609                                     &voff, &vlen, &vtype)) != 0; ) {\r
1610         const char * key = s + koff;\r
1611         const char * val = s + voff;\r
1612 \r
1613         // set device id, "key" is the JSON key\r
1614         MatrixDevice d;\r
1615         snprintf(d.deviceId, DEVICE_ID_SIZE,\r
1616             "%.*s", klen-2, key+1);\r
1617 \r
1618         // look for device key in value\r
1619         STATIC char deviceKeyQuery[JSON_QUERY_SIZE];\r
1620         snprintf(deviceKeyQuery, JSON_QUERY_SIZE,\r
1621             "$.keys.curve25519:%s", d.deviceId);\r
1622         mjson_get_string(val, vlen,\r
1623             deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);\r
1624 \r
1625         // look for signing key in value\r
1626         STATIC char signingKeyQuery[JSON_QUERY_SIZE];\r
1627         snprintf(signingKeyQuery, JSON_QUERY_SIZE,\r
1628             "$.keys.ed25519:%s", d.deviceId);\r
1629         mjson_get_string(val, vlen,\r
1630             signingKeyQuery, d.signingKey, SIGNING_KEY_SIZE);\r
1631 \r
1632         // add device\r
1633         if (client->numDevices < NUM_DEVICES)\r
1634         {\r
1635             bool foundDevice = false;\r
1636             for (int i = 0; i < client->numDevices; i++)\r
1637                 if (strcmp(client->devices[i].deviceId, d.deviceId) == 0)\r
1638                     foundDevice = true;\r
1639 \r
1640             if (! foundDevice) {\r
1641                 printf("new device: %s %s %s\n", d.deviceId, d.deviceKey, d.signingKey);\r
1642                 client->devices[client->numDevices] = d;\r
1643                 client->numDevices++;\r
1644             }\r
1645         }\r
1646         else\r
1647         {\r
1648             return false;\r
1649         }\r
1650     }\r
1651 \r
1652     return true;\r
1653 }\r
1654 \r
1655 bool\r
1656 MatrixClientDeleteDevice(\r
1657     MatrixClient * client)\r
1658 {\r
1659     STATIC char deleteRequest[1024];\r
1660     snprintf(deleteRequest, 1024,\r
1661         "{\"devices\":[\"%s\"]}",\r
1662         client->deviceId);\r
1663     STATIC char deleteResponse[1024];\r
1664     bool res = MatrixHttpPost(client->hc, "/_matrix/client/v3/delete_devices",\r
1665         deleteRequest, deleteResponse, 1024, true);\r
1666     return res;\r
1667 }