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 #include "cred_manager.h"
17
18 #include "account_related_cred_plugin.h"
19 #include "device_auth_defines.h"
20 #include "hc_log.h"
21 #include "hc_vector.h"
22 #include "hitrace_adapter.h"
23
24 DECLARE_HC_VECTOR(CredPluginVec, CredPlugin *);
25 IMPLEMENT_HC_VECTOR(CredPluginVec, CredPlugin *, 2)
26
27 static CredPluginVec g_credPluginVec;
28
ProcCred(int32_t pluginName,int32_t osAccountId,int32_t cmdId,CJson * in,CJson * out)29 int32_t ProcCred(int32_t pluginName, int32_t osAccountId, int32_t cmdId, CJson *in, CJson *out)
30 {
31 uint32_t index;
32 CredPlugin **pluginPtr;
33 FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
34 if ((*pluginPtr)->pluginName == pluginName) {
35 return (*pluginPtr)->procCred(osAccountId, cmdId, in, out);
36 }
37 }
38 LOGE("[CredMgr]: There is no matched cred plugin. [Name]: %d", pluginName);
39 return HC_ERR_NOT_SUPPORT;
40 }
41
InitCredMgr(void)42 int32_t InitCredMgr(void)
43 {
44 g_credPluginVec = CREATE_HC_VECTOR(CredPluginVec);
45 CredPlugin *plugin = GetAccountRelatedCredPlugin();
46 if (plugin != NULL) {
47 int32_t res = plugin->init();
48 if (res == HC_SUCCESS) {
49 (void)g_credPluginVec.pushBackT(&g_credPluginVec, plugin);
50 } else {
51 LOGW("[CredMgr]: Init account related cred plugin fail. [Res]: %d", res);
52 }
53 }
54 LOGI("[CredMgr]: Init success!");
55 return HC_SUCCESS;
56 }
57
DestroyCredMgr(void)58 void DestroyCredMgr(void)
59 {
60 uint32_t index;
61 CredPlugin **pluginPtr;
62 FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
63 (*pluginPtr)->destroy();
64 }
65 DESTROY_HC_VECTOR(CredPluginVec, &g_credPluginVec);
66 }
67
AddCredPlugin(const CredPlugin * plugin)68 int32_t AddCredPlugin(const CredPlugin *plugin)
69 {
70 if (plugin == NULL || plugin->init == NULL ||
71 plugin->destroy == NULL || plugin->procCred == NULL) {
72 LOGE("The plugin is invalid.");
73 return HC_ERR_INVALID_PARAMS;
74 }
75 int32_t res = plugin->init();
76 if (res != HC_SUCCESS) {
77 LOGE("[CredMgr]: Init cred plugin fail. [Res]: %d", res);
78 return HC_ERR_INIT_FAILED;
79 }
80 bool isNeedReplace = false;
81 uint32_t index;
82 CredPlugin **pluginPtr;
83 FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
84 if ((*pluginPtr)->pluginName == plugin->pluginName) {
85 isNeedReplace = true;
86 break;
87 }
88 }
89 if (g_credPluginVec.pushBack(&g_credPluginVec, &plugin) == NULL) {
90 LOGE("[CredMgr]: Push cred plugin to vector fail.");
91 plugin->destroy();
92 return HC_ERR_ALLOC_MEMORY;
93 }
94 if (isNeedReplace) {
95 LOGI("[CredMgr]: Replace cred plugin. [Name]: %d", plugin->pluginName);
96 HC_VECTOR_POPELEMENT(&g_credPluginVec, pluginPtr, index);
97 } else {
98 LOGI("[CredMgr]: Add new cred plugin. [Name]: %d", plugin->pluginName);
99 }
100 return HC_SUCCESS;
101 }
102
DelCredPlugin(int32_t pluginName)103 void DelCredPlugin(int32_t pluginName)
104 {
105 uint32_t index;
106 CredPlugin **pluginPtr;
107 FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
108 if ((*pluginPtr)->pluginName == pluginName) {
109 LOGI("[CredMgr]: Delete cred plugin success. [Name]: %d", pluginName);
110 (*pluginPtr)->destroy();
111 HC_VECTOR_POPELEMENT(&g_credPluginVec, pluginPtr, index);
112 break;
113 }
114 }
115 }
116