Check-in [3eb54f93b1]
Overview
Comment:Added softokn3 wrapper module
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:3eb54f93b1fec6bf225cf0eb43bfda91652764f4
User & Date: rkeene on 2019-08-08 16:52:55
Other Links: manifest | tags
Context
2019-08-09
01:34
Fixed padding conditions Leaf check-in: b63163f527 user: rkeene tags: trunk
2019-08-08
16:52
Added softokn3 wrapper module check-in: 3eb54f93b1 user: rkeene tags: trunk
2019-06-12
22:41
More testing of the Tcl implementation of the SSH agent check-in: 24e37c4dab user: rkeene tags: trunk
Changes

Modified build/tcl/Makefile from [04cbb3e324] to [ef0ac61411].

     1      1   all: ssh-agent-noasync.js
     2      2   
     3      3   ssh-agent-noasync.js: ../chrome/ssh-agent.js
     4         -	cc -Dawait='' -Dasync='' -nostdinc -C -E -x c ../chrome/ssh-agent.js -o - | grep -v '^# ' > ssh-agent-noasync.js.new
            4  +	$(CC) -Dawait='' -Dasync='' -nostdinc -C -E -x c ../chrome/ssh-agent.js -o - | grep -v '^# ' > ssh-agent-noasync.js.new
     5      5   	mv ssh-agent-noasync.js.new ssh-agent-noasync.js
     6      6   
            7  +softokn3-pkcs11.so: softokn3-pkcs11.c
            8  +	$(CC) -fPIC -Wall -shared -o softokn3-pkcs11.so softokn3-pkcs11.c
            9  +
     7     10   clean:
     8     11   	rm -f ssh-agent-noasync.js.new ssh-agent-noasync.js
           12  +	rm -f softokn3-pkcs11.so
     9     13   
    10     14   distclean: clean
    11     15   
    12     16   .PHONY: all clean distclean

