+bool\r
+MatrixClientSetDeviceId(\r
+ MatrixClient * client,\r
+ const char * deviceId)\r
+{\r
+ int deviceIdLen = strlen(deviceId);\r
+\r
+ if (deviceIdLen > DEVICE_ID_SIZE - 1)\r
+ return false;\r
+\r
+ for (int i = 0; i < deviceIdLen; i++)\r
+ client->deviceId[i] = deviceId[i];\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientSetUserId(\r
+ MatrixClient * client,\r
+ const char * userId)\r
+{\r
+ int userIdLen = strlen(userId);\r
+\r
+ if (userIdLen > USER_ID_SIZE - 1)\r
+ return false;\r
+\r
+ for (int i = 0; i < userIdLen; i++)\r
+ client->userId[i] = userId[i];\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientGenerateOnetimeKeys(\r
+ MatrixClient * client,\r
+ int numberOfKeys)\r
+{\r
+ static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];\r
+ Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
+\r
+ size_t res =\r
+ olm_account_generate_one_time_keys(client->olmAccount.account,\r
+ numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
+\r
+ return res != olm_error();\r
+}\r
+\r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
+bool\r
+MatrixClientUploadOnetimeKeys(\r
+ MatrixClient * client)\r
+{\r
+ static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
+\r
+ mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+ "{\"one_time_keys\":{");\r
+\r
+ static char onetimeKeysBuffer[1024];\r
+ olm_account_one_time_keys(client->olmAccount.account,\r
+ onetimeKeysBuffer, 1024);\r
+\r
+ const char *keys;\r
+ int keysLen;\r
+ mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);\r
+\r
+ int koff, klen, voff, vlen, vtype, off = 0;\r
+ while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {\r
+ static char keyJson[JSON_ONETIME_KEY_SIZE];\r
+ \r
+ snprintf(keyJson, JSON_ONETIME_KEY_SIZE,\r
+ "{\"key\":\"%.*s\"}",\r
+ vlen-2, keys + voff+1);\r
+\r
+ static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];\r
+\r
+ JsonSign(client,\r
+ keyJson, JSON_ONETIME_KEY_SIZE,\r
+ keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
+ \r
+ mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
+ "\"signed_curve25519:%.*s\":%s,",\r
+ klen-2, keys + koff+1,\r
+ keyJsonSigned);\r
+ }\r
+\r
+ mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
+ "}}");\r
+\r
+ static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+ MatrixHttpPost(client,\r
+ KEYS_UPLOAD_URL,\r
+ requestBuffer,\r
+ responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+ true);\r
+\r
+ return true;\r
+}\r
+\r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
+bool\r
+MatrixClientUploadDeviceKeys(\r
+ MatrixClient * client)\r
+{\r
+ static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
+\r
+ mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+ "{\"device_keys\":{"\r
+ "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"\r
+ "\"device_id\":\"%s\","\r
+ "\"keys\":{"\r
+ "\"curve25519:%s\":\"%s\","\r
+ "\"ed25519:%s\":\"%s\""\r
+ "},"\r
+ "\"user_id\":\"%s\""\r
+ "}}",\r
+ client->deviceId,\r
+ client->deviceId, client->deviceKey,\r
+ client->deviceId, client->signingKey,\r
+ client->userId);\r
+\r
+ static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ JsonSign(client,\r
+ deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+ deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
+\r
+\r
+ static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+ MatrixHttpPost(client,\r
+ KEYS_UPLOAD_URL,\r
+ deviceKeysSignedBuffer,\r
+ responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+ true);\r
+\r
+ return true;\r
+}\r
+\r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim\r
+bool\r
+MatrixClientClaimOnetimeKey(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ char * outOnetimeKey, int outOnetimeKeyCap)\r
+{\r
+ static char requestBuffer[KEYS_CLAIM_REQUEST_SIZE];\r
+ mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE,\r
+ "{"\r
+ "\"one_time_keys\": {"\r
+ "\"%s\": {"\r
+ "\"%s\": \"signed_curve25519\""\r
+ "}"\r
+ "},"\r
+ "\"timeout\": 10000"\r
+ "}",\r
+ userId,\r
+ deviceId);\r
+\r
+ static char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE];\r
+ MatrixHttpPost(client,\r
+ KEYS_CLAIM_URL,\r
+ requestBuffer,\r
+ responseBuffer, KEYS_CLAIM_RESPONSE_SIZE,\r
+ true);\r
+ \r
+ char userIdEscaped[USER_ID_SIZE];\r
+ JsonEscape(userId, strlen(userId),\r
+ userIdEscaped, USER_ID_SIZE);\r
+ \r
+ static char query[JSON_QUERY_SIZE];\r
+ snprintf(query, JSON_QUERY_SIZE,\r
+ "$.one_time_keys.%s.%s",\r
+ userIdEscaped,\r
+ deviceId);\r
+ \r
+ const char * keyObject;\r
+ int keyObjectSize;\r
+ mjson_find(responseBuffer, strlen(responseBuffer),\r
+ query,\r
+ &keyObject, &keyObjectSize);\r
+ \r
+ int koff, klen, voff, vlen, vtype;\r
+ mjson_next(keyObject, keyObjectSize, 0,\r
+ &koff, &klen, &voff, &vlen, &vtype);\r
+ \r
+ mjson_get_string(keyObject + voff, vlen,\r
+ "$.key", outOnetimeKey, outOnetimeKeyCap);\r
+ \r
+ // TODO: verify signature\r
+ \r
+ return true;\r
+}\r
+\r