Browse Source

PROV: Adapt the RSA, DSA and DH KEYMGMT implementations

They now all respond to requests for key size, bits and security bits.

Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/10778)
Richard Levitte 4 years ago
parent
commit
9e5aaf7886

+ 40 - 0
providers/implementations/keymgmt/dh_kmgmt.c

@@ -18,8 +18,10 @@
 
 static OSSL_OP_keymgmt_importdomparams_fn dh_importdomparams;
 static OSSL_OP_keymgmt_exportdomparams_fn dh_exportdomparams;
+static OSSL_OP_keymgmt_get_key_params_fn dh_get_domparam_params;
 static OSSL_OP_keymgmt_importkey_fn dh_importkey;
 static OSSL_OP_keymgmt_exportkey_fn dh_exportkey;
+static OSSL_OP_keymgmt_get_key_params_fn dh_get_key_params;
 
 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
 {
@@ -185,6 +187,41 @@ static int dh_exportkey(void *key, OSSL_CALLBACK *param_cb, void *cbarg)
     return ret;
 }
 
+/*
+ * Same function for domain parameters and for keys.
+ * "dpk" = "domain parameters & keys"
+ */
+static ossl_inline int dh_get_dpk_params(void *key, OSSL_PARAM params[])
+{
+    DH *dh = key;
+    OSSL_PARAM *p;
+
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
+        && !OSSL_PARAM_set_int(p, DH_bits(dh)))
+        return 0;
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
+        && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
+        return 0;
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
+        && !OSSL_PARAM_set_int(p, DH_size(dh)))
+        return 0;
+    return 1;
+}
+
+/*
+ * We have wrapper functions to make sure we get signatures right, see
+ * the forward declarations at the beginning of this file.
+ */
+static int dh_get_domparam_params(void *domparams, OSSL_PARAM params[])
+{
+    return dh_get_dpk_params(domparams, params);
+}
+
+static int dh_get_key_params(void *key, OSSL_PARAM params[])
+{
+    return dh_get_dpk_params(key, params);
+}
+
 const OSSL_DISPATCH dh_keymgmt_functions[] = {
     /*
      * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
@@ -192,9 +229,12 @@ const OSSL_DISPATCH dh_keymgmt_functions[] = {
      */
     { OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dh_importdomparams },
     { OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS, (void (*)(void))dh_exportdomparams },
+    { OSSL_FUNC_KEYMGMT_GET_DOMPARAM_PARAMS,
+      (void (*) (void))dh_get_domparam_params },
     { OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DH_free },
     { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dh_importkey },
     { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))dh_exportkey },
     { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DH_free },
+    { OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS,  (void (*) (void))dh_get_key_params },
     { 0, NULL }
 };

+ 40 - 4
providers/implementations/keymgmt/dsa_kmgmt.c

@@ -19,8 +19,10 @@
 
 static OSSL_OP_keymgmt_importdomparams_fn dsa_importdomparams;
 static OSSL_OP_keymgmt_exportdomparams_fn dsa_exportdomparams;
+static OSSL_OP_keymgmt_get_domparam_params_fn dsa_get_domparam_params;
 static OSSL_OP_keymgmt_importkey_fn dsa_importkey;
 static OSSL_OP_keymgmt_exportkey_fn dsa_exportkey;
+static OSSL_OP_keymgmt_get_key_params_fn dsa_get_key_params;
 
 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
 {
@@ -191,16 +193,50 @@ static int dsa_exportkey(void *key, OSSL_CALLBACK *param_cb, void *cbarg)
     return ret;
 }
 
+/*
+ * Same function for domain parameters and for keys.
+ * "dpk" = "domain parameters & keys"
+ */
+static ossl_inline int dsa_get_dpk_params(void *key, OSSL_PARAM params[])
+{
+    DSA *dsa = key;
+    OSSL_PARAM *p;
+
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
+        && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
+        return 0;
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
+        && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
+        return 0;
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
+        && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
+        return 0;
+    return 1;
+}
+
+/*
+ * We have wrapper functions to make sure we get signatures right, see
+ * the forward declarations at the beginning of this file.
+ */
+static int dsa_get_domparam_params(void *domparams, OSSL_PARAM params[])
+{
+    return dsa_get_dpk_params(domparams, params);
+}
+
+static int dsa_get_key_params(void *key, OSSL_PARAM params[])
+{
+    return dsa_get_dpk_params(key, params);
+}
+
 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
-    /*
-     * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
-     * implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
-     */
     { OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dsa_importdomparams },
     { OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS, (void (*)(void))dsa_exportdomparams },
     { OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DSA_free },
+    { OSSL_FUNC_KEYMGMT_GET_DOMPARAM_PARAMS,
+      (void (*) (void))dsa_get_domparam_params },
     { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dsa_importkey },
     { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))dsa_exportkey },
     { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DSA_free },
+    { OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS,  (void (*) (void))dsa_get_key_params },
     { 0, NULL }
 };

+ 19 - 0
providers/implementations/keymgmt/rsa_kmgmt.c

@@ -20,6 +20,7 @@
 
 static OSSL_OP_keymgmt_importkey_fn rsa_importkey;
 static OSSL_OP_keymgmt_exportkey_fn rsa_exportkey;
+static OSSL_OP_keymgmt_get_key_params_fn rsa_get_key_params;
 
 DEFINE_STACK_OF(BIGNUM)
 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
@@ -244,11 +245,29 @@ static const OSSL_PARAM *rsa_importkey_types(void)
     return rsa_key_types;
 }
 
+static int rsa_get_key_params(void *key, OSSL_PARAM params[])
+{
+    RSA *rsa = key;
+    OSSL_PARAM *p;
+
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
+        && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
+        return 0;
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
+        && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
+        return 0;
+    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
+        && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
+        return 0;
+    return 1;
+}
+
 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
     { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))rsa_importkey },
     { OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES, (void (*)(void))rsa_importkey_types },
     { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))rsa_exportkey },
     { OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES, (void (*)(void))rsa_exportkey_types },
     { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))RSA_free },
+    { OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS,  (void (*) (void))rsa_get_key_params },
     { 0, NULL }
 };