Added build/tcl/softokn3-pkcs11.c version [4e157658f4].

            1  +#include <unistd.h>
            2  +#include <stdlib.h>
            3  +#include <string.h>
            4  +#include <dlfcn.h>
            5  +
            6  +#include <stdio.h>
            7  +
            8  +/*
            9  + * Create minimal PKCS#11 module interface needed to wrap
           10  + */
           11  +#define CK_DEFINE_FUNCTION(type, func) type func
           12  +
           13  +#define CKR_GENERAL_ERROR 0x00000005
           14  +#define CKR_OK            0x00000000
           15  +
           16  +typedef void (*CK_CREATEMUTEX)();
           17  +typedef void (*CK_DESTROYMUTEX)();
           18  +typedef void (*CK_LOCKMUTEX)();
           19  +typedef void (*CK_UNLOCKMUTEX)();
           20  +typedef unsigned long CK_FLAGS;
           21  +typedef unsigned long CK_RV;
           22  +typedef void * CK_VOID_PTR;
           23  +
           24  +typedef struct CK_C_INITIALIZE_ARGS {
           25  +	CK_CREATEMUTEX CreateMutex;
           26  +	CK_DESTROYMUTEX DestroyMutex;
           27  +	CK_LOCKMUTEX LockMutex;
           28  +	CK_UNLOCKMUTEX UnlockMutex;
           29  +	CK_FLAGS flags;
           30  +	CK_VOID_PTR pReserved;
           31  +} CK_C_INITIALIZE_ARGS;
           32  +
           33  +typedef struct {
           34  +	unsigned char major;
           35  +	unsigned char minor;
           36  +} CK_VERSION;
           37  +
           38  +typedef struct {
           39  +	CK_VERSION version;
           40  +	CK_RV (*C_Initialize)(CK_VOID_PTR);
           41  +	CK_RV (*C_Finalize)(CK_VOID_PTR);
           42  +} CK_FUNCTION_LIST;
           43  +
           44  +typedef CK_FUNCTION_LIST* CK_FUNCTION_LIST_PTR;
           45  +typedef CK_FUNCTION_LIST** CK_FUNCTION_LIST_PTR_PTR;
           46  +
           47  +/*
           48  + * This is the size of the full PKCS#11 function list structure
           49  + * (CK_FUNCTION_LIST).  We only actually care about wrapping the
           50  + * initialization function, so we have to compute the full size
           51  + *
           52  + * Size is sizeOf(ourFunctionListStruct) + (numberOfFunctions-2 * functionPointerSize)
           53  + */
           54  +#define SIZE_OF_PKCS11_FUNCTION_LIST (sizeof(CK_FUNCTION_LIST) + (67 * sizeof(void *)))
           55  +
           56  +/*
           57  + * Real C_Initialize() function for this module
           58  + */
           59  +static CK_RV (*Real_C_Initialize)(CK_VOID_PTR) = NULL;
           60  +
           61  +static CK_DEFINE_FUNCTION(CK_RV, Proxy_C_Initialize)(CK_VOID_PTR pInitArgs) {
           62  +	char nssConfig[1024];
           63  +	char *nssDBDir = NULL;
           64  +	CK_C_INITIALIZE_ARGS *InitArgs = NULL;
           65  +
           66  +	InitArgs = pInitArgs;
           67  +
           68  +	if (InitArgs == NULL) {
           69  +		InitArgs = malloc(sizeof(*InitArgs));
           70  +
           71  +		InitArgs->CreateMutex = NULL;
           72  +		InitArgs->DestroyMutex = NULL;
           73  +		InitArgs->LockMutex = NULL;
           74  +		InitArgs->UnlockMutex = NULL;
           75  +		InitArgs->flags = 0;
           76  +		InitArgs->pReserved = NULL;
           77  +	}
           78  +
           79  +	if (InitArgs->pReserved == NULL) {
           80  +		nssDBDir = getenv("SOFTOKN3_NSS_DIR");
           81  +
           82  +		if (nssDBDir) {
           83  +			snprintf(nssConfig, sizeof(nssConfig),
           84  +			         "configdir='%s' certPrefix='' keyPrefix='' secmod='secmod.db' flags=readOnly",
           85  +				 nssDBDir
           86  +			);
           87  +
           88  +			InitArgs->pReserved = (void *) nssConfig;
           89  +		}
           90  +	}
           91  +
           92  +	if (Real_C_Initialize == NULL) {
           93  +		return(CKR_GENERAL_ERROR);
           94  +	}
           95  +
           96  +	return(Real_C_Initialize(InitArgs));
           97  +}
           98  +
           99  +CK_DEFINE_FUNCTION(CK_RV, C_GetFunctionList)(CK_FUNCTION_LIST_PTR_PTR ppFunctionList) {
          100  +	CK_RV (*Real_C_GetFunctionList)(CK_FUNCTION_LIST_PTR_PTR);
          101  +	CK_FUNCTION_LIST_PTR copyFunctionList;
          102  +	void *handle;
          103  +	CK_RV retval;
          104  +	char *module;
          105  +
          106  +	module = getenv("SOFTOKN3_MODULE");
          107  +	if (!module) {
          108  +		module = "/usr/lib64/libsoftokn3.so";
          109  +	}
          110  +
          111  +	/* handle = dlmopen(LM_ID_NEWLM, module, RTLD_NOW | RTLD_LOCAL); */
          112  +	handle = dlopen(module, RTLD_NOW | RTLD_LOCAL);
          113  +	if (handle == NULL) {
          114  +		fprintf(stderr, "Unable to open \"%s\": %s\n", module, dlerror());
          115  +
          116  +		return(CKR_GENERAL_ERROR);
          117  +	}
          118  +
          119  +	Real_C_GetFunctionList = dlsym(handle, "C_GetFunctionList");
          120  +
          121  +	if (Real_C_GetFunctionList == NULL) {
          122  +		return(CKR_GENERAL_ERROR);
          123  +	}
          124  +
          125  +	retval = Real_C_GetFunctionList(ppFunctionList);
          126  +
          127  +	if (retval != CKR_OK) {
          128  +		return(retval);
          129  +	}
          130  +
          131  +        copyFunctionList = malloc(SIZE_OF_PKCS11_FUNCTION_LIST);
          132  +        memcpy(copyFunctionList, *ppFunctionList, SIZE_OF_PKCS11_FUNCTION_LIST);
          133  +        *ppFunctionList = copyFunctionList;
          134  +
          135  +	Real_C_Initialize = (*ppFunctionList)->C_Initialize;
          136  +	(*ppFunctionList)->C_Initialize = Proxy_C_Initialize;
          137  +
          138  +	return(retval);
          139  +}