1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#[cfg(test)]
mod tests;

use std::{
    error::Error,
    fmt,
    io::{self, prelude::*},
};

use bitflags::bitflags;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};

use crate::tpdu::X224TPDUType;

pub const NEGOTIATION_REQUEST_LEN: usize = 27;
pub const NEGOTIATION_RESPONSE_LEN: usize = 8;

const COOKIE_PREFIX: &str = "Cookie: mstshash=";
const ROUTING_TOKEN_PREFIX: &str = "Cookie: msts=";

const RDP_NEG_DATA_LENGTH: u16 = 8;

bitflags! {
    /// The communication protocol which the client and server agree to transfer
    /// data on during the X.224 phase.
    ///
    /// # MSDN
    ///
    /// * [RDP Negotiation Request (RDP_NEG_REQ)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/902b090b-9cb3-4efc-92bf-ee13373371e3)
    pub struct SecurityProtocol: u32 {
        const RDP = 0;
        const SSL = 1;
        const HYBRID = 2;
        const RDSTLS = 4;
        const HYBRID_EX = 8;
    }
}

bitflags! {
    /// Holds the negotiation protocol flags of the *request* message.
    ///
    /// # MSDN
    ///
    /// * [RDP Negotiation Request (RDP_NEG_REQ)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/902b090b-9cb3-4efc-92bf-ee13373371e3)
    #[derive(Default)]
    pub struct NegotiationRequestFlags: u8 {
        const RESTRICTED_ADMIN_MODE_REQUIRED = 0x01;
        const REDIRECTED_AUTHENTICATION_MODE_REQUIRED = 0x02;
        const CORRELATION_INFO_PRESENT = 0x08;
    }
}

bitflags! {
    /// Holds the negotiation protocol flags of the *response* message.
    ///
    /// # MSDN
    ///
    /// * [RDP Negotiation Response (RDP_NEG_RSP)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/b2975bdc-6d56-49ee-9c57-f2ff3a0b6817)
    #[derive(Default)]
    pub struct NegotiationResponseFlags: u8 {
        const EXTENDED_CLIENT_DATA_SUPPORTED = 0x01;
        const DYNVC_GFX_PROTOCOL_SUPPORTED = 0x02;
        const RDP_NEG_RSP_RESERVED = 0x04;
        const RESTRICTED_ADMIN_MODE_SUPPORTED = 0x08;
        const REDIRECTED_AUTHENTICATION_MODE_SUPPORTED = 0x10;
    }
}

/// The type of the negotiation error. Contained in
/// [`NegotiationError`](enum.NegotiationError.html).
#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive, ToPrimitive)]
pub enum NegotiationFailureCodes {
    SSLRequiredByServer = 1,
    SSLNotAllowedByServer = 2,
    SSLCertNotOnServer = 3,
    InconsistentFlags = 4,
    HybridRequiredByServer = 5,
    /// Used when the failure caused by [`NegotiationFailure`](struct.NegotiationFailure.html).
    SSLWithUserAuthRequiredByServer = 6,
}

/// The kind of the negotiation request message, including the message as a
/// [`String`](https://doc.rust-lang.org/std/string/struct.String.html).
///
/// # MSDN
///
/// * [Client X.224 Connection Request PDU](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/18a27ef9-6f9a-4501-b000-94b1fe3c2c10)
#[derive(PartialEq, Debug)]
pub enum NegoData {
    RoutingToken(String),
    Cookie(String),
}

///  The type of the error that may result from a negotiation process.
#[derive(Debug)]
pub enum NegotiationError {
    /// Corresponds for an I/O error that may occur during a negotiation process
    /// (invalid response code, invalid security protocol code, etc.)
    IOError(io::Error),
    /// May indicate about a negotiation error recieved from a server.
    NegotiationFailure(NegotiationFailureCodes),
}

impl fmt::Display for NegotiationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NegotiationError::IOError(e) => e.fmt(f),
            NegotiationError::NegotiationFailure(code) => {
                write!(f, "Received negotiation error from server, code={:?}", code)
            }
        }
    }
}

impl Error for NegotiationError {}

impl From<io::Error> for NegotiationError {
    fn from(e: io::Error) -> Self {
        NegotiationError::IOError(e)
    }
}

#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive, ToPrimitive)]
enum NegotiationMessage {
    Request = 1,
    Response = 2,
    Failure = 3,
}

