From 6276789fb5e283c11ac73602c2a5f646eab96975 Mon Sep 17 00:00:00 2001 From: Valerie Peng Date: Tue, 9 Apr 2024 21:51:51 +0000 Subject: [PATCH] 8328785: IOException: Symbol not found: C_GetInterface for PKCS11 interface prior to V3.0 Reviewed-by: djelinski, weijun --- .../unix/native/libj2pkcs11/p11_md.c | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/p11_md.c b/src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/p11_md.c index 85b9cad03a7..099ef00c6d9 100644 --- a/src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/p11_md.c +++ b/src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/p11_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 2002 Graz University of Technology. All rights reserved. @@ -103,7 +103,6 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect /* * Load the PKCS #11 DLL */ - dlerror(); /* clear any old error message not fetched */ #ifdef DEBUG hModule = dlopen(libraryNameStr, RTLD_NOW); #else @@ -124,9 +123,6 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect goto cleanup; } - // clear any old error message not fetched - dlerror(); - #ifdef DEBUG C_GetInterfaceList = (CK_C_GetInterfaceList) dlsym(hModule, "C_GetInterfaceList"); @@ -158,47 +154,42 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect } #endif - if (jGetFunctionList != NULL) { + // if none specified, then we try 3.0 API first before trying 2.40 + if (jGetFunctionList == NULL) { + C_GetInterface = (CK_C_GetInterface) dlsym(hModule, "C_GetInterface"); + if (C_GetInterface != NULL) { + TRACE0("Connect: Found C_GetInterface func\n"); + rv = (C_GetInterface)(NULL, NULL, &interface, 0L); + // don't use ckAssertReturnValueOK as we want to continue trying + // C_GetFunctionList() or method named by "getFunctionListStr" + if (rv == CKR_OK) { + goto setModuleData; + } + } + getFunctionListStr = "C_GetFunctionList"; + } else { getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0); if (getFunctionListStr == NULL) { goto cleanup; } - C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, + } + + dlerror(); // clear any old error message not fetched + C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, getFunctionListStr); + if (C_GetFunctionList == NULL) { if ((systemErrorMessage = dlerror()) != NULL){ + TRACE2("Connect: error finding %s func: %s\n", getFunctionListStr, + systemErrorMessage); p11ThrowIOException(env, systemErrorMessage); - goto cleanup; - } - if (C_GetFunctionList == NULL) { + } else { TRACE1("Connect: No %s func\n", getFunctionListStr); p11ThrowIOException(env, "ERROR: C_GetFunctionList == NULL"); - goto cleanup; } - TRACE1("Connect: Found %s func\n", getFunctionListStr); - } else { - // if none specified, then we try 3.0 API first before trying 2.40 - C_GetInterface = (CK_C_GetInterface) dlsym(hModule, "C_GetInterface"); - if ((C_GetInterface != NULL) && (dlerror() == NULL)) { - TRACE0("Connect: Found C_GetInterface func\n"); - rv = (C_GetInterface)(NULL, NULL, &interface, 0L); - if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { - goto setModuleData; - } - } - C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, - "C_GetFunctionList"); - if ((systemErrorMessage = dlerror()) != NULL){ - p11ThrowIOException(env, systemErrorMessage); - goto cleanup; - } - if (C_GetFunctionList == NULL) { - TRACE0("Connect: No C_GetFunctionList func\n"); - p11ThrowIOException(env, "ERROR: C_GetFunctionList == NULL"); - goto cleanup; - } - TRACE0("Connect: Found C_GetFunctionList func\n"); + goto cleanup; } + TRACE1("Connect: Found %s func\n", getFunctionListStr); setModuleData: /*