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 inherits = require('util').inherits;
21var events = require('events');
22var type = require('../core').type;
23var log = require('../core').log;
24var error = require('../core').error;
27 * Message type present in X224 packet header
30 X224_TPDU_CONNECTION_REQUEST : 0xE0,
31 X224_TPDU_CONNECTION_CONFIRM : 0xD0,
32 X224_TPDU_DISCONNECT_REQUEST : 0x80,
33 X224_TPDU_DATA : 0xF0,
34 X224_TPDU_ERROR : 0x70
38 * Type of negotiation present in negotiation packet
40var NegotiationType = {
41 TYPE_RDP_NEG_REQ : 0x01,
42 TYPE_RDP_NEG_RSP : 0x02,
43 TYPE_RDP_NEG_FAILURE : 0x03
47 * Protocols available for x224 layer
50 PROTOCOL_RDP : 0x00000000,
51 PROTOCOL_SSL : 0x00000001,
52 PROTOCOL_HYBRID : 0x00000002,
53 PROTOCOL_HYBRID_EX : 0x00000008
57 * Use to negotiate security layer of RDP stack
58 * In node-rdpjs only ssl is available
59 * @param opt {object} component type options
60 * @see request -> http://msdn.microsoft.com/en-us/library/cc240500.aspx
61 * @see response -> http://msdn.microsoft.com/en-us/library/cc240506.aspx
62 * @see failure ->http://msdn.microsoft.com/en-us/library/cc240507.aspx
64function negotiation(opt) {
66 type : new type.UInt8(),
67 flag : new type.UInt8(),
68 length : new type.UInt16Le(0x0008, { constant : true }),
69 result : new type.UInt32Le()
71 return new type.Component(self, opt);
75 * X224 client connection request
76 * @param opt {object} component type options
77 * @see http://msdn.microsoft.com/en-us/library/cc240470.aspx
79function clientConnectionRequestPDU(opt, cookie) {
81 len : new type.UInt8(function() {
82 return new type.Component(self).size() - 1;
84 code : new type.UInt8(MessageType.X224_TPDU_CONNECTION_REQUEST, { constant : true }),
85 padding : new type.Component([new type.UInt16Le(), new type.UInt16Le(), new type.UInt8()]),
86 cookie : cookie || new type.Factory( function (s) {
89 var token = s.buffer.readUInt16LE(s.offset + offset);
90 if (token === 0x0a0d) {
91 self.cookie = new type.BinaryString(null, { readLength : new type.CallableValue(offset + 2) }).read(s);
98 }, { conditional : function () {
99 return self.len.value > 14;
101 protocolNeg : negotiation({ optional : true })
104 return new type.Component(self, opt);
108 * X224 Server connection confirm
109 * @param opt {object} component type options
110 * @see http://msdn.microsoft.com/en-us/library/cc240506.aspx
112function serverConnectionConfirm(opt) {
114 len : new type.UInt8(function() {
115 return new type.Component(self).size() - 1;
117 code : new type.UInt8(MessageType.X224_TPDU_CONNECTION_CONFIRM, { constant : true }),
118 padding : new type.Component([new type.UInt16Le(), new type.UInt16Le(), new type.UInt8()]),
119 protocolNeg : negotiation({ optional : true })
122 return new type.Component(self, opt);
126 * Header of each data message from x224 layer
127 * @returns {type.Component}
129function x224DataHeader() {
131 header : new type.UInt8(2),
132 messageType : new type.UInt8(MessageType.X224_TPDU_DATA, { constant : true }),
133 separator : new type.UInt8(0x80, { constant : true })
135 return new type.Component(self);
139 * Common X224 Automata
140 * @param presentation {Layer} presentation layer
142function X224(transport) {
143 this.transport = transport;
144 this.requestedProtocol = Protocols.PROTOCOL_SSL | Protocols.PROTOCOL_HYBRID;
145 this.selectedProtocol = Protocols.PROTOCOL_SSL | Protocols.PROTOCOL_HYBRID;
148 this.transport.on('close', function() {
150 }).on('error', function (err) {
151 self.emit('error', err);
156inherits(X224, events.EventEmitter);
159 * Main data received function
160 * after connection sequence
161 * @param s {type.Stream} stream formated from transport layer
163X224.prototype.recvData = function(s) {
165 x224DataHeader().read(s);
166 this.emit('data', s);
170 * Format message from x224 layer to transport layer
171 * @param message {type}
172 * @returns {type.Component} x224 formated message
174X224.prototype.send = function(message) {
175 this.transport.send(new type.Component([x224DataHeader(), message]));
179 * Client x224 automata
180 * @param transport {events.EventEmitter} (bind data events)
182function Client(transport, config) {
183 this.config = config;
184 X224.call(this, transport);
187//inherit from X224 automata
188inherits(Client, X224);
191 * Client automata connect event
193Client.prototype.connect = function() {
194 var message = clientConnectionRequestPDU(null, new type.BinaryString());
195 message.obj.protocolNeg.obj.type.value = NegotiationType.TYPE_RDP_NEG_REQ;
196 message.obj.protocolNeg.obj.result.value = this.requestedProtocol;
197 this.transport.send(message);
199 // next state wait connection confirm packet
201 this.transport.once('data', function(s) {
202 self.recvConnectionConfirm(s);
209Client.prototype.close = function() {
210 this.transport.close();
214 * Receive connection from server
217Client.prototype.recvConnectionConfirm = function(s) {
218 var message = serverConnectionConfirm().read(s);
220 if (message.obj.protocolNeg.obj.type.value == NegotiationType.TYPE_RDP_NEG_FAILURE) {
221 this.emit('error', { err: 'NODE_RDP_PROTOCOL_X224_NEG_FAILURE', code: message.obj.protocolNeg.obj.result.value });
223 //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)");
226 if (message.obj.protocolNeg.obj.type.value == NegotiationType.TYPE_RDP_NEG_RSP) {
227 this.selectedProtocol = message.obj.protocolNeg.obj.result.value;
230 if ([Protocols.PROTOCOL_HYBRID_EX].indexOf(this.selectedProtocol) !== -1) {
231 this.emit('error', 'NODE_RDP_PROTOCOL_X224_NLA_NOT_SUPPORTED');
233 //throw new error.ProtocolError('NODE_RDP_PROTOCOL_X224_NLA_NOT_SUPPORTED');
236 if (this.selectedProtocol == Protocols.PROTOCOL_RDP) {
237 log.debug("RDP standard security selected");
241 if (this.selectedProtocol == Protocols.PROTOCOL_HYBRID) {
242 log.debug("NLA security layer selected");
244 var transportEx = this.transport.transport;
245 this.transport.transport.startTLS(function () {
246 //console.log('TLS connected, start cssp_connect()');
247 var NLA = require('./nla');
248 self.nla = new NLA(transportEx, function () { self.nlaCompleted(); }, self.config.domain, self.config.userName, self.config.password);
249 self.nla.sendNegotiateMessage();
254 // finish connection sequence
256 this.transport.on('data', function(s) {
260 if (this.selectedProtocol == Protocols.PROTOCOL_SSL) {
261 log.debug("SSL standard security selected");
262 this.transport.transport.startTLS(function() {
263 self.emit('connect', self.selectedProtocol);
270 * Called when NLA is completed
272Client.prototype.nlaCompleted = function () {
275 this.transport.on('data', function (s) { self.recvData(s); });
276 this.emit('connect', this.selectedProtocol);
281 * Server x224 automata
283function Server(transport, keyFilePath, crtFilePath) {
284 X224.call(this, transport);
285 this.keyFilePath = keyFilePath;
286 this.crtFilePath = crtFilePath;
288 this.transport.once('data', function (s) {
289 self.recvConnectionRequest(s);
293//inherit from X224 automata
294inherits(Server, X224);
297 * @see http://msdn.microsoft.com/en-us/library/cc240470.aspx
298 * @param s {type.Stream}
300Server.prototype.recvConnectionRequest = function (s) {
301 var request = clientConnectionRequestPDU().read(s);
302 if (!request.obj.protocolNeg.isReaded) {
303 throw new Error('NODE_RDP_PROTOCOL_X224_NO_BASIC_SECURITY_LAYER');
306 this.requestedProtocol = request.obj.protocolNeg.obj.result.value;
307 this.selectedProtocol = this.requestedProtocol & Protocols.PROTOCOL_SSL;
309 if (!(this.selectedProtocol & Protocols.PROTOCOL_SSL)) {
310 var confirm = serverConnectionConfirm();
311 confirm.obj.protocolNeg.obj.type.value = NegociationType.TYPE_RDP_NEG_FAILURE;
312 confirm.obj.protocolNeg.obj.result.value = NegotiationFailureCode.SSL_REQUIRED_BY_SERVER;
313 this.transport.send(confirm);
317 this.sendConnectionConfirm();
322 * Start SSL connection if needed
323 * @see http://msdn.microsoft.com/en-us/library/cc240501.aspx
325Server.prototype.sendConnectionConfirm = function () {
326 var confirm = serverConnectionConfirm();
327 confirm.obj.protocolNeg.obj.type.value = NegotiationType.TYPE_RDP_NEG_RSP;
328 confirm.obj.protocolNeg.obj.result.value = this.selectedProtocol;
329 this.transport.send(confirm);
331 // finish connection sequence
333 this.transport.on('data', function(s) {
337 this.transport.transport.listenTLS(this.keyFilePath, this.crtFilePath, function() {
338 log.debug('start SSL connection');
339 self.emit('connect', self.requestedProtocol);