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 512
\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
\r
25 #define JSON_QUERY_SIZE 128
\r
30 Randomize(uint8_t * random, int randomLen)
\r
32 static bool first = false;
\r
33 if (first) { srand(time(0)); first = false; }
\r
35 for (int i = 0; i < randomLen; i++)
\r
37 random[i] = rand() % 256;
\r
43 char * sIn, int sInLen,
\r
44 char * sOut, int sOutCap)
\r
48 for (int i = 0; i < sInLen; i++)
\r
53 if (sIn[i] == '.' ||
\r
57 sOut[sOutIndex++] = '\\';
\r
59 sOut[sOutIndex++] = sIn[i];
\r
62 if (sOutIndex < sOutCap)
\r
63 sOut[sOutIndex] = '\0';
\r
68 // TODO: in/outbound sessions
\r
70 MatrixOlmSessionInit(
\r
71 MatrixOlmSession * session,
\r
72 const char * deviceId)
\r
74 memset(session, 0, sizeof(MatrixOlmSession));
\r
76 static uint8_t random[MEGOLM_INIT_RANDOM_SIZE];
\r
77 Randomize(random, MEGOLM_INIT_RANDOM_SIZE);
\r
79 session->deviceId = deviceId;
\r
82 olm_session(session->memory);
\r
84 return session->session != NULL;
\r
88 MatrixOlmSessionEncrypt(
\r
89 MatrixOlmSession * session,
\r
90 const char * plaintext,
\r
91 char * outBuffer, int outBufferCap)
\r
93 static uint8_t random[OLM_ENCRYPT_RANDOM_SIZE];
\r
94 Randomize(random, OLM_ENCRYPT_RANDOM_SIZE);
\r
96 size_t res = olm_encrypt(session->session,
\r
97 plaintext, strlen(plaintext),
\r
98 random, OLM_ENCRYPT_RANDOM_SIZE,
\r
99 outBuffer, outBufferCap);
\r
101 return res != olm_error();
\r
104 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session
\r
106 MatrixMegolmOutSessionInit(
\r
107 MatrixMegolmOutSession * session,
\r
108 const char * roomId)
\r
110 memset(session, 0, sizeof(MatrixMegolmOutSession));
\r
112 static uint8_t random[MEGOLM_INIT_RANDOM_SIZE];
\r
113 Randomize(random, MEGOLM_INIT_RANDOM_SIZE);
\r
115 session->roomId = roomId;
\r
118 olm_outbound_group_session(session->memory);
\r
120 olm_init_outbound_group_session(
\r
123 MEGOLM_INIT_RANDOM_SIZE);
\r
125 olm_outbound_group_session_id(session->session,
\r
126 (uint8_t *)session->id,
\r
127 MEGOLM_SESSION_ID_SIZE);
\r
129 olm_outbound_group_session_key(session->session,
\r
130 (uint8_t *)session->key,
\r
131 MEGOLM_SESSION_KEY_SIZE);
\r
137 MatrixMegolmOutSessionEncrypt(
\r
138 MatrixMegolmOutSession * session,
\r
139 const char * plaintext,
\r
140 char * outBuffer, int outBufferCap)
\r
142 size_t res = olm_group_encrypt(session->session,
\r
143 (uint8_t *)plaintext, strlen(plaintext),
\r
144 (uint8_t *)outBuffer, outBufferCap);
\r
146 return res != olm_error();
\r
153 MatrixClient * client,
\r
154 const char * server)
\r
156 memset(client, 0, sizeof(MatrixClient));
\r
158 strcpy(client->server, server);
\r
164 MatrixClientSetAccessToken(
\r
165 MatrixClient * client,
\r
166 const char * accessToken)
\r
168 int accessTokenLen = strlen(accessToken);
\r
170 if (accessTokenLen < ACCESS_TOKEN_SIZE - 1)
\r
173 for (int i = 0; i < accessTokenLen; i++)
\r
174 client->accessToken[i] = accessToken[i];
\r
179 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login
\r
181 MatrixClientLoginPassword(
\r
182 MatrixClient * client,
\r
183 const char * username,
\r
184 const char * password,
\r
185 const char * displayName)
\r
187 static char requestBuffer[LOGIN_REQUEST_SIZE];
\r
189 mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,
\r
191 "\"type\": \"m.login.password\","
\r
192 "\"identifier\": {"
\r
193 "\"type\": \"m.id.user\","
\r
196 "\"password\": \"%s\","
\r
197 "\"initial_device_display_name\": \"%s\""
\r
203 static char responseBuffer[LOGIN_RESPONSE_SIZE];
\r
205 MatrixHttpPost(client,
\r
208 responseBuffer, LOGIN_RESPONSE_SIZE,
\r
211 int responseLen = strlen(responseBuffer);
\r
216 mjson_get_string(responseBuffer, responseLen,
\r
218 client->accessToken, ACCESS_TOKEN_SIZE);
\r
219 mjson_get_string(responseBuffer, responseLen,
\r
221 client->deviceId, DEVICE_ID_SIZE);
\r
222 mjson_get_string(responseBuffer, responseLen,
\r
224 client->expireMs, EXPIRE_MS_SIZE);
\r
225 mjson_get_string(responseBuffer, responseLen,
\r
227 client->refreshToken, REFRESH_TOKEN_SIZE);
\r
232 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
\r
234 MatrixClientSendEvent(
\r
235 MatrixClient * client,
\r
236 const char * roomId,
\r
237 const char * msgType,
\r
238 const char * msgBody)
\r
240 static char requestUrl[MAX_URL_LEN];
\r
241 sprintf(requestUrl,
\r
242 ROOMEVENT_URL, roomId, msgType, (int)time(NULL));
\r
244 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
246 MatrixHttpPut(client,
\r
249 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
255 // https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted
\r
256 // https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event
\r
258 MatrixClientSendEventEncrypted(
\r
259 MatrixClient * client,
\r
260 const char * roomId,
\r
261 const char * msgType,
\r
262 const char * msgBody)
\r
265 static char requestBuffer[ROOMEVENT_REQUEST_SIZE];
\r
266 sprintf(requestBuffer,
\r
270 "\"room_id\":\"%s\""
\r
276 // get megolm session
\r
277 MatrixMegolmOutSession * outSession;
\r
278 MatrixClientGetMegolmOutSession(client, roomId, &outSession);
\r
281 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
282 MatrixMegolmOutSessionEncrypt(outSession,
\r
284 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
286 // encrypted event json
\r
287 const char * senderKey = client->deviceKey;
\r
288 const char * sessionId = outSession->id;
\r
289 const char * deviceId = client->deviceId;
\r
291 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
292 sprintf(encryptedEventBuffer,
\r
294 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
295 "\"sender_key\":\"%s\","
\r
296 "\"ciphertext\":\"%s\","
\r
297 "\"session_id\":\"%s\","
\r
298 "\"device_id\":\"%s\""
\r
306 return MatrixClientSendEvent(client,
\r
308 "m.room.encrypted",
\r
309 encryptedEventBuffer);
\r
312 // https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync
\r
315 MatrixClient * client,
\r
316 char * outSyncBuffer, int outSyncCap)
\r
319 MatrixHttpGet(client,
\r
320 "/_matrix/client/v3/sync",
\r
321 outSyncBuffer, outSyncCap,
\r
326 MatrixClientShareMegolmOutSession(
\r
327 MatrixClient * client,
\r
328 const char * deviceId,
\r
329 MatrixMegolmOutSession * session)
\r
331 // generate room key event
\r
332 char eventBuffer[KEY_SHARE_EVENT_LEN];
\r
333 sprintf(eventBuffer,
\r
335 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
336 "\"room_id\":\"%s\","
\r
337 "\"session_id\":\"%s\","
\r
338 "\"session_key\":\"%s\""
\r
346 MatrixOlmSession * olmSession;
\r
347 MatrixClientGetOlmSession(client, deviceId, &olmSession);
\r
350 char encryptedBuffer[KEY_SHARE_EVENT_LEN];
\r
351 MatrixOlmSessionEncrypt(olmSession,
\r
353 encryptedBuffer, KEY_SHARE_EVENT_LEN);
\r
356 MatrixClientSendToDeviceEncrypted(client,
\r
366 // MatrixClientSetMegolmOutSession(
\r
367 // MatrixClient * client,
\r
368 // const char * roomId,
\r
369 // MatrixMegolmOutSession session)
\r
371 // if (client->numMegolmOutSessions < 10)
\r
373 // session.roomId = roomId;
\r
374 // client->megolmOutSessions[client->numMegolmOutSessions] = session;
\r
375 // client->numMegolmOutSessions++;
\r
383 MatrixClientGetMegolmOutSession(
\r
384 MatrixClient * client,
\r
385 const char * roomId,
\r
386 MatrixMegolmOutSession ** outSession)
\r
388 for (int i = 0; i < client->numMegolmOutSessions; i++)
\r
390 if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)
\r
392 *outSession = &client->megolmOutSessions[i];
\r
397 if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)
\r
399 MatrixMegolmOutSessionInit(
\r
400 &client->megolmOutSessions[client->numMegolmOutSessions],
\r
403 *outSession = &client->megolmOutSessions[client->numMegolmOutSessions];
\r
405 client->numMegolmOutSessions++;
\r
414 MatrixClientGetOlmSession(
\r
415 MatrixClient * client,
\r
416 const char * deviceId,
\r
417 MatrixOlmSession ** outSession)
\r
419 for (int i = 0; i < client->numOlmSessions; i++)
\r
421 if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)
\r
423 *outSession = &client->olmSessions[i];
\r
428 if (client->numOlmSessions < NUM_OLM_SESSIONS)
\r
430 MatrixOlmSessionInit(
\r
431 &client->olmSessions[client->numOlmSessions],
\r
434 *outSession = &client->olmSessions[client->numOlmSessions];
\r
436 client->numOlmSessions++;
\r
444 // https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid
\r
446 MatrixClientSendToDevice(
\r
447 MatrixClient * client,
\r
448 const char * userId,
\r
449 const char * deviceId,
\r
450 const char * message,
\r
451 const char * msgType)
\r
453 static char requestUrl[MAX_URL_LEN];
\r
454 sprintf(requestUrl,
\r
455 TODEVICE_URL, msgType, (int)time(NULL));
\r
457 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
458 snprintf(eventBuffer, TODEVICE_EVENT_SIZE,
\r
470 static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];
\r
472 MatrixHttpPut(client,
\r
475 responseBuffer, ROOMEVENT_RESPONSE_SIZE,
\r
482 MatrixClientSendToDeviceEncrypted(
\r
483 MatrixClient * client,
\r
484 const char * userId,
\r
485 const char * deviceId,
\r
486 const char * message,
\r
487 const char * msgType)
\r
490 MatrixOlmSession * olmSession;
\r
491 MatrixClientGetOlmSession(client, deviceId, &olmSession);
\r
493 // create event json
\r
494 char deviceKey[DEVICE_KEY_SIZE];
\r
495 MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE);
\r
496 const char * senderKey = client->deviceKey;
\r
498 static char eventBuffer[TODEVICE_EVENT_SIZE];
\r
499 sprintf(eventBuffer,
\r
501 "\"type\": \"%s\","
\r
502 "\"content\": \"%s\","
\r
503 "\"sender\": \"%s\","
\r
504 "\"recipient\": \"%s\","
\r
505 "\"recipient_keys\": {"
\r
506 "\"ed25519\": \"%s\""
\r
509 "\"ed25519\": \"%s\""
\r
515 userId, // recipient user id
\r
516 deviceKey, // recipient device key
\r
517 client->deviceKey);
\r
520 static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];
\r
521 MatrixOlmSessionEncrypt(olmSession,
\r
523 encryptedBuffer, ENCRYPTED_REQUEST_SIZE);
\r
525 static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];
\r
526 sprintf(encryptedEventBuffer,
\r
528 "\"algorithm\":\"m.megolm.v1.aes-sha2\","
\r
529 "\"sender_key\":\"%s\","
\r
543 return MatrixClientSendToDevice(
\r
547 encryptedEventBuffer,
\r
548 "m.room.encrypted");
\r
552 MatrixClientFindDevice(
\r
553 MatrixClient * client,
\r
554 const char * deviceId,
\r
555 MatrixDevice ** outDevice)
\r
557 MatrixClientRequestDeviceKeys(client);
\r
559 for (int i = 0; i < client->numDevices; i++)
\r
561 if (strcmp(client->devices[i].deviceId, deviceId) == 0)
\r
563 *outDevice = &client->devices[i];
\r
573 MatrixClientGetDeviceKey(
\r
574 MatrixClient * client,
\r
575 const char * deviceId,
\r
576 char * outDeviceKey, int outDeviceKeyCap)
\r
578 MatrixClientRequestDeviceKeys(client);
\r
580 MatrixDevice * device;
\r
581 if (MatrixClientFindDevice(client, deviceId, &device))
\r
583 strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap);
\r
590 // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery
\r
592 MatrixClientRequestDeviceKeys(
\r
593 MatrixClient * client)
\r
595 char userIdEscaped[USER_ID_SIZE];
\r
596 JsonEscape(client->userId, strlen(client->userId),
\r
597 userIdEscaped, USER_ID_SIZE);
\r
599 char request[KEYS_QUERY_REQUEST_SIZE];
\r
600 snprintf(request, KEYS_QUERY_REQUEST_SIZE,
\r
601 "{\"device_keys\":{\"%s\":[]}}", userIdEscaped);
\r
603 char responseBuffer[KEYS_QUERY_RESPONSE_SIZE];
\r
604 bool requestResult = MatrixHttpPost(client,
\r
607 responseBuffer, KEYS_QUERY_RESPONSE_SIZE,
\r
612 // query for retrieving device keys for user id
\r
613 char query[JSON_QUERY_SIZE];
\r
614 snprintf(query, JSON_QUERY_SIZE,
\r
615 "$.device_keys.%s", userIdEscaped);
\r
619 mjson_find(responseBuffer, strlen(responseBuffer),
\r
624 int koff, klen, voff, vlen, vtype, off;
\r
625 for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,
\r
626 &voff, &vlen, &vtype)) != 0; ) {
\r
627 const char * key = s + koff;
\r
628 const char * val = s + voff;
\r
630 // set device id, "key" is the JSON key
\r
632 strncpy(d.deviceId, key, klen);
\r
634 // look for device key in value
\r
635 char deviceKeyQuery[JSON_QUERY_SIZE];
\r
636 snprintf(deviceKeyQuery, JSON_QUERY_SIZE,
\r
637 "$.keys.curve25519:%.*s", klen, key);
\r
638 mjson_get_string(val, vlen,
\r
639 deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);
\r
642 if (client->numDevices < NUM_DEVICES)
\r
644 client->devices[client->numDevices] = d;
\r
645 client->numDevices++;
\r