]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.h
matrix.c: add masterKey and verified, add HandleSync/HandleEvent
[matrix_esp_thesis] / src / matrix.h
index 073f61002d6c2486cd64d0580ef8199230d04d4b..81b61702d5a4178288c33f011f79936f8917e75e 100644 (file)
 \r
 \r
 #define USER_ID_SIZE 64\r
+#define ROOM_ID_SIZE 128\r
 #define SERVER_SIZE 20\r
 #define ACCESS_TOKEN_SIZE 40\r
 #define DEVICE_ID_SIZE 20\r
 #define EXPIRE_MS_SIZE 20\r
 #define REFRESH_TOKEN_SIZE 20\r
-#define MAX_URL_LEN 128\r
+#define MAX_URL_LEN 1024\r
 \r
 #define OLM_IDENTITY_KEYS_JSON_SIZE 128\r
 #define DEVICE_KEY_SIZE 44\r
 #define SIGNING_KEY_SIZE 44\r
 #define ONETIME_KEY_SIZE 44\r
+#define MASTER_KEY_SIZE 44\r
 \r
 #define KEY_SHARE_EVENT_LEN 1024\r
 \r
 #define JSON_ONETIME_KEY_SIGNED_SIZE 256\r
 #define JSON_SIGNATURE_SIZE 256\r
 \r
-#define NUM_MEGOLM_SESSIONS 10\r
-#define NUM_OLM_SESSIONS 10\r
+#define NUM_MEGOLM_SESSIONS 2\r
+#define NUM_OLM_SESSIONS 2\r
 #define NUM_DEVICES 10\r
 \r
+// HTTP\r
+\r
+typedef struct MatrixHttpConnection MatrixHttpConnection;\r
+\r
+bool\r
+MatrixHttpInit(\r
+    MatrixHttpConnection ** hc,\r
+    const char * host);\r
+\r
+bool\r
+MatrixHttpDeinit(\r
+    MatrixHttpConnection ** hc);\r
+    \r
+bool\r
+MatrixHttpSetAccessToken(\r
+    MatrixHttpConnection * hc,\r
+    const char * accessToken);\r
+\r
+bool\r
+MatrixHttpGet(\r
+    MatrixHttpConnection * hc,\r
+    const char * url,\r
+    char * outResponseBuffer, int outResponseCap,\r
+    bool authenticated);\r
+\r
+bool\r
+MatrixHttpPost(\r
+    MatrixHttpConnection * hc,\r
+    const char * url,\r
+    const char * requestBuffer,\r
+    char * outResponseBuffer, int outResponseCap,\r
+    bool authenticated);\r
+\r
+bool\r
+MatrixHttpPut(\r
+    MatrixHttpConnection * hc,\r
+    const char * url,\r
+    const char * requestBuffer,\r
+    char * outResponseBuffer, int outResponseCap,\r
+    bool authenticated);\r
+\r
+\r
+\r
 // Matrix Device\r
 \r
 typedef struct MatrixDevice {\r
     char deviceId[DEVICE_ID_SIZE];\r
     char deviceKey[DEVICE_KEY_SIZE];\r
+    char signingKey[SIGNING_KEY_SIZE];\r
 } MatrixDevice;\r
 \r
 \r
@@ -69,17 +115,48 @@ bool
 MatrixOlmAccountInit(\r
     MatrixOlmAccount * account);\r
 \r
+bool\r
+MatrixOlmAccountUnpickle(\r
+    MatrixOlmAccount * account,\r
+    void * pickled, int pickledLen,\r
+    const void * key, int keyLen);\r
+\r
+bool\r
+MatrixOlmAccountGetDeviceKey(\r
+    MatrixOlmAccount * account,\r
+    char * key, int keyCap);\r
+    \r
+bool\r
+MatrixOlmAccountGetSigningKey(\r
+    MatrixOlmAccount * account,\r
+    char * key, int keyCap);\r
+\r
 \r
 // Matrix Olm Session\r
 \r
 typedef struct MatrixOlmSession {\r
-    const char * deviceId;\r
+    const char * deviceId; // TODO: char[]\r
 \r
     int type;\r
     OlmSession * session;\r
     char memory[OLM_SESSION_MEMORY_SIZE];\r
 } MatrixOlmSession;\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
