4141
4242#ifndef HAVE_PKCS11_STATIC
4343#include <dlfcn.h>
44+ static void * dlib = NULL ;
45+ void (* wolfPKCS11_Debugging_On_fp )(void ) = NULL ;
46+ void (* wolfPKCS11_Debugging_Off_fp )(void ) = NULL ;
4447#endif
4548
49+ static CK_FUNCTION_LIST_PTR pFunctionList = NULL ;
50+
4651#ifdef DEBUG_WOLFPKCS11
4752static FILE * original_stdout = NULL ;
4853static FILE * capture_file = NULL ;
@@ -80,6 +85,67 @@ static int check_debug_output(void)
8085 fclose (capture_file );
8186 return found_debug ;
8287}
88+
89+ /* Wrapper functions for debugging */
90+ static void call_wolfPKCS11_Debugging_On (void ) {
91+ #ifndef HAVE_PKCS11_STATIC
92+ if (wolfPKCS11_Debugging_On_fp != NULL ) {
93+ wolfPKCS11_Debugging_On_fp ();
94+ }
95+ #else
96+ wolfPKCS11_Debugging_On ();
97+ #endif
98+ }
99+
100+ static void call_wolfPKCS11_Debugging_Off (void ) {
101+ #ifndef HAVE_PKCS11_STATIC
102+ if (wolfPKCS11_Debugging_Off_fp != NULL ) {
103+ wolfPKCS11_Debugging_Off_fp ();
104+ }
105+ #else
106+ wolfPKCS11_Debugging_Off ();
107+ #endif
108+ }
109+
110+ static CK_RV debug_init (const char * library ) {
111+ CK_RV ret = CKR_OK ;
112+ #ifndef HAVE_PKCS11_STATIC
113+ void * func ;
114+
115+ dlib = dlopen (library , RTLD_NOW | RTLD_LOCAL );
116+ if (dlib == NULL ) {
117+ fprintf (stderr , "dlopen error: %s\n" , dlerror ());
118+ return -1 ;
119+ }
120+
121+ func = (void * )(CK_C_GetFunctionList )dlsym (dlib , "C_GetFunctionList" );
122+ if (func == NULL ) {
123+ fprintf (stderr , "Failed to get function list function\n" );
124+ dlclose (dlib );
125+ return -1 ;
126+ }
127+
128+ wolfPKCS11_Debugging_On_fp = (void (* )(void ))dlsym (dlib ,
129+ "wolfPKCS11_Debugging_On" );
130+ wolfPKCS11_Debugging_Off_fp = (void (* )(void ))dlsym (dlib ,
131+ "wolfPKCS11_Debugging_Off" );
132+
133+ ret = ((CK_C_GetFunctionList )func )(& pFunctionList );
134+ #else
135+ ret = C_GetFunctionList (& pFunctionList );
136+ (void )library ;
137+ #endif
138+ return ret ;
139+ }
140+
141+ static void debug_cleanup (void ) {
142+ #ifndef HAVE_PKCS11_STATIC
143+ if (dlib ) {
144+ dlclose (dlib );
145+ dlib = NULL ;
146+ }
147+ #endif
148+ }
83149#endif
84150
85151int main (void )
@@ -90,8 +156,8 @@ int main(void)
90156 return 77 ;
91157#else
92158 CK_RV rv ;
93- CK_FUNCTION_LIST_PTR pFunctionList ;
94159 int debug_found ;
160+ const char * library = "./src/.libs/libwolfpkcs11.so" ;
95161
96162#ifndef WOLFPKCS11_NO_ENV
97163 if (!XGETENV ("WOLFPKCS11_TOKEN_PATH" )) {
@@ -102,22 +168,27 @@ int main(void)
102168 printf ("=== wolfPKCS11 Debug Test Program ===\n" );
103169 printf ("Debug mode is ENABLED (DEBUG_WOLFPKCS11 defined)\n" );
104170
171+ printf ("\nInitializing library:\n" );
172+ rv = debug_init (library );
173+ if (rv != CKR_OK ) {
174+ printf ("Failed to initialize library: %lu\n" , (unsigned long )rv );
175+ return 1 ;
176+ }
177+
105178 printf ("\nTesting debug control functions:\n" );
106- wolfPKCS11_Debugging_On ();
179+ call_wolfPKCS11_Debugging_On ();
107180 printf ("Debug enabled\n" );
108181
109- wolfPKCS11_Debugging_Off ();
182+ call_wolfPKCS11_Debugging_Off ();
110183 printf ("Debug disabled\n" );
111184
112- wolfPKCS11_Debugging_On ();
185+ call_wolfPKCS11_Debugging_On ();
113186 printf ("Debug re-enabled\n" );
114187
115188 printf ("\nTesting PKCS#11 functions with debug output capture:\n" );
116189
117190 setup_output_capture ();
118191
119- rv = C_GetFunctionList (& pFunctionList );
120-
121192 if (rv == CKR_OK && pFunctionList != NULL ) {
122193 rv = pFunctionList -> C_Initialize (NULL );
123194
@@ -133,13 +204,15 @@ int main(void)
133204 printf ("C_GetFunctionList returned: %lu\n" , (unsigned long )rv );
134205 printf ("Debug output detection: %s\n" , debug_found ? "PASS" : "FAIL" );
135206
136- wolfPKCS11_Debugging_Off ();
207+ call_wolfPKCS11_Debugging_Off ();
137208 printf ("Debug disabled at end\n" );
138209
210+ debug_cleanup ();
139211 printf ("\n=== Test Complete ===\n" );
140212
141213 if (!debug_found ) {
142- printf ("ERROR: No debug output was detected during PKCS#11 function calls\n" );
214+ printf ("ERROR: No debug output was detected during "
215+ "PKCS#11 function calls\n" );
143216 return 1 ;
144217 }
145218
0 commit comments