Skip to content

Commit ddf3e9e

Browse files
authored
Merge pull request #178 from haydenroche5/wolfssl_debug
2 parents 9b08099 + 3f3c480 commit ddf3e9e

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

include/wolfengine/we_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include <wolfssl/wolfcrypt/ecc.h>
8282
#include <wolfssl/wolfcrypt/random.h>
8383
#include <wolfssl/wolfcrypt/pwdbased.h>
84+
#include <wolfssl/wolfcrypt/logging.h>
8485
#ifdef HAVE_WOLFSSL_WOLFCRYPT_KDF_H
8586
#include <wolfssl/wolfcrypt/kdf.h>
8687
#endif

src/we_internal.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,13 @@ static int wolfengine_destroy(ENGINE *e)
11171117
return 1;
11181118
}
11191119

1120-
#define WOLFENGINE_CMD_ENABLE_DEBUG ENGINE_CMD_BASE
1121-
#define WOLFENGINE_CMD_SET_LOG_LEVEL (ENGINE_CMD_BASE + 1)
1122-
#define WOLFENGINE_CMD_SET_LOG_COMPONENTS (ENGINE_CMD_BASE + 2)
1123-
#define WOLFENGINE_CMD_SET_LOGGING_CB (ENGINE_CMD_BASE + 3)
1124-
#define WOLFENGINE_CMD_ENABLE_FIPS_CHECKS (ENGINE_CMD_BASE + 4)
1120+
#define WOLFENGINE_CMD_ENABLE_DEBUG ENGINE_CMD_BASE
1121+
#define WOLFENGINE_CMD_SET_LOG_LEVEL (ENGINE_CMD_BASE + 1)
1122+
#define WOLFENGINE_CMD_SET_LOG_COMPONENTS (ENGINE_CMD_BASE + 2)
1123+
#define WOLFENGINE_CMD_SET_LOGGING_CB (ENGINE_CMD_BASE + 3)
1124+
#define WOLFENGINE_CMD_ENABLE_FIPS_CHECKS (ENGINE_CMD_BASE + 4)
1125+
#define WOLFENGINE_CMD_ENABLE_DEBUG_WOLFSSL (ENGINE_CMD_BASE + 5)
1126+
#define WOLFENGINE_CMD_SET_LOGGING_CB_WOLFSSL (ENGINE_CMD_BASE + 6)
11251127

