2011-07-04 01:29:09 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* Transport Layer Security
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <freerdp/utils/stream.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
|
|
|
|
#include "tls.h"
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
boolean tls_connect(rdpTls* tls)
|
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());
|
|
|
|
|
|
|
|
if (tls->ctx == NULL)
|
|
|
|
{
|
|
|
|
printf("SSL_CTX_new failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is necessary, because the Microsoft TLS implementation is not perfect.
|
|
|
|
* SSL_OP_ALL enables a couple of workarounds for buggy TLS implementations,
|
|
|
|
* but the most important workaround being SSL_OP_TLS_BLOCK_PADDING_BUG.
|
|
|
|
* As the size of the encrypted payload may give hints about its contents,
|
|
|
|
* block padding is normally used, but the Microsoft TLS implementation
|
|
|
|
* won't recognize it and will disconnect you after sending a TLS alert.
|
|
|
|
*/
|
|
|
|
SSL_CTX_set_options(tls->ctx, SSL_OP_ALL);
|
|
|
|
|
2011-07-04 01:29:09 +04:00
|
|
|
tls->ssl = SSL_new(tls->ctx);
|
|
|
|
|
|
|
|
if (tls->ssl == NULL)
|
|
|
|
{
|
|
|
|
printf("SSL_new failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SSL_set_fd(tls->ssl, tls->sockfd) < 1)
|
|
|
|
{
|
|
|
|
printf("SSL_set_fd failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
return False;
|
|
|
|
}
|
2011-07-18 07:16:31 +04:00
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean tls_accept(rdpTls* tls, const char* cert_file, const char* privatekey_file)
|
|
|
|
{
|
|
|
|
int connection_status;
|
|
|
|
|
|
|
|
tls->ctx = SSL_CTX_new(TLSv1_server_method());
|
|
|
|
|
|
|
|
if (tls->ctx == NULL)
|
|
|
|
{
|
|
|
|
printf("SSL_CTX_new failed\n");
|
|
|
|
return False;
|
2011-07-04 01:29:09 +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
|
|
|
{
|
2011-08-19 09:35:29 +04:00
|
|
|
printf("SSL_CTX_use_RSAPrivateKey_file failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
tls->ssl = SSL_new(tls->ctx);
|
|
|
|
|
|
|
|
if (tls->ssl == NULL)
|
|
|
|
{
|
|
|
|
printf("SSL_new failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SSL_use_certificate_file(tls->ssl, cert_file, SSL_FILETYPE_PEM) <= 0)
|
|
|
|
{
|
|
|
|
printf("SSL_use_certificate_file failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SSL_set_fd(tls->ssl, tls->sockfd) < 1)
|
|
|
|
{
|
|
|
|
printf("SSL_set_fd failed\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_status = SSL_accept(tls->ssl);
|
|
|
|
|
|
|
|
if (connection_status <= 0)
|
|
|
|
{
|
|
|
|
if (tls_print_error("SSL_accept", tls->ssl, connection_status))
|
2011-07-04 01:29:09 +04:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
printf("TLS connection accepted\n");
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
boolean tls_disconnect(rdpTls* tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2011-08-26 14:14:34 +04:00
|
|
|
SSL_shutdown(tls->ssl);
|
2011-07-04 01:29:09 +04:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
int tls_read(rdpTls* tls, uint8* data, int length)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2011-08-01 18:21:06 +04:00
|
|
|
status = SSL_read(tls->ssl, data, length);
|
|
|
|
|
|
|
|
switch (SSL_get_error(tls->ssl, status))
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2011-08-01 18:21:06 +04:00
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2011-08-19 09:35:29 +04:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2011-08-01 18:21:06 +04:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-08-19 09:35:29 +04:00
|
|
|
tls_print_error("SSL_read", tls->ssl, status);
|
2011-08-01 18:21:06 +04:00
|
|
|
status = -1;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
int tls_write(rdpTls* tls, uint8* data, int length)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2011-07-10 23:34:43 +04:00
|
|
|
int status;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
status = SSL_write(tls->ssl, data, length);
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
switch (SSL_get_error(tls->ssl, status))
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
break;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2011-08-01 11:04:07 +04:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
status = 0;
|
|
|
|
break;
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
default:
|
|
|
|
tls_print_error("SSL_write", tls->ssl, status);
|
|
|
|
status = -1;
|
2011-08-07 22:37:26 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
boolean 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:
|
|
|
|
printf("%s: Server closed TLS connection\n", func);
|
|
|
|
return True;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
printf("SSL_ERROR_WANT_READ\n");
|
|
|
|
return False;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
printf("SSL_ERROR_WANT_WRITE\n");
|
|
|
|
return False;
|
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
printf("%s: I/O error\n", func);
|
|
|
|
return True;
|
|
|
|
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
printf("%s: Failure in SSL library (protocol error?)\n", func);
|
|
|
|
return True;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("%s: Unknown error\n", func);
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
CryptoCert tls_get_certificate(rdpTls * tls)
|
2011-07-07 18:33:42 +04:00
|
|
|
{
|
|
|
|
CryptoCert cert;
|
2011-07-07 22:11:12 +04:00
|
|
|
X509* server_cert;
|
2011-07-07 18:33:42 +04:00
|
|
|
|
|
|
|
server_cert = SSL_get_peer_certificate(tls->ssl);
|
|
|
|
|
|
|
|
if (!server_cert)
|
|
|
|
{
|
|
|
|
printf("ssl_verify: failed to get the server SSL certificate\n");
|
|
|
|
cert = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cert = xmalloc(sizeof(*cert));
|
|
|
|
cert->px509 = server_cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
rdpTls* tls_new()
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
2011-07-07 22:11:12 +04:00
|
|
|
rdpTls* tls;
|
|
|
|
|
|
|
|
tls = (rdpTls*) xzalloc(sizeof(rdpTls));
|
2011-07-04 01:29:09 +04:00
|
|
|
|
|
|
|
if (tls != NULL)
|
|
|
|
{
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tls;
|
|
|
|
}
|
2011-08-29 00:46:36 +04:00
|
|
|
|
2011-08-30 20:20:36 +04:00
|
|
|
int tls_verify_certificate(CryptoCert cert, char* hostname)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
|
|
|
boolean ret;
|
2011-08-31 12:35:50 +04:00
|
|
|
rdpCertstore* certstore;
|
2011-08-30 20:20:36 +04:00
|
|
|
ret = x509_verify_cert(cert);
|
|
|
|
|
|
|
|
if (!ret)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2011-08-29 01:54:53 +04:00
|
|
|
rdpCertdata* certdata;
|
2011-08-30 20:20:36 +04:00
|
|
|
certdata = crypto_get_certdata(cert->px509, hostname);
|
2011-08-31 12:35:50 +04:00
|
|
|
certstore = certstore_new(certdata);
|
2011-08-30 20:20:36 +04:00
|
|
|
|
|
|
|
if (match_certdata(certstore) == 0)
|
2011-08-29 00:56:34 +04:00
|
|
|
goto end;
|
2011-08-30 20:20:36 +04:00
|
|
|
|
|
|
|
if (certstore->match == 1)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
|
|
|
char answer;
|
2011-08-30 20:20:36 +04:00
|
|
|
crypto_cert_printinfo(cert->px509);
|
|
|
|
|
2011-08-31 12:35:50 +04:00
|
|
|
while (1)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2011-08-30 20:20:36 +04:00
|
|
|
printf("Do you trust the above certificate? (Y/N) ");
|
2011-08-31 12:35:50 +04:00
|
|
|
answer = fgetc(stdin);
|
2011-08-30 20:20:36 +04:00
|
|
|
|
2011-08-31 12:35:50 +04:00
|
|
|
if (answer == 'y' || answer == 'Y')
|
2011-08-30 20:20:36 +04:00
|
|
|
{
|
|
|
|
print_certdata(certstore);
|
|
|
|
break;
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
2011-08-31 12:35:50 +04:00
|
|
|
else if (answer == 'n' || answer == 'N')
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2011-08-29 00:56:34 +04:00
|
|
|
certstore_free(certstore);
|
|
|
|
return 1;
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-30 20:20:36 +04:00
|
|
|
else if (certstore->match == -1)
|
2011-08-29 00:46:36 +04:00
|
|
|
{
|
2011-08-29 00:56:34 +04:00
|
|
|
tls_print_cert_error();
|
|
|
|
certstore_free(certstore);
|
|
|
|
return 1;
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
2011-08-30 20:20:36 +04:00
|
|
|
|
|
|
|
end:
|
2011-08-29 00:46:36 +04:00
|
|
|
certstore_free(certstore);
|
|
|
|
}
|
2011-08-30 20:20:36 +04:00
|
|
|
|
2011-08-29 01:54:53 +04:00
|
|
|
return 0;
|
2011-08-29 00:46:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tls_print_cert_error()
|
2011-08-26 07:10:49 +04:00
|
|
|
{
|
2011-08-29 00:46:36 +04:00
|
|
|
printf("#####################################\n");
|
|
|
|
printf("##############WARNING################\n");
|
|
|
|
printf("#####################################\n");
|
2011-08-30 20:20:36 +04:00
|
|
|
printf("The thumbprint of certificate received\n");
|
2011-08-29 00:46:36 +04:00
|
|
|
printf("did not match the stored thumbprint.You\n");
|
|
|
|
printf("might be a victim of MAN in the MIDDLE\n");
|
|
|
|
printf("ATTACK.It is also possible that server's\n");
|
|
|
|
printf("certificate have been changed.In that case\n");
|
|
|
|
printf("contact your server administrator\n");
|
2011-08-26 07:10:49 +04:00
|
|
|
}
|
2011-07-04 01:29:09 +04:00
|
|
|
|
2011-07-07 22:11:12 +04:00
|
|
|
void tls_free(rdpTls* tls)
|
2011-07-04 01:29:09 +04:00
|
|
|
{
|
|
|
|
if (tls != NULL)
|
|
|
|
{
|
2011-08-25 09:45:43 +04:00
|
|
|
if (tls->ssl)
|
|
|
|
SSL_free(tls->ssl);
|
|
|
|
if (tls->ctx)
|
|
|
|
SSL_CTX_free(tls->ctx);
|
2011-07-04 01:29:09 +04:00
|
|
|
xfree(tls);
|
|
|
|
}
|
|
|
|
}
|