2 * Copyright (c) 2014-2015 Sylvain Peyrefitte
4 * This file is part of node-rdpjs.
6 * node-rdpjs is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20var type = require('../../core').type;
23 LICENSE_REQUEST : 0x01,
24 PLATFORM_CHALLENGE : 0x02,
26 UPGRADE_LICENSE : 0x04,
28 NEW_LICENSE_REQUEST : 0x13,
29 PLATFORM_CHALLENGE_RESPONSE : 0x15,
34 * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
37 ERR_INVALID_SERVER_CERTIFICATE : 0x00000001,
38 ERR_NO_LICENSE : 0x00000002,
39 ERR_INVALID_SCOPE : 0x00000004,
40 ERR_NO_LICENSE_SERVER : 0x00000006,
41 STATUS_VALID_CLIENT : 0x00000007,
42 ERR_INVALID_CLIENT : 0x00000008,
43 ERR_INVALID_PRODUCTID : 0x0000000B,
44 ERR_INVALID_MESSAGE_LEN : 0x0000000C,
45 ERR_INVALID_MAC : 0x00000003
49 * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
51var StateTransition = {
52 ST_TOTAL_ABORT : 0x00000001,
53 ST_NO_TRANSITION : 0x00000002,
54 ST_RESET_PHASE_TO_START : 0x00000003,
55 ST_RESEND_LAST_MESSAGE : 0x00000004
59 * @see http://msdn.microsoft.com/en-us/library/cc240481.aspx
63 BB_DATA_BLOB : 0x0001,
64 BB_RANDOM_BLOB : 0x0002,
65 BB_CERTIFICATE_BLOB : 0x0003,
66 BB_ERROR_BLOB : 0x0004,
67 BB_ENCRYPTED_DATA_BLOB : 0x0009,
68 BB_KEY_EXCHG_ALG_BLOB : 0x000D,
69 BB_SCOPE_BLOB : 0x000E,
70 BB_CLIENT_USER_NAME_BLOB : 0x000F,
71 BB_CLIENT_MACHINE_NAME_BLOB : 0x0010
75 PREAMBLE_VERSION_2_0 : 0x2,
76 PREAMBLE_VERSION_3_0 : 0x3,
77 EXTENDED_ERROR_MSG_SUPPORTED : 0x80
81 * Binary blob to emcompass license information
82 * @see http://msdn.microsoft.com/en-us/library/cc240481.aspx
83 * @param blobType {BinaryBlobType.*}
84 * @returns {type.Component}
86function licenseBinaryBlob(blobType) {
87 blobType = blobType || BinaryBlobType.BB_ANY_BLOB;
89 wBlobType : new type.UInt16Le(blobType, { constant : (blobType === BinaryBlobType.BB_ANY_BLOB)?false:true }),
90 wBlobLen : new type.UInt16Le(function() {
91 return self.blobData.size();
93 blobData : new type.BinaryString(null, { readLength : new type.CallableValue(function() {
94 return self.wBlobLen.value;
98 return new type.Component(self);
102 * Error message in license PDU automata
103 * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
104 * @param opt {object} type options
105 * @returns {type.Component}
107function licensingErrorMessage(opt) {
109 __TYPE__ : MessageType.ERROR_ALERT,
110 dwErrorCode : new type.UInt32Le(),
111 dwStateTransition : new type.UInt32Le(),
112 blob : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB)
115 return new type.Component(self, opt);
119 * License product informations
120 * @see http://msdn.microsoft.com/en-us/library/cc241915.aspx
121 * @returns {type.Component}
123function productInformation() {
125 dwVersion : new type.UInt32Le(),
126 cbCompanyName : new type.UInt32Le(function() {
127 return self.pbCompanyName.size();
129 // may contain "Microsoft Corporation" from server microsoft
130 pbCompanyName : new type.BinaryString(Buffer.from('Microsoft Corporation', 'ucs2'), { readLength : new type.CallableValue(function() {
131 return self.cbCompanyName.value;
133 cbProductId : new type.UInt32Le(function() {
134 return self.pbProductId.size();
136 // may contain "A02" from microsoft license server
137 pbProductId : new type.BinaryString(Buffer.from('A02', 'ucs2'), { readLength : new type.CallableValue(function() {
138 return self.cbProductId.value;
142 return new type.Component(self);
146 * Use in license negotiation
147 * @see http://msdn.microsoft.com/en-us/library/cc241917.aspx
148 * @returns {type.Component}
152 scope : licenseBinaryBlob(BinaryBlobType.BB_SCOPE_BLOB)
155 return new type.Component(self);
159 * @see http://msdn.microsoft.com/en-us/library/cc241916.aspx
160 * @returns {type.Component}
162function scopeList() {
164 scopeCount : new type.UInt32Le(function() {
165 return self.scopeArray.length;
167 scopeArray : new type.Factory(function(s) {
168 self.scopeArray = new type.Component([]);
169 for(var i = 0; i < self.scopeCount.value; i++) {
170 self.scopeArray.obj.push(scope().read(s));
175 return new type.Component(self);
179 * @see http://msdn.microsoft.com/en-us/library/cc241914.aspx
180 * @param opt {object} type options
181 * @returns {type.Component}
183function serverLicenseRequest(opt) {
185 __TYPE__ : MessageType.LICENSE_REQUEST,
186 serverRandom : new type.BinaryString(Buffer.from(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) } ),
187 productInfo : productInformation(),
188 keyExchangeList : licenseBinaryBlob(BinaryBlobType.BB_KEY_EXCHG_ALG_BLOB),
189 serverCertificate : licenseBinaryBlob(BinaryBlobType.BB_CERTIFICATE_BLOB),
190 scopeList : scopeList()
193 return new type.Component(self, opt);
197 * @see http://msdn.microsoft.com/en-us/library/cc241918.aspx
198 * @param opt {object} type options
199 * @returns {type.Component}
201function clientNewLicenseRequest(opt) {
203 __TYPE__ : MessageType.NEW_LICENSE_REQUEST,
204 preferredKeyExchangeAlg : new type.UInt32Le(0x00000001, { constant : true }),
205 // pure microsoft client ;-)
206 // http://msdn.microsoft.com/en-us/library/1040af38-c733-4fb3-acd1-8db8cc979eda#id10
207 platformId : new type.UInt32Le(0x04000000 | 0x00010000),
208 clientRandom : new type.BinaryString(Buffer.from(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) }),
209 encryptedPreMasterSecret : licenseBinaryBlob(BinaryBlobType.BB_RANDOM_BLOB),
210 ClientUserName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_USER_NAME_BLOB),
211 ClientMachineName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_MACHINE_NAME_BLOB)
214 return new type.Component(self, opt);
218 * @see http://msdn.microsoft.com/en-us/library/cc241921.aspx
219 * @param opt {object} type options
220 * @returns {type.Component}
222function serverPlatformChallenge(opt) {
224 __TYPE__ : MessageType.PLATFORM_CHALLENGE,
225 connectFlags : new type.UInt32Le(),
226 encryptedPlatformChallenge : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB),
227 MACData : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00')), { readLength : new type.CallableValue(16) })
230 return new type.Component(self, opt);
234 * @see http://msdn.microsoft.com/en-us/library/cc241922.aspx
235 * @param opt {object} type options
236 * @returns {type.Component}
238function clientPLatformChallengeResponse(opt) {
240 __TYPE__ : MessageType.PLATFORM_CHALLENGE_RESPONSE,
241 encryptedPlatformChallengeResponse : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
242 encryptedHWID : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
243 MACData : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00'), 'binary'), { readLength : new type.CallableValue(16) })
246 return new type.Component(self, opt);
250 * Global license packet
251 * @param packet {type.* | null} send packet
252 * @returns {type.Component}
254function licensePacket(message) {
256 bMsgtype : new type.UInt8(function() {
257 return self.licensingMessage.obj.__TYPE__;
259 flag : new type.UInt8(Preambule.PREAMBLE_VERSION_3_0),
260 wMsgSize : new type.UInt16Le(function() {
261 return new type.Component(self).size();
263 licensingMessage : message || new type.Factory(function(s) {
264 switch(self.bMsgtype.value) {
265 case MessageType.ERROR_ALERT:
266 self.licensingMessage = licensingErrorMessage({ readLength : new type.CallableValue(function() {
267 return self.wMsgSize.value - 4;
270 case MessageType.LICENSE_REQUEST:
271 self.licensingMessage = serverLicenseRequest({ readLength : new type.CallableValue(function() {
272 return self.wMsgSize.value - 4;
275 case MessageType.NEW_LICENSE_REQUEST:
276 self.licensingMessage = clientNewLicenseRequest({ readLength : new type.CallableValue(function() {
277 return self.wMsgSize.value - 4;
280 case MessageType.PLATFORM_CHALLENGE:
281 self.licensingMessage = serverPlatformChallenge({ readLength : new type.CallableValue(function() {
282 return self.wMsgSize.value - 4;
285 case MessageType.PLATFORM_CHALLENGE_RESPONSE:
286 self.licensingMessage = clientPLatformChallengeResponse({ readLength : new type.CallableValue(function() {
287 return self.wMsgSize.value - 4;
291 log.error('unknown license message type ' + self.bMsgtype.value);
296 return new type.Component(self);
303 MessageType : MessageType,
304 ErrorCode : ErrorCode,
305 StateTransition : StateTransition,
306 licensePacket : licensePacket,
307 clientNewLicenseRequest : clientNewLicenseRequest,
308 clientPLatformChallengeResponse : clientPLatformChallengeResponse