+ JsonSign(client,\r
+ keyJson, keyJsonLen,\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
+ if (requestBuffer[strlen(requestBuffer)-1] == ',')\r
+ requestBuffer[strlen(requestBuffer)-1] = '\0';\r
+\r
+ mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
+ "}");\r
+ \r
+ // static char onetimeKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ // JsonSign(client,\r
+ // requestBuffer, strlen(requestBuffer),\r
+ // onetimeKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
+ \r
+ // static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ // snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+ // "{\"one_time_keys\":%s}", onetimeKeysSignedBuffer);\r
+ static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+ "{\"one_time_keys\":%s}", requestBuffer);\r
+\r
+ static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+ MatrixHttpPost(client,\r
+ KEYS_UPLOAD_URL,\r
+ finalEvent,\r
+ responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+ true);\r
+\r
+ printf("%s\n", responseBuffer);\r
+\r
+ return true;\r
+}\r
+\r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
+bool\r
+MatrixClientUploadDeviceKey(\r
+ MatrixClient * client)\r
+{\r
+ char thisDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
+ char thisSigningKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
+\r
+ static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
+\r
+ int deviceKeysBufferLen =\r
+ mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+ "{"\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, thisDeviceKey,\r
+ client->deviceId, thisSigningKey,\r
+ client->userId);\r
+\r
+ static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ JsonSign(client,\r
+ deviceKeysBuffer, deviceKeysBufferLen,\r
+ deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
+ \r
+ static char finalEvent[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
+ snprintf(finalEvent, KEYS_UPLOAD_REQUEST_SIGNED_SIZE,\r
+ "{\"device_keys\":%s}", deviceKeysSignedBuffer);\r
+\r
+ static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+ MatrixHttpPost(client,\r
+ KEYS_UPLOAD_URL,\r
+ finalEvent,\r
+ responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+ true);\r
+ \r
+ printf("%s\n", responseBuffer);\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