From f39bfedf9841ce6e4e767e9b8587c6c733766088 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 4 May 2023 17:34:33 +0200 Subject: [PATCH 1/7] use sha3-512 hash instead of cyrb53 to authenticate peerIds on reconnect --- index.js | 84 +++++++++++++++++++++++--------------------------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/index.js b/index.js index 6792a36..53f213a 100644 --- a/index.js +++ b/index.js @@ -136,7 +136,7 @@ class PairDropServer { displayName: peer.name.displayName, deviceName: peer.name.deviceName, peerId: peer.id, - peerIdHash: peer.id.hashCode128BitSalted() + peerIdHash: hasher.hashCodeSalted(peer.id) } }); } @@ -238,26 +238,8 @@ class PairDropServer { this._notifyPeers(sender); } - getRandomString(length) { - let string = ""; - while (string.length < length) { - let arr = new Uint16Array(length); - crypto.webcrypto.getRandomValues(arr); - arr = Array.apply([], arr); /* turn into non-typed array */ - arr = arr.map(function (r) { - return r % 128 - }) - arr = arr.filter(function (r) { - /* strip non-printables: if we transform into desirable range we have a propability bias, so I suppose we better skip this character */ - return r === 45 || r >= 47 && r <= 57 || r >= 64 && r <= 90 || r >= 97 && r <= 122; - }); - string += String.fromCharCode.apply(String, arr); - } - return string.substring(0, length) - } - _onPairDeviceInitiate(sender) { - let roomSecret = this.getRandomString(64); + let roomSecret = randomizer.getRandomString(64); let roomKey = this._createRoomKey(sender, roomSecret); if (sender.roomKey) this._removeRoomKey(sender.roomKey); sender.roomKey = roomKey; @@ -583,7 +565,7 @@ class Peer { separator: ' ', dictionaries: [colors, animals], style: 'capital', - seed: this.id.hashCode() + seed: cyrb53(this.id) }) this.name = { @@ -609,7 +591,7 @@ class Peer { } isPeerIdHashValid(peerId, peerIdHash) { - return peerIdHash === peerId.hashCode128BitSalted(); + return peerIdHash === hasher.hashCodeSalted(peerId); } addRoomSecret(roomSecret) { @@ -625,39 +607,43 @@ class Peer { } } -Object.defineProperty(String.prototype, 'hashCode', { - value: function() { - return cyrb53(this); - } -}); - -Object.defineProperty(String.prototype, 'hashCode128BitSalted', { - value: function() { - return hasher.hashCode128BitSalted(this); - } -}); - const hasher = (() => { - let seeds; + let password; return { - hashCode128BitSalted(str) { - if (!seeds) { - // seeds are created on first call to salt hash. - seeds = [4]; - for (let i=0; i<4; i++) { - const randomBuffer = new Uint32Array(1); - crypto.webcrypto.getRandomValues(randomBuffer); - seeds[i] = randomBuffer[0]; - } + hashCodeSalted(salt) { + if (!password) { + // password is created on first call. + password = randomizer.getRandomString(128); } - let hashCode = ""; - for (let i=0; i<4; i++) { - hashCode += cyrb53(str, seeds[i]); - } - return hashCode; + + return crypto.createHash("sha3-512") + .update(password) + .update(crypto.createHash("sha3-512").update(salt, "utf8").digest("hex")) + .digest("hex"); } } +})() +const randomizer = (() => { + return { + getRandomString(length) { + let string = ""; + while (string.length < length) { + let arr = new Uint16Array(length); + crypto.webcrypto.getRandomValues(arr); + arr = Array.apply([], arr); /* turn into non-typed array */ + arr = arr.map(function (r) { + return r % 128 + }) + arr = arr.filter(function (r) { + /* strip non-printables: if we transform into desirable range we have a probability bias, so I suppose we better skip this character */ + return r === 45 || r >= 47 && r <= 57 || r >= 64 && r <= 90 || r >= 97 && r <= 122; + }); + string += String.fromCharCode.apply(String, arr); + } + return string.substring(0, length) + } + } })() /* From 0ac3c5a11f920e9c8fff982321c8dc2dfb697f57 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 4 May 2023 17:39:12 +0200 Subject: [PATCH 2/7] remove debugging logs --- public_included_ws_fallback/scripts/network.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/public_included_ws_fallback/scripts/network.js b/public_included_ws_fallback/scripts/network.js index 78f6f5a..6575b18 100644 --- a/public_included_ws_fallback/scripts/network.js +++ b/public_included_ws_fallback/scripts/network.js @@ -734,7 +734,6 @@ class WSPeer extends Peer { } sendJSON(message) { - console.debug(message) message.to = this._peerId; message.roomType = this._roomType; message.roomSecret = this._roomSecret; @@ -854,7 +853,6 @@ class PeersManager { _onWsDisconnected() { for (const peerId in this.peers) { - console.debug(this.peers[peerId].rtcSupported); if (this.peers[peerId] && (!this.peers[peerId].rtcSupported || !window.isRtcSupported)) { Events.fire('peer-disconnected', peerId); } From 241ea4f98865c5a79c1a046dca3e98b0b774672b Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 4 May 2023 17:38:51 +0200 Subject: [PATCH 3/7] implement auto_accept (#91) and manual unpairing via new Edit Paired Devices Dialog and a BrowserTabsConnector --- index.js | 38 +- public/index.html | 26 +- public/scripts/network.js | 233 +++++++-- public/scripts/ui.js | 489 ++++++++++++----- public/scripts/util.js | 4 + public/styles.css | 163 ++++-- public_included_ws_fallback/index.html | 26 +- .../scripts/network.js | 255 ++++++--- public_included_ws_fallback/scripts/ui.js | 491 +++++++++++++----- public_included_ws_fallback/scripts/util.js | 8 +- public_included_ws_fallback/styles.css | 150 +++++- 11 files changed, 1442 insertions(+), 441 deletions(-) diff --git a/index.js b/index.js index 53f213a..bde7f98 100644 --- a/index.js +++ b/index.js @@ -159,11 +159,8 @@ class PairDropServer { case 'room-secrets': this._onRoomSecrets(sender, message); break; - case 'room-secret-deleted': - this._onRoomSecretDeleted(sender, message); - break; - case 'room-secrets-cleared': - this._onRoomSecretsCleared(sender, message); + case 'room-secrets-deleted': + this._onRoomSecretsDeleted(sender, message); break; case 'pair-device-initiate': this._onPairDeviceInitiate(sender); @@ -213,29 +210,26 @@ class PairDropServer { this._joinSecretRooms(sender, roomSecrets); } - _onRoomSecretDeleted(sender, message) { - this._deleteSecretRoom(sender, message.roomSecret) - } - - _onRoomSecretsCleared(sender, message) { + _onRoomSecretsDeleted(sender, message) { for (let i = 0; i - -