-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcs.c
455 lines (402 loc) · 9.31 KB
/
mcs.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"
enum
{
/* ASN.1 Universal tags */
TagBool = 1,
TagInt = 2,
TagOctetString = 4,
TagEnum = 10,
TagSeq = 16, /* also TagSeq OF */
/* 2.2.1.3 Client MCS Connect Initial PDU with GCC Conference Create Request */
ClientCore= 0xC001,
ClientCluster= 0xC004,
ClientSec= 0xC002,
ClientNet= 0xC003,
CanErrinfo= 1,
Want32bpp= 2,
/* 2.2.1.3.1 User Data Header (TS_UD_HEADER) */
SrvCore = 0x0C01,
};
enum
{
Bits5 = 0x1F,
Bits7 = 0x7F,
};
static uchar*
gbuint7(uchar *p, uchar* ep, int* pv)
{
uint u,v,go;
v = 0;
go = 1;
while(go){
if(p >= ep){
werrstr(Eshort);
return nil;
}
u = *p;
v = (v<<7) | (u&Bits7);
if(v&(Bits7<<24)){
werrstr(Ebignum);
return nil;
}
go = u&(1<<7);
p++;
}
*pv = v;
return p;
}
uchar*
gbtag(uchar *p, uchar* ep, int* ptag)
{
if(p >= ep){
werrstr(Eshort);
return nil;
}
*ptag = p[0] & Bits5;
p += 1;
if(*ptag == Bits5)
p = gbuint7(p, ep, ptag);
return p;
}
uchar*
gblen(uchar *p, uchar* ep, int* plen)
{
int c,v;
if(p >= ep){
werrstr(Eshort);
return nil;
}
v = *p++;
if(v < (1<<7)){
*plen = v;
return p;
}
c = v&Bits7;
if(p+c >= ep){
werrstr(Eshort);
return nil;
}
switch(c){
default: werrstr(Ebignum); return nil;
case 0: *plen = 0; break;
case 1: *plen = p[0]; break;
case 2: *plen = nhgets(p); break;
case 3: *plen = (nhgets(p)<<8)|p[2]; break;
case 4: *plen = nhgetl(p); break;
}
return p+c;
}
void
pbshort(uchar* p, int v)
{
p[0]=2;
p[1]=2;
hnputs(p+2,v);
}
int
mcstype(uchar* p, uchar* ep)
{
if(tptype(p,ep) != Data){
werrstr("not an X.224 Data TPDU");
return -1;
}
p = tpdat(p, ep);
if(p == nil)
return -1;
if(p >= ep){
werrstr(Eshort);
return -1;
}
return p[0]>>2;
}
int
ismcshangup(uchar* p, uchar* ep)
{
return (mcstype(p,ep) == Mdpu);
}
int
mcschan(uchar *p, uchar* ep)
{
if(mcstype(p,ep) != Msdi){
werrstr("not an MCS Send Data Indication: %r");
return -1;
}
if((p = tpdat(p, ep)) == nil)
return -1;
if(p+5 > ep){
werrstr(Eshort);
return -1;
}
return nhgets(p+3);
}
uchar*
mcsdat(uchar *p, uchar* ep)
{
if(mcstype(p,ep) != Msdi){
werrstr("not an MCS Send Data Indication: %r");
return nil;
}
if((p = tpdat(p, ep)) == nil)
return nil;
if(p+6 > ep){
werrstr(Eshort);
return nil;
}
if(p[6] & 0x80)
p += 8;
else
p += 7;
if(p > ep){
werrstr(Eshort);
return nil;
}
return p;
}
/* 2.2.1.3 Client MCS Connect Initial PDU with GCC Conference Create Request */
int
mkmcsci(uchar* buf, int nbuf, int ndata)
{
uchar* p;
p = buf;
if(nbuf < ndata+MCSCIFIXLEN){
werrstr("buffer too small");
return -1;
}
hnputs(p, 0x7f65); /* Connect-Initial tag */
p[2] = 0x82; /* length in next 2 bytes */
hnputs(p+3, ndata+MCSCIFIXLEN-2*2-1);
p += 5;
/* BER callingDomainSelector */
p[0] = TagOctetString;
p[1] = 1; /* len */
p[2] = 1;
p += 3;
/* BER calledDomainSelector */
p[0] = TagOctetString;
p[1] = 1; /* len */
p[2] = 1;
p += 3;
/* BER upwardFlag */
p[0] = TagBool;
p[1] = 1; /* len */
p[2] = 0xff;
p += 3;
/* BER MCS DomainParamaters: targetParameters */
p[0] = 0x30; /* tag */
p[1] = 8*4; /* len */
pbshort(p+2, 34); /* maxChannelIds */
pbshort(p+6, 2); /* maxUserIds */
pbshort(p+10, 0); /* maxTokenIds */
pbshort(p+14, 1); /* maxPriorities */
pbshort(p+18, 0); /* minThroughput */
pbshort(p+22, 1); /* maxHeight (of a MCS provider) */
pbshort(p+26, 65535); /* maxMCSPDUsize */
pbshort(p+30, 2); /* (MCS) protocolVersion */
p += 34;
/* BER MCS DomainParamaters: minimumParameters */
p[0] = 0x30; /* tag */
p[1] = 8*4; /* len */
pbshort(p+2, 1); /* maxChannelIds */
pbshort(p+6, 1); /* maxUserIds */
pbshort(p+10, 1); /* maxTokenIds */
pbshort(p+14, 1); /* maxPriorities */
pbshort(p+18, 0); /* minThroughput */
pbshort(p+22, 1); /* maxHeight (of a MCS provider) */
pbshort(p+26, 1056); /* maxMCSPDUsize */
pbshort(p+30, 2); /* (MCS) protocolVersion */
p += 34;
/* BER MCS DomainParamaters: maximumParameters */
p[0] = 0x30; /* tag */
p[1] = 8*4; /* len */
pbshort(p+2, 65535); /* maxChannelIds */
pbshort(p+6, 65535); /* maxUserIds */
pbshort(p+10, 65535); /* maxTokenIds */
pbshort(p+14, 1); /* maxPriorities */
pbshort(p+18, 0); /* minThroughput */
pbshort(p+22, 1); /* maxHeight (of a MCS provider) */
pbshort(p+26, 65535); /* maxMCSPDUsize */
pbshort(p+30, 2); /* (MCS) protocolVersion */
p += 34;
/* BER userData */
p[0] = TagOctetString;
p[1] = 0x82; /* length in next 2 bytes */
hnputs(p+2, ndata);
/* userData should follow */
return MCSCIFIXLEN+ndata;
}
/* GCC Conference Create Request [T.124 section 8.7] in ASN.1 PER [X.691] */
int
sizegccr(Msg* m)
{
int size, nv;
nv = m->nvc;
size = 9+14+216+12+12 + 8+12*nv;
return size; // should agree with the below
}
static uchar t124IdentifierKeyOid[7] = {0, 5, 0, 20, 124, 0, 1};
int
putgccr(uchar* buf, uint nb, Msg* m)
{
int i;
uchar *p, *ep;
long gccsize, earlyCapabilityFlags;
int ver, depth, width, height, sproto, wantconsole;
char* sysname;
Vchan *v;
int nv;
p = buf;
ep = buf+nb;
gccsize = sizegccr(m)-9;
if(p+gccsize+9 > ep){
werrstr(Eshort);
return -1;
}
ver = m->ver;
depth = m->depth;
width = m->xsz;
height = m->ysz;
sysname = m->sysname;
sproto = m->sproto;
wantconsole = m->wantconsole;
v = m->vctab;
nv = m->nvc;
earlyCapabilityFlags = CanErrinfo;
if(depth == 32)
earlyCapabilityFlags |= Want32bpp;
if(depth > 24)
depth = 24;
memcpy(p, t124IdentifierKeyOid, 7);
// connectPDU as a PER octet string
hnputs(p+7, (gccsize | 0x8000)); // connectPDU length
hnputs(p+9, 8); // ConferenceCreateRequest
hnputs(p+11, 16);
p[13] = 0;
iputs(p+14, 0xC001); // userData key: h221NonStandard
p[16] = 0;
memcpy(p+17, "Duca", 4); // H.221 nonstandard key (3.2.5.3.3)
hnputs(p+21, ((gccsize-14) | 0x8000)); // userData length
p += 23;
// 2.2.1.3.2 Client Core Data
iputs(p+0, ClientCore);
iputs(p+2, 216); // length of the data block
iputl(p+4, ver); // rdpVersion: RDP5=0x00080004
iputs(p+8, width); // desktopWidth ≤ 4096
iputs(p+10, height); // desktopHeight ≤ 2048
iputs(p+12, 0xCA01); // colorDepth=8bpp, ignored
iputs(p+14, 0xAA03); // SASSequence
iputl(p+16, 0x409); // keyboardLayout=us
iputl(p+20, 2600); // clientBuild
memset(p+24, 32, 0); // clientName[32]
toutf16(p+24, 32, sysname, strlen(sysname)+1);
iputs(p+54, 0); // zero-terminateclientName
iputl(p+56, 4); // keyboardType: 4="IBM enhanced (101-key or 102-key)"
iputl(p+60, 0); // keyboardSubType
iputl(p+64, 12); // keyboardFunctionKey
memset(p+68, 64, 0); // imeFileName[64]
iputs(p+132, 0xCA01); // postBeta2ColorDepth=8bpp, ignored
iputs(p+134, 1); // clientProductId
iputl(p+136, 0); // serialNumber
iputs(p+140, depth); // highColorDepth: 4, 8, 15, 16, 24 bpp.
iputs(p+142, 1+2+4+8); // supportedColorDepths: 1=24, 2=16, 4=15, 8=32 bpp
iputs(p+144, earlyCapabilityFlags); // earlyCapabilityFlags
memset(p+146, 64, 0); // clientDigProductId[64]
p[210] = 7; // connectionType: 7=autodetect
p[211] = 0; // pad1octet
iputl(p+212, sproto); // serverSelectedProtocol
p += 216;
// 2.2.1.3.3 Client Security Data
iputs(p+0, ClientSec);
iputs(p+2, 12); // length of the data block
iputl(p+4, 0); // (legacy) encryptionMethods
iputl(p+8, 0); // extEncryptionMethods
p += 12;
// 2.2.1.3.5 Client Cluster Data *optional*
iputs(p+0, ClientCluster);
iputs(p+2, 12); // length of the data block
iputl(p+4, (wantconsole? 11 : 9)); // Flags
iputl(p+8, 0); // RedirectedSessionID
p += 12;
// 2.2.1.3.4 Client Network Data *optional*
// type[2] len[2] nchan[4] nchan*(name[8] options[4])
iputs(p+0, ClientNet);
iputs(p+2, 8+12*nv);
iputl(p+4, nv);
for(i=0; i<nv; i++){
memcpy(p+8+12*i+0, v[i].name, 8);
hnputl(p+8+12*i+8, v[i].flags);
}
p += 8+12*nv;
return p-buf;
}
int
getmcr(Msg* m, uchar* b, uint nb)
{
int len, tag, r, utype, ulen;
uchar *p, *ep;
p = b;
ep = b+nb;
m->type = Mconnected;
/* at MCS Connect-Response ASN.1 BER-encoded structure */
if((p = gbtag(p, ep, &tag)) == nil || tag != Mcr || (p = gblen(p, ep, &len)) == nil)
return -1;
/* result */
if((p = gbtag(p, ep, &tag)) == nil || tag != TagEnum
|| (p = gblen(p, ep, &len)) == nil || len < 0 || p+len > ep)
return -1;
r = p[0];
if(r != 0){
werrstr("MCS Connect-Response: %d", r);
return -1;
}
p += len;
/* calledConnectId */
if((p = gbtag(p, ep, &tag)) == nil || tag != TagInt
|| (p = gblen(p, ep, &len)) == nil || len < 0 || p+len > ep)
return -1;
p += len;
/* domainParamaters */
if((p = gbtag(p, ep, &tag)) == nil || tag != TagSeq
|| (p = gblen(p, ep, &len)) == nil || len < 0 || p+len > ep)
return -1;
p += len;
/* Mcr userData */
if((p = gbtag(p, ep, &tag)) == nil || tag != TagOctetString
|| (p = gblen(p, ep, &len)) == nil || len < 0 || p+len > ep)
return -1;
/* GCC ConferenceCreateResponse [T.124] sect 8.7 */
if(p[21]&(1<<7))
p += 23;
else
p += 22;
while(p<ep){
/* 2.2.1.3.1 User Data Header (TS_UD_HEADER) */
utype = igets(p+0);
ulen = igets(p+2);
switch(utype){
case SrvCore: /* 2.2.1.4.2 Server Core Data */
m->ver = igetl(p+4);
break;
/* BUG: exract channel IDs from SrvNet */
}
p += ulen;
}
return p-b;
}
/* MCS Send Data Request */
int
putmsdr(uchar* p, int nb, int ndata, int chanid, int mcsuid)
{
if(nb < 8){
werrstr(Esmall);
return -1;
}
p[0] = (Msdr<<2);
hnputs(p+1, mcsuid);
hnputs(p+3, chanid);
p[5] = 0x70;
hnputs(p+6, ndata|0x8000);
return 8;
}