From 2452ddfd39b7c19cf4a3bec7eba1285cd71450b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Sun, 8 Jan 2006 22:52:54 +0000 Subject: [PATCH] Add test for RSA. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@16489 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/des/test_engine_dso.c | 60 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/lib/des/test_engine_dso.c b/lib/des/test_engine_dso.c index ea8516545..3bb6d7b49 100644 --- a/lib/des/test_engine_dso.c +++ b/lib/des/test_engine_dso.c @@ -46,13 +46,16 @@ RCSID("$Id$"); #include -static int version_flag = 0; -static int help_flag = 0; -static char *id_flag = 0; +static int version_flag; +static int help_flag; +static char *id_flag; +static char *rsa_flag; static struct getargs args[] = { { "id", 0, arg_string, &id_flag, "id", NULL }, + { "rsa", 0, arg_string, &rsa_flag, + "rsa-der-file", NULL }, { "version", 0, arg_flag, &version_flag, "print version", NULL }, { "help", 0, arg_flag, &help_flag, @@ -74,6 +77,7 @@ main(int argc, char **argv) { ENGINE *engine; int idx = 0; + int have_rsa; setprogname(argv[0]); @@ -100,9 +104,57 @@ main(int argc, char **argv) printf("name: %s\n", ENGINE_get_name(engine)); printf("id: %s\n", ENGINE_get_id(engine)); - printf("RSA: %s\n", ENGINE_get_RSA(engine) ? "yes" : "no"); + have_rsa = ENGINE_get_RSA(engine) != NULL; + printf("RSA: %s\n", have_rsa ? "yes" : "no"); printf("DH: %s\n", ENGINE_get_DH(engine) ? "yes" : "no"); + if (rsa_flag && have_rsa) { + unsigned char buf[1024 * 4]; + const unsigned char *p; + size_t size; + int keylen; + RSA *rsa; + FILE *f; + + f = fopen(rsa_flag, "r"); + if (f == NULL) + err(1, "could not open file %s", rsa_flag); + + size = fread(buf, 1, sizeof(buf), f); + if (size == 0) + err(1, "failed to read file %s", rsa_flag); + if (size == sizeof(buf)) + err(1, "key too long in file %s!", rsa_flag); + fclose(f); + + p = buf; + rsa = d2i_RSAPrivateKey(NULL, &p, size); + if (rsa == NULL) + err(1, "failed to parse key in file %s", rsa_flag); + + RSA_set_method(rsa, ENGINE_get_RSA(engine)); + + memcpy(buf, "hejsan", 7); + keylen = RSA_private_encrypt(7, buf, buf, rsa, RSA_PKCS1_PADDING); + if (keylen <= 0) + errx(1, "failed to private encrypt"); + + keylen = RSA_public_decrypt(keylen, buf, buf, rsa, RSA_PKCS1_PADDING); + if (keylen <= 0) + errx(1, "failed to public decrypt"); + + if (keylen != 7) + errx(1, "output buffer not same length"); + + if (memcmp(buf, "hejsan", 7) != 0) + errx(1, "string not the same after decryption"); + + RSA_free(rsa); + + printf("rsa test passed\n"); + + } + ENGINE_finish(engine); return 0;