X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/1273c8ea309926e377cbd5cc6dab6740910aa6ff..f2840d9dd5b8a0683abee189e408c5a6de294eb7:/src/matrix.c diff --git a/src/matrix.c b/src/matrix.c index cc34a0a..966a038 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -15,19 +15,30 @@ #define ROOMEVENT_RESPONSE_SIZE 1024 #define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d" -#define TODEVICE_EVENT_SIZE 512 +#define TODEVICE_EVENT_SIZE (1024*5) #define TODEVICE_URL "/_matrix/client/v3/sendToDevice/%s/%d" #define KEYS_QUERY_URL "/_matrix/client/v3/keys/query" #define KEYS_QUERY_REQUEST_SIZE 256 -#define KEYS_QUERY_RESPONSE_SIZE 1024 +#define KEYS_QUERY_RESPONSE_SIZE (1024*10) + +#define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload" +#define KEYS_UPLOAD_REQUEST_SIZE 1024 +#define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048 +#define KEYS_UPLOAD_RESPONSE_SIZE 2048 + +#define KEYS_CLAIM_URL "/_matrix/client/v3/keys/claim" +#define KEYS_CLAIM_REQUEST_SIZE 1024 +#define KEYS_CLAIM_RESPONSE_SIZE 1024 #define JSON_QUERY_SIZE 128 void -Randomize(uint8_t * random, int randomLen) +Randomize( + uint8_t * random, + int randomLen) { static bool first = false; if (first) { srand(time(0)); first = false; } @@ -40,7 +51,7 @@ Randomize(uint8_t * random, int randomLen) bool JsonEscape( - char * sIn, int sInLen, + const char * sIn, int sInLen, char * sOut, int sOutCap) { int sOutIndex = 0; @@ -65,22 +76,86 @@ JsonEscape( return true; } +bool JsonSign( + MatrixClient * client, + const char * sIn, int sInLen, + char * sOut, int sOutCap) +{ + static char signature[OLM_SIGNATURE_SIZE]; + size_t res = + olm_account_sign(client->olmAccount.account, + sIn, sInLen, + signature, OLM_SIGNATURE_SIZE); + + int signatureLen = res; + + static char signatureJson[JSON_SIGNATURE_SIZE]; + int signatureJsonLen = + mjson_snprintf(signatureJson, JSON_SIGNATURE_SIZE, + "{" + "\"signatures\":{" + "\"%s\":{" + "\"ed25519:%s\":\"%.*s\"" + "}" + "}" + "}", + client->userId, + client->deviceId, + signatureLen, signature); + + struct mjson_fixedbuf result = { sOut, sOutCap, 0 }; + mjson_merge( + sIn, sInLen, + signatureJson, signatureJsonLen, + mjson_print_fixed_buf, + &result); + + return true; +} + + +bool +MatrixOlmAccountInit( + MatrixOlmAccount * account) +{ + account->account = olm_account(account->memory); + + static uint8_t random[OLM_ACCOUNT_RANDOM_SIZE]; + Randomize(random, OLM_ACCOUNT_RANDOM_SIZE); + + size_t res = olm_create_account( + account->account, + random, + OLM_ACCOUNT_RANDOM_SIZE); + + return res != olm_error(); +} + // TODO: in/outbound sessions bool -MatrixOlmSessionInit( +MatrixOlmSessionFrom( MatrixOlmSession * session, - const char * deviceId) + OlmAccount * olmAccount, + const char * deviceId, + const char * deviceKey, + const char * deviceOnetimeKey) { memset(session, 0, sizeof(MatrixOlmSession)); - static uint8_t random[MEGOLM_INIT_RANDOM_SIZE]; - Randomize(random, MEGOLM_INIT_RANDOM_SIZE); - session->deviceId = deviceId; session->session = olm_session(session->memory); + static uint8_t random[OLM_OUTBOUND_SESSION_RANDOM_SIZE]; + Randomize(random, OLM_OUTBOUND_SESSION_RANDOM_SIZE); + + olm_create_outbound_session(session->session, + olmAccount, + deviceKey, strlen(deviceKey), + deviceOnetimeKey, strlen(deviceOnetimeKey), + random, OLM_OUTBOUND_SESSION_RANDOM_SIZE); + return session->session != NULL; } @@ -157,6 +232,24 @@ MatrixClientInit( strcpy(client->server, server); + // init olm account + MatrixOlmAccountInit(&client->olmAccount); + + // set device key + static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE]; + size_t res = + olm_account_identity_keys( + client->olmAccount.account, + deviceKeysJson, + OLM_IDENTITY_KEYS_JSON_SIZE); + + mjson_get_string(deviceKeysJson, res, + "$.curve25519", + client->deviceKey, DEVICE_KEY_SIZE); + mjson_get_string(deviceKeysJson, res, + "$.ed25519", + client->signingKey, SIGNING_KEY_SIZE); + return true; } @@ -167,7 +260,7 @@ MatrixClientSetAccessToken( { int accessTokenLen = strlen(accessToken); - if (accessTokenLen < ACCESS_TOKEN_SIZE - 1) + if (accessTokenLen > ACCESS_TOKEN_SIZE - 1) return false; for (int i = 0; i < accessTokenLen; i++) @@ -176,6 +269,200 @@ MatrixClientSetAccessToken( return true; } +bool +MatrixClientSetDeviceId( + MatrixClient * client, + const char * deviceId) +{ + int deviceIdLen = strlen(deviceId); + + if (deviceIdLen > DEVICE_ID_SIZE - 1) + return false; + + for (int i = 0; i < deviceIdLen; i++) + client->deviceId[i] = deviceId[i]; + + return true; +} + +bool +MatrixClientSetUserId( + MatrixClient * client, + const char * userId) +{ + int userIdLen = strlen(userId); + + if (userIdLen > USER_ID_SIZE - 1) + return false; + + for (int i = 0; i < userIdLen; i++) + client->userId[i] = userId[i]; + + return true; +} + +bool +MatrixClientGenerateOnetimeKeys( + MatrixClient * client, + int numberOfKeys) +{ + static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE]; + Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE); + + size_t res = + olm_account_generate_one_time_keys(client->olmAccount.account, + numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE); + + return res != olm_error(); +} + +// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload +bool +MatrixClientUploadOnetimeKeys( + MatrixClient * client) +{ + static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE]; + + mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE, + "{\"one_time_keys\":{"); + + static char onetimeKeysBuffer[1024]; + olm_account_one_time_keys(client->olmAccount.account, + onetimeKeysBuffer, 1024); + + const char *keys; + int keysLen; + mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen); + + int koff, klen, voff, vlen, vtype, off = 0; + while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) { + static char keyJson[JSON_ONETIME_KEY_SIZE]; + + snprintf(keyJson, JSON_ONETIME_KEY_SIZE, + "{\"key\":\"%.*s\"}", + vlen-2, keys + voff+1); + + static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE]; + + JsonSign(client, + keyJson, JSON_ONETIME_KEY_SIZE, + keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE); + + mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), + "\"signed_curve25519:%.*s\":%s,", + klen-2, keys + koff+1, + keyJsonSigned); + } + + mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), + "}}"); + + static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE]; + MatrixHttpPost(client, + KEYS_UPLOAD_URL, + requestBuffer, + responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE, + true); + + return true; +} + +// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload +bool +MatrixClientUploadDeviceKeys( + MatrixClient * client) +{ + static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE]; + + mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, + "{\"device_keys\":{" + "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"]," + "\"device_id\":\"%s\"," + "\"keys\":{" + "\"curve25519:%s\":\"%s\"," + "\"ed25519:%s\":\"%s\"" + "}," + "\"user_id\":\"%s\"" + "}}", + client->deviceId, + client->deviceId, client->deviceKey, + client->deviceId, client->signingKey, + client->userId); + + static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; + JsonSign(client, + deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, + deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE); + + + static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE]; + MatrixHttpPost(client, + KEYS_UPLOAD_URL, + deviceKeysSignedBuffer, + responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE, + true); + + return true; +} + +// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysclaim +bool +MatrixClientClaimOnetimeKey( + MatrixClient * client, + const char * userId, + const char * deviceId, + char * outOnetimeKey, int outOnetimeKeyCap) +{ + static char requestBuffer[KEYS_CLAIM_REQUEST_SIZE]; + mjson_snprintf(requestBuffer, KEYS_CLAIM_REQUEST_SIZE, + "{" + "\"one_time_keys\": {" + "\"%s\": {" + "\"%s\": \"signed_curve25519\"" + "}" + "}," + "\"timeout\": 10000" + "}", + userId, + deviceId); + + static char responseBuffer[KEYS_CLAIM_RESPONSE_SIZE]; + MatrixHttpPost(client, + KEYS_CLAIM_URL, + requestBuffer, + responseBuffer, KEYS_CLAIM_RESPONSE_SIZE, + true); + + char userIdEscaped[USER_ID_SIZE]; + JsonEscape(userId, strlen(userId), + userIdEscaped, USER_ID_SIZE); + + static char query[JSON_QUERY_SIZE]; + snprintf(query, JSON_QUERY_SIZE, + "$.one_time_keys.%s.%s", + userIdEscaped, + deviceId); + + const char * keyObject; + int keyObjectSize; + mjson_find(responseBuffer, strlen(responseBuffer), + query, + &keyObject, &keyObjectSize); + + int koff, klen, voff, vlen, vtype; + mjson_next(keyObject, keyObjectSize, 0, + &koff, &klen, &voff, &vlen, &vtype); + + mjson_get_string(keyObject + voff, vlen, + "$.key", outOnetimeKey, outOnetimeKeyCap); + + printf("onetime key: %s\n", outOnetimeKey); + + // TODO: verify signature + + return true; +} + // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login bool MatrixClientLoginPassword( @@ -325,11 +612,12 @@ MatrixClientSync( bool MatrixClientShareMegolmOutSession( MatrixClient * client, + const char * userId, const char * deviceId, MatrixMegolmOutSession * session) { // generate room key event - char eventBuffer[KEY_SHARE_EVENT_LEN]; + static char eventBuffer[KEY_SHARE_EVENT_LEN]; sprintf(eventBuffer, "{" "\"algorithm\":\"m.megolm.v1.aes-sha2\"," @@ -344,7 +632,7 @@ MatrixClientShareMegolmOutSession( // get olm session MatrixOlmSession * olmSession; - MatrixClientGetOlmSession(client, deviceId, &olmSession); + MatrixClientGetOlmSession(client, userId, deviceId, &olmSession); // encrypt char encryptedBuffer[KEY_SHARE_EVENT_LEN]; @@ -362,6 +650,36 @@ MatrixClientShareMegolmOutSession( return true; } +bool +MatrixClientShareMegolmOutSessionTest( + MatrixClient * client, + const char * deviceId, + MatrixMegolmOutSession * session) +{ + // generate room key event + char eventBuffer[KEY_SHARE_EVENT_LEN]; + sprintf(eventBuffer, + "{" + "\"algorithm\":\"m.megolm.v1.aes-sha2\"," + "\"room_id\":\"%s\"," + "\"session_id\":\"%s\"," + "\"session_key\":\"%s\"" + "}", + session->roomId, + session->id, + session->key + ); + + // send + MatrixClientSendToDevice(client, + client->userId, + deviceId, + eventBuffer, + "m.room_key"); + + return true; +} + // bool // MatrixClientSetMegolmOutSession( // MatrixClient * client, @@ -413,6 +731,7 @@ MatrixClientGetMegolmOutSession( bool MatrixClientGetOlmSession( MatrixClient * client, + const char * userId, const char * deviceId, MatrixOlmSession ** outSession) { @@ -427,9 +746,23 @@ MatrixClientGetOlmSession( if (client->numOlmSessions < NUM_OLM_SESSIONS) { - MatrixOlmSessionInit( + static char deviceKey[DEVICE_KEY_SIZE]; + MatrixClientGetDeviceKey(client, + deviceId, + deviceKey, DEVICE_KEY_SIZE); + + char onetimeKey[ONETIME_KEY_SIZE]; + MatrixClientClaimOnetimeKey(client, + userId, + deviceId, + onetimeKey, ONETIME_KEY_SIZE); + + MatrixOlmSessionFrom( &client->olmSessions[client->numOlmSessions], - deviceId); + client->olmAccount.account, + deviceId, + deviceKey, + onetimeKey); *outSession = &client->olmSessions[client->numOlmSessions]; @@ -488,7 +821,7 @@ MatrixClientSendToDeviceEncrypted( { // get olm session MatrixOlmSession * olmSession; - MatrixClientGetOlmSession(client, deviceId, &olmSession); + MatrixClientGetOlmSession(client, userId, deviceId, &olmSession); // create event json char deviceKey[DEVICE_KEY_SIZE]; @@ -592,68 +925,65 @@ bool MatrixClientRequestDeviceKeys( MatrixClient * client) { - char userIdEscaped[USER_ID_SIZE]; + static char userIdEscaped[USER_ID_SIZE]; JsonEscape(client->userId, strlen(client->userId), userIdEscaped, USER_ID_SIZE); - char request[KEYS_QUERY_REQUEST_SIZE]; + static char request[KEYS_QUERY_REQUEST_SIZE]; snprintf(request, KEYS_QUERY_REQUEST_SIZE, - "{\"device_keys\":{\"%s\":[]}}", userIdEscaped); + "{\"device_keys\":{\"%s\":[]}}", client->userId); - char responseBuffer[KEYS_QUERY_RESPONSE_SIZE]; + static char responseBuffer[KEYS_QUERY_RESPONSE_SIZE]; bool requestResult = MatrixHttpPost(client, KEYS_QUERY_URL, request, responseBuffer, KEYS_QUERY_RESPONSE_SIZE, true); - if (requestResult) - { - // query for retrieving device keys for user id - char query[JSON_QUERY_SIZE]; - snprintf(query, JSON_QUERY_SIZE, - "$.device_keys.%s", userIdEscaped); - - const char * s; - int slen; - mjson_find(responseBuffer, strlen(responseBuffer), - query, &s, &slen); + if (! requestResult) + return false; - // loop over keys - - int koff, klen, voff, vlen, vtype, off; - for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen, - &voff, &vlen, &vtype)) != 0; ) { - const char * key = s + koff; - const char * val = s + voff; - - // set device id, "key" is the JSON key - MatrixDevice d; - strncpy(d.deviceId, key, klen); - - // look for device key in value - char deviceKeyQuery[JSON_QUERY_SIZE]; - snprintf(deviceKeyQuery, JSON_QUERY_SIZE, - "$.keys.curve25519:%.*s", klen, key); - mjson_get_string(val, vlen, - deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE); - - // add device - if (client->numDevices < NUM_DEVICES) - { - client->devices[client->numDevices] = d; - client->numDevices++; - } - else - { - return false; - } - } + // query for retrieving device keys for user id + static char query[JSON_QUERY_SIZE]; + snprintf(query, JSON_QUERY_SIZE, + "$.device_keys.%s", userIdEscaped); + + const char * s; + int slen; + mjson_find(responseBuffer, strlen(responseBuffer), + query, &s, &slen); - return true; - } - else - { - return false; + // loop over keys + + int koff, klen, voff, vlen, vtype, off = 0; + for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen, + &voff, &vlen, &vtype)) != 0; ) { + const char * key = s + koff; + const char * val = s + voff; + + // set device id, "key" is the JSON key + MatrixDevice d; + snprintf(d.deviceId, DEVICE_ID_SIZE, + "%.*s", klen-2, key+1); + + // look for device key in value + static char deviceKeyQuery[JSON_QUERY_SIZE]; + snprintf(deviceKeyQuery, JSON_QUERY_SIZE, + "$.keys.curve25519:%s", d.deviceId); + mjson_get_string(val, vlen, + deviceKeyQuery, d.deviceKey, DEVICE_KEY_SIZE); + + // add device + if (client->numDevices < NUM_DEVICES) + { + client->devices[client->numDevices] = d; + client->numDevices++; + } + else + { + return false; + } } + + return true; }