lic.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2014-2015 Sylvain Peyrefitte
  3. *
  4. * This file is part of node-rdpjs.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. var type = require('../../core').type;
  20. var MessageType = {
  21. LICENSE_REQUEST : 0x01,
  22. PLATFORM_CHALLENGE : 0x02,
  23. NEW_LICENSE : 0x03,
  24. UPGRADE_LICENSE : 0x04,
  25. LICENSE_INFO : 0x12,
  26. NEW_LICENSE_REQUEST : 0x13,
  27. PLATFORM_CHALLENGE_RESPONSE : 0x15,
  28. ERROR_ALERT : 0xFF
  29. };
  30. /**
  31. * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
  32. */
  33. var ErrorCode = {
  34. ERR_INVALID_SERVER_CERTIFICATE : 0x00000001,
  35. ERR_NO_LICENSE : 0x00000002,
  36. ERR_INVALID_SCOPE : 0x00000004,
  37. ERR_NO_LICENSE_SERVER : 0x00000006,
  38. STATUS_VALID_CLIENT : 0x00000007,
  39. ERR_INVALID_CLIENT : 0x00000008,
  40. ERR_INVALID_PRODUCTID : 0x0000000B,
  41. ERR_INVALID_MESSAGE_LEN : 0x0000000C,
  42. ERR_INVALID_MAC : 0x00000003
  43. };
  44. /**
  45. * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
  46. */
  47. var StateTransition = {
  48. ST_TOTAL_ABORT : 0x00000001,
  49. ST_NO_TRANSITION : 0x00000002,
  50. ST_RESET_PHASE_TO_START : 0x00000003,
  51. ST_RESEND_LAST_MESSAGE : 0x00000004
  52. };
  53. /**
  54. * @see http://msdn.microsoft.com/en-us/library/cc240481.aspx
  55. */
  56. var BinaryBlobType = {
  57. BB_ANY_BLOB : 0x0000,
  58. BB_DATA_BLOB : 0x0001,
  59. BB_RANDOM_BLOB : 0x0002,
  60. BB_CERTIFICATE_BLOB : 0x0003,
  61. BB_ERROR_BLOB : 0x0004,
  62. BB_ENCRYPTED_DATA_BLOB : 0x0009,
  63. BB_KEY_EXCHG_ALG_BLOB : 0x000D,
  64. BB_SCOPE_BLOB : 0x000E,
  65. BB_CLIENT_USER_NAME_BLOB : 0x000F,
  66. BB_CLIENT_MACHINE_NAME_BLOB : 0x0010
  67. };
  68. var Preambule = {
  69. PREAMBLE_VERSION_2_0 : 0x2,
  70. PREAMBLE_VERSION_3_0 : 0x3,
  71. EXTENDED_ERROR_MSG_SUPPORTED : 0x80
  72. };
  73. /**
  74. * Binary blob to emcompass license information
  75. * @see http://msdn.microsoft.com/en-us/library/cc240481.aspx
  76. * @param blobType {BinaryBlobType.*}
  77. * @returns {type.Component}
  78. */
  79. function licenseBinaryBlob(blobType) {
  80. blobType = blobType || BinaryBlobType.BB_ANY_BLOB;
  81. var self = {
  82. wBlobType : new type.UInt16Le(blobType, { constant : (blobType === BinaryBlobType.BB_ANY_BLOB)?false:true }),
  83. wBlobLen : new type.UInt16Le(function() {
  84. return self.blobData.size();
  85. }),
  86. blobData : new type.BinaryString(null, { readLength : new type.CallableValue(function() {
  87. return self.wBlobLen.value;
  88. })})
  89. };
  90. return new type.Component(self);
  91. }
  92. /**
  93. * Error message in license PDU automata
  94. * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx
  95. * @param opt {object} type options
  96. * @returns {type.Component}
  97. */
  98. function licensingErrorMessage(opt) {
  99. var self = {
  100. __TYPE__ : MessageType.ERROR_ALERT,
  101. dwErrorCode : new type.UInt32Le(),
  102. dwStateTransition : new type.UInt32Le(),
  103. blob : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB)
  104. };
  105. return new type.Component(self, opt);
  106. }
  107. /**
  108. * License product informations
  109. * @see http://msdn.microsoft.com/en-us/library/cc241915.aspx
  110. * @returns {type.Component}
  111. */
  112. function productInformation() {
  113. var self = {
  114. dwVersion : new type.UInt32Le(),
  115. cbCompanyName : new type.UInt32Le(function() {
  116. return self.pbCompanyName.size();
  117. }),
  118. // may contain "Microsoft Corporation" from server microsoft
  119. pbCompanyName : new type.BinaryString(Buffer.from('Microsoft Corporation', 'ucs2'), { readLength : new type.CallableValue(function() {
  120. return self.cbCompanyName.value;
  121. })}),
  122. cbProductId : new type.UInt32Le(function() {
  123. return self.pbProductId.size();
  124. }),
  125. // may contain "A02" from microsoft license server
  126. pbProductId : new type.BinaryString(Buffer.from('A02', 'ucs2'), { readLength : new type.CallableValue(function() {
  127. return self.cbProductId.value;
  128. })})
  129. };
  130. return new type.Component(self);
  131. }
  132. /**
  133. * Use in license negotiation
  134. * @see http://msdn.microsoft.com/en-us/library/cc241917.aspx
  135. * @returns {type.Component}
  136. */
  137. function scope() {
  138. var self = {
  139. scope : licenseBinaryBlob(BinaryBlobType.BB_SCOPE_BLOB)
  140. };
  141. return new type.Component(self);
  142. }
  143. /**
  144. * @see http://msdn.microsoft.com/en-us/library/cc241916.aspx
  145. * @returns {type.Component}
  146. */
  147. function scopeList() {
  148. var self = {
  149. scopeCount : new type.UInt32Le(function() {
  150. return self.scopeArray.length;
  151. }),
  152. scopeArray : new type.Factory(function(s) {
  153. self.scopeArray = new type.Component([]);
  154. for(var i = 0; i < self.scopeCount.value; i++) {
  155. self.scopeArray.obj.push(scope().read(s));
  156. }
  157. })
  158. };
  159. return new type.Component(self);
  160. }
  161. /**
  162. * @see http://msdn.microsoft.com/en-us/library/cc241914.aspx
  163. * @param opt {object} type options
  164. * @returns {type.Component}
  165. */
  166. function serverLicenseRequest(opt) {
  167. var self = {
  168. __TYPE__ : MessageType.LICENSE_REQUEST,
  169. serverRandom : new type.BinaryString(Buffer.from(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) } ),
  170. productInfo : productInformation(),
  171. keyExchangeList : licenseBinaryBlob(BinaryBlobType.BB_KEY_EXCHG_ALG_BLOB),
  172. serverCertificate : licenseBinaryBlob(BinaryBlobType.BB_CERTIFICATE_BLOB),
  173. scopeList : scopeList()
  174. };
  175. return new type.Component(self, opt);
  176. }
  177. /**
  178. * @see http://msdn.microsoft.com/en-us/library/cc241918.aspx
  179. * @param opt {object} type options
  180. * @returns {type.Component}
  181. */
  182. function clientNewLicenseRequest(opt) {
  183. var self = {
  184. __TYPE__ : MessageType.NEW_LICENSE_REQUEST,
  185. preferredKeyExchangeAlg : new type.UInt32Le(0x00000001, { constant : true }),
  186. // pure microsoft client ;-)
  187. // http://msdn.microsoft.com/en-us/library/1040af38-c733-4fb3-acd1-8db8cc979eda#id10
  188. platformId : new type.UInt32Le(0x04000000 | 0x00010000),
  189. clientRandom : new type.BinaryString(Buffer.from(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) }),
  190. encryptedPreMasterSecret : licenseBinaryBlob(BinaryBlobType.BB_RANDOM_BLOB),
  191. ClientUserName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_USER_NAME_BLOB),
  192. ClientMachineName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_MACHINE_NAME_BLOB)
  193. };
  194. return new type.Component(self, opt);
  195. }
  196. /**
  197. * @see http://msdn.microsoft.com/en-us/library/cc241921.aspx
  198. * @param opt {object} type options
  199. * @returns {type.Component}
  200. */
  201. function serverPlatformChallenge(opt) {
  202. var self = {
  203. __TYPE__ : MessageType.PLATFORM_CHALLENGE,
  204. connectFlags : new type.UInt32Le(),
  205. encryptedPlatformChallenge : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB),
  206. MACData : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00')), { readLength : new type.CallableValue(16) })
  207. };
  208. return new type.Component(self, opt);
  209. }
  210. /**
  211. * @see http://msdn.microsoft.com/en-us/library/cc241922.aspx
  212. * @param opt {object} type options
  213. * @returns {type.Component}
  214. */
  215. function clientPLatformChallengeResponse(opt) {
  216. var self = {
  217. __TYPE__ : MessageType.PLATFORM_CHALLENGE_RESPONSE,
  218. encryptedPlatformChallengeResponse : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
  219. encryptedHWID : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
  220. MACData : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00'), 'binary'), { readLength : new type.CallableValue(16) })
  221. };
  222. return new type.Component(self, opt);
  223. };
  224. /**
  225. * Global license packet
  226. * @param packet {type.* | null} send packet
  227. * @returns {type.Component}
  228. */
  229. function licensePacket(message) {
  230. var self = {
  231. bMsgtype : new type.UInt8(function() {
  232. return self.licensingMessage.obj.__TYPE__;
  233. }),
  234. flag : new type.UInt8(Preambule.PREAMBLE_VERSION_3_0),
  235. wMsgSize : new type.UInt16Le(function() {
  236. return new type.Component(self).size();
  237. }),
  238. licensingMessage : message || new type.Factory(function(s) {
  239. switch(self.bMsgtype.value) {
  240. case MessageType.ERROR_ALERT:
  241. self.licensingMessage = licensingErrorMessage({ readLength : new type.CallableValue(function() {
  242. return self.wMsgSize.value - 4;
  243. })}).read(s);
  244. break;
  245. case MessageType.LICENSE_REQUEST:
  246. self.licensingMessage = serverLicenseRequest({ readLength : new type.CallableValue(function() {
  247. return self.wMsgSize.value - 4;
  248. })}).read(s);
  249. break;
  250. case MessageType.NEW_LICENSE_REQUEST:
  251. self.licensingMessage = clientNewLicenseRequest({ readLength : new type.CallableValue(function() {
  252. return self.wMsgSize.value - 4;
  253. })}).read(s);
  254. break;
  255. case MessageType.PLATFORM_CHALLENGE:
  256. self.licensingMessage = serverPlatformChallenge({ readLength : new type.CallableValue(function() {
  257. return self.wMsgSize.value - 4;
  258. })}).read(s);
  259. break;
  260. case MessageType.PLATFORM_CHALLENGE_RESPONSE:
  261. self.licensingMessage = clientPLatformChallengeResponse({ readLength : new type.CallableValue(function() {
  262. return self.wMsgSize.value - 4;
  263. })}).read(s);
  264. break;
  265. default:
  266. log.error('unknown license message type ' + self.bMsgtype.value);
  267. }
  268. })
  269. };
  270. return new type.Component(self);
  271. }
  272. /**
  273. * Module exports
  274. */
  275. module.exports = {
  276. MessageType : MessageType,
  277. ErrorCode : ErrorCode,
  278. StateTransition : StateTransition,
  279. licensePacket : licensePacket,
  280. clientNewLicenseRequest : clientNewLicenseRequest,
  281. clientPLatformChallengeResponse : clientPLatformChallengeResponse
  282. };