2014-06-29 00:04:49 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Remote Assistance
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2014-06-29 20:36:28 +04:00
|
|
|
#include <winpr/print.h>
|
2014-06-29 00:04:49 +04:00
|
|
|
#include <winpr/windows.h>
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include <openssl/rc4.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/engine.h>
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2014-06-29 00:04:49 +04:00
|
|
|
#include <freerdp/client/file.h>
|
|
|
|
#include <freerdp/client/cmdline.h>
|
|
|
|
|
2014-06-30 17:40:24 +04:00
|
|
|
#include <freerdp/assistance.h>
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("common")
|
|
|
|
|
2014-06-29 20:36:28 +04:00
|
|
|
/**
|
|
|
|
* Password encryption in establishing a remote assistance session of type 1:
|
|
|
|
* http://blogs.msdn.com/b/openspecification/archive/2011/10/31/password-encryption-in-establishing-a-remote-assistance-session-of-type-1.aspx
|
|
|
|
*
|
|
|
|
* Creation of PassStub for the Remote Assistance Ticket:
|
|
|
|
* http://social.msdn.microsoft.com/Forums/en-US/6316c3f4-ea09-4343-a4a1-9cca46d70d28/creation-of-passstub-for-the-remote-assistance-ticket?forum=os_windowsprotocols
|
|
|
|
*/
|
|
|
|
|
2014-06-29 00:04:49 +04:00
|
|
|
/**
|
|
|
|
* CryptDeriveKey Function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379916/
|
|
|
|
*
|
|
|
|
* Let n be the required derived key length, in bytes.
|
|
|
|
* The derived key is the first n bytes of the hash value after the hash computation
|
|
|
|
* has been completed by CryptDeriveKey. If the hash is not a member of the SHA-2
|
|
|
|
* family and the required key is for either 3DES or AES, the key is derived as follows:
|
|
|
|
*
|
|
|
|
* Form a 64-byte buffer by repeating the constant 0x36 64 times.
|
|
|
|
* Let k be the length of the hash value that is represented by the input parameter hBaseData.
|
|
|
|
* Set the first k bytes of the buffer to the result of an XOR operation of the first k bytes
|
|
|
|
* of the buffer with the hash value that is represented by the input parameter hBaseData.
|
|
|
|
*
|
|
|
|
* Form a 64-byte buffer by repeating the constant 0x5C 64 times.
|
|
|
|
* Set the first k bytes of the buffer to the result of an XOR operation of the first k bytes
|
|
|
|
* of the buffer with the hash value that is represented by the input parameter hBaseData.
|
|
|
|
*
|
|
|
|
* Hash the result of step 1 by using the same hash algorithm as that used to compute the hash
|
|
|
|
* value that is represented by the hBaseData parameter.
|
|
|
|
*
|
|
|
|
* Hash the result of step 2 by using the same hash algorithm as that used to compute the hash
|
|
|
|
* value that is represented by the hBaseData parameter.
|
|
|
|
*
|
|
|
|
* Concatenate the result of step 3 with the result of step 4.
|
|
|
|
* Use the first n bytes of the result of step 5 as the derived key.
|
|
|
|
*/
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
int freerdp_assistance_crypt_derive_key_sha1(BYTE* hash, int hashLength, BYTE* key, int keyLength)
|
2014-06-30 02:48:37 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BYTE* buffer;
|
|
|
|
BYTE pad1[64];
|
|
|
|
BYTE pad2[64];
|
|
|
|
SHA_CTX hashCtx;
|
|
|
|
|
|
|
|
memset(pad1, 0x36, 64);
|
|
|
|
memset(pad2, 0x5C, 64);
|
|
|
|
|
|
|
|
for (i = 0; i < hashLength; i++)
|
|
|
|
{
|
|
|
|
pad1[i] ^= hash[i];
|
|
|
|
pad2[i] ^= hash[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (BYTE*) calloc(1, hashLength * 2);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return -1;
|
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
SHA1_Init(&hashCtx);
|
|
|
|
SHA1_Update(&hashCtx, pad1, 64);
|
|
|
|
SHA1_Final((void*) buffer, &hashCtx);
|
2014-06-30 02:48:37 +04:00
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
SHA1_Init(&hashCtx);
|
|
|
|
SHA1_Update(&hashCtx, pad2, 64);
|
|
|
|
SHA1_Final((void*) &buffer[hashLength], &hashCtx);
|
2014-06-30 02:48:37 +04:00
|
|
|
|
|
|
|
CopyMemory(key, buffer, keyLength);
|
|
|
|
|
|
|
|
free(buffer);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-01 01:17:06 +04:00
|
|
|
int freerdp_assistance_parse_address_list(rdpAssistanceFile* file, char* list)
|
2014-06-30 04:38:33 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
char* str;
|
|
|
|
int count;
|
|
|
|
int length;
|
2014-07-01 01:17:06 +04:00
|
|
|
char** tokens;
|
|
|
|
|
|
|
|
count = 1;
|
|
|
|
str = _strdup(list);
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = strlen(str);
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if (str[i] == ';')
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = (char**) malloc(sizeof(char*) * count);
|
2015-05-22 21:14:57 +03:00
|
|
|
if (!tokens)
|
2015-06-17 23:08:02 +03:00
|
|
|
{
|
|
|
|
free(str);
|
2015-05-22 21:14:57 +03:00
|
|
|
return -1;
|
2015-06-17 23:08:02 +03:00
|
|
|
}
|
2015-05-22 21:14:57 +03:00
|
|
|
|
2014-07-01 01:17:06 +04:00
|
|
|
count = 0;
|
|
|
|
tokens[count++] = str;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if (str[i] == ';')
|
|
|
|
{
|
|
|
|
str[i] = '\0';
|
|
|
|
tokens[count++] = &str[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:32:45 +03:00
|
|
|
file->MachineCount = count;
|
|
|
|
file->MachineAddresses = (char**) calloc(count, sizeof(char*));
|
|
|
|
file->MachinePorts = (UINT32*) calloc(count, sizeof(UINT32));
|
|
|
|
|
|
|
|
if (!file->MachineAddresses || !file->MachinePorts)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out;
|
2015-05-06 23:32:45 +03:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
length = strlen(tokens[i]);
|
|
|
|
|
|
|
|
p = tokens[i];
|
|
|
|
|
|
|
|
q = strchr(p, ':');
|
|
|
|
|
|
|
|
if (!q)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out;
|
2015-05-06 23:32:45 +03:00
|
|
|
|
|
|
|
q[0] = '\0';
|
|
|
|
q++;
|
|
|
|
|
|
|
|
file->MachineAddresses[i] = _strdup(p);
|
|
|
|
file->MachinePorts[i] = (UINT32) atoi(q);
|
|
|
|
|
2015-05-22 21:14:57 +03:00
|
|
|
if (!file->MachineAddresses[i])
|
2015-06-23 22:29:21 +03:00
|
|
|
goto out;
|
2015-05-22 21:14:57 +03:00
|
|
|
|
2015-05-06 23:32:45 +03:00
|
|
|
q[-1] = ':';
|
|
|
|
}
|
|
|
|
|
2014-07-01 01:17:06 +04:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
length = strlen(tokens[i]);
|
|
|
|
|
|
|
|
if (length > 8)
|
|
|
|
{
|
|
|
|
if (strncmp(tokens[i], "169.254.", 8) == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = tokens[i];
|
|
|
|
|
|
|
|
q = strchr(p, ':');
|
|
|
|
|
|
|
|
if (!q)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out;
|
2014-07-01 01:17:06 +04:00
|
|
|
|
|
|
|
q[0] = '\0';
|
|
|
|
q++;
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (file->MachineAddress)
|
|
|
|
free(file->MachineAddress);
|
2014-07-01 01:17:06 +04:00
|
|
|
file->MachineAddress = _strdup(p);
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!file->MachineAddress)
|
|
|
|
goto out;
|
2014-07-01 01:17:06 +04:00
|
|
|
file->MachinePort = (UINT32) atoi(q);
|
|
|
|
|
2015-05-22 21:14:57 +03:00
|
|
|
if (!file->MachineAddress)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out;
|
2015-05-22 21:14:57 +03:00
|
|
|
|
2014-07-01 01:17:06 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-05-06 23:32:45 +03:00
|
|
|
|
2015-06-24 15:59:59 +03:00
|
|
|
free(tokens);
|
|
|
|
free(str);
|
|
|
|
return 1;
|
2015-06-17 23:08:02 +03:00
|
|
|
out:
|
2015-06-24 13:12:40 +03:00
|
|
|
if (file->MachineAddresses)
|
|
|
|
{
|
|
|
|
for (i=0; i<count; i++)
|
|
|
|
free (file->MachineAddresses[i]);
|
|
|
|
}
|
|
|
|
free (file->MachineAddresses);
|
|
|
|
free (file->MachinePorts);
|
|
|
|
|
|
|
|
file->MachineCount = 0;
|
|
|
|
file->MachinePorts = NULL;
|
|
|
|
file->MachineAddresses = NULL;
|
|
|
|
|
2015-05-22 21:14:57 +03:00
|
|
|
free(tokens);
|
2015-06-17 23:08:02 +03:00
|
|
|
free(str);
|
2015-06-24 15:59:59 +03:00
|
|
|
return -1;
|
2014-07-01 01:17:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int freerdp_assistance_parse_connection_string1(rdpAssistanceFile* file)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char* str;
|
|
|
|
int count;
|
|
|
|
int length;
|
2014-06-30 04:38:33 +04:00
|
|
|
char* tokens[8];
|
2015-06-17 23:08:02 +03:00
|
|
|
int ret;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <ProtocolVersion>,<protocolType>,<machineAddressList>,<assistantAccountPwd>,
|
|
|
|
* <RASessionID>,<RASessionName>,<RASessionPwd>,<protocolSpecificParms>
|
|
|
|
*/
|
|
|
|
|
|
|
|
count = 1;
|
|
|
|
str = _strdup(file->RCTicket);
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = strlen(str);
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if (str[i] == ',')
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count != 8)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
tokens[count++] = str;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if (str[i] == ',')
|
|
|
|
{
|
|
|
|
str[i] = '\0';
|
|
|
|
tokens[count++] = &str[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(tokens[0], "65538") != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(tokens[1], "1") != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(tokens[3], "*") != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(tokens[5], "*") != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(tokens[6], "*") != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
file->RASessionId = _strdup(tokens[4]);
|
|
|
|
|
|
|
|
if (!file->RASessionId)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
file->RASpecificParams = _strdup(tokens[7]);
|
|
|
|
|
|
|
|
if (!file->RASpecificParams)
|
|
|
|
return -1;
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
ret = freerdp_assistance_parse_address_list(file, tokens[2]);
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
free(str);
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (ret != 1)
|
|
|
|
return -1;
|
|
|
|
|
2014-06-30 04:38:33 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypted Connection String 2:
|
|
|
|
*
|
|
|
|
* <E>
|
|
|
|
* <A KH="BNRjdu97DyczQSRuMRrDWoue+HA=" ID="+ULZ6ifjoCa6cGPMLQiGHRPwkg6VyJqGwxMnO6GcelwUh9a6/FBq3It5ADSndmLL"/>
|
|
|
|
* <C>
|
|
|
|
* <T ID="1" SID="0">
|
|
|
|
* <L P="49228" N="fe80::1032:53d9:5a01:909b%3"/>
|
|
|
|
* <L P="49229" N="fe80::3d8f:9b2d:6b4e:6aa%6"/>
|
|
|
|
* <L P="49230" N="192.168.1.200"/>
|
|
|
|
* <L P="49231" N="169.254.6.170"/>
|
|
|
|
* </T>
|
|
|
|
* </C>
|
|
|
|
* </E>
|
|
|
|
*/
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
int freerdp_assistance_parse_connection_string2(rdpAssistanceFile* file)
|
2014-06-30 04:38:33 +04:00
|
|
|
{
|
|
|
|
char* str;
|
2015-06-17 23:08:02 +03:00
|
|
|
char* tag;
|
|
|
|
char* end;
|
|
|
|
char* p;
|
|
|
|
int ret = -1;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
str = file->ConnectionString2;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!strstr(str, "<E>"))
|
2014-06-30 04:38:33 +04:00
|
|
|
return -1;
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!strstr(str, "<C>"))
|
2014-06-30 04:38:33 +04:00
|
|
|
return -1;
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
str = _strdup(file->ConnectionString2);
|
|
|
|
if (!str)
|
|
|
|
return -1;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!(tag = strstr(str, "<A")))
|
|
|
|
goto out_fail;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
/* Parse Auth String Node (<A>) */
|
|
|
|
end = strstr(tag, "/>");
|
|
|
|
if (!end)
|
|
|
|
goto out_fail;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
*end = '\0';
|
2014-06-30 04:38:33 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
p = strstr(tag, "KH=\"");
|
2014-06-30 04:38:33 +04:00
|
|
|
if (p)
|
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
char *q;
|
|
|
|
size_t length;
|
2014-06-30 04:38:33 +04:00
|
|
|
p += sizeof("KH=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
free(file->RASpecificParams);
|
|
|
|
file->RASpecificParams = (char*) malloc(length + 1);
|
|
|
|
if (!file->RASpecificParams)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
CopyMemory(file->RASpecificParams, p, length);
|
|
|
|
file->RASpecificParams[length] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
p = strstr(tag, "ID=\"");
|
2014-06-30 04:38:33 +04:00
|
|
|
if (p)
|
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
char *q;
|
|
|
|
size_t length;
|
2014-06-30 04:38:33 +04:00
|
|
|
p += sizeof("ID=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
free(file->RASessionId);
|
|
|
|
file->RASessionId = (char*) malloc(length + 1);
|
|
|
|
if (!file->RASessionId)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
CopyMemory(file->RASessionId, p, length);
|
|
|
|
file->RASessionId[length] = '\0';
|
|
|
|
}
|
2015-06-17 23:08:02 +03:00
|
|
|
*end = '/';
|
2014-06-30 04:38:33 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
/* Parse <L last address is used */
|
2014-08-15 02:41:22 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
p = strstr(str, "<L P=\"");
|
2014-08-15 02:41:22 +04:00
|
|
|
while (p)
|
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
char *q;
|
|
|
|
int port;
|
|
|
|
size_t length;
|
2014-08-15 02:41:22 +04:00
|
|
|
p += sizeof("<L P=\"") - 1;
|
|
|
|
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-08-15 02:41:22 +04:00
|
|
|
|
|
|
|
q[0] = '\0';
|
|
|
|
q++;
|
|
|
|
|
|
|
|
port = atoi(p);
|
|
|
|
|
|
|
|
p = strstr(q, " N=\"");
|
|
|
|
|
|
|
|
if (!p)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-08-15 02:41:22 +04:00
|
|
|
|
|
|
|
p += sizeof(" N=\"") - 1;
|
|
|
|
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
2015-06-17 23:08:02 +03:00
|
|
|
goto out_fail;
|
2014-08-15 02:41:22 +04:00
|
|
|
|
|
|
|
q[0] = '\0';
|
|
|
|
q++;
|
|
|
|
|
|
|
|
length = strlen(p);
|
|
|
|
|
|
|
|
if (length > 8)
|
|
|
|
{
|
|
|
|
if (strncmp(p, "169.254.", 8) != 0)
|
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
if (file->MachineAddress)
|
|
|
|
free(file->MachineAddress);
|
2014-08-15 02:41:22 +04:00
|
|
|
file->MachineAddress = _strdup(p);
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!file->MachineAddress)
|
|
|
|
goto out_fail;
|
2014-08-15 02:41:22 +04:00
|
|
|
file->MachinePort = (UINT32) port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strstr(q, "<L P=\"");
|
|
|
|
}
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
ret = 1;
|
|
|
|
out_fail:
|
2014-06-30 04:38:33 +04:00
|
|
|
free(str);
|
2015-06-17 23:08:02 +03:00
|
|
|
return ret;
|
2014-06-30 04:38:33 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
char* freerdp_assistance_construct_expert_blob(const char* name, const char* pass)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int nameLength;
|
|
|
|
int passLength;
|
|
|
|
char* ExpertBlob = NULL;
|
|
|
|
|
|
|
|
if (!name || !pass)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
nameLength = strlen(name) + strlen("NAME=");
|
|
|
|
passLength = strlen(pass) + strlen("PASS=");
|
|
|
|
|
|
|
|
size = nameLength + passLength + 64;
|
|
|
|
ExpertBlob = (char*) calloc(1, size);
|
|
|
|
|
|
|
|
if (!ExpertBlob)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sprintf_s(ExpertBlob, size, "%d;NAME=%s%d;PASS=%s",
|
|
|
|
nameLength, name, passLength, pass);
|
|
|
|
|
|
|
|
return ExpertBlob;
|
|
|
|
}
|
|
|
|
|
2014-07-16 04:09:19 +04:00
|
|
|
char* freerdp_assistance_generate_pass_stub(DWORD flags)
|
|
|
|
{
|
|
|
|
UINT32 nums[14];
|
|
|
|
char* passStub = NULL;
|
|
|
|
char set1[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*_";
|
|
|
|
char set2[12] = "!@#$&^*()-+=";
|
|
|
|
char set3[10] = "0123456789";
|
|
|
|
char set4[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
char set5[26] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
passStub = (char*) malloc(15);
|
|
|
|
|
|
|
|
if (!passStub)
|
2014-08-14 01:48:57 +04:00
|
|
|
return NULL;
|
2014-07-16 04:09:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PassStub generation:
|
|
|
|
*
|
|
|
|
* Characters 0 and 5-13 are from the set A-Z a-z 0-9 * _
|
|
|
|
* Character 1 is from the set !@#$&^*()-+=
|
|
|
|
* Character 2 is from the set 0-9
|
|
|
|
* Character 3 is from the set A-Z
|
|
|
|
* Character 4 is from the set a-z
|
|
|
|
*
|
|
|
|
* Example: WB^6HsrIaFmEpi
|
|
|
|
*/
|
|
|
|
|
|
|
|
RAND_bytes((BYTE*) nums, sizeof(nums));
|
|
|
|
|
|
|
|
passStub[0] = set1[nums[0] % sizeof(set1)]; /* character 0 */
|
|
|
|
passStub[1] = set2[nums[1] % sizeof(set2)]; /* character 1 */
|
|
|
|
passStub[2] = set3[nums[2] % sizeof(set3)]; /* character 2 */
|
|
|
|
passStub[3] = set4[nums[3] % sizeof(set4)]; /* character 3 */
|
|
|
|
passStub[4] = set5[nums[4] % sizeof(set5)]; /* character 4 */
|
|
|
|
passStub[5] = set1[nums[5] % sizeof(set1)]; /* character 5 */
|
|
|
|
passStub[6] = set1[nums[6] % sizeof(set1)]; /* character 6 */
|
|
|
|
passStub[7] = set1[nums[7] % sizeof(set1)]; /* character 7 */
|
|
|
|
passStub[8] = set1[nums[8] % sizeof(set1)]; /* character 8 */
|
|
|
|
passStub[9] = set1[nums[9] % sizeof(set1)]; /* character 9 */
|
|
|
|
passStub[10] = set1[nums[10] % sizeof(set1)]; /* character 10 */
|
|
|
|
passStub[11] = set1[nums[11] % sizeof(set1)]; /* character 11 */
|
|
|
|
passStub[12] = set1[nums[12] % sizeof(set1)]; /* character 12 */
|
|
|
|
passStub[13] = set1[nums[13] % sizeof(set1)]; /* character 13 */
|
|
|
|
passStub[14] = '\0';
|
|
|
|
|
|
|
|
return passStub;
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
BYTE* freerdp_assistance_encrypt_pass_stub(const char* password, const char* passStub, int* pEncryptedSize)
|
2014-06-29 01:03:16 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
MD5_CTX md5Ctx;
|
2014-06-29 20:36:28 +04:00
|
|
|
int cbPasswordW;
|
|
|
|
int cbPassStubW;
|
2014-06-30 20:51:27 +04:00
|
|
|
int EncryptedSize;
|
|
|
|
BYTE PasswordHash[16];
|
2014-06-30 02:48:37 +04:00
|
|
|
EVP_CIPHER_CTX rc4Ctx;
|
2014-09-12 16:36:29 +04:00
|
|
|
BYTE* pbIn, *pbOut;
|
2014-06-30 17:21:45 +04:00
|
|
|
int cbOut, cbIn, cbFinal;
|
2014-06-30 20:51:27 +04:00
|
|
|
WCHAR* PasswordW = NULL;
|
|
|
|
WCHAR* PassStubW = NULL;
|
2014-06-29 20:36:28 +04:00
|
|
|
|
2014-06-29 01:03:16 +04:00
|
|
|
status = ConvertToUnicode(CP_UTF8, 0, password, -1, &PasswordW, 0);
|
|
|
|
|
|
|
|
if (status <= 0)
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-29 20:36:28 +04:00
|
|
|
cbPasswordW = (status - 1) * 2;
|
|
|
|
|
2014-06-29 01:03:16 +04:00
|
|
|
MD5_Init(&md5Ctx);
|
2014-06-29 20:36:28 +04:00
|
|
|
MD5_Update(&md5Ctx, PasswordW, cbPasswordW);
|
2014-06-29 01:03:16 +04:00
|
|
|
MD5_Final((void*) PasswordHash, &md5Ctx);
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
status = ConvertToUnicode(CP_UTF8, 0, passStub, -1, &PassStubW, 0);
|
2014-06-29 20:36:28 +04:00
|
|
|
|
|
|
|
if (status <= 0)
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-06-29 20:36:28 +04:00
|
|
|
|
|
|
|
cbPassStubW = (status - 1) * 2;
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
EncryptedSize = cbPassStubW + 4;
|
2014-06-29 20:36:28 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
pbIn = (BYTE*) calloc(1, EncryptedSize);
|
|
|
|
pbOut = (BYTE*) calloc(1, EncryptedSize);
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-11-17 00:58:36 +03:00
|
|
|
if (!pbIn || !pbOut)
|
|
|
|
{
|
|
|
|
free(PasswordW);
|
|
|
|
free(PassStubW);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(pbIn);
|
|
|
|
free(pbOut);
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
if (!EncryptedSize)
|
2014-11-17 00:58:36 +03:00
|
|
|
{
|
|
|
|
free(PasswordW);
|
|
|
|
free(PassStubW);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(pbIn);
|
|
|
|
free(pbOut);
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
*((UINT32*) pbIn) = cbPassStubW;
|
|
|
|
CopyMemory(&pbIn[4], PassStubW, cbPassStubW);
|
2014-06-29 20:36:28 +04:00
|
|
|
|
2014-11-17 00:58:36 +03:00
|
|
|
free(PasswordW);
|
|
|
|
free(PassStubW);
|
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
EVP_CIPHER_CTX_init(&rc4Ctx);
|
|
|
|
|
2014-06-30 17:21:45 +04:00
|
|
|
status = EVP_EncryptInit_ex(&rc4Ctx, EVP_rc4(), NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "EVP_CipherInit_ex failure");
|
2014-11-17 00:58:36 +03:00
|
|
|
free (pbOut);
|
|
|
|
free (pbIn);
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-06-30 17:21:45 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
status = EVP_EncryptInit_ex(&rc4Ctx, NULL, NULL, PasswordHash, NULL);
|
2014-06-30 02:48:37 +04:00
|
|
|
|
|
|
|
if (!status)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "EVP_CipherInit_ex failure");
|
2014-11-17 00:58:36 +03:00
|
|
|
free (pbOut);
|
|
|
|
free (pbIn);
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-06-30 02:48:37 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 17:21:45 +04:00
|
|
|
cbOut = cbFinal = 0;
|
2014-06-30 20:51:27 +04:00
|
|
|
cbIn = EncryptedSize;
|
2014-06-30 17:21:45 +04:00
|
|
|
|
|
|
|
status = EVP_EncryptUpdate(&rc4Ctx, pbOut, &cbOut, pbIn, cbIn);
|
2014-11-17 00:58:36 +03:00
|
|
|
free(pbIn);
|
2014-06-30 02:48:37 +04:00
|
|
|
|
|
|
|
if (!status)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "EVP_CipherUpdate failure");
|
2014-11-17 00:58:36 +03:00
|
|
|
free (pbOut);
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-06-30 02:48:37 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 17:21:45 +04:00
|
|
|
status = EVP_EncryptFinal_ex(&rc4Ctx, pbOut + cbOut, &cbFinal);
|
2014-06-30 02:48:37 +04:00
|
|
|
|
|
|
|
if (!status)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "EVP_CipherFinal_ex failure");
|
2014-11-17 00:58:36 +03:00
|
|
|
free (pbOut);
|
2014-06-30 20:51:27 +04:00
|
|
|
return NULL;
|
2014-06-30 02:48:37 +04:00
|
|
|
}
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
EVP_CIPHER_CTX_cleanup(&rc4Ctx);
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
*pEncryptedSize = EncryptedSize;
|
|
|
|
|
|
|
|
return pbOut;
|
2014-06-29 01:03:16 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
int freerdp_assistance_decrypt2(rdpAssistanceFile* file, const char* password)
|
2014-06-29 00:04:49 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
SHA_CTX shaCtx;
|
2014-06-30 02:48:37 +04:00
|
|
|
int cbPasswordW;
|
2014-06-30 04:38:33 +04:00
|
|
|
int cchOutW = 0;
|
|
|
|
WCHAR* pbOutW = NULL;
|
2014-06-29 00:04:49 +04:00
|
|
|
EVP_CIPHER_CTX aesDec;
|
|
|
|
WCHAR* PasswordW = NULL;
|
2014-09-12 16:36:29 +04:00
|
|
|
BYTE* pbIn, *pbOut;
|
2014-06-30 02:48:37 +04:00
|
|
|
int cbOut, cbIn, cbFinal;
|
|
|
|
BYTE DerivedKey[AES_BLOCK_SIZE];
|
2014-06-30 03:57:46 +04:00
|
|
|
BYTE InitializationVector[AES_BLOCK_SIZE];
|
2014-06-29 00:04:49 +04:00
|
|
|
BYTE PasswordHash[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
status = ConvertToUnicode(CP_UTF8, 0, password, -1, &PasswordW, 0);
|
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
cbPasswordW = (status - 1) * 2;
|
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
SHA1_Init(&shaCtx);
|
|
|
|
SHA1_Update(&shaCtx, PasswordW, cbPasswordW);
|
|
|
|
SHA1_Final((void*) PasswordHash, &shaCtx);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
status = freerdp_assistance_crypt_derive_key_sha1(PasswordHash, sizeof(PasswordHash),
|
2014-09-12 16:36:29 +04:00
|
|
|
DerivedKey, sizeof(DerivedKey));
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
2014-11-17 00:58:36 +03:00
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(PasswordW);
|
2014-06-29 00:04:49 +04:00
|
|
|
return -1;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
ZeroMemory(InitializationVector, sizeof(InitializationVector));
|
|
|
|
|
2014-06-29 00:04:49 +04:00
|
|
|
EVP_CIPHER_CTX_init(&aesDec);
|
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
status = EVP_DecryptInit_ex(&aesDec, EVP_aes_128_cbc(), NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (status != 1)
|
2014-11-17 00:58:36 +03:00
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(PasswordW);
|
2014-06-30 03:57:46 +04:00
|
|
|
return -1;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-30 03:57:46 +04:00
|
|
|
|
|
|
|
EVP_CIPHER_CTX_set_key_length(&aesDec, (128 / 8));
|
2014-06-30 02:48:37 +04:00
|
|
|
EVP_CIPHER_CTX_set_padding(&aesDec, 0);
|
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
status = EVP_DecryptInit_ex(&aesDec, EVP_aes_128_cbc(), NULL, DerivedKey, InitializationVector);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
if (status != 1)
|
2014-11-17 00:58:36 +03:00
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(PasswordW);
|
2014-06-29 00:04:49 +04:00
|
|
|
return -1;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
cbOut = cbFinal = 0;
|
|
|
|
cbIn = file->EncryptedLHTicketLength;
|
2014-06-30 04:38:33 +04:00
|
|
|
pbIn = (BYTE*) file->EncryptedLHTicket;
|
|
|
|
pbOut = (BYTE*) calloc(1, cbIn + AES_BLOCK_SIZE + 2);
|
2014-06-30 02:48:37 +04:00
|
|
|
|
2014-06-30 04:38:33 +04:00
|
|
|
if (!pbOut)
|
2014-11-17 00:58:36 +03:00
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(PasswordW);
|
2014-06-29 00:04:49 +04:00
|
|
|
return -1;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
status = EVP_DecryptUpdate(&aesDec, pbOut, &cbOut, pbIn, cbIn);
|
|
|
|
|
2014-06-29 00:04:49 +04:00
|
|
|
if (status != 1)
|
2014-11-17 00:58:36 +03:00
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(PasswordW);
|
|
|
|
free(pbOut);
|
2014-06-29 00:04:49 +04:00
|
|
|
return -1;
|
2014-11-17 00:58:36 +03:00
|
|
|
}
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
status = EVP_DecryptFinal_ex(&aesDec, pbOut + cbOut, &cbFinal);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
if (status != 1)
|
2014-06-30 02:48:37 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "EVP_DecryptFinal_ex failure");
|
2015-05-11 10:07:39 +03:00
|
|
|
free(PasswordW);
|
|
|
|
free(pbOut);
|
2014-06-29 00:04:49 +04:00
|
|
|
return -1;
|
2014-06-30 02:48:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX_cleanup(&aesDec);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2014-06-30 04:38:33 +04:00
|
|
|
cbOut += cbFinal;
|
|
|
|
cbFinal = 0;
|
|
|
|
|
|
|
|
pbOutW = (WCHAR*) pbOut;
|
|
|
|
cchOutW = cbOut / 2;
|
|
|
|
|
|
|
|
file->ConnectionString2 = NULL;
|
|
|
|
status = ConvertFromUnicode(CP_UTF8, 0, pbOutW, cchOutW, &file->ConnectionString2, 0, NULL, NULL);
|
|
|
|
|
2014-06-30 03:57:46 +04:00
|
|
|
free(PasswordW);
|
2014-06-30 04:38:33 +04:00
|
|
|
free(pbOut);
|
|
|
|
|
2014-11-17 00:58:36 +03:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
status = freerdp_assistance_parse_connection_string2(file);
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "freerdp_assistance_parse_connection_string2: %d", status);
|
2015-06-17 23:08:02 +03:00
|
|
|
return status;
|
2014-06-29 00:04:49 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
int freerdp_assistance_decrypt(rdpAssistanceFile* file, const char* password)
|
2014-06-29 01:03:16 +04:00
|
|
|
{
|
2014-06-30 20:51:27 +04:00
|
|
|
int status = 1;
|
|
|
|
|
|
|
|
file->EncryptedPassStub = freerdp_assistance_encrypt_pass_stub(password,
|
2014-09-12 16:36:29 +04:00
|
|
|
file->PassStub, &file->EncryptedPassStubLength);
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
if (!file->EncryptedPassStub)
|
|
|
|
return -1;
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 02:48:37 +04:00
|
|
|
if (file->Type > 1)
|
|
|
|
{
|
2014-06-30 20:51:27 +04:00
|
|
|
status = freerdp_assistance_decrypt2(file, password);
|
2014-06-30 02:48:37 +04:00
|
|
|
}
|
2014-06-29 01:03:16 +04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
BYTE* freerdp_assistance_hex_string_to_bin(const char* str, int* size)
|
2014-06-29 00:04:49 +04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int length;
|
|
|
|
BYTE* buffer;
|
|
|
|
int i, ln, hn;
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
length = strlen(str);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
if ((length % 2) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
length /= 2;
|
|
|
|
*size = length;
|
|
|
|
|
|
|
|
buffer = (BYTE*) malloc(length);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
hn = ln = 0;
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
c = str[(i * 2) + 0];
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
if ((c >= '0') && (c <= '9'))
|
|
|
|
hn = c - '0';
|
|
|
|
else if ((c >= 'a') && (c <= 'f'))
|
|
|
|
hn = (c - 'a') + 10;
|
|
|
|
else if ((c >= 'A') && (c <= 'F'))
|
|
|
|
hn = (c - 'A') + 10;
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
c = str[(i * 2) + 1];
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
if ((c >= '0') && (c <= '9'))
|
|
|
|
ln = c - '0';
|
|
|
|
else if ((c >= 'a') && (c <= 'f'))
|
|
|
|
ln = (c - 'a') + 10;
|
|
|
|
else if ((c >= 'A') && (c <= 'F'))
|
|
|
|
ln = (c - 'A') + 10;
|
|
|
|
|
|
|
|
buffer[i] = (hn << 4) | ln;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
char* freerdp_assistance_bin_to_hex_string(const BYTE* data, int size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char* p;
|
|
|
|
int ln, hn;
|
|
|
|
char bin2hex[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
p = (char*) malloc((size + 1) * 2);
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2014-06-30 20:51:27 +04:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
ln = data[i] & 0xF;
|
|
|
|
hn = (data[i] >> 4) & 0xF;
|
|
|
|
|
|
|
|
p[i * 2] = bin2hex[hn];
|
|
|
|
p[(i * 2) + 1] = bin2hex[ln];
|
|
|
|
}
|
|
|
|
|
|
|
|
p[size * 2] = '\0';
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int freerdp_assistance_parse_file_buffer(rdpAssistanceFile* file, const char* buffer, size_t size)
|
2014-06-29 00:04:49 +04:00
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
char* r;
|
|
|
|
int value;
|
2014-06-29 01:03:16 +04:00
|
|
|
int status;
|
2014-06-29 00:04:49 +04:00
|
|
|
size_t length;
|
|
|
|
|
|
|
|
p = strstr(buffer, "UPLOADINFO");
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p = strstr(p + sizeof("UPLOADINFO") - 1, "TYPE=\"");
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p = strstr(buffer, "UPLOADDATA");
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Parse USERNAME */
|
|
|
|
|
|
|
|
p = strstr(buffer, "USERNAME=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("USERNAME=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
file->Username = (char*) malloc(length + 1);
|
|
|
|
|
|
|
|
if (!file->Username)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(file->Username, p, length);
|
|
|
|
file->Username[length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse LHTICKET */
|
|
|
|
|
|
|
|
p = strstr(buffer, "LHTICKET=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("LHTICKET=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
file->LHTicket = (char*) malloc(length + 1);
|
|
|
|
|
|
|
|
if (!file->LHTicket)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(file->LHTicket, p, length);
|
|
|
|
file->LHTicket[length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse RCTICKET */
|
|
|
|
|
|
|
|
p = strstr(buffer, "RCTICKET=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("RCTICKET=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
file->RCTicket = (char*) malloc(length + 1);
|
|
|
|
|
|
|
|
if (!file->RCTicket)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(file->RCTicket, p, length);
|
|
|
|
file->RCTicket[length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse RCTICKETENCRYPTED */
|
|
|
|
|
|
|
|
p = strstr(buffer, "RCTICKETENCRYPTED=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("RCTICKETENCRYPTED=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
|
|
|
|
if ((length == 1) && (p[0] == '1'))
|
|
|
|
file->RCTicketEncrypted = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse PassStub */
|
|
|
|
|
|
|
|
p = strstr(buffer, "PassStub=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("PassStub=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
file->PassStub = (char*) malloc(length + 1);
|
|
|
|
|
|
|
|
if (!file->PassStub)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(file->PassStub, p, length);
|
|
|
|
file->PassStub[length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse DtStart */
|
|
|
|
|
|
|
|
p = strstr(buffer, "DtStart=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("DtStart=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
|
|
|
|
r = (char*) malloc(length + 1);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(r, p, length);
|
|
|
|
r[length] = '\0';
|
|
|
|
|
|
|
|
value = atoi(r);
|
|
|
|
free(r);
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
file->DtStart = (UINT32) value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse DtLength */
|
|
|
|
|
|
|
|
p = strstr(buffer, "DtLength=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof("DtLength=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
|
|
|
|
r = (char*) malloc(length + 1);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CopyMemory(r, p, length);
|
|
|
|
r[length] = '\0';
|
|
|
|
|
|
|
|
value = atoi(r);
|
|
|
|
free(r);
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
file->DtLength = (UINT32) value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse L (LowSpeed) */
|
|
|
|
|
|
|
|
p = strstr(buffer, " L=\"");
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p += sizeof(" L=\"") - 1;
|
|
|
|
q = strchr(p, '"');
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
length = q - p;
|
|
|
|
|
|
|
|
if ((length == 1) && (p[0] == '1'))
|
|
|
|
file->LowSpeed = TRUE;
|
|
|
|
}
|
|
|
|
|
2014-06-29 20:36:28 +04:00
|
|
|
file->Type = (file->LHTicket) ? 2 : 1;
|
|
|
|
|
|
|
|
if (file->LHTicket)
|
|
|
|
{
|
2014-06-30 20:51:27 +04:00
|
|
|
file->EncryptedLHTicket = freerdp_assistance_hex_string_to_bin(file->LHTicket,
|
2014-06-29 20:36:28 +04:00
|
|
|
&file->EncryptedLHTicketLength);
|
|
|
|
}
|
2014-06-29 01:03:16 +04:00
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
status = freerdp_assistance_parse_connection_string1(file);
|
2014-06-29 01:03:16 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "freerdp_assistance_parse_connection_string1 failure: %d", status);
|
2014-06-29 01:03:16 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-06-29 00:04:49 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
int freerdp_assistance_parse_file(rdpAssistanceFile* file, const char* name)
|
2014-06-29 02:33:46 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
BYTE* buffer;
|
|
|
|
FILE* fp = NULL;
|
|
|
|
size_t readSize;
|
|
|
|
long int fileSize;
|
|
|
|
|
|
|
|
fp = fopen(name, "r");
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
fileSize = ftell(fp);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (fileSize < 1)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (BYTE*) malloc(fileSize + 2);
|
2015-05-06 23:32:45 +03:00
|
|
|
|
|
|
|
if (!buffer)
|
2015-08-28 11:53:43 +03:00
|
|
|
{
|
|
|
|
fclose(fp);
|
2015-05-06 23:32:45 +03:00
|
|
|
return -1;
|
2015-08-28 11:53:43 +03:00
|
|
|
}
|
2015-05-06 23:32:45 +03:00
|
|
|
|
2014-06-29 02:33:46 +04:00
|
|
|
readSize = fread(buffer, fileSize, 1, fp);
|
|
|
|
|
|
|
|
if (!readSize)
|
|
|
|
{
|
|
|
|
if (!ferror(fp))
|
|
|
|
readSize = fileSize;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
if (readSize < 1)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[fileSize] = '\0';
|
|
|
|
buffer[fileSize + 1] = '\0';
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
status = freerdp_assistance_parse_file_buffer(file, (char*) buffer, fileSize);
|
2014-06-29 02:33:46 +04:00
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int freerdp_client_populate_settings_from_assistance_file(rdpAssistanceFile* file, rdpSettings* settings)
|
|
|
|
{
|
2015-05-06 23:32:45 +03:00
|
|
|
UINT32 i;
|
|
|
|
|
2014-06-29 02:33:46 +04:00
|
|
|
freerdp_set_param_bool(settings, FreeRDP_RemoteAssistanceMode, TRUE);
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!file->RASessionId || !file->MachineAddress)
|
2014-06-29 02:33:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (freerdp_set_param_string(settings, FreeRDP_RemoteAssistanceSessionId, file->RASessionId) != 0)
|
|
|
|
return -1;
|
2014-06-29 02:33:46 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (file->RCTicket && (freerdp_set_param_string(settings, FreeRDP_RemoteAssistanceRCTicket, file->RCTicket) != 0))
|
|
|
|
return -1;
|
2014-06-30 20:51:27 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (file->PassStub && (freerdp_set_param_string(settings, FreeRDP_RemoteAssistancePassStub, file->PassStub) != 0))
|
|
|
|
return -1;
|
2014-06-30 20:51:27 +04:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (freerdp_set_param_string(settings, FreeRDP_ServerHostname, file->MachineAddress) != 0)
|
2014-06-29 04:22:36 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
freerdp_set_param_uint32(settings, FreeRDP_ServerPort, file->MachinePort);
|
|
|
|
|
2015-05-06 23:32:45 +03:00
|
|
|
freerdp_target_net_addresses_free(settings);
|
|
|
|
|
|
|
|
settings->TargetNetAddressCount = file->MachineCount;
|
|
|
|
|
2015-05-22 21:14:57 +03:00
|
|
|
if (settings->TargetNetAddressCount)
|
2015-05-06 23:32:45 +03:00
|
|
|
{
|
2015-05-22 21:14:57 +03:00
|
|
|
settings->TargetNetAddresses = (char**) calloc(file->MachineCount, sizeof(char*));
|
|
|
|
settings->TargetNetPorts = (UINT32*) calloc(file->MachineCount, sizeof(UINT32));
|
|
|
|
|
|
|
|
if (!settings->TargetNetAddresses || !settings->TargetNetPorts)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < settings->TargetNetAddressCount; i++)
|
|
|
|
{
|
|
|
|
settings->TargetNetAddresses[i] = _strdup(file->MachineAddresses[i]);
|
|
|
|
settings->TargetNetPorts[i] = file->MachinePorts[i];
|
|
|
|
|
|
|
|
if (!settings->TargetNetAddresses[i])
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-06 23:32:45 +03:00
|
|
|
}
|
|
|
|
|
2014-06-29 02:33:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-07 14:52:35 +03:00
|
|
|
rdpAssistanceFile* freerdp_assistance_file_new(void)
|
2014-06-29 00:04:49 +04:00
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
return (rdpAssistanceFile*) calloc(1, sizeof(rdpAssistanceFile));
|
2014-06-29 00:04:49 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 20:51:27 +04:00
|
|
|
void freerdp_assistance_file_free(rdpAssistanceFile* file)
|
2014-06-29 00:04:49 +04:00
|
|
|
{
|
2015-05-06 23:32:45 +03:00
|
|
|
UINT32 i;
|
|
|
|
|
2014-06-29 00:04:49 +04:00
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(file->Username);
|
|
|
|
free(file->LHTicket);
|
|
|
|
free(file->RCTicket);
|
|
|
|
free(file->PassStub);
|
2014-06-30 03:57:46 +04:00
|
|
|
free(file->ConnectionString1);
|
2014-06-29 01:03:16 +04:00
|
|
|
free(file->ConnectionString2);
|
|
|
|
free(file->EncryptedLHTicket);
|
2014-06-30 03:57:46 +04:00
|
|
|
free(file->RASessionId);
|
|
|
|
free(file->RASpecificParams);
|
|
|
|
free(file->MachineAddress);
|
|
|
|
free(file->EncryptedPassStub);
|
2014-06-29 00:04:49 +04:00
|
|
|
|
2015-05-06 23:32:45 +03:00
|
|
|
for (i = 0; i < file->MachineCount; i++)
|
|
|
|
{
|
|
|
|
free(file->MachineAddresses[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(file->MachineAddresses);
|
|
|
|
free(file->MachinePorts);
|
|
|
|
|
2014-06-29 00:04:49 +04:00
|
|
|
free(file);
|
|
|
|
}
|