| 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 | |
| 20 | var type = require('../../core').type; |
| 21 | |
| 22 | var MessageType = { |
| 23 | LICENSE_REQUEST : 0x01, |
| 24 | PLATFORM_CHALLENGE : 0x02, |
| 25 | NEW_LICENSE : 0x03, |
| 26 | UPGRADE_LICENSE : 0x04, |
| 27 | LICENSE_INFO : 0x12, |
| 28 | NEW_LICENSE_REQUEST : 0x13, |
| 29 | PLATFORM_CHALLENGE_RESPONSE : 0x15, |
| 30 | ERROR_ALERT : 0xFF |
| 31 | }; |
| 32 | |
| 33 | /** |
| 34 | * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx |
| 35 | */ |
| 36 | var ErrorCode = { |
| 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 |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * @see http://msdn.microsoft.com/en-us/library/cc240482.aspx |
| 50 | */ |
| 51 | var StateTransition = { |
| 52 | ST_TOTAL_ABORT : 0x00000001, |
| 53 | ST_NO_TRANSITION : 0x00000002, |
| 54 | ST_RESET_PHASE_TO_START : 0x00000003, |
| 55 | ST_RESEND_LAST_MESSAGE : 0x00000004 |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * @see http://msdn.microsoft.com/en-us/library/cc240481.aspx |
| 60 | */ |
| 61 | var BinaryBlobType = { |
| 62 | BB_ANY_BLOB : 0x0000, |
| 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 |
| 72 | }; |
| 73 | |
| 74 | var Preambule = { |
| 75 | PREAMBLE_VERSION_2_0 : 0x2, |
| 76 | PREAMBLE_VERSION_3_0 : 0x3, |
| 77 | EXTENDED_ERROR_MSG_SUPPORTED : 0x80 |
| 78 | }; |
| 79 | |
| 80 | /** |
| 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} |
| 85 | */ |
| 86 | function licenseBinaryBlob(blobType) { |
| 87 | blobType = blobType || BinaryBlobType.BB_ANY_BLOB; |
| 88 | var self = { |
| 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(); |
| 92 | }), |
| 93 | blobData : new type.BinaryString(null, { readLength : new type.CallableValue(function() { |
| 94 | return self.wBlobLen.value; |
| 95 | })}) |
| 96 | }; |
| 97 | |
| 98 | return new type.Component(self); |
| 99 | } |
| 100 | |
| 101 | /** |
| 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} |
| 106 | */ |
| 107 | function licensingErrorMessage(opt) { |
| 108 | var self = { |
| 109 | __TYPE__ : MessageType.ERROR_ALERT, |
| 110 | dwErrorCode : new type.UInt32Le(), |
| 111 | dwStateTransition : new type.UInt32Le(), |
| 112 | blob : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB) |
| 113 | }; |
| 114 | |
| 115 | return new type.Component(self, opt); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * License product informations |
| 120 | * @see http://msdn.microsoft.com/en-us/library/cc241915.aspx |
| 121 | * @returns {type.Component} |
| 122 | */ |
| 123 | function productInformation() { |
| 124 | var self = { |
| 125 | dwVersion : new type.UInt32Le(), |
| 126 | cbCompanyName : new type.UInt32Le(function() { |
| 127 | return self.pbCompanyName.size(); |
| 128 | }), |
| 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; |
| 132 | })}), |
| 133 | cbProductId : new type.UInt32Le(function() { |
| 134 | return self.pbProductId.size(); |
| 135 | }), |
| 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; |
| 139 | })}) |
| 140 | }; |
| 141 | |
| 142 | return new type.Component(self); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Use in license negotiation |
| 147 | * @see http://msdn.microsoft.com/en-us/library/cc241917.aspx |
| 148 | * @returns {type.Component} |
| 149 | */ |
| 150 | function scope() { |
| 151 | var self = { |
| 152 | scope : licenseBinaryBlob(BinaryBlobType.BB_SCOPE_BLOB) |
| 153 | }; |
| 154 | |
| 155 | return new type.Component(self); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @see http://msdn.microsoft.com/en-us/library/cc241916.aspx |
| 160 | * @returns {type.Component} |
| 161 | */ |
| 162 | function scopeList() { |
| 163 | var self = { |
| 164 | scopeCount : new type.UInt32Le(function() { |
| 165 | return self.scopeArray.length; |
| 166 | }), |
| 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)); |
| 171 | } |
| 172 | }) |
| 173 | }; |
| 174 | |
| 175 | return new type.Component(self); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @see http://msdn.microsoft.com/en-us/library/cc241914.aspx |
| 180 | * @param opt {object} type options |
| 181 | * @returns {type.Component} |
| 182 | */ |
| 183 | function serverLicenseRequest(opt) { |
| 184 | var self = { |
| 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() |
| 191 | }; |
| 192 | |
| 193 | return new type.Component(self, opt); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @see http://msdn.microsoft.com/en-us/library/cc241918.aspx |
| 198 | * @param opt {object} type options |
| 199 | * @returns {type.Component} |
| 200 | */ |
| 201 | function clientNewLicenseRequest(opt) { |
| 202 | var self = { |
| 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) |
| 212 | }; |
| 213 | |
| 214 | return new type.Component(self, opt); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @see http://msdn.microsoft.com/en-us/library/cc241921.aspx |
| 219 | * @param opt {object} type options |
| 220 | * @returns {type.Component} |
| 221 | */ |
| 222 | function serverPlatformChallenge(opt) { |
| 223 | var self = { |
| 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) }) |
| 228 | }; |
| 229 | |
| 230 | return new type.Component(self, opt); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @see http://msdn.microsoft.com/en-us/library/cc241922.aspx |
| 235 | * @param opt {object} type options |
| 236 | * @returns {type.Component} |
| 237 | */ |
| 238 | function clientPLatformChallengeResponse(opt) { |
| 239 | var self = { |
| 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) }) |
| 244 | }; |
| 245 | |
| 246 | return new type.Component(self, opt); |
| 247 | }; |
| 248 | |
| 249 | /** |
| 250 | * Global license packet |
| 251 | * @param packet {type.* | null} send packet |
| 252 | * @returns {type.Component} |
| 253 | */ |
| 254 | function licensePacket(message) { |
| 255 | var self = { |
| 256 | bMsgtype : new type.UInt8(function() { |
| 257 | return self.licensingMessage.obj.__TYPE__; |
| 258 | }), |
| 259 | flag : new type.UInt8(Preambule.PREAMBLE_VERSION_3_0), |
| 260 | wMsgSize : new type.UInt16Le(function() { |
| 261 | return new type.Component(self).size(); |
| 262 | }), |
| 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; |
| 268 | })}).read(s); |
| 269 | break; |
| 270 | case MessageType.LICENSE_REQUEST: |
| 271 | self.licensingMessage = serverLicenseRequest({ readLength : new type.CallableValue(function() { |
| 272 | return self.wMsgSize.value - 4; |
| 273 | })}).read(s); |
| 274 | break; |
| 275 | case MessageType.NEW_LICENSE_REQUEST: |
| 276 | self.licensingMessage = clientNewLicenseRequest({ readLength : new type.CallableValue(function() { |
| 277 | return self.wMsgSize.value - 4; |
| 278 | })}).read(s); |
| 279 | break; |
| 280 | case MessageType.PLATFORM_CHALLENGE: |
| 281 | self.licensingMessage = serverPlatformChallenge({ readLength : new type.CallableValue(function() { |
| 282 | return self.wMsgSize.value - 4; |
| 283 | })}).read(s); |
| 284 | break; |
| 285 | case MessageType.PLATFORM_CHALLENGE_RESPONSE: |
| 286 | self.licensingMessage = clientPLatformChallengeResponse({ readLength : new type.CallableValue(function() { |
| 287 | return self.wMsgSize.value - 4; |
| 288 | })}).read(s); |
| 289 | break; |
| 290 | default: |
| 291 | log.error('unknown license message type ' + self.bMsgtype.value); |
| 292 | } |
| 293 | }) |
| 294 | }; |
| 295 | |
| 296 | return new type.Component(self); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Module exports |
| 301 | */ |
| 302 | module.exports = { |
| 303 | MessageType : MessageType, |
| 304 | ErrorCode : ErrorCode, |
| 305 | StateTransition : StateTransition, |
| 306 | licensePacket : licensePacket, |
| 307 | clientNewLicenseRequest : clientNewLicenseRequest, |
| 308 | clientPLatformChallengeResponse : clientPLatformChallengeResponse |
| 309 | }; |