Index: Makefile.in ================================================================== --- Makefile.in +++ Makefile.in @@ -48,11 +48,11 @@ rm -f libcackey_g.a $(AR) rc libcackey_g.a cackey_g.o -$(RANLIB) libcackey_g.a libcackey_wrap.@SHOBJEXT@: libcackey_wrap.o - $(CC) $(SHOBJFLAGS) $(SHOBJLDFLAGS) -o libcackey_wrap.@SHOBJEXT@ libcackey_wrap.o + $(CC) $(SHOBJFLAGS) $(SHOBJLDFLAGS) -o libcackey_wrap.@SHOBJEXT@ libcackey_wrap.o -ldl -@WEAKENSYMS@ "libcackey_wrap.@SHOBJEXT@" -@REMOVESYMS@ "libcackey_wrap.@SHOBJEXT@" libcackey_wrap.o: libcackey_wrap.c $(CC) $(SHOBJFLAGS) -o libcackey_wrap.o -c libcackey_wrap.c Index: build/libcackey_wrap.c.in ================================================================== --- build/libcackey_wrap.c.in +++ build/libcackey_wrap.c.in @@ -15,13 +15,61 @@ #ifndef NULL_PTR # define NULL_PTR 0 #endif #include "./pkcs11/pkcs11.h" + +#ifndef CACKEY_LIBRARY_FILE +#define CACKEY_LIBRARY_FILE "libcackey_g.so" +#endif static void *libcackey_wrap_handle = NULL_PTR; +static void libcackey_wrap_init(void); + +static CK_RV libcackey_wrap_createmutex(CK_VOID_PTR_PTR ppMutex) { + int (*create_mutex)(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); + pthread_mutex_t *pthread_mutex; + int pthread_retval; + + libcackey_wrap_init(); + + pthread_mutex = malloc(sizeof(*pthread_mutex)); + + create_mutex = dlsym(libcackey_wrap_handle, "pthread_mutex_init"); + pthread_retval = create_mutex(pthread_mutex, NULL); + if (pthread_retval != 0) { + return(CKR_GENERAL_ERROR); + } + + *ppMutex = pthread_mutex; + + return(CKR_OK); +} + +#define libcackey_wrap_genericmutexfunc(funcName, pthreadName) \ + static CK_RV funcName(CK_VOID_PTR pMutex) { \ + int (*func)(pthread_mutex_t *mutex); \ + pthread_mutex_t *pthread_mutex; \ + int pthread_retval; \ + libcackey_wrap_init(); \ + pthread_mutex = pMutex; \ + func = dlsym(libcackey_wrap_handle, pthreadName); \ + pthread_retval = func(pthread_mutex); \ + if (pthread_retval != 0) { \ + return(CKR_GENERAL_ERROR); \ + } \ + if (strcmp(pthreadName, "pthread_mutex_destroy") == 0) { \ + free(pthread_mutex); \ + } \ + return(CKR_OK); \ + } + +libcackey_wrap_genericmutexfunc(libcackey_wrap_destroymutex, "pthread_mutex_destroy") +libcackey_wrap_genericmutexfunc(libcackey_wrap_lockmutex, "pthread_mutex_lock") +libcackey_wrap_genericmutexfunc(libcackey_wrap_unlockmutex, "pthread_mutex_unlock") + static void libcackey_wrap_init(void) { Dl_info libinfo; int dladdr_ret; char *library, *libraryDir, *libraryDirLastSlash; @@ -55,11 +103,11 @@ return; } *libraryDirLastSlash = '\0'; - asprintf(&library, "%s/libcackey.so", libraryDir); + asprintf(&library, "%s/" CACKEY_LIBRARY_FILE, libraryDir); libcackey_wrap_handle = dlmopen(LM_ID_NEWLM, library, RTLD_LOCAL | RTLD_NOW); if (!libcackey_wrap_handle) { fprintf(stderr, "Unable to load \"%s\": %s\n", library, dlerror()); @@ -69,7 +117,19 @@ return; } free(library); + return; +} + +static void libcackey_wrap_fini(void) { + if (!libcackey_wrap_handle) { + return; + } + + dlclose(libcackey_wrap_handle); + + libcackey_wrap_handle = NULL_PTR; + return; } Index: build/make-libcackey_wrap ================================================================== --- build/make-libcackey_wrap +++ build/make-libcackey_wrap @@ -2,12 +2,28 @@ cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1 cat libcackey_wrap.c.in +functionList=() +while IFS='' read -r line; do + function="$(echo "${line}" | cut -f 2 -d , | cut -f 1 -d ')' | sed 's@ *@@g')" + + if [ "${function}" = 'C_LoginMutexArg' ]; then + continue + fi + + functionList=("${functionList[@]}" "${function}") +done < <(grep '^CK_DEFINE_FUNCTION' ../cackey.c) + grep '^CK_DEFINE_FUNCTION' ../cackey.c | while IFS='' read -r line; do function="$(echo "${line}" | cut -f 2 -d , | cut -f 1 -d ')' | sed 's@ *@@g')" + + if [ "${function}" = 'C_LoginMutexArg' ]; then + continue + fi + args="$(echo "${line}" | cut -f 3 -d '(' | cut -f 1 -d ')' | tr ',' $'\n' | sed 's@^ *@@')" argNames=() while read argType argName; do argNames=("${argNames[@]}" "${argName}") done <<<"${args}" @@ -19,13 +35,62 @@ argNamesList="$(echo "${argNamesList}" | cut -c 3-)" echo '' echo "${line}" echo $'\t'"CK_RV (*func)($(echo "${args}" | tr $'\n' ',' | sed 's@,*$@@;s@,@, @g'));" + case "${function}" in + C_Finalize) + echo $'\t''CK_RV retval;' + ;; + C_Initialize) + echo $'\t''CK_C_INITIALIZE_ARGS CK_PTR args, localargs;' + ;; + C_GetFunctionList) + echo $'\t''CK_RV retval;' + echo $'\t''CK_FUNCTION_LIST_PTR pFunctionList;' + ;; + esac + echo '' echo $'\t''libcackey_wrap_init();' + + if [ "${function}" = 'C_Initialize' ]; then + echo '' + echo $'\t''if (pInitArgs) {' + echo $'\t\t''args = pInitArgs;' + echo $'\t\t''if ((args->flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {' +#echo 'fprintf(stderr, "replacing=%p\n", pInitArgs); fflush(stderr); abort();' + echo $'\t\t\t''memcpy(&localargs, args, sizeof(*args));' + echo $'\t\t\t''localargs.CreateMutex = libcackey_wrap_createmutex;' + echo $'\t\t\t''localargs.DestroyMutex = libcackey_wrap_destroymutex;' + echo $'\t\t\t''localargs.LockMutex = libcackey_wrap_lockmutex;' + echo $'\t\t\t''localargs.UnlockMutex = libcackey_wrap_unlockmutex;' + echo $'\t\t\t''localargs.flags &= ~CKF_OS_LOCKING_OK;' + echo $'\t\t\t''pInitArgs = &localargs;' + echo $'\t\t''}' + echo $'\t''}' + fi echo '' echo $'\t'"func = dlsym(libcackey_wrap_handle, \"${function}\");" echo '' - echo $'\t'"return(func($argNamesList));" + if [ "${function}" = 'C_Finalize' ]; then + echo $'\t'"retval = func($argNamesList);" + echo '' + echo $'\t''libcackey_wrap_fini();' + echo '' + echo $'\t''return(retval);' + elif [ "${function}" = 'C_GetFunctionList' ]; then + echo $'\t'"retval = func($argNamesList);" + echo '' + echo $'\t''if (retval == CKR_OK) {' + echo $'\t\t''pFunctionList = *ppFunctionList;' + for function in "${functionList[@]}"; do + echo $'\t\t'"pFunctionList->${function} = ${function};" + done + echo $'\t''}' + echo '' + echo $'\t''return(retval);' + else + echo $'\t'"return(func($argNamesList));" + fi echo '}' done