Skip to content

Commit

Permalink
add second wolfCrypt error code span, and add DEADLOCK_AVERTED_E.
Browse files Browse the repository at this point in the history
  • Loading branch information
douzzer committed Nov 13, 2024
1 parent 524f0f5 commit 28430c5
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -25672,7 +25672,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_LAST_E) ||
(error <= WC_SPAN2_FIRST_E && error >= WC_SPAN2_LAST_E))
{
return wc_GetErrorString(error);
}

Expand All @@ -25684,7 +25686,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
10 changes: 6 additions & 4 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5659,12 +5659,13 @@ static int wolfSSL_RAND_InitMutex(void);

#if WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS
#ifndef WOLFSSL_ATOMIC_OPS
#error WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS with !WOLFSSL_MUTEX_INITIALIZER requires 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 with !WOLFSSL_MUTEX_INITIALIZER requires WOLFSSL_ATOMIC_INITIALIZER
#error WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS requires WOLFSSL_ATOMIC_INITIALIZER
#endif
static wolfSSL_Atomic_Int inits_count_mutex_valid2 = WOLFSSL_ATOMIC_INITIALIZER(0);
static wolfSSL_Atomic_Int inits_count_mutex_valid2 =
WOLFSSL_ATOMIC_INITIALIZER(0);
#endif /* WOLFSSL_CLEANUP_THREADSAFE_BY_ATOMIC_OPS && !WOLFSSL_MUTEX_INITIALIZER */

#if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
Expand Down Expand Up @@ -15776,7 +15777,8 @@ int wolfSSL_ERR_GET_REASON(unsigned long err)
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
10 changes: 6 additions & 4 deletions support/gen-debug-trace-error-codes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ BEGIN {
if ((errcode_a[1] == "MIN_CODE_E") ||
(errcode_a[1] == "MAX_CODE_E") ||
(errcode_a[1] == "WC_FIRST_E") ||
(errcode_a[1] == "WC_LAST_E") ||
(errcode_a[1] == "WOLFSSL_FIRST_E") ||
(errcode_a[1] == "WOLFSSL_LAST_E"))
(errcode_a[1] ~ "WC.*MIN_CODE_E") ||
(errcode_a[1] ~ "WC.*MAX_CODE_E") ||
(errcode_a[1] ~ "WC.*_FIRST_E") ||
(errcode_a[1] ~ "WC.*_LAST_E") ||
(errcode_a[1] ~ "WOLFSSL.*_FIRST_E") ||
(errcode_a[1] ~ "WOLFSSL.*_LAST_E"))
{
next;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -89866,9 +89866,9 @@ static int error_test(void)
{ -15, -17 },
{ -19, -19 },
{ -26, -27 },
{ -30, WC_FIRST_E+1 },
{ -30, WC_SPAN1_FIRST_E + 1 },
#else
{ -9, WC_FIRST_E+1 },
{ -9, WC_SPAN1_FIRST_E + 1 },
#endif
{ -124, -124 },
{ -166, -169 },
Expand All @@ -89879,14 +89879,15 @@ static int error_test(void)
{ -358, -358 },
{ -384, -384 },
{ -466, -499 },
{ WOLFSSL_LAST_E-1, WOLFSSL_LAST_E-1 }
{ WOLFSSL_LAST_E - 1, WC_SPAN2_FIRST_E + 1 },
{ WC_SPAN2_LAST_E - 1, WC_SPAN2_MIN_CODE_E }
};

