x224.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 inherits = require('util').inherits;
  20. var events = require('events');
  21. var type = require('../core').type;
  22. var log = require('../core').log;
  23. var error = require('../core').error;
  24. /**
  25. * Message type present in X224 packet header
  26. */
  27. var MessageType = {
  28. X224_TPDU_CONNECTION_REQUEST : 0xE0,
  29. X224_TPDU_CONNECTION_CONFIRM : 0xD0,
  30. X224_TPDU_DISCONNECT_REQUEST : 0x80,
  31. X224_TPDU_DATA : 0xF0,
  32. X224_TPDU_ERROR : 0x70
  33. };
  34. /**
  35. * Type of negotiation present in negotiation packet
  36. */
  37. var NegotiationType = {
  38. TYPE_RDP_NEG_REQ : 0x01,
  39. TYPE_RDP_NEG_RSP : 0x02,
  40. TYPE_RDP_NEG_FAILURE : 0x03
  41. };
  42. /**
  43. * Protocols available for x224 layer
  44. */
  45. var Protocols = {
  46. PROTOCOL_RDP : 0x00000000,
  47. PROTOCOL_SSL : 0x00000001,
  48. PROTOCOL_HYBRID : 0x00000002,
  49. PROTOCOL_HYBRID_EX : 0x00000008
  50. };
  51. /**
  52. * Use to negotiate security layer of RDP stack
  53. * In node-rdpjs only ssl is available
  54. * @param opt {object} component type options
  55. * @see request -> http://msdn.microsoft.com/en-us/library/cc240500.aspx
  56. * @see response -> http://msdn.microsoft.com/en-us/library/cc240506.aspx
  57. * @see failure ->http://msdn.microsoft.com/en-us/library/cc240507.aspx
  58. */
  59. function negotiation(opt) {
  60. var self = {
  61. type : new type.UInt8(),
  62. flag : new type.UInt8(),
  63. length : new type.UInt16Le(0x0008, { constant : true }),
  64. result : new type.UInt32Le()
  65. };
  66. return new type.Component(self, opt);
  67. }
  68. /**
  69. * X224 client connection request
  70. * @param opt {object} component type options
  71. * @see http://msdn.microsoft.com/en-us/library/cc240470.aspx
  72. */
  73. function clientConnectionRequestPDU(opt, cookie) {
  74. var self = {
  75. len : new type.UInt8(function() {
  76. return new type.Component(self).size() - 1;
  77. }),
  78. code : new type.UInt8(MessageType.X224_TPDU_CONNECTION_REQUEST, { constant : true }),
  79. padding : new type.Component([new type.UInt16Le(), new type.UInt16Le(), new type.UInt8()]),
  80. cookie : cookie || new type.Factory( function (s) {
  81. var offset = 0;
  82. while (true) {
  83. var token = s.buffer.readUInt16LE(s.offset + offset);
  84. if (token === 0x0a0d) {
  85. self.cookie = new type.BinaryString(null, { readLength : new type.CallableValue(offset + 2) }).read(s);
  86. return;
  87. }
  88. else {
  89. offset += 1;
  90. }
  91. }
  92. }, { conditional : function () {
  93. return self.len.value > 14;
  94. }}),
  95. protocolNeg : negotiation({ optional : true })
  96. };
  97. return new type.Component(self, opt);
  98. }
  99. /**
  100. * X224 Server connection confirm
  101. * @param opt {object} component type options
  102. * @see http://msdn.microsoft.com/en-us/library/cc240506.aspx
  103. */
  104. function serverConnectionConfirm(opt) {
  105. var self = {
  106. len : new type.UInt8(function() {
  107. return new type.Component(self).size() - 1;
  108. }),
  109. code : new type.UInt8(MessageType.X224_TPDU_CONNECTION_CONFIRM, { constant : true }),
  110. padding : new type.Component([new type.UInt16Le(), new type.UInt16Le(), new type.UInt8()]),
  111. protocolNeg : negotiation({ optional : true })
  112. };
  113. return new type.Component(self, opt);
  114. }
  115. /**
  116. * Header of each data message from x224 layer
  117. * @returns {type.Component}
  118. */
  119. function x224DataHeader() {
  120. var self = {
  121. header : new type.UInt8(2),
  122. messageType : new type.UInt8(MessageType.X224_TPDU_DATA, { constant : true }),
  123. separator : new type.UInt8(0x80, { constant : true })
  124. };
  125. return new type.Component(self);
  126. }
  127. /**
  128. * Common X224 Automata
  129. * @param presentation {Layer} presentation layer
  130. */
  131. function X224(transport) {
  132. this.transport = transport;
  133. this.requestedProtocol = Protocols.PROTOCOL_SSL | Protocols.PROTOCOL_HYBRID;
  134. this.selectedProtocol = Protocols.PROTOCOL_SSL | Protocols.PROTOCOL_HYBRID;
  135. var self = this;
  136. this.transport.on('close', function() {
  137. self.emit('close');
  138. }).on('error', function (err) {
  139. self.emit('error', err);
  140. });
  141. }
  142. //inherit from Layer
  143. inherits(X224, events.EventEmitter);
  144. /**
  145. * Main data received function
  146. * after connection sequence
  147. * @param s {type.Stream} stream formated from transport layer
  148. */
  149. X224.prototype.recvData = function(s) {
  150. // check header
  151. x224DataHeader().read(s);
  152. this.emit('data', s);
  153. };
  154. /**
  155. * Format message from x224 layer to transport layer
  156. * @param message {type}
  157. * @returns {type.Component} x224 formated message
  158. */
  159. X224.prototype.send = function(message) {
  160. this.transport.send(new type.Component([x224DataHeader(), message]));
  161. };
  162. /**
  163. * Client x224 automata
  164. * @param transport {events.EventEmitter} (bind data events)
  165. */
  166. function Client(transport, config) {
  167. this.config = config;
  168. X224.call(this, transport);
  169. }
  170. //inherit from X224 automata
  171. inherits(Client, X224);
  172. /**
  173. * Client automata connect event
  174. */
  175. Client.prototype.connect = function() {
  176. var message = clientConnectionRequestPDU(null, new type.BinaryString());
  177. message.obj.protocolNeg.obj.type.value = NegotiationType.TYPE_RDP_NEG_REQ;
  178. message.obj.protocolNeg.obj.result.value = this.requestedProtocol;
  179. this.transport.send(message);
  180. // next state wait connection confirm packet
  181. var self = this;
  182. this.transport.once('data', function(s) {
  183. self.recvConnectionConfirm(s);
  184. });
  185. };
  186. /**
  187. * close stack
  188. */
  189. Client.prototype.close = function() {
  190. this.transport.close();
  191. };
  192. /**
  193. * Receive connection from server
  194. * @param s {Stream}
  195. */
  196. Client.prototype.recvConnectionConfirm = function(s) {
  197. var message = serverConnectionConfirm().read(s);
  198. if (message.obj.protocolNeg.obj.type.value == NegotiationType.TYPE_RDP_NEG_FAILURE) {
  199. this.emit('error', { err: 'NODE_RDP_PROTOCOL_X224_NEG_FAILURE', code: message.obj.protocolNeg.obj.result.value });
  200. return;
  201. //throw new error.ProtocolError('NODE_RDP_PROTOCOL_X224_NEG_FAILURE', 'Failure code:' + message.obj.protocolNeg.obj.result.value + " (see https://msdn.microsoft.com/en-us/library/cc240507.aspx)");
  202. }
  203. if (message.obj.protocolNeg.obj.type.value == NegotiationType.TYPE_RDP_NEG_RSP) {
  204. this.selectedProtocol = message.obj.protocolNeg.obj.result.value;
  205. }
  206. if ([Protocols.PROTOCOL_HYBRID_EX].indexOf(this.selectedProtocol) !== -1) {
  207. this.emit('error', 'NODE_RDP_PROTOCOL_X224_NLA_NOT_SUPPORTED');
  208. return;
  209. //throw new error.ProtocolError('NODE_RDP_PROTOCOL_X224_NLA_NOT_SUPPORTED');
  210. }
  211. if (this.selectedProtocol == Protocols.PROTOCOL_RDP) {
  212. log.debug("RDP standard security selected");
  213. return;
  214. }
  215. if (this.selectedProtocol == Protocols.PROTOCOL_HYBRID) {
  216. log.debug("NLA security layer selected");
  217. var self = this;
  218. var transportEx = this.transport.transport;
  219. this.transport.transport.startTLS(function () {
  220. //console.log('TLS connected, start cssp_connect()');
  221. var NLA = require('./nla');
  222. self.nla = new NLA(transportEx, function () { self.nlaCompleted(); }, self.config.domain, self.config.userName, self.config.password);
  223. self.nla.sendNegotiateMessage();
  224. });
  225. return;
  226. }
  227. // finish connection sequence
  228. var self = this;
  229. this.transport.on('data', function(s) {
  230. self.recvData(s);
  231. });
  232. if (this.selectedProtocol == Protocols.PROTOCOL_SSL) {
  233. log.debug("SSL standard security selected");
  234. this.transport.transport.startTLS(function() {
  235. self.emit('connect', self.selectedProtocol);
  236. });
  237. return;
  238. }
  239. };
  240. /**
  241. * Called when NLA is completed
  242. */
  243. Client.prototype.nlaCompleted = function () {
  244. const self = this;
  245. delete self.nla;
  246. this.transport.on('data', function (s) { self.recvData(s); });
  247. this.emit('connect', this.selectedProtocol);
  248. }
  249. /**
  250. * Server x224 automata
  251. */
  252. function Server(transport, keyFilePath, crtFilePath) {
  253. X224.call(this, transport);
  254. this.keyFilePath = keyFilePath;
  255. this.crtFilePath = crtFilePath;
  256. var self = this;
  257. this.transport.once('data', function (s) {
  258. self.recvConnectionRequest(s);
  259. });
  260. }
  261. //inherit from X224 automata
  262. inherits(Server, X224);
  263. /**
  264. * @see http://msdn.microsoft.com/en-us/library/cc240470.aspx
  265. * @param s {type.Stream}
  266. */
  267. Server.prototype.recvConnectionRequest = function (s) {
  268. var request = clientConnectionRequestPDU().read(s);
  269. if (!request.obj.protocolNeg.isReaded) {
  270. throw new Error('NODE_RDP_PROTOCOL_X224_NO_BASIC_SECURITY_LAYER');
  271. }
  272. this.requestedProtocol = request.obj.protocolNeg.obj.result.value;
  273. this.selectedProtocol = this.requestedProtocol & Protocols.PROTOCOL_SSL;
  274. if (!(this.selectedProtocol & Protocols.PROTOCOL_SSL)) {
  275. var confirm = serverConnectionConfirm();
  276. confirm.obj.protocolNeg.obj.type.value = NegociationType.TYPE_RDP_NEG_FAILURE;
  277. confirm.obj.protocolNeg.obj.result.value = NegotiationFailureCode.SSL_REQUIRED_BY_SERVER;
  278. this.transport.send(confirm);
  279. this.close();
  280. }
  281. else {
  282. this.sendConnectionConfirm();
  283. }
  284. };
  285. /**
  286. * Start SSL connection if needed
  287. * @see http://msdn.microsoft.com/en-us/library/cc240501.aspx
  288. */
  289. Server.prototype.sendConnectionConfirm = function () {
  290. var confirm = serverConnectionConfirm();
  291. confirm.obj.protocolNeg.obj.type.value = NegotiationType.TYPE_RDP_NEG_RSP;
  292. confirm.obj.protocolNeg.obj.result.value = this.selectedProtocol;
  293. this.transport.send(confirm);
  294. // finish connection sequence
  295. var self = this;
  296. this.transport.on('data', function(s) {
  297. self.recvData(s);
  298. });
  299. this.transport.transport.listenTLS(this.keyFilePath, this.crtFilePath, function() {
  300. log.debug('start SSL connection');
  301. self.emit('connect', self.requestedProtocol);
  302. });
  303. };
  304. /**
  305. * Module exports
  306. */
  307. module.exports = {
  308. Client : Client,
  309. Server : Server
  310. };