-
Notifications
You must be signed in to change notification settings - Fork 9
/
rcpu.js
293 lines (272 loc) · 7.55 KB
/
rcpu.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"use strict";
const ANAMELEN = 28;
const AERRLEN = 64;
const DOMLEN = 48;
const DESKEYLEN = 7;
const AESKEYLEN = 16;
const CHALLEN = 8;
const NETCHLEN = 16;
const CONFIGLEN = 16;
const SECRETLEN = 32;
const PASSWDLEN = 28;
const NONCELEN = 32;
const PAKKEYLEN = 32;
const PAKSLEN = (448+7)/8|0;
const PAKPLEN = 4 * PAKSLEN;
const PAKHASHLEN = 2 * PAKPLEN;
const PAKXLEN = PAKSLEN;
const PAKYLEN = PAKSLEN;
const AuthTreq = 1;
const AuthChal = 2;
const AuthPass = 3;
const AuthOK = 4;
const AuthErr = 5;
const AuthMod = 6;
const AuthOKvar = 9;
const AuthPAK = 19;
const AuthTs = 64;
const AuthTc = 65;
const AuthAs = 66;
const AuthAc = 67;
const AuthTp = 68;
const PAKPRIVSZ = 4 + PAKXLEN + PAKYLEN;
const AUTHKEYSZ = DESKEYLEN + AESKEYLEN + PAKKEYLEN + PAKHASHLEN;
const TICKETLEN = 12 + CHALLEN + 2 * ANAMELEN + NONCELEN + 16;
const AUTHENTLEN = 12 + CHALLEN + NONCELEN + 16;
const Ticketreq = Struct([
'type', U8,
'authid', FixedString(ANAMELEN),
'authdom', FixedString(DOMLEN),
'chal', Bytes(CHALLEN),
'hostid', FixedString(ANAMELEN),
'uid', FixedString(ANAMELEN),
'paky', Bytes(PAKYLEN)
]);
const Ticket = Struct([
'num', U8,
'chal', Bytes(CHALLEN),
'cuid', FixedString(ANAMELEN),
'suid', FixedString(ANAMELEN),
'key', Bytes(NONCELEN)
]);
const Authenticator = Struct([
'num', U8,
'chal', Bytes(CHALLEN),
'rand', Bytes(NONCELEN)
]);
function tsmemcmp(a, b, n)
{
var diff;
diff = 0;
for(var i = 0; i < n; i++)
diff |= a[i] != b[i];
return diff;
}
function asrdresp(chan, len)
{
return chan.read(b=>1).then(c => {
switch(c[0]){
case AuthOK:
return chan.read(b=>len);
case AuthErr:
return chan.read(b=>64).then(e => {throw new Error("remote: " + from_cstr(e))});
case AuthOKvar:
return chan.read(b=>5).then(b => {
var n = from_cstr(b)|0;
if(n <= 0 || n > len)
throw new Error("AS protocol botch");
return chan.read(b=>n)
});
default:
throw new Error("AS protocol botch");
}
});
}
function convM2T(b, key)
{
return withBufP(TICKETLEN, (buf, buf_array) => {
buf_array().set(b);
if(C.form1M2B(buf, TICKETLEN, key) < 0)
throw new Error("?password mismatch with auth server");
return unpack(Ticket, buf_array().slice());
});
}
function convA2M(s, key)
{
return withBuf(AUTHENTLEN, (buf, buf_array) => {
buf_array().set(pack(Authenticator, s).data());
C.form1B2M(buf, 1 + CHALLEN + NONCELEN, key);
return buf_array().slice();
});
}
function convM2A(b, key)
{
return withBuf(AUTHENTLEN, (buf, buf_array) => {
buf_array().set(b);
if(C.form1M2B(buf, AUTHENTLEN, key) < 0)
throw new Error("?you and auth server agree about password. ?server is confused.");
return unpack(Authenticator, buf_array().slice());
});
}
function getastickets(authkey, tr)
{
return withBufP(PAKYLEN, (ybuf, ybuf_array) =>
withBufP(PAKPRIVSZ, priv => {
return dial(auth_url).then(chan => {
tr.type = AuthPAK;
return chan.write(pack(Ticketreq, tr).data())
.then(() => {
C.authpak_new(priv, authkey, ybuf, 1);
return chan.write(ybuf_array());
}).then(() => asrdresp(chan, 2*PAKYLEN)
).then(buf => {
tr.paky.set(buf.subarray(0, PAKYLEN));
ybuf_array().set(buf.subarray(PAKYLEN));
if(C.authpak_finish(priv, authkey, ybuf))
throw new Error("getastickets failure");
tr.type = AuthTreq;
return chan.write(pack(Ticketreq, tr).data());
}).then(() => asrdresp(chan, 0)
).then(() => chan.read(b=>2*TICKETLEN)
);
});
}));
}
function dp9ik(chan, dom) {
var crand, cchal;
var tr;
var authkey, auth;
var sticket, cticket;
return withBufP(AUTHKEYSZ, authkey => {
crand = new Uint8Array(2*NONCELEN);
cchal = new Uint8Array(CHALLEN);
window.crypto.getRandomValues(crand);
window.crypto.getRandomValues(cchal);
return chan.write(cchal)
.then(() => chan.read(b=>Ticketreq.len))
.then(b => {
tr = unpack(Ticketreq, b);
tr.hostid = user;
tr.uid = user;
C.passtokey(authkey, password);
C.authpak_hash(authkey, tr.uid);
return getastickets(authkey, tr);
}).then(tbuf => {
sticket = tbuf.subarray(TICKETLEN);
let k = Module.HEAPU8.subarray(authkey + AESKEYLEN + DESKEYLEN, authkey + AESKEYLEN + DESKEYLEN + PAKKEYLEN);
return convM2T(tbuf.subarray(0, TICKETLEN), k);
}).then(tick => {
cticket = tick;
return chan.write(tr.paky);
}).then(() => chan.write(sticket))
.then(() => {
let auth = {num: AuthAc, rand: crand.subarray(0, NONCELEN), chal: tr.chal};
return chan.write(convA2M(auth, cticket.key));
}).then(() => chan.read(b=>AUTHENTLEN))
.then(b => {
auth = convM2A(b, cticket.key);
if(auth.num != AuthAs || tsmemcmp(auth.chal, cchal, CHALLEN) != 0)
throw new Error("protocol botch");
crand.subarray(NONCELEN).set(auth.rand);
var ai = {
suid: cticket.suid,
cuid: cticket.cuid,
};
ai.secret = withBuf(256, (secret, secret_array) => {
C.hkdf_x_plan9(crand, cticket.key, secret);
return secret_array().slice();
});
return ai;
})
.finally(() => {
if(cticket){
cticket.key.fill(0);
cticket.chal.fill(0);
}
if(sticket)
sticket.fill(0);
C.memset(authkey, 0, AUTHKEYSZ);
crand.fill(0);
cchal.fill(0);
});
});
}
function p9any(chan) {
var v2, dom;
return readstr(chan).then(str => {
v2 = str.startsWith("v2 ");
if(v2)
str = str.substr(4);
var doms = str
.split(' ')
.filter(s => s.startsWith('dp9ik@'))
.map(s => s.substr(6));
if(doms.length === 0)
throw new Error("server did not offer dp9ik");
dom = doms[0];
return chan.write(new TextEncoder("utf-8").encode('dp9ik ' + dom + '\0'));
}).then(() => {
if(v2)
return readstr(chan).then(s => {
if(s != 'OK')
throw new Error('did not get OK in p9any: got ' + s);
});
}).then(() => dp9ik(chan, dom));
}
function rcpu(failure) {
const script =
"syscall fversion 0 65536 buf 256 >/dev/null >[2=1]\n" +
"mount -nc /fd/0 /mnt/term || exit\n" +
"bind -q /mnt/term/dev/cons /dev/cons\n" +
"if(test -r /mnt/term/dev/kbd){\n" +
" </dev/cons >/dev/cons >[2=1] aux/kbdfs -dq -m /mnt/term/dev\n" +
" bind -q /mnt/term/dev/cons /dev/cons\n" +
"}\n" +
"</dev/cons >/dev/cons >[2=1] service=cpu rc -li\n" +
"echo -n hangup >/proc/$pid/notepg\n";
return dial(rcpu_url)
.then(rawchan => p9any(rawchan).then(ai => tlsClient(rawchan, ai.secret)).catch(failure))
.then(chan => {
if(chan)
return chan.write(new TextEncoder("utf-8").encode(script.length + "\n" + script))
.then(() => chan);
});
}
function main() {
if(user === undefined || user === null || password === undefined || password === null){
document.getElementById('loading').style.display = 'none';
document.getElementById('login').style.display = 'grid';
if(user !== undefined){
document.getElementById('user').value = user;
document.getElementById('password').focus();
}else
document.getElementById('user').focus();
}else
go(true);
}
function go(no_ui) {
if(!no_ui){
user = document.getElementById('user').value;
password = document.getElementById('password').value;
}
document.getElementById('login').style.display = 'none';
document.getElementById('loading').style.display = '';
rcpu(e => {
document.getElementById('loading').style.display = 'none';
document.getElementById('login').style.display = 'grid';
document.getElementById('error').style.display = 'block';
document.getElementById('error').innerHTML = e.toString();
password = undefined;
main();
}).then(chan => {
if(chan){
document.getElementById('thegrey').style.display = 'none';
document.getElementById('loading').style.display = 'none';
document.getElementById('canvas').style.display = 'block';
devcons();
devdraw();
devaudio();
return NineP(chan);
}
});
}