Blame SOURCES/openssl-thread-test.c

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