summaryrefslogtreecommitdiffstats
path: root/test/evp_test.c
diff options
context:
space:
mode:
authorNicola Tuveri <nic.tuv@gmail.com>2017-04-18 19:37:01 +0300
committerMatt Caswell <matt@openssl.org>2017-08-21 11:19:16 +0100
commit7b39931eeebf97a0d46af858eda9cb1943f0743c (patch)
tree78f13b244c8a4bf5627df35eae5194306f07fc23 /test/evp_test.c
parent36097bd81a8ec365c94e33ec3bac2f143cbf0b16 (diff)
evp_test.c: Add PrivPubKeyPair test
Reviewed-by: Rich Salz <rsalz@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/3246)
Diffstat (limited to 'test/evp_test.c')
-rw-r--r--test/evp_test.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/test/evp_test.c b/test/evp_test.c
index d52bbfee05..fb2ca279bf 100644
--- a/test/evp_test.c
+++ b/test/evp_test.c
@@ -273,6 +273,7 @@ static const struct evp_test_method pderive_test_method;
static const struct evp_test_method pbe_test_method;
static const struct evp_test_method encode_test_method;
static const struct evp_test_method kdf_test_method;
+static const struct evp_test_method keypair_test_method;
static const struct evp_test_method *evp_test_list[] = {
&digest_test_method,
@@ -286,6 +287,7 @@ static const struct evp_test_method *evp_test_list[] = {
&pbe_test_method,
&encode_test_method,
&kdf_test_method,
+ &keypair_test_method,
NULL
};
@@ -2040,3 +2042,131 @@ static const struct evp_test_method kdf_test_method = {
kdf_test_parse,
kdf_test_run
};
+
+struct keypair_test_data {
+ EVP_PKEY *privk;
+ EVP_PKEY *pubk;
+};
+
+static int keypair_test_init(struct evp_test *t, const char *pair)
+{
+ int rv = 0;
+ EVP_PKEY *pk = NULL, *pubk = NULL;
+ char *pub, *priv = NULL;
+ const char *err = "INTERNAL_ERROR";
+ struct keypair_test_data *data;
+
+ priv = OPENSSL_strdup(pair);
+ if (priv == NULL)
+ return 0;
+ pub = strchr(priv, ':');
+ if ( pub == NULL ) {
+ fprintf(stderr, "Wrong syntax \"%s\"\n", pair);
+ goto end;
+ }
+ *pub++ = 0; /* split priv and pub strings */
+
+ if (find_key(&pk, priv, t->private) == 0) {
+ fprintf(stderr, "Cannot find private key: %s\n", priv);
+ err = "MISSING_PRIVATE_KEY";
+ goto end;
+ }
+ if (find_key(&pubk, pub, t->public) == 0) {
+ fprintf(stderr, "Cannot find public key: %s\n", pub);
+ err = "MISSING_PUBLIC_KEY";
+ goto end;
+ }
+
+ if (pk == NULL && pubk == NULL) {
+ /* Both keys are listed but unsupported: skip this test */
+ t->skip = 1;
+ rv = 1;
+ goto end;
+ }
+
+ data = OPENSSL_malloc(sizeof(*data));
+ if (data == NULL )
+ goto end;
+
+ data->privk = pk;
+ data->pubk = pubk;
+ t->data = data;
+
+ rv = 1;
+ err = NULL;
+
+end:
+ if (priv)
+ OPENSSL_free(priv);
+ t->err = err;
+ return rv;
+}
+
+static void keypair_test_cleanup(struct evp_test *t)
+{
+ struct keypair_test_data *data = t->data;
+ t->data = NULL;
+ if (data)
+ test_free(data);
+ return;
+}
+
+/* For test that do not accept any custom keyword:
+ * return 0 if called
+ */
+static int void_test_parse(struct evp_test *t, const char *keyword, const char *value)
+{
+ return 0;
+}
+
+static int keypair_test_run(struct evp_test *t)
+{
+ int rv = 0;
+ const struct keypair_test_data *pair = t->data;
+ const char *err = "INTERNAL_ERROR";
+
+ if (pair == NULL)
+ goto end;
+
+ if (pair->privk == NULL || pair->pubk == NULL) {
+ /* this can only happen if only one of the keys is not set
+ * which means that one of them was unsupported while the
+ * other isn't: hence a key type mismatch.
+ */
+ err = "KEYPAIR_TYPE_MISMATCH";
+ rv = 1;
+ goto end;
+ }
+
+ if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
+ if ( 0 == rv ) {
+ err = "KEYPAIR_MISMATCH";
+ } else if ( -1 == rv ) {
+ err = "KEYPAIR_TYPE_MISMATCH";
+ } else if ( -2 == rv ) {
+ err = "UNSUPPORTED_KEY_COMPARISON";
+ } else {
+ fprintf(stderr, "Unexpected error in key comparison\n");
+ rv = 0;
+ goto end;
+ }
+ rv = 1;
+ goto end;
+ }
+
+ rv = 1;
+ err = NULL;
+
+end:
+ t->err = err;
+ return rv;
+}
+
+static const struct evp_test_method keypair_test_method = {
+ "PrivPubKeyPair",
+ keypair_test_init,
+ keypair_test_cleanup,
+ void_test_parse,
+ keypair_test_run
+};
+