Diff

Differences From Artifact [de5cf6edd0]:

To Artifact [47e7747ccc]:


43
44
45
46
47
48
49








50
51
52
53
54
55
56
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64







+
+
+
+
+
+
+
+







#  ifdef HAVE_LIBZ
#    include <zlib.h>
#  endif
#else
#  ifdef HAVE_LIBZ
#    undef HAVE_LIBZ
#  endif
#endif
#ifndef _THREAD_EMULATION
#  ifdef HAVE_DLFCN_H
#    ifdef HAVE_DLSYM
#      include <dlfcn.h>
#      define HAVE_CACKEY_MUTEX_PTHREAD_FUNCS 1
#    endif
#  endif
#endif
#ifdef CACKEY_DEBUG_SEARCH_SPEEDTEST
#  include <sys/time.h>
#endif

#define CK_PTR *
#define CK_DEFINE_FUNCTION(returnType, name) returnType name
3734
3735
3736
3737
3738
3739
3740


































3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758

3759
3760
3761
3762
3763
3764
3765
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799

3800
3801
3802
3803
3804
3805
3806
3807







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

















-
+







		return(-1);
	}
#  endif
#endif

	return(x509_read_ret);
}

/* Dynamically load pthreads from the running application */
#ifdef HAVE_CACKEY_MUTEX_PTHREAD_FUNCS
#warning building with pthreads opened via dlsym
struct cackey_mutex_pthread_funcs_st {
	int (*pthread_mutex_init)(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
	int (*pthread_mutex_lock)(pthread_mutex_t *mutex);
	int (*pthread_mutex_unlock)(pthread_mutex_t *mutex);
};

static struct cackey_mutex_pthread_funcs_st *cackey_mutex_pthread_funcs(void) {
	static struct cackey_mutex_pthread_funcs_st funcs = {0};
	static int init = 0;

	if (init != 0) {
		return(&funcs);
	}

	funcs.pthread_mutex_init = dlsym(RTLD_DEFAULT, "pthread_mutex_init");
	funcs.pthread_mutex_lock = dlsym(RTLD_DEFAULT, "pthread_mutex_lock");
	funcs.pthread_mutex_unlock = dlsym(RTLD_DEFAULT, "pthread_mutex_unlock");

	init = 1;

	return(&funcs);
}
#define cackey_pthread_mutex_init cackey_mutex_pthread_funcs()->pthread_mutex_init
#define cackey_pthread_mutex_lock cackey_mutex_pthread_funcs()->pthread_mutex_lock
#define cackey_pthread_mutex_unlock cackey_mutex_pthread_funcs()->pthread_mutex_unlock
#else
#define cackey_pthread_mutex_init pthread_mutex_init
#define cackey_pthread_mutex_lock pthread_mutex_lock
#define cackey_pthread_mutex_unlock pthread_mutex_unlock
#endif

/* Returns 0 on success */
static int cackey_mutex_create(void **mutex) {
	pthread_mutex_t *pthread_mutex;
	int pthread_retval;
	CK_RV custom_retval;

	CACKEY_DEBUG_PRINTF("Called.");

	if ((cackey_args.flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {
		pthread_mutex = malloc(sizeof(*pthread_mutex));
		if (!pthread_mutex) {
			CACKEY_DEBUG_PRINTF("Failed to allocate memory.");

			return(-1);
		}

		pthread_retval = pthread_mutex_init(pthread_mutex, NULL);
		pthread_retval = cackey_pthread_mutex_init(pthread_mutex, NULL);
		if (pthread_retval != 0) {
			CACKEY_DEBUG_PRINTF("pthread_mutex_init() returned error (%i).", pthread_retval);

			return(-1);
		}

		*mutex = pthread_mutex;
3787
3788
3789
3790
3791
3792
3793
3794

3795
3796
3797
3798
3799
3800
3801
3829
3830
3831
3832
3833
3834
3835

3836
3837
3838
3839
3840
3841
3842
3843







-
+







	CK_RV custom_retval;

	CACKEY_DEBUG_PRINTF("Called.");

	if ((cackey_args.flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {
		pthread_mutex = mutex;

		pthread_retval = pthread_mutex_lock(pthread_mutex);
		pthread_retval = cackey_pthread_mutex_lock(pthread_mutex);
		if (pthread_retval != 0) {
			CACKEY_DEBUG_PRINTF("pthread_mutex_lock() returned error (%i).", pthread_retval);

			return(-1);
		}
	} else {
		if (cackey_args.LockMutex) {
3821
3822
3823
3824
3825
3826
3827
3828

3829
3830
3831
3832
3833
3834
3835
3863
3864
3865
3866
3867
3868
3869

3870
3871
3872
3873
3874
3875
3876
3877







-
+







	CK_RV custom_retval;

	CACKEY_DEBUG_PRINTF("Called.");

	if ((cackey_args.flags & CKF_OS_LOCKING_OK) == CKF_OS_LOCKING_OK) {
		pthread_mutex = mutex;

		pthread_retval = pthread_mutex_unlock(pthread_mutex);
		pthread_retval = cackey_pthread_mutex_unlock(pthread_mutex);
		if (pthread_retval != 0) {
			CACKEY_DEBUG_PRINTF("pthread_mutex_unlock() returned error (%i).", pthread_retval);

			return(-1);
		}
	} else {
		if (cackey_args.UnlockMutex) {
4614
4615
4616
4617
4618
4619
4620








4621
4622
4623
4624
4625
4626
4627
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677







+
+
+
+
+
+
+
+








		if (args->CreateMutex == NULL || args->DestroyMutex == NULL || args->LockMutex == NULL || args->UnlockMutex == NULL) {
			if (args->CreateMutex != NULL || args->DestroyMutex != NULL || args->LockMutex != NULL || args->UnlockMutex != NULL) {
				CACKEY_DEBUG_PRINTF("Error. Some, but not All threading primitives provided.");

				return(CKR_ARGUMENTS_BAD);
			}
		} else {
#ifdef HAVE_CACKEY_MUTEX_PTHREAD_FUNCS
			if (cackey_mutex_pthread_funcs()->pthread_mutex_init == NULL || cackey_mutex_pthread_funcs()->pthread_mutex_lock == NULL || cackey_mutex_pthread_funcs()->pthread_mutex_unlock == NULL) {
				CACKEY_DEBUG_PRINTF("Error. Library is not linked to pthreads and we are unable to find them at runtime.");

				return(CKR_GENERAL_ERROR);
			}
#endif
		}
	} else {
		cackey_args.CreateMutex = NULL;
		cackey_args.DestroyMutex = NULL;
		cackey_args.LockMutex = NULL;
		cackey_args.UnlockMutex = NULL;
		cackey_args.flags = 0;