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
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* 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 <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <fstream>
|
2015-04-01 15:32:04 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
//
|
|
|
|
// SecureSocket
|
|
|
|
//
|
2015-01-14 17:24:45 +00:00
|
|
|
|
|
|
|
#define MAX_ERROR_SIZE 65535
|
|
|
|
|
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-01 15:35:03 +00:00
|
|
|
int verifyCertFingerprint(X509_STORE_CTX* ctx, void* arg);
|
|
|
|
|
2015-04-01 15:04:10 +00:00
|
|
|
bool CSecureSocket::s_verifyFingerprintFailed = false;
|
|
|
|
|
|
|
|
CSecureSocket::CSecureSocket(
|
2015-01-14 17:24:45 +00:00
|
|
|
IEventQueue* events,
|
2015-04-01 15:04:10 +00:00
|
|
|
CSocketMultiplexer* socketMultiplexer) :
|
|
|
|
CTCPSocket(events, socketMultiplexer),
|
|
|
|
m_secureReady(false),
|
|
|
|
m_certFingerprint()
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SecureSocket::SecureSocket(
|
|
|
|
IEventQueue* events,
|
|
|
|
SocketMultiplexer* socketMultiplexer,
|
|
|
|
ArchSocket socket) :
|
|
|
|
TCPSocket(events, socketMultiplexer, socket),
|
2015-01-26 13:23:11 +00:00
|
|
|
m_secureReady(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
|
|
|
{
|
|
|
|
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-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()
|
|
|
|
{
|
|
|
|
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-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-03-04 13:59:53 +00:00
|
|
|
bool fatal, retry;
|
|
|
|
checkResult(r, fatal, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-01-14 17:24:45 +00:00
|
|
|
if (retry) {
|
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r > 0 ? (UInt32)r : 0;
|
|
|
|
}
|
|
|
|
|
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-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-03-04 13:59:53 +00:00
|
|
|
bool fatal, retry;
|
|
|
|
checkResult(r, fatal, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-01-14 17:24:45 +00:00
|
|
|
if (retry) {
|
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
return r > 0 ? (UInt32)r : 0;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-26 13:23:11 +00:00
|
|
|
SecureSocket::loadCertificates(const char* filename)
|
2015-01-14 17:24:45 +00:00
|
|
|
{
|
2015-01-26 13:23:11 +00:00
|
|
|
int r = 0;
|
|
|
|
r = SSL_CTX_use_certificate_file(m_ssl->m_context, filename, SSL_FILETYPE_PEM);
|
|
|
|
if (r <= 0) {
|
2015-03-04 12:15:56 +00:00
|
|
|
throwError("could not use ssl certificate");
|
2015-01-26 13:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = SSL_CTX_use_PrivateKey_file(m_ssl->m_context, filename, SSL_FILETYPE_PEM);
|
|
|
|
if (r <= 0) {
|
2015-03-04 12:15:56 +00:00
|
|
|
throwError("could not use ssl private key");
|
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-03-04 12:15:56 +00:00
|
|
|
throwError("could not verify ssl private key");
|
2015-01-26 13:23:11 +00:00
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
if (server) {
|
|
|
|
// create new server-method instance
|
2015-01-26 13:23:11 +00:00
|
|
|
method = SSLv23_server_method();
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// create new client-method instance
|
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-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
|
|
|
}
|
2015-04-01 15:04:10 +00:00
|
|
|
|
|
|
|
if (!server) {
|
2015-04-01 15:39:40 +00:00
|
|
|
void* p = reinterpret_cast<void*>(const_cast<char*>(m_certFingerprint.c_str()));
|
|
|
|
SSL_CTX_set_cert_verify_callback(
|
|
|
|
m_ssl->m_context,
|
|
|
|
verifyCertFingerprint,
|
|
|
|
p);
|
2015-04-01 15:04:10 +00:00
|
|
|
}
|
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-01-26 13:23:11 +00:00
|
|
|
bool
|
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-03-04 13:59:53 +00:00
|
|
|
bool fatal, retry;
|
|
|
|
checkResult(r, fatal, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-03-04 13:59:53 +00:00
|
|
|
if (fatal) {
|
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"));
|
|
|
|
ARCH->sleep(1);
|
|
|
|
}
|
2015-01-26 13:23:11 +00:00
|
|
|
|
2015-03-04 11:43:52 +00:00
|
|
|
m_secureReady = !retry;
|
2015-03-04 13:59:53 +00:00
|
|
|
if (m_secureReady) {
|
|
|
|
LOG((CLOG_INFO "accepted secure socket"));
|
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
return retry;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
bool
|
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-03-04 13:59:53 +00:00
|
|
|
bool fatal, retry;
|
|
|
|
checkResult(r, fatal, retry);
|
2015-03-04 13:42:19 +00:00
|
|
|
|
2015-03-04 13:59:53 +00:00
|
|
|
if (fatal) {
|
2015-03-04 13:42:19 +00:00
|
|
|
// tell user and sleep so the socket isn't hammered.
|
|
|
|
LOG((CLOG_ERR "failed to connect secure socket"));
|
|
|
|
LOG((CLOG_INFO "server connection may not be secure"));
|
|
|
|
ARCH->sleep(1);
|
|
|
|
}
|
2015-01-26 13:23:11 +00:00
|
|
|
|
2015-01-29 15:40:30 +00:00
|
|
|
m_secureReady = !retry;
|
2015-01-06 14:20:05 +00:00
|
|
|
|
2015-04-01 15:04:10 +00:00
|
|
|
if (s_verifyFingerprintFailed) {
|
|
|
|
throwError("failed to verify server certificate fingerprint");
|
|
|
|
}
|
|
|
|
|
2015-01-29 15:40:30 +00:00
|
|
|
if (m_secureReady) {
|
2015-03-04 13:59:53 +00:00
|
|
|
LOG((CLOG_INFO "connected to secure socket"));
|
2015-01-29 15:40:30 +00:00
|
|
|
showCertificate();
|
|
|
|
}
|
2015-01-26 13:23:11 +00:00
|
|
|
|
|
|
|
return retry;
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-09 13:46:35 +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-03-04 12:15:56 +00:00
|
|
|
throwError("server has no ssl certificate");
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 13:42:19 +00:00
|
|
|
void
|
2015-03-04 13:59:53 +00:00
|
|
|
SecureSocket::checkResult(int n, bool& fatal, bool& 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.
|
|
|
|
|
|
|
|
fatal = false;
|
2015-03-04 13:42:19 +00:00
|
|
|
retry = false;
|
|
|
|
|
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-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-03-04 12:15:56 +00:00
|
|
|
LOG((CLOG_DEBUG2 "secure socket error: SSL_ERROR_ZERO_RETURN"));
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2015-03-04 11:43:52 +00:00
|
|
|
LOG((CLOG_DEBUG2 "secure socket error: SSL_ERROR_WANT_READ"));
|
|
|
|
retry = true;
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2015-03-04 11:43:52 +00:00
|
|
|
LOG((CLOG_DEBUG2 "secure socket error: SSL_ERROR_WANT_WRITE"));
|
|
|
|
retry = true;
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
2015-03-04 11:43:52 +00:00
|
|
|
LOG((CLOG_DEBUG2 "secure socket error: SSL_ERROR_WANT_CONNECT"));
|
|
|
|
retry = true;
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_ACCEPT:
|
2015-03-04 11:43:52 +00:00
|
|
|
LOG((CLOG_DEBUG2 "secure socket error: SSL_ERROR_WANT_ACCEPT"));
|
|
|
|
retry = true;
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2015-03-04 13:42:19 +00:00
|
|
|
LOG((CLOG_ERR "secure socket error: SSL_ERROR_SYSCALL"));
|
2015-03-04 13:59:53 +00:00
|
|
|
fatal = true;
|
2015-03-04 13:42:19 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-06 14:20:05 +00:00
|
|
|
case SSL_ERROR_SSL:
|
2015-03-04 13:42:19 +00:00
|
|
|
LOG((CLOG_ERR "secure socket error: SSL_ERROR_SSL"));
|
2015-03-04 13:59:53 +00:00
|
|
|
fatal = true;
|
2015-01-06 14:20:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-03-04 13:42:19 +00:00
|
|
|
LOG((CLOG_ERR "secure socket error: SSL_ERROR_SSL"));
|
2015-03-04 13:59:53 +00:00
|
|
|
fatal = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fatal) {
|
|
|
|
showError();
|
2015-03-04 13:42:19 +00:00
|
|
|
sendEvent(getEvents()->forISocket().disconnected());
|
|
|
|
sendEvent(getEvents()->forIStream().inputShutdown());
|
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
|
|
|
|
SecureSocket::showError()
|
|
|
|
{
|
2015-03-04 13:42:19 +00:00
|
|
|
String error = getError();
|
|
|
|
if (!error.empty()) {
|
|
|
|
LOG((CLOG_ERR "secure socket error: %s", error.c_str()));
|
|
|
|
}
|
2015-01-06 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-09 13:46:35 +00:00
|
|
|
SecureSocket::throwError(const char* reason)
|
2015-01-06 14:20:05 +00:00
|
|
|
{
|
2015-03-04 13:59:53 +00:00
|
|
|
String error = getError();
|
|
|
|
if (!error.empty()) {
|
|
|
|
throw XSocket(synergy::string::sprintf(
|
2015-04-01 15:04:10 +00:00
|
|
|
"%s: %s", reason, error.c_str()));
|
2015-03-04 13:59:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw XSocket(reason);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
SecureSocket::serviceConnect(ISocketMultiplexerJob* job,
|
|
|
|
bool, bool write, bool error)
|
|
|
|
{
|
|
|
|
Lock lock(&getMutex());
|
|
|
|
|
|
|
|
bool retry = true;
|
|
|
|
#ifdef SYSAPI_WIN32
|
|
|
|
retry = secureConnect(static_cast<int>(getSocket()->m_socket));
|
|
|
|
#elif SYSAPI_UNIX
|
|
|
|
retry = secureConnect(getSocket()->m_fd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return retry ? job : newJob();
|
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
SecureSocket::serviceAccept(ISocketMultiplexerJob* job,
|
|
|
|
bool, bool write, bool error)
|
|
|
|
{
|
|
|
|
Lock lock(&getMutex());
|
|
|
|
|
|
|
|
bool retry = true;
|
|
|
|
#ifdef SYSAPI_WIN32
|
|
|
|
retry = secureAccept(static_cast<int>(getSocket()->m_socket));
|
|
|
|
#elif SYSAPI_UNIX
|
|
|
|
retry = secureAccept(getSocket()->m_fd);
|
|
|
|
#endif
|
2015-01-29 15:40:30 +00:00
|
|
|
|
2015-01-26 13:23:11 +00:00
|
|
|
return retry ? job : newJob();
|
|
|
|
}
|
2015-04-01 15:04:10 +00:00
|
|
|
|
|
|
|
int
|
2015-04-01 15:32:04 +00:00
|
|
|
verifyCertFingerprint(X509_STORE_CTX* ctx, void* arg)
|
2015-04-01 15:04:10 +00:00
|
|
|
{
|
|
|
|
X509 *cert = ctx->cert;
|
|
|
|
|
|
|
|
EVP_MD* tempDigest;
|
|
|
|
unsigned char tempFingerprint[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int tempFingerprintLen;
|
|
|
|
tempDigest = (EVP_MD*)EVP_sha1();
|
|
|
|
if (X509_digest(cert, tempDigest, tempFingerprint, &tempFingerprintLen) <= 0) {
|
2015-04-01 15:32:04 +00:00
|
|
|
CSecureSocket::s_verifyFingerprintFailed = true;
|
2015-04-01 15:04:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex;
|
2015-04-01 15:32:04 +00:00
|
|
|
for (unsigned int i = 0; i < tempFingerprintLen; i++) {
|
2015-04-01 15:04:10 +00:00
|
|
|
ss << std::setw(2) << std::setfill('0') << (int)tempFingerprint[i];
|
|
|
|
}
|
|
|
|
CString fingerprint = ss.str();
|
|
|
|
std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(), ::toupper);
|
|
|
|
|
|
|
|
CString fileLine;
|
|
|
|
CString certificateFingerprint;
|
|
|
|
char* certFingerprintFilename = reinterpret_cast<char*>(arg);
|
|
|
|
std::ifstream file;
|
|
|
|
file.open(certFingerprintFilename);
|
|
|
|
|
|
|
|
while (!file.eof()) {
|
|
|
|
getline(file,fileLine);
|
|
|
|
size_t found = fileLine.find('=');
|
|
|
|
if (found != CString::npos) {
|
|
|
|
certificateFingerprint = fileLine.substr(found + 1);
|
|
|
|
|
|
|
|
if (!certificateFingerprint.empty()) {
|
|
|
|
certificateFingerprint.erase(std::remove(certificateFingerprint.begin(), certificateFingerprint.end(), ':'), certificateFingerprint.end());
|
|
|
|
|
|
|
|
if(certificateFingerprint.compare(fingerprint) == 0) {
|
|
|
|
file.close();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
2015-04-01 15:32:04 +00:00
|
|
|
CSecureSocket::s_verifyFingerprintFailed = true;
|
2015-04-01 15:04:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|