TclPKCS11

Check-in [f7f7813de0]
Login
Overview
Comment:Added additional OS support for loading libraries (mostly untested)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f7f7813de096fd232fe8639783444a41061a8f7b4a9fb717c0c42d82dc7032ed
User & Date: rkeene on 2010-10-10 13:39:01
Other Links: manifest | tags
Context
2010-10-10
15:27
Began TEA-ifying TclPKCS11 check-in: 16597afcf3 user: rkeene tags: trunk
13:39
Added additional OS support for loading libraries (mostly untested) check-in: f7f7813de0 user: rkeene tags: trunk
05:36
Updated test driver to not give false errors if a non-PIN related decryption error occurs check-in: be76ba4355 user: rkeene tags: trunk
Changes

Modified Makefile from [ed33656445] to [454cbc79b9].

1
2
3
4
5
6
7
8
9
10
CC = gcc
CFLAGS = -g3 -ggdb3 -fPIC -DPIC -Wall
CPPFLAGS = -DTCL_USE_STUBS
SHFLAGS = -nostartfiles -rdynamic -shared
LIBS = -ldl -ltclstub8.5 

all: tclpkcs11.so

pkcs11.h: pkcs11f.h pkcs11t.h
tclpkcs11.o: tclpkcs11.c pkcs11.h

|
|







1
2
3
4
5
6
7
8
9
10
CC = gcc
CFLAGS = -fPIC -DPIC -Wall
CPPFLAGS = -DTCL_USE_STUBS=1 -DHAVE_DLOPEN=1 -DHAVE_DLFCN_H=1
SHFLAGS = -nostartfiles -rdynamic -shared
LIBS = -ldl -ltclstub8.5 

all: tclpkcs11.so

pkcs11.h: pkcs11f.h pkcs11t.h
tclpkcs11.o: tclpkcs11.c pkcs11.h

Modified tclpkcs11.c from [05f498a3e1] to [e61975e985].

1
2
3

4







5
6
7
8
9
10
11
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <dlfcn.h>







#include <tcl.h>

#if 10 * TCL_MAJOR_VERSION + TCL_MINOR_VERSION >= 86
#  define TCL_INCLUDES_LOADFILE 1
#endif

/* PKCS#11 Definitions for the local platform */



>
|
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_DLFCN_H
#  include <dlfcn.h>
#endif
#ifdef HAVE_DL_H
#  include <dl.h>
#endif
#ifdef _WIN32
#  include <windows.h>
#endif
#include <tcl.h>

#if 10 * TCL_MAJOR_VERSION + TCL_MINOR_VERSION >= 86
#  define TCL_INCLUDES_LOADFILE 1
#endif

/* PKCS#11 Definitions for the local platform */
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
			break;
		}
	}

	return(outbufidx);
}

/* PKCS#11 CreateMutex implementation that use Tcl Mutexes */
static CK_RV tclpkcs11_create_mutex(void **mutex) {
	Tcl_Mutex *retval;

	if (!mutex) {
		return(CKR_GENERAL_ERROR);
	}








|







289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
			break;
		}
	}

	return(outbufidx);
}

/* PKCS#11 Mutex functions implementation that use Tcl Mutexes */
static CK_RV tclpkcs11_create_mutex(void **mutex) {
	Tcl_Mutex *retval;

	if (!mutex) {
		return(CKR_GENERAL_ERROR);
	}

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403




404

405
406
407
408
409
410
411
412
413
414
415
416
417




418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433












434

435
436
437
438
439
440
441
	return(CKR_OK);
}

/*
 * Platform Specific Functions 
 */
