From f9b04f9e7e200be5e82cf8283a7fb9c4ead0dd37 Mon Sep 17 00:00:00 2001 From: Sahana Prasad Date: Fri, 29 Sep 2023 15:38:26 +0200 Subject: [PATCH] Tests to check gen keys through check_rsa_key() and check_ec_key() Signed-off-by: Sahana Prasad --- tests/tgenkey.c | 94 +++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/tests/tgenkey.c b/tests/tgenkey.c index 63ec4fc0..79b1096f 100644 --- a/tests/tgenkey.c +++ b/tests/tgenkey.c @@ -30,6 +30,52 @@ static void hexify(char *out, unsigned char *byte, size_t len) out[len * 3] = '\0'; } +static void check_rsa_key(EVP_PKEY *pubkey) +{ + BIGNUM *tmp = NULL; + int ret; + + ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &tmp); + if (ret != 1) { + fprintf(stderr, "Failed to get E param from public key"); + exit(EXIT_FAILURE); + } else { + BN_free(tmp); + tmp = NULL; + } + ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &tmp); + if (ret != 1) { + fprintf(stderr, "Failed to get N param from public key"); + exit(EXIT_FAILURE); + } else { + BN_free(tmp); + tmp = NULL; + } +} + +static void check_ec_key(EVP_PKEY *pubkey) +{ + BIGNUM *tmp = NULL; + int ret; + + ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_X, &tmp); + if (ret != 1) { + fprintf(stderr, "Failed to get X param from public key"); + exit(EXIT_FAILURE); + } else { + BN_free(tmp); + tmp = NULL; + } + ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_Y, &tmp); + if (ret != 1) { + fprintf(stderr, "Failed to get Y param from public key"); + exit(EXIT_FAILURE); + } else { + BN_free(tmp); + tmp = NULL; + } +} + static void check_keys(OSSL_STORE_CTX *store, const char *key_type) { OSSL_STORE_INFO *info; @@ -70,46 +116,10 @@ static void check_keys(OSSL_STORE_CTX *store, const char *key_type) } /* check we can get pub params from key */ - if (strcmp(key_type, "RSA") == 0) { - BIGNUM *tmp = NULL; - int ret; - - ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &tmp); - if (ret != 1) { - fprintf(stderr, "Failed to get E param from public key"); - exit(EXIT_FAILURE); - } else { - BN_free(tmp); - tmp = NULL; - } - ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &tmp); - if (ret != 1) { - fprintf(stderr, "Failed to get N param from public key"); - exit(EXIT_FAILURE); - } else { - BN_free(tmp); - tmp = NULL; - } + if (strcmp(key_type, "RSA") == 0 || strcmp(key_type, "RSA-PSS") == 0) { + check_rsa_key(pubkey); } else if (strcmp(key_type, "EC") == 0) { - BIGNUM *tmp = NULL; - int ret; - - ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_X, &tmp); - if (ret != 1) { - fprintf(stderr, "Failed to get X param from public key"); - exit(EXIT_FAILURE); - } else { - BN_free(tmp); - tmp = NULL; - } - ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_Y, &tmp); - if (ret != 1) { - fprintf(stderr, "Failed to get Y param from public key"); - exit(EXIT_FAILURE); - } else { - BN_free(tmp); - tmp = NULL; - } + check_ec_key(pubkey); } EVP_PKEY_free(privkey); @@ -161,6 +171,12 @@ static void gen_keys(const char *key_type, const char *label, const char *idhex, exit(EXIT_FAILURE); } + if (strcmp(key_type, "RSA") == 0 || strcmp(key_type, "RSA-PSS") == 0) { + check_rsa_key(key); + } else if (strcmp(key_type, "EC") == 0) { + check_ec_key(key); + } + EVP_PKEY_free(key); key = NULL; EVP_PKEY_CTX_free(ctx);