Blame SOURCES/openssl-thread-test.c

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