Check-in [3b7b2eddb4]
Overview
Comment:More work on improving the wrapper
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | better-wrapping
Files: files | file ages | folders
SHA1:3b7b2eddb484df522b782618348c690777f166be
User & Date: rkeene on 2017-12-28 01:32:36
Other Links: manifest | tags
Context
2018-06-29
18:58
More work on improving wrapper Leaf check-in: 3fdad99785 user: rkeene tags: better-wrapping
2017-12-28
01:32
More work on improving the wrapper check-in: 3b7b2eddb4 user: rkeene tags: better-wrapping
01:32
Updated to support supplying an empty PIN command to be equivelant to unsetting check-in: 9043258dc4 user: rkeene tags: trunk
Changes

Modified Makefile.in from [abdaf2d437] to [0b2c29b908].

    46     46   
    47     47   libcackey_g.a: cackey_g.o
    48     48   	rm -f libcackey_g.a
    49     49   	$(AR) rc libcackey_g.a cackey_g.o
    50     50   	-$(RANLIB) libcackey_g.a
    51     51   
    52     52   libcackey_wrap.@SHOBJEXT@: libcackey_wrap.o
    53         -	$(CC) $(SHOBJFLAGS) $(SHOBJLDFLAGS) -o libcackey_wrap.@SHOBJEXT@ libcackey_wrap.o
           53  +	$(CC) $(SHOBJFLAGS) $(SHOBJLDFLAGS) -o libcackey_wrap.@SHOBJEXT@ libcackey_wrap.o -ldl
    54     54   	-@WEAKENSYMS@ "libcackey_wrap.@SHOBJEXT@"
    55     55   	-@REMOVESYMS@ "libcackey_wrap.@SHOBJEXT@"
    56     56   	
    57     57   libcackey_wrap.o: libcackey_wrap.c
    58     58   	$(CC) $(SHOBJFLAGS) -o libcackey_wrap.o -c libcackey_wrap.c
    59     59   
    60     60   test: test.c libcackey_g.@SHOBJEXT@

