Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.1.3] Bug Fixes and Touchups #170

Merged
merged 19 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
encryption working between element and syphon again (and all other e2…
…e clients)
  • Loading branch information
ereio committed Sep 14, 2020
commit 8e77deca61a4bad60e39f4b21da2acfa57166c8a
1 change: 1 addition & 0 deletions assets/translations/hi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
20 changes: 11 additions & 9 deletions lib/global/algos.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Dart imports:
import 'dart:convert';

import 'package:flutter/material.dart';

/**
* Clock functions in code
*
* final stopwatch = Stopwatch()..start();
* print('[fetchRooms] TIMESTAMP ${stopwatch.elapsed}');
* stopwatch.stop();
*/

List<int> fibonacci(int n) {
if (n == 0) {
return [0];
Expand All @@ -16,13 +26,5 @@ List<int> fibonacci(int n) {
void printJson(Map jsonMap) {
JsonEncoder encoder = new JsonEncoder.withIndent(' ');
String prettyEvent = encoder.convert(jsonMap);
print(prettyEvent);
debugPrint(prettyEvent, wrapWidth: 2048);
}

/**
* Clock functions in code
*
* final stopwatch = Stopwatch()..start();
* print('[fetchRooms] TIMESTAMP ${stopwatch.elapsed}');
* stopwatch.stop();
*/
7 changes: 2 additions & 5 deletions lib/global/libs/matrix/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:convert';

// Package imports:
import 'package:http/http.dart' as http;
import 'package:syphon/global/algos.dart';

// Project imports:
import 'package:syphon/global/libs/matrix/encryption.dart';
Expand Down Expand Up @@ -245,11 +246,7 @@ abstract class Events {

// Use astrick to send to all known devices for user
Map body = {
"messages": {
'$userId': {
'$deviceId': content,
},
}
"messages": content,
};

final response = await http.put(
Expand Down
111 changes: 74 additions & 37 deletions lib/store/crypto/actions.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
*
*
*
* TODO: not sure if we ever need unsigned keys
*/

Expand Down Expand Up @@ -102,7 +100,22 @@ class AddInboundKeySession {
class AddOutboundMessageSession {
String roomId;
String session;
AddOutboundMessageSession({this.roomId, this.session});
AddOutboundMessageSession({
this.roomId,
this.session,
});
}

class UpdateMessageSessionOutbound {
String roomId;
String session;
int messageIndex;

UpdateMessageSessionOutbound({
this.roomId,
this.session,
this.messageIndex,
});
}

class AddInboundMessageSession {
Expand Down Expand Up @@ -273,9 +286,7 @@ ThunkAction<AppState> generateIdentityKeys() {
final authUser = store.state.authStore.user;
final olmAccount = store.state.cryptoStore.olmAccount;

final identityKeysString = olmAccount.identity_keys();

final identityKeys = await json.decode(identityKeysString);
final identityKeys = await json.decode(olmAccount.identity_keys());
// fingerprint keypair - ed25519
final fingerprintKeyName = '${Algorithms.ed25519}:${authUser.deviceId}';

Expand Down Expand Up @@ -336,9 +347,6 @@ ThunkAction<AppState> uploadIdentityKeys({DeviceKey deviceKey}) {
'device_keys': deviceKey.toMap(),
};

debugPrint('[uploadIdentityKeys] uploaded keys json object below');
printJson(deviceKeyMap);

// upload the public device keys
final data = await MatrixApi.uploadKeys(
protocol: protocol,
Expand Down Expand Up @@ -536,32 +544,36 @@ ThunkAction<AppState> updateKeySessions({
// find the identityKey for the device
final deviceKey = store.state.cryptoStore
.deviceKeys[oneTimeKey.userId][oneTimeKey.deviceId];
final keyId = Keys.identity(deviceId: deviceKey.deviceId);
final identityKey = deviceKey.keys[keyId];

// Poorly decided to save key sessions by deviceId at first but then
// realised that you may have the same identityKey for diff
// devices and you also don't have the device id in the
// toDevice event payload -__-, convert back to identity key
final roomKeyEventContentEncrypted = await store.dispatch(
final roomKeyEventEncrypted = await store.dispatch(
encryptKeyContent(
roomId: room.id,
identityKey: identityKey,
recipient: deviceKey.userId,
recipientKeys: deviceKey,
eventType: EventTypes.roomKey,
content: roomKeyEventContent,
),
);

// format payload for toDevice events
final payload = {
'${deviceKey.userId}': {
'${deviceKey.deviceId}': roomKeyEventEncrypted,
},
};

final randomNumber = Random.secure().nextInt(1 << 31).toString();
final response = await MatrixApi.sendEventToDevice(
trxId: randomNumber,
protocol: protocol,
accessToken: store.state.authStore.user.accessToken,
homeserver: store.state.authStore.user.homeserver,
userId: deviceKey.userId,
deviceId: deviceKey.deviceId,
eventType: EventTypes.encrypted,
content: roomKeyEventContentEncrypted,
trxId: randomNumber,
content: payload,
);

if (response['errcode'] != null) {
Expand Down Expand Up @@ -684,7 +696,7 @@ ThunkAction<AppState> claimOneTimeKeys({
final keyId = Keys.identity(deviceId: deviceKey.deviceId);
final identityKey = deviceKey.keys[keyId];

store.dispatch(createOutboundKeySession(
store.dispatch(createKeySessionOutbound(
identityKey: identityKey,
oneTimeKey: oneTimeKey.keys.values.elementAt(0),
));
Expand Down Expand Up @@ -713,7 +725,7 @@ ThunkAction<AppState> claimOneTimeKeys({
* https://matrix.org/docs/spec/client_server/r0.4.0#m-olm-v1-curve25519-aes-sha2
*
*/
ThunkAction<AppState> createOutboundKeySession({
ThunkAction<AppState> createKeySessionOutbound({
String oneTimeKey,
String identityKey,
}) {
Expand All @@ -728,7 +740,7 @@ ThunkAction<AppState> createOutboundKeySession({
final serializedKeySession = outboundKeySession.pickle(identityKey);

// sychronous
store.dispatch(saveOutboundKeySession(
store.dispatch(saveKeySessionOutbound(
identityKey: identityKey,
session: serializedKeySession,
));
Expand All @@ -738,7 +750,7 @@ ThunkAction<AppState> createOutboundKeySession({
};
}

ThunkAction<AppState> saveOutboundKeySession({
ThunkAction<AppState> saveKeySessionOutbound({
String identityKey,
String session,
}) {
Expand All @@ -750,9 +762,9 @@ ThunkAction<AppState> saveOutboundKeySession({
};
}

ThunkAction<AppState> saveInboundKeySession({
String identityKey,
ThunkAction<AppState> saveKeySessionInbound({
String session,
String identityKey,
}) {
return (Store<AppState> store) {
store.dispatch(AddInboundKeySession(
Expand All @@ -762,7 +774,7 @@ ThunkAction<AppState> saveInboundKeySession({
};
}

ThunkAction<AppState> loadOutboundKeySession({
ThunkAction<AppState> loadKeySessionOutbound({
String identityKey, // sender_key
}) {
return (Store<AppState> store) async {
Expand All @@ -781,7 +793,7 @@ ThunkAction<AppState> loadOutboundKeySession({
return session;
}
} catch (error) {
debugPrint('[loadOutboundKeySession] $error');
debugPrint('[loadKeySessionOutbound] $error');
return null;
}
};
Expand All @@ -791,7 +803,7 @@ ThunkAction<AppState> loadOutboundKeySession({
*
* https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#molmv1curve25519-aes-sha2
*/
ThunkAction<AppState> loadInboundKeySession({
ThunkAction<AppState> loadKeySessionInbound({
int type,
String body,
String identityKey, // sender_key
Expand All @@ -814,7 +826,7 @@ ThunkAction<AppState> loadInboundKeySession({
}
}
} catch (error) {
debugPrint('[loadInboundKeySession] $error');
debugPrint('[loadKeySessionInbound] $error');
}

try {
Expand All @@ -830,7 +842,7 @@ ThunkAction<AppState> loadInboundKeySession({

// Save sessions as needed
await store.dispatch(saveOlmAccount());
await store.dispatch(saveInboundKeySession(
await store.dispatch(saveKeySessionInbound(
session: newKeySession.pickle(identityKey),
identityKey: identityKey,
));
Expand All @@ -839,7 +851,7 @@ ThunkAction<AppState> loadInboundKeySession({
return newKeySession;
}
} catch (error) {
debugPrint('[loadInboundKeySession] $error');
debugPrint('[loadKeySessionInbound] $error');
}

return null;
Expand All @@ -851,7 +863,7 @@ ThunkAction<AppState> loadInboundKeySession({
*
* https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session
*/
ThunkAction<AppState> createInboundMessageSession({
ThunkAction<AppState> createMessageSessionInbound({
String roomId,
String identityKey,
String sessionKey,
Expand All @@ -871,7 +883,7 @@ ThunkAction<AppState> createInboundMessageSession({
};
}

ThunkAction<AppState> loadMessageSession({
ThunkAction<AppState> loadMessageSessionInbound({
String roomId,
String identityKey,
}) {
Expand All @@ -889,13 +901,35 @@ ThunkAction<AppState> loadMessageSession({
};
}

/**
*
* Save Message Session Inbound
*
* Saves the message session and index after encrypting and sending an event
*/
ThunkAction<AppState> saveMessageSessionInbound({
String roomId,
String identityKey,
olm.InboundGroupSession session,
int messageIndex,
}) {
return (Store<AppState> store) async {
return await store.dispatch(AddInboundMessageSession(
roomId: roomId,
identityKey: identityKey,
session: session.pickle(roomId),
messageIndex: messageIndex,
));
};
}

/**
*
* Outbound Message Session Functionality
*
* https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#starting-a-megolm-session
*/
ThunkAction<AppState> createOutboundMessageSession({String roomId}) {
ThunkAction<AppState> createMessageSessionOutbound({String roomId}) {
return (Store<AppState> store) async {
// Get current user device identity key
final deviceId = store.state.authStore.user.deviceId;
Expand Down Expand Up @@ -937,15 +971,15 @@ ThunkAction<AppState> createOutboundMessageSession({String roomId}) {
* one would likely need more based on identity + device,
* assuming you've imported keys but lets keep it simple for alpha
*/
ThunkAction<AppState> loadOutboundMessageSession({String roomId}) {
ThunkAction<AppState> loadMessageSessionOutbound({String roomId}) {
return (Store<AppState> store) async {
// Load session for identity
var outboundMessageSessionSerialized =
store.state.cryptoStore.outboundMessageSessions[roomId];

if (outboundMessageSessionSerialized == null) {
outboundMessageSessionSerialized = await store.dispatch(
createOutboundMessageSession(roomId: roomId),
createMessageSessionOutbound(roomId: roomId),
);
}

Expand All @@ -955,13 +989,16 @@ ThunkAction<AppState> loadOutboundMessageSession({String roomId}) {
};
}

ThunkAction<AppState> saveOutboundMessageSession({
ThunkAction<AppState> saveMessageSessionOutbound({
String roomId,
String session,
}) {
return (Store<AppState> store) async {
store.dispatch(
AddOutboundMessageSession(roomId: roomId, session: session),
AddOutboundMessageSession(
roomId: roomId,
session: session,
),
);
};
}
Expand All @@ -970,7 +1007,7 @@ ThunkAction<AppState> exportMessageSession({String roomId}) {
return (Store<AppState> store) async {
final olm.OutboundGroupSession outboundMessageSession =
await store.dispatch(
loadOutboundMessageSession(roomId: roomId),
loadMessageSessionOutbound(roomId: roomId),
);

return {
Expand Down
Loading