forked from citronneur/node-rdpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlic.js
More file actions
309 lines (282 loc) · 9.79 KB
/
lic.js
File metadata and controls
309 lines (282 loc) · 9.79 KB
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
/*
* Copyright (c) 2014-2015 Sylvain Peyrefitte
*
* This file is part of node-rdpjs.
*
* node-rdpjs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var type = require('../../core').type;
var MessageType = {
LICENSE_REQUEST : 0x01,
PLATFORM_CHALLENGE : 0x02,
NEW_LICENSE : 0x03,
UPGRADE_LICENSE : 0x04,
LICENSE_INFO : 0x12,
NEW_LICENSE_REQUEST : 0x13,
PLATFORM_CHALLENGE_RESPONSE : 0x15,
ERROR_ALERT : 0xFF
};
/**
* @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
*/
var ErrorCode = {
ERR_INVALID_SERVER_CERTIFICATE : 0x00000001,
ERR_NO_LICENSE : 0x00000002,
ERR_INVALID_SCOPE : 0x00000004,
ERR_NO_LICENSE_SERVER : 0x00000006,
STATUS_VALID_CLIENT : 0x00000007,
ERR_INVALID_CLIENT : 0x00000008,
ERR_INVALID_PRODUCTID : 0x0000000B,
ERR_INVALID_MESSAGE_LEN : 0x0000000C,
ERR_INVALID_MAC : 0x00000003
};
/**
* @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
*/
var StateTransition = {
ST_TOTAL_ABORT : 0x00000001,
ST_NO_TRANSITION : 0x00000002,
ST_RESET_PHASE_TO_START : 0x00000003,
ST_RESEND_LAST_MESSAGE : 0x00000004
};
/**
* @see http://msdn.microsoft.com/en-us/library/cc240481.aspx
*/
var BinaryBlobType = {
BB_ANY_BLOB : 0x0000,
BB_DATA_BLOB : 0x0001,
BB_RANDOM_BLOB : 0x0002,
BB_CERTIFICATE_BLOB : 0x0003,
BB_ERROR_BLOB : 0x0004,
BB_ENCRYPTED_DATA_BLOB : 0x0009,
BB_KEY_EXCHG_ALG_BLOB : 0x000D,
BB_SCOPE_BLOB : 0x000E,
BB_CLIENT_USER_NAME_BLOB : 0x000F,
BB_CLIENT_MACHINE_NAME_BLOB : 0x0010
};
var Preambule = {
PREAMBLE_VERSION_2_0 : 0x2,
PREAMBLE_VERSION_3_0 : 0x3,
EXTENDED_ERROR_MSG_SUPPORTED : 0x80
};
/**
* Binary blob to emcompass license information
* @see http://msdn.microsoft.com/en-us/library/cc240481.aspx
* @param blobType {BinaryBlobType.*}
* @returns {type.Component}
*/
function licenseBinaryBlob(blobType) {
blobType = blobType || BinaryBlobType.BB_ANY_BLOB;
var self = {
wBlobType : new type.UInt16Le(blobType, { constant : (blobType === BinaryBlobType.BB_ANY_BLOB)?false:true }),
wBlobLen : new type.UInt16Le(function() {
return self.blobData.size();
}),
blobData : new type.BinaryString(null, { readLength : new type.CallableValue(function() {
return self.wBlobLen.value;
})})
};
return new type.Component(self);
}
/**
* Error message in license PDU automata
* @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
* @param opt {object} type options
* @returns {type.Component}
*/
function licensingErrorMessage(opt) {
var self = {
__TYPE__ : MessageType.ERROR_ALERT,
dwErrorCode : new type.UInt32Le(),
dwStateTransition : new type.UInt32Le(),
blob : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB)
};
return new type.Component(self, opt);
}
/**
* License product informations
* @see http://msdn.microsoft.com/en-us/library/cc241915.aspx
* @returns {type.Component}
*/
function productInformation() {
var self = {
dwVersion : new type.UInt32Le(),
cbCompanyName : new type.UInt32Le(function() {
return self.pbCompanyName.size();
}),
// may contain "Microsoft Corporation" from server microsoft
pbCompanyName : new type.BinaryString(new Buffer('Microsoft Corporation', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbCompanyName.value;
})}),
cbProductId : new type.UInt32Le(function() {
return self.pbProductId.size();
}),
// may contain "A02" from microsoft license server
pbProductId : new type.BinaryString(new Buffer('A02', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbProductId.value;
})})
};
return new type.Component(self);
}
/**
* Use in license negotiation
* @see http://msdn.microsoft.com/en-us/library/cc241917.aspx
* @returns {type.Component}
*/
function scope() {
var self = {
scope : licenseBinaryBlob(BinaryBlobType.BB_SCOPE_BLOB)
};
return new type.Component(self);
}
/**
* @see http://msdn.microsoft.com/en-us/library/cc241916.aspx
* @returns {type.Component}
*/
function scopeList() {
var self = {
scopeCount : new type.UInt32Le(function() {
return self.scopeArray.length;
}),
scopeArray : new type.Factory(function(s) {
self.scopeArray = new type.Component([]);
for(var i = 0; i < self.scopeCount.value; i++) {
self.scopeArray.obj.push(scope().read(s));
}
})
};
return new type.Component(self);
}
/**
* @see http://msdn.microsoft.com/en-us/library/cc241914.aspx
* @param opt {object} type options
* @returns {type.Component}
*/
function serverLicenseRequest(opt) {
var self = {
__TYPE__ : MessageType.LICENSE_REQUEST,
serverRandom : new type.BinaryString(new Buffer(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) } ),
productInfo : productInformation(),
keyExchangeList : licenseBinaryBlob(BinaryBlobType.BB_KEY_EXCHG_ALG_BLOB),
serverCertificate : licenseBinaryBlob(BinaryBlobType.BB_CERTIFICATE_BLOB),
scopeList : scopeList()
};
return new type.Component(self, opt);
}
/**
* @see http://msdn.microsoft.com/en-us/library/cc241918.aspx
* @param opt {object} type options
* @returns {type.Component}
*/
function clientNewLicenseRequest(opt) {
var self = {
__TYPE__ : MessageType.NEW_LICENSE_REQUEST,
preferredKeyExchangeAlg : new type.UInt32Le(0x00000001, { constant : true }),
// pure microsoft client ;-)
// http://msdn.microsoft.com/en-us/library/1040af38-c733-4fb3-acd1-8db8cc979eda#id10
platformId : new type.UInt32Le(0x04000000 | 0x00010000),
clientRandom : new type.BinaryString(new Buffer(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) }),
encryptedPreMasterSecret : licenseBinaryBlob(BinaryBlobType.BB_RANDOM_BLOB),
ClientUserName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_USER_NAME_BLOB),
ClientMachineName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_MACHINE_NAME_BLOB)
};
return new type.Component(self, opt);
}
/**
* @see http://msdn.microsoft.com/en-us/library/cc241921.aspx
* @param opt {object} type options
* @returns {type.Component}
*/
function serverPlatformChallenge(opt) {
var self = {
__TYPE__ : MessageType.PLATFORM_CHALLENGE,
connectFlags : new type.UInt32Le(),
encryptedPlatformChallenge : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB),
MACData : new type.BinaryString(new Buffer(Array(16 + 1).join('\x00')), { readLength : new type.CallableValue(16) })
};
return new type.Component(self, opt);
}
/**
* @see http://msdn.microsoft.com/en-us/library/cc241922.aspx
* @param opt {object} type options
* @returns {type.Component}
*/
function clientPLatformChallengeResponse(opt) {
var self = {
__TYPE__ : MessageType.PLATFORM_CHALLENGE_RESPONSE,
encryptedPlatformChallengeResponse : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
encryptedHWID : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
MACData : new type.BinaryString(new Buffer(Array(16 + 1).join('\x00'), 'binary'), { readLength : new type.CallableValue(16) })
};
return new type.Component(self, opt);
};
/**
* Global license packet
* @param packet {type.* | null} send packet
* @returns {type.Component}
*/
function licensePacket(message) {
var self = {
bMsgtype : new type.UInt8(function() {
return self.licensingMessage.obj.__TYPE__;
}),
flag : new type.UInt8(Preambule.PREAMBLE_VERSION_3_0),
wMsgSize : new type.UInt16Le(function() {
return new type.Component(self).size();
}),
licensingMessage : message || new type.Factory(function(s) {
switch(self.bMsgtype.value) {
case MessageType.ERROR_ALERT:
self.licensingMessage = licensingErrorMessage({ readLength : new type.CallableValue(function() {
return self.wMsgSize.value - 4;
})}).read(s);
break;
case MessageType.LICENSE_REQUEST:
self.licensingMessage = serverLicenseRequest({ readLength : new type.CallableValue(function() {
return self.wMsgSize.value - 4;
})}).read(s);
break;
case MessageType.NEW_LICENSE_REQUEST:
self.licensingMessage = clientNewLicenseRequest({ readLength : new type.CallableValue(function() {
return self.wMsgSize.value - 4;
})}).read(s);
break;
case MessageType.PLATFORM_CHALLENGE:
self.licensingMessage = serverPlatformChallenge({ readLength : new type.CallableValue(function() {
return self.wMsgSize.value - 4;
})}).read(s);
break;
case MessageType.PLATFORM_CHALLENGE_RESPONSE:
self.licensingMessage = clientPLatformChallengeResponse({ readLength : new type.CallableValue(function() {
return self.wMsgSize.value - 4;
})}).read(s);
break;
default:
log.error('unknown license message type ' + self.bMsgtype.value);
}
})
};
return new type.Component(self);
}
/**
* Module exports
*/
module.exports = {
MessageType : MessageType,
ErrorCode : ErrorCode,
StateTransition : StateTransition,
licensePacket : licensePacket,
clientNewLicenseRequest : clientNewLicenseRequest,
clientPLatformChallengeResponse : clientPLatformChallengeResponse
};