From: Patrick Date: Thu, 22 Jun 2023 13:56:21 +0000 (+0200) Subject: generate identity keys X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/9826729ea9eb492b0b25c52b934d9f1283bb70dd generate identity keys --- diff --git a/examples/SendEncrypted.c b/examples/SendEncrypted.c index db2f83c..0c4a7c8 100644 --- a/examples/SendEncrypted.c +++ b/examples/SendEncrypted.c @@ -17,6 +17,8 @@ main(void) MatrixClientSetAccessToken(&client, ACCESS_TOKEN); + MatrixClientSetDeviceId(&client, + DEVICE_ID); // MatrixMegolmOutSession megolmOutSession; // MatrixMegolmOutSessionInit(&megolmOutSession); diff --git a/src/matrix.c b/src/matrix.c index cc34a0a..33988b4 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -157,6 +157,33 @@ MatrixClientInit( strcpy(client->server, server); + // init olm account + client->olmAccount = olm_account(client->olmAccountMemory); + + static uint8_t random[OLM_ACCOUNT_RANDOM_SIZE]; + Randomize(random, OLM_ACCOUNT_RANDOM_SIZE); + + size_t res; + res = olm_create_account( + client->olmAccount, + random, + OLM_ACCOUNT_RANDOM_SIZE); + + // set device key + static char deviceKeysJson[OLM_IDENTITY_KEYS_JSON_SIZE]; + res = + olm_account_identity_keys( + client->olmAccount, + 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 +194,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 +203,22 @@ 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; +} + // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login bool MatrixClientLoginPassword( diff --git a/src/matrix.h b/src/matrix.h index 6538e12..60561aa 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -18,10 +18,15 @@ #define REFRESH_TOKEN_SIZE 20 #define MAX_URL_LEN 128 -#define DEVICE_KEY_SIZE 20 +#define OLM_IDENTITY_KEYS_JSON_SIZE 128 +#define DEVICE_KEY_SIZE 44 +#define SIGNING_KEY_SIZE 44 #define KEY_SHARE_EVENT_LEN 1024 +#define OLM_ACCOUNT_MEMORY_SIZE 7528 +#define OLM_ACCOUNT_RANDOM_SIZE 32+32 + #define OLM_SESSION_MEMORY_SIZE 3352 #define OLM_ENCRYPT_RANDOM_SIZE 32 @@ -97,7 +102,7 @@ MatrixMegolmOutSessionEncrypt( typedef struct MatrixClient { OlmAccount * olmAccount; - OlmSession * olmSession; + char olmAccountMemory[OLM_ACCOUNT_MEMORY_SIZE]; MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS]; int numMegolmInSessions; @@ -110,6 +115,7 @@ typedef struct MatrixClient { int numDevices; char deviceKey[DEVICE_KEY_SIZE]; + char signingKey[DEVICE_KEY_SIZE]; char userId[USER_ID_SIZE]; char server[SERVER_SIZE]; @@ -131,6 +137,11 @@ MatrixClientSetAccessToken( MatrixClient * client, const char * accessToken); +bool +MatrixClientSetDeviceId( + MatrixClient * client, + const char * deviceId); + bool MatrixClientLoginPassword( MatrixClient * client,