From: Patrick Date: Mon, 10 Jul 2023 18:57:49 +0000 (+0200) Subject: more changes towards sharing megolm sessions X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/6646bd126f9faf1e687b203512d29da243acaa90 more changes towards sharing megolm sessions --- diff --git a/examples/Keys.c b/examples/Keys.c index f59a1d5..f79488b 100644 --- a/examples/Keys.c +++ b/examples/Keys.c @@ -25,6 +25,8 @@ main(void) MatrixClientUploadOnetimeKeys(&client); MatrixClientUploadDeviceKeys(&client); + + printf("device key: %s\n", client.deviceKey); MatrixHttpDeinit(&client); diff --git a/examples/SendEncrypted.c b/examples/SendEncrypted.c index 7e0b1d4..0005592 100644 --- a/examples/SendEncrypted.c +++ b/examples/SendEncrypted.c @@ -23,18 +23,20 @@ main(void) MatrixClientSetUserId(&client, USER_ID); - // MatrixMegolmOutSession megolmOutSession; - // MatrixMegolmOutSessionInit(&megolmOutSession); - // MatrixClientSetMegolmOutSession(&client, - // ROOM_ID, - // megolmOutSession); + MatrixClientUploadDeviceKeys(&client); MatrixClientSendEventEncrypted(&client, ROOM_ID, "m.room.message", "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}"); + MatrixClientSendToDeviceEncrypted(&client, + USER_ID, + "ULZZOKJBYN", + "{}", + "m.dummy"); + MatrixClientShareMegolmOutSession(&client, USER_ID, "ULZZOKJBYN", diff --git a/examples/Sync.c b/examples/Sync.c index a49cf65..e845056 100644 --- a/examples/Sync.c +++ b/examples/Sync.c @@ -3,7 +3,6 @@ #define SERVER "https://matrix.org" #define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" -#define DEVICE_ID "MAZNCCZLBR" int main(void) @@ -17,9 +16,9 @@ main(void) MatrixClientSetAccessToken(&client, ACCESS_TOKEN); - static char syncBuffer[20000]; + static char syncBuffer[40000]; MatrixClientSync(&client, - syncBuffer, 20000); + syncBuffer, 40000); printf("%s", syncBuffer); MatrixHttpDeinit(&client); diff --git a/src/matrix.c b/src/matrix.c index 966a038..1d2c3fa 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -9,7 +9,7 @@ #define LOGIN_RESPONSE_SIZE 1024 #define LOGIN_URL "/_matrix/client/v3/login" -#define ENCRYPTED_REQUEST_SIZE 512 +#define ENCRYPTED_REQUEST_SIZE (1024*5) #define ENCRYPTED_EVENT_SIZE 1024 #define ROOMEVENT_REQUEST_SIZE 256 #define ROOMEVENT_RESPONSE_SIZE 1024 @@ -40,7 +40,7 @@ Randomize( uint8_t * random, int randomLen) { - static bool first = false; + static bool first = true; if (first) { srand(time(0)); first = false; } for (int i = 0; i < randomLen; i++) @@ -133,7 +133,7 @@ MatrixOlmAccountInit( // TODO: in/outbound sessions bool -MatrixOlmSessionFrom( +MatrixOlmSessionTo( MatrixOlmSession * session, OlmAccount * olmAccount, const char * deviceId, @@ -150,11 +150,16 @@ MatrixOlmSessionFrom( 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); + size_t res = + olm_create_outbound_session(session->session, + olmAccount, + deviceKey, strlen(deviceKey), + deviceOnetimeKey, strlen(deviceOnetimeKey), + random, OLM_OUTBOUND_SESSION_RANDOM_SIZE); + + if (res == olm_error()) { + printf("error olm: %s\n", olm_account_last_error(olmAccount)); + } return session->session != NULL; } @@ -455,8 +460,6 @@ MatrixClientClaimOnetimeKey( mjson_get_string(keyObject + voff, vlen, "$.key", outOnetimeKey, outOnetimeKeyCap); - - printf("onetime key: %s\n", outOnetimeKey); // TODO: verify signature @@ -579,15 +582,15 @@ MatrixClientSendEventEncrypted( sprintf(encryptedEventBuffer, "{" "\"algorithm\":\"m.megolm.v1.aes-sha2\"," - "\"sender_key\":\"%s\"," "\"ciphertext\":\"%s\"," - "\"session_id\":\"%s\"," - "\"device_id\":\"%s\"" + "\"device_id\":\"%s\"," + "\"sender_key\":\"%s\"," + "\"session_id\":\"%s\"" "}", - senderKey, encryptedBuffer, - sessionId, - deviceId); + deviceId, + senderKey, + sessionId); // send return MatrixClientSendEvent(client, @@ -630,21 +633,21 @@ MatrixClientShareMegolmOutSession( session->key ); - // get olm session - MatrixOlmSession * olmSession; - MatrixClientGetOlmSession(client, userId, deviceId, &olmSession); + // // get olm session + // MatrixOlmSession * olmSession; + // MatrixClientGetOlmSession(client, userId, deviceId, &olmSession); - // encrypt - char encryptedBuffer[KEY_SHARE_EVENT_LEN]; - MatrixOlmSessionEncrypt(olmSession, - eventBuffer, - encryptedBuffer, KEY_SHARE_EVENT_LEN); + // // encrypt + // char encryptedBuffer[KEY_SHARE_EVENT_LEN]; + // MatrixOlmSessionEncrypt(olmSession, + // eventBuffer, + // encryptedBuffer, KEY_SHARE_EVENT_LEN); // send MatrixClientSendToDeviceEncrypted(client, - client->userId, + userId, deviceId, - encryptedBuffer, + eventBuffer, "m.room_key"); return true; @@ -757,7 +760,7 @@ MatrixClientGetOlmSession( deviceId, onetimeKey, ONETIME_KEY_SIZE); - MatrixOlmSessionFrom( + MatrixOlmSessionTo( &client->olmSessions[client->numOlmSessions], client->olmAccount.account, deviceId, @@ -826,7 +829,6 @@ MatrixClientSendToDeviceEncrypted( // create event json char deviceKey[DEVICE_KEY_SIZE]; MatrixClientGetDeviceKey(client, deviceId, deviceKey, DEVICE_KEY_SIZE); - const char * senderKey = client->deviceKey; static char eventBuffer[TODEVICE_EVENT_SIZE]; sprintf(eventBuffer, @@ -858,19 +860,21 @@ MatrixClientSendToDeviceEncrypted( static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE]; sprintf(encryptedEventBuffer, "{" - "\"algorithm\":\"m.megolm.v1.aes-sha2\"," - "\"sender_key\":\"%s\"," + "\"algorithm\":\"m.olm.v1.curve25519-aes-sha2\"," "\"ciphertext\":{" "\"%s\":{" "\"body\":\"%s\"," - "\"type\":\"%d\"" + "\"type\":%d" "}" - "}" + "}," + "\"device_id\":\"%s\"," + "\"sender_key\":\"%s\"" "}", - senderKey, deviceKey, encryptedBuffer, - olmSession->type); + 0, //olmSession->type, + client->deviceId, + client->deviceKey); // send return MatrixClientSendToDevice( @@ -908,9 +912,16 @@ MatrixClientGetDeviceKey( const char * deviceId, char * outDeviceKey, int outDeviceKeyCap) { + MatrixDevice * device; + + if (MatrixClientFindDevice(client, deviceId, &device)) + { + strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap); + return true; + } + MatrixClientRequestDeviceKeys(client); - MatrixDevice * device; if (MatrixClientFindDevice(client, deviceId, &device)) { strncpy(outDeviceKey, device->deviceKey, outDeviceKeyCap); diff --git a/src/matrix.h b/src/matrix.h index 5963d7d..a06eab9 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -93,7 +93,7 @@ typedef struct MatrixOlmSession { } MatrixOlmSession; bool -MatrixOlmSessionFrom( +MatrixOlmSessionTo( MatrixOlmSession * session, OlmAccount * olmAccount, const char * deviceId, diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c index 17edbc8..0b6c267 100644 --- a/src/matrix_http_mongoose.c +++ b/src/matrix_http_mongoose.c @@ -220,20 +220,6 @@ MatrixHttpPut( else authorizationHeader[0] = '\0'; - printf("PUT %s HTTP/1.0\r\n" - "Host: %.*s\r\n" - "%s" - "Content-Type: application/json\r\n" - "Content-Length: %d\r\n" - "\r\n" - "%s" - "\r\n", - url, - host.len, host.ptr, - authorizationHeader, - strlen(requestBuffer), - requestBuffer); - mg_printf(conn->connection, "PUT %s HTTP/1.0\r\n" "Host: %.*s\r\n"