38 pthread_mutex_t mutex;
42 static HANDLE *mutex_buf;
44 static pthread_mutex_t *mutex_buf;
46 static void dyn_lock_function(
int mode,
struct CRYPTO_dynlock_value *l,
const char *file,
int line);
49 static void locking_function(
int mode,
int n,
const char *file,
int line);
50 static unsigned long id_function(
void);
62 priv_key_file = kFile;
68 SSL_CTX *SSLServer::get_ctx(
void)
78 SSL_load_error_strings();
81 OpenSSL_add_all_algorithms();
84 mutex_buf = (HANDLE *) malloc(CRYPTO_num_locks() *
sizeof(HANDLE));
86 mutex_buf = (pthread_mutex_t *) malloc(CRYPTO_num_locks() *
sizeof(pthread_mutex_t));
88 if (mutex_buf == NULL)
90 for (i = 0; i < CRYPTO_num_locks(); i++)
93 mutex_buf[i] = CreateMutex( NULL, FALSE, NULL );
95 pthread_mutex_init(&mutex_buf[i], NULL);
99 CRYPTO_set_locking_callback(locking_function);
100 CRYPTO_set_id_callback(id_function);
102 CRYPTO_set_dynlock_create_callback(dyn_create_function);
103 CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
104 CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
106 RAND_load_file(
"/dev/urandom", 1024);
113 int SSLServer::tls_cleanup(
void)
117 if (mutex_buf == NULL)
119 CRYPTO_set_dynlock_create_callback(NULL);
120 CRYPTO_set_dynlock_lock_callback(NULL);
121 CRYPTO_set_dynlock_destroy_callback(NULL);
123 CRYPTO_set_locking_callback(NULL);
124 CRYPTO_set_id_callback(NULL);
126 for (i = 0; i < CRYPTO_num_locks(); i++)
129 CloseHandle(mutex_buf[i]);
131 pthread_mutex_destroy(&mutex_buf[i]);
140 int SSLServer::set_priv_key(
string priv_key)
142 priv_key_file = priv_key;
147 int SSLServer::set_cert_file(
string server_cert_file)
149 cert_file = server_cert_file;
154 int SSLServer::set_verify_client(
bool status_flag)
156 verify_client = status_flag;
161 int SSLServer::set_ca_cert(
string ca_cert)
163 ca_certificate = ca_cert;
173 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L) // openssl returns a const SSL_METHOD
174 const SSL_METHOD *method = NULL;
176 SSL_METHOD *method = NULL;
178 STACK_OF(X509_NAME) *cert_names;
180 method = SSLv23_server_method();
184 ctx = SSL_CTX_new(method);
187 SSL_CTX_set_mode(ctx,SSL_MODE_AUTO_RETRY);
188 cert_names = SSL_load_client_CA_file(ca_certificate.c_str());
189 if (cert_names != NULL)
190 SSL_CTX_set_client_CA_list(ctx, cert_names);
193 if(!SSL_CTX_load_verify_locations(ctx,ca_certificate.c_str(),NULL))
195 SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,0);
203 if ( SSL_CTX_use_certificate_chain_file(ctx, cert_file.c_str()) <= 0)
205 if ( SSL_CTX_use_PrivateKey_file(ctx, priv_key_file.c_str(), SSL_FILETYPE_PEM) <= 0)
208 if ( !SSL_CTX_check_private_key(ctx) )
222 static void locking_function(
int mode,
int n,
const char *file,
int line)
224 if (mode & CRYPTO_LOCK)
228 dwWaitResult = WaitForSingleObject(mutex_buf[n],INFINITE);
230 pthread_mutex_lock(&mutex_buf[n]);
236 ReleaseMutex(mutex_buf[n]);
238 pthread_mutex_unlock(&mutex_buf[n]);
249 static unsigned long id_function(
void)
252 return(
unsigned long) GetCurrentThreadId();
255 return ((
unsigned long) pthread_self());
269 value->mutex = CreateMutex( NULL, FALSE, NULL );
271 pthread_mutex_init(&value->mutex, NULL);
277 static void dyn_lock_function(
int mode,
struct CRYPTO_dynlock_value *l,
const char *file,
int line)
279 if (mode & CRYPTO_LOCK)
283 dwWaitResult = WaitForSingleObject(l->mutex,INFINITE);
285 pthread_mutex_lock(&l->mutex);
291 ReleaseMutex(l->mutex);
293 pthread_mutex_unlock(&l->mutex);
302 CloseHandle(l->mutex);
304 pthread_mutex_destroy(&l->mutex);
static generic_server * pinstance
Pointer to singleton framework instance.
Singleton class to manage framework state and provide utility functions. This class stores master Vec...
This is a singleton class and provides framework functionality.
This is the base class for all plugins. All plug-ins should derive from this class. This class has a bunch of virtual functions that all plug-in could/should implement. In addition to virtual functions, this class also provides a lot of utility functions for all plug-ins.
int CreateCTX(void)
Load algorithms and create context.
Struct to store OS agnostic mutex. Required for Openssl locking functions.
int LoadCerts(void)
Load certification files.
Master program that initiates GENERIC_SERVER, spawns a thread for each plug-in and runs message loop ...
Utility class for TLS. Please refer excellent documentation at: http://www.openssl.org/docs/ssl/ssl.html.