8 #define LOGIN_REQUEST_SIZE 1024
\r
9 #define LOGIN_RESPONSE_SIZE 1024
\r
10 #define LOGIN_URL "/_matrix/client/v3/login"
\r
12 #define ENCRYPTED_REQUEST_SIZE (1024*5)
\r
13 #define ENCRYPTED_EVENT_SIZE (1024*10)
\r
14 #define ROOMEVENT_REQUEST_SIZE 256
\r
15 #define ROOMEVENT_RESPONSE_SIZE 1024
\r
16 #define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"
\r
18 #define TODEVICE_EVENT_SIZE (1024*5)
\r
19 #define TODEVICE_URL "/_matrix/client/v3/sendToDevice/%s/%d"
\r
21 #define KEYS_QUERY_URL "/_matrix/client/v3/keys/query"
\r
22 #define KEYS_QUERY_REQUEST_SIZE 256
\r
23 #define KEYS_QUERY_RESPONSE_SIZE (1024*10)
\r
25 #define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload"
\r
26 #define KEYS_UPLOAD_REQUEST_SIZE 1024
\r
27 #define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048
\r
28 #define KEYS_UPLOAD_RESPONSE_SIZE 2048
\r
30 #define KEYS_CLAIM_URL "/_matrix/client/v3/keys/claim"
\r
31 #define KEYS_CLAIM_REQUEST_SIZE 1024
\r
32 #define KEYS_CLAIM_RESPONSE_SIZE 1024
\r
34 #define JSON_QUERY_SIZE 128
\r
43 static bool first = true;
\r
44 if (first) { srand(time(0)); first = false; }
\r
46 for (int i = 0; i < randomLen; i++)
\r
48 random[i] = rand() % 256;
\r
54 const char * sIn, int sInLen,
\r
55 char * sOut, int sOutCap)
\r
59 for (int i = 0; i < sInLen; i++)
\r
64 if (sIn[i] == '.' ||
\r
68 sOut[sOutIndex++] = '\\';
\r
70 sOut[sOutIndex++] = sIn[i];
\r
73 if (sOutIndex < sOutCap)
\r
74 sOut[sOutIndex] = '\0';
\r
80 MatrixClient * client,
\r
81 const char * sIn, int sInLen,
\r
82 char * sOut, int sOutCap)
\r
84 static char signature[OLM_SIGNATURE_SIZE];
\r
86 olm_account_sign(client->olmAccount.account,
\r
88 signature, OLM_SIGNATURE_SIZE);
\r
90 int signatureLen = res;
\r
92 static char signatureJson[JSON_SIGNATURE_SIZE];
\r
93 int signatureJsonLen =
\r
94 mjson_snprintf(signatureJson, JSON_SIGNATURE_SIZE,
\r
98 "\"ed25519:%s\":\"%.*s\""
\r
104 signatureLen, signature);
\r
106 struct mjson_fixedbuf result = { sOut, sOutCap, 0 };
\r
109 signatureJson, signatureJsonLen,
\r
110 mjson_print_fixed_buf,
\r
118 MatrixOlmAccountInit(
\r
119 MatrixOlmAccount * account)
\r
121 account->account = olm_account(account->memory);
\r
123 static uint8_t random[OLM_ACCOUNT_RANDOM_SIZE];
\r
124 Randomize(random, OLM_ACCOUNT_RANDOM_SIZE);
\r
126 size_t res = olm_create_account(
\r
129 OLM_ACCOUNT_RANDOM_SIZE);
\r
131 return res != olm_error();
\r
134 // TODO: in/outbound sessions
\r
136 MatrixOlmSessionTo(
\r
137 MatrixOlmSession * session,
\r
138 OlmAccount * olmAccount,
\r
139 const char * deviceId,
\r
140 const char * deviceKey,
\r
141 const char * deviceOnetimeKey)
\r
143 memset(session, 0, sizeof(MatrixOlmSession));
\r
145 session->deviceId = deviceId;
\r
148 olm_session(session->memory);
\r
150 static uint8_t random[OLM_OUTBOUND_SESSION_RANDOM_SIZE];
\r
151 Randomize(random, OLM_OUTBOUND_SESSION_RANDOM_SIZE);
\r
154 olm_create_outbound_session(session->session,
\r
156 deviceKey, strlen(deviceKey),
\r
157 deviceOnetimeKey, strlen(deviceOnetimeKey),
\r
158 random, OLM_OUTBOUND_SESSION_RANDOM_SIZE);
\r
160 if (res == olm_error()) {
\r
161 printf("error olm: %s\n", olm_account_last_error(olmAccount));
\r
164 return session->session != NULL;
\r
168 MatrixOlmSessionEncrypt(
\r
169 MatrixOlmSession * session,
\r
170 const char * plaintext,
\r
171 char * outBuffer, int outBufferCap)
\r
173 static uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];
\r
174 Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);
\r
176 size_t res = olm_encrypt(session->session,
\r
177 plaintext, strlen(plaintext),
\r
178 random, OLM_ENCRYPT_RANDOM_SIZE,
\r
179 outBuffer, outBufferCap);
\r
181 return res != olm_error();
\r
184 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session
\r
186 MatrixMegolmOutSessionInit(
\r
187 MatrixMegolmOutSession * session,
\r
188 const char * roomId)
\r
190 memset(session, 0, sizeof(MatrixMegolmOutSession));
\r
192 static uint8_t random[MEGOLM_INIT_RANDOM_SIZE];
\r
193 Randomize(random, MEGOLM_INIT_RANDOM_SIZE);
\r
195 session->roomId = roomId;
\r
198 olm_outbound_group_session(session->memory);
\r
200 olm_init_outbound_group_session(
\r
203 MEGOLM_INIT_RANDOM_SIZE);
\r
205 olm_outbound_group_session_id(session->session,
\r
206 (uint8_t *)session->id,
\r
207 MEGOLM_SESSION_ID_SIZE);
\r
209 olm_outbound_group_session_key(session->session,
\r
210 (uint8_t *)session->key,
\r
211 MEGOLM_SESSION_KEY_SIZE);
\r
217 MatrixMegolmOutSessionEncrypt(
\r
218 MatrixMegolmOutSession * session,
\r
219 const char * plaintext,
\r
220 char * outBuffer, int outBufferCap)
\r
222 size_t res = olm_group_encrypt(session->session,
\r
223 (uint8_t *)plaintext, strlen(plaintext),
\r
224 (uint8_t *)outBuffer, outBufferCap);
\r
226 return res != olm_error();
\r
233 MatrixClient * client,
\r
234 const char * server)
\r
236 memset(client, 0, sizeof(MatrixClient));
\r
238 strcpy(client->server, server);
\r
240 // init olm account
\r
241 MatrixOlmAccountInit(&client->olmAccount);
\r
244 static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];
\r
246 olm_account_identity_keys(
\r
247 client->olmAccount.account,
\r
249 OLM_IDENTITY_KEYS_JSON_SIZE);
\r
251 mjson_get_string(deviceKeysJson, res,
\r
253 client->deviceKey, DEVICE_KEY_SIZE);
\r
254 mjson_get_string(deviceKeysJson, res,
\r
256 client->signingKey, SIGNING_KEY_SIZE);
\r
262 MatrixClientSetAccessToken(
\r
263 MatrixClient * client,
\r
264 const char * accessToken)
\r
266 int accessTokenLen = strlen(accessToken);
\r
268 if (accessTokenLen > ACCESS_TOKEN_SIZE - 1)
\r
271 for (int i = 0; i < accessTokenLen; i++)
\r
272 client->accessToken[i] = accessToken[i];
\r
278 MatrixClientSetDeviceId(
\r
279 MatrixClient * client,
\r
280 const char * deviceId)
\r
282 int deviceIdLen = strlen(deviceId);
\r
284 if (deviceIdLen > DEVICE_ID_SIZE - 1)
\r
287 for (int i = 0; i < deviceIdLen; i++)
\r
288 client->deviceId[i] = deviceId[i];
\r
294 MatrixClientSetUserId(
\r
295 MatrixClient * client,
\r
296 const char * userId)
\r
298 int userIdLen = strlen(userId);
\r
300 if (userIdLen > USER_ID_SIZE - 1)
\r
303 for (int i = 0; i < userIdLen; i++)
\r
304 client->userId[i] = userId[i];
\r
310 MatrixClientGenerateOnetimeKeys(
\r
311 MatrixClient * client,
\r
314 static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];
\r
315 Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);
\r
318 olm_account_generate_one_time_keys(client->olmAccount.account,
\r
319 numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);
\r
321 return res != olm_error();
\r
324 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload
\r
326 MatrixClientUploadOnetimeKeys(
\r
327 MatrixClient * client)
\r
329 static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];
\r
331 mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
332 "{\"one_time_keys\":{");
\r
334 static char onetimeKeysBuffer[1024];
\r
335 olm_account_one_time_keys(client->olmAccount.account,
\r
336 onetimeKeysBuffer, 1024);
\r
340 mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);
\r
342 int koff, klen, voff, vlen, vtype, off = 0;
\r
343 while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {
\r
344 static char keyJson[JSON_ONETIME_KEY_SIZE];
\r
346 snprintf(keyJson, JSON_ONETIME_KEY_SIZE,
\r
347 "{\"key\":\"%.*s\"}",
\r
348 vlen-2, keys + voff+1);
\r
350 static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];
\r
353 keyJson, JSON_ONETIME_KEY_SIZE,
\r
354 keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);
\r
356 mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),
\r
357 "\"signed_curve25519:%.*s\":%s,",
\r
358 klen-2, keys + koff+1,
\r
362 mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),
\r
365 static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];
\r
366 MatrixHttpPost(client,
\r
369 responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,
\r
375 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload
\r
377 MatrixClientUploadDeviceKeys(
\r
378 MatrixClient * client)
\r
380 static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];
\r
382 mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
383 "{\"device_keys\":{"
\r
384 "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"
\r
385 "\"device_id\":\"%s\","
\r
387 "\"curve25519:%s\":\"%s\","
\r
388 "\"ed25519:%s\":\"%s\""
\r
390 "\"user_id\":\"%s\""
\r
393 client->deviceId, client->deviceKey,
\r
394 client->deviceId, client->signingKey,
\r
397 static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];
\r
399 deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
400 deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);
\r
403 static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];
\r
404 MatrixHttpPost(client,
\r
406 deviceKeysSignedBuffer,
\r
407 responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,
\r
413 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim
\r
415 MatrixClientClaimOnetimeKey(
\r
416 MatrixClient * client,
\r
417 const char * userId,
\r
418 const char * deviceId,
\r
419 char * outOnetimeKey, int outOnetimeKeyCap)
\r
421 static char requestBuffer[KEYS_CLAIM_REQUEST_SIZE];
\r
422 mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE,
\r
424 "\"one_time_keys\": {"
\r
426 "\"%s\": \"signed_curve25519\""
\r
429 "\"timeout\": 10000"
\r
434 static char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE];
\r
435 MatrixHttpPost(client,
\r
438 responseBuffer, KEYS_CLAIM_RESPONSE_SIZE,
\r
441 char userIdEscaped[USER_ID_SIZE];
\r
442 JsonEscape(userId, strlen(userId),
\r
443 userIdEscaped, USER_ID_SIZE);
\r
445 static char query[JSON_QUERY_SIZE];
\r
446 snprintf(query, JSON_QUERY_SIZE,
\r
447 "$.one_time_keys.%s.%s",
\r
451 const char * keyObject;
\r
453 mjson_find(responseBuffer, strlen(responseBuffer),
\r
455 &keyObject, &keyObjectSize);
\r
457 int koff, klen, voff, vlen, vtype;
\r
458 mjson_next(keyObject, keyObjectSize, 0,
\r
459 &koff, &klen, &voff, &vlen, &vtype);
\r
461 mjson_get_string(keyObject + voff, vlen,
\r
462 "$.key", outOnetimeKey, outOnetimeKeyCap);
\r
464 // TODO: verify signature
\r
469 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login
\r
471 MatrixClientLoginPassword(
\r
472 MatrixClient * client,
\r
473 const char * username,
\r
474 const char * password,
\r
475 const char * displayName)
\r
477 static char requestBuffer[LOGIN_REQUEST_SIZE];
\r
479 mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,
\r
481 "\"type\": \"m.login.password\","
\r
482 "\"identifier\": {"
\r
483 "\"type\": \"m.id.user\","
\r
486 "\"password\": \"%s\","
\r
487 "\"initial_device_display_name\": \"%s\""
\r
493 static char responseBuffer[LOGIN_RESPONSE_SIZE];
\r
495 MatrixHttpPost(client,
\r
498 responseBuffer, LOGIN_RESPONSE_SIZE,
\r
501 int responseLen = strlen(responseBuffer);
\r
506 mjson_get_string(responseBuffer, responseLen,
\r
508 client->accessToken, ACCESS_TOKEN_SIZE);
\r
509 mjson_get_string(responseBuffer, responseLen,
\r
511 client->deviceId, DEVICE_ID_SIZE);
\r
512 mjson_get_string(responseBuffer, responseLen,
\r
514 client->expireMs, EXPIRE_MS_SIZE);
\r
515 mjson_get_string(responseBuffer, responseLen,
\r
517 client->refreshToken, REFRESH_TOKEN_SIZE);
\r
522 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
\r
524 MatrixClientSendEvent(
\r
525 MatrixClient * client,
\r
526 const char * roomId,
\r
527 const char * msgType,
\r
528 const char * msgBody)
\r
530 static char requestUrl[MAX_URL_LEN];
\r
531 sprintf(requestUrl,
\r
532 ROOMEVENT_URL, roomId, msgType, (int)time(NULL));
\r
534 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
536 MatrixHttpPut(client,
\r
539 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
545 // https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted
\r
546 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event
\r
548 MatrixClientSendEventEncrypted(
\r
549 MatrixClient * client,
\r
550 const char * roomId,
\r
551 const char * msgType,
\r
552 const char * msgBody)
\r
555 static char requestBuffer[ROOMEVENT_REQUEST_SIZE];
\r
556 sprintf(requestBuffer,
\r
560 "\"room_id\":\"%s\""
\r
566 // get megolm session
\r
567 MatrixMegolmOutSession * outSession;
\r
568 MatrixClientGetMegolmOutSession(client, roomId, &outSession);
\r
571 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
572 MatrixMegolmOutSessionEncrypt(outSession,
\r
574 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
576 // encrypted event json
\r
577 const char * senderKey = client->deviceKey;
\r
578 const char * sessionId = outSession->id;
\r
579 const char * deviceId = client->deviceId;
\r
581 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
582 sprintf(encryptedEventBuffer,
\r
584 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
585 "\"ciphertext\":\"%s\","
\r
586 "\"device_id\":\"%s\","
\r
587 "\"sender_key\":\"%s\","
\r
588 "\"session_id\":\"%s\""
\r
596 return MatrixClientSendEvent(client,
\r
598 "m.room.encrypted",
\r
599 encryptedEventBuffer);
\r
602 // https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync
\r
605 MatrixClient * client,
\r
606 char * outSyncBuffer, int outSyncCap)
\r
609 MatrixHttpGet(client,
\r
610 "/_matrix/client/v3/sync",
\r
611 outSyncBuffer, outSyncCap,
\r
616 MatrixClientShareMegolmOutSession(
\r
617 MatrixClient * client,
\r
618 const char * userId,
\r
619 const char * deviceId,
\r
620 MatrixMegolmOutSession * session)
\r
622 // generate room key event
\r
623 static char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
624 sprintf(eventBuffer,
\r
626 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
627 "\"room_id\":\"%s\","
\r
628 "\"session_id\":\"%s\","
\r
629 "\"session_key\":\"%s\""
\r
636 // // get olm session
\r
637 // MatrixOlmSession * olmSession;
\r
638 // MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);
\r
641 // char encryptedBuffer[KEY_SHARE_EVENT_LEN];
\r
642 // MatrixOlmSessionEncrypt(olmSession,
\r
644 // encryptedBuffer, KEY_SHARE_EVENT_LEN);
\r
647 MatrixClientSendToDeviceEncrypted(client,
\r
657 MatrixClientShareMegolmOutSessionTest(
\r
658 MatrixClient * client,
\r
659 const char * deviceId,
\r
660 MatrixMegolmOutSession * session)
\r
662 // generate room key event
\r
663 char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
664 sprintf(eventBuffer,
\r
666 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
667 "\"room_id\":\"%s\","
\r
668 "\"session_id\":\"%s\","
\r
669 "\"session_key\":\"%s\""
\r
677 MatrixClientSendToDevice(client,
\r
687 // MatrixClientSetMegolmOutSession(
\r
688 // MatrixClient * client,
\r
689 // const char * roomId,
\r
690 // MatrixMegolmOutSession session)
\r
692 // if (client->numMegolmOutSessions < 10)
\r
694 // session.roomId = roomId;
\r
695 // client->megolmOutSessions[client->numMegolmOutSessions] = session;
\r
696 // client->numMegolmOutSessions++;
\r
704 MatrixClientGetMegolmOutSession(
\r
705 MatrixClient * client,
\r
706 const char * roomId,
\r
707 MatrixMegolmOutSession ** outSession)
\r
709 for (int i = 0; i < client->numMegolmOutSessions; i++)
\r
711 if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)
\r
713 *outSession = &client->megolmOutSessions[i];
\r
718 if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)
\r
720 MatrixMegolmOutSessionInit(
\r
721 &client->megolmOutSessions[client->numMegolmOutSessions],
\r
724 *outSession = &client->megolmOutSessions[client->numMegolmOutSessions];
\r
726 client->numMegolmOutSessions++;
\r
735 MatrixClientGetOlmSession(
\r
736 MatrixClient * client,
\r
737 const char * userId,
\r
738 const char * deviceId,
\r
739 MatrixOlmSession ** outSession)
\r
741 for (int i = 0; i < client->numOlmSessions; i++)
\r
743 if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)
\r
745 *outSession = &client->olmSessions[i];
\r
750 if (client->numOlmSessions < NUM_OLM_SESSIONS)
\r
752 static char deviceKey[DEVICE_KEY_SIZE];
\r
753 MatrixClientGetDeviceKey(client,
\r
755 deviceKey, DEVICE_KEY_SIZE);
\r
757 char onetimeKey[ONETIME_KEY_SIZE];
\r
758 MatrixClientClaimOnetimeKey(client,
\r
761 onetimeKey, ONETIME_KEY_SIZE);
\r
763 MatrixOlmSessionTo(
\r
764 &client->olmSessions[client->numOlmSessions],
\r
765 client->olmAccount.account,
\r
770 *outSession = &client->olmSessions[client->numOlmSessions];
\r
772 client->numOlmSessions++;
\r
780 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid
\r
782 MatrixClientSendToDevice(
\r
783 MatrixClient * client,
\r
784 const char * userId,
\r
785 const char * deviceId,
\r
786 const char * message,
\r
787 const char * msgType)
\r
789 static char requestUrl[MAX_URL_LEN];
\r
790 sprintf(requestUrl,
\r
791 TODEVICE_URL, msgType, (int)time(NULL));
\r
793 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
794 snprintf(eventBuffer, TODEVICE_EVENT_SIZE,
\r
806 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
808 MatrixHttpPut(client,
\r
811 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
818 MatrixClientSendToDeviceEncrypted(
\r
819 MatrixClient * client,
\r
820 const char * userId,
\r
821 const char * deviceId,
\r
822 const char * message,
\r
823 const char * msgType)
\r
826 MatrixOlmSession * olmSession;
\r
827 MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);
\r
829 // create event json
\r
830 char deviceKey[DEVICE_KEY_SIZE];
\r
831 MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE);
\r
833 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
834 sprintf(eventBuffer,
\r
836 "\"type\": \"%s\","
\r
837 "\"content\": \"%s\","
\r
838 "\"sender\": \"%s\","
\r
839 "\"recipient\": \"%s\","
\r
840 "\"recipient_keys\": {"
\r
841 "\"ed25519\": \"%s\""
\r
844 "\"ed25519\": \"%s\""
\r
850 userId, // recipient user id
\r
851 deviceKey, // recipient device key
\r
852 client->deviceKey);
\r
855 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
856 MatrixOlmSessionEncrypt(olmSession,
\r
858 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
860 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
861 sprintf(encryptedEventBuffer,
\r
863 "\"algorithm\":\"m.olm.v1.curve25519-aes-sha2\","
\r
870 "\"device_id\":\"%s\","
\r
871 "\"sender_key\":\"%s\""
\r
875 0, //olmSession->type,
\r
877 client->deviceKey);
\r
880 return MatrixClientSendToDevice(
\r
884 encryptedEventBuffer,
\r
885 "m.room.encrypted");
\r
889 MatrixClientFindDevice(
\r
890 MatrixClient * client,
\r
891 const char * deviceId,
\r
892 MatrixDevice ** outDevice)
\r
894 MatrixClientRequestDeviceKeys(client);
\r
896 for (int i = 0; i < client->numDevices; i++)
\r
898 if (strcmp(client->devices[i].deviceId, deviceId) == 0)
\r
900 *outDevice = &client->devices[i];
\r
910 MatrixClientGetDeviceKey(
\r
911 MatrixClient * client,
\r
912 const char * deviceId,
\r
913 char * outDeviceKey, int outDeviceKeyCap)
\r
915 MatrixDevice * device;
\r
917 if (MatrixClientFindDevice(client, deviceId, &device))
\r
919 strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);
\r
923 MatrixClientRequestDeviceKeys(client);
\r
925 if (MatrixClientFindDevice(client, deviceId, &device))
\r
927 strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);
\r
934 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery
\r
936 MatrixClientRequestDeviceKeys(
\r
937 MatrixClient * client)
\r
939 static char userIdEscaped[USER_ID_SIZE];
\r
940 JsonEscape(client->userId, strlen(client->userId),
\r
941 userIdEscaped, USER_ID_SIZE);
\r
943 static char request[KEYS_QUERY_REQUEST_SIZE];
\r
944 snprintf(request, KEYS_QUERY_REQUEST_SIZE,
\r
945 "{\"device_keys\":{\"%s\":[]}}", client->userId);
\r
947 static char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];
\r
948 bool requestResult = MatrixHttpPost(client,
\r
951 responseBuffer, KEYS_QUERY_RESPONSE_SIZE,
\r
954 if (! requestResult)
\r
957 // query for retrieving device keys for user id
\r
958 static char query[JSON_QUERY_SIZE];
\r
959 snprintf(query, JSON_QUERY_SIZE,
\r
960 "$.device_keys.%s", userIdEscaped);
\r
964 mjson_find(responseBuffer, strlen(responseBuffer),
\r
969 int koff, klen, voff, vlen, vtype, off = 0;
\r
970 for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,
\r
971 &voff, &vlen, &vtype)) != 0; ) {
\r
972 const char * key = s + koff;
\r
973 const char * val = s + voff;
\r
975 // set device id, "key" is the JSON key
\r
977 snprintf(d.deviceId, DEVICE_ID_SIZE,
\r
978 "%.*s", klen-2, key+1);
\r
980 // look for device key in value
\r
981 static char deviceKeyQuery[JSON_QUERY_SIZE];
\r
982 snprintf(deviceKeyQuery, JSON_QUERY_SIZE,
\r
983 "$.keys.curve25519:%s", d.deviceId);
\r
984 mjson_get_string(val, vlen,
\r
985 deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);
\r
988 if (client->numDevices < NUM_DEVICES)
\r
990 client->devices[client->numDevices] = d;
\r
991 client->numDevices++;
\r