/* Check that all errors have a string and it's the same through the two
* APIs. Check that the values that are not errors map to the unknown
* string.
*/
for (i = 0; i >= WOLFSSL_LAST_E-1; i--) {
for (i = 0; i >= WC_SPAN2_MIN_CODE_E; i--) {
int this_missing = 0;
for (j = 0; j < (int)XELEM_CNT(missing); ++j) {
if ((i <= missing[j].first) && (i >= missing[j].last)) {
Expand Down Expand Up @@ -89948,8 +89949,7 @@ static int test_wolfSSL_ERR_strings(void)
ExpectNotNull(err = wolfSSL_ERR_func_error_string(WC_NO_ERR_TRACE((word32)UNSUPPORTED_SUITE)));
ExpectIntEQ((*err == '\0'), 1);

/* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-WOLFSSL_PEM_R_PROBLEMS_GETTING_PASSWORD_E));
ExpectIntEQ((*err == '\0'), 1);
#endif
#endif
Expand Down
4 changes: 4 additions & 0 deletions wolfcrypt/src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,11 @@ const char* wc_GetErrorString(int error)
case PBKDF2_KAT_FIPS_E:
return "wolfCrypt FIPS PBKDF2 Known Answer Test Failure";

case DEADLOCK_AVERTED_E:
return "Deadlock averted -- retry the call";

case MAX_CODE_E:
case WC_SPAN1_MIN_CODE_E:
case MIN_CODE_E:
default:
return "unknown error number";
Expand Down
6 changes: 4 additions & 2 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2777,14 +2777,16 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
int last;
} missing[] = {
{ -124, -124 },
{ -166, -169 }
{ -166, -169 },
{ WC_SPAN1_LAST_E - 1, WC_SPAN2_FIRST_E + 1 },
{ WC_SPAN2_LAST_E - 1, WC_SPAN2_MIN_CODE_E }
};

/* Check that all errors have a string and it's the same through the two
* APIs. Check that the values that are not errors map to the unknown
* string.
*/
for (i = WC_FIRST_E; i >= WC_LAST_E; i--) {
for (i = WC_SPAN1_FIRST_E; i >= WC_SPAN2_MIN_CODE_E; i--) {
int this_missing = 0;
for (j = 0; j < (int)XELEM_CNT(missing); ++j) {
if ((i <= missing[j].first) && (i >= missing[j].last)) {
Expand Down
4 changes: 4 additions & 0 deletions wolfssl/error-ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ enum wolfSSL_ErrorCodes {
WOLFSSL_EVP_R_PRIVATE_KEY_DECODE_ERROR = -515,

WOLFSSL_LAST_E = -515

/* codes -1000 to -1999 are reserved for wolfCrypt. */
};

wc_static_assert((int)WC_LAST_E <= (int)WOLFSSL_LAST_E);

/* I/O Callback default errors */
enum IOerrors {
WOLFSSL_CBIO_ERR_GENERAL = -1, /* general unexpected err */
Expand Down
25 changes: 21 additions & 4 deletions wolfssl/wolfcrypt/error-crypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ enum wolfCrypt_ErrorCodes {
* reasons of backward compatibility.
*/

MAX_CODE_E = -96, /* errors -97 - -299 */
WC_FIRST_E = -97, /* errors -97 - -299 */
MAX_CODE_E = -96, /* WC_FIRST_E + 1, for backward compat. */
WC_FIRST_E = -97, /* First code used for wolfCrypt */

WC_SPAN1_FIRST_E = -97, /* errors -97 - -300 */

MP_MEM = -97, /* MP dynamic memory allocation failed. */
MP_VAL = -98, /* MP value passed is not able to be used. */
Expand Down Expand Up @@ -290,8 +292,23 @@ enum wolfCrypt_ErrorCodes {
SM4_GCM_AUTH_E = -298, /* SM4-GCM Authentication check failure */
SM4_CCM_AUTH_E = -299, /* SM4-CCM Authentication check failure */

WC_LAST_E = -299, /* Update this to indicate last error */
MIN_CODE_E = -300 /* errors -2 - -299 */
WC_SPAN1_LAST_E = -299, /* Last used code in span 1 */
WC_SPAN1_MIN_CODE_E = -300, /* Last usable code in span 1 */

WC_SPAN2_FIRST_E = -1000,

DEADLOCK_AVERTED_E = -1000, /* Deadlock averted -- retry the call */

WC_SPAN2_LAST_E = -1000, /* Update to indicate last used error code */
WC_SPAN2_MIN_CODE_E = -1999, /* Last usable code in span 2 */

WC_LAST_E = -1000, /* the last code used either here or in
* error-ssl.h
*/

MIN_CODE_E = -1999 /* the last code allocated either here or in
* error-ssl.h
*/

/* add new companion error id strings for any new error codes
wolfcrypt/src/error.c !!! */
Expand Down

0 comments on commit 28430c5

Please sign in to comment.