1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include "plugin_adapter.h"
18
19 #include <stddef.h>
20 #include "cred_plugin_def.h"
21 #include "cred_manager.h"
22 #include "dev_auth_dynamic_load.h"
23 #include "dev_auth_module_manager.h"
24 #include "device_auth_defines.h"
25 #include "device_auth_ext.h"
26 #include "ext_part_proxy.h"
27 #include "hc_log.h"
28
29 typedef const CredPlugin *(*GetCredPluginFunc)(void);
30 typedef const AuthModuleBase *(*GetAuthModulePluginFunc)(void);
31
32 #define CRED_PLUGIN_FUNC "GetExtendCredPlugin"
33 #define AUTH_MODULE_PLUGIN_FUNC "GetExtendAuthModulePlugin"
34
35 #define FUNC_NAME_INIT_EXT_PART "InitExtPart"
36 #define FUNC_NAME_EXT_PLUGIN_LIST "GetExtPlugins"
37 #define FUNC_NAME_DESTROY_EXT_PART "DestroyExtPart"
38 #define LIBDEVICE_AUTH_EXT "libdevice_auth_ext.z.so"
39
40 static void *g_handle = NULL;
41 static ExtPartProxy g_pluginFunc;
42
GetPluginFuncFromLib(void * handle)43 static const ExtPartProxy *GetPluginFuncFromLib(void *handle)
44 {
45 do {
46 g_pluginFunc.initExtPartFunc = DevAuthDlsym(handle, FUNC_NAME_INIT_EXT_PART);
47 if (g_pluginFunc.initExtPartFunc == NULL) {
48 LOGE("[Plugin]: Get init func from dynamic plugin fail.");
49 break;
50 }
51 g_pluginFunc.getPluginListFunc = DevAuthDlsym(handle, FUNC_NAME_EXT_PLUGIN_LIST);
52 if (g_pluginFunc.getPluginListFunc == NULL) {
53 LOGE("[Plugin]: Get plug list func from dynamic plugin fail.");
54 break;
55 }
56 g_pluginFunc.destroyExtPartFunc = DevAuthDlsym(handle, FUNC_NAME_DESTROY_EXT_PART);
57 if (g_pluginFunc.destroyExtPartFunc == NULL) {
58 LOGE("[Plugin]: Get destroy func from dynamic plugin fail.");
59 break;
60 }
61 return &g_pluginFunc;
62 } while (0);
63 return NULL;
64 }
65
GetCredPluginFromLib(void * handle)66 static const CredPlugin *GetCredPluginFromLib(void *handle)
67 {
68 GetCredPluginFunc getCredPluginFunc = (GetCredPluginFunc)DevAuthDlsym(handle, CRED_PLUGIN_FUNC);
69 if (getCredPluginFunc == NULL) {
70 LOGE("[Plugin]: get func from dynamic plugin fail.");
71 return NULL;
72 }
73 return getCredPluginFunc();
74 }
75
GetAuthModulePluginFromLib(void * handle)76 static const AuthModuleBase *GetAuthModulePluginFromLib(void *handle)
77 {
78 GetAuthModulePluginFunc getAuthModulePluginFunc =
79 (GetAuthModulePluginFunc)DevAuthDlsym(handle, AUTH_MODULE_PLUGIN_FUNC);
80 if (getAuthModulePluginFunc == NULL) {
81 LOGE("[Plugin]: get func from dynamic plugin fail.");
82 return NULL;
83 }
84 return getAuthModulePluginFunc();
85 }
86
LoadDynamicPlugin(void * handle)87 static int32_t LoadDynamicPlugin(void *handle)
88 {
89 const CredPlugin *credPlugin = GetCredPluginFromLib(handle);
90 int32_t res = HC_SUCCESS;
91 if (credPlugin != NULL) {
92 res = AddCredPlugin(credPlugin);
93 if (res != HC_SUCCESS) {
94 LOGE("[Plugin]: init cred plugin fail. [Res]: %d", res);
95 return res;
96 }
97 }
98 const AuthModuleBase *authModulePlugin = GetAuthModulePluginFromLib(handle);
99 if (authModulePlugin != NULL) {
100 res = AddAuthModulePlugin(authModulePlugin);
101 if (res != HC_SUCCESS) {
102 LOGE("[Plugin]: init auth module plugin fail. [Res]: %d", res);
103 return res;
104 }
105 }
106 const ExtPartProxy *pluginFunc = GetPluginFuncFromLib(handle);
107 if (pluginFunc != NULL) {
108 res = AddExtPlugin(pluginFunc);
109 if (res != HC_SUCCESS) {
110 LOGE("[Plugin]: init ext plugin fail. [Res]: %d", res);
111 return res;
112 }
113 }
114 return HC_SUCCESS;
115 }
116
UnloadDynamicPlugin(void * handle)117 static void UnloadDynamicPlugin(void *handle)
118 {
119 const CredPlugin *credPlugin = GetCredPluginFromLib(handle);
120 const AuthModuleBase *authModulePlugin = GetAuthModulePluginFromLib(handle);
121 if (credPlugin != NULL) {
122 DelCredPlugin(credPlugin->pluginName);
123 }
124 if (authModulePlugin != NULL) {
125 DelAuthModulePlugin(authModulePlugin->moduleType);
126 }
127 }
128
LoadExtendPlugin(void)129 void LoadExtendPlugin(void)
130 {
131 if (g_handle != NULL) {
132 LOGE("[Plugin]: The plugin has been loaded.");
133 return;
134 }
135 g_handle = DevAuthDlopen(LIBDEVICE_AUTH_EXT);
136 if (g_handle == NULL) {
137 LOGI("[Plugin]: There are no plugin that need to be loaded.");
138 return;
139 }
140 LOGI("[Plugin]: Open lib32 dynamic plugin success.");
141 if (LoadDynamicPlugin(g_handle) != HC_SUCCESS) {
142 DevAuthDlclose(g_handle);
143 g_handle = NULL;
144 }
145 }
146
UnloadExtendPlugin(void)147 void UnloadExtendPlugin(void)
148 {
149 if (g_handle == NULL) {
150 LOGE("[Plugin]: The plugin has not been loaded.");
151 return;
152 }
153 DestroyExt(&g_pluginFunc);
154 UnloadDynamicPlugin(g_handle);
155 DevAuthDlclose(g_handle);
156 g_handle = NULL;
157 LOGI("[Plugin]: unload extend plugin success.");
158 }