1
1
/*
2
- * Copyright (c) 2003, 2023 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
*/
4
4
5
5
/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
@@ -103,7 +103,6 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
103
103
/*
104
104
* Load the PKCS #11 DLL
105
105
*/
106
- dlerror (); /* clear any old error message not fetched */
107
106
#ifdef DEBUG
108
107
hModule = dlopen (libraryNameStr , RTLD_NOW );
109
108
#else
@@ -124,9 +123,6 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
124
123
goto cleanup ;
125
124
}
126
125
127
- // clear any old error message not fetched
128
- dlerror ();
129
-
130
126
#ifdef DEBUG
131
127
C_GetInterfaceList = (CK_C_GetInterfaceList ) dlsym (hModule ,
132
128
"C_GetInterfaceList" );
@@ -158,47 +154,42 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
158
154
}
159
155
#endif
160
156
161
- if (jGetFunctionList != NULL ) {
157
+ // if none specified, then we try 3.0 API first before trying 2.40
158
+ if (jGetFunctionList == NULL ) {
159
+ C_GetInterface = (CK_C_GetInterface ) dlsym (hModule , "C_GetInterface" );
160
+ if (C_GetInterface != NULL ) {
161
+ TRACE0 ("Connect: Found C_GetInterface func\n" );
162
+ rv = (C_GetInterface )(NULL , NULL , & interface , 0L );
163
+ // don't use ckAssertReturnValueOK as we want to continue trying
164
+ // C_GetFunctionList() or method named by "getFunctionListStr"
165
+ if (rv == CKR_OK ) {
166
+ goto setModuleData ;
167
+ }
168
+ }
169
+ getFunctionListStr = "C_GetFunctionList" ;
170
+ } else {
162
171
getFunctionListStr = (* env )-> GetStringUTFChars (env ,
163
172
jGetFunctionList , 0 );
164
173
if (getFunctionListStr == NULL ) {
165
174
goto cleanup ;
166
175
}
167
- C_GetFunctionList = (CK_C_GetFunctionList ) dlsym (hModule ,
176
+ }
177
+
178
+ dlerror (); // clear any old error message not fetched
179
+ C_GetFunctionList = (CK_C_GetFunctionList ) dlsym (hModule ,
168
180
getFunctionListStr );
181
+ if (C_GetFunctionList == NULL ) {
169
182
if ((systemErrorMessage = dlerror ()) != NULL ){
183
+ TRACE2 ("Connect: error finding %s func: %s\n" , getFunctionListStr ,
184
+ systemErrorMessage );
170
185
p11ThrowIOException (env , systemErrorMessage );
171
- goto cleanup ;
172
- }
173
- if (C_GetFunctionList == NULL ) {
186
+ } else {
174
187
TRACE1 ("Connect: No %s func\n" , getFunctionListStr );
175
188
p11ThrowIOException (env , "ERROR: C_GetFunctionList == NULL" );
176
- goto cleanup ;
177
189
}
178
- TRACE1 ("Connect: Found %s func\n" , getFunctionListStr );
179
- } else {
180
- // if none specified, then we try 3.0 API first before trying 2.40
181
- C_GetInterface = (CK_C_GetInterface ) dlsym (hModule , "C_GetInterface" );
182
- if ((C_GetInterface != NULL ) && (dlerror () == NULL )) {
183
- TRACE0 ("Connect: Found C_GetInterface func\n" );
184
- rv = (C_GetInterface )(NULL , NULL , & interface , 0L );
185
- if (ckAssertReturnValueOK (env , rv ) == CK_ASSERT_OK ) {
186
- goto setModuleData ;
187
- }
188
- }
189
- C_GetFunctionList = (CK_C_GetFunctionList ) dlsym (hModule ,
190
- "C_GetFunctionList" );
191
- if ((systemErrorMessage = dlerror ()) != NULL ){
192
- p11ThrowIOException (env , systemErrorMessage );
193
- goto cleanup ;
194
- }
195
- if (C_GetFunctionList == NULL ) {
196
- TRACE0 ("Connect: No C_GetFunctionList func\n" );
197
- p11ThrowIOException (env , "ERROR: C_GetFunctionList == NULL" );
198
- goto cleanup ;
199
- }
200
- TRACE0 ("Connect: Found C_GetFunctionList func\n" );
190
+ goto cleanup ;
201
191
}
192
+ TRACE1 ("Connect: Found %s func\n" , getFunctionListStr );
202
193
203
194
setModuleData :
204
195
/*
0 commit comments