X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/5cb22046a33f24c1a696990f95e13d534efef497..1273c8ea309926e377cbd5cc6dab6740910aa6ff:/src/matrix.h diff --git a/src/matrix.h b/src/matrix.h index b7f3a5b..6538e12 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -2,13 +2,15 @@ #define MATRIX__H #include +#include #include +#include #include -// TODO: fix +#define USER_ID_SIZE 64 #define SERVER_SIZE 20 #define ACCESS_TOKEN_SIZE 40 #define DEVICE_ID_SIZE 20 @@ -16,16 +18,105 @@ #define REFRESH_TOKEN_SIZE 20 #define MAX_URL_LEN 128 +#define DEVICE_KEY_SIZE 20 + +#define KEY_SHARE_EVENT_LEN 1024 + +#define OLM_SESSION_MEMORY_SIZE 3352 +#define OLM_ENCRYPT_RANDOM_SIZE 32 + +#define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232 +#define MEGOLM_SESSION_ID_SIZE 44 +#define MEGOLM_SESSION_KEY_SIZE 306 +#define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32) + +#define NUM_MEGOLM_SESSIONS 10 +#define NUM_OLM_SESSIONS 10 +#define NUM_DEVICES 10 + +void +Randomize(uint8_t * random, int randomLen); + +bool +JsonEscape( + char * sIn, int sInLen, + char * sOut, int sOutCap); + +typedef struct MatrixDevice { + char deviceId[DEVICE_ID_SIZE]; + char deviceKey[DEVICE_KEY_SIZE]; +} MatrixDevice; + +typedef struct MatrixOlmSession { + const char * deviceId; + + int type; + OlmSession * session; + char memory[OLM_SESSION_MEMORY_SIZE]; +} MatrixOlmSession; + +bool +MatrixOlmSessionInit( + MatrixOlmSession * session, + const char * deviceId); + +bool +MatrixOlmSessionEncrypt( + MatrixOlmSession * session, + const char * plaintext, + char * outBuffer, int outBufferCap); + + + +typedef struct MatrixMegolmInSession { + OlmInboundGroupSession * session; +} MatrixMegolmInSession; + +typedef struct MatrixMegolmOutSession { + const char * roomId; + + OlmOutboundGroupSession * session; + char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; + + char id[MEGOLM_SESSION_ID_SIZE]; + char key[MEGOLM_SESSION_KEY_SIZE]; +} MatrixMegolmOutSession; + +bool +MatrixMegolmOutSessionInit( + MatrixMegolmOutSession * session, + const char * roomId); + +bool +MatrixMegolmOutSessionEncrypt( + MatrixMegolmOutSession * session, + const char * plaintext, + char * outBuffer, int outBufferCap); + + typedef struct MatrixClient { OlmAccount * olmAccount; OlmSession * olmSession; + + MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS]; + int numMegolmInSessions; + MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS]; + int numMegolmOutSessions; + MatrixOlmSession olmSessions[NUM_OLM_SESSIONS]; + int numOlmSessions; + + MatrixDevice devices[NUM_DEVICES]; + int numDevices; - char server[SERVER_SIZE]; int serverLen; - char accessTokenBuffer[ACCESS_TOKEN_SIZE]; int accessTokenLen; - char deviceIdBuffer[DEVICE_ID_SIZE]; int deviceIdLen; - char expireMsBuffer[EXPIRE_MS_SIZE]; int expireMsLen; - char refreshTokenBuffer[REFRESH_TOKEN_SIZE]; int refreshTokenLen; + char deviceKey[DEVICE_KEY_SIZE]; + + char userId[USER_ID_SIZE]; + char server[SERVER_SIZE]; + char accessToken[ACCESS_TOKEN_SIZE]; + char deviceId[DEVICE_ID_SIZE]; + char expireMs[EXPIRE_MS_SIZE]; + char refreshToken[REFRESH_TOKEN_SIZE]; void * httpUserData; } MatrixClient; @@ -33,14 +124,96 @@ typedef struct MatrixClient { bool MatrixClientInit( MatrixClient * client, - char * server, int serverLen); + const char * server); + +bool +MatrixClientSetAccessToken( + MatrixClient * client, + const char * accessToken); bool MatrixClientLoginPassword( MatrixClient * client, - char * username, int usernameLen, - char * password, int passwordLen, - char * displayName, int displayNameLen); + const char * username, + const char * password, + const char * displayName); + +bool +MatrixClientSendEvent( + MatrixClient * client, + const char * roomId, + const char * msgType, + const char * msgBody); + +bool +MatrixClientSendEventEncrypted( + MatrixClient * client, + const char * roomId, + const char * msgType, + const char * msgBody); + +bool +MatrixClientSync( + MatrixClient * client, + char * outSyncBuffer, int outSyncCap); + +bool +MatrixClientShareMegolmOutSession( + MatrixClient * client, + const char * deviceId, + MatrixMegolmOutSession * session); + +bool +MatrixClientGetMegolmOutSession( + MatrixClient * client, + const char * roomId, + MatrixMegolmOutSession ** outSession); + +bool +MatrixClientSetMegolmOutSession( + MatrixClient * client, + const char * roomId, + MatrixMegolmOutSession session); + +bool +MatrixClientGetOlmSession( + MatrixClient * client, + const char * deviceId, + MatrixOlmSession ** outSession); + +bool +MatrixClientSendToDevice( + MatrixClient * client, + const char * userId, + const char * deviceId, + const char * message, + const char * msgType); + +bool +MatrixClientSendToDeviceEncrypted( + MatrixClient * client, + const char * userId, + const char * deviceId, + const char * message, + const char * msgType); + +bool +MatrixClientGetDeviceKey( + MatrixClient * client, + const char * deviceId, + char * outDeviceKey, int outDeviceKeyCap); + +bool +MatrixClientGetDeviceKey( + MatrixClient * client, + const char * deviceId, + char * outDeviceKey, int outDeviceKeyCap); + +bool +MatrixClientRequestDeviceKeys( + MatrixClient * client); + + bool MatrixHttpInit( @@ -54,13 +227,23 @@ bool MatrixHttpGet( MatrixClient * client, const char * url, - char * outResponseBuffer, int outResponseCap, int * outResponseLen); + char * outResponseBuffer, int outResponseCap, + bool authenticated); bool MatrixHttpPost( MatrixClient * client, const char * url, - char * requestBuffer, int requestLen, - char * outResponseBuffer, int outResponseCap, int * outResponseLen); + const char * requestBuffer, + char * outResponseBuffer, int outResponseCap, + bool authenticated); + +bool +MatrixHttpPut( + MatrixClient * client, + const char * url, + const char * requestBuffer, + char * outResponseBuffer, int outResponseCap, + bool authenticated); #endif