2011-03-16 12:46:19 +00:00
|
|
|
/**
|
|
|
|
* @file BSecurity.c
|
|
|
|
* @author Ambroz Bizjak <ambrop7@gmail.com>
|
|
|
|
*
|
|
|
|
* @section LICENSE
|
|
|
|
*
|
2012-01-27 13:24:37 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the author nor the
|
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
2011-03-16 12:46:19 +00:00
|
|
|
*
|
2012-01-27 13:24:37 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2011-03-16 12:46:19 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-16 16:52:17 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2011-03-16 12:46:19 +00:00
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
|
|
|
#include <misc/debug.h>
|
|
|
|
#include <misc/balloc.h>
|
|
|
|
|
|
|
|
#include <security/BSecurity.h>
|
|
|
|
|
|
|
|
int bsecurity_initialized = 0;
|
|
|
|
|
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
|
|
|
pthread_mutex_t *bsecurity_locks;
|
|
|
|
int bsecurity_num_locks;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
|
|
|
|
|
|
|
static unsigned long id_callback (void)
|
|
|
|
{
|
|
|
|
ASSERT(bsecurity_initialized)
|
|
|
|
|
|
|
|
return (unsigned long)pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void locking_callback (int mode, int type, const char *file, int line)
|
|
|
|
{
|
|
|
|
ASSERT(bsecurity_initialized)
|
|
|
|
ASSERT(type >= 0)
|
|
|
|
ASSERT(type < bsecurity_num_locks)
|
|
|
|
|
|
|
|
if ((mode & CRYPTO_LOCK)) {
|
|
|
|
ASSERT_FORCE(pthread_mutex_lock(&bsecurity_locks[type]) == 0)
|
|
|
|
} else {
|
|
|
|
ASSERT_FORCE(pthread_mutex_unlock(&bsecurity_locks[type]) == 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-03-16 17:55:34 +00:00
|
|
|
int BSecurity_GlobalInitThreadSafe (void)
|
2011-03-16 12:46:19 +00:00
|
|
|
{
|
|
|
|
ASSERT(!bsecurity_initialized)
|
|
|
|
|
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
|
|
|
|
2011-03-16 17:55:34 +00:00
|
|
|
// get number of locks
|
|
|
|
int num_locks = CRYPTO_num_locks();
|
|
|
|
ASSERT_FORCE(num_locks >= 0)
|
|
|
|
|
|
|
|
// alloc locks array
|
|
|
|
if (!(bsecurity_locks = BAllocArray(num_locks, sizeof(bsecurity_locks[0])))) {
|
|
|
|
goto fail0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// init locks
|
|
|
|
bsecurity_num_locks = 0;
|
|
|
|
for (int i = 0; i < num_locks; i++) {
|
|
|
|
if (pthread_mutex_init(&bsecurity_locks[i], NULL) != 0) {
|
|
|
|
goto fail1;
|
2011-03-16 12:46:19 +00:00
|
|
|
}
|
2011-03-16 17:55:34 +00:00
|
|
|
bsecurity_num_locks++;
|
2011-03-16 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bsecurity_initialized = 1;
|
|
|
|
|
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
2011-03-16 17:55:34 +00:00
|
|
|
CRYPTO_set_id_callback(id_callback);
|
|
|
|
CRYPTO_set_locking_callback(locking_callback);
|
2011-03-16 12:46:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
|
|
|
fail1:
|
|
|
|
while (bsecurity_num_locks > 0) {
|
|
|
|
ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
|
|
|
|
bsecurity_num_locks--;
|
|
|
|
}
|
|
|
|
BFree(bsecurity_locks);
|
|
|
|
fail0:
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-16 17:55:34 +00:00
|
|
|
void BSecurity_GlobalFreeThreadSafe (void)
|
2011-03-16 16:52:17 +00:00
|
|
|
{
|
|
|
|
ASSERT(bsecurity_initialized)
|
|
|
|
|
|
|
|
#ifdef BADVPN_THREADWORK_USE_PTHREAD
|
|
|
|
|
2011-03-16 17:55:34 +00:00
|
|
|
// remove callbacks
|
|
|
|
CRYPTO_set_locking_callback(NULL);
|
|
|
|
CRYPTO_set_id_callback(NULL);
|
|
|
|
|
|
|
|
// free locks
|
|
|
|
while (bsecurity_num_locks > 0) {
|
|
|
|
ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
|
|
|
|
bsecurity_num_locks--;
|
2011-03-16 16:52:17 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 17:55:34 +00:00
|
|
|
// free locks array
|
|
|
|
BFree(bsecurity_locks);
|
|
|
|
|
2011-03-16 16:52:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bsecurity_initialized = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-16 17:55:34 +00:00
|
|
|
void BSecurity_GlobalAssertThreadSafe (int thread_safe)
|
2011-03-16 12:46:19 +00:00
|
|
|
{
|
2011-03-16 17:55:34 +00:00
|
|
|
ASSERT(thread_safe == 0 || thread_safe == 1)
|
|
|
|
ASSERT(!(thread_safe) || bsecurity_initialized)
|
2011-03-16 12:46:19 +00:00
|
|
|
}
|