2015-01-06 14:20:05 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2015-01-09 13:46:35 +00:00
|
|
|
* Copyright (C) 2015 Synergy Si Ltd.
|
2015-01-06 14:20:05 +00:00
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2015-01-06 14:20:05 +00:00
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SecureSocket.h"
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
#include "net/TSocketMultiplexerMethodJob.h"
|
2015-01-14 17:24:45 +00:00
|
|
|
#include "net/TCPSocket.h"
|
2015-01-26 13:23:11 +00:00
|
|
|
#include "mt/Lock.h"
|
2015-01-14 17:24:45 +00:00
|
|
|
#include "arch/XArch.h"
|
|
|
|
#include "base/Log.h"
|
|
|
|
|
2015-04-01 15:32:04 +00:00
|
|
|
#include <openssl/ssl.h>
|
2015-01-06 14:20:05 +00:00
|
|
|
#include <openssl/err.h>
|
2015-01-14 17:24:45 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
2015-04-01 15:04:10 +00:00
|
|
|
#include <fstream>
|
2015-04-01 15:32:04 +00:00
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
//
|
|
|
|
// SecureSocket
|
|
|
|
//
|
2015-01-14 17:24:45 +00:00
|
|
|
|
|
|
|
#define MAX_ERROR_SIZE 65535
|
2015-05-20 20:50:18 +00:00
|
|
|
#define MAX_RETRY_COUNT 60
|
2015-01-14 17:24:45 +00:00
|
|
|
|
2015-04-21 15:46:05 +00:00
|
|
|
static const char kFingerprintDirName[] = "SSL/Fingerprints";
|
|
|
|
//static const char kFingerprintLocalFilename[] = "Local.txt";
|
|
|
|
static const char kFingerprintTrustedServersFilename[] = "TrustedServers.txt";
|
|
|
|
//static const char kFingerprintTrustedClientsFilename[] = "TrustedClients.txt";
|
2015-04-15 13:16:28 +00:00
|
|
|
|
2015-01-09 13:46:35 +00:00
|
|
|
struct Ssl {
|
2015-01-06 14:20:05 +00:00
|
|
|
SSL_CTX* m_context;
|
|
|
|
SSL* m_ssl;
|
|
|
|
};
|
2015-01-09 13:46:35 +00:00
|
|
|
|
2015-04-14 12:44:10 +00:00
|
|
|
SecureSocket::SecureSocket(
|
2015-01-14 17:24:45 +00:00
|
|
|
IEventQueue* events,
|
2015-04-14 12:44:10 +00:00
|
|
|
SocketMultiplexer* socketMultiplexer) :
|
|
|
|
TCPSocket(events, socketMultiplexer),
|
2015-05-20 20:50:18 +00:00
|
|
|
m_secureReady(false),
|
2015-05-22 17:56:13 +00:00
|
|
|
m_maxRetry(MAX_RETRY_COUNT),
|
|
|
|
m_fatal(false)
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SecureSocket::SecureSocket(
|
|
|
|
IEventQueue* events,
|
|
|
|
SocketMultiplexer* socketMultiplexer,
|
|
|
|
ArchSocket socket) :
|
|
|
|
TCPSocket(events, socketMultiplexer, socket),
|
2015-05-20 20:50:18 +00:00
|
|
|
m_secureReady(false),
|
2015-05-22 17:56:13 +00:00
|
|
|
m_maxRetry(MAX_RETRY_COUNT),
|
|
|
|
m_fatal(false)
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-09 13:46:35 +00:00
|
|
|
SecureSocket::~SecureSocket()
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
2015-01-06 14:20:05 +00:00
|
|
|
if (m_ssl->m_ssl != NULL) {
|
2015-02-02 15:33:40 +00:00
|
|
|
SSL_shutdown(m_ssl->m_ssl);
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
SSL_free(m_ssl->m_ssl);
|
2015-01-28 17:33:10 +00:00
|
|
|
m_ssl->m_ssl = NULL;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
if (m_ssl->m_context != NULL) {
|
|
|
|
SSL_CTX_free(m_ssl->m_context);
|
2015-01-28 17:33:10 +00:00
|
|
|
m_ssl->m_context = NULL;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
2015-05-22 17:56:13 +00:00
|
|
|
ARCH->sleep(1);
|
2015-01-28 17:33:10 +00:00
|
|
|
delete m_ssl;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 15:40:30 +00:00
|
|
|
void
|
|
|
|
SecureSocket::close()
|
|
|
|
{
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
|
|
|
|
2015-01-29 15:40:30 +00:00
|
|
|
SSL_shutdown(m_ssl->m_ssl);
|
|
|
|
|
|
|
|
TCPSocket::close();
|
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
void
|
|
|
|
SecureSocket::secureConnect()
|
|
|
|
{
|
|
|
|
setJob(new TSocketMultiplexerMethodJob<SecureSocket>(
|
|
|
|
this, &SecureSocket::serviceConnect,
|
|
|
|
getSocket(), isReadable(), isWritable()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SecureSocket::secureAccept()
|
|
|
|
{
|
|
|
|
setJob(new TSocketMultiplexerMethodJob<SecureSocket>(
|
|
|
|
this, &SecureSocket::serviceAccept,
|
|
|
|
getSocket(), isReadable(), isWritable()));
|
|
|
|
}
|
|
|
|
|
2015-01-14 17:24:45 +00:00
|
|
|
UInt32
|
2015-01-26 13:23:11 +00:00
|
|
|
SecureSocket::secureRead(void* buffer, UInt32 n)
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
|
|
|
int r = 0;
|
2015-01-28 17:33:10 +00:00
|
|
|
if (m_ssl->m_ssl != NULL) {
|
2015-04-29 12:25:01 +00:00
|
|
|
LOG((CLOG_DEBUG2 "reading secure socket"));
|
2015-01-14 17:24:45 +00:00
|
|
|
r = SSL_read(m_ssl->m_ssl, buffer, n);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-05-20 19:08:25 +00:00
|
|
|
static int retry;
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
// Check result will cleanup the connection in the case of a fatal
|
|
|
|
checkResult(r, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-01-14 17:24:45 +00:00
|
|
|
if (retry) {
|
2015-05-22 17:56:13 +00:00
|
|
|
return 0;
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
if (isFatal()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// According to SSL spec, r must not be negative and not have an error code
|
|
|
|
// from SSL_get_error(). If this happens, it is itself an error. Let the
|
|
|
|
// parent handle the case
|
|
|
|
return (UInt32)r;
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
UInt32
|
|
|
|
SecureSocket::secureWrite(const void* buffer, UInt32 n)
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
|
|
|
int r = 0;
|
2015-01-28 17:33:10 +00:00
|
|
|
if (m_ssl->m_ssl != NULL) {
|
2015-05-22 17:56:13 +00:00
|
|
|
LOG((CLOG_DEBUG2 "writing secure socket:%p", this));
|
|
|
|
|
2015-01-14 17:24:45 +00:00
|
|
|
r = SSL_write(m_ssl->m_ssl, buffer, n);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-05-20 19:08:25 +00:00
|
|
|
static int retry;
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
// Check result will cleanup the connection in the case of a fatal
|
|
|
|
checkResult(r, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-01-14 17:24:45 +00:00
|
|
|
if (retry) {
|
2015-05-22 17:56:13 +00:00
|
|
|
return 0;
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
if (isFatal()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// According to SSL spec, r must not be negative and not have an error code
|
|
|
|
// from SSL_get_error(). If this happens, it is itself an error. Let the
|
|
|
|
// parent handle the case
|
|
|
|
return (UInt32)r;
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
bool
|
|
|
|
SecureSocket::isSecureReady()
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
2015-01-26 13:23:11 +00:00
|
|
|
return m_secureReady;
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SecureSocket::initSsl(bool server)
|
|
|
|
{
|
|
|
|
m_ssl = new Ssl();
|
|
|
|
m_ssl->m_context = NULL;
|
|
|
|
m_ssl->m_ssl = NULL;
|
|
|
|
|
|
|
|
initContext(server);
|
|
|
|
}
|
|
|
|
|
2015-04-10 13:07:53 +00:00
|
|
|
bool
|
2015-04-14 13:03:43 +00:00
|
|
|
SecureSocket::loadCertificates(String& filename)
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
2015-04-10 13:07:53 +00:00
|
|
|
if (filename.empty()) {
|
|
|
|
showError("ssl certificate is not specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::ifstream file(filename.c_str());
|
|
|
|
bool exist = file.good();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (!exist) {
|
2015-04-14 13:03:43 +00:00
|
|
|
String errorMsg("ssl certificate doesn't exist: ");
|
2015-04-10 13:07:53 +00:00
|
|
|
errorMsg.append(filename);
|
|
|
|
showError(errorMsg.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
int r = 0;
|
2015-04-10 13:07:53 +00:00
|
|
|
r = SSL_CTX_use_certificate_file(m_ssl->m_context, filename.c_str(), SSL_FILETYPE_PEM);
|
2015-01-26 13:23:11 +00:00
|
|
|
if (r <= 0) {
|
2015-04-10 13:07:53 +00:00
|
|
|
showError("could not use ssl certificate");
|
|
|
|
return false;
|
2015-01-26 13:23:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 13:07:53 +00:00
|
|
|
r = SSL_CTX_use_PrivateKey_file(m_ssl->m_context, filename.c_str(), SSL_FILETYPE_PEM);
|
2015-01-26 13:23:11 +00:00
|
|
|
if (r <= 0) {
|
2015-04-10 13:07:53 +00:00
|
|
|
showError("could not use ssl private key");
|
|
|
|
return false;
|
2015-01-26 13:23:11 +00:00
|
|
|
}
|
2015-01-14 17:24:45 +00:00
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
r = SSL_CTX_check_private_key(m_ssl->m_context);
|
|
|
|
if (!r) {
|
2015-04-10 13:07:53 +00:00
|
|
|
showError("could not verify ssl private key");
|
|
|
|
return false;
|
2015-01-26 13:23:11 +00:00
|
|
|
}
|
2015-04-10 13:07:53 +00:00
|
|
|
|
|
|
|
return true;
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
void
|
2015-01-09 13:46:35 +00:00
|
|
|
SecureSocket::initContext(bool server)
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
SSL_library_init();
|
|
|
|
|
2015-02-10 12:22:30 +00:00
|
|
|
const SSL_METHOD* method;
|
2015-01-06 14:20:05 +00:00
|
|
|
|
|
|
|
// load & register all cryptos, etc.
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
|
|
|
|
// load all error messages
|
|
|
|
SSL_load_error_strings();
|
|
|
|
|
2015-04-23 10:28:15 +00:00
|
|
|
// SSLv23_method uses TLSv1, with the ability to fall back to SSLv3
|
2015-01-06 14:20:05 +00:00
|
|
|
if (server) {
|
2015-01-26 13:23:11 +00:00
|
|
|
method = SSLv23_server_method();
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-01-26 13:23:11 +00:00
|
|
|
method = SSLv23_client_method();
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create new context from method
|
2015-02-10 12:22:30 +00:00
|
|
|
SSL_METHOD* m = const_cast<SSL_METHOD*>(method);
|
|
|
|
m_ssl->m_context = SSL_CTX_new(m);
|
2015-04-23 11:47:17 +00:00
|
|
|
|
|
|
|
// drop SSLv3 support
|
|
|
|
SSL_CTX_set_options(m_ssl->m_context, SSL_OP_NO_SSLv3);
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
if (m_ssl->m_context == NULL) {
|
2015-03-04 11:43:52 +00:00
|
|
|
showError();
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-09 13:46:35 +00:00
|
|
|
SecureSocket::createSSL()
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
// I assume just one instance is needed
|
|
|
|
// get new SSL state with context
|
|
|
|
if (m_ssl->m_ssl == NULL) {
|
|
|
|
m_ssl->m_ssl = SSL_new(m_ssl->m_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
int
|
2015-01-14 17:24:45 +00:00
|
|
|
SecureSocket::secureAccept(int socket)
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
createSSL();
|
|
|
|
|
|
|
|
// set connection socket to SSL state
|
|
|
|
SSL_set_fd(m_ssl->m_ssl, socket);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-03-04 13:59:53 +00:00
|
|
|
LOG((CLOG_DEBUG2 "accepting secure socket"));
|
2015-01-06 14:20:05 +00:00
|
|
|
int r = SSL_accept(m_ssl->m_ssl);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-05-20 19:08:25 +00:00
|
|
|
static int retry;
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
checkResult(r, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
if (isFatal()) {
|
2015-03-04 13:42:19 +00:00
|
|
|
// tell user and sleep so the socket isn't hammered.
|
|
|
|
LOG((CLOG_ERR "failed to accept secure socket"));
|
|
|
|
LOG((CLOG_INFO "client connection may not be secure"));
|
2015-05-21 22:22:39 +00:00
|
|
|
m_secureReady = false;
|
2015-03-04 13:42:19 +00:00
|
|
|
ARCH->sleep(1);
|
2015-05-21 22:22:39 +00:00
|
|
|
return -1; // Failed, error out
|
2015-03-04 13:42:19 +00:00
|
|
|
}
|
2015-01-26 13:23:11 +00:00
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
// If not fatal and no retry, state is good
|
2015-05-20 21:14:30 +00:00
|
|
|
if (retry == 0) {
|
|
|
|
m_secureReady = true;
|
2015-05-21 22:22:39 +00:00
|
|
|
LOG((CLOG_INFO "accepted secure socket"));
|
|
|
|
return 1;
|
2015-05-20 21:14:30 +00:00
|
|
|
}
|
2015-05-20 19:08:25 +00:00
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
// If not fatal and retry is set, not ready, and return retry
|
|
|
|
if (retry > 0) {
|
|
|
|
LOG((CLOG_DEBUG2 "retry accepting secure socket"));
|
|
|
|
m_secureReady = false;
|
|
|
|
return 0;
|
2015-03-04 13:59:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
// no good state exists here
|
|
|
|
LOG((CLOG_ERR "unexpected state attempting to accept connection"));
|
|
|
|
return -1;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
int
|
2015-01-14 17:24:45 +00:00
|
|
|
SecureSocket::secureConnect(int socket)
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
createSSL();
|
|
|
|
|
|
|
|
// attach the socket descriptor
|
|
|
|
SSL_set_fd(m_ssl->m_ssl, socket);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-03-04 13:59:53 +00:00
|
|
|
LOG((CLOG_DEBUG2 "connecting secure socket"));
|
2015-01-27 11:04:23 +00:00
|
|
|
int r = SSL_connect(m_ssl->m_ssl);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-05-20 19:08:25 +00:00
|
|
|
static int retry;
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
checkResult(r, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
if (isFatal()) {
|
2015-03-04 13:42:19 +00:00
|
|
|
LOG((CLOG_ERR "failed to connect secure socket"));
|
2015-05-21 22:22:39 +00:00
|
|
|
return -1;
|
2015-03-04 13:42:19 +00:00
|
|
|
}
|
2015-01-26 13:23:11 +00:00
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
// If we should retry, not ready and return 0
|
|
|
|
if (retry > 0) {
|
|
|
|
LOG((CLOG_DEBUG2 "retry connect secure socket"));
|
2015-05-20 21:14:30 +00:00
|
|
|
m_secureReady = false;
|
2015-05-21 22:22:39 +00:00
|
|
|
return 0;
|
2015-05-20 21:14:30 +00:00
|
|
|
}
|
2015-01-06 14:20:05 +00:00
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
// No error, set ready, process and return ok
|
|
|
|
m_secureReady = true;
|
|
|
|
if (verifyCertFingerprint()) {
|
|
|
|
LOG((CLOG_INFO "connected to secure socket"));
|
|
|
|
if (!showCertificate()) {
|
2015-04-02 10:56:51 +00:00
|
|
|
disconnect();
|
2015-05-21 22:22:39 +00:00
|
|
|
return -1;// Cert fail, error
|
2015-04-02 10:56:51 +00:00
|
|
|
}
|
2015-01-29 15:40:30 +00:00
|
|
|
}
|
2015-05-21 22:22:39 +00:00
|
|
|
else {
|
|
|
|
LOG((CLOG_ERR "failed to verify server certificate fingerprint"));
|
|
|
|
disconnect();
|
|
|
|
return -1; // Fingerprint failed, error
|
|
|
|
}
|
|
|
|
LOG((CLOG_DEBUG2 "connected secure socket"));
|
|
|
|
return 1;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 13:07:53 +00:00
|
|
|
bool
|
2015-04-14 13:03:43 +00:00
|
|
|
SecureSocket::showCertificate()
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
X509* cert;
|
|
|
|
char* line;
|
|
|
|
|
|
|
|
// get the server's certificate
|
|
|
|
cert = SSL_get_peer_certificate(m_ssl->m_ssl);
|
|
|
|
if (cert != NULL) {
|
|
|
|
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
|
2015-03-04 12:15:56 +00:00
|
|
|
LOG((CLOG_INFO "server ssl certificate info: %s", line));
|
2015-01-06 14:20:05 +00:00
|
|
|
OPENSSL_free(line);
|
|
|
|
X509_free(cert);
|
|
|
|
}
|
|
|
|
else {
|
2015-04-10 13:07:53 +00:00
|
|
|
showError("server has no ssl certificate");
|
|
|
|
return false;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
2015-04-10 13:07:53 +00:00
|
|
|
|
|
|
|
return true;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 13:42:19 +00:00
|
|
|
void
|
2015-05-22 17:56:13 +00:00
|
|
|
SecureSocket::checkResult(int n, int& retry)
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
2015-03-04 13:59:53 +00:00
|
|
|
// ssl errors are a little quirky. the "want" errors are normal and
|
|
|
|
// should result in a retry.
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
int errorCode = SSL_get_error(m_ssl->m_ssl, n);
|
|
|
|
switch (errorCode) {
|
|
|
|
case SSL_ERROR_NONE:
|
2015-05-20 19:08:25 +00:00
|
|
|
retry = 0;
|
2015-03-04 12:26:54 +00:00
|
|
|
// operation completed
|
2015-03-04 11:43:52 +00:00
|
|
|
break;
|
2015-01-29 15:40:30 +00:00
|
|
|
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2015-03-04 13:59:53 +00:00
|
|
|
// connection closed
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
|
|
|
LOG((CLOG_DEBUG "SSL connection has been closed"));
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
|
|
|
case SSL_ERROR_WANT_ACCEPT:
|
2015-05-20 19:08:25 +00:00
|
|
|
retry += 1;
|
2015-05-20 23:54:42 +00:00
|
|
|
// If there are a lot of retrys, it's worth warning about
|
|
|
|
if ( retry % 5 == 0 ) {
|
2015-05-22 17:56:13 +00:00
|
|
|
LOG((CLOG_DEBUG "need to retry the same SSL function(%d) retry:%d", errorCode, retry));
|
2015-05-20 23:54:42 +00:00
|
|
|
}
|
|
|
|
else if ( retry == (maxRetry() / 2) ) {
|
|
|
|
LOG((CLOG_WARN "need to retry the same SSL function(%d) retry:%d", errorCode, retry));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG((CLOG_DEBUG2 "need to retry the same SSL function(%d) retry:%d", errorCode, retry));
|
|
|
|
}
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2015-04-29 12:25:01 +00:00
|
|
|
LOG((CLOG_ERR "some secure socket I/O error occurred"));
|
2015-04-17 13:01:35 +00:00
|
|
|
if (ERR_peek_error() == 0) {
|
|
|
|
if (n == 0) {
|
|
|
|
LOG((CLOG_ERR "an EOF violates the protocol"));
|
|
|
|
}
|
|
|
|
else if (n == -1) {
|
|
|
|
// underlying socket I/O reproted an error
|
|
|
|
try {
|
|
|
|
ARCH->throwErrorOnSocket(getSocket());
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
LOG((CLOG_ERR "%s", e.what()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
2015-03-04 13:42:19 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
case SSL_ERROR_SSL:
|
2015-04-29 12:25:01 +00:00
|
|
|
LOG((CLOG_ERR "a failure in the SSL library occurred"));
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-04-29 12:25:01 +00:00
|
|
|
LOG((CLOG_ERR "unknown secure socket error"));
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
2015-03-04 13:59:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:27:25 +00:00
|
|
|
// If the retry max would exceed the allowed, treat it as a fatal error
|
|
|
|
if (retry > maxRetry()) {
|
|
|
|
LOG((CLOG_ERR "Maximum retry count exceeded:%d",retry));
|
2015-05-22 17:56:13 +00:00
|
|
|
isFatal(true);
|
2015-05-20 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:56:13 +00:00
|
|
|
if (isFatal()) {
|
2015-05-20 19:08:25 +00:00
|
|
|
retry = 0;
|
2015-03-04 13:59:53 +00:00
|
|
|
showError();
|
2015-04-02 10:56:51 +00:00
|
|
|
disconnect();
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
2015-03-04 11:43:52 +00:00
|
|
|
}
|
2015-03-03 19:21:14 +00:00
|
|
|
|
2015-03-04 11:43:52 +00:00
|
|
|
void
|
2015-04-14 13:03:43 +00:00
|
|
|
SecureSocket::showError(const char* reason)
|
2015-03-04 11:43:52 +00:00
|
|
|
{
|
2015-04-10 13:07:53 +00:00
|
|
|
if (reason != NULL) {
|
|
|
|
LOG((CLOG_ERR "%s", reason));
|
2015-03-04 13:42:19 +00:00
|
|
|
}
|
2015-01-06 14:20:05 +00:00
|
|
|
|
2015-04-14 13:03:43 +00:00
|
|
|
String error = getError();
|
2015-03-04 13:59:53 +00:00
|
|
|
if (!error.empty()) {
|
2015-04-10 13:07:53 +00:00
|
|
|
LOG((CLOG_ERR "%s", error.c_str()));
|
2015-03-04 13:59:53 +00:00
|
|
|
}
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 12:15:56 +00:00
|
|
|
String
|
2015-01-09 13:46:35 +00:00
|
|
|
SecureSocket::getError()
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
|
|
|
unsigned long e = ERR_get_error();
|
2015-03-04 11:43:52 +00:00
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
if (e != 0) {
|
2015-03-04 12:15:56 +00:00
|
|
|
char error[MAX_ERROR_SIZE];
|
|
|
|
ERR_error_string_n(e, error, MAX_ERROR_SIZE);
|
|
|
|
return error;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-03-04 13:59:53 +00:00
|
|
|
return "";
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-26 13:23:11 +00:00
|
|
|
|
2015-04-02 10:56:51 +00:00
|
|
|
void
|
2015-04-14 12:44:10 +00:00
|
|
|
SecureSocket::disconnect()
|
2015-01-26 13:23:11 +00:00
|
|
|
{
|
2015-04-21 15:54:49 +00:00
|
|
|
sendEvent(getEvents()->forISocket().stopRetry());
|
2015-04-02 10:56:51 +00:00
|
|
|
sendEvent(getEvents()->forISocket().disconnected());
|
|
|
|
sendEvent(getEvents()->forIStream().inputShutdown());
|
2015-01-26 13:23:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 12:06:49 +00:00
|
|
|
void
|
|
|
|
SecureSocket::formatFingerprint(String& fingerprint, bool hex, bool separator)
|
|
|
|
{
|
|
|
|
if (hex) {
|
|
|
|
// to hexidecimal
|
|
|
|
synergy::string::toHex(fingerprint, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// all uppercase
|
|
|
|
synergy::string::uppercase(fingerprint);
|
|
|
|
|
|
|
|
if (separator) {
|
|
|
|
// add colon to separate each 2 charactors
|
|
|
|
size_t separators = fingerprint.size() / 2;
|
|
|
|
for (size_t i = 1; i < separators; i++) {
|
|
|
|
fingerprint.insert(i * 3 - 1, ":");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:56:51 +00:00
|
|
|
bool
|
2015-04-14 12:44:10 +00:00
|
|
|
SecureSocket::verifyCertFingerprint()
|
2015-04-01 15:04:10 +00:00
|
|
|
{
|
2015-04-02 10:56:51 +00:00
|
|
|
// calculate received certificate fingerprint
|
|
|
|
X509 *cert = cert = SSL_get_peer_certificate(m_ssl->m_ssl);
|
2015-04-01 15:04:10 +00:00
|
|
|
EVP_MD* tempDigest;
|
|
|
|
unsigned char tempFingerprint[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int tempFingerprintLen;
|
|
|
|
tempDigest = (EVP_MD*)EVP_sha1();
|
2015-04-15 13:16:28 +00:00
|
|
|
int digestResult = X509_digest(cert, tempDigest, tempFingerprint, &tempFingerprintLen);
|
|
|
|
|
|
|
|
if (digestResult <= 0) {
|
|
|
|
LOG((CLOG_ERR "failed to calculate fingerprint, digest result: %d", digestResult));
|
2015-04-02 10:56:51 +00:00
|
|
|
return false;
|
2015-04-01 15:04:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 12:06:49 +00:00
|
|
|
// format fingerprint into hexdecimal format with colon separator
|
2015-04-14 12:44:10 +00:00
|
|
|
String fingerprint(reinterpret_cast<char*>(tempFingerprint), tempFingerprintLen);
|
2015-04-15 12:06:49 +00:00
|
|
|
formatFingerprint(fingerprint);
|
2015-04-15 12:25:18 +00:00
|
|
|
LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str()));
|
2015-04-01 15:04:10 +00:00
|
|
|
|
2015-04-15 13:16:28 +00:00
|
|
|
String trustedServersFilename;
|
|
|
|
trustedServersFilename = synergy::string::sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
ARCH->getProfileDirectory().c_str(),
|
|
|
|
kFingerprintDirName,
|
|
|
|
kFingerprintTrustedServersFilename);
|
|
|
|
|
2015-04-02 10:56:51 +00:00
|
|
|
// check if this fingerprint exist
|
2015-04-14 12:44:10 +00:00
|
|
|
String fileLine;
|
2015-04-01 15:04:10 +00:00
|
|
|
std::ifstream file;
|
2015-04-15 13:16:28 +00:00
|
|
|
file.open(trustedServersFilename.c_str());
|
2015-04-01 15:04:10 +00:00
|
|
|
|
2015-04-15 12:25:18 +00:00
|
|
|
bool isValid = false;
|
2015-04-21 15:46:05 +00:00
|
|
|
while (!file.eof() && file.is_open()) {
|
2015-04-01 15:04:10 +00:00
|
|
|
getline(file,fileLine);
|
2015-04-15 12:06:49 +00:00
|
|
|
if (!fileLine.empty()) {
|
|
|
|
if (fileLine.compare(fingerprint) == 0) {
|
2015-04-15 12:25:18 +00:00
|
|
|
isValid = true;
|
|
|
|
break;
|
2015-04-01 15:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
2015-04-15 12:25:18 +00:00
|
|
|
return isValid;
|
2015-04-02 10:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
2015-04-14 12:44:10 +00:00
|
|
|
SecureSocket::serviceConnect(ISocketMultiplexerJob* job,
|
2015-04-02 10:56:51 +00:00
|
|
|
bool, bool write, bool error)
|
|
|
|
{
|
2015-04-14 12:44:10 +00:00
|
|
|
Lock lock(&getMutex());
|
2015-04-02 10:56:51 +00:00
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
int status = 0;
|
2015-04-02 10:56:51 +00:00
|
|
|
#ifdef SYSAPI_WIN32
|
2015-05-21 22:22:39 +00:00
|
|
|
status = secureConnect(static_cast<int>(getSocket()->m_socket));
|
2015-04-02 10:56:51 +00:00
|
|
|
#elif SYSAPI_UNIX
|
|
|
|
retry = secureConnect(getSocket()->m_fd);
|
|
|
|
#endif
|
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
if (status > 0) {
|
|
|
|
return newJob();
|
|
|
|
}
|
|
|
|
else if (status == 0) {
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
// If status < 0, error happened
|
|
|
|
return NULL;
|
2015-04-02 10:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
2015-04-14 12:44:10 +00:00
|
|
|
SecureSocket::serviceAccept(ISocketMultiplexerJob* job,
|
2015-04-02 10:56:51 +00:00
|
|
|
bool, bool write, bool error)
|
|
|
|
{
|
2015-04-14 12:44:10 +00:00
|
|
|
Lock lock(&getMutex());
|
2015-04-02 10:56:51 +00:00
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
int status = 0;
|
2015-04-02 10:56:51 +00:00
|
|
|
#ifdef SYSAPI_WIN32
|
2015-05-21 22:22:39 +00:00
|
|
|
status = secureAccept(static_cast<int>(getSocket()->m_socket));
|
2015-04-02 10:56:51 +00:00
|
|
|
#elif SYSAPI_UNIX
|
2015-05-21 22:22:39 +00:00
|
|
|
status = secureAccept(getSocket()->m_fd);
|
2015-04-02 10:56:51 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-21 22:22:39 +00:00
|
|
|
if (status > 0) {
|
|
|
|
return newJob();
|
|
|
|
}
|
|
|
|
else if (status == 0) {
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
// If status < 0, error happened
|
|
|
|
return NULL;
|
2015-04-01 15:04:10 +00:00
|
|
|
}
|