X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/a6eff84624ab1f3786d02aa2ec740b9a88090d94..HEAD:/src/matrix.h diff --git a/src/matrix.h b/src/matrix.h index 72e865f..226a341 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -11,16 +11,19 @@ #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 MASTER_KEY_SIZE 44 #define KEY_SHARE_EVENT_LEN 1024 @@ -29,8 +32,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 +48,59 @@ #define JSON_ONETIME_KEY_SIGNED_SIZE 256 #define JSON_SIGNATURE_SIZE 256 -#define NUM_MEGOLM_SESSIONS 10 -#define NUM_OLM_SESSIONS 10 +#define NUM_MEGOLM_SESSIONS 2 +#define NUM_OLM_SESSIONS 2 #define NUM_DEVICES 10 -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 +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 +115,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 +143,29 @@ typedef struct MatrixOlmSession { } MatrixOlmSession; bool -MatrixOlmSessionInit( +MatrixOlmSessionUnpickle( MatrixOlmSession * session, - const char * deviceId); + const char * deviceId, + void * pickled, int pickledLen, + const void * key, int keyLen); + +// create an olm sesseion from a type 0 message +bool +MatrixOlmSessionFrom( + MatrixOlmSession * session, + OlmAccount * olmAccount, + const char * deviceId, + const char * deviceKey, + const char * encrypted); + +// create a new olm session from a claimed onetime key +bool +MatrixOlmSessionTo( + MatrixOlmSession * session, + OlmAccount * olmAccount, + const char * deviceId, + const char * deviceKey, + const char * deviceOnetimeKey); bool MatrixOlmSessionEncrypt( @@ -101,28 +173,53 @@ 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, @@ -144,24 +241,22 @@ typedef struct MatrixClient { MatrixDevice devices[NUM_DEVICES]; int numDevices; - - 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]; + char masterKey[MASTER_KEY_SIZE]; - void * httpUserData; + bool verified; + + MatrixHttpConnection * hc; } MatrixClient; bool MatrixClientInit( - MatrixClient * client, - const char * server); + MatrixClient * client); bool MatrixClientSetAccessToken( @@ -191,6 +286,13 @@ bool MatrixClientUploadDeviceKeys( MatrixClient * client); +bool +MatrixClientClaimOnetimeKey( + MatrixClient * client, + const char * userId, + const char * deviceId, + char * outOnetimeKey, int outOnetimeKeyCap); + bool MatrixClientLoginPassword( MatrixClient * client, @@ -215,35 +317,87 @@ MatrixClientSendEventEncrypted( bool MatrixClientSync( MatrixClient * client, - char * outSyncBuffer, int outSyncCap); + char * outSyncBuffer, int outSyncCap, + char * nextBatch, int nextBatchCap); bool -MatrixClientShareMegolmOutSession( +MatrixClientGetRoomEvent( MatrixClient * client, - const char * deviceId, - MatrixMegolmOutSession * session); + const char * roomId, + const char * eventId, + char * outEvent, int outEventCap); bool -MatrixClientShareMegolmOutSessionTest( +MatrixClientShareMegolmOutSession( MatrixClient * client, + const char * userId, const char * deviceId, MatrixMegolmOutSession * session); +// try to lookup outgoing megolm session, return true if found bool MatrixClientGetMegolmOutSession( MatrixClient * client, const char * roomId, MatrixMegolmOutSession ** outSession); +// create a new outgoing megolm session and store it locally +bool +MatrixClientNewMegolmOutSession( + MatrixClient * client, + const char * roomId, + MatrixMegolmOutSession ** outSession); + +// try to lookup incoming megolm session, return true if found +bool +MatrixClientGetMegolmInSession( + MatrixClient * client, + const char * roomId, int roomIdLen, + const char * sessionId, int sessionIdLen, + MatrixMegolmInSession ** outSession); + +// create a new incoming megolm session and store it locally bool -MatrixClientSetMegolmOutSession( +MatrixClientNewMegolmInSession( MatrixClient * client, const char * roomId, - MatrixMegolmOutSession session); + const char * sessionId, + const char * sessionKey, + MatrixMegolmInSession ** outSession); + +// send a m.room_key_request to the device identified by userId/devideId +bool +MatrixClientRequestMegolmInSession( + MatrixClient * client, + const char * roomId, + const char * sessionId, + const char * senderKey, + const char * userId, + const char * deviceId); +// try to lookup olm session, return true if found bool MatrixClientGetOlmSession( MatrixClient * client, + const char * userId, + const char * deviceId, + MatrixOlmSession ** outSession); + +// create a new olm session from a type 0 message and store it locally +bool +MatrixClientNewOlmSessionIn( + MatrixClient * client, + const char * userId, + const char * deviceId, + const char * encrypted, + MatrixOlmSession ** outSession); + +// create a new olm session with device userId/deviceId and store it locally +// this automatically claims the onetime key +bool +MatrixClientNewOlmSessionOut( + MatrixClient * client, + const char * userId, const char * deviceId, MatrixOlmSession ** outSession); @@ -264,56 +418,62 @@ MatrixClientSendToDeviceEncrypted( const char * msgType); bool -MatrixClientGetDeviceKey( +MatrixClientSendDummy( MatrixClient * client, - const char * deviceId, - char * outDeviceKey, int outDeviceKeyCap); + const char * userId, + const char * deviceId); +// lookup device key locally and if not present get it from server bool -MatrixClientGetDeviceKey( +MatrixClientRequestDeviceKey( MatrixClient * client, const char * deviceId, char * outDeviceKey, int outDeviceKeyCap); - + +// lookup signing key locally and if not present get it from server bool -MatrixClientRequestDeviceKeys( - MatrixClient * client); - - +MatrixClientRequestSigningKey( + MatrixClient * client, + const char * deviceId, + char * outSigningKey, int outSigningKeyCap); +// lookup the master key for this user and if not present get it from server bool -MatrixHttpInit( - MatrixClient * client); +MatrixClientRequestMasterKey( + MatrixClient * client, + char * outMasterKey, int outMasterKeyCap); +// call keys/query and store retrieved information +// this is called by the other Request* functions bool -MatrixHttpConnect( +MatrixClientRequestDeviceKeys( MatrixClient * client); +// delete this device on the server bool -MatrixHttpDeinit( +MatrixClientDeleteDevice( MatrixClient * client); -bool -MatrixHttpGet( - MatrixClient * client, - const char * url, - char * outResponseBuffer, int outResponseCap, - bool authenticated); -bool -MatrixHttpPost( - MatrixClient * client, - const char * url, - const char * requestBuffer, - char * outResponseBuffer, int outResponseCap, - bool authenticated); +// util + +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