+bool\r
+MatrixOlmSessionFrom(\r
+    MatrixOlmSession * session,\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * encrypted);\r
+\r
 bool\r
 MatrixOlmSessionTo(\r
     MatrixOlmSession * session,\r
@@ -94,21 +171,46 @@ MatrixOlmSessionEncrypt(
     const char * plaintext,\r
     char * outBuffer, int outBufferCap);\r
 \r
+bool\r
+MatrixOlmSessionDecrypt(\r
+    MatrixOlmSession * session,\r
+    size_t messageType,\r
+    char * encrypted,\r
+    char * outBuffer, int outBufferCap);\r
+\r
 \r
 // Matrix Megolm Session\r
 \r
 typedef struct MatrixMegolmInSession {\r
+    char roomId[ROOM_ID_SIZE];\r
+    char id[MEGOLM_SESSION_ID_SIZE];\r
+    char key[MEGOLM_SESSION_KEY_SIZE];\r
+\r
     OlmInboundGroupSession * session;\r
+    char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
+\r
 } MatrixMegolmInSession;\r
 \r
-typedef struct MatrixMegolmOutSession {\r
-    const char * roomId;\r
+bool\r
+MatrixMegolmInSessionInit(\r
+    MatrixMegolmInSession * session,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * sessionKey, int sessionKeyLen);\r
 \r
-    OlmOutboundGroupSession * session;\r
-    char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
+bool\r
+MatrixMegolmInSessionDecrypt(\r
+    MatrixMegolmInSession * session,\r
+    const char * encrypted, int encryptedLen,\r
+    char * outDecrypted, int outDecryptedCap);\r
 \r
+typedef struct MatrixMegolmOutSession {\r
+    char roomId[ROOM_ID_SIZE];\r
     char id[MEGOLM_SESSION_ID_SIZE];\r
     char key[MEGOLM_SESSION_KEY_SIZE];\r
+\r
+    OlmOutboundGroupSession * session;\r
+    char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
 } MatrixMegolmOutSession;\r
 \r
 bool\r
@@ -122,18 +224,6 @@ MatrixMegolmOutSessionEncrypt(
     const char * plaintext,\r
     char * outBuffer, int outBufferCap);\r
 \r
-bool\r
-MatrixMegolmOutSessionSave(\r
-    MatrixMegolmOutSession * session,\r
-    const char * filename,\r
-    const char * key);\r
-    \r
-bool\r
-MatrixMegolmOutSessionLoad(\r
-    MatrixMegolmOutSession * session,\r
-    const char * filename,\r
-    const char * key);\r
-\r
 \r
 // Matrix Client\r
 \r
@@ -150,33 +240,24 @@ typedef struct MatrixClient {
     MatrixDevice devices[NUM_DEVICES];\r
     int numDevices;\r
     \r
-    char deviceKey[DEVICE_KEY_SIZE];\r
-    char signingKey[DEVICE_KEY_SIZE];\r
+    // char deviceKey[DEVICE_KEY_SIZE];\r
+    // char signingKey[DEVICE_KEY_SIZE];\r
 \r
     char userId[USER_ID_SIZE];\r
-    char server[SERVER_SIZE];\r
     char accessToken[ACCESS_TOKEN_SIZE];\r
     char deviceId[DEVICE_ID_SIZE];\r
     char expireMs[EXPIRE_MS_SIZE];\r
     char refreshToken[REFRESH_TOKEN_SIZE];\r
+    char masterKey[MASTER_KEY_SIZE];\r
+\r
+    bool verified;\r
 \r
-    void * httpUserData;\r
+    MatrixHttpConnection * hc;\r
 } MatrixClient;\r
 \r
 bool\r
 MatrixClientInit(\r
-    MatrixClient * client,\r
-    const char * server);\r
-\r
-bool\r
-MatrixClientSave(\r
-    MatrixClient * client,\r
-    const char * filename);\r
-\r
-bool\r
-MatrixClientLoad(\r
-    MatrixClient * client,\r
-    const char * filename);\r
+    MatrixClient * client);\r
 \r
 bool\r
 MatrixClientSetAccessToken(\r
@@ -234,10 +315,29 @@ MatrixClientSendEventEncrypted(
     const char * msgType,\r
     const char * msgBody);\r
 \r
+void\r
+HandleEvent(\r
+    MatrixClient * client,\r
+    const char * event, int eventLen);\r
+\r
+void\r
+HandleRoomEvent(\r
+    MatrixClient * client,\r
+    const char * room, int roomLen,\r
+    const char * event, int eventLen);\r
+\r
 bool\r
 MatrixClientSync(\r
     MatrixClient * client,\r
-    char * outSync, int outSyncCap);\r
+    char * outSyncBuffer, int outSyncCap,\r
+    char * nextBatch, int nextBatchCap);\r
+\r
+bool\r
+MatrixClientGetRoomEvent(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * eventId,\r
+    char * outEvent, int outEventCap);\r
 \r
 bool\r
 MatrixClientShareMegolmOutSession(\r
@@ -249,6 +349,7 @@ MatrixClientShareMegolmOutSession(
 bool\r
 MatrixClientShareMegolmOutSessionTest(\r
     MatrixClient * client,\r
+    const char * userId,\r
     const char * deviceId,\r
     MatrixMegolmOutSession * session);\r
 \r
@@ -259,10 +360,34 @@ MatrixClientGetMegolmOutSession(
     MatrixMegolmOutSession ** outSession);\r
 \r
 bool\r
-MatrixClientSetMegolmOutSession(\r
+MatrixClientNewMegolmOutSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
-    MatrixMegolmOutSession session);\r
+    MatrixMegolmOutSession ** outSession);\r
+\r
+bool\r
+MatrixClientGetMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId, int roomIdLen,\r
+    const char * sessionId, int sessionIdLen,\r
+    MatrixMegolmInSession ** outSession);\r
+\r
+bool\r
+MatrixClientNewMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * sessionKey,\r
+    MatrixMegolmInSession ** outSession);\r
+    \r
+bool\r
+MatrixClientRequestMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * sessionId,\r
+    const char * senderKey,\r
+    const char * userId,\r
+    const char * deviceId); // TODO: remove deviceId (query all devices)\r
 \r
 bool\r
 MatrixClientGetOlmSession(\r
@@ -271,6 +396,21 @@ MatrixClientGetOlmSession(
     const char * deviceId,\r
     MatrixOlmSession ** outSession);\r
 \r
+bool\r
+MatrixClientNewOlmSessionIn(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    const char * encrypted,\r
+    MatrixOlmSession ** outSession);\r
+    \r
+bool\r
+MatrixClientNewOlmSessionOut(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    MatrixOlmSession ** outSession);\r
+\r
 bool\r
 MatrixClientSendToDevice(\r
     MatrixClient * client,\r
@@ -288,58 +428,36 @@ MatrixClientSendToDeviceEncrypted(
     const char * msgType);\r
 \r
 bool\r
-MatrixClientGetDeviceKey(\r
+MatrixClientSendDummy(\r
     MatrixClient * client,\r
-    const char * deviceId,\r
-    char * outDeviceKey, int outDeviceKeyCap);\r
+    const char * userId,\r
+    const char * deviceId);\r
 \r
 bool\r
-MatrixClientGetDeviceKey(\r
+MatrixClientRequestDeviceKey(\r
     MatrixClient * client,\r
     const char * deviceId,\r
     char * outDeviceKey, int outDeviceKeyCap);\r
-\r
+    \r
 bool\r
-MatrixClientRequestDeviceKeys(\r
-    MatrixClient * client);\r
-\r
-\r
-\r
+MatrixClientRequestSigningKey(\r
+    MatrixClient * client,\r
+    const char * deviceId,\r
+    char * outSigningKey, int outSigningKeyCap);\r
 \r
 bool\r
-MatrixHttpInit(\r
-    MatrixClient * client);\r
+MatrixClientRequestMasterKey(\r
+    MatrixClient * client,\r
+    char * outMasterKey, int outMasterKeyCap);\r
 \r
 bool\r
-MatrixHttpConnect(\r
+MatrixClientRequestDeviceKeys(\r
     MatrixClient * client);\r
 \r
 bool\r
-MatrixHttpDeinit(\r
+MatrixClientDeleteDevice(\r
     MatrixClient * client);\r
 \r
-bool\r
-MatrixHttpGet(\r
-    MatrixClient * client,\r
-    const char * url,\r
-    char * outResponseBuffer, int outResponseCap,\r
-    bool authenticated);\r
-\r
-bool\r
-MatrixHttpPost(\r
-    MatrixClient * client,\r
-    const char * url,\r
-    const char * requestBuffer,\r
-    char * outResponseBuffer, int outResponseCap,\r
-    bool authenticated);\r
-\r
-bool\r
-MatrixHttpPut(\r
-    MatrixClient * client,\r
-    const char * url,\r
-    const char * requestBuffer,\r
-    char * outResponseBuffer, int outResponseCap,\r
-    bool authenticated);\r
 \r
 // util\r
 \r
@@ -351,7 +469,13 @@ JsonEscape(
     const char * sIn, int sInLen,\r
     char * sOut, int sOutCap);\r
     \r
-bool JsonSign(\r
+bool\r
+JsonCanonicalize(\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+    \r
+bool\r
+JsonSign(\r
     MatrixClient * client,\r
     const char * sIn, int sInLen,\r
     char * sOut, int sOutCap);\r