#include <matrix.h>\r
\r
#define SERVER "https://matrix.org"\r
-#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
-#define DEVICE_ID "MAZNCCZLBR"\r
#define USER_ID "@pscho:matrix.org"\r
#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org"\r
\r
printf("Usage: %s %s\n", cmd, args);\r
}\r
\r
-void\r
+bool\r
ExecuteCommand(\r
MatrixClient * client,\r
const char * cmd,\r
int nargs, char ** args\r
) {\r
-#define CHECK_ARGS(N, ARGS) if (nargs != N) { Usage(cmd, ARGS); return; }\r
- /**/ if (CheckCommand(cmd, "devicekey")) {\r
- printf("%s\n", client->deviceKey);\r
+#define CHECK_ARGS(N, ARGS) if (nargs != N) { Usage(cmd, ARGS); return true; }\r
+\r
+ /**/ if (CheckCommand(cmd, "exit")) {\r
+ return false;\r
+ }\r
+ else if (CheckCommand(cmd, "devicekey")) {\r
+ static char key[DEVICE_KEY_SIZE];\r
+ if (MatrixOlmAccountGetDeviceKey(&client->olmAccount, key, DEVICE_KEY_SIZE))\r
+ printf("%s\n", key);\r
+ }\r
+ else if (CheckCommand(cmd, "accesstoken")) {\r
+ printf("%s\n", client->accessToken);\r
}\r
else if (CheckCommand(cmd, "genkeys")) {\r
CHECK_ARGS(1, "<number of keys>")\r
\r
MatrixClientGenerateOnetimeKeys(client, atoi(args[0]));\r
}\r
- else if (CheckCommand(cmd, "uploadkeys")) {\r
+ else if (CheckCommand(cmd, "uploadonetimekeys")) {\r
MatrixClientUploadOnetimeKeys(client);\r
}\r
+ else if (CheckCommand(cmd, "uploaddevicekey")) {\r
+ MatrixClientUploadDeviceKey(client);\r
+ }\r
else if (CheckCommand(cmd, "onetimekeys")) {\r
static char buffer[1024];\r
olm_account_one_time_keys(client->olmAccount.account, buffer, 1024);\r
printf("%s\n", buffer);\r
}\r
+ else if (CheckCommand(cmd, "sendto")) {\r
+ CHECK_ARGS(3, "<device_id> <msgtype> <msg>")\r
+\r
+ MatrixClientSendToDevice(client,\r
+ USER_ID,\r
+ args[0],\r
+ args[2],\r
+ args[1]);\r
+ }\r
+ else if (CheckCommand(cmd, "sendtoe")) {\r
+ CHECK_ARGS(3, "<device_id> <msgtype> <msg>")\r
+\r
+ MatrixClientSendToDeviceEncrypted(client,\r
+ USER_ID,\r
+ args[0],\r
+ args[2],\r
+ args[1]);\r
+ }\r
else if (CheckCommand(cmd, "getkeys")) {\r
MatrixClientRequestDeviceKeys(client);\r
for (int i = 0; i < client->numDevices; i++)\r
" ", mjson_print_fixed_buf, &fb);\r
printf("%.*s\n", fb.len, fb.ptr);\r
}\r
+ else if (CheckCommand(cmd, "login")) {\r
+ CHECK_ARGS(3, "<username> <password> <displayname>")\r
+\r
+ MatrixClientLoginPassword(client,\r
+ args[0],\r
+ args[1],\r
+ args[2]);\r
+ }\r
else if (CheckCommand(cmd, "save")) {\r
CHECK_ARGS(1, "<filename>")\r
\r
"m.room.message",\r
body);\r
}\r
+ else if (CheckCommand(cmd, "sendencrypted")) {\r
+ CHECK_ARGS(2, "<room_id> <message>")\r
+\r
+ static char body[1024];\r
+ snprintf(body, 1024,\r
+ "{\"body\":\"%s\",\"msgtype\":\"m.text\"}",\r
+ args[1]);\r
+\r
+ MatrixClientSendEventEncrypted(client,\r
+ args[0],\r
+ "m.room.message",\r
+ body);\r
+ }\r
else if (CheckCommand(cmd, "setuserid")) {\r
CHECK_ARGS(1, "<user_id>")\r
\r
printf("Unknown command\n");\r
}\r
#undef CHECK_ARGS\r
+\r
+ return true;\r
}\r
\r
int\r
\r
MatrixHttpInit(&client);\r
\r
- MatrixClientSetAccessToken(&client,\r
- ACCESS_TOKEN);\r
- MatrixClientSetDeviceId(&client,\r
- DEVICE_ID);\r
- MatrixClientSetUserId(&client,\r
- USER_ID);\r
+\r
+ MatrixClientSetUserId(&client, USER_ID);\r
+ MatrixClientLoginPassword(&client, "@pscho:matrix.org", "Wc23EbmB9G3faMq", "abc");\r
+ MatrixClientGenerateOnetimeKeys(&client, 10);\r
+ MatrixClientUploadDeviceKey(&client);\r
+ MatrixClientUploadOnetimeKeys(&client);\r
+\r
\r
static char cmd[BUFFER_SIZE];\r
static char args_[NUMBER_ARGS][BUFFER_SIZE];\r
for (int i = 0; i < NUMBER_ARGS; i++)\r
args[i] = args_[i];\r
int nargs;\r
- do {\r
+ while (1) {\r
GetCommand(cmd, &nargs, args);\r
\r
- ExecuteCommand(&client, cmd, nargs, args);\r
- \r
- } while (strcmp(cmd, "exit") != 0);\r
+ bool res =\r
+ ExecuteCommand(&client, cmd, nargs, args);\r
\r
+ if (! res)\r
+ break;\r
+ }\r
+ \r
+ MatrixClientDeleteDevice(&client);\r
+\r
MatrixHttpDeinit(&client);\r
\r
return 0;\r
#define KEYS_QUERY_RESPONSE_SIZE (1024*10)\r
\r
#define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload"\r
-#define KEYS_UPLOAD_REQUEST_SIZE 1024\r
-#define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048\r
+#define KEYS_UPLOAD_REQUEST_SIZE 1024*4\r
+#define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048*4\r
#define KEYS_UPLOAD_RESPONSE_SIZE 2048\r
\r
#define KEYS_CLAIM_URL "/_matrix/client/v3/keys/claim"\r
signature, OLM_SIGNATURE_SIZE);\r
\r
int signatureLen = res;\r
+ \r
+ char thisSigningKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
\r
static char signatureJson[JSON_SIGNATURE_SIZE];\r
int signatureJsonLen =\r
"}"\r
"}",\r
client->userId,\r
- client->deviceId,\r
+ "1",\r
signatureLen, signature);\r
\r
struct mjson_fixedbuf result = { sOut, sOutCap, 0 };\r
return res != olm_error();\r
}\r
\r
-// TODO: in/outbound sessions\r
+bool\r
+MatrixOlmAccountUnpickle(\r
+ MatrixOlmAccount * account,\r
+ void * pickled, int pickledLen,\r
+ const void * key, int keyLen)\r
+{\r
+ size_t res;\r
+ res = olm_unpickle_account(account->account,\r
+ key, keyLen,\r
+ pickled, pickledLen);\r
+ if (res == olm_error()) {\r
+ printf("error unpickling olm account:%s\n",\r
+ olm_account_last_error(account->account));\r
+ }\r
+ return res != olm_error();\r
+}\r
+\r
+bool\r
+MatrixOlmAccountGetDeviceKey(\r
+ MatrixOlmAccount * account,\r
+ char * key, int keyCap)\r
+{\r
+ static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
+ size_t res =\r
+ olm_account_identity_keys(account->account,\r
+ deviceKeysJson, OLM_IDENTITY_KEYS_JSON_SIZE);\r
+ mjson_get_string(deviceKeysJson, res,\r
+ "$.curve25519",\r
+ key, keyCap);\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixOlmAccountGetSigningKey(\r
+ MatrixOlmAccount * account,\r
+ char * key, int keyCap)\r
+{\r
+ static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
+ size_t res =\r
+ olm_account_identity_keys(account->account,\r
+ deviceKeysJson, OLM_IDENTITY_KEYS_JSON_SIZE);\r
+ mjson_get_string(deviceKeysJson, res,\r
+ "$.ed25519",\r
+ key, keyCap);\r
+ return true;\r
+}\r
+\r
+// TODO:in/outbound sessions\r
bool\r
MatrixOlmSessionTo(\r
MatrixOlmSession * session,\r
random, OLM_OUTBOUND_SESSION_RANDOM_SIZE);\r
\r
if (res == olm_error()) {\r
- printf("error olm: %s\n", olm_account_last_error(olmAccount));\r
+ printf("error olm:%s\n", olm_session_last_error(session->session));\r
}\r
\r
return session->session != NULL;\r
}\r
\r
+bool\r
+MatrixOlmSessionUnpickle(\r
+ MatrixOlmSession * session,\r
+ const char * deviceId,\r
+ void * pickled, int pickledLen,\r
+ const void * key, int keyLen)\r
+{\r
+ memset(session, 0, sizeof(MatrixOlmSession));\r
+\r
+ session->deviceId = deviceId;\r
+\r
+ session->session =\r
+ olm_session(session->memory);\r
+ \r
+ size_t res;\r
+ res = olm_unpickle_session(session->session,\r
+ key, keyLen,\r
+ pickled, pickledLen);\r
+ \r
+ if (res == olm_error()) {\r
+ printf("error unpickling olm session:%s\n", olm_session_last_error(session->session));\r
+ }\r
+\r
+ return res != olm_error();\r
+}\r
+\r
bool\r
MatrixOlmSessionEncrypt(\r
MatrixOlmSession * session,\r
// init olm account\r
MatrixOlmAccountInit(&client->olmAccount);\r
\r
- // set device key\r
- static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE];\r
- size_t res =\r
- olm_account_identity_keys(\r
- client->olmAccount.account,\r
- deviceKeysJson,\r
- OLM_IDENTITY_KEYS_JSON_SIZE);\r
-\r
- mjson_get_string(deviceKeysJson, res,\r
- "$.curve25519",\r
- client->deviceKey, DEVICE_KEY_SIZE);\r
- mjson_get_string(deviceKeysJson, res,\r
- "$.ed25519",\r
- client->signingKey, SIGNING_KEY_SIZE);\r
-\r
return true;\r
}\r
\r
{\r
FILE * f = fopen(filename, "w");\r
\r
- fwrite(client->deviceKey, 1, DEVICE_KEY_SIZE, f);\r
- fwrite(client->signingKey, 1, DEVICE_KEY_SIZE, f);\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
+\r
+ fwrite(thisDeviceKey, 1, DEVICE_KEY_SIZE, f);\r
+ fwrite(thisSigningKey, 1, DEVICE_KEY_SIZE, f);\r
fwrite(client->userId, 1, USER_ID_SIZE, f);\r
fwrite(client->server, 1, SERVER_SIZE, f);\r
fwrite(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);\r
{\r
FILE * f = fopen(filename, "r");\r
\r
- fread(client->deviceKey, 1, DEVICE_KEY_SIZE, f);\r
- fread(client->signingKey, 1, DEVICE_KEY_SIZE, f);\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
+\r
+ fread(thisDeviceKey, 1, DEVICE_KEY_SIZE, f);\r
+ fread(thisSigningKey, 1, DEVICE_KEY_SIZE, f);\r
fread(client->userId, 1, USER_ID_SIZE, f);\r
fread(client->server, 1, SERVER_SIZE, f);\r
fread(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);\r
\r
// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
bool\r
-MatrixClientUploadDeviceKeys(\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
mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
"\"user_id\":\"%s\""\r
"}}",\r
client->deviceId,\r
- client->deviceId, client->deviceKey,\r
- client->deviceId, client->signingKey,\r
+ client->deviceId, thisDeviceKey,\r
+ client->deviceId, thisSigningKey,\r
client->userId);\r
\r
static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\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
+ "\"one_time_keys\":{"\r
+ "\"%s\":{"\r
+ "\"%s\":\"signed_curve25519\""\r
"}"\r
"},"\r
- "\"timeout\": 10000"\r
+ "\"timeout\":10000"\r
"}",\r
userId,\r
deviceId);\r
mjson_get_string(keyObject + voff, vlen,\r
"$.key", outOnetimeKey, outOnetimeKeyCap);\r
\r
- // TODO: verify signature\r
+ // TODO:verify signature\r
\r
return true;\r
}\r
\r
mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,\r
"{"\r
- "\"type\": \"m.login.password\","\r
- "\"identifier\": {"\r
- "\"type\": \"m.id.user\","\r
- "\"user\": \"%s\""\r
+ "\"type\":\"m.login.password\","\r
+ "\"identifier\":{"\r
+ "\"type\":\"m.id.user\","\r
+ "\"user\":\"%s\""\r
"},"\r
- "\"password\": \"%s\","\r
- "\"initial_device_display_name\": \"%s\""\r
+ "\"password\":\"%s\","\r
+ "\"initial_device_display_name\":\"%s\""\r
"}",\r
username,\r
password,\r
responseBuffer, LOGIN_RESPONSE_SIZE,\r
false);\r
\r
- int responseLen = strlen(responseBuffer);\r
- \r
if (!result)\r
return false;\r
+ \r
+ int responseLen = strlen(responseBuffer);\r
\r
mjson_get_string(responseBuffer, responseLen,\r
"$.access_token",\r
requestBuffer,\r
encryptedBuffer, ENCRYPTED_REQUEST_SIZE);\r
\r
+ char thisDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
+ \r
+\r
// encrypted event json\r
- const char * senderKey = client->deviceKey;\r
+ const char * senderKey = thisDeviceKey;\r
const char * sessionId = outSession->id;\r
const char * deviceId = client->deviceId;\r
\r
bool\r
MatrixClientShareMegolmOutSessionTest(\r
MatrixClient * client,\r
+ const char * userId,\r
const char * deviceId,\r
MatrixMegolmOutSession * session)\r
{\r
\r
// send\r
MatrixClientSendToDevice(client,\r
- client->userId,\r
+ userId,\r
deviceId,\r
eventBuffer,\r
"m.room_key");\r
if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
{\r
static char deviceKey[DEVICE_KEY_SIZE];\r
- MatrixClientGetDeviceKey(client,\r
+ MatrixClientRequestDeviceKey(client,\r
deviceId,\r
deviceKey, DEVICE_KEY_SIZE);\r
\r
static char eventBuffer[TODEVICE_EVENT_SIZE];\r
snprintf(eventBuffer, TODEVICE_EVENT_SIZE,\r
"{"\r
- "\"messages\": {"\r
- "\"%s\": {"\r
+ "\"messages\":{"\r
+ "\"%s\":{"\r
"\"%s\":%s"\r
"}"\r
"}"\r
MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);\r
\r
// create event json\r
- char deviceKey[DEVICE_KEY_SIZE];\r
- MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE);\r
+ char targetDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixClientRequestDeviceKey(client, deviceId, targetDeviceKey, DEVICE_KEY_SIZE);\r
+ char targetSigningKey[SIGNING_KEY_SIZE];\r
+ MatrixClientRequestSigningKey(client, deviceId, targetSigningKey, SIGNING_KEY_SIZE);\r
\r
+ char thisSigningKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, thisSigningKey, DEVICE_KEY_SIZE);\r
+\r
static char eventBuffer[TODEVICE_EVENT_SIZE];\r
sprintf(eventBuffer,\r
"{"\r
- "\"type\": \"%s\","\r
- "\"content\": \"%s\","\r
- "\"sender\": \"%s\","\r
- "\"recipient\": \"%s\","\r
- "\"recipient_keys\": {"\r
- "\"ed25519\": \"%s\""\r
+ "\"type\":\"%s\","\r
+ "\"content\":%s,"\r
+ "\"sender\":\"%s\","\r
+ "\"recipient\":\"%s\","\r
+ "\"recipient_keys\":{"\r
+ "\"ed25519\":\"%s\""\r
"},"\r
- "\"keys\": {"\r
- "\"ed25519\": \"%s\""\r
+ "\"keys\":{"\r
+ "\"ed25519\":\"%s\""\r
"}"\r
"}",\r
msgType,\r
message,\r
client->userId,\r
userId, // recipient user id\r
- deviceKey, // recipient device key\r
- client->deviceKey);\r
+ targetSigningKey, // recipient device key\r
+ thisSigningKey);\r
+ \r
+ printf("%s\n", eventBuffer);\r
\r
// encrypt\r
static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];\r
eventBuffer,\r
encryptedBuffer, ENCRYPTED_REQUEST_SIZE);\r
\r
+ char thisDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
+\r
+\r
static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];\r
sprintf(encryptedEventBuffer,\r
"{"\r
"\"device_id\":\"%s\","\r
"\"sender_key\":\"%s\""\r
"}",\r
- deviceKey,\r
+ targetDeviceKey,\r
encryptedBuffer,\r
- 0, //olmSession->type,\r
+ olm_session_has_received_message(olmSession->session),\r
client->deviceId,\r
- client->deviceKey);\r
+ thisDeviceKey);\r
\r
// send\r
return MatrixClientSendToDevice(\r
}\r
\r
bool\r
-MatrixClientGetDeviceKey(\r
+MatrixClientRequestDeviceKey(\r
MatrixClient * client,\r
const char * deviceId,\r
char * outDeviceKey, int outDeviceKeyCap)\r
return false;\r
}\r
\r
+bool\r
+MatrixClientRequestSigningKey(\r
+ MatrixClient * client,\r
+ const char * deviceId,\r
+ char * outSigningKey, int outSigningKeyCap)\r
+{\r
+ MatrixDevice * device;\r
+ \r
+ if (MatrixClientFindDevice(client, deviceId, &device))\r
+ {\r
+ strncpy(outSigningKey, device->signingKey, outSigningKeyCap);\r
+ return true;\r
+ }\r
+\r
+ MatrixClientRequestDeviceKeys(client);\r
+ \r
+ if (MatrixClientFindDevice(client, deviceId, &device))\r
+ {\r
+ strncpy(outSigningKey, device->signingKey, outSigningKeyCap);\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
// https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3keysquery\r
bool\r
MatrixClientRequestDeviceKeys(\r
int slen;\r
mjson_find(responseBuffer, strlen(responseBuffer),\r
query, &s, &slen);\r
-\r
+ \r
// loop over keys\r
\r
int koff, klen, voff, vlen, vtype, off = 0;\r
mjson_get_string(val, vlen,\r
deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE);\r
\r
+ // look for signing key in value\r
+ static char signingKeyQuery[JSON_QUERY_SIZE];\r
+ snprintf(signingKeyQuery, JSON_QUERY_SIZE,\r
+ "$.keys.ed25519:%s", d.deviceId);\r
+ mjson_get_string(val, vlen,\r
+ signingKeyQuery, d.signingKey, SIGNING_KEY_SIZE);\r
+\r
// add device\r
if (client->numDevices < NUM_DEVICES)\r
{\r
- client->devices[client->numDevices] = d;\r
- client->numDevices++;\r
+ bool foundDevice = false;\r
+ for (int i = 0; i < client->numDevices; i++)\r
+ if (strcmp(client->devices[i].deviceId, d.deviceId) == 0)\r
+ foundDevice = true;\r
+\r
+ if (! foundDevice) {\r
+ printf("new device: %s %s %s\n", d.deviceId, d.deviceKey, d.signingKey);\r
+ client->devices[client->numDevices] = d;\r
+ client->numDevices++;\r
+ }\r
}\r
else\r
{\r
\r
return true;\r
}\r
+\r
+bool\r
+MatrixClientDeleteDevice(\r
+ MatrixClient * client)\r
+{\r
+ static char deleteRequest[1024];\r
+ snprintf(deleteRequest, 1024,\r
+ "{\"devices\":[\"%s\"]}",\r
+ client->deviceId);\r
+ static char deleteResponse[1024];\r
+ bool res = MatrixHttpPost(client, "/_matrix/client/v3/delete_devices",\r
+ deleteRequest, deleteResponse, 1024, true);\r
+ return res;\r
+}
\ No newline at end of file