Skip to content

Commit

Permalink
Merge branch 'fix-conversion' of github.com:gasbytes/wolfssl into fix…
Browse files Browse the repository at this point in the history
…-conversion
  • Loading branch information
gasbytes committed Nov 20, 2024
2 parents c7f1f3d + 58fa7f1 commit 584da13
Show file tree
Hide file tree
Showing 30 changed files with 3,569 additions and 907 deletions.
13 changes: 9 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ AC_ARG_ENABLE([kyber],
)

ENABLED_WC_KYBER=no
ENABLED_ML_KEM=yes
ENABLED_ML_KEM=unset
for v in `echo $ENABLED_KYBER | tr "," " "`
do
case $v in
Expand All @@ -1335,9 +1335,8 @@ do
original)
ENABLED_ORIGINAL=yes
;;
original-only)
ENABLED_ORIGINAL=yes
ENABLED_ML_KEM=no
ml-kem)
ENABLED_ML_KEM=yes
;;
*)
AC_MSG_ERROR([Invalid choice for KYBER []: $ENABLED_KYBER.])
Expand Down Expand Up @@ -1366,6 +1365,12 @@ then
if test "$ENABLED_KYBER1024" = ""; then
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_NO_KYBER1024"
fi
if test "$ENABLED_ML_KEM" = "unset"; then
ENABLED_ML_KEM=no
fi
fi
if test "$ENABLED_ML_KEM" = "unset"; then
ENABLED_ML_KEM=yes
fi
if test "$ENABLED_ML_KEM" = "yes"; then
if test "$ENABLED_KYBER512" = ""; then
Expand Down
9 changes: 8 additions & 1 deletion src/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,14 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio)
(bio->type == WOLFSSL_BIO_DGRAM))
{
bio->num.fd = SOCKET_INVALID;
} else {
}
else if (bio->type == WOLFSSL_BIO_FILE) {
#ifndef NO_FILESYSTEM
bio->ptr.fh = XBADFILE;
#endif
bio->num.fd = SOCKET_INVALID;
}
else {
bio->num.length = 0;
}
bio->init = 1;
Expand Down
31 changes: 4 additions & 27 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2769,25 +2769,6 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx)
(void)heapAtCTXInit;
}

#ifdef WOLFSSL_STATIC_MEMORY
static void SSL_CtxResourceFreeStaticMem(void* heap)
{
#ifndef SINGLE_THREADED
if (heap != NULL
#ifdef WOLFSSL_HEAP_TEST
/* avoid dereferencing a test value */
&& heap != (void*)WOLFSSL_HEAP_TEST
#endif
) {
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
WOLFSSL_HEAP* mem = hint->memory;
wc_FreeMutex(&mem->memory_mutex);
}
#else
(void)heap;
#endif
}
#endif /* WOLFSSL_STATIC_MEMORY */

void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
{
Expand All @@ -2809,9 +2790,6 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
if (ctx->err == WC_NO_ERR_TRACE(CTX_INIT_MUTEX_E)) {
SSL_CtxResourceFree(ctx);
XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
#ifdef WOLFSSL_STATIC_MEMORY
SSL_CtxResourceFreeStaticMem(heap);
#endif
}
return;
}
Expand All @@ -2829,9 +2807,6 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
#endif
wolfSSL_RefFree(&ctx->ref);
XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
#ifdef WOLFSSL_STATIC_MEMORY
SSL_CtxResourceFreeStaticMem(heap);
#endif
}
else {
WOLFSSL_MSG("CTX ref count not 0 yet, no free");
Expand Down Expand Up @@ -25673,7 +25648,9 @@ const char* wolfSSL_ERR_reason_error_string(unsigned long e)
}

/* pass to wolfCrypt */
if (error <= WC_FIRST_E && error >= WC_LAST_E) {
if ((error <= WC_SPAN1_FIRST_E && error >= WC_SPAN1_MIN_CODE_E) ||
(error <= WC_SPAN2_FIRST_E && error >= WC_SPAN2_MIN_CODE_E))
{
return wc_GetErrorString(error);
}

Expand All @@ -25685,7 +25662,7 @@ const char* wolfSSL_ERR_reason_error_string(unsigned long e)
#endif
}