/// Writes the negotiation request to the output buffer. The request is composed
/// of a cookie string, a [security protocol](struct.SecurityProtocol.html), and
/// [negotiation request flags](struct.NegotiationRequestFlags.html).
///
/// # Arguments
///
/// * `buffer` - the output buffer
/// * `cookie` - the cookie string slice
/// * `protocol` - the [security protocol](struct.SecurityProtocol.html) of the message
/// * `flags` - the [negotiation request flags](struct.NegotiationRequestFlags.html)
///
/// # MSDN
///
/// * [Client X.224 Connection Request PDU](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/18a27ef9-6f9a-4501-b000-94b1fe3c2c10)
/// * [RDP Negotiation Request (RDP_NEG_REQ)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/902b090b-9cb3-4efc-92bf-ee13373371e3)
pub fn write_negotiation_request(
    mut buffer: impl io::Write,
    cookie: &str,
    protocol: SecurityProtocol,
    flags: NegotiationRequestFlags,
) -> io::Result<()> {
    writeln!(buffer, "{}{}\r", COOKIE_PREFIX, cookie)?;

    if protocol.bits() > SecurityProtocol::RDP.bits() {
        write_negotiation_data(
            buffer,
            NegotiationMessage::Request,
            flags.bits(),
            protocol.bits(),
        )?;
    }

    Ok(())
}

/// Parses the negotiation request represented by the arguments and returns a tuple with
/// [negotiation data](enum.NegoData.html) (optional), a [security protocol](struct.SecurityProtocol.html),
/// and [negotiation request flags](struct.NegotiationRequestFlags.html).
///
/// # Arguments
///
/// * `code` - the [type](enum.X224TPDUType.html) of the X.224 request
/// * `slice` - the input buffer of the request
///
/// # MSDN
///
/// * [Client X.224 Connection Request PDU](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/18a27ef9-6f9a-4501-b000-94b1fe3c2c10)
/// * [RDP Negotiation Request (RDP_NEG_REQ)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/902b090b-9cb3-4efc-92bf-ee13373371e3)
pub fn parse_negotiation_request(
    code: X224TPDUType,
    mut slice: &[u8],
) -> io::Result<(Option<NegoData>, SecurityProtocol, NegotiationRequestFlags)> {
    if code != X224TPDUType::ConnectionRequest {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "expected X224 connection request",
        ));
    }

    let nego_data = if let Some((nego_data, read_len)) = read_nego_data(slice) {
        slice.consume(read_len);

        Some(nego_data)
    } else {
        None
    };

    if slice.len() >= 8 {
        let neg_req = NegotiationMessage::from_u8(slice.read_u8()?).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid negotiation request code",
            )
        })?;
        if neg_req != NegotiationMessage::Request {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid negotiation request code",
            ));
        }

        let flags = NegotiationRequestFlags::from_bits(slice.read_u8()?).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid negotiation request flags",
            )
        })?;
        let _length = slice.read_u16::<LittleEndian>()?;
        let protocol =
            SecurityProtocol::from_bits(slice.read_u32::<LittleEndian>()?).ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidData, "invalid security protocol code")
            })?;

        Ok((nego_data, protocol, flags))
    } else {
        Ok((
            nego_data,
            SecurityProtocol::RDP,
            NegotiationRequestFlags::default(),
        ))
    }
}

/// Writes the negotiation response to an output buffer. The response is composed
/// of a [security protocol](struct.SecurityProtocol.html) and
/// [negotiation response flags](struct.NegotiationRequestFlags.html).
///
/// # Arguments
///
/// * `buffer` - the output buffer
/// * `flags` - the [negotiation response flags](struct.NegotiationResponseFlags.html)
/// * `protocol` - the [security protocol](struct.SecurityProtocol.html) of the message
///
/// # MSDN
///
/// * [Server X.224 Connection Confirm PDU](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/13757f8f-66db-4273-9d2c-385c33b1e483)
/// * [RDP Negotiation Response (RDP_NEG_RSP)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/b2975bdc-6d56-49ee-9c57-f2ff3a0b6817)
pub fn write_negotiation_response(
    buffer: impl io::Write,
    flags: NegotiationResponseFlags,
    protocol: SecurityProtocol,
) -> io::Result<()> {
    write_negotiation_data(
        buffer,
        NegotiationMessage::Response,
        flags.bits(),
        protocol.bits(),
    )
}

