@novnc/novnc 1.3.0-g679b45f → 1.3.0-g69e0f0f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/core/decoders/raw.js +1 -1
- package/core/display.js +12 -0
- package/core/ra2.js +567 -0
- package/core/rfb.js +327 -73
- package/core/util/md5.js +79 -0
- package/docs/API.md +97 -3
- package/lib/decoders/raw.js +1 -1
- package/lib/decoders/tightpng.js +3 -3
- package/lib/display.js +15 -0
- package/lib/ra2.js +1259 -0
- package/lib/rfb.js +457 -91
- package/lib/util/md5.js +103 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -128,13 +128,17 @@ Running the command below will install the latest release of noVNC from Snap:
|
|
|
128
128
|
|
|
129
129
|
`sudo snap install novnc`
|
|
130
130
|
|
|
131
|
-
#### Running noVNC
|
|
131
|
+
#### Running noVNC from Snap Directly
|
|
132
132
|
|
|
133
133
|
You can run the Snap-package installed novnc directly with, for example:
|
|
134
134
|
|
|
135
135
|
`novnc --listen 6081 --vnc localhost:5901 # /snap/bin/novnc if /snap/bin is not in your PATH`
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
If you want to use certificate files, due to standard Snap confinement restrictions you need to have them in the /home/\<user\>/snap/novnc/current/ directory. If your username is jsmith an example command would be:
|
|
138
|
+
|
|
139
|
+
`novnc --listen 8443 --cert ~jsmith/snap/novnc/current/self.crt --key ~jsmith/snap/novnc/current/self.key --vnc ubuntu.example.com:5901`
|
|
140
|
+
|
|
141
|
+
#### Running noVNC from Snap as a Service (Daemon)
|
|
138
142
|
The Snap package also has the capability to run a 'novnc' service which can be
|
|
139
143
|
configured to listen on multiple ports connecting to multiple VNC servers
|
|
140
144
|
(effectively a service runing multiple instances of novnc).
|
|
@@ -208,6 +212,7 @@ that list and you think you should be, feel free to send a PR to fix that.
|
|
|
208
212
|
* UI and Icons : Pierre Ossman, Chris Gordon
|
|
209
213
|
* Original Logo : Michael Sersen
|
|
210
214
|
* tight encoding : Michael Tinglof (Mercuri.ca)
|
|
215
|
+
* RealVNC RSA AES authentication : USTC Vlab Team
|
|
211
216
|
|
|
212
217
|
* Included libraries:
|
|
213
218
|
* base64 : Martijn Pieters (Digital Creations 2), Samuel Sieb (sieb.net)
|
package/core/decoders/raw.js
CHANGED
package/core/display.js
CHANGED
|
@@ -224,6 +224,18 @@ export default class Display {
|
|
|
224
224
|
this.viewportChangePos(0, 0);
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
+
getImageData() {
|
|
228
|
+
return this._drawCtx.getImageData(0, 0, this.width, this.height);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
toDataURL(type, encoderOptions) {
|
|
232
|
+
return this._backbuffer.toDataURL(type, encoderOptions);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
toBlob(callback, type, quality) {
|
|
236
|
+
return this._backbuffer.toBlob(callback, type, quality);
|
|
237
|
+
}
|
|
238
|
+
|
|
227
239
|
// Track what parts of the visible canvas that need updating
|
|
228
240
|
_damage(x, y, w, h) {
|
|
229
241
|
if (x < this._damageBounds.left) {
|
package/core/ra2.js
ADDED
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
import Base64 from './base64.js';
|
|
2
|
+
import { encodeUTF8 } from './util/strings.js';
|
|
3
|
+
import EventTargetMixin from './util/eventtarget.js';
|
|
4
|
+
|
|
5
|
+
export class AESEAXCipher {
|
|
6
|
+
constructor() {
|
|
7
|
+
this._rawKey = null;
|
|
8
|
+
this._ctrKey = null;
|
|
9
|
+
this._cbcKey = null;
|
|
10
|
+
this._zeroBlock = new Uint8Array(16);
|
|
11
|
+
this._prefixBlock0 = this._zeroBlock;
|
|
12
|
+
this._prefixBlock1 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
|
|
13
|
+
this._prefixBlock2 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async _encryptBlock(block) {
|
|
17
|
+
const encrypted = await window.crypto.subtle.encrypt({
|
|
18
|
+
name: "AES-CBC",
|
|
19
|
+
iv: this._zeroBlock,
|
|
20
|
+
}, this._cbcKey, block);
|
|
21
|
+
return new Uint8Array(encrypted).slice(0, 16);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async _initCMAC() {
|
|
25
|
+
const k1 = await this._encryptBlock(this._zeroBlock);
|
|
26
|
+
const k2 = new Uint8Array(16);
|
|
27
|
+
const v = k1[0] >>> 6;
|
|
28
|
+
for (let i = 0; i < 15; i++) {
|
|
29
|
+
k2[i] = (k1[i + 1] >> 6) | (k1[i] << 2);
|
|
30
|
+
k1[i] = (k1[i + 1] >> 7) | (k1[i] << 1);
|
|
31
|
+
}
|
|
32
|
+
const lut = [0x0, 0x87, 0x0e, 0x89];
|
|
33
|
+
k2[14] ^= v >>> 1;
|
|
34
|
+
k2[15] = (k1[15] << 2) ^ lut[v];
|
|
35
|
+
k1[15] = (k1[15] << 1) ^ lut[v >> 1];
|
|
36
|
+
this._k1 = k1;
|
|
37
|
+
this._k2 = k2;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async _encryptCTR(data, counter) {
|
|
41
|
+
const encrypted = await window.crypto.subtle.encrypt({
|
|
42
|
+
"name": "AES-CTR",
|
|
43
|
+
counter: counter,
|
|
44
|
+
length: 128
|
|
45
|
+
}, this._ctrKey, data);
|
|
46
|
+
return new Uint8Array(encrypted);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async _decryptCTR(data, counter) {
|
|
50
|
+
const decrypted = await window.crypto.subtle.decrypt({
|
|
51
|
+
"name": "AES-CTR",
|
|
52
|
+
counter: counter,
|
|
53
|
+
length: 128
|
|
54
|
+
}, this._ctrKey, data);
|
|
55
|
+
return new Uint8Array(decrypted);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async _computeCMAC(data, prefixBlock) {
|
|
59
|
+
if (prefixBlock.length !== 16) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const n = Math.floor(data.length / 16);
|
|
63
|
+
const m = Math.ceil(data.length / 16);
|
|
64
|
+
const r = data.length - n * 16;
|
|
65
|
+
const cbcData = new Uint8Array((m + 1) * 16);
|
|
66
|
+
cbcData.set(prefixBlock);
|
|
67
|
+
cbcData.set(data, 16);
|
|
68
|
+
if (r === 0) {
|
|
69
|
+
for (let i = 0; i < 16; i++) {
|
|
70
|
+
cbcData[n * 16 + i] ^= this._k1[i];
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
cbcData[(n + 1) * 16 + r] = 0x80;
|
|
74
|
+
for (let i = 0; i < 16; i++) {
|
|
75
|
+
cbcData[(n + 1) * 16 + i] ^= this._k2[i];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
let cbcEncrypted = await window.crypto.subtle.encrypt({
|
|
79
|
+
name: "AES-CBC",
|
|
80
|
+
iv: this._zeroBlock,
|
|
81
|
+
}, this._cbcKey, cbcData);
|
|
82
|
+
|
|
83
|
+
cbcEncrypted = new Uint8Array(cbcEncrypted);
|
|
84
|
+
const mac = cbcEncrypted.slice(cbcEncrypted.length - 32, cbcEncrypted.length - 16);
|
|
85
|
+
return mac;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async setKey(key) {
|
|
89
|
+
this._rawKey = key;
|
|
90
|
+
this._ctrKey = await window.crypto.subtle.importKey(
|
|
91
|
+
"raw", key, {"name": "AES-CTR"}, false, ["encrypt", "decrypt"]);
|
|
92
|
+
this._cbcKey = await window.crypto.subtle.importKey(
|
|
93
|
+
"raw", key, {"name": "AES-CBC"}, false, ["encrypt", "decrypt"]);
|
|
94
|
+
await this._initCMAC();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async encrypt(message, associatedData, nonce) {
|
|
98
|
+
const nCMAC = await this._computeCMAC(nonce, this._prefixBlock0);
|
|
99
|
+
const encrypted = await this._encryptCTR(message, nCMAC);
|
|
100
|
+
const adCMAC = await this._computeCMAC(associatedData, this._prefixBlock1);
|
|
101
|
+
const mac = await this._computeCMAC(encrypted, this._prefixBlock2);
|
|
102
|
+
for (let i = 0; i < 16; i++) {
|
|
103
|
+
mac[i] ^= nCMAC[i] ^ adCMAC[i];
|
|
104
|
+
}
|
|
105
|
+
const res = new Uint8Array(16 + encrypted.length);
|
|
106
|
+
res.set(encrypted);
|
|
107
|
+
res.set(mac, encrypted.length);
|
|
108
|
+
return res;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async decrypt(encrypted, associatedData, nonce, mac) {
|
|
112
|
+
const nCMAC = await this._computeCMAC(nonce, this._prefixBlock0);
|
|
113
|
+
const adCMAC = await this._computeCMAC(associatedData, this._prefixBlock1);
|
|
114
|
+
const computedMac = await this._computeCMAC(encrypted, this._prefixBlock2);
|
|
115
|
+
for (let i = 0; i < 16; i++) {
|
|
116
|
+
computedMac[i] ^= nCMAC[i] ^ adCMAC[i];
|
|
117
|
+
}
|
|
118
|
+
if (computedMac.length !== mac.length) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
for (let i = 0; i < mac.length; i++) {
|
|
122
|
+
if (computedMac[i] !== mac[i]) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const res = await this._decryptCTR(encrypted, nCMAC);
|
|
127
|
+
return res;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export class RA2Cipher {
|
|
132
|
+
constructor() {
|
|
133
|
+
this._cipher = new AESEAXCipher();
|
|
134
|
+
this._counter = new Uint8Array(16);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async setKey(key) {
|
|
138
|
+
await this._cipher.setKey(key);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async makeMessage(message) {
|
|
142
|
+
const ad = new Uint8Array([(message.length & 0xff00) >>> 8, message.length & 0xff]);
|
|
143
|
+
const encrypted = await this._cipher.encrypt(message, ad, this._counter);
|
|
144
|
+
for (let i = 0; i < 16 && this._counter[i]++ === 255; i++);
|
|
145
|
+
const res = new Uint8Array(message.length + 2 + 16);
|
|
146
|
+
res.set(ad);
|
|
147
|
+
res.set(encrypted, 2);
|
|
148
|
+
return res;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async receiveMessage(length, encrypted, mac) {
|
|
152
|
+
const ad = new Uint8Array([(length & 0xff00) >>> 8, length & 0xff]);
|
|
153
|
+
const res = await this._cipher.decrypt(encrypted, ad, this._counter, mac);
|
|
154
|
+
for (let i = 0; i < 16 && this._counter[i]++ === 255; i++);
|
|
155
|
+
return res;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export class RSACipher {
|
|
160
|
+
constructor(keyLength) {
|
|
161
|
+
this._key = null;
|
|
162
|
+
this._keyLength = keyLength;
|
|
163
|
+
this._keyBytes = Math.ceil(keyLength / 8);
|
|
164
|
+
this._n = null;
|
|
165
|
+
this._e = null;
|
|
166
|
+
this._d = null;
|
|
167
|
+
this._nBigInt = null;
|
|
168
|
+
this._eBigInt = null;
|
|
169
|
+
this._dBigInt = null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
_base64urlDecode(data) {
|
|
173
|
+
data = data.replace(/-/g, "+").replace(/_/g, "/");
|
|
174
|
+
data = data.padEnd(Math.ceil(data.length / 4) * 4, "=");
|
|
175
|
+
return Base64.decode(data);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
_u8ArrayToBigInt(arr) {
|
|
179
|
+
let hex = '0x';
|
|
180
|
+
for (let i = 0; i < arr.length; i++) {
|
|
181
|
+
hex += arr[i].toString(16).padStart(2, '0');
|
|
182
|
+
}
|
|
183
|
+
return BigInt(hex);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
_padArray(arr, length) {
|
|
187
|
+
const res = new Uint8Array(length);
|
|
188
|
+
res.set(arr, length - arr.length);
|
|
189
|
+
return res;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
_bigIntToU8Array(bigint, padLength=0) {
|
|
193
|
+
let hex = bigint.toString(16);
|
|
194
|
+
if (padLength === 0) {
|
|
195
|
+
padLength = Math.ceil(hex.length / 2) * 2;
|
|
196
|
+
}
|
|
197
|
+
hex = hex.padStart(padLength * 2, '0');
|
|
198
|
+
const length = hex.length / 2;
|
|
199
|
+
const arr = new Uint8Array(length);
|
|
200
|
+
for (let i = 0; i < length; i++) {
|
|
201
|
+
arr[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
202
|
+
}
|
|
203
|
+
return arr;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
_modPow(b, e, m) {
|
|
207
|
+
if (m === 1n) {
|
|
208
|
+
return 0;
|
|
209
|
+
}
|
|
210
|
+
let r = 1n;
|
|
211
|
+
b = b % m;
|
|
212
|
+
while (e > 0) {
|
|
213
|
+
if (e % 2n === 1n) {
|
|
214
|
+
r = (r * b) % m;
|
|
215
|
+
}
|
|
216
|
+
e = e / 2n;
|
|
217
|
+
b = (b * b) % m;
|
|
218
|
+
}
|
|
219
|
+
return r;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async generateKey() {
|
|
223
|
+
this._key = await window.crypto.subtle.generateKey(
|
|
224
|
+
{
|
|
225
|
+
name: "RSA-OAEP",
|
|
226
|
+
modulusLength: this._keyLength,
|
|
227
|
+
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
|
228
|
+
hash: {name: "SHA-256"},
|
|
229
|
+
},
|
|
230
|
+
true, ["encrypt", "decrypt"]);
|
|
231
|
+
const privateKey = await window.crypto.subtle.exportKey("jwk", this._key.privateKey);
|
|
232
|
+
this._n = this._padArray(this._base64urlDecode(privateKey.n), this._keyBytes);
|
|
233
|
+
this._nBigInt = this._u8ArrayToBigInt(this._n);
|
|
234
|
+
this._e = this._padArray(this._base64urlDecode(privateKey.e), this._keyBytes);
|
|
235
|
+
this._eBigInt = this._u8ArrayToBigInt(this._e);
|
|
236
|
+
this._d = this._padArray(this._base64urlDecode(privateKey.d), this._keyBytes);
|
|
237
|
+
this._dBigInt = this._u8ArrayToBigInt(this._d);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
setPublicKey(n, e) {
|
|
241
|
+
if (n.length !== this._keyBytes || e.length !== this._keyBytes) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
this._n = new Uint8Array(this._keyBytes);
|
|
245
|
+
this._e = new Uint8Array(this._keyBytes);
|
|
246
|
+
this._n.set(n);
|
|
247
|
+
this._e.set(e);
|
|
248
|
+
this._nBigInt = this._u8ArrayToBigInt(this._n);
|
|
249
|
+
this._eBigInt = this._u8ArrayToBigInt(this._e);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
encrypt(message) {
|
|
253
|
+
if (message.length > this._keyBytes - 11) {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
const ps = new Uint8Array(this._keyBytes - message.length - 3);
|
|
257
|
+
window.crypto.getRandomValues(ps);
|
|
258
|
+
for (let i = 0; i < ps.length; i++) {
|
|
259
|
+
ps[i] = Math.floor(ps[i] * 254 / 255 + 1);
|
|
260
|
+
}
|
|
261
|
+
const em = new Uint8Array(this._keyBytes);
|
|
262
|
+
em[1] = 0x02;
|
|
263
|
+
em.set(ps, 2);
|
|
264
|
+
em.set(message, ps.length + 3);
|
|
265
|
+
const emBigInt = this._u8ArrayToBigInt(em);
|
|
266
|
+
const c = this._modPow(emBigInt, this._eBigInt, this._nBigInt);
|
|
267
|
+
return this._bigIntToU8Array(c, this._keyBytes);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
decrypt(message) {
|
|
271
|
+
if (message.length !== this._keyBytes) {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
const msgBigInt = this._u8ArrayToBigInt(message);
|
|
275
|
+
const emBigInt = this._modPow(msgBigInt, this._dBigInt, this._nBigInt);
|
|
276
|
+
const em = this._bigIntToU8Array(emBigInt, this._keyBytes);
|
|
277
|
+
if (em[0] !== 0x00 || em[1] !== 0x02) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
let i = 2;
|
|
281
|
+
for (; i < em.length; i++) {
|
|
282
|
+
if (em[i] === 0x00) {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (i === em.length) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
return em.slice(i + 1, em.length);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
get keyLength() {
|
|
293
|
+
return this._keyLength;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
get n() {
|
|
297
|
+
return this._n;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
get e() {
|
|
301
|
+
return this._e;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
get d() {
|
|
305
|
+
return this._d;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export default class RSAAESAuthenticationState extends EventTargetMixin {
|
|
310
|
+
constructor(sock, getCredentials) {
|
|
311
|
+
super();
|
|
312
|
+
this._hasStarted = false;
|
|
313
|
+
this._checkSock = null;
|
|
314
|
+
this._checkCredentials = null;
|
|
315
|
+
this._approveServerResolve = null;
|
|
316
|
+
this._sockReject = null;
|
|
317
|
+
this._credentialsReject = null;
|
|
318
|
+
this._approveServerReject = null;
|
|
319
|
+
this._sock = sock;
|
|
320
|
+
this._getCredentials = getCredentials;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
_waitSockAsync(len) {
|
|
324
|
+
return new Promise((resolve, reject) => {
|
|
325
|
+
const hasData = () => !this._sock.rQwait('RA2', len);
|
|
326
|
+
if (hasData()) {
|
|
327
|
+
resolve();
|
|
328
|
+
} else {
|
|
329
|
+
this._checkSock = () => {
|
|
330
|
+
if (hasData()) {
|
|
331
|
+
resolve();
|
|
332
|
+
this._checkSock = null;
|
|
333
|
+
this._sockReject = null;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
this._sockReject = reject;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
_waitApproveKeyAsync() {
|
|
342
|
+
return new Promise((resolve, reject) => {
|
|
343
|
+
this._approveServerResolve = resolve;
|
|
344
|
+
this._approveServerReject = reject;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
_waitCredentialsAsync(subtype) {
|
|
349
|
+
const hasCredentials = () => {
|
|
350
|
+
if (subtype === 1 && this._getCredentials().username !== undefined &&
|
|
351
|
+
this._getCredentials().password !== undefined) {
|
|
352
|
+
return true;
|
|
353
|
+
} else if (subtype === 2 && this._getCredentials().password !== undefined) {
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
return false;
|
|
357
|
+
};
|
|
358
|
+
return new Promise((resolve, reject) => {
|
|
359
|
+
if (hasCredentials()) {
|
|
360
|
+
resolve();
|
|
361
|
+
} else {
|
|
362
|
+
this._checkCredentials = () => {
|
|
363
|
+
if (hasCredentials()) {
|
|
364
|
+
resolve();
|
|
365
|
+
this._checkCredentials = null;
|
|
366
|
+
this._credentialsReject = null;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
this._credentialsReject = reject;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
checkInternalEvents() {
|
|
375
|
+
if (this._checkSock !== null) {
|
|
376
|
+
this._checkSock();
|
|
377
|
+
}
|
|
378
|
+
if (this._checkCredentials !== null) {
|
|
379
|
+
this._checkCredentials();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
approveServer() {
|
|
384
|
+
if (this._approveServerResolve !== null) {
|
|
385
|
+
this._approveServerResolve();
|
|
386
|
+
this._approveServerResolve = null;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
disconnect() {
|
|
391
|
+
if (this._sockReject !== null) {
|
|
392
|
+
this._sockReject(new Error("disconnect normally"));
|
|
393
|
+
this._sockReject = null;
|
|
394
|
+
}
|
|
395
|
+
if (this._credentialsReject !== null) {
|
|
396
|
+
this._credentialsReject(new Error("disconnect normally"));
|
|
397
|
+
this._credentialsReject = null;
|
|
398
|
+
}
|
|
399
|
+
if (this._approveServerReject !== null) {
|
|
400
|
+
this._approveServerReject(new Error("disconnect normally"));
|
|
401
|
+
this._approveServerReject = null;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async negotiateRA2neAuthAsync() {
|
|
406
|
+
this._hasStarted = true;
|
|
407
|
+
// 1: Receive server public key
|
|
408
|
+
await this._waitSockAsync(4);
|
|
409
|
+
const serverKeyLengthBuffer = this._sock.rQslice(0, 4);
|
|
410
|
+
const serverKeyLength = this._sock.rQshift32();
|
|
411
|
+
if (serverKeyLength < 1024) {
|
|
412
|
+
throw new Error("RA2: server public key is too short: " + serverKeyLength);
|
|
413
|
+
} else if (serverKeyLength > 8192) {
|
|
414
|
+
throw new Error("RA2: server public key is too long: " + serverKeyLength);
|
|
415
|
+
}
|
|
416
|
+
const serverKeyBytes = Math.ceil(serverKeyLength / 8);
|
|
417
|
+
await this._waitSockAsync(serverKeyBytes * 2);
|
|
418
|
+
const serverN = this._sock.rQshiftBytes(serverKeyBytes);
|
|
419
|
+
const serverE = this._sock.rQshiftBytes(serverKeyBytes);
|
|
420
|
+
const serverRSACipher = new RSACipher(serverKeyLength);
|
|
421
|
+
serverRSACipher.setPublicKey(serverN, serverE);
|
|
422
|
+
const serverPublickey = new Uint8Array(4 + serverKeyBytes * 2);
|
|
423
|
+
serverPublickey.set(serverKeyLengthBuffer);
|
|
424
|
+
serverPublickey.set(serverN, 4);
|
|
425
|
+
serverPublickey.set(serverE, 4 + serverKeyBytes);
|
|
426
|
+
|
|
427
|
+
// verify server public key
|
|
428
|
+
this.dispatchEvent(new CustomEvent("serververification", {
|
|
429
|
+
detail: { type: "RSA", publickey: serverPublickey }
|
|
430
|
+
}));
|
|
431
|
+
await this._waitApproveKeyAsync();
|
|
432
|
+
|
|
433
|
+
// 2: Send client public key
|
|
434
|
+
const clientKeyLength = 2048;
|
|
435
|
+
const clientKeyBytes = Math.ceil(clientKeyLength / 8);
|
|
436
|
+
const clientRSACipher = new RSACipher(clientKeyLength);
|
|
437
|
+
await clientRSACipher.generateKey();
|
|
438
|
+
const clientN = clientRSACipher.n;
|
|
439
|
+
const clientE = clientRSACipher.e;
|
|
440
|
+
const clientPublicKey = new Uint8Array(4 + clientKeyBytes * 2);
|
|
441
|
+
clientPublicKey[0] = (clientKeyLength & 0xff000000) >>> 24;
|
|
442
|
+
clientPublicKey[1] = (clientKeyLength & 0xff0000) >>> 16;
|
|
443
|
+
clientPublicKey[2] = (clientKeyLength & 0xff00) >>> 8;
|
|
444
|
+
clientPublicKey[3] = clientKeyLength & 0xff;
|
|
445
|
+
clientPublicKey.set(clientN, 4);
|
|
446
|
+
clientPublicKey.set(clientE, 4 + clientKeyBytes);
|
|
447
|
+
this._sock.send(clientPublicKey);
|
|
448
|
+
|
|
449
|
+
// 3: Send client random
|
|
450
|
+
const clientRandom = new Uint8Array(16);
|
|
451
|
+
window.crypto.getRandomValues(clientRandom);
|
|
452
|
+
const clientEncryptedRandom = serverRSACipher.encrypt(clientRandom);
|
|
453
|
+
const clientRandomMessage = new Uint8Array(2 + serverKeyBytes);
|
|
454
|
+
clientRandomMessage[0] = (serverKeyBytes & 0xff00) >>> 8;
|
|
455
|
+
clientRandomMessage[1] = serverKeyBytes & 0xff;
|
|
456
|
+
clientRandomMessage.set(clientEncryptedRandom, 2);
|
|
457
|
+
this._sock.send(clientRandomMessage);
|
|
458
|
+
|
|
459
|
+
// 4: Receive server random
|
|
460
|
+
await this._waitSockAsync(2);
|
|
461
|
+
if (this._sock.rQshift16() !== clientKeyBytes) {
|
|
462
|
+
throw new Error("RA2: wrong encrypted message length");
|
|
463
|
+
}
|
|
464
|
+
const serverEncryptedRandom = this._sock.rQshiftBytes(clientKeyBytes);
|
|
465
|
+
const serverRandom = clientRSACipher.decrypt(serverEncryptedRandom);
|
|
466
|
+
if (serverRandom === null || serverRandom.length !== 16) {
|
|
467
|
+
throw new Error("RA2: corrupted server encrypted random");
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// 5: Compute session keys and set ciphers
|
|
471
|
+
let clientSessionKey = new Uint8Array(32);
|
|
472
|
+
let serverSessionKey = new Uint8Array(32);
|
|
473
|
+
clientSessionKey.set(serverRandom);
|
|
474
|
+
clientSessionKey.set(clientRandom, 16);
|
|
475
|
+
serverSessionKey.set(clientRandom);
|
|
476
|
+
serverSessionKey.set(serverRandom, 16);
|
|
477
|
+
clientSessionKey = await window.crypto.subtle.digest("SHA-1", clientSessionKey);
|
|
478
|
+
clientSessionKey = new Uint8Array(clientSessionKey).slice(0, 16);
|
|
479
|
+
serverSessionKey = await window.crypto.subtle.digest("SHA-1", serverSessionKey);
|
|
480
|
+
serverSessionKey = new Uint8Array(serverSessionKey).slice(0, 16);
|
|
481
|
+
const clientCipher = new RA2Cipher();
|
|
482
|
+
await clientCipher.setKey(clientSessionKey);
|
|
483
|
+
const serverCipher = new RA2Cipher();
|
|
484
|
+
await serverCipher.setKey(serverSessionKey);
|
|
485
|
+
|
|
486
|
+
// 6: Compute and exchange hashes
|
|
487
|
+
let serverHash = new Uint8Array(8 + serverKeyBytes * 2 + clientKeyBytes * 2);
|
|
488
|
+
let clientHash = new Uint8Array(8 + serverKeyBytes * 2 + clientKeyBytes * 2);
|
|
489
|
+
serverHash.set(serverPublickey);
|
|
490
|
+
serverHash.set(clientPublicKey, 4 + serverKeyBytes * 2);
|
|
491
|
+
clientHash.set(clientPublicKey);
|
|
492
|
+
clientHash.set(serverPublickey, 4 + clientKeyBytes * 2);
|
|
493
|
+
serverHash = await window.crypto.subtle.digest("SHA-1", serverHash);
|
|
494
|
+
clientHash = await window.crypto.subtle.digest("SHA-1", clientHash);
|
|
495
|
+
serverHash = new Uint8Array(serverHash);
|
|
496
|
+
clientHash = new Uint8Array(clientHash);
|
|
497
|
+
this._sock.send(await clientCipher.makeMessage(clientHash));
|
|
498
|
+
await this._waitSockAsync(2 + 20 + 16);
|
|
499
|
+
if (this._sock.rQshift16() !== 20) {
|
|
500
|
+
throw new Error("RA2: wrong server hash");
|
|
501
|
+
}
|
|
502
|
+
const serverHashReceived = await serverCipher.receiveMessage(
|
|
503
|
+
20, this._sock.rQshiftBytes(20), this._sock.rQshiftBytes(16));
|
|
504
|
+
if (serverHashReceived === null) {
|
|
505
|
+
throw new Error("RA2: failed to authenticate the message");
|
|
506
|
+
}
|
|
507
|
+
for (let i = 0; i < 20; i++) {
|
|
508
|
+
if (serverHashReceived[i] !== serverHash[i]) {
|
|
509
|
+
throw new Error("RA2: wrong server hash");
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// 7: Receive subtype
|
|
514
|
+
await this._waitSockAsync(2 + 1 + 16);
|
|
515
|
+
if (this._sock.rQshift16() !== 1) {
|
|
516
|
+
throw new Error("RA2: wrong subtype");
|
|
517
|
+
}
|
|
518
|
+
let subtype = (await serverCipher.receiveMessage(
|
|
519
|
+
1, this._sock.rQshiftBytes(1), this._sock.rQshiftBytes(16)));
|
|
520
|
+
if (subtype === null) {
|
|
521
|
+
throw new Error("RA2: failed to authenticate the message");
|
|
522
|
+
}
|
|
523
|
+
subtype = subtype[0];
|
|
524
|
+
if (subtype === 1) {
|
|
525
|
+
if (this._getCredentials().username === undefined ||
|
|
526
|
+
this._getCredentials().password === undefined) {
|
|
527
|
+
this.dispatchEvent(new CustomEvent(
|
|
528
|
+
"credentialsrequired",
|
|
529
|
+
{ detail: { types: ["username", "password"] } }));
|
|
530
|
+
}
|
|
531
|
+
} else if (subtype === 2) {
|
|
532
|
+
if (this._getCredentials().password === undefined) {
|
|
533
|
+
this.dispatchEvent(new CustomEvent(
|
|
534
|
+
"credentialsrequired",
|
|
535
|
+
{ detail: { types: ["password"] } }));
|
|
536
|
+
}
|
|
537
|
+
} else {
|
|
538
|
+
throw new Error("RA2: wrong subtype");
|
|
539
|
+
}
|
|
540
|
+
await this._waitCredentialsAsync(subtype);
|
|
541
|
+
let username;
|
|
542
|
+
if (subtype === 1) {
|
|
543
|
+
username = encodeUTF8(this._getCredentials().username).slice(0, 255);
|
|
544
|
+
} else {
|
|
545
|
+
username = "";
|
|
546
|
+
}
|
|
547
|
+
const password = encodeUTF8(this._getCredentials().password).slice(0, 255);
|
|
548
|
+
const credentials = new Uint8Array(username.length + password.length + 2);
|
|
549
|
+
credentials[0] = username.length;
|
|
550
|
+
credentials[username.length + 1] = password.length;
|
|
551
|
+
for (let i = 0; i < username.length; i++) {
|
|
552
|
+
credentials[i + 1] = username.charCodeAt(i);
|
|
553
|
+
}
|
|
554
|
+
for (let i = 0; i < password.length; i++) {
|
|
555
|
+
credentials[username.length + 2 + i] = password.charCodeAt(i);
|
|
556
|
+
}
|
|
557
|
+
this._sock.send(await clientCipher.makeMessage(credentials));
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
get hasStarted() {
|
|
561
|
+
return this._hasStarted;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
set hasStarted(s) {
|
|
565
|
+
this._hasStarted = s;
|
|
566
|
+
}
|
|
567
|
+
}
|