Modified build/libcackey_wrap.c.in from [4c95cbc88b] to [98f3aad3ae].

    13     13   #define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name)
    14     14   #define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name)
    15     15   #ifndef NULL_PTR
    16     16   #  define NULL_PTR 0
    17     17   #endif
    18     18   
    19     19   #include "./pkcs11/pkcs11.h"
           20  +
           21  +#ifndef CACKEY_LIBRARY_FILE
           22  +#define CACKEY_LIBRARY_FILE "libcackey_g.so"
           23  +#endif
    20     24   
    21     25   static void *libcackey_wrap_handle = NULL_PTR;
    22     26   
           27  +static void libcackey_wrap_init(void);
           28  +
           29  +static CK_RV libcackey_wrap_createmutex(CK_VOID_PTR_PTR ppMutex) {
           30  +	int (*create_mutex)(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
           31  +	pthread_mutex_t *pthread_mutex;
           32  +	int pthread_retval;
           33  +
           34  +	libcackey_wrap_init();
           35  +
           36  +	pthread_mutex = malloc(sizeof(*pthread_mutex));
           37  +
           38  +	create_mutex = dlsym(libcackey_wrap_handle, "pthread_mutex_init");
           39  +	pthread_retval = create_mutex(pthread_mutex, NULL);
           40  +	if (pthread_retval != 0) {
           41  +		return(CKR_GENERAL_ERROR);
           42  +	}
           43  +
           44  +	*ppMutex = pthread_mutex;
           45  +
           46  +	return(CKR_OK);
           47  +}
           48  +
           49  +#define libcackey_wrap_genericmutexfunc(funcName, pthreadName) \
           50  +	static CK_RV funcName(CK_VOID_PTR pMutex) { \
           51  +		int (*func)(pthread_mutex_t *mutex); \
           52  +		pthread_mutex_t *pthread_mutex; \
           53  +		int pthread_retval; \
           54  +		libcackey_wrap_init(); \
           55  +		pthread_mutex = pMutex; \
           56  +		func = dlsym(libcackey_wrap_handle, pthreadName); \
           57  +		pthread_retval = func(pthread_mutex); \
           58  +		if (pthread_retval != 0) { \
           59  +			return(CKR_GENERAL_ERROR); \
           60  +		} \
           61  +		if (strcmp(pthreadName, "pthread_mutex_destroy") == 0) { \
           62  +			free(pthread_mutex); \
           63  +		} \
           64  +		return(CKR_OK); \
           65  +	}
           66  +
           67  +libcackey_wrap_genericmutexfunc(libcackey_wrap_destroymutex, "pthread_mutex_destroy")
           68  +libcackey_wrap_genericmutexfunc(libcackey_wrap_lockmutex, "pthread_mutex_lock")
           69  +libcackey_wrap_genericmutexfunc(libcackey_wrap_unlockmutex, "pthread_mutex_unlock")
           70  +
    23     71   static void libcackey_wrap_init(void) {
    24     72   	Dl_info libinfo;
    25     73   	int dladdr_ret;
    26     74   	char *library, *libraryDir, *libraryDirLastSlash;
    27     75   
    28     76   	if (libcackey_wrap_handle) {
    29     77   		return;
................................................................................
    53    101   
    54    102   		abort();
    55    103   
    56    104   		return;
    57    105   	}
    58    106   	*libraryDirLastSlash = '\0';
    59    107   
    60         -	asprintf(&library, "%s/libcackey.so", libraryDir);
          108  +	asprintf(&library, "%s/" CACKEY_LIBRARY_FILE, libraryDir);
    61    109   
    62    110   	libcackey_wrap_handle = dlmopen(LM_ID_NEWLM, library, RTLD_LOCAL | RTLD_NOW);
    63    111   
    64    112   	if (!libcackey_wrap_handle) {
    65    113   		fprintf(stderr, "Unable to load \"%s\": %s\n", library, dlerror());
    66    114   
    67    115   		abort();
    68    116   
    69    117   		return;
    70    118   	}
    71    119   
    72    120   	free(library);
    73    121   
          122  +	return;
          123  +}
          124  +
          125  +static void libcackey_wrap_fini(void) {
          126  +	if (!libcackey_wrap_handle) {
          127  +		return;
          128  +	}
          129  +
          130  +	dlclose(libcackey_wrap_handle);
          131  +
          132  +	libcackey_wrap_handle = NULL_PTR;
          133  +
    74    134   	return;
    75    135   }

Modified build/make-libcackey_wrap from [833dbdb173] to [f1fa196b14].

     1      1   #! /usr/bin/env bash
     2      2   
     3      3   cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
     4      4   
     5      5   cat libcackey_wrap.c.in
     6      6   
            7  +functionList=()
            8  +while IFS='' read -r line; do
            9  +	function="$(echo "${line}" | cut -f 2 -d , | cut -f 1 -d ')' | sed 's@ *@@g')"
           10  +
           11  +	if [ "${function}" = 'C_LoginMutexArg' ]; then
           12  +		continue
           13  +	fi
           14  +
           15  +	functionList=("${functionList[@]}" "${function}")
           16  +done < <(grep '^CK_DEFINE_FUNCTION' ../cackey.c)
           17  +
     7     18   grep '^CK_DEFINE_FUNCTION' ../cackey.c | while IFS='' read -r line; do
     8     19   	function="$(echo "${line}" | cut -f 2 -d , | cut -f 1 -d ')' | sed 's@ *@@g')"
           20  +
           21  +	if [ "${function}" = 'C_LoginMutexArg' ]; then
           22  +		continue
           23  +	fi
           24  +
     9     25   	args="$(echo "${line}" | cut -f 3 -d '(' | cut -f 1 -d ')' | tr ',' $'\n' | sed 's@^ *@@')"
    10     26   	argNames=()
    11     27   	while read argType argName; do
    12     28   		argNames=("${argNames[@]}" "${argName}")
    13     29   	done <<<"${args}"
    14     30   
    15     31   	argNamesList=''
................................................................................
    17     33   		argNamesList="${argNamesList}, ${argName}"
    18     34   	done
    19     35   	argNamesList="$(echo "${argNamesList}" | cut -c 3-)"
    20     36   
    21     37   	echo ''
    22     38   	echo "${line}"
    23     39   	echo $'\t'"CK_RV (*func)($(echo "${args}" | tr $'\n' ',' | sed 's@,*$@@;s@,@, @g'));"
           40  +	case "${function}" in
           41  +		C_Finalize)
           42  +			echo $'\t''CK_RV retval;'
           43  +			;;
           44  +		C_Initialize)
           45  +			echo $'\t''CK_C_INITIALIZE_ARGS CK_PTR args, localargs;'
           46  +			;;
           47  +		C_GetFunctionList)
           48  +			echo $'\t''CK_RV retval;'
           49  +			echo $'\t''CK_FUNCTION_LIST_PTR pFunctionList;'
           50  +			;;
           51  +	esac
           52  +
    24     53   	echo ''
    25     54   	echo $'\t''libcackey_wrap_init();'
           55  +
           56  +	if [ "${function}" = 'C_Initialize' ]; then
           57  +		echo ''
           58  +		echo $'\t''if (pInitArgs) {'
           59  +		echo $'\t\t''args = pInitArgs;'
           60  +		echo $'\t\t''if ((args->flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {'
           61  +#echo 'fprintf(stderr, "replacing=%p\n", pInitArgs); fflush(stderr); abort();'
           62  +		echo $'\t\t\t''memcpy(&localargs, args, sizeof(*args));'
           63  +		echo $'\t\t\t''localargs.CreateMutex = libcackey_wrap_createmutex;'
           64  +		echo $'\t\t\t''localargs.DestroyMutex = libcackey_wrap_destroymutex;'
           65  +		echo $'\t\t\t''localargs.LockMutex = libcackey_wrap_lockmutex;'
           66  +		echo $'\t\t\t''localargs.UnlockMutex = libcackey_wrap_unlockmutex;'
           67  +		echo $'\t\t\t''localargs.flags &= ~CKF_OS_LOCKING_OK;'
           68  +		echo $'\t\t\t''pInitArgs = &localargs;'
           69  +		echo $'\t\t''}'
           70  +		echo $'\t''}'
           71  +	fi
    26     72   	echo ''
    27     73   	echo $'\t'"func = dlsym(libcackey_wrap_handle, \"${function}\");"
    28     74   	echo ''
    29         -	echo $'\t'"return(func($argNamesList));"
           75  +	if [ "${function}" = 'C_Finalize' ]; then
           76  +		echo $'\t'"retval = func($argNamesList);"
           77  +		echo ''
           78  +		echo $'\t''libcackey_wrap_fini();'
           79  +		echo ''
           80  +		echo $'\t''return(retval);'
           81  +	elif [ "${function}" = 'C_GetFunctionList' ]; then
           82  +		echo $'\t'"retval = func($argNamesList);"
           83  +		echo ''
           84  +		echo $'\t''if (retval == CKR_OK) {'
           85  +		echo $'\t\t''pFunctionList = *ppFunctionList;'
           86  +		for function in "${functionList[@]}"; do
           87  +			echo $'\t\t'"pFunctionList->${function} = ${function};"
           88  +		done
           89  +		echo $'\t''}'
           90  +		echo ''
           91  +		echo $'\t''return(retval);'
           92  +	else
           93  +		echo $'\t'"return(func($argNamesList));"
           94  +	fi
    30     95   	echo '}'
    31     96   done