]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.h
remove newline
[matrix_esp_thesis] / src / matrix.h
index 38fb767742cdaf23948313d06ac47a6cfe09d9e3..226a341152b9e298b2984d0086398d3a6a08540c 100644 (file)
 \r
 \r
 #define USER_ID_SIZE 64\r
 \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 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
 \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
 \r
 #define KEY_SHARE_EVENT_LEN 1024\r
 \r
@@ -29,8 +32,9 @@
 \r
 #define OLM_SESSION_MEMORY_SIZE 3352\r
 #define OLM_ENCRYPT_RANDOM_SIZE 32\r
 \r
 #define OLM_SESSION_MEMORY_SIZE 3352\r
 #define OLM_ENCRYPT_RANDOM_SIZE 32\r
+#define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)\r
 \r
 \r
-#define OLM_ONETIME_KEYS_RANDOM_SIZE 32*10\r
+#define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)\r
 #define OLM_KEY_ID_SIZE 32\r
 \r
 #define OLM_SIGNATURE_SIZE 128\r
 #define OLM_KEY_ID_SIZE 32\r
 \r
 #define OLM_SIGNATURE_SIZE 128\r
 #define JSON_ONETIME_KEY_SIGNED_SIZE 256\r
 #define JSON_SIGNATURE_SIZE 256\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
 #define NUM_DEVICES 10\r
 \r
-void\r
-Randomize(uint8_t * random, int randomLen);\r
+// HTTP\r
+\r
+typedef struct MatrixHttpConnection MatrixHttpConnection;\r
 \r
 bool\r
 \r
 bool\r
-JsonEscape(\r
-    char * sIn, int sInLen,\r
-    char * sOut, int sOutCap);\r
+MatrixHttpInit(\r
+    MatrixHttpConnection ** hc,\r
+    const char * host);\r
+\r
+bool\r
+MatrixHttpDeinit(\r
+    MatrixHttpConnection ** hc);\r
     \r
     \r
-bool JsonSign(\r
-    char * sIn, int sInLen,\r
-    char * sOut, int sOutCap);\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
 \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
 } MatrixDevice;\r
 \r
 \r
@@ -79,11 +115,27 @@ bool
 MatrixOlmAccountInit(\r
     MatrixOlmAccount * account);\r
 \r
 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
 \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
 \r
     int type;\r
     OlmSession * session;\r
@@ -91,9 +143,29 @@ typedef struct MatrixOlmSession {
 } MatrixOlmSession;\r
 \r
 bool\r
 } MatrixOlmSession;\r
 \r
 bool\r
-MatrixOlmSessionInit(\r
+MatrixOlmSessionUnpickle(\r
     MatrixOlmSession * session,\r
     MatrixOlmSession * session,\r
-    const char * deviceId);\r
+    const char * deviceId,\r
+    void * pickled, int pickledLen,\r
+    const void * key, int keyLen);\r
+\r
+// create an olm sesseion from a type 0 message\r
+bool\r
+MatrixOlmSessionFrom(\r
+    MatrixOlmSession * session,\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * encrypted);\r
+\r
+// create a new olm session from a claimed onetime key\r
+bool\r
+MatrixOlmSessionTo(\r
+    MatrixOlmSession * session,\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * deviceOnetimeKey);\r
 \r
 bool\r
 MatrixOlmSessionEncrypt(\r
 \r
 bool\r
 MatrixOlmSessionEncrypt(\r
@@ -101,28 +173,53 @@ MatrixOlmSessionEncrypt(
     const char * plaintext,\r
     char * outBuffer, int outBufferCap);\r
 \r
     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
 \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
     OlmInboundGroupSession * session;\r
+    char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
+\r
 } MatrixMegolmInSession;\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
 \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
 \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
     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
 MatrixMegolmOutSessionInit(\r
     MatrixMegolmOutSession * session,\r
     const char * roomId);\r
 } MatrixMegolmOutSession;\r
 \r
 bool\r
 MatrixMegolmOutSessionInit(\r
     MatrixMegolmOutSession * session,\r
     const char * roomId);\r
