Blame SOURCES/openssl-thread-test.c

e62613
/* Test program to verify that RSA signing is thread-safe in OpenSSL. */
e62613
e62613
#include <assert.h>
e62613
#include <errno.h>
e62613
#include <fcntl.h>
e62613
#include <limits.h>
e62613
#include <pthread.h>
e62613
#include <stdio.h>
e62613
#include <string.h>
e62613
#include <unistd.h>
e62613
e62613
#include <openssl/crypto.h>
e62613
#include <openssl/err.h>
e62613
#include <openssl/objects.h>
e62613
#include <openssl/rand.h>
e62613
#include <openssl/rsa.h>
e62613
#include <openssl/md5.h>
e62613
#include <openssl/ssl.h>
e62613
e62613
/* Just assume we want to do engine stuff if we're using 0.9.6b or
e62613
 * higher. This assumption is only valid for versions bundled with RHL. */
e62613
#if OPENSSL_VERSION_NUMBER  >= 0x0090602fL
e62613
#include <openssl/engine.h>
e62613
#define USE_ENGINE
e62613
#endif
e62613
e62613
#define MAX_THREAD_COUNT	10000
e62613
#define ITERATION_COUNT		10
e62613
#define MAIN_COUNT		100
e62613
e62613
/* OpenSSL requires us to provide thread ID and locking primitives. */
e62613
pthread_mutex_t *mutex_locks = NULL;
e62613
static unsigned long
e62613
thread_id_cb(void)
e62613
{
e62613
	return (unsigned long) pthread_self();
e62613
}
e62613
static void
e62613
lock_cb(int mode, int n, const char *file, int line)
e62613
{
e62613
	if (mode & CRYPTO_LOCK) {
e62613
		pthread_mutex_lock(&mutex_locks[n]);
e62613
	} else {
e62613
		pthread_mutex_unlock(&mutex_locks[n]);
e62613
	}
e62613
}
e62613
e62613
struct thread_args {
e62613
	RSA *rsa;
e62613
	int digest_type;
e62613
	unsigned char *digest;
e62613
	unsigned int digest_len;
e62613
	unsigned char *signature;
e62613
	unsigned int signature_len;
e62613
	pthread_t main_thread;
e62613
};
e62613
e62613
static int print = 0;
e62613
e62613
pthread_mutex_t sign_lock = PTHREAD_MUTEX_INITIALIZER;
e62613
static int locked_sign = 0;
e62613
static void SIGN_LOCK() {if (locked_sign) pthread_mutex_lock(&sign_lock);}
e62613
static void SIGN_UNLOCK() {if (locked_sign) pthread_mutex_unlock(&sign_lock);}
e62613
e62613
pthread_mutex_t verify_lock = PTHREAD_MUTEX_INITIALIZER;
e62613
static int locked_verify = 0;
e62613
static void VERIFY_LOCK() {if (locked_verify) pthread_mutex_lock(&verify_lock);}
e62613
static void VERIFY_UNLOCK() {if (locked_verify) pthread_mutex_unlock(&verify_lock);}
e62613
e62613
pthread_mutex_t failure_count_lock = PTHREAD_MUTEX_INITIALIZER;
e62613
long failure_count = 0;
e62613
static void
e62613
failure()
e62613
{
e62613
	pthread_mutex_lock(&failure_count_lock);
e62613
	failure_count++;
e62613
	pthread_mutex_unlock(&failure_count_lock);
e62613
}
e62613
e62613
static void *
e62613
thread_main(void *argp)
e62613
{
e62613
	struct thread_args *args = argp;
e62613
	unsigned char *signature;
e62613
	unsigned int signature_len, signature_alloc_len;
e62613
	int ret, i;
e62613
e62613
	signature_alloc_len = args->signature_len;
e62613
	if (RSA_size(args->rsa) > signature_alloc_len) {
e62613
		signature_alloc_len = RSA_size(args->rsa);
e62613
	}
e62613
	signature = malloc(signature_alloc_len);
e62613
	if (signature == NULL) {
e62613
		fprintf(stderr, "Skipping checks in thread %lu -- %s.\n",
e62613
			(unsigned long) pthread_self(), strerror(errno));
e62613
		pthread_exit(0);
e62613
		return NULL;
e62613
	}
e62613
	for (i = 0; i < ITERATION_COUNT; i++) {
e62613
		signature_len = signature_alloc_len;
e62613
		SIGN_LOCK();
e62613
		ret = RSA_check_key(args->rsa);
e62613
		ERR_print_errors_fp(stdout);
e62613
		if (ret != 1) {
e62613
			failure();
e62613
			break;
e62613
		}
e62613
		ret = RSA_sign(args->digest_type,
e62613
			       args->digest,
e62613
			       args->digest_len,
e62613
			       signature, &signature_len,
e62613
			       args->rsa);
e62613
		SIGN_UNLOCK();
e62613
		ERR_print_errors_fp(stdout);
e62613
		if (ret != 1) {
e62613
			failure();
e62613
			break;
e62613
		}
e62613
e62613
		VERIFY_LOCK();
e62613
		ret = RSA_verify(args->digest_type,
e62613
			         args->digest,
e62613
			         args->digest_len,
e62613
			         signature, signature_len,
e62613
			         args->rsa);
e62613
		VERIFY_UNLOCK();
e62613
		if (ret != 1) {
e62613
			fprintf(stderr,
e62613
				"Signature from thread %lu(%d) fails "
e62613
				"verification (passed in thread #%lu)!\n",
e62613
				(long) pthread_self(), i,
e62613
				(long) args->main_thread);
e62613
			ERR_print_errors_fp(stdout);
e62613
			failure();
e62613
			continue;
e62613
		}
e62613
		if (print) {
e62613
			fprintf(stderr, ">%d\n", i);
e62613
		}
e62613
	}
e62613
	free(signature);
e62613
e62613
	pthread_exit(0);
e62613
e62613
	return NULL;
e62613
}
e62613
e62613
unsigned char *
e62613
xmemdup(unsigned char *s, size_t len)
e62613
{
e62613
	unsigned char *r;
e62613
	r = malloc(len);
e62613
	if (r == NULL) {
e62613
		fprintf(stderr, "Out of memory.\n");
e62613
		ERR_print_errors_fp(stdout);
e62613
		assert(r != NULL);
e62613
	}
e62613
	memcpy(r, s, len);
e62613
	return r;
e62613
}
e62613
e62613
int
e62613
main(int argc, char **argv)
e62613
{
e62613
	RSA *rsa;
e62613
	MD5_CTX md5;
e62613
	int fd, i;
e62613
	pthread_t threads[MAX_THREAD_COUNT];
e62613
	int thread_count = 1000;
e62613
	unsigned char *message, *digest;
e62613
	unsigned int message_len, digest_len;
e62613
	unsigned char *correct_signature;
e62613
	unsigned int correct_siglen, ret;
e62613
	struct thread_args master_args, *args;
e62613
	int sync = 0, seed = 0;
e62613
	int again = 1;
e62613
#ifdef USE_ENGINE
e62613
	char *engine = NULL;
e62613
	ENGINE *e = NULL;
e62613
#endif
e62613
e62613
	pthread_mutex_init(&failure_count_lock, NULL);
e62613
e62613
	for (i = 1; i < argc; i++) {
e62613
		if (strcmp(argv[i], "--seed") == 0) {
e62613
			printf("Seeding PRNG.\n");
e62613
			seed++;
e62613
		} else
e62613
		if (strcmp(argv[i], "--sync") == 0) {
e62613
			printf("Running synchronized.\n");
e62613
			sync++;
e62613
		} else
e62613
		if ((strcmp(argv[i], "--threads") == 0) && (i < argc - 1)) {
e62613
			i++;
e62613
			thread_count = atol(argv[i]);
e62613
			if (thread_count > MAX_THREAD_COUNT) {
e62613
				thread_count = MAX_THREAD_COUNT;
e62613
			}
e62613
			printf("Starting %d threads.\n", thread_count);
e62613
			sync++;
e62613
		} else
e62613
		if (strcmp(argv[i], "--sign") == 0) {
e62613
			printf("Locking signing.\n");
e62613
			locked_sign++;
e62613
		} else
e62613
		if (strcmp(argv[i], "--verify") == 0) {
e62613
			printf("Locking verifies.\n");
e62613
			locked_verify++;
e62613
		} else
e62613
		if (strcmp(argv[i], "--print") == 0) {
e62613
			printf("Tracing.\n");
e62613
			print++;
e62613
#ifdef USE_ENGINE
e62613
		} else
e62613
		if ((strcmp(argv[i], "--engine") == 0) && (i < argc - 1)) {
e62613
			printf("Using engine \"%s\".\n", argv[i + 1]);
e62613
			engine = argv[i + 1];
e62613
			i++;
e62613
#endif
e62613
		} else {
e62613
			printf("Bad argument: %s\n", argv[i]);
e62613
			return 1;
e62613
		}
e62613
	}
e62613
e62613
	/* Get some random data to sign. */
e62613
	fd = open("/dev/urandom", O_RDONLY);
e62613
	if (fd == -1) {
e62613
		fprintf(stderr, "Error opening /dev/urandom: %s\n",
e62613
			strerror(errno));
e62613
	}
e62613
e62613
	if (print) {
e62613
		fprintf(stderr, "Reading random data.\n");
e62613
	}
e62613
	message = malloc(message_len = 9371);
e62613
	read(fd, message, message_len);
e62613
	close(fd);
e62613
e62613
	/* Initialize the SSL library and set up thread-safe locking. */
e62613
	ERR_load_crypto_strings();
e62613
	SSL_library_init();
e62613
	mutex_locks = malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
e62613
	for (i = 0; i < CRYPTO_num_locks(); i++) {
e62613
		pthread_mutex_init(&mutex_locks[i], NULL);
e62613
	}
e62613
	CRYPTO_set_id_callback(thread_id_cb);
e62613
	CRYPTO_set_locking_callback(lock_cb);
e62613
	ERR_print_errors_fp(stdout);
e62613
e62613
	/* Seed the PRNG if we were asked to do so. */
e62613
	if (seed) {
e62613
		if (print) {
e62613
			fprintf(stderr, "Seeding PRNG.\n");
e62613
		}
e62613
		RAND_add(message, message_len, message_len);
e62613
		ERR_print_errors_fp(stdout);
e62613
	}
e62613
e62613
	/* Turn on a hardware crypto device if asked to do so. */
e62613
#ifdef USE_ENGINE
e62613
	if (engine) {
e62613
#if OPENSSL_VERSION_NUMBER  >= 0x0090700fL
e62613
		ENGINE_load_builtin_engines();
e62613
#endif
e62613
		if (print) {
e62613
			fprintf(stderr, "Initializing \"%s\" engine.\n",
e62613
				engine);
e62613
		}
e62613
		e = ENGINE_by_id(engine);
e62613
		ERR_print_errors_fp(stdout);
e62613
		if (e) {
e62613
			i = ENGINE_init(e);
e62613
			ERR_print_errors_fp(stdout);
e62613
			i = ENGINE_set_default_RSA(e);
e62613
			ERR_print_errors_fp(stdout);
e62613
		}
e62613
	}
e62613
#endif
e62613
e62613
	/* Compute the digest for the signature. */
e62613
	if (print) {
e62613
		fprintf(stderr, "Computing digest.\n");
e62613
	}
e62613
	digest = malloc(digest_len = MD5_DIGEST_LENGTH);
e62613
	MD5_Init(&md5;;
e62613
	MD5_Update(&md5, message, message_len);
e62613
	MD5_Final(digest, &md5;;
e62613
e62613
	/* Generate a signing key. */
e62613
	if (print) {
e62613
		fprintf(stderr, "Generating key.\n");
e62613
	}
e62613
	rsa = RSA_generate_key(4096, 3, NULL, NULL);
e62613
	ERR_print_errors_fp(stdout);
e62613
	if (rsa == NULL) {
e62613
		_exit(1);
e62613
	}
e62613
e62613
	/* Sign the data. */
e62613
	correct_siglen = RSA_size(rsa);
e62613
	correct_signature = malloc(correct_siglen);
e62613
	for (i = 0; i < MAIN_COUNT; i++) {
e62613
		if (print) {
e62613
			fprintf(stderr, "Signing data (%d).\n", i);
e62613
		}
e62613
		ret = RSA_check_key(rsa);
e62613
		ERR_print_errors_fp(stdout);
e62613
		if (ret != 1) {
e62613
			failure();
e62613
		}
e62613
		correct_siglen = RSA_size(rsa);
e62613
		ret = RSA_sign(NID_md5, digest, digest_len,
e62613
			       correct_signature, &correct_siglen,
e62613
			       rsa);
e62613
		ERR_print_errors_fp(stdout);
e62613
		if (ret != 1) {
e62613
			_exit(2);
e62613
		}
e62613
		if (print) {
e62613
			fprintf(stderr, "Verifying data (%d).\n", i);
e62613
		}
e62613
		ret = RSA_verify(NID_md5, digest, digest_len,
e62613
			         correct_signature, correct_siglen,
e62613
			         rsa);
e62613
		if (ret != 1) {
e62613
			_exit(2);
e62613
		}
e62613
	}
e62613
e62613
	/* Collect up the inforamtion which other threads will need for
e62613
	 * comparing their signature results with ours. */
e62613
	master_args.rsa = rsa;
e62613
	master_args.digest_type = NID_md5;
e62613
	master_args.digest = digest;
e62613
	master_args.digest_len = digest_len;
e62613
	master_args.signature = correct_signature;
e62613
	master_args.signature_len = correct_siglen;
e62613
	master_args.main_thread = pthread_self();
e62613
	
e62613
	fprintf(stdout, "Performing %d signatures in each of %d threads "
e62613
		"(%d, %d).\n", ITERATION_COUNT, thread_count,
e62613
		digest_len, correct_siglen);
e62613
	fflush(NULL);
e62613
e62613
	/* Start up all of the threads. */
e62613
	for (i = 0; i < thread_count; i++) {
e62613
		args = malloc(sizeof(struct thread_args));
e62613
		args->rsa = RSAPrivateKey_dup(master_args.rsa);
e62613
		args->digest_type = master_args.digest_type;
e62613
		args->digest_len = master_args.digest_len;
e62613
		args->digest = xmemdup(master_args.digest, args->digest_len);
e62613
		args->signature_len = master_args.signature_len;
e62613
		args->signature = xmemdup(master_args.signature,
e62613
					  args->signature_len);
e62613
		args->main_thread = pthread_self();
e62613
		ret = pthread_create(&threads[i], NULL, thread_main, args);
e62613
		while ((ret != 0) && (errno == EAGAIN)) {
e62613
			ret = pthread_create(&threads[i], NULL,
e62613
					     thread_main, &args);
e62613
			fprintf(stderr, "Thread limit hit at %d.\n", i);
e62613
		}
e62613
		if (ret != 0) {
e62613
			fprintf(stderr, "Unable to create thread %d: %s.\n",
e62613
				i, strerror(errno));
e62613
			threads[i] = -1;
e62613
		} else {
e62613
			if (sync) {
e62613
				ret = pthread_join(threads[i], NULL);
e62613
				assert(ret == 0);
e62613
			}
e62613
			if (print) {
e62613
				fprintf(stderr, "%d\n", i);
e62613
			}
e62613
		}
e62613
	}
e62613
e62613
	/* Wait for all threads to complete.  So long as we can find an
e62613
	 * unjoined thread, keep joining threads. */
e62613
	do {
e62613
		again = 0;
e62613
		for (i = 0; i < thread_count; i++) {
e62613
			/* If we have an unterminated thread, join it. */
e62613
			if (threads[i] != -1) {
e62613
				again = 1;
e62613
				if (print) {
e62613
					fprintf(stderr, "Joining thread %d.\n",
e62613
						i);
e62613
				}
e62613
				pthread_join(threads[i], NULL);
e62613
				threads[i] = -1;
e62613
				break;
e62613
			}
e62613
		}
e62613
	} while (again == 1);
e62613
e62613
	fprintf(stderr, "%ld failures\n", failure_count);
e62613
e62613
	return (failure_count != 0);
e62613
}