+
+/*
+ * evp_keymgmt_util_match() doesn't just look at the provider side "origin",
+ * but also in the operation cache to see if there's any common keymgmt that
+ * supplies OP_keymgmt_match.
+ *
+ * evp_keymgmt_util_match() adheres to the return values that EVP_PKEY_eq()
+ * and EVP_PKEY_parameters_eq() return, i.e.:
+ *
+ * 1 same key
+ * 0 not same key
+ * -1 not same key type
+ * -2 unsupported operation
+ */
+int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection)
+{
+ EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
+ void *keydata1 = NULL, *keydata2 = NULL;
+
+ if (pk1 == NULL || pk2 == NULL) {
+ if (pk1 == NULL && pk2 == NULL)
+ return 1;
+ return 0;
+ }
+
+ keymgmt1 = pk1->keymgmt;
+ keydata1 = pk1->keydata;
+ keymgmt2 = pk2->keymgmt;
+ keydata2 = pk2->keydata;
+
+ if (keymgmt1 != keymgmt2) {
+ /*
+ * The condition for a successful cross export is that the
+ * keydata to be exported is NULL (typed, but otherwise empty
+ * EVP_PKEY), or that it was possible to export it with
+ * evp_keymgmt_util_export_to_provider().
+ *
+ * We use |ok| to determine if it's ok to cross export one way,
+ * but also to determine if we should attempt a cross export
+ * the other way. There's no point doing it both ways.
+ */
+ int ok = 1;
+
+ /* Complex case, where the keymgmt differ */
+ if (keymgmt1 != NULL
+ && keymgmt2 != NULL
+ && !match_type(keymgmt1, keymgmt2)) {
+ ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
+ return -1; /* Not the same type */
+ }
+
+ /*
+ * The key types are determined to match, so we try cross export,
+ * but only to keymgmt's that supply a matching function.
+ */
+ if (keymgmt2 != NULL
+ && keymgmt2->match != NULL) {
+ void *tmp_keydata = NULL;
+
+ ok = 1;
+ if (keydata1 != NULL) {
+ tmp_keydata =
+ evp_keymgmt_util_export_to_provider(pk1, keymgmt2);
+ ok = (tmp_keydata != NULL);
+ }
+ if (ok) {
+ keymgmt1 = keymgmt2;
+ keydata1 = tmp_keydata;
+ }
+ }
+ /*
+ * If we've successfully cross exported one way, there's no point
+ * doing it the other way, hence the |!ok| check.
+ */
+ if (!ok
+ && keymgmt1 != NULL
+ && keymgmt1->match != NULL) {
+ void *tmp_keydata = NULL;
+
+ ok = 1;
+ if (keydata2 != NULL) {
+ tmp_keydata =
+ evp_keymgmt_util_export_to_provider(pk2, keymgmt1);
+ ok = (tmp_keydata != NULL);
+ }
+ if (ok) {
+ keymgmt2 = keymgmt1;
+ keydata2 = tmp_keydata;
+ }
+ }
+ }
+
+ /* If we still don't have matching keymgmt implementations, we give up */
+ if (keymgmt1 != keymgmt2)
+ return -2;
+
+ /* If both keydata are NULL, then they're the same key */
+ if (keydata1 == NULL && keydata2 == NULL)
+ return 1;
+ /* If only one of the keydata is NULL, then they're different keys */
+ if (keydata1 == NULL || keydata2 == NULL)
+ return 0;
+ /* If both keydata are non-NULL, we let the backend decide */
+ return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
+}
+
+int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection)
+{
+ /* Save copies of pointers we want to play with without affecting |to| */
+ EVP_KEYMGMT *to_keymgmt = to->keymgmt;
+ void *to_keydata = to->keydata, *alloc_keydata = NULL;
+
+ /* An unassigned key can't be copied */
+ if (from == NULL || from->keydata == NULL)
+ return 0;
+
+ /*
+ * If |to| is unassigned, ensure it gets the same KEYMGMT as |from|,
+ * Note that the final setting of KEYMGMT is done further down, with
+ * EVP_PKEY_set_type_by_keymgmt(); we don't want to do that prematurely.
+ */
+ if (to_keymgmt == NULL)
+ to_keymgmt = from->keymgmt;
+
+ if (to_keymgmt == from->keymgmt && to_keymgmt->copy != NULL) {
+ /* Make sure there's somewhere to copy to */
+ if (to_keydata == NULL
+ && ((to_keydata = alloc_keydata = evp_keymgmt_newdata(to_keymgmt))
+ == NULL)) {
+ ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+
+ /*
+ * |to| and |from| have the same keymgmt, and the copy function is
+ * implemented, so just copy and be done
+ */
+ if (!evp_keymgmt_copy(to_keymgmt, to_keydata, from->keydata,
+ selection))
+ return 0;
+ } else if (match_type(to_keymgmt, from->keymgmt)) {
+ struct evp_keymgmt_util_try_import_data_st import_data;
+
+ import_data.keymgmt = to_keymgmt;
+ import_data.keydata = to_keydata;
+ import_data.selection = selection;
+
+ if (!evp_keymgmt_util_export(from, selection,
+ &evp_keymgmt_util_try_import,
+ &import_data)) {
+ evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
+ return 0;
+ }
+
+ /*
+ * In case to_keydata was previously unallocated,
+ * evp_keymgmt_util_try_import() may have created it for us.
+ */
+ if (to_keydata == NULL)
+ to_keydata = alloc_keydata = import_data.keydata;
+ } else {
+ ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
+ return 0;
+ }
+
+ /*
+ * We only need to set the |to| type when its |keymgmt| isn't set.
+ * We can then just set its |keydata| to what we have, which might
+ * be exactly what it had when entering this function.
+ * This is a bit different from using evp_keymgmt_util_assign_pkey(),
+ * which isn't as careful with |to|'s original |keymgmt|, since it's
+ * meant to forcibly reassign an EVP_PKEY no matter what, which is
+ * why we don't use that one here.
+ */
+ if (to->keymgmt == NULL
+ && !EVP_PKEY_set_type_by_keymgmt(to, to_keymgmt)) {
+ evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
+ return 0;
+ }
+ to->keydata = to_keydata;
+ evp_keymgmt_util_cache_keyinfo(to);
+
+ return 1;
+}
+
+void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
+ void *genctx, OSSL_CALLBACK *cb, void *cbarg)
+{
+ void *keydata = NULL;
+
+ if ((keydata = evp_keymgmt_gen(keymgmt, genctx, cb, cbarg)) == NULL
+ || !evp_keymgmt_util_assign_pkey(target, keymgmt, keydata)) {
+ evp_keymgmt_freedata(keymgmt, keydata);
+ keydata = NULL;
+ }
+
+ return keydata;
+}
+
+/*
+ * Returns the same numbers as EVP_PKEY_get_default_digest_name()
+ * When the string from the EVP_KEYMGMT implementation is "", we use
+ * SN_undef, since that corresponds to what EVP_PKEY_get_default_nid()
+ * returns for no digest.
+ */
+int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
+ void *keydata,
+ char *mdname, size_t mdname_sz)
+{
+ OSSL_PARAM params[3];
+ char mddefault[100] = "";
+ char mdmandatory[100] = "";
+ char *result = NULL;
+ int rv = -2;
+
+ params[0] =
+ OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
+ mddefault, sizeof(mddefault));
+ params[1] =
+ OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
+ mdmandatory,
+ sizeof(mdmandatory));
+ params[2] = OSSL_PARAM_construct_end();
+
+ if (!evp_keymgmt_get_params(keymgmt, keydata, params))
+ return 0;
+
+ if (OSSL_PARAM_modified(params + 1)) {
+ if (params[1].return_size <= 1) /* Only a NUL byte */
+ result = SN_undef;
+ else
+ result = mdmandatory;
+ rv = 2;
+ } else if (OSSL_PARAM_modified(params)) {
+ if (params[0].return_size <= 1) /* Only a NUL byte */
+ result = SN_undef;
+ else
+ result = mddefault;
+ rv = 1;
+ }
+ if (rv > 0)
+ OPENSSL_strlcpy(mdname, result, mdname_sz);
+ return rv;
+}