Skip to content

Commit 10a60fc

Browse files
authored
Merge pull request #9427 from douzzer/20251113-ZD20815
20251113-ZD20815
2 parents 6ff57b8 + 7916db7 commit 10a60fc

File tree

5 files changed

+112
-41
lines changed

5 files changed

+112
-41
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ __ARCH_STRSTR_NO_REDIRECT
969969
__ARM_ARCH_7M__
970970
__ARM_FEATURE_CRYPTO
971971
__ASSEMBLER__
972+
__ATOMIC_CONSUME
972973
__ATOMIC_RELAXED
973974
__AVR_ARCH__
974975
__AVR__

src/ssl.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7559,7 +7559,7 @@ int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx)
75597559
* Return the private key of the WOLFSSL_CTX struct
75607560
* @return WOLFSSL_EVP_PKEY* The caller doesn *NOT*` free the returned object.
75617561
*/
7562-
WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx)
7562+
WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx)
75637563
{
75647564
WOLFSSL_EVP_PKEY* res;
75657565
const unsigned char *key;
@@ -7605,12 +7605,25 @@ WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx)
76057605
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
76067606
wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
76077607
#endif
7608-
res = wolfSSL_d2i_PrivateKey(type,
7609-
(WOLFSSL_EVP_PKEY**)&ctx->privateKeyPKey, &key,
7608+
res = wolfSSL_d2i_PrivateKey(type, NULL, &key,
76107609
(long)ctx->privateKey->length);
76117610
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
76127611
wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
76137612
#endif
7613+
if (res) {
7614+
#ifdef WOLFSSL_ATOMIC_OPS
7615+
WOLFSSL_EVP_PKEY *current_pkey = NULL;
7616+
if (! wolfSSL_Atomic_Ptr_CompareExchange(
7617+
(void **)&ctx->privateKeyPKey,
7618+
(void **)&current_pkey, res))
7619+
{
7620+
wolfSSL_EVP_PKEY_free(res);
7621+
res = current_pkey;
7622+
}
7623+
#else
7624+
ctx->privateKeyPKey = res;
7625+
#endif
7626+
}
76147627
}
76157628

76167629
return res;

wolfcrypt/src/wc_port.c

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,12 +1271,15 @@ char* wc_strdup_ex(const char *src, int memType) {
12711271
}
12721272
#endif
12731273

1274-
#if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
1274+
#ifdef WOLFSSL_ATOMIC_OPS
1275+
1276+
#if defined(WOLFSSL_USER_DEFINED_ATOMICS)
1277+
1278+
#elif defined(SINGLE_THREADED)
1279+
1280+
#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
1281+
/* direct calls using gcc-style compiler built-ins */
12751282

1276-
#ifdef HAVE_C___ATOMIC
1277-
/* Atomic ops using standard C lib */
1278-
#ifdef __cplusplus
1279-
/* C++ using direct calls to compiler built-in functions */
12801283
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
12811284
{
12821285
*c = i;
@@ -1357,7 +1360,16 @@ int wolfSSL_Atomic_Uint_CompareExchange(
13571360
c, expected_i, new_i, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
13581361
}
13591362

1360-
#else
1363+
int wolfSSL_Atomic_Ptr_CompareExchange(
1364+
void **c, void **expected_ptr, void *new_ptr)
1365+
{
1366+
return __atomic_compare_exchange_n(
1367+
c, expected_ptr, new_ptr, 0 /* weak */,
1368+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1369+
}
1370+
1371+
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \
1372+
!defined(__cplusplus)
13611373

13621374
/* Default C Implementation */
13631375
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
@@ -1444,9 +1456,19 @@ int wolfSSL_Atomic_Uint_CompareExchange(
14441456
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
14451457
}
14461458

1447-
#endif /* __cplusplus */
1459+
int wolfSSL_Atomic_Ptr_CompareExchange(
1460+
void **c, void **expected_ptr, void *new_ptr)
1461+
{
1462+
/* use gcc-built-in __atomic_compare_exchange_n(), not
1463+
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1464+
* requirements.
1465+
*/
1466+
return __atomic_compare_exchange_n(
1467+
c, expected_ptr, new_ptr, 0 /* weak */,
1468+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1469+
}
14481470

1449-
#elif defined(_MSC_VER)
1471+
#elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)
14501472

14511473
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
14521474
{
@@ -1527,8 +1549,8 @@ unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
15271549
int wolfSSL_Atomic_Uint_CompareExchange(
15281550
wolfSSL_Atomic_Uint* c, unsigned int *expected_i, unsigned int new_i)
15291551
{
1530-
long actual_i = InterlockedCompareExchange
1531-
((wolfSSL_Atomic_Int *)c, (long)new_i, (long)*expected_i);
1552+
long actual_i = InterlockedCompareExchange(
1553+
(wolfSSL_Atomic_Int *)c, (long)new_i, (long)*expected_i);
15321554
if (actual_i == (long)*expected_i) {
15331555
return 1;
15341556
}
@@ -1538,6 +1560,32 @@ int wolfSSL_Atomic_Uint_CompareExchange(
15381560
}
15391561
}
15401562

1563+
int wolfSSL_Atomic_Ptr_CompareExchange(
1564+
void ** c, void **expected_ptr, void *new_ptr)
1565+
{
1566+
#ifdef _WIN64
1567+
LONG64 actual_ptr = InterlockedCompareExchange64(
1568+
(LONG64 *)c, (LONG64)new_ptr, (LONG64)*expected_ptr);
1569+
if (actual_ptr == (LONG64)*expected_ptr) {
1570+
return 1;
1571+
}
1572+
else {
1573+
*expected_ptr = (void *)actual_ptr;
1574+
return 0;
1575+
}
1576+
#else /* !_WIN64 */
1577+
LONG actual_ptr = InterlockedCompareExchange(
1578+
(LONG *)c, (LONG)new_ptr, (LONG)*expected_ptr);
1579+
if (actual_ptr == (LONG)*expected_ptr) {
1580+
return 1;
1581+
}
1582+
else {
1583+
*expected_ptr = (void *)actual_ptr;
1584+
return 0;
1585+
}
1586+
#endif /* !_WIN64 */
1587+
}
1588+
15411589
#endif
15421590

15431591
#endif /* WOLFSSL_ATOMIC_OPS */

wolfssl/ssl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3228,7 +3228,7 @@ WOLFSSL_API int wolfSSL_want_write(WOLFSSL* ssl);
32283228
#ifdef OPENSSL_EXTRA
32293229
WOLFSSL_API int wolfSSL_want(WOLFSSL* ssl);
32303230

3231-
WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx);
3231+
WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx);
32323232

32333233
#include <stdarg.h> /* var_arg */
32343234
WOLFSSL_API int wolfSSL_BIO_vprintf(WOLFSSL_BIO* bio, const char* format,

wolfssl/wolfcrypt/wc_port.h

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -494,31 +494,26 @@
494494
#define WOLFSSL_ATOMIC_LOAD(x) (x)
495495
#define WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)
496496
#define WOLFSSL_ATOMIC_OPS
497-
#elif defined(HAVE_C___ATOMIC)
498-
#ifdef __cplusplus
499-
#if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
500-
/* C++ using direct calls to compiler built-in functions */
501-
typedef volatile int wolfSSL_Atomic_Int;
502-
typedef volatile unsigned int wolfSSL_Atomic_Uint;
503-
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
504-
#define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), \
505-
__ATOMIC_CONSUME)
506-
#define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), \
507-
val, __ATOMIC_RELEASE)
508-
#define WOLFSSL_ATOMIC_OPS
509-
#endif
510-
#else
511-
#ifdef WOLFSSL_HAVE_ATOMIC_H
512-
/* Default C Implementation */
513-
#include <stdatomic.h>
514-
typedef atomic_int wolfSSL_Atomic_Int;
515-
typedef atomic_uint wolfSSL_Atomic_Uint;
516-
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
517-
#define WOLFSSL_ATOMIC_LOAD(x) atomic_load(&(x))
518-
#define WOLFSSL_ATOMIC_STORE(x, val) atomic_store(&(x), val)
519-
#define WOLFSSL_ATOMIC_OPS
520-
#endif /* WOLFSSL_HAVE_ATOMIC_H */
521-
#endif
497+
#elif defined(__GNUC__) && defined(__ATOMIC_CONSUME)
498+
/* direct calls using gcc-style compiler built-ins */
499+
typedef volatile int wolfSSL_Atomic_Int;
500+
typedef volatile unsigned int wolfSSL_Atomic_Uint;
501+
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
502+
#define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), \
503+
__ATOMIC_CONSUME)
504+
#define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), \
505+
val, __ATOMIC_RELEASE)
506+
#define WOLFSSL_ATOMIC_OPS
507+
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \
508+
!defined(__cplusplus)
509+
/* Default C Implementation */
510+
#include <stdatomic.h>
511+
typedef atomic_int wolfSSL_Atomic_Int;
512+
typedef atomic_uint wolfSSL_Atomic_Uint;
513+
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
514+
#define WOLFSSL_ATOMIC_LOAD(x) atomic_load(&(x))
515+
#define WOLFSSL_ATOMIC_STORE(x, val) atomic_store(&(x), val)
516+
#define WOLFSSL_ATOMIC_OPS
522517
#elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)
523518
/* Use MSVC compiler intrinsics for atomic ops */
524519
#ifdef _WIN32_WCE
@@ -534,8 +529,8 @@
534529
#define WOLFSSL_ATOMIC_OPS
535530
#endif
536531

537-
#ifndef WOLFSSL_ATOMIC_INITIALIZER
538-
/* If we weren't able to implement atomics above, disable them here. */
532+
/* If we weren't able to implement atomics above, disable them here. */
533+
#ifndef WOLFSSL_ATOMIC_OPS
539534
#define WOLFSSL_NO_ATOMICS
540535
#endif
541536
#endif
@@ -586,6 +581,8 @@
586581
wolfSSL_Atomic_Uint* c, unsigned int i);
587582
WOLFSSL_API int wolfSSL_Atomic_Uint_CompareExchange(
588583
wolfSSL_Atomic_Uint* c, unsigned int *expected_i, unsigned int new_i);
584+
WOLFSSL_API int wolfSSL_Atomic_Ptr_CompareExchange(
585+
void** c, void **expected_ptr, void *new_ptr);
589586
#else
590587
/* Code using these fallback implementations in non-SINGLE_THREADED builds
591588
* needs to arrange its own explicit fallback to int for wolfSSL_Atomic_Int
@@ -623,6 +620,18 @@
623620
return 0;
624621
}
625622
}
623+
static WC_INLINE int wolfSSL_Atomic_Ptr_CompareExchange(
624+
void **c, void *expected_ptr, void *new_ptr)
625+
{
626+
if (*(char **)c == *(char **)expected_ptr) {
627+
*(char **)c = (char *)new_ptr;
628+
return 1;
629+
}
630+
else {
631+
*(char **)expected_ptr = *(char **)c;
632+
return 0;
633+
}
634+
}
626635
static WC_INLINE unsigned int wolfSSL_Atomic_Uint_FetchAdd(
627636
unsigned int *c, unsigned int i)
628637
{

0 commit comments

Comments
 (0)