-    \r
+\r
 bool\r
 MatrixMegolmOutSessionEncrypt(\r
     MatrixMegolmOutSession * session,\r
 bool\r
 MatrixMegolmOutSessionEncrypt(\r
     MatrixMegolmOutSession * session,\r
@@ -144,24 +241,22 @@ typedef struct MatrixClient {
     \r
     MatrixDevice devices[NUM_DEVICES];\r
     int numDevices;\r
     \r
     MatrixDevice devices[NUM_DEVICES];\r
     int numDevices;\r
-    \r
-    char deviceKey[DEVICE_KEY_SIZE];\r
-    char signingKey[DEVICE_KEY_SIZE];\r
 \r
     char userId[USER_ID_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 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
 \r
-    void * httpUserData;\r
+    bool verified;\r
+\r
+    MatrixHttpConnection * hc;\r
 } MatrixClient;\r
 \r
 bool\r
 MatrixClientInit(\r
 } MatrixClient;\r
 \r
 bool\r
 MatrixClientInit(\r
-    MatrixClient * client,\r
-    const char * server);\r
+    MatrixClient * client);\r
 \r
 bool\r
 MatrixClientSetAccessToken(\r
 \r
 bool\r
 MatrixClientSetAccessToken(\r
@@ -191,6 +286,13 @@ bool
 MatrixClientUploadDeviceKeys(\r
     MatrixClient * client);\r
 \r
 MatrixClientUploadDeviceKeys(\r
     MatrixClient * client);\r
 \r
+bool\r
+MatrixClientClaimOnetimeKey(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    char * outOnetimeKey, int outOnetimeKeyCap);\r
+\r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r
@@ -215,35 +317,87 @@ MatrixClientSendEventEncrypted(
 bool\r
 MatrixClientSync(\r
     MatrixClient * client,\r
 bool\r
 MatrixClientSync(\r
     MatrixClient * client,\r
-    char * outSyncBuffer, int outSyncCap);\r
+    char * outSyncBuffer, int outSyncCap,\r
+    char * nextBatch, int nextBatchCap);\r
 \r
 bool\r
 \r
 bool\r
-MatrixClientShareMegolmOutSession(\r
+MatrixClientGetRoomEvent(\r
     MatrixClient * client,\r
     MatrixClient * client,\r
-    const char * deviceId,\r
-    MatrixMegolmOutSession * session);\r
+    const char * roomId,\r
+    const char * eventId,\r
+    char * outEvent, int outEventCap);\r
 \r
 bool\r
 \r
 bool\r
-MatrixClientShareMegolmOutSessionTest(\r
+MatrixClientShareMegolmOutSession(\r
     MatrixClient * client,\r
     MatrixClient * client,\r
+    const char * userId,\r
     const char * deviceId,\r
     MatrixMegolmOutSession * session);\r
 \r
     const char * deviceId,\r
     MatrixMegolmOutSession * session);\r
 \r
+// try to lookup outgoing megolm session, return true if found\r
 bool\r
 MatrixClientGetMegolmOutSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
     MatrixMegolmOutSession ** outSession);\r
 \r
 bool\r
 MatrixClientGetMegolmOutSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
     MatrixMegolmOutSession ** outSession);\r
 \r
+// create a new outgoing megolm session and store it locally\r
+bool\r
+MatrixClientNewMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    MatrixMegolmOutSession ** outSession);\r
+\r
+// try to lookup incoming megolm session, return true if found\r
+bool\r
+MatrixClientGetMegolmInSession(\r
+    MatrixClient * client,\r
+    const char * roomId, int roomIdLen,\r
+    const char * sessionId, int sessionIdLen,\r
+    MatrixMegolmInSession ** outSession);\r
+\r
+// create a new incoming megolm session and store it locally\r
 bool\r
 bool\r
-MatrixClientSetMegolmOutSession(\r
+MatrixClientNewMegolmInSession(\r
     MatrixClient * client,\r
     const char * roomId,\r
     MatrixClient * client,\r
     const char * roomId,\r
-    MatrixMegolmOutSession session);\r
+    const char * sessionId,\r
+    const char * sessionKey,\r
+    MatrixMegolmInSession ** outSession);\r
+\r
+// send a m.room_key_request to the device identified by userId/devideId\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);\r
 \r
 \r
+// try to lookup olm session, return true if found\r
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    MatrixOlmSession ** outSession);\r
+\r
+// create a new olm session from a type 0 message and store it locally\r
+bool\r
+MatrixClientNewOlmSessionIn(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    const char * encrypted,\r
+    MatrixOlmSession ** outSession);\r
+\r
+// create a new olm session with device userId/deviceId and store it locally\r
+// this automatically claims the onetime key\r
+bool\r
+MatrixClientNewOlmSessionOut(\r
+    MatrixClient * client,\r
+    const char * userId,\r
     const char * deviceId,\r
     MatrixOlmSession ** outSession);\r
 \r
     const char * deviceId,\r
     MatrixOlmSession ** outSession);\r
 \r
