diff --git a/ChangeLog b/ChangeLog index 249434478..0531a8431 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,7 @@ Features * PSK and DHE-PSK based ciphersuites added * Memory allocation abstraction layer added * Buffer-based memory allocator added (no malloc() / free() / HEAP usage) + * Threading abstraction layer added (dummy / pthread / alternate) * Public Key abstraction layer added * Parsing Elliptic Curve keys * Parsing Elliptic Curve certificates diff --git a/include/polarssl/config.h b/include/polarssl/config.h index f2ac41c67..4a9d0efd5 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -659,6 +659,39 @@ */ #define POLARSSL_SSL_TRUNCATED_HMAC +/** + * \def POLARSSL_THREADING_ALT + * + * Provide your own alternate threading implementation. + * + * Requires: POLARSSL_THREADING_C + * + * Uncomment this to allow your own alternate threading implementation. +#define POLARSSL_THREADING_ALT + */ + +/** + * \def POLARSSL_THREADING_DUMMY + * + * Provide a dummy threading implementation. + * + * Requires: POLARSSL_THREADING_C + * + * Uncomment this to enable code to compile like with threading enabled +#define POLARSSL_THREADING_DUMMY + */ + +/** + * \def POLARSSL_THREADING_PTHREAD + * + * Enable the pthread wrapper layer for the threading layer. + * + * Requires: POLARSSL_THREADING_C + * + * Uncomment this to enable pthread mutexes. +#define POLARSSL_THREADING_PTHREAD + */ + /** * \def POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 * @@ -1435,6 +1468,27 @@ */ #define POLARSSL_SSL_TLS_C +/** + * \def POLARSSL_THREADING_C + * + * Enable the threading abstraction layer. + * By default PolarSSL assumes it is used in a non-threaded environment or that + * contexts are not shared between threads. If you do intend to use contexts + * between threads, you will need to enable this layer to prevent race + * conditions. + * + * Module: library/threading.c + * + * This allows different threading implementations (self-implemented or + * provided). + * + * You will have to enable either POLARSSL_THREADING_ALT, + * POLARSSL_THREADING_PTHREAD or POLARSSL_THREADING_DUMMY. + * + * Enable this layer to allow use of mutexes within PolarSSL +#define POLARSSL_THREADING_C + */ + /** * \def POLARSSL_TIMING_C * @@ -1784,6 +1838,32 @@ #error "POLARSSL_SSL_SESSION_TICKETS_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_THREADING_DUMMY) +#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL) +#error "POLARSSL_THREADING_DUMMY defined, but not all prerequisites" +#endif +#define POLARSSL_THREADING_IMPL +#endif + +#if defined(POLARSSL_THREADING_PTHREAD) +#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL) +#error "POLARSSL_THREADING_PTHREAD defined, but not all prerequisites" +#endif +#define POLARSSL_THREADING_IMPL +#endif + +#if defined(POLARSSL_THREADING_ALT) +#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL) +#error "POLARSSL_THREADING_ALT defined, but not all prerequisites" +#endif +#define POLARSSL_THREADING_IMPL +#endif + +#if defined(POLARSSL_THREADING_C) && !defined(POLARSSL_THREADING_IMPL) +#error "POLARSSL_THREADING_C defined, single threading implementation required" +#endif +#undef POLARSSL_THREADING_IMPL + #if defined(POLARSSL_X509_USE_C) && ( !defined(POLARSSL_BIGNUM_C) || \ !defined(POLARSSL_OID_C) || !defined(POLARSSL_ASN1_PARSE_C) || \ !defined(POLARSSL_PK_PARSE_C) ) diff --git a/include/polarssl/error.h b/include/polarssl/error.h index ab3d63211..a64960c06 100644 --- a/include/polarssl/error.h +++ b/include/polarssl/error.h @@ -53,6 +53,7 @@ * MPI 7 0x0002-0x0010 * GCM 2 0x0012-0x0014 * BLOWFISH 2 0x0016-0x0018 + * THREADING 3 0x001A-0x001E * AES 2 0x0020-0x0022 * CAMELLIA 2 0x0024-0x0026 * XTEA 1 0x0028-0x0028 diff --git a/include/polarssl/threading.h b/include/polarssl/threading.h new file mode 100644 index 000000000..4afaeea39 --- /dev/null +++ b/include/polarssl/threading.h @@ -0,0 +1,84 @@ +/** + * \file threading.h + * + * \brief Threading abstraction layer + * + * Copyright (C) 2006-2013, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef POLARSSL_THREADING_H +#define POLARSSL_THREADING_H + +#include "config.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define POLARSSL_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */ +#define POLARSSL_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */ +#define POLARSSL_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */ + +#if defined(POLARSSL_THREADING_DUMMY) +typedef void threading_mutex_t; +#endif + +#if defined(POLARSSL_THREADING_PTHREAD) +#include +typedef pthread_mutex_t threading_mutex_t; +#endif + +#if defined(POLARSSL_THREADING_ALT) +/* You should define the threading_mutex_t type in your header */ +#include "threading_alt.h" + +/** + * \brief Set your alternate threading implementation function + * pointers + * + * \param mutex_init the malloc function implementation + * \param mutex_free the free function implementation + * \param mutex_lock the lock function implementation + * \param mutex_unlock the unlock function implementation + * + * \return 0 if successful + */ +int threading_set_alt( int (*mutex_init)( threading_mutex_t * ), + int (*mutex_free)( threading_mutex_t * ), + int (*mutex_lock)( threading_mutex_t * ), + int (*mutex_unlock)( threading_mutex_t * ) ); +#endif /* POLARSSL_THREADING_ALT_C */ + +/* + * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock + */ +extern int (*polarssl_mutex_init)( threading_mutex_t *mutex ); +extern int (*polarssl_mutex_free)( threading_mutex_t *mutex ); +extern int (*polarssl_mutex_lock)( threading_mutex_t *mutex ); +extern int (*polarssl_mutex_unlock)( threading_mutex_t *mutex ); + +#ifdef __cplusplus +} +#endif + +#endif /* threading.h */ diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index dda7ff1bd..bc22f2682 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -52,6 +52,7 @@ set(src ssl_cli.c ssl_srv.c ssl_tls.c + threading.c timing.c version.c x509.c diff --git a/library/Makefile b/library/Makefile index 003b93304..4c37adf9c 100644 --- a/library/Makefile +++ b/library/Makefile @@ -54,7 +54,8 @@ OBJS= aes.o arc4.o asn1parse.o \ rsa.o sha1.o sha256.o \ sha512.o ssl_cache.o ssl_cli.o \ ssl_srv.o ssl_ciphersuites.o \ - ssl_tls.o timing.o version.o \ + ssl_tls.o threading.o timing.o \ + version.o \ x509.o x509_create.o \ x509_crl.o x509_crt.o x509_csr.o \ x509write_crt.o x509write_csr.o \ diff --git a/library/error.c b/library/error.c index 85068971a..a7e146782 100644 --- a/library/error.c +++ b/library/error.c @@ -145,6 +145,10 @@ #include "polarssl/ssl.h" #endif +#if defined(POLARSSL_THREADING_C) +#include "polarssl/threading.h" +#endif + #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C) #include "polarssl/x509.h" #endif @@ -647,6 +651,15 @@ void polarssl_strerror( int ret, char *buf, size_t buflen ) snprintf( buf, buflen, "SHA512 - Read/write error in file" ); #endif /* POLARSSL_SHA512_C */ +#if defined(POLARSSL_THREADING_C) + if( use_ret == -(POLARSSL_ERR_THREADING_FEATURE_UNAVAILABLE) ) + snprintf( buf, buflen, "THREADING - The selected feature is not available" ); + if( use_ret == -(POLARSSL_ERR_THREADING_BAD_INPUT_DATA) ) + snprintf( buf, buflen, "THREADING - Bad input parameters to function" ); + if( use_ret == -(POLARSSL_ERR_THREADING_MUTEX_ERROR) ) + snprintf( buf, buflen, "THREADING - Locking / unlocking / free failed with error code" ); +#endif /* POLARSSL_THREADING_C */ + #if defined(POLARSSL_XTEA_C) if( use_ret == -(POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH) ) snprintf( buf, buflen, "XTEA - The data input has an invalid length" ); diff --git a/library/threading.c b/library/threading.c new file mode 100644 index 000000000..e79348aa3 --- /dev/null +++ b/library/threading.c @@ -0,0 +1,134 @@ +/* + * Threading abstraction layer + * + * Copyright (C) 2006-2013, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "polarssl/config.h" + +#if defined(POLARSSL_THREADING_C) + +#include "polarssl/threading.h" + +#if defined(POLARSSL_THREADING_DUMMY) +static int threading_mutex_init_dummy( threading_mutex_t *mutex ) +{ + ((void) mutex ); + return( 0 ); +} + +static int threading_mutex_free_dummy( threading_mutex_t *mutex ) +{ + ((void) mutex ); + return( 0 ); +} + +static int threading_mutex_lock_dummy( threading_mutex_t *mutex ) +{ + ((void) mutex ); + return( 0 ); +} + +static int threading_mutex_unlock_dummy( threading_mutex_t *mutex ) +{ + ((void) mutex ); + return( 0 ); +} + +int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_dummy; +int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_dummy; +int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_dummy; +int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_dummy; +#endif /* POLARSSL_THREADING_DUMMY */ + +#if defined(POLARSSL_THREADING_PTHREAD) +static int threading_mutex_init_pthread( threading_mutex_t *mutex ) +{ + if( mutex == NULL ) + return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); + + if( pthread_mutex_init( mutex, NULL ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); + + return( 0 ); +} + +static int threading_mutex_free_pthread( threading_mutex_t *mutex ) +{ + if( mutex == NULL ) + return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); + + if( pthread_mutex_destroy( mutex ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); + + return( 0 ); +} + +static int threading_mutex_lock_pthread( threading_mutex_t *mutex ) +{ + if( mutex == NULL ) + return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); + + if( pthread_mutex_lock( mutex ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); + + return( 0 ); +} + +static int threading_mutex_unlock_pthread( threading_mutex_t *mutex ) +{ + if( mutex == NULL ) + return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); + + if( pthread_mutex_unlock( mutex ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); + + return( 0 ); +} + +int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread; +int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread; +int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread; +int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread; +#endif /* POLARSSL_THREADING_PTHREAD */ + +#if defined(POLARSSL_THREADING_ALT) +int (*polarssl_mutex_init)( threading_mutex_t * ) = NULL; +int (*polarssl_mutex_free)( threading_mutex_t * ) = NULL; +int (*polarssl_mutex_lock)( threading_mutex_t * ) = NULL; +int (*polarssl_mutex_unlock)( threading_mutex_t * ) = NULL; + +int threading_set_own( int (*mutex_init)( threading_mutex_t * ), + int (*mutex_free)( threading_mutex_t * ), + int (*mutex_lock)( threading_mutex_t * ), + int (*mutex_unlock)( threading_mutex_t * ) ) +{ + polarssl_mutex_init = mutex_init; + polarssl_mutex_free = mutex_free; + polarssl_mutex_lock = mutex_lock; + polarssl_mutex_unlock = mutex_unlock; + + return( 0 ); +} +#endif /* POLARSSL_THREADING_ALT_C */ + +#endif /* POLARSSL_THREADING_C */ diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index 85dde353c..0dff537d4 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -11,7 +11,8 @@ my $error_format_file = $data_dir.'/error.fmt'; my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM", "BASE64", "XTEA", "PBKDF2", "OID", "PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY", - "MD2", "MD4", "MD5", "SHA1", "SHA256", "SHA512", "GCM" ); + "MD2", "MD4", "MD5", "SHA1", "SHA256", "SHA512", + "GCM", "THREADING" ); my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL", "PK", "PKCS12", "PKCS5" );