@@ -130,7 +130,7 @@ static int UTIL_CTX_enumerate_slots_unlocked(UTIL_CTX *ctx)
130130 UTIL_CTX_log (ctx , LOG_ERR , "Failed to enumerate slots\n" );
131131 return 0 ;
132132 }
133- if (ctx -> slot_count < 1 ) {
133+ if (! ctx -> slot_list || ctx -> slot_count < 1 ) {
134134 UTIL_CTX_log (ctx , LOG_ERR , "No slot found\n" );
135135 return 0 ;
136136 }
@@ -156,28 +156,27 @@ int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx)
156156
157157int UTIL_CTX_init_libp11 (UTIL_CTX * ctx )
158158{
159- PKCS11_CTX * pkcs11_ctx ;
160-
161- if (ctx -> pkcs11_ctx && ctx -> slot_list )
159+ if (ctx -> pkcs11_ctx && ctx -> slot_list && ctx -> slot_count > 0 )
162160 return 0 ;
163161
164162 UTIL_CTX_log (ctx , LOG_NOTICE , "PKCS#11: Initializing the module: %s\n" , ctx -> module );
165163
166- pkcs11_ctx = PKCS11_CTX_new ();
167- PKCS11_set_vlog_a_method (pkcs11_ctx , ctx -> vlog );
168- PKCS11_CTX_init_args (pkcs11_ctx , ctx -> init_args );
169- PKCS11_set_ui_method (pkcs11_ctx , ctx -> ui_method , ctx -> ui_data );
170- if (PKCS11_CTX_load (pkcs11_ctx , ctx -> module ) < 0 ) {
164+ ctx -> pkcs11_ctx = PKCS11_CTX_new ();
165+ if (!ctx -> pkcs11_ctx )
166+ return -1 ;
167+ PKCS11_set_vlog_a_method (ctx -> pkcs11_ctx , ctx -> vlog );
168+ PKCS11_CTX_init_args (ctx -> pkcs11_ctx , ctx -> init_args );
169+ PKCS11_set_ui_method (ctx -> pkcs11_ctx , ctx -> ui_method , ctx -> ui_data );
170+ if (PKCS11_CTX_load (ctx -> pkcs11_ctx , ctx -> module ) < 0 ) {
171171 UTIL_CTX_log (ctx , LOG_ERR , "Unable to load module %s\n" , ctx -> module );
172- PKCS11_CTX_free ( pkcs11_ctx );
172+ UTIL_CTX_free_libp11 ( ctx );
173173 return -1 ;
174174 }
175- ctx -> pkcs11_ctx = pkcs11_ctx ;
176-
177- if (UTIL_CTX_enumerate_slots_unlocked (ctx ) != 1 )
175+ if (UTIL_CTX_enumerate_slots_unlocked (ctx ) != 1 ) {
176+ UTIL_CTX_free_libp11 (ctx );
178177 return -1 ;
179-
180- return ctx -> pkcs11_ctx && ctx -> slot_list ? 0 : -1 ;
178+ }
179+ return 0 ;
181180}
182181
183182void UTIL_CTX_free_libp11 (UTIL_CTX * ctx )
0 commit comments