@novnc/novnc 1.2.0 → 1.3.0-g0ef7582
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/LICENSE.txt +0 -6
- package/README.md +16 -6
- package/core/decoders/copyrect.js +5 -0
- package/core/decoders/hextile.js +57 -3
- package/core/decoders/jpeg.js +141 -0
- package/core/decoders/raw.js +12 -2
- package/core/decoders/tight.js +24 -8
- package/core/decoders/zrle.js +185 -0
- package/core/display.js +21 -151
- package/core/encodings.js +4 -0
- package/core/input/domkeytable.js +25 -21
- package/core/input/keyboard.js +22 -127
- package/core/input/util.js +18 -35
- package/core/input/vkeys.js +0 -1
- package/core/input/xtscancodes.js +5 -3
- package/core/ra2.js +567 -0
- package/core/rfb.js +487 -171
- package/core/util/browser.js +0 -17
- package/core/util/cursor.js +1 -11
- package/core/util/events.js +0 -4
- package/core/util/md5.js +79 -0
- package/core/websock.js +76 -17
- package/docs/API.md +107 -6
- package/docs/LIBRARY.md +3 -7
- package/lib/base64.js +24 -38
- package/lib/decoders/copyrect.js +6 -11
- package/lib/decoders/hextile.js +68 -44
- package/lib/decoders/jpeg.js +146 -0
- package/lib/decoders/raw.js +14 -21
- package/lib/decoders/rre.js +3 -17
- package/lib/decoders/tight.js +43 -93
- package/lib/decoders/tightpng.js +11 -33
- package/lib/decoders/zrle.js +185 -0
- package/lib/deflator.js +9 -26
- package/lib/des.js +22 -38
- package/lib/display.js +100 -315
- package/lib/encodings.js +7 -8
- package/lib/inflator.js +6 -22
- package/lib/input/domkeytable.js +240 -208
- package/lib/input/fixedkeys.js +10 -5
- package/lib/input/gesturehandler.js +84 -154
- package/lib/input/keyboard.js +87 -238
- package/lib/input/keysym.js +16 -272
- package/lib/input/keysymdef.js +7 -9
- package/lib/input/util.js +69 -156
- package/lib/input/vkeys.js +2 -7
- package/lib/input/xtscancodes.js +10 -171
- package/lib/ra2.js +1033 -0
- package/lib/rfb.js +947 -1149
- package/lib/util/browser.js +25 -52
- package/lib/util/cursor.js +25 -81
- package/lib/util/element.js +3 -5
- package/lib/util/events.js +26 -35
- package/lib/util/eventtarget.js +4 -16
- package/lib/util/int.js +2 -3
- package/lib/util/logging.js +3 -21
- package/lib/util/md5.js +83 -0
- package/lib/util/strings.js +3 -5
- package/lib/vendor/pako/lib/utils/common.js +10 -19
- package/lib/vendor/pako/lib/zlib/adler32.js +4 -8
- package/lib/vendor/pako/lib/zlib/constants.js +4 -7
- package/lib/vendor/pako/lib/zlib/crc32.js +6 -13
- package/lib/vendor/pako/lib/zlib/deflate.js +304 -708
- package/lib/vendor/pako/lib/zlib/gzheader.js +2 -14
- package/lib/vendor/pako/lib/zlib/inffast.js +61 -177
- package/lib/vendor/pako/lib/zlib/inflate.js +421 -909
- package/lib/vendor/pako/lib/zlib/inftrees.js +66 -172
- package/lib/vendor/pako/lib/zlib/messages.js +3 -13
- package/lib/vendor/pako/lib/zlib/trees.js +250 -592
- package/lib/vendor/pako/lib/zlib/zstream.js +3 -19
- package/lib/websock.js +119 -111
- package/package.json +2 -10
- package/core/util/polyfill.js +0 -61
- package/lib/util/polyfill.js +0 -72
- package/lib/vendor/promise.js +0 -255
package/core/input/util.js
CHANGED
|
@@ -22,9 +22,8 @@ export function getKeycode(evt) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// The de-facto standard is to use Windows Virtual-Key codes
|
|
25
|
-
// in the 'keyCode' field for non-printable characters
|
|
26
|
-
|
|
27
|
-
if ((evt.type !== 'keypress') && (evt.keyCode in vkeys)) {
|
|
25
|
+
// in the 'keyCode' field for non-printable characters
|
|
26
|
+
if (evt.keyCode in vkeys) {
|
|
28
27
|
let code = vkeys[evt.keyCode];
|
|
29
28
|
|
|
30
29
|
// macOS has messed up this code for some reason
|
|
@@ -69,26 +68,6 @@ export function getKeycode(evt) {
|
|
|
69
68
|
export function getKey(evt) {
|
|
70
69
|
// Are we getting a proper key value?
|
|
71
70
|
if (evt.key !== undefined) {
|
|
72
|
-
// IE and Edge use some ancient version of the spec
|
|
73
|
-
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
|
|
74
|
-
switch (evt.key) {
|
|
75
|
-
case 'Spacebar': return ' ';
|
|
76
|
-
case 'Esc': return 'Escape';
|
|
77
|
-
case 'Scroll': return 'ScrollLock';
|
|
78
|
-
case 'Win': return 'Meta';
|
|
79
|
-
case 'Apps': return 'ContextMenu';
|
|
80
|
-
case 'Up': return 'ArrowUp';
|
|
81
|
-
case 'Left': return 'ArrowLeft';
|
|
82
|
-
case 'Right': return 'ArrowRight';
|
|
83
|
-
case 'Down': return 'ArrowDown';
|
|
84
|
-
case 'Del': return 'Delete';
|
|
85
|
-
case 'Divide': return '/';
|
|
86
|
-
case 'Multiply': return '*';
|
|
87
|
-
case 'Subtract': return '-';
|
|
88
|
-
case 'Add': return '+';
|
|
89
|
-
case 'Decimal': return evt.char;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
71
|
// Mozilla isn't fully in sync with the spec yet
|
|
93
72
|
switch (evt.key) {
|
|
94
73
|
case 'OS': return 'Meta';
|
|
@@ -110,18 +89,7 @@ export function getKey(evt) {
|
|
|
110
89
|
return 'Delete';
|
|
111
90
|
}
|
|
112
91
|
|
|
113
|
-
|
|
114
|
-
// can trust the value provided
|
|
115
|
-
if (!browser.isIE() && !browser.isEdge()) {
|
|
116
|
-
return evt.key;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// IE and Edge have broken handling of AltGraph so we can only
|
|
120
|
-
// trust them for non-printable characters (and unfortunately
|
|
121
|
-
// they also specify 'Unidentified' for some problem keys)
|
|
122
|
-
if ((evt.key.length !== 1) && (evt.key !== 'Unidentified')) {
|
|
123
|
-
return evt.key;
|
|
124
|
-
}
|
|
92
|
+
return evt.key;
|
|
125
93
|
}
|
|
126
94
|
|
|
127
95
|
// Try to deduce it based on the physical key
|
|
@@ -189,6 +157,21 @@ export function getKeysym(evt) {
|
|
|
189
157
|
}
|
|
190
158
|
}
|
|
191
159
|
|
|
160
|
+
// Windows sends alternating symbols for some keys when using a
|
|
161
|
+
// Japanese layout. We have no way of synchronising with the IM
|
|
162
|
+
// running on the remote system, so we send some combined keysym
|
|
163
|
+
// instead and hope for the best.
|
|
164
|
+
if (browser.isWindows()) {
|
|
165
|
+
switch (key) {
|
|
166
|
+
case 'Zenkaku':
|
|
167
|
+
case 'Hankaku':
|
|
168
|
+
return KeyTable.XK_Zenkaku_Hankaku;
|
|
169
|
+
case 'Romaji':
|
|
170
|
+
case 'KanaMode':
|
|
171
|
+
return KeyTable.XK_Romaji;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
192
175
|
return DOMKeyTable[key][location];
|
|
193
176
|
}
|
|
194
177
|
|
package/core/input/vkeys.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* This file is auto-generated from keymaps.csv
|
|
3
|
-
* Database checksum sha256(
|
|
2
|
+
* This file is auto-generated from keymaps.csv
|
|
3
|
+
* Database checksum sha256(76d68c10e97d37fe2ea459e210125ae41796253fb217e900bf2983ade13a7920)
|
|
4
4
|
* To re-generate, run:
|
|
5
|
-
* keymap-gen --lang=js
|
|
5
|
+
* keymap-gen code-map --lang=js keymaps.csv html atset1
|
|
6
6
|
*/
|
|
7
7
|
export default {
|
|
8
8
|
"Again": 0xe005, /* html:Again (Again) -> linux:129 (KEY_AGAIN) -> atset1:57349 */
|
|
@@ -111,6 +111,8 @@ export default {
|
|
|
111
111
|
"KeyX": 0x2d, /* html:KeyX (KeyX) -> linux:45 (KEY_X) -> atset1:45 */
|
|
112
112
|
"KeyY": 0x15, /* html:KeyY (KeyY) -> linux:21 (KEY_Y) -> atset1:21 */
|
|
113
113
|
"KeyZ": 0x2c, /* html:KeyZ (KeyZ) -> linux:44 (KEY_Z) -> atset1:44 */
|
|
114
|
+
"Lang1": 0x72, /* html:Lang1 (Lang1) -> linux:122 (KEY_HANGEUL) -> atset1:114 */
|
|
115
|
+
"Lang2": 0x71, /* html:Lang2 (Lang2) -> linux:123 (KEY_HANJA) -> atset1:113 */
|
|
114
116
|
"Lang3": 0x78, /* html:Lang3 (Lang3) -> linux:90 (KEY_KATAKANA) -> atset1:120 */
|
|
115
117
|
"Lang4": 0x77, /* html:Lang4 (Lang4) -> linux:91 (KEY_HIRAGANA) -> atset1:119 */
|
|
116
118
|
"Lang5": 0x76, /* html:Lang5 (Lang5) -> linux:85 (KEY_ZENKAKUHANKAKU) -> atset1:118 */
|
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
|
+
}
|