X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/fb5f3a8652d1cd3e4d8207f8718359f90636b5b3..b231ec39ba5b9df6288a50204e5dabe9f591c830:/src/matrix.h diff --git a/src/matrix.h b/src/matrix.h index 38fb767..c07c742 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -11,16 +11,18 @@ #define USER_ID_SIZE 64 +#define ROOM_ID_SIZE 128 #define SERVER_SIZE 20 #define ACCESS_TOKEN_SIZE 40 #define DEVICE_ID_SIZE 20 #define EXPIRE_MS_SIZE 20 #define REFRESH_TOKEN_SIZE 20 -#define MAX_URL_LEN 128 +#define MAX_URL_LEN 1024 #define OLM_IDENTITY_KEYS_JSON_SIZE 128 #define DEVICE_KEY_SIZE 44 #define SIGNING_KEY_SIZE 44 +#define ONETIME_KEY_SIZE 44 #define KEY_SHARE_EVENT_LEN 1024 @@ -29,8 +31,9 @@ #define OLM_SESSION_MEMORY_SIZE 3352 #define OLM_ENCRYPT_RANDOM_SIZE 32 +#define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2) -#define OLM_ONETIME_KEYS_RANDOM_SIZE 32*10 +#define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10) #define OLM_KEY_ID_SIZE 32 #define OLM_SIGNATURE_SIZE 128 @@ -44,27 +47,63 @@ #define JSON_ONETIME_KEY_SIGNED_SIZE 256 #define JSON_SIGNATURE_SIZE 256 -#define NUM_MEGOLM_SESSIONS 10 -#define NUM_OLM_SESSIONS 10 -#define NUM_DEVICES 10 +#define NUM_MEGOLM_SESSIONS 2 +#define NUM_OLM_SESSIONS 2 +#define NUM_DEVICES 5 -void -Randomize(uint8_t * random, int randomLen); +// HTTP + +typedef struct MatrixHttpConnection MatrixHttpConnection; bool -JsonEscape( - char * sIn, int sInLen, - char * sOut, int sOutCap); +MatrixHttpInit( + MatrixHttpConnection ** hc, + const char * host); + +// bool +// MatrixHttpConnect( +// MatrixHttpConnection * hc); + +bool +MatrixHttpDeinit( + MatrixHttpConnection ** hc); -bool JsonSign( - char * sIn, int sInLen, - char * sOut, int sOutCap); +bool +MatrixHttpSetAccessToken( + MatrixHttpConnection * hc, + const char * accessToken); + +bool +MatrixHttpGet( + MatrixHttpConnection * hc, + const char * url, + char * outResponseBuffer, int outResponseCap, + bool authenticated); + +bool +MatrixHttpPost( + MatrixHttpConnection * hc, + const char * url, + const char * requestBuffer, + char * outResponseBuffer, int outResponseCap, + bool authenticated); + +bool +MatrixHttpPut( + MatrixHttpConnection * hc, + const char * url, + const char * requestBuffer, + char * outResponseBuffer, int outResponseCap, + bool authenticated); + + // Matrix Device typedef struct MatrixDevice { char deviceId[DEVICE_ID_SIZE]; char deviceKey[DEVICE_KEY_SIZE]; + char signingKey[SIGNING_KEY_SIZE]; } MatrixDevice; @@ -79,11 +118,27 @@ bool MatrixOlmAccountInit( MatrixOlmAccount * account); +bool +MatrixOlmAccountUnpickle( + MatrixOlmAccount * account, + void * pickled, int pickledLen, + const void * key, int keyLen); + +bool +MatrixOlmAccountGetDeviceKey( + MatrixOlmAccount * account, + char * key, int keyCap); + +bool +MatrixOlmAccountGetSigningKey( + MatrixOlmAccount * account, + char * key, int keyCap); + // Matrix Olm Session typedef struct MatrixOlmSession { - const char * deviceId; + const char * deviceId; // TODO: char[] int type; OlmSession * session; @@ -91,9 +146,27 @@ typedef struct MatrixOlmSession { } MatrixOlmSession; bool -MatrixOlmSessionInit( +MatrixOlmSessionUnpickle( MatrixOlmSession * session, - const char * deviceId); + const char * deviceId, + void * pickled, int pickledLen, + const void * key, int keyLen); + +bool +MatrixOlmSessionFrom( + MatrixOlmSession * session, + OlmAccount * olmAccount, + const char * deviceId, + const char * deviceKey, + const char * encrypted); + +bool +MatrixOlmSessionTo( + MatrixOlmSession * session, + OlmAccount * olmAccount, + const char * deviceId, + const char * deviceKey, + const char * deviceOnetimeKey); bool MatrixOlmSessionEncrypt( @@ -101,34 +174,71 @@ MatrixOlmSessionEncrypt( const char * plaintext, char * outBuffer, int outBufferCap); +bool +MatrixOlmSessionDecrypt( + MatrixOlmSession * session, + size_t messageType, + char * encrypted, + char * outBuffer, int outBufferCap); + // Matrix Megolm Session typedef struct MatrixMegolmInSession { + char roomId[ROOM_ID_SIZE]; + char id[MEGOLM_SESSION_ID_SIZE]; + char key[MEGOLM_SESSION_KEY_SIZE]; + OlmInboundGroupSession * session; + char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; + } MatrixMegolmInSession; -typedef struct MatrixMegolmOutSession { - const char * roomId; +bool +MatrixMegolmInSessionInit( + MatrixMegolmInSession * session, + const char * roomId, + const char * sessionId, + const char * sessionKey, int sessionKeyLen); - OlmOutboundGroupSession * session; - char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; +bool +MatrixMegolmInSessionDecrypt( + MatrixMegolmInSession * session, + const char * encrypted, int encryptedLen, + char * outDecrypted, int outDecryptedCap); +typedef struct MatrixMegolmOutSession { + char roomId[ROOM_ID_SIZE]; char id[MEGOLM_SESSION_ID_SIZE]; char key[MEGOLM_SESSION_KEY_SIZE]; + + OlmOutboundGroupSession * session; + char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; } MatrixMegolmOutSession; bool MatrixMegolmOutSessionInit( MatrixMegolmOutSession * session, const char * roomId); - + bool MatrixMegolmOutSessionEncrypt( MatrixMegolmOutSession * session, const char * plaintext, char * outBuffer, int outBufferCap); +bool +MatrixMegolmOutSessionSave( + MatrixMegolmOutSession * session, + const char * filename, + const char * key); + +bool +MatrixMegolmOutSessionLoad( + MatrixMegolmOutSession * session, + const char * filename, + const char * key); + // Matrix Client @@ -145,23 +255,31 @@ typedef struct MatrixClient { MatrixDevice devices[NUM_DEVICES]; int numDevices; - char deviceKey[DEVICE_KEY_SIZE]; - char signingKey[DEVICE_KEY_SIZE]; + // char deviceKey[DEVICE_KEY_SIZE]; + // char signingKey[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; + MatrixHttpConnection * hc; } MatrixClient; bool MatrixClientInit( + MatrixClient * client); + +bool +MatrixClientSave( MatrixClient * client, - const char * server); + const char * filename); + +bool +MatrixClientLoad( + MatrixClient * client, + const char * filename); bool MatrixClientSetAccessToken( @@ -188,9 +306,16 @@ MatrixClientUploadOnetimeKeys( MatrixClient * client); bool -MatrixClientUploadDeviceKeys( +MatrixClientUploadDeviceKey( MatrixClient * client); +bool +MatrixClientClaimOnetimeKey( + MatrixClient * client, + const char * userId, + const char * deviceId, + char * outOnetimeKey, int outOnetimeKeyCap); + bool MatrixClientLoginPassword( MatrixClient * client, @@ -215,17 +340,27 @@ MatrixClientSendEventEncrypted( bool MatrixClientSync( MatrixClient * client, - char * outSyncBuffer, int outSyncCap); + char * outSync, int outSyncCap, + const char * nextBatch); + +bool +MatrixClientGetRoomEvent( + MatrixClient * client, + const char * roomId, + const char * eventId, + char * outEvent, int outEventCap); bool MatrixClientShareMegolmOutSession( MatrixClient * client, + const char * userId, const char * deviceId, MatrixMegolmOutSession * session); bool MatrixClientShareMegolmOutSessionTest( MatrixClient * client, + const char * userId, const char * deviceId, MatrixMegolmOutSession * session); @@ -236,14 +371,54 @@ MatrixClientGetMegolmOutSession( MatrixMegolmOutSession ** outSession); bool -MatrixClientSetMegolmOutSession( +MatrixClientNewMegolmOutSession( MatrixClient * client, const char * roomId, - MatrixMegolmOutSession session); + MatrixMegolmOutSession ** outSession); + +bool +MatrixClientGetMegolmInSession( + MatrixClient * client, + const char * roomId, int roomIdLen, + const char * sessionId, int sessionIdLen, + MatrixMegolmInSession ** outSession); + +bool +MatrixClientNewMegolmInSession( + MatrixClient * client, + const char * roomId, + const char * sessionId, + const char * sessionKey, + MatrixMegolmInSession ** outSession); + +bool +MatrixClientRequestMegolmInSession( + MatrixClient * client, + const char * roomId, + const char * sessionId, + const char * senderKey, + const char * userId, + const char * deviceId); // TODO: remove deviceId (query all devices) bool MatrixClientGetOlmSession( MatrixClient * client, + const char * userId, + const char * deviceId, + MatrixOlmSession ** outSession); + +bool +MatrixClientNewOlmSessionIn( + MatrixClient * client, + const char * userId, + const char * deviceId, + const char * encrypted, + MatrixOlmSession ** outSession); + +bool +MatrixClientNewOlmSessionOut( + MatrixClient * client, + const char * userId, const char * deviceId, MatrixOlmSession ** outSession); @@ -264,52 +439,51 @@ MatrixClientSendToDeviceEncrypted( const char * msgType); bool -MatrixClientGetDeviceKey( +MatrixClientSendDummy( MatrixClient * client, - const char * deviceId, - char * outDeviceKey, int outDeviceKeyCap); + const char * userId, + const char * deviceId); bool -MatrixClientGetDeviceKey( +MatrixClientRequestDeviceKey( MatrixClient * client, const char * deviceId, char * outDeviceKey, int outDeviceKeyCap); + +bool +MatrixClientRequestSigningKey( + MatrixClient * client, + const char * deviceId, + char * outSigningKey, int outSigningKeyCap); bool MatrixClientRequestDeviceKeys( MatrixClient * client); - - bool -MatrixHttpInit( +MatrixClientDeleteDevice( MatrixClient * client); -bool -MatrixHttpDeinit( - MatrixClient * client); -bool -MatrixHttpGet( - MatrixClient * client, - const char * url, - char * outResponseBuffer, int outResponseCap, - bool authenticated); +// util -bool -MatrixHttpPost( - MatrixClient * client, - const char * url, - const char * requestBuffer, - char * outResponseBuffer, int outResponseCap, - bool authenticated); +void +Randomize(uint8_t * random, int randomLen); bool -MatrixHttpPut( +JsonEscape( + const char * sIn, int sInLen, + char * sOut, int sOutCap); + +bool +JsonCanonicalize( + const char * sIn, int sInLen, + char * sOut, int sOutCap); + +bool +JsonSign( MatrixClient * client, - const char * url, - const char * requestBuffer, - char * outResponseBuffer, int outResponseCap, - bool authenticated); + const char * sIn, int sInLen, + char * sOut, int sOutCap); #endif