//
// SSLInitializer.cpp
//
// $Id: //poco/1.3/NetSSL_OpenSSL/src/SSLInitializer.cpp#2 $
//
// Library: NetSSL_OpenSSL
// Package: SSLCore
// Module:  SSLInitializer
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#include "Poco/Net/SSLInitializer.h"
#include "Poco/Net/KeyConsoleHandler.h"
#include "Poco/Net/KeyFileHandler.h"
#include "Poco/RandomStream.h"
#include "Poco/Thread.h"
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>


using Poco::RandomInputStream;
using Poco::Thread;
using Poco::FastMutex;


namespace Poco {
namespace Net {


FastMutex* SSLInitializer::_mutexes(0);
int SSLInitializer::_rc(0);


static SSLInitializer initializer;


SSLInitializer::SSLInitializer()
{
	initialize();
}


SSLInitializer::~SSLInitializer()
{
	uninitialize();
}


void SSLInitializer::initialize()
{
	if (++_rc == 1)
	{
		poco_assert (1 == SSL_library_init()); // always returns 1
		SSL_load_error_strings();
		
		char seed[SEEDSIZE];
		RandomInputStream rnd;
		rnd.read(seed, sizeof(seed));
		RAND_seed(seed, SEEDSIZE);
		
		int nMutexes = CRYPTO_num_locks();
		_mutexes = new FastMutex[nMutexes];
		CRYPTO_set_locking_callback(&SSLInitializer::lock);
#ifndef POCO_OS_FAMILY_WINDOWS // SF# 1828231: random unhandled exceptions when linking with ssl
		CRYPTO_set_id_callback(&SSLInitializer::id);
#endif
		CRYPTO_set_dynlock_create_callback(&SSLInitializer::dynlockCreate);
		CRYPTO_set_dynlock_lock_callback(&SSLInitializer::dynlock);
		CRYPTO_set_dynlock_destroy_callback(&SSLInitializer::dynlockDestroy);
	}
}


void SSLInitializer::uninitialize()
{
	if (--_rc == 0)
	{
		delete [] _mutexes;
	}
}


void SSLInitializer::lock(int mode, int n, const char* file, int line)
{
	if (mode & CRYPTO_LOCK)
		_mutexes[n].lock();
	else
		_mutexes[n].unlock();
}


unsigned long SSLInitializer::id()
{
	Thread* pThread = Thread::current();
	return pThread ? pThread->id() : 0;
}


struct CRYPTO_dynlock_value* SSLInitializer::dynlockCreate(const char* file, int line)
{
	return new CRYPTO_dynlock_value;
}


void SSLInitializer::dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line)
{
	poco_check_ptr (lock);

	if (mode & CRYPTO_LOCK)
		lock->_mutex.lock();
	else
		lock->_mutex.unlock();
}


void SSLInitializer::dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line)
{
	delete lock;
}


} } // namespace Poco::Net
