EVP: Implement support for key downgrading in backends
[openssl.git] / providers / implementations / keymgmt / rsa_kmgmt.c
index 2826d337b4fa34049733cc9df3c1708c691bef4b..176cf34e0abe5ec3be4294a20829cfd4f4b5cb75 100644 (file)
@@ -51,80 +51,6 @@ static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
 DEFINE_STACK_OF(BIGNUM)
 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
 
-static int collect_numbers(STACK_OF(BIGNUM) *numbers,
-                           const OSSL_PARAM params[], const char *key)
-{
-    const OSSL_PARAM *p = NULL;
-
-    if (numbers == NULL)
-        return 0;
-
-    for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
-        BIGNUM *tmp = NULL;
-
-        if (!OSSL_PARAM_get_BN(p, &tmp))
-            return 0;
-        sk_BIGNUM_push(numbers, tmp);
-    }
-
-    return 1;
-}
-
-static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
-{
-    const OSSL_PARAM *param_n, *param_e,  *param_d;
-    BIGNUM *n = NULL, *e = NULL, *d = NULL;
-    STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
-    int is_private = 0;
-
-    if (rsa == NULL)
-        return 0;
-
-    param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
-    param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
-    param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
-
-    if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
-        || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
-        || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
-        goto err;
-
-    is_private = (d != NULL);
-
-    if (!RSA_set0_key(rsa, n, e, d))
-        goto err;
-    n = e = d = NULL;
-
-    if (is_private) {
-        if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
-                             OSSL_PKEY_PARAM_RSA_FACTOR)
-            || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
-                                OSSL_PKEY_PARAM_RSA_EXPONENT)
-            || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
-                                OSSL_PKEY_PARAM_RSA_COEFFICIENT))
-            goto err;
-
-        /* It's ok if this private key just has n, e and d */
-        if (sk_BIGNUM_num(factors) != 0
-            && !rsa_set0_all_params(rsa, factors, exps, coeffs))
-            goto err;
-    }
-
-    sk_BIGNUM_free(factors);
-    sk_BIGNUM_free(exps);
-    sk_BIGNUM_free(coeffs);
-    return 1;
-
- err:
-    BN_free(n);
-    BN_free(e);
-    BN_free(d);
-    sk_BIGNUM_pop_free(factors, BN_free);
-    sk_BIGNUM_pop_free(exps, BN_free);
-    sk_BIGNUM_pop_free(coeffs, BN_free);
-    return 0;
-}
-
 static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
                           STACK_OF(BIGNUM_const) *numbers)
 {
@@ -240,7 +166,7 @@ static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
     /* TODO(3.0) PSS and OAEP should bring on parameters */
 
     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
-        ok = ok && params_to_key(rsa, params);
+        ok = ok && rsa_fromdata(rsa, params);
 
     return ok;
 }