switch ((enum wolfSSL_ErrorCodes)error) {
switch ((enum wolfSSL_ErrorCodes)error) { /* // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange) */

case UNSUPPORTED_SUITE :
return "unsupported cipher suite";
Expand Down
46 changes: 23 additions & 23 deletions src/sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ BOOL APIENTRY DllMain( HMODULE hModule,
#endif /* _WIN32 */


static WOLFSSL_GLOBAL int TraceOn = 0; /* Trace is off by default */
static WOLFSSL_GLOBAL XFILE TraceFile = 0;
static WC_THREADSHARED int TraceOn = 0; /* Trace is off by default */
static WC_THREADSHARED XFILE TraceFile = 0;


/* windows uses .rc table for this */
Expand Down Expand Up @@ -566,52 +566,52 @@ typedef struct SnifferSession {


/* Sniffer Server List and mutex */
static THREAD_LS_T WOLFSSL_GLOBAL SnifferServer* ServerList = NULL;
static THREAD_LS_T SnifferServer* ServerList = NULL;
#ifndef HAVE_C___ATOMIC
static WOLFSSL_GLOBAL wolfSSL_Mutex ServerListMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(ServerListMutex);
static WC_THREADSHARED wolfSSL_Mutex ServerListMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(ServerListMutex);
#endif

/* Session Hash Table, mutex, and count */
static THREAD_LS_T WOLFSSL_GLOBAL SnifferSession* SessionTable[HASH_SIZE];
static THREAD_LS_T SnifferSession* SessionTable[HASH_SIZE];
#ifndef HAVE_C___ATOMIC
static WOLFSSL_GLOBAL wolfSSL_Mutex SessionMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(SessionMutex);
static WC_THREADSHARED wolfSSL_Mutex SessionMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(SessionMutex);
#endif
static THREAD_LS_T WOLFSSL_GLOBAL int SessionCount = 0;
static THREAD_LS_T int SessionCount = 0;

static WOLFSSL_GLOBAL int RecoveryEnabled = 0; /* global switch */
static WOLFSSL_GLOBAL int MaxRecoveryMemory = -1;
static WC_THREADSHARED int RecoveryEnabled = 0; /* global switch */
static WC_THREADSHARED int MaxRecoveryMemory = -1;
/* per session max recovery memory */
#ifndef WOLFSSL_SNIFFER_NO_RECOVERY
/* Recovery of missed data switches and stats */
static WOLFSSL_GLOBAL wolfSSL_Mutex RecoveryMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(RecoveryMutex); /* for stats */
static WC_THREADSHARED wolfSSL_Mutex RecoveryMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(RecoveryMutex); /* for stats */
/* # of sessions with missed data */
static WOLFSSL_GLOBAL word32 MissedDataSessions = 0;
static WC_THREADSHARED word32 MissedDataSessions = 0;
#endif

/* Connection Info Callback */
static WOLFSSL_GLOBAL SSLConnCb ConnectionCb;
static WOLFSSL_GLOBAL void* ConnectionCbCtx = NULL;
static WC_THREADSHARED SSLConnCb ConnectionCb;
static WC_THREADSHARED void* ConnectionCbCtx = NULL;

#ifdef WOLFSSL_SNIFFER_STATS
/* Sessions Statistics */
static WOLFSSL_GLOBAL SSLStats SnifferStats;
static WOLFSSL_GLOBAL wolfSSL_Mutex StatsMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(StatsMutex);
static WC_THREADSHARED SSLStats SnifferStats;
static WC_THREADSHARED wolfSSL_Mutex StatsMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(StatsMutex);
#endif

#ifdef WOLFSSL_SNIFFER_KEY_CALLBACK
static WOLFSSL_GLOBAL SSLKeyCb KeyCb;
static WOLFSSL_GLOBAL void* KeyCbCtx = NULL;
static WC_THREADSHARED SSLKeyCb KeyCb;
static WC_THREADSHARED void* KeyCbCtx = NULL;
#endif

#ifdef WOLFSSL_SNIFFER_WATCH
/* Watch Key Callback */
static WOLFSSL_GLOBAL SSLWatchCb WatchCb;
static WOLFSSL_GLOBAL void* WatchCbCtx = NULL;
static WC_THREADSHARED SSLWatchCb WatchCb;
static WC_THREADSHARED void* WatchCbCtx = NULL;
#endif

#ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
/* Store Data Callback */
static WOLFSSL_GLOBAL SSLStoreDataCb StoreDataCb;
static WC_THREADSHARED SSLStoreDataCb StoreDataCb;
#endif


Expand Down Expand Up @@ -656,7 +656,7 @@ static void UpdateMissedDataSessions(void)


#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_ASYNC_CRYPT)
static WOLFSSL_GLOBAL int CryptoDeviceId = INVALID_DEVID;
static WC_THREADSHARED int CryptoDeviceId = INVALID_DEVID;
#endif

#if defined(WOLFSSL_SNIFFER_KEYLOGFILE)
Expand Down Expand Up @@ -7234,11 +7234,11 @@ typedef struct SecretNode {
#define WOLFSSL_SNIFFER_KEYLOGFILE_HASH_TABLE_SIZE HASH_SIZE
#endif

static THREAD_LS_T WOLFSSL_GLOBAL
static THREAD_LS_T
SecretNode*
secretHashTable[WOLFSSL_SNIFFER_KEYLOGFILE_HASH_TABLE_SIZE] = {NULL};
#ifndef HAVE_C___ATOMIC
static WOLFSSL_GLOBAL wolfSSL_Mutex secretListMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(secretListMutex);
static WC_THREADSHARED wolfSSL_Mutex secretListMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(secretListMutex);
#endif

static unsigned int secretHashFunction(unsigned char* clientRandom);
Expand Down
88 changes: 74 additions & 14 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,12 +1032,12 @@ int GetEchConfigsEx(WOLFSSL_EchConfig* configs, byte* output, word32* outputLen)
#endif

/* prevent multiple mutex initializations */
static volatile WOLFSSL_GLOBAL int initRefCount = 0;
static volatile WC_THREADSHARED int initRefCount = 0;
/* init ref count mutex */
static WOLFSSL_GLOBAL wolfSSL_Mutex inits_count_mutex
static WC_THREADSHARED wolfSSL_Mutex inits_count_mutex
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(inits_count_mutex);
#ifndef WOLFSSL_MUTEX_INITIALIZER
static WOLFSSL_GLOBAL int inits_count_mutex_valid = 0;
static WC_THREADSHARED volatile int inits_count_mutex_valid = 0;
#endif

/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
Expand Down Expand Up @@ -5641,12 +5641,48 @@ int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
static int wolfSSL_RAND_InitMutex(void);
#endif

/* If we don't have static mutex initializers, but we do have static atomic
* initializers, activate WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS to leverage
* the latter.
*
* See further explanation below in wolfSSL_Init().
*/
#ifndef WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
#if !defined(WOLFSSL_MUTEX_INITIALIZER) && !defined(SINGLE_THREADED) && \
defined(WOLFSSL_ATOMIC_OPS) && defined(WOLFSSL_ATOMIC_INITIALIZER)
#define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS 1
#else
#define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS 0
#endif
#elif defined(WOLFSSL_MUTEX_INITIALIZER) || defined(SINGLE_THREADED)
#undef WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
#define WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS 0
#endif

#if WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
#ifndef WOLFSSL_ATOMIC_OPS
#error WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS requires WOLFSSL_ATOMIC_OPS
#endif
#ifndef WOLFSSL_ATOMIC_INITIALIZER
#error WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS requires WOLFSSL_ATOMIC_INITIALIZER
#endif
static wolfSSL_Atomic_Int inits_count_mutex_atomic_initing_flag =
WOLFSSL_ATOMIC_INITIALIZER(0);
#endif /* WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS && !WOLFSSL_MUTEX_INITIALIZER */

#if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
static void AtExitCleanup(void)
{
if (initRefCount > 0) {
initRefCount = 1;
(void)wolfSSL_Cleanup();
#if WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
if (inits_count_mutex_valid == 1) {
(void)wc_FreeMutex(&inits_count_mutex);
inits_count_mutex_valid = 0;
inits_count_mutex_atomic_initing_flag = 0;
}
#endif
}
}
#endif
Expand All @@ -5663,8 +5699,31 @@ int wolfSSL_Init(void)

#ifndef WOLFSSL_MUTEX_INITIALIZER
if (inits_count_mutex_valid == 0) {
#if WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS

/* Without this mitigation, if two threads enter wolfSSL_Init() at the
* same time, and both see zero inits_count_mutex_valid, then both will
* run wc_InitMutex(&inits_count_mutex), leading to process corruption
* or (best case) a resource leak.
*
* When WOLFSSL_ATOMIC_INITIALIZER() is available, we can mitigate this
* by use an atomic counting int as a mutex.
*/

if (wolfSSL_Atomic_Int_FetchAdd(&inits_count_mutex_atomic_initing_flag,
1) != 0)
{
(void)wolfSSL_Atomic_Int_FetchSub(
&inits_count_mutex_atomic_initing_flag, 1);
return DEADLOCK_AVERTED_E;
}
#endif /* WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS */
if (wc_InitMutex(&inits_count_mutex) != 0) {
WOLFSSL_MSG("Bad Init Mutex count");
#if WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
(void)wolfSSL_Atomic_Int_FetchSub(
&inits_count_mutex_atomic_initing_flag, 1);
#endif
return BAD_MUTEX_E;
}
else {
Expand Down Expand Up @@ -10425,7 +10484,8 @@ int wolfSSL_Cleanup(void)
#endif
#endif /* !NO_SESSION_CACHE */

#ifndef WOLFSSL_MUTEX_INITIALIZER
#if !defined(WOLFSSL_MUTEX_INITIALIZER) && \
!WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
if ((inits_count_mutex_valid == 1) &&
(wc_FreeMutex(&inits_count_mutex) != 0)) {
if (ret == WOLFSSL_SUCCESS)
Expand Down Expand Up @@ -14577,19 +14637,19 @@ const char* wolfSSL_get_curve_name(WOLFSSL* ssl)
case WOLFSSL_P521_ML_KEM_1024:
return "P521_ML_KEM_1024";
#elif defined(WOLFSSL_WC_KYBER)
#ifdef WOLFSSL_WC_ML_KEM_512
#ifndef WOLFSSL_NO_ML_KEM_512
case WOLFSSL_ML_KEM_512:
return "ML_KEM_512";
case WOLFSSL_P256_ML_KEM_512:
return "P256_ML_KEM_512";
#endif
#ifdef WOLFSSL_WC_ML_KEM_768
#ifndef WOLFSSL_NO_ML_KEM_768
case WOLFSSL_ML_KEM_768:
return "ML_KEM_768";
case WOLFSSL_P384_ML_KEM_768:
return "P384_ML_KEM_768";
#endif
#ifdef WOLFSSL_WC_ML_KEM_1024
#ifndef WOLFSSL_NO_ML_KEM_1024
case WOLFSSL_ML_KEM_1024:
return "ML_KEM_1024";
case WOLFSSL_P521_ML_KEM_1024:
Expand All @@ -14612,19 +14672,19 @@ const char* wolfSSL_get_curve_name(WOLFSSL* ssl)
case WOLFSSL_P521_KYBER_LEVEL5:
return "P521_KYBER_LEVEL5";
#elif defined(WOLFSSL_WC_KYBER)
#ifdef WOLFSSL_KYBER512
#ifndef WOLFSSL_NO_KYBER512
case WOLFSSL_KYBER_LEVEL1:
return "KYBER_LEVEL1";
case WOLFSSL_P256_KYBER_LEVEL1:
return "P256_KYBER_LEVEL1";
#endif
#ifdef WOLFSSL_KYBER768
#ifndef WOLFSSL_NO_KYBER768
case WOLFSSL_KYBER_LEVEL3:
return "KYBER_LEVEL3";
case WOLFSSL_P384_KYBER_LEVEL3:
return "P384_KYBER_LEVEL3";
#endif
#ifdef WOLFSSL_KYBER1024
#ifndef WOLFSSL_NO_KYBER1024
case WOLFSSL_KYBER_LEVEL5:
return "KYBER_LEVEL5";
case WOLFSSL_P521_KYBER_LEVEL5:
Expand Down Expand Up @@ -15734,11 +15794,11 @@ int wolfSSL_ERR_GET_REASON(unsigned long err)
return ASN1_R_HEADER_TOO_LONG;
#endif

/* check if error value is in range of wolfSSL errors */
/* check if error value is in range of wolfCrypt or wolfSSL errors */
ret = 0 - ret; /* setting as negative value */
/* wolfCrypt range is less than MAX (-100)
wolfSSL range is MIN (-300) and lower */
if ((ret <= WC_FIRST_E && ret >= WC_LAST_E) ||

if ((ret <= WC_SPAN1_FIRST_E && ret >= WC_SPAN1_LAST_E) ||
(ret <= WC_SPAN2_FIRST_E && ret >= WC_SPAN2_LAST_E) ||
(ret <= WOLFSSL_FIRST_E && ret >= WOLFSSL_LAST_E))
{
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/ssl_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ static WOLFSSL_ASN1_STRING* d2i_ASN1_STRING(WOLFSSL_ASN1_STRING** out,
byte tag = 0;
int length = 0;

WOLFSSL_ENTER("d2i_ASN1_GENERALSTRING");
WOLFSSL_ENTER("d2i_ASN1_STRING");

if (src == NULL || *src == NULL || len == 0)
return NULL;
Expand Down
Loading

0 comments on commit 584da13

Please sign in to comment.