Blame SOURCES/openssl-thread-test.c

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