11261128
/**
11271129
* wolfEngine control command list.
@@ -1177,6 +1179,14 @@ static ENGINE_CMD_DEFN wolfengine_cmd_defns[] = {
11771179
"enable_fips_checks",
11781180
"Enable wolfEngine FIPS checks (1=enable, 0=disable)",
11791181
ENGINE_CMD_FLAG_NUMERIC },
1182+
{ WOLFENGINE_CMD_ENABLE_DEBUG_WOLFSSL,
1183+
"enable_debug_wolfssl",
1184+
"Enable wolfSSL debug logging (1=enable, 0=disable)",
1185+
ENGINE_CMD_FLAG_NUMERIC },
1186+
{ WOLFENGINE_CMD_SET_LOGGING_CB_WOLFSSL,
1187+
"set_logging_cb_wolfssl",
1188+
"Set wolfSSL logging callback",
1189+
ENGINE_CMD_FLAG_INTERNAL },
11801190

11811191
/* last element MUST be NULL/0 entry, do not remove */
11821192
{0, NULL, NULL, 0}
@@ -1224,6 +1234,16 @@ static int wolfengine_ctrl(ENGINE* e, int cmd, long i, void* p,
12241234
wolfEngine_Debugging_OFF();
12251235
}
12261236
break;
1237+
case WOLFENGINE_CMD_ENABLE_DEBUG_WOLFSSL:
1238+
if (i > 0) {
1239+
if (wolfSSL_Debugging_ON() < 0) {
1240+
ret = 0;
1241+
}
1242+
}
1243+
else {
1244+
wolfSSL_Debugging_OFF();
1245+
}
1246+
break;
12271247
case WOLFENGINE_CMD_SET_LOG_LEVEL:
12281248
if (wolfEngine_SetLogLevel((int)i) < 0) {
12291249
WOLFENGINE_ERROR_MSG(WE_LOG_ENGINE,
@@ -1250,6 +1270,18 @@ static int wolfengine_ctrl(ENGINE* e, int cmd, long i, void* p,
12501270
"wolfEngine user logging callback registered");
12511271
}
12521272
break;
1273+
case WOLFENGINE_CMD_SET_LOGGING_CB_WOLFSSL:
1274+
/* if f is NULL, resets logging back to default */
1275+
if (wolfSSL_SetLoggingCb((wolfSSL_Logging_cb)f) != 0) {
1276+
WOLFENGINE_ERROR_MSG(WE_LOG_ENGINE,
1277+
"Error registering wolfSSL logging callback");
1278+
ret = 0;
1279+
}
1280+
else {
1281+
WOLFENGINE_MSG(WE_LOG_ENGINE,
1282+
"wolfSSL user logging callback registered");
1283+
}
1284+
break;
12531285
case WOLFENGINE_CMD_ENABLE_FIPS_CHECKS:
12541286
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
12551287
wolfEngine_SetFipsChecks(i);

test/test_logging.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ static void my_Logging_cb(const int logLevel, const int component,
3636
log_cnt++;
3737
}
3838

39+
static void my_wolfSSL_log_cb(const int logLevel, const char* const logMessage)
40+
{
41+
(void)logLevel;
42+
(void)logMessage;
43+
log_cnt++;
44+
}
45+
3946
/******************************************************************************/
4047

4148
int test_logging(ENGINE *e, void *data)
@@ -72,6 +79,21 @@ int test_logging(ENGINE *e, void *data)
7279
}
7380
#endif
7481

82+
/* test enabling wolfSSL debug logging */
83+
PRINT_MSG("Enable wolfSSL debug logging");
84+
ret = ENGINE_ctrl_cmd(e, "enable_debug_wolfssl", 1, NULL, NULL, 0);
85+
#ifdef DEBUG_WOLFSSL
86+
if (ret != 1) {
87+
PRINT_ERR_MSG("Failed to enable wolfSSL debug logging");
88+
err = 1;
89+
}
90+
#else
91+
if (ret != 0) {
92+
PRINT_ERR_MSG("Allowed to enable wolfSSL debug when not compiled in");
93+
err = 1;
94+
}
95+
#endif
96+
7597
/* test setting logging level */
7698
PRINT_MSG("Set logging level");
7799
ret = ENGINE_ctrl_cmd(e, "log_level", defaultLogLevel, NULL, NULL, 0);
@@ -103,6 +125,23 @@ int test_logging(ENGINE *e, void *data)
103125
}
104126
#endif
105127

128+
/* test registering logging callback */
129+
PRINT_MSG("Set wolfSSL logging callback");
130+
ret = ENGINE_ctrl_cmd(e, "set_logging_cb_wolfssl", 0, NULL,
131+
(void(*)(void))my_wolfSSL_log_cb, 0);
132+
#ifdef DEBUG_WOLFSSL
133+
if (ret != 1) {
134+
PRINT_ERR_MSG("Failed to set wolfSSL logging callback");
135+
err = 1;
136+
}
137+
#else
138+
if (ret != 0) {
139+
PRINT_ERR_MSG("Allowed to register wolfSSL debug cb when not compiled"
140+
" in");
141+
err = 1;
142+
}
143+
#endif
144+
106145
/* force a few logs to print, if debug has been enabled */
107146
WOLFENGINE_MSG(WE_LOG_ENGINE, msg);
108147
WOLFENGINE_ERROR(WE_LOG_ENGINE, -1);

0 commit comments

Comments
 (0)