forked from wolfSSL/wolfssl
Merge pull request #8187 from douzzer/20241114-wolfSSL_CTX_UnloadIntermediateCerts-thread-safety
20241114-wolfSSL_CTX_UnloadIntermediateCerts-thread-safety
This commit is contained in:
@ -2297,7 +2297,7 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap)
|
|||||||
ctx->minDowngrade = WOLFSSL_MIN_DOWNGRADE;
|
ctx->minDowngrade = WOLFSSL_MIN_DOWNGRADE;
|
||||||
}
|
}
|
||||||
|
|
||||||
wolfSSL_RefInit(&ctx->ref, &ret);
|
wolfSSL_RefWithMutexInit(&ctx->ref, &ret);
|
||||||
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
WOLFSSL_MSG("Mutex error on CTX init");
|
WOLFSSL_MSG("Mutex error on CTX init");
|
||||||
@ -2782,7 +2782,7 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* decrement CTX reference count */
|
/* decrement CTX reference count */
|
||||||
wolfSSL_RefDec(&ctx->ref, &isZero, &ret);
|
wolfSSL_RefWithMutexDec(&ctx->ref, &isZero, &ret);
|
||||||
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
/* check error state, if mutex error code then mutex init failed but
|
/* check error state, if mutex error code then mutex init failed but
|
||||||
|
20
src/ssl.c
20
src/ssl.c
@ -1136,7 +1136,7 @@ WOLFSSL_CTX* wolfSSL_CTX_new(WOLFSSL_METHOD* method)
|
|||||||
int wolfSSL_CTX_up_ref(WOLFSSL_CTX* ctx)
|
int wolfSSL_CTX_up_ref(WOLFSSL_CTX* ctx)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
wolfSSL_RefInc(&ctx->ref, &ret);
|
wolfSSL_RefWithMutexInc(&ctx->ref, &ret);
|
||||||
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
||||||
return ((ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE);
|
return ((ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE);
|
||||||
#else
|
#else
|
||||||
@ -11104,18 +11104,30 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
|
|||||||
|
|
||||||
int wolfSSL_CTX_UnloadIntermediateCerts(WOLFSSL_CTX* ctx)
|
int wolfSSL_CTX_UnloadIntermediateCerts(WOLFSSL_CTX* ctx)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
WOLFSSL_ENTER("wolfSSL_CTX_UnloadIntermediateCerts");
|
WOLFSSL_ENTER("wolfSSL_CTX_UnloadIntermediateCerts");
|
||||||
|
|
||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
ret = wolfSSL_RefWithMutexLock(&ctx->ref);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
if (ctx->ref.count > 1) {
|
if (ctx->ref.count > 1) {
|
||||||
WOLFSSL_MSG("ctx object must have a ref count of 1 before "
|
WOLFSSL_MSG("ctx object must have a ref count of 1 before "
|
||||||
"unloading intermediate certs");
|
"unloading intermediate certs");
|
||||||
return BAD_STATE_E;
|
ret = BAD_STATE_E;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ret = wolfSSL_CertManagerUnloadIntermediateCerts(ctx->cm);
|
||||||
}
|
}
|
||||||
|
|
||||||
return wolfSSL_CertManagerUnloadIntermediateCerts(ctx->cm);
|
if (wolfSSL_RefWithMutexUnlock(&ctx->ref) != 0)
|
||||||
|
WOLFSSL_MSG("Failed to unlock mutex!");
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -20579,7 +20591,7 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
|
|||||||
InitSSL_CTX_Suites(ctx);
|
InitSSL_CTX_Suites(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
wolfSSL_RefInc(&ctx->ref, &ret);
|
wolfSSL_RefWithMutexInc(&ctx->ref, &ret);
|
||||||
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
/* can only fail on serious stuff, like mutex not working
|
/* can only fail on serious stuff, like mutex not working
|
||||||
|
@ -1386,6 +1386,9 @@ int wolfSSL_X509_add_ext(WOLFSSL_X509 *x509, WOLFSSL_X509_EXTENSION *ext,
|
|||||||
if (ext->value.length == sizeof(word16)) {
|
if (ext->value.length == sizeof(word16)) {
|
||||||
/* if ext->value is already word16, set directly */
|
/* if ext->value is already word16, set directly */
|
||||||
x509->keyUsage = *(word16*)ext->value.data;
|
x509->keyUsage = *(word16*)ext->value.data;
|
||||||
|
#ifdef BIG_ENDIAN_ORDER
|
||||||
|
x509->keyUsage = rotlFixed16(x509->keyUsage, 8U);
|
||||||
|
#endif
|
||||||
x509->keyUsageCrit = (byte)ext->crit;
|
x509->keyUsageCrit = (byte)ext->crit;
|
||||||
x509->keyUsageSet = 1;
|
x509->keyUsageSet = 1;
|
||||||
}
|
}
|
||||||
@ -1406,7 +1409,7 @@ int wolfSSL_X509_add_ext(WOLFSSL_X509 *x509, WOLFSSL_X509_EXTENSION *ext,
|
|||||||
case WC_NID_ext_key_usage:
|
case WC_NID_ext_key_usage:
|
||||||
if (ext && ext->value.data) {
|
if (ext && ext->value.data) {
|
||||||
if (ext->value.length == sizeof(byte)) {
|
if (ext->value.length == sizeof(byte)) {
|
||||||
/* if ext->value is already word16, set directly */
|
/* if ext->value is already 1 byte, set directly */
|
||||||
x509->extKeyUsage = *(byte*)ext->value.data;
|
x509->extKeyUsage = *(byte*)ext->value.data;
|
||||||
x509->extKeyUsageCrit = (byte)ext->crit;
|
x509->extKeyUsageCrit = (byte)ext->crit;
|
||||||
}
|
}
|
||||||
|
@ -83073,7 +83073,10 @@ static int test_wolfSSL_d2i_X509_REQ(void)
|
|||||||
* (PEM_read_X509_REQ)*/
|
* (PEM_read_X509_REQ)*/
|
||||||
ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
|
ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
|
||||||
ExpectNull(PEM_read_X509_REQ(XBADFILE, &req, NULL, NULL));
|
ExpectNull(PEM_read_X509_REQ(XBADFILE, &req, NULL, NULL));
|
||||||
ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
|
if (EXPECT_SUCCESS())
|
||||||
|
ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
|
||||||
|
else if (f != XBADFILE)
|
||||||
|
XFCLOSE(f);
|
||||||
ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
|
ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
|
||||||
|
|
||||||
X509_free(req);
|
X509_free(req);
|
||||||
|
@ -115,8 +115,6 @@ masking and clearing memory logic.
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WC_RC2
|
|
||||||
|
|
||||||
/* This routine performs a left circular arithmetic shift of <x> by <y> value */
|
/* This routine performs a left circular arithmetic shift of <x> by <y> value */
|
||||||
WC_MISC_STATIC WC_INLINE word16 rotlFixed16(word16 x, word16 y)
|
WC_MISC_STATIC WC_INLINE word16 rotlFixed16(word16 x, word16 y)
|
||||||
{
|
{
|
||||||
@ -130,8 +128,6 @@ WC_MISC_STATIC WC_INLINE word16 rotrFixed16(word16 x, word16 y)
|
|||||||
return (x >> y) | (x << (sizeof(x) * 8 - y));
|
return (x >> y) | (x << (sizeof(x) * 8 - y));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* WC_RC2 */
|
|
||||||
|
|
||||||
/* This routine performs a byte swap of 32-bit word value. */
|
/* This routine performs a byte swap of 32-bit word value. */
|
||||||
#if defined(__CCRX__) && !defined(NO_INLINE) /* shortest version for CC-RX */
|
#if defined(__CCRX__) && !defined(NO_INLINE) /* shortest version for CC-RX */
|
||||||
#define ByteReverseWord32(value) _builtin_revl(value)
|
#define ByteReverseWord32(value) _builtin_revl(value)
|
||||||
|
@ -1232,7 +1232,7 @@ char* wc_strdup_ex(const char *src, int memType) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WOLFSSL_ATOMIC_OPS
|
#if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
|
||||||
|
|
||||||
#ifdef HAVE_C___ATOMIC
|
#ifdef HAVE_C___ATOMIC
|
||||||
/* Atomic ops using standard C lib */
|
/* Atomic ops using standard C lib */
|
||||||
@ -1292,8 +1292,9 @@ int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
|
|||||||
|
|
||||||
#endif /* WOLFSSL_ATOMIC_OPS */
|
#endif /* WOLFSSL_ATOMIC_OPS */
|
||||||
|
|
||||||
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_ATOMIC_OPS)
|
#if !defined(SINGLE_THREADED)
|
||||||
void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err)
|
|
||||||
|
void wolfSSL_RefWithMutexInit(wolfSSL_RefWithMutex* ref, int* err)
|
||||||
{
|
{
|
||||||
int ret = wc_InitMutex(&ref->mutex);
|
int ret = wc_InitMutex(&ref->mutex);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
@ -1304,14 +1305,14 @@ void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err)
|
|||||||
*err = ret;
|
*err = ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wolfSSL_RefFree(wolfSSL_Ref* ref)
|
void wolfSSL_RefWithMutexFree(wolfSSL_RefWithMutex* ref)
|
||||||
{
|
{
|
||||||
if (wc_FreeMutex(&ref->mutex) != 0) {
|
if (wc_FreeMutex(&ref->mutex) != 0) {
|
||||||
WOLFSSL_MSG("Failed to free mutex of reference counting!");
|
WOLFSSL_MSG("Failed to free mutex of reference counting!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err)
|
void wolfSSL_RefWithMutexInc(wolfSSL_RefWithMutex* ref, int* err)
|
||||||
{
|
{
|
||||||
int ret = wc_LockMutex(&ref->mutex);
|
int ret = wc_LockMutex(&ref->mutex);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
@ -1324,7 +1325,17 @@ void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err)
|
|||||||
*err = ret;
|
*err = ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err)
|
int wolfSSL_RefWithMutexLock(wolfSSL_RefWithMutex* ref)
|
||||||
|
{
|
||||||
|
return wc_LockMutex(&ref->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int wolfSSL_RefWithMutexUnlock(wolfSSL_RefWithMutex* ref)
|
||||||
|
{
|
||||||
|
return wc_UnLockMutex(&ref->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wolfSSL_RefWithMutexDec(wolfSSL_RefWithMutex* ref, int* isZero, int* err)
|
||||||
{
|
{
|
||||||
int ret = wc_LockMutex(&ref->mutex);
|
int ret = wc_LockMutex(&ref->mutex);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
@ -1341,7 +1352,7 @@ void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err)
|
|||||||
}
|
}
|
||||||
*err = ret;
|
*err = ret;
|
||||||
}
|
}
|
||||||
#endif
|
#endif /* ! SINGLE_THREADED */
|
||||||
|
|
||||||
#if WOLFSSL_CRYPT_HW_MUTEX
|
#if WOLFSSL_CRYPT_HW_MUTEX
|
||||||
/* Mutex for protection of cryptography hardware */
|
/* Mutex for protection of cryptography hardware */
|
||||||
|
@ -3724,7 +3724,7 @@ struct WOLFSSL_CTX {
|
|||||||
#ifdef SINGLE_THREADED
|
#ifdef SINGLE_THREADED
|
||||||
WC_RNG* rng; /* to be shared with WOLFSSL w/o locking */
|
WC_RNG* rng; /* to be shared with WOLFSSL w/o locking */
|
||||||
#endif
|
#endif
|
||||||
wolfSSL_Ref ref;
|
wolfSSL_RefWithMutex ref;
|
||||||
int err; /* error code in case of mutex not created */
|
int err; /* error code in case of mutex not created */
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
buffer serverDH_P;
|
buffer serverDH_P;
|
||||||
|
@ -2759,16 +2759,20 @@ WOLFSSL_API void wolfSSL_ERR_print_errors(WOLFSSL_BIO *bio);
|
|||||||
enum { /* ssl Constants */
|
enum { /* ssl Constants */
|
||||||
WOLFSSL_ERROR_NONE = 0, /* for most functions */
|
WOLFSSL_ERROR_NONE = 0, /* for most functions */
|
||||||
WOLFSSL_FAILURE = 0, /* for some functions */
|
WOLFSSL_FAILURE = 0, /* for some functions */
|
||||||
|
WOLFSSL_SUCCESS = 1,
|
||||||
|
|
||||||
#if defined(WOLFSSL_DEBUG_TRACE_ERROR_CODES) && \
|
#if defined(WOLFSSL_DEBUG_TRACE_ERROR_CODES) && \
|
||||||
(defined(BUILDING_WOLFSSL) || \
|
(defined(BUILDING_WOLFSSL) || \
|
||||||
defined(WOLFSSL_DEBUG_TRACE_ERROR_CODES_ALWAYS))
|
defined(WOLFSSL_DEBUG_TRACE_ERROR_CODES_ALWAYS))
|
||||||
#define WOLFSSL_FAILURE WC_ERR_TRACE(WOLFSSL_FAILURE)
|
#define WOLFSSL_FAILURE WC_ERR_TRACE(WOLFSSL_FAILURE)
|
||||||
#define CONST_NUM_ERR_WOLFSSL_FAILURE 0
|
#define CONST_NUM_ERR_WOLFSSL_FAILURE 0
|
||||||
|
/* include CONST_NUM_ERR_ variants of the success codes, so that they
|
||||||
|
* can be harmlessly wrapped in WC_NO_ERR_TRACE().
|
||||||
|
*/
|
||||||
|
#define CONST_NUM_ERR_WOLFSSL_ERROR_NONE 0
|
||||||
|
#define CONST_NUM_ERR_WOLFSSL_SUCCESS 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
WOLFSSL_SUCCESS = 1,
|
|
||||||
|
|
||||||
/* WOLFSSL_SHUTDOWN_NOT_DONE is returned by wolfSSL_shutdown and
|
/* WOLFSSL_SHUTDOWN_NOT_DONE is returned by wolfSSL_shutdown and
|
||||||
* wolfSSL_SendUserCanceled when the other end
|
* wolfSSL_SendUserCanceled when the other end
|
||||||
* of the connection has yet to send its close notify alert as part of the
|
* of the connection has yet to send its close notify alert as part of the
|
||||||
|
@ -46,12 +46,10 @@ word32 rotlFixed(word32 x, word32 y);
|
|||||||
WOLFSSL_LOCAL
|
WOLFSSL_LOCAL
|
||||||
word32 rotrFixed(word32 x, word32 y);
|
word32 rotrFixed(word32 x, word32 y);
|
||||||
|
|
||||||
#ifdef WC_RC2
|
|
||||||
WOLFSSL_LOCAL
|
WOLFSSL_LOCAL
|
||||||
word16 rotlFixed16(word16 x, word16 y);
|
word16 rotlFixed16(word16 x, word16 y);
|
||||||
WOLFSSL_LOCAL
|
WOLFSSL_LOCAL
|
||||||
word16 rotrFixed16(word16 x, word16 y);
|
word16 rotrFixed16(word16 x, word16 y);
|
||||||
#endif
|
|
||||||
|
|
||||||
WOLFSSL_LOCAL
|
WOLFSSL_LOCAL
|
||||||
word32 ByteReverseWord32(word32 value);
|
word32 ByteReverseWord32(word32 value);
|
||||||
|
@ -318,25 +318,6 @@ typedef struct w64wrapper {
|
|||||||
#define WOLFSSL_MAX_16BIT 0xffffU
|
#define WOLFSSL_MAX_16BIT 0xffffU
|
||||||
#define WOLFSSL_MAX_32BIT 0xffffffffU
|
#define WOLFSSL_MAX_32BIT 0xffffffffU
|
||||||
|
|
||||||
#ifndef WARN_UNUSED_RESULT
|
|
||||||
#if defined(WOLFSSL_LINUXKM) && defined(__must_check)
|
|
||||||
#define WARN_UNUSED_RESULT __must_check
|
|
||||||
#elif (defined(__GNUC__) && (__GNUC__ >= 4)) || \
|
|
||||||
(defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
|
|
||||||
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
|
||||||
#else
|
|
||||||
#define WARN_UNUSED_RESULT
|
|
||||||
#endif
|
|
||||||
#endif /* WARN_UNUSED_RESULT */
|
|
||||||
|
|
||||||
#ifndef WC_MAYBE_UNUSED
|
|
||||||
#if (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) || defined(__IAR_SYSTEMS_ICC__)
|
|
||||||
#define WC_MAYBE_UNUSED __attribute__((unused))
|
|
||||||
#else
|
|
||||||
#define WC_MAYBE_UNUSED
|
|
||||||
#endif
|
|
||||||
#endif /* WC_MAYBE_UNUSED */
|
|
||||||
|
|
||||||
#ifndef WC_DO_NOTHING
|
#ifndef WC_DO_NOTHING
|
||||||
#define WC_DO_NOTHING do {} while (0)
|
#define WC_DO_NOTHING do {} while (0)
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@ -347,43 +328,6 @@ typedef struct w64wrapper {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* use inlining if compiler allows */
|
|
||||||
#ifndef WC_INLINE
|
|
||||||
#ifndef NO_INLINE
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#define WC_INLINE __inline
|
|
||||||
#elif defined(__GNUC__)
|
|
||||||
#ifdef WOLFSSL_VXWORKS
|
|
||||||
#define WC_INLINE __inline__
|
|
||||||
#else
|
|
||||||
#define WC_INLINE inline
|
|
||||||
#endif
|
|
||||||
#elif defined(__IAR_SYSTEMS_ICC__)
|
|
||||||
#define WC_INLINE inline
|
|
||||||
#elif defined(THREADX)
|
|
||||||
#define WC_INLINE _Inline
|
|
||||||
#elif defined(__ghc__)
|
|
||||||
#ifndef __cplusplus
|
|
||||||
#define WC_INLINE __inline
|
|
||||||
#else
|
|
||||||
#define WC_INLINE inline
|
|
||||||
#endif
|
|
||||||
#elif defined(__CCRX__)
|
|
||||||
#define WC_INLINE inline
|
|
||||||
#elif defined(__DCC__)
|
|
||||||
#ifndef __cplusplus
|
|
||||||
#define WC_INLINE __inline__
|
|
||||||
#else
|
|
||||||
#define WC_INLINE inline
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define WC_INLINE WC_MAYBE_UNUSED
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define WC_INLINE WC_MAYBE_UNUSED
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
|
#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
|
||||||
#define INLINE WC_INLINE
|
#define INLINE WC_INLINE
|
||||||
#endif
|
#endif
|
||||||
|
@ -62,6 +62,63 @@
|
|||||||
#include "../../linuxkm/linuxkm_wc_port.h"
|
#include "../../linuxkm/linuxkm_wc_port.h"
|
||||||
#endif /* WOLFSSL_LINUXKM */
|
#endif /* WOLFSSL_LINUXKM */
|
||||||
|
|
||||||
|
#ifndef WARN_UNUSED_RESULT
|
||||||
|
#if defined(WOLFSSL_LINUXKM) && defined(__must_check)
|
||||||
|
#define WARN_UNUSED_RESULT __must_check
|
||||||
|
#elif (defined(__GNUC__) && (__GNUC__ >= 4)) || \
|
||||||
|
(defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
|
||||||
|
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
||||||
|
#else
|
||||||
|
#define WARN_UNUSED_RESULT
|
||||||
|
#endif
|
||||||
|
#endif /* !WARN_UNUSED_RESULT */
|
||||||
|
|
||||||
|
#ifndef WC_MAYBE_UNUSED
|
||||||
|
#if (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) || \
|
||||||
|
defined(__IAR_SYSTEMS_ICC__)
|
||||||
|
#define WC_MAYBE_UNUSED __attribute__((unused))
|
||||||
|
#else
|
||||||
|
#define WC_MAYBE_UNUSED
|
||||||
|
#endif
|
||||||
|
#endif /* !WC_MAYBE_UNUSED */
|
||||||
|
|
||||||
|
/* use inlining if compiler allows */
|
||||||
|
#ifndef WC_INLINE
|
||||||
|
#ifndef NO_INLINE
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define WC_INLINE __inline
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#ifdef WOLFSSL_VXWORKS
|
||||||
|
#define WC_INLINE __inline__
|
||||||
|
#else
|
||||||
|
#define WC_INLINE inline
|
||||||
|
#endif
|
||||||
|
#elif defined(__IAR_SYSTEMS_ICC__)
|
||||||
|
#define WC_INLINE inline
|
||||||
|
#elif defined(THREADX)
|
||||||
|
#define WC_INLINE _Inline
|
||||||
|
#elif defined(__ghc__)
|
||||||
|
#ifndef __cplusplus
|
||||||
|
#define WC_INLINE __inline
|
||||||
|
#else
|
||||||
|
#define WC_INLINE inline
|
||||||
|
#endif
|
||||||
|
#elif defined(__CCRX__)
|
||||||
|
#define WC_INLINE inline
|
||||||
|
#elif defined(__DCC__)
|
||||||
|
#ifndef __cplusplus
|
||||||
|
#define WC_INLINE __inline__
|
||||||
|
#else
|
||||||
|
#define WC_INLINE inline
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define WC_INLINE WC_MAYBE_UNUSED
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define WC_INLINE WC_MAYBE_UNUSED
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
/* THREADING/MUTEX SECTION */
|
/* THREADING/MUTEX SECTION */
|
||||||
#if defined(SINGLE_THREADED) && defined(NO_FILESYSTEM)
|
#if defined(SINGLE_THREADED) && defined(NO_FILESYSTEM)
|
||||||
/* No system headers required for build. */
|
/* No system headers required for build. */
|
||||||
@ -335,7 +392,11 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef WOLFSSL_NO_ATOMICS
|
#ifndef WOLFSSL_NO_ATOMICS
|
||||||
#ifdef HAVE_C___ATOMIC
|
#ifdef SINGLE_THREADED
|
||||||
|
typedef int wolfSSL_Atomic_Int;
|
||||||
|
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
|
||||||
|
#define WOLFSSL_ATOMIC_OPS
|
||||||
|
#elif defined(HAVE_C___ATOMIC)
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
|
#if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
|
||||||
/* C++ using direct calls to compiler built-in functions */
|
/* C++ using direct calls to compiler built-in functions */
|
||||||
@ -365,34 +426,47 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif /* WOLFSSL_NO_ATOMICS */
|
#endif /* WOLFSSL_NO_ATOMICS */
|
||||||
|
|
||||||
#ifdef WOLFSSL_ATOMIC_OPS
|
#if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
|
||||||
WOLFSSL_API void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);
|
WOLFSSL_API void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);
|
||||||
/* Fetch* functions return the value of the counter immediately preceding
|
/* Fetch* functions return the value of the counter immediately preceding
|
||||||
* the effects of the function. */
|
* the effects of the function. */
|
||||||
WOLFSSL_API int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i);
|
WOLFSSL_API int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i);
|
||||||
WOLFSSL_API int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i);
|
WOLFSSL_API int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i);
|
||||||
#else
|
#else
|
||||||
/* Code using these fallback macros needs to arrange its own fallback for
|
/* Code using these fallback implementations in non-SINGLE_THREADED builds
|
||||||
* wolfSSL_Atomic_Int, which is never defined if
|
* needs to arrange its own explicit fallback to int for wolfSSL_Atomic_Int,
|
||||||
* !defined(WOLFSSL_ATOMIC_OPS). This forces local awareness of
|
* which is not defined if !defined(WOLFSSL_ATOMIC_OPS) &&
|
||||||
* thread-unsafe semantics.
|
* !defined(SINGLE_THREADED). This forces local awareness of thread-unsafe
|
||||||
|
* semantics.
|
||||||
*/
|
*/
|
||||||
#define wolfSSL_Atomic_Int_Init(c, i) (*(c) = (i))
|
#define wolfSSL_Atomic_Int_Init(c, i) (*(c) = (i))
|
||||||
#define wolfSSL_Atomic_Int_FetchAdd(c, i) (*(c) += (i), *(c) - (i))
|
static WC_INLINE int wolfSSL_Atomic_Int_FetchAdd(int *c, int i) {
|
||||||
#define wolfSSL_Atomic_Int_FetchSub(c, i) (*(c) -= (i), *(c) + (i))
|
int ret = *c;
|
||||||
|
*c += i;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
static WC_INLINE int wolfSSL_Atomic_Int_FetchSub(int *c, int i) {
|
||||||
|
int ret = *c;
|
||||||
|
*c -= i;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Reference counting. */
|
/* Reference counting. */
|
||||||
typedef struct wolfSSL_Ref {
|
typedef struct wolfSSL_RefWithMutex {
|
||||||
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_ATOMIC_OPS)
|
#if !defined(SINGLE_THREADED)
|
||||||
wolfSSL_Mutex mutex;
|
wolfSSL_Mutex mutex;
|
||||||
#endif
|
#endif
|
||||||
#ifdef WOLFSSL_ATOMIC_OPS
|
|
||||||
wolfSSL_Atomic_Int count;
|
|
||||||
#else
|
|
||||||
int count;
|
int count;
|
||||||
#endif
|
} wolfSSL_RefWithMutex;
|
||||||
|
|
||||||
|
#if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
|
||||||
|
typedef struct wolfSSL_Ref {
|
||||||
|
wolfSSL_Atomic_Int count;
|
||||||
} wolfSSL_Ref;
|
} wolfSSL_Ref;
|
||||||
|
#else
|
||||||
|
typedef struct wolfSSL_RefWithMutex wolfSSL_Ref;
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(SINGLE_THREADED) || defined(WOLFSSL_ATOMIC_OPS)
|
#if defined(SINGLE_THREADED) || defined(WOLFSSL_ATOMIC_OPS)
|
||||||
|
|
||||||
@ -419,10 +493,33 @@ typedef struct wolfSSL_Ref {
|
|||||||
|
|
||||||
#define WOLFSSL_REFCNT_ERROR_RETURN
|
#define WOLFSSL_REFCNT_ERROR_RETURN
|
||||||
|
|
||||||
WOLFSSL_LOCAL void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err);
|
#define wolfSSL_RefInit wolfSSL_RefWithMutexInit
|
||||||
WOLFSSL_LOCAL void wolfSSL_RefFree(wolfSSL_Ref* ref);
|
#define wolfSSL_RefFree wolfSSL_RefWithMutexFree
|
||||||
WOLFSSL_LOCAL void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err);
|
#define wolfSSL_RefInc wolfSSL_RefWithMutexInc
|
||||||
WOLFSSL_LOCAL void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err);
|
#define wolfSSL_RefDec wolfSSL_RefWithMutexDec
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(SINGLE_THREADED)
|
||||||
|
|
||||||
|
#define wolfSSL_RefWithMutexInit wolfSSL_RefInit
|
||||||
|
#define wolfSSL_RefWithMutexFree wolfSSL_RefFree
|
||||||
|
#define wolfSSL_RefWithMutexInc wolfSSL_RefInc
|
||||||
|
#define wolfSSL_RefWithMutexLock(ref) 0
|
||||||
|
#define wolfSSL_RefWithMutexUnlock(ref) 0
|
||||||
|
#define wolfSSL_RefWithMutexDec wolfSSL_RefDec
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
WOLFSSL_LOCAL void wolfSSL_RefWithMutexInit(wolfSSL_RefWithMutex* ref,
|
||||||
|
int* err);
|
||||||
|
WOLFSSL_LOCAL void wolfSSL_RefWithMutexFree(wolfSSL_RefWithMutex* ref);
|
||||||
|
WOLFSSL_LOCAL void wolfSSL_RefWithMutexInc(wolfSSL_RefWithMutex* ref,
|
||||||
|
int* err);
|
||||||
|
WOLFSSL_LOCAL int wolfSSL_RefWithMutexLock(wolfSSL_RefWithMutex* ref);
|
||||||
|
WOLFSSL_LOCAL int wolfSSL_RefWithMutexUnlock(wolfSSL_RefWithMutex* ref);
|
||||||
|
WOLFSSL_LOCAL void wolfSSL_RefWithMutexDec(wolfSSL_RefWithMutex* ref,
|
||||||
|
int* isZero, int* err);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user