-
Notifications
You must be signed in to change notification settings - Fork 29
/
peer-to-peer-message-handler.js
187 lines (179 loc) · 6.38 KB
/
peer-to-peer-message-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// @flow
import _isEqual from 'lodash/fp/isEqual.js';
import { verifyAndGetDeviceList } from '../shared/device-list-utils.js';
import type {
IdentityServiceClient,
DeviceOlmInboundKeys,
} from '../types/identity-service-types.js';
import {
peerToPeerMessageTypes,
type PeerToPeerMessage,
} from '../types/tunnelbroker/peer-to-peer-message-types.js';
import { getConfig } from '../utils/config.js';
import { getContentSigningKey } from '../utils/crypto-utils.js';
import { getMessageForException } from '../utils/errors.js';
import { hasHigherDeviceID, olmSessionErrors } from '../utils/olm-utils.js';
import { getClientMessageIDFromTunnelbrokerMessageID } from '../utils/peer-to-peer-communication-utils.js';
async function peerToPeerMessageHandler(
message: PeerToPeerMessage,
identityClient: IdentityServiceClient,
messageID: string,
): Promise<void> {
const { olmAPI, sqliteAPI } = getConfig();
if (message.type === peerToPeerMessageTypes.OUTBOUND_SESSION_CREATION) {
const { senderInfo, encryptedData, sessionVersion } = message;
const { userID: senderUserID, deviceID: senderDeviceID } = senderInfo;
let deviceKeys: ?DeviceOlmInboundKeys = null;
try {
const { keys } = await identityClient.getInboundKeysForUser(senderUserID);
deviceKeys = keys[senderDeviceID];
} catch (e) {
console.log(e.message);
}
if (!deviceKeys) {
console.log(
'Error creating inbound session with device ' +
`${senderDeviceID}: No keys for the device, ` +
`session version: ${sessionVersion}`,
);
return;
}
try {
await olmAPI.initializeCryptoAccount();
const result = await olmAPI.contentInboundSessionCreator(
deviceKeys.identityKeysBlob.primaryIdentityPublicKeys,
encryptedData,
sessionVersion,
false,
);
console.log(
'Created inbound session with device ' +
`${senderDeviceID}: ${result}, ` +
`session version: ${sessionVersion}`,
);
} catch (e) {
if (e.message?.includes(olmSessionErrors.alreadyCreated)) {
console.log(
'Received session request with lower session version from ' +
`${senderDeviceID}, session version: ${sessionVersion}`,
);
} else if (e.message?.includes(olmSessionErrors.raceCondition)) {
const currentDeviceID = await getContentSigningKey();
if (hasHigherDeviceID(currentDeviceID, senderDeviceID)) {
console.log(
'Race condition while creating session with ' +
`${senderDeviceID}, session version: ${sessionVersion}, ` +
`this device has a higher deviceID and the session will be kept`,
);
} else {
const result = await olmAPI.contentInboundSessionCreator(
deviceKeys.identityKeysBlob.primaryIdentityPublicKeys,
encryptedData,
sessionVersion,
true,
);
console.log(
'Overwrite session with device ' +
`${senderDeviceID}: ${result}, ` +
`session version: ${sessionVersion}`,
);
// Resend all not-yet confirmed messages that were encrypted
// with overwrite session. Tracked in ENG-6982.
}
} else {
console.log(
'Error creating inbound session with device ' +
`${senderDeviceID}: ${e.message}, ` +
`session version: ${sessionVersion}`,
);
}
}
} else if (message.type === peerToPeerMessageTypes.ENCRYPTED_MESSAGE) {
try {
await olmAPI.initializeCryptoAccount();
const decrypted = await olmAPI.decryptSequentialAndPersist(
message.encryptedData,
message.senderInfo.deviceID,
messageID,
);
console.log(
'Decrypted message from device ' +
`${message.senderInfo.deviceID}: ${decrypted}`,
);
} catch (e) {
if (e.message?.includes(olmSessionErrors.messageAlreadyDecrypted)) {
console.log(
'Received already decrypted message from device ' +
`${message.senderInfo.deviceID}.`,
);
} else if (e.message?.includes(olmSessionErrors.messageOutOfOrder)) {
console.log(
'Received out-of-order message from device ' +
`${message.senderInfo.deviceID}.`,
);
} else {
console.log(
'Error decrypting message from device ' +
`${message.senderInfo.deviceID}: ${e.message}`,
);
}
}
} else if (message.type === peerToPeerMessageTypes.REFRESH_KEY_REQUEST) {
try {
await olmAPI.initializeCryptoAccount();
const oneTimeKeys = await olmAPI.getOneTimeKeys(message.numberOfKeys);
await identityClient.uploadOneTimeKeys(oneTimeKeys);
} catch (e) {
console.log(`Error uploading one-time keys: ${e.message}`);
}
} else if (message.type === peerToPeerMessageTypes.DEVICE_LIST_UPDATED) {
try {
const result = await verifyAndGetDeviceList(
identityClient,
message.userID,
null,
);
if (result.valid) {
console.log(
`Received valid device list update for user ${message.userID}`,
);
const receivedRawList = JSON.parse(
message.signedDeviceList.rawDeviceList,
);
// additional check for broadcasted and Identity device list equality
const listsAreEqual = _isEqual(result.deviceList)(receivedRawList);
console.log(
`Identity and received device lists are ${
listsAreEqual ? '' : 'not'
} equal.`,
);
} else {
console.log(
`Received invalid device list update for user ${message.userID}. Reason: ${result.reason}`,
);
}
} catch (e) {
console.log(
`Error verifying device list for user ${message.userID}: ${e}`,
);
}
} else if (message.type === peerToPeerMessageTypes.MESSAGE_PROCESSED) {
try {
const { deviceID, messageID: tunnelbrokerMessageID } = message;
const clientMessageID = getClientMessageIDFromTunnelbrokerMessageID(
tunnelbrokerMessageID,
);
await sqliteAPI.removeOutboundP2PMessagesOlderThan(
clientMessageID,
deviceID,
);
} catch (e) {
console.log(
`Error removing message after processing: ${
getMessageForException(e) ?? 'unknown error'
}`,
);
}
}
}
export { peerToPeerMessageHandler };