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 512
\r
13 #define ENCRYPTED_EVENT_SIZE 1024
\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 = false;
\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 MatrixOlmSessionFrom(
\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
153 olm_create_outbound_session(session->session,
\r
155 deviceKey, strlen(deviceKey),
\r
156 deviceOnetimeKey, strlen(deviceOnetimeKey),
\r
157 random, OLM_OUTBOUND_SESSION_RANDOM_SIZE);
\r
159 return session->session != NULL;
\r
163 MatrixOlmSessionEncrypt(
\r
164 MatrixOlmSession * session,
\r
165 const char * plaintext,
\r
166 char * outBuffer, int outBufferCap)
\r
168 static uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];
\r
169 Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);
\r
171 size_t res = olm_encrypt(session->session,
\r
172 plaintext, strlen(plaintext),
\r
173 random, OLM_ENCRYPT_RANDOM_SIZE,
\r
174 outBuffer, outBufferCap);
\r
176 return res != olm_error();
\r
179 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session
\r
181 MatrixMegolmOutSessionInit(
\r
182 MatrixMegolmOutSession * session,
\r
183 const char * roomId)
\r
185 memset(session, 0, sizeof(MatrixMegolmOutSession));
\r
187 static uint8_t random[MEGOLM_INIT_RANDOM_SIZE];
\r
188 Randomize(random, MEGOLM_INIT_RANDOM_SIZE);
\r
190 session->roomId = roomId;
\r
193 olm_outbound_group_session(session->memory);
\r
195 olm_init_outbound_group_session(
\r
198 MEGOLM_INIT_RANDOM_SIZE);
\r
200 olm_outbound_group_session_id(session->session,
\r
201 (uint8_t *)session->id,
\r
202 MEGOLM_SESSION_ID_SIZE);
\r
204 olm_outbound_group_session_key(session->session,
\r
205 (uint8_t *)session->key,
\r
206 MEGOLM_SESSION_KEY_SIZE);
\r
212 MatrixMegolmOutSessionEncrypt(
\r
213 MatrixMegolmOutSession * session,
\r
214 const char * plaintext,
\r
215 char * outBuffer, int outBufferCap)
\r
217 size_t res = olm_group_encrypt(session->session,
\r
218 (uint8_t *)plaintext, strlen(plaintext),
\r
219 (uint8_t *)outBuffer, outBufferCap);
\r
221 return res != olm_error();
\r
228 MatrixClient * client,
\r
229 const char * server)
\r
231 memset(client, 0, sizeof(MatrixClient));
\r
233 strcpy(client->server, server);
\r
235 // init olm account
\r
236 MatrixOlmAccountInit(&client->olmAccount);
\r
239 static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];
\r
241 olm_account_identity_keys(
\r
242 client->olmAccount.account,
\r
244 OLM_IDENTITY_KEYS_JSON_SIZE);
\r
246 mjson_get_string(deviceKeysJson, res,
\r
248 client->deviceKey, DEVICE_KEY_SIZE);
\r
249 mjson_get_string(deviceKeysJson, res,
\r
251 client->signingKey, SIGNING_KEY_SIZE);
\r
257 MatrixClientSetAccessToken(
\r
258 MatrixClient * client,
\r
259 const char * accessToken)
\r
261 int accessTokenLen = strlen(accessToken);
\r
263 if (accessTokenLen > ACCESS_TOKEN_SIZE - 1)
\r
266 for (int i = 0; i < accessTokenLen; i++)
\r
267 client->accessToken[i] = accessToken[i];
\r
273 MatrixClientSetDeviceId(
\r
274 MatrixClient * client,
\r
275 const char * deviceId)
\r
277 int deviceIdLen = strlen(deviceId);
\r
279 if (deviceIdLen > DEVICE_ID_SIZE - 1)
\r
282 for (int i = 0; i < deviceIdLen; i++)
\r
283 client->deviceId[i] = deviceId[i];
\r
289 MatrixClientSetUserId(
\r
290 MatrixClient * client,
\r
291 const char * userId)
\r
293 int userIdLen = strlen(userId);
\r
295 if (userIdLen > USER_ID_SIZE - 1)
\r
298 for (int i = 0; i < userIdLen; i++)
\r
299 client->userId[i] = userId[i];
\r
305 MatrixClientGenerateOnetimeKeys(
\r
306 MatrixClient * client,
\r
309 static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];
\r
310 Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);
\r
313 olm_account_generate_one_time_keys(client->olmAccount.account,
\r
314 numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);
\r
316 return res != olm_error();
\r
319 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload
\r
321 MatrixClientUploadOnetimeKeys(
\r
322 MatrixClient * client)
\r
324 static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];
\r
326 mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
327 "{\"one_time_keys\":{");
\r
329 static char onetimeKeysBuffer[1024];
\r
330 olm_account_one_time_keys(client->olmAccount.account,
\r
331 onetimeKeysBuffer, 1024);
\r
335 mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);
\r
337 int koff, klen, voff, vlen, vtype, off = 0;
\r
338 while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {
\r
339 static char keyJson[JSON_ONETIME_KEY_SIZE];
\r
341 snprintf(keyJson, JSON_ONETIME_KEY_SIZE,
\r
342 "{\"key\":\"%.*s\"}",
\r
343 vlen-2, keys + voff+1);
\r
345 static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];
\r
348 keyJson, JSON_ONETIME_KEY_SIZE,
\r
349 keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);
\r
351 mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),
\r
352 "\"signed_curve25519:%.*s\":%s,",
\r
353 klen-2, keys + koff+1,
\r
357 mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),
\r
360 static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];
\r
361 MatrixHttpPost(client,
\r
364 responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,
\r
370 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload
\r
372 MatrixClientUploadDeviceKeys(
\r
373 MatrixClient * client)
\r
375 static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];
\r
377 mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
378 "{\"device_keys\":{"
\r
379 "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"
\r
380 "\"device_id\":\"%s\","
\r
382 "\"curve25519:%s\":\"%s\","
\r
383 "\"ed25519:%s\":\"%s\""
\r
385 "\"user_id\":\"%s\""
\r
388 client->deviceId, client->deviceKey,
\r
389 client->deviceId, client->signingKey,
\r
392 static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];
\r
394 deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
395 deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);
\r
398 static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];
\r
399 MatrixHttpPost(client,
\r
401 deviceKeysSignedBuffer,
\r
402 responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,
\r
408 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim
\r
410 MatrixClientClaimOnetimeKey(
\r
411 MatrixClient * client,
\r
412 const char * userId,
\r
413 const char * deviceId,
\r
414 char * outOnetimeKey, int outOnetimeKeyCap)
\r
416 static char requestBuffer[KEYS_CLAIM_REQUEST_SIZE];
\r
417 mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE,
\r
419 "\"one_time_keys\": {"
\r
421 "\"%s\": \"signed_curve25519\""
\r
424 "\"timeout\": 10000"
\r
429 static char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE];
\r
430 MatrixHttpPost(client,
\r
433 responseBuffer, KEYS_CLAIM_RESPONSE_SIZE,
\r
436 char userIdEscaped[USER_ID_SIZE];
\r
437 JsonEscape(userId, strlen(userId),
\r
438 userIdEscaped, USER_ID_SIZE);
\r
440 static char query[JSON_QUERY_SIZE];
\r
441 snprintf(query, JSON_QUERY_SIZE,
\r
442 "$.one_time_keys.%s.%s",
\r
446 const char * keyObject;
\r
448 mjson_find(responseBuffer, strlen(responseBuffer),
\r
450 &keyObject, &keyObjectSize);
\r
452 int koff, klen, voff, vlen, vtype;
\r
453 mjson_next(keyObject, keyObjectSize, 0,
\r
454 &koff, &klen, &voff, &vlen, &vtype);
\r
456 mjson_get_string(keyObject + voff, vlen,
\r
457 "$.key", outOnetimeKey, outOnetimeKeyCap);
\r
459 printf("onetime key: %s\n", outOnetimeKey);
\r
461 // TODO: verify signature
\r
466 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login
\r
468 MatrixClientLoginPassword(
\r
469 MatrixClient * client,
\r
470 const char * username,
\r
471 const char * password,
\r
472 const char * displayName)
\r
474 static char requestBuffer[LOGIN_REQUEST_SIZE];
\r
476 mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,
\r
478 "\"type\": \"m.login.password\","
\r
479 "\"identifier\": {"
\r
480 "\"type\": \"m.id.user\","
\r
483 "\"password\": \"%s\","
\r
484 "\"initial_device_display_name\": \"%s\""
\r
490 static char responseBuffer[LOGIN_RESPONSE_SIZE];
\r
492 MatrixHttpPost(client,
\r
495 responseBuffer, LOGIN_RESPONSE_SIZE,
\r
498 int responseLen = strlen(responseBuffer);
\r
503 mjson_get_string(responseBuffer, responseLen,
\r
505 client->accessToken, ACCESS_TOKEN_SIZE);
\r
506 mjson_get_string(responseBuffer, responseLen,
\r
508 client->deviceId, DEVICE_ID_SIZE);
\r
509 mjson_get_string(responseBuffer, responseLen,
\r
511 client->expireMs, EXPIRE_MS_SIZE);
\r
512 mjson_get_string(responseBuffer, responseLen,
\r
514 client->refreshToken, REFRESH_TOKEN_SIZE);
\r
519 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
\r
521 MatrixClientSendEvent(
\r
522 MatrixClient * client,
\r
523 const char * roomId,
\r
524 const char * msgType,
\r
525 const char * msgBody)
\r
527 static char requestUrl[MAX_URL_LEN];
\r
528 sprintf(requestUrl,
\r
529 ROOMEVENT_URL, roomId, msgType, (int)time(NULL));
\r
531 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
533 MatrixHttpPut(client,
\r
536 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
542 // https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted
\r
543 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event
\r
545 MatrixClientSendEventEncrypted(
\r
546 MatrixClient * client,
\r
547 const char * roomId,
\r
548 const char * msgType,
\r
549 const char * msgBody)
\r
552 static char requestBuffer[ROOMEVENT_REQUEST_SIZE];
\r
553 sprintf(requestBuffer,
\r
557 "\"room_id\":\"%s\""
\r
563 // get megolm session
\r
564 MatrixMegolmOutSession * outSession;
\r
565 MatrixClientGetMegolmOutSession(client, roomId, &outSession);
\r
568 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
569 MatrixMegolmOutSessionEncrypt(outSession,
\r
571 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
573 // encrypted event json
\r
574 const char * senderKey = client->deviceKey;
\r
575 const char * sessionId = outSession->id;
\r
576 const char * deviceId = client->deviceId;
\r
578 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
579 sprintf(encryptedEventBuffer,
\r
581 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
582 "\"sender_key\":\"%s\","
\r
583 "\"ciphertext\":\"%s\","
\r
584 "\"session_id\":\"%s\","
\r
585 "\"device_id\":\"%s\""
\r
593 return MatrixClientSendEvent(client,
\r
595 "m.room.encrypted",
\r
596 encryptedEventBuffer);
\r
599 // https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync
\r
602 MatrixClient * client,
\r
603 char * outSyncBuffer, int outSyncCap)
\r
606 MatrixHttpGet(client,
\r
607 "/_matrix/client/v3/sync",
\r
608 outSyncBuffer, outSyncCap,
\r
613 MatrixClientShareMegolmOutSession(
\r
614 MatrixClient * client,
\r
615 const char * userId,
\r
616 const char * deviceId,
\r
617 MatrixMegolmOutSession * session)
\r
619 // generate room key event
\r
620 static char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
621 sprintf(eventBuffer,
\r
623 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
624 "\"room_id\":\"%s\","
\r
625 "\"session_id\":\"%s\","
\r
626 "\"session_key\":\"%s\""
\r
634 MatrixOlmSession * olmSession;
\r
635 MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);
\r
638 char encryptedBuffer[KEY_SHARE_EVENT_LEN];
\r
639 MatrixOlmSessionEncrypt(olmSession,
\r
641 encryptedBuffer, KEY_SHARE_EVENT_LEN);
\r
644 MatrixClientSendToDeviceEncrypted(client,
\r
654 MatrixClientShareMegolmOutSessionTest(
\r
655 MatrixClient * client,
\r
656 const char * deviceId,
\r
657 MatrixMegolmOutSession * session)
\r
659 // generate room key event
\r
660 char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
661 sprintf(eventBuffer,
\r
663 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
664 "\"room_id\":\"%s\","
\r
665 "\"session_id\":\"%s\","
\r
666 "\"session_key\":\"%s\""
\r
674 MatrixClientSendToDevice(client,
\r
684 // MatrixClientSetMegolmOutSession(
\r
685 // MatrixClient * client,
\r
686 // const char * roomId,
\r
687 // MatrixMegolmOutSession session)
\r
689 // if (client->numMegolmOutSessions < 10)
\r
691 // session.roomId = roomId;
\r
692 // client->megolmOutSessions[client->numMegolmOutSessions] = session;
\r
693 // client->numMegolmOutSessions++;
\r
701 MatrixClientGetMegolmOutSession(
\r
702 MatrixClient * client,
\r
703 const char * roomId,
\r
704 MatrixMegolmOutSession ** outSession)
\r
706 for (int i = 0; i < client->numMegolmOutSessions; i++)
\r
708 if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)
\r
710 *outSession = &client->megolmOutSessions[i];
\r
715 if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)
\r
717 MatrixMegolmOutSessionInit(
\r
718 &client->megolmOutSessions[client->numMegolmOutSessions],
\r
721 *outSession = &client->megolmOutSessions[client->numMegolmOutSessions];
\r
723 client->numMegolmOutSessions++;
\r
732 MatrixClientGetOlmSession(
\r
733 MatrixClient * client,
\r
734 const char * userId,
\r
735 const char * deviceId,
\r
736 MatrixOlmSession ** outSession)
\r
738 for (int i = 0; i < client->numOlmSessions; i++)
\r
740 if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)
\r
742 *outSession = &client->olmSessions[i];
\r
747 if (client->numOlmSessions < NUM_OLM_SESSIONS)
\r
749 static char deviceKey[DEVICE_KEY_SIZE];
\r
750 MatrixClientGetDeviceKey(client,
\r
752 deviceKey, DEVICE_KEY_SIZE);
\r
754 char onetimeKey[ONETIME_KEY_SIZE];
\r
755 MatrixClientClaimOnetimeKey(client,
\r
758 onetimeKey, ONETIME_KEY_SIZE);
\r
760 MatrixOlmSessionFrom(
\r
761 &client->olmSessions[client->numOlmSessions],
\r
762 client->olmAccount.account,
\r
767 *outSession = &client->olmSessions[client->numOlmSessions];
\r
769 client->numOlmSessions++;
\r
777 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid
\r
779 MatrixClientSendToDevice(
\r
780 MatrixClient * client,
\r
781 const char * userId,
\r
782 const char * deviceId,
\r
783 const char * message,
\r
784 const char * msgType)
\r
786 static char requestUrl[MAX_URL_LEN];
\r
787 sprintf(requestUrl,
\r
788 TODEVICE_URL, msgType, (int)time(NULL));
\r
790 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
791 snprintf(eventBuffer, TODEVICE_EVENT_SIZE,
\r
803 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
805 MatrixHttpPut(client,
\r
808 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
815 MatrixClientSendToDeviceEncrypted(
\r
816 MatrixClient * client,
\r
817 const char * userId,
\r
818 const char * deviceId,
\r
819 const char * message,
\r
820 const char * msgType)
\r
823 MatrixOlmSession * olmSession;
\r
824 MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);
\r
826 // create event json
\r
827 char deviceKey[DEVICE_KEY_SIZE];
\r
828 MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE);
\r
829 const char * senderKey = client->deviceKey;
\r
831 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
832 sprintf(eventBuffer,
\r
834 "\"type\": \"%s\","
\r
835 "\"content\": \"%s\","
\r
836 "\"sender\": \"%s\","
\r
837 "\"recipient\": \"%s\","
\r
838 "\"recipient_keys\": {"
\r
839 "\"ed25519\": \"%s\""
\r
842 "\"ed25519\": \"%s\""
\r
848 userId, // recipient user id
\r
849 deviceKey, // recipient device key
\r
850 client->deviceKey);
\r
853 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
854 MatrixOlmSessionEncrypt(olmSession,
\r
856 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
858 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
859 sprintf(encryptedEventBuffer,
\r
861 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
862 "\"sender_key\":\"%s\","
\r
876 return MatrixClientSendToDevice(
\r
880 encryptedEventBuffer,
\r
881 "m.room.encrypted");
\r
885 MatrixClientFindDevice(
\r
886 MatrixClient * client,
\r
887 const char * deviceId,
\r
888 MatrixDevice ** outDevice)
\r
890 MatrixClientRequestDeviceKeys(client);
\r
892 for (int i = 0; i < client->numDevices; i++)
\r
894 if (strcmp(client->devices[i].deviceId, deviceId) == 0)
\r
896 *outDevice = &client->devices[i];
\r
906 MatrixClientGetDeviceKey(
\r
907 MatrixClient * client,
\r
908 const char * deviceId,
\r
909 char * outDeviceKey, int outDeviceKeyCap)
\r
911 MatrixClientRequestDeviceKeys(client);
\r
913 MatrixDevice * device;
\r
914 if (MatrixClientFindDevice(client, deviceId, &device))
\r
916 strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);
\r
923 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery
\r
925 MatrixClientRequestDeviceKeys(
\r
926 MatrixClient * client)
\r
928 static char userIdEscaped[USER_ID_SIZE];
\r
929 JsonEscape(client->userId, strlen(client->userId),
\r
930 userIdEscaped, USER_ID_SIZE);
\r
932 static char request[KEYS_QUERY_REQUEST_SIZE];
\r
933 snprintf(request, KEYS_QUERY_REQUEST_SIZE,
\r
934 "{\"device_keys\":{\"%s\":[]}}", client->userId);
\r
936 static char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];
\r
937 bool requestResult = MatrixHttpPost(client,
\r
940 responseBuffer, KEYS_QUERY_RESPONSE_SIZE,
\r
943 if (! requestResult)
\r
946 // query for retrieving device keys for user id
\r
947 static char query[JSON_QUERY_SIZE];
\r
948 snprintf(query, JSON_QUERY_SIZE,
\r
949 "$.device_keys.%s", userIdEscaped);
\r
953 mjson_find(responseBuffer, strlen(responseBuffer),
\r
958 int koff, klen, voff, vlen, vtype, off = 0;
\r
959 for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,
\r
960 &voff, &vlen, &vtype)) != 0; ) {
\r
961 const char * key = s + koff;
\r
962 const char * val = s + voff;
\r
964 // set device id, "key" is the JSON key
\r
966 snprintf(d.deviceId, DEVICE_ID_SIZE,
\r
967 "%.*s", klen-2, key+1);
\r
969 // look for device key in value
\r
970 static char deviceKeyQuery[JSON_QUERY_SIZE];
\r
971 snprintf(deviceKeyQuery, JSON_QUERY_SIZE,
\r
972 "$.keys.curve25519:%s", d.deviceId);
\r
973 mjson_get_string(val, vlen,
\r
974 deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);
\r
977 if (client->numDevices < NUM_DEVICES)
\r
979 client->devices[client->numDevices] = d;
\r
980 client->numDevices++;
\r