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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-08-30 16:19:50 +04:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-01-09 09:20:08 +04:00
|
|
|
#include <winpr/sspi.h>
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-12-21 12:23:50 +04:00
|
|
|
#include <freerdp/utils/tcp.h>
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <freerdp/crypto/tls.h>
|
2011-07-04 01:29:09 +04:00
|
|
|
|
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;
|
|
|
|
X509* server_cert;
|
|
|
|
|
2012-03-19 04:08:05 +04:00
|
|
|
if (peer)
|
|
|
|
server_cert = SSL_get_peer_certificate(tls->ssl);
|
|
|
|
else
|
|
|
|
server_cert = SSL_get_certificate(tls->ssl);
|
2012-02-12 22:05:56 +04:00
|
|
|
|
|
|
|
if (!server_cert)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "tls_get_certificate: failed to get the server TLS certificate\n");
|
2012-02-12 22:05:56 +04:00
|
|
|
cert = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
cert = malloc(sizeof(*cert));
|
2012-02-12 22:05:56 +04:00
|
|
|
cert->px509 = server_cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
#define TLS_SERVER_END_POINT "tls-server-end-point:"
|
|
|
|
|
|
|
|
SecPkgContext_Bindings* tls_get_channel_bindings(X509* cert)
|
|
|
|
{
|
|
|
|
int PrefixLength;
|
|
|
|
BYTE CertificateHash[32];
|
|
|
|
UINT32 CertificateHashLength;
|
|
|
|
BYTE* ChannelBindingToken;
|
|
|
|
UINT32 ChannelBindingTokenLength;
|
|
|
|
SEC_CHANNEL_BINDINGS* ChannelBindings;
|
|
|
|
SecPkgContext_Bindings* ContextBindings;
|
|
|
|
|
|
|
|
ZeroMemory(CertificateHash, sizeof(CertificateHash));
|
|
|
|
X509_digest(cert, EVP_sha256(), CertificateHash, &CertificateHashLength);
|
|
|
|
|
|
|
|
PrefixLength = strlen(TLS_SERVER_END_POINT);
|
|
|
|
ChannelBindingTokenLength = PrefixLength + CertificateHashLength;
|
|
|
|
|
|
|
|
ContextBindings = (SecPkgContext_Bindings*) malloc(sizeof(SecPkgContext_Bindings));
|
|
|
|
ZeroMemory(ContextBindings, sizeof(SecPkgContext_Bindings));
|
|
|
|
|
|
|
|
ContextBindings->BindingsLength = sizeof(SEC_CHANNEL_BINDINGS) + ChannelBindingTokenLength;
|
|
|
|
ChannelBindings = (SEC_CHANNEL_BINDINGS*) malloc(ContextBindings->BindingsLength);
|
|
|
|
ZeroMemory(ChannelBindings, ContextBindings->BindingsLength);
|
|
|
|
ContextBindings->Bindings = ChannelBindings;
|
|
|
|
|
|
|
|
ChannelBindings->cbApplicationDataLength = ChannelBindingTokenLength;
|
|
|
|
ChannelBindings->dwApplicationDataOffset = sizeof(SEC_CHANNEL_BINDINGS);
|
|
|
|
ChannelBindingToken = &((BYTE*) ChannelBindings)[ChannelBindings->dwApplicationDataOffset];
|
|
|
|
|
|
|
|
strcpy((char*) ChannelBindingToken, TLS_SERVER_END_POINT);
|
|
|
|
CopyMemory(&ChannelBindingToken[PrefixLength], CertificateHash, CertificateHashLength);
|
|
|
|
|
|
|
|
return ContextBindings;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:27:22 +04:00
|
|
|
static void tls_ssl_info_callback(const SSL* ssl, int type, int val)
|
|
|
|
{
|
|
|
|
if (type & SSL_CB_HANDSHAKE_START)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
int tls_connect(rdpTls* tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2012-02-12 22:05:56 +04:00
|
|
|
CryptoCert cert;
|
2012-06-25 19:17:47 +04:00
|
|
|
long options = 0;
|
2014-04-02 00:23:27 +04:00
|
|
|
int verify_status;
|
2011-07-04 01:29:09 +04:00
|
|
|
int connection_status;
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
tls->ctx = SSL_CTX_new(TLSv1_client_method());
|
|
|
|
|
2013-10-11 14:12:50 +04:00
|
|
|
if (!tls->ctx)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_CTX_new failed\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2013-02-10 02:13:53 +04: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_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.
|
2011-08-19 09:35:29 +04:00
|
|
|
*/
|
2012-06-25 19:17:47 +04:00
|
|
|
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;
|
|
|
|
|
|
|
|
SSL_CTX_set_options(tls->ctx, options);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2011-07-04 01:29:09 +04:00
|
|
|
tls->ssl = SSL_new(tls->ctx);
|
|
|
|
|
2013-10-11 14:12:50 +04:00
|
|
|
if (!tls->ssl)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_new failed\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2013-10-11 14:12:50 +04:00
|
|
|
if (tls->tsg)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2013-10-11 14:12:50 +04:00
|
|
|
tls->bio = BIO_new(tls->methods);
|
|
|
|
|
|
|
|
if (!tls->bio)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "BIO_new failed\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
tls->bio->ptr = tls->tsg;
|
|
|
|
|
|
|
|
SSL_set_bio(tls->ssl, tls->bio, tls->bio);
|
2013-10-11 23:27:22 +04:00
|
|
|
|
|
|
|
SSL_CTX_set_info_callback(tls->ctx, tls_ssl_info_callback);
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (SSL_set_fd(tls->ssl, tls->sockfd) < 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "SSL_set_fd failed\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
connection_status = SSL_connect(tls->ssl);
|
|
|
|
|
|
|
|
if (connection_status <= 0)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2011-08-19 09:35:29 +04:00
|
|
|
if (tls_print_error("SSL_connect", tls->ssl, connection_status))
|
2012-02-12 21:07:34 +04:00
|
|
|
{
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2012-02-12 21:07:34 +04:00
|
|
|
}
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
2011-07-18 07:16:31 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
cert = tls_get_certificate(tls, TRUE);
|
2012-02-12 21:46:53 +04:00
|
|
|
|
2013-10-11 13:07:33 +04:00
|
|
|
if (!cert)
|
2012-02-12 21:46:53 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "tls_connect: tls_get_certificate failed to return the server certificate.\n");
|
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);
|
|
|
|
|
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
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "tls_connect: crypto_cert_get_public_key failed to return the server public key.\n");
|
2012-09-24 12:40:32 +04:00
|
|
|
tls_free_certificate(cert);
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2012-02-12 21:46:53 +04:00
|
|
|
}
|
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
verify_status = tls_verify_certificate(tls, cert, tls->hostname, tls->port);
|
|
|
|
|
|
|
|
if (verify_status < 1)
|
2012-06-13 07:09:30 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "tls_connect: certificate not trusted, aborting.\n");
|
2012-02-12 21:46:53 +04:00
|
|
|
tls_disconnect(tls);
|
2012-04-11 00:24:08 +04:00
|
|
|
}
|
2012-02-12 21:46:53 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL tls_accept(rdpTls* tls, const char* cert_file, const char* privatekey_file)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2012-03-19 04:08:05 +04:00
|
|
|
CryptoCert cert;
|
2012-06-25 19:17:47 +04:00
|
|
|
long options = 0;
|
2011-08-19 09:35:29 +04:00
|
|
|
int connection_status;
|
|
|
|
|
2012-02-23 03:38:52 +04:00
|
|
|
tls->ctx = SSL_CTX_new(SSLv23_server_method());
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
if (tls->ctx == NULL)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_CTX_new failed\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2012-02-23 03:38:52 +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;
|
|
|
|
|
|
|
|
SSL_CTX_set_options(tls->ctx, options);
|
2012-02-23 03:38:52 +04:00
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
if (SSL_CTX_use_RSAPrivateKey_file(tls->ctx, privatekey_file, SSL_FILETYPE_PEM) <= 0)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_CTX_use_RSAPrivateKey_file failed\n");
|
2013-04-24 02:17:01 +04:00
|
|
|
fprintf(stderr, "PrivateKeyFile: %s\n", privatekey_file);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
tls->ssl = SSL_new(tls->ctx);
|
|
|
|
|
2013-04-24 02:17:01 +04:00
|
|
|
if (!tls->ssl)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_new failed\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SSL_use_certificate_file(tls->ssl, cert_file, SSL_FILETYPE_PEM) <= 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_use_certificate_file failed\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SSL_set_fd(tls->ssl, tls->sockfd) < 1)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "SSL_set_fd failed\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2012-06-13 07:09:30 +04:00
|
|
|
while (1)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2012-06-13 07:09:30 +04:00
|
|
|
connection_status = SSL_accept(tls->ssl);
|
|
|
|
|
|
|
|
if (connection_status <= 0)
|
|
|
|
{
|
|
|
|
switch (SSL_get_error(tls->ssl, connection_status))
|
|
|
|
{
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (tls_print_error("SSL_accept", tls->ssl, connection_status))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-06-13 07:09:30 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2013-05-31 03:44:58 +04:00
|
|
|
cert = tls_get_certificate(tls, FALSE);
|
|
|
|
|
|
|
|
if (!cert)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tls_connect: tls_get_certificate failed to return the server certificate.\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!crypto_cert_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tls_connect: crypto_cert_get_public_key failed to return the server public key.\n");
|
|
|
|
tls_free_certificate(cert);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cert);
|
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "TLS connection accepted\n");
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL tls_disconnect(rdpTls* tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2013-11-14 13:09:40 +04:00
|
|
|
if (!tls)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-08-09 03:53:58 +04:00
|
|
|
if (tls->ssl)
|
2013-12-19 04:44:18 +04:00
|
|
|
{
|
|
|
|
if (tls->alertDescription != TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SSL_set_quiet_shutdown(tls->ssl, 1);
|
|
|
|
|
|
|
|
if ((tls->alertLevel == TLS_ALERT_LEVEL_FATAL) && (tls->ssl->session))
|
|
|
|
SSL_CTX_remove_session(tls->ssl->ctx, tls->ssl->session);
|
|
|
|
|
|
|
|
tls->ssl->s3->alert_dispatch = 1;
|
|
|
|
tls->ssl->s3->send_alert[0] = tls->alertLevel;
|
|
|
|
tls->ssl->s3->send_alert[1] = tls->alertDescription;
|
|
|
|
|
|
|
|
if (tls->ssl->s3->wbuf.left == 0)
|
|
|
|
tls->ssl->method->ssl_dispatch_alert(tls->ssl);
|
|
|
|
|
|
|
|
SSL_shutdown(tls->ssl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SSL_shutdown(tls->ssl);
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
int tls_read(rdpTls* tls, BYTE* data, int length)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2012-11-28 03:34:00 +04:00
|
|
|
int error;
|
2011-07-04 01:29:09 +04:00
|
|
|
int status;
|
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (!tls)
|
|
|
|
return -1;
|
2011-08-01 18:21:06 +04:00
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (!tls->ssl)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
status = SSL_read(tls->ssl, data, length);
|
|
|
|
|
2013-12-04 18:36:25 +04:00
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
return -1; /* peer disconnected */
|
|
|
|
}
|
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (status <= 0)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2012-11-28 03:34:00 +04:00
|
|
|
error = SSL_get_error(tls->ssl, status);
|
2011-08-01 18:21:06 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
//fprintf(stderr, "tls_read: length: %d status: %d error: 0x%08X\n",
|
2012-11-28 03:34:00 +04:00
|
|
|
// length, status, error);
|
2011-08-01 18:21:06 +04:00
|
|
|
|
2012-11-28 03:34:00 +04:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
|
2013-05-16 04:19:26 +04:00
|
|
|
case SSL_ERROR_SYSCALL:
|
2014-04-29 19:05:30 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK)
|
|
|
|
#else
|
2013-11-21 00:21:29 +04:00
|
|
|
if ((errno == EAGAIN) || (errno == 0))
|
2014-04-29 19:05:30 +04:00
|
|
|
#endif
|
2013-05-16 04:19:26 +04:00
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-29 17:48:11 +04:00
|
|
|
if (tls_print_error("SSL_read", tls->ssl, status))
|
|
|
|
{
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
}
|
2013-05-16 04:19:26 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-11-28 03:34:00 +04:00
|
|
|
default:
|
2014-04-29 17:48:11 +04:00
|
|
|
if (tls_print_error("SSL_read", tls->ssl, status))
|
|
|
|
{
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
}
|
2012-11-28 03:34:00 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2011-08-01 18:21:06 +04:00
|
|
|
return status;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
int tls_write(rdpTls* tls, BYTE* data, int length)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2012-11-28 03:34:00 +04:00
|
|
|
int error;
|
2011-07-10 23:34:43 +04:00
|
|
|
int status;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (!tls)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!tls->ssl)
|
|
|
|
return -1;
|
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
status = SSL_write(tls->ssl, data, length);
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2013-12-21 02:56:59 +04:00
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
return -1; /* peer disconnected */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status < 0)
|
2011-08-01 11:04:07 +04:00
|
|
|
{
|
2012-11-28 03:34:00 +04:00
|
|
|
error = SSL_get_error(tls->ssl, status);
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
//fprintf(stderr, "tls_write: length: %d status: %d error: 0x%08X\n", length, status, error);
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2012-11-28 03:34:00 +04:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
|
2013-05-16 04:19:26 +04:00
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tls_print_error("SSL_write", tls->ssl, status);
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-11-28 03:34:00 +04:00
|
|
|
default:
|
|
|
|
tls_print_error("SSL_write", tls->ssl, status);
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
return status;
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
int tls_write_all(rdpTls* tls, BYTE* data, int length)
|
2012-04-18 10:28:05 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
int sent = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = tls_write(tls, &data[sent], length - sent);
|
|
|
|
|
|
|
|
if (status > 0)
|
|
|
|
sent += status;
|
2012-12-21 12:23:50 +04:00
|
|
|
else if (status == 0)
|
|
|
|
tls_wait_write(tls);
|
2012-04-18 10:28:05 +04:00
|
|
|
|
|
|
|
if (sent >= length)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (status >= 0);
|
|
|
|
|
|
|
|
if (status > 0)
|
|
|
|
return length;
|
|
|
|
else
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-12-21 12:23:50 +04:00
|
|
|
int tls_wait_read(rdpTls* tls)
|
|
|
|
{
|
|
|
|
return freerdp_tcp_wait_read(tls->sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int tls_wait_write(rdpTls* tls)
|
|
|
|
{
|
|
|
|
return freerdp_tcp_wait_write(tls->sockfd);
|
|
|
|
}
|
|
|
|
|
2012-02-23 03:36:15 +04:00
|
|
|
static void tls_errors(const char *prefix)
|
|
|
|
{
|
|
|
|
unsigned long error;
|
|
|
|
|
|
|
|
while ((error = ERR_get_error()) != 0)
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: %s\n", prefix, ERR_error_string(error, NULL));
|
2012-02-23 03:36:15 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL tls_print_error(char* func, SSL* connection, int value)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
|
|
|
switch (SSL_get_error(connection, value))
|
|
|
|
{
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: Server closed TLS connection\n", func);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: SSL_ERROR_WANT_READ\n", func);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: SSL_ERROR_WANT_WRITE\n", func);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2014-04-29 19:05:30 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
fprintf(stderr, "%s: I/O error: %d\n", func, WSAGetLastError());
|
|
|
|
#else
|
2013-05-16 04:19:26 +04:00
|
|
|
fprintf(stderr, "%s: I/O error: %s (%d)\n", func, strerror(errno), errno);
|
2014-04-29 19:05:30 +04:00
|
|
|
#endif
|
2012-02-23 03:36:15 +04:00
|
|
|
tls_errors(func);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
case SSL_ERROR_SSL:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: Failure in SSL library (protocol error?)\n", func);
|
2012-02-23 03:36:15 +04:00
|
|
|
tls_errors(func);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
default:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: Unknown error\n", func);
|
2012-02-23 03:36:15 +04:00
|
|
|
tls_errors(func);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 01:29:09 +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;
|
|
|
|
}
|
|
|
|
|
2013-06-17 23:19:01 +04:00
|
|
|
BOOL tls_match_hostname(char *pattern, int pattern_length, char *hostname)
|
|
|
|
{
|
|
|
|
if (strlen(hostname) == pattern_length)
|
|
|
|
{
|
|
|
|
if (memcmp((void*) hostname, (void*) pattern, pattern_length) == 0)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-02-11 07:12:13 +04:00
|
|
|
if ((pattern_length > 2) && (pattern[0] == '*') && (pattern[1] == '.') && (((int) strlen(hostname)) >= pattern_length))
|
2013-06-17 23:19:01 +04:00
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
char* check_hostname = &hostname[strlen(hostname) - pattern_length + 1];
|
|
|
|
|
|
|
|
if (memcmp((void*) check_hostname, (void*) &pattern[1], pattern_length - 1) == 0)
|
2013-06-17 23:19:01 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
int tls_verify_certificate(rdpTls* tls, CryptoCert cert, char* hostname, int 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;
|
2012-02-27 19:55:49 +04:00
|
|
|
char* common_name = NULL;
|
|
|
|
int common_name_length = 0;
|
|
|
|
char** alt_names = NULL;
|
|
|
|
int alt_names_count = 0;
|
|
|
|
int* alt_names_lengths = NULL;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL certificate_status;
|
|
|
|
BOOL hostname_match = FALSE;
|
|
|
|
BOOL verification_status = FALSE;
|
2012-02-05 00:04:03 +04:00
|
|
|
rdpCertificateData* certificate_data;
|
2012-02-03 02:36:07 +04:00
|
|
|
|
2013-11-18 22:54:33 +04:00
|
|
|
if (tls->settings->ExternalCertificateManagement)
|
|
|
|
{
|
|
|
|
BIO* bio;
|
|
|
|
int status;
|
|
|
|
int length;
|
|
|
|
int offset;
|
|
|
|
BYTE* pemCert;
|
|
|
|
freerdp* instance = (freerdp*) tls->settings->instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't manage certificates internally, leave it up entirely to the external client implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
bio = BIO_new(BIO_s_mem());
|
2013-11-25 21:08:58 +04:00
|
|
|
|
|
|
|
if (!bio)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tls_verify_certificate: BIO_new() failure\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-11-25 21:08:58 +04:00
|
|
|
}
|
2013-11-18 22:54:33 +04:00
|
|
|
|
|
|
|
status = PEM_write_bio_X509(bio, cert->px509);
|
|
|
|
|
2013-11-25 21:08:58 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tls_verify_certificate: PEM_write_bio_X509 failure: %d\n", status);
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-11-25 21:08:58 +04:00
|
|
|
}
|
|
|
|
|
2013-11-18 22:54:33 +04:00
|
|
|
offset = 0;
|
|
|
|
length = 2048;
|
|
|
|
pemCert = (BYTE*) malloc(length + 1);
|
|
|
|
|
|
|
|
status = BIO_read(bio, pemCert, length);
|
2013-11-25 21:08:58 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tls_verify_certificate: failed to read certificate\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-11-25 21:08:58 +04:00
|
|
|
}
|
|
|
|
|
2013-11-18 22:54:33 +04:00
|
|
|
offset += status;
|
|
|
|
|
|
|
|
while (offset >= length)
|
|
|
|
{
|
|
|
|
length *= 2;
|
|
|
|
pemCert = (BYTE*) realloc(pemCert, length + 1);
|
|
|
|
|
|
|
|
status = BIO_read(bio, &pemCert[offset], length);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset += status;
|
|
|
|
}
|
|
|
|
|
2013-11-25 21:08:58 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tls_verify_certificate: failed to read certificate\n");
|
2014-04-02 00:23:27 +04:00
|
|
|
return -1;
|
2013-11-25 21:08:58 +04:00
|
|
|
}
|
|
|
|
|
2013-11-18 22:54:33 +04:00
|
|
|
length = offset;
|
|
|
|
pemCert[length] = '\0';
|
|
|
|
|
|
|
|
status = -1;
|
2013-11-25 21:08:58 +04:00
|
|
|
|
2013-11-18 22:54:33 +04:00
|
|
|
if (instance->VerifyX509Certificate)
|
|
|
|
{
|
2013-11-25 23:30:43 +04:00
|
|
|
status = instance->VerifyX509Certificate(instance, pemCert, length, hostname, port, 0);
|
2013-11-18 22:54:33 +04:00
|
|
|
}
|
2013-11-25 21:08:58 +04:00
|
|
|
|
|
|
|
fprintf(stderr, "VerifyX509Certificate: (length = %d) status: %d\n%s\n",
|
|
|
|
length, status, pemCert);
|
2013-11-18 22:54:33 +04:00
|
|
|
|
|
|
|
free(pemCert);
|
2013-11-25 21:08:58 +04:00
|
|
|
BIO_free(bio);
|
2013-11-18 22:54:33 +04:00
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return (status == 0) ? 0 : 1;
|
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) */
|
2012-11-08 00:13:14 +04:00
|
|
|
if (tls->settings->IgnoreCertificate)
|
2014-04-02 00:23:27 +04:00
|
|
|
return 1; /* success! */
|
2011-08-30 20:20:36 +04:00
|
|
|
|
2012-02-05 00:16:41 +04:00
|
|
|
/* if user explicitly specified a certificate name, use it instead of the hostname */
|
2012-11-08 00:13:14 +04:00
|
|
|
if (tls->settings->CertificateName)
|
|
|
|
hostname = tls->settings->CertificateName;
|
2012-02-05 00:16:41 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* attempt verification using OpenSSL and the ~/.freerdp/certs certificate store */
|
|
|
|
certificate_status = x509_verify_certificate(cert, tls->certificate_store->path);
|
2011-12-10 20:23:48 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* verify certificate name match */
|
|
|
|
certificate_data = crypto_get_certificate_data(cert->px509, hostname);
|
2011-08-30 20:20:36 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* extra common name and alternative names */
|
2012-02-08 07:16:57 +04:00
|
|
|
common_name = crypto_cert_subject_common_name(cert->px509, &common_name_length);
|
|
|
|
alt_names = crypto_cert_subject_alt_name(cert->px509, &alt_names_count, &alt_names_lengths);
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* compare against common name */
|
2012-02-08 07:16:57 +04:00
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
if (common_name)
|
2012-02-08 07:16:57 +04:00
|
|
|
{
|
2013-06-17 23:19:01 +04:00
|
|
|
if (tls_match_hostname(common_name, common_name_length, hostname))
|
|
|
|
hostname_match = TRUE;
|
2012-02-08 07:16:57 +04:00
|
|
|
}
|
2012-02-04 11:21:39 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* compare against alternative names */
|
2012-02-08 07:16:57 +04:00
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
if (alt_names)
|
2012-02-05 00:04:03 +04:00
|
|
|
{
|
2012-02-08 07:16:57 +04:00
|
|
|
for (index = 0; index < alt_names_count; index++)
|
|
|
|
{
|
2013-06-17 23:19:01 +04:00
|
|
|
if (tls_match_hostname(alt_names[index], alt_names_lengths[index], hostname))
|
2012-02-08 07:16:57 +04:00
|
|
|
{
|
2013-06-17 23:19:01 +04:00
|
|
|
hostname_match = TRUE;
|
|
|
|
break;
|
2012-02-08 07:16:57 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-05 00:04:03 +04:00
|
|
|
}
|
2012-02-04 11:21:39 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* if the certificate is valid and the certificate name matches, verification succeeds */
|
|
|
|
if (certificate_status && hostname_match)
|
2012-02-27 19:55:49 +04:00
|
|
|
{
|
|
|
|
if (common_name)
|
2012-05-04 17:50:33 +04:00
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(common_name);
|
2012-06-27 13:10:17 +04:00
|
|
|
common_name = NULL;
|
2012-05-04 17:50:33 +04:00
|
|
|
}
|
2012-02-27 19:55:49 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
verification_status = TRUE; /* success! */
|
2012-02-27 19:55:49 +04:00
|
|
|
}
|
2012-02-05 00:04:03 +04:00
|
|
|
|
|
|
|
/* if the certificate is valid but the certificate name does not match, warn user, do not accept */
|
|
|
|
if (certificate_status && !hostname_match)
|
|
|
|
tls_print_certificate_name_mismatch_error(hostname, common_name, alt_names, alt_names_count);
|
2012-02-04 11:21:39 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* verification could not succeed with OpenSSL, use known_hosts file and prompt user for manual verification */
|
|
|
|
|
|
|
|
if (!certificate_status)
|
|
|
|
{
|
|
|
|
char* issuer;
|
|
|
|
char* subject;
|
|
|
|
char* fingerprint;
|
2012-06-29 05:05:10 +04:00
|
|
|
freerdp* instance = (freerdp*) tls->settings->instance;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL accept_certificate = FALSE;
|
2011-08-30 20:20:36 +04:00
|
|
|
|
2011-12-10 20:23:48 +04:00
|
|
|
issuer = crypto_cert_issuer(cert->px509);
|
|
|
|
subject = crypto_cert_subject(cert->px509);
|
|
|
|
fingerprint = crypto_cert_fingerprint(cert->px509);
|
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
/* search for matching entry in known_hosts file */
|
|
|
|
match = certificate_data_match(tls->certificate_store, certificate_data);
|
|
|
|
|
2012-02-03 03:20:02 +04:00
|
|
|
if (match == 1)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2012-02-05 00:04:03 +04:00
|
|
|
/* no entry was found in known_hosts file, prompt user for manual verification */
|
|
|
|
if (!hostname_match)
|
|
|
|
tls_print_certificate_name_mismatch_error(hostname, common_name, alt_names, alt_names_count);
|
2011-10-19 11:42:53 +04:00
|
|
|
|
2012-02-05 00:04:03 +04:00
|
|
|
if (instance->VerifyCertificate)
|
2014-03-21 02:19:54 +04:00
|
|
|
{
|
2012-02-05 00:04:03 +04:00
|
|
|
accept_certificate = instance->VerifyCertificate(instance, subject, issuer, fingerprint);
|
2014-03-21 02:19:54 +04:00
|
|
|
}
|
|
|
|
|
2011-12-10 20:23:48 +04:00
|
|
|
if (!accept_certificate)
|
2012-02-05 00:04:03 +04:00
|
|
|
{
|
|
|
|
/* user did not accept, abort and do not add entry in known_hosts file */
|
2012-10-09 10:31:28 +04:00
|
|
|
verification_status = FALSE; /* failure! */
|
2012-02-05 00:04:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* user accepted certificate, add entry in known_hosts file */
|
|
|
|
certificate_data_print(tls->certificate_store, certificate_data);
|
2012-10-09 10:31:28 +04:00
|
|
|
verification_status = TRUE; /* success! */
|
2012-02-05 00:04:03 +04:00
|
|
|
}
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
2012-02-03 03:20:02 +04:00
|
|
|
else if (match == -1)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2012-06-29 05:05:10 +04:00
|
|
|
/* entry was found in known_hosts file, but fingerprint does not match. ask user to use it */
|
2013-05-17 15:11:10 +04:00
|
|
|
tls_print_certificate_error(hostname, fingerprint, tls->certificate_store->file);
|
2012-06-29 05:05:10 +04:00
|
|
|
|
|
|
|
if (instance->VerifyChangedCertificate)
|
2014-03-21 02:19:54 +04:00
|
|
|
{
|
2012-06-29 05:05:10 +04:00
|
|
|
accept_certificate = instance->VerifyChangedCertificate(instance, subject, issuer, fingerprint, "");
|
2014-03-21 02:19:54 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 05:05:10 +04:00
|
|
|
if (!accept_certificate)
|
|
|
|
{
|
|
|
|
/* user did not accept, abort and do not change known_hosts file */
|
2012-10-09 10:31:28 +04:00
|
|
|
verification_status = FALSE; /* failure! */
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* user accepted new certificate, add replace fingerprint for this host in known_hosts file */
|
|
|
|
certificate_data_replace(tls->certificate_store, certificate_data);
|
2012-10-09 10:31:28 +04:00
|
|
|
verification_status = TRUE; /* success! */
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
2012-02-05 00:04:03 +04:00
|
|
|
else if (match == 0)
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
verification_status = TRUE; /* success! */
|
2012-02-05 00:04:03 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(issuer);
|
|
|
|
free(subject);
|
|
|
|
free(fingerprint);
|
2012-02-27 19:55:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (certificate_data)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(certificate_data->fingerprint);
|
|
|
|
free(certificate_data->hostname);
|
|
|
|
free(certificate_data);
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
2011-08-30 20:20:36 +04:00
|
|
|
|
2012-11-22 18:06:45 +04:00
|
|
|
#ifndef _WIN32
|
2013-07-01 21:07:35 +04:00
|
|
|
if (common_name)
|
|
|
|
free(common_name);
|
2012-11-22 18:06:45 +04:00
|
|
|
#endif
|
2012-11-19 22:26:56 +04:00
|
|
|
|
2013-08-28 19:10:58 +04:00
|
|
|
if (alt_names)
|
2013-09-02 18:14:22 +04:00
|
|
|
crypto_cert_subject_alt_name_free(alt_names_count, alt_names_lengths,
|
|
|
|
alt_names);
|
2013-08-28 19:10:58 +04:00
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
return (verification_status == 0) ? 0 : 1;
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:11:10 +04:00
|
|
|
void tls_print_certificate_error(char* hostname, char* fingerprint, char *hosts_file)
|
2011-08-26 07:10:49 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "The host key for %s has changed\n", hostname);
|
|
|
|
fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
|
|
|
|
fprintf(stderr, "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\n");
|
|
|
|
fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
|
|
|
|
fprintf(stderr, "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\n");
|
|
|
|
fprintf(stderr, "Someone could be eavesdropping on you right now (man-in-the-middle attack)!\n");
|
|
|
|
fprintf(stderr, "It is also possible that a host key has just been changed.\n");
|
|
|
|
fprintf(stderr, "The fingerprint for the host key sent by the remote host is\n%s\n", fingerprint);
|
|
|
|
fprintf(stderr, "Please contact your system administrator.\n");
|
2013-05-17 15:11:10 +04:00
|
|
|
fprintf(stderr, "Add correct host key in %s to get rid of this message.\n", hosts_file);
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Host key for %s has changed and you have requested strict checking.\n", hostname);
|
|
|
|
fprintf(stderr, "Host key verification failed.\n");
|
2011-08-26 07:10:49 +04:00
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2012-02-04 11:21:39 +04:00
|
|
|
void tls_print_certificate_name_mismatch_error(char* hostname, char* common_name, char** alt_names, int alt_names_count)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
2013-08-30 16:19:50 +04:00
|
|
|
assert(NULL != hostname);
|
2014-02-14 18:25:48 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
|
|
|
|
fprintf(stderr, "@ WARNING: CERTIFICATE NAME MISMATCH! @\n");
|
|
|
|
fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
|
|
|
|
fprintf(stderr, "The hostname used for this connection (%s) \n", hostname);
|
2013-07-01 21:07:35 +04:00
|
|
|
fprintf(stderr, "does not match %s given in the certificate:\n", alt_names_count < 1 ? "the name" : "any of the names");
|
|
|
|
fprintf(stderr, "Common Name (CN):\n");
|
|
|
|
fprintf(stderr, "\t%s\n", common_name ? common_name : "no CN found in certificate");
|
2014-02-14 18:25:48 +04:00
|
|
|
if (alt_names_count > 0)
|
2012-02-04 11:21:39 +04:00
|
|
|
{
|
2013-08-30 16:19:50 +04:00
|
|
|
assert(NULL != alt_names);
|
2013-07-01 21:07:35 +04:00
|
|
|
fprintf(stderr, "Alternative names:\n");
|
2014-02-14 18:25:48 +04:00
|
|
|
for (index = 0; index < alt_names_count; index++)
|
2012-02-04 11:21:39 +04:00
|
|
|
{
|
2014-02-14 18:25:48 +04:00
|
|
|
assert(alt_names[index]);
|
|
|
|
fprintf(stderr, "\t %s\n", alt_names[index]);
|
2012-02-04 11:21:39 +04:00
|
|
|
}
|
|
|
|
}
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "A valid certificate for the wrong name should NOT be trusted!\n");
|
2012-02-04 11:21:39 +04:00
|
|
|
}
|
|
|
|
|
2012-02-03 03:20:02 +04:00
|
|
|
rdpTls* tls_new(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
rdpTls* tls;
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
tls = (rdpTls*) malloc(sizeof(rdpTls));
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (tls)
|
2012-02-03 03:20:02 +04:00
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
ZeroMemory(tls, sizeof(rdpTls));
|
|
|
|
|
2012-02-03 03:20:02 +04:00
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
|
|
|
|
tls->settings = settings;
|
|
|
|
tls->certificate_store = certificate_store_new(settings);
|
2013-12-19 04:44:18 +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;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
void tls_free(rdpTls* tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2013-11-01 07:35:24 +04:00
|
|
|
if (tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2011-08-25 09:45:43 +04:00
|
|
|
if (tls->ssl)
|
2013-10-23 01:05:41 +04:00
|
|
|
{
|
2011-08-25 09:45:43 +04:00
|
|
|
SSL_free(tls->ssl);
|
2013-10-23 01:05:41 +04:00
|
|
|
tls->ssl = NULL;
|
|
|
|
}
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2011-08-25 09:45:43 +04:00
|
|
|
if (tls->ctx)
|
2013-10-23 01:05:41 +04:00
|
|
|
{
|
2011-08-25 09:45:43 +04:00
|
|
|
SSL_CTX_free(tls->ctx);
|
2013-10-23 01:05:41 +04:00
|
|
|
tls->ctx = NULL;
|
|
|
|
}
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2012-09-24 12:40:32 +04:00
|
|
|
if (tls->PublicKey)
|
2013-10-23 01:05:41 +04:00
|
|
|
{
|
2012-09-24 12:40:32 +04:00
|
|
|
free(tls->PublicKey);
|
2013-10-23 01:05:41 +04:00
|
|
|
tls->PublicKey = NULL;
|
|
|
|
}
|
2012-02-12 21:46:53 +04:00
|
|
|
|
2013-01-26 02:52:37 +04:00
|
|
|
if (tls->Bindings)
|
|
|
|
{
|
|
|
|
free(tls->Bindings->Bindings);
|
|
|
|
free(tls->Bindings);
|
2013-10-23 01:05:41 +04:00
|
|
|
tls->Bindings = NULL;
|
2013-01-26 02:52:37 +04:00
|
|
|
}
|
|
|
|
|
2012-02-03 03:20:02 +04:00
|
|
|
certificate_store_free(tls->certificate_store);
|
2013-10-23 01:05:41 +04:00
|
|
|
tls->certificate_store = NULL;
|
2012-02-03 03:20:02 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(tls);
|
2011-07-04 01:29:09 +04:00
|
|
|
}
|
|
|
|
}
|