Index: .fossil-settings/ignore-glob ================================================================== --- .fossil-settings/ignore-glob +++ .fossil-settings/ignore-glob @@ -15,10 +15,13 @@ libcackey_g.dll libcackey.dll libcackey_g.a libcackey.a libcackey.syms +libcackey_wrap.so +libcackey_wrap.o +libcackey_wrap.c build/certs test test-afl test-afl.data build/chrome/archive Index: Makefile.in ================================================================== --- Makefile.in +++ Makefile.in @@ -47,10 +47,18 @@ libcackey_g.a: cackey_g.o 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 + -@WEAKENSYMS@ "libcackey_wrap.@SHOBJEXT@" + -@REMOVESYMS@ "libcackey_wrap.@SHOBJEXT@" + +libcackey_wrap.o: libcackey_wrap.c + $(CC) $(SHOBJFLAGS) -o libcackey_wrap.o -c libcackey_wrap.c + test: test.c libcackey_g.@SHOBJEXT@ $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o test test.c -Wl,-R,. libcackey_g.@SHOBJEXT@ $(LIBS) test-afl.data: test tmpLogFile='log.$(shell openssl rand -hex 16)'; \ @@ -76,10 +84,11 @@ clean: rm -f libcackey.@SHOBJEXT@ libcackey_g.@SHOBJEXT@ rm -f libcackey.@SHOBJEXT@.def libcackey_g.@SHOBJEXT@.def rm -f libcackey.@SHOBJEXT@.a libcackey_g.@SHOBJEXT@.a + rm -f libcackey_wrap.@SHOBJEXT@ libcackey_wrap.o rm -f cackey.o cackey_g.o rm -f test rm -f splint-cackey.txt distclean: clean Index: autogen.sh ================================================================== --- autogen.sh +++ autogen.sh @@ -1,9 +1,12 @@ #! /bin/sh rm -f aclocal.m4 +rm -f libcackey_wrap.c +./build/make-libcackey_wrap > libcackey_wrap.c + ${MAKE:-make} -C aclocal autoconf; autoheader rm -rf autom4te.cache/ ADDED build/libcackey_wrap.c.in Index: build/libcackey_wrap.c.in ================================================================== --- /dev/null +++ build/libcackey_wrap.c.in @@ -0,0 +1,75 @@ +#define _GNU_SOURCE +#include + +#include "config.h" + +#include +#include +#include + +#define CK_PTR * +#define CK_DEFINE_FUNCTION(returnType, name) returnType name +#define CK_DECLARE_FUNCTION(returnType, name) returnType name +#define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name) +#define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name) +#ifndef NULL_PTR +# define NULL_PTR 0 +#endif + +#include "./pkcs11/pkcs11.h" + +static void *libcackey_wrap_handle = NULL_PTR; + +static void libcackey_wrap_init(void) { + Dl_info libinfo; + int dladdr_ret; + char *library, *libraryDir, *libraryDirLastSlash; + + if (libcackey_wrap_handle) { + return; + } + + dladdr_ret = dladdr(libcackey_wrap_init, &libinfo); + if (dladdr_ret == 0) { + fprintf(stderr, "Unable to resolve path: %s\n", dlerror()); + + abort(); + + return; + } + + if (!libinfo.dli_fname) { + fprintf(stderr, "Unable to lookup filename\n"); + + abort(); + + return; + } + + libraryDir = strdup(libinfo.dli_fname); + libraryDirLastSlash = strrchr(libraryDir, '/'); + if (!libraryDirLastSlash) { + fprintf(stderr, "File name returned is not an absolute path: %s\n", libraryDir); + + abort(); + + return; + } + *libraryDirLastSlash = '\0'; + + asprintf(&library, "%s/libcackey.so", 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()); + + abort(); + + return; + } + + free(library); + + return; +} ADDED build/make-libcackey_wrap Index: build/make-libcackey_wrap ================================================================== --- /dev/null +++ build/make-libcackey_wrap @@ -0,0 +1,31 @@ +#! /usr/bin/env bash + +cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1 + +cat libcackey_wrap.c.in + +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')" + 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}" + + argNamesList='' + for argName in "${argNames[@]}"; do + argNamesList="${argNamesList}, ${argName}" + done + argNamesList="$(echo "${argNamesList}" | cut -c 3-)" + + echo '' + echo "${line}" + echo $'\t'"CK_RV (*func)($(echo "${args}" | tr $'\n' ',' | sed 's@,*$@@;s@,@, @g'));" + echo '' + echo $'\t''libcackey_wrap_init();' + echo '' + echo $'\t'"func = dlsym(libcackey_wrap_handle, \"${function}\");" + echo '' + echo $'\t'"return(func($argNamesList));" + echo '}' +done