rename mac_key_* to ossl_mac_key_*
[openssl.git] / providers / implementations / keymgmt / mac_legacy_kmgmt.c
index 779240e036f8f72d65a1e1f0032b2246ef0f63d4..08de2a07ccec25d18212b80880903980c2b2168b 100644 (file)
@@ -29,6 +29,8 @@ static OSSL_FUNC_keymgmt_free_fn mac_free;
 static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init;
 static OSSL_FUNC_keymgmt_gen_fn mac_gen;
 static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup;
+static OSSL_FUNC_keymgmt_gen_set_params_fn mac_gen_set_params;
+static OSSL_FUNC_keymgmt_gen_settable_params_fn mac_gen_settable_params;
 static OSSL_FUNC_keymgmt_get_params_fn mac_get_params;
 static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params;
 static OSSL_FUNC_keymgmt_set_params_fn mac_set_params;
@@ -40,18 +42,29 @@ static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types;
 static OSSL_FUNC_keymgmt_export_fn mac_export;
 static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types;
 
+static OSSL_FUNC_keymgmt_new_fn mac_new_cmac;
+static OSSL_FUNC_keymgmt_gettable_params_fn cmac_gettable_params;
+static OSSL_FUNC_keymgmt_import_types_fn cmac_imexport_types;
+static OSSL_FUNC_keymgmt_export_types_fn cmac_imexport_types;
+static OSSL_FUNC_keymgmt_gen_set_params_fn cmac_gen_set_params;
+static OSSL_FUNC_keymgmt_gen_settable_params_fn cmac_gen_settable_params;
+
 struct mac_gen_ctx {
-    OPENSSL_CTX *libctx;
+    OSSL_LIB_CTX *libctx;
     int selection;
     unsigned char *priv_key;
     size_t priv_key_len;
     PROV_CIPHER cipher;
 };
 
-MAC_KEY *mac_key_new(OPENSSL_CTX *libctx, int cmac)
+MAC_KEY *ossl_mac_key_new(OSSL_LIB_CTX *libctx, int cmac)
 {
-    MAC_KEY *mackey = OPENSSL_zalloc(sizeof(*mackey));
+    MAC_KEY *mackey;
+
+    if (!ossl_prov_is_running())
+        return NULL;
 
+    mackey = OPENSSL_zalloc(sizeof(*mackey));
     if (mackey == NULL)
         return NULL;
 
@@ -67,7 +80,7 @@ MAC_KEY *mac_key_new(OPENSSL_CTX *libctx, int cmac)
     return mackey;
 }
 
-void mac_key_free(MAC_KEY *mackey)
+void ossl_mac_key_free(MAC_KEY *mackey)
 {
     int ref = 0;
 
@@ -85,35 +98,45 @@ void mac_key_free(MAC_KEY *mackey)
     OPENSSL_free(mackey);
 }
 
-int mac_key_up_ref(MAC_KEY *mackey)
+int ossl_mac_key_up_ref(MAC_KEY *mackey)
 {
     int ref = 0;
 
+    /* This is effectively doing a new operation on the MAC_KEY and should be
+     * adequately guarded again modules' error states.  However, both current
+     * calls here are guarded propery in signature/mac_legacy.c.  Thus, it
+     * could be removed here.  The concern is that something in the future
+     * might call this function without adequate guards.  It's a cheap call,
+     * it seems best to leave it even though it is currently redundant.
+     */
+    if (!ossl_prov_is_running())
+        return 0;
+
     CRYPTO_UP_REF(&mackey->refcnt, &ref, mackey->lock);
     return 1;
 }
 
 static void *mac_new(void *provctx)
 {
-    return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx), 0);
+    return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0);
 }
 
 static void *mac_new_cmac(void *provctx)
 {
-    return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx), 1);
+    return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1);
 }
 
 static void mac_free(void *mackey)
 {
-    mac_key_free(mackey);
+    ossl_mac_key_free(mackey);
 }
 
-static int mac_has(void *keydata, int selection)
+static int mac_has(const void *keydata, int selection)
 {
-    MAC_KEY *key = keydata;
+    const MAC_KEY *key = keydata;
     int ok = 0;
 
-    if (key != NULL) {
+    if (ossl_prov_is_running() && key != NULL) {
         /*
          * MAC keys always have all the parameters they need (i.e. none).
          * Therefore we always return with 1, if asked about parameters.
@@ -133,6 +156,9 @@ static int mac_match(const void *keydata1, const void *keydata2, int selection)
     const MAC_KEY *key2 = keydata2;
     int ok = 1;
 
+    if (!ossl_prov_is_running())
+        return 0;
+
     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
         if ((key1->priv_key == NULL && key2->priv_key != NULL)
                 || (key1->priv_key != NULL && key2->priv_key == NULL)
@@ -201,7 +227,7 @@ static int mac_import(void *keydata, int selection, const OSSL_PARAM params[])
 {
     MAC_KEY *key = keydata;
 
-    if (key == NULL)
+    if (!ossl_prov_is_running() || key == NULL)
         return 0;
 
     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
@@ -247,7 +273,7 @@ static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
     OSSL_PARAM *params = NULL;
     int ret = 0;
 
-    if (key == NULL)
+    if (!ossl_prov_is_running() || key == NULL)
         return 0;
 
     tmpl = OSSL_PARAM_BLD_new();
@@ -320,7 +346,6 @@ static const OSSL_PARAM *cmac_gettable_params(void *provctx)
     return gettable_params;
 }
 
-
 static int mac_set_params(void *keydata, const OSSL_PARAM params[])
 {
     MAC_KEY *key = keydata;
@@ -347,9 +372,12 @@ static const OSSL_PARAM *mac_settable_params(void *provctx)
 
 static void *mac_gen_init(void *provctx, int selection)
 {
-    OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
+    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
     struct mac_gen_ctx *gctx = NULL;
 
+    if (!ossl_prov_is_running())
+        return NULL;
+
     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
         gctx->libctx = libctx;
         gctx->selection = selection;
@@ -423,10 +451,10 @@ static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg)
     struct mac_gen_ctx *gctx = genctx;
     MAC_KEY *key;
 
-    if (gctx == NULL)
+    if (!ossl_prov_is_running() || gctx == NULL)
         return NULL;
 
-    if ((key = mac_key_new(gctx->libctx, 0)) == NULL) {
+    if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) {
         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
@@ -437,7 +465,7 @@ static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg)
 
     if (gctx->priv_key == NULL) {
         ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY);
-        mac_key_free(key);
+        ossl_mac_key_free(key);
         return NULL;
     }
 
@@ -469,7 +497,7 @@ static void mac_gen_cleanup(void *genctx)
     OPENSSL_free(gctx);
 }
 
-const OSSL_DISPATCH mac_keymgmt_functions[] = {
+const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = {
     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new },
     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
@@ -491,7 +519,7 @@ const OSSL_DISPATCH mac_keymgmt_functions[] = {
     { 0, NULL }
 };
 
-const OSSL_DISPATCH cmac_keymgmt_functions[] = {
+const OSSL_DISPATCH ossl_cossl_mac_legacy_keymgmt_functions[] = {
     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac },
     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
@@ -512,3 +540,4 @@ const OSSL_DISPATCH cmac_keymgmt_functions[] = {
     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },
     { 0, NULL }
 };
+