@@ -264,52 +418,62 @@ MatrixClientSendToDeviceEncrypted(
     const char * msgType);\r
 \r
 bool\r
     const char * msgType);\r
 \r
 bool\r
-MatrixClientGetDeviceKey(\r
+MatrixClientSendDummy(\r
     MatrixClient * client,\r
     MatrixClient * client,\r
-    const char * deviceId,\r
-    char * outDeviceKey, int outDeviceKeyCap);\r
+    const char * userId,\r
+    const char * deviceId);\r
 \r
 \r
+// lookup device key locally and if not present get it from server\r
 bool\r
 bool\r
-MatrixClientGetDeviceKey(\r
+MatrixClientRequestDeviceKey(\r
     MatrixClient * client,\r
     const char * deviceId,\r
     char * outDeviceKey, int outDeviceKeyCap);\r
     MatrixClient * client,\r
     const char * deviceId,\r
     char * outDeviceKey, int outDeviceKeyCap);\r
-\r
+    \r
+// lookup signing key locally and if not present get it from server\r
 bool\r
 bool\r
-MatrixClientRequestDeviceKeys(\r
-    MatrixClient * client);\r
-\r
+MatrixClientRequestSigningKey(\r
+    MatrixClient * client,\r
+    const char * deviceId,\r
+    char * outSigningKey, int outSigningKeyCap);\r
 \r
 \r
+// lookup the master key for this user and if not present get it from server\r
+bool\r
+MatrixClientRequestMasterKey(\r
+    MatrixClient * client,\r
+    char * outMasterKey, int outMasterKeyCap);\r
 \r
 \r
+// call keys/query and store retrieved information\r
+// this is called by the other Request* functions\r
 bool\r
 bool\r
-MatrixHttpInit(\r
+MatrixClientRequestDeviceKeys(\r
     MatrixClient * client);\r
 \r
     MatrixClient * client);\r
 \r
+// delete this device on the server\r
 bool\r
 bool\r
-MatrixHttpDeinit(\r
+MatrixClientDeleteDevice(\r
     MatrixClient * client);\r
 \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
 \r
-bool\r
-MatrixHttpPost(\r
-    MatrixClient * client,\r
-    const char * url,\r
-    const char * requestBuffer,\r
-    char * outResponseBuffer, int outResponseCap,\r
-    bool authenticated);\r
+// util\r
+\r
+void\r
+Randomize(uint8_t * random, int randomLen);\r
 \r
 bool\r
 \r
 bool\r
-MatrixHttpPut(\r
+JsonEscape(\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+    \r
+bool\r
+JsonCanonicalize(\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+    \r
+bool\r
+JsonSign(\r
     MatrixClient * client,\r
     MatrixClient * client,\r
-    const char * url,\r
-    const char * requestBuffer,\r
-    char * outResponseBuffer, int outResponseCap,\r
-    bool authenticated);\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
 \r
 #endif\r
 \r
 #endif\r