/// Writes the negotiation response error to an output buffer.
///
/// # Arguments
///
/// * `buffer` - the output buffer
/// * `error` - the [failure code](enum.NegotiationFailureCodes.html)
pub fn write_negotiation_response_error(
    buffer: impl io::Write,
    error: NegotiationFailureCodes,
) -> io::Result<()> {
    write_negotiation_data(
        buffer,
        NegotiationMessage::Failure,
        0,
        error.to_u32().unwrap() & !0x8000_0000,
    )
}

/// Parses the negotiation response represented by the arguments and
/// returns a tuple with a [security protocol](struct.SecurityProtocol.html)
/// and [negotiation response flags](struct.NegotiationResponseFlags.html)
/// upon success.
///
/// # Arguments
///
/// * `code` - the [type](enum.X224TPDUType.html) of the X.224 message
/// * `stream` - the data type that contains the response
///
/// # MSDN
///
/// * [RDP Negotiation Response (RDP_NEG_RSP)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/b2975bdc-6d56-49ee-9c57-f2ff3a0b6817)
pub fn parse_negotiation_response(
    code: X224TPDUType,
    mut stream: impl io::Read,
) -> Result<(SecurityProtocol, NegotiationResponseFlags), NegotiationError> {
    if code != X224TPDUType::ConnectionConfirm {
        return Err(NegotiationError::IOError(io::Error::new(
            io::ErrorKind::InvalidData,
            "expected X224 connection confirm",
        )));
    }

    let neg_resp = NegotiationMessage::from_u8(stream.read_u8()?).ok_or_else(|| {
        NegotiationError::IOError(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid negotiation response code",
        ))
    })?;
    let flags = NegotiationResponseFlags::from_bits(stream.read_u8()?).ok_or_else(|| {
        NegotiationError::IOError(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid negotiation response flags",
        ))
    })?;
    let _length = stream.read_u16::<LittleEndian>()?;

    if neg_resp == NegotiationMessage::Response {
        let selected_protocol = SecurityProtocol::from_bits(stream.read_u32::<LittleEndian>()?)
            .ok_or_else(|| {
                NegotiationError::IOError(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "invalid security protocol code",
                ))
            })?;
        Ok((selected_protocol, flags))
    } else if neg_resp == NegotiationMessage::Failure {
        let error = NegotiationFailureCodes::from_u32(stream.read_u32::<LittleEndian>()?)
            .ok_or_else(|| {
                NegotiationError::IOError(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "invalid security protocol code",
                ))
            })?;
        Err(NegotiationError::NegotiationFailure(error))
    } else {
        Err(NegotiationError::IOError(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid negotiation response code",
        )))
    }
}

fn read_nego_data(stream: &[u8]) -> Option<(NegoData, usize)> {
    if let Ok((routing_token, read_len)) = read_string_with_cr_lf(stream, ROUTING_TOKEN_PREFIX) {
        Some((NegoData::RoutingToken(routing_token), read_len))
    } else if let Ok((cookie, read_len)) = read_string_with_cr_lf(stream, COOKIE_PREFIX) {
        Some((NegoData::Cookie(cookie), read_len))
    } else {
        None
    }
}

fn read_string_with_cr_lf(
    mut stream: impl io::BufRead,
    start: &str,
) -> io::Result<(String, usize)> {
    let mut read_start = String::new();
    stream
        .by_ref()
        .take(start.len() as u64)
        .read_to_string(&mut read_start)?;

    if read_start == start {
        let mut value = String::new();
        stream.read_line(&mut value)?;
        match value.pop() {
            Some('\n') => (),
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "message uncorrectly terminated",
                ));
            }
        }
        value.pop(); // cr
        let value_len = value.len();

        Ok((value, start.len() + value_len + 2))
    } else {
        Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid or unsuppored message",
        ))
    }
}

fn write_negotiation_data(
    mut cursor: impl io::Write,
    message: NegotiationMessage,
    flags: u8,
    data: u32,
) -> io::Result<()> {
    cursor.write_u8(message.to_u8().unwrap())?;
    cursor.write_u8(flags)?;
    cursor.write_u16::<LittleEndian>(RDP_NEG_DATA_LENGTH)?;
    cursor.write_u32::<LittleEndian>(data)?;

    Ok(())
}