static void *tclpkcs11_int_load_module(const char *pathname) {
#ifdef TCL_INCLUDES_LOADFILE
	int tcl_rv;
	Tcl_LoadHandle *new_handle;

	new_handle = (Tcl_LoadHandle *) ckalloc(sizeof(*new_handle));

	tcl_rv = Tcl_LoadFile(NULL, Tcl_NewStringObj(pathname, -1), NULL, 0, NULL, new_handle);
	if (tcl_rv != TCL_OK) {
		return(NULL);
	}

	return(new_handle);
#else
	/* XXX: TODO: Replace this with Tcl_Load() in 8.6 or otherwise a system-specific loading mechanism */
	return(dlopen(pathname, RTLD_LAZY | RTLD_LOCAL));




#endif

}
static void tclpkcs11_int_unload_module(void *handle) {
#ifdef TCL_INCLUDES_LOADFILE
	Tcl_LoadHandle *tcl_handle;

	tcl_handle = handle;

	Tcl_FSUnloadFile(NULL, *tcl_handle);

	ckfree(handle);
#else
	/* XXX: TODO: Replace this with Tcl_Unload() in 8.6 or otherwise a system-specific unloading mechanism */
	dlclose(handle);




#endif
	return;
}
static void *tclpkcs11_int_lookup_sym(void *handle, const char *sym) {
#ifdef TCL_INCLUDES_LOADFILE
	Tcl_LoadHandle *tcl_handle;
	void *retval;

	tcl_handle = handle;

	retval = Tcl_FindSymbol(NULL, *tcl_handle, sym);

	return(retval);
#else
	/* XXX: TODO: Replace this with ... ? in 8.6 or otherwise a system-specific symbol lookup mechanism */
	return(dlsym(handle, sym));












#endif

}

/*
 * Tcl Commands
 */
static int tclpkcs11_load_module(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	struct tclpkcs11_interpdata *interpdata;







|











|
<

>
>
>
>

>


|







|
<

>
>
>
>




|








|
<

>
>
>
>
>
>
>
>
>
>
>
>

>







390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
	return(CKR_OK);
}

/*
 * Platform Specific Functions 
 */
static void *tclpkcs11_int_load_module(const char *pathname) {
#if defined(TCL_INCLUDES_LOADFILE)
	int tcl_rv;
	Tcl_LoadHandle *new_handle;

	new_handle = (Tcl_LoadHandle *) ckalloc(sizeof(*new_handle));

	tcl_rv = Tcl_LoadFile(NULL, Tcl_NewStringObj(pathname, -1), NULL, 0, NULL, new_handle);
	if (tcl_rv != TCL_OK) {
		return(NULL);
	}

	return(new_handle);
#elif defined(HAVE_DLOPEN)

	return(dlopen(pathname, RTLD_LAZY | RTLD_LOCAL));
#elif defined(HAVE_SHL_LOAD)
	return(shl_load(pathname, BIND_DEFERRED, 0L));
#elif defined(_WIN32)
	return(LoadLibrary(pathname));
#endif
	return(NULL);
}
static void tclpkcs11_int_unload_module(void *handle) {
#if defined(TCL_INCLUDES_LOADFILE)
	Tcl_LoadHandle *tcl_handle;

	tcl_handle = handle;

	Tcl_FSUnloadFile(NULL, *tcl_handle);

	ckfree(handle);
#elif defined(HAVE_DLOPEN)

	dlclose(handle);
#elif defined(HAVE_SHL_LOAD)
	shl_unload(handle);
#elif defined(_WIN32)
	FreeLibrary(handle);
#endif
	return;
}
static void *tclpkcs11_int_lookup_sym(void *handle, const char *sym) {
#if defined(TCL_INCLUDES_LOADFILE)
	Tcl_LoadHandle *tcl_handle;
	void *retval;

	tcl_handle = handle;

	retval = Tcl_FindSymbol(NULL, *tcl_handle, sym);

	return(retval);
#elif defined(HAVE_DLOPEN)

	return(dlsym(handle, sym));
#elif defined(HAVE_SHL_LOAD)
	void *retval;
	int shl_findsym_ret;

	shl_findsym_ret = shl_findsym(handle, sym, TYPE_PROCEDURE, &retval);
	if (shl_findsym_ret != 0) {
		return(NULL);
	}

	return(retval);
#elif defined(_WIN32)
	return(GetProcAddress(handle, sym));
#endif
	return(NULL);
}

/*
 * Tcl Commands
 */
static int tclpkcs11_load_module(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
	struct tclpkcs11_interpdata *interpdata;