2012-02-17 09:58:30 +04:00
|
|
|
/**
|
2012-02-21 09:56:55 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-02-17 09:58:30 +04:00
|
|
|
* Certificate Handling
|
|
|
|
*
|
|
|
|
* Copyright 2011 Jiten Pathy
|
2012-02-21 09:56:55 +04:00
|
|
|
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2012-02-17 09:58:30 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2015-07-13 16:52:06 +03:00
|
|
|
#include <ctype.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2015-06-11 12:16:45 +03:00
|
|
|
#include <winpr/crypto.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-22 23:52:43 +04:00
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/path.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
#include <freerdp/settings.h>
|
|
|
|
|
|
|
|
#include <freerdp/crypto/certificate.h>
|
|
|
|
|
|
|
|
struct rdp_certificate_store
|
|
|
|
{
|
|
|
|
char* file;
|
|
|
|
char* certs_path;
|
|
|
|
char* server_path;
|
|
|
|
const rdpSettings* settings;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rdp_certificate_data
|
|
|
|
{
|
|
|
|
char* hostname;
|
|
|
|
UINT16 port;
|
|
|
|
char* subject;
|
|
|
|
char* issuer;
|
|
|
|
char* fingerprint;
|
|
|
|
char* pem;
|
|
|
|
};
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
static const char certificate_store_dir[] = "certs";
|
2013-06-07 00:45:19 +04:00
|
|
|
static const char certificate_server_dir[] = "server";
|
2015-06-12 10:30:01 +03:00
|
|
|
static const char certificate_known_hosts_file[] = "known_hosts2";
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <freerdp/crypto/certificate.h>
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("crypto")
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static BOOL certificate_get_file_data(rdpCertificateStore* store, rdpCertificateData* data);
|
|
|
|
static BOOL duplicate(char** data, const char* value);
|
|
|
|
|
|
|
|
static HANDLE open_file(const char* name, DWORD dwDesiredAccess, DWORD dwShareMode,
|
|
|
|
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes)
|
|
|
|
{
|
|
|
|
HANDLE fp;
|
|
|
|
WCHAR* wfile = NULL;
|
|
|
|
int rc = ConvertToUnicode(CP_UTF8, 0, name, -1, &wfile, 0);
|
|
|
|
if (rc <= 0)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
fp = CreateFileW(wfile, dwDesiredAccess, 0, NULL, dwCreationDisposition, dwFlagsAndAttributes,
|
|
|
|
NULL);
|
|
|
|
free(wfile);
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
static rdpCertificateData* certificate_split_line(char* line);
|
2018-05-11 12:09:54 +03:00
|
|
|
static BOOL certificate_line_is_comment(const char* line, size_t length)
|
|
|
|
{
|
2018-11-30 11:55:10 +03:00
|
|
|
while (length > 0)
|
2018-05-11 12:09:54 +03:00
|
|
|
{
|
2018-11-30 11:55:10 +03:00
|
|
|
switch (*line)
|
2018-05-11 12:09:54 +03:00
|
|
|
{
|
2018-11-30 11:55:10 +03:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
line++;
|
|
|
|
length--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2018-05-11 12:09:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 19:49:24 +03:00
|
|
|
return TRUE;
|
2018-05-11 12:09:54 +03:00
|
|
|
}
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
static void certificate_store_uninit(rdpCertificateStore* certificate_store)
|
|
|
|
{
|
|
|
|
if (certificate_store)
|
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
free(certificate_store->certs_path);
|
2020-11-18 09:51:45 +03:00
|
|
|
free(certificate_store->file);
|
2021-04-13 12:01:43 +03:00
|
|
|
free(certificate_store->server_path);
|
2020-11-18 09:51:45 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
|
|
|
|
static BOOL ensure_path_exists(const char* path)
|
|
|
|
{
|
|
|
|
BOOL res = FALSE;
|
|
|
|
WCHAR* wpath = NULL;
|
|
|
|
/* Use wide character functions to allow proper unicode handling on windows */
|
|
|
|
int rc = ConvertToUnicode(CP_UTF8, 0, path, -1, &wpath, 0);
|
|
|
|
if (rc <= 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!PathFileExistsW(wpath))
|
|
|
|
{
|
|
|
|
if (!PathMakePathW(wpath, 0))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error creating directory '%s'", path);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_INFO(TAG, "creating directory %s", path);
|
|
|
|
}
|
|
|
|
res = TRUE;
|
|
|
|
fail:
|
|
|
|
free(wpath);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static BOOL certificate_store_init(rdpCertificateStore* certificate_store)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
const rdpSettings* settings;
|
2020-11-18 09:51:45 +03:00
|
|
|
const char* ConfigPath;
|
|
|
|
if (!certificate_store)
|
|
|
|
return FALSE;
|
2012-02-17 09:58:30 +04:00
|
|
|
settings = certificate_store->settings;
|
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
if (!settings)
|
|
|
|
return FALSE;
|
|
|
|
ConfigPath = settings->ConfigPath;
|
|
|
|
if (!ConfigPath)
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!(certificate_store->certs_path =
|
|
|
|
GetCombinedPath(ConfigPath, (char*)certificate_store_dir)))
|
2015-05-05 20:45:34 +03:00
|
|
|
goto fail;
|
2021-04-13 12:01:43 +03:00
|
|
|
certificate_store->server_path = GetCombinedPath(ConfigPath, (char*)certificate_server_dir);
|
|
|
|
if (!certificate_store->server_path)
|
2015-05-05 20:45:34 +03:00
|
|
|
goto fail;
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(certificate_store->file =
|
2020-11-18 09:51:45 +03:00
|
|
|
GetCombinedPath(ConfigPath, (char*)certificate_known_hosts_file)))
|
2015-05-05 20:45:34 +03:00
|
|
|
goto fail;
|
2021-04-13 12:01:43 +03:00
|
|
|
PathCchConvertStyleA(certificate_store->file, strlen(certificate_store->file), PATH_STYLE_UNIX);
|
|
|
|
|
|
|
|
if (!ensure_path_exists(ConfigPath) || !ensure_path_exists(certificate_store->certs_path) ||
|
|
|
|
!ensure_path_exists(certificate_store->server_path))
|
|
|
|
goto fail;
|
2014-07-18 05:15:22 +04:00
|
|
|
|
2015-05-05 20:45:34 +03:00
|
|
|
return TRUE;
|
|
|
|
fail:
|
2015-06-09 16:33:13 +03:00
|
|
|
WLog_ERR(TAG, "certificate store initialization failed");
|
2020-11-18 09:51:45 +03:00
|
|
|
certificate_store_uninit(certificate_store);
|
2015-05-05 20:45:34 +03:00
|
|
|
return FALSE;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static int compare_pem(const char* current, const char* stored)
|
|
|
|
{
|
|
|
|
int rc = 1;
|
|
|
|
X509* xcur = NULL;
|
|
|
|
X509* xstore = NULL;
|
|
|
|
char* fpcur = NULL;
|
|
|
|
char* fpstore = NULL;
|
|
|
|
if (!current || !stored)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
xcur = crypto_cert_from_pem(current, strlen(current), FALSE);
|
|
|
|
xstore = crypto_cert_from_pem(stored, strlen(stored), FALSE);
|
|
|
|
if (!xcur || !xstore)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fpcur = crypto_cert_fingerprint(xcur);
|
|
|
|
fpstore = crypto_cert_fingerprint(xstore);
|
|
|
|
if (!fpcur || !fpstore)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = strcmp(fpcur, fpstore);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(fpcur);
|
|
|
|
free(fpstore);
|
|
|
|
X509_free(xcur);
|
|
|
|
X509_free(xstore);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:33:58 +03:00
|
|
|
static int certificate_data_match_raw(rdpCertificateStore* certificate_store,
|
2021-04-13 12:01:43 +03:00
|
|
|
const rdpCertificateData* certificate_data, char** psubject,
|
|
|
|
char** pissuer, char** fprint, char** ppem)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2015-06-09 17:12:41 +03:00
|
|
|
BOOL found = FALSE;
|
2015-12-09 20:29:41 +03:00
|
|
|
HANDLE fp;
|
2021-04-13 12:01:43 +03:00
|
|
|
char* data = NULL;
|
|
|
|
char* mdata = NULL;
|
|
|
|
char* pline = NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
int match = 1;
|
2015-12-09 20:29:41 +03:00
|
|
|
DWORD lowSize, highSize;
|
|
|
|
UINT64 size;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
DWORD read;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
|
|
|
fp = open_file(certificate_store->file, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if (fp == INVALID_HANDLE_VALUE)
|
2012-02-17 09:58:30 +04:00
|
|
|
return match;
|
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if ((lowSize = GetFileSize(fp, &highSize)) == INVALID_FILE_SIZE)
|
2015-07-13 15:16:04 +03:00
|
|
|
{
|
2021-04-13 11:39:01 +03:00
|
|
|
WLog_ERR(TAG, "GetFileSize(%s) returned %s [0x%08" PRIX32 "]", certificate_store->file,
|
|
|
|
strerror(errno), GetLastError());
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-07-13 15:16:04 +03:00
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
size = (UINT64)lowSize | ((UINT64)highSize << 32);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
if (size < 1)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
mdata = (char*)malloc(size + 2);
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2015-06-09 16:33:13 +03:00
|
|
|
if (!mdata)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2015-06-09 16:33:13 +03:00
|
|
|
data = mdata;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if (!ReadFile(fp, data, size, &read, NULL) || (read != size))
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
data[size] = '\n';
|
|
|
|
data[size + 1] = '\0';
|
2015-06-09 16:33:13 +03:00
|
|
|
pline = StrSep(&data, "\r\n");
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
while ((pline != NULL) && !found)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
size_t length = strlen(pline);
|
|
|
|
rdpCertificateData* cert = NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (length == 0)
|
|
|
|
goto next;
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (certificate_line_is_comment(pline, length))
|
|
|
|
goto next;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
cert = certificate_split_line(pline);
|
|
|
|
if (!cert)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "Invalid %s entry %s!", certificate_known_hosts_file, pline);
|
|
|
|
goto next;
|
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
|
|
|
const char* old = certificate_data_get_host(cert);
|
|
|
|
const char* cur = certificate_data_get_host(certificate_data);
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (strcmp(old, cur) != 0)
|
|
|
|
goto next;
|
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (certificate_data_get_port(cert) != certificate_data_get_port(certificate_data))
|
|
|
|
goto next;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
|
|
|
const char* fingerprint = certificate_data_get_fingerprint(cert);
|
|
|
|
const char* subject = certificate_data_get_subject(cert);
|
|
|
|
const char* issuer = certificate_data_get_issuer(cert);
|
|
|
|
const char* pem = certificate_data_get_pem(cert);
|
|
|
|
const char* cur_fp = certificate_data_get_fingerprint(certificate_data);
|
|
|
|
const char* cur_pem = certificate_data_get_pem(certificate_data);
|
|
|
|
|
|
|
|
duplicate(psubject, subject);
|
|
|
|
duplicate(pissuer, issuer);
|
|
|
|
duplicate(fprint, fingerprint);
|
|
|
|
duplicate(ppem, pem);
|
|
|
|
|
|
|
|
match = -1;
|
|
|
|
if (fingerprint && cur_fp)
|
|
|
|
match = (strcmp(cur_fp, fingerprint) == 0) ? 0 : -1;
|
|
|
|
|
|
|
|
if (cur_pem && pem)
|
|
|
|
match = compare_pem(cur_pem, pem);
|
|
|
|
|
|
|
|
found = TRUE;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
next:
|
|
|
|
certificate_data_free(cert);
|
2015-06-09 16:33:13 +03:00
|
|
|
pline = StrSep(&data, "\r\n");
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
fail:
|
2015-06-09 16:33:13 +03:00
|
|
|
free(mdata);
|
2021-04-13 12:01:43 +03:00
|
|
|
CloseHandle(fp);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static WCHAR* certificate_get_cert_file_name(rdpCertificateStore* store,
|
|
|
|
const rdpCertificateData* data)
|
|
|
|
{
|
|
|
|
size_t x, offset = 0;
|
|
|
|
char* pem = NULL;
|
|
|
|
WCHAR* wpem = NULL;
|
2021-06-18 10:58:46 +03:00
|
|
|
WINPR_DIGEST_CTX* ctx = NULL;
|
2021-04-13 12:01:43 +03:00
|
|
|
BYTE hash[WINPR_SHA3_256_DIGEST_LENGTH] = { 0 };
|
|
|
|
char fname[WINPR_SHA3_256_DIGEST_LENGTH * 2 + 6] = { 0 };
|
|
|
|
|
|
|
|
if (!store || !store->server_path || !data || !data->hostname)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ctx = winpr_Digest_New();
|
|
|
|
if (!ctx)
|
|
|
|
goto fail;
|
|
|
|
if (!winpr_Digest_Init(ctx, WINPR_MD_SHA256))
|
|
|
|
goto fail;
|
|
|
|
if (!winpr_Digest_Update(ctx, (const BYTE*)data->hostname, strlen(data->hostname)))
|
|
|
|
goto fail;
|
2021-06-18 10:58:46 +03:00
|
|
|
if (!winpr_Digest_Update(ctx, (const BYTE*)&data->port, sizeof(data->port)))
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
|
|
|
if (!winpr_Digest_Final(ctx, hash, sizeof(hash)))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (x = 0; x < sizeof(hash); x++)
|
|
|
|
{
|
|
|
|
int rc = _snprintf(&fname[offset], sizeof(fname) - offset, "%02x", hash[x]);
|
|
|
|
if (rc != 2)
|
|
|
|
goto fail;
|
|
|
|
offset += (size_t)rc;
|
|
|
|
}
|
|
|
|
_snprintf(&fname[offset], sizeof(fname) - offset, ".pem");
|
|
|
|
pem = GetCombinedPath(store->server_path, fname);
|
|
|
|
if (!pem)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ConvertToUnicode(CP_UTF8, 0, pem, -1, &wpem, 0);
|
|
|
|
fail:
|
|
|
|
free(pem);
|
|
|
|
winpr_Digest_Free(ctx);
|
|
|
|
return wpem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL duplicate(char** data, const char* value)
|
2015-06-11 00:33:58 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
char* tmp = NULL;
|
|
|
|
if (!data)
|
|
|
|
return FALSE;
|
|
|
|
if (value)
|
|
|
|
tmp = _strdup(value);
|
2015-06-11 10:58:34 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
free(*data);
|
|
|
|
*data = tmp;
|
|
|
|
if ((value == tmp) && (tmp == NULL))
|
2015-06-11 10:58:34 +03:00
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
return tmp != NULL;
|
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static rdpCertificateData* load_from_file(rdpCertificateStore* store, const char* hostname,
|
|
|
|
UINT16 port)
|
|
|
|
{
|
|
|
|
rdpCertificateData* loaded;
|
|
|
|
if (!store || !hostname)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
loaded = certificate_data_new(hostname, port);
|
|
|
|
if (!loaded)
|
|
|
|
return NULL;
|
|
|
|
if (!certificate_get_file_data(store, loaded))
|
|
|
|
{
|
|
|
|
certificate_data_free(loaded);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return loaded;
|
2015-06-11 00:33:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static BOOL certificate_get_file_data(rdpCertificateStore* store, rdpCertificateData* data)
|
2015-06-11 00:33:58 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
DWORD read;
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
HANDLE fp = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
X509* certificate = NULL;
|
|
|
|
LARGE_INTEGER position = { 0 };
|
|
|
|
const LARGE_INTEGER offset = { 0 };
|
|
|
|
WCHAR* fname = certificate_get_cert_file_name(store, data);
|
|
|
|
if (!fname)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fp = CreateFileW(fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (fp == INVALID_HANDLE_VALUE)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!SetFilePointerEx(fp, offset, &position, FILE_END))
|
|
|
|
goto fail;
|
|
|
|
if (!SetFilePointerEx(fp, offset, NULL, FILE_BEGIN))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
{
|
|
|
|
char* pem = realloc(data->pem, position.QuadPart + 1);
|
|
|
|
if (!pem)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data->pem = pem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFile(fp, data->pem, position.QuadPart, &read, NULL))
|
|
|
|
goto fail;
|
|
|
|
data->pem[read] = '\0';
|
|
|
|
|
|
|
|
certificate = crypto_cert_from_pem(data->pem, read, FALSE);
|
|
|
|
if (!certificate)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
free(data->fingerprint);
|
|
|
|
data->fingerprint = crypto_cert_fingerprint(certificate);
|
|
|
|
if (!data->fingerprint)
|
|
|
|
goto fail;
|
|
|
|
free(data->issuer);
|
|
|
|
data->issuer = crypto_cert_issuer(certificate);
|
|
|
|
if (!data->issuer)
|
|
|
|
goto fail;
|
|
|
|
free(data->subject);
|
|
|
|
data->subject = crypto_cert_subject(certificate);
|
|
|
|
if (!data->subject)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
CloseHandle(fp);
|
|
|
|
X509_free(certificate);
|
|
|
|
free(fname);
|
|
|
|
return rc;
|
2015-06-11 00:33:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static int certificate_match_data_file(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data)
|
2012-06-29 05:05:10 +04:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
int rc = 1;
|
|
|
|
const char* pem;
|
|
|
|
const char* loaded_pem;
|
|
|
|
|
|
|
|
rdpCertificateData* loaded =
|
|
|
|
load_from_file(certificate_store, certificate_data->hostname, certificate_data->port);
|
|
|
|
if (!loaded)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
pem = certificate_data_get_pem(certificate_data);
|
|
|
|
loaded_pem = certificate_data_get_pem(loaded);
|
|
|
|
if (!pem)
|
|
|
|
{
|
|
|
|
const char* fp = certificate_data_get_fingerprint(certificate_data);
|
|
|
|
const char* load_fp = certificate_data_get_fingerprint(loaded);
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
if (strcmp(fp, load_fp) == 0)
|
|
|
|
rc = 0;
|
|
|
|
else
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = compare_pem(pem, loaded_pem);
|
|
|
|
}
|
|
|
|
|
|
|
|
certificate_data_free(loaded);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-05-27 09:38:59 +03:00
|
|
|
static BOOL useKnownHosts(rdpCertificateStore* certificate_store)
|
|
|
|
{
|
|
|
|
BOOL use;
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(certificate_store);
|
2021-05-27 09:38:59 +03:00
|
|
|
|
|
|
|
use = freerdp_settings_get_bool(certificate_store->settings, FreeRDP_CertificateUseKnownHosts);
|
|
|
|
WLog_INFO(TAG, "known_hosts=%d", use);
|
|
|
|
return use;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define check_certificate_store_and_data(store, data) \
|
|
|
|
check_certificate_store_and_data_((store), (data), __FILE__, __FUNCTION__, __LINE__)
|
|
|
|
static BOOL check_certificate_store_and_data_(const rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data,
|
|
|
|
const char* file, const char* fkt, size_t line)
|
|
|
|
{
|
|
|
|
if (!certificate_store)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "[%s, %s:%" PRIuz "] certificate_store=NULL", file, fkt, line);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!certificate_data)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "[%s, %s:%" PRIuz "] certificate_data=NULL", file, fkt, line);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
int certificate_store_contains_data(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data)
|
|
|
|
{
|
2021-05-27 09:38:59 +03:00
|
|
|
if (!check_certificate_store_and_data(certificate_store, certificate_data))
|
|
|
|
return -1;
|
|
|
|
if (useKnownHosts(certificate_store))
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
|
|
|
char* pem = NULL;
|
|
|
|
int rc =
|
|
|
|
certificate_data_match_raw(certificate_store, certificate_data, NULL, NULL, NULL, &pem);
|
|
|
|
|
|
|
|
/* Upgrade entry, append PEM */
|
|
|
|
if ((rc == 0) && !pem && certificate_data->pem)
|
|
|
|
{
|
|
|
|
certificate_store_save_data(certificate_store, certificate_data);
|
|
|
|
}
|
|
|
|
free(pem);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return certificate_match_data_file(certificate_store, certificate_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* decode(const char* value)
|
|
|
|
{
|
|
|
|
size_t len, length;
|
|
|
|
char* converted = NULL;
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
|
|
|
len = strlen(value);
|
|
|
|
crypto_base64_decode(value, len, (BYTE**)&converted, &length);
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* encode(const char* value)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
|
|
|
len = strlen(value);
|
|
|
|
return (char*)crypto_base64_encode((BYTE*)value, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* allocated_printf(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
int rc1, rc2;
|
|
|
|
size_t size;
|
|
|
|
va_list ap;
|
|
|
|
char* buffer = NULL;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rc1 = vsnprintf(NULL, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (rc1 <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size = (size_t)rc1;
|
|
|
|
buffer = calloc(size + 2, sizeof(char));
|
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rc2 = vsnprintf(buffer, size + 1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (rc2 != rc1)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* certificate_data_get_host_file_entry(const rdpCertificateData* data)
|
|
|
|
{
|
|
|
|
char* buffer = NULL;
|
|
|
|
const char* hostname = certificate_data_get_host(data);
|
|
|
|
const UINT16 port = certificate_data_get_port(data);
|
|
|
|
char* subject = encode(certificate_data_get_subject(data));
|
|
|
|
char* issuer = encode(certificate_data_get_issuer(data));
|
|
|
|
const char* fingerprint = certificate_data_get_fingerprint(data);
|
|
|
|
char* pem = encode(certificate_data_get_pem(data));
|
|
|
|
|
2021-05-27 09:38:59 +03:00
|
|
|
/* Issuer and subject may be NULL */
|
|
|
|
if (!data || !hostname || !fingerprint)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
|
|
|
|
2021-05-27 09:38:59 +03:00
|
|
|
if (!subject)
|
|
|
|
subject = _strdup("");
|
|
|
|
if (!issuer)
|
|
|
|
issuer = _strdup("");
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (pem)
|
|
|
|
buffer = allocated_printf("%s %" PRIu16 " %s %s %s %s\n", hostname, port, fingerprint,
|
|
|
|
subject, issuer, pem);
|
|
|
|
else
|
|
|
|
buffer = allocated_printf("%s %" PRIu16 " %s %s %s\n", hostname, port, fingerprint, subject,
|
|
|
|
issuer);
|
|
|
|
fail:
|
|
|
|
free(subject);
|
|
|
|
free(issuer);
|
|
|
|
free(pem);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL write_line_and_free(const char* filename, HANDLE fp, char* line)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
DWORD size, written;
|
|
|
|
if ((fp == INVALID_HANDLE_VALUE) || !line)
|
|
|
|
return FALSE;
|
|
|
|
size = strlen(line);
|
|
|
|
rc = WriteFile(fp, line, size, &written, NULL);
|
|
|
|
|
|
|
|
if (!rc || (written != size))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WriteFile(%s) returned %s [0x%08X]", filename, strerror(errno), errno);
|
|
|
|
rc = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL certificate_data_replace_hosts_file(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data,
|
|
|
|
BOOL remove, BOOL append)
|
|
|
|
{
|
2021-05-27 09:38:59 +03:00
|
|
|
BOOL newfile = TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
HANDLE fp = INVALID_HANDLE_VALUE;
|
2015-06-10 10:15:28 +03:00
|
|
|
BOOL rc = FALSE;
|
2015-07-13 15:16:04 +03:00
|
|
|
size_t length;
|
2021-04-13 12:01:43 +03:00
|
|
|
char* data = NULL;
|
|
|
|
char* sdata = NULL;
|
|
|
|
char* pline = NULL;
|
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
UINT64 size;
|
2021-04-13 12:01:43 +03:00
|
|
|
DWORD read;
|
2015-12-09 20:29:41 +03:00
|
|
|
DWORD lowSize, highSize;
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateData* copy = NULL;
|
|
|
|
|
2021-05-27 09:38:59 +03:00
|
|
|
fp = open_file(certificate_store->file, GENERIC_READ | GENERIC_WRITE, 0, OPEN_ALWAYS,
|
2021-04-13 12:01:43 +03:00
|
|
|
FILE_ATTRIBUTE_NORMAL);
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if (fp == INVALID_HANDLE_VALUE)
|
2021-05-27 09:38:59 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to open known_hosts file %s, aborting", certificate_store->file);
|
2015-07-13 15:16:04 +03:00
|
|
|
return FALSE;
|
2021-05-27 09:38:59 +03:00
|
|
|
}
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
/* Create a copy, if a PEM was provided it will replace subject, issuer, fingerprint */
|
|
|
|
copy = certificate_data_new(certificate_data->hostname, certificate_data->port);
|
|
|
|
if (!copy)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (certificate_data->pem)
|
|
|
|
{
|
|
|
|
if (!certificate_data_set_pem(copy, certificate_data->pem))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!certificate_data_set_subject(copy, certificate_data->subject) ||
|
|
|
|
!certificate_data_set_issuer(copy, certificate_data->issuer) ||
|
|
|
|
!certificate_data_set_fingerprint(copy, certificate_data->fingerprint))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if ((lowSize = GetFileSize(fp, &highSize)) == INVALID_FILE_SIZE)
|
2015-06-10 11:34:02 +03:00
|
|
|
{
|
2021-04-13 11:39:01 +03:00
|
|
|
WLog_ERR(TAG, "GetFileSize(%s) returned %s [0x%08" PRIX32 "]", certificate_store->file,
|
|
|
|
strerror(errno), GetLastError());
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-07-13 15:16:04 +03:00
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
size = (UINT64)lowSize | ((UINT64)highSize << 32);
|
2015-07-13 15:16:04 +03:00
|
|
|
|
2021-05-27 09:38:59 +03:00
|
|
|
/* Newly created file, just write the new entry */
|
|
|
|
if (size > 0)
|
|
|
|
newfile = FALSE;
|
|
|
|
|
|
|
|
if (newfile)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
data = (char*)malloc(size + 2);
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2015-06-09 16:33:13 +03:00
|
|
|
if (!data)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if (!ReadFile(fp, data, size, &read, NULL) || (read != size))
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if (SetFilePointer(fp, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "SetFilePointer(%s) returned %s [0x%08" PRIX32 "]", certificate_store->file,
|
|
|
|
strerror(errno), GetLastError());
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-12-09 20:29:41 +03:00
|
|
|
}
|
2015-06-11 10:23:32 +03:00
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
if (!SetEndOfFile(fp))
|
2015-06-11 10:23:32 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "SetEndOfFile(%s) returned %s [0x%08" PRIX32 "]", certificate_store->file,
|
|
|
|
strerror(errno), GetLastError());
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-11 10:23:32 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 11:34:02 +03:00
|
|
|
/* Write the file back out, with appropriate fingerprint substitutions */
|
2012-06-29 05:05:10 +04:00
|
|
|
data[size] = '\n';
|
|
|
|
data[size + 1] = '\0';
|
2015-06-09 16:33:13 +03:00
|
|
|
sdata = data;
|
|
|
|
pline = StrSep(&sdata, "\r\n");
|
2012-06-29 05:05:10 +04:00
|
|
|
|
|
|
|
while (pline != NULL)
|
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateData* cert = NULL;
|
2012-06-29 05:05:10 +04:00
|
|
|
length = strlen(pline);
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2018-05-11 12:09:54 +03:00
|
|
|
if (certificate_line_is_comment(pline, length))
|
|
|
|
{
|
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
else if (!(cert = certificate_split_line(pline)))
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Skipping invalid %s entry %s!", certificate_known_hosts_file,
|
|
|
|
pline);
|
2012-06-29 05:05:10 +04:00
|
|
|
else
|
2015-06-09 16:33:13 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
char* line;
|
2019-02-07 16:18:02 +03:00
|
|
|
|
2015-06-09 16:33:13 +03:00
|
|
|
/* If this is the replaced hostname, use the updated fingerprint. */
|
2021-04-13 12:01:43 +03:00
|
|
|
if ((strcmp(certificate_data_get_host(cert), certificate_data_get_host(copy)) ==
|
|
|
|
0) &&
|
|
|
|
(certificate_data_get_port(cert) == certificate_data_get_port(copy)))
|
2015-06-10 10:15:28 +03:00
|
|
|
{
|
|
|
|
rc = TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
if (remove && !append)
|
|
|
|
goto next;
|
2015-12-09 20:29:41 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
line = certificate_data_get_host_file_entry(copy);
|
2019-02-07 16:18:02 +03:00
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
else
|
|
|
|
line = certificate_data_get_host_file_entry(cert);
|
2019-02-07 16:18:02 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!line)
|
|
|
|
goto next;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!write_line_and_free(certificate_store->file, fp, line))
|
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
}
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
next:
|
|
|
|
certificate_data_free(cert);
|
2015-06-09 16:33:13 +03:00
|
|
|
pline = StrSep(&sdata, "\r\n");
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
fail:
|
|
|
|
if (!rc && append)
|
|
|
|
{
|
2021-05-27 09:38:59 +03:00
|
|
|
if (fp != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
char* line = certificate_data_get_host_file_entry(copy);
|
|
|
|
if (line)
|
|
|
|
rc = write_line_and_free(certificate_store->file, fp, line);
|
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2015-12-09 20:29:41 +03:00
|
|
|
CloseHandle(fp);
|
2015-06-09 16:33:13 +03:00
|
|
|
free(data);
|
2021-04-13 12:01:43 +03:00
|
|
|
certificate_data_free(copy);
|
2015-06-10 10:15:28 +03:00
|
|
|
return rc;
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static BOOL certificate_data_write_to_file(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
size_t len;
|
|
|
|
HANDLE handle;
|
|
|
|
DWORD numberOfBytesWritten;
|
|
|
|
WCHAR* fname = certificate_get_cert_file_name(certificate_store, certificate_data);
|
|
|
|
if (!fname)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
handle = CreateFileW(fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
|
|
goto fail;
|
|
|
|
len = strlen(certificate_data->pem);
|
|
|
|
rc = WriteFile(handle, certificate_data->pem, len, &numberOfBytesWritten, NULL);
|
|
|
|
CloseHandle(handle);
|
|
|
|
fail:
|
|
|
|
free(fname);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL certificate_data_remove_file(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
WCHAR* fname = certificate_get_cert_file_name(certificate_store, certificate_data);
|
|
|
|
if (!fname)
|
|
|
|
return FALSE;
|
|
|
|
if (PathFileExistsW(fname))
|
|
|
|
rc = DeleteFileW(fname);
|
|
|
|
else
|
|
|
|
rc = TRUE;
|
|
|
|
free(fname);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL certificate_store_remove_data(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data)
|
|
|
|
{
|
2021-05-27 09:38:59 +03:00
|
|
|
if (!check_certificate_store_and_data(certificate_store, certificate_data))
|
|
|
|
return FALSE;
|
|
|
|
if (useKnownHosts(certificate_store))
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
|
|
|
/* Ignore return, if the entry was invalid just continue */
|
|
|
|
certificate_data_replace_hosts_file(certificate_store, certificate_data, TRUE, FALSE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return certificate_data_remove_file(certificate_store, certificate_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpCertificateData* certificate_split_line(char* line)
|
2015-06-09 16:33:13 +03:00
|
|
|
{
|
2018-11-30 11:55:10 +03:00
|
|
|
char* cur;
|
2021-04-13 12:01:43 +03:00
|
|
|
char* host = NULL;
|
|
|
|
char* subject = NULL;
|
|
|
|
char* issuer = NULL;
|
|
|
|
char* fingerprint = NULL;
|
|
|
|
char* pem = NULL;
|
|
|
|
UINT16 port = 0;
|
|
|
|
rdpCertificateData* data = NULL;
|
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
size_t length = strlen(line);
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
if (length <= 0)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
cur = StrSep(&line, " \t");
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
if (!cur)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
host = cur;
|
2018-11-30 11:55:10 +03:00
|
|
|
cur = StrSep(&line, " \t");
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
if (!cur)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (sscanf(cur, "%hu", &port) != 1)
|
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
cur = StrSep(&line, " \t");
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
if (!cur)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
fingerprint = cur;
|
2018-11-30 11:55:10 +03:00
|
|
|
cur = StrSep(&line, " \t");
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
if (!cur)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
subject = cur;
|
2018-11-30 11:55:10 +03:00
|
|
|
cur = StrSep(&line, " \t");
|
|
|
|
|
|
|
|
if (!cur)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
issuer = cur;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
/* Optional field */
|
|
|
|
cur = StrSep(&line, " \t");
|
|
|
|
if (cur)
|
|
|
|
pem = cur;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
data = certificate_data_new(host, port);
|
|
|
|
if (!data)
|
|
|
|
goto fail;
|
|
|
|
if (pem)
|
2015-07-13 15:16:04 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
BOOL rc;
|
|
|
|
char* dpem = NULL;
|
|
|
|
size_t length;
|
|
|
|
crypto_base64_decode(pem, strlen(pem), (BYTE**)&dpem, &length);
|
|
|
|
rc = certificate_data_set_pem(data, dpem);
|
|
|
|
free(dpem);
|
|
|
|
if (!rc)
|
|
|
|
goto fail;
|
2015-07-13 15:16:04 +03:00
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOL rc;
|
|
|
|
size_t length;
|
|
|
|
char* dsubject = NULL;
|
|
|
|
char* dissuer = NULL;
|
|
|
|
crypto_base64_decode(subject, strlen(subject), (BYTE**)&dsubject, &length);
|
|
|
|
crypto_base64_decode(issuer, strlen(issuer), (BYTE**)&dissuer, &length);
|
|
|
|
|
|
|
|
rc = certificate_data_set_subject(data, dsubject) &&
|
|
|
|
certificate_data_set_issuer(data, dissuer) &&
|
|
|
|
certificate_data_set_fingerprint(data, fingerprint);
|
|
|
|
free(dsubject);
|
|
|
|
free(dissuer);
|
|
|
|
if (!rc)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
fail:
|
|
|
|
certificate_data_free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-07-13 15:16:04 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static BOOL update_from_pem(rdpCertificateData* data)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
char* subject = NULL;
|
|
|
|
char* issuer = NULL;
|
|
|
|
char* fingerprint = NULL;
|
|
|
|
X509* x1 = NULL;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!data || !data->pem)
|
2015-07-13 15:16:04 +03:00
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
x1 = crypto_cert_from_pem(data->pem, strlen(data->pem), FALSE);
|
|
|
|
if (!x1)
|
|
|
|
goto fail;
|
2021-05-27 09:38:59 +03:00
|
|
|
|
|
|
|
/* Subject and issuer might be NULL */
|
2021-04-13 12:01:43 +03:00
|
|
|
subject = crypto_cert_subject(x1);
|
|
|
|
issuer = crypto_cert_issuer(x1);
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
fingerprint = crypto_cert_fingerprint(x1);
|
|
|
|
if (!fingerprint)
|
|
|
|
goto fail;
|
|
|
|
duplicate(&data->subject, subject);
|
|
|
|
duplicate(&data->issuer, issuer);
|
|
|
|
duplicate(&data->fingerprint, fingerprint);
|
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
free(subject);
|
|
|
|
free(issuer);
|
|
|
|
free(fingerprint);
|
|
|
|
X509_free(x1);
|
|
|
|
return rc;
|
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
BOOL certificate_store_save_data(rdpCertificateStore* certificate_store,
|
|
|
|
const rdpCertificateData* certificate_data)
|
|
|
|
{
|
2021-05-27 09:38:59 +03:00
|
|
|
if (!check_certificate_store_and_data(certificate_store, certificate_data))
|
|
|
|
return FALSE;
|
|
|
|
if (useKnownHosts(certificate_store))
|
2021-04-13 12:01:43 +03:00
|
|
|
return certificate_data_replace_hosts_file(certificate_store, certificate_data, TRUE, TRUE);
|
|
|
|
else
|
|
|
|
return certificate_data_write_to_file(certificate_store, certificate_data);
|
|
|
|
}
|
2019-02-07 16:18:02 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateData* certificate_store_load_data(rdpCertificateStore* certificate_store,
|
|
|
|
const char* host, UINT16 port)
|
|
|
|
{
|
2021-05-27 09:38:59 +03:00
|
|
|
if (useKnownHosts(certificate_store))
|
2015-12-09 20:29:41 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
int rc;
|
|
|
|
rdpCertificateData* data = certificate_data_new(host, port);
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rc = certificate_data_match_raw(certificate_store, data, &data->subject, &data->issuer,
|
|
|
|
&data->fingerprint, &data->pem);
|
|
|
|
if ((rc == 0) || (rc == -1))
|
|
|
|
return data;
|
|
|
|
certificate_data_free(data);
|
|
|
|
return NULL;
|
2015-12-09 20:29:41 +03:00
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
else
|
2015-07-13 15:16:04 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
return load_from_file(certificate_store, host, port);
|
2015-07-13 15:16:04 +03:00
|
|
|
}
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateData* certificate_data_new(const char* hostname, UINT16 port)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2015-07-13 16:52:06 +03:00
|
|
|
size_t i;
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateData* certdata = NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2015-06-11 12:34:22 +03:00
|
|
|
if (!hostname)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2015-06-11 12:34:22 +03:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
certdata = (rdpCertificateData*)calloc(1, sizeof(rdpCertificateData));
|
|
|
|
|
2014-03-26 02:13:08 +04:00
|
|
|
if (!certdata)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2014-03-26 02:13:08 +04:00
|
|
|
|
2015-06-09 16:33:13 +03:00
|
|
|
certdata->port = port;
|
2014-03-26 02:13:08 +04:00
|
|
|
certdata->hostname = _strdup(hostname);
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!certdata->hostname)
|
2015-06-11 12:16:45 +03:00
|
|
|
goto fail;
|
2018-11-30 11:55:10 +03:00
|
|
|
for (i = 0; i < strlen(hostname); i++)
|
2015-07-13 16:52:06 +03:00
|
|
|
certdata->hostname[i] = tolower(certdata->hostname[i]);
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
return certdata;
|
2015-06-11 12:16:45 +03:00
|
|
|
fail:
|
2021-04-13 12:01:43 +03:00
|
|
|
certificate_data_free(certdata);
|
2014-03-26 02:13:08 +04:00
|
|
|
return NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void certificate_data_free(rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
if (certificate_data != NULL)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(certificate_data->hostname);
|
2015-06-11 12:16:45 +03:00
|
|
|
free(certificate_data->subject);
|
|
|
|
free(certificate_data->issuer);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(certificate_data->fingerprint);
|
2021-04-13 12:01:43 +03:00
|
|
|
free(certificate_data->pem);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(certificate_data);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
const char* certificate_data_get_host(const rdpCertificateData* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
return cert->hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT16 certificate_data_get_port(const rdpCertificateData* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return 0;
|
|
|
|
return cert->port;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL certificate_data_set_pem(rdpCertificateData* cert, const char* pem)
|
|
|
|
{
|
|
|
|
if (!cert)
|
2021-05-27 09:38:59 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
return FALSE;
|
2021-05-27 09:38:59 +03:00
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!duplicate(&cert->pem, pem))
|
|
|
|
return FALSE;
|
|
|
|
if (!pem)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return update_from_pem(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL certificate_data_set_subject(rdpCertificateData* cert, const char* subject)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return FALSE;
|
|
|
|
return duplicate(&cert->subject, subject);
|
|
|
|
}
|
|
|
|
BOOL certificate_data_set_issuer(rdpCertificateData* cert, const char* issuer)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return FALSE;
|
|
|
|
return duplicate(&cert->issuer, issuer);
|
|
|
|
}
|
|
|
|
BOOL certificate_data_set_fingerprint(rdpCertificateData* cert, const char* fingerprint)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return FALSE;
|
|
|
|
return duplicate(&cert->fingerprint, fingerprint);
|
|
|
|
}
|
|
|
|
const char* certificate_data_get_pem(const rdpCertificateData* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
return cert->pem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* certificate_data_get_subject(const rdpCertificateData* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
return cert->subject;
|
|
|
|
}
|
|
|
|
const char* certificate_data_get_issuer(const rdpCertificateData* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
return cert->issuer;
|
|
|
|
}
|
|
|
|
const char* certificate_data_get_fingerprint(const rdpCertificateData* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
return cert->fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpCertificateStore* certificate_store_new(const rdpSettings* settings)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateStore* certificate_store =
|
|
|
|
(rdpCertificateStore*)calloc(1, sizeof(rdpCertificateStore));
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
if (!certificate_store)
|
|
|
|
return NULL;
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
certificate_store->settings = settings;
|
2014-07-18 05:15:22 +04:00
|
|
|
|
2015-05-05 20:45:34 +03:00
|
|
|
if (!certificate_store_init(certificate_store))
|
|
|
|
{
|
2021-05-18 13:16:21 +03:00
|
|
|
certificate_store_free(certificate_store);
|
2015-05-05 20:45:34 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
return certificate_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
void certificate_store_free(rdpCertificateStore* certstore)
|
|
|
|
{
|
2020-11-18 09:51:45 +03:00
|
|
|
certificate_store_uninit(certstore);
|
2021-04-13 12:01:43 +03:00
|
|
|
free(certstore);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* certificate_store_get_hosts_file(const rdpCertificateStore* certificate_store)
|
|
|
|
{
|
|
|
|
if (!certificate_store)
|
|
|
|
return NULL;
|
|
|
|
return certificate_store->file;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* certificate_store_get_certs_path(const rdpCertificateStore* certificate_store)
|
|
|
|
{
|
|
|
|
if (!certificate_store)
|
|
|
|
return NULL;
|
|
|
|
return certificate_store->certs_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* certificate_store_get_hosts_path(const rdpCertificateStore* certificate_store)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!certificate_store)
|
|
|
|
return NULL;
|
|
|
|
return certificate_store->server_path;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|