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
263 MatrixClient * client,
\r
264 const char * filename)
\r
266 FILE * f = fopen(filename, "w");
\r
268 fwrite(client->deviceKey, 1, DEVICE_KEY_SIZE, f);
\r
269 fwrite(client->signingKey, 1, DEVICE_KEY_SIZE, f);
\r
270 fwrite(client->userId, 1, USER_ID_SIZE, f);
\r
271 fwrite(client->server, 1, SERVER_SIZE, f);
\r
272 fwrite(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);
\r
273 fwrite(client->deviceId, 1, DEVICE_ID_SIZE, f);
\r
274 fwrite(client->expireMs, 1, EXPIRE_MS_SIZE, f);
\r
275 fwrite(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f);
\r
277 fwrite(&client->numDevices, sizeof(int), 1, f);
\r
278 for (int i = 0; i < client->numDevices; i++) {
\r
279 fwrite(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f);
\r
280 fwrite(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f);
\r
289 MatrixClient * client,
\r
290 const char * filename)
\r
292 FILE * f = fopen(filename, "r");
\r
294 fread(client->deviceKey, 1, DEVICE_KEY_SIZE, f);
\r
295 fread(client->signingKey, 1, DEVICE_KEY_SIZE, f);
\r
296 fread(client->userId, 1, USER_ID_SIZE, f);
\r
297 fread(client->server, 1, SERVER_SIZE, f);
\r
298 fread(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);
\r
299 fread(client->deviceId, 1, DEVICE_ID_SIZE, f);
\r
300 fread(client->expireMs, 1, EXPIRE_MS_SIZE, f);
\r
301 fread(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f);
\r
303 fread(&client->numDevices, sizeof(int), 1, f);
\r
304 for (int i = 0; i < client->numDevices; i++) {
\r
305 fread(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f);
\r
306 fread(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f);
\r
314 MatrixClientSetAccessToken(
\r
315 MatrixClient * client,
\r
316 const char * accessToken)
\r
318 int accessTokenLen = strlen(accessToken);
\r
320 if (accessTokenLen > ACCESS_TOKEN_SIZE - 1)
\r
323 for (int i = 0; i < accessTokenLen; i++)
\r
324 client->accessToken[i] = accessToken[i];
\r
325 client->accessToken[accessTokenLen] = '\0';
\r
331 MatrixClientSetDeviceId(
\r
332 MatrixClient * client,
\r
333 const char * deviceId)
\r
335 int deviceIdLen = strlen(deviceId);
\r
337 if (deviceIdLen > DEVICE_ID_SIZE - 1)
\r
340 for (int i = 0; i < deviceIdLen; i++)
\r
341 client->deviceId[i] = deviceId[i];
\r
342 client->deviceId[deviceIdLen] = '\0';
\r
348 MatrixClientSetUserId(
\r
349 MatrixClient * client,
\r
350 const char * userId)
\r
352 int userIdLen = strlen(userId);
\r
354 if (userIdLen > USER_ID_SIZE - 1)
\r
357 for (int i = 0; i < userIdLen; i++)
\r
358 client->userId[i] = userId[i];
\r
359 client->userId[userIdLen] = '\0';
\r
365 MatrixClientGenerateOnetimeKeys(
\r
366 MatrixClient * client,
\r
369 static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];
\r
370 Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);
\r
373 olm_account_generate_one_time_keys(client->olmAccount.account,
\r
374 numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);
\r
376 return res != olm_error();
\r
379 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload
\r
381 MatrixClientUploadOnetimeKeys(
\r
382 MatrixClient * client)
\r
384 static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];
\r
386 mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
387 "{\"one_time_keys\":{");
\r
389 static char onetimeKeysBuffer[1024];
\r
390 olm_account_one_time_keys(client->olmAccount.account,
\r
391 onetimeKeysBuffer, 1024);
\r
395 mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);
\r
397 int koff, klen, voff, vlen, vtype, off = 0;
\r
398 while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {
\r
399 static char keyJson[JSON_ONETIME_KEY_SIZE];
\r
401 snprintf(keyJson, JSON_ONETIME_KEY_SIZE,
\r
402 "{\"key\":\"%.*s\"}",
\r
403 vlen-2, keys + voff+1);
\r
405 static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];
\r
408 keyJson, JSON_ONETIME_KEY_SIZE,
\r
409 keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);
\r
411 mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),
\r
412 "\"signed_curve25519:%.*s\":%s,",
\r
413 klen-2, keys + koff+1,
\r
417 mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),
\r
420 static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];
\r
421 MatrixHttpPost(client,
\r
424 responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,
\r
430 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload
\r
432 MatrixClientUploadDeviceKeys(
\r
433 MatrixClient * client)
\r
435 static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];
\r
437 mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
438 "{\"device_keys\":{"
\r
439 "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"
\r
440 "\"device_id\":\"%s\","
\r
442 "\"curve25519:%s\":\"%s\","
\r
443 "\"ed25519:%s\":\"%s\""
\r
445 "\"user_id\":\"%s\""
\r
448 client->deviceId, client->deviceKey,
\r
449 client->deviceId, client->signingKey,
\r
452 static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];
\r
454 deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,
\r
455 deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);
\r
458 static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];
\r
459 MatrixHttpPost(client,
\r
461 deviceKeysSignedBuffer,
\r
462 responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,
\r
468 // https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim
\r
470 MatrixClientClaimOnetimeKey(
\r
471 MatrixClient * client,
\r
472 const char * userId,
\r
473 const char * deviceId,
\r
474 char * outOnetimeKey, int outOnetimeKeyCap)
\r
476 static char requestBuffer[KEYS_CLAIM_REQUEST_SIZE];
\r
477 mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE,
\r
479 "\"one_time_keys\": {"
\r
481 "\"%s\": \"signed_curve25519\""
\r
484 "\"timeout\": 10000"
\r
489 static char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE];
\r
490 MatrixHttpPost(client,
\r
493 responseBuffer, KEYS_CLAIM_RESPONSE_SIZE,
\r
496 char userIdEscaped[USER_ID_SIZE];
\r
497 JsonEscape(userId, strlen(userId),
\r
498 userIdEscaped, USER_ID_SIZE);
\r
500 static char query[JSON_QUERY_SIZE];
\r
501 snprintf(query, JSON_QUERY_SIZE,
\r
502 "$.one_time_keys.%s.%s",
\r
506 const char * keyObject;
\r
508 mjson_find(responseBuffer, strlen(responseBuffer),
\r
510 &keyObject, &keyObjectSize);
\r
512 int koff, klen, voff, vlen, vtype;
\r
513 mjson_next(keyObject, keyObjectSize, 0,
\r
514 &koff, &klen, &voff, &vlen, &vtype);
\r
516 mjson_get_string(keyObject + voff, vlen,
\r
517 "$.key", outOnetimeKey, outOnetimeKeyCap);
\r
519 // TODO: verify signature
\r
524 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login
\r
526 MatrixClientLoginPassword(
\r
527 MatrixClient * client,
\r
528 const char * username,
\r
529 const char * password,
\r
530 const char * displayName)
\r
532 static char requestBuffer[LOGIN_REQUEST_SIZE];
\r
534 mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,
\r
536 "\"type\": \"m.login.password\","
\r
537 "\"identifier\": {"
\r
538 "\"type\": \"m.id.user\","
\r
541 "\"password\": \"%s\","
\r
542 "\"initial_device_display_name\": \"%s\""
\r
548 static char responseBuffer[LOGIN_RESPONSE_SIZE];
\r
550 MatrixHttpPost(client,
\r
553 responseBuffer, LOGIN_RESPONSE_SIZE,
\r
556 int responseLen = strlen(responseBuffer);
\r
561 mjson_get_string(responseBuffer, responseLen,
\r
563 client->accessToken, ACCESS_TOKEN_SIZE);
\r
564 mjson_get_string(responseBuffer, responseLen,
\r
566 client->deviceId, DEVICE_ID_SIZE);
\r
567 mjson_get_string(responseBuffer, responseLen,
\r
569 client->expireMs, EXPIRE_MS_SIZE);
\r
570 mjson_get_string(responseBuffer, responseLen,
\r
572 client->refreshToken, REFRESH_TOKEN_SIZE);
\r
577 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
\r
579 MatrixClientSendEvent(
\r
580 MatrixClient * client,
\r
581 const char * roomId,
\r
582 const char * msgType,
\r
583 const char * msgBody)
\r
585 static char requestUrl[MAX_URL_LEN];
\r
586 sprintf(requestUrl,
\r
587 ROOMEVENT_URL, roomId, msgType, (int)time(NULL));
\r
589 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
591 MatrixHttpPut(client,
\r
594 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
600 // https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted
\r
601 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event
\r
603 MatrixClientSendEventEncrypted(
\r
604 MatrixClient * client,
\r
605 const char * roomId,
\r
606 const char * msgType,
\r
607 const char * msgBody)
\r
610 static char requestBuffer[ROOMEVENT_REQUEST_SIZE];
\r
611 sprintf(requestBuffer,
\r
615 "\"room_id\":\"%s\""
\r
621 // get megolm session
\r
622 MatrixMegolmOutSession * outSession;
\r
623 MatrixClientGetMegolmOutSession(client, roomId, &outSession);
\r
626 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
627 MatrixMegolmOutSessionEncrypt(outSession,
\r
629 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
631 // encrypted event json
\r
632 const char * senderKey = client->deviceKey;
\r
633 const char * sessionId = outSession->id;
\r
634 const char * deviceId = client->deviceId;
\r
636 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
637 sprintf(encryptedEventBuffer,
\r
639 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
640 "\"ciphertext\":\"%s\","
\r
641 "\"device_id\":\"%s\","
\r
642 "\"sender_key\":\"%s\","
\r
643 "\"session_id\":\"%s\""
\r
651 return MatrixClientSendEvent(client,
\r
653 "m.room.encrypted",
\r
654 encryptedEventBuffer);
\r
657 // https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync
\r
660 MatrixClient * client,
\r
661 char * outSyncBuffer, int outSyncCap)
\r
664 MatrixHttpGet(client,
\r
665 "/_matrix/client/v3/sync",
\r
666 outSyncBuffer, outSyncCap,
\r
671 MatrixClientShareMegolmOutSession(
\r
672 MatrixClient * client,
\r
673 const char * userId,
\r
674 const char * deviceId,
\r
675 MatrixMegolmOutSession * session)
\r
677 // generate room key event
\r
678 static char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
679 sprintf(eventBuffer,
\r
681 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
682 "\"room_id\":\"%s\","
\r
683 "\"session_id\":\"%s\","
\r
684 "\"session_key\":\"%s\""
\r
691 // // get olm session
\r
692 // MatrixOlmSession * olmSession;
\r
693 // MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);
\r
696 // char encryptedBuffer[KEY_SHARE_EVENT_LEN];
\r
697 // MatrixOlmSessionEncrypt(olmSession,
\r
699 // encryptedBuffer, KEY_SHARE_EVENT_LEN);
\r
702 MatrixClientSendToDeviceEncrypted(client,
\r
712 MatrixClientShareMegolmOutSessionTest(
\r
713 MatrixClient * client,
\r
714 const char * deviceId,
\r
715 MatrixMegolmOutSession * session)
\r
717 // generate room key event
\r
718 char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
719 sprintf(eventBuffer,
\r
721 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
722 "\"room_id\":\"%s\","
\r
723 "\"session_id\":\"%s\","
\r
724 "\"session_key\":\"%s\""
\r
732 MatrixClientSendToDevice(client,
\r
742 // MatrixClientSetMegolmOutSession(
\r
743 // MatrixClient * client,
\r
744 // const char * roomId,
\r
745 // MatrixMegolmOutSession session)
\r
747 // if (client->numMegolmOutSessions < 10)
\r
749 // session.roomId = roomId;
\r
750 // client->megolmOutSessions[client->numMegolmOutSessions] = session;
\r
751 // client->numMegolmOutSessions++;
\r
759 MatrixClientGetMegolmOutSession(
\r
760 MatrixClient * client,
\r
761 const char * roomId,
\r
762 MatrixMegolmOutSession ** outSession)
\r
764 for (int i = 0; i < client->numMegolmOutSessions; i++)
\r
766 if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)
\r
768 *outSession = &client->megolmOutSessions[i];
\r
773 if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)
\r
775 MatrixMegolmOutSessionInit(
\r
776 &client->megolmOutSessions[client->numMegolmOutSessions],
\r
779 *outSession = &client->megolmOutSessions[client->numMegolmOutSessions];
\r
781 client->numMegolmOutSessions++;
\r
790 MatrixClientGetOlmSession(
\r
791 MatrixClient * client,
\r
792 const char * userId,
\r
793 const char * deviceId,
\r
794 MatrixOlmSession ** outSession)
\r
796 for (int i = 0; i < client->numOlmSessions; i++)
\r
798 if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)
\r
800 *outSession = &client->olmSessions[i];
\r
805 if (client->numOlmSessions < NUM_OLM_SESSIONS)
\r
807 static char deviceKey[DEVICE_KEY_SIZE];
\r
808 MatrixClientGetDeviceKey(client,
\r
810 deviceKey, DEVICE_KEY_SIZE);
\r
812 char onetimeKey[ONETIME_KEY_SIZE];
\r
813 MatrixClientClaimOnetimeKey(client,
\r
816 onetimeKey, ONETIME_KEY_SIZE);
\r
818 MatrixOlmSessionTo(
\r
819 &client->olmSessions[client->numOlmSessions],
\r
820 client->olmAccount.account,
\r
825 *outSession = &client->olmSessions[client->numOlmSessions];
\r
827 client->numOlmSessions++;
\r
835 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid
\r
837 MatrixClientSendToDevice(
\r
838 MatrixClient * client,
\r
839 const char * userId,
\r
840 const char * deviceId,
\r
841 const char * message,
\r
842 const char * msgType)
\r
844 static char requestUrl[MAX_URL_LEN];
\r
845 sprintf(requestUrl,
\r
846 TODEVICE_URL, msgType, (int)time(NULL));
\r
848 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
849 snprintf(eventBuffer, TODEVICE_EVENT_SIZE,
\r
861 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
863 MatrixHttpPut(client,
\r
866 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
873 MatrixClientSendToDeviceEncrypted(
\r
874 MatrixClient * client,
\r
875 const char * userId,
\r
876 const char * deviceId,
\r
877 const char * message,
\r
878 const char * msgType)
\r
881 MatrixOlmSession * olmSession;
\r
882 MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);
\r
884 // create event json
\r
885 char deviceKey[DEVICE_KEY_SIZE];
\r
886 MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE);
\r
888 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
889 sprintf(eventBuffer,
\r
891 "\"type\": \"%s\","
\r
892 "\"content\": \"%s\","
\r
893 "\"sender\": \"%s\","
\r
894 "\"recipient\": \"%s\","
\r
895 "\"recipient_keys\": {"
\r
896 "\"ed25519\": \"%s\""
\r
899 "\"ed25519\": \"%s\""
\r
905 userId, // recipient user id
\r
906 deviceKey, // recipient device key
\r
907 client->deviceKey);
\r
910 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
911 MatrixOlmSessionEncrypt(olmSession,
\r
913 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
915 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
916 sprintf(encryptedEventBuffer,
\r
918 "\"algorithm\":\"m.olm.v1.curve25519-aes-sha2\","
\r
925 "\"device_id\":\"%s\","
\r
926 "\"sender_key\":\"%s\""
\r
930 0, //olmSession->type,
\r
932 client->deviceKey);
\r
935 return MatrixClientSendToDevice(
\r
939 encryptedEventBuffer,
\r
940 "m.room.encrypted");
\r
944 MatrixClientFindDevice(
\r
945 MatrixClient * client,
\r
946 const char * deviceId,
\r
947 MatrixDevice ** outDevice)
\r
949 MatrixClientRequestDeviceKeys(client);
\r
951 for (int i = 0; i < client->numDevices; i++)
\r
953 if (strcmp(client->devices[i].deviceId, deviceId) == 0)
\r
955 *outDevice = &client->devices[i];
\r
965 MatrixClientGetDeviceKey(
\r
966 MatrixClient * client,
\r
967 const char * deviceId,
\r
968 char * outDeviceKey, int outDeviceKeyCap)
\r
970 MatrixDevice * device;
\r
972 if (MatrixClientFindDevice(client, deviceId, &device))
\r
974 strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);
\r
978 MatrixClientRequestDeviceKeys(client);
\r
980 if (MatrixClientFindDevice(client, deviceId, &device))
\r
982 strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);
\r
989 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery
\r
991 MatrixClientRequestDeviceKeys(
\r
992 MatrixClient * client)
\r
994 static char userIdEscaped[USER_ID_SIZE];
\r
995 JsonEscape(client->userId, strlen(client->userId),
\r
996 userIdEscaped, USER_ID_SIZE);
\r
998 static char request[KEYS_QUERY_REQUEST_SIZE];
\r
999 snprintf(request, KEYS_QUERY_REQUEST_SIZE,
\r
1000 "{\"device_keys\":{\"%s\":[]}}", client->userId);
\r
1002 static char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];
\r
1003 bool requestResult = MatrixHttpPost(client,
\r
1006 responseBuffer, KEYS_QUERY_RESPONSE_SIZE,
\r
1009 if (! requestResult)
\r
1012 // query for retrieving device keys for user id
\r
1013 static char query[JSON_QUERY_SIZE];
\r
1014 snprintf(query, JSON_QUERY_SIZE,
\r
1015 "$.device_keys.%s", userIdEscaped);
\r
1019 mjson_find(responseBuffer, strlen(responseBuffer),
\r
1020 query, &s, &slen);
\r
1024 int koff, klen, voff, vlen, vtype, off = 0;
\r
1025 for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,
\r
1026 &voff, &vlen, &vtype)) != 0; ) {
\r
1027 const char * key = s + koff;
\r
1028 const char * val = s + voff;
\r
1030 // set device id, "key" is the JSON key
\r
1032 snprintf(d.deviceId, DEVICE_ID_SIZE,
\r
1033 "%.*s", klen-2, key+1);
\r
1035 // look for device key in value
\r
1036 static char deviceKeyQuery[JSON_QUERY_SIZE];
\r
1037 snprintf(deviceKeyQuery, JSON_QUERY_SIZE,
\r
1038 "$.keys.curve25519:%s", d.deviceId);
\r
1039 mjson_get_string(val, vlen,
\r
1040 deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);
\r
1043 if (client->numDevices < NUM_DEVICES)
\r
1045 client->devices[client->numDevices] = d;
\r
1046 client->numDevices++;
\r