From 448b7a5d4bbf7da1820bcc88ebdd4a9e5cba73a5 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Wed, 22 Jan 2014 10:30:46 -0800 Subject: neutrinordp: added a header --- libxrdp/xrdp_rdp.c | 1 + 1 file changed, 1 insertion(+) (limited to 'libxrdp/xrdp_rdp.c') diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index 7e68ec1f..cdbb46a2 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -23,6 +23,7 @@ #if defined(XRDP_NEUTRINORDP) #include +#include #endif /*****************************************************************************/ -- cgit v1.2.3 From 1d9c773dbfe9ab5ceda85773ed8933d73742a9e6 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Sun, 16 Feb 2014 00:34:56 -0800 Subject: libxrdp: improve mcs processing --- libxrdp/xrdp_rdp.c | 232 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 195 insertions(+), 37 deletions(-) (limited to 'libxrdp/xrdp_rdp.c') diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index cdbb46a2..4cc2727f 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -483,65 +483,223 @@ xrdp_rdp_send_data_update_sync(struct xrdp_rdp *self) } /*****************************************************************************/ +/* http://msdn.microsoft.com/en-us/library/cc240510.aspx + 2.2.1.3.2 Client Core Data (TS_UD_CS_CORE) */ static int APP_CC -xrdp_rdp_parse_client_mcs_data(struct xrdp_rdp *self) +xrdp_rdp_parse_client_mcs_data_CS_CORE(struct xrdp_rdp* self, struct stream* s) { - struct stream *p = (struct stream *)NULL; - int i = 0; - - p = &(self->sec_layer->client_mcs_data); - p->p = p->data; - if (!s_check_rem(p, 31 + 2 + 2 + 120 + 2)) + int colorDepth; + int postBeta2ColorDepth; + int highColorDepth; + int supportedColorDepths; + int earlyCapabilityFlags; + + in_uint8s(s, 4); /* version */ + in_uint16_le(s, self->client_info.width); + in_uint16_le(s, self->client_info.height); + in_uint16_le(s, colorDepth); + g_writeln("colorDepth 0x%4.4x (0xca00 4bpp 0xca01 8bpp)", colorDepth); + switch (colorDepth) { - g_writeln("xrdp_rdp_parse_client_mcs_data: error"); - return 1; + case 0xca00: /* RNS_UD_COLOR_4BPP */ + self->client_info.bpp = 4; + break; + case 0xca01: /* RNS_UD_COLOR_8BPP */ + self->client_info.bpp = 8; + break; } - in_uint8s(p, 31); - in_uint16_le(p, self->client_info.width); - in_uint16_le(p, self->client_info.height); - in_uint8s(p, 120); - self->client_info.bpp = 8; - in_uint16_le(p, i); - - switch (i) + in_uint8s(s, 2); /* SASSequence */ + in_uint8s(s, 4); /* keyboardLayout */ + in_uint8s(s, 4); /* clientBuild */ + in_uint8s(s, 32); /* clientName */ + in_uint8s(s, 4); /* keyboardType */ + in_uint8s(s, 4); /* keyboardSubType */ + in_uint8s(s, 4); /* keyboardFunctionKey */ + in_uint8s(s, 64); /* imeFileName */ + in_uint16_le(s, postBeta2ColorDepth); + g_writeln("postBeta2ColorDepth 0x%4.4x (0xca00 4bpp 0xca01 8bpp " + "0xca02 15bpp 0xca03 16bpp 0xca04 24bpp)", postBeta2ColorDepth); + + switch (postBeta2ColorDepth) { - case 0xca01: - if (!s_check_rem(p, 6 + 1)) - { - return 1; - } - in_uint8s(p, 6); - in_uint8(p, i); - - if (i > 8) - { - self->client_info.bpp = i; - } - + case 0xca00: /* RNS_UD_COLOR_4BPP */ + self->client_info.bpp = 4; break; - case 0xca02: + case 0xca01: /* RNS_UD_COLOR_8BPP */ + self->client_info.bpp = 8; + break; + case 0xca02: /* RNS_UD_COLOR_16BPP_555 */ self->client_info.bpp = 15; break; - case 0xca03: + case 0xca03: /* RNS_UD_COLOR_16BPP_565 */ self->client_info.bpp = 16; break; - case 0xca04: + case 0xca04: /* RNS_UD_COLOR_24BPP */ self->client_info.bpp = 24; break; } + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint8s(s, 2); /* clientProductId */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* serialNumber */ + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint16_le(s, highColorDepth); + g_writeln("highColorDepth 0x%4.4x (0x0004 4bpp 0x0008 8bpp 0x000f 15bpp " + "0x0010 16 bpp 0x0018 24bpp)", highColorDepth); + self->client_info.bpp = highColorDepth; + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint16_le(s, supportedColorDepths); + g_writeln("supportedColorDepths 0x%4.4x (0x0001 24bpp 0x0002 16bpp " + "0x0004 15bpp 0x0008 32bpp)", supportedColorDepths); + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint16_le(s, earlyCapabilityFlags); + self->client_info.mcs_early_capability_flags = earlyCapabilityFlags; + g_writeln("earlyCapabilityFlags 0x%4.4x (0x0002 want32)", + earlyCapabilityFlags); + if ((earlyCapabilityFlags & 0x0002) && (supportedColorDepths & 0x0008)) + { + self->client_info.bpp = 32; + } + + if (!s_check_rem(s, 64)) + { + return 0; + } + in_uint8s(s, 64); /* clientDigProductId */ + + if (!s_check_rem(s, 1)) + { + return 0; + } + in_uint8(s, self->client_info.mcs_connection_type); /* connectionType */ + g_writeln("got client client connection type 0x%8.8x", + self->client_info.mcs_connection_type); + + if (!s_check_rem(s, 1)) + { + return 0; + } + in_uint8s(s, 1); /* pad1octet */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* serverSelectedProtocol */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* desktopPhysicalWidth */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* desktopPhysicalHeight */ + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint8s(s, 2); /* reserved */ + + return 0; +} + +/*****************************************************************************/ +static int APP_CC +xrdp_rdp_parse_client_mcs_data(struct xrdp_rdp* self) +{ + struct stream* s; + int i; + int header_type; + int length; + char* hold_p; + char* hold_end; + + s = &(self->sec_layer->client_mcs_data); + s->p = s->data; + in_uint8s(s, 23); + while (s->p < s->end) + { + if (!s_check_rem(s, 4)) + { + g_writeln("xrdp_rdp_parse_client_mcs_data: parse error, bytes " + "left %d", (int)(s->end - s->p)); + return 1; + } + hold_p = s->p; + hold_end = s->end; + in_uint16_le(s, header_type); + in_uint16_le(s, length); + g_writeln("type 0x%2.2x len %d", header_type, length); + if (length < 4) + { + g_writeln("xrdp_rdp_parse_client_mcs_data: parse error"); + return 1; + } + if (!s_check_rem(s, length - 4)) + { + g_writeln("xrdp_rdp_parse_client_mcs_data: parse error"); + return 1; + } + s->end = s->p + length; + switch (header_type) + { + case 0xc001: /* CS_CORE */ + xrdp_rdp_parse_client_mcs_data_CS_CORE(self, s); + break; + case 0xc002: /* CS_SECURITY */ + break; + case 0xc003: /* CS_NET */ + break; + case 0xc004: /* CS_CLUSTER */ + break; + default: + g_writeln("xrdp_rdp_parse_client_mcs_data: error unknown " + "header type 0x%4.4x", header_type); + break; + } + s->p = hold_p + length; + s->end = hold_end; + } if (self->client_info.max_bpp > 0) { if (self->client_info.bpp > self->client_info.max_bpp) { + g_writeln("xrdp_rdp_parse_client_mcs_data: client asked for %dbpp " + "connection but configuration is limited to %dbpp", + self->client_info.bpp, self->client_info.max_bpp); self->client_info.bpp = self->client_info.max_bpp; } } + s->p = s->data; + + g_writeln("xrdp_rdp_parse_client_mcs_data: client bpp %d", + self->client_info.bpp); - p->p = p->data; - DEBUG(("client width %d, client height %d bpp %d", - self->client_info.width, self->client_info.height, - self->client_info.bpp)); return 0; } -- cgit v1.2.3 From b81bd005bf52dabf3ca054050a9be9a40929a8f3 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Thu, 20 Feb 2014 00:41:53 -0800 Subject: libxrdp: started adding FIPS crypt --- libxrdp/libxrdp.h | 20 +++++++++++++++++--- libxrdp/xrdp_rdp.c | 7 ++++++- libxrdp/xrdp_sec.c | 36 +++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 21 deletions(-) (limited to 'libxrdp/xrdp_rdp.c') diff --git a/libxrdp/libxrdp.h b/libxrdp/libxrdp.h index ebaff796..117c9f7f 100644 --- a/libxrdp/libxrdp.h +++ b/libxrdp/libxrdp.h @@ -1,7 +1,7 @@ /** * xrdp: A Remote Desktop Protocol server. * - * Copyright (C) Jay Sorg 2004-2013 + * Copyright (C) Jay Sorg 2004-2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,20 @@ struct xrdp_mcs struct list* channel_list; }; +/* Encryption Methods */ +#define CRYPT_METHOD_NONE 0x00000000 +#define CRYPT_METHOD_40BIT 0x00000001 +#define CRYPT_METHOD_128BIT 0x00000002 +#define CRYPT_METHOD_56BIT 0x00000008 +#define CRYPT_METHOD_FIPS 0x00000010 + +/* Encryption Levels */ +#define CRYPT_LEVEL_NONE 0x00000000 +#define CRYPT_LEVEL_LOW 0x00000001 +#define CRYPT_LEVEL_CLIENT_COMPATIBLE 0x00000002 +#define CRYPT_LEVEL_HIGH 0x00000003 +#define CRYPT_LEVEL_FIPS 0x00000004 + /* sec */ struct xrdp_sec { @@ -90,9 +104,9 @@ struct xrdp_sec char encrypt_key[16]; char decrypt_update_key[16]; char encrypt_update_key[16]; - int rc4_key_size; /* 1 = 40 bit, 2 = 128 bit */ + int crypt_method; int rc4_key_len; /* 8 = 40 bit, 16 = 128 bit */ - int crypt_level; /* 1, 2, 3 = low, meduim, high */ + int crypt_level; char sign_key[16]; void* decrypt_rc4_info; void* encrypt_rc4_info; diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index 4cc2727f..1bf0c8f7 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -1,7 +1,7 @@ /** * xrdp: A Remote Desktop Protocol server. * - * Copyright (C) Jay Sorg 2004-2013 + * Copyright (C) Jay Sorg 2004-2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,6 +80,10 @@ xrdp_rdp_read_config(struct xrdp_client_info *client_info) { client_info->crypt_level = 3; } + else if (g_strcasecmp(value, "fips") == 0) + { + client_info->crypt_level = 4; + } else { log_message(LOG_LEVEL_ALWAYS,"Warning: Your configured crypt level is" @@ -628,6 +632,7 @@ xrdp_rdp_parse_client_mcs_data_CS_CORE(struct xrdp_rdp* self, struct stream* s) } /*****************************************************************************/ +/* TODO: move xrdp_sec.c::xrdp_sec_in_mcs_data here */ static int APP_CC xrdp_rdp_parse_client_mcs_data(struct xrdp_rdp* self) { diff --git a/libxrdp/xrdp_sec.c b/libxrdp/xrdp_sec.c index d1bd49de..71cb513f 100644 --- a/libxrdp/xrdp_sec.c +++ b/libxrdp/xrdp_sec.c @@ -1,7 +1,7 @@ /** * xrdp: A Remote Desktop Protocol server. * - * Copyright (C) Jay Sorg 2004-2013 + * Copyright (C) Jay Sorg 2004-2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -145,22 +145,25 @@ xrdp_sec_create(struct xrdp_rdp *owner, struct trans *trans, int crypt_level, DEBUG((" in xrdp_sec_create")); self = (struct xrdp_sec *)g_malloc(sizeof(struct xrdp_sec), 1); self->rdp_layer = owner; - self->rc4_key_size = 1; /* 1 = 40 bit, 2 = 128 bit */ - self->crypt_level = 1; /* 1, 2, 3 = low, medium, high */ - + self->crypt_method = CRYPT_METHOD_40BIT; + self->crypt_level = CRYPT_LEVEL_LOW; switch (crypt_level) { - case 1: - self->rc4_key_size = 1; - self->crypt_level = 1; + case 1: /* low */ + self->crypt_method = CRYPT_METHOD_40BIT; + self->crypt_level = CRYPT_LEVEL_LOW; + break; + case 2: /* medium */ + self->crypt_method = CRYPT_METHOD_40BIT; + self->crypt_level = CRYPT_LEVEL_CLIENT_COMPATIBLE; break; - case 2: - self->rc4_key_size = 1; - self->crypt_level = 2; + case 3: /* high */ + self->crypt_method = CRYPT_METHOD_128BIT; + self->crypt_level = CRYPT_LEVEL_HIGH; break; - case 3: - self->rc4_key_size = 2; - self->crypt_level = 3; + case 4: /* fips */ + self->crypt_method = CRYPT_METHOD_FIPS; + self->crypt_level = CRYPT_LEVEL_FIPS; break; default: g_writeln("Fatal : Illegal crypt_level"); @@ -715,7 +718,7 @@ xrdp_sec_establish_keys(struct xrdp_sec *self) xrdp_sec_hash_16(self->decrypt_key, session_key + 32, self->client_random, self->server_random); - if (self->rc4_key_size == 1) + if (self->crypt_method == CRYPT_METHOD_40BIT) { xrdp_sec_make_40bit(self->sign_key); xrdp_sec_make_40bit(self->encrypt_key); @@ -1152,9 +1155,8 @@ xrdp_sec_out_mcs_data(struct xrdp_sec *self) out_uint16_le(s, SEC_TAG_SRV_CRYPT); out_uint16_le(s, 0x00ec); /* len is 236 */ - out_uint32_le(s, self->rc4_key_size); /* key len 1 = 40 bit 2 = 128 bit */ - out_uint32_le(s, self->crypt_level); /* crypt level 1 = low 2 = medium */ - /* 3 = high */ + out_uint32_le(s, self->crypt_method); + out_uint32_le(s, self->crypt_level); out_uint32_le(s, 32); /* 32 bytes random len */ out_uint32_le(s, 0xb8); /* 184 bytes rsa info(certificate) len */ out_uint8a(s, self->server_random, 32); -- cgit v1.2.3 From 8e4520b3c3e67ef4c38489ada820236d5287aac1 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Sat, 22 Feb 2014 22:44:58 -0800 Subject: libxrdp: work on fips --- libxrdp/libxrdp.h | 5 + libxrdp/xrdp_mcs.c | 11 +- libxrdp/xrdp_rdp.c | 226 ----------------------- libxrdp/xrdp_sec.c | 530 +++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 508 insertions(+), 264 deletions(-) (limited to 'libxrdp/xrdp_rdp.c') diff --git a/libxrdp/libxrdp.h b/libxrdp/libxrdp.h index 117c9f7f..9ae839e6 100644 --- a/libxrdp/libxrdp.h +++ b/libxrdp/libxrdp.h @@ -116,6 +116,11 @@ struct xrdp_sec char pri_exp[64]; int channel_code; int multimon; + char fips_encrypt_key[24]; + char fips_decrypt_key[24]; + char fips_sign_key[20]; + void* encrypt_fips_info; + void* decrypt_fips_info; }; /* channel */ diff --git a/libxrdp/xrdp_mcs.c b/libxrdp/xrdp_mcs.c index c145158c..02b8ee69 100644 --- a/libxrdp/xrdp_mcs.c +++ b/libxrdp/xrdp_mcs.c @@ -1,7 +1,7 @@ /** * xrdp: A Remote Desktop Protocol server. * - * Copyright (C) Jay Sorg 2004-2013 + * Copyright (C) Jay Sorg 2004-2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -946,16 +946,17 @@ xrdp_mcs_send(struct xrdp_mcs *self, struct stream *s, int chan) * Internal help function to close the socket * @param self */ -void close_rdp_socket(struct xrdp_mcs *self) +void APP_CC +close_rdp_socket(struct xrdp_mcs *self) { - if(self->iso_layer->tcp_layer) + if (self->iso_layer->tcp_layer != 0) { - if(self->iso_layer->tcp_layer->trans) + if (self->iso_layer->tcp_layer->trans != 0) { g_tcp_close(self->iso_layer->tcp_layer->trans->sck); self->iso_layer->tcp_layer->trans->sck = 0 ; g_writeln("xrdp_mcs_disconnect - socket closed"); - return ; + return; } } g_writeln("Failed to close socket"); diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index 1bf0c8f7..b357e954 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -486,228 +486,6 @@ xrdp_rdp_send_data_update_sync(struct xrdp_rdp *self) return 0; } -/*****************************************************************************/ -/* http://msdn.microsoft.com/en-us/library/cc240510.aspx - 2.2.1.3.2 Client Core Data (TS_UD_CS_CORE) */ -static int APP_CC -xrdp_rdp_parse_client_mcs_data_CS_CORE(struct xrdp_rdp* self, struct stream* s) -{ - int colorDepth; - int postBeta2ColorDepth; - int highColorDepth; - int supportedColorDepths; - int earlyCapabilityFlags; - - in_uint8s(s, 4); /* version */ - in_uint16_le(s, self->client_info.width); - in_uint16_le(s, self->client_info.height); - in_uint16_le(s, colorDepth); - g_writeln("colorDepth 0x%4.4x (0xca00 4bpp 0xca01 8bpp)", colorDepth); - switch (colorDepth) - { - case 0xca00: /* RNS_UD_COLOR_4BPP */ - self->client_info.bpp = 4; - break; - case 0xca01: /* RNS_UD_COLOR_8BPP */ - self->client_info.bpp = 8; - break; - } - in_uint8s(s, 2); /* SASSequence */ - in_uint8s(s, 4); /* keyboardLayout */ - in_uint8s(s, 4); /* clientBuild */ - in_uint8s(s, 32); /* clientName */ - in_uint8s(s, 4); /* keyboardType */ - in_uint8s(s, 4); /* keyboardSubType */ - in_uint8s(s, 4); /* keyboardFunctionKey */ - in_uint8s(s, 64); /* imeFileName */ - in_uint16_le(s, postBeta2ColorDepth); - g_writeln("postBeta2ColorDepth 0x%4.4x (0xca00 4bpp 0xca01 8bpp " - "0xca02 15bpp 0xca03 16bpp 0xca04 24bpp)", postBeta2ColorDepth); - - switch (postBeta2ColorDepth) - { - case 0xca00: /* RNS_UD_COLOR_4BPP */ - self->client_info.bpp = 4; - break; - case 0xca01: /* RNS_UD_COLOR_8BPP */ - self->client_info.bpp = 8; - break; - case 0xca02: /* RNS_UD_COLOR_16BPP_555 */ - self->client_info.bpp = 15; - break; - case 0xca03: /* RNS_UD_COLOR_16BPP_565 */ - self->client_info.bpp = 16; - break; - case 0xca04: /* RNS_UD_COLOR_24BPP */ - self->client_info.bpp = 24; - break; - } - if (!s_check_rem(s, 2)) - { - return 0; - } - in_uint8s(s, 2); /* clientProductId */ - - if (!s_check_rem(s, 4)) - { - return 0; - } - in_uint8s(s, 4); /* serialNumber */ - - if (!s_check_rem(s, 2)) - { - return 0; - } - in_uint16_le(s, highColorDepth); - g_writeln("highColorDepth 0x%4.4x (0x0004 4bpp 0x0008 8bpp 0x000f 15bpp " - "0x0010 16 bpp 0x0018 24bpp)", highColorDepth); - self->client_info.bpp = highColorDepth; - - if (!s_check_rem(s, 2)) - { - return 0; - } - in_uint16_le(s, supportedColorDepths); - g_writeln("supportedColorDepths 0x%4.4x (0x0001 24bpp 0x0002 16bpp " - "0x0004 15bpp 0x0008 32bpp)", supportedColorDepths); - - if (!s_check_rem(s, 2)) - { - return 0; - } - in_uint16_le(s, earlyCapabilityFlags); - self->client_info.mcs_early_capability_flags = earlyCapabilityFlags; - g_writeln("earlyCapabilityFlags 0x%4.4x (0x0002 want32)", - earlyCapabilityFlags); - if ((earlyCapabilityFlags & 0x0002) && (supportedColorDepths & 0x0008)) - { - self->client_info.bpp = 32; - } - - if (!s_check_rem(s, 64)) - { - return 0; - } - in_uint8s(s, 64); /* clientDigProductId */ - - if (!s_check_rem(s, 1)) - { - return 0; - } - in_uint8(s, self->client_info.mcs_connection_type); /* connectionType */ - g_writeln("got client client connection type 0x%8.8x", - self->client_info.mcs_connection_type); - - if (!s_check_rem(s, 1)) - { - return 0; - } - in_uint8s(s, 1); /* pad1octet */ - - if (!s_check_rem(s, 4)) - { - return 0; - } - in_uint8s(s, 4); /* serverSelectedProtocol */ - - if (!s_check_rem(s, 4)) - { - return 0; - } - in_uint8s(s, 4); /* desktopPhysicalWidth */ - - if (!s_check_rem(s, 4)) - { - return 0; - } - in_uint8s(s, 4); /* desktopPhysicalHeight */ - - if (!s_check_rem(s, 2)) - { - return 0; - } - in_uint8s(s, 2); /* reserved */ - - return 0; -} - -/*****************************************************************************/ -/* TODO: move xrdp_sec.c::xrdp_sec_in_mcs_data here */ -static int APP_CC -xrdp_rdp_parse_client_mcs_data(struct xrdp_rdp* self) -{ - struct stream* s; - int i; - int header_type; - int length; - char* hold_p; - char* hold_end; - - s = &(self->sec_layer->client_mcs_data); - s->p = s->data; - - in_uint8s(s, 23); - while (s->p < s->end) - { - if (!s_check_rem(s, 4)) - { - g_writeln("xrdp_rdp_parse_client_mcs_data: parse error, bytes " - "left %d", (int)(s->end - s->p)); - return 1; - } - hold_p = s->p; - hold_end = s->end; - in_uint16_le(s, header_type); - in_uint16_le(s, length); - g_writeln("type 0x%2.2x len %d", header_type, length); - if (length < 4) - { - g_writeln("xrdp_rdp_parse_client_mcs_data: parse error"); - return 1; - } - if (!s_check_rem(s, length - 4)) - { - g_writeln("xrdp_rdp_parse_client_mcs_data: parse error"); - return 1; - } - s->end = s->p + length; - switch (header_type) - { - case 0xc001: /* CS_CORE */ - xrdp_rdp_parse_client_mcs_data_CS_CORE(self, s); - break; - case 0xc002: /* CS_SECURITY */ - break; - case 0xc003: /* CS_NET */ - break; - case 0xc004: /* CS_CLUSTER */ - break; - default: - g_writeln("xrdp_rdp_parse_client_mcs_data: error unknown " - "header type 0x%4.4x", header_type); - break; - } - s->p = hold_p + length; - s->end = hold_end; - } - if (self->client_info.max_bpp > 0) - { - if (self->client_info.bpp > self->client_info.max_bpp) - { - g_writeln("xrdp_rdp_parse_client_mcs_data: client asked for %dbpp " - "connection but configuration is limited to %dbpp", - self->client_info.bpp, self->client_info.max_bpp); - self->client_info.bpp = self->client_info.max_bpp; - } - } - s->p = s->data; - - g_writeln("xrdp_rdp_parse_client_mcs_data: client bpp %d", - self->client_info.bpp); - - return 0; -} - /*****************************************************************************/ int APP_CC xrdp_rdp_incoming(struct xrdp_rdp *self) @@ -718,19 +496,15 @@ xrdp_rdp_incoming(struct xrdp_rdp *self) { return 1; } - self->mcs_channel = self->sec_layer->mcs_layer->userid + MCS_USERCHANNEL_BASE; - xrdp_rdp_parse_client_mcs_data(self); DEBUG(("out xrdp_rdp_incoming mcs channel %d", self->mcs_channel)); - g_strncpy(self->client_info.client_addr, self->sec_layer->mcs_layer->iso_layer->tcp_layer->trans->addr, sizeof(self->client_info.client_addr) - 1); g_strncpy(self->client_info.client_port, self->sec_layer->mcs_layer->iso_layer->tcp_layer->trans->port, sizeof(self->client_info.client_port) - 1); - return 0; } diff --git a/libxrdp/xrdp_sec.c b/libxrdp/xrdp_sec.c index 71cb513f..693407a5 100644 --- a/libxrdp/xrdp_sec.c +++ b/libxrdp/xrdp_sec.c @@ -21,6 +21,12 @@ #include "libxrdp.h" #include "log.h" +#define LOG_LEVEL 1 +#define LLOG(_level, _args) \ + do { if (_level < LOG_LEVEL) { g_write _args ; } } while (0) +#define LLOGLN(_level, _args) \ + do { if (_level < LOG_LEVEL) { g_writeln _args ; } } while (0) + /* some compilers need unsigned char to avoid warnings */ static tui8 g_pad_54[40] = { @@ -100,6 +106,83 @@ static tui8 g_lic3[20] = 0xf3, 0x99, 0x00, 0x00 }; +static const tui8 g_fips_reverse_table[256] = +{ + 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, + 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, + 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, + 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, + 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, + 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, + 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, + 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, + 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, + 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, + 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, + 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, + 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, + 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, + 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, + 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, + 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, + 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, + 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, + 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, + 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, + 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, + 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, + 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, + 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, + 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, + 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, + 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, + 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, + 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, + 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, + 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff +}; + +static const tui8 g_fips_oddparity_table[256] = +{ + 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07, + 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e, + 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16, + 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f, + 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26, + 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f, + 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37, + 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e, + 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46, + 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f, + 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57, + 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e, + 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67, + 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e, + 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76, + 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f, + 0x80, 0x80, 0x83, 0x83, 0x85, 0x85, 0x86, 0x86, + 0x89, 0x89, 0x8a, 0x8a, 0x8c, 0x8c, 0x8f, 0x8f, + 0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97, + 0x98, 0x98, 0x9b, 0x9b, 0x9d, 0x9d, 0x9e, 0x9e, + 0xa1, 0xa1, 0xa2, 0xa2, 0xa4, 0xa4, 0xa7, 0xa7, + 0xa8, 0xa8, 0xab, 0xab, 0xad, 0xad, 0xae, 0xae, + 0xb0, 0xb0, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb6, + 0xb9, 0xb9, 0xba, 0xba, 0xbc, 0xbc, 0xbf, 0xbf, + 0xc1, 0xc1, 0xc2, 0xc2, 0xc4, 0xc4, 0xc7, 0xc7, + 0xc8, 0xc8, 0xcb, 0xcb, 0xcd, 0xcd, 0xce, 0xce, + 0xd0, 0xd0, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd6, + 0xd9, 0xd9, 0xda, 0xda, 0xdc, 0xdc, 0xdf, 0xdf, + 0xe0, 0xe0, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe6, + 0xe9, 0xe9, 0xea, 0xea, 0xec, 0xec, 0xef, 0xef, + 0xf1, 0xf1, 0xf2, 0xf2, 0xf4, 0xf4, 0xf7, 0xf7, + 0xf8, 0xf8, 0xfb, 0xfb, 0xfd, 0xfd, 0xfe, 0xfe +}; + +static const tui8 g_fips_ivec[8] = +{ + 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF +}; + /*****************************************************************************/ static void APP_CC hex_str_to_bin(char *in, char *out, int out_len) @@ -145,8 +228,8 @@ xrdp_sec_create(struct xrdp_rdp *owner, struct trans *trans, int crypt_level, DEBUG((" in xrdp_sec_create")); self = (struct xrdp_sec *)g_malloc(sizeof(struct xrdp_sec), 1); self->rdp_layer = owner; - self->crypt_method = CRYPT_METHOD_40BIT; - self->crypt_level = CRYPT_LEVEL_LOW; + self->crypt_method = CRYPT_METHOD_NONE; + self->crypt_level = CRYPT_LEVEL_NONE; switch (crypt_level) { case 1: /* low */ @@ -207,6 +290,8 @@ xrdp_sec_delete(struct xrdp_sec *self) xrdp_mcs_delete(self->mcs_layer); ssl_rc4_info_delete(self->decrypt_rc4_info); /* TODO clear all data */ ssl_rc4_info_delete(self->encrypt_rc4_info); /* TODO clear all data */ + ssl_des3_info_delete(self->decrypt_fips_info); + ssl_des3_info_delete(self->encrypt_fips_info); g_free(self->client_mcs_data.data); g_free(self->server_mcs_data.data); /* Crypto information must always be cleared */ @@ -224,7 +309,11 @@ xrdp_sec_init(struct xrdp_sec *self, struct stream *s) return 1; } - if (self->crypt_level > 1) + if (self->crypt_level == CRYPT_LEVEL_FIPS) + { + s_push_layer(s, sec_hdr, 4 + 4 + 8); + } + else if (self->crypt_level > CRYPT_LEVEL_LOW) { s_push_layer(s, sec_hdr, 4 + 8); } @@ -288,34 +377,48 @@ xrdp_sec_update(char *key, char *update_key, int key_len) static void APP_CC xrdp_sec_decrypt(struct xrdp_sec *self, char *data, int len) { - if (self->decrypt_use_count == 4096) + if (self->crypt_method == CRYPT_METHOD_FIPS) { - xrdp_sec_update(self->decrypt_key, self->decrypt_update_key, - self->rc4_key_len); - ssl_rc4_set_key(self->decrypt_rc4_info, self->decrypt_key, - self->rc4_key_len); - self->decrypt_use_count = 0; + ssl_des3_decrypt(self->decrypt_fips_info, len, data, data); } + else + { + if (self->decrypt_use_count == 4096) + { + xrdp_sec_update(self->decrypt_key, self->decrypt_update_key, + self->rc4_key_len); + ssl_rc4_set_key(self->decrypt_rc4_info, self->decrypt_key, + self->rc4_key_len); + self->decrypt_use_count = 0; + } - ssl_rc4_crypt(self->decrypt_rc4_info, data, len); - self->decrypt_use_count++; + ssl_rc4_crypt(self->decrypt_rc4_info, data, len); + self->decrypt_use_count++; + } } /*****************************************************************************/ static void APP_CC xrdp_sec_encrypt(struct xrdp_sec *self, char *data, int len) { - if (self->encrypt_use_count == 4096) + if (self->crypt_method == CRYPT_METHOD_FIPS) { - xrdp_sec_update(self->encrypt_key, self->encrypt_update_key, - self->rc4_key_len); - ssl_rc4_set_key(self->encrypt_rc4_info, self->encrypt_key, - self->rc4_key_len); - self->encrypt_use_count = 0; + ssl_des3_encrypt(self->encrypt_fips_info, len, data, data); } + else + { + if (self->encrypt_use_count == 4096) + { + xrdp_sec_update(self->encrypt_key, self->encrypt_update_key, + self->rc4_key_len); + ssl_rc4_set_key(self->encrypt_rc4_info, self->encrypt_key, + self->rc4_key_len); + self->encrypt_use_count = 0; + } - ssl_rc4_crypt(self->encrypt_rc4_info, data, len); - self->encrypt_use_count++; + ssl_rc4_crypt(self->encrypt_rc4_info, data, len); + self->encrypt_use_count++; + } } /*****************************************************************************/ @@ -698,6 +801,50 @@ xrdp_sec_hash_16(char *out, char *in, char *salt1, char *salt2) ssl_md5_info_delete(md5_info); } +/*****************************************************************************/ +static void APP_CC +fips_expand_key_bits(const char *in, char *out) +{ + tui8 buf[32]; + tui8 c; + int i; + int b; + int p; + int r; + + /* reverse every byte in the key */ + for (i = 0; i < 21; i++) + { + c = in[i]; + buf[i] = g_fips_reverse_table[c]; + } + /* insert a zero-bit after every 7th bit */ + for (i = 0, b = 0; i < 24; i++, b += 7) + { + p = b / 8; + r = b % 8; + if (r == 0) + { + out[i] = buf[p] & 0xfe; + } + else + { + /* c is accumulator */ + c = buf[p] << r; + c |= buf[p + 1] >> (8 - r); + out[i] = c & 0xfe; + } + } + /* reverse every byte */ + /* alter lsb so the byte has odd parity */ + for (i = 0; i < 24; i++) + { + c = out[i]; + c = g_fips_reverse_table[c]; + out[i] = g_fips_oddparity_table[c]; + } +} + /*****************************************************************************/ static void APP_CC xrdp_sec_establish_keys(struct xrdp_sec *self) @@ -705,6 +852,46 @@ xrdp_sec_establish_keys(struct xrdp_sec *self) char session_key[48]; char temp_hash[48]; char input[48]; + char client_encrypt_key[32]; + char client_decrypt_key[32]; + const char *fips_ivec; + void *sha1; + + if (self->crypt_method == CRYPT_METHOD_FIPS) + { + LLOGLN(0, ("xrdp_sec_establish_keys: fips")); + sha1 = ssl_sha1_info_create(); + ssl_sha1_transform(sha1, self->client_random + 16, 16); + ssl_sha1_transform(sha1, self->server_random + 16, 16); + ssl_sha1_complete(sha1, client_encrypt_key); + client_encrypt_key[20] = client_encrypt_key[0]; + fips_expand_key_bits(client_encrypt_key, self->fips_encrypt_key); + ssl_sha1_info_delete(sha1); + + sha1 = ssl_sha1_info_create(); + ssl_sha1_transform(sha1, self->client_random, 16); + ssl_sha1_transform(sha1, self->server_random, 16); + ssl_sha1_complete(sha1, client_decrypt_key); + client_decrypt_key[20] = client_decrypt_key[0]; + fips_expand_key_bits(client_decrypt_key, self->fips_decrypt_key); + ssl_sha1_info_delete(sha1); + + sha1 = ssl_sha1_info_create(); + ssl_sha1_transform(sha1, client_decrypt_key, 20); + ssl_sha1_transform(sha1, client_encrypt_key, 20); + ssl_sha1_complete(sha1, self->fips_sign_key); + ssl_sha1_info_delete(sha1); + + fips_ivec = (const char *) g_fips_ivec; + self->encrypt_fips_info = + ssl_des3_encrypt_info_create(self->fips_encrypt_key, fips_ivec); + self->decrypt_fips_info = + ssl_des3_encrypt_info_create(self->fips_decrypt_key, fips_ivec); + } + else + { + LLOGLN(0, ("xrdp_sec_establish_keys: not fips")); + } g_memcpy(input, self->client_random, 24); g_memcpy(input + 24, self->server_random, 24); @@ -761,12 +948,24 @@ xrdp_sec_recv(struct xrdp_sec *self, struct stream *s, int *chan) if (flags & SEC_ENCRYPT) /* 0x08 */ { - if (!s_check_rem(s, 8)) + if (self->crypt_method == CRYPT_METHOD_FIPS) { - return 1; + if (!s_check_rem(s, 12)) + { + return 1; + } + in_uint8s(s, 12); /* len(2), version(1), pad(1), signature(8) */ + xrdp_sec_decrypt(self, s->p, (int)(s->end - s->p)); + } + else + { + if (!s_check_rem(s, 8)) + { + return 1; + } + in_uint8s(s, 8); /* signature(8) */ + xrdp_sec_decrypt(self, s->p, (int)(s->end - s->p)); } - in_uint8s(s, 8); /* signature */ - xrdp_sec_decrypt(self, s->p, (int)(s->end - s->p)); } if (flags & SEC_CLIENT_RANDOM) /* 0x01 */ @@ -884,7 +1083,10 @@ xrdp_sec_send(struct xrdp_sec *self, struct stream *s, int chan) DEBUG((" in xrdp_sec_send")); s_pop_layer(s, sec_hdr); - if (self->crypt_level > 1) + if (self->crypt_level == CRYPT_LEVEL_FIPS) + { + } + else if (self->crypt_level > CRYPT_LEVEL_LOW) { out_uint32_le(s, SEC_ENCRYPT); datalen = (int)((s->end - s->p) - 8); @@ -905,6 +1107,235 @@ xrdp_sec_send(struct xrdp_sec *self, struct stream *s, int chan) return 0; } +/*****************************************************************************/ +/* http://msdn.microsoft.com/en-us/library/cc240510.aspx + 2.2.1.3.2 Client Core Data (TS_UD_CS_CORE) */ +static int APP_CC +xrdp_sec_process_mcs_data_CS_CORE(struct xrdp_sec* self, struct stream* s) +{ + int colorDepth; + int postBeta2ColorDepth; + int highColorDepth; + int supportedColorDepths; + int earlyCapabilityFlags; + + in_uint8s(s, 4); /* version */ + in_uint16_le(s, self->rdp_layer->client_info.width); + in_uint16_le(s, self->rdp_layer->client_info.height); + in_uint16_le(s, colorDepth); + g_writeln("colorDepth 0x%4.4x (0xca00 4bpp 0xca01 8bpp)", colorDepth); + switch (colorDepth) + { + case 0xca00: /* RNS_UD_COLOR_4BPP */ + self->rdp_layer->client_info.bpp = 4; + break; + case 0xca01: /* RNS_UD_COLOR_8BPP */ + self->rdp_layer->client_info.bpp = 8; + break; + } + in_uint8s(s, 2); /* SASSequence */ + in_uint8s(s, 4); /* keyboardLayout */ + in_uint8s(s, 4); /* clientBuild */ + in_uint8s(s, 32); /* clientName */ + in_uint8s(s, 4); /* keyboardType */ + in_uint8s(s, 4); /* keyboardSubType */ + in_uint8s(s, 4); /* keyboardFunctionKey */ + in_uint8s(s, 64); /* imeFileName */ + in_uint16_le(s, postBeta2ColorDepth); + g_writeln("postBeta2ColorDepth 0x%4.4x (0xca00 4bpp 0xca01 8bpp " + "0xca02 15bpp 0xca03 16bpp 0xca04 24bpp)", postBeta2ColorDepth); + + switch (postBeta2ColorDepth) + { + case 0xca00: /* RNS_UD_COLOR_4BPP */ + self->rdp_layer->client_info.bpp = 4; + break; + case 0xca01: /* RNS_UD_COLOR_8BPP */ + self->rdp_layer->client_info.bpp = 8; + break; + case 0xca02: /* RNS_UD_COLOR_16BPP_555 */ + self->rdp_layer->client_info.bpp = 15; + break; + case 0xca03: /* RNS_UD_COLOR_16BPP_565 */ + self->rdp_layer->client_info.bpp = 16; + break; + case 0xca04: /* RNS_UD_COLOR_24BPP */ + self->rdp_layer->client_info.bpp = 24; + break; + } + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint8s(s, 2); /* clientProductId */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* serialNumber */ + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint16_le(s, highColorDepth); + g_writeln("highColorDepth 0x%4.4x (0x0004 4bpp 0x0008 8bpp 0x000f 15bpp " + "0x0010 16 bpp 0x0018 24bpp)", highColorDepth); + self->rdp_layer->client_info.bpp = highColorDepth; + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint16_le(s, supportedColorDepths); + g_writeln("supportedColorDepths 0x%4.4x (0x0001 24bpp 0x0002 16bpp " + "0x0004 15bpp 0x0008 32bpp)", supportedColorDepths); + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint16_le(s, earlyCapabilityFlags); + self->rdp_layer->client_info.mcs_early_capability_flags = earlyCapabilityFlags; + g_writeln("earlyCapabilityFlags 0x%4.4x (0x0002 want32)", + earlyCapabilityFlags); + if ((earlyCapabilityFlags & 0x0002) && (supportedColorDepths & 0x0008)) + { + self->rdp_layer->client_info.bpp = 32; + } + + if (!s_check_rem(s, 64)) + { + return 0; + } + in_uint8s(s, 64); /* clientDigProductId */ + + if (!s_check_rem(s, 1)) + { + return 0; + } + in_uint8(s, self->rdp_layer->client_info.mcs_connection_type); /* connectionType */ + g_writeln("got client client connection type 0x%8.8x", + self->rdp_layer->client_info.mcs_connection_type); + + if (!s_check_rem(s, 1)) + { + return 0; + } + in_uint8s(s, 1); /* pad1octet */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* serverSelectedProtocol */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* desktopPhysicalWidth */ + + if (!s_check_rem(s, 4)) + { + return 0; + } + in_uint8s(s, 4); /* desktopPhysicalHeight */ + + if (!s_check_rem(s, 2)) + { + return 0; + } + in_uint8s(s, 2); /* reserved */ + + return 0; +} + +/*****************************************************************************/ +static int APP_CC +xrdp_sec_process_mcs_data_CS_SECURITY(struct xrdp_sec *self, struct stream* s) +{ + int crypt_method; + int found; + + g_writeln("xrdp_sec_process_mcs_data_CS_SECURITY:"); + in_uint32_le(s, crypt_method); + if (crypt_method & CRYPT_METHOD_40BIT) + { + g_writeln(" client supports 40 bit encryption"); + } + if (crypt_method & CRYPT_METHOD_128BIT) + { + g_writeln(" client supports 128 bit encryption"); + } + if (crypt_method & CRYPT_METHOD_56BIT) + { + g_writeln(" client supports 56 bit encryption"); + } + if (crypt_method & CRYPT_METHOD_FIPS) + { + g_writeln(" client supports fips encryption"); + } + found = 0; + if ((found == 0) && + (self->crypt_method & CRYPT_METHOD_FIPS) && + (self->crypt_level == CRYPT_LEVEL_FIPS)) + { + if (crypt_method & CRYPT_METHOD_FIPS) + { + g_writeln(" client and server support fips, using fips"); + self->crypt_method = CRYPT_METHOD_FIPS; + self->crypt_level = CRYPT_LEVEL_FIPS; + found = 1; + } + } + if ((found == 0) && + (self->crypt_method & CRYPT_METHOD_128BIT) && + (self->crypt_level == CRYPT_LEVEL_HIGH)) + { + if (crypt_method & CRYPT_METHOD_128BIT) + { + g_writeln(" client and server support high crypt, using " + "high crypt"); + self->crypt_method = CRYPT_METHOD_128BIT; + self->crypt_level = CRYPT_LEVEL_HIGH; + found = 1; + } + } + if ((found == 0) && + (self->crypt_method & CRYPT_METHOD_40BIT) && + (self->crypt_level == CRYPT_LEVEL_CLIENT_COMPATIBLE)) + { + if (crypt_method & CRYPT_METHOD_40BIT) + { + g_writeln(" client and server support medium crypt, using " + "medium crypt"); + self->crypt_method = CRYPT_METHOD_40BIT; + self->crypt_level = CRYPT_LEVEL_CLIENT_COMPATIBLE; + found = 1; + } + } + if ((found == 0) && + (self->crypt_method & CRYPT_METHOD_40BIT) && + (self->crypt_level == CRYPT_LEVEL_LOW)) + { + if (crypt_method & CRYPT_METHOD_40BIT) + { + g_writeln(" client and server support low crypt, using " + "low crypt"); + self->crypt_method = CRYPT_METHOD_40BIT; + self->crypt_level = CRYPT_LEVEL_LOW; + found = 1; + } + } + if (found == 0) + { + g_writeln(" no security"); + } + return 0; +} + /*****************************************************************************/ /* this adds the mcs channels in the list of channels to be used when creating the server mcs data */ @@ -954,6 +1385,7 @@ xrdp_sec_process_mcs_data_channels(struct xrdp_sec *self, struct stream *s) return 0; } + /*****************************************************************************/ /* reads the client monitors data */ static int APP_CC @@ -1009,6 +1441,7 @@ xrdp_sec_process_mcs_data_monitors(struct xrdp_sec *self, struct stream *s) } return 0; } + /*****************************************************************************/ /* process client mcs data, we need some things in here to create the server mcs data */ @@ -1038,39 +1471,70 @@ xrdp_sec_process_mcs_data(struct xrdp_sec *self) if (size < 4 || !s_check_rem(s, size - 4)) { - g_writeln("error in xrdp_sec_process_mcs_data tag %d size %d", - tag, size); + LLOGLN(0, ("error in xrdp_sec_process_mcs_data tag %d size %d", + tag, size)); break; } + LLOGLN(10, ("xrdp_sec_process_mcs_data: 0x%8.8x", tag)); switch (tag) { - case SEC_TAG_CLI_INFO: + case SEC_TAG_CLI_INFO: /* CS_CORE 0xC001 */ + if (xrdp_sec_process_mcs_data_CS_CORE(self, s) != 0) + { + return 1; + } break; - case SEC_TAG_CLI_CRYPT: + case SEC_TAG_CLI_CRYPT: /* CS_SECURITY 0xC002 */ + if (xrdp_sec_process_mcs_data_CS_SECURITY(self, s) != 0) + { + return 1; + } break; - case SEC_TAG_CLI_CHANNELS: + case SEC_TAG_CLI_CHANNELS: /* CS_NET 0xC003 */ if (xrdp_sec_process_mcs_data_channels(self, s) != 0) { return 1; } break; - case SEC_TAG_CLI_4: + case SEC_TAG_CLI_4: /* CS_CLUSTER 0xC004 */ break; - case SEC_TAG_CLI_MONITOR: + case SEC_TAG_CLI_MONITOR: /* CS_MONITOR 0xC005 */ if (xrdp_sec_process_mcs_data_monitors(self, s) != 0) { return 1; } break; + /* CS_MCS_MSGCHANNEL 0xC006 + CS_MONITOR_EX 0xC008 + CS_MULTITRANSPORT 0xC00A + SC_CORE 0x0C01 + SC_SECURITY 0x0C02 + SC_NET 0x0C03 + SC_MCS_MSGCHANNEL 0x0C04 + SC_MULTITRANSPORT 0x0C08 */ default: - g_writeln("error unknown xrdp_sec_process_mcs_data tag %d size %d", - tag, size); + LLOGLN(0, ("error unknown xrdp_sec_process_mcs_data tag %d " + "size %d", tag, size)); break; } s->p = hold_p + size; } + + if (self->rdp_layer->client_info.max_bpp > 0) + { + if (self->rdp_layer->client_info.bpp > + self->rdp_layer->client_info.max_bpp) + { + LLOGLN(0, ("xrdp_rdp_parse_client_mcs_data: client asked " + "for %dbpp connection but configuration is limited " + "to %dbpp", self->rdp_layer->client_info.bpp, + self->rdp_layer->client_info.max_bpp)); + self->rdp_layer->client_info.bpp = + self->rdp_layer->client_info.max_bpp; + } + } /* set p to beginning */ s->p = s->data; -- cgit v1.2.3 From 950ef882f434bae754e4221f79ac4002b77556b5 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Sun, 23 Feb 2014 20:45:59 -0800 Subject: libxrdp: work on fips --- libxrdp/libxrdp.h | 1 + libxrdp/xrdp_rdp.c | 5 +- libxrdp/xrdp_sec.c | 221 ++++++++++++++++++++++++++++++++++------------------- 3 files changed, 148 insertions(+), 79 deletions(-) (limited to 'libxrdp/xrdp_rdp.c') diff --git a/libxrdp/libxrdp.h b/libxrdp/libxrdp.h index 9ae839e6..07c166e6 100644 --- a/libxrdp/libxrdp.h +++ b/libxrdp/libxrdp.h @@ -121,6 +121,7 @@ struct xrdp_sec char fips_sign_key[20]; void* encrypt_fips_info; void* decrypt_fips_info; + void* sign_fips_info; }; /* channel */ diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index b357e954..0b7678d1 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -305,7 +305,10 @@ xrdp_rdp_recv(struct xrdp_rdp *self, struct stream *s, int *code) } else { - g_writeln("Wrong channel Id to be handled by xrdp_channel_process %d", chan); + if (chan != 1) + { + g_writeln("Wrong channel Id to be handled by xrdp_channel_process %d", chan); + } } s->next_packet = 0; diff --git a/libxrdp/xrdp_sec.c b/libxrdp/xrdp_sec.c index 693407a5..c1768022 100644 --- a/libxrdp/xrdp_sec.c +++ b/libxrdp/xrdp_sec.c @@ -292,6 +292,7 @@ xrdp_sec_delete(struct xrdp_sec *self) ssl_rc4_info_delete(self->encrypt_rc4_info); /* TODO clear all data */ ssl_des3_info_delete(self->decrypt_fips_info); ssl_des3_info_delete(self->encrypt_fips_info); + ssl_hmac_info_delete(self->sign_fips_info); g_free(self->client_mcs_data.data); g_free(self->server_mcs_data.data); /* Crypto information must always be cleared */ @@ -373,52 +374,57 @@ xrdp_sec_update(char *key, char *update_key, int key_len) return 0; } +/*****************************************************************************/ +static void APP_CC +xrdp_sec_fips_decrypt(struct xrdp_sec *self, char *data, int len) +{ + LLOGLN(10, ("xrdp_sec_fips_decrypt:")); + ssl_des3_decrypt(self->decrypt_fips_info, len, data, data); + self->decrypt_use_count++; +} + /*****************************************************************************/ static void APP_CC xrdp_sec_decrypt(struct xrdp_sec *self, char *data, int len) { - if (self->crypt_method == CRYPT_METHOD_FIPS) + LLOGLN(10, ("xrdp_sec_decrypt:")); + if (self->decrypt_use_count == 4096) { - ssl_des3_decrypt(self->decrypt_fips_info, len, data, data); + xrdp_sec_update(self->decrypt_key, self->decrypt_update_key, + self->rc4_key_len); + ssl_rc4_set_key(self->decrypt_rc4_info, self->decrypt_key, + self->rc4_key_len); + self->decrypt_use_count = 0; } - else - { - if (self->decrypt_use_count == 4096) - { - xrdp_sec_update(self->decrypt_key, self->decrypt_update_key, - self->rc4_key_len); - ssl_rc4_set_key(self->decrypt_rc4_info, self->decrypt_key, - self->rc4_key_len); - self->decrypt_use_count = 0; - } + ssl_rc4_crypt(self->decrypt_rc4_info, data, len); + self->decrypt_use_count++; +} - ssl_rc4_crypt(self->decrypt_rc4_info, data, len); - self->decrypt_use_count++; - } +/*****************************************************************************/ +static void APP_CC +xrdp_sec_fips_encrypt(struct xrdp_sec *self, char *data, int len) +{ + LLOGLN(10, ("xrdp_sec_fips_encrypt:")); + ssl_des3_encrypt(self->encrypt_fips_info, len, data, data); + self->encrypt_use_count++; } /*****************************************************************************/ static void APP_CC xrdp_sec_encrypt(struct xrdp_sec *self, char *data, int len) { - if (self->crypt_method == CRYPT_METHOD_FIPS) + LLOGLN(10, ("xrdp_sec_encrypt:")); + if (self->encrypt_use_count == 4096) { - ssl_des3_encrypt(self->encrypt_fips_info, len, data, data); + xrdp_sec_update(self->encrypt_key, self->encrypt_update_key, + self->rc4_key_len); + ssl_rc4_set_key(self->encrypt_rc4_info, self->encrypt_key, + self->rc4_key_len); + self->encrypt_use_count = 0; } - else - { - if (self->encrypt_use_count == 4096) - { - xrdp_sec_update(self->encrypt_key, self->encrypt_update_key, - self->rc4_key_len); - ssl_rc4_set_key(self->encrypt_rc4_info, self->encrypt_key, - self->rc4_key_len); - self->encrypt_use_count = 0; - } - ssl_rc4_crypt(self->encrypt_rc4_info, data, len); - self->encrypt_use_count++; - } + ssl_rc4_crypt(self->encrypt_rc4_info, data, len); + self->encrypt_use_count++; } /*****************************************************************************/ @@ -487,6 +493,8 @@ xrdp_sec_process_logon_info(struct xrdp_sec *self, struct stream *s) { /* must be or error */ DEBUG(("xrdp_sec_process_logon_info: flags wrong, major error")); + LLOGLN(0, ("xrdp_sec_process_logon_info: flags wrong, likely decrypt " + "not working")); return 1; } @@ -663,8 +671,9 @@ xrdp_sec_process_logon_info(struct xrdp_sec *self, struct stream *s) static int APP_CC xrdp_sec_send_lic_initial(struct xrdp_sec *self) { - struct stream *s = (struct stream *)NULL; + struct stream *s; + LLOGLN(10, ("xrdp_sec_send_lic_initial:")); make_stream(s); init_stream(s, 8192); @@ -845,53 +854,60 @@ fips_expand_key_bits(const char *in, char *out) } } -/*****************************************************************************/ +/****************************************************************************/ +static void APP_CC +xrdp_sec_fips_establish_keys(struct xrdp_sec *self) +{ + char server_encrypt_key[32]; + char server_decrypt_key[32]; + const char *fips_ivec; + void *sha1; + + LLOGLN(0, ("xrdp_sec_fips_establish_keys:")); + + sha1 = ssl_sha1_info_create(); + ssl_sha1_clear(sha1); + ssl_sha1_transform(sha1, self->client_random + 16, 16); + ssl_sha1_transform(sha1, self->server_random + 16, 16); + ssl_sha1_complete(sha1, server_decrypt_key); + + server_decrypt_key[20] = server_decrypt_key[0]; + fips_expand_key_bits(server_decrypt_key, self->fips_decrypt_key); + ssl_sha1_info_delete(sha1); + + sha1 = ssl_sha1_info_create(); + ssl_sha1_clear(sha1); + ssl_sha1_transform(sha1, self->client_random, 16); + ssl_sha1_transform(sha1, self->server_random, 16); + ssl_sha1_complete(sha1, server_encrypt_key); + server_encrypt_key[20] = server_encrypt_key[0]; + fips_expand_key_bits(server_encrypt_key, self->fips_encrypt_key); + ssl_sha1_info_delete(sha1); + + sha1 = ssl_sha1_info_create(); + ssl_sha1_clear(sha1); + ssl_sha1_transform(sha1, server_encrypt_key, 20); + ssl_sha1_transform(sha1, server_decrypt_key, 20); + ssl_sha1_complete(sha1, self->fips_sign_key); + ssl_sha1_info_delete(sha1); + + fips_ivec = (const char *) g_fips_ivec; + self->encrypt_fips_info = + ssl_des3_encrypt_info_create(self->fips_encrypt_key, fips_ivec); + self->decrypt_fips_info = + ssl_des3_decrypt_info_create(self->fips_decrypt_key, fips_ivec); + self->sign_fips_info = ssl_hmac_info_create(); +} + +/****************************************************************************/ static void APP_CC xrdp_sec_establish_keys(struct xrdp_sec *self) { char session_key[48]; char temp_hash[48]; char input[48]; - char client_encrypt_key[32]; - char client_decrypt_key[32]; - const char *fips_ivec; - void *sha1; - if (self->crypt_method == CRYPT_METHOD_FIPS) - { - LLOGLN(0, ("xrdp_sec_establish_keys: fips")); - sha1 = ssl_sha1_info_create(); - ssl_sha1_transform(sha1, self->client_random + 16, 16); - ssl_sha1_transform(sha1, self->server_random + 16, 16); - ssl_sha1_complete(sha1, client_encrypt_key); - client_encrypt_key[20] = client_encrypt_key[0]; - fips_expand_key_bits(client_encrypt_key, self->fips_encrypt_key); - ssl_sha1_info_delete(sha1); - - sha1 = ssl_sha1_info_create(); - ssl_sha1_transform(sha1, self->client_random, 16); - ssl_sha1_transform(sha1, self->server_random, 16); - ssl_sha1_complete(sha1, client_decrypt_key); - client_decrypt_key[20] = client_decrypt_key[0]; - fips_expand_key_bits(client_decrypt_key, self->fips_decrypt_key); - ssl_sha1_info_delete(sha1); - - sha1 = ssl_sha1_info_create(); - ssl_sha1_transform(sha1, client_decrypt_key, 20); - ssl_sha1_transform(sha1, client_encrypt_key, 20); - ssl_sha1_complete(sha1, self->fips_sign_key); - ssl_sha1_info_delete(sha1); - - fips_ivec = (const char *) g_fips_ivec; - self->encrypt_fips_info = - ssl_des3_encrypt_info_create(self->fips_encrypt_key, fips_ivec); - self->decrypt_fips_info = - ssl_des3_encrypt_info_create(self->fips_decrypt_key, fips_ivec); - } - else - { - LLOGLN(0, ("xrdp_sec_establish_keys: not fips")); - } + LLOGLN(0, ("xrdp_sec_establish_keys:")); g_memcpy(input, self->client_random, 24); g_memcpy(input + 24, self->server_random, 24); @@ -930,6 +946,8 @@ xrdp_sec_recv(struct xrdp_sec *self, struct stream *s, int *chan) { int flags; int len; + int ver; + int pad; DEBUG((" in xrdp_sec_recv")); @@ -954,8 +972,14 @@ xrdp_sec_recv(struct xrdp_sec *self, struct stream *s, int *chan) { return 1; } - in_uint8s(s, 12); /* len(2), version(1), pad(1), signature(8) */ - xrdp_sec_decrypt(self, s->p, (int)(s->end - s->p)); + in_uint16_le(s, len); + in_uint8(s, ver); + in_uint8(s, pad); + LLOGLN(10, ("xrdp_sec_recv: len %d ver %d pad %d", len, ver, pad)); + in_uint8s(s, 8); /* signature(8) */ + LLOGLN(10, ("xrdp_sec_recv: data len %d", (int)(s->end - s->p))); + xrdp_sec_fips_decrypt(self, s->p, (int)(s->end - s->p)); + s->end -= pad; } else { @@ -978,7 +1002,14 @@ xrdp_sec_recv(struct xrdp_sec *self, struct stream *s, int *chan) in_uint8a(s, self->client_crypt_random, 64); xrdp_sec_rsa_op(self->client_random, self->client_crypt_random, self->pub_mod, self->pri_exp); - xrdp_sec_establish_keys(self); + if (self->crypt_method == CRYPT_METHOD_FIPS) + { + xrdp_sec_fips_establish_keys(self); + } + else + { + xrdp_sec_establish_keys(self); + } *chan = 1; /* just set a non existing channel and exit */ DEBUG((" out xrdp_sec_recv")); return 0; @@ -1073,18 +1104,51 @@ xrdp_sec_sign(struct xrdp_sec *self, char *out, int out_len, ssl_md5_info_delete(md5_info); } +/*****************************************************************************/ +/* Generate a MAC hash (5.2.3.1), using a combination of SHA1 and MD5 */ +static void APP_CC +xrdp_sec_fips_sign(struct xrdp_sec *self, char *out, int out_len, + char *data, int data_len) +{ + char buf[20]; + char use_count_le[4]; + + use_count_le[0] = (self->encrypt_use_count >> 0) & 0xFF; + use_count_le[1] = (self->encrypt_use_count >> 8) & 0xFF; + use_count_le[2] = (self->encrypt_use_count >> 16) & 0xFF; + use_count_le[3] = (self->encrypt_use_count >> 24) & 0xFF; + ssl_hmac_sha1_init(self->sign_fips_info, self->fips_sign_key, 20); + ssl_hmac_transform(self->sign_fips_info, data, data_len); + ssl_hmac_transform(self->sign_fips_info, use_count_le, 4); + ssl_hmac_complete(self->sign_fips_info, buf, 20); + g_memcpy(out, buf, out_len); +} + /*****************************************************************************/ /* returns error */ int APP_CC xrdp_sec_send(struct xrdp_sec *self, struct stream *s, int chan) { int datalen; + int pad; + LLOGLN(10, ("xrdp_sec_send:")); DEBUG((" in xrdp_sec_send")); s_pop_layer(s, sec_hdr); if (self->crypt_level == CRYPT_LEVEL_FIPS) { + LLOGLN(10, ("xrdp_sec_send: fips")); + out_uint32_le(s, SEC_ENCRYPT); + datalen = (int)((s->end - s->p) - 12); + out_uint16_le(s, 16); /* crypto header size */ + out_uint8(s, 1); /* fips version */ + pad = (8 - (datalen % 8)) & 7; + g_memset(s->end, 0, pad); + s->end += pad; + out_uint8(s, pad); /* fips pad */ + xrdp_sec_fips_sign(self, s->p, 8, s->p + 8, datalen); + xrdp_sec_fips_encrypt(self, s->p + 8, datalen + pad); } else if (self->crypt_level > CRYPT_LEVEL_LOW) { @@ -1303,7 +1367,7 @@ xrdp_sec_process_mcs_data_CS_SECURITY(struct xrdp_sec *self, struct stream* s) found = 1; } } - if ((found == 0) && + if ((found == 0) && (self->crypt_method & CRYPT_METHOD_40BIT) && (self->crypt_level == CRYPT_LEVEL_CLIENT_COMPATIBLE)) { @@ -1316,7 +1380,7 @@ xrdp_sec_process_mcs_data_CS_SECURITY(struct xrdp_sec *self, struct stream* s) found = 1; } } - if ((found == 0) && + if ((found == 0) && (self->crypt_method & CRYPT_METHOD_40BIT) && (self->crypt_level == CRYPT_LEVEL_LOW)) { @@ -1521,7 +1585,7 @@ xrdp_sec_process_mcs_data(struct xrdp_sec *self) s->p = hold_p + size; } - + if (self->rdp_layer->client_info.max_bpp > 0) { if (self->rdp_layer->client_info.bpp > @@ -1722,8 +1786,8 @@ xrdp_sec_incoming(struct xrdp_sec *self) char *value = NULL; char key_file[256]; + LLOGLN(10, ("xrdp_sec_incoming:")); g_memset(key_file, 0, sizeof(char) * 256); - DEBUG((" in xrdp_sec_incoming")); g_random(self->server_random, 32); items = list_create(); @@ -1786,6 +1850,7 @@ xrdp_sec_incoming(struct xrdp_sec *self) { return 1; } + LLOGLN(10, ("xrdp_sec_incoming: out")); return 0; } -- cgit v1.2.3 From e9e14f22f9eb97334bc75327d9f41c9f41a39dcc Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Tue, 25 Feb 2014 20:47:34 -0800 Subject: started work on fastpath, restructure PDU in --- libxrdp/Makefile.am | 1 - libxrdp/libxrdp.c | 59 ++++++++++++++++++--------- libxrdp/libxrdp.h | 9 +---- libxrdp/libxrdpinc.h | 4 +- libxrdp/xrdp_iso.c | 42 ++++++++++--------- libxrdp/xrdp_mcs.c | 21 ++++++---- libxrdp/xrdp_rdp.c | 11 +++-- xrdp/xrdp_process.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 194 insertions(+), 64 deletions(-) (limited to 'libxrdp/xrdp_rdp.c') diff --git a/libxrdp/Makefile.am b/libxrdp/Makefile.am index 389453b1..e83fce10 100644 --- a/libxrdp/Makefile.am +++ b/libxrdp/Makefile.am @@ -55,7 +55,6 @@ libxrdp_la_SOURCES = \ xrdp_orders.c \ xrdp_rdp.c \ xrdp_sec.c \ - xrdp_tcp.c \ xrdp_bitmap_compress.c \ xrdp_bitmap32_compress.c \ xrdp_jpeg_compress.c \ diff --git a/libxrdp/libxrdp.c b/libxrdp/libxrdp.c index ac5bbe98..7ab1f914 100644 --- a/libxrdp/libxrdp.c +++ b/libxrdp/libxrdp.c @@ -32,8 +32,6 @@ libxrdp_init(tbus id, struct trans *trans) session->rdp = xrdp_rdp_create(session, trans); session->orders = xrdp_orders_create(session, (struct xrdp_rdp *)session->rdp); session->client_info = &(((struct xrdp_rdp *)session->rdp)->client_info); - make_stream(session->s); - init_stream(session->s, 8192 * 2); return session; } @@ -48,7 +46,6 @@ libxrdp_exit(struct xrdp_session *session) xrdp_orders_delete((struct xrdp_orders *)session->orders); xrdp_rdp_delete((struct xrdp_rdp *)session->rdp); - free_stream(session->s); g_free(session); return 0; } @@ -69,19 +66,38 @@ libxrdp_process_incomming(struct xrdp_session *session) /******************************************************************************/ int EXPORT_CC -libxrdp_process_data(struct xrdp_session *session) +libxrdp_process_data(struct xrdp_session *session, struct stream *s) { int cont; int rv; int code; int term; int dead_lock_counter; + struct xrdp_rdp *rdp; + struct stream *ls; + + if (session->in_process_data != 0) + { + g_writeln("libxrdp_process_data: error reentry"); + return 1; + } + session->in_process_data++; + + ls = 0; + if (s == 0) + { + make_stream(ls); + init_stream(ls, 8192 * 4); + s = ls; + } term = 0; cont = 1; rv = 0; dead_lock_counter = 0; + rdp = (struct xrdp_rdp *) (session->rdp); + while ((cont || !session->up_and_running) && !term) { if (session->is_term != 0) @@ -94,8 +110,7 @@ libxrdp_process_data(struct xrdp_session *session) code = 0; - if (xrdp_rdp_recv((struct xrdp_rdp *)(session->rdp), - session->s, &code) != 0) + if (xrdp_rdp_recv(rdp, s, &code) != 0) { rv = 1; break; @@ -106,13 +121,14 @@ libxrdp_process_data(struct xrdp_session *session) switch (code) { case -1: - xrdp_rdp_send_demand_active((struct xrdp_rdp *)session->rdp); + xrdp_rdp_send_demand_active(rdp); - // send Monitor Layout PDU for multimon - if (session->client_info->monitorCount > 0 && session->client_info->multimon == 1) + /* send Monitor Layout PDU for multimon */ + if (session->client_info->monitorCount > 0 && + session->client_info->multimon == 1) { DEBUG(("sending monitor layout pdu")); - if (xrdp_rdp_send_monitorlayout((struct xrdp_rdp *)session->rdp) != 0) + if (xrdp_rdp_send_monitorlayout(rdp) != 0) { g_writeln("xrdp_rdp_send_monitorlayout: error"); } @@ -124,13 +140,11 @@ libxrdp_process_data(struct xrdp_session *session) dead_lock_counter++; break; case RDP_PDU_CONFIRM_ACTIVE: /* 3 */ - xrdp_rdp_process_confirm_active((struct xrdp_rdp *)session->rdp, - session->s); + xrdp_rdp_process_confirm_active(rdp, s); break; case RDP_PDU_DATA: /* 7 */ - if (xrdp_rdp_process_data((struct xrdp_rdp *)session->rdp, - session->s) != 0) + if (xrdp_rdp_process_data(rdp, s) != 0) { DEBUG(("libxrdp_process_data returned non zero")); cont = 0; @@ -149,17 +163,24 @@ libxrdp_process_data(struct xrdp_session *session) /*This situation can happen and this is a workaround*/ cont = 0; g_writeln("Serious programming error we were locked in a deadly loop") ; - g_writeln("remaining :%d", session->s->end - session->s->next_packet); - session->s->next_packet = 0; + g_writeln("remaining :%d", s->end - s->next_packet); + s->next_packet = 0; } if (cont) { - cont = (session->s->next_packet != 0) && - (session->s->next_packet < session->s->end); + cont = (s->next_packet != 0) && + (s->next_packet < s->end); } } + if (s == ls) + { + free_stream(s); + } + + session->in_process_data--; + return rv; } @@ -769,7 +790,7 @@ libxrdp_reset(struct xrdp_session *session, /* process till up and running */ session->up_and_running = 0; - if (libxrdp_process_data(session) != 0) + if (libxrdp_process_data(session, 0) != 0) { g_writeln("non handled error from libxrdp_process_data"); } diff --git a/libxrdp/libxrdp.h b/libxrdp/libxrdp.h index 07c166e6..d9e5e6d1 100644 --- a/libxrdp/libxrdp.h +++ b/libxrdp/libxrdp.h @@ -37,20 +37,13 @@ #include "file_loc.h" #include "xrdp_client_info.h" -/* tcp */ -struct xrdp_tcp -{ - struct trans* trans; - struct xrdp_iso* iso_layer; /* owner */ -}; - /* iso */ struct xrdp_iso { struct xrdp_mcs* mcs_layer; /* owner */ - struct xrdp_tcp* tcp_layer; int requestedProtocol; int selectedProtocol; + struct trans* trans; }; /* used in mcs */ diff --git a/libxrdp/libxrdpinc.h b/libxrdp/libxrdpinc.h index e5f52a05..d322eafa 100644 --- a/libxrdp/libxrdpinc.h +++ b/libxrdp/libxrdpinc.h @@ -70,8 +70,8 @@ struct xrdp_session void* orders; struct xrdp_client_info* client_info; int up_and_running; - struct stream* s; int (*is_term)(void); + int in_process_data; /* inc / dec libxrdp_process_data calls */ }; struct xrdp_session* DEFAULT_CC @@ -83,7 +83,7 @@ libxrdp_disconnect(struct xrdp_session* session); int DEFAULT_CC libxrdp_process_incomming(struct xrdp_session* session); int DEFAULT_CC -libxrdp_process_data(struct xrdp_session* session); +libxrdp_process_data(struct xrdp_session* session, struct stream *s); int DEFAULT_CC libxrdp_send_palette(struct xrdp_session* session, int* palette); int DEFAULT_CC diff --git a/libxrdp/xrdp_iso.c b/libxrdp/xrdp_iso.c index 69c242d3..31c279b5 100644 --- a/libxrdp/xrdp_iso.c +++ b/libxrdp/xrdp_iso.c @@ -30,7 +30,7 @@ xrdp_iso_create(struct xrdp_mcs *owner, struct trans *trans) DEBUG((" in xrdp_iso_create")); self = (struct xrdp_iso *)g_malloc(sizeof(struct xrdp_iso), 1); self->mcs_layer = owner; - self->tcp_layer = xrdp_tcp_create(self, trans); + self->trans = trans; DEBUG((" out xrdp_iso_create")); return self; } @@ -44,7 +44,6 @@ xrdp_iso_delete(struct xrdp_iso *self) return; } - xrdp_tcp_delete(self->tcp_layer); g_free(self); } @@ -91,13 +90,21 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code, int *len) { int ver; // TPKT Version int plen; // TPKT PacketLength + int do_read; *code = 0; // X.224 Packet Type *len = 0; // X.224 Length Indicator - if (xrdp_tcp_recv(self->tcp_layer, s, 4) != 0) + /* early in connection sequence, iso needs to do a force read */ + do_read = s != self->trans->in_s; + + if (do_read) { - return 1; + init_stream(s, 4); + if (trans_force_read_s(self->trans, s, 4) != 0) + { + return 1; + } } in_uint8(s, ver); @@ -115,9 +122,13 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code, int *len) return 1; } - if (xrdp_tcp_recv(self->tcp_layer, s, plen - 4) != 0) + if (do_read) { - return 1; + init_stream(s, plen - 4); + if (trans_force_read_s(self->trans, s, plen - 4) != 0) + { + return 1; + } } if (!s_check_rem(s, 2)) @@ -147,6 +158,7 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code, int *len) return 0; } + /*****************************************************************************/ /* returns error */ int APP_CC @@ -177,10 +189,7 @@ xrdp_iso_recv(struct xrdp_iso *self, struct stream *s) static int APP_CC xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s, int code) { - if (xrdp_tcp_init(self->tcp_layer, s) != 0) - { - return 1; - } + init_stream(s, 8192 * 4); /* 32 KB */ /* TPKT HEADER - 4 bytes */ out_uint8(s, 3); /* version */ @@ -217,7 +226,7 @@ xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s, int code) s_mark_end(s); - if (xrdp_tcp_send(self->tcp_layer, s) != 0) + if (trans_force_write_s(self->trans, s) != 0) { return 1; } @@ -228,10 +237,7 @@ xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s, int code) static int APP_CC xrdp_iso_send_rdpnegfailure(struct xrdp_iso *self, struct stream *s, int code, int failureCode) { - if (xrdp_tcp_init(self->tcp_layer, s) != 0) - { - return 1; - } + init_stream(s, 8192 * 4); /* 32 KB */ /* TPKT HEADER - 4 bytes */ out_uint8(s, 3); /* version */ @@ -250,7 +256,7 @@ xrdp_iso_send_rdpnegfailure(struct xrdp_iso *self, struct stream *s, int code, i out_uint32_le(s, failureCode); /* failure code */ s_mark_end(s); - if (xrdp_tcp_send(self->tcp_layer, s) != 0) + if (trans_force_write_s(self->trans, s) != 0) { return 1; } @@ -379,7 +385,7 @@ xrdp_iso_incoming(struct xrdp_iso *self) int APP_CC xrdp_iso_init(struct xrdp_iso *self, struct stream *s) { - xrdp_tcp_init(self->tcp_layer, s); + init_stream(s, 8192 * 4); /* 32 KB */ s_push_layer(s, iso_hdr, 7); return 0; } @@ -401,7 +407,7 @@ xrdp_iso_send(struct xrdp_iso *self, struct stream *s) out_uint8(s, ISO_PDU_DT); out_uint8(s, 0x80); - if (xrdp_tcp_send(self->tcp_layer, s) != 0) + if (trans_force_write_s(self->trans, s) != 0) { return 1; } diff --git a/libxrdp/xrdp_mcs.c b/libxrdp/xrdp_mcs.c index 02b8ee69..9dcb5b51 100644 --- a/libxrdp/xrdp_mcs.c +++ b/libxrdp/xrdp_mcs.c @@ -75,8 +75,8 @@ xrdp_mcs_delete(struct xrdp_mcs *self) } /*****************************************************************************/ -/* This function sends channel join confirm*/ -/* returns error = 1 ok = 0*/ +/* This function sends channel join confirm */ +/* returns error = 1 ok = 0 */ static int APP_CC xrdp_mcs_send_cjcf(struct xrdp_mcs *self, int userid, int chanid) { @@ -151,6 +151,12 @@ xrdp_mcs_recv(struct xrdp_mcs *self, struct stream *s, int *chan) /* this is channels getting added from the client */ if (appid == MCS_CJRQ) { + if (s == self->iso_layer->trans->in_s) + { + /* this should not happen */ + g_writeln("xrdp_mcs_recv: error, MCS_CJRQ at wrong time"); + return 1; + } if (!s_check_rem(s, 4)) { return 1; @@ -165,13 +171,12 @@ xrdp_mcs_recv(struct xrdp_mcs *self, struct stream *s, int *chan) { log_message(LOG_LEVEL_ERROR,"Non handled error from xrdp_mcs_send_cjcf") ; } - continue; } if (appid == MCS_SDRQ || appid == MCS_SDIN) { - break ; + break; } else { @@ -949,12 +954,12 @@ xrdp_mcs_send(struct xrdp_mcs *self, struct stream *s, int chan) void APP_CC close_rdp_socket(struct xrdp_mcs *self) { - if (self->iso_layer->tcp_layer != 0) + if (self->iso_layer != 0) { - if (self->iso_layer->tcp_layer->trans != 0) + if (self->iso_layer->trans != 0) { - g_tcp_close(self->iso_layer->tcp_layer->trans->sck); - self->iso_layer->tcp_layer->trans->sck = 0 ; + g_tcp_close(self->iso_layer->trans->sck); + self->iso_layer->trans->sck = 0 ; g_writeln("xrdp_mcs_disconnect - socket closed"); return; } diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index 0b7678d1..ec882f4e 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -198,8 +198,10 @@ xrdp_rdp_create(struct xrdp_session *session, struct trans *trans) /* read ini settings */ xrdp_rdp_read_config(&self->client_info); /* create sec layer */ - self->sec_layer = xrdp_sec_create(self, trans, self->client_info.crypt_level, - self->client_info.channel_code, self->client_info.multimon); + self->sec_layer = xrdp_sec_create(self, trans, + self->client_info.crypt_level, + self->client_info.channel_code, + self->client_info.multimon); /* default 8 bit v1 color bitmap cache entries and size */ self->client_info.cache1_entries = 600; self->client_info.cache1_size = 256; @@ -503,10 +505,10 @@ xrdp_rdp_incoming(struct xrdp_rdp *self) MCS_USERCHANNEL_BASE; DEBUG(("out xrdp_rdp_incoming mcs channel %d", self->mcs_channel)); g_strncpy(self->client_info.client_addr, - self->sec_layer->mcs_layer->iso_layer->tcp_layer->trans->addr, + self->sec_layer->mcs_layer->iso_layer->trans->addr, sizeof(self->client_info.client_addr) - 1); g_strncpy(self->client_info.client_port, - self->sec_layer->mcs_layer->iso_layer->tcp_layer->trans->port, + self->sec_layer->mcs_layer->iso_layer->trans->port, sizeof(self->client_info.client_port) - 1); return 0; } @@ -1568,6 +1570,7 @@ xrdp_rdp_process_data_font(struct xrdp_rdp *self, struct stream *s) xrdp_rdp_send_fontmap(self); self->session->up_and_running = 1; + g_writeln("yeah, up_and_running"); DEBUG(("up_and_running set")); xrdp_rdp_send_data_update_sync(self); } diff --git a/xrdp/xrdp_process.c b/xrdp/xrdp_process.c index 4c52eaac..78576f1f 100644 --- a/xrdp/xrdp_process.c +++ b/xrdp/xrdp_process.c @@ -61,7 +61,7 @@ xrdp_process_delete(struct xrdp_process *self) /*****************************************************************************/ static int APP_CC -xrdp_process_loop(struct xrdp_process *self) +xrdp_process_loop(struct xrdp_process *self, struct stream *s) { int rv; @@ -69,7 +69,7 @@ xrdp_process_loop(struct xrdp_process *self) if (self->session != 0) { - rv = libxrdp_process_data(self->session); + rv = libxrdp_process_data(self->session, s); } if ((self->wm == 0) && (self->session->up_and_running) && (rv == 0)) @@ -114,18 +114,117 @@ xrdp_process_mod_end(struct xrdp_process *self) return 0; } +/*****************************************************************************/ +static int APP_CC +xrdp_process_get_pdu_bytes(const char *aheader) +{ + int rv; + const tui8 *header; + + rv = -1; + header = (const tui8 *) aheader; + if (header[0] == 0x03) + { + /* TPKT */ + rv = (header[2] << 8) | header[3]; + } + else if (header[0] == 0x30) + { + /* TSRequest (NLA) */ + if (header[1] & 0x80) + { + if ((header[1] & ~(0x80)) == 1) + { + rv = header[2]; + rv += 3; + } + else if ((header[1] & ~(0x80)) == 2) + { + rv = (header[2] << 8) | header[3]; + rv += 4; + } + else + { + g_writeln("xrdp_process_get_packet_bytes: error TSRequest!"); + return -1; + } + } + else + { + rv = header[1]; + rv += 2; + } + } + else + { + /* Fast-Path */ + if (header[1] & 0x80) + { + rv = ((header[1] & 0x7F) << 8) | header[2]; + } + else + { + rv = header[1]; + } + } + return rv; +} + /*****************************************************************************/ static int DEFAULT_CC xrdp_process_data_in(struct trans *self) { struct xrdp_process *pro; + struct stream *s; + int len; DEBUG(("xrdp_process_data_in")); pro = (struct xrdp_process *)(self->callback_data); - if (xrdp_process_loop(pro) != 0) + s = pro->server_trans->in_s; + switch (pro->server_trans->extra_flags) { - return 1; + case 0: + /* early in connection sequence, we're in this mode */ + if (xrdp_process_loop(pro, 0) != 0) + { + g_writeln("xrdp_process_data_in: " + "xrdp_process_loop failed"); + return 1; + } + if (pro->session->up_and_running) + { + pro->server_trans->extra_flags = 1; + pro->server_trans->header_size = 4; + } + break; + + case 1: + /* we have enough now to get the PDU bytes */ + len = xrdp_process_get_pdu_bytes(s->p); + if (len == -1) + { + g_writeln("xrdp_process_data_in: " + "xrdp_process_get_packet_bytes failed"); + return 1; + } + pro->server_trans->header_size = len; + pro->server_trans->extra_flags = 2; + break; + + case 2: + /* the whole PDU is read in now process */ + s->p = s->data; + if (xrdp_process_loop(pro, s) != 0) + { + g_writeln("xrdp_process_data_in: " + "xrdp_process_loop failed"); + return 1; + } + init_stream(s, 0); + pro->server_trans->header_size = 4; + pro->server_trans->extra_flags = 1; + break; } return 0; @@ -145,8 +244,12 @@ xrdp_process_main_loop(struct xrdp_process *self) DEBUG(("xrdp_process_main_loop")); self->status = 1; + self->server_trans->extra_flags = 0; + self->server_trans->header_size = 0; + self->server_trans->no_stream_init_on_data_in = 1; self->server_trans->trans_data_in = xrdp_process_data_in; self->server_trans->callback_data = self; + init_stream(self->server_trans->in_s, 8192 * 4); self->session = libxrdp_init((tbus)self, self->server_trans); /* this callback function is in xrdp_wm.c */ self->session->callback = callback; -- cgit v1.2.3