2011-07-04 01:29:09 +04:00
|
|
|
/**
|
2012-02-21 09:56:55 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-04 01:29:09 +04:00
|
|
|
* Transport Layer Security
|
|
|
|
*
|
2012-02-21 09:56:55 +04:00
|
|
|
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2011-07-04 01:29:09 +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
|
|
|
|
*
|
2011-08-29 00:46:36 +04:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-04 01:29:09 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2015-03-10 23:48:35 +03:00
|
|
|
#include <string.h>
|
2017-11-14 18:10:52 +03:00
|
|
|
#include <errno.h>
|
2013-08-30 16:19:50 +04:00
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
#include <winpr/crt.h>
|
2018-08-24 10:54:25 +03:00
|
|
|
#include <winpr/string.h>
|
2013-01-09 09:20:08 +04:00
|
|
|
#include <winpr/sspi.h>
|
2014-07-28 23:55:57 +04:00
|
|
|
#include <winpr/ssl.h>
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2014-05-21 19:32:14 +04:00
|
|
|
#include <freerdp/utils/ringbuffer.h>
|
2011-07-04 01:29:09 +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/tls.h>
|
2014-05-21 19:32:14 +04:00
|
|
|
#include "../core/tcp.h"
|
2016-11-21 19:28:54 +03:00
|
|
|
#include "opensslcompat.h"
|
2014-05-12 20:01:29 +04:00
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
2015-03-17 23:09:17 +03:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("crypto")
|
2014-07-02 17:15:46 +04:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
/**
|
|
|
|
* Earlier Microsoft iOS RDP clients have sent a null or even double null
|
|
|
|
* terminated hostname in the SNI TLS extension.
|
|
|
|
* If the length indicator does not equal the hostname strlen OpenSSL
|
|
|
|
* will abort (see openssl:ssl/t1_lib.c).
|
|
|
|
* Here is a tcpdump segment of Microsoft Remote Desktop Client Version
|
|
|
|
* 8.1.7 running on an iPhone 4 with iOS 7.1.2 showing the transmitted
|
|
|
|
* SNI hostname TLV blob when connection to server "abcd":
|
|
|
|
* 00 name_type 0x00 (host_name)
|
|
|
|
* 00 06 length_in_bytes 0x0006
|
|
|
|
* 61 62 63 64 00 00 host_name "abcd\0\0"
|
|
|
|
*
|
|
|
|
* Currently the only (runtime) workaround is setting an openssl tls
|
|
|
|
* extension debug callback that sets the SSL context's servername_done
|
|
|
|
* to 1 which effectively disables the parsing of that extension type.
|
|
|
|
*
|
|
|
|
* Nowadays this workaround is not required anymore but still can be
|
|
|
|
* activated by adding the following define:
|
|
|
|
*
|
|
|
|
* #define MICROSOFT_IOS_SNI_BUG
|
|
|
|
*/
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
SSL* ssl;
|
2015-09-16 02:59:41 +03:00
|
|
|
CRITICAL_SECTION lock;
|
2022-02-14 16:59:22 +03:00
|
|
|
} BIO_RDP_TLS;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2022-06-23 09:34:20 +03:00
|
|
|
static BOOL tls_prep(rdpTls* tls, BIO* underlying, int options, BOOL clientMode);
|
2018-11-30 11:55:10 +03:00
|
|
|
static int tls_verify_certificate(rdpTls* tls, CryptoCert cert, const char* hostname, UINT16 port);
|
|
|
|
static void tls_print_certificate_name_mismatch_error(const char* hostname, UINT16 port,
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* common_name, char** alt_names,
|
|
|
|
int alt_names_count);
|
2018-11-30 11:55:10 +03:00
|
|
|
static void tls_print_certificate_error(const char* hostname, UINT16 port, const char* fingerprint,
|
|
|
|
const char* hosts_file);
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
static int bio_rdp_tls_write(BIO* bio, const char* buf, int size)
|
|
|
|
{
|
2015-09-16 02:59:41 +03:00
|
|
|
int error;
|
2014-06-02 05:37:20 +04:00
|
|
|
int status;
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (!buf || !tls)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL);
|
2015-09-16 02:59:41 +03:00
|
|
|
EnterCriticalSection(&tls->lock);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = SSL_write(tls->ssl, buf, size);
|
2015-09-16 02:59:41 +03:00
|
|
|
error = SSL_get_error(tls->ssl, status);
|
|
|
|
LeaveCriticalSection(&tls->lock);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
{
|
2015-09-16 02:59:41 +03:00
|
|
|
switch (error)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-07-15 07:36:35 +04:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-07-15 07:36:35 +04:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_X509_LOOKUP:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, BIO_RR_CONNECT);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2015-03-25 11:06:32 +03:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bio_rdp_tls_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
2015-09-16 02:59:41 +03:00
|
|
|
int error;
|
2014-06-02 05:37:20 +04:00
|
|
|
int status;
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (!buf || !tls)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL);
|
2015-09-16 02:59:41 +03:00
|
|
|
EnterCriticalSection(&tls->lock);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = SSL_read(tls->ssl, buf, size);
|
2015-09-16 02:59:41 +03:00
|
|
|
error = SSL_get_error(tls->ssl, status);
|
|
|
|
LeaveCriticalSection(&tls->lock);
|
2015-10-16 10:50:18 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
2015-09-16 02:59:41 +03:00
|
|
|
switch (error)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-07-15 07:36:35 +04:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-07-15 07:36:35 +04:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_X509_LOOKUP:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_ACCEPT:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, BIO_RR_ACCEPT);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, BIO_RR_CONNECT);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2015-03-25 11:06:32 +03:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 23:09:17 +03:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-03-17 23:09:17 +03:00
|
|
|
if (status > 0)
|
|
|
|
{
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(buf, status);
|
|
|
|
}
|
|
|
|
|
2016-09-20 09:58:04 +03:00
|
|
|
#endif
|
2014-06-02 05:37:20 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bio_rdp_tls_puts(BIO* bio, const char* str)
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t size;
|
2014-06-02 05:37:20 +04:00
|
|
|
int status;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size = strlen(str);
|
2022-07-02 16:53:10 +03:00
|
|
|
ERR_clear_error();
|
2014-06-02 05:37:20 +04:00
|
|
|
status = BIO_write(bio, str, size);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bio_rdp_tls_gets(BIO* bio, char* str, int size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr)
|
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO* ssl_rbio;
|
|
|
|
BIO* ssl_wbio;
|
|
|
|
BIO* next_bio;
|
2014-06-02 05:37:20 +04:00
|
|
|
int status = -1;
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
|
2016-11-21 19:28:54 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (!tls)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!tls->ssl && (cmd != BIO_C_SET_SSL))
|
|
|
|
return 0;
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
next_bio = BIO_next(bio);
|
|
|
|
ssl_rbio = tls->ssl ? SSL_get_rbio(tls->ssl) : NULL;
|
|
|
|
ssl_wbio = tls->ssl ? SSL_get_wbio(tls->ssl) : NULL;
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case BIO_CTRL_RESET:
|
|
|
|
SSL_shutdown(tls->ssl);
|
2016-11-24 16:53:19 +03:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (SSL_in_connect_init(tls->ssl))
|
|
|
|
SSL_set_connect_state(tls->ssl);
|
|
|
|
else if (SSL_in_accept_init(tls->ssl))
|
|
|
|
SSL_set_accept_state(tls->ssl);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
SSL_clear(tls->ssl);
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (next_bio)
|
|
|
|
status = BIO_ctrl(next_bio, cmd, num, ptr);
|
|
|
|
else if (ssl_rbio)
|
|
|
|
status = BIO_ctrl(ssl_rbio, cmd, num, ptr);
|
2014-06-02 05:37:20 +04:00
|
|
|
else
|
|
|
|
status = 1;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_GET_FD:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_ctrl(ssl_rbio, cmd, num, ptr);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_INFO:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_SET_CALLBACK:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_GET_CALLBACK:
|
2019-11-06 17:24:51 +03:00
|
|
|
*((ULONG_PTR*)ptr) = (ULONG_PTR)SSL_get_info_callback(tls->ssl);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_SSL_MODE:
|
|
|
|
if (num)
|
|
|
|
SSL_set_connect_state(tls->ssl);
|
|
|
|
else
|
|
|
|
SSL_set_accept_state(tls->ssl);
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_GET_CLOSE:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_get_shutdown(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_SET_CLOSE:
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_set_shutdown(bio, (int)num);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_WPENDING:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_ctrl(ssl_wbio, cmd, num, ptr);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_PENDING:
|
|
|
|
status = SSL_pending(tls->ssl);
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status == 0)
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_pending(ssl_rbio);
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
BIO_clear_retry_flags(bio);
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_ctrl(ssl_wbio, cmd, num, ptr);
|
2022-04-28 11:49:42 +03:00
|
|
|
if (status != 1)
|
|
|
|
WLog_DBG(TAG, "BIO_ctrl returned %d", status);
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_copy_next_retry(bio);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_PUSH:
|
2016-11-21 19:28:54 +03:00
|
|
|
if (next_bio && (next_bio != ssl_rbio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2021-05-23 16:03:45 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
|
|
|
|
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
|
2016-11-21 19:28:54 +03:00
|
|
|
SSL_set_bio(tls->ssl, next_bio, next_bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO);
|
2016-11-21 19:28:54 +03:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* We are going to pass ownership of next to the SSL object...but
|
|
|
|
* we don't own a reference to pass yet - so up ref
|
|
|
|
*/
|
|
|
|
BIO_up_ref(next_bio);
|
|
|
|
SSL_set_bio(tls->ssl, next_bio, next_bio);
|
|
|
|
#endif
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_POP:
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
/* Only detach if we are the BIO explicitly being popped */
|
2014-06-02 05:37:20 +04:00
|
|
|
if (bio == ptr)
|
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
if (ssl_rbio != ssl_wbio)
|
|
|
|
BIO_free_all(ssl_wbio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2021-05-23 16:03:45 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
|
|
|
|
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (next_bio)
|
2014-06-02 05:37:20 +04:00
|
|
|
CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
tls->ssl->wbio = tls->ssl->rbio = NULL;
|
2016-11-21 19:28:54 +03:00
|
|
|
#else
|
|
|
|
/* OpenSSL 1.1: This will also clear the reference we obtained during push */
|
|
|
|
SSL_set_bio(tls->ssl, NULL, NULL);
|
|
|
|
#endif
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_GET_SSL:
|
|
|
|
if (ptr)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
*((SSL**)ptr) = tls->ssl;
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_SET_SSL:
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_set_shutdown(bio, (int)num);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (ptr)
|
2016-11-21 19:28:54 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
tls->ssl = (SSL*)ptr;
|
2016-11-21 19:28:54 +03:00
|
|
|
ssl_rbio = SSL_get_rbio(tls->ssl);
|
|
|
|
}
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (ssl_rbio)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
if (next_bio)
|
|
|
|
BIO_push(ssl_rbio, next_bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_next(bio, ssl_rbio);
|
2021-05-23 16:03:45 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
|
|
|
|
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
|
2016-11-21 19:28:54 +03:00
|
|
|
CRYPTO_add(&(ssl_rbio->references), 1, CRYPTO_LOCK_BIO);
|
|
|
|
#else
|
|
|
|
BIO_up_ref(ssl_rbio);
|
|
|
|
#endif
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_init(bio, 1);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_DO_STATE_MACHINE:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, 0);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = SSL_do_handshake(tls->ssl);
|
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
{
|
|
|
|
switch (SSL_get_error(tls->ssl, status))
|
|
|
|
{
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_retry_reason(bio, BIO_get_retry_reason(next_bio));
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_ctrl(ssl_rbio, cmd, num, ptr);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bio_rdp_tls_new(BIO* bio)
|
|
|
|
{
|
|
|
|
BIO_RDP_TLS* tls;
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
|
|
|
|
if (!(tls = calloc(1, sizeof(BIO_RDP_TLS))))
|
2014-06-02 05:37:20 +04:00
|
|
|
return 0;
|
|
|
|
|
2015-09-16 02:59:41 +03:00
|
|
|
InitializeCriticalSectionAndSpinCount(&tls->lock, 4000);
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_set_data(bio, (void*)tls);
|
2014-06-02 05:37:20 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bio_rdp_tls_free(BIO* bio)
|
|
|
|
{
|
|
|
|
BIO_RDP_TLS* tls;
|
|
|
|
|
|
|
|
if (!bio)
|
|
|
|
return 0;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
tls = (BIO_RDP_TLS*)BIO_get_data(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (!tls)
|
|
|
|
return 0;
|
|
|
|
|
2020-07-23 11:48:39 +03:00
|
|
|
BIO_set_data(bio, NULL);
|
2016-11-21 19:28:54 +03:00
|
|
|
if (BIO_get_shutdown(bio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
if (BIO_get_init(bio) && tls->ssl)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
SSL_shutdown(tls->ssl);
|
|
|
|
SSL_free(tls->ssl);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_init(bio, 0);
|
|
|
|
BIO_set_flags(bio, 0);
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
2015-09-16 02:59:41 +03:00
|
|
|
DeleteCriticalSection(&tls->lock);
|
2014-06-02 05:37:20 +04:00
|
|
|
free(tls);
|
2020-07-23 11:48:39 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long bio_rdp_tls_callback_ctrl(BIO* bio, int cmd, bio_info_cb* fp)
|
|
|
|
{
|
2019-02-21 15:50:16 +03:00
|
|
|
long status = 0;
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_RDP_TLS* tls;
|
|
|
|
|
|
|
|
if (!bio)
|
|
|
|
return 0;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
tls = (BIO_RDP_TLS*)BIO_get_data(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (!tls)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case BIO_CTRL_SET_CALLBACK:
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
|
|
|
typedef void (*fkt_t)(const SSL*, int, int);
|
|
|
|
/* Documented since https://www.openssl.org/docs/man1.1.1/man3/BIO_set_callback.html
|
|
|
|
* the argument is not really of type bio_info_cb* and must be cast
|
|
|
|
* to the required type */
|
|
|
|
fkt_t fkt = (fkt_t)(void*)fp;
|
|
|
|
SSL_set_info_callback(tls->ssl, fkt);
|
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
break;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
default:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_callback_ctrl(SSL_get_rbio(tls->ssl), cmd, fp);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define BIO_TYPE_RDP_TLS 68
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2018-01-19 12:59:10 +03:00
|
|
|
static BIO_METHOD* BIO_s_rdp_tls(void)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
static BIO_METHOD* bio_methods = NULL;
|
|
|
|
|
|
|
|
if (bio_methods == NULL)
|
|
|
|
{
|
|
|
|
if (!(bio_methods = BIO_meth_new(BIO_TYPE_RDP_TLS, "RdpTls")))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIO_meth_set_write(bio_methods, bio_rdp_tls_write);
|
|
|
|
BIO_meth_set_read(bio_methods, bio_rdp_tls_read);
|
|
|
|
BIO_meth_set_puts(bio_methods, bio_rdp_tls_puts);
|
|
|
|
BIO_meth_set_gets(bio_methods, bio_rdp_tls_gets);
|
|
|
|
BIO_meth_set_ctrl(bio_methods, bio_rdp_tls_ctrl);
|
|
|
|
BIO_meth_set_create(bio_methods, bio_rdp_tls_new);
|
|
|
|
BIO_meth_set_destroy(bio_methods, bio_rdp_tls_free);
|
|
|
|
BIO_meth_set_callback_ctrl(bio_methods, bio_rdp_tls_callback_ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bio_methods;
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
2018-01-19 12:59:10 +03:00
|
|
|
static BIO* BIO_new_rdp_tls(SSL_CTX* ctx, int client)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
BIO* bio;
|
|
|
|
SSL* ssl;
|
|
|
|
bio = BIO_new(BIO_s_rdp_tls());
|
|
|
|
|
|
|
|
if (!bio)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ssl = SSL_new(ctx);
|
|
|
|
|
|
|
|
if (!ssl)
|
|
|
|
{
|
2018-11-08 14:09:49 +03:00
|
|
|
BIO_free_all(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client)
|
|
|
|
SSL_set_connect_state(ssl);
|
|
|
|
else
|
|
|
|
SSL_set_accept_state(ssl);
|
|
|
|
|
|
|
|
BIO_set_ssl(bio, ssl, BIO_CLOSE);
|
|
|
|
return bio;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static CryptoCert tls_get_certificate(rdpTls* tls, BOOL peer)
|
2012-02-12 22:05:56 +04:00
|
|
|
{
|
|
|
|
CryptoCert cert;
|
2014-05-21 19:32:14 +04:00
|
|
|
X509* remote_cert;
|
2019-11-06 17:24:51 +03:00
|
|
|
STACK_OF(X509) * chain;
|
2012-02-12 22:05:56 +04:00
|
|
|
|
2012-03-19 04:08:05 +04:00
|
|
|
if (peer)
|
2014-05-21 19:32:14 +04:00
|
|
|
remote_cert = SSL_get_peer_certificate(tls->ssl);
|
2012-03-19 04:08:05 +04:00
|
|
|
else
|
2016-09-20 09:58:04 +03:00
|
|
|
remote_cert = X509_dup(SSL_get_certificate(tls->ssl));
|
2012-02-12 22:05:56 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!remote_cert)
|
2012-02-12 22:05:56 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "failed to get the server TLS certificate");
|
2014-05-21 19:32:14 +04:00
|
|
|
return NULL;
|
2012-02-12 22:05:56 +04:00
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
cert = malloc(sizeof(*cert));
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!cert)
|
2012-02-12 22:05:56 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
X509_free(remote_cert);
|
|
|
|
return NULL;
|
2012-02-12 22:05:56 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
cert->px509 = remote_cert;
|
2015-03-13 20:50:54 +03:00
|
|
|
/* Get the peer's chain. If it does not exist, we're setting NULL (clean data either way) */
|
|
|
|
chain = SSL_get_peer_cert_chain(tls->ssl);
|
|
|
|
cert->px509chain = chain;
|
2012-02-12 22:05:56 +04:00
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tls_free_certificate(CryptoCert cert)
|
|
|
|
{
|
|
|
|
X509_free(cert->px509);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(cert);
|
2012-02-12 22:05:56 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define TLS_SERVER_END_POINT "tls-server-end-point:"
|
2013-01-09 09:20:08 +04:00
|
|
|
|
2018-01-19 12:59:10 +03:00
|
|
|
static SecPkgContext_Bindings* tls_get_channel_bindings(X509* cert)
|
2013-01-09 09:20:08 +04:00
|
|
|
{
|
|
|
|
UINT32 CertificateHashLength;
|
|
|
|
BYTE* ChannelBindingToken;
|
|
|
|
UINT32 ChannelBindingTokenLength;
|
|
|
|
SEC_CHANNEL_BINDINGS* ChannelBindings;
|
|
|
|
SecPkgContext_Bindings* ContextBindings;
|
2018-09-03 09:48:33 +03:00
|
|
|
const size_t PrefixLength = strnlen(TLS_SERVER_END_POINT, ARRAYSIZE(TLS_SERVER_END_POINT));
|
2020-02-12 15:47:35 +03:00
|
|
|
BYTE* CertificateHash = crypto_cert_hash(cert, "sha256", &CertificateHashLength);
|
|
|
|
if (!CertificateHash)
|
|
|
|
return NULL;
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
ChannelBindingTokenLength = PrefixLength + CertificateHashLength;
|
2019-11-06 17:24:51 +03:00
|
|
|
ContextBindings = (SecPkgContext_Bindings*)calloc(1, sizeof(SecPkgContext_Bindings));
|
2013-01-09 09:20:08 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!ContextBindings)
|
2020-02-12 15:47:35 +03:00
|
|
|
goto out_free;
|
2013-01-09 09:20:08 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ContextBindings->BindingsLength = sizeof(SEC_CHANNEL_BINDINGS) + ChannelBindingTokenLength;
|
|
|
|
ChannelBindings = (SEC_CHANNEL_BINDINGS*)calloc(1, ContextBindings->BindingsLength);
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!ChannelBindings)
|
|
|
|
goto out_free;
|
2013-01-09 09:20:08 +04:00
|
|
|
|
2016-09-20 09:58:04 +03:00
|
|
|
ContextBindings->Bindings = ChannelBindings;
|
2013-01-09 09:20:08 +04:00
|
|
|
ChannelBindings->cbApplicationDataLength = ChannelBindingTokenLength;
|
|
|
|
ChannelBindings->dwApplicationDataOffset = sizeof(SEC_CHANNEL_BINDINGS);
|
2019-11-06 17:24:51 +03:00
|
|
|
ChannelBindingToken = &((BYTE*)ChannelBindings)[ChannelBindings->dwApplicationDataOffset];
|
2018-09-03 09:48:33 +03:00
|
|
|
memcpy(ChannelBindingToken, TLS_SERVER_END_POINT, PrefixLength);
|
|
|
|
memcpy(ChannelBindingToken + PrefixLength, CertificateHash, CertificateHashLength);
|
2020-02-18 15:22:04 +03:00
|
|
|
free(CertificateHash);
|
2013-01-09 09:20:08 +04:00
|
|
|
return ContextBindings;
|
2014-05-21 19:32:14 +04:00
|
|
|
out_free:
|
2020-02-12 15:47:35 +03:00
|
|
|
free(CertificateHash);
|
2014-05-21 19:32:14 +04:00
|
|
|
free(ContextBindings);
|
|
|
|
return NULL;
|
2013-10-11 23:27:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-05 13:52:07 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x010000000L
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL tls_prepare(rdpTls* tls, BIO* underlying, const SSL_METHOD* method, int options,
|
|
|
|
BOOL clientMode)
|
2016-02-05 13:52:07 +03:00
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL tls_prepare(rdpTls* tls, BIO* underlying, SSL_METHOD* method, int options,
|
|
|
|
BOOL clientMode)
|
2016-02-05 13:52:07 +03:00
|
|
|
#endif
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
rdpSettings* settings = tls->settings;
|
2014-05-21 19:32:14 +04:00
|
|
|
tls->ctx = SSL_CTX_new(method);
|
2021-09-17 10:03:18 +03:00
|
|
|
tls->underlying = underlying;
|
2015-02-06 01:01:56 +03:00
|
|
|
|
2013-10-11 14:12:50 +04:00
|
|
|
if (!tls->ctx)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "SSL_CTX_new failed");
|
2014-05-21 19:32:14 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
SSL_CTX_set_mode(tls->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE);
|
2012-06-25 19:17:47 +04:00
|
|
|
SSL_CTX_set_options(tls->ctx, options);
|
2014-05-21 19:32:14 +04:00
|
|
|
SSL_CTX_set_read_ahead(tls->ctx, 1);
|
2018-11-18 17:09:37 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
SSL_CTX_set_min_proto_version(tls->ctx, TLS1_VERSION); /* min version */
|
|
|
|
SSL_CTX_set_max_proto_version(tls->ctx, 0); /* highest supported version by library */
|
|
|
|
#endif
|
2018-11-22 21:10:05 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
|
2018-11-08 13:01:56 +03:00
|
|
|
SSL_CTX_set_security_level(tls->ctx, settings->TlsSecLevel);
|
|
|
|
#endif
|
|
|
|
|
2015-02-06 22:21:26 +03:00
|
|
|
if (settings->AllowedTlsCiphers)
|
2015-02-06 01:01:56 +03:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
if (!SSL_CTX_set_cipher_list(tls->ctx, settings->AllowedTlsCiphers))
|
2015-02-06 01:01:56 +03:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "SSL_CTX_set_cipher_list %s failed", settings->AllowedTlsCiphers);
|
2014-07-17 16:59:06 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 11:59:14 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
tls->bio = BIO_new_rdp_tls(tls->ctx, clientMode);
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (BIO_get_ssl(tls->bio, &tls->ssl) < 0)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "unable to retrieve the SSL of the connection");
|
2014-05-21 19:32:14 +04:00
|
|
|
return FALSE;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
BIO_push(tls->bio, underlying);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-01-19 12:59:10 +03:00
|
|
|
static int tls_do_handshake(rdpTls* tls, BOOL clientMode)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
CryptoCert cert;
|
2015-09-17 21:32:40 +03:00
|
|
|
int verify_status;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
do
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
2015-09-17 21:32:40 +03:00
|
|
|
int fd;
|
|
|
|
int status;
|
2014-07-02 17:15:46 +04:00
|
|
|
struct pollfd pollfds;
|
2015-09-17 21:32:40 +03:00
|
|
|
#elif !defined(_WIN32)
|
2018-05-03 13:24:16 +03:00
|
|
|
SOCKET fd;
|
2015-09-17 21:32:40 +03:00
|
|
|
int status;
|
2014-05-21 19:32:14 +04:00
|
|
|
fd_set rset;
|
2015-09-17 21:32:40 +03:00
|
|
|
struct timeval tv;
|
|
|
|
#else
|
|
|
|
HANDLE event;
|
|
|
|
DWORD status;
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif
|
2014-05-21 19:32:14 +04:00
|
|
|
status = BIO_do_handshake(tls->bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status == 1)
|
|
|
|
break;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!BIO_should_retry(tls->bio))
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-10-11 14:12:50 +04:00
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
#ifndef _WIN32
|
2014-05-21 19:32:14 +04:00
|
|
|
/* we select() only for read even if we should test both read and write
|
|
|
|
* depending of what have blocked */
|
|
|
|
fd = BIO_get_fd(tls->bio, NULL);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (fd < 0)
|
2013-10-11 14:12:50 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "unable to retrieve BIO fd");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
#else
|
|
|
|
BIO_get_event(tls->bio, &event);
|
|
|
|
|
|
|
|
if (!event)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "unable to retrieve BIO event");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2016-09-20 09:58:04 +03:00
|
|
|
#endif
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
pollfds.fd = fd;
|
|
|
|
pollfds.events = POLLIN;
|
|
|
|
pollfds.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-04-18 11:38:42 +03:00
|
|
|
status = poll(&pollfds, 1, 10);
|
2019-11-06 17:24:51 +03:00
|
|
|
} while ((status < 0) && (errno == EINTR));
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
#elif !defined(_WIN32)
|
2014-07-02 17:15:46 +04:00
|
|
|
FD_ZERO(&rset);
|
2014-05-21 19:32:14 +04:00
|
|
|
FD_SET(fd, &rset);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 10 * 1000; /* 10ms */
|
2014-07-02 17:15:46 +04:00
|
|
|
status = _select(fd + 1, &rset, NULL, NULL, &tv);
|
2015-09-17 21:32:40 +03:00
|
|
|
#else
|
|
|
|
status = WaitForSingleObject(event, 10);
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif
|
2015-09-17 21:32:40 +03:00
|
|
|
#ifndef _WIN32
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status < 0)
|
2012-02-12 21:07:34 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "error during select()");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2012-02-12 21:07:34 +04:00
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
#else
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
if ((status != WAIT_OBJECT_0) && (status != WAIT_TIMEOUT))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "error during WaitForSingleObject(): 0x%08" PRIX32 "", status);
|
2015-09-17 21:32:40 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
} while (TRUE);
|
2011-07-18 07:16:31 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
cert = tls_get_certificate(tls, clientMode);
|
2015-09-17 21:32:40 +03:00
|
|
|
|
2013-10-11 13:07:33 +04:00
|
|
|
if (!cert)
|
2012-02-12 21:46:53 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "tls_get_certificate failed to return the server certificate.");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2012-02-12 21:46:53 +04:00
|
|
|
}
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
tls->Bindings = tls_get_channel_bindings(cert->px509);
|
2015-09-17 21:32:40 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls->Bindings)
|
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "unable to retrieve bindings");
|
2014-06-03 17:19:00 +04:00
|
|
|
verify_status = -1;
|
|
|
|
goto out;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
2013-01-09 09:20:08 +04:00
|
|
|
|
2012-09-24 12:40:32 +04:00
|
|
|
if (!crypto_cert_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength))
|
2012-02-12 21:46:53 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "crypto_cert_get_public_key failed to return the server public key.");
|
2014-06-03 17:19:00 +04:00
|
|
|
verify_status = -1;
|
|
|
|
goto out;
|
2012-02-12 21:46:53 +04:00
|
|
|
}
|
|
|
|
|
2015-09-17 21:32:40 +03:00
|
|
|
/* server-side NLA needs public keys (keys from us, the server) but no certificate verify */
|
2014-06-03 16:59:58 +04:00
|
|
|
verify_status = 1;
|
2015-09-17 21:32:40 +03:00
|
|
|
|
2014-06-03 16:59:58 +04:00
|
|
|
if (clientMode)
|
2014-06-03 13:04:35 +04:00
|
|
|
{
|
2014-06-03 16:59:58 +04:00
|
|
|
verify_status = tls_verify_certificate(tls, cert, tls->hostname, tls->port);
|
2014-06-03 13:04:35 +04:00
|
|
|
|
2014-06-03 16:59:58 +04:00
|
|
|
if (verify_status < 1)
|
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "certificate not trusted, aborting.");
|
2015-03-30 12:56:09 +03:00
|
|
|
tls_send_alert(tls);
|
2014-06-03 16:59:58 +04:00
|
|
|
}
|
2012-04-11 00:24:08 +04:00
|
|
|
}
|
2012-02-12 21:46:53 +04:00
|
|
|
|
2014-06-03 17:19:00 +04:00
|
|
|
out:
|
2012-02-12 22:05:56 +04:00
|
|
|
tls_free_certificate(cert);
|
2014-04-04 18:08:44 +04:00
|
|
|
return verify_status;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2015-02-18 23:36:57 +03:00
|
|
|
int tls_connect(rdpTls* tls, BIO* underlying)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
int options = 0;
|
|
|
|
/**
|
|
|
|
* SSL_OP_NO_COMPRESSION:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does not advertise support
|
|
|
|
* for TLS compression, but alternative servers may support it.
|
|
|
|
* This was observed between early versions of the FreeRDP server
|
|
|
|
* and the FreeRDP client, and caused major performance issues,
|
|
|
|
* which is why we're disabling it.
|
|
|
|
*/
|
|
|
|
#ifdef SSL_OP_NO_COMPRESSION
|
|
|
|
options |= SSL_OP_NO_COMPRESSION;
|
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* SSL_OP_TLS_BLOCK_PADDING_BUG:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does *not* support TLS padding.
|
|
|
|
* It absolutely needs to be disabled otherwise it won't work.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
|
|
|
|
/**
|
|
|
|
* SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
|
|
|
|
*
|
|
|
|
* Just like TLS padding, the Microsoft RDP server does not
|
|
|
|
* support empty fragments. This needs to be disabled.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
|
2016-01-12 19:43:14 +03:00
|
|
|
|
2022-06-23 03:17:43 +03:00
|
|
|
if (!tls_prep(tls, underlying, options, TRUE))
|
2022-03-07 15:47:43 +03:00
|
|
|
return 0;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2018-05-01 16:43:36 +03:00
|
|
|
#if !defined(OPENSSL_NO_TLSEXT) && !defined(LIBRESSL_VERSION_NUMBER)
|
2016-02-04 10:34:51 +03:00
|
|
|
SSL_set_tlsext_host_name(tls->ssl, tls->hostname);
|
|
|
|
#endif
|
2014-05-21 19:32:14 +04:00
|
|
|
return tls_do_handshake(tls, TRUE);
|
|
|
|
}
|
|
|
|
|
2022-06-23 03:17:43 +03:00
|
|
|
BOOL tls_prep(rdpTls* tls, BIO* underlying, int options, BOOL clientMode)
|
|
|
|
{
|
2022-06-23 09:34:20 +03:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
/**
|
|
|
|
* disable SSLv2 and SSLv3
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_NO_SSLv2;
|
|
|
|
options |= SSL_OP_NO_SSLv3;
|
|
|
|
|
|
|
|
return tls_prepare(tls, underlying, SSLv23_client_method(), options, clientMode);
|
|
|
|
#else
|
|
|
|
const BOOL enabled = freerdp_settings_get_bool(tls->settings, FreeRDP_EnforceTLSv1_2);
|
|
|
|
if (enabled)
|
2022-06-23 03:17:43 +03:00
|
|
|
{
|
2022-06-23 09:34:20 +03:00
|
|
|
return tls_prepare(tls, underlying, TLSv1_2_client_method(), options, clientMode);
|
2022-06-23 03:17:43 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-23 09:34:20 +03:00
|
|
|
return tls_prepare(tls, underlying, TLS_client_method(), options, clientMode);
|
2022-06-23 03:17:43 +03:00
|
|
|
}
|
2022-06-23 09:34:20 +03:00
|
|
|
#endif
|
2022-06-23 03:17:43 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \
|
|
|
|
!defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
static void tls_openssl_tlsext_debug_callback(SSL* s, int client_server, int type,
|
|
|
|
unsigned char* data, int len, void* arg)
|
2015-03-30 14:48:51 +03:00
|
|
|
{
|
2016-09-20 09:58:04 +03:00
|
|
|
if (type == TLSEXT_TYPE_server_name)
|
|
|
|
{
|
2015-03-30 14:48:51 +03:00
|
|
|
WLog_DBG(TAG, "Client uses SNI (extension disabled)");
|
|
|
|
s->servername_done = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-20 09:58:04 +03:00
|
|
|
BOOL tls_accept(rdpTls* tls, BIO* underlying, rdpSettings* settings)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
long options = 0;
|
2016-09-20 09:58:04 +03:00
|
|
|
BIO* bio;
|
2020-04-10 16:06:47 +03:00
|
|
|
EVP_PKEY* privkey;
|
2016-09-20 09:58:04 +03:00
|
|
|
X509* x509;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
/**
|
2012-06-25 19:17:47 +04:00
|
|
|
* SSL_OP_NO_SSLv2:
|
|
|
|
*
|
2012-02-23 03:38:52 +04:00
|
|
|
* We only want SSLv3 and TLSv1, so disable SSLv2.
|
|
|
|
* SSLv3 is used by, eg. Microsoft RDC for Mac OS X.
|
|
|
|
*/
|
2012-06-25 19:17:47 +04:00
|
|
|
options |= SSL_OP_NO_SSLv2;
|
|
|
|
/**
|
|
|
|
* SSL_OP_NO_COMPRESSION:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does not advertise support
|
|
|
|
* for TLS compression, but alternative servers may support it.
|
|
|
|
* This was observed between early versions of the FreeRDP server
|
|
|
|
* and the FreeRDP client, and caused major performance issues,
|
|
|
|
* which is why we're disabling it.
|
|
|
|
*/
|
2012-07-03 20:45:09 +04:00
|
|
|
#ifdef SSL_OP_NO_COMPRESSION
|
2012-06-25 19:17:47 +04:00
|
|
|
options |= SSL_OP_NO_COMPRESSION;
|
2012-07-03 20:45:09 +04:00
|
|
|
#endif
|
2012-06-25 19:17:47 +04:00
|
|
|
/**
|
|
|
|
* SSL_OP_TLS_BLOCK_PADDING_BUG:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does *not* support TLS padding.
|
|
|
|
* It absolutely needs to be disabled otherwise it won't work.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
|
|
|
|
/**
|
|
|
|
* SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
|
|
|
|
*
|
|
|
|
* Just like TLS padding, the Microsoft RDP server does not
|
|
|
|
* support empty fragments. This needs to be disabled.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls_prepare(tls, underlying, SSLv23_server_method(), options, FALSE))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2016-01-21 02:17:59 +03:00
|
|
|
if (settings->PrivateKeyFile)
|
|
|
|
{
|
2016-10-17 15:49:26 +03:00
|
|
|
bio = BIO_new_file(settings->PrivateKeyFile, "rb");
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2016-01-21 02:17:59 +03:00
|
|
|
if (!bio)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "BIO_new_file failed for private key %s", settings->PrivateKeyFile);
|
2016-01-21 02:17:59 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (settings->PrivateKeyContent)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
bio = BIO_new_mem_buf(settings->PrivateKeyContent, strlen(settings->PrivateKeyContent));
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2016-01-21 02:17:59 +03:00
|
|
|
if (!bio)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_new_mem_buf failed for private key");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "no private key defined");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:06:47 +03:00
|
|
|
privkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
|
2018-11-08 14:09:49 +03:00
|
|
|
BIO_free_all(bio);
|
2016-01-21 02:17:59 +03:00
|
|
|
|
2020-04-10 16:06:47 +03:00
|
|
|
if (!privkey)
|
2016-01-21 02:17:59 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid private key");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:06:47 +03:00
|
|
|
if (SSL_use_PrivateKey(tls->ssl, privkey) <= 0)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2020-04-10 16:06:47 +03:00
|
|
|
WLog_ERR(TAG, "SSL_CTX_use_PrivateKey_file failed");
|
|
|
|
EVP_PKEY_free(privkey);
|
2016-01-21 02:17:59 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings->CertificateFile)
|
2021-04-13 12:01:43 +03:00
|
|
|
x509 = crypto_cert_from_pem(settings->CertificateFile, strlen(settings->CertificateFile),
|
|
|
|
TRUE);
|
2016-01-21 02:17:59 +03:00
|
|
|
else if (settings->CertificateContent)
|
2021-04-13 12:01:43 +03:00
|
|
|
x509 = crypto_cert_from_pem(settings->CertificateContent,
|
|
|
|
strlen(settings->CertificateContent), FALSE);
|
2016-01-21 02:17:59 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "no certificate defined");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!x509)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid certificate");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2016-01-21 02:17:59 +03:00
|
|
|
if (SSL_use_certificate(tls->ssl, x509) <= 0)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "SSL_use_certificate_file failed");
|
2016-01-21 02:17:59 +03:00
|
|
|
X509_free(x509);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \
|
|
|
|
!defined(LIBRESSL_VERSION_NUMBER)
|
2015-03-30 14:48:51 +03:00
|
|
|
SSL_set_tlsext_debug_callback(tls->ssl, tls_openssl_tlsext_debug_callback);
|
|
|
|
#endif
|
2014-05-21 19:32:14 +04:00
|
|
|
return tls_do_handshake(tls, FALSE) > 0;
|
|
|
|
}
|
|
|
|
|
2015-03-30 12:56:09 +03:00
|
|
|
BOOL tls_send_alert(rdpTls* tls)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
if (!tls)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls->ssl)
|
|
|
|
return TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/**
|
|
|
|
* FIXME: The following code does not work on OpenSSL > 1.1.0 because the
|
|
|
|
* SSL struct is opaqe now
|
|
|
|
*/
|
2019-01-07 16:18:14 +03:00
|
|
|
#if (!defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER < 0x10100000L)) || \
|
2019-11-06 17:24:51 +03:00
|
|
|
(defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER <= 0x2080300fL))
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (tls->alertDescription != TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
/**
|
|
|
|
* OpenSSL doesn't really expose an API for sending a TLS alert manually.
|
|
|
|
*
|
|
|
|
* The following code disables the sending of the default "close notify"
|
|
|
|
* and then proceeds to force sending a custom TLS alert before shutting down.
|
|
|
|
*
|
|
|
|
* Manually sending a TLS alert is necessary in certain cases,
|
|
|
|
* like when server-side NLA results in an authentication failure.
|
|
|
|
*/
|
2016-11-21 19:28:54 +03:00
|
|
|
SSL_SESSION* ssl_session = SSL_get_session(tls->ssl);
|
|
|
|
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(tls->ssl);
|
2014-05-21 19:32:14 +04:00
|
|
|
SSL_set_quiet_shutdown(tls->ssl, 1);
|
2012-06-13 07:09:30 +04:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if ((tls->alertLevel == TLS_ALERT_LEVEL_FATAL) && (ssl_session))
|
|
|
|
SSL_CTX_remove_session(ssl_ctx, ssl_session);
|
2012-06-13 07:09:30 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tls->ssl->s3->alert_dispatch = 1;
|
|
|
|
tls->ssl->s3->send_alert[0] = tls->alertLevel;
|
|
|
|
tls->ssl->s3->send_alert[1] = tls->alertDescription;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (tls->ssl->s3->wbuf.left == 0)
|
|
|
|
tls->ssl->method->ssl_dispatch_alert(tls->ssl);
|
2013-05-31 03:44:58 +04:00
|
|
|
}
|
2019-02-21 15:50:16 +03:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
#endif
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
int tls_write_all(rdpTls* tls, const BYTE* data, int length)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2015-01-14 21:25:54 +03:00
|
|
|
int status;
|
2015-02-14 00:02:37 +03:00
|
|
|
int offset = 0;
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO* bio = tls->bio;
|
2013-12-04 18:36:25 +04:00
|
|
|
|
2015-02-14 00:02:37 +03:00
|
|
|
while (offset < length)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2022-07-01 06:30:21 +03:00
|
|
|
ERR_clear_error();
|
2015-02-14 00:02:37 +03:00
|
|
|
status = BIO_write(bio, &data[offset], length - offset);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status > 0)
|
2014-07-02 17:15:46 +04:00
|
|
|
{
|
2015-02-14 00:02:37 +03:00
|
|
|
offset += status;
|
2014-07-02 17:15:46 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-14 00:02:37 +03:00
|
|
|
if (!BIO_should_retry(bio))
|
|
|
|
return -1;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2015-02-14 00:02:37 +03:00
|
|
|
if (BIO_write_blocked(bio))
|
|
|
|
status = BIO_wait_write(bio, 100);
|
|
|
|
else if (BIO_read_blocked(bio))
|
2021-07-28 10:20:06 +03:00
|
|
|
return -2; /* Abort write, there is data that must be read */
|
2015-02-14 00:02:37 +03:00
|
|
|
else
|
|
|
|
USleep(100);
|
2012-04-18 10:28:05 +04:00
|
|
|
|
2015-02-14 00:02:37 +03:00
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
2012-04-18 10:28:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return length;
|
2012-12-21 12:23:50 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 04:44:18 +04:00
|
|
|
int tls_set_alert_code(rdpTls* tls, int level, int description)
|
|
|
|
{
|
|
|
|
tls->alertLevel = level;
|
|
|
|
tls->alertDescription = description;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
static BOOL tls_match_hostname(const char* pattern, const size_t pattern_length,
|
|
|
|
const char* hostname)
|
2013-06-17 23:19:01 +04:00
|
|
|
{
|
|
|
|
if (strlen(hostname) == pattern_length)
|
|
|
|
{
|
2016-09-20 09:58:04 +03:00
|
|
|
if (_strnicmp(hostname, pattern, pattern_length) == 0)
|
2013-06-17 23:19:01 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((pattern_length > 2) && (pattern[0] == '*') && (pattern[1] == '.') &&
|
|
|
|
((strlen(hostname)) >= pattern_length))
|
2013-06-17 23:19:01 +04:00
|
|
|
{
|
2018-11-30 11:55:10 +03:00
|
|
|
const char* check_hostname = &hostname[strlen(hostname) - pattern_length + 1];
|
2014-02-11 07:12:13 +04:00
|
|
|
|
2016-09-20 09:58:04 +03:00
|
|
|
if (_strnicmp(check_hostname, &pattern[1], pattern_length - 1) == 0)
|
2013-06-17 23:19:01 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:08:26 +03:00
|
|
|
static BOOL is_redirected(rdpTls* tls)
|
|
|
|
{
|
|
|
|
rdpSettings* settings = tls->settings;
|
|
|
|
|
|
|
|
if (LB_NOREDIRECT & settings->RedirectionFlags)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return settings->RedirectionFlags != 0;
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:03:49 +03:00
|
|
|
static BOOL is_accepted(rdpTls* tls, const BYTE* pem, size_t length)
|
|
|
|
{
|
|
|
|
rdpSettings* settings = tls->settings;
|
|
|
|
char* AccpetedKey;
|
|
|
|
UINT32 AcceptedKeyLength;
|
|
|
|
|
|
|
|
if (tls->isGatewayTransport)
|
|
|
|
{
|
|
|
|
AccpetedKey = settings->GatewayAcceptedCert;
|
|
|
|
AcceptedKeyLength = settings->GatewayAcceptedCertLength;
|
|
|
|
}
|
2018-07-16 18:08:26 +03:00
|
|
|
else if (is_redirected(tls))
|
2018-07-10 11:03:49 +03:00
|
|
|
{
|
|
|
|
AccpetedKey = settings->RedirectionAcceptedCert;
|
|
|
|
AcceptedKeyLength = settings->RedirectionAcceptedCertLength;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AccpetedKey = settings->AcceptedCert;
|
|
|
|
AcceptedKeyLength = settings->AcceptedCertLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AcceptedKeyLength > 0)
|
|
|
|
{
|
|
|
|
if (AcceptedKeyLength == length)
|
|
|
|
{
|
|
|
|
if (memcmp(AccpetedKey, pem, AcceptedKeyLength) == 0)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tls->isGatewayTransport)
|
|
|
|
{
|
|
|
|
free(settings->GatewayAcceptedCert);
|
|
|
|
settings->GatewayAcceptedCert = NULL;
|
|
|
|
settings->GatewayAcceptedCertLength = 0;
|
|
|
|
}
|
2018-07-16 18:08:26 +03:00
|
|
|
else if (is_redirected(tls))
|
2018-07-10 11:03:49 +03:00
|
|
|
{
|
|
|
|
free(settings->RedirectionAcceptedCert);
|
|
|
|
settings->RedirectionAcceptedCert = NULL;
|
|
|
|
settings->RedirectionAcceptedCertLength = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(settings->AcceptedCert);
|
|
|
|
settings->AcceptedCert = NULL;
|
|
|
|
settings->AcceptedCertLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-08-26 09:58:39 +03:00
|
|
|
static BOOL compare_fingerprint(const char* fp, const char* hash, CryptoCert cert, BOOL separator)
|
|
|
|
{
|
|
|
|
BOOL equal;
|
|
|
|
char* strhash;
|
|
|
|
|
|
|
|
WINPR_ASSERT(fp);
|
|
|
|
WINPR_ASSERT(hash);
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
|
|
|
|
strhash = crypto_cert_fingerprint_by_hash_ex(cert->px509, hash, separator);
|
|
|
|
if (!strhash)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
equal = (_stricmp(strhash, fp) == 0);
|
|
|
|
free(strhash);
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL compare_fingerprint_all(const char* fp, const char* hash, CryptoCert cert)
|
|
|
|
{
|
|
|
|
if (compare_fingerprint(fp, hash, cert, FALSE))
|
|
|
|
return TRUE;
|
|
|
|
if (compare_fingerprint(fp, hash, cert, TRUE))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:01:50 +03:00
|
|
|
static BOOL is_accepted_fingerprint(CryptoCert cert, const char* CertificateAcceptedFingerprints)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
if (CertificateAcceptedFingerprints)
|
|
|
|
{
|
|
|
|
char* context = NULL;
|
|
|
|
char* copy = _strdup(CertificateAcceptedFingerprints);
|
|
|
|
char* cur = strtok_s(copy, ",", &context);
|
|
|
|
while (cur)
|
|
|
|
{
|
2020-05-18 12:18:55 +03:00
|
|
|
char* subcontext = NULL;
|
|
|
|
const char* h = strtok_s(cur, ":", &subcontext);
|
2020-02-12 14:01:50 +03:00
|
|
|
const char* fp;
|
|
|
|
|
2020-02-12 15:17:42 +03:00
|
|
|
if (!h)
|
2021-08-26 09:58:39 +03:00
|
|
|
goto next;
|
2020-02-12 14:01:50 +03:00
|
|
|
|
|
|
|
fp = h + strlen(h) + 1;
|
|
|
|
if (!fp)
|
2021-08-26 09:58:39 +03:00
|
|
|
goto next;
|
2020-02-12 14:01:50 +03:00
|
|
|
|
2021-08-26 09:58:39 +03:00
|
|
|
if (compare_fingerprint_all(fp, h, cert))
|
2020-02-12 15:17:42 +03:00
|
|
|
{
|
|
|
|
rc = TRUE;
|
|
|
|
break;
|
2020-02-12 14:01:50 +03:00
|
|
|
}
|
2021-08-26 09:58:39 +03:00
|
|
|
next:
|
2020-02-12 14:01:50 +03:00
|
|
|
cur = strtok_s(NULL, ",", &context);
|
|
|
|
}
|
|
|
|
free(copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:41:51 +03:00
|
|
|
static BOOL accept_cert(rdpTls* tls, const BYTE* pem, UINT32 length)
|
2018-07-10 11:03:49 +03:00
|
|
|
{
|
|
|
|
rdpSettings* settings = tls->settings;
|
2019-11-06 17:24:51 +03:00
|
|
|
char* dupPem = _strdup((const char*)pem);
|
2018-12-07 14:36:18 +03:00
|
|
|
|
|
|
|
if (!dupPem)
|
|
|
|
return FALSE;
|
2018-07-10 11:03:49 +03:00
|
|
|
|
|
|
|
if (tls->isGatewayTransport)
|
|
|
|
{
|
2018-12-07 14:36:18 +03:00
|
|
|
settings->GatewayAcceptedCert = dupPem;
|
2018-07-10 11:03:49 +03:00
|
|
|
settings->GatewayAcceptedCertLength = length;
|
|
|
|
}
|
2018-07-16 18:08:26 +03:00
|
|
|
else if (is_redirected(tls))
|
2018-07-10 11:03:49 +03:00
|
|
|
{
|
2018-12-07 14:36:18 +03:00
|
|
|
settings->RedirectionAcceptedCert = dupPem;
|
2018-07-10 11:03:49 +03:00
|
|
|
settings->RedirectionAcceptedCertLength = length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-07 14:36:18 +03:00
|
|
|
settings->AcceptedCert = dupPem;
|
2018-07-10 11:03:49 +03:00
|
|
|
settings->AcceptedCertLength = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static BOOL tls_extract_pem(CryptoCert cert, BYTE** PublicKey, size_t* PublicKeyLength)
|
2018-07-10 15:57:13 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!cert || !PublicKey)
|
2018-07-10 15:57:13 +03:00
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
*PublicKey = crypto_cert_pem(cert->px509, cert->px509chain, PublicKeyLength);
|
|
|
|
return *PublicKey != NULL;
|
2018-07-10 15:57:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
int tls_verify_certificate(rdpTls* tls, CryptoCert cert, const char* hostname, UINT16 port)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2012-02-03 03:20:02 +04:00
|
|
|
int match;
|
2012-02-04 11:21:39 +04:00
|
|
|
int index;
|
2021-04-13 12:01:43 +03:00
|
|
|
size_t length;
|
2019-07-15 16:51:46 +03:00
|
|
|
BOOL certificate_status;
|
2012-02-27 19:55:49 +04:00
|
|
|
char* common_name = NULL;
|
|
|
|
int common_name_length = 0;
|
2018-08-24 15:03:04 +03:00
|
|
|
char** dns_names = 0;
|
|
|
|
int dns_names_count = 0;
|
|
|
|
int* dns_names_lengths = NULL;
|
2019-07-15 16:51:46 +03:00
|
|
|
int verification_status = -1;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL hostname_match = FALSE;
|
2018-11-30 12:25:06 +03:00
|
|
|
rdpCertificateData* certificate_data = NULL;
|
|
|
|
BYTE* pemCert = NULL;
|
|
|
|
DWORD flags = VERIFY_CERT_FLAG_NONE;
|
2022-03-23 15:18:35 +03:00
|
|
|
freerdp* instance;
|
|
|
|
|
|
|
|
WINPR_ASSERT(tls);
|
|
|
|
WINPR_ASSERT(tls->settings);
|
|
|
|
|
|
|
|
instance = (freerdp*)tls->settings->instance;
|
|
|
|
WINPR_ASSERT(instance);
|
2012-02-03 02:36:07 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (freerdp_shall_disconnect_context(instance->context))
|
2020-02-19 18:44:42 +03:00
|
|
|
return -1;
|
|
|
|
|
2018-07-10 15:57:13 +03:00
|
|
|
if (!tls_extract_pem(cert, &pemCert, &length))
|
2018-11-30 12:25:06 +03:00
|
|
|
goto end;
|
2013-11-18 22:54:33 +04:00
|
|
|
|
2018-07-10 11:03:49 +03:00
|
|
|
/* Check, if we already accepted this key. */
|
|
|
|
if (is_accepted(tls, pemCert, length))
|
2018-12-14 11:52:25 +03:00
|
|
|
{
|
2019-07-15 16:51:46 +03:00
|
|
|
verification_status = 1;
|
2018-11-30 12:25:06 +03:00
|
|
|
goto end;
|
2018-12-14 11:52:25 +03:00
|
|
|
}
|
2015-06-10 11:59:14 +03:00
|
|
|
|
2020-02-12 14:01:50 +03:00
|
|
|
if (is_accepted_fingerprint(cert, tls->settings->CertificateAcceptedFingerprints))
|
|
|
|
{
|
|
|
|
verification_status = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
if (tls->isGatewayTransport || is_redirected(tls))
|
2018-11-30 12:25:06 +03:00
|
|
|
flags |= VERIFY_CERT_FLAG_LEGACY;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
|
|
|
if (tls->isGatewayTransport)
|
2018-11-30 12:25:06 +03:00
|
|
|
flags |= VERIFY_CERT_FLAG_GATEWAY;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
|
|
|
if (is_redirected(tls))
|
2018-11-30 12:25:06 +03:00
|
|
|
flags |= VERIFY_CERT_FLAG_REDIRECT;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2018-11-30 12:36:15 +03:00
|
|
|
/* Certificate management is done by the application */
|
2018-07-10 11:03:49 +03:00
|
|
|
if (tls->settings->ExternalCertificateManagement)
|
|
|
|
{
|
2013-11-18 22:54:33 +04:00
|
|
|
if (instance->VerifyX509Certificate)
|
2019-11-06 17:24:51 +03:00
|
|
|
verification_status =
|
|
|
|
instance->VerifyX509Certificate(instance, pemCert, length, hostname, port, flags);
|
2015-10-16 10:50:18 +03:00
|
|
|
else
|
|
|
|
WLog_ERR(TAG, "No VerifyX509Certificate callback registered!");
|
2013-11-18 22:54:33 +04:00
|
|
|
|
2019-07-15 16:51:46 +03:00
|
|
|
if (verification_status > 0)
|
2018-07-10 11:03:49 +03:00
|
|
|
accept_cert(tls, pemCert, length);
|
2019-07-15 16:51:46 +03:00
|
|
|
else if (verification_status < 0)
|
2015-10-16 10:50:18 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "VerifyX509Certificate failed: (length = %d) status: [%d] %s", length,
|
|
|
|
verification_status, pemCert);
|
2018-11-30 12:25:06 +03:00
|
|
|
goto end;
|
2015-10-16 10:50:18 +03:00
|
|
|
}
|
2013-11-18 22:54:33 +04:00
|
|
|
}
|
2012-02-05 00:04:03 +04:00
|
|
|
/* ignore certificate verification if user explicitly required it (discouraged) */
|
2018-11-30 12:36:15 +03:00
|
|
|
else if (tls->settings->IgnoreCertificate)
|
2019-07-15 16:51:46 +03:00
|
|
|
verification_status = 1; /* success! */
|
2018-11-30 12:36:15 +03:00
|
|
|
else if (!tls->isGatewayTransport && (tls->settings->AuthenticationLevel == 0))
|
2019-07-15 16:51:46 +03:00
|
|
|
verification_status = 1; /* success! */
|
2018-11-30 12:36:15 +03:00
|
|
|
else
|
2012-02-05 00:04:03 +04:00
|
|
|
{
|
2018-11-30 12:36:15 +03:00
|
|
|
/* if user explicitly specified a certificate name, use it instead of the hostname */
|
|
|
|
if (!tls->isGatewayTransport && tls->settings->CertificateName)
|
|
|
|
hostname = tls->settings->CertificateName;
|
|
|
|
|
|
|
|
/* attempt verification using OpenSSL and the ~/.freerdp/certs certificate store */
|
2021-04-13 12:01:43 +03:00
|
|
|
certificate_status =
|
|
|
|
x509_verify_certificate(cert, certificate_store_get_certs_path(tls->certificate_store));
|
2018-11-30 12:36:15 +03:00
|
|
|
/* verify certificate name match */
|
|
|
|
certificate_data = crypto_get_certificate_data(cert->px509, hostname, port);
|
2021-05-27 09:38:59 +03:00
|
|
|
if (!certificate_data)
|
|
|
|
goto end;
|
2018-11-30 12:36:15 +03:00
|
|
|
/* extra common name and alternative names */
|
|
|
|
common_name = crypto_cert_subject_common_name(cert->px509, &common_name_length);
|
2019-11-06 17:24:51 +03:00
|
|
|
dns_names = crypto_cert_get_dns_names(cert->px509, &dns_names_count, &dns_names_lengths);
|
2018-11-30 12:36:15 +03:00
|
|
|
|
|
|
|
/* compare against common name */
|
|
|
|
|
|
|
|
if (common_name)
|
2012-02-08 07:16:57 +04:00
|
|
|
{
|
2018-11-30 12:36:15 +03:00
|
|
|
if (tls_match_hostname(common_name, common_name_length, hostname))
|
2013-06-17 23:19:01 +04:00
|
|
|
hostname_match = TRUE;
|
2012-02-08 07:16:57 +04:00
|
|
|
}
|
2012-02-04 11:21:39 +04:00
|
|
|
|
2018-11-30 12:36:15 +03:00
|
|
|
/* compare against alternative names */
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2018-11-30 12:36:15 +03:00
|
|
|
if (dns_names)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2018-11-30 12:36:15 +03:00
|
|
|
for (index = 0; index < dns_names_count; index++)
|
2018-11-30 12:25:06 +03:00
|
|
|
{
|
2018-11-30 12:36:15 +03:00
|
|
|
if (tls_match_hostname(dns_names[index], dns_names_lengths[index], hostname))
|
|
|
|
{
|
|
|
|
hostname_match = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-31 13:16:55 +03:00
|
|
|
}
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2020-02-12 14:01:50 +03:00
|
|
|
/* if the certificate is valid and the certificate name matches, verification succeeds
|
|
|
|
*/
|
2018-11-30 12:36:15 +03:00
|
|
|
if (certificate_status && hostname_match)
|
2019-07-15 16:51:46 +03:00
|
|
|
verification_status = 1; /* success! */
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2018-11-30 12:36:15 +03:00
|
|
|
if (!hostname_match)
|
|
|
|
flags |= VERIFY_CERT_FLAG_MISMATCH;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* verification could not succeed with OpenSSL, use known_hosts file and prompt user for
|
|
|
|
* manual verification */
|
2018-11-30 12:36:15 +03:00
|
|
|
if (!certificate_status || !hostname_match)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2018-11-30 12:36:15 +03:00
|
|
|
DWORD accept_certificate = 0;
|
2021-07-29 11:07:04 +03:00
|
|
|
size_t pem_length = 0;
|
2021-04-13 12:01:43 +03:00
|
|
|
char* issuer = crypto_cert_issuer(cert->px509);
|
|
|
|
char* subject = crypto_cert_subject(cert->px509);
|
2021-07-29 11:07:04 +03:00
|
|
|
char* pem = (char*)crypto_cert_pem(cert->px509, NULL, &pem_length);
|
2020-04-21 10:06:39 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!pem)
|
|
|
|
goto end;
|
2020-04-21 10:06:39 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
/* search for matching entry in known_hosts file */
|
|
|
|
match = certificate_store_contains_data(tls->certificate_store, certificate_data);
|
2018-11-30 12:36:15 +03:00
|
|
|
|
|
|
|
if (match == 1)
|
2018-11-30 11:55:10 +03:00
|
|
|
{
|
2020-02-12 14:01:50 +03:00
|
|
|
/* no entry was found in known_hosts file, prompt user for manual verification
|
|
|
|
*/
|
2018-11-30 12:36:15 +03:00
|
|
|
if (!hostname_match)
|
2019-11-06 17:24:51 +03:00
|
|
|
tls_print_certificate_name_mismatch_error(hostname, port, common_name,
|
|
|
|
dns_names, dns_names_count);
|
2018-11-30 12:36:15 +03:00
|
|
|
|
|
|
|
/* Automatically accept certificate on first use */
|
|
|
|
if (tls->settings->AutoAcceptCertificate)
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "No certificate stored, automatically accepting.");
|
2018-11-30 11:55:10 +03:00
|
|
|
accept_certificate = 1;
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
2018-12-14 12:17:52 +03:00
|
|
|
else if (tls->settings->AutoDenyCertificate)
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "No certificate stored, automatically denying.");
|
|
|
|
accept_certificate = 0;
|
|
|
|
}
|
2018-11-30 12:36:15 +03:00
|
|
|
else if (instance->VerifyX509Certificate)
|
|
|
|
{
|
2021-07-29 11:07:04 +03:00
|
|
|
int rc = instance->VerifyX509Certificate(instance, pemCert, pem_length,
|
|
|
|
hostname, port, flags);
|
2018-11-30 12:36:15 +03:00
|
|
|
|
|
|
|
if (rc == 1)
|
|
|
|
accept_certificate = 1;
|
|
|
|
else if (rc > 1)
|
|
|
|
accept_certificate = 2;
|
|
|
|
else
|
|
|
|
accept_certificate = 0;
|
|
|
|
}
|
|
|
|
else if (instance->VerifyCertificateEx)
|
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
const BOOL use_pem = freerdp_settings_get_bool(
|
2022-03-23 15:18:35 +03:00
|
|
|
tls->settings, FreeRDP_CertificateCallbackPreferPEM);
|
2021-04-13 12:01:43 +03:00
|
|
|
char* fp = NULL;
|
|
|
|
DWORD cflags = flags;
|
|
|
|
if (use_pem)
|
|
|
|
{
|
|
|
|
cflags |= VERIFY_CERT_FLAG_FP_IS_PEM;
|
|
|
|
fp = pem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fp = crypto_cert_fingerprint(cert->px509);
|
2018-11-30 12:36:15 +03:00
|
|
|
accept_certificate = instance->VerifyCertificateEx(
|
2021-04-13 12:01:43 +03:00
|
|
|
instance, hostname, port, common_name, subject, issuer, fp, cflags);
|
|
|
|
if (!use_pem)
|
|
|
|
free(fp);
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
2021-06-22 15:39:10 +03:00
|
|
|
#if defined(WITH_FREERDP_DEPRECATED)
|
2018-11-30 12:36:15 +03:00
|
|
|
else if (instance->VerifyCertificate)
|
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
char* fp = crypto_cert_fingerprint(cert->px509);
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "The VerifyCertificate callback is deprecated, migrate your "
|
|
|
|
"application to VerifyCertificateEx");
|
2021-04-13 12:01:43 +03:00
|
|
|
accept_certificate = instance->VerifyCertificate(instance, common_name, subject,
|
|
|
|
issuer, fp, !hostname_match);
|
|
|
|
free(fp);
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
2021-06-22 15:39:10 +03:00
|
|
|
#endif
|
2018-11-30 12:25:06 +03:00
|
|
|
}
|
2018-11-30 12:36:15 +03:00
|
|
|
else if (match == -1)
|
2014-03-21 02:19:54 +04:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
rdpCertificateData* stored_data =
|
|
|
|
certificate_store_load_data(tls->certificate_store, hostname, port);
|
2020-02-12 14:01:50 +03:00
|
|
|
/* entry was found in known_hosts file, but fingerprint does not match. ask user
|
|
|
|
* to use it */
|
2021-04-13 12:01:43 +03:00
|
|
|
tls_print_certificate_error(
|
|
|
|
hostname, port, pem, certificate_store_get_hosts_file(tls->certificate_store));
|
2018-11-30 12:36:15 +03:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (!stored_data)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Failed to get certificate entry for %s:%d", hostname, port);
|
2018-11-30 12:36:15 +03:00
|
|
|
|
2018-12-14 12:17:52 +03:00
|
|
|
if (tls->settings->AutoDenyCertificate)
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "No certificate stored, automatically denying.");
|
|
|
|
accept_certificate = 0;
|
|
|
|
}
|
|
|
|
else if (instance->VerifyX509Certificate)
|
2018-11-30 12:36:15 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
const int rc =
|
2021-07-29 11:07:04 +03:00
|
|
|
instance->VerifyX509Certificate(instance, pemCert, pem_length, hostname,
|
|
|
|
port, flags | VERIFY_CERT_FLAG_CHANGED);
|
2018-11-30 12:36:15 +03:00
|
|
|
|
|
|
|
if (rc == 1)
|
|
|
|
accept_certificate = 1;
|
|
|
|
else if (rc > 1)
|
|
|
|
accept_certificate = 2;
|
|
|
|
else
|
|
|
|
accept_certificate = 0;
|
|
|
|
}
|
|
|
|
else if (instance->VerifyChangedCertificateEx)
|
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
DWORD cflags = flags | VERIFY_CERT_FLAG_CHANGED;
|
|
|
|
const char* old_subject = certificate_data_get_subject(stored_data);
|
|
|
|
const char* old_issuer = certificate_data_get_issuer(stored_data);
|
|
|
|
const char* old_fp = certificate_data_get_fingerprint(stored_data);
|
|
|
|
const char* old_pem = certificate_data_get_pem(stored_data);
|
2021-09-21 10:56:56 +03:00
|
|
|
const BOOL fpIsAllocated =
|
|
|
|
!old_pem || !freerdp_settings_get_bool(
|
2022-03-23 15:18:35 +03:00
|
|
|
tls->settings, FreeRDP_CertificateCallbackPreferPEM);
|
2021-04-13 12:01:43 +03:00
|
|
|
char* fp;
|
2021-09-21 10:56:56 +03:00
|
|
|
if (!fpIsAllocated)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
|
|
|
cflags |= VERIFY_CERT_FLAG_FP_IS_PEM;
|
|
|
|
fp = pem;
|
|
|
|
old_fp = old_pem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fp = crypto_cert_fingerprint(cert->px509);
|
|
|
|
}
|
2018-11-30 12:36:15 +03:00
|
|
|
accept_certificate = instance->VerifyChangedCertificateEx(
|
2021-10-21 10:07:52 +03:00
|
|
|
instance, hostname, port, common_name, subject, issuer, fp, old_subject,
|
2021-04-13 12:01:43 +03:00
|
|
|
old_issuer, old_fp, cflags);
|
2021-09-21 10:56:56 +03:00
|
|
|
if (fpIsAllocated)
|
2021-04-13 12:01:43 +03:00
|
|
|
free(fp);
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
2021-06-22 15:39:10 +03:00
|
|
|
#if defined(WITH_FREERDP_DEPRECATED)
|
2018-11-30 12:36:15 +03:00
|
|
|
else if (instance->VerifyChangedCertificate)
|
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
char* fp = crypto_cert_fingerprint(cert->px509);
|
|
|
|
const char* old_subject = certificate_data_get_subject(stored_data);
|
|
|
|
const char* old_issuer = certificate_data_get_issuer(stored_data);
|
|
|
|
const char* old_fingerprint = certificate_data_get_fingerprint(stored_data);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "The VerifyChangedCertificate callback is deprecated, migrate "
|
|
|
|
"your application to VerifyChangedCertificateEx");
|
2018-11-30 12:36:15 +03:00
|
|
|
accept_certificate = instance->VerifyChangedCertificate(
|
2021-04-13 12:01:43 +03:00
|
|
|
instance, common_name, subject, issuer, fp, old_subject, old_issuer,
|
|
|
|
old_fingerprint);
|
|
|
|
free(fp);
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
2021-06-22 15:39:10 +03:00
|
|
|
#endif
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
certificate_data_free(stored_data);
|
2018-11-30 12:36:15 +03:00
|
|
|
}
|
|
|
|
else if (match == 0)
|
|
|
|
accept_certificate = 2; /* success! */
|
2015-06-11 00:33:58 +03:00
|
|
|
|
2018-11-30 12:36:15 +03:00
|
|
|
/* Save certificate or do a simple accept / reject */
|
2016-09-20 09:58:04 +03:00
|
|
|
switch (accept_certificate)
|
2012-06-29 05:05:10 +04:00
|
|
|
{
|
2015-12-14 16:02:49 +03:00
|
|
|
case 1:
|
2018-12-10 14:03:55 +03:00
|
|
|
|
2015-12-14 16:02:49 +03:00
|
|
|
/* user accepted certificate, add entry in known_hosts file */
|
2021-04-13 12:01:43 +03:00
|
|
|
verification_status =
|
|
|
|
certificate_store_save_data(tls->certificate_store, certificate_data) ? 1
|
|
|
|
: -1;
|
2015-12-14 16:02:49 +03:00
|
|
|
break;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-12-14 16:02:49 +03:00
|
|
|
case 2:
|
|
|
|
/* user did accept temporaty, do not add to known hosts file */
|
2019-07-15 16:51:46 +03:00
|
|
|
verification_status = 1;
|
2015-12-14 16:02:49 +03:00
|
|
|
break;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-12-14 16:02:49 +03:00
|
|
|
default:
|
|
|
|
/* user did not accept, abort and do not add entry in known_hosts file */
|
2019-07-15 16:51:46 +03:00
|
|
|
verification_status = -1; /* failure! */
|
2015-12-14 16:02:49 +03:00
|
|
|
break;
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
2018-11-30 12:36:15 +03:00
|
|
|
|
|
|
|
free(issuer);
|
|
|
|
free(subject);
|
2021-04-13 12:01:43 +03:00
|
|
|
free(pem);
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
2012-02-05 00:04:03 +04:00
|
|
|
|
2019-07-15 16:51:46 +03:00
|
|
|
if (verification_status > 0)
|
2018-11-30 12:36:15 +03:00
|
|
|
accept_cert(tls, pemCert, length);
|
2012-02-27 19:55:49 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 12:25:06 +03:00
|
|
|
end:
|
2015-06-23 16:40:37 +03:00
|
|
|
certificate_data_free(certificate_data);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(common_name);
|
2012-11-19 22:26:56 +04:00
|
|
|
|
2018-08-24 15:03:04 +03:00
|
|
|
if (dns_names)
|
2019-11-06 17:24:51 +03:00
|
|
|
crypto_cert_dns_names_free(dns_names_count, dns_names_lengths, dns_names);
|
2013-08-28 19:10:58 +04:00
|
|
|
|
2018-11-30 12:25:06 +03:00
|
|
|
free(pemCert);
|
2019-07-15 16:51:46 +03:00
|
|
|
return verification_status;
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
void tls_print_certificate_error(const char* hostname, UINT16 port, const char* fingerprint,
|
|
|
|
const char* hosts_file)
|
2011-08-26 07:10:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "The host key for %s:%" PRIu16 " has changed", hostname, port);
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
WLog_ERR(TAG, "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
|
|
|
|
WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
WLog_ERR(TAG, "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "It is also possible that a host key has just been changed.");
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint);
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "Please contact your system administrator.");
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", hosts_file);
|
|
|
|
WLog_ERR(TAG, "Host key for %s has changed and you have requested strict checking.", hostname);
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "Host key verification failed.");
|
2011-08-26 07:10:49 +04:00
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2018-11-30 11:55:10 +03:00
|
|
|
void tls_print_certificate_name_mismatch_error(const char* hostname, UINT16 port,
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* common_name, char** alt_names,
|
|
|
|
int alt_names_count)
|
2012-02-04 11:21:39 +04:00
|
|
|
{
|
|
|
|
int index;
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(NULL != hostname);
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
WLog_ERR(TAG, "@ WARNING: CERTIFICATE NAME MISMATCH! @");
|
|
|
|
WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "The hostname used for this connection (%s:%" PRIu16 ") ", hostname, port);
|
2015-06-10 11:59:14 +03:00
|
|
|
WLog_ERR(TAG, "does not match %s given in the certificate:",
|
2016-09-20 09:58:04 +03:00
|
|
|
alt_names_count < 1 ? "the name" : "any of the names");
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "Common Name (CN):");
|
|
|
|
WLog_ERR(TAG, "\t%s", common_name ? common_name : "no CN found in certificate");
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-02-14 18:25:48 +04:00
|
|
|
if (alt_names_count > 0)
|
2012-02-04 11:21:39 +04:00
|
|
|
{
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(NULL != alt_names);
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "Alternative names:");
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2014-02-14 18:25:48 +04:00
|
|
|
for (index = 0; index < alt_names_count; index++)
|
2012-02-04 11:21:39 +04:00
|
|
|
{
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(alt_names[index]);
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "\t %s", alt_names[index]);
|
2012-02-04 11:21:39 +04:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-02-06 22:21:26 +03:00
|
|
|
WLog_ERR(TAG, "A valid certificate for the wrong name should NOT be trusted!");
|
2012-02-04 11:21:39 +04:00
|
|
|
}
|
|
|
|
|
2012-02-03 03:20:02 +04:00
|
|
|
rdpTls* tls_new(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
rdpTls* tls;
|
2019-11-06 17:24:51 +03:00
|
|
|
tls = (rdpTls*)calloc(1, sizeof(rdpTls));
|
2014-05-30 22:36:18 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls)
|
|
|
|
return NULL;
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tls->settings = settings;
|
2014-05-30 22:36:18 +04:00
|
|
|
|
2014-07-18 05:15:22 +04:00
|
|
|
if (!settings->ServerMode)
|
|
|
|
{
|
|
|
|
tls->certificate_store = certificate_store_new(settings);
|
|
|
|
|
|
|
|
if (!tls->certificate_store)
|
|
|
|
goto out_free;
|
|
|
|
}
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tls->alertLevel = TLS_ALERT_LEVEL_WARNING;
|
|
|
|
tls->alertDescription = TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY;
|
2012-02-03 03:20:02 +04:00
|
|
|
return tls;
|
2014-05-21 19:32:14 +04:00
|
|
|
out_free:
|
|
|
|
free(tls);
|
|
|
|
return NULL;
|
2012-02-03 03:20:02 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
void tls_free(rdpTls* tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls)
|
|
|
|
return;
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (tls->ctx)
|
|
|
|
{
|
|
|
|
SSL_CTX_free(tls->ctx);
|
|
|
|
tls->ctx = NULL;
|
|
|
|
}
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2018-11-08 14:09:49 +03:00
|
|
|
/* tls->underlying is a stacked BIO under tls->bio.
|
|
|
|
* BIO_free_all will free recursivly. */
|
2021-09-17 10:03:18 +03:00
|
|
|
if (tls->bio)
|
|
|
|
BIO_free_all(tls->bio);
|
|
|
|
else if (tls->underlying)
|
|
|
|
BIO_free_all(tls->underlying);
|
2018-11-08 14:09:49 +03:00
|
|
|
tls->bio = NULL;
|
|
|
|
tls->underlying = NULL;
|
2015-02-18 23:36:57 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (tls->PublicKey)
|
|
|
|
{
|
|
|
|
free(tls->PublicKey);
|
|
|
|
tls->PublicKey = NULL;
|
|
|
|
}
|
2012-02-12 21:46:53 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (tls->Bindings)
|
|
|
|
{
|
|
|
|
free(tls->Bindings->Bindings);
|
|
|
|
free(tls->Bindings);
|
|
|
|
tls->Bindings = NULL;
|
|
|
|
}
|
2013-01-26 02:52:37 +04:00
|
|
|
|
2014-07-18 05:15:22 +04:00
|
|
|
if (tls->certificate_store)
|
|
|
|
{
|
|
|
|
certificate_store_free(tls->certificate_store);
|
|
|
|
tls->certificate_store = NULL;
|
|
|
|
}
